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