Initial import
[samba] / source / smbd / oplock_irix.c
1 /*
2    Unix SMB/CIFS implementation.
3    IRIX kernel oplock processing
4    Copyright (C) Andrew Tridgell 1992-1998
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23 #if HAVE_KERNEL_OPLOCKS_IRIX
24
25 static int oplock_pipe_write = -1;
26 static int oplock_pipe_read = -1;
27
28 /****************************************************************************
29  Test to see if IRIX kernel oplocks work.
30 ****************************************************************************/
31
32 static BOOL irix_oplocks_available(void)
33 {
34         int fd;
35         int pfd[2];
36         pstring tmpname;
37
38         oplock_set_capability(True, False);
39
40         slprintf(tmpname,sizeof(tmpname)-1, "%s/koplock.%d", lp_lockdir(), (int)sys_getpid());
41
42         if(pipe(pfd) != 0) {
43                 DEBUG(0,("check_kernel_oplocks: Unable to create pipe. Error was %s\n",
44                          strerror(errno) ));
45                 return False;
46         }
47
48         if((fd = sys_open(tmpname, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600)) < 0) {
49                 DEBUG(0,("check_kernel_oplocks: Unable to open temp test file %s. Error was %s\n",
50                          tmpname, strerror(errno) ));
51                 unlink( tmpname );
52                 close(pfd[0]);
53                 close(pfd[1]);
54                 return False;
55         }
56
57         unlink(tmpname);
58
59         if(sys_fcntl_long(fd, F_OPLKREG, pfd[1]) == -1) {
60                 DEBUG(0,("check_kernel_oplocks: Kernel oplocks are not available on this machine. \
61 Disabling kernel oplock support.\n" ));
62                 close(pfd[0]);
63                 close(pfd[1]);
64                 close(fd);
65                 return False;
66         }
67
68         if(sys_fcntl_long(fd, F_OPLKACK, OP_REVOKE) < 0 ) {
69                 DEBUG(0,("check_kernel_oplocks: Error when removing kernel oplock. Error was %s. \
70 Disabling kernel oplock support.\n", strerror(errno) ));
71                 close(pfd[0]);
72                 close(pfd[1]);
73                 close(fd);
74                 return False;
75         }
76
77         close(pfd[0]);
78         close(pfd[1]);
79         close(fd);
80
81         return True;
82 }
83
84 /****************************************************************************
85  * Deal with the IRIX kernel <--> smbd
86  * oplock break protocol.
87 ****************************************************************************/
88
89 static files_struct *irix_oplock_receive_message(fd_set *fds)
90 {
91         extern int smb_read_error;
92         oplock_stat_t os;
93         char dummy;
94         files_struct *fsp;
95
96         /* Ensure we only get one call per select fd set. */
97         FD_CLR(oplock_pipe_read, fds);
98
99         /*
100          * Read one byte of zero to clear the
101          * kernel break notify message.
102          */
103
104         if(read(oplock_pipe_read, &dummy, 1) != 1) {
105                 DEBUG(0,("irix_oplock_receive_message: read of kernel "
106                          "notification failed. Error was %s.\n",
107                          strerror(errno) ));
108                 smb_read_error = READ_ERROR;
109                 return NULL;
110         }
111
112         /*
113          * Do a query to get the
114          * device and inode of the file that has the break
115          * request outstanding.
116          */
117
118         if(sys_fcntl_ptr(oplock_pipe_read, F_OPLKSTAT, &os) < 0) {
119                 DEBUG(0,("irix_oplock_receive_message: fcntl of kernel "
120                          "notification failed. Error was %s.\n",
121                          strerror(errno) ));
122                 if(errno == EAGAIN) {
123                         /*
124                          * Duplicate kernel break message - ignore.
125                          */
126                         return NULL;
127                 }
128                 smb_read_error = READ_ERROR;
129                 return NULL;
130         }
131
132         /*
133          * We only have device and inode info here - we have to guess that this
134          * is the first fsp open with this dev,ino pair.
135          */
136
137         if ((fsp = file_find_di_first((SMB_DEV_T)os.os_dev,
138                                       (SMB_INO_T)os.os_ino)) == NULL) {
139                 DEBUG(0,("irix_oplock_receive_message: unable to find open "
140                          "file with dev = %x, inode = %.0f\n",
141                          (unsigned int)os.os_dev, (double)os.os_ino ));
142                 return NULL;
143         }
144      
145         DEBUG(5,("irix_oplock_receive_message: kernel oplock break request "
146                  "received for dev = %x, inode = %.0f\n, file_id = %ul",
147                  (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id ));
148
149         return fsp;
150 }
151
152 /****************************************************************************
153  Attempt to set an kernel oplock on a file.
154 ****************************************************************************/
155
156 static BOOL irix_set_kernel_oplock(files_struct *fsp, int oplock_type)
157 {
158         if (sys_fcntl_long(fsp->fh->fd, F_OPLKREG, oplock_pipe_write) == -1) {
159                 if(errno != EAGAIN) {
160                         DEBUG(0,("irix_set_kernel_oplock: Unable to get kernel oplock on file %s, dev = %x, \
161 inode = %.0f, file_id = %ul. Error was %s\n", 
162                                  fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id,
163                                  strerror(errno) ));
164                 } else {
165                         DEBUG(5,("irix_set_kernel_oplock: Refused oplock on file %s, fd = %d, dev = %x, \
166 inode = %.0f, file_id = %ul. Another process had the file open.\n",
167                                  fsp->fsp_name, fsp->fh->fd, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id ));
168                 }
169                 return False;
170         }
171         
172         DEBUG(10,("irix_set_kernel_oplock: got kernel oplock on file %s, dev = %x, inode = %.0f, file_id = %ul\n",
173                   fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id));
174
175         return True;
176 }
177
178 /****************************************************************************
179  Release a kernel oplock on a file.
180 ****************************************************************************/
181
182 static void irix_release_kernel_oplock(files_struct *fsp)
183 {
184         if (DEBUGLVL(10)) {
185                 /*
186                  * Check and print out the current kernel
187                  * oplock state of this file.
188                  */
189                 int state = sys_fcntl_long(fsp->fh->fd, F_OPLKACK, -1);
190                 dbgtext("irix_release_kernel_oplock: file %s, dev = %x, inode = %.0f file_id = %ul, has kernel \
191 oplock state of %x.\n", fsp->fsp_name, (unsigned int)fsp->dev,
192                         (double)fsp->inode, fsp->file_id, state );
193         }
194
195         /*
196          * Remove the kernel oplock on this file.
197          */
198         if(sys_fcntl_long(fsp->fh->fd, F_OPLKACK, OP_REVOKE) < 0) {
199                 if( DEBUGLVL( 0 )) {
200                         dbgtext("irix_release_kernel_oplock: Error when removing kernel oplock on file " );
201                         dbgtext("%s, dev = %x, inode = %.0f, file_id = %ul. Error was %s\n",
202                                 fsp->fsp_name, (unsigned int)fsp->dev, 
203                                 (double)fsp->inode, fsp->file_id, strerror(errno) );
204                 }
205         }
206 }
207
208 /****************************************************************************
209  Set *maxfd to include oplock read pipe.
210  Note that fds MAY BE NULL ! If so we must do our own select.
211 ****************************************************************************/
212
213 static BOOL irix_oplock_msg_waiting(fd_set *fds)
214 {
215         int maxfd, selrtn;
216         fd_set myfds;
217         struct timeval to;
218
219         if (oplock_pipe_read == -1)
220                 return False;
221
222         if (fds) {
223                 return FD_ISSET(oplock_pipe_read, fds);
224         }
225
226         /* Do a zero-time select. We just need to find out if there
227          * are any outstanding messages. We use sys_select_intr as
228          * we need to ignore any signals. */
229
230         FD_ZERO(&myfds);
231         FD_SET(oplock_pipe_read, &myfds);
232
233         to = timeval_set(0, 0);
234         selrtn = sys_select_intr(oplock_pipe_read+1,&myfds,NULL,NULL,&to);
235         return (selrtn == 1) ? True : False;
236 }
237
238 /****************************************************************************
239  Setup kernel oplocks.
240 ****************************************************************************/
241
242 struct kernel_oplocks *irix_init_kernel_oplocks(void) 
243 {
244         int pfd[2];
245         static struct kernel_oplocks koplocks;
246
247         if (!irix_oplocks_available())
248                 return NULL;
249
250         if(pipe(pfd) != 0) {
251                 DEBUG(0,("setup_kernel_oplock_pipe: Unable to create pipe. Error was %s\n",
252                          strerror(errno) ));
253                 return False;
254         }
255
256         oplock_pipe_read = pfd[0];
257         oplock_pipe_write = pfd[1];
258
259         koplocks.receive_message = irix_oplock_receive_message;
260         koplocks.set_oplock = irix_set_kernel_oplock;
261         koplocks.release_oplock = irix_release_kernel_oplock;
262         koplocks.msg_waiting = irix_oplock_msg_waiting;
263         koplocks.notification_fd = oplock_pipe_read;
264
265         return &koplocks;
266 }
267 #else
268  void oplock_irix_dummy(void) {}
269 #endif /* HAVE_KERNEL_OPLOCKS_IRIX */