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
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
26 #if HAVE_KERNEL_OPLOCKS_LINUX
29 #define F_SETLEASE 1024
33 #define F_GETLEASE 1025
40 #ifndef RT_SIGNAL_LEASE
41 #define RT_SIGNAL_LEASE (SIGRTMIN+1)
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
);
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"));
69 /****************************************************************************
70 Call SETLEASE. If we get EACCES then we try setting up the right capability and
72 Use the SMB_VFS_LINUX_SETLEASE instead of this call directly.
73 ****************************************************************************/
75 int linux_setlease(int fd
, int leasetype
)
81 * Ensure the lease owner is root to allow
82 * correct delivery of lease-break signals.
87 /* First set the signal handler. */
88 if (linux_set_lease_sighandler(fd
) == -1) {
93 ret
= fcntl(fd
, F_SETLEASE
, leasetype
);
108 /****************************************************************************
109 * Deal with the Linux kernel <--> smbd
110 * oplock break protocol.
111 ****************************************************************************/
113 static void linux_oplock_signal_handler(struct tevent_context
*ev_ctx
,
114 struct tevent_signal
*se
,
115 int signum
, int count
,
116 void *_info
, void *private_data
)
118 struct kernel_oplocks
*ctx
=
119 talloc_get_type_abort(private_data
,
120 struct kernel_oplocks
);
121 struct smbd_server_connection
*sconn
=
122 talloc_get_type_abort(ctx
->private_data
,
123 struct smbd_server_connection
);
124 siginfo_t
*info
= (siginfo_t
*)_info
;
125 int fd
= info
->si_fd
;
129 * This function doesn't expect any specific impersonation, as it only
130 * sends messages to other smbd processes. And messaging_send_iov_from()
131 * already handles EACCES.
134 fsp
= file_find_fd(sconn
, fd
);
136 DEBUG(0,("linux_oplock_signal_handler: failed to find fsp for file fd=%d (file was closed ?)\n", fd
));
139 break_kernel_oplock(sconn
->msg_ctx
, fsp
);
142 /****************************************************************************
143 Attempt to set an kernel oplock on a file.
144 ****************************************************************************/
146 static bool linux_set_kernel_oplock(struct kernel_oplocks
*ctx
,
147 files_struct
*fsp
, int oplock_type
)
149 if ( SMB_VFS_LINUX_SETLEASE(fsp
, F_WRLCK
) == -1) {
150 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
151 "fd = %d, file_id = %s. (%s)\n",
152 fsp_str_dbg(fsp
), fsp
->fh
->fd
,
153 file_id_string_tos(&fsp
->file_id
),
158 DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
159 "file_id = %s gen_id = %lu\n",
160 fsp_str_dbg(fsp
), file_id_string_tos(&fsp
->file_id
),
166 /****************************************************************************
167 Release a kernel oplock on a file.
168 ****************************************************************************/
170 static void linux_release_kernel_oplock(struct kernel_oplocks
*ctx
,
171 files_struct
*fsp
, int oplock_type
)
175 * Check and print out the current kernel
176 * oplock state of this file.
178 int state
= fcntl(fsp
->fh
->fd
, F_GETLEASE
, 0);
179 dbgtext("linux_release_kernel_oplock: file %s, file_id = %s "
180 "gen_id = %lu has kernel oplock state "
181 "of %x.\n", fsp_str_dbg(fsp
),
182 file_id_string_tos(&fsp
->file_id
),
183 fsp
->fh
->gen_id
, state
);
187 * Remove the kernel oplock on this file.
189 if ( SMB_VFS_LINUX_SETLEASE(fsp
, F_UNLCK
) == -1) {
191 dbgtext("linux_release_kernel_oplock: Error when "
192 "removing kernel oplock on file " );
193 dbgtext("%s, file_id = %s, gen_id = %lu. "
194 "Error was %s\n", fsp_str_dbg(fsp
),
195 file_id_string_tos(&fsp
->file_id
),
196 fsp
->fh
->gen_id
, strerror(errno
) );
201 /****************************************************************************
202 See if the kernel supports oplocks.
203 ****************************************************************************/
205 static bool linux_oplocks_available(void)
208 fd
= open("/dev/null", O_RDONLY
);
210 return False
; /* uggh! */
211 ret
= fcntl(fd
, F_GETLEASE
, 0);
213 return ret
== F_UNLCK
;
216 /****************************************************************************
217 Setup kernel oplocks.
218 ****************************************************************************/
220 static const struct kernel_oplocks_ops linux_koplocks
= {
221 .set_oplock
= linux_set_kernel_oplock
,
222 .release_oplock
= linux_release_kernel_oplock
,
223 .contend_level2_oplocks_begin
= NULL
,
224 .contend_level2_oplocks_end
= NULL
,
227 struct kernel_oplocks
*linux_init_kernel_oplocks(struct smbd_server_connection
*sconn
)
229 struct kernel_oplocks
*ctx
;
230 struct tevent_signal
*se
;
232 if (!linux_oplocks_available()) {
233 DEBUG(3,("Linux kernel oplocks not available\n"));
237 ctx
= talloc_zero(sconn
, struct kernel_oplocks
);
239 DEBUG(0,("Linux Kernel oplocks talloc_Zero failed\n"));
243 ctx
->ops
= &linux_koplocks
;
244 ctx
->private_data
= sconn
;
247 * linux_oplock_signal_handler() only
248 * sends messages to other smbd processes
249 * and doesn't require any impersonation.
250 * So we can just use the raw tevent_context.
252 se
= tevent_add_signal(sconn
->raw_ev_ctx
,
254 RT_SIGNAL_LEASE
, SA_SIGINFO
,
255 linux_oplock_signal_handler
,
258 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler"));
263 DEBUG(3,("Linux kernel oplocks enabled\n"));
268 void oplock_linux_dummy(void);
270 void oplock_linux_dummy(void) {}
271 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */