s3-swat: Fix typo.
[Samba.git] / source3 / smbd / oplock_linux.c
blob2bc0a55518ce7b2dee54707e4a896a87c91b834f
1 /*
2 Unix SMB/CIFS implementation.
3 kernel oplock processing for Linux
4 Copyright (C) Andrew Tridgell 2000
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.
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.
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/>.
20 #define DBGC_CLASS DBGC_LOCKING
21 #include "includes.h"
22 #include "smbd/globals.h"
24 #if HAVE_KERNEL_OPLOCKS_LINUX
26 #ifndef F_SETLEASE
27 #define F_SETLEASE 1024
28 #endif
30 #ifndef F_GETLEASE
31 #define F_GETLEASE 1025
32 #endif
34 #ifndef CAP_LEASE
35 #define CAP_LEASE 28
36 #endif
38 #ifndef RT_SIGNAL_LEASE
39 #define RT_SIGNAL_LEASE (SIGRTMIN+1)
40 #endif
42 #ifndef F_SETSIG
43 #define F_SETSIG 10
44 #endif
47 * public function to get linux lease capability. Needed by some VFS modules (eg. gpfs.c)
49 void linux_set_lease_capability(void)
51 set_effective_capability(LEASE_CAPABILITY);
54 /*
55 * Call to set the kernel lease signal handler
57 int linux_set_lease_sighandler(int fd)
59 if (fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
60 DEBUG(3,("Failed to set signal handler for kernel lease\n"));
61 return -1;
64 return 0;
67 /****************************************************************************
68 Call SETLEASE. If we get EACCES then we try setting up the right capability and
69 try again.
70 Use the SMB_VFS_LINUX_SETLEASE instead of this call directly.
71 ****************************************************************************/
73 int linux_setlease(int fd, int leasetype)
75 int ret;
77 ret = fcntl(fd, F_SETLEASE, leasetype);
78 if (ret == -1 && errno == EACCES) {
79 set_effective_capability(LEASE_CAPABILITY);
80 ret = fcntl(fd, F_SETLEASE, leasetype);
83 return ret;
86 /****************************************************************************
87 * Deal with the Linux kernel <--> smbd
88 * oplock break protocol.
89 ****************************************************************************/
91 static void linux_oplock_signal_handler(struct tevent_context *ev_ctx,
92 struct tevent_signal *se,
93 int signum, int count,
94 void *_info, void *private_data)
96 siginfo_t *info = (siginfo_t *)_info;
97 int fd = info->si_fd;
98 files_struct *fsp;
100 fsp = file_find_fd(fd);
101 if (fsp == NULL) {
102 DEBUG(0,("linux_oplock_signal_handler: failed to find fsp for file fd=%d (file was closed ?)\n", fd ));
103 return;
105 break_kernel_oplock(smbd_messaging_context(), fsp);
108 /****************************************************************************
109 Attempt to set an kernel oplock on a file.
110 ****************************************************************************/
112 static bool linux_set_kernel_oplock(struct kernel_oplocks *ctx,
113 files_struct *fsp, int oplock_type)
115 if ( SMB_VFS_LINUX_SETLEASE(fsp, F_WRLCK) == -1) {
116 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
117 "fd = %d, file_id = %s. (%s)\n",
118 fsp->fsp_name, fsp->fh->fd,
119 file_id_string_tos(&fsp->file_id),
120 strerror(errno)));
121 return False;
124 DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
125 "file_id = %s gen_id = %lu\n",
126 fsp->fsp_name, file_id_string_tos(&fsp->file_id),
127 fsp->fh->gen_id));
129 return True;
132 /****************************************************************************
133 Release a kernel oplock on a file.
134 ****************************************************************************/
136 static void linux_release_kernel_oplock(struct kernel_oplocks *ctx,
137 files_struct *fsp, int oplock_type)
139 if (DEBUGLVL(10)) {
141 * Check and print out the current kernel
142 * oplock state of this file.
144 int state = fcntl(fsp->fh->fd, F_GETLEASE, 0);
145 dbgtext("linux_release_kernel_oplock: file %s, file_id = %s "
146 "gen_id = %lu has kernel oplock state "
147 "of %x.\n", fsp->fsp_name, file_id_string_tos(&fsp->file_id),
148 fsp->fh->gen_id, state );
152 * Remove the kernel oplock on this file.
154 if ( SMB_VFS_LINUX_SETLEASE(fsp, F_UNLCK) == -1) {
155 if (DEBUGLVL(0)) {
156 dbgtext("linux_release_kernel_oplock: Error when "
157 "removing kernel oplock on file " );
158 dbgtext("%s, file_id = %s, gen_id = %lu. "
159 "Error was %s\n", fsp->fsp_name,
160 file_id_string_tos(&fsp->file_id),
161 fsp->fh->gen_id, strerror(errno) );
166 /****************************************************************************
167 See if the kernel supports oplocks.
168 ****************************************************************************/
170 static bool linux_oplocks_available(void)
172 int fd, ret;
173 fd = open("/dev/null", O_RDONLY);
174 if (fd == -1)
175 return False; /* uggh! */
176 ret = fcntl(fd, F_GETLEASE, 0);
177 close(fd);
178 return ret == F_UNLCK;
181 /****************************************************************************
182 Setup kernel oplocks.
183 ****************************************************************************/
185 static const struct kernel_oplocks_ops linux_koplocks = {
186 .set_oplock = linux_set_kernel_oplock,
187 .release_oplock = linux_release_kernel_oplock,
188 .contend_level2_oplocks_begin = NULL,
189 .contend_level2_oplocks_end = NULL,
192 struct kernel_oplocks *linux_init_kernel_oplocks(TALLOC_CTX *mem_ctx)
194 struct kernel_oplocks *ctx;
195 struct tevent_signal *se;
197 if (!linux_oplocks_available()) {
198 DEBUG(3,("Linux kernel oplocks not available\n"));
199 return NULL;
202 ctx = talloc_zero(mem_ctx, struct kernel_oplocks);
203 if (!ctx) {
204 DEBUG(0,("Linux Kernel oplocks talloc_Zero failed\n"));
205 return NULL;
208 ctx->ops = &linux_koplocks;
210 se = tevent_add_signal(smbd_event_context(),
211 ctx,
212 RT_SIGNAL_LEASE, SA_SIGINFO,
213 linux_oplock_signal_handler,
214 ctx);
215 if (!se) {
216 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler"));
217 TALLOC_FREE(ctx);
218 return NULL;
221 ctx->private_data = se;
223 DEBUG(3,("Linux kernel oplocks enabled\n"));
225 return ctx;
227 #else
228 void oplock_linux_dummy(void);
230 void oplock_linux_dummy(void) {}
231 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */