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