2 Unix SMB/Netbios implementation.
4 IRIX kernel oplock processing
5 Copyright (C) Andrew Tridgell 1992-1998
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_IRIX
25 extern int DEBUGLEVEL;
27 static int oplock_pipe_write = -1;
28 static int oplock_pipe_read = -1;
30 /****************************************************************************
31 test to see if IRIX kernel oplocks work
32 ****************************************************************************/
33 static BOOL irix_oplocks_available(void)
39 oplock_set_capability(True, False);
41 slprintf(tmpname,sizeof(tmpname)-1, "%s/koplock.%d", lp_lockdir(), (int)sys_getpid());
44 DEBUG(0,("check_kernel_oplocks: Unable to create pipe. Error was %s\n",
49 if((fd = sys_open(tmpname, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600)) < 0) {
50 DEBUG(0,("check_kernel_oplocks: Unable to open temp test file %s. Error was %s\n",
51 tmpname, strerror(errno) ));
60 if(fcntl(fd, F_OPLKREG, pfd[1]) == -1) {
61 DEBUG(0,("check_kernel_oplocks: Kernel oplocks are not available on this machine. \
62 Disabling kernel oplock support.\n" ));
69 if(fcntl(fd, F_OPLKACK, OP_REVOKE) < 0 ) {
70 DEBUG(0,("check_kernel_oplocks: Error when removing kernel oplock. Error was %s. \
71 Disabling kernel oplock support.\n", strerror(errno) ));
87 /****************************************************************************
88 * Deal with the IRIX kernel <--> smbd
89 * oplock break protocol.
90 ****************************************************************************/
91 static BOOL irix_oplock_receive_message(fd_set *fds, char *buffer, int buffer_len)
93 extern int smb_read_error;
100 * Read one byte of zero to clear the
101 * kernel break notify message.
104 if(read(oplock_pipe_read, &dummy, 1) != 1) {
105 DEBUG(0,("receive_local_message: read of kernel notification failed. \
106 Error was %s.\n", strerror(errno) ));
107 smb_read_error = READ_ERROR;
112 * Do a query to get the
113 * device and inode of the file that has the break
114 * request outstanding.
117 if(fcntl(oplock_pipe_read, F_OPLKSTAT, &os) < 0) {
118 DEBUG(0,("receive_local_message: fcntl of kernel notification failed. \
119 Error was %s.\n", strerror(errno) ));
120 if(errno == EAGAIN) {
122 * Duplicate kernel break message - ignore.
124 memset(buffer, '\0', KERNEL_OPLOCK_BREAK_MSG_LEN);
127 smb_read_error = READ_ERROR;
131 dev = (SMB_DEV_T)os.os_dev;
132 inode = (SMB_INO_T)os.os_ino;
134 DEBUG(5,("receive_local_message: kernel oplock break request received for \
135 dev = %x, inode = %.0f\n", (unsigned int)dev, (double)inode ));
138 * Create a kernel oplock break message.
141 /* Setup the message header */
142 SIVAL(buffer,OPBRK_CMD_LEN_OFFSET,KERNEL_OPLOCK_BREAK_MSG_LEN);
143 SSVAL(buffer,OPBRK_CMD_PORT_OFFSET,0);
145 buffer += OPBRK_CMD_HEADER_LEN;
147 SSVAL(buffer,OPBRK_MESSAGE_CMD_OFFSET,KERNEL_OPLOCK_BREAK_CMD);
149 memcpy(buffer + KERNEL_OPLOCK_BREAK_DEV_OFFSET, (char *)&dev, sizeof(dev));
150 memcpy(buffer + KERNEL_OPLOCK_BREAK_INODE_OFFSET, (char *)&inode, sizeof(inode));
156 /****************************************************************************
157 Attempt to set an kernel oplock on a file.
158 ****************************************************************************/
159 static BOOL irix_set_kernel_oplock(files_struct *fsp, int oplock_type)
161 if (fcntl(fsp->fd, F_OPLKREG, oplock_pipe_write) == -1) {
162 if(errno != EAGAIN) {
163 DEBUG(0,("set_file_oplock: Unable to get kernel oplock on file %s, dev = %x, \
164 inode = %.0f. Error was %s\n",
165 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode,
168 DEBUG(5,("set_file_oplock: Refused oplock on file %s, fd = %d, dev = %x, \
169 inode = %.0f. Another process had the file open.\n",
170 fsp->fsp_name, fsp->fd, (unsigned int)fsp->dev, (double)fsp->inode ));
175 DEBUG(10,("set_file_oplock: got kernel oplock on file %s, dev = %x, inode = %.0f\n",
176 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode));
182 /****************************************************************************
183 Release a kernel oplock on a file.
184 ****************************************************************************/
185 static void irix_release_kernel_oplock(files_struct *fsp)
189 * Check and print out the current kernel
190 * oplock state of this file.
192 int state = fcntl(fsp->fd, F_OPLKACK, -1);
193 dbgtext("release_kernel_oplock: file %s, dev = %x, inode = %.0f has kernel \
194 oplock state of %x.\n", fsp->fsp_name, (unsigned int)fsp->dev,
195 (double)fsp->inode, state );
199 * Remove the kernel oplock on this file.
201 if(fcntl(fsp->fd, F_OPLKACK, OP_REVOKE) < 0) {
203 dbgtext("release_kernel_oplock: Error when removing kernel oplock on file " );
204 dbgtext("%s, dev = %x, inode = %.0f. Error was %s\n",
205 fsp->fsp_name, (unsigned int)fsp->dev,
206 (double)fsp->inode, strerror(errno) );
212 /****************************************************************************
213 parse a kernel oplock message
214 ****************************************************************************/
215 static BOOL irix_kernel_oplock_parse(char *msg_start, int msg_len, SMB_INO_T *inode, SMB_DEV_T *dev)
217 /* Ensure that the msg length is correct. */
218 if(msg_len != KERNEL_OPLOCK_BREAK_MSG_LEN) {
219 DEBUG(0,("incorrect length for KERNEL_OPLOCK_BREAK_CMD (was %d, should be %d).\n",
220 msg_len, KERNEL_OPLOCK_BREAK_MSG_LEN));
224 memcpy((char *)inode, msg_start+KERNEL_OPLOCK_BREAK_INODE_OFFSET, sizeof(*inode));
225 memcpy((char *)dev, msg_start+KERNEL_OPLOCK_BREAK_DEV_OFFSET, sizeof(*dev));
227 DEBUG(5,("kernel oplock break request for file dev = %x, inode = %.0f\n",
228 (unsigned int)*dev, (double)*inode));
234 /****************************************************************************
235 set *maxfd to include oplock read pipe
236 ****************************************************************************/
237 static BOOL irix_oplock_msg_waiting(fd_set *fds)
239 if (oplock_pipe_read == -1) return False;
241 return FD_ISSET(oplock_pipe_read,fds);
245 /****************************************************************************
247 ****************************************************************************/
248 struct kernel_oplocks *irix_init_kernel_oplocks(void)
251 static struct kernel_oplocks koplocks;
253 if (!irix_oplocks_available()) return NULL;
256 DEBUG(0,("setup_kernel_oplock_pipe: Unable to create pipe. Error was %s\n",
261 oplock_pipe_read = pfd[0];
262 oplock_pipe_write = pfd[1];
264 koplocks.receive_message = irix_oplock_receive_message;
265 koplocks.set_oplock = irix_set_kernel_oplock;
266 koplocks.release_oplock = irix_release_kernel_oplock;
267 koplocks.parse_message = irix_kernel_oplock_parse;
268 koplocks.msg_waiting = irix_oplock_msg_waiting;
269 koplocks.notification_fd = oplock_pipe_read;
277 void oplock_irix_dummy(void) {}
278 #endif /* HAVE_KERNEL_OPLOCKS_IRIX */