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