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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define DBGC_CLASS DBGC_LOCKING
24 #if HAVE_KERNEL_OPLOCKS_LINUX
26 /* these can be removed when they are in glibc headers */
27 struct cap_user_header {
31 struct cap_user_data {
37 extern int capget(struct cap_user_header * hdrp,
38 struct cap_user_data * datap);
39 extern int capset(struct cap_user_header * hdrp,
40 const struct cap_user_data * datap);
42 static SIG_ATOMIC_T signals_received;
43 #define FD_PENDING_SIZE 100
44 static SIG_ATOMIC_T fd_pending_array[FD_PENDING_SIZE];
47 #define F_SETLEASE 1024
51 #define F_GETLEASE 1025
58 #ifndef RT_SIGNAL_LEASE
59 #define RT_SIGNAL_LEASE (SIGRTMIN+1)
66 /****************************************************************************
67 Handle a LEASE signal, incrementing the signals_received and blocking the signal.
68 ****************************************************************************/
70 static void signal_handler(int sig, siginfo_t *info, void *unused)
72 if (signals_received < FD_PENDING_SIZE - 1) {
73 fd_pending_array[signals_received] = (SIG_ATOMIC_T)info->si_fd;
75 } /* Else signal is lost. */
76 sys_select_signal(RT_SIGNAL_LEASE);
79 /****************************************************************************
80 Try to gain a linux capability.
81 ****************************************************************************/
83 static void set_capability(unsigned capability)
85 #ifndef _LINUX_CAPABILITY_VERSION
86 #define _LINUX_CAPABILITY_VERSION 0x19980330
88 header.version = _LINUX_CAPABILITY_VERSION;
91 if (capget(&header, &data) == -1) {
92 DEBUG(3,("Unable to get kernel capabilities (%s)\n",
97 data.effective |= (1<<capability);
99 if (capset(&header, &data) == -1) {
100 DEBUG(3,("Unable to set %d capability (%s)\n",
101 capability, strerror(errno)));
105 /****************************************************************************
106 Call SETLEASE. If we get EACCES then we try setting up the right capability and
108 ****************************************************************************/
110 static int linux_setlease(int fd, int leasetype)
114 if (fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
115 DEBUG(3,("Failed to set signal handler for kernel lease\n"));
119 ret = fcntl(fd, F_SETLEASE, leasetype);
120 if (ret == -1 && errno == EACCES) {
121 set_capability(CAP_LEASE);
122 ret = fcntl(fd, F_SETLEASE, leasetype);
128 /****************************************************************************
129 * Deal with the Linux kernel <--> smbd
130 * oplock break protocol.
131 ****************************************************************************/
133 static files_struct *linux_oplock_receive_message(fd_set *fds)
138 BlockSignals(True, RT_SIGNAL_LEASE);
139 fd = fd_pending_array[0];
140 fsp = file_find_fd(fd);
141 fd_pending_array[0] = (SIG_ATOMIC_T)-1;
142 if (signals_received > 1)
143 memmove(CONST_DISCARD(void *, &fd_pending_array[0]),
144 CONST_DISCARD(void *, &fd_pending_array[1]),
145 sizeof(SIG_ATOMIC_T)*(signals_received-1));
147 /* now we can receive more signals */
148 BlockSignals(False, RT_SIGNAL_LEASE);
153 /****************************************************************************
154 Attempt to set an kernel oplock on a file.
155 ****************************************************************************/
157 static BOOL linux_set_kernel_oplock(files_struct *fsp, int oplock_type)
159 if (linux_setlease(fsp->fh->fd, F_WRLCK) == -1) {
160 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
161 "fd = %d, dev = %x, inode = %.0f. (%s)\n",
162 fsp->fsp_name, fsp->fh->fd,
163 (unsigned int)fsp->dev, (double)fsp->inode,
168 DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
169 "dev = %x, inode = %.0f, file_id = %lu\n",
170 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode,
176 /****************************************************************************
177 Release a kernel oplock on a file.
178 ****************************************************************************/
180 static void linux_release_kernel_oplock(files_struct *fsp)
184 * Check and print out the current kernel
185 * oplock state of this file.
187 int state = fcntl(fsp->fh->fd, F_GETLEASE, 0);
188 dbgtext("linux_release_kernel_oplock: file %s, dev = %x, "
189 "inode = %.0f file_id = %lu has kernel oplock state "
190 "of %x.\n", fsp->fsp_name, (unsigned int)fsp->dev,
191 (double)fsp->inode, fsp->fh->file_id, state );
195 * Remove the kernel oplock on this file.
197 if (linux_setlease(fsp->fh->fd, F_UNLCK) == -1) {
199 dbgtext("linux_release_kernel_oplock: Error when "
200 "removing kernel oplock on file " );
201 dbgtext("%s, dev = %x, inode = %.0f, file_id = %lu. "
202 "Error was %s\n", fsp->fsp_name,
203 (unsigned int)fsp->dev, (double)fsp->inode,
204 fsp->fh->file_id, strerror(errno) );
209 /****************************************************************************
210 See if a oplock message is waiting.
211 ****************************************************************************/
213 static BOOL linux_oplock_msg_waiting(fd_set *fds)
215 return signals_received != 0;
218 /****************************************************************************
219 See if the kernel supports oplocks.
220 ****************************************************************************/
222 static BOOL linux_oplocks_available(void)
225 fd = open("/dev/null", O_RDONLY);
227 return False; /* uggh! */
228 ret = fcntl(fd, F_GETLEASE, 0);
230 return ret == F_UNLCK;
233 /****************************************************************************
234 Setup kernel oplocks.
235 ****************************************************************************/
237 struct kernel_oplocks *linux_init_kernel_oplocks(void)
239 static struct kernel_oplocks koplocks;
240 struct sigaction act;
242 if (!linux_oplocks_available()) {
243 DEBUG(3,("Linux kernel oplocks not available\n"));
249 act.sa_handler = NULL;
250 act.sa_sigaction = signal_handler;
251 act.sa_flags = SA_SIGINFO;
252 sigemptyset( &act.sa_mask );
253 if (sigaction(RT_SIGNAL_LEASE, &act, NULL) != 0) {
254 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler\n"));
258 koplocks.receive_message = linux_oplock_receive_message;
259 koplocks.set_oplock = linux_set_kernel_oplock;
260 koplocks.release_oplock = linux_release_kernel_oplock;
261 koplocks.msg_waiting = linux_oplock_msg_waiting;
262 koplocks.notification_fd = -1;
264 /* the signal can start off blocked due to a bug in bash */
265 BlockSignals(False, RT_SIGNAL_LEASE);
267 DEBUG(3,("Linux kernel oplocks enabled\n"));
272 void oplock_linux_dummy(void);
274 void oplock_linux_dummy(void) {}
275 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */