s3: Fix an uninitialized variable reference
[Samba.git] / source / smbd / blocking.c
blob41cbb4a128c7f19bc701cd3bb1f48d7ae4fe72b4
1 /*
2 Unix SMB/CIFS implementation.
3 Blocking Locking functions
4 Copyright (C) Jeremy Allison 1998-2003
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.
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.
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/>.
20 #include "includes.h"
21 #undef DBGC_CLASS
22 #define DBGC_CLASS DBGC_LOCKING
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 *****************************************************************************/
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 bool encrypted;
45 } blocking_lock_record;
47 /* dlink list we store pending lock records on. */
48 static blocking_lock_record *blocking_lock_queue;
50 /* dlink list we move cancelled lock records onto. */
51 static blocking_lock_record *blocking_lock_cancelled_queue;
53 /* The event that makes us process our blocking lock queue */
54 static struct timed_event *brl_timeout;
56 /****************************************************************************
57 Destructor for the above structure.
58 ****************************************************************************/
60 static void free_blocking_lock_record(blocking_lock_record *blr)
62 SAFE_FREE(blr->inbuf);
63 SAFE_FREE(blr);
66 /****************************************************************************
67 Determine if this is a secondary element of a chained SMB.
68 **************************************************************************/
70 static bool in_chained_smb(void)
72 return (chain_size != 0);
75 static void received_unlock_msg(struct messaging_context *msg,
76 void *private_data,
77 uint32_t msg_type,
78 struct server_id server_id,
79 DATA_BLOB *data);
80 static void process_blocking_lock_queue(void);
82 static void brl_timeout_fn(struct event_context *event_ctx,
83 struct timed_event *te,
84 struct timeval now,
85 void *private_data)
87 SMB_ASSERT(brl_timeout == te);
88 TALLOC_FREE(brl_timeout);
90 change_to_root_user(); /* TODO: Possibly run all timed events as
91 * root */
93 process_blocking_lock_queue();
96 /****************************************************************************
97 We need a version of timeval_min that treats zero timval as infinite.
98 ****************************************************************************/
100 static struct timeval timeval_brl_min(const struct timeval *tv1,
101 const struct timeval *tv2)
103 if (timeval_is_zero(tv1)) {
104 return *tv2;
106 if (timeval_is_zero(tv2)) {
107 return *tv1;
109 return timeval_min(tv1, tv2);
112 /****************************************************************************
113 After a change to blocking_lock_queue, recalculate the timed_event for the
114 next processing.
115 ****************************************************************************/
117 static bool recalc_brl_timeout(void)
119 blocking_lock_record *brl;
120 struct timeval next_timeout;
122 TALLOC_FREE(brl_timeout);
124 next_timeout = timeval_zero();
126 for (brl = blocking_lock_queue; brl; brl = brl->next) {
127 if (timeval_is_zero(&brl->expire_time)) {
129 * If we're blocked on pid 0xFFFFFFFF this is
130 * a POSIX lock, so calculate a timeout of
131 * 10 seconds into the future.
133 if (brl->blocking_pid == 0xFFFFFFFF) {
134 struct timeval psx_to = timeval_current_ofs(10, 0);
135 next_timeout = timeval_brl_min(&next_timeout, &psx_to);
138 continue;
141 next_timeout = timeval_brl_min(&next_timeout, &brl->expire_time);
144 if (timeval_is_zero(&next_timeout)) {
145 return True;
148 if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
149 next_timeout,
150 brl_timeout_fn, NULL))) {
151 return False;
154 return True;
158 /****************************************************************************
159 Function to push a blocking lock request onto the lock queue.
160 ****************************************************************************/
162 bool push_blocking_lock_request( struct byte_range_lock *br_lck,
163 const struct smb_request *req,
164 files_struct *fsp,
165 int lock_timeout,
166 int lock_num,
167 uint32 lock_pid,
168 enum brl_type lock_type,
169 enum brl_flavour lock_flav,
170 SMB_BIG_UINT offset,
171 SMB_BIG_UINT count,
172 uint32 blocking_pid)
174 static bool set_lock_msg;
175 size_t length = smb_len(req->inbuf)+4;
176 blocking_lock_record *blr;
177 NTSTATUS status;
179 if(in_chained_smb() ) {
180 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
181 return False;
185 * Now queue an entry on the blocking lock queue. We setup
186 * the expiration time here.
189 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
190 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
191 return False;
194 blr->next = NULL;
195 blr->prev = NULL;
197 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
198 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
199 SAFE_FREE(blr);
200 return False;
203 blr->com_type = CVAL(req->inbuf,smb_com);
204 blr->fsp = fsp;
205 if (lock_timeout == -1) {
206 blr->expire_time.tv_sec = 0;
207 blr->expire_time.tv_usec = 0; /* Never expire. */
208 } else {
209 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
210 (lock_timeout % 1000) * 1000);
212 blr->lock_num = lock_num;
213 blr->lock_pid = lock_pid;
214 blr->blocking_pid = blocking_pid;
215 blr->lock_flav = lock_flav;
216 blr->lock_type = lock_type;
217 blr->offset = offset;
218 blr->count = count;
219 memcpy(blr->inbuf, req->inbuf, length);
220 blr->length = length;
221 blr->encrypted = req->encrypted;
223 /* Add a pending lock record for this. */
224 status = brl_lock(smbd_messaging_context(), br_lck,
225 lock_pid,
226 procid_self(),
227 offset,
228 count,
229 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
230 blr->lock_flav,
231 lock_timeout ? True : False, /* blocking_lock. */
232 NULL);
234 if (!NT_STATUS_IS_OK(status)) {
235 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
236 DLIST_REMOVE(blocking_lock_queue, blr);
237 free_blocking_lock_record(blr);
238 return False;
241 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
242 recalc_brl_timeout();
244 /* Ensure we'll receive messages when this is unlocked. */
245 if (!set_lock_msg) {
246 messaging_register(smbd_messaging_context(), NULL,
247 MSG_SMB_UNLOCK, received_unlock_msg);
248 set_lock_msg = True;
251 DEBUG(3,("push_blocking_lock_request: lock request length=%u blocked with "
252 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
253 (unsigned int)length, (unsigned int)blr->expire_time.tv_sec,
254 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
255 blr->fsp->fnum, blr->fsp->fsp_name ));
257 /* Push the MID of this packet on the signing queue. */
258 srv_defer_sign_response(SVAL(req->inbuf,smb_mid));
260 return True;
263 /****************************************************************************
264 Return a lockingX success SMB.
265 *****************************************************************************/
267 static void reply_lockingX_success(blocking_lock_record *blr)
269 struct smb_request *req;
271 if (!(req = talloc(talloc_tos(), struct smb_request))) {
272 smb_panic("Could not allocate smb_request");
275 init_smb_request(req, (uint8 *)blr->inbuf, 0, blr->encrypted);
276 reply_outbuf(req, 2, 0);
279 * As this message is a lockingX call we must handle
280 * any following chained message correctly.
281 * This is normally handled in construct_reply(),
282 * but as that calls switch_message, we can't use
283 * that here and must set up the chain info manually.
286 chain_reply(req);
288 if (!srv_send_smb(smbd_server_fd(),
289 (char *)req->outbuf,
290 IS_CONN_ENCRYPTED(blr->fsp->conn))) {
291 exit_server_cleanly("send_blocking_reply: srv_send_smb failed.");
295 /****************************************************************************
296 Return a generic lock fail error blocking call.
297 *****************************************************************************/
299 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
301 char outbuf[smb_size];
302 char *inbuf = blr->inbuf;
304 construct_reply_common(inbuf, outbuf);
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;
312 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
313 /* Store the last lock error. */
314 files_struct *fsp = blr->fsp;
316 if (fsp) {
317 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
318 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
319 fsp->last_lock_failure.context.pid = procid_self();
320 fsp->last_lock_failure.start = blr->offset;
321 fsp->last_lock_failure.size = blr->count;
322 fsp->last_lock_failure.fnum = fsp->fnum;
323 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
324 fsp->last_lock_failure.lock_flav = blr->lock_flav;
328 ERROR_NT(status);
329 if (!srv_send_smb(smbd_server_fd(),outbuf, blr->encrypted)) {
330 exit_server_cleanly("generic_blocking_lock_error: srv_send_smb failed.");
334 /****************************************************************************
335 Return a lock fail error for a lockingX call. Undo all the locks we have
336 obtained first.
337 *****************************************************************************/
339 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
341 char *inbuf = blr->inbuf;
342 files_struct *fsp = blr->fsp;
343 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
344 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
345 uint32 lock_pid;
346 unsigned char locktype = CVAL(inbuf,smb_vwv3);
347 bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
348 char *data;
349 int i;
351 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
354 * Data now points at the beginning of the list
355 * of smb_lkrng structs.
359 * Ensure we don't do a remove on the lock that just failed,
360 * as under POSIX rules, if we have a lock already there, we
361 * will delete it (and we shouldn't) .....
364 for(i = blr->lock_num - 1; i >= 0; i--) {
365 bool err;
367 lock_pid = get_lock_pid( data, i, large_file_format);
368 count = get_lock_count( data, i, large_file_format);
369 offset = get_lock_offset( data, i, large_file_format, &err);
372 * We know err cannot be set as if it was the lock
373 * request would never have been queued. JRA.
376 do_unlock(smbd_messaging_context(),
377 fsp,
378 lock_pid,
379 count,
380 offset,
381 WINDOWS_LOCK);
384 generic_blocking_lock_error(blr, status);
387 /****************************************************************************
388 Return a lock fail error.
389 *****************************************************************************/
391 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
393 switch(blr->com_type) {
394 case SMBlockingX:
395 reply_lockingX_error(blr, status);
396 break;
397 case SMBtrans2:
398 case SMBtranss2:
400 char outbuf[smb_size];
401 char *inbuf = blr->inbuf;
402 construct_reply_common(inbuf, outbuf);
403 /* construct_reply_common has done us the favor to pre-fill the
404 * command field with SMBtranss2 which is wrong :-)
406 SCVAL(outbuf,smb_com,SMBtrans2);
407 ERROR_NT(status);
408 if (!srv_send_smb(smbd_server_fd(),
409 outbuf,
410 IS_CONN_ENCRYPTED(blr->fsp->conn))) {
411 exit_server_cleanly("blocking_lock_reply_error: srv_send_smb failed.");
413 break;
415 default:
416 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
417 exit_server("PANIC - unknown type on blocking lock queue");
421 /****************************************************************************
422 Attempt to finish off getting all pending blocking locks for a lockingX call.
423 Returns True if we want to be removed from the list.
424 *****************************************************************************/
426 static bool process_lockingX(blocking_lock_record *blr)
428 char *inbuf = blr->inbuf;
429 unsigned char locktype = CVAL(inbuf,smb_vwv3);
430 files_struct *fsp = blr->fsp;
431 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
432 uint16 num_locks = SVAL(inbuf,smb_vwv7);
433 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
434 uint32 lock_pid;
435 bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
436 char *data;
437 NTSTATUS status = NT_STATUS_OK;
439 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
442 * Data now points at the beginning of the list
443 * of smb_lkrng structs.
446 for(; blr->lock_num < num_locks; blr->lock_num++) {
447 struct byte_range_lock *br_lck = NULL;
448 bool err;
450 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
451 count = get_lock_count( data, blr->lock_num, large_file_format);
452 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
455 * We know err cannot be set as if it was the lock
456 * request would never have been queued. JRA.
458 errno = 0;
459 br_lck = do_lock(smbd_messaging_context(),
460 fsp,
461 lock_pid,
462 count,
463 offset,
464 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
465 READ_LOCK : WRITE_LOCK),
466 WINDOWS_LOCK,
467 True,
468 &status,
469 &blr->blocking_pid);
471 TALLOC_FREE(br_lck);
473 if (NT_STATUS_IS_ERR(status)) {
474 break;
478 if(blr->lock_num == num_locks) {
480 * Success - we got all the locks.
483 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
484 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
486 reply_lockingX_success(blr);
487 return True;
488 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
489 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
491 * We have other than a "can't get lock"
492 * error. Free any locks we had and return an error.
493 * Return True so we get dequeued.
496 blocking_lock_reply_error(blr, status);
497 return True;
501 * Still can't get all the locks - keep waiting.
504 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
505 Waiting....\n",
506 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
508 return False;
511 /****************************************************************************
512 Attempt to get the posix lock request from a SMBtrans2 call.
513 Returns True if we want to be removed from the list.
514 *****************************************************************************/
516 static bool process_trans2(blocking_lock_record *blr)
518 struct smb_request *req;
519 char params[2];
520 NTSTATUS status;
521 struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
522 blr->fsp,
523 blr->lock_pid,
524 blr->count,
525 blr->offset,
526 blr->lock_type,
527 blr->lock_flav,
528 True,
529 &status,
530 &blr->blocking_pid);
531 TALLOC_FREE(br_lck);
533 if (!NT_STATUS_IS_OK(status)) {
534 if (ERROR_WAS_LOCK_DENIED(status)) {
535 /* Still can't get the lock, just keep waiting. */
536 return False;
539 * We have other than a "can't get lock"
540 * error. Send an error and return True so we get dequeued.
542 blocking_lock_reply_error(blr, status);
543 return True;
546 /* We finally got the lock, return success. */
548 if (!(req = talloc(talloc_tos(), struct smb_request))) {
549 blocking_lock_reply_error(blr, NT_STATUS_NO_MEMORY);
550 return True;
553 init_smb_request(req, (uint8 *)blr->inbuf, 0, blr->encrypted);
555 SCVAL(req->inbuf, smb_com, SMBtrans2);
556 SSVAL(params,0,0);
557 /* Fake up max_data_bytes here - we know it fits. */
558 send_trans2_replies(blr->fsp->conn, req, params, 2, NULL, 0, 0xffff);
559 return True;
563 /****************************************************************************
564 Process a blocking lock SMB.
565 Returns True if we want to be removed from the list.
566 *****************************************************************************/
568 static bool blocking_lock_record_process(blocking_lock_record *blr)
570 switch(blr->com_type) {
571 case SMBlockingX:
572 return process_lockingX(blr);
573 case SMBtrans2:
574 case SMBtranss2:
575 return process_trans2(blr);
576 default:
577 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
578 exit_server("PANIC - unknown type on blocking lock queue");
580 return False; /* Keep compiler happy. */
583 /****************************************************************************
584 Cancel entries by fnum from the blocking lock pending queue.
585 *****************************************************************************/
587 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
589 blocking_lock_record *blr, *next = NULL;
591 for(blr = blocking_lock_queue; blr; blr = next) {
592 next = blr->next;
593 if(blr->fsp->fnum == fsp->fnum) {
594 unsigned char locktype = 0;
596 if (blr->com_type == SMBlockingX) {
597 locktype = CVAL(blr->inbuf,smb_vwv3);
600 if (br_lck) {
601 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
602 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
604 brl_lock_cancel(br_lck,
605 blr->lock_pid,
606 procid_self(),
607 blr->offset,
608 blr->count,
609 blr->lock_flav);
611 blocking_lock_cancel(fsp,
612 blr->lock_pid,
613 blr->offset,
614 blr->count,
615 blr->lock_flav,
616 locktype,
617 NT_STATUS_RANGE_NOT_LOCKED);
619 /* We're closing the file fsp here, so ensure
620 * we don't have a dangling pointer. */
621 blr->fsp = NULL;
626 /****************************************************************************
627 Delete entries by mid from the blocking lock pending queue. Always send reply.
628 *****************************************************************************/
630 void remove_pending_lock_requests_by_mid(int mid)
632 blocking_lock_record *blr, *next = NULL;
634 for(blr = blocking_lock_queue; blr; blr = next) {
635 next = blr->next;
636 if(SVAL(blr->inbuf,smb_mid) == mid) {
637 files_struct *fsp = blr->fsp;
638 struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
640 if (br_lck) {
641 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
642 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
644 brl_lock_cancel(br_lck,
645 blr->lock_pid,
646 procid_self(),
647 blr->offset,
648 blr->count,
649 blr->lock_flav);
650 TALLOC_FREE(br_lck);
653 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
654 DLIST_REMOVE(blocking_lock_queue, blr);
655 free_blocking_lock_record(blr);
660 /****************************************************************************
661 Is this mid a blocking lock request on the queue ?
662 *****************************************************************************/
664 bool blocking_lock_was_deferred(int mid)
666 blocking_lock_record *blr, *next = NULL;
668 for(blr = blocking_lock_queue; blr; blr = next) {
669 next = blr->next;
670 if(SVAL(blr->inbuf,smb_mid) == mid) {
671 return True;
674 return False;
677 /****************************************************************************
678 Set a flag as an unlock request affects one of our pending locks.
679 *****************************************************************************/
681 static void received_unlock_msg(struct messaging_context *msg,
682 void *private_data,
683 uint32_t msg_type,
684 struct server_id server_id,
685 DATA_BLOB *data)
687 DEBUG(10,("received_unlock_msg\n"));
688 process_blocking_lock_queue();
691 /****************************************************************************
692 Process the blocking lock queue. Note that this is only called as root.
693 *****************************************************************************/
695 static void process_blocking_lock_queue(void)
697 struct timeval tv_curr = timeval_current();
698 blocking_lock_record *blr, *next = NULL;
701 * Go through the queue and see if we can get any of the locks.
704 for (blr = blocking_lock_queue; blr; blr = next) {
705 connection_struct *conn = NULL;
706 uint16 vuid;
707 files_struct *fsp = NULL;
709 next = blr->next;
712 * Ensure we don't have any old chain_fsp values
713 * sitting around....
715 chain_size = 0;
716 file_chain_reset();
717 fsp = blr->fsp;
719 conn = conn_find(SVAL(blr->inbuf,smb_tid));
720 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
721 SVAL(blr->inbuf,smb_uid);
723 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
724 fsp->fnum, fsp->fsp_name ));
726 if(!change_to_user(conn,vuid)) {
727 struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
730 * Remove the entry and return an error to the client.
733 if (br_lck) {
734 brl_lock_cancel(br_lck,
735 blr->lock_pid,
736 procid_self(),
737 blr->offset,
738 blr->count,
739 blr->lock_flav);
740 TALLOC_FREE(br_lck);
743 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
744 vuid ));
745 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
746 DLIST_REMOVE(blocking_lock_queue, blr);
747 free_blocking_lock_record(blr);
748 continue;
751 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
752 struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
755 * Remove the entry and return an error to the client.
758 if (br_lck) {
759 brl_lock_cancel(br_lck,
760 blr->lock_pid,
761 procid_self(),
762 blr->offset,
763 blr->count,
764 blr->lock_flav);
765 TALLOC_FREE(br_lck);
768 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
769 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
770 DLIST_REMOVE(blocking_lock_queue, blr);
771 free_blocking_lock_record(blr);
772 change_to_root_user();
773 continue;
777 * Go through the remaining locks and try and obtain them.
778 * The call returns True if all locks were obtained successfully
779 * and False if we still need to wait.
782 if(blocking_lock_record_process(blr)) {
783 struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
785 if (br_lck) {
786 brl_lock_cancel(br_lck,
787 blr->lock_pid,
788 procid_self(),
789 blr->offset,
790 blr->count,
791 blr->lock_flav);
792 TALLOC_FREE(br_lck);
795 DLIST_REMOVE(blocking_lock_queue, blr);
796 free_blocking_lock_record(blr);
797 change_to_root_user();
798 continue;
801 change_to_root_user();
804 * We couldn't get the locks for this record on the list.
805 * If the time has expired, return a lock error.
808 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
809 struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
812 * Lock expired - throw away all previously
813 * obtained locks and return lock error.
816 if (br_lck) {
817 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
818 fsp->fnum, fsp->fsp_name ));
820 brl_lock_cancel(br_lck,
821 blr->lock_pid,
822 procid_self(),
823 blr->offset,
824 blr->count,
825 blr->lock_flav);
826 TALLOC_FREE(br_lck);
829 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
830 DLIST_REMOVE(blocking_lock_queue, blr);
831 free_blocking_lock_record(blr);
835 recalc_brl_timeout();
838 /****************************************************************************
839 Handle a cancel message. Lock already moved onto the cancel queue.
840 *****************************************************************************/
842 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
844 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
845 void *private_data,
846 uint32_t msg_type,
847 struct server_id server_id,
848 DATA_BLOB *data)
850 NTSTATUS err;
851 const char *msg = (const char *)data->data;
852 blocking_lock_record *blr;
854 if (data->data == NULL) {
855 smb_panic("process_blocking_lock_cancel_message: null msg");
858 if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
859 DEBUG(0, ("process_blocking_lock_cancel_message: "
860 "Got invalid msg len %d\n", (int)data->length));
861 smb_panic("process_blocking_lock_cancel_message: bad msg");
864 memcpy(&blr, msg, sizeof(blr));
865 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
867 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
868 nt_errstr(err) ));
870 blocking_lock_reply_error(blr, err);
871 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
872 free_blocking_lock_record(blr);
875 /****************************************************************************
876 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
877 *****************************************************************************/
879 bool blocking_lock_cancel(files_struct *fsp,
880 uint32 lock_pid,
881 SMB_BIG_UINT offset,
882 SMB_BIG_UINT count,
883 enum brl_flavour lock_flav,
884 unsigned char locktype,
885 NTSTATUS err)
887 static bool initialized;
888 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
889 blocking_lock_record *blr;
891 if (!initialized) {
892 /* Register our message. */
893 messaging_register(smbd_messaging_context(), NULL,
894 MSG_SMB_BLOCKING_LOCK_CANCEL,
895 process_blocking_lock_cancel_message);
897 initialized = True;
900 for (blr = blocking_lock_queue; blr; blr = blr->next) {
901 if (fsp == blr->fsp &&
902 lock_pid == blr->lock_pid &&
903 offset == blr->offset &&
904 count == blr->count &&
905 lock_flav == blr->lock_flav) {
906 break;
910 if (!blr) {
911 return False;
914 /* Check the flags are right. */
915 if (blr->com_type == SMBlockingX &&
916 (locktype & LOCKING_ANDX_LARGE_FILES) !=
917 (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
918 return False;
921 /* Move to cancelled queue. */
922 DLIST_REMOVE(blocking_lock_queue, blr);
923 DLIST_ADD(blocking_lock_cancelled_queue, blr);
925 /* Create the message. */
926 memcpy(msg, &blr, sizeof(blr));
927 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
929 messaging_send_buf(smbd_messaging_context(), procid_self(),
930 MSG_SMB_BLOCKING_LOCK_CANCEL,
931 (uint8 *)&msg, sizeof(msg));
933 return True;