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
24 /****************************************************************************
25 This is the structure to queue to implement blocking locks.
26 notify. It consists of the requesting SMB and the expiry time.
27 *****************************************************************************/
29 typedef struct blocking_lock_record {
30 struct blocking_lock_record *next;
31 struct blocking_lock_record *prev;
33 struct timeval expire_time;
38 uint32_t blocking_pid; /* PID that blocks us. */
39 enum brl_flavour lock_flav;
40 enum brl_type lock_type;
41 struct smb_request *req;
42 } blocking_lock_record;
44 /* dlink list we store pending lock records on. */
45 static blocking_lock_record *blocking_lock_queue;
47 /* dlink list we move cancelled lock records onto. */
48 static blocking_lock_record *blocking_lock_cancelled_queue;
50 /* The event that makes us process our blocking lock queue */
51 static struct timed_event *brl_timeout;
53 /****************************************************************************
54 Determine if this is a secondary element of a chained SMB.
55 **************************************************************************/
57 static bool in_chained_smb(void)
59 return (chain_size != 0);
62 static void received_unlock_msg(struct messaging_context *msg,
65 struct server_id server_id,
67 static void process_blocking_lock_queue(void);
69 static void brl_timeout_fn(struct event_context *event_ctx,
70 struct timed_event *te,
71 const struct timeval *now,
74 SMB_ASSERT(brl_timeout == te);
75 TALLOC_FREE(brl_timeout);
77 change_to_root_user(); /* TODO: Possibly run all timed events as
80 process_blocking_lock_queue();
83 /****************************************************************************
84 After a change to blocking_lock_queue, recalculate the timed_event for the
86 ****************************************************************************/
88 static bool recalc_brl_timeout(void)
90 blocking_lock_record *brl;
91 struct timeval next_timeout;
93 TALLOC_FREE(brl_timeout);
95 next_timeout = timeval_zero();
97 for (brl = blocking_lock_queue; brl; brl = brl->next) {
98 if (timeval_is_zero(&brl->expire_time)) {
100 * If we're blocked on pid 0xFFFFFFFF this is
101 * a POSIX lock, so calculate a timeout of
102 * 10 seconds into the future.
104 if (brl->blocking_pid == 0xFFFFFFFF) {
105 struct timeval psx_to = timeval_current_ofs(10, 0);
106 next_timeout = timeval_min(&next_timeout, &psx_to);
112 if (timeval_is_zero(&next_timeout)) {
113 next_timeout = brl->expire_time;
116 next_timeout = timeval_min(&next_timeout,
121 if (timeval_is_zero(&next_timeout)) {
125 if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
126 next_timeout, "brl_timeout",
127 brl_timeout_fn, NULL))) {
135 /****************************************************************************
136 Function to push a blocking lock request onto the lock queue.
137 ****************************************************************************/
139 bool push_blocking_lock_request( struct byte_range_lock *br_lck,
140 struct smb_request *req,
145 enum brl_type lock_type,
146 enum brl_flavour lock_flav,
149 uint32_t blocking_pid)
151 static bool set_lock_msg;
152 blocking_lock_record *blr;
155 if(in_chained_smb() ) {
156 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
161 * Now queue an entry on the blocking lock queue. We setup
162 * the expiration time here.
165 blr = talloc(NULL, struct blocking_lock_record);
167 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
175 if (lock_timeout == -1) {
176 blr->expire_time.tv_sec = 0;
177 blr->expire_time.tv_usec = 0; /* Never expire. */
179 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
180 (lock_timeout % 1000) * 1000);
182 blr->lock_num = lock_num;
183 blr->lock_pid = lock_pid;
184 blr->blocking_pid = blocking_pid;
185 blr->lock_flav = lock_flav;
186 blr->lock_type = lock_type;
187 blr->offset = offset;
190 /* Add a pending lock record for this. */
191 status = brl_lock(smbd_messaging_context(), br_lck,
196 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
198 lock_timeout ? True : False, /* blocking_lock. */
201 if (!NT_STATUS_IS_OK(status)) {
202 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
203 DLIST_REMOVE(blocking_lock_queue, blr);
208 blr->req = talloc_move(blr, &req);
210 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
211 recalc_brl_timeout();
213 /* Ensure we'll receive messages when this is unlocked. */
215 messaging_register(smbd_messaging_context(), NULL,
216 MSG_SMB_UNLOCK, received_unlock_msg);
220 DEBUG(3,("push_blocking_lock_request: lock request blocked with "
221 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
222 (unsigned int)blr->expire_time.tv_sec,
223 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
224 blr->fsp->fnum, blr->fsp->fsp_name ));
226 /* Push the MID of this packet on the signing queue. */
227 srv_defer_sign_response(blr->req->mid);
232 /****************************************************************************
233 Return a lockingX success SMB.
234 *****************************************************************************/
236 static void reply_lockingX_success(blocking_lock_record *blr)
238 reply_outbuf(blr->req, 2, 0);
241 * As this message is a lockingX call we must handle
242 * any following chained message correctly.
243 * This is normally handled in construct_reply(),
244 * but as that calls switch_message, we can't use
245 * that here and must set up the chain info manually.
248 chain_reply(blr->req);
250 if (!srv_send_smb(smbd_server_fd(), (char *)blr->req->outbuf,
251 IS_CONN_ENCRYPTED(blr->fsp->conn))) {
252 exit_server_cleanly("send_blocking_reply: srv_send_smb failed.");
255 TALLOC_FREE(blr->req->outbuf);
258 /****************************************************************************
259 Return a generic lock fail error blocking call.
260 *****************************************************************************/
262 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
264 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
265 FILE_LOCK_CONFLICT! (tridge) */
266 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
267 status = NT_STATUS_FILE_LOCK_CONFLICT;
270 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
271 /* Store the last lock error. */
272 files_struct *fsp = blr->fsp;
275 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
276 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
277 fsp->last_lock_failure.context.pid = procid_self();
278 fsp->last_lock_failure.start = blr->offset;
279 fsp->last_lock_failure.size = blr->count;
280 fsp->last_lock_failure.fnum = fsp->fnum;
281 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
282 fsp->last_lock_failure.lock_flav = blr->lock_flav;
286 reply_nterror(blr->req, status);
287 if (!srv_send_smb(smbd_server_fd(), (char *)blr->req->outbuf,
288 blr->req->encrypted)) {
289 exit_server_cleanly("generic_blocking_lock_error: srv_send_smb failed.");
291 TALLOC_FREE(blr->req->outbuf);
294 /****************************************************************************
295 Return a lock fail error for a lockingX call. Undo all the locks we have
297 *****************************************************************************/
299 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
301 files_struct *fsp = blr->fsp;
302 uint16 num_ulocks = SVAL(blr->req->vwv+6, 0);
303 uint64_t count = (uint64_t)0, offset = (uint64_t) 0;
305 unsigned char locktype = CVAL(blr->req->vwv+3, 0);
306 bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
310 data = (uint8_t *)blr->req->buf
311 + ((large_file_format ? 20 : 10)*num_ulocks);
314 * Data now points at the beginning of the list
315 * of smb_lkrng structs.
319 * Ensure we don't do a remove on the lock that just failed,
320 * as under POSIX rules, if we have a lock already there, we
321 * will delete it (and we shouldn't) .....
324 for(i = blr->lock_num - 1; i >= 0; i--) {
327 lock_pid = get_lock_pid( data, i, large_file_format);
328 count = get_lock_count( data, i, large_file_format);
329 offset = get_lock_offset( data, i, large_file_format, &err);
332 * We know err cannot be set as if it was the lock
333 * request would never have been queued. JRA.
336 do_unlock(smbd_messaging_context(),
344 generic_blocking_lock_error(blr, status);
347 /****************************************************************************
348 Return a lock fail error.
349 *****************************************************************************/
351 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
353 switch(blr->req->cmd) {
355 reply_lockingX_error(blr, status);
359 reply_nterror(blr->req, status);
362 * construct_reply_common has done us the favor to pre-fill
363 * the command field with SMBtranss2 which is wrong :-)
365 SCVAL(blr->req->outbuf,smb_com,SMBtrans2);
367 if (!srv_send_smb(smbd_server_fd(),
368 (char *)blr->req->outbuf,
369 IS_CONN_ENCRYPTED(blr->fsp->conn))) {
370 exit_server_cleanly("blocking_lock_reply_error: "
371 "srv_send_smb failed.");
373 TALLOC_FREE(blr->req->outbuf);
376 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
377 exit_server("PANIC - unknown type on blocking lock queue");
381 /****************************************************************************
382 Attempt to finish off getting all pending blocking locks for a lockingX call.
383 Returns True if we want to be removed from the list.
384 *****************************************************************************/
386 static bool process_lockingX(blocking_lock_record *blr)
388 unsigned char locktype = CVAL(blr->req->vwv+3, 0);
389 files_struct *fsp = blr->fsp;
390 uint16 num_ulocks = SVAL(blr->req->vwv+6, 0);
391 uint16 num_locks = SVAL(blr->req->vwv+7, 0);
392 uint64_t count = (uint64_t)0, offset = (uint64_t)0;
394 bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
396 NTSTATUS status = NT_STATUS_OK;
398 data = (uint8_t *)blr->req->buf
399 + ((large_file_format ? 20 : 10)*num_ulocks);
402 * Data now points at the beginning of the list
403 * of smb_lkrng structs.
406 for(; blr->lock_num < num_locks; blr->lock_num++) {
407 struct byte_range_lock *br_lck = NULL;
410 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
411 count = get_lock_count( data, blr->lock_num, large_file_format);
412 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
415 * We know err cannot be set as if it was the lock
416 * request would never have been queued. JRA.
419 br_lck = do_lock(smbd_messaging_context(),
424 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
425 READ_LOCK : WRITE_LOCK),
433 if (NT_STATUS_IS_ERR(status)) {
438 if(blr->lock_num == num_locks) {
440 * Success - we got all the locks.
443 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
444 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
446 reply_lockingX_success(blr);
450 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
451 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
453 * We have other than a "can't get lock"
454 * error. Free any locks we had and return an error.
455 * Return True so we get dequeued.
457 blocking_lock_reply_error(blr, status);
462 * Still can't get all the locks - keep waiting.
465 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
467 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
472 /****************************************************************************
473 Attempt to get the posix lock request from a SMBtrans2 call.
474 Returns True if we want to be removed from the list.
475 *****************************************************************************/
477 static bool process_trans2(blocking_lock_record *blr)
481 struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
493 if (!NT_STATUS_IS_OK(status)) {
494 if (ERROR_WAS_LOCK_DENIED(status)) {
495 /* Still can't get the lock, just keep waiting. */
499 * We have other than a "can't get lock"
500 * error. Send an error and return True so we get dequeued.
502 blocking_lock_reply_error(blr, status);
506 /* We finally got the lock, return success. */
508 SCVAL(blr->req->inbuf, smb_com, SMBtrans2);
510 /* Fake up max_data_bytes here - we know it fits. */
511 send_trans2_replies(blr->fsp->conn, blr->req, params, 2, NULL, 0, 0xffff);
516 /****************************************************************************
517 Process a blocking lock SMB.
518 Returns True if we want to be removed from the list.
519 *****************************************************************************/
521 static bool blocking_lock_record_process(blocking_lock_record *blr)
523 switch(blr->req->cmd) {
525 return process_lockingX(blr);
528 return process_trans2(blr);
530 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
531 exit_server("PANIC - unknown type on blocking lock queue");
533 return False; /* Keep compiler happy. */
536 /****************************************************************************
537 Cancel entries by fnum from the blocking lock pending queue.
538 *****************************************************************************/
540 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
542 blocking_lock_record *blr, *next = NULL;
544 for(blr = blocking_lock_queue; blr; blr = next) {
545 unsigned char locktype = 0;
548 if (blr->fsp->fnum != fsp->fnum) {
552 if (blr->req->cmd == SMBlockingX) {
553 locktype = CVAL(blr->req->vwv+3, 0);
556 DEBUG(10, ("remove_pending_lock_requests_by_fid - removing "
557 "request type %d for file %s fnum = %d\n",
558 blr->req->cmd, fsp->fsp_name, fsp->fnum));
560 brl_lock_cancel(br_lck,
567 blocking_lock_cancel(fsp,
573 NT_STATUS_RANGE_NOT_LOCKED);
575 /* We're closing the file fsp here, so ensure
576 * we don't have a dangling pointer. */
581 /****************************************************************************
582 Delete entries by mid from the blocking lock pending queue. Always send reply.
583 *****************************************************************************/
585 void remove_pending_lock_requests_by_mid(int mid)
587 blocking_lock_record *blr, *next = NULL;
589 for(blr = blocking_lock_queue; blr; blr = next) {
591 struct byte_range_lock *br_lck;
595 if (blr->req->mid != mid) {
600 br_lck = brl_get_locks(talloc_tos(), fsp);
603 DEBUG(10, ("remove_pending_lock_requests_by_mid - "
604 "removing request type %d for file %s fnum "
605 "= %d\n", blr->req->cmd, fsp->fsp_name,
608 brl_lock_cancel(br_lck,
617 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
618 DLIST_REMOVE(blocking_lock_queue, blr);
623 /****************************************************************************
624 Is this mid a blocking lock request on the queue ?
625 *****************************************************************************/
627 bool blocking_lock_was_deferred(int mid)
629 blocking_lock_record *blr, *next = NULL;
631 for(blr = blocking_lock_queue; blr; blr = next) {
633 if(blr->req->mid == mid) {
640 /****************************************************************************
641 Set a flag as an unlock request affects one of our pending locks.
642 *****************************************************************************/
644 static void received_unlock_msg(struct messaging_context *msg,
647 struct server_id server_id,
650 DEBUG(10,("received_unlock_msg\n"));
651 process_blocking_lock_queue();
654 /****************************************************************************
655 Process the blocking lock queue. Note that this is only called as root.
656 *****************************************************************************/
658 static void process_blocking_lock_queue(void)
660 struct timeval tv_curr = timeval_current();
661 blocking_lock_record *blr, *next = NULL;
662 bool recalc_timeout = False;
665 * Go through the queue and see if we can get any of the locks.
668 for (blr = blocking_lock_queue; blr; blr = next) {
673 * Go through the remaining locks and try and obtain them.
674 * The call returns True if all locks were obtained successfully
675 * and False if we still need to wait.
678 if(blocking_lock_record_process(blr)) {
679 struct byte_range_lock *br_lck = brl_get_locks(
680 talloc_tos(), blr->fsp);
683 brl_lock_cancel(br_lck,
692 DLIST_REMOVE(blocking_lock_queue, blr);
694 recalc_timeout = True;
699 * We couldn't get the locks for this record on the list.
700 * If the time has expired, return a lock error.
703 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
704 struct byte_range_lock *br_lck = brl_get_locks(
705 talloc_tos(), blr->fsp);
708 * Lock expired - throw away all previously
709 * obtained locks and return lock error.
713 DEBUG(5,("process_blocking_lock_queue: "
714 "pending lock fnum = %d for file %s "
715 "timed out.\n", blr->fsp->fnum,
716 blr->fsp->fsp_name ));
718 brl_lock_cancel(br_lck,
727 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
728 DLIST_REMOVE(blocking_lock_queue, blr);
730 recalc_timeout = True;
734 if (recalc_timeout) {
735 recalc_brl_timeout();
739 /****************************************************************************
740 Handle a cancel message. Lock already moved onto the cancel queue.
741 *****************************************************************************/
743 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
745 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
748 struct server_id server_id,
752 const char *msg = (const char *)data->data;
753 blocking_lock_record *blr;
755 if (data->data == NULL) {
756 smb_panic("process_blocking_lock_cancel_message: null msg");
759 if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
760 DEBUG(0, ("process_blocking_lock_cancel_message: "
761 "Got invalid msg len %d\n", (int)data->length));
762 smb_panic("process_blocking_lock_cancel_message: bad msg");
765 memcpy(&blr, msg, sizeof(blr));
766 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
768 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
771 blocking_lock_reply_error(blr, err);
772 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
776 /****************************************************************************
777 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
778 *****************************************************************************/
780 bool blocking_lock_cancel(files_struct *fsp,
784 enum brl_flavour lock_flav,
785 unsigned char locktype,
788 static bool initialized;
789 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
790 blocking_lock_record *blr;
793 /* Register our message. */
794 messaging_register(smbd_messaging_context(), NULL,
795 MSG_SMB_BLOCKING_LOCK_CANCEL,
796 process_blocking_lock_cancel_message);
801 for (blr = blocking_lock_queue; blr; blr = blr->next) {
802 if (fsp == blr->fsp &&
803 lock_pid == blr->lock_pid &&
804 offset == blr->offset &&
805 count == blr->count &&
806 lock_flav == blr->lock_flav) {
815 /* Check the flags are right. */
816 if (blr->req->cmd == SMBlockingX &&
817 (locktype & LOCKING_ANDX_LARGE_FILES) !=
818 (CVAL(blr->req->vwv+3, 0) & LOCKING_ANDX_LARGE_FILES)) {
822 /* Move to cancelled queue. */
823 DLIST_REMOVE(blocking_lock_queue, blr);
824 DLIST_ADD(blocking_lock_cancelled_queue, blr);
826 /* Create the message. */
827 memcpy(msg, &blr, sizeof(blr));
828 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
830 messaging_send_buf(smbd_messaging_context(), procid_self(),
831 MSG_SMB_BLOCKING_LOCK_CANCEL,
832 (uint8 *)&msg, sizeof(msg));