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(int msg_type, struct server_id src,
78 void *buf, size_t len,
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 const struct timeval *now,
87 SMB_ASSERT(brl_timeout == te);
88 TALLOC_FREE(brl_timeout);
90 change_to_root_user(); /* TODO: Possibly run all timed events as
93 process_blocking_lock_queue();
96 /****************************************************************************
97 After a change to blocking_lock_queue, recalculate the timed_event for the
99 ****************************************************************************/
101 static BOOL recalc_brl_timeout(void)
103 blocking_lock_record *brl;
104 struct timeval next_timeout;
106 TALLOC_FREE(brl_timeout);
108 next_timeout = timeval_zero();
110 for (brl = blocking_lock_queue; brl; brl = brl->next) {
111 if (timeval_is_zero(&brl->expire_time)) {
113 * If we're blocked on pid 0xFFFFFFFF this is
114 * a POSIX lock, so calculate a timeout of
115 * 10 seconds into the future.
117 if (brl->blocking_pid == 0xFFFFFFFF) {
118 struct timeval psx_to = timeval_current_ofs(10, 0);
119 next_timeout = timeval_min(&next_timeout, &psx_to);
125 if (timeval_is_zero(&next_timeout)) {
126 next_timeout = brl->expire_time;
129 next_timeout = timeval_min(&next_timeout,
134 if (timeval_is_zero(&next_timeout)) {
138 if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
139 next_timeout, "brl_timeout",
140 brl_timeout_fn, NULL))) {
148 /****************************************************************************
149 Function to push a blocking lock request onto the lock queue.
150 ****************************************************************************/
152 BOOL push_blocking_lock_request( struct byte_range_lock *br_lck,
153 char *inbuf, int length,
158 enum brl_type lock_type,
159 enum brl_flavour lock_flav,
164 static BOOL set_lock_msg;
165 blocking_lock_record *blr;
168 if(in_chained_smb() ) {
169 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
174 * Now queue an entry on the blocking lock queue. We setup
175 * the expiration time here.
178 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
179 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
186 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
187 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
192 blr->com_type = CVAL(inbuf,smb_com);
194 if (lock_timeout == -1) {
195 blr->expire_time.tv_sec = 0;
196 blr->expire_time.tv_usec = 0; /* Never expire. */
198 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
199 (lock_timeout % 1000) * 1000);
201 blr->lock_num = lock_num;
202 blr->lock_pid = lock_pid;
203 blr->blocking_pid = blocking_pid;
204 blr->lock_flav = lock_flav;
205 blr->lock_type = lock_type;
206 blr->offset = offset;
208 memcpy(blr->inbuf, inbuf, length);
209 blr->length = length;
211 /* Add a pending lock record for this. */
212 status = brl_lock(smbd_messaging_context(), br_lck,
217 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
219 lock_timeout ? True : False, /* blocking_lock. */
222 if (!NT_STATUS_IS_OK(status)) {
223 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
224 DLIST_REMOVE(blocking_lock_queue, blr);
225 free_blocking_lock_record(blr);
229 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
230 recalc_brl_timeout();
232 /* Ensure we'll receive messages when this is unlocked. */
234 message_register(MSG_SMB_UNLOCK, received_unlock_msg,
239 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with "
240 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
241 length, (unsigned int)blr->expire_time.tv_sec,
242 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
243 blr->fsp->fnum, blr->fsp->fsp_name ));
245 /* Push the MID of this packet on the signing queue. */
246 srv_defer_sign_response(SVAL(inbuf,smb_mid));
251 /****************************************************************************
252 Return a smd with a given size.
253 *****************************************************************************/
255 static void send_blocking_reply(char *outbuf, int outsize, const char *inbuf)
258 smb_setlen(inbuf, outbuf,outsize - 4);
261 if (!send_smb(smbd_server_fd(),outbuf)) {
262 exit_server_cleanly("send_blocking_reply: send_smb failed.");
266 /****************************************************************************
267 Return a lockingX success SMB.
268 *****************************************************************************/
270 static void reply_lockingX_success(blocking_lock_record *blr)
272 char *outbuf = get_OutBuffer();
273 int bufsize = BUFFER_SIZE;
274 char *inbuf = blr->inbuf;
277 construct_reply_common(inbuf, outbuf);
278 set_message(inbuf,outbuf,2,0,True);
281 * As this message is a lockingX call we must handle
282 * any following chained message correctly.
283 * This is normally handled in construct_reply(),
284 * but as that calls switch_message, we can't use
285 * that here and must set up the chain info manually.
288 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
290 outsize += chain_size;
292 send_blocking_reply(outbuf,outsize,inbuf);
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 = get_OutBuffer();
302 char *inbuf = blr->inbuf;
303 construct_reply_common(inbuf, outbuf);
305 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
306 FILE_LOCK_CONFLICT! (tridge) */
307 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
308 status = NT_STATUS_FILE_LOCK_CONFLICT;
311 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
312 /* Store the last lock error. */
313 files_struct *fsp = blr->fsp;
315 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
316 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
317 fsp->last_lock_failure.context.pid = procid_self();
318 fsp->last_lock_failure.start = blr->offset;
319 fsp->last_lock_failure.size = blr->count;
320 fsp->last_lock_failure.fnum = fsp->fnum;
321 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
322 fsp->last_lock_failure.lock_flav = blr->lock_flav;
326 if (!send_smb(smbd_server_fd(),outbuf)) {
327 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
331 /****************************************************************************
332 Return a lock fail error for a lockingX call. Undo all the locks we have
334 *****************************************************************************/
336 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
338 char *inbuf = blr->inbuf;
339 files_struct *fsp = blr->fsp;
340 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
341 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
343 unsigned char locktype = CVAL(inbuf,smb_vwv3);
344 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
348 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
351 * Data now points at the beginning of the list
352 * of smb_lkrng structs.
356 * Ensure we don't do a remove on the lock that just failed,
357 * as under POSIX rules, if we have a lock already there, we
358 * will delete it (and we shouldn't) .....
361 for(i = blr->lock_num - 1; i >= 0; i--) {
364 lock_pid = get_lock_pid( data, i, large_file_format);
365 count = get_lock_count( data, i, large_file_format);
366 offset = get_lock_offset( data, i, large_file_format, &err);
369 * We know err cannot be set as if it was the lock
370 * request would never have been queued. JRA.
373 do_unlock(smbd_messaging_context(),
381 generic_blocking_lock_error(blr, status);
384 /****************************************************************************
385 Return a lock fail error.
386 *****************************************************************************/
388 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
390 switch(blr->com_type) {
392 reply_lockingX_error(blr, status);
397 char *outbuf = get_OutBuffer();
398 char *inbuf = blr->inbuf;
399 construct_reply_common(inbuf, outbuf);
400 /* construct_reply_common has done us the favor to pre-fill the
401 * command field with SMBtranss2 which is wrong :-)
403 SCVAL(outbuf,smb_com,SMBtrans2);
405 if (!send_smb(smbd_server_fd(),outbuf)) {
406 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
411 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
412 exit_server("PANIC - unknown type on blocking lock queue");
416 /****************************************************************************
417 Attempt to finish off getting all pending blocking locks for a lockingX call.
418 Returns True if we want to be removed from the list.
419 *****************************************************************************/
421 static BOOL process_lockingX(blocking_lock_record *blr)
423 char *inbuf = blr->inbuf;
424 unsigned char locktype = CVAL(inbuf,smb_vwv3);
425 files_struct *fsp = blr->fsp;
426 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
427 uint16 num_locks = SVAL(inbuf,smb_vwv7);
428 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
430 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
432 NTSTATUS status = NT_STATUS_OK;
434 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
437 * Data now points at the beginning of the list
438 * of smb_lkrng structs.
441 for(; blr->lock_num < num_locks; blr->lock_num++) {
442 struct byte_range_lock *br_lck = NULL;
445 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
446 count = get_lock_count( data, blr->lock_num, large_file_format);
447 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
450 * We know err cannot be set as if it was the lock
451 * request would never have been queued. JRA.
454 br_lck = do_lock(smbd_messaging_context(),
459 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
460 READ_LOCK : WRITE_LOCK),
468 if (NT_STATUS_IS_ERR(status)) {
473 if(blr->lock_num == num_locks) {
475 * Success - we got all the locks.
478 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
479 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
481 reply_lockingX_success(blr);
483 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
484 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
486 * We have other than a "can't get lock"
487 * error. Free any locks we had and return an error.
488 * Return True so we get dequeued.
491 blocking_lock_reply_error(blr, status);
496 * Still can't get all the locks - keep waiting.
499 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
501 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
506 /****************************************************************************
507 Attempt to get the posix lock request from a SMBtrans2 call.
508 Returns True if we want to be removed from the list.
509 *****************************************************************************/
511 static BOOL process_trans2(blocking_lock_record *blr)
513 char *inbuf = blr->inbuf;
517 struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
529 if (!NT_STATUS_IS_OK(status)) {
530 if (ERROR_WAS_LOCK_DENIED(status)) {
531 /* Still can't get the lock, just keep waiting. */
535 * We have other than a "can't get lock"
536 * error. Send an error and return True so we get dequeued.
538 blocking_lock_reply_error(blr, status);
542 /* We finally got the lock, return success. */
543 outbuf = get_OutBuffer();
544 construct_reply_common(inbuf, outbuf);
545 SCVAL(outbuf,smb_com,SMBtrans2);
547 /* Fake up max_data_bytes here - we know it fits. */
548 send_trans2_replies(inbuf, outbuf, max_send, params, 2, NULL, 0, 0xffff);
553 /****************************************************************************
554 Process a blocking lock SMB.
555 Returns True if we want to be removed from the list.
556 *****************************************************************************/
558 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
560 switch(blr->com_type) {
562 return process_lockingX(blr);
565 return process_trans2(blr);
567 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
568 exit_server("PANIC - unknown type on blocking lock queue");
570 return False; /* Keep compiler happy. */
573 /****************************************************************************
574 Cancel entries by fnum from the blocking lock pending queue.
575 *****************************************************************************/
577 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
579 blocking_lock_record *blr, *next = NULL;
581 for(blr = blocking_lock_queue; blr; blr = next) {
583 if(blr->fsp->fnum == fsp->fnum) {
584 unsigned char locktype = 0;
586 if (blr->com_type == SMBlockingX) {
587 locktype = CVAL(blr->inbuf,smb_vwv3);
591 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
592 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
594 brl_lock_cancel(br_lck,
601 blocking_lock_cancel(fsp,
607 NT_STATUS_RANGE_NOT_LOCKED);
613 /****************************************************************************
614 Delete entries by mid from the blocking lock pending queue. Always send reply.
615 *****************************************************************************/
617 void remove_pending_lock_requests_by_mid(int mid)
619 blocking_lock_record *blr, *next = NULL;
621 for(blr = blocking_lock_queue; blr; blr = next) {
623 if(SVAL(blr->inbuf,smb_mid) == mid) {
624 files_struct *fsp = blr->fsp;
625 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
628 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
629 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
631 brl_lock_cancel(br_lck,
640 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
641 DLIST_REMOVE(blocking_lock_queue, blr);
642 free_blocking_lock_record(blr);
647 /****************************************************************************
648 Is this mid a blocking lock request on the queue ?
649 *****************************************************************************/
651 BOOL blocking_lock_was_deferred(int mid)
653 blocking_lock_record *blr, *next = NULL;
655 for(blr = blocking_lock_queue; blr; blr = next) {
657 if(SVAL(blr->inbuf,smb_mid) == mid) {
664 /****************************************************************************
665 Set a flag as an unlock request affects one of our pending locks.
666 *****************************************************************************/
668 static void received_unlock_msg(int msg_type, struct server_id src,
669 void *buf, size_t len,
672 DEBUG(10,("received_unlock_msg\n"));
673 process_blocking_lock_queue();
676 /****************************************************************************
677 Process the blocking lock queue. Note that this is only called as root.
678 *****************************************************************************/
680 static void process_blocking_lock_queue(void)
682 struct timeval tv_curr = timeval_current();
683 blocking_lock_record *blr, *next = NULL;
684 BOOL recalc_timeout = False;
687 * Go through the queue and see if we can get any of the locks.
690 for (blr = blocking_lock_queue; blr; blr = next) {
691 connection_struct *conn = NULL;
693 files_struct *fsp = NULL;
698 * Ensure we don't have any old chain_fsp values
705 conn = conn_find(SVAL(blr->inbuf,smb_tid));
706 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
707 SVAL(blr->inbuf,smb_uid);
709 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
710 fsp->fnum, fsp->fsp_name ));
712 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
713 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
716 * Lock expired - throw away all previously
717 * obtained locks and return lock error.
721 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
722 fsp->fnum, fsp->fsp_name ));
724 brl_lock_cancel(br_lck,
733 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
734 DLIST_REMOVE(blocking_lock_queue, blr);
735 free_blocking_lock_record(blr);
736 recalc_timeout = True;
740 if(!change_to_user(conn,vuid)) {
741 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
744 * Remove the entry and return an error to the client.
748 brl_lock_cancel(br_lck,
757 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
759 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
760 DLIST_REMOVE(blocking_lock_queue, blr);
761 free_blocking_lock_record(blr);
762 recalc_timeout = True;
766 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
767 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
770 * Remove the entry and return an error to the client.
774 brl_lock_cancel(br_lck,
783 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
784 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
785 DLIST_REMOVE(blocking_lock_queue, blr);
786 free_blocking_lock_record(blr);
787 recalc_timeout = True;
788 change_to_root_user();
793 * Go through the remaining locks and try and obtain them.
794 * The call returns True if all locks were obtained successfully
795 * and False if we still need to wait.
798 if(blocking_lock_record_process(blr)) {
799 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
802 brl_lock_cancel(br_lck,
811 DLIST_REMOVE(blocking_lock_queue, blr);
812 free_blocking_lock_record(blr);
813 recalc_timeout = True;
815 change_to_root_user();
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(int msg_type,
830 struct server_id src,
831 void *buf, size_t len,
835 const char *msg = (const char *)buf;
836 blocking_lock_record *blr;
839 smb_panic("process_blocking_lock_cancel_message: null msg\n");
842 if (len != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
843 DEBUG(0, ("process_blocking_lock_cancel_message: "
844 "Got invalid msg len %d\n", (int)len));
845 smb_panic("process_blocking_lock_cancel_message: bad msg\n");
848 memcpy(&blr, msg, sizeof(blr));
849 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
851 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
854 blocking_lock_reply_error(blr, err);
855 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
856 free_blocking_lock_record(blr);
859 /****************************************************************************
860 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
861 *****************************************************************************/
863 BOOL blocking_lock_cancel(files_struct *fsp,
867 enum brl_flavour lock_flav,
868 unsigned char locktype,
871 static BOOL initialized;
872 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
873 blocking_lock_record *blr;
876 /* Register our message. */
877 message_register(MSG_SMB_BLOCKING_LOCK_CANCEL,
878 process_blocking_lock_cancel_message,
884 for (blr = blocking_lock_queue; blr; blr = blr->next) {
885 if (fsp == blr->fsp &&
886 lock_pid == blr->lock_pid &&
887 offset == blr->offset &&
888 count == blr->count &&
889 lock_flav == blr->lock_flav) {
898 /* Check the flags are right. */
899 if (blr->com_type == SMBlockingX &&
900 (locktype & LOCKING_ANDX_LARGE_FILES) !=
901 (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
905 /* Move to cancelled queue. */
906 DLIST_REMOVE(blocking_lock_queue, blr);
907 DLIST_ADD(blocking_lock_cancelled_queue, blr);
909 /* Create the message. */
910 memcpy(msg, &blr, sizeof(blr));
911 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
913 messaging_send_buf(smbd_messaging_context(), procid_self(),
914 MSG_SMB_BLOCKING_LOCK_CANCEL,
915 (uint8 *)&msg, sizeof(msg));