r23620: Convert set_nt_acl to return NTSTATUS. Also fix the chown
[tprouty/samba.git] / source / smbd / blocking.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Blocking Locking functions
4    Copyright (C) Jeremy Allison 1998-2003
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 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_LOCKING
24
25 extern int max_send;
26
27 /****************************************************************************
28  This is the structure to queue to implement blocking locks.
29  notify. It consists of the requesting SMB and the expiry time.
30 *****************************************************************************/
31
32 typedef struct _blocking_lock_record {
33         struct _blocking_lock_record *next;
34         struct _blocking_lock_record *prev;
35         int com_type;
36         files_struct *fsp;
37         struct timeval expire_time;
38         int lock_num;
39         SMB_BIG_UINT offset;
40         SMB_BIG_UINT count;
41         uint32 lock_pid;
42         uint32 blocking_pid; /* PID that blocks us. */
43         enum brl_flavour lock_flav;
44         enum brl_type lock_type;
45         char *inbuf;
46         int length;
47 } blocking_lock_record;
48
49 /* dlink list we store pending lock records on. */
50 static blocking_lock_record *blocking_lock_queue;
51
52 /* dlink list we move cancelled lock records onto. */
53 static blocking_lock_record *blocking_lock_cancelled_queue;
54
55 /* The event that makes us process our blocking lock queue */
56 static struct timed_event *brl_timeout;
57
58 /****************************************************************************
59  Destructor for the above structure.
60 ****************************************************************************/
61
62 static void free_blocking_lock_record(blocking_lock_record *blr)
63 {
64         SAFE_FREE(blr->inbuf);
65         SAFE_FREE(blr);
66 }
67
68 /****************************************************************************
69  Determine if this is a secondary element of a chained SMB.
70   **************************************************************************/
71
72 static BOOL in_chained_smb(void)
73 {
74         return (chain_size != 0);
75 }
76
77 static void received_unlock_msg(struct messaging_context *msg,
78                                 void *private_data,
79                                 uint32_t msg_type,
80                                 struct server_id server_id,
81                                 DATA_BLOB *data);
82 static void process_blocking_lock_queue(void);
83
84 static void brl_timeout_fn(struct event_context *event_ctx,
85                            struct timed_event *te,
86                            const struct timeval *now,
87                            void *private_data)
88 {
89         SMB_ASSERT(brl_timeout == te);
90         TALLOC_FREE(brl_timeout);
91
92         change_to_root_user();  /* TODO: Possibly run all timed events as
93                                  * root */
94
95         process_blocking_lock_queue();
96 }
97
98 /****************************************************************************
99  After a change to blocking_lock_queue, recalculate the timed_event for the
100  next processing.
101 ****************************************************************************/
102
103 static BOOL recalc_brl_timeout(void)
104 {
105         blocking_lock_record *brl;
106         struct timeval next_timeout;
107
108         TALLOC_FREE(brl_timeout);
109
110         next_timeout = timeval_zero();  
111
112         for (brl = blocking_lock_queue; brl; brl = brl->next) {
113                 if (timeval_is_zero(&brl->expire_time)) {
114                         /*
115                          * If we're blocked on pid 0xFFFFFFFF this is
116                          * a POSIX lock, so calculate a timeout of
117                          * 10 seconds into the future.
118                          */
119                         if (brl->blocking_pid == 0xFFFFFFFF) {
120                                 struct timeval psx_to = timeval_current_ofs(10, 0);
121                                 next_timeout = timeval_min(&next_timeout, &psx_to);
122                         }
123
124                         continue;
125                 }
126
127                 if (timeval_is_zero(&next_timeout)) {
128                         next_timeout = brl->expire_time;
129                 }
130                 else {
131                         next_timeout = timeval_min(&next_timeout,
132                                                    &brl->expire_time);
133                 }
134         }
135
136         if (timeval_is_zero(&next_timeout)) {
137                 return True;
138         }
139
140         if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
141                                             next_timeout, "brl_timeout",
142                                             brl_timeout_fn, NULL))) {
143                 return False;
144         }
145
146         return True;
147 }
148
149
150 /****************************************************************************
151  Function to push a blocking lock request onto the lock queue.
152 ****************************************************************************/
153
154 BOOL push_blocking_lock_request( struct byte_range_lock *br_lck,
155                 char *inbuf, int length,
156                 files_struct *fsp,
157                 int lock_timeout,
158                 int lock_num,
159                 uint32 lock_pid,
160                 enum brl_type lock_type,
161                 enum brl_flavour lock_flav,
162                 SMB_BIG_UINT offset,
163                 SMB_BIG_UINT count,
164                 uint32 blocking_pid)
165 {
166         static BOOL set_lock_msg;
167         blocking_lock_record *blr;
168         NTSTATUS status;
169
170         if(in_chained_smb() ) {
171                 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
172                 return False;
173         }
174
175         /*
176          * Now queue an entry on the blocking lock queue. We setup
177          * the expiration time here.
178          */
179
180         if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
181                 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
182                 return False;
183         }
184
185         blr->next = NULL;
186         blr->prev = NULL;
187
188         if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
189                 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
190                 SAFE_FREE(blr);
191                 return False;
192         }
193
194         blr->com_type = CVAL(inbuf,smb_com);
195         blr->fsp = fsp;
196         if (lock_timeout == -1) {
197                 blr->expire_time.tv_sec = 0;
198                 blr->expire_time.tv_usec = 0; /* Never expire. */
199         } else {
200                 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
201                                         (lock_timeout % 1000) * 1000);
202         }
203         blr->lock_num = lock_num;
204         blr->lock_pid = lock_pid;
205         blr->blocking_pid = blocking_pid;
206         blr->lock_flav = lock_flav;
207         blr->lock_type = lock_type;
208         blr->offset = offset;
209         blr->count = count;
210         memcpy(blr->inbuf, inbuf, length);
211         blr->length = length;
212
213         /* Add a pending lock record for this. */
214         status = brl_lock(smbd_messaging_context(), br_lck,
215                         lock_pid,
216                         procid_self(),
217                         offset,
218                         count,
219                         lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
220                         blr->lock_flav,
221                         lock_timeout ? True : False, /* blocking_lock. */
222                         NULL);
223
224         if (!NT_STATUS_IS_OK(status)) {
225                 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
226                 DLIST_REMOVE(blocking_lock_queue, blr);
227                 free_blocking_lock_record(blr);
228                 return False;
229         }
230
231         DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
232         recalc_brl_timeout();
233
234         /* Ensure we'll receive messages when this is unlocked. */
235         if (!set_lock_msg) {
236                 messaging_register(smbd_messaging_context(), NULL,
237                                    MSG_SMB_UNLOCK, received_unlock_msg);
238                 set_lock_msg = True;
239         }
240
241         DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with "
242                 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
243                 length, (unsigned int)blr->expire_time.tv_sec,
244                 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
245                 blr->fsp->fnum, blr->fsp->fsp_name ));
246
247         /* Push the MID of this packet on the signing queue. */
248         srv_defer_sign_response(SVAL(inbuf,smb_mid));
249
250         return True;
251 }
252
253 /****************************************************************************
254  Return a smd with a given size.
255 *****************************************************************************/
256
257 static void send_blocking_reply(char *outbuf, int outsize, const char *inbuf)
258 {
259         if(outsize > 4) {
260                 smb_setlen(inbuf, outbuf,outsize - 4);
261         }
262
263         if (!send_smb(smbd_server_fd(),outbuf)) {
264                 exit_server_cleanly("send_blocking_reply: send_smb failed.");
265         }
266 }
267
268 /****************************************************************************
269  Return a lockingX success SMB.
270 *****************************************************************************/
271
272 static void reply_lockingX_success(blocking_lock_record *blr)
273 {
274         char *outbuf = get_OutBuffer();
275         int bufsize = BUFFER_SIZE;
276         char *inbuf = blr->inbuf;
277         int outsize = 0;
278
279         construct_reply_common(inbuf, outbuf);
280         set_message(inbuf,outbuf,2,0,True);
281
282         /*
283          * As this message is a lockingX call we must handle
284          * any following chained message correctly.
285          * This is normally handled in construct_reply(),
286          * but as that calls switch_message, we can't use
287          * that here and must set up the chain info manually.
288          */
289
290         outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
291
292         outsize += chain_size;
293
294         send_blocking_reply(outbuf,outsize,inbuf);
295 }
296
297 /****************************************************************************
298  Return a generic lock fail error blocking call.
299 *****************************************************************************/
300
301 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
302 {
303         char *outbuf = get_OutBuffer();
304         char *inbuf = blr->inbuf;
305         construct_reply_common(inbuf, outbuf);
306
307         /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
308            FILE_LOCK_CONFLICT! (tridge) */
309         if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
310                 status = NT_STATUS_FILE_LOCK_CONFLICT;
311         }
312
313         if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
314                 /* Store the last lock error. */
315                 files_struct *fsp = blr->fsp;
316
317                 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
318                 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
319                 fsp->last_lock_failure.context.pid = procid_self();
320                 fsp->last_lock_failure.start = blr->offset;
321                 fsp->last_lock_failure.size = blr->count;
322                 fsp->last_lock_failure.fnum = fsp->fnum;
323                 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
324                 fsp->last_lock_failure.lock_flav = blr->lock_flav;
325         }
326
327         ERROR_NT(status);
328         if (!send_smb(smbd_server_fd(),outbuf)) {
329                 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
330         }
331 }
332
333 /****************************************************************************
334  Return a lock fail error for a lockingX call. Undo all the locks we have 
335  obtained first.
336 *****************************************************************************/
337
338 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
339 {
340         char *inbuf = blr->inbuf;
341         files_struct *fsp = blr->fsp;
342         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
343         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
344         uint32 lock_pid;
345         unsigned char locktype = CVAL(inbuf,smb_vwv3);
346         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
347         char *data;
348         int i;
349
350         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
351         
352         /* 
353          * Data now points at the beginning of the list
354          * of smb_lkrng structs.
355          */
356
357         /*
358          * Ensure we don't do a remove on the lock that just failed,
359          * as under POSIX rules, if we have a lock already there, we
360          * will delete it (and we shouldn't) .....
361          */
362         
363         for(i = blr->lock_num - 1; i >= 0; i--) {
364                 BOOL err;
365                 
366                 lock_pid = get_lock_pid( data, i, large_file_format);
367                 count = get_lock_count( data, i, large_file_format);
368                 offset = get_lock_offset( data, i, large_file_format, &err);
369                 
370                 /*
371                  * We know err cannot be set as if it was the lock
372                  * request would never have been queued. JRA.
373                  */
374                 
375                 do_unlock(smbd_messaging_context(),
376                         fsp,
377                         lock_pid,
378                         count,
379                         offset,
380                         WINDOWS_LOCK);
381         }
382         
383         generic_blocking_lock_error(blr, status);
384 }
385
386 /****************************************************************************
387  Return a lock fail error.
388 *****************************************************************************/
389
390 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
391 {
392         switch(blr->com_type) {
393         case SMBlockingX:
394                 reply_lockingX_error(blr, status);
395                 break;
396         case SMBtrans2:
397         case SMBtranss2:
398                 {
399                         char *outbuf = get_OutBuffer();
400                         char *inbuf = blr->inbuf;
401                         construct_reply_common(inbuf, outbuf);
402                         /* construct_reply_common has done us the favor to pre-fill the
403                          * command field with SMBtranss2 which is wrong :-)
404                          */
405                         SCVAL(outbuf,smb_com,SMBtrans2);
406                         ERROR_NT(status);
407                         if (!send_smb(smbd_server_fd(),outbuf)) {
408                                 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
409                         }
410                         break;
411                 }
412         default:
413                 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
414                 exit_server("PANIC - unknown type on blocking lock queue");
415         }
416 }
417
418 /****************************************************************************
419  Attempt to finish off getting all pending blocking locks for a lockingX call.
420  Returns True if we want to be removed from the list.
421 *****************************************************************************/
422
423 static BOOL process_lockingX(blocking_lock_record *blr)
424 {
425         char *inbuf = blr->inbuf;
426         unsigned char locktype = CVAL(inbuf,smb_vwv3);
427         files_struct *fsp = blr->fsp;
428         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
429         uint16 num_locks = SVAL(inbuf,smb_vwv7);
430         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
431         uint32 lock_pid;
432         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
433         char *data;
434         NTSTATUS status = NT_STATUS_OK;
435
436         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
437
438         /* 
439          * Data now points at the beginning of the list
440          * of smb_lkrng structs.
441          */
442
443         for(; blr->lock_num < num_locks; blr->lock_num++) {
444                 struct byte_range_lock *br_lck = NULL;
445                 BOOL err;
446
447                 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
448                 count = get_lock_count( data, blr->lock_num, large_file_format);
449                 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
450                 
451                 /*
452                  * We know err cannot be set as if it was the lock
453                  * request would never have been queued. JRA.
454                  */
455                 errno = 0;
456                 br_lck = do_lock(smbd_messaging_context(),
457                                 fsp,
458                                 lock_pid,
459                                 count,
460                                 offset, 
461                                 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
462                                         READ_LOCK : WRITE_LOCK),
463                                 WINDOWS_LOCK,
464                                 True,
465                                 &status,
466                                 &blr->blocking_pid);
467
468                 TALLOC_FREE(br_lck);
469
470                 if (NT_STATUS_IS_ERR(status)) {
471                         break;
472                 }
473         }
474
475         if(blr->lock_num == num_locks) {
476                 /*
477                  * Success - we got all the locks.
478                  */
479                 
480                 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
481                          fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
482
483                 reply_lockingX_success(blr);
484                 return True;
485         } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
486                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
487                         /*
488                          * We have other than a "can't get lock"
489                          * error. Free any locks we had and return an error.
490                          * Return True so we get dequeued.
491                          */
492                 
493                 blocking_lock_reply_error(blr, status);
494                 return True;
495         }
496
497         /*
498          * Still can't get all the locks - keep waiting.
499          */
500         
501         DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
502 Waiting....\n", 
503                   blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
504         
505         return False;
506 }
507
508 /****************************************************************************
509  Attempt to get the posix lock request from a SMBtrans2 call.
510  Returns True if we want to be removed from the list.
511 *****************************************************************************/
512
513 static BOOL process_trans2(blocking_lock_record *blr)
514 {
515         char *inbuf = blr->inbuf;
516         char *outbuf;
517         char params[2];
518         NTSTATUS status;
519         struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
520                                                 blr->fsp,
521                                                 blr->lock_pid,
522                                                 blr->count,
523                                                 blr->offset,
524                                                 blr->lock_type,
525                                                 blr->lock_flav,
526                                                 True,
527                                                 &status,
528                                                 &blr->blocking_pid);
529         TALLOC_FREE(br_lck);
530
531         if (!NT_STATUS_IS_OK(status)) {
532                 if (ERROR_WAS_LOCK_DENIED(status)) {
533                         /* Still can't get the lock, just keep waiting. */
534                         return False;
535                 }       
536                 /*
537                  * We have other than a "can't get lock"
538                  * error. Send an error and return True so we get dequeued.
539                  */
540                 blocking_lock_reply_error(blr, status);
541                 return True;
542         }
543
544         /* We finally got the lock, return success. */
545         outbuf = get_OutBuffer();
546         construct_reply_common(inbuf, outbuf);
547         SCVAL(outbuf,smb_com,SMBtrans2);
548         SSVAL(params,0,0);
549         /* Fake up max_data_bytes here - we know it fits. */
550         send_trans2_replies(inbuf, outbuf, max_send, params, 2, NULL, 0, 0xffff);
551         return True;
552 }
553
554
555 /****************************************************************************
556  Process a blocking lock SMB.
557  Returns True if we want to be removed from the list.
558 *****************************************************************************/
559
560 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
561 {
562         switch(blr->com_type) {
563                 case SMBlockingX:
564                         return process_lockingX(blr);
565                 case SMBtrans2:
566                 case SMBtranss2:
567                         return process_trans2(blr);
568                 default:
569                         DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
570                         exit_server("PANIC - unknown type on blocking lock queue");
571         }
572         return False; /* Keep compiler happy. */
573 }
574
575 /****************************************************************************
576  Cancel entries by fnum from the blocking lock pending queue.
577 *****************************************************************************/
578
579 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
580 {
581         blocking_lock_record *blr, *next = NULL;
582
583         for(blr = blocking_lock_queue; blr; blr = next) {
584                 next = blr->next;
585                 if(blr->fsp->fnum == fsp->fnum) {
586                         unsigned char locktype = 0;
587
588                         if (blr->com_type == SMBlockingX) {
589                                 locktype = CVAL(blr->inbuf,smb_vwv3);
590                         }
591
592                         if (br_lck) {
593                                 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
594 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
595
596                                 brl_lock_cancel(br_lck,
597                                         blr->lock_pid,
598                                         procid_self(),
599                                         blr->offset,
600                                         blr->count,
601                                         blr->lock_flav);
602
603                                 blocking_lock_cancel(fsp,
604                                         blr->lock_pid,
605                                         blr->offset,
606                                         blr->count,
607                                         blr->lock_flav,
608                                         locktype,
609                                         NT_STATUS_RANGE_NOT_LOCKED);
610                         }
611                 }
612         }
613 }
614
615 /****************************************************************************
616  Delete entries by mid from the blocking lock pending queue. Always send reply.
617 *****************************************************************************/
618
619 void remove_pending_lock_requests_by_mid(int mid)
620 {
621         blocking_lock_record *blr, *next = NULL;
622
623         for(blr = blocking_lock_queue; blr; blr = next) {
624                 next = blr->next;
625                 if(SVAL(blr->inbuf,smb_mid) == mid) {
626                         files_struct *fsp = blr->fsp;
627                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
628
629                         if (br_lck) {
630                                 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
631 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
632
633                                 brl_lock_cancel(br_lck,
634                                         blr->lock_pid,
635                                         procid_self(),
636                                         blr->offset,
637                                         blr->count,
638                                         blr->lock_flav);
639                                 TALLOC_FREE(br_lck);
640                         }
641
642                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
643                         DLIST_REMOVE(blocking_lock_queue, blr);
644                         free_blocking_lock_record(blr);
645                 }
646         }
647 }
648
649 /****************************************************************************
650  Is this mid a blocking lock request on the queue ?
651 *****************************************************************************/
652
653 BOOL blocking_lock_was_deferred(int mid)
654 {
655         blocking_lock_record *blr, *next = NULL;
656
657         for(blr = blocking_lock_queue; blr; blr = next) {
658                 next = blr->next;
659                 if(SVAL(blr->inbuf,smb_mid) == mid) {
660                         return True;
661                 }
662         }
663         return False;
664 }
665
666 /****************************************************************************
667   Set a flag as an unlock request affects one of our pending locks.
668 *****************************************************************************/
669
670 static void received_unlock_msg(struct messaging_context *msg,
671                                 void *private_data,
672                                 uint32_t msg_type,
673                                 struct server_id server_id,
674                                 DATA_BLOB *data)
675 {
676         DEBUG(10,("received_unlock_msg\n"));
677         process_blocking_lock_queue();
678 }
679
680 /****************************************************************************
681  Process the blocking lock queue. Note that this is only called as root.
682 *****************************************************************************/
683
684 static void process_blocking_lock_queue(void)
685 {
686         struct timeval tv_curr = timeval_current();
687         blocking_lock_record *blr, *next = NULL;
688         BOOL recalc_timeout = False;
689
690         /*
691          * Go through the queue and see if we can get any of the locks.
692          */
693
694         for (blr = blocking_lock_queue; blr; blr = next) {
695                 connection_struct *conn = NULL;
696                 uint16 vuid;
697                 files_struct *fsp = NULL;
698
699                 next = blr->next;
700
701                 /*
702                  * Ensure we don't have any old chain_fsp values
703                  * sitting around....
704                  */
705                 chain_size = 0;
706                 file_chain_reset();
707                 fsp = blr->fsp;
708
709                 conn = conn_find(SVAL(blr->inbuf,smb_tid));
710                 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
711                                 SVAL(blr->inbuf,smb_uid);
712
713                 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
714                         fsp->fnum, fsp->fsp_name ));
715
716                 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
717                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
718
719                         /*
720                          * Lock expired - throw away all previously
721                          * obtained locks and return lock error.
722                          */
723
724                         if (br_lck) {
725                                 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
726                                         fsp->fnum, fsp->fsp_name ));
727
728                                 brl_lock_cancel(br_lck,
729                                         blr->lock_pid,
730                                         procid_self(),
731                                         blr->offset,
732                                         blr->count,
733                                         blr->lock_flav);
734                                 TALLOC_FREE(br_lck);
735                         }
736
737                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
738                         DLIST_REMOVE(blocking_lock_queue, blr);
739                         free_blocking_lock_record(blr);
740                         recalc_timeout = True;
741                         continue;
742                 }
743
744                 if(!change_to_user(conn,vuid)) {
745                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
746
747                         /*
748                          * Remove the entry and return an error to the client.
749                          */
750
751                         if (br_lck) {
752                                 brl_lock_cancel(br_lck,
753                                         blr->lock_pid,
754                                         procid_self(),
755                                         blr->offset,
756                                         blr->count,
757                                         blr->lock_flav);
758                                 TALLOC_FREE(br_lck);
759                         }
760
761                         DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
762                                 vuid ));
763                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
764                         DLIST_REMOVE(blocking_lock_queue, blr);
765                         free_blocking_lock_record(blr);
766                         recalc_timeout = True;
767                         continue;
768                 }
769
770                 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
771                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
772
773                         /*
774                          * Remove the entry and return an error to the client.
775                          */
776
777                         if (br_lck) {
778                                 brl_lock_cancel(br_lck,
779                                         blr->lock_pid,
780                                         procid_self(),
781                                         blr->offset,
782                                         blr->count,
783                                         blr->lock_flav);
784                                 TALLOC_FREE(br_lck);
785                         }
786
787                         DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
788                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
789                         DLIST_REMOVE(blocking_lock_queue, blr);
790                         free_blocking_lock_record(blr);
791                         recalc_timeout = True;
792                         change_to_root_user();
793                         continue;
794                 }
795
796                 /*
797                  * Go through the remaining locks and try and obtain them.
798                  * The call returns True if all locks were obtained successfully
799                  * and False if we still need to wait.
800                  */
801
802                 if(blocking_lock_record_process(blr)) {
803                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
804
805                         if (br_lck) {
806                                 brl_lock_cancel(br_lck,
807                                         blr->lock_pid,
808                                         procid_self(),
809                                         blr->offset,
810                                         blr->count,
811                                         blr->lock_flav);
812                                 TALLOC_FREE(br_lck);
813                         }
814
815                         DLIST_REMOVE(blocking_lock_queue, blr);
816                         free_blocking_lock_record(blr);
817                         recalc_timeout = True;
818                 }
819                 change_to_root_user();
820         }
821
822         if (recalc_timeout) {
823                 recalc_brl_timeout();
824         }
825 }
826
827 /****************************************************************************
828  Handle a cancel message. Lock already moved onto the cancel queue.
829 *****************************************************************************/
830
831 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
832
833 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
834                                                  void *private_data,
835                                                  uint32_t msg_type,
836                                                  struct server_id server_id,
837                                                  DATA_BLOB *data)
838 {
839         NTSTATUS err;
840         const char *msg = (const char *)data->data;
841         blocking_lock_record *blr;
842
843         if (data->data == NULL) {
844                 smb_panic("process_blocking_lock_cancel_message: null msg");
845         }
846
847         if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
848                 DEBUG(0, ("process_blocking_lock_cancel_message: "
849                           "Got invalid msg len %d\n", (int)data->length));
850                 smb_panic("process_blocking_lock_cancel_message: bad msg");
851         }
852
853         memcpy(&blr, msg, sizeof(blr));
854         memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
855
856         DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
857                 nt_errstr(err) ));
858
859         blocking_lock_reply_error(blr, err);
860         DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
861         free_blocking_lock_record(blr);
862 }
863
864 /****************************************************************************
865  Send ourselves a blocking lock cancelled message. Handled asynchronously above.
866 *****************************************************************************/
867
868 BOOL blocking_lock_cancel(files_struct *fsp,
869                         uint32 lock_pid,
870                         SMB_BIG_UINT offset,
871                         SMB_BIG_UINT count,
872                         enum brl_flavour lock_flav,
873                         unsigned char locktype,
874                         NTSTATUS err)
875 {
876         static BOOL initialized;
877         char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
878         blocking_lock_record *blr;
879
880         if (!initialized) {
881                 /* Register our message. */
882                 messaging_register(smbd_messaging_context(), NULL,
883                                    MSG_SMB_BLOCKING_LOCK_CANCEL,
884                                    process_blocking_lock_cancel_message);
885
886                 initialized = True;
887         }
888
889         for (blr = blocking_lock_queue; blr; blr = blr->next) {
890                 if (fsp == blr->fsp &&
891                                 lock_pid == blr->lock_pid &&
892                                 offset == blr->offset &&
893                                 count == blr->count &&
894                                 lock_flav == blr->lock_flav) {
895                         break;
896                 }
897         }
898
899         if (!blr) {
900                 return False;
901         }
902
903         /* Check the flags are right. */
904         if (blr->com_type == SMBlockingX &&
905                 (locktype & LOCKING_ANDX_LARGE_FILES) !=
906                         (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
907                 return False;
908         }
909
910         /* Move to cancelled queue. */
911         DLIST_REMOVE(blocking_lock_queue, blr);
912         DLIST_ADD(blocking_lock_cancelled_queue, blr);
913
914         /* Create the message. */
915         memcpy(msg, &blr, sizeof(blr));
916         memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
917
918         messaging_send_buf(smbd_messaging_context(), procid_self(),
919                            MSG_SMB_BLOCKING_LOCK_CANCEL,
920                            (uint8 *)&msg, sizeof(msg));
921
922         return True;
923 }