preparing for release of 3.0-alpha11
[Samba/ekacnet.git] / source / smbd / oplock_irix.c
blob1052046175314482485e1491ca92e4a49e3be9eb
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.x
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.
22 #include "includes.h"
24 #if HAVE_KERNEL_OPLOCKS_IRIX
26 static int oplock_pipe_write = -1;
27 static int oplock_pipe_read = -1;
29 /****************************************************************************
30 Test to see if IRIX kernel oplocks work.
31 ****************************************************************************/
33 static BOOL irix_oplocks_available(void)
35 int fd;
36 int pfd[2];
37 pstring tmpname;
39 oplock_set_capability(True, False);
41 slprintf(tmpname,sizeof(tmpname)-1, "%s/koplock.%d", lp_lockdir(), (int)sys_getpid());
43 if(pipe(pfd) != 0) {
44 DEBUG(0,("check_kernel_oplocks: Unable to create pipe. Error was %s\n",
45 strerror(errno) ));
46 return False;
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) ));
52 unlink( tmpname );
53 close(pfd[0]);
54 close(pfd[1]);
55 return False;
58 unlink(tmpname);
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" ));
63 close(pfd[0]);
64 close(pfd[1]);
65 close(fd);
66 return False;
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) ));
72 close(pfd[0]);
73 close(pfd[1]);
74 close(fd);
75 return False;
78 close(pfd[0]);
79 close(pfd[1]);
80 close(fd);
82 return True;
85 /****************************************************************************
86 * Deal with the IRIX kernel <--> smbd
87 * oplock break protocol.
88 ****************************************************************************/
90 static BOOL irix_oplock_receive_message(fd_set *fds, char *buffer, int buffer_len)
92 extern int smb_read_error;
93 oplock_stat_t os;
94 char dummy;
95 files_struct *fsp;
98 * Read one byte of zero to clear the
99 * kernel break notify message.
102 if(read(oplock_pipe_read, &dummy, 1) != 1) {
103 DEBUG(0,("receive_local_message: read of kernel notification failed. \
104 Error was %s.\n", strerror(errno) ));
105 smb_read_error = READ_ERROR;
106 return False;
110 * Do a query to get the
111 * device and inode of the file that has the break
112 * request outstanding.
115 if(fcntl(oplock_pipe_read, F_OPLKSTAT, &os) < 0) {
116 DEBUG(0,("receive_local_message: fcntl of kernel notification failed. \
117 Error was %s.\n", strerror(errno) ));
118 if(errno == EAGAIN) {
120 * Duplicate kernel break message - ignore.
122 memset(buffer, '\0', KERNEL_OPLOCK_BREAK_MSG_LEN);
123 return True;
125 smb_read_error = READ_ERROR;
126 return False;
130 * We only have device and inode info here - we have to guess that this
131 * is the first fsp open with this dev,ino pair.
134 if ((fsp = file_find_di_first((SMB_DEV_T)os.os_dev, (SMB_INO_T)os.os_ino)) == NULL) {
135 DEBUG(0,("receive_local_message: unable to find open file with dev = %x, inode = %.0f\n",
136 (unsigned int)os.os_dev, (double)os.os_ino ));
137 return False;
140 DEBUG(5,("receive_local_message: kernel oplock break request received for \
141 dev = %x, inode = %.0f\n, file_id = %ul", (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id ));
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));
159 return True;
162 /****************************************************************************
163 Attempt to set an kernel oplock on a file.
164 ****************************************************************************/
166 static BOOL irix_set_kernel_oplock(files_struct *fsp, int oplock_type)
168 if (fcntl(fsp->fd, F_OPLKREG, oplock_pipe_write) == -1) {
169 if(errno != EAGAIN) {
170 DEBUG(0,("set_file_oplock: Unable to get kernel oplock on file %s, dev = %x, \
171 inode = %.0f, file_id = %ul. Error was %s\n",
172 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id,
173 strerror(errno) ));
174 } else {
175 DEBUG(5,("set_file_oplock: Refused oplock on file %s, fd = %d, dev = %x, \
176 inode = %.0f, file_id = %ul. Another process had the file open.\n",
177 fsp->fsp_name, fsp->fd, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id ));
179 return False;
182 DEBUG(10,("set_file_oplock: got kernel oplock on file %s, dev = %x, inode = %.0f, file_id = %ul\n",
183 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id));
185 return True;
188 /****************************************************************************
189 Release a kernel oplock on a file.
190 ****************************************************************************/
192 static void irix_release_kernel_oplock(files_struct *fsp)
194 if (DEBUGLVL(10)) {
196 * Check and print out the current kernel
197 * oplock state of this file.
199 int state = fcntl(fsp->fd, F_OPLKACK, -1);
200 dbgtext("release_kernel_oplock: file %s, dev = %x, inode = %.0f file_id = %ul, 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(fcntl(fsp->fd, F_OPLKACK, OP_REVOKE) < 0) {
209 if( DEBUGLVL( 0 )) {
210 dbgtext("release_kernel_oplock: Error when removing kernel oplock on file " );
211 dbgtext("%s, dev = %x, inode = %.0f, file_id = %ul. 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 irix_kernel_oplock_parse(char *msg_start, int msg_len,
223 SMB_INO_T *inode, 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));
229 return False;
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(5,("kernel oplock break request for file dev = %x, inode = %.0f, file_id = %ul\n",
237 (unsigned int)*dev, (double)*inode, *file_id));
239 return True;
242 /****************************************************************************
243 Set *maxfd to include oplock read pipe.
244 ****************************************************************************/
246 static BOOL irix_oplock_msg_waiting(fd_set *fds)
248 if (oplock_pipe_read == -1)
249 return False;
251 return FD_ISSET(oplock_pipe_read,fds);
254 /****************************************************************************
255 Setup kernel oplocks.
256 ****************************************************************************/
258 struct kernel_oplocks *irix_init_kernel_oplocks(void)
260 int pfd[2];
261 static struct kernel_oplocks koplocks;
263 if (!irix_oplocks_available())
264 return NULL;
266 if(pipe(pfd) != 0) {
267 DEBUG(0,("setup_kernel_oplock_pipe: Unable to create pipe. Error was %s\n",
268 strerror(errno) ));
269 return False;
272 oplock_pipe_read = pfd[0];
273 oplock_pipe_write = pfd[1];
275 koplocks.receive_message = irix_oplock_receive_message;
276 koplocks.set_oplock = irix_set_kernel_oplock;
277 koplocks.release_oplock = irix_release_kernel_oplock;
278 koplocks.parse_message = irix_kernel_oplock_parse;
279 koplocks.msg_waiting = irix_oplock_msg_waiting;
280 koplocks.notification_fd = oplock_pipe_read;
282 return &koplocks;
284 #else
285 void oplock_irix_dummy(void) {}
286 #endif /* HAVE_KERNEL_OPLOCKS_IRIX */