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