RIP BOOL. Convert BOOL -> bool. I found a few interesting
[tprouty/samba.git] / source / smbd / oplock_linux.c
1 /* 
2    Unix SMB/CIFS implementation.
3    kernel oplock processing for Linux
4    Copyright (C) Andrew Tridgell 2000
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #define DBGC_CLASS DBGC_LOCKING
21 #include "includes.h"
22
23 #if HAVE_KERNEL_OPLOCKS_LINUX
24
25 /* these can be removed when they are in glibc headers */
26 struct  cap_user_header {
27         uint32 version;
28         int pid;
29 } header;
30 struct cap_user_data {
31         uint32 effective;
32         uint32 permitted;
33         uint32 inheritable;
34 } data;
35
36 extern int capget(struct cap_user_header * hdrp,
37                   struct cap_user_data * datap);
38 extern int capset(struct cap_user_header * hdrp,
39                   const struct cap_user_data * datap);
40
41 static SIG_ATOMIC_T signals_received;
42 #define FD_PENDING_SIZE 100
43 static SIG_ATOMIC_T fd_pending_array[FD_PENDING_SIZE];
44
45 #ifndef F_SETLEASE
46 #define F_SETLEASE      1024
47 #endif
48
49 #ifndef F_GETLEASE
50 #define F_GETLEASE      1025
51 #endif
52
53 #ifndef CAP_LEASE
54 #define CAP_LEASE 28
55 #endif
56
57 #ifndef RT_SIGNAL_LEASE
58 #define RT_SIGNAL_LEASE (SIGRTMIN+1)
59 #endif
60
61 #ifndef F_SETSIG
62 #define F_SETSIG 10
63 #endif
64
65 /****************************************************************************
66  Handle a LEASE signal, incrementing the signals_received and blocking the signal.
67 ****************************************************************************/
68
69 static void signal_handler(int sig, siginfo_t *info, void *unused)
70 {
71         if (signals_received < FD_PENDING_SIZE - 1) {
72                 fd_pending_array[signals_received] = (SIG_ATOMIC_T)info->si_fd;
73                 signals_received++;
74         } /* Else signal is lost. */
75         sys_select_signal(RT_SIGNAL_LEASE);
76 }
77
78 /****************************************************************************
79  Try to gain a linux capability.
80 ****************************************************************************/
81
82 static void set_capability(unsigned capability)
83 {
84 #ifndef _LINUX_CAPABILITY_VERSION
85 #define _LINUX_CAPABILITY_VERSION 0x19980330
86 #endif
87         header.version = _LINUX_CAPABILITY_VERSION;
88         header.pid = 0;
89
90         if (capget(&header, &data) == -1) {
91                 DEBUG(3,("Unable to get kernel capabilities (%s)\n",
92                          strerror(errno)));
93                 return;
94         }
95
96         data.effective |= (1<<capability);
97
98         if (capset(&header, &data) == -1) {
99                 DEBUG(3,("Unable to set %d capability (%s)\n", 
100                          capability, strerror(errno)));
101         }
102 }
103
104 /*
105  Call to set the kernel lease signal handler
106 */
107 int linux_set_lease_sighandler(int fd)
108 {
109         if (fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
110                 DEBUG(3,("Failed to set signal handler for kernel lease\n"));
111                 return -1;
112         }
113
114         return 0;
115 }
116
117 /****************************************************************************
118  Call SETLEASE. If we get EACCES then we try setting up the right capability and
119  try again.
120  Use the SMB_VFS_LINUX_SETLEASE instead of this call directly.
121 ****************************************************************************/
122
123 int linux_setlease(int fd, int leasetype)
124 {
125         int ret;
126
127         ret = fcntl(fd, F_SETLEASE, leasetype);
128         if (ret == -1 && errno == EACCES) {
129                 set_capability(CAP_LEASE);
130                 ret = fcntl(fd, F_SETLEASE, leasetype);
131         }
132
133         return ret;
134 }
135
136 /****************************************************************************
137  * Deal with the Linux kernel <--> smbd
138  * oplock break protocol.
139 ****************************************************************************/
140
141 static files_struct *linux_oplock_receive_message(fd_set *fds)
142 {
143         int fd;
144         files_struct *fsp;
145
146         BlockSignals(True, RT_SIGNAL_LEASE);
147         fd = fd_pending_array[0];
148         fsp = file_find_fd(fd);
149         fd_pending_array[0] = (SIG_ATOMIC_T)-1;
150         if (signals_received > 1)
151                 memmove(CONST_DISCARD(void *, &fd_pending_array[0]),
152                         CONST_DISCARD(void *, &fd_pending_array[1]),
153                         sizeof(SIG_ATOMIC_T)*(signals_received-1));
154         signals_received--;
155         /* now we can receive more signals */
156         BlockSignals(False, RT_SIGNAL_LEASE);
157
158         return fsp;
159 }
160
161 /****************************************************************************
162  Attempt to set an kernel oplock on a file.
163 ****************************************************************************/
164
165 static bool linux_set_kernel_oplock(files_struct *fsp, int oplock_type)
166 {
167         if ( SMB_VFS_LINUX_SETLEASE(fsp,fsp->fh->fd, F_WRLCK) == -1) {
168                 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
169                          "fd = %d, file_id = %s. (%s)\n",
170                          fsp->fsp_name, fsp->fh->fd, 
171                          file_id_string_tos(&fsp->file_id),
172                          strerror(errno)));
173                 return False;
174         }
175         
176         DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
177                  "file_id = %s gen_id = %lu\n",
178                  fsp->fsp_name, file_id_string_tos(&fsp->file_id),
179                  fsp->fh->gen_id));
180
181         return True;
182 }
183
184 /****************************************************************************
185  Release a kernel oplock on a file.
186 ****************************************************************************/
187
188 static void linux_release_kernel_oplock(files_struct *fsp)
189 {
190         if (DEBUGLVL(10)) {
191                 /*
192                  * Check and print out the current kernel
193                  * oplock state of this file.
194                  */
195                 int state = fcntl(fsp->fh->fd, F_GETLEASE, 0);
196                 dbgtext("linux_release_kernel_oplock: file %s, file_id = %s "
197                         "gen_id = %lu has kernel oplock state "
198                         "of %x.\n", fsp->fsp_name, file_id_string_tos(&fsp->file_id),
199                         fsp->fh->gen_id, state );
200         }
201
202         /*
203          * Remove the kernel oplock on this file.
204          */
205         if ( SMB_VFS_LINUX_SETLEASE(fsp,fsp->fh->fd, F_UNLCK) == -1) {
206                 if (DEBUGLVL(0)) {
207                         dbgtext("linux_release_kernel_oplock: Error when "
208                                 "removing kernel oplock on file " );
209                         dbgtext("%s, file_id = %s, gen_id = %lu. "
210                                 "Error was %s\n", fsp->fsp_name,
211                                 file_id_string_tos(&fsp->file_id),
212                                 fsp->fh->gen_id, strerror(errno) );
213                 }
214         }
215 }
216
217 /****************************************************************************
218  See if a oplock message is waiting.
219 ****************************************************************************/
220
221 static bool linux_oplock_msg_waiting(fd_set *fds)
222 {
223         return signals_received != 0;
224 }
225
226 /****************************************************************************
227  See if the kernel supports oplocks.
228 ****************************************************************************/
229
230 static bool linux_oplocks_available(void)
231 {
232         int fd, ret;
233         fd = open("/dev/null", O_RDONLY);
234         if (fd == -1)
235                 return False; /* uggh! */
236         ret = fcntl(fd, F_GETLEASE, 0);
237         close(fd);
238         return ret == F_UNLCK;
239 }
240
241 /****************************************************************************
242  Setup kernel oplocks.
243 ****************************************************************************/
244
245 struct kernel_oplocks *linux_init_kernel_oplocks(void) 
246 {
247         static struct kernel_oplocks koplocks;
248         struct sigaction act;
249
250         if (!linux_oplocks_available()) {
251                 DEBUG(3,("Linux kernel oplocks not available\n"));
252                 return NULL;
253         }
254
255         ZERO_STRUCT(act);
256
257         act.sa_handler = NULL;
258         act.sa_sigaction = signal_handler;
259         act.sa_flags = SA_SIGINFO;
260         sigemptyset( &act.sa_mask );
261         if (sigaction(RT_SIGNAL_LEASE, &act, NULL) != 0) {
262                 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler\n"));
263                 return NULL;
264         }
265
266         koplocks.receive_message = linux_oplock_receive_message;
267         koplocks.set_oplock = linux_set_kernel_oplock;
268         koplocks.release_oplock = linux_release_kernel_oplock;
269         koplocks.msg_waiting = linux_oplock_msg_waiting;
270         koplocks.notification_fd = -1;
271
272         /* the signal can start off blocked due to a bug in bash */
273         BlockSignals(False, RT_SIGNAL_LEASE);
274
275         DEBUG(3,("Linux kernel oplocks enabled\n"));
276
277         return &koplocks;
278 }
279 #else
280  void oplock_linux_dummy(void);
281
282  void oplock_linux_dummy(void) {}
283 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */