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;
76 TALLOC_FREE(brl_timeout);
78 next_timeout = timeval_zero();
80 for (blr = blocking_lock_queue; blr; blr = blr->next) {
81 if (timeval_is_zero(&blr->expire_time)) {
83 * If we're blocked on pid 0xFFFFFFFF this is
84 * a POSIX lock, so calculate a timeout of
85 * 10 seconds into the future.
87 if (blr->blocking_pid == 0xFFFFFFFF) {
88 struct timeval psx_to = timeval_current_ofs(10, 0);
89 next_timeout = timeval_brl_min(&next_timeout, &psx_to);
95 next_timeout = timeval_brl_min(&next_timeout, &blr->expire_time);
98 if (timeval_is_zero(&next_timeout)) {
99 DEBUG(10, ("Next timeout = Infinite.\n"));
104 struct timeval cur, from_now;
106 cur = timeval_current();
107 from_now = timeval_until(&cur, &next_timeout);
108 DEBUG(10, ("Next timeout = %d.%d seconds from now.\n",
109 (int)from_now.tv_sec, (int)from_now.tv_usec));
112 if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
114 brl_timeout_fn, NULL))) {
122 /****************************************************************************
123 Function to push a blocking lock request onto the lock queue.
124 ****************************************************************************/
126 bool push_blocking_lock_request( struct byte_range_lock *br_lck,
127 struct smb_request *req,
132 enum brl_type lock_type,
133 enum brl_flavour lock_flav,
136 uint32_t blocking_pid)
138 struct blocking_lock_record *blr;
141 if(req_is_in_chain(req)) {
142 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
147 * Now queue an entry on the blocking lock queue. We setup
148 * the expiration time here.
151 blr = talloc(NULL, struct blocking_lock_record);
153 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
161 if (lock_timeout == -1) {
162 blr->expire_time.tv_sec = 0;
163 blr->expire_time.tv_usec = 0; /* Never expire. */
165 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
166 (lock_timeout % 1000) * 1000);
168 blr->lock_num = lock_num;
169 blr->lock_pid = lock_pid;
170 blr->blocking_pid = blocking_pid;
171 blr->lock_flav = lock_flav;
172 blr->lock_type = lock_type;
173 blr->offset = offset;
176 /* Specific brl_lock() implementations can fill this in. */
177 blr->blr_private = NULL;
179 /* Add a pending lock record for this. */
180 status = brl_lock(smbd_messaging_context(),
186 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
188 lock_timeout ? True : False, /* blocking_lock. */
192 if (!NT_STATUS_IS_OK(status)) {
193 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
198 SMB_PERFCOUNT_DEFER_OP(&req->pcd, &req->pcd);
199 blr->req = talloc_move(blr, &req);
201 DLIST_ADD_END(blocking_lock_queue, blr, struct blocking_lock_record *);
202 recalc_brl_timeout();
204 /* Ensure we'll receive messages when this is unlocked. */
205 if (!blocking_lock_unlock_state) {
206 messaging_register(smbd_messaging_context(), NULL,
207 MSG_SMB_UNLOCK, received_unlock_msg);
208 blocking_lock_unlock_state = true;
211 DEBUG(3,("push_blocking_lock_request: lock request blocked with "
212 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
213 (unsigned int)blr->expire_time.tv_sec,
214 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
215 blr->fsp->fnum, fsp_str_dbg(blr->fsp)));
220 /****************************************************************************
221 Return a lockingX success SMB.
222 *****************************************************************************/
224 static void reply_lockingX_success(struct blocking_lock_record *blr)
226 reply_outbuf(blr->req, 2, 0);
229 * As this message is a lockingX call we must handle
230 * any following chained message correctly.
231 * This is normally handled in construct_reply(),
232 * but as that calls switch_message, we can't use
233 * that here and must set up the chain info manually.
236 chain_reply(blr->req);
237 TALLOC_FREE(blr->req->outbuf);
240 /****************************************************************************
241 Return a generic lock fail error blocking call.
242 *****************************************************************************/
244 static void generic_blocking_lock_error(struct blocking_lock_record *blr, NTSTATUS status)
246 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
247 FILE_LOCK_CONFLICT! (tridge) */
248 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
249 status = NT_STATUS_FILE_LOCK_CONFLICT;
252 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
253 /* Store the last lock error. */
254 files_struct *fsp = blr->fsp;
257 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
258 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
259 fsp->last_lock_failure.context.pid = procid_self();
260 fsp->last_lock_failure.start = blr->offset;
261 fsp->last_lock_failure.size = blr->count;
262 fsp->last_lock_failure.fnum = fsp->fnum;
263 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
264 fsp->last_lock_failure.lock_flav = blr->lock_flav;
268 reply_nterror(blr->req, status);
269 if (!srv_send_smb(smbd_server_fd(), (char *)blr->req->outbuf,
270 true, blr->req->seqnum+1,
271 blr->req->encrypted, NULL)) {
272 exit_server_cleanly("generic_blocking_lock_error: srv_send_smb failed.");
274 TALLOC_FREE(blr->req->outbuf);
277 /****************************************************************************
278 Return a lock fail error for a lockingX call. Undo all the locks we have
280 *****************************************************************************/
282 static void reply_lockingX_error(struct blocking_lock_record *blr, NTSTATUS status)
284 files_struct *fsp = blr->fsp;
285 uint16 num_ulocks = SVAL(blr->req->vwv+6, 0);
286 uint64_t count = (uint64_t)0, offset = (uint64_t) 0;
288 unsigned char locktype = CVAL(blr->req->vwv+3, 0);
289 bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
293 data = (uint8_t *)blr->req->buf
294 + ((large_file_format ? 20 : 10)*num_ulocks);
297 * Data now points at the beginning of the list
298 * of smb_lkrng structs.
302 * Ensure we don't do a remove on the lock that just failed,
303 * as under POSIX rules, if we have a lock already there, we
304 * will delete it (and we shouldn't) .....
307 for(i = blr->lock_num - 1; i >= 0; i--) {
310 lock_pid = get_lock_pid( data, i, large_file_format);
311 count = get_lock_count( data, i, large_file_format);
312 offset = get_lock_offset( data, i, large_file_format, &err);
315 * We know err cannot be set as if it was the lock
316 * request would never have been queued. JRA.
319 do_unlock(smbd_messaging_context(),
327 generic_blocking_lock_error(blr, status);
330 /****************************************************************************
331 Return a lock fail error.
332 *****************************************************************************/
334 static void blocking_lock_reply_error(struct blocking_lock_record *blr, NTSTATUS status)
336 DEBUG(10, ("Replying with error=%s. BLR = %p\n", nt_errstr(status), blr));
338 switch(blr->req->cmd) {
340 reply_lockingX_error(blr, status);
344 reply_nterror(blr->req, status);
347 * construct_reply_common has done us the favor to pre-fill
348 * the command field with SMBtranss2 which is wrong :-)
350 SCVAL(blr->req->outbuf,smb_com,SMBtrans2);
352 if (!srv_send_smb(smbd_server_fd(),
353 (char *)blr->req->outbuf,
354 true, blr->req->seqnum+1,
355 IS_CONN_ENCRYPTED(blr->fsp->conn),
357 exit_server_cleanly("blocking_lock_reply_error: "
358 "srv_send_smb failed.");
360 TALLOC_FREE(blr->req->outbuf);
363 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
364 exit_server("PANIC - unknown type on blocking lock queue");
368 /****************************************************************************
369 Attempt to finish off getting all pending blocking locks for a lockingX call.
370 Returns True if we want to be removed from the list.
371 *****************************************************************************/
373 static bool process_lockingX(struct blocking_lock_record *blr)
375 unsigned char locktype = CVAL(blr->req->vwv+3, 0);
376 files_struct *fsp = blr->fsp;
377 uint16 num_ulocks = SVAL(blr->req->vwv+6, 0);
378 uint16 num_locks = SVAL(blr->req->vwv+7, 0);
379 uint64_t count = (uint64_t)0, offset = (uint64_t)0;
381 bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
383 NTSTATUS status = NT_STATUS_OK;
385 data = (uint8_t *)blr->req->buf
386 + ((large_file_format ? 20 : 10)*num_ulocks);
389 * Data now points at the beginning of the list
390 * of smb_lkrng structs.
393 for(; blr->lock_num < num_locks; blr->lock_num++) {
394 struct byte_range_lock *br_lck = NULL;
397 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
398 count = get_lock_count( data, blr->lock_num, large_file_format);
399 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
402 * We know err cannot be set as if it was the lock
403 * request would never have been queued. JRA.
406 br_lck = do_lock(smbd_messaging_context(),
411 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
412 READ_LOCK : WRITE_LOCK),
421 if (NT_STATUS_IS_ERR(status)) {
426 if(blr->lock_num == num_locks) {
428 * Success - we got all the locks.
431 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d "
432 "num_locks=%d\n", fsp_str_dbg(fsp), fsp->fnum,
433 (unsigned int)locktype, num_locks));
435 reply_lockingX_success(blr);
439 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
440 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
442 * We have other than a "can't get lock"
443 * error. Free any locks we had and return an error.
444 * Return True so we get dequeued.
446 blocking_lock_reply_error(blr, status);
451 * Still can't get all the locks - keep waiting.
454 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
456 blr->lock_num, num_locks, fsp_str_dbg(fsp), fsp->fnum));
461 /****************************************************************************
462 Attempt to get the posix lock request from a SMBtrans2 call.
463 Returns True if we want to be removed from the list.
464 *****************************************************************************/
466 static bool process_trans2(struct blocking_lock_record *blr)
470 struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
483 if (!NT_STATUS_IS_OK(status)) {
484 if (ERROR_WAS_LOCK_DENIED(status)) {
485 /* Still can't get the lock, just keep waiting. */
489 * We have other than a "can't get lock"
490 * error. Send an error and return True so we get dequeued.
492 blocking_lock_reply_error(blr, status);
496 /* We finally got the lock, return success. */
499 /* Fake up max_data_bytes here - we know it fits. */
500 send_trans2_replies(blr->fsp->conn, blr->req, params, 2, NULL, 0, 0xffff);
505 /****************************************************************************
506 Process a blocking lock SMB.
507 Returns True if we want to be removed from the list.
508 *****************************************************************************/
510 static bool blocking_lock_record_process(struct blocking_lock_record *blr)
512 switch(blr->req->cmd) {
514 return process_lockingX(blr);
517 return process_trans2(blr);
519 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
520 exit_server("PANIC - unknown type on blocking lock queue");
522 return False; /* Keep compiler happy. */
525 /****************************************************************************
526 Cancel entries by fnum from the blocking lock pending queue.
527 *****************************************************************************/
529 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
531 struct blocking_lock_record *blr, *blr_cancelled, *next = NULL;
533 for(blr = blocking_lock_queue; blr; blr = next) {
534 unsigned char locktype = 0;
537 if (blr->fsp->fnum != fsp->fnum) {
541 if (blr->req->cmd == SMBlockingX) {
542 locktype = CVAL(blr->req->vwv+3, 0);
545 DEBUG(10, ("remove_pending_lock_requests_by_fid - removing "
546 "request type %d for file %s fnum = %d\n",
547 blr->req->cmd, fsp_str_dbg(fsp), fsp->fnum));
549 blr_cancelled = blocking_lock_cancel(fsp,
555 NT_STATUS_RANGE_NOT_LOCKED);
557 SMB_ASSERT(blr_cancelled == blr);
559 brl_lock_cancel(br_lck,
567 /* We're closing the file fsp here, so ensure
568 * we don't have a dangling pointer. */
573 /****************************************************************************
574 Delete entries by mid from the blocking lock pending queue. Always send reply.
575 *****************************************************************************/
577 void remove_pending_lock_requests_by_mid(int mid)
579 struct blocking_lock_record *blr, *next = NULL;
581 for(blr = blocking_lock_queue; blr; blr = next) {
583 struct byte_range_lock *br_lck;
587 if (blr->req->mid != mid) {
592 br_lck = brl_get_locks(talloc_tos(), fsp);
595 DEBUG(10, ("remove_pending_lock_requests_by_mid - "
596 "removing request type %d for file %s fnum "
597 "= %d\n", blr->req->cmd, fsp_str_dbg(fsp),
600 brl_lock_cancel(br_lck,
610 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
611 DLIST_REMOVE(blocking_lock_queue, blr);
616 /****************************************************************************
617 Is this mid a blocking lock request on the queue ?
618 *****************************************************************************/
620 bool blocking_lock_was_deferred(int mid)
622 struct blocking_lock_record *blr, *next = NULL;
624 for(blr = blocking_lock_queue; blr; blr = next) {
626 if(blr->req->mid == mid) {
633 /****************************************************************************
634 Set a flag as an unlock request affects one of our pending locks.
635 *****************************************************************************/
637 static void received_unlock_msg(struct messaging_context *msg,
640 struct server_id server_id,
643 DEBUG(10,("received_unlock_msg\n"));
644 process_blocking_lock_queue();
647 /****************************************************************************
648 Process the blocking lock queue. Note that this is only called as root.
649 *****************************************************************************/
651 void process_blocking_lock_queue(void)
653 struct timeval tv_curr = timeval_current();
654 struct blocking_lock_record *blr, *next = NULL;
655 bool recalc_timeout = False;
658 * Go through the queue and see if we can get any of the locks.
661 for (blr = blocking_lock_queue; blr; blr = next) {
666 * Go through the remaining locks and try and obtain them.
667 * The call returns True if all locks were obtained successfully
668 * and False if we still need to wait.
671 DEBUG(10, ("Processing BLR = %p\n", blr));
673 if(blocking_lock_record_process(blr)) {
674 struct byte_range_lock *br_lck = brl_get_locks(
675 talloc_tos(), blr->fsp);
677 DEBUG(10, ("BLR_process returned true: cancelling and "
678 "removing lock. BLR = %p\n", blr));
681 brl_lock_cancel(br_lck,
691 DLIST_REMOVE(blocking_lock_queue, blr);
693 recalc_timeout = True;
698 * We couldn't get the locks for this record on the list.
699 * If the time has expired, return a lock error.
702 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
703 struct byte_range_lock *br_lck = brl_get_locks(
704 talloc_tos(), blr->fsp);
706 DEBUG(10, ("Lock timed out! BLR = %p\n", blr));
709 * Lock expired - throw away all previously
710 * obtained locks and return lock error.
714 DEBUG(5,("process_blocking_lock_queue: "
715 "pending lock fnum = %d for file %s "
716 "timed out.\n", blr->fsp->fnum,
717 fsp_str_dbg(blr->fsp)));
719 brl_lock_cancel(br_lck,
729 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
730 DLIST_REMOVE(blocking_lock_queue, blr);
732 recalc_timeout = True;
736 if (recalc_timeout) {
737 recalc_brl_timeout();
741 /****************************************************************************
742 Handle a cancel message. Lock already moved onto the cancel queue.
743 *****************************************************************************/
745 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(struct blocking_lock_record *) + sizeof(NTSTATUS))
747 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
750 struct server_id server_id,
754 const char *msg = (const char *)data->data;
755 struct blocking_lock_record *blr;
757 if (data->data == NULL) {
758 smb_panic("process_blocking_lock_cancel_message: null msg");
761 if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
762 DEBUG(0, ("process_blocking_lock_cancel_message: "
763 "Got invalid msg len %d\n", (int)data->length));
764 smb_panic("process_blocking_lock_cancel_message: bad msg");
767 memcpy(&blr, msg, sizeof(blr));
768 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
770 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
773 blocking_lock_reply_error(blr, err);
774 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
778 /****************************************************************************
779 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
780 Returns the blocking_lock_record that is being cancelled.
781 *****************************************************************************/
783 struct blocking_lock_record *blocking_lock_cancel(files_struct *fsp,
787 enum brl_flavour lock_flav,
788 unsigned char locktype,
791 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
792 struct blocking_lock_record *blr;
794 if (!blocking_lock_cancel_state) {
795 /* Register our message. */
796 messaging_register(smbd_messaging_context(), NULL,
797 MSG_SMB_BLOCKING_LOCK_CANCEL,
798 process_blocking_lock_cancel_message);
800 blocking_lock_cancel_state = True;
803 for (blr = blocking_lock_queue; blr; blr = blr->next) {
804 if (fsp == blr->fsp &&
805 lock_pid == blr->lock_pid &&
806 offset == blr->offset &&
807 count == blr->count &&
808 lock_flav == blr->lock_flav) {
817 /* Check the flags are right. */
818 if (blr->req->cmd == SMBlockingX &&
819 (locktype & LOCKING_ANDX_LARGE_FILES) !=
820 (CVAL(blr->req->vwv+3, 0) & LOCKING_ANDX_LARGE_FILES)) {
824 /* Move to cancelled queue. */
825 DLIST_REMOVE(blocking_lock_queue, blr);
826 DLIST_ADD(blocking_lock_cancelled_queue, blr);
828 /* Create the message. */
829 memcpy(msg, &blr, sizeof(blr));
830 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
832 messaging_send_buf(smbd_messaging_context(), procid_self(),
833 MSG_SMB_BLOCKING_LOCK_CANCEL,
834 (uint8 *)&msg, sizeof(msg));