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