This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[sfrench/samba-autobuild/.git] / source3 / 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #if HAVE_KERNEL_OPLOCKS_LINUX
24
25 static VOLATILE sig_atomic_t signals_received;
26 static VOLATILE sig_atomic_t signals_processed;
27 static VOLATILE sig_atomic_t fd_pending; /* the fd of the current pending signal */
28
29 #ifndef F_SETLEASE
30 #define F_SETLEASE      1024
31 #endif
32
33 #ifndef F_GETLEASE
34 #define F_GETLEASE      1025
35 #endif
36
37 #ifndef CAP_LEASE
38 #define CAP_LEASE 28
39 #endif
40
41 #ifndef RT_SIGNAL_LEASE
42 #define RT_SIGNAL_LEASE 33
43 #endif
44
45 #ifndef F_SETSIG
46 #define F_SETSIG 10
47 #endif
48
49 /****************************************************************************
50  Handle a LEASE signal, incrementing the signals_received and blocking the signal.
51 ****************************************************************************/
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
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  Call SETLEASE. If we get EACCES then we try setting up the right capability and
99  try again
100 ****************************************************************************/
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  * Deal with the Linux kernel <--> smbd
122  * oplock break protocol.
123 ****************************************************************************/
124
125 static BOOL linux_oplock_receive_message(fd_set *fds, char *buffer, int buffer_len)
126 {
127         BOOL ret = True;
128         struct files_struct *fsp;
129
130         if (signals_received == signals_processed)
131                 return False;
132
133         if ((fsp = file_find_fd(fd_pending)) == NULL) {
134                 DEBUG(0,("Invalid file descriptor %d in kernel oplock break!\n", (int)fd_pending));
135                 ret = False;
136                 goto out;
137         }
138
139         DEBUG(3,("receive_local_message: kernel oplock break request received for \
140 dev = %x, inode = %.0f\n", (unsigned int)fsp->dev, (double)fsp->inode ));
141      
142         /*
143          * Create a kernel oplock break message.
144          */
145      
146         /* Setup the message header */
147         SIVAL(buffer,OPBRK_CMD_LEN_OFFSET,KERNEL_OPLOCK_BREAK_MSG_LEN);
148         SSVAL(buffer,OPBRK_CMD_PORT_OFFSET,0);
149      
150         buffer += OPBRK_CMD_HEADER_LEN;
151      
152         SSVAL(buffer,OPBRK_MESSAGE_CMD_OFFSET,KERNEL_OPLOCK_BREAK_CMD);
153      
154         memcpy(buffer + KERNEL_OPLOCK_BREAK_DEV_OFFSET, (char *)&fsp->dev, sizeof(fsp->dev));
155         memcpy(buffer + KERNEL_OPLOCK_BREAK_INODE_OFFSET, (char *)&fsp->inode, sizeof(fsp->inode));     
156         memcpy(buffer + KERNEL_OPLOCK_BREAK_FILEID_OFFSET, (char *)&fsp->file_id, sizeof(fsp->file_id));        
157
158  out:
159         /* now we can receive more signals */
160         fd_pending = (sig_atomic_t)-1;
161         signals_processed++;
162         BlockSignals(False, RT_SIGNAL_LEASE);
163      
164         return ret;
165 }
166
167 /****************************************************************************
168  Attempt to set an kernel oplock on a file.
169 ****************************************************************************/
170
171 static BOOL linux_set_kernel_oplock(files_struct *fsp, int oplock_type)
172 {
173         if (linux_setlease(fsp->fd, F_WRLCK) == -1) {
174                 DEBUG(3,("set_file_oplock: Refused oplock on file %s, fd = %d, dev = %x, \
175 inode = %.0f. (%s)\n",
176                          fsp->fsp_name, fsp->fd, 
177                          (unsigned int)fsp->dev, (double)fsp->inode, strerror(errno)));
178                 return False;
179         }
180         
181         DEBUG(3,("set_file_oplock: got kernel oplock on file %s, dev = %x, inode = %.0f, file_id = %lu\n",
182                   fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id));
183
184         return True;
185 }
186
187 /****************************************************************************
188  Release a kernel oplock on a file.
189 ****************************************************************************/
190
191 static void linux_release_kernel_oplock(files_struct *fsp)
192 {
193         if (DEBUGLVL(10)) {
194                 /*
195                  * Check and print out the current kernel
196                  * oplock state of this file.
197                  */
198                 int state = fcntl(fsp->fd, F_GETLEASE, 0);
199                 dbgtext("release_kernel_oplock: file %s, dev = %x, inode = %.0f file_id = %lu has kernel \
200 oplock state of %x.\n", fsp->fsp_name, (unsigned int)fsp->dev,
201                         (double)fsp->inode, fsp->file_id, state );
202         }
203
204         /*
205          * Remove the kernel oplock on this file.
206          */
207         if (linux_setlease(fsp->fd, F_UNLCK) == -1) {
208                 if (DEBUGLVL(0)) {
209                         dbgtext("release_kernel_oplock: Error when removing kernel oplock on file " );
210                         dbgtext("%s, dev = %x, inode = %.0f, file_id = %lu. Error was %s\n",
211                                 fsp->fsp_name, (unsigned int)fsp->dev, 
212                                 (double)fsp->inode, fsp->file_id, strerror(errno) );
213                 }
214         }
215 }
216
217 /****************************************************************************
218  Parse a kernel oplock message.
219 ****************************************************************************/
220
221 static BOOL linux_kernel_oplock_parse(char *msg_start, int msg_len, SMB_INO_T *inode,
222                 SMB_DEV_T *dev, unsigned long *file_id)
223 {
224         /* Ensure that the msg length is correct. */
225         if (msg_len != KERNEL_OPLOCK_BREAK_MSG_LEN) {
226                 DEBUG(0,("incorrect length for KERNEL_OPLOCK_BREAK_CMD (was %d, should be %d).\n", 
227                          msg_len, KERNEL_OPLOCK_BREAK_MSG_LEN));
228                 return False;
229         }
230
231         memcpy((char *)inode, msg_start+KERNEL_OPLOCK_BREAK_INODE_OFFSET, sizeof(*inode));
232         memcpy((char *)dev, msg_start+KERNEL_OPLOCK_BREAK_DEV_OFFSET, sizeof(*dev));
233         memcpy((char *)file_id, msg_start+KERNEL_OPLOCK_BREAK_FILEID_OFFSET, sizeof(*file_id));
234
235         DEBUG(3,("kernel oplock break request for file dev = %x, inode = %.0f, file_id = %lu\n", 
236                 (unsigned int)*dev, (double)*inode, *file_id));
237
238         return True;
239 }
240
241 /****************************************************************************
242  See if a oplock message is waiting.
243 ****************************************************************************/
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
254 static BOOL linux_oplocks_available(void)
255 {
256         int fd, ret;
257         fd = open("/dev/null", O_RDONLY);
258         if (fd == -1)
259                 return False; /* uggh! */
260         ret = fcntl(fd, F_GETLEASE, 0);
261         close(fd);
262         return ret == F_UNLCK;
263 }
264
265 /****************************************************************************
266  Setup kernel oplocks.
267 ****************************************************************************/
268
269 struct kernel_oplocks *linux_init_kernel_oplocks(void) 
270 {
271         static struct kernel_oplocks koplocks;
272         struct sigaction act;
273
274         if (!linux_oplocks_available()) {
275                 DEBUG(3,("Linux kernel oplocks not available\n"));
276                 return NULL;
277         }
278
279         act.sa_handler = NULL;
280         act.sa_sigaction = signal_handler;
281         act.sa_flags = SA_SIGINFO;
282         if (sigaction(RT_SIGNAL_LEASE, &act, NULL) != 0) {
283                 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler\n"));
284                 return NULL;
285         }
286
287         koplocks.receive_message = linux_oplock_receive_message;
288         koplocks.set_oplock = linux_set_kernel_oplock;
289         koplocks.release_oplock = linux_release_kernel_oplock;
290         koplocks.parse_message = linux_kernel_oplock_parse;
291         koplocks.msg_waiting = linux_oplock_msg_waiting;
292         koplocks.notification_fd = -1;
293
294         DEBUG(3,("Linux kernel oplocks enabled\n"));
295
296         return &koplocks;
297 }
298 #else
299  void oplock_linux_dummy(void) {}
300 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */