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/>.
21 #include "smbd/globals.h"
24 #define DBGC_CLASS DBGC_LOCKING
26 /****************************************************************************
27 Determine if this is a secondary element of a chained SMB.
28 **************************************************************************/
30 static void received_unlock_msg(struct messaging_context
*msg
,
33 struct server_id server_id
,
36 static void brl_timeout_fn(struct event_context
*event_ctx
,
37 struct timed_event
*te
,
41 SMB_ASSERT(brl_timeout
== te
);
42 TALLOC_FREE(brl_timeout
);
44 change_to_root_user(); /* TODO: Possibly run all timed events as
47 process_blocking_lock_queue();
50 /****************************************************************************
51 After a change to blocking_lock_queue, recalculate the timed_event for the
53 ****************************************************************************/
55 static bool recalc_brl_timeout(void)
57 struct blocking_lock_record
*blr
;
58 struct timeval next_timeout
;
60 TALLOC_FREE(brl_timeout
);
62 next_timeout
= timeval_zero();
64 for (blr
= blocking_lock_queue
; blr
; blr
= blr
->next
) {
65 if (timeval_is_zero(&blr
->expire_time
)) {
67 * If we're blocked on pid 0xFFFFFFFF this is
68 * a POSIX lock, so calculate a timeout of
69 * 10 seconds into the future.
71 if (blr
->blocking_pid
== 0xFFFFFFFF) {
72 struct timeval psx_to
= timeval_current_ofs(10, 0);
73 next_timeout
= timeval_min(&next_timeout
, &psx_to
);
79 if (timeval_is_zero(&next_timeout
)) {
80 next_timeout
= blr
->expire_time
;
83 next_timeout
= timeval_min(&next_timeout
,
88 if (timeval_is_zero(&next_timeout
)) {
89 DEBUG(10, ("Next timeout = Infinite.\n"));
94 struct timeval cur
, from_now
;
96 cur
= timeval_current();
97 from_now
= timeval_until(&cur
, &next_timeout
);
98 DEBUG(10, ("Next timeout = %d.%d seconds from now.\n",
99 (int)from_now
.tv_sec
, (int)from_now
.tv_usec
));
102 if (!(brl_timeout
= event_add_timed(smbd_event_context(), NULL
,
104 brl_timeout_fn
, NULL
))) {
112 /****************************************************************************
113 Function to push a blocking lock request onto the lock queue.
114 ****************************************************************************/
116 bool push_blocking_lock_request( struct byte_range_lock
*br_lck
,
117 struct smb_request
*req
,
122 enum brl_type lock_type
,
123 enum brl_flavour lock_flav
,
126 uint32_t blocking_pid
)
128 struct blocking_lock_record
*blr
;
131 if(req_is_in_chain(req
)) {
132 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
137 * Now queue an entry on the blocking lock queue. We setup
138 * the expiration time here.
141 blr
= talloc(NULL
, struct blocking_lock_record
);
143 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
151 if (lock_timeout
== -1) {
152 blr
->expire_time
.tv_sec
= 0;
153 blr
->expire_time
.tv_usec
= 0; /* Never expire. */
155 blr
->expire_time
= timeval_current_ofs(lock_timeout
/1000,
156 (lock_timeout
% 1000) * 1000);
158 blr
->lock_num
= lock_num
;
159 blr
->lock_pid
= lock_pid
;
160 blr
->blocking_pid
= blocking_pid
;
161 blr
->lock_flav
= lock_flav
;
162 blr
->lock_type
= lock_type
;
163 blr
->offset
= offset
;
166 /* Specific brl_lock() implementations can fill this in. */
167 blr
->blr_private
= NULL
;
169 /* Add a pending lock record for this. */
170 status
= brl_lock(smbd_messaging_context(),
176 lock_type
== READ_LOCK
? PENDING_READ_LOCK
: PENDING_WRITE_LOCK
,
178 lock_timeout
? True
: False
, /* blocking_lock. */
182 if (!NT_STATUS_IS_OK(status
)) {
183 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
188 SMB_PERFCOUNT_DEFER_OP(&req
->pcd
, &req
->pcd
);
189 blr
->req
= talloc_move(blr
, &req
);
191 DLIST_ADD_END(blocking_lock_queue
, blr
, struct blocking_lock_record
*);
192 recalc_brl_timeout();
194 /* Ensure we'll receive messages when this is unlocked. */
195 if (!blocking_lock_unlock_state
) {
196 messaging_register(smbd_messaging_context(), NULL
,
197 MSG_SMB_UNLOCK
, received_unlock_msg
);
198 blocking_lock_unlock_state
= true;
201 DEBUG(3,("push_blocking_lock_request: lock request blocked with "
202 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
203 (unsigned int)blr
->expire_time
.tv_sec
,
204 (unsigned int)blr
->expire_time
.tv_usec
, lock_timeout
,
205 blr
->fsp
->fnum
, blr
->fsp
->fsp_name
));
210 /****************************************************************************
211 Return a lockingX success SMB.
212 *****************************************************************************/
214 static void reply_lockingX_success(struct blocking_lock_record
*blr
)
216 reply_outbuf(blr
->req
, 2, 0);
219 * As this message is a lockingX call we must handle
220 * any following chained message correctly.
221 * This is normally handled in construct_reply(),
222 * but as that calls switch_message, we can't use
223 * that here and must set up the chain info manually.
226 chain_reply(blr
->req
);
227 TALLOC_FREE(blr
->req
->outbuf
);
230 /****************************************************************************
231 Return a generic lock fail error blocking call.
232 *****************************************************************************/
234 static void generic_blocking_lock_error(struct blocking_lock_record
*blr
, NTSTATUS status
)
236 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
237 FILE_LOCK_CONFLICT! (tridge) */
238 if (NT_STATUS_EQUAL(status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
239 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
242 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_LOCK_CONFLICT
)) {
243 /* Store the last lock error. */
244 files_struct
*fsp
= blr
->fsp
;
247 fsp
->last_lock_failure
.context
.smbpid
= blr
->lock_pid
;
248 fsp
->last_lock_failure
.context
.tid
= fsp
->conn
->cnum
;
249 fsp
->last_lock_failure
.context
.pid
= procid_self();
250 fsp
->last_lock_failure
.start
= blr
->offset
;
251 fsp
->last_lock_failure
.size
= blr
->count
;
252 fsp
->last_lock_failure
.fnum
= fsp
->fnum
;
253 fsp
->last_lock_failure
.lock_type
= READ_LOCK
; /* Don't care. */
254 fsp
->last_lock_failure
.lock_flav
= blr
->lock_flav
;
258 reply_nterror(blr
->req
, status
);
259 if (!srv_send_smb(smbd_server_fd(), (char *)blr
->req
->outbuf
,
260 true, blr
->req
->seqnum
+1,
261 blr
->req
->encrypted
, NULL
)) {
262 exit_server_cleanly("generic_blocking_lock_error: srv_send_smb failed.");
264 TALLOC_FREE(blr
->req
->outbuf
);
267 /****************************************************************************
268 Return a lock fail error for a lockingX call. Undo all the locks we have
270 *****************************************************************************/
272 static void reply_lockingX_error(struct blocking_lock_record
*blr
, NTSTATUS status
)
274 files_struct
*fsp
= blr
->fsp
;
275 uint16 num_ulocks
= SVAL(blr
->req
->vwv
+6, 0);
276 uint64_t count
= (uint64_t)0, offset
= (uint64_t) 0;
278 unsigned char locktype
= CVAL(blr
->req
->vwv
+3, 0);
279 bool large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
);
283 data
= (uint8_t *)blr
->req
->buf
284 + ((large_file_format
? 20 : 10)*num_ulocks
);
287 * Data now points at the beginning of the list
288 * of smb_lkrng structs.
292 * Ensure we don't do a remove on the lock that just failed,
293 * as under POSIX rules, if we have a lock already there, we
294 * will delete it (and we shouldn't) .....
297 for(i
= blr
->lock_num
- 1; i
>= 0; i
--) {
300 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
301 count
= get_lock_count( data
, i
, large_file_format
);
302 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
305 * We know err cannot be set as if it was the lock
306 * request would never have been queued. JRA.
309 do_unlock(smbd_messaging_context(),
317 generic_blocking_lock_error(blr
, status
);
320 /****************************************************************************
321 Return a lock fail error.
322 *****************************************************************************/
324 static void blocking_lock_reply_error(struct blocking_lock_record
*blr
, NTSTATUS status
)
326 DEBUG(10, ("Replying with error=%s. BLR = %p\n", nt_errstr(status
), blr
));
328 switch(blr
->req
->cmd
) {
330 reply_lockingX_error(blr
, status
);
334 reply_nterror(blr
->req
, status
);
337 * construct_reply_common has done us the favor to pre-fill
338 * the command field with SMBtranss2 which is wrong :-)
340 SCVAL(blr
->req
->outbuf
,smb_com
,SMBtrans2
);
342 if (!srv_send_smb(smbd_server_fd(),
343 (char *)blr
->req
->outbuf
,
344 true, blr
->req
->seqnum
+1,
345 IS_CONN_ENCRYPTED(blr
->fsp
->conn
),
347 exit_server_cleanly("blocking_lock_reply_error: "
348 "srv_send_smb failed.");
350 TALLOC_FREE(blr
->req
->outbuf
);
353 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
354 exit_server("PANIC - unknown type on blocking lock queue");
358 /****************************************************************************
359 Attempt to finish off getting all pending blocking locks for a lockingX call.
360 Returns True if we want to be removed from the list.
361 *****************************************************************************/
363 static bool process_lockingX(struct blocking_lock_record
*blr
)
365 unsigned char locktype
= CVAL(blr
->req
->vwv
+3, 0);
366 files_struct
*fsp
= blr
->fsp
;
367 uint16 num_ulocks
= SVAL(blr
->req
->vwv
+6, 0);
368 uint16 num_locks
= SVAL(blr
->req
->vwv
+7, 0);
369 uint64_t count
= (uint64_t)0, offset
= (uint64_t)0;
371 bool large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
);
373 NTSTATUS status
= NT_STATUS_OK
;
375 data
= (uint8_t *)blr
->req
->buf
376 + ((large_file_format
? 20 : 10)*num_ulocks
);
379 * Data now points at the beginning of the list
380 * of smb_lkrng structs.
383 for(; blr
->lock_num
< num_locks
; blr
->lock_num
++) {
384 struct byte_range_lock
*br_lck
= NULL
;
387 lock_pid
= get_lock_pid( data
, blr
->lock_num
, large_file_format
);
388 count
= get_lock_count( data
, blr
->lock_num
, large_file_format
);
389 offset
= get_lock_offset( data
, blr
->lock_num
, large_file_format
, &err
);
392 * We know err cannot be set as if it was the lock
393 * request would never have been queued. JRA.
396 br_lck
= do_lock(smbd_messaging_context(),
401 ((locktype
& LOCKING_ANDX_SHARED_LOCK
) ?
402 READ_LOCK
: WRITE_LOCK
),
411 if (NT_STATUS_IS_ERR(status
)) {
416 if(blr
->lock_num
== num_locks
) {
418 * Success - we got all the locks.
421 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
422 fsp
->fsp_name
, fsp
->fnum
, (unsigned int)locktype
, num_locks
) );
424 reply_lockingX_success(blr
);
428 if (!NT_STATUS_EQUAL(status
,NT_STATUS_LOCK_NOT_GRANTED
) &&
429 !NT_STATUS_EQUAL(status
,NT_STATUS_FILE_LOCK_CONFLICT
)) {
431 * We have other than a "can't get lock"
432 * error. Free any locks we had and return an error.
433 * Return True so we get dequeued.
435 blocking_lock_reply_error(blr
, status
);
440 * Still can't get all the locks - keep waiting.
443 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
445 blr
->lock_num
, num_locks
, fsp
->fsp_name
, fsp
->fnum
));
450 /****************************************************************************
451 Attempt to get the posix lock request from a SMBtrans2 call.
452 Returns True if we want to be removed from the list.
453 *****************************************************************************/
455 static bool process_trans2(struct blocking_lock_record
*blr
)
459 struct byte_range_lock
*br_lck
= do_lock(smbd_messaging_context(),
472 if (!NT_STATUS_IS_OK(status
)) {
473 if (ERROR_WAS_LOCK_DENIED(status
)) {
474 /* Still can't get the lock, just keep waiting. */
478 * We have other than a "can't get lock"
479 * error. Send an error and return True so we get dequeued.
481 blocking_lock_reply_error(blr
, status
);
485 /* We finally got the lock, return success. */
488 /* Fake up max_data_bytes here - we know it fits. */
489 send_trans2_replies(blr
->fsp
->conn
, blr
->req
, params
, 2, NULL
, 0, 0xffff);
494 /****************************************************************************
495 Process a blocking lock SMB.
496 Returns True if we want to be removed from the list.
497 *****************************************************************************/
499 static bool blocking_lock_record_process(struct blocking_lock_record
*blr
)
501 switch(blr
->req
->cmd
) {
503 return process_lockingX(blr
);
506 return process_trans2(blr
);
508 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
509 exit_server("PANIC - unknown type on blocking lock queue");
511 return False
; /* Keep compiler happy. */
514 /****************************************************************************
515 Cancel entries by fnum from the blocking lock pending queue.
516 *****************************************************************************/
518 void cancel_pending_lock_requests_by_fid(files_struct
*fsp
, struct byte_range_lock
*br_lck
)
520 struct blocking_lock_record
*blr
, *blr_cancelled
, *next
= NULL
;
522 for(blr
= blocking_lock_queue
; blr
; blr
= next
) {
523 unsigned char locktype
= 0;
526 if (blr
->fsp
->fnum
!= fsp
->fnum
) {
530 if (blr
->req
->cmd
== SMBlockingX
) {
531 locktype
= CVAL(blr
->req
->vwv
+3, 0);
534 DEBUG(10, ("remove_pending_lock_requests_by_fid - removing "
535 "request type %d for file %s fnum = %d\n",
536 blr
->req
->cmd
, fsp
->fsp_name
, fsp
->fnum
));
538 blr_cancelled
= blocking_lock_cancel(fsp
,
544 NT_STATUS_RANGE_NOT_LOCKED
);
546 SMB_ASSERT(blr_cancelled
== blr
);
548 brl_lock_cancel(br_lck
,
556 /* We're closing the file fsp here, so ensure
557 * we don't have a dangling pointer. */
562 /****************************************************************************
563 Delete entries by mid from the blocking lock pending queue. Always send reply.
564 *****************************************************************************/
566 void remove_pending_lock_requests_by_mid(int mid
)
568 struct blocking_lock_record
*blr
, *next
= NULL
;
570 for(blr
= blocking_lock_queue
; blr
; blr
= next
) {
572 struct byte_range_lock
*br_lck
;
576 if (blr
->req
->mid
!= mid
) {
581 br_lck
= brl_get_locks(talloc_tos(), fsp
);
584 DEBUG(10, ("remove_pending_lock_requests_by_mid - "
585 "removing request type %d for file %s fnum "
586 "= %d\n", blr
->req
->cmd
, fsp
->fsp_name
,
589 brl_lock_cancel(br_lck
,
599 blocking_lock_reply_error(blr
,NT_STATUS_FILE_LOCK_CONFLICT
);
600 DLIST_REMOVE(blocking_lock_queue
, blr
);
605 /****************************************************************************
606 Is this mid a blocking lock request on the queue ?
607 *****************************************************************************/
609 bool blocking_lock_was_deferred(int mid
)
611 struct blocking_lock_record
*blr
, *next
= NULL
;
613 for(blr
= blocking_lock_queue
; blr
; blr
= next
) {
615 if(blr
->req
->mid
== mid
) {
622 /****************************************************************************
623 Set a flag as an unlock request affects one of our pending locks.
624 *****************************************************************************/
626 static void received_unlock_msg(struct messaging_context
*msg
,
629 struct server_id server_id
,
632 DEBUG(10,("received_unlock_msg\n"));
633 process_blocking_lock_queue();
636 /****************************************************************************
637 Process the blocking lock queue. Note that this is only called as root.
638 *****************************************************************************/
640 void process_blocking_lock_queue(void)
642 struct timeval tv_curr
= timeval_current();
643 struct blocking_lock_record
*blr
, *next
= NULL
;
644 bool recalc_timeout
= False
;
647 * Go through the queue and see if we can get any of the locks.
650 for (blr
= blocking_lock_queue
; blr
; blr
= next
) {
655 * Go through the remaining locks and try and obtain them.
656 * The call returns True if all locks were obtained successfully
657 * and False if we still need to wait.
660 DEBUG(10, ("Processing BLR = %p\n", blr
));
662 if(blocking_lock_record_process(blr
)) {
663 struct byte_range_lock
*br_lck
= brl_get_locks(
664 talloc_tos(), blr
->fsp
);
666 DEBUG(10, ("BLR_process returned true: cancelling and "
667 "removing lock. BLR = %p\n", blr
));
670 brl_lock_cancel(br_lck
,
680 DLIST_REMOVE(blocking_lock_queue
, blr
);
682 recalc_timeout
= True
;
687 * We couldn't get the locks for this record on the list.
688 * If the time has expired, return a lock error.
691 if (!timeval_is_zero(&blr
->expire_time
) && timeval_compare(&blr
->expire_time
, &tv_curr
) <= 0) {
692 struct byte_range_lock
*br_lck
= brl_get_locks(
693 talloc_tos(), blr
->fsp
);
695 DEBUG(10, ("Lock timed out! BLR = %p\n", blr
));
698 * Lock expired - throw away all previously
699 * obtained locks and return lock error.
703 DEBUG(5,("process_blocking_lock_queue: "
704 "pending lock fnum = %d for file %s "
705 "timed out.\n", blr
->fsp
->fnum
,
706 blr
->fsp
->fsp_name
));
708 brl_lock_cancel(br_lck
,
718 blocking_lock_reply_error(blr
,NT_STATUS_FILE_LOCK_CONFLICT
);
719 DLIST_REMOVE(blocking_lock_queue
, blr
);
721 recalc_timeout
= True
;
725 if (recalc_timeout
) {
726 recalc_brl_timeout();
730 /****************************************************************************
731 Handle a cancel message. Lock already moved onto the cancel queue.
732 *****************************************************************************/
734 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(struct blocking_lock_record *) + sizeof(NTSTATUS))
736 static void process_blocking_lock_cancel_message(struct messaging_context
*ctx
,
739 struct server_id server_id
,
743 const char *msg
= (const char *)data
->data
;
744 struct blocking_lock_record
*blr
;
746 if (data
->data
== NULL
) {
747 smb_panic("process_blocking_lock_cancel_message: null msg");
750 if (data
->length
!= MSG_BLOCKING_LOCK_CANCEL_SIZE
) {
751 DEBUG(0, ("process_blocking_lock_cancel_message: "
752 "Got invalid msg len %d\n", (int)data
->length
));
753 smb_panic("process_blocking_lock_cancel_message: bad msg");
756 memcpy(&blr
, msg
, sizeof(blr
));
757 memcpy(&err
, &msg
[sizeof(blr
)], sizeof(NTSTATUS
));
759 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
762 blocking_lock_reply_error(blr
, err
);
763 DLIST_REMOVE(blocking_lock_cancelled_queue
, blr
);
767 /****************************************************************************
768 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
769 Returns the blocking_lock_record that is being cancelled.
770 *****************************************************************************/
772 struct blocking_lock_record
*blocking_lock_cancel(files_struct
*fsp
,
776 enum brl_flavour lock_flav
,
777 unsigned char locktype
,
780 char msg
[MSG_BLOCKING_LOCK_CANCEL_SIZE
];
781 struct blocking_lock_record
*blr
;
783 if (!blocking_lock_cancel_state
) {
784 /* Register our message. */
785 messaging_register(smbd_messaging_context(), NULL
,
786 MSG_SMB_BLOCKING_LOCK_CANCEL
,
787 process_blocking_lock_cancel_message
);
789 blocking_lock_cancel_state
= True
;
792 for (blr
= blocking_lock_queue
; blr
; blr
= blr
->next
) {
793 if (fsp
== blr
->fsp
&&
794 lock_pid
== blr
->lock_pid
&&
795 offset
== blr
->offset
&&
796 count
== blr
->count
&&
797 lock_flav
== blr
->lock_flav
) {
806 /* Check the flags are right. */
807 if (blr
->req
->cmd
== SMBlockingX
&&
808 (locktype
& LOCKING_ANDX_LARGE_FILES
) !=
809 (CVAL(blr
->req
->vwv
+3, 0) & LOCKING_ANDX_LARGE_FILES
)) {
813 /* Move to cancelled queue. */
814 DLIST_REMOVE(blocking_lock_queue
, blr
);
815 DLIST_ADD(blocking_lock_cancelled_queue
, blr
);
817 /* Create the message. */
818 memcpy(msg
, &blr
, sizeof(blr
));
819 memcpy(&msg
[sizeof(blr
)], &err
, sizeof(NTSTATUS
));
821 messaging_send_buf(smbd_messaging_context(), procid_self(),
822 MSG_SMB_BLOCKING_LOCK_CANCEL
,
823 (uint8
*)&msg
, sizeof(msg
));