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