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
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;
34 struct timeval expire_time;
39 uint32 blocking_pid; /* PID that blocks us. */
40 enum brl_flavour lock_flav;
41 enum brl_type lock_type;
44 } blocking_lock_record;
46 /* dlink list we store pending lock records on. */
47 static blocking_lock_record *blocking_lock_queue;
49 /* dlink list we move cancelled lock records onto. */
50 static blocking_lock_record *blocking_lock_cancelled_queue;
52 /* The event that makes us process our blocking lock queue */
53 static struct timed_event *brl_timeout;
55 /****************************************************************************
56 Destructor for the above structure.
57 ****************************************************************************/
59 static void free_blocking_lock_record(blocking_lock_record *blr)
61 SAFE_FREE(blr->inbuf);
65 /****************************************************************************
66 Determine if this is a secondary element of a chained SMB.
67 **************************************************************************/
69 static bool in_chained_smb(void)
71 return (chain_size != 0);
74 static void received_unlock_msg(struct messaging_context *msg,
77 struct server_id server_id,
79 static void process_blocking_lock_queue(void);
81 static void brl_timeout_fn(struct event_context *event_ctx,
82 struct timed_event *te,
83 const struct timeval *now,
86 SMB_ASSERT(brl_timeout == te);
87 TALLOC_FREE(brl_timeout);
89 change_to_root_user(); /* TODO: Possibly run all timed events as
92 process_blocking_lock_queue();
95 /****************************************************************************
96 After a change to blocking_lock_queue, recalculate the timed_event for the
98 ****************************************************************************/
100 static bool recalc_brl_timeout(void)
102 blocking_lock_record *brl;
103 struct timeval next_timeout;
105 TALLOC_FREE(brl_timeout);
107 next_timeout = timeval_zero();
109 for (brl = blocking_lock_queue; brl; brl = brl->next) {
110 if (timeval_is_zero(&brl->expire_time)) {
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.
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);
124 if (timeval_is_zero(&next_timeout)) {
125 next_timeout = brl->expire_time;
128 next_timeout = timeval_min(&next_timeout,
133 if (timeval_is_zero(&next_timeout)) {
137 if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
138 next_timeout, "brl_timeout",
139 brl_timeout_fn, NULL))) {
147 /****************************************************************************
148 Function to push a blocking lock request onto the lock queue.
149 ****************************************************************************/
151 bool push_blocking_lock_request( struct byte_range_lock *br_lck,
152 const char *inbuf, int length,
157 enum brl_type lock_type,
158 enum brl_flavour lock_flav,
163 static bool set_lock_msg;
164 blocking_lock_record *blr;
167 if(in_chained_smb() ) {
168 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
173 * Now queue an entry on the blocking lock queue. We setup
174 * the expiration time here.
177 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
178 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
185 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
186 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
191 blr->com_type = CVAL(inbuf,smb_com);
193 if (lock_timeout == -1) {
194 blr->expire_time.tv_sec = 0;
195 blr->expire_time.tv_usec = 0; /* Never expire. */
197 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
198 (lock_timeout % 1000) * 1000);
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;
207 memcpy(blr->inbuf, inbuf, length);
208 blr->length = length;
210 /* Add a pending lock record for this. */
211 status = brl_lock(smbd_messaging_context(), br_lck,
216 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
218 lock_timeout ? True : False, /* blocking_lock. */
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);
228 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
229 recalc_brl_timeout();
231 /* Ensure we'll receive messages when this is unlocked. */
233 messaging_register(smbd_messaging_context(), NULL,
234 MSG_SMB_UNLOCK, received_unlock_msg);
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 ));
244 /* Push the MID of this packet on the signing queue. */
245 srv_defer_sign_response(SVAL(inbuf,smb_mid));
250 /****************************************************************************
251 Return a lockingX success SMB.
252 *****************************************************************************/
254 static void reply_lockingX_success(blocking_lock_record *blr)
256 struct smb_request *req;
258 if (!(req = talloc(talloc_tos(), struct smb_request))) {
259 smb_panic("Could not allocate smb_request");
262 init_smb_request(req, (uint8 *)blr->inbuf);
263 reply_outbuf(req, 2, 0);
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.
275 if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
276 exit_server_cleanly("send_blocking_reply: send_smb failed.");
280 /****************************************************************************
281 Return a generic lock fail error blocking call.
282 *****************************************************************************/
284 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
286 char outbuf[smb_size];
287 char *inbuf = blr->inbuf;
289 construct_reply_common(inbuf, outbuf);
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;
297 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
298 /* Store the last lock error. */
299 files_struct *fsp = blr->fsp;
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;
312 if (!send_smb(smbd_server_fd(),outbuf)) {
313 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
317 /****************************************************************************
318 Return a lock fail error for a lockingX call. Undo all the locks we have
320 *****************************************************************************/
322 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
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;
329 unsigned char locktype = CVAL(inbuf,smb_vwv3);
330 bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
334 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
337 * Data now points at the beginning of the list
338 * of smb_lkrng structs.
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) .....
347 for(i = blr->lock_num - 1; i >= 0; i--) {
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);
355 * We know err cannot be set as if it was the lock
356 * request would never have been queued. JRA.
359 do_unlock(smbd_messaging_context(),
367 generic_blocking_lock_error(blr, status);
370 /****************************************************************************
371 Return a lock fail error.
372 *****************************************************************************/
374 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
376 switch(blr->com_type) {
378 reply_lockingX_error(blr, status);
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 :-)
389 SCVAL(outbuf,smb_com,SMBtrans2);
391 if (!send_smb(smbd_server_fd(),outbuf)) {
392 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
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");
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 *****************************************************************************/
407 static bool process_lockingX(blocking_lock_record *blr)
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;
416 bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
418 NTSTATUS status = NT_STATUS_OK;
420 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
423 * Data now points at the beginning of the list
424 * of smb_lkrng structs.
427 for(; blr->lock_num < num_locks; blr->lock_num++) {
428 struct byte_range_lock *br_lck = NULL;
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);
436 * We know err cannot be set as if it was the lock
437 * request would never have been queued. JRA.
440 br_lck = do_lock(smbd_messaging_context(),
445 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
446 READ_LOCK : WRITE_LOCK),
454 if (NT_STATUS_IS_ERR(status)) {
459 if(blr->lock_num == num_locks) {
461 * Success - we got all the locks.
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) );
467 reply_lockingX_success(blr);
469 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
470 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
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.
477 blocking_lock_reply_error(blr, status);
482 * Still can't get all the locks - keep waiting.
485 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
487 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
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 *****************************************************************************/
497 static bool process_trans2(blocking_lock_record *blr)
499 struct smb_request *req;
502 struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
514 if (!NT_STATUS_IS_OK(status)) {
515 if (ERROR_WAS_LOCK_DENIED(status)) {
516 /* Still can't get the lock, just keep waiting. */
520 * We have other than a "can't get lock"
521 * error. Send an error and return True so we get dequeued.
523 blocking_lock_reply_error(blr, status);
527 /* We finally got the lock, return success. */
529 if (!(req = talloc(talloc_tos(), struct smb_request))) {
530 blocking_lock_reply_error(blr, NT_STATUS_NO_MEMORY);
534 init_smb_request(req, (uint8 *)blr->inbuf);
536 SCVAL(req->inbuf, smb_com, SMBtrans2);
538 /* Fake up max_data_bytes here - we know it fits. */
539 send_trans2_replies(req, params, 2, NULL, 0, 0xffff);
544 /****************************************************************************
545 Process a blocking lock SMB.
546 Returns True if we want to be removed from the list.
547 *****************************************************************************/
549 static bool blocking_lock_record_process(blocking_lock_record *blr)
551 switch(blr->com_type) {
553 return process_lockingX(blr);
556 return process_trans2(blr);
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");
561 return False; /* Keep compiler happy. */
564 /****************************************************************************
565 Cancel entries by fnum from the blocking lock pending queue.
566 *****************************************************************************/
568 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
570 blocking_lock_record *blr, *next = NULL;
572 for(blr = blocking_lock_queue; blr; blr = next) {
574 if(blr->fsp->fnum == fsp->fnum) {
575 unsigned char locktype = 0;
577 if (blr->com_type == SMBlockingX) {
578 locktype = CVAL(blr->inbuf,smb_vwv3);
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 ));
585 brl_lock_cancel(br_lck,
592 blocking_lock_cancel(fsp,
598 NT_STATUS_RANGE_NOT_LOCKED);
604 /****************************************************************************
605 Delete entries by mid from the blocking lock pending queue. Always send reply.
606 *****************************************************************************/
608 void remove_pending_lock_requests_by_mid(int mid)
610 blocking_lock_record *blr, *next = NULL;
612 for(blr = blocking_lock_queue; blr; 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);
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 ));
622 brl_lock_cancel(br_lck,
631 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
632 DLIST_REMOVE(blocking_lock_queue, blr);
633 free_blocking_lock_record(blr);
638 /****************************************************************************
639 Is this mid a blocking lock request on the queue ?
640 *****************************************************************************/
642 bool blocking_lock_was_deferred(int mid)
644 blocking_lock_record *blr, *next = NULL;
646 for(blr = blocking_lock_queue; blr; blr = next) {
648 if(SVAL(blr->inbuf,smb_mid) == mid) {
655 /****************************************************************************
656 Set a flag as an unlock request affects one of our pending locks.
657 *****************************************************************************/
659 static void received_unlock_msg(struct messaging_context *msg,
662 struct server_id server_id,
665 DEBUG(10,("received_unlock_msg\n"));
666 process_blocking_lock_queue();
669 /****************************************************************************
670 Process the blocking lock queue. Note that this is only called as root.
671 *****************************************************************************/
673 static void process_blocking_lock_queue(void)
675 struct timeval tv_curr = timeval_current();
676 blocking_lock_record *blr, *next = NULL;
677 bool recalc_timeout = False;
680 * Go through the queue and see if we can get any of the locks.
683 for (blr = blocking_lock_queue; blr; blr = next) {
684 connection_struct *conn = NULL;
686 files_struct *fsp = NULL;
691 * Ensure we don't have any old chain_fsp values
698 conn = conn_find(SVAL(blr->inbuf,smb_tid));
699 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
700 SVAL(blr->inbuf,smb_uid);
702 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
703 fsp->fnum, fsp->fsp_name ));
705 if(!change_to_user(conn,vuid)) {
706 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
709 * Remove the entry and return an error to the client.
713 brl_lock_cancel(br_lck,
722 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
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;
731 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
732 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
735 * Remove the entry and return an error to the client.
739 brl_lock_cancel(br_lck,
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();
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.
763 if(blocking_lock_record_process(blr)) {
764 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
767 brl_lock_cancel(br_lck,
776 DLIST_REMOVE(blocking_lock_queue, blr);
777 free_blocking_lock_record(blr);
778 recalc_timeout = True;
779 change_to_root_user();
783 change_to_root_user();
786 * We couldn't get the locks for this record on the list.
787 * If the time has expired, return a lock error.
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);
794 * Lock expired - throw away all previously
795 * obtained locks and return lock error.
799 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
800 fsp->fnum, fsp->fsp_name ));
802 brl_lock_cancel(br_lck,
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;
818 if (recalc_timeout) {
819 recalc_brl_timeout();
823 /****************************************************************************
824 Handle a cancel message. Lock already moved onto the cancel queue.
825 *****************************************************************************/
827 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
829 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
832 struct server_id server_id,
836 const char *msg = (const char *)data->data;
837 blocking_lock_record *blr;
839 if (data->data == NULL) {
840 smb_panic("process_blocking_lock_cancel_message: null msg");
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");
849 memcpy(&blr, msg, sizeof(blr));
850 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
852 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
855 blocking_lock_reply_error(blr, err);
856 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
857 free_blocking_lock_record(blr);
860 /****************************************************************************
861 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
862 *****************************************************************************/
864 bool blocking_lock_cancel(files_struct *fsp,
868 enum brl_flavour lock_flav,
869 unsigned char locktype,
872 static bool initialized;
873 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
874 blocking_lock_record *blr;
877 /* Register our message. */
878 messaging_register(smbd_messaging_context(), NULL,
879 MSG_SMB_BLOCKING_LOCK_CANCEL,
880 process_blocking_lock_cancel_message);
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) {
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)) {
906 /* Move to cancelled queue. */
907 DLIST_REMOVE(blocking_lock_queue, blr);
908 DLIST_ADD(blocking_lock_cancelled_queue, blr);
910 /* Create the message. */
911 memcpy(msg, &blr, sizeof(blr));
912 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
914 messaging_send_buf(smbd_messaging_context(), procid_self(),
915 MSG_SMB_BLOCKING_LOCK_CANCEL,
916 (uint8 *)&msg, sizeof(msg));