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/>.
22 #define DBGC_CLASS DBGC_LOCKING
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 *****************************************************************************/
31 typedef struct _blocking_lock_record {
32 struct _blocking_lock_record *next;
33 struct _blocking_lock_record *prev;
36 struct timeval expire_time;
41 uint32 blocking_pid; /* PID that blocks us. */
42 enum brl_flavour lock_flav;
43 enum brl_type lock_type;
46 } blocking_lock_record;
48 /* dlink list we store pending lock records on. */
49 static blocking_lock_record *blocking_lock_queue;
51 /* dlink list we move cancelled lock records onto. */
52 static blocking_lock_record *blocking_lock_cancelled_queue;
54 /* The event that makes us process our blocking lock queue */
55 static struct timed_event *brl_timeout;
57 /****************************************************************************
58 Destructor for the above structure.
59 ****************************************************************************/
61 static void free_blocking_lock_record(blocking_lock_record *blr)
63 SAFE_FREE(blr->inbuf);
67 /****************************************************************************
68 Determine if this is a secondary element of a chained SMB.
69 **************************************************************************/
71 static BOOL in_chained_smb(void)
73 return (chain_size != 0);
76 static void received_unlock_msg(struct messaging_context *msg,
79 struct server_id server_id,
81 static void process_blocking_lock_queue(void);
83 static void brl_timeout_fn(struct event_context *event_ctx,
84 struct timed_event *te,
85 const struct timeval *now,
88 SMB_ASSERT(brl_timeout == te);
89 TALLOC_FREE(brl_timeout);
91 change_to_root_user(); /* TODO: Possibly run all timed events as
94 process_blocking_lock_queue();
97 /****************************************************************************
98 After a change to blocking_lock_queue, recalculate the timed_event for the
100 ****************************************************************************/
102 static BOOL recalc_brl_timeout(void)
104 blocking_lock_record *brl;
105 struct timeval next_timeout;
107 TALLOC_FREE(brl_timeout);
109 next_timeout = timeval_zero();
111 for (brl = blocking_lock_queue; brl; brl = brl->next) {
112 if (timeval_is_zero(&brl->expire_time)) {
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.
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);
126 if (timeval_is_zero(&next_timeout)) {
127 next_timeout = brl->expire_time;
130 next_timeout = timeval_min(&next_timeout,
135 if (timeval_is_zero(&next_timeout)) {
139 if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
140 next_timeout, "brl_timeout",
141 brl_timeout_fn, NULL))) {
149 /****************************************************************************
150 Function to push a blocking lock request onto the lock queue.
151 ****************************************************************************/
153 BOOL push_blocking_lock_request( struct byte_range_lock *br_lck,
154 char *inbuf, int length,
159 enum brl_type lock_type,
160 enum brl_flavour lock_flav,
165 static BOOL set_lock_msg;
166 blocking_lock_record *blr;
169 if(in_chained_smb() ) {
170 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
175 * Now queue an entry on the blocking lock queue. We setup
176 * the expiration time here.
179 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
180 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
187 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
188 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
193 blr->com_type = CVAL(inbuf,smb_com);
195 if (lock_timeout == -1) {
196 blr->expire_time.tv_sec = 0;
197 blr->expire_time.tv_usec = 0; /* Never expire. */
199 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
200 (lock_timeout % 1000) * 1000);
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;
209 memcpy(blr->inbuf, inbuf, length);
210 blr->length = length;
212 /* Add a pending lock record for this. */
213 status = brl_lock(smbd_messaging_context(), br_lck,
218 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
220 lock_timeout ? True : False, /* blocking_lock. */
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);
230 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
231 recalc_brl_timeout();
233 /* Ensure we'll receive messages when this is unlocked. */
235 messaging_register(smbd_messaging_context(), NULL,
236 MSG_SMB_UNLOCK, received_unlock_msg);
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 ));
246 /* Push the MID of this packet on the signing queue. */
247 srv_defer_sign_response(SVAL(inbuf,smb_mid));
252 /****************************************************************************
253 Return a smd with a given size.
254 *****************************************************************************/
256 static void send_blocking_reply(char *outbuf, int outsize, const char *inbuf)
259 smb_setlen(inbuf, outbuf,outsize - 4);
262 if (!send_smb(smbd_server_fd(),outbuf)) {
263 exit_server_cleanly("send_blocking_reply: send_smb failed.");
267 /****************************************************************************
268 Return a lockingX success SMB.
269 *****************************************************************************/
271 static void reply_lockingX_success(blocking_lock_record *blr)
273 char *outbuf = get_OutBuffer();
274 int bufsize = BUFFER_SIZE;
275 char *inbuf = blr->inbuf;
278 construct_reply_common(inbuf, outbuf);
279 set_message(inbuf,outbuf,2,0,True);
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.
289 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
291 outsize += chain_size;
293 send_blocking_reply(outbuf,outsize,inbuf);
296 /****************************************************************************
297 Return a generic lock fail error blocking call.
298 *****************************************************************************/
300 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
302 char *outbuf = get_OutBuffer();
303 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 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;
327 if (!send_smb(smbd_server_fd(),outbuf)) {
328 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
332 /****************************************************************************
333 Return a lock fail error for a lockingX call. Undo all the locks we have
335 *****************************************************************************/
337 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
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;
344 unsigned char locktype = CVAL(inbuf,smb_vwv3);
345 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
349 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
352 * Data now points at the beginning of the list
353 * of smb_lkrng structs.
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) .....
362 for(i = blr->lock_num - 1; i >= 0; i--) {
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);
370 * We know err cannot be set as if it was the lock
371 * request would never have been queued. JRA.
374 do_unlock(smbd_messaging_context(),
382 generic_blocking_lock_error(blr, status);
385 /****************************************************************************
386 Return a lock fail error.
387 *****************************************************************************/
389 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
391 switch(blr->com_type) {
393 reply_lockingX_error(blr, status);
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 :-)
404 SCVAL(outbuf,smb_com,SMBtrans2);
406 if (!send_smb(smbd_server_fd(),outbuf)) {
407 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
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");
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 *****************************************************************************/
422 static BOOL process_lockingX(blocking_lock_record *blr)
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;
431 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
433 NTSTATUS status = NT_STATUS_OK;
435 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
438 * Data now points at the beginning of the list
439 * of smb_lkrng structs.
442 for(; blr->lock_num < num_locks; blr->lock_num++) {
443 struct byte_range_lock *br_lck = NULL;
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);
451 * We know err cannot be set as if it was the lock
452 * request would never have been queued. JRA.
455 br_lck = do_lock(smbd_messaging_context(),
460 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
461 READ_LOCK : WRITE_LOCK),
469 if (NT_STATUS_IS_ERR(status)) {
474 if(blr->lock_num == num_locks) {
476 * Success - we got all the locks.
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) );
482 reply_lockingX_success(blr);
484 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
485 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
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.
492 blocking_lock_reply_error(blr, status);
497 * Still can't get all the locks - keep waiting.
500 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
502 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
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 *****************************************************************************/
512 static BOOL process_trans2(blocking_lock_record *blr)
514 char *inbuf = blr->inbuf;
518 struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
530 if (!NT_STATUS_IS_OK(status)) {
531 if (ERROR_WAS_LOCK_DENIED(status)) {
532 /* Still can't get the lock, just keep waiting. */
536 * We have other than a "can't get lock"
537 * error. Send an error and return True so we get dequeued.
539 blocking_lock_reply_error(blr, status);
543 /* We finally got the lock, return success. */
544 outbuf = get_OutBuffer();
545 construct_reply_common(inbuf, outbuf);
546 SCVAL(outbuf,smb_com,SMBtrans2);
548 /* Fake up max_data_bytes here - we know it fits. */
549 send_trans2_replies(inbuf, outbuf, max_send, params, 2, NULL, 0, 0xffff);
554 /****************************************************************************
555 Process a blocking lock SMB.
556 Returns True if we want to be removed from the list.
557 *****************************************************************************/
559 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
561 switch(blr->com_type) {
563 return process_lockingX(blr);
566 return process_trans2(blr);
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");
571 return False; /* Keep compiler happy. */
574 /****************************************************************************
575 Cancel entries by fnum from the blocking lock pending queue.
576 *****************************************************************************/
578 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
580 blocking_lock_record *blr, *next = NULL;
582 for(blr = blocking_lock_queue; blr; blr = next) {
584 if(blr->fsp->fnum == fsp->fnum) {
585 unsigned char locktype = 0;
587 if (blr->com_type == SMBlockingX) {
588 locktype = CVAL(blr->inbuf,smb_vwv3);
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 ));
595 brl_lock_cancel(br_lck,
602 blocking_lock_cancel(fsp,
608 NT_STATUS_RANGE_NOT_LOCKED);
614 /****************************************************************************
615 Delete entries by mid from the blocking lock pending queue. Always send reply.
616 *****************************************************************************/
618 void remove_pending_lock_requests_by_mid(int mid)
620 blocking_lock_record *blr, *next = NULL;
622 for(blr = blocking_lock_queue; blr; 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);
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 ));
632 brl_lock_cancel(br_lck,
641 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
642 DLIST_REMOVE(blocking_lock_queue, blr);
643 free_blocking_lock_record(blr);
648 /****************************************************************************
649 Is this mid a blocking lock request on the queue ?
650 *****************************************************************************/
652 BOOL blocking_lock_was_deferred(int mid)
654 blocking_lock_record *blr, *next = NULL;
656 for(blr = blocking_lock_queue; blr; blr = next) {
658 if(SVAL(blr->inbuf,smb_mid) == mid) {
665 /****************************************************************************
666 Set a flag as an unlock request affects one of our pending locks.
667 *****************************************************************************/
669 static void received_unlock_msg(struct messaging_context *msg,
672 struct server_id server_id,
675 DEBUG(10,("received_unlock_msg\n"));
676 process_blocking_lock_queue();
679 /****************************************************************************
680 Process the blocking lock queue. Note that this is only called as root.
681 *****************************************************************************/
683 static void process_blocking_lock_queue(void)
685 struct timeval tv_curr = timeval_current();
686 blocking_lock_record *blr, *next = NULL;
687 BOOL recalc_timeout = False;
690 * Go through the queue and see if we can get any of the locks.
693 for (blr = blocking_lock_queue; blr; blr = next) {
694 connection_struct *conn = NULL;
696 files_struct *fsp = NULL;
701 * Ensure we don't have any old chain_fsp values
708 conn = conn_find(SVAL(blr->inbuf,smb_tid));
709 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
710 SVAL(blr->inbuf,smb_uid);
712 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
713 fsp->fnum, fsp->fsp_name ));
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);
719 * Lock expired - throw away all previously
720 * obtained locks and return lock error.
724 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
725 fsp->fnum, fsp->fsp_name ));
727 brl_lock_cancel(br_lck,
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;
743 if(!change_to_user(conn,vuid)) {
744 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
747 * Remove the entry and return an error to the client.
751 brl_lock_cancel(br_lck,
760 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
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;
769 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
770 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
773 * Remove the entry and return an error to the client.
777 brl_lock_cancel(br_lck,
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();
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.
801 if(blocking_lock_record_process(blr)) {
802 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
805 brl_lock_cancel(br_lck,
814 DLIST_REMOVE(blocking_lock_queue, blr);
815 free_blocking_lock_record(blr);
816 recalc_timeout = True;
818 change_to_root_user();
821 if (recalc_timeout) {
822 recalc_brl_timeout();
826 /****************************************************************************
827 Handle a cancel message. Lock already moved onto the cancel queue.
828 *****************************************************************************/
830 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
832 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
835 struct server_id server_id,
839 const char *msg = (const char *)data->data;
840 blocking_lock_record *blr;
842 if (data->data == NULL) {
843 smb_panic("process_blocking_lock_cancel_message: null msg");
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");
852 memcpy(&blr, msg, sizeof(blr));
853 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
855 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
858 blocking_lock_reply_error(blr, err);
859 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
860 free_blocking_lock_record(blr);
863 /****************************************************************************
864 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
865 *****************************************************************************/
867 BOOL blocking_lock_cancel(files_struct *fsp,
871 enum brl_flavour lock_flav,
872 unsigned char locktype,
875 static BOOL initialized;
876 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
877 blocking_lock_record *blr;
880 /* Register our message. */
881 messaging_register(smbd_messaging_context(), NULL,
882 MSG_SMB_BLOCKING_LOCK_CANCEL,
883 process_blocking_lock_cancel_message);
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) {
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)) {
909 /* Move to cancelled queue. */
910 DLIST_REMOVE(blocking_lock_queue, blr);
911 DLIST_ADD(blocking_lock_cancelled_queue, blr);
913 /* Create the message. */
914 memcpy(msg, &blr, sizeof(blr));
915 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
917 messaging_send_buf(smbd_messaging_context(), procid_self(),
918 MSG_SMB_BLOCKING_LOCK_CANCEL,
919 (uint8 *)&msg, sizeof(msg));