r21879: Move process_blocking_lock_queue to a timed event.
[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 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 process_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(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)
241 {
242         if(outsize > 4)
243                 smb_setlen(outbuf,outsize - 4);
244
245         if (!send_smb(smbd_server_fd(),outbuf))
246                 exit_server_cleanly("send_blocking_reply: send_smb failed.");
247 }
248
249 /****************************************************************************
250  Return a lockingX success SMB.
251 *****************************************************************************/
252
253 static void reply_lockingX_success(blocking_lock_record *blr)
254 {
255         char *outbuf = get_OutBuffer();
256         int bufsize = BUFFER_SIZE;
257         char *inbuf = blr->inbuf;
258         int outsize = 0;
259
260         construct_reply_common(inbuf, outbuf);
261         set_message(outbuf,2,0,True);
262
263         /*
264          * As this message is a lockingX call we must handle
265          * any following chained message correctly.
266          * This is normally handled in construct_reply(),
267          * but as that calls switch_message, we can't use
268          * that here and must set up the chain info manually.
269          */
270
271         outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
272
273         outsize += chain_size;
274
275         send_blocking_reply(outbuf,outsize);
276 }
277
278 /****************************************************************************
279  Return a generic lock fail error blocking call.
280 *****************************************************************************/
281
282 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
283 {
284         char *outbuf = get_OutBuffer();
285         char *inbuf = blr->inbuf;
286         construct_reply_common(inbuf, outbuf);
287
288         /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
289            FILE_LOCK_CONFLICT! (tridge) */
290         if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
291                 status = NT_STATUS_FILE_LOCK_CONFLICT;
292         }
293
294         if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
295                 /* Store the last lock error. */
296                 files_struct *fsp = blr->fsp;
297
298                 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
299                 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
300                 fsp->last_lock_failure.context.pid = procid_self();
301                 fsp->last_lock_failure.start = blr->offset;
302                 fsp->last_lock_failure.size = blr->count;
303                 fsp->last_lock_failure.fnum = fsp->fnum;
304                 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
305                 fsp->last_lock_failure.lock_flav = blr->lock_flav;
306         }
307
308         ERROR_NT(status);
309         if (!send_smb(smbd_server_fd(),outbuf)) {
310                 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
311         }
312 }
313
314 /****************************************************************************
315  Return a lock fail error for a lockingX call. Undo all the locks we have 
316  obtained first.
317 *****************************************************************************/
318
319 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
320 {
321         char *inbuf = blr->inbuf;
322         files_struct *fsp = blr->fsp;
323         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
324         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
325         uint32 lock_pid;
326         unsigned char locktype = CVAL(inbuf,smb_vwv3);
327         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
328         char *data;
329         int i;
330
331         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
332         
333         /* 
334          * Data now points at the beginning of the list
335          * of smb_lkrng structs.
336          */
337
338         /*
339          * Ensure we don't do a remove on the lock that just failed,
340          * as under POSIX rules, if we have a lock already there, we
341          * will delete it (and we shouldn't) .....
342          */
343         
344         for(i = blr->lock_num - 1; i >= 0; i--) {
345                 BOOL err;
346                 
347                 lock_pid = get_lock_pid( data, i, large_file_format);
348                 count = get_lock_count( data, i, large_file_format);
349                 offset = get_lock_offset( data, i, large_file_format, &err);
350                 
351                 /*
352                  * We know err cannot be set as if it was the lock
353                  * request would never have been queued. JRA.
354                  */
355                 
356                 do_unlock(fsp,
357                         lock_pid,
358                         count,
359                         offset,
360                         WINDOWS_LOCK);
361         }
362         
363         generic_blocking_lock_error(blr, status);
364 }
365
366 /****************************************************************************
367  Return a lock fail error.
368 *****************************************************************************/
369
370 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
371 {
372         switch(blr->com_type) {
373         case SMBlockingX:
374                 reply_lockingX_error(blr, status);
375                 break;
376         case SMBtrans2:
377         case SMBtranss2:
378                 {
379                         char *outbuf = get_OutBuffer();
380                         char *inbuf = blr->inbuf;
381                         construct_reply_common(inbuf, outbuf);
382                         /* construct_reply_common has done us the favor to pre-fill the
383                          * command field with SMBtranss2 which is wrong :-)
384                          */
385                         SCVAL(outbuf,smb_com,SMBtrans2);
386                         ERROR_NT(status);
387                         if (!send_smb(smbd_server_fd(),outbuf)) {
388                                 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
389                         }
390                         break;
391                 }
392         default:
393                 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
394                 exit_server("PANIC - unknown type on blocking lock queue");
395         }
396 }
397
398 /****************************************************************************
399  Attempt to finish off getting all pending blocking locks for a lockingX call.
400  Returns True if we want to be removed from the list.
401 *****************************************************************************/
402
403 static BOOL process_lockingX(blocking_lock_record *blr)
404 {
405         char *inbuf = blr->inbuf;
406         unsigned char locktype = CVAL(inbuf,smb_vwv3);
407         files_struct *fsp = blr->fsp;
408         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
409         uint16 num_locks = SVAL(inbuf,smb_vwv7);
410         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
411         uint32 lock_pid;
412         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
413         char *data;
414         NTSTATUS status = NT_STATUS_OK;
415
416         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
417
418         /* 
419          * Data now points at the beginning of the list
420          * of smb_lkrng structs.
421          */
422
423         for(; blr->lock_num < num_locks; blr->lock_num++) {
424                 struct byte_range_lock *br_lck = NULL;
425                 BOOL err;
426
427                 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
428                 count = get_lock_count( data, blr->lock_num, large_file_format);
429                 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
430                 
431                 /*
432                  * We know err cannot be set as if it was the lock
433                  * request would never have been queued. JRA.
434                  */
435                 errno = 0;
436                 br_lck = do_lock(fsp,
437                                 lock_pid,
438                                 count,
439                                 offset, 
440                                 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
441                                         READ_LOCK : WRITE_LOCK),
442                                 WINDOWS_LOCK,
443                                 True,
444                                 &status);
445
446                 TALLOC_FREE(br_lck);
447
448                 if (NT_STATUS_IS_ERR(status)) {
449                         break;
450                 }
451         }
452
453         if(blr->lock_num == num_locks) {
454                 /*
455                  * Success - we got all the locks.
456                  */
457                 
458                 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
459                          fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
460
461                 reply_lockingX_success(blr);
462                 return True;
463         } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
464                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
465                         /*
466                          * We have other than a "can't get lock"
467                          * error. Free any locks we had and return an error.
468                          * Return True so we get dequeued.
469                          */
470                 
471                 blocking_lock_reply_error(blr, status);
472                 return True;
473         }
474
475         /*
476          * Still can't get all the locks - keep waiting.
477          */
478         
479         DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
480 Waiting....\n", 
481                   blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
482         
483         return False;
484 }
485
486 /****************************************************************************
487  Attempt to get the posix lock request from a SMBtrans2 call.
488  Returns True if we want to be removed from the list.
489 *****************************************************************************/
490
491 static BOOL process_trans2(blocking_lock_record *blr)
492 {
493         char *inbuf = blr->inbuf;
494         char *outbuf;
495         char params[2];
496         NTSTATUS status;
497         struct byte_range_lock *br_lck = do_lock(blr->fsp,
498                                                 blr->lock_pid,
499                                                 blr->count,
500                                                 blr->offset,
501                                                 blr->lock_type,
502                                                 blr->lock_flav,
503                                                 True,
504                                                 &status);
505         TALLOC_FREE(br_lck);
506
507         if (!NT_STATUS_IS_OK(status)) {
508                 if (ERROR_WAS_LOCK_DENIED(status)) {
509                         /* Still can't get the lock, just keep waiting. */
510                         return False;
511                 }       
512                 /*
513                  * We have other than a "can't get lock"
514                  * error. Send an error and return True so we get dequeued.
515                  */
516                 blocking_lock_reply_error(blr, status);
517                 return True;
518         }
519
520         /* We finally got the lock, return success. */
521         outbuf = get_OutBuffer();
522         construct_reply_common(inbuf, outbuf);
523         SCVAL(outbuf,smb_com,SMBtrans2);
524         SSVAL(params,0,0);
525         /* Fake up max_data_bytes here - we know it fits. */
526         send_trans2_replies(outbuf, max_send, params, 2, NULL, 0, 0xffff);
527         return True;
528 }
529
530
531 /****************************************************************************
532  Process a blocking lock SMB.
533  Returns True if we want to be removed from the list.
534 *****************************************************************************/
535
536 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
537 {
538         switch(blr->com_type) {
539                 case SMBlockingX:
540                         return process_lockingX(blr);
541                 case SMBtrans2:
542                 case SMBtranss2:
543                         return process_trans2(blr);
544                 default:
545                         DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
546                         exit_server("PANIC - unknown type on blocking lock queue");
547         }
548         return False; /* Keep compiler happy. */
549 }
550
551 /****************************************************************************
552  Cancel entries by fnum from the blocking lock pending queue.
553 *****************************************************************************/
554
555 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
556 {
557         blocking_lock_record *blr, *next = NULL;
558
559         for(blr = blocking_lock_queue; blr; blr = next) {
560                 next = blr->next;
561                 if(blr->fsp->fnum == fsp->fnum) {
562                         unsigned char locktype = 0;
563
564                         if (blr->com_type == SMBlockingX) {
565                                 locktype = CVAL(blr->inbuf,smb_vwv3);
566                         }
567
568                         if (br_lck) {
569                                 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
570 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
571
572                                 brl_lock_cancel(br_lck,
573                                         blr->lock_pid,
574                                         procid_self(),
575                                         blr->offset,
576                                         blr->count,
577                                         blr->lock_flav);
578
579                                 blocking_lock_cancel(fsp,
580                                         blr->lock_pid,
581                                         blr->offset,
582                                         blr->count,
583                                         blr->lock_flav,
584                                         locktype,
585                                         NT_STATUS_RANGE_NOT_LOCKED);
586                         }
587                 }
588         }
589 }
590
591 /****************************************************************************
592  Delete entries by mid from the blocking lock pending queue. Always send reply.
593 *****************************************************************************/
594
595 void remove_pending_lock_requests_by_mid(int mid)
596 {
597         blocking_lock_record *blr, *next = NULL;
598
599         for(blr = blocking_lock_queue; blr; blr = next) {
600                 next = blr->next;
601                 if(SVAL(blr->inbuf,smb_mid) == mid) {
602                         files_struct *fsp = blr->fsp;
603                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
604
605                         if (br_lck) {
606                                 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
607 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
608
609                                 brl_lock_cancel(br_lck,
610                                         blr->lock_pid,
611                                         procid_self(),
612                                         blr->offset,
613                                         blr->count,
614                                         blr->lock_flav);
615                                 TALLOC_FREE(br_lck);
616                         }
617
618                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
619                         DLIST_REMOVE(blocking_lock_queue, blr);
620                         free_blocking_lock_record(blr);
621                 }
622         }
623 }
624
625 /****************************************************************************
626  Is this mid a blocking lock request on the queue ?
627 *****************************************************************************/
628
629 BOOL blocking_lock_was_deferred(int mid)
630 {
631         blocking_lock_record *blr, *next = NULL;
632
633         for(blr = blocking_lock_queue; blr; blr = next) {
634                 next = blr->next;
635                 if(SVAL(blr->inbuf,smb_mid) == mid) {
636                         return True;
637                 }
638         }
639         return False;
640 }
641
642 /****************************************************************************
643   Set a flag as an unlock request affects one of our pending locks.
644 *****************************************************************************/
645
646 static void received_unlock_msg(int msg_type, struct process_id src,
647                                 void *buf, size_t len,
648                                 void *private_data)
649 {
650         DEBUG(10,("received_unlock_msg\n"));
651         process_blocking_lock_queue();
652 }
653
654 /****************************************************************************
655  Process the blocking lock queue. Note that this is only called as root.
656 *****************************************************************************/
657
658 static void process_blocking_lock_queue(void)
659 {
660         struct timeval tv_curr = timeval_current();
661         blocking_lock_record *blr, *next = NULL;
662         BOOL recalc_timeout = False;
663
664         /*
665          * Go through the queue and see if we can get any of the locks.
666          */
667
668         for (blr = blocking_lock_queue; blr; blr = next) {
669                 connection_struct *conn = NULL;
670                 uint16 vuid;
671                 files_struct *fsp = NULL;
672
673                 next = blr->next;
674
675                 /*
676                  * Ensure we don't have any old chain_fsp values
677                  * sitting around....
678                  */
679                 chain_size = 0;
680                 file_chain_reset();
681                 fsp = blr->fsp;
682
683                 conn = conn_find(SVAL(blr->inbuf,smb_tid));
684                 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
685                                 SVAL(blr->inbuf,smb_uid);
686
687                 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
688                         fsp->fnum, fsp->fsp_name ));
689
690                 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
691                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
692
693                         /*
694                          * Lock expired - throw away all previously
695                          * obtained locks and return lock error.
696                          */
697
698                         if (br_lck) {
699                                 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
700                                         fsp->fnum, fsp->fsp_name ));
701
702                                 brl_lock_cancel(br_lck,
703                                         blr->lock_pid,
704                                         procid_self(),
705                                         blr->offset,
706                                         blr->count,
707                                         blr->lock_flav);
708                                 TALLOC_FREE(br_lck);
709                         }
710
711                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
712                         DLIST_REMOVE(blocking_lock_queue, blr);
713                         free_blocking_lock_record(blr);
714                         recalc_timeout = True;
715                         continue;
716                 }
717
718                 if(!change_to_user(conn,vuid)) {
719                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
720
721                         /*
722                          * Remove the entry and return an error to the client.
723                          */
724
725                         if (br_lck) {
726                                 brl_lock_cancel(br_lck,
727                                         blr->lock_pid,
728                                         procid_self(),
729                                         blr->offset,
730                                         blr->count,
731                                         blr->lock_flav);
732                                 TALLOC_FREE(br_lck);
733                         }
734
735                         DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
736                                 vuid ));
737                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
738                         DLIST_REMOVE(blocking_lock_queue, blr);
739                         free_blocking_lock_record(blr);
740                         recalc_timeout = True;
741                         continue;
742                 }
743
744                 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
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 service Error was %s.\n", strerror(errno) ));
762                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
763                         DLIST_REMOVE(blocking_lock_queue, blr);
764                         free_blocking_lock_record(blr);
765                         recalc_timeout = True;
766                         change_to_root_user();
767                         continue;
768                 }
769
770                 /*
771                  * Go through the remaining locks and try and obtain them.
772                  * The call returns True if all locks were obtained successfully
773                  * and False if we still need to wait.
774                  */
775
776                 if(blocking_lock_record_process(blr)) {
777                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
778
779                         if (br_lck) {
780                                 brl_lock_cancel(br_lck,
781                                         blr->lock_pid,
782                                         procid_self(),
783                                         blr->offset,
784                                         blr->count,
785                                         blr->lock_flav);
786                                 TALLOC_FREE(br_lck);
787                         }
788
789                         DLIST_REMOVE(blocking_lock_queue, blr);
790                         free_blocking_lock_record(blr);
791                         recalc_timeout = True;
792                 }
793                 change_to_root_user();
794         }
795
796         if (recalc_timeout) {
797                 recalc_brl_timeout();
798         }
799 }
800
801 /****************************************************************************
802  Handle a cancel message. Lock already moved onto the cancel queue.
803 *****************************************************************************/
804
805 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
806
807 static void process_blocking_lock_cancel_message(int msg_type,
808                                                  struct process_id src,
809                                                  void *buf, size_t len,
810                                                  void *private_data)
811 {
812         NTSTATUS err;
813         const char *msg = (const char *)buf;
814         blocking_lock_record *blr;
815
816         if (buf == NULL) {
817                 smb_panic("process_blocking_lock_cancel_message: null msg\n");
818         }
819
820         if (len != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
821                 DEBUG(0, ("process_blocking_lock_cancel_message: "
822                         "Got invalid msg len %d\n", (int)len));
823                 smb_panic("process_blocking_lock_cancel_message: bad msg\n");
824         }
825
826         memcpy(&blr, msg, sizeof(blr));
827         memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
828
829         DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
830                 nt_errstr(err) ));
831
832         blocking_lock_reply_error(blr, err);
833         DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
834         free_blocking_lock_record(blr);
835 }
836
837 /****************************************************************************
838  Send ourselves a blocking lock cancelled message. Handled asynchronously above.
839 *****************************************************************************/
840
841 BOOL blocking_lock_cancel(files_struct *fsp,
842                         uint32 lock_pid,
843                         SMB_BIG_UINT offset,
844                         SMB_BIG_UINT count,
845                         enum brl_flavour lock_flav,
846                         unsigned char locktype,
847                         NTSTATUS err)
848 {
849         static BOOL initialized;
850         char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
851         blocking_lock_record *blr;
852
853         if (!initialized) {
854                 /* Register our message. */
855                 message_register(MSG_SMB_BLOCKING_LOCK_CANCEL,
856                                  process_blocking_lock_cancel_message,
857                                  NULL);
858
859                 initialized = True;
860         }
861
862         for (blr = blocking_lock_queue; blr; blr = blr->next) {
863                 if (fsp == blr->fsp &&
864                                 lock_pid == blr->lock_pid &&
865                                 offset == blr->offset &&
866                                 count == blr->count &&
867                                 lock_flav == blr->lock_flav) {
868                         break;
869                 }
870         }
871
872         if (!blr) {
873                 return False;
874         }
875
876         /* Check the flags are right. */
877         if (blr->com_type == SMBlockingX &&
878                 (locktype & LOCKING_ANDX_LARGE_FILES) !=
879                         (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
880                 return False;
881         }
882
883         /* Move to cancelled queue. */
884         DLIST_REMOVE(blocking_lock_queue, blr);
885         DLIST_ADD(blocking_lock_cancelled_queue, blr);
886
887         /* Create the message. */
888         memcpy(msg, &blr, sizeof(blr));
889         memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
890
891         message_send_pid(pid_to_procid(sys_getpid()),
892                         MSG_SMB_BLOCKING_LOCK_CANCEL,
893                         &msg, sizeof(msg), True);
894
895         return True;
896 }