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 We need a version of timeval_min that treats zero timval as infinite.
52 ****************************************************************************/
54 static struct timeval
timeval_brl_min(const struct timeval
*tv1
,
55 const struct timeval
*tv2
)
57 if (timeval_is_zero(tv1
)) {
60 if (timeval_is_zero(tv2
)) {
63 return timeval_min(tv1
, tv2
);
66 /****************************************************************************
67 After a change to blocking_lock_queue, recalculate the timed_event for the
69 ****************************************************************************/
71 static bool recalc_brl_timeout(void)
73 struct blocking_lock_record
*blr
;
74 struct timeval next_timeout
;
75 int max_brl_timeout
= lp_parm_int(-1, "brl", "recalctime", 5);
77 TALLOC_FREE(brl_timeout
);
79 next_timeout
= timeval_zero();
81 for (blr
= blocking_lock_queue
; blr
; blr
= blr
->next
) {
82 if (timeval_is_zero(&blr
->expire_time
)) {
84 * If we're blocked on pid 0xFFFFFFFF this is
85 * a POSIX lock, so calculate a timeout of
86 * 10 seconds into the future.
88 if (blr
->blocking_pid
== 0xFFFFFFFF) {
89 struct timeval psx_to
= timeval_current_ofs(10, 0);
90 next_timeout
= timeval_brl_min(&next_timeout
, &psx_to
);
96 next_timeout
= timeval_brl_min(&next_timeout
, &blr
->expire_time
);
99 if (timeval_is_zero(&next_timeout
)) {
100 DEBUG(10, ("Next timeout = Infinite.\n"));
105 to account for unclean shutdowns by clients we need a
106 maximum timeout that we use for checking pending locks. If
107 we have any pending locks at all, then check if the pending
108 lock can continue at least every brl:recalctime seconds
111 This saves us needing to do a message_send_all() in the
112 SIGCHLD handler in the parent daemon. That
113 message_send_all() caused O(n^2) work to be done when IP
114 failovers happened in clustered Samba, which could make the
115 entire system unusable for many minutes.
118 if (max_brl_timeout
> 0) {
119 struct timeval min_to
= timeval_current_ofs(max_brl_timeout
, 0);
120 next_timeout
= timeval_min(&next_timeout
, &min_to
);
124 struct timeval cur
, from_now
;
126 cur
= timeval_current();
127 from_now
= timeval_until(&cur
, &next_timeout
);
128 DEBUG(10, ("Next timeout = %d.%d seconds from now.\n",
129 (int)from_now
.tv_sec
, (int)from_now
.tv_usec
));
132 if (!(brl_timeout
= event_add_timed(smbd_event_context(), NULL
,
134 brl_timeout_fn
, NULL
))) {
142 /****************************************************************************
143 Function to push a blocking lock request onto the lock queue.
144 ****************************************************************************/
146 bool push_blocking_lock_request( struct byte_range_lock
*br_lck
,
147 struct smb_request
*req
,
152 enum brl_type lock_type
,
153 enum brl_flavour lock_flav
,
156 uint32_t blocking_pid
)
158 struct blocking_lock_record
*blr
;
161 if(req_is_in_chain(req
)) {
162 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
167 * Now queue an entry on the blocking lock queue. We setup
168 * the expiration time here.
171 blr
= talloc(NULL
, struct blocking_lock_record
);
173 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
181 if (lock_timeout
== -1) {
182 blr
->expire_time
.tv_sec
= 0;
183 blr
->expire_time
.tv_usec
= 0; /* Never expire. */
185 blr
->expire_time
= timeval_current_ofs(lock_timeout
/1000,
186 (lock_timeout
% 1000) * 1000);
188 blr
->lock_num
= lock_num
;
189 blr
->lock_pid
= lock_pid
;
190 blr
->blocking_pid
= blocking_pid
;
191 blr
->lock_flav
= lock_flav
;
192 blr
->lock_type
= lock_type
;
193 blr
->offset
= offset
;
196 /* Specific brl_lock() implementations can fill this in. */
197 blr
->blr_private
= NULL
;
199 /* Add a pending lock record for this. */
200 status
= brl_lock(smbd_messaging_context(),
206 lock_type
== READ_LOCK
? PENDING_READ_LOCK
: PENDING_WRITE_LOCK
,
212 if (!NT_STATUS_IS_OK(status
)) {
213 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
218 SMB_PERFCOUNT_DEFER_OP(&req
->pcd
, &req
->pcd
);
219 blr
->req
= talloc_move(blr
, &req
);
221 DLIST_ADD_END(blocking_lock_queue
, blr
, struct blocking_lock_record
*);
222 recalc_brl_timeout();
224 /* Ensure we'll receive messages when this is unlocked. */
225 if (!blocking_lock_unlock_state
) {
226 messaging_register(smbd_messaging_context(), NULL
,
227 MSG_SMB_UNLOCK
, received_unlock_msg
);
228 blocking_lock_unlock_state
= true;
231 DEBUG(3,("push_blocking_lock_request: lock request blocked with "
232 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
233 (unsigned int)blr
->expire_time
.tv_sec
,
234 (unsigned int)blr
->expire_time
.tv_usec
, lock_timeout
,
235 blr
->fsp
->fnum
, fsp_str_dbg(blr
->fsp
)));
240 /****************************************************************************
241 Return a lockingX success SMB.
242 *****************************************************************************/
244 static void reply_lockingX_success(struct blocking_lock_record
*blr
)
246 reply_outbuf(blr
->req
, 2, 0);
249 * As this message is a lockingX call we must handle
250 * any following chained message correctly.
251 * This is normally handled in construct_reply(),
252 * but as that calls switch_message, we can't use
253 * that here and must set up the chain info manually.
256 chain_reply(blr
->req
);
257 TALLOC_FREE(blr
->req
->outbuf
);
260 /****************************************************************************
261 Return a generic lock fail error blocking call.
262 *****************************************************************************/
264 static void generic_blocking_lock_error(struct blocking_lock_record
*blr
, NTSTATUS status
)
266 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
267 FILE_LOCK_CONFLICT! (tridge) */
268 if (NT_STATUS_EQUAL(status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
269 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
272 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_LOCK_CONFLICT
)) {
273 /* Store the last lock error. */
274 files_struct
*fsp
= blr
->fsp
;
277 fsp
->last_lock_failure
.context
.smbpid
= blr
->lock_pid
;
278 fsp
->last_lock_failure
.context
.tid
= fsp
->conn
->cnum
;
279 fsp
->last_lock_failure
.context
.pid
= procid_self();
280 fsp
->last_lock_failure
.start
= blr
->offset
;
281 fsp
->last_lock_failure
.size
= blr
->count
;
282 fsp
->last_lock_failure
.fnum
= fsp
->fnum
;
283 fsp
->last_lock_failure
.lock_type
= READ_LOCK
; /* Don't care. */
284 fsp
->last_lock_failure
.lock_flav
= blr
->lock_flav
;
288 reply_nterror(blr
->req
, status
);
289 if (!srv_send_smb(smbd_server_fd(), (char *)blr
->req
->outbuf
,
290 true, blr
->req
->seqnum
+1,
291 blr
->req
->encrypted
, NULL
)) {
292 exit_server_cleanly("generic_blocking_lock_error: srv_send_smb failed.");
294 TALLOC_FREE(blr
->req
->outbuf
);
297 /****************************************************************************
298 Return a lock fail error for a lockingX call. Undo all the locks we have
300 *****************************************************************************/
302 static void undo_locks_obtained(struct blocking_lock_record
*blr
)
304 files_struct
*fsp
= blr
->fsp
;
305 uint16 num_ulocks
= SVAL(blr
->req
->vwv
+6, 0);
306 uint64_t count
= (uint64_t)0, offset
= (uint64_t) 0;
308 unsigned char locktype
= CVAL(blr
->req
->vwv
+3, 0);
309 bool large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
);
313 data
= (uint8_t *)blr
->req
->buf
314 + ((large_file_format
? 20 : 10)*num_ulocks
);
317 * Data now points at the beginning of the list
318 * of smb_lkrng structs.
322 * Ensure we don't do a remove on the lock that just failed,
323 * as under POSIX rules, if we have a lock already there, we
324 * will delete it (and we shouldn't) .....
327 for(i
= blr
->lock_num
- 1; i
>= 0; i
--) {
330 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
331 count
= get_lock_count( data
, i
, large_file_format
);
332 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
335 * We know err cannot be set as if it was the lock
336 * request would never have been queued. JRA.
339 do_unlock(smbd_messaging_context(),
348 /****************************************************************************
349 Return a lock fail error.
350 *****************************************************************************/
352 static void blocking_lock_reply_error(struct blocking_lock_record
*blr
, NTSTATUS status
)
354 DEBUG(10, ("Replying with error=%s. BLR = %p\n", nt_errstr(status
), blr
));
356 switch(blr
->req
->cmd
) {
359 * This code can be called during the rundown of a
360 * file after it was already closed. In that case,
361 * blr->fsp==NULL and we do not need to undo any
362 * locks, they are already gone.
364 if (blr
->fsp
!= NULL
) {
365 undo_locks_obtained(blr
);
367 generic_blocking_lock_error(blr
, status
);
371 reply_nterror(blr
->req
, status
);
374 * construct_reply_common has done us the favor to pre-fill
375 * the command field with SMBtranss2 which is wrong :-)
377 SCVAL(blr
->req
->outbuf
,smb_com
,SMBtrans2
);
379 if (!srv_send_smb(smbd_server_fd(),
380 (char *)blr
->req
->outbuf
,
381 true, blr
->req
->seqnum
+1,
382 IS_CONN_ENCRYPTED(blr
->fsp
->conn
),
384 exit_server_cleanly("blocking_lock_reply_error: "
385 "srv_send_smb failed.");
387 TALLOC_FREE(blr
->req
->outbuf
);
390 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
391 exit_server("PANIC - unknown type on blocking lock queue");
395 /****************************************************************************
396 Attempt to finish off getting all pending blocking locks for a lockingX call.
397 Returns True if we want to be removed from the list.
398 *****************************************************************************/
400 static bool process_lockingX(struct blocking_lock_record
*blr
)
402 unsigned char locktype
= CVAL(blr
->req
->vwv
+3, 0);
403 files_struct
*fsp
= blr
->fsp
;
404 uint16 num_ulocks
= SVAL(blr
->req
->vwv
+6, 0);
405 uint16 num_locks
= SVAL(blr
->req
->vwv
+7, 0);
406 uint64_t count
= (uint64_t)0, offset
= (uint64_t)0;
408 bool large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
);
410 NTSTATUS status
= NT_STATUS_OK
;
412 data
= (uint8_t *)blr
->req
->buf
413 + ((large_file_format
? 20 : 10)*num_ulocks
);
416 * Data now points at the beginning of the list
417 * of smb_lkrng structs.
420 for(; blr
->lock_num
< num_locks
; blr
->lock_num
++) {
421 struct byte_range_lock
*br_lck
= NULL
;
424 lock_pid
= get_lock_pid( data
, blr
->lock_num
, large_file_format
);
425 count
= get_lock_count( data
, blr
->lock_num
, large_file_format
);
426 offset
= get_lock_offset( data
, blr
->lock_num
, large_file_format
, &err
);
429 * We know err cannot be set as if it was the lock
430 * request would never have been queued. JRA.
433 br_lck
= do_lock(smbd_messaging_context(),
438 ((locktype
& LOCKING_ANDX_SHARED_LOCK
) ?
439 READ_LOCK
: WRITE_LOCK
),
448 if (NT_STATUS_IS_ERR(status
)) {
453 if(blr
->lock_num
== num_locks
) {
455 * Success - we got all the locks.
458 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d "
459 "num_locks=%d\n", fsp_str_dbg(fsp
), fsp
->fnum
,
460 (unsigned int)locktype
, num_locks
));
462 reply_lockingX_success(blr
);
466 if (!NT_STATUS_EQUAL(status
,NT_STATUS_LOCK_NOT_GRANTED
) &&
467 !NT_STATUS_EQUAL(status
,NT_STATUS_FILE_LOCK_CONFLICT
)) {
469 * We have other than a "can't get lock"
470 * error. Free any locks we had and return an error.
471 * Return True so we get dequeued.
473 blocking_lock_reply_error(blr
, status
);
478 * Still can't get all the locks - keep waiting.
481 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
483 blr
->lock_num
, num_locks
, fsp_str_dbg(fsp
), fsp
->fnum
));
488 /****************************************************************************
489 Attempt to get the posix lock request from a SMBtrans2 call.
490 Returns True if we want to be removed from the list.
491 *****************************************************************************/
493 static bool process_trans2(struct blocking_lock_record
*blr
)
497 struct byte_range_lock
*br_lck
= do_lock(smbd_messaging_context(),
510 if (!NT_STATUS_IS_OK(status
)) {
511 if (ERROR_WAS_LOCK_DENIED(status
)) {
512 /* Still can't get the lock, just keep waiting. */
516 * We have other than a "can't get lock"
517 * error. Send an error and return True so we get dequeued.
519 blocking_lock_reply_error(blr
, status
);
523 /* We finally got the lock, return success. */
526 /* Fake up max_data_bytes here - we know it fits. */
527 send_trans2_replies(blr
->fsp
->conn
, blr
->req
, params
, 2, NULL
, 0, 0xffff);
532 /****************************************************************************
533 Process a blocking lock SMB.
534 Returns True if we want to be removed from the list.
535 *****************************************************************************/
537 static bool blocking_lock_record_process(struct blocking_lock_record
*blr
)
539 switch(blr
->req
->cmd
) {
541 return process_lockingX(blr
);
544 return process_trans2(blr
);
546 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
547 exit_server("PANIC - unknown type on blocking lock queue");
549 return False
; /* Keep compiler happy. */
552 /****************************************************************************
553 Cancel entries by fnum from the blocking lock pending queue.
554 *****************************************************************************/
556 void cancel_pending_lock_requests_by_fid(files_struct
*fsp
, struct byte_range_lock
*br_lck
)
558 struct blocking_lock_record
*blr
, *blr_cancelled
, *next
= NULL
;
560 for(blr
= blocking_lock_queue
; blr
; blr
= next
) {
561 unsigned char locktype
= 0;
564 if (blr
->fsp
->fnum
!= fsp
->fnum
) {
568 if (blr
->req
->cmd
== SMBlockingX
) {
569 locktype
= CVAL(blr
->req
->vwv
+3, 0);
572 DEBUG(10, ("remove_pending_lock_requests_by_fid - removing "
573 "request type %d for file %s fnum = %d\n",
574 blr
->req
->cmd
, fsp_str_dbg(fsp
), fsp
->fnum
));
576 blr_cancelled
= blocking_lock_cancel(fsp
,
582 NT_STATUS_RANGE_NOT_LOCKED
);
584 SMB_ASSERT(blr_cancelled
== blr
);
586 brl_lock_cancel(br_lck
,
594 /* We're closing the file fsp here, so ensure
595 * we don't have a dangling pointer. */
600 /****************************************************************************
601 Delete entries by mid from the blocking lock pending queue. Always send reply.
602 *****************************************************************************/
604 void remove_pending_lock_requests_by_mid(int mid
)
606 struct blocking_lock_record
*blr
, *next
= NULL
;
608 for(blr
= blocking_lock_queue
; blr
; blr
= next
) {
610 struct byte_range_lock
*br_lck
;
614 if (blr
->req
->mid
!= mid
) {
619 br_lck
= brl_get_locks(talloc_tos(), fsp
);
622 DEBUG(10, ("remove_pending_lock_requests_by_mid - "
623 "removing request type %d for file %s fnum "
624 "= %d\n", blr
->req
->cmd
, fsp_str_dbg(fsp
),
627 brl_lock_cancel(br_lck
,
637 blocking_lock_reply_error(blr
,NT_STATUS_FILE_LOCK_CONFLICT
);
638 DLIST_REMOVE(blocking_lock_queue
, blr
);
643 /****************************************************************************
644 Is this mid a blocking lock request on the queue ?
645 *****************************************************************************/
647 bool blocking_lock_was_deferred(int mid
)
649 struct blocking_lock_record
*blr
, *next
= NULL
;
651 for(blr
= blocking_lock_queue
; blr
; blr
= next
) {
653 if(blr
->req
->mid
== mid
) {
660 /****************************************************************************
661 Set a flag as an unlock request affects one of our pending locks.
662 *****************************************************************************/
664 static void received_unlock_msg(struct messaging_context
*msg
,
667 struct server_id server_id
,
670 DEBUG(10,("received_unlock_msg\n"));
671 process_blocking_lock_queue();
674 /****************************************************************************
675 Process the blocking lock queue. Note that this is only called as root.
676 *****************************************************************************/
678 void process_blocking_lock_queue(void)
680 struct timeval tv_curr
= timeval_current();
681 struct blocking_lock_record
*blr
, *next
= NULL
;
684 * Go through the queue and see if we can get any of the locks.
687 for (blr
= blocking_lock_queue
; blr
; blr
= next
) {
692 * Go through the remaining locks and try and obtain them.
693 * The call returns True if all locks were obtained successfully
694 * and False if we still need to wait.
697 DEBUG(10, ("Processing BLR = %p\n", blr
));
699 /* We use set_current_service so connections with
700 * pending locks are not marked as idle.
703 set_current_service(blr
->fsp
->conn
,
704 SVAL(blr
->req
->inbuf
,smb_flg
),
707 if(blocking_lock_record_process(blr
)) {
708 struct byte_range_lock
*br_lck
= brl_get_locks(
709 talloc_tos(), blr
->fsp
);
711 DEBUG(10, ("BLR_process returned true: cancelling and "
712 "removing lock. BLR = %p\n", blr
));
715 brl_lock_cancel(br_lck
,
725 DLIST_REMOVE(blocking_lock_queue
, blr
);
731 * We couldn't get the locks for this record on the list.
732 * If the time has expired, return a lock error.
735 if (!timeval_is_zero(&blr
->expire_time
) && timeval_compare(&blr
->expire_time
, &tv_curr
) <= 0) {
736 struct byte_range_lock
*br_lck
= brl_get_locks(
737 talloc_tos(), blr
->fsp
);
739 DEBUG(10, ("Lock timed out! BLR = %p\n", blr
));
742 * Lock expired - throw away all previously
743 * obtained locks and return lock error.
747 DEBUG(5,("process_blocking_lock_queue: "
748 "pending lock fnum = %d for file %s "
749 "timed out.\n", blr
->fsp
->fnum
,
750 fsp_str_dbg(blr
->fsp
)));
752 brl_lock_cancel(br_lck
,
762 blocking_lock_reply_error(blr
,NT_STATUS_FILE_LOCK_CONFLICT
);
763 DLIST_REMOVE(blocking_lock_queue
, blr
);
768 recalc_brl_timeout();
771 /****************************************************************************
772 Handle a cancel message. Lock already moved onto the cancel queue.
773 *****************************************************************************/
775 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(struct blocking_lock_record *) + sizeof(NTSTATUS))
777 static void process_blocking_lock_cancel_message(struct messaging_context
*ctx
,
780 struct server_id server_id
,
784 const char *msg
= (const char *)data
->data
;
785 struct blocking_lock_record
*blr
;
787 if (data
->data
== NULL
) {
788 smb_panic("process_blocking_lock_cancel_message: null msg");
791 if (data
->length
!= MSG_BLOCKING_LOCK_CANCEL_SIZE
) {
792 DEBUG(0, ("process_blocking_lock_cancel_message: "
793 "Got invalid msg len %d\n", (int)data
->length
));
794 smb_panic("process_blocking_lock_cancel_message: bad msg");
797 memcpy(&blr
, msg
, sizeof(blr
));
798 memcpy(&err
, &msg
[sizeof(blr
)], sizeof(NTSTATUS
));
800 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
803 blocking_lock_reply_error(blr
, err
);
804 DLIST_REMOVE(blocking_lock_cancelled_queue
, blr
);
808 /****************************************************************************
809 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
810 Returns the blocking_lock_record that is being cancelled.
811 *****************************************************************************/
813 struct blocking_lock_record
*blocking_lock_cancel(files_struct
*fsp
,
817 enum brl_flavour lock_flav
,
818 unsigned char locktype
,
821 char msg
[MSG_BLOCKING_LOCK_CANCEL_SIZE
];
822 struct blocking_lock_record
*blr
;
824 if (!blocking_lock_cancel_state
) {
825 /* Register our message. */
826 messaging_register(smbd_messaging_context(), NULL
,
827 MSG_SMB_BLOCKING_LOCK_CANCEL
,
828 process_blocking_lock_cancel_message
);
830 blocking_lock_cancel_state
= True
;
833 for (blr
= blocking_lock_queue
; blr
; blr
= blr
->next
) {
834 if (fsp
== blr
->fsp
&&
835 lock_pid
== blr
->lock_pid
&&
836 offset
== blr
->offset
&&
837 count
== blr
->count
&&
838 lock_flav
== blr
->lock_flav
) {
847 /* Check the flags are right. */
848 if (blr
->req
->cmd
== SMBlockingX
&&
849 (locktype
& LOCKING_ANDX_LARGE_FILES
) !=
850 (CVAL(blr
->req
->vwv
+3, 0) & LOCKING_ANDX_LARGE_FILES
)) {
854 /* Move to cancelled queue. */
855 DLIST_REMOVE(blocking_lock_queue
, blr
);
856 DLIST_ADD(blocking_lock_cancelled_queue
, blr
);
858 /* Create the message. */
859 memcpy(msg
, &blr
, sizeof(blr
));
860 memcpy(&msg
[sizeof(blr
)], &err
, sizeof(NTSTATUS
));
862 messaging_send_buf(smbd_messaging_context(), procid_self(),
863 MSG_SMB_BLOCKING_LOCK_CANCEL
,
864 (uint8
*)&msg
, sizeof(msg
));