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 const 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 struct smb_request *req;
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. */
544 if (!(req = talloc(tmp_talloc_ctx(), struct smb_request))) {
545 blocking_lock_reply_error(blr, NT_STATUS_NO_MEMORY);
549 init_smb_request(req, (uint8 *)blr->inbuf);
551 SCVAL(req->inbuf, smb_com, SMBtrans2);
553 /* Fake up max_data_bytes here - we know it fits. */
554 send_trans2_replies(req, params, 2, NULL, 0, 0xffff);
559 /****************************************************************************
560 Process a blocking lock SMB.
561 Returns True if we want to be removed from the list.
562 *****************************************************************************/
564 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
566 switch(blr->com_type) {
568 return process_lockingX(blr);
571 return process_trans2(blr);
573 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
574 exit_server("PANIC - unknown type on blocking lock queue");
576 return False; /* Keep compiler happy. */
579 /****************************************************************************
580 Cancel entries by fnum from the blocking lock pending queue.
581 *****************************************************************************/
583 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
585 blocking_lock_record *blr, *next = NULL;
587 for(blr = blocking_lock_queue; blr; blr = next) {
589 if(blr->fsp->fnum == fsp->fnum) {
590 unsigned char locktype = 0;
592 if (blr->com_type == SMBlockingX) {
593 locktype = CVAL(blr->inbuf,smb_vwv3);
597 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
598 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
600 brl_lock_cancel(br_lck,
607 blocking_lock_cancel(fsp,
613 NT_STATUS_RANGE_NOT_LOCKED);
619 /****************************************************************************
620 Delete entries by mid from the blocking lock pending queue. Always send reply.
621 *****************************************************************************/
623 void remove_pending_lock_requests_by_mid(int mid)
625 blocking_lock_record *blr, *next = NULL;
627 for(blr = blocking_lock_queue; blr; blr = next) {
629 if(SVAL(blr->inbuf,smb_mid) == mid) {
630 files_struct *fsp = blr->fsp;
631 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
634 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
635 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
637 brl_lock_cancel(br_lck,
646 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
647 DLIST_REMOVE(blocking_lock_queue, blr);
648 free_blocking_lock_record(blr);
653 /****************************************************************************
654 Is this mid a blocking lock request on the queue ?
655 *****************************************************************************/
657 BOOL blocking_lock_was_deferred(int mid)
659 blocking_lock_record *blr, *next = NULL;
661 for(blr = blocking_lock_queue; blr; blr = next) {
663 if(SVAL(blr->inbuf,smb_mid) == mid) {
670 /****************************************************************************
671 Set a flag as an unlock request affects one of our pending locks.
672 *****************************************************************************/
674 static void received_unlock_msg(struct messaging_context *msg,
677 struct server_id server_id,
680 DEBUG(10,("received_unlock_msg\n"));
681 process_blocking_lock_queue();
684 /****************************************************************************
685 Process the blocking lock queue. Note that this is only called as root.
686 *****************************************************************************/
688 static void process_blocking_lock_queue(void)
690 struct timeval tv_curr = timeval_current();
691 blocking_lock_record *blr, *next = NULL;
692 BOOL recalc_timeout = False;
695 * Go through the queue and see if we can get any of the locks.
698 for (blr = blocking_lock_queue; blr; blr = next) {
699 connection_struct *conn = NULL;
701 files_struct *fsp = NULL;
706 * Ensure we don't have any old chain_fsp values
713 conn = conn_find(SVAL(blr->inbuf,smb_tid));
714 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
715 SVAL(blr->inbuf,smb_uid);
717 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
718 fsp->fnum, fsp->fsp_name ));
720 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
721 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
724 * Lock expired - throw away all previously
725 * obtained locks and return lock error.
729 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
730 fsp->fnum, fsp->fsp_name ));
732 brl_lock_cancel(br_lck,
741 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
742 DLIST_REMOVE(blocking_lock_queue, blr);
743 free_blocking_lock_record(blr);
744 recalc_timeout = True;
748 if(!change_to_user(conn,vuid)) {
749 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
752 * Remove the entry and return an error to the client.
756 brl_lock_cancel(br_lck,
765 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
767 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
768 DLIST_REMOVE(blocking_lock_queue, blr);
769 free_blocking_lock_record(blr);
770 recalc_timeout = True;
774 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
775 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
778 * Remove the entry and return an error to the client.
782 brl_lock_cancel(br_lck,
791 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
792 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
793 DLIST_REMOVE(blocking_lock_queue, blr);
794 free_blocking_lock_record(blr);
795 recalc_timeout = True;
796 change_to_root_user();
801 * Go through the remaining locks and try and obtain them.
802 * The call returns True if all locks were obtained successfully
803 * and False if we still need to wait.
806 if(blocking_lock_record_process(blr)) {
807 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
810 brl_lock_cancel(br_lck,
819 DLIST_REMOVE(blocking_lock_queue, blr);
820 free_blocking_lock_record(blr);
821 recalc_timeout = True;
823 change_to_root_user();
826 if (recalc_timeout) {
827 recalc_brl_timeout();
831 /****************************************************************************
832 Handle a cancel message. Lock already moved onto the cancel queue.
833 *****************************************************************************/
835 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
837 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
840 struct server_id server_id,
844 const char *msg = (const char *)data->data;
845 blocking_lock_record *blr;
847 if (data->data == NULL) {
848 smb_panic("process_blocking_lock_cancel_message: null msg");
851 if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
852 DEBUG(0, ("process_blocking_lock_cancel_message: "
853 "Got invalid msg len %d\n", (int)data->length));
854 smb_panic("process_blocking_lock_cancel_message: bad msg");
857 memcpy(&blr, msg, sizeof(blr));
858 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
860 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
863 blocking_lock_reply_error(blr, err);
864 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
865 free_blocking_lock_record(blr);
868 /****************************************************************************
869 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
870 *****************************************************************************/
872 BOOL blocking_lock_cancel(files_struct *fsp,
876 enum brl_flavour lock_flav,
877 unsigned char locktype,
880 static BOOL initialized;
881 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
882 blocking_lock_record *blr;
885 /* Register our message. */
886 messaging_register(smbd_messaging_context(), NULL,
887 MSG_SMB_BLOCKING_LOCK_CANCEL,
888 process_blocking_lock_cancel_message);
893 for (blr = blocking_lock_queue; blr; blr = blr->next) {
894 if (fsp == blr->fsp &&
895 lock_pid == blr->lock_pid &&
896 offset == blr->offset &&
897 count == blr->count &&
898 lock_flav == blr->lock_flav) {
907 /* Check the flags are right. */
908 if (blr->com_type == SMBlockingX &&
909 (locktype & LOCKING_ANDX_LARGE_FILES) !=
910 (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
914 /* Move to cancelled queue. */
915 DLIST_REMOVE(blocking_lock_queue, blr);
916 DLIST_ADD(blocking_lock_cancelled_queue, blr);
918 /* Create the message. */
919 memcpy(msg, &blr, sizeof(blr));
920 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
922 messaging_send_buf(smbd_messaging_context(), procid_self(),
923 MSG_SMB_BLOCKING_LOCK_CANCEL,
924 (uint8 *)&msg, sizeof(msg));