2 #if HAVE_KERNEL_OPLOCKS_LINUX
5 Unix SMB/Netbios implementation.
7 kernel oplock processing for Linux
8 Copyright (C) Andrew Tridgell 2000
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 extern int DEBUGLEVEL;
29 static unsigned signals_received;
30 static unsigned signals_processed;
31 static int fd_pending; /* the fd of the current pending SIGIO */
33 /****************************************************************************
34 handle a SIGIO, incrementing the signals_received and blocking SIGIO
35 ****************************************************************************/
36 static void sigio_handler(int signal, siginfo_t *info, void *unused)
38 fd_pending = info->si_fd;
40 BlockSignals(True, SIGIO);
43 /****************************************************************************
44 * Deal with the Linux kernel <--> smbd
45 * oplock break protocol.
46 ****************************************************************************/
47 static BOOL linux_oplock_receive_message(fd_set *fds, char *buffer, int buffer_len)
54 if (signals_received == signals_processed) return False;
56 if (sys_fstat(fd_pending,&sbuf) == -1) {
57 DEBUG(0,("Invalid file descriptor %d in kernel oplock break!\n", fd_pending));
65 DEBUG(5,("receive_local_message: kernel oplock break request received for \
66 dev = %x, inode = %.0f\n", (unsigned int)dev, (double)inode ));
69 * Create a kernel oplock break message.
72 /* Setup the message header */
73 SIVAL(buffer,OPBRK_CMD_LEN_OFFSET,KERNEL_OPLOCK_BREAK_MSG_LEN);
74 SSVAL(buffer,OPBRK_CMD_PORT_OFFSET,0);
76 buffer += OPBRK_CMD_HEADER_LEN;
78 SSVAL(buffer,OPBRK_MESSAGE_CMD_OFFSET,KERNEL_OPLOCK_BREAK_CMD);
80 memcpy(buffer + KERNEL_OPLOCK_BREAK_DEV_OFFSET, (char *)&dev, sizeof(dev));
81 memcpy(buffer + KERNEL_OPLOCK_BREAK_INODE_OFFSET, (char *)&inode, sizeof(inode));
84 /* now we can receive more signals */
87 BlockSignals(False, SIGIO);
93 /****************************************************************************
94 Attempt to set an kernel oplock on a file.
95 ****************************************************************************/
96 static BOOL linux_set_kernel_oplock(files_struct *fsp, int oplock_type)
98 if (fcntl(fsp->fd, F_SETLEASE, F_WRLCK) == -1) {
99 DEBUG(5,("set_file_oplock: Refused oplock on file %s, fd = %d, dev = %x, \
101 fsp->fsp_name, fsp->fd, (unsigned int)fsp->dev, (double)fsp->inode));
105 DEBUG(10,("set_file_oplock: got kernel oplock on file %s, dev = %x, inode = %.0f\n",
106 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode));
112 /****************************************************************************
113 Release a kernel oplock on a file.
114 ****************************************************************************/
115 static void linux_release_kernel_oplock(files_struct *fsp)
119 * Check and print out the current kernel
120 * oplock state of this file.
122 int state = fcntl(fsp->fd, F_GETLEASE, 0);
123 dbgtext("release_kernel_oplock: file %s, dev = %x, inode = %.0f has kernel \
124 oplock state of %x.\n", fsp->fsp_name, (unsigned int)fsp->dev,
125 (double)fsp->inode, state );
129 * Remove the kernel oplock on this file.
131 if (fcntl(fsp->fd, F_SETLEASE, F_UNLCK) == -1) {
133 dbgtext("release_kernel_oplock: Error when removing kernel oplock on file " );
134 dbgtext("%s, dev = %x, inode = %.0f. Error was %s\n",
135 fsp->fsp_name, (unsigned int)fsp->dev,
136 (double)fsp->inode, strerror(errno) );
142 /****************************************************************************
143 parse a kernel oplock message
144 ****************************************************************************/
145 static BOOL linux_kernel_oplock_parse(char *msg_start, int msg_len, SMB_INO_T *inode, SMB_DEV_T *dev)
147 /* Ensure that the msg length is correct. */
148 if (msg_len != KERNEL_OPLOCK_BREAK_MSG_LEN) {
149 DEBUG(0,("process_local_message: incorrect length for KERNEL_OPLOCK_BREAK_CMD (was %d, \
150 should be %d).\n", msg_len, KERNEL_OPLOCK_BREAK_MSG_LEN));
154 memcpy((char *)inode, msg_start+KERNEL_OPLOCK_BREAK_INODE_OFFSET, sizeof(*inode));
155 memcpy((char *)dev, msg_start+KERNEL_OPLOCK_BREAK_DEV_OFFSET, sizeof(*dev));
157 DEBUG(5,("process_local_message: kernel oplock break request for \
158 file dev = %x, inode = %.0f\n", (unsigned int)dev, (double)inode));
164 /****************************************************************************
165 see if a oplock message is waiting
166 ****************************************************************************/
167 static BOOL linux_oplock_msg_waiting(fd_set *fds)
169 return signals_processed != signals_received;
173 /****************************************************************************
175 ****************************************************************************/
176 struct kernel_oplocks *linux_init_kernel_oplocks(void)
178 static struct kernel_oplocks koplocks;
179 struct sigaction act;
181 act.sa_handler = NULL;
182 act.sa_sigaction = sigio_handler;
183 act.sa_flags = SA_SIGINFO;
184 if (sigaction(SIGIO, &act, NULL) != 0) {
185 DEBUG(0,("Failed to setup SIGIO handler\n"));
189 koplocks.receive_message = linux_oplock_receive_message;
190 koplocks.set_oplock = linux_set_kernel_oplock;
191 koplocks.release_oplock = linux_release_kernel_oplock;
192 koplocks.parse_message = linux_kernel_oplock_parse;
193 koplocks.msg_waiting = linux_oplock_msg_waiting;
194 koplocks.notification_fd = -1;
202 void oplock_linux_dummy(void) {}
203 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */