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