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