r8219: Merge the new open code from HEAD to 3.0. Haven't yet run the torture
[samba.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 /* 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", strerror(errno)));
92                 return;
93         }
94
95         data.effective |= (1<<capability);
96
97         if (capset(&header, &data) == -1) {
98                 DEBUG(3,("Unable to set %d capability (%s)\n", 
99                          capability, strerror(errno)));
100         }
101 }
102
103 /****************************************************************************
104  Call SETLEASE. If we get EACCES then we try setting up the right capability and
105  try again
106 ****************************************************************************/
107
108 static int linux_setlease(int fd, int leasetype)
109 {
110         int ret;
111
112         if (fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
113                 DEBUG(3,("Failed to set signal handler for kernel lease\n"));
114                 return -1;
115         }
116
117         ret = fcntl(fd, F_SETLEASE, leasetype);
118         if (ret == -1 && errno == EACCES) {
119                 set_capability(CAP_LEASE);
120                 ret = fcntl(fd, F_SETLEASE, leasetype);
121         }
122
123         return ret;
124 }
125
126 /****************************************************************************
127  * Deal with the Linux kernel <--> smbd
128  * oplock break protocol.
129 ****************************************************************************/
130
131 static BOOL linux_oplock_receive_message(fd_set *fds, char *buffer, int buffer_len)
132 {
133         int fd;
134         struct files_struct *fsp;
135
136         BlockSignals(True, RT_SIGNAL_LEASE);
137         fd = fd_pending_array[0];
138         fsp = file_find_fd(fd);
139         fd_pending_array[0] = (SIG_ATOMIC_T)-1;
140         if (signals_received > 1)
141                 memmove(CONST_DISCARD(void *, &fd_pending_array[0]),
142                         CONST_DISCARD(void *, &fd_pending_array[1]),
143                         sizeof(SIG_ATOMIC_T)*(signals_received-1));
144         signals_received--;
145         /* now we can receive more signals */
146         BlockSignals(False, RT_SIGNAL_LEASE);
147
148         if (fsp == NULL) {
149                 DEBUG(0,("Invalid file descriptor %d in kernel oplock break!\n", (int)fd));
150                 return False;
151         }
152
153         DEBUG(3,("linux_oplock_receive_message: kernel oplock break request received for \
154 dev = %x, inode = %.0f fd = %d, fileid = %lu \n", (unsigned int)fsp->dev, (double)fsp->inode,
155                         fd, fsp->file_id));
156      
157         /*
158          * Create a kernel oplock break message.
159          */
160      
161         /* Setup the message header */
162         SIVAL(buffer,OPBRK_CMD_LEN_OFFSET,KERNEL_OPLOCK_BREAK_MSG_LEN);
163         SSVAL(buffer,OPBRK_CMD_PORT_OFFSET,0);
164      
165         buffer += OPBRK_CMD_HEADER_LEN;
166      
167         SSVAL(buffer,OPBRK_MESSAGE_CMD_OFFSET,KERNEL_OPLOCK_BREAK_CMD);
168      
169         memcpy(buffer + KERNEL_OPLOCK_BREAK_DEV_OFFSET, (char *)&fsp->dev, sizeof(fsp->dev));
170         memcpy(buffer + KERNEL_OPLOCK_BREAK_INODE_OFFSET, (char *)&fsp->inode, sizeof(fsp->inode));     
171         memcpy(buffer + KERNEL_OPLOCK_BREAK_FILEID_OFFSET, (char *)&fsp->file_id, sizeof(fsp->file_id));        
172
173         return True;
174 }
175
176 /****************************************************************************
177  Attempt to set an kernel oplock on a file.
178 ****************************************************************************/
179
180 static BOOL linux_set_kernel_oplock(files_struct *fsp, int oplock_type)
181 {
182         if (linux_setlease(fsp->fh->fd, F_WRLCK) == -1) {
183                 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, fd = %d, dev = %x, \
184 inode = %.0f. (%s)\n",
185                          fsp->fsp_name, fsp->fh->fd, 
186                          (unsigned int)fsp->dev, (double)fsp->inode, strerror(errno)));
187                 return False;
188         }
189         
190         DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, dev = %x, inode = %.0f, file_id = %lu\n",
191                   fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id));
192
193         return True;
194 }
195
196 /****************************************************************************
197  Release a kernel oplock on a file.
198 ****************************************************************************/
199
200 static void linux_release_kernel_oplock(files_struct *fsp)
201 {
202         if (DEBUGLVL(10)) {
203                 /*
204                  * Check and print out the current kernel
205                  * oplock state of this file.
206                  */
207                 int state = fcntl(fsp->fh->fd, F_GETLEASE, 0);
208                 dbgtext("linux_release_kernel_oplock: file %s, dev = %x, inode = %.0f file_id = %lu has kernel \
209 oplock state of %x.\n", fsp->fsp_name, (unsigned int)fsp->dev,
210                         (double)fsp->inode, fsp->file_id, state );
211         }
212
213         /*
214          * Remove the kernel oplock on this file.
215          */
216         if (linux_setlease(fsp->fh->fd, F_UNLCK) == -1) {
217                 if (DEBUGLVL(0)) {
218                         dbgtext("linux_release_kernel_oplock: Error when removing kernel oplock on file " );
219                         dbgtext("%s, dev = %x, inode = %.0f, file_id = %lu. Error was %s\n",
220                                 fsp->fsp_name, (unsigned int)fsp->dev, 
221                                 (double)fsp->inode, fsp->file_id, strerror(errno) );
222                 }
223         }
224 }
225
226 /****************************************************************************
227  Parse a kernel oplock message.
228 ****************************************************************************/
229
230 static BOOL linux_kernel_oplock_parse(char *msg_start, int msg_len, SMB_INO_T *inode,
231                 SMB_DEV_T *dev, unsigned long *file_id)
232 {
233         /* Ensure that the msg length is correct. */
234         if (msg_len != KERNEL_OPLOCK_BREAK_MSG_LEN) {
235                 DEBUG(0,("incorrect length for KERNEL_OPLOCK_BREAK_CMD (was %d, should be %lu).\n", 
236                          msg_len, (unsigned long)KERNEL_OPLOCK_BREAK_MSG_LEN));
237                 return False;
238         }
239
240         memcpy((char *)inode, msg_start+KERNEL_OPLOCK_BREAK_INODE_OFFSET, sizeof(*inode));
241         memcpy((char *)dev, msg_start+KERNEL_OPLOCK_BREAK_DEV_OFFSET, sizeof(*dev));
242         memcpy((char *)file_id, msg_start+KERNEL_OPLOCK_BREAK_FILEID_OFFSET, sizeof(*file_id));
243
244         DEBUG(3,("kernel oplock break request for file dev = %x, inode = %.0f, file_id = %lu\n", 
245                 (unsigned int)*dev, (double)*inode, *file_id));
246
247         return True;
248 }
249
250 /****************************************************************************
251  See if a oplock message is waiting.
252 ****************************************************************************/
253
254 static BOOL linux_oplock_msg_waiting(fd_set *fds)
255 {
256         return signals_received != 0;
257 }
258
259 /****************************************************************************
260  See if the kernel supports oplocks.
261 ****************************************************************************/
262
263 static BOOL linux_oplocks_available(void)
264 {
265         int fd, ret;
266         fd = open("/dev/null", O_RDONLY);
267         if (fd == -1)
268                 return False; /* uggh! */
269         ret = fcntl(fd, F_GETLEASE, 0);
270         close(fd);
271         return ret == F_UNLCK;
272 }
273
274 /****************************************************************************
275  Setup kernel oplocks.
276 ****************************************************************************/
277
278 struct kernel_oplocks *linux_init_kernel_oplocks(void) 
279 {
280         static struct kernel_oplocks koplocks;
281         struct sigaction act;
282
283         if (!linux_oplocks_available()) {
284                 DEBUG(3,("Linux kernel oplocks not available\n"));
285                 return NULL;
286         }
287
288         ZERO_STRUCT(act);
289
290         act.sa_handler = NULL;
291         act.sa_sigaction = signal_handler;
292         act.sa_flags = SA_SIGINFO;
293         sigemptyset( &act.sa_mask );
294         if (sigaction(RT_SIGNAL_LEASE, &act, NULL) != 0) {
295                 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler\n"));
296                 return NULL;
297         }
298
299         koplocks.receive_message = linux_oplock_receive_message;
300         koplocks.set_oplock = linux_set_kernel_oplock;
301         koplocks.release_oplock = linux_release_kernel_oplock;
302         koplocks.parse_message = linux_kernel_oplock_parse;
303         koplocks.msg_waiting = linux_oplock_msg_waiting;
304         koplocks.notification_fd = -1;
305
306         /* the signal can start off blocked due to a bug in bash */
307         BlockSignals(False, RT_SIGNAL_LEASE);
308
309         DEBUG(3,("Linux kernel oplocks enabled\n"));
310
311         return &koplocks;
312 }
313 #else
314  void oplock_linux_dummy(void);
315
316  void oplock_linux_dummy(void) {}
317 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */