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