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
, fsp_str_dbg(blr
->fsp
)));
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 "
422 "num_locks=%d\n", fsp_str_dbg(fsp
), fsp
->fnum
,
423 (unsigned int)locktype
, num_locks
));
425 reply_lockingX_success(blr
);
429 if (!NT_STATUS_EQUAL(status
,NT_STATUS_LOCK_NOT_GRANTED
) &&
430 !NT_STATUS_EQUAL(status
,NT_STATUS_FILE_LOCK_CONFLICT
)) {
432 * We have other than a "can't get lock"
433 * error. Free any locks we had and return an error.
434 * Return True so we get dequeued.
436 blocking_lock_reply_error(blr
, status
);
441 * Still can't get all the locks - keep waiting.
444 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
446 blr
->lock_num
, num_locks
, fsp_str_dbg(fsp
), fsp
->fnum
));
451 /****************************************************************************
452 Attempt to get the posix lock request from a SMBtrans2 call.
453 Returns True if we want to be removed from the list.
454 *****************************************************************************/
456 static bool process_trans2(struct blocking_lock_record
*blr
)
460 struct byte_range_lock
*br_lck
= do_lock(smbd_messaging_context(),
473 if (!NT_STATUS_IS_OK(status
)) {
474 if (ERROR_WAS_LOCK_DENIED(status
)) {
475 /* Still can't get the lock, just keep waiting. */
479 * We have other than a "can't get lock"
480 * error. Send an error and return True so we get dequeued.
482 blocking_lock_reply_error(blr
, status
);
486 /* We finally got the lock, return success. */
489 /* Fake up max_data_bytes here - we know it fits. */
490 send_trans2_replies(blr
->fsp
->conn
, blr
->req
, params
, 2, NULL
, 0, 0xffff);
495 /****************************************************************************
496 Process a blocking lock SMB.
497 Returns True if we want to be removed from the list.
498 *****************************************************************************/
500 static bool blocking_lock_record_process(struct blocking_lock_record
*blr
)
502 switch(blr
->req
->cmd
) {
504 return process_lockingX(blr
);
507 return process_trans2(blr
);
509 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
510 exit_server("PANIC - unknown type on blocking lock queue");
512 return False
; /* Keep compiler happy. */
515 /****************************************************************************
516 Cancel entries by fnum from the blocking lock pending queue.
517 *****************************************************************************/
519 void cancel_pending_lock_requests_by_fid(files_struct
*fsp
, struct byte_range_lock
*br_lck
)
521 struct blocking_lock_record
*blr
, *blr_cancelled
, *next
= NULL
;
523 for(blr
= blocking_lock_queue
; blr
; blr
= next
) {
524 unsigned char locktype
= 0;
527 if (blr
->fsp
->fnum
!= fsp
->fnum
) {
531 if (blr
->req
->cmd
== SMBlockingX
) {
532 locktype
= CVAL(blr
->req
->vwv
+3, 0);
535 DEBUG(10, ("remove_pending_lock_requests_by_fid - removing "
536 "request type %d for file %s fnum = %d\n",
537 blr
->req
->cmd
, fsp_str_dbg(fsp
), fsp
->fnum
));
539 blr_cancelled
= blocking_lock_cancel(fsp
,
545 NT_STATUS_RANGE_NOT_LOCKED
);
547 SMB_ASSERT(blr_cancelled
== blr
);
549 brl_lock_cancel(br_lck
,
557 /* We're closing the file fsp here, so ensure
558 * we don't have a dangling pointer. */
563 /****************************************************************************
564 Delete entries by mid from the blocking lock pending queue. Always send reply.
565 *****************************************************************************/
567 void remove_pending_lock_requests_by_mid(int mid
)
569 struct blocking_lock_record
*blr
, *next
= NULL
;
571 for(blr
= blocking_lock_queue
; blr
; blr
= next
) {
573 struct byte_range_lock
*br_lck
;
577 if (blr
->req
->mid
!= mid
) {
582 br_lck
= brl_get_locks(talloc_tos(), fsp
);
585 DEBUG(10, ("remove_pending_lock_requests_by_mid - "
586 "removing request type %d for file %s fnum "
587 "= %d\n", blr
->req
->cmd
, fsp_str_dbg(fsp
),
590 brl_lock_cancel(br_lck
,
600 blocking_lock_reply_error(blr
,NT_STATUS_FILE_LOCK_CONFLICT
);
601 DLIST_REMOVE(blocking_lock_queue
, blr
);
606 /****************************************************************************
607 Is this mid a blocking lock request on the queue ?
608 *****************************************************************************/
610 bool blocking_lock_was_deferred(int mid
)
612 struct blocking_lock_record
*blr
, *next
= NULL
;
614 for(blr
= blocking_lock_queue
; blr
; blr
= next
) {
616 if(blr
->req
->mid
== mid
) {
623 /****************************************************************************
624 Set a flag as an unlock request affects one of our pending locks.
625 *****************************************************************************/
627 static void received_unlock_msg(struct messaging_context
*msg
,
630 struct server_id server_id
,
633 DEBUG(10,("received_unlock_msg\n"));
634 process_blocking_lock_queue();
637 /****************************************************************************
638 Process the blocking lock queue. Note that this is only called as root.
639 *****************************************************************************/
641 void process_blocking_lock_queue(void)
643 struct timeval tv_curr
= timeval_current();
644 struct blocking_lock_record
*blr
, *next
= NULL
;
645 bool recalc_timeout
= False
;
648 * Go through the queue and see if we can get any of the locks.
651 for (blr
= blocking_lock_queue
; blr
; blr
= next
) {
656 * Go through the remaining locks and try and obtain them.
657 * The call returns True if all locks were obtained successfully
658 * and False if we still need to wait.
661 DEBUG(10, ("Processing BLR = %p\n", blr
));
663 if(blocking_lock_record_process(blr
)) {
664 struct byte_range_lock
*br_lck
= brl_get_locks(
665 talloc_tos(), blr
->fsp
);
667 DEBUG(10, ("BLR_process returned true: cancelling and "
668 "removing lock. BLR = %p\n", blr
));
671 brl_lock_cancel(br_lck
,
681 DLIST_REMOVE(blocking_lock_queue
, blr
);
683 recalc_timeout
= True
;
688 * We couldn't get the locks for this record on the list.
689 * If the time has expired, return a lock error.
692 if (!timeval_is_zero(&blr
->expire_time
) && timeval_compare(&blr
->expire_time
, &tv_curr
) <= 0) {
693 struct byte_range_lock
*br_lck
= brl_get_locks(
694 talloc_tos(), blr
->fsp
);
696 DEBUG(10, ("Lock timed out! BLR = %p\n", blr
));
699 * Lock expired - throw away all previously
700 * obtained locks and return lock error.
704 DEBUG(5,("process_blocking_lock_queue: "
705 "pending lock fnum = %d for file %s "
706 "timed out.\n", blr
->fsp
->fnum
,
707 fsp_str_dbg(blr
->fsp
)));
709 brl_lock_cancel(br_lck
,
719 blocking_lock_reply_error(blr
,NT_STATUS_FILE_LOCK_CONFLICT
);
720 DLIST_REMOVE(blocking_lock_queue
, blr
);
722 recalc_timeout
= True
;
726 if (recalc_timeout
) {
727 recalc_brl_timeout();
731 /****************************************************************************
732 Handle a cancel message. Lock already moved onto the cancel queue.
733 *****************************************************************************/
735 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(struct blocking_lock_record *) + sizeof(NTSTATUS))
737 static void process_blocking_lock_cancel_message(struct messaging_context
*ctx
,
740 struct server_id server_id
,
744 const char *msg
= (const char *)data
->data
;
745 struct blocking_lock_record
*blr
;
747 if (data
->data
== NULL
) {
748 smb_panic("process_blocking_lock_cancel_message: null msg");
751 if (data
->length
!= MSG_BLOCKING_LOCK_CANCEL_SIZE
) {
752 DEBUG(0, ("process_blocking_lock_cancel_message: "
753 "Got invalid msg len %d\n", (int)data
->length
));
754 smb_panic("process_blocking_lock_cancel_message: bad msg");
757 memcpy(&blr
, msg
, sizeof(blr
));
758 memcpy(&err
, &msg
[sizeof(blr
)], sizeof(NTSTATUS
));
760 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
763 blocking_lock_reply_error(blr
, err
);
764 DLIST_REMOVE(blocking_lock_cancelled_queue
, blr
);
768 /****************************************************************************
769 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
770 Returns the blocking_lock_record that is being cancelled.
771 *****************************************************************************/
773 struct blocking_lock_record
*blocking_lock_cancel(files_struct
*fsp
,
777 enum brl_flavour lock_flav
,
778 unsigned char locktype
,
781 char msg
[MSG_BLOCKING_LOCK_CANCEL_SIZE
];
782 struct blocking_lock_record
*blr
;
784 if (!blocking_lock_cancel_state
) {
785 /* Register our message. */
786 messaging_register(smbd_messaging_context(), NULL
,
787 MSG_SMB_BLOCKING_LOCK_CANCEL
,
788 process_blocking_lock_cancel_message
);
790 blocking_lock_cancel_state
= True
;
793 for (blr
= blocking_lock_queue
; blr
; blr
= blr
->next
) {
794 if (fsp
== blr
->fsp
&&
795 lock_pid
== blr
->lock_pid
&&
796 offset
== blr
->offset
&&
797 count
== blr
->count
&&
798 lock_flav
== blr
->lock_flav
) {
807 /* Check the flags are right. */
808 if (blr
->req
->cmd
== SMBlockingX
&&
809 (locktype
& LOCKING_ANDX_LARGE_FILES
) !=
810 (CVAL(blr
->req
->vwv
+3, 0) & LOCKING_ANDX_LARGE_FILES
)) {
814 /* Move to cancelled queue. */
815 DLIST_REMOVE(blocking_lock_queue
, blr
);
816 DLIST_ADD(blocking_lock_cancelled_queue
, blr
);
818 /* Create the message. */
819 memcpy(msg
, &blr
, sizeof(blr
));
820 memcpy(&msg
[sizeof(blr
)], &err
, sizeof(NTSTATUS
));
822 messaging_send_buf(smbd_messaging_context(), procid_self(),
823 MSG_SMB_BLOCKING_LOCK_CANCEL
,
824 (uint8
*)&msg
, sizeof(msg
));