r25055: Add file_id_string_tos
[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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #define DBGC_CLASS DBGC_LOCKING
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         set_effective_capability(KERNEL_OPLOCK_CAPABILITY);
39
40         slprintf(tmpname,sizeof(tmpname)-1, "%s/koplock.%d", lp_lockdir(),
41                  (int)sys_getpid());
42
43         if(pipe(pfd) != 0) {
44                 DEBUG(0,("check_kernel_oplocks: Unable to create pipe. Error "
45                          "was %s\n",
46                          strerror(errno) ));
47                 return False;
48         }
49
50         if((fd = sys_open(tmpname, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600)) < 0) {
51                 DEBUG(0,("check_kernel_oplocks: Unable to open temp test file "
52                          "%s. Error was %s\n",
53                          tmpname, strerror(errno) ));
54                 unlink( tmpname );
55                 close(pfd[0]);
56                 close(pfd[1]);
57                 return False;
58         }
59
60         unlink(tmpname);
61
62         if(sys_fcntl_long(fd, F_OPLKREG, pfd[1]) == -1) {
63                 DEBUG(0,("check_kernel_oplocks: Kernel oplocks are not "
64                          "available on this machine. Disabling kernel oplock "
65                          "support.\n" ));
66                 close(pfd[0]);
67                 close(pfd[1]);
68                 close(fd);
69                 return False;
70         }
71
72         if(sys_fcntl_long(fd, F_OPLKACK, OP_REVOKE) < 0 ) {
73                 DEBUG(0,("check_kernel_oplocks: Error when removing kernel "
74                          "oplock. Error was %s. Disabling kernel oplock "
75                          "support.\n", strerror(errno) ));
76                 close(pfd[0]);
77                 close(pfd[1]);
78                 close(fd);
79                 return False;
80         }
81
82         close(pfd[0]);
83         close(pfd[1]);
84         close(fd);
85
86         return True;
87 }
88
89 /****************************************************************************
90  * Deal with the IRIX kernel <--> smbd
91  * oplock break protocol.
92 ****************************************************************************/
93
94 static files_struct *irix_oplock_receive_message(fd_set *fds)
95 {
96         extern int smb_read_error;
97         oplock_stat_t os;
98         char dummy;
99         struct file_id fileid;
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          * NOTE: this doesn't work if any VFS modules overloads
143          *       the file_id_create() hook!
144          */
145
146         fileid = file_id_create_dev((SMB_DEV_T)os.os_dev,
147                                     (SMB_INO_T)os.os_ino);
148         if ((fsp = file_find_di_first(fileid)) == NULL) {
149                 DEBUG(0,("irix_oplock_receive_message: unable to find open "
150                          "file with dev = %x, inode = %.0f\n",
151                          (unsigned int)os.os_dev, (double)os.os_ino ));
152                 return NULL;
153         }
154      
155         DEBUG(5,("irix_oplock_receive_message: kernel oplock break request "
156                  "received for file_id %s gen_id = %ul",
157                  file_id_string_tos(&fsp->file_id),
158                  fsp->fh->gen_id ));
159
160         return fsp;
161 }
162
163 /****************************************************************************
164  Attempt to set an kernel oplock on a file.
165 ****************************************************************************/
166
167 static BOOL irix_set_kernel_oplock(files_struct *fsp, int oplock_type)
168 {
169         if (sys_fcntl_long(fsp->fh->fd, F_OPLKREG, oplock_pipe_write) == -1) {
170                 if(errno != EAGAIN) {
171                         DEBUG(0,("irix_set_kernel_oplock: Unable to get "
172                                  "kernel oplock on file %s, file_id %s "
173                                  "gen_id = %ul. Error was %s\n", 
174                                  fsp->fsp_name, file_id_string_tos(&fsp->file_id), 
175                                  fsp->fh->gen_id,
176                                  strerror(errno) ));
177                 } else {
178                         DEBUG(5,("irix_set_kernel_oplock: Refused oplock on "
179                                  "file %s, fd = %d, file_id = 5s, "
180                                  "gen_id = %ul. Another process had the file "
181                                  "open.\n",
182                                  fsp->fsp_name, fsp->fh->fd,
183                                  file_id_string_tos(&fsp->file_id),
184                                  fsp->fh->gen_id ));
185                 }
186                 return False;
187         }
188         
189         DEBUG(10,("irix_set_kernel_oplock: got kernel oplock on file %s, file_id = %s "
190                   "gen_id = %ul\n",
191                   fsp->fsp_name, file_id_string_tos(&fsp->file_id),
192                   fsp->fh->gen_id));
193
194         return True;
195 }
196
197 /****************************************************************************
198  Release a kernel oplock on a file.
199 ****************************************************************************/
200
201 static void irix_release_kernel_oplock(files_struct *fsp)
202 {
203         if (DEBUGLVL(10)) {
204                 /*
205                  * Check and print out the current kernel
206                  * oplock state of this file.
207                  */
208                 int state = sys_fcntl_long(fsp->fh->fd, F_OPLKACK, -1);
209                 dbgtext("irix_release_kernel_oplock: file %s, file_id = %s"
210                         "gen_id = %ul, has kernel oplock state "
211                         "of %x.\n", fsp->fsp_name, file_id_string_tos(&fsp->file_id),
212                         fsp->fh->gen_id, state );
213         }
214
215         /*
216          * Remove the kernel oplock on this file.
217          */
218         if(sys_fcntl_long(fsp->fh->fd, F_OPLKACK, OP_REVOKE) < 0) {
219                 if( DEBUGLVL( 0 )) {
220                         dbgtext("irix_release_kernel_oplock: Error when "
221                                 "removing kernel oplock on file " );
222                         dbgtext("%s, file_id = %s gen_id = %ul. "
223                                 "Error was %s\n",
224                                 fsp->fsp_name, file_id_string_tos(&fsp->file_id),
225                                 fsp->fh->gen_id,
226                                 strerror(errno) );
227                 }
228         }
229 }
230
231 /****************************************************************************
232  See if there is a message waiting in this fd set.
233  Note that fds MAY BE NULL ! If so we must do our own select.
234 ****************************************************************************/
235
236 static BOOL irix_oplock_msg_waiting(fd_set *fds)
237 {
238         int selrtn;
239         fd_set myfds;
240         struct timeval to;
241
242         if (oplock_pipe_read == -1)
243                 return False;
244
245         if (fds) {
246                 return FD_ISSET(oplock_pipe_read, fds);
247         }
248
249         /* Do a zero-time select. We just need to find out if there
250          * are any outstanding messages. We use sys_select_intr as
251          * we need to ignore any signals. */
252
253         FD_ZERO(&myfds);
254         FD_SET(oplock_pipe_read, &myfds);
255
256         to = timeval_set(0, 0);
257         selrtn = sys_select_intr(oplock_pipe_read+1,&myfds,NULL,NULL,&to);
258         return (selrtn == 1) ? True : False;
259 }
260
261 /****************************************************************************
262  Setup kernel oplocks.
263 ****************************************************************************/
264
265 struct kernel_oplocks *irix_init_kernel_oplocks(void) 
266 {
267         int pfd[2];
268         static struct kernel_oplocks koplocks;
269
270         if (!irix_oplocks_available())
271                 return NULL;
272
273         if(pipe(pfd) != 0) {
274                 DEBUG(0,("setup_kernel_oplock_pipe: Unable to create pipe. "
275                          "Error was %s\n", strerror(errno) ));
276                 return False;
277         }
278
279         oplock_pipe_read = pfd[0];
280         oplock_pipe_write = pfd[1];
281
282         koplocks.receive_message = irix_oplock_receive_message;
283         koplocks.set_oplock = irix_set_kernel_oplock;
284         koplocks.release_oplock = irix_release_kernel_oplock;
285         koplocks.msg_waiting = irix_oplock_msg_waiting;
286         koplocks.notification_fd = oplock_pipe_read;
287
288         return &koplocks;
289 }
290 #else
291  void oplock_irix_dummy(void);
292  void oplock_irix_dummy(void) {}
293 #endif /* HAVE_KERNEL_OPLOCKS_IRIX */