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 enum brl_flavour lock_flav;
43 enum brl_type lock_type;
46 } blocking_lock_record;
48 /* dlink list we store pending lock records on. */
49 static blocking_lock_record *blocking_lock_queue;
51 /* dlink list we move cancelled lock records onto. */
52 static blocking_lock_record *blocking_lock_cancelled_queue;
54 /****************************************************************************
55 Destructor for the above structure.
56 ****************************************************************************/
58 static void free_blocking_lock_record(blocking_lock_record *blr)
60 SAFE_FREE(blr->inbuf);
64 /****************************************************************************
65 Determine if this is a secondary element of a chained SMB.
66 **************************************************************************/
68 static BOOL in_chained_smb(void)
70 return (chain_size != 0);
73 static void received_unlock_msg(int msg_type, struct process_id src,
74 void *buf, size_t len,
77 /****************************************************************************
78 Function to push a blocking lock request onto the lock queue.
79 ****************************************************************************/
81 BOOL push_blocking_lock_request( struct byte_range_lock *br_lck,
82 char *inbuf, int length,
87 enum brl_type lock_type,
88 enum brl_flavour lock_flav,
89 SMB_BIG_UINT offset, SMB_BIG_UINT count)
91 static BOOL set_lock_msg;
92 blocking_lock_record *blr;
95 if(in_chained_smb() ) {
96 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
101 * Now queue an entry on the blocking lock queue. We setup
102 * the expiration time here.
105 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
106 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
113 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
114 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
119 blr->com_type = CVAL(inbuf,smb_com);
121 if (lock_timeout == -1) {
122 blr->expire_time.tv_sec = 0;
123 blr->expire_time.tv_usec = 0; /* Never expire. */
125 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
126 (lock_timeout % 1000) * 1000);
128 blr->lock_num = lock_num;
129 blr->lock_pid = lock_pid;
130 blr->lock_flav = lock_flav;
131 blr->lock_type = lock_type;
132 blr->offset = offset;
134 memcpy(blr->inbuf, inbuf, length);
135 blr->length = length;
137 /* Add a pending lock record for this. */
138 status = brl_lock(br_lck,
143 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
145 lock_timeout ? True : False); /* blocking_lock. */
147 if (!NT_STATUS_IS_OK(status)) {
148 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
149 DLIST_REMOVE(blocking_lock_queue, blr);
150 free_blocking_lock_record(blr);
154 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
156 /* Ensure we'll receive messages when this is unlocked. */
158 message_register(MSG_SMB_UNLOCK, received_unlock_msg,
163 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with "
164 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
165 length, (unsigned int)blr->expire_time.tv_sec,
166 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
167 blr->fsp->fnum, blr->fsp->fsp_name ));
169 /* Push the MID of this packet on the signing queue. */
170 srv_defer_sign_response(SVAL(inbuf,smb_mid));
175 /****************************************************************************
176 Return a smd with a given size.
177 *****************************************************************************/
179 static void send_blocking_reply(char *outbuf, int outsize)
182 smb_setlen(outbuf,outsize - 4);
184 if (!send_smb(smbd_server_fd(),outbuf))
185 exit_server_cleanly("send_blocking_reply: send_smb failed.");
188 /****************************************************************************
189 Return a lockingX success SMB.
190 *****************************************************************************/
192 static void reply_lockingX_success(blocking_lock_record *blr)
194 char *outbuf = get_OutBuffer();
195 int bufsize = BUFFER_SIZE;
196 char *inbuf = blr->inbuf;
199 construct_reply_common(inbuf, outbuf);
200 set_message(outbuf,2,0,True);
203 * As this message is a lockingX call we must handle
204 * any following chained message correctly.
205 * This is normally handled in construct_reply(),
206 * but as that calls switch_message, we can't use
207 * that here and must set up the chain info manually.
210 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
212 outsize += chain_size;
214 send_blocking_reply(outbuf,outsize);
217 /****************************************************************************
218 Return a generic lock fail error blocking call.
219 *****************************************************************************/
221 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
223 char *outbuf = get_OutBuffer();
224 char *inbuf = blr->inbuf;
225 construct_reply_common(inbuf, outbuf);
227 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
228 FILE_LOCK_CONFLICT! (tridge) */
229 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
230 status = NT_STATUS_FILE_LOCK_CONFLICT;
233 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
234 /* Store the last lock error. */
235 files_struct *fsp = blr->fsp;
237 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
238 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
239 fsp->last_lock_failure.context.pid = procid_self();
240 fsp->last_lock_failure.start = blr->offset;
241 fsp->last_lock_failure.size = blr->count;
242 fsp->last_lock_failure.fnum = fsp->fnum;
243 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
244 fsp->last_lock_failure.lock_flav = blr->lock_flav;
248 if (!send_smb(smbd_server_fd(),outbuf)) {
249 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
253 /****************************************************************************
254 Return a lock fail error for a lockingX call. Undo all the locks we have
256 *****************************************************************************/
258 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
260 char *inbuf = blr->inbuf;
261 files_struct *fsp = blr->fsp;
262 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
263 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
265 unsigned char locktype = CVAL(inbuf,smb_vwv3);
266 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
270 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
273 * Data now points at the beginning of the list
274 * of smb_lkrng structs.
278 * Ensure we don't do a remove on the lock that just failed,
279 * as under POSIX rules, if we have a lock already there, we
280 * will delete it (and we shouldn't) .....
283 for(i = blr->lock_num - 1; i >= 0; i--) {
286 lock_pid = get_lock_pid( data, i, large_file_format);
287 count = get_lock_count( data, i, large_file_format);
288 offset = get_lock_offset( data, i, large_file_format, &err);
291 * We know err cannot be set as if it was the lock
292 * request would never have been queued. JRA.
302 generic_blocking_lock_error(blr, status);
305 /****************************************************************************
306 Return a lock fail error.
307 *****************************************************************************/
309 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
311 switch(blr->com_type) {
313 reply_lockingX_error(blr, status);
318 char *outbuf = get_OutBuffer();
319 char *inbuf = blr->inbuf;
320 construct_reply_common(inbuf, outbuf);
321 /* construct_reply_common has done us the favor to pre-fill the
322 * command field with SMBtranss2 which is wrong :-)
324 SCVAL(outbuf,smb_com,SMBtrans2);
326 if (!send_smb(smbd_server_fd(),outbuf)) {
327 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
332 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
333 exit_server("PANIC - unknown type on blocking lock queue");
337 /****************************************************************************
338 Attempt to finish off getting all pending blocking locks for a lockingX call.
339 Returns True if we want to be removed from the list.
340 *****************************************************************************/
342 static BOOL process_lockingX(blocking_lock_record *blr)
344 char *inbuf = blr->inbuf;
345 unsigned char locktype = CVAL(inbuf,smb_vwv3);
346 files_struct *fsp = blr->fsp;
347 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
348 uint16 num_locks = SVAL(inbuf,smb_vwv7);
349 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
351 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
353 NTSTATUS status = NT_STATUS_OK;
355 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
358 * Data now points at the beginning of the list
359 * of smb_lkrng structs.
362 for(; blr->lock_num < num_locks; blr->lock_num++) {
363 struct byte_range_lock *br_lck = NULL;
366 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
367 count = get_lock_count( data, blr->lock_num, large_file_format);
368 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
371 * We know err cannot be set as if it was the lock
372 * request would never have been queued. JRA.
375 br_lck = do_lock(fsp,
379 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
380 READ_LOCK : WRITE_LOCK),
387 if (NT_STATUS_IS_ERR(status)) {
392 if(blr->lock_num == num_locks) {
394 * Success - we got all the locks.
397 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
398 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
400 reply_lockingX_success(blr);
402 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
403 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
405 * We have other than a "can't get lock"
406 * error. Free any locks we had and return an error.
407 * Return True so we get dequeued.
410 blocking_lock_reply_error(blr, status);
415 * Still can't get all the locks - keep waiting.
418 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
420 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
425 /****************************************************************************
426 Attempt to get the posix lock request from a SMBtrans2 call.
427 Returns True if we want to be removed from the list.
428 *****************************************************************************/
430 static BOOL process_trans2(blocking_lock_record *blr)
432 char *inbuf = blr->inbuf;
436 struct byte_range_lock *br_lck = do_lock(blr->fsp,
446 if (!NT_STATUS_IS_OK(status)) {
447 if (ERROR_WAS_LOCK_DENIED(status)) {
448 /* Still can't get the lock, just keep waiting. */
452 * We have other than a "can't get lock"
453 * error. Send an error and return True so we get dequeued.
455 blocking_lock_reply_error(blr, status);
459 /* We finally got the lock, return success. */
460 outbuf = get_OutBuffer();
461 construct_reply_common(inbuf, outbuf);
462 SCVAL(outbuf,smb_com,SMBtrans2);
464 /* Fake up max_data_bytes here - we know it fits. */
465 send_trans2_replies(outbuf, max_send, params, 2, NULL, 0, 0xffff);
470 /****************************************************************************
471 Process a blocking lock SMB.
472 Returns True if we want to be removed from the list.
473 *****************************************************************************/
475 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
477 switch(blr->com_type) {
479 return process_lockingX(blr);
482 return process_trans2(blr);
484 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
485 exit_server("PANIC - unknown type on blocking lock queue");
487 return False; /* Keep compiler happy. */
490 /****************************************************************************
491 Cancel entries by fnum from the blocking lock pending queue.
492 *****************************************************************************/
494 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
496 blocking_lock_record *blr, *next = NULL;
498 for(blr = blocking_lock_queue; blr; blr = next) {
500 if(blr->fsp->fnum == fsp->fnum) {
501 unsigned char locktype = 0;
503 if (blr->com_type == SMBlockingX) {
504 locktype = CVAL(blr->inbuf,smb_vwv3);
508 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
509 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
511 brl_lock_cancel(br_lck,
518 blocking_lock_cancel(fsp,
524 NT_STATUS_RANGE_NOT_LOCKED);
530 /****************************************************************************
531 Delete entries by mid from the blocking lock pending queue. Always send reply.
532 *****************************************************************************/
534 void remove_pending_lock_requests_by_mid(int mid)
536 blocking_lock_record *blr, *next = NULL;
538 for(blr = blocking_lock_queue; blr; blr = next) {
540 if(SVAL(blr->inbuf,smb_mid) == mid) {
541 files_struct *fsp = blr->fsp;
542 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
545 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
546 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
548 brl_lock_cancel(br_lck,
557 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
558 DLIST_REMOVE(blocking_lock_queue, blr);
559 free_blocking_lock_record(blr);
564 /****************************************************************************
565 Is this mid a blocking lock request on the queue ?
566 *****************************************************************************/
568 BOOL blocking_lock_was_deferred(int mid)
570 blocking_lock_record *blr, *next = NULL;
572 for(blr = blocking_lock_queue; blr; blr = next) {
574 if(SVAL(blr->inbuf,smb_mid) == mid) {
581 /****************************************************************************
582 Set a flag as an unlock request affects one of our pending locks.
583 *****************************************************************************/
585 static void received_unlock_msg(int msg_type, struct process_id src,
586 void *buf, size_t len,
589 DEBUG(10,("received_unlock_msg\n"));
590 process_blocking_lock_queue();
593 /****************************************************************************
594 Return the number of milliseconds to the next blocking locks timeout, or default_timeout
595 *****************************************************************************/
597 unsigned int blocking_locks_timeout_ms(unsigned int default_timeout_ms)
599 unsigned int timeout_ms = default_timeout_ms;
600 struct timeval tv_curr;
601 SMB_BIG_INT min_tv_dif_us = 0x7FFFFFFF; /* A large +ve number. */
602 blocking_lock_record *blr = blocking_lock_queue;
604 /* note that we avoid the GetTimeOfDay() syscall if there are no blocking locks */
609 tv_curr = timeval_current();
611 for (; blr; blr = blr->next) {
612 SMB_BIG_INT tv_dif_us;
614 if (timeval_is_zero(&blr->expire_time)) {
615 continue; /* Never timeout. */
618 tv_dif_us = usec_time_diff(&blr->expire_time, &tv_curr);
619 min_tv_dif_us = MIN(min_tv_dif_us, tv_dif_us);
622 if (min_tv_dif_us < 0) {
626 timeout_ms = (unsigned int)(min_tv_dif_us / (SMB_BIG_INT)1000);
628 if (timeout_ms < 1) {
632 DEBUG(10,("blocking_locks_timeout_ms: returning %u\n", timeout_ms));
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 blocking_lock_record *blr, *next = NULL;
647 * Go through the queue and see if we can get any of the locks.
650 for (blr = blocking_lock_queue; blr; blr = next) {
651 connection_struct *conn = NULL;
653 files_struct *fsp = NULL;
658 * Ensure we don't have any old chain_fsp values
665 conn = conn_find(SVAL(blr->inbuf,smb_tid));
666 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
667 SVAL(blr->inbuf,smb_uid);
669 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
670 fsp->fnum, fsp->fsp_name ));
672 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
673 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
676 * Lock expired - throw away all previously
677 * obtained locks and return lock error.
681 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
682 fsp->fnum, fsp->fsp_name ));
684 brl_lock_cancel(br_lck,
693 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
694 DLIST_REMOVE(blocking_lock_queue, blr);
695 free_blocking_lock_record(blr);
699 if(!change_to_user(conn,vuid)) {
700 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
703 * Remove the entry and return an error to the client.
707 brl_lock_cancel(br_lck,
716 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
718 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
719 DLIST_REMOVE(blocking_lock_queue, blr);
720 free_blocking_lock_record(blr);
724 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
725 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
728 * Remove the entry and return an error to the client.
732 brl_lock_cancel(br_lck,
741 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
742 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
743 DLIST_REMOVE(blocking_lock_queue, blr);
744 free_blocking_lock_record(blr);
745 change_to_root_user();
750 * Go through the remaining locks and try and obtain them.
751 * The call returns True if all locks were obtained successfully
752 * and False if we still need to wait.
755 if(blocking_lock_record_process(blr)) {
756 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
759 brl_lock_cancel(br_lck,
768 DLIST_REMOVE(blocking_lock_queue, blr);
769 free_blocking_lock_record(blr);
771 change_to_root_user();
775 /****************************************************************************
776 Handle a cancel message. Lock already moved onto the cancel queue.
777 *****************************************************************************/
779 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
781 static void process_blocking_lock_cancel_message(int msg_type,
782 struct process_id src,
783 void *buf, size_t len,
787 const char *msg = (const char *)buf;
788 blocking_lock_record *blr;
791 smb_panic("process_blocking_lock_cancel_message: null msg\n");
794 if (len != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
795 DEBUG(0, ("process_blocking_lock_cancel_message: "
796 "Got invalid msg len %d\n", (int)len));
797 smb_panic("process_blocking_lock_cancel_message: bad msg\n");
800 memcpy(&blr, msg, sizeof(blr));
801 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
803 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
806 blocking_lock_reply_error(blr, err);
807 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
808 free_blocking_lock_record(blr);
811 /****************************************************************************
812 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
813 *****************************************************************************/
815 BOOL blocking_lock_cancel(files_struct *fsp,
819 enum brl_flavour lock_flav,
820 unsigned char locktype,
823 static BOOL initialized;
824 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
825 blocking_lock_record *blr;
828 /* Register our message. */
829 message_register(MSG_SMB_BLOCKING_LOCK_CANCEL,
830 process_blocking_lock_cancel_message,
836 for (blr = blocking_lock_queue; blr; blr = blr->next) {
837 if (fsp == blr->fsp &&
838 lock_pid == blr->lock_pid &&
839 offset == blr->offset &&
840 count == blr->count &&
841 lock_flav == blr->lock_flav) {
850 /* Check the flags are right. */
851 if (blr->com_type == SMBlockingX &&
852 (locktype & LOCKING_ANDX_LARGE_FILES) !=
853 (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
857 /* Move to cancelled queue. */
858 DLIST_REMOVE(blocking_lock_queue, blr);
859 DLIST_ADD(blocking_lock_cancelled_queue, blr);
861 /* Create the message. */
862 memcpy(msg, &blr, sizeof(blr));
863 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
865 message_send_pid(pid_to_procid(sys_getpid()),
866 MSG_SMB_BLOCKING_LOCK_CANCEL,
867 &msg, sizeof(msg), True);