s3-docs: Fix typo.
[Samba/gebeck_regimport.git] / source3 / smbd / oplock_linux.c
bloba455707deea842320e48f82e55c217e1b6fab737
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 "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
26 #if HAVE_KERNEL_OPLOCKS_LINUX
28 #ifndef F_SETLEASE
29 #define F_SETLEASE 1024
30 #endif
32 #ifndef F_GETLEASE
33 #define F_GETLEASE 1025
34 #endif
36 #ifndef CAP_LEASE
37 #define CAP_LEASE 28
38 #endif
40 #ifndef RT_SIGNAL_LEASE
41 #define RT_SIGNAL_LEASE (SIGRTMIN+1)
42 #endif
44 #ifndef F_SETSIG
45 #define F_SETSIG 10
46 #endif
49 * public function to get linux lease capability. Needed by some VFS modules (eg. gpfs.c)
51 void linux_set_lease_capability(void)
53 set_effective_capability(LEASE_CAPABILITY);
56 /*
57 * Call to set the kernel lease signal handler
59 int linux_set_lease_sighandler(int fd)
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;
66 return 0;
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 ****************************************************************************/
75 int linux_setlease(int fd, int leasetype)
77 int ret;
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);
85 return ret;
88 /****************************************************************************
89 * Deal with the Linux kernel <--> smbd
90 * oplock break protocol.
91 ****************************************************************************/
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)
98 siginfo_t *info = (siginfo_t *)_info;
99 int fd = info->si_fd;
100 files_struct *fsp;
102 fsp = file_find_fd(smbd_server_conn, fd);
103 if (fsp == NULL) {
104 DEBUG(0,("linux_oplock_signal_handler: failed to find fsp for file fd=%d (file was closed ?)\n", fd ));
105 return;
107 break_kernel_oplock(fsp->conn->sconn->msg_ctx, fsp);
110 /****************************************************************************
111 Attempt to set an kernel oplock on a file.
112 ****************************************************************************/
114 static bool linux_set_kernel_oplock(struct kernel_oplocks *ctx,
115 files_struct *fsp, int oplock_type)
117 if ( SMB_VFS_LINUX_SETLEASE(fsp, F_WRLCK) == -1) {
118 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
119 "fd = %d, file_id = %s. (%s)\n",
120 fsp_str_dbg(fsp), fsp->fh->fd,
121 file_id_string_tos(&fsp->file_id),
122 strerror(errno)));
123 return False;
126 DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
127 "file_id = %s gen_id = %lu\n",
128 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
129 fsp->fh->gen_id));
131 return True;
134 /****************************************************************************
135 Release a kernel oplock on a file.
136 ****************************************************************************/
138 static void linux_release_kernel_oplock(struct kernel_oplocks *ctx,
139 files_struct *fsp, int oplock_type)
141 if (DEBUGLVL(10)) {
143 * Check and print out the current kernel
144 * oplock state of this file.
146 int state = fcntl(fsp->fh->fd, F_GETLEASE, 0);
147 dbgtext("linux_release_kernel_oplock: file %s, file_id = %s "
148 "gen_id = %lu has kernel oplock state "
149 "of %x.\n", fsp_str_dbg(fsp),
150 file_id_string_tos(&fsp->file_id),
151 fsp->fh->gen_id, state );
155 * Remove the kernel oplock on this file.
157 if ( SMB_VFS_LINUX_SETLEASE(fsp, F_UNLCK) == -1) {
158 if (DEBUGLVL(0)) {
159 dbgtext("linux_release_kernel_oplock: Error when "
160 "removing kernel oplock on file " );
161 dbgtext("%s, file_id = %s, gen_id = %lu. "
162 "Error was %s\n", fsp_str_dbg(fsp),
163 file_id_string_tos(&fsp->file_id),
164 fsp->fh->gen_id, strerror(errno) );
169 /****************************************************************************
170 See if the kernel supports oplocks.
171 ****************************************************************************/
173 static bool linux_oplocks_available(void)
175 int fd, ret;
176 fd = open("/dev/null", O_RDONLY);
177 if (fd == -1)
178 return False; /* uggh! */
179 ret = fcntl(fd, F_GETLEASE, 0);
180 close(fd);
181 return ret == F_UNLCK;
184 /****************************************************************************
185 Setup kernel oplocks.
186 ****************************************************************************/
188 static const struct kernel_oplocks_ops linux_koplocks = {
189 .set_oplock = linux_set_kernel_oplock,
190 .release_oplock = linux_release_kernel_oplock,
191 .contend_level2_oplocks_begin = NULL,
192 .contend_level2_oplocks_end = NULL,
195 struct kernel_oplocks *linux_init_kernel_oplocks(TALLOC_CTX *mem_ctx)
197 struct kernel_oplocks *ctx;
198 struct tevent_signal *se;
200 if (!linux_oplocks_available()) {
201 DEBUG(3,("Linux kernel oplocks not available\n"));
202 return NULL;
205 ctx = talloc_zero(mem_ctx, struct kernel_oplocks);
206 if (!ctx) {
207 DEBUG(0,("Linux Kernel oplocks talloc_Zero failed\n"));
208 return NULL;
211 ctx->ops = &linux_koplocks;
213 se = tevent_add_signal(server_event_context(),
214 ctx,
215 RT_SIGNAL_LEASE, SA_SIGINFO,
216 linux_oplock_signal_handler,
217 ctx);
218 if (!se) {
219 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler"));
220 TALLOC_FREE(ctx);
221 return NULL;
224 ctx->private_data = se;
226 DEBUG(3,("Linux kernel oplocks enabled\n"));
228 return ctx;
230 #else
231 void oplock_linux_dummy(void);
233 void oplock_linux_dummy(void) {}
234 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */