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