This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[samba.git] / source / smbd / blocking.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Blocking Locking functions
4    Copyright (C) Jeremy Allison 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 extern char *OutBuffer;
24
25 /****************************************************************************
26  This is the structure to queue to implement blocking locks.
27  notify. It consists of the requesting SMB and the expiry time.
28 *****************************************************************************/
29
30 typedef struct {
31   ubi_slNode msg_next;
32   int com_type;
33   files_struct *fsp;
34   time_t expire_time;
35   int lock_num;
36   char *inbuf;
37   int length;
38 } blocking_lock_record;
39
40 static ubi_slList blocking_lock_queue = { NULL, (ubi_slNodePtr)&blocking_lock_queue, 0};
41
42 /****************************************************************************
43  Destructor for the above structure.
44 ****************************************************************************/
45
46 static void free_blocking_lock_record(blocking_lock_record *blr)
47 {
48   SAFE_FREE(blr->inbuf);
49   SAFE_FREE(blr);
50 }
51
52 /****************************************************************************
53  Get the files_struct given a particular queued SMB.
54 *****************************************************************************/
55
56 static files_struct *get_fsp_from_pkt(char *inbuf)
57 {
58   switch(CVAL(inbuf,smb_com)) {
59   case SMBlock:
60   case SMBlockread:
61     return file_fsp(inbuf,smb_vwv0);
62   case SMBlockingX:
63     return file_fsp(inbuf,smb_vwv2);
64   default:
65     DEBUG(0,("get_fsp_from_pkt: PANIC - unknown type on blocking lock queue - exiting.!\n"));
66     exit_server("PANIC - unknown type on blocking lock queue");
67   }
68   return NULL; /* Keep compiler happy. */
69 }
70
71 /****************************************************************************
72  Determine if this is a secondary element of a chained SMB.
73   **************************************************************************/
74
75 static BOOL in_chained_smb(void)
76 {
77   return (chain_size != 0);
78 }
79
80 /****************************************************************************
81  Function to push a blocking lock request onto the lock queue.
82 ****************************************************************************/
83
84 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout, int lock_num)
85 {
86   blocking_lock_record *blr;
87
88   if(in_chained_smb() ) {
89     DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
90     return False;
91   }
92
93   /*
94    * Now queue an entry on the blocking lock queue. We setup
95    * the expiration time here.
96    */
97
98   if((blr = (blocking_lock_record *)malloc(sizeof(blocking_lock_record))) == NULL) {
99     DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
100     return False;
101   }
102
103   if((blr->inbuf = (char *)malloc(length)) == NULL) {
104     DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
105     SAFE_FREE(blr);
106     return False;
107   }
108
109   blr->com_type = CVAL(inbuf,smb_com);
110   blr->fsp = get_fsp_from_pkt(inbuf);
111   blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
112   blr->lock_num = lock_num;
113   memcpy(blr->inbuf, inbuf, length);
114   blr->length = length;
115
116   ubi_slAddTail(&blocking_lock_queue, blr);
117
118
119   DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
120 for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
121         blr->fsp->fnum, blr->fsp->fsp_name ));
122
123   return True;
124 }
125
126 /****************************************************************************
127  Return a smd with a given size.
128 *****************************************************************************/
129
130 static void send_blocking_reply(char *outbuf, int outsize)
131 {
132         if(outsize > 4)
133                 smb_setlen(outbuf,outsize - 4);
134
135         if (!send_smb(smbd_server_fd(),outbuf))
136                 exit_server("send_blocking_reply: send_smb failed.");
137 }
138
139 /****************************************************************************
140  Return a lockingX success SMB.
141 *****************************************************************************/
142
143 static void reply_lockingX_success(blocking_lock_record *blr)
144 {
145   char *outbuf = OutBuffer;
146   int bufsize = BUFFER_SIZE;
147   char *inbuf = blr->inbuf;
148   int outsize = 0;
149
150   construct_reply_common(inbuf, outbuf);
151   set_message(outbuf,2,0,True);
152
153   /*
154    * As this message is a lockingX call we must handle
155    * any following chained message correctly.
156    * This is normally handled in construct_reply(),
157    * but as that calls switch_message, we can't use
158    * that here and must set up the chain info manually.
159    */
160
161   outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
162
163   outsize += chain_size;
164
165   send_blocking_reply(outbuf,outsize);
166 }
167
168 /****************************************************************************
169  Return a generic lock fail error blocking call.
170 *****************************************************************************/
171
172 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
173 {
174         char *outbuf = OutBuffer;
175         char *inbuf = blr->inbuf;
176         construct_reply_common(inbuf, outbuf);
177
178         /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
179            FILE_LOCK_CONFLICT! (tridge) */
180         if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
181                 status = NT_STATUS_FILE_LOCK_CONFLICT;
182         }
183
184         ERROR_NT(status);
185         if (!send_smb(smbd_server_fd(),outbuf))
186                 exit_server("generic_blocking_lock_error: send_smb failed.");
187 }
188
189 /****************************************************************************
190  Return a lock fail error for a lockingX call. Undo all the locks we have 
191  obtained first.
192 *****************************************************************************/
193
194 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
195 {
196         char *inbuf = blr->inbuf;
197         files_struct *fsp = blr->fsp;
198         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
199         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
200         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
201         uint16 lock_pid;
202         unsigned char locktype = CVAL(inbuf,smb_vwv3);
203         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
204         char *data;
205         int i;
206
207         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
208         
209         /* 
210          * Data now points at the beginning of the list
211          * of smb_lkrng structs.
212          */
213
214         /*
215          * Ensure we don't do a remove on the lock that just failed,
216          * as under POSIX rules, if we have a lock already there, we
217          * will delete it (and we shouldn't) .....
218          */
219         
220         for(i = blr->lock_num - 1; i >= 0; i--) {
221                 BOOL err;
222                 
223                 lock_pid = get_lock_pid( data, i, large_file_format);
224                 count = get_lock_count( data, i, large_file_format);
225                 offset = get_lock_offset( data, i, large_file_format, &err);
226                 
227                 /*
228                  * We know err cannot be set as if it was the lock
229                  * request would never have been queued. JRA.
230                  */
231                 
232                 do_unlock(fsp,conn,lock_pid,count,offset);
233         }
234         
235         generic_blocking_lock_error(blr, status);
236 }
237
238 /****************************************************************************
239  Return a lock fail error.
240 *****************************************************************************/
241
242 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
243 {
244         switch(blr->com_type) {
245         case SMBlock:
246         case SMBlockread:
247                 generic_blocking_lock_error(blr, status);
248                 break;
249         case SMBlockingX:
250                 reply_lockingX_error(blr, status);
251                 break;
252         default:
253                 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
254                 exit_server("PANIC - unknown type on blocking lock queue");
255         }
256 }
257
258 /****************************************************************************
259  Attempt to finish off getting all pending blocking locks for a lockread call.
260  Returns True if we want to be removed from the list.
261 *****************************************************************************/
262
263 static BOOL process_lockread(blocking_lock_record *blr)
264 {
265         char *outbuf = OutBuffer;
266         char *inbuf = blr->inbuf;
267         ssize_t nread = -1;
268         char *data, *p;
269         int outsize = 0;
270         SMB_OFF_T startpos;
271         size_t numtoread;
272         NTSTATUS status;
273         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
274         files_struct *fsp = blr->fsp;
275
276         numtoread = SVAL(inbuf,smb_vwv1);
277         startpos = IVAL(inbuf,smb_vwv2);
278         
279         numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
280         data = smb_buf(outbuf) + 3;
281  
282         status = do_lock_spin( fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtoread, 
283                           (SMB_BIG_UINT)startpos, READ_LOCK);
284         if (NT_STATUS_V(status)) {
285                 if ((errno != EACCES) && (errno != EAGAIN)) {
286                         /*
287                          * We have other than a "can't get lock" POSIX
288                          * error. Send an error.
289                          * Return True so we get dequeued.
290                          */
291                         generic_blocking_lock_error(blr, status);
292                         return True;
293                 }
294
295                 /*
296                  * Still waiting for lock....
297                  */
298                 
299                 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
300                           fsp->fsp_name));
301                 return False;
302         }
303
304         nread = read_file(fsp,data,startpos,numtoread);
305
306         if (nread < 0) {
307                 generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
308                 return True;
309         }
310         
311         construct_reply_common(inbuf, outbuf);
312         outsize = set_message(outbuf,5,0,True);
313         
314         outsize += nread;
315         SSVAL(outbuf,smb_vwv0,nread);
316         SSVAL(outbuf,smb_vwv5,nread+3);
317         p = smb_buf(outbuf);
318         *p++ = 1;
319         SSVAL(p,0,nread); p += 2;
320         set_message_end(outbuf, p+nread);
321         
322         DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
323                    fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
324         
325         send_blocking_reply(outbuf,outsize);
326         return True;
327 }
328
329 /****************************************************************************
330  Attempt to finish off getting all pending blocking locks for a lock call.
331  Returns True if we want to be removed from the list.
332 *****************************************************************************/
333
334 static BOOL process_lock(blocking_lock_record *blr)
335 {
336         char *outbuf = OutBuffer;
337         char *inbuf = blr->inbuf;
338         int outsize;
339         SMB_OFF_T count = 0, offset = 0;
340         NTSTATUS status;
341         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
342         files_struct *fsp = blr->fsp;
343
344         count = IVAL(inbuf,smb_vwv1);
345         offset = IVAL(inbuf,smb_vwv3);
346
347         errno = 0;
348         status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)count, 
349                          (SMB_BIG_UINT)offset, WRITE_LOCK);
350         if (NT_STATUS_IS_ERR(status)) {
351                 if((errno != EACCES) && (errno != EAGAIN)) {
352                         /*
353                          * We have other than a "can't get lock" POSIX
354                          * error. Send an error.
355                          * Return True so we get dequeued.
356                          */
357                         
358                         blocking_lock_reply_error(blr, status);
359                         return True;
360                 }
361                 /*
362                  * Still can't get the lock - keep waiting.
363                  */
364                 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
365                           fsp->fsp_name));
366                 return False;
367         }
368
369         /*
370          * Success - we got the lock.
371          */
372         
373         DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
374                  fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
375         
376         construct_reply_common(inbuf, outbuf);
377         outsize = set_message(outbuf,0,0,True);
378         send_blocking_reply(outbuf,outsize);
379         return True;
380 }
381
382 /****************************************************************************
383  Attempt to finish off getting all pending blocking locks for a lockingX call.
384  Returns True if we want to be removed from the list.
385 *****************************************************************************/
386
387 static BOOL process_lockingX(blocking_lock_record *blr)
388 {
389         char *inbuf = blr->inbuf;
390         unsigned char locktype = CVAL(inbuf,smb_vwv3);
391         files_struct *fsp = blr->fsp;
392         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
393         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
394         uint16 num_locks = SVAL(inbuf,smb_vwv7);
395         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
396         uint16 lock_pid;
397         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
398         char *data;
399         NTSTATUS status = NT_STATUS_OK;
400
401         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
402
403         /* 
404          * Data now points at the beginning of the list
405          * of smb_lkrng structs.
406          */
407         
408         for(; blr->lock_num < num_locks; blr->lock_num++) {
409                 BOOL err;
410
411                 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
412                 count = get_lock_count( data, blr->lock_num, large_file_format);
413                 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
414                 
415                 /*
416                  * We know err cannot be set as if it was the lock
417                  * request would never have been queued. JRA.
418                  */
419                 errno = 0;
420                 status = do_lock_spin(fsp,conn,lock_pid,count,offset, 
421                                  ((locktype & 1) ? READ_LOCK : WRITE_LOCK));
422                 if (NT_STATUS_IS_ERR(status)) break;
423         }
424
425         if(blr->lock_num == num_locks) {
426                 /*
427                  * Success - we got all the locks.
428                  */
429                 
430                 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
431                          fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
432
433                 reply_lockingX_success(blr);
434                 return True;
435         } else if ((errno != EACCES) && (errno != EAGAIN)) {
436                 /*
437                  * We have other than a "can't get lock" POSIX
438                  * error. Free any locks we had and return an error.
439                  * Return True so we get dequeued.
440                  */
441                 
442                 blocking_lock_reply_error(blr, status);
443                 return True;
444         }
445
446         /*
447          * Still can't get all the locks - keep waiting.
448          */
449         
450         DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
451 Waiting....\n", 
452                   blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
453         
454         return False;
455 }
456
457 /****************************************************************************
458  Process a blocking lock SMB.
459  Returns True if we want to be removed from the list.
460 *****************************************************************************/
461
462 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
463 {
464   switch(blr->com_type) {
465   case SMBlock:
466     return process_lock(blr);
467   case SMBlockread:
468     return process_lockread(blr);
469   case SMBlockingX:
470     return process_lockingX(blr);
471   default:
472     DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
473     exit_server("PANIC - unknown type on blocking lock queue");
474   }
475   return False; /* Keep compiler happy. */
476 }
477
478 /****************************************************************************
479  Delete entries by fnum from the blocking lock pending queue.
480 *****************************************************************************/
481
482 void remove_pending_lock_requests_by_fid(files_struct *fsp)
483 {
484   blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
485   blocking_lock_record *prev = NULL;
486
487   while(blr != NULL) {
488     if(blr->fsp->fnum == fsp->fnum) {
489
490       DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
491 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
492
493       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
494       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
495       continue;
496     }
497
498     prev = blr;
499     blr = (blocking_lock_record *)ubi_slNext(blr);
500   }
501 }
502
503 /****************************************************************************
504  Delete entries by mid from the blocking lock pending queue. Always send reply.
505 *****************************************************************************/
506
507 void remove_pending_lock_requests_by_mid(int mid)
508 {
509   blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
510   blocking_lock_record *prev = NULL;
511
512   while(blr != NULL) {
513     if(SVAL(blr->inbuf,smb_mid) == mid) {
514       files_struct *fsp = blr->fsp;
515
516       DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
517 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
518
519       blocking_lock_reply_error(blr,NT_STATUS_CANCELLED);
520       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
521       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
522       continue;
523     }
524
525     prev = blr;
526     blr = (blocking_lock_record *)ubi_slNext(blr);
527   }
528 }
529
530 /****************************************************************************
531  Return True if the blocking lock queue has entries.
532 *****************************************************************************/
533
534 BOOL blocking_locks_pending(void)
535 {
536   blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
537   return (blr == NULL ? False : True);
538 }
539
540 /****************************************************************************
541  Process the blocking lock queue. Note that this is only called as root.
542 *****************************************************************************/
543
544 void process_blocking_lock_queue(time_t t)
545 {
546   blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
547   blocking_lock_record *prev = NULL;
548
549   if(blr == NULL)
550     return;
551
552   /*
553    * Go through the queue and see if we can get any of the locks.
554    */
555
556   while(blr != NULL) {
557     connection_struct *conn = NULL;
558     uint16 vuid;
559     files_struct *fsp = NULL;
560
561     /*
562      * Ensure we don't have any old chain_fsp values
563      * sitting around....
564      */
565     chain_size = 0;
566     file_chain_reset();
567     fsp = blr->fsp;
568
569     conn = conn_find(SVAL(blr->inbuf,smb_tid));
570     vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
571                   SVAL(blr->inbuf,smb_uid);
572
573     DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
574           fsp->fnum, fsp->fsp_name ));
575
576     if((blr->expire_time != -1) && (blr->expire_time > t)) {
577       /*
578        * Lock expired - throw away all previously
579        * obtained locks and return lock error.
580        */
581       DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
582           fsp->fnum, fsp->fsp_name ));
583
584       blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
585       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
586       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
587       continue;
588     }
589
590     if(!change_to_user(conn,vuid)) {
591       DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
592             vuid ));
593       /*
594        * Remove the entry and return an error to the client.
595        */
596       blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
597       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
598       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
599       continue;
600     }
601
602     if(!set_current_service(conn,True)) {
603       DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
604       /*
605        * Remove the entry and return an error to the client.
606        */
607       blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
608       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
609       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
610       change_to_root_user();
611       continue;
612     }
613
614     /*
615      * Go through the remaining locks and try and obtain them.
616      * The call returns True if all locks were obtained successfully
617      * and False if we still need to wait.
618      */
619
620     if(blocking_lock_record_process(blr)) {
621       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
622       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
623       change_to_root_user();
624       continue;
625     }
626
627     change_to_root_user();
628
629     /*
630      * Move to the next in the list.
631      */
632     prev = blr;
633     blr = (blocking_lock_record *)ubi_slNext(blr);
634   }
635 }