2 Unix SMB/Netbios implementation.
4 kernel oplock processing for Linux
5 Copyright (C) Andrew Tridgell 2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #if HAVE_KERNEL_OPLOCKS_LINUX
26 static VOLATILE sig_atomic_t signals_received;
27 static VOLATILE sig_atomic_t signals_processed;
28 static VOLATILE sig_atomic_t fd_pending; /* the fd of the current pending signal */
31 #define F_SETLEASE 1024
35 #define F_GETLEASE 1025
42 #ifndef RT_SIGNAL_LEASE
43 #define RT_SIGNAL_LEASE 33
50 /****************************************************************************
51 Handle a LEASE signal, incrementing the signals_received and blocking the signal.
52 ****************************************************************************/
54 static void signal_handler(int sig, siginfo_t *info, void *unused)
56 BlockSignals(True, sig);
57 fd_pending = (sig_atomic_t)info->si_fd;
62 /****************************************************************************
63 Try to gain a linux capability.
64 ****************************************************************************/
66 static void set_capability(unsigned capability)
68 #ifndef _LINUX_CAPABILITY_VERSION
69 #define _LINUX_CAPABILITY_VERSION 0x19980330
71 /* these can be removed when they are in glibc headers */
82 header.version = _LINUX_CAPABILITY_VERSION;
85 if (capget(&header, &data) == -1) {
86 DEBUG(3,("Unable to get kernel capabilities (%s)\n", strerror(errno)));
90 data.effective |= (1<<capability);
92 if (capset(&header, &data) == -1) {
93 DEBUG(3,("Unable to set %d capability (%s)\n",
94 capability, strerror(errno)));
98 /****************************************************************************
99 Call SETLEASE. If we get EACCES then we try setting up the right capability and
101 ****************************************************************************/
103 static int linux_setlease(int fd, int leasetype)
107 if (fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
108 DEBUG(3,("Failed to set signal handler for kernel lease\n"));
112 ret = fcntl(fd, F_SETLEASE, leasetype);
113 if (ret == -1 && errno == EACCES) {
114 set_capability(CAP_LEASE);
115 ret = fcntl(fd, F_SETLEASE, leasetype);
121 /****************************************************************************
122 * Deal with the Linux kernel <--> smbd
123 * oplock break protocol.
124 ****************************************************************************/
126 static BOOL linux_oplock_receive_message(fd_set *fds, char *buffer, int buffer_len)
129 struct files_struct *fsp;
131 if (signals_received == signals_processed)
134 if ((fsp = file_find_fd(fd_pending)) == NULL) {
135 DEBUG(0,("Invalid file descriptor %d in kernel oplock break!\n", (int)fd_pending));
140 DEBUG(3,("receive_local_message: kernel oplock break request received for \
141 dev = %x, inode = %.0f\n", (unsigned int)fsp->dev, (double)fsp->inode ));
144 * Create a kernel oplock break message.
147 /* Setup the message header */
148 SIVAL(buffer,OPBRK_CMD_LEN_OFFSET,KERNEL_OPLOCK_BREAK_MSG_LEN);
149 SSVAL(buffer,OPBRK_CMD_PORT_OFFSET,0);
151 buffer += OPBRK_CMD_HEADER_LEN;
153 SSVAL(buffer,OPBRK_MESSAGE_CMD_OFFSET,KERNEL_OPLOCK_BREAK_CMD);
155 memcpy(buffer + KERNEL_OPLOCK_BREAK_DEV_OFFSET, (char *)&fsp->dev, sizeof(fsp->dev));
156 memcpy(buffer + KERNEL_OPLOCK_BREAK_INODE_OFFSET, (char *)&fsp->inode, sizeof(fsp->inode));
157 memcpy(buffer + KERNEL_OPLOCK_BREAK_FILEID_OFFSET, (char *)&fsp->file_id, sizeof(fsp->file_id));
160 /* now we can receive more signals */
161 fd_pending = (sig_atomic_t)-1;
163 BlockSignals(False, RT_SIGNAL_LEASE);
168 /****************************************************************************
169 Attempt to set an kernel oplock on a file.
170 ****************************************************************************/
172 static BOOL linux_set_kernel_oplock(files_struct *fsp, int oplock_type)
174 if (linux_setlease(fsp->fd, F_WRLCK) == -1) {
175 DEBUG(3,("set_file_oplock: Refused oplock on file %s, fd = %d, dev = %x, \
176 inode = %.0f. (%s)\n",
177 fsp->fsp_name, fsp->fd,
178 (unsigned int)fsp->dev, (double)fsp->inode, strerror(errno)));
182 DEBUG(3,("set_file_oplock: got kernel oplock on file %s, dev = %x, inode = %.0f, file_id = %lu\n",
183 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id));
188 /****************************************************************************
189 Release a kernel oplock on a file.
190 ****************************************************************************/
192 static void linux_release_kernel_oplock(files_struct *fsp)
196 * Check and print out the current kernel
197 * oplock state of this file.
199 int state = fcntl(fsp->fd, F_GETLEASE, 0);
200 dbgtext("release_kernel_oplock: file %s, dev = %x, inode = %.0f file_id = %lu has kernel \
201 oplock state of %x.\n", fsp->fsp_name, (unsigned int)fsp->dev,
202 (double)fsp->inode, fsp->file_id, state );
206 * Remove the kernel oplock on this file.
208 if (linux_setlease(fsp->fd, F_UNLCK) == -1) {
210 dbgtext("release_kernel_oplock: Error when removing kernel oplock on file " );
211 dbgtext("%s, dev = %x, inode = %.0f, file_id = %lu. Error was %s\n",
212 fsp->fsp_name, (unsigned int)fsp->dev,
213 (double)fsp->inode, fsp->file_id, strerror(errno) );
218 /****************************************************************************
219 Parse a kernel oplock message.
220 ****************************************************************************/
222 static BOOL linux_kernel_oplock_parse(char *msg_start, int msg_len, SMB_INO_T *inode,
223 SMB_DEV_T *dev, unsigned long *file_id)
225 /* Ensure that the msg length is correct. */
226 if (msg_len != KERNEL_OPLOCK_BREAK_MSG_LEN) {
227 DEBUG(0,("incorrect length for KERNEL_OPLOCK_BREAK_CMD (was %d, should be %d).\n",
228 msg_len, KERNEL_OPLOCK_BREAK_MSG_LEN));
232 memcpy((char *)inode, msg_start+KERNEL_OPLOCK_BREAK_INODE_OFFSET, sizeof(*inode));
233 memcpy((char *)dev, msg_start+KERNEL_OPLOCK_BREAK_DEV_OFFSET, sizeof(*dev));
234 memcpy((char *)file_id, msg_start+KERNEL_OPLOCK_BREAK_FILEID_OFFSET, sizeof(*file_id));
236 DEBUG(3,("kernel oplock break request for file dev = %x, inode = %.0f, file_id = %lu\n",
237 (unsigned int)*dev, (double)*inode, *file_id));
242 /****************************************************************************
243 See if a oplock message is waiting.
244 ****************************************************************************/
246 static BOOL linux_oplock_msg_waiting(fd_set *fds)
248 return signals_processed != signals_received;
251 /****************************************************************************
252 See if the kernel supports oplocks.
253 ****************************************************************************/
255 static BOOL linux_oplocks_available(void)
258 fd = open("/dev/null", O_RDONLY);
260 return False; /* uggh! */
261 ret = fcntl(fd, F_GETLEASE, 0);
263 return ret == F_UNLCK;
266 /****************************************************************************
267 Setup kernel oplocks.
268 ****************************************************************************/
270 struct kernel_oplocks *linux_init_kernel_oplocks(void)
272 static struct kernel_oplocks koplocks;
273 struct sigaction act;
275 if (!linux_oplocks_available()) {
276 DEBUG(3,("Linux kernel oplocks not available\n"));
280 act.sa_handler = NULL;
281 act.sa_sigaction = signal_handler;
282 act.sa_flags = SA_SIGINFO;
283 if (sigaction(RT_SIGNAL_LEASE, &act, NULL) != 0) {
284 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler\n"));
288 koplocks.receive_message = linux_oplock_receive_message;
289 koplocks.set_oplock = linux_set_kernel_oplock;
290 koplocks.release_oplock = linux_release_kernel_oplock;
291 koplocks.parse_message = linux_kernel_oplock_parse;
292 koplocks.msg_waiting = linux_oplock_msg_waiting;
293 koplocks.notification_fd = -1;
295 DEBUG(3,("Linux kernel oplocks enabled\n"));
300 void oplock_linux_dummy(void) {}
301 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */