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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DBGC_CLASS DBGC_LOCKING
27 /****************************************************************************
28 This is the structure to queue to implement blocking locks.
29 notify. It consists of the requesting SMB and the expiry time.
30 *****************************************************************************/
32 typedef struct _blocking_lock_record {
33 struct _blocking_lock_record *next;
34 struct _blocking_lock_record *prev;
37 struct timeval expire_time;
42 uint32 blocking_pid; /* PID that blocks us. */
43 enum brl_flavour lock_flav;
44 enum brl_type lock_type;
47 } blocking_lock_record;
49 /* dlink list we store pending lock records on. */
50 static blocking_lock_record *blocking_lock_queue;
52 /* dlink list we move cancelled lock records onto. */
53 static blocking_lock_record *blocking_lock_cancelled_queue;
55 /* The event that makes us process our blocking lock queue */
56 static struct timed_event *brl_timeout;
58 /****************************************************************************
59 Destructor for the above structure.
60 ****************************************************************************/
62 static void free_blocking_lock_record(blocking_lock_record *blr)
64 SAFE_FREE(blr->inbuf);
68 /****************************************************************************
69 Determine if this is a secondary element of a chained SMB.
70 **************************************************************************/
72 static BOOL in_chained_smb(void)
74 return (chain_size != 0);
77 static void received_unlock_msg(struct messaging_context *msg,
80 struct server_id server_id,
82 static void process_blocking_lock_queue(void);
84 static void brl_timeout_fn(struct event_context *event_ctx,
85 struct timed_event *te,
86 const struct timeval *now,
89 SMB_ASSERT(brl_timeout == te);
90 TALLOC_FREE(brl_timeout);
92 change_to_root_user(); /* TODO: Possibly run all timed events as
95 process_blocking_lock_queue();
98 /****************************************************************************
99 After a change to blocking_lock_queue, recalculate the timed_event for the
101 ****************************************************************************/
103 static BOOL recalc_brl_timeout(void)
105 blocking_lock_record *brl;
106 struct timeval next_timeout;
108 TALLOC_FREE(brl_timeout);
110 next_timeout = timeval_zero();
112 for (brl = blocking_lock_queue; brl; brl = brl->next) {
113 if (timeval_is_zero(&brl->expire_time)) {
115 * If we're blocked on pid 0xFFFFFFFF this is
116 * a POSIX lock, so calculate a timeout of
117 * 10 seconds into the future.
119 if (brl->blocking_pid == 0xFFFFFFFF) {
120 struct timeval psx_to = timeval_current_ofs(10, 0);
121 next_timeout = timeval_min(&next_timeout, &psx_to);
127 if (timeval_is_zero(&next_timeout)) {
128 next_timeout = brl->expire_time;
131 next_timeout = timeval_min(&next_timeout,
136 if (timeval_is_zero(&next_timeout)) {
140 if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
141 next_timeout, "brl_timeout",
142 brl_timeout_fn, NULL))) {
150 /****************************************************************************
151 Function to push a blocking lock request onto the lock queue.
152 ****************************************************************************/
154 BOOL push_blocking_lock_request( struct byte_range_lock *br_lck,
155 char *inbuf, int length,
160 enum brl_type lock_type,
161 enum brl_flavour lock_flav,
166 static BOOL set_lock_msg;
167 blocking_lock_record *blr;
170 if(in_chained_smb() ) {
171 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
176 * Now queue an entry on the blocking lock queue. We setup
177 * the expiration time here.
180 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
181 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
188 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
189 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
194 blr->com_type = CVAL(inbuf,smb_com);
196 if (lock_timeout == -1) {
197 blr->expire_time.tv_sec = 0;
198 blr->expire_time.tv_usec = 0; /* Never expire. */
200 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
201 (lock_timeout % 1000) * 1000);
203 blr->lock_num = lock_num;
204 blr->lock_pid = lock_pid;
205 blr->blocking_pid = blocking_pid;
206 blr->lock_flav = lock_flav;
207 blr->lock_type = lock_type;
208 blr->offset = offset;
210 memcpy(blr->inbuf, inbuf, length);
211 blr->length = length;
213 /* Add a pending lock record for this. */
214 status = brl_lock(smbd_messaging_context(), br_lck,
219 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
221 lock_timeout ? True : False, /* blocking_lock. */
224 if (!NT_STATUS_IS_OK(status)) {
225 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
226 DLIST_REMOVE(blocking_lock_queue, blr);
227 free_blocking_lock_record(blr);
231 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
232 recalc_brl_timeout();
234 /* Ensure we'll receive messages when this is unlocked. */
236 messaging_register(smbd_messaging_context(), NULL,
237 MSG_SMB_UNLOCK, received_unlock_msg);
241 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with "
242 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
243 length, (unsigned int)blr->expire_time.tv_sec,
244 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
245 blr->fsp->fnum, blr->fsp->fsp_name ));
247 /* Push the MID of this packet on the signing queue. */
248 srv_defer_sign_response(SVAL(inbuf,smb_mid));
253 /****************************************************************************
254 Return a smd with a given size.
255 *****************************************************************************/
257 static void send_blocking_reply(char *outbuf, int outsize, const char *inbuf)
260 smb_setlen(inbuf, outbuf,outsize - 4);
263 if (!send_smb(smbd_server_fd(),outbuf)) {
264 exit_server_cleanly("send_blocking_reply: send_smb failed.");
268 /****************************************************************************
269 Return a lockingX success SMB.
270 *****************************************************************************/
272 static void reply_lockingX_success(blocking_lock_record *blr)
274 char *outbuf = get_OutBuffer();
275 int bufsize = BUFFER_SIZE;
276 char *inbuf = blr->inbuf;
279 construct_reply_common(inbuf, outbuf);
280 set_message(inbuf,outbuf,2,0,True);
283 * As this message is a lockingX call we must handle
284 * any following chained message correctly.
285 * This is normally handled in construct_reply(),
286 * but as that calls switch_message, we can't use
287 * that here and must set up the chain info manually.
290 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
292 outsize += chain_size;
294 send_blocking_reply(outbuf,outsize,inbuf);
297 /****************************************************************************
298 Return a generic lock fail error blocking call.
299 *****************************************************************************/
301 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
303 char *outbuf = get_OutBuffer();
304 char *inbuf = blr->inbuf;
305 construct_reply_common(inbuf, outbuf);
307 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
308 FILE_LOCK_CONFLICT! (tridge) */
309 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
310 status = NT_STATUS_FILE_LOCK_CONFLICT;
313 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
314 /* Store the last lock error. */
315 files_struct *fsp = blr->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 if (!send_smb(smbd_server_fd(),outbuf)) {
329 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
333 /****************************************************************************
334 Return a lock fail error for a lockingX call. Undo all the locks we have
336 *****************************************************************************/
338 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
340 char *inbuf = blr->inbuf;
341 files_struct *fsp = blr->fsp;
342 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
343 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
345 unsigned char locktype = CVAL(inbuf,smb_vwv3);
346 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
350 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
353 * Data now points at the beginning of the list
354 * of smb_lkrng structs.
358 * Ensure we don't do a remove on the lock that just failed,
359 * as under POSIX rules, if we have a lock already there, we
360 * will delete it (and we shouldn't) .....
363 for(i = blr->lock_num - 1; i >= 0; i--) {
366 lock_pid = get_lock_pid( data, i, large_file_format);
367 count = get_lock_count( data, i, large_file_format);
368 offset = get_lock_offset( data, i, large_file_format, &err);
371 * We know err cannot be set as if it was the lock
372 * request would never have been queued. JRA.
375 do_unlock(smbd_messaging_context(),
383 generic_blocking_lock_error(blr, status);
386 /****************************************************************************
387 Return a lock fail error.
388 *****************************************************************************/
390 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
392 switch(blr->com_type) {
394 reply_lockingX_error(blr, status);
399 char *outbuf = get_OutBuffer();
400 char *inbuf = blr->inbuf;
401 construct_reply_common(inbuf, outbuf);
402 /* construct_reply_common has done us the favor to pre-fill the
403 * command field with SMBtranss2 which is wrong :-)
405 SCVAL(outbuf,smb_com,SMBtrans2);
407 if (!send_smb(smbd_server_fd(),outbuf)) {
408 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
413 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
414 exit_server("PANIC - unknown type on blocking lock queue");
418 /****************************************************************************
419 Attempt to finish off getting all pending blocking locks for a lockingX call.
420 Returns True if we want to be removed from the list.
421 *****************************************************************************/
423 static BOOL process_lockingX(blocking_lock_record *blr)
425 char *inbuf = blr->inbuf;
426 unsigned char locktype = CVAL(inbuf,smb_vwv3);
427 files_struct *fsp = blr->fsp;
428 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
429 uint16 num_locks = SVAL(inbuf,smb_vwv7);
430 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
432 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
434 NTSTATUS status = NT_STATUS_OK;
436 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
439 * Data now points at the beginning of the list
440 * of smb_lkrng structs.
443 for(; blr->lock_num < num_locks; blr->lock_num++) {
444 struct byte_range_lock *br_lck = NULL;
447 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
448 count = get_lock_count( data, blr->lock_num, large_file_format);
449 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
452 * We know err cannot be set as if it was the lock
453 * request would never have been queued. JRA.
456 br_lck = do_lock(smbd_messaging_context(),
461 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
462 READ_LOCK : WRITE_LOCK),
470 if (NT_STATUS_IS_ERR(status)) {
475 if(blr->lock_num == num_locks) {
477 * Success - we got all the locks.
480 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
481 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
483 reply_lockingX_success(blr);
485 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
486 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
488 * We have other than a "can't get lock"
489 * error. Free any locks we had and return an error.
490 * Return True so we get dequeued.
493 blocking_lock_reply_error(blr, status);
498 * Still can't get all the locks - keep waiting.
501 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
503 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
508 /****************************************************************************
509 Attempt to get the posix lock request from a SMBtrans2 call.
510 Returns True if we want to be removed from the list.
511 *****************************************************************************/
513 static BOOL process_trans2(blocking_lock_record *blr)
515 char *inbuf = blr->inbuf;
519 struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
531 if (!NT_STATUS_IS_OK(status)) {
532 if (ERROR_WAS_LOCK_DENIED(status)) {
533 /* Still can't get the lock, just keep waiting. */
537 * We have other than a "can't get lock"
538 * error. Send an error and return True so we get dequeued.
540 blocking_lock_reply_error(blr, status);
544 /* We finally got the lock, return success. */
545 outbuf = get_OutBuffer();
546 construct_reply_common(inbuf, outbuf);
547 SCVAL(outbuf,smb_com,SMBtrans2);
549 /* Fake up max_data_bytes here - we know it fits. */
550 send_trans2_replies(inbuf, outbuf, max_send, params, 2, NULL, 0, 0xffff);
555 /****************************************************************************
556 Process a blocking lock SMB.
557 Returns True if we want to be removed from the list.
558 *****************************************************************************/
560 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
562 switch(blr->com_type) {
564 return process_lockingX(blr);
567 return process_trans2(blr);
569 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
570 exit_server("PANIC - unknown type on blocking lock queue");
572 return False; /* Keep compiler happy. */
575 /****************************************************************************
576 Cancel entries by fnum from the blocking lock pending queue.
577 *****************************************************************************/
579 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
581 blocking_lock_record *blr, *next = NULL;
583 for(blr = blocking_lock_queue; blr; blr = next) {
585 if(blr->fsp->fnum == fsp->fnum) {
586 unsigned char locktype = 0;
588 if (blr->com_type == SMBlockingX) {
589 locktype = CVAL(blr->inbuf,smb_vwv3);
593 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
594 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
596 brl_lock_cancel(br_lck,
603 blocking_lock_cancel(fsp,
609 NT_STATUS_RANGE_NOT_LOCKED);
615 /****************************************************************************
616 Delete entries by mid from the blocking lock pending queue. Always send reply.
617 *****************************************************************************/
619 void remove_pending_lock_requests_by_mid(int mid)
621 blocking_lock_record *blr, *next = NULL;
623 for(blr = blocking_lock_queue; blr; blr = next) {
625 if(SVAL(blr->inbuf,smb_mid) == mid) {
626 files_struct *fsp = blr->fsp;
627 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
630 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
631 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
633 brl_lock_cancel(br_lck,
642 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
643 DLIST_REMOVE(blocking_lock_queue, blr);
644 free_blocking_lock_record(blr);
649 /****************************************************************************
650 Is this mid a blocking lock request on the queue ?
651 *****************************************************************************/
653 BOOL blocking_lock_was_deferred(int mid)
655 blocking_lock_record *blr, *next = NULL;
657 for(blr = blocking_lock_queue; blr; blr = next) {
659 if(SVAL(blr->inbuf,smb_mid) == mid) {
666 /****************************************************************************
667 Set a flag as an unlock request affects one of our pending locks.
668 *****************************************************************************/
670 static void received_unlock_msg(struct messaging_context *msg,
673 struct server_id server_id,
676 DEBUG(10,("received_unlock_msg\n"));
677 process_blocking_lock_queue();
680 /****************************************************************************
681 Process the blocking lock queue. Note that this is only called as root.
682 *****************************************************************************/
684 static void process_blocking_lock_queue(void)
686 struct timeval tv_curr = timeval_current();
687 blocking_lock_record *blr, *next = NULL;
688 BOOL recalc_timeout = False;
691 * Go through the queue and see if we can get any of the locks.
694 for (blr = blocking_lock_queue; blr; blr = next) {
695 connection_struct *conn = NULL;
697 files_struct *fsp = NULL;
702 * Ensure we don't have any old chain_fsp values
709 conn = conn_find(SVAL(blr->inbuf,smb_tid));
710 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
711 SVAL(blr->inbuf,smb_uid);
713 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
714 fsp->fnum, fsp->fsp_name ));
716 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
717 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
720 * Lock expired - throw away all previously
721 * obtained locks and return lock error.
725 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
726 fsp->fnum, fsp->fsp_name ));
728 brl_lock_cancel(br_lck,
737 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
738 DLIST_REMOVE(blocking_lock_queue, blr);
739 free_blocking_lock_record(blr);
740 recalc_timeout = True;
744 if(!change_to_user(conn,vuid)) {
745 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
748 * Remove the entry and return an error to the client.
752 brl_lock_cancel(br_lck,
761 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
763 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
764 DLIST_REMOVE(blocking_lock_queue, blr);
765 free_blocking_lock_record(blr);
766 recalc_timeout = True;
770 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
771 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
774 * Remove the entry and return an error to the client.
778 brl_lock_cancel(br_lck,
787 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
788 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
789 DLIST_REMOVE(blocking_lock_queue, blr);
790 free_blocking_lock_record(blr);
791 recalc_timeout = True;
792 change_to_root_user();
797 * Go through the remaining locks and try and obtain them.
798 * The call returns True if all locks were obtained successfully
799 * and False if we still need to wait.
802 if(blocking_lock_record_process(blr)) {
803 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
806 brl_lock_cancel(br_lck,
815 DLIST_REMOVE(blocking_lock_queue, blr);
816 free_blocking_lock_record(blr);
817 recalc_timeout = True;
819 change_to_root_user();
822 if (recalc_timeout) {
823 recalc_brl_timeout();
827 /****************************************************************************
828 Handle a cancel message. Lock already moved onto the cancel queue.
829 *****************************************************************************/
831 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
833 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
836 struct server_id server_id,
840 const char *msg = (const char *)data->data;
841 blocking_lock_record *blr;
843 if (data->data == NULL) {
844 smb_panic("process_blocking_lock_cancel_message: null msg");
847 if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
848 DEBUG(0, ("process_blocking_lock_cancel_message: "
849 "Got invalid msg len %d\n", (int)data->length));
850 smb_panic("process_blocking_lock_cancel_message: bad msg");
853 memcpy(&blr, msg, sizeof(blr));
854 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
856 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
859 blocking_lock_reply_error(blr, err);
860 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
861 free_blocking_lock_record(blr);
864 /****************************************************************************
865 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
866 *****************************************************************************/
868 BOOL blocking_lock_cancel(files_struct *fsp,
872 enum brl_flavour lock_flav,
873 unsigned char locktype,
876 static BOOL initialized;
877 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
878 blocking_lock_record *blr;
881 /* Register our message. */
882 messaging_register(smbd_messaging_context(), NULL,
883 MSG_SMB_BLOCKING_LOCK_CANCEL,
884 process_blocking_lock_cancel_message);
889 for (blr = blocking_lock_queue; blr; blr = blr->next) {
890 if (fsp == blr->fsp &&
891 lock_pid == blr->lock_pid &&
892 offset == blr->offset &&
893 count == blr->count &&
894 lock_flav == blr->lock_flav) {
903 /* Check the flags are right. */
904 if (blr->com_type == SMBlockingX &&
905 (locktype & LOCKING_ANDX_LARGE_FILES) !=
906 (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
910 /* Move to cancelled queue. */
911 DLIST_REMOVE(blocking_lock_queue, blr);
912 DLIST_ADD(blocking_lock_cancelled_queue, blr);
914 /* Create the message. */
915 memcpy(msg, &blr, sizeof(blr));
916 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
918 messaging_send_buf(smbd_messaging_context(), procid_self(),
919 MSG_SMB_BLOCKING_LOCK_CANCEL,
920 (uint8 *)&msg, sizeof(msg));