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