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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DBGC_CLASS DBGC_LOCKING
27 /****************************************************************************
28 This is the structure to queue to implement blocking locks.
29 notify. It consists of the requesting SMB and the expiry time.
30 *****************************************************************************/
32 typedef struct _blocking_lock_record {
33 struct _blocking_lock_record *next;
34 struct _blocking_lock_record *prev;
37 struct timeval expire_time;
42 uint32 blocking_pid; /* PID that blocks us. */
43 enum brl_flavour lock_flav;
44 enum brl_type lock_type;
47 } blocking_lock_record;
49 /* dlink list we store pending lock records on. */
50 static blocking_lock_record *blocking_lock_queue;
52 /* dlink list we move cancelled lock records onto. */
53 static blocking_lock_record *blocking_lock_cancelled_queue;
55 /****************************************************************************
56 Destructor for the above structure.
57 ****************************************************************************/
59 static void free_blocking_lock_record(blocking_lock_record *blr)
61 SAFE_FREE(blr->inbuf);
65 /****************************************************************************
66 Determine if this is a secondary element of a chained SMB.
67 **************************************************************************/
69 static BOOL in_chained_smb(void)
71 return (chain_size != 0);
74 static void received_unlock_msg(int msg_type, struct process_id src,
75 void *buf, size_t len,
78 /****************************************************************************
79 Function to push a blocking lock request onto the lock queue.
80 ****************************************************************************/
82 BOOL push_blocking_lock_request( struct byte_range_lock *br_lck,
83 char *inbuf, int length,
88 enum brl_type lock_type,
89 enum brl_flavour lock_flav,
94 static BOOL set_lock_msg;
95 blocking_lock_record *blr;
98 if(in_chained_smb() ) {
99 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
104 * Now queue an entry on the blocking lock queue. We setup
105 * the expiration time here.
108 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
109 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
116 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
117 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
122 blr->com_type = CVAL(inbuf,smb_com);
124 if (lock_timeout == -1) {
125 blr->expire_time.tv_sec = 0;
126 blr->expire_time.tv_usec = 0; /* Never expire. */
128 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
129 (lock_timeout % 1000) * 1000);
131 blr->lock_num = lock_num;
132 blr->lock_pid = lock_pid;
133 blr->blocking_pid = blocking_pid;
134 blr->lock_flav = lock_flav;
135 blr->lock_type = lock_type;
136 blr->offset = offset;
138 memcpy(blr->inbuf, inbuf, length);
139 blr->length = length;
141 /* Add a pending lock record for this. */
142 status = brl_lock(br_lck,
147 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
149 lock_timeout ? True : False, /* blocking_lock. */
152 if (!NT_STATUS_IS_OK(status)) {
153 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
154 DLIST_REMOVE(blocking_lock_queue, blr);
155 free_blocking_lock_record(blr);
159 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
161 /* Ensure we'll receive messages when this is unlocked. */
163 message_register(MSG_SMB_UNLOCK, received_unlock_msg,
168 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with "
169 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
170 length, (unsigned int)blr->expire_time.tv_sec,
171 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
172 blr->fsp->fnum, blr->fsp->fsp_name ));
174 /* Push the MID of this packet on the signing queue. */
175 srv_defer_sign_response(SVAL(inbuf,smb_mid));
180 /****************************************************************************
181 Return a smd with a given size.
182 *****************************************************************************/
184 static void send_blocking_reply(char *outbuf, int outsize)
187 smb_setlen(outbuf,outsize - 4);
189 if (!send_smb(smbd_server_fd(),outbuf))
190 exit_server_cleanly("send_blocking_reply: send_smb failed.");
193 /****************************************************************************
194 Return a lockingX success SMB.
195 *****************************************************************************/
197 static void reply_lockingX_success(blocking_lock_record *blr)
199 char *outbuf = get_OutBuffer();
200 int bufsize = BUFFER_SIZE;
201 char *inbuf = blr->inbuf;
204 construct_reply_common(inbuf, outbuf);
205 set_message(outbuf,2,0,True);
208 * As this message is a lockingX call we must handle
209 * any following chained message correctly.
210 * This is normally handled in construct_reply(),
211 * but as that calls switch_message, we can't use
212 * that here and must set up the chain info manually.
215 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
217 outsize += chain_size;
219 send_blocking_reply(outbuf,outsize);
222 /****************************************************************************
223 Return a generic lock fail error blocking call.
224 *****************************************************************************/
226 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
228 char *outbuf = get_OutBuffer();
229 char *inbuf = blr->inbuf;
230 construct_reply_common(inbuf, outbuf);
232 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
233 FILE_LOCK_CONFLICT! (tridge) */
234 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
235 status = NT_STATUS_FILE_LOCK_CONFLICT;
238 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
239 /* Store the last lock error. */
240 files_struct *fsp = blr->fsp;
242 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
243 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
244 fsp->last_lock_failure.context.pid = procid_self();
245 fsp->last_lock_failure.start = blr->offset;
246 fsp->last_lock_failure.size = blr->count;
247 fsp->last_lock_failure.fnum = fsp->fnum;
248 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
249 fsp->last_lock_failure.lock_flav = blr->lock_flav;
253 if (!send_smb(smbd_server_fd(),outbuf)) {
254 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
258 /****************************************************************************
259 Return a lock fail error for a lockingX call. Undo all the locks we have
261 *****************************************************************************/
263 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
265 char *inbuf = blr->inbuf;
266 files_struct *fsp = blr->fsp;
267 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
268 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
270 unsigned char locktype = CVAL(inbuf,smb_vwv3);
271 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
275 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
278 * Data now points at the beginning of the list
279 * of smb_lkrng structs.
283 * Ensure we don't do a remove on the lock that just failed,
284 * as under POSIX rules, if we have a lock already there, we
285 * will delete it (and we shouldn't) .....
288 for(i = blr->lock_num - 1; i >= 0; i--) {
291 lock_pid = get_lock_pid( data, i, large_file_format);
292 count = get_lock_count( data, i, large_file_format);
293 offset = get_lock_offset( data, i, large_file_format, &err);
296 * We know err cannot be set as if it was the lock
297 * request would never have been queued. JRA.
307 generic_blocking_lock_error(blr, status);
310 /****************************************************************************
311 Return a lock fail error.
312 *****************************************************************************/
314 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
316 switch(blr->com_type) {
318 reply_lockingX_error(blr, status);
323 char *outbuf = get_OutBuffer();
324 char *inbuf = blr->inbuf;
325 construct_reply_common(inbuf, outbuf);
326 /* construct_reply_common has done us the favor to pre-fill the
327 * command field with SMBtranss2 which is wrong :-)
329 SCVAL(outbuf,smb_com,SMBtrans2);
331 if (!send_smb(smbd_server_fd(),outbuf)) {
332 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
337 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
338 exit_server("PANIC - unknown type on blocking lock queue");
342 /****************************************************************************
343 Attempt to finish off getting all pending blocking locks for a lockingX call.
344 Returns True if we want to be removed from the list.
345 *****************************************************************************/
347 static BOOL process_lockingX(blocking_lock_record *blr)
349 char *inbuf = blr->inbuf;
350 unsigned char locktype = CVAL(inbuf,smb_vwv3);
351 files_struct *fsp = blr->fsp;
352 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
353 uint16 num_locks = SVAL(inbuf,smb_vwv7);
354 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
356 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
358 NTSTATUS status = NT_STATUS_OK;
360 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
363 * Data now points at the beginning of the list
364 * of smb_lkrng structs.
367 for(; blr->lock_num < num_locks; blr->lock_num++) {
368 struct byte_range_lock *br_lck = NULL;
371 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
372 count = get_lock_count( data, blr->lock_num, large_file_format);
373 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
376 * We know err cannot be set as if it was the lock
377 * request would never have been queued. JRA.
380 br_lck = do_lock(fsp,
384 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
385 READ_LOCK : WRITE_LOCK),
393 if (NT_STATUS_IS_ERR(status)) {
398 if(blr->lock_num == num_locks) {
400 * Success - we got all the locks.
403 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
404 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
406 reply_lockingX_success(blr);
408 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
409 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
411 * We have other than a "can't get lock"
412 * error. Free any locks we had and return an error.
413 * Return True so we get dequeued.
416 blocking_lock_reply_error(blr, status);
421 * Still can't get all the locks - keep waiting.
424 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
426 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
431 /****************************************************************************
432 Attempt to get the posix lock request from a SMBtrans2 call.
433 Returns True if we want to be removed from the list.
434 *****************************************************************************/
436 static BOOL process_trans2(blocking_lock_record *blr)
438 char *inbuf = blr->inbuf;
442 struct byte_range_lock *br_lck = do_lock(blr->fsp,
453 if (!NT_STATUS_IS_OK(status)) {
454 if (ERROR_WAS_LOCK_DENIED(status)) {
455 /* Still can't get the lock, just keep waiting. */
459 * We have other than a "can't get lock"
460 * error. Send an error and return True so we get dequeued.
462 blocking_lock_reply_error(blr, status);
466 /* We finally got the lock, return success. */
467 outbuf = get_OutBuffer();
468 construct_reply_common(inbuf, outbuf);
469 SCVAL(outbuf,smb_com,SMBtrans2);
471 /* Fake up max_data_bytes here - we know it fits. */
472 send_trans2_replies(outbuf, max_send, params, 2, NULL, 0, 0xffff);
477 /****************************************************************************
478 Process a blocking lock SMB.
479 Returns True if we want to be removed from the list.
480 *****************************************************************************/
482 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
484 switch(blr->com_type) {
486 return process_lockingX(blr);
489 return process_trans2(blr);
491 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
492 exit_server("PANIC - unknown type on blocking lock queue");
494 return False; /* Keep compiler happy. */
497 /****************************************************************************
498 Cancel entries by fnum from the blocking lock pending queue.
499 *****************************************************************************/
501 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
503 blocking_lock_record *blr, *next = NULL;
505 for(blr = blocking_lock_queue; blr; blr = next) {
507 if(blr->fsp->fnum == fsp->fnum) {
508 unsigned char locktype = 0;
510 if (blr->com_type == SMBlockingX) {
511 locktype = CVAL(blr->inbuf,smb_vwv3);
515 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
516 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
518 brl_lock_cancel(br_lck,
525 blocking_lock_cancel(fsp,
531 NT_STATUS_RANGE_NOT_LOCKED);
537 /****************************************************************************
538 Delete entries by mid from the blocking lock pending queue. Always send reply.
539 *****************************************************************************/
541 void remove_pending_lock_requests_by_mid(int mid)
543 blocking_lock_record *blr, *next = NULL;
545 for(blr = blocking_lock_queue; blr; blr = next) {
547 if(SVAL(blr->inbuf,smb_mid) == mid) {
548 files_struct *fsp = blr->fsp;
549 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
552 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
553 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
555 brl_lock_cancel(br_lck,
564 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
565 DLIST_REMOVE(blocking_lock_queue, blr);
566 free_blocking_lock_record(blr);
571 /****************************************************************************
572 Is this mid a blocking lock request on the queue ?
573 *****************************************************************************/
575 BOOL blocking_lock_was_deferred(int mid)
577 blocking_lock_record *blr, *next = NULL;
579 for(blr = blocking_lock_queue; blr; blr = next) {
581 if(SVAL(blr->inbuf,smb_mid) == mid) {
588 /****************************************************************************
589 Set a flag as an unlock request affects one of our pending locks.
590 *****************************************************************************/
592 static void received_unlock_msg(int msg_type, struct process_id src,
593 void *buf, size_t len,
596 DEBUG(10,("received_unlock_msg\n"));
597 process_blocking_lock_queue();
600 /****************************************************************************
601 Return the number of milliseconds to the next blocking locks timeout, or default_timeout
602 *****************************************************************************/
604 unsigned int blocking_locks_timeout_ms(unsigned int default_timeout_ms)
606 unsigned int timeout_ms = default_timeout_ms;
607 struct timeval tv_curr;
608 SMB_BIG_INT min_tv_dif_us = default_timeout_ms * 1000;
609 blocking_lock_record *blr = blocking_lock_queue;
611 /* note that we avoid the GetTimeOfDay() syscall if there are no blocking locks */
616 tv_curr = timeval_current();
618 for (; blr; blr = blr->next) {
619 SMB_BIG_INT tv_dif_us;
621 if (timeval_is_zero(&blr->expire_time)) {
623 * If we're blocked on pid 0xFFFFFFFF this is
624 * a POSIX lock, so calculate a timeout of
627 if (blr->blocking_pid == 0xFFFFFFFF) {
628 tv_dif_us = 10 * 1000 * 1000;
629 min_tv_dif_us = MIN(min_tv_dif_us, tv_dif_us);
631 continue; /* Never timeout. */
634 tv_dif_us = usec_time_diff(&blr->expire_time, &tv_curr);
635 min_tv_dif_us = MIN(min_tv_dif_us, tv_dif_us);
638 if (min_tv_dif_us < 0) {
642 timeout_ms = (unsigned int)(min_tv_dif_us / (SMB_BIG_INT)1000);
644 if (timeout_ms < 1) {
648 DEBUG(10,("blocking_locks_timeout_ms: returning %u\n", timeout_ms));
653 /****************************************************************************
654 Process the blocking lock queue. Note that this is only called as root.
655 *****************************************************************************/
657 void process_blocking_lock_queue(void)
659 struct timeval tv_curr = timeval_current();
660 blocking_lock_record *blr, *next = NULL;
663 * Go through the queue and see if we can get any of the locks.
666 for (blr = blocking_lock_queue; blr; blr = next) {
667 connection_struct *conn = NULL;
669 files_struct *fsp = NULL;
674 * Ensure we don't have any old chain_fsp values
681 conn = conn_find(SVAL(blr->inbuf,smb_tid));
682 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
683 SVAL(blr->inbuf,smb_uid);
685 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
686 fsp->fnum, fsp->fsp_name ));
688 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
689 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
692 * Lock expired - throw away all previously
693 * obtained locks and return lock error.
697 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
698 fsp->fnum, fsp->fsp_name ));
700 brl_lock_cancel(br_lck,
709 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
710 DLIST_REMOVE(blocking_lock_queue, blr);
711 free_blocking_lock_record(blr);
715 if(!change_to_user(conn,vuid)) {
716 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
719 * Remove the entry and return an error to the client.
723 brl_lock_cancel(br_lck,
732 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
734 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
735 DLIST_REMOVE(blocking_lock_queue, blr);
736 free_blocking_lock_record(blr);
740 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
741 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
744 * Remove the entry and return an error to the client.
748 brl_lock_cancel(br_lck,
757 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
758 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
759 DLIST_REMOVE(blocking_lock_queue, blr);
760 free_blocking_lock_record(blr);
761 change_to_root_user();
766 * Go through the remaining locks and try and obtain them.
767 * The call returns True if all locks were obtained successfully
768 * and False if we still need to wait.
771 if(blocking_lock_record_process(blr)) {
772 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
775 brl_lock_cancel(br_lck,
784 DLIST_REMOVE(blocking_lock_queue, blr);
785 free_blocking_lock_record(blr);
787 change_to_root_user();
791 /****************************************************************************
792 Handle a cancel message. Lock already moved onto the cancel queue.
793 *****************************************************************************/
795 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
797 static void process_blocking_lock_cancel_message(int msg_type,
798 struct process_id src,
799 void *buf, size_t len,
803 const char *msg = (const char *)buf;
804 blocking_lock_record *blr;
807 smb_panic("process_blocking_lock_cancel_message: null msg\n");
810 if (len != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
811 DEBUG(0, ("process_blocking_lock_cancel_message: "
812 "Got invalid msg len %d\n", (int)len));
813 smb_panic("process_blocking_lock_cancel_message: bad msg\n");
816 memcpy(&blr, msg, sizeof(blr));
817 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
819 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
822 blocking_lock_reply_error(blr, err);
823 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
824 free_blocking_lock_record(blr);
827 /****************************************************************************
828 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
829 *****************************************************************************/
831 BOOL blocking_lock_cancel(files_struct *fsp,
835 enum brl_flavour lock_flav,
836 unsigned char locktype,
839 static BOOL initialized;
840 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
841 blocking_lock_record *blr;
844 /* Register our message. */
845 message_register(MSG_SMB_BLOCKING_LOCK_CANCEL,
846 process_blocking_lock_cancel_message,
852 for (blr = blocking_lock_queue; blr; blr = blr->next) {
853 if (fsp == blr->fsp &&
854 lock_pid == blr->lock_pid &&
855 offset == blr->offset &&
856 count == blr->count &&
857 lock_flav == blr->lock_flav) {
866 /* Check the flags are right. */
867 if (blr->com_type == SMBlockingX &&
868 (locktype & LOCKING_ANDX_LARGE_FILES) !=
869 (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
873 /* Move to cancelled queue. */
874 DLIST_REMOVE(blocking_lock_queue, blr);
875 DLIST_ADD(blocking_lock_cancelled_queue, blr);
877 /* Create the message. */
878 memcpy(msg, &blr, sizeof(blr));
879 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
881 message_send_pid(pid_to_procid(sys_getpid()),
882 MSG_SMB_BLOCKING_LOCK_CANCEL,
883 &msg, sizeof(msg), True);