s3:smbd/oplock: pass smbd_server_connection to linux_init_kernel_oplocks()
[kai/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 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 "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25
26 #if HAVE_KERNEL_OPLOCKS_LINUX
27
28 #ifndef F_SETLEASE
29 #define F_SETLEASE      1024
30 #endif
31
32 #ifndef F_GETLEASE
33 #define F_GETLEASE      1025
34 #endif
35
36 #ifndef CAP_LEASE
37 #define CAP_LEASE 28
38 #endif
39
40 #ifndef RT_SIGNAL_LEASE
41 #define RT_SIGNAL_LEASE (SIGRTMIN+1)
42 #endif
43
44 #ifndef F_SETSIG
45 #define F_SETSIG 10
46 #endif
47
48 /*
49  * public function to get linux lease capability. Needed by some VFS modules (eg. gpfs.c)
50  */
51 void linux_set_lease_capability(void)
52 {
53         set_effective_capability(LEASE_CAPABILITY);
54 }
55
56 /* 
57  * Call to set the kernel lease signal handler
58  */
59 int linux_set_lease_sighandler(int fd)
60 {
61         if (fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
62                 DEBUG(3,("Failed to set signal handler for kernel lease\n"));
63                 return -1;
64         }
65
66         return 0;
67 }
68
69 /****************************************************************************
70  Call SETLEASE. If we get EACCES then we try setting up the right capability and
71  try again.
72  Use the SMB_VFS_LINUX_SETLEASE instead of this call directly.
73 ****************************************************************************/
74
75 int linux_setlease(int fd, int leasetype)
76 {
77         int ret;
78
79         ret = fcntl(fd, F_SETLEASE, leasetype);
80         if (ret == -1 && errno == EACCES) {
81                 set_effective_capability(LEASE_CAPABILITY);
82                 ret = fcntl(fd, F_SETLEASE, leasetype);
83         }
84
85         return ret;
86 }
87
88 /****************************************************************************
89  * Deal with the Linux kernel <--> smbd
90  * oplock break protocol.
91 ****************************************************************************/
92
93 static void linux_oplock_signal_handler(struct tevent_context *ev_ctx,
94                                         struct tevent_signal *se,
95                                         int signum, int count,
96                                         void *_info, void *private_data)
97 {
98         struct kernel_oplocks *ctx =
99                 talloc_get_type_abort(private_data,
100                 struct kernel_oplocks);
101         struct smbd_server_connection *sconn =
102                 talloc_get_type_abort(ctx->private_data,
103                 struct smbd_server_connection);
104         siginfo_t *info = (siginfo_t *)_info;
105         int fd = info->si_fd;
106         files_struct *fsp;
107
108         fsp = file_find_fd(sconn, fd);
109         if (fsp == NULL) {
110                 DEBUG(0,("linux_oplock_signal_handler: failed to find fsp for file fd=%d (file was closed ?)\n", fd ));
111                 return;
112         }
113         break_kernel_oplock(sconn->msg_ctx, fsp);
114 }
115
116 /****************************************************************************
117  Attempt to set an kernel oplock on a file.
118 ****************************************************************************/
119
120 static bool linux_set_kernel_oplock(struct kernel_oplocks *ctx,
121                                     files_struct *fsp, int oplock_type)
122 {
123         if ( SMB_VFS_LINUX_SETLEASE(fsp, F_WRLCK) == -1) {
124                 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
125                          "fd = %d, file_id = %s. (%s)\n",
126                          fsp_str_dbg(fsp), fsp->fh->fd,
127                          file_id_string_tos(&fsp->file_id),
128                          strerror(errno)));
129                 return False;
130         }
131         
132         DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
133                  "file_id = %s gen_id = %lu\n",
134                  fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
135                  fsp->fh->gen_id));
136
137         return True;
138 }
139
140 /****************************************************************************
141  Release a kernel oplock on a file.
142 ****************************************************************************/
143
144 static void linux_release_kernel_oplock(struct kernel_oplocks *ctx,
145                                         files_struct *fsp, int oplock_type)
146 {
147         if (DEBUGLVL(10)) {
148                 /*
149                  * Check and print out the current kernel
150                  * oplock state of this file.
151                  */
152                 int state = fcntl(fsp->fh->fd, F_GETLEASE, 0);
153                 dbgtext("linux_release_kernel_oplock: file %s, file_id = %s "
154                         "gen_id = %lu has kernel oplock state "
155                         "of %x.\n", fsp_str_dbg(fsp),
156                         file_id_string_tos(&fsp->file_id),
157                         fsp->fh->gen_id, state );
158         }
159
160         /*
161          * Remove the kernel oplock on this file.
162          */
163         if ( SMB_VFS_LINUX_SETLEASE(fsp, F_UNLCK) == -1) {
164                 if (DEBUGLVL(0)) {
165                         dbgtext("linux_release_kernel_oplock: Error when "
166                                 "removing kernel oplock on file " );
167                         dbgtext("%s, file_id = %s, gen_id = %lu. "
168                                 "Error was %s\n", fsp_str_dbg(fsp),
169                                 file_id_string_tos(&fsp->file_id),
170                                 fsp->fh->gen_id, strerror(errno) );
171                 }
172         }
173 }
174
175 /****************************************************************************
176  See if the kernel supports oplocks.
177 ****************************************************************************/
178
179 static bool linux_oplocks_available(void)
180 {
181         int fd, ret;
182         fd = open("/dev/null", O_RDONLY);
183         if (fd == -1)
184                 return False; /* uggh! */
185         ret = fcntl(fd, F_GETLEASE, 0);
186         close(fd);
187         return ret == F_UNLCK;
188 }
189
190 /****************************************************************************
191  Setup kernel oplocks.
192 ****************************************************************************/
193
194 static const struct kernel_oplocks_ops linux_koplocks = {
195         .set_oplock                     = linux_set_kernel_oplock,
196         .release_oplock                 = linux_release_kernel_oplock,
197         .contend_level2_oplocks_begin   = NULL,
198         .contend_level2_oplocks_end     = NULL,
199 };
200
201 struct kernel_oplocks *linux_init_kernel_oplocks(struct smbd_server_connection *sconn)
202 {
203         struct kernel_oplocks *ctx;
204         struct tevent_signal *se;
205
206         if (!linux_oplocks_available()) {
207                 DEBUG(3,("Linux kernel oplocks not available\n"));
208                 return NULL;
209         }
210
211         ctx = talloc_zero(sconn, struct kernel_oplocks);
212         if (!ctx) {
213                 DEBUG(0,("Linux Kernel oplocks talloc_Zero failed\n"));
214                 return NULL;
215         }
216
217         ctx->ops = &linux_koplocks;
218         ctx->private_data = sconn;
219
220         se = tevent_add_signal(sconn->ev_ctx,
221                                ctx,
222                                RT_SIGNAL_LEASE, SA_SIGINFO,
223                                linux_oplock_signal_handler,
224                                ctx);
225         if (!se) {
226                 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler"));
227                 TALLOC_FREE(ctx);
228                 return NULL;
229         }
230
231         ctx->private_data = se;
232
233         DEBUG(3,("Linux kernel oplocks enabled\n"));
234
235         return ctx;
236 }
237 #else
238  void oplock_linux_dummy(void);
239
240  void oplock_linux_dummy(void) {}
241 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */