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