8087167ff44a5d568004cc696f4973486541b390
[metze/samba/wip.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #define DBGC_CLASS DBGC_LOCKING
21 #include "includes.h"
22 #include "smbd/globals.h"
23
24 #if HAVE_KERNEL_OPLOCKS_LINUX
25
26 #ifndef F_SETLEASE
27 #define F_SETLEASE      1024
28 #endif
29
30 #ifndef F_GETLEASE
31 #define F_GETLEASE      1025
32 #endif
33
34 #ifndef CAP_LEASE
35 #define CAP_LEASE 28
36 #endif
37
38 #ifndef RT_SIGNAL_LEASE
39 #define RT_SIGNAL_LEASE (SIGRTMIN+1)
40 #endif
41
42 #ifndef F_SETSIG
43 #define F_SETSIG 10
44 #endif
45
46 /****************************************************************************
47  Handle a LEASE signal, incrementing the signals_received and blocking the signal.
48 ****************************************************************************/
49
50 static void signal_handler(int sig, siginfo_t *info, void *unused)
51 {
52         if (oplock_signals_received < FD_PENDING_SIZE - 1) {
53                 fd_pending_array[oplock_signals_received] = (SIG_ATOMIC_T)info->si_fd;
54                 oplock_signals_received++;
55         } /* Else signal is lost. */
56         sys_select_signal(RT_SIGNAL_LEASE);
57 }
58
59 /*
60  * public function to get linux lease capability. Needed by some VFS modules (eg. gpfs.c)
61  */
62 void linux_set_lease_capability(void)
63 {
64         set_effective_capability(LEASE_CAPABILITY);
65 }
66
67 /* 
68  * Call to set the kernel lease signal handler
69  */
70 int linux_set_lease_sighandler(int fd)
71 {
72         if (fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
73                 DEBUG(3,("Failed to set signal handler for kernel lease\n"));
74                 return -1;
75         }
76
77         return 0;
78 }
79
80 /****************************************************************************
81  Call SETLEASE. If we get EACCES then we try setting up the right capability and
82  try again.
83  Use the SMB_VFS_LINUX_SETLEASE instead of this call directly.
84 ****************************************************************************/
85
86 int linux_setlease(int fd, int leasetype)
87 {
88         int ret;
89
90         ret = fcntl(fd, F_SETLEASE, leasetype);
91         if (ret == -1 && errno == EACCES) {
92                 set_effective_capability(LEASE_CAPABILITY);
93                 ret = fcntl(fd, F_SETLEASE, leasetype);
94         }
95
96         return ret;
97 }
98
99 /****************************************************************************
100  * Deal with the Linux kernel <--> smbd
101  * oplock break protocol.
102 ****************************************************************************/
103
104 static files_struct *linux_oplock_receive_message(struct kernel_oplocks *ctx)
105 {
106         int fd;
107         files_struct *fsp;
108
109         BlockSignals(True, RT_SIGNAL_LEASE);
110         fd = fd_pending_array[0];
111         fsp = file_find_fd(fd);
112         fd_pending_array[0] = (SIG_ATOMIC_T)-1;
113         if (oplock_signals_received > 1)
114                 memmove(CONST_DISCARD(void *, &fd_pending_array[0]),
115                         CONST_DISCARD(void *, &fd_pending_array[1]),
116                         sizeof(SIG_ATOMIC_T)*(oplock_signals_received-1));
117         oplock_signals_received--;
118         /* now we can receive more signals */
119         BlockSignals(False, RT_SIGNAL_LEASE);
120
121         return fsp;
122 }
123
124 /****************************************************************************
125  Attempt to set an kernel oplock on a file.
126 ****************************************************************************/
127
128 static bool linux_set_kernel_oplock(struct kernel_oplocks *ctx,
129                                     files_struct *fsp, int oplock_type)
130 {
131         if ( SMB_VFS_LINUX_SETLEASE(fsp, F_WRLCK) == -1) {
132                 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
133                          "fd = %d, file_id = %s. (%s)\n",
134                          fsp->fsp_name, fsp->fh->fd, 
135                          file_id_string_tos(&fsp->file_id),
136                          strerror(errno)));
137                 return False;
138         }
139         
140         DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
141                  "file_id = %s gen_id = %lu\n",
142                  fsp->fsp_name, file_id_string_tos(&fsp->file_id),
143                  fsp->fh->gen_id));
144
145         return True;
146 }
147
148 /****************************************************************************
149  Release a kernel oplock on a file.
150 ****************************************************************************/
151
152 static void linux_release_kernel_oplock(struct kernel_oplocks *ctx,
153                                         files_struct *fsp)
154 {
155         if (DEBUGLVL(10)) {
156                 /*
157                  * Check and print out the current kernel
158                  * oplock state of this file.
159                  */
160                 int state = fcntl(fsp->fh->fd, F_GETLEASE, 0);
161                 dbgtext("linux_release_kernel_oplock: file %s, file_id = %s "
162                         "gen_id = %lu has kernel oplock state "
163                         "of %x.\n", fsp->fsp_name, file_id_string_tos(&fsp->file_id),
164                         fsp->fh->gen_id, state );
165         }
166
167         /*
168          * Remove the kernel oplock on this file.
169          */
170         if ( SMB_VFS_LINUX_SETLEASE(fsp, F_UNLCK) == -1) {
171                 if (DEBUGLVL(0)) {
172                         dbgtext("linux_release_kernel_oplock: Error when "
173                                 "removing kernel oplock on file " );
174                         dbgtext("%s, file_id = %s, gen_id = %lu. "
175                                 "Error was %s\n", fsp->fsp_name,
176                                 file_id_string_tos(&fsp->file_id),
177                                 fsp->fh->gen_id, strerror(errno) );
178                 }
179         }
180 }
181
182 /****************************************************************************
183  See if a oplock message is waiting.
184 ****************************************************************************/
185
186 static bool linux_oplock_msg_waiting(struct kernel_oplocks *ctx)
187 {
188         return oplock_signals_received != 0;
189 }
190
191 /****************************************************************************
192  See if the kernel supports oplocks.
193 ****************************************************************************/
194
195 static bool linux_oplocks_available(void)
196 {
197         int fd, ret;
198         fd = open("/dev/null", O_RDONLY);
199         if (fd == -1)
200                 return False; /* uggh! */
201         ret = fcntl(fd, F_GETLEASE, 0);
202         close(fd);
203         return ret == F_UNLCK;
204 }
205
206 /****************************************************************************
207  Setup kernel oplocks.
208 ****************************************************************************/
209
210 static const struct kernel_oplocks_ops linux_koplocks = {
211         .receive_message        = linux_oplock_receive_message,
212         .set_oplock             = linux_set_kernel_oplock,
213         .release_oplock         = linux_release_kernel_oplock,
214         .msg_waiting            = linux_oplock_msg_waiting
215 };
216
217 struct kernel_oplocks *linux_init_kernel_oplocks(TALLOC_CTX *mem_ctx)
218 {
219         struct sigaction act;
220         struct kernel_oplocks *ctx;
221
222         if (!linux_oplocks_available()) {
223                 DEBUG(3,("Linux kernel oplocks not available\n"));
224                 return NULL;
225         }
226
227         ctx = talloc_zero(mem_ctx, struct kernel_oplocks);
228         if (!ctx) {
229                 DEBUG(0,("Linux Kernel oplocks talloc_Zero failed\n"));
230                 return NULL;
231         }
232
233         ctx->ops = &linux_koplocks;
234
235         ZERO_STRUCT(act);
236
237         act.sa_handler = NULL;
238         act.sa_sigaction = signal_handler;
239         act.sa_flags = SA_SIGINFO;
240         sigemptyset( &act.sa_mask );
241         if (sigaction(RT_SIGNAL_LEASE, &act, NULL) != 0) {
242                 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler\n"));
243                 return NULL;
244         }
245
246         /* the signal can start off blocked due to a bug in bash */
247         BlockSignals(False, RT_SIGNAL_LEASE);
248
249         DEBUG(3,("Linux kernel oplocks enabled\n"));
250
251         return ctx;
252 }
253 #else
254  void oplock_linux_dummy(void);
255
256  void oplock_linux_dummy(void) {}
257 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */