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
25 /****************************************************************************
26 This is the structure to queue to implement blocking locks.
27 notify. It consists of the requesting SMB and the expiry time.
28 *****************************************************************************/
30 typedef struct _blocking_lock_record {
31 struct _blocking_lock_record *next;
32 struct _blocking_lock_record *prev;
35 struct timeval expire_time;
40 enum brl_flavour lock_flav;
41 enum brl_type lock_type;
44 } blocking_lock_record;
46 /* dlink list we store pending lock records on. */
47 static blocking_lock_record *blocking_lock_queue;
49 /* dlink list we move cancelled lock records onto. */
50 static blocking_lock_record *blocking_lock_cancelled_queue;
52 /****************************************************************************
53 Destructor for the above structure.
54 ****************************************************************************/
56 static void free_blocking_lock_record(blocking_lock_record *blr)
58 SAFE_FREE(blr->inbuf);
62 /****************************************************************************
63 Determine if this is a secondary element of a chained SMB.
64 **************************************************************************/
66 static BOOL in_chained_smb(void)
68 return (chain_size != 0);
71 static void received_unlock_msg(int msg_type, struct process_id src,
72 void *buf, size_t len);
74 /****************************************************************************
75 Function to push a blocking lock request onto the lock queue.
76 ****************************************************************************/
78 BOOL push_blocking_lock_request( struct byte_range_lock *br_lck,
79 char *inbuf, int length,
84 enum brl_type lock_type,
85 enum brl_flavour lock_flav,
86 SMB_BIG_UINT offset, SMB_BIG_UINT count)
88 static BOOL set_lock_msg;
89 blocking_lock_record *blr, *tmp;
92 if(in_chained_smb() ) {
93 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
98 * Now queue an entry on the blocking lock queue. We setup
99 * the expiration time here.
102 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
103 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
110 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
111 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
116 blr->com_type = CVAL(inbuf,smb_com);
118 if (lock_timeout == -1) {
119 blr->expire_time.tv_sec = 0;
120 blr->expire_time.tv_usec = 0; /* Never expire. */
122 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
123 (lock_timeout % 1000) * 1000);
125 blr->lock_num = lock_num;
126 blr->lock_pid = lock_pid;
127 blr->lock_flav = lock_flav;
128 blr->lock_type = lock_type;
129 blr->offset = offset;
131 memcpy(blr->inbuf, inbuf, length);
132 blr->length = length;
134 /* Add a pending lock record for this. */
135 status = brl_lock(br_lck,
140 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
142 lock_timeout ? True : False); /* blocking_lock. */
144 if (!NT_STATUS_IS_OK(status)) {
145 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
146 DLIST_REMOVE(blocking_lock_queue, blr);
147 free_blocking_lock_record(blr);
151 DLIST_ADD_END(blocking_lock_queue, blr, tmp);
153 /* Ensure we'll receive messages when this is unlocked. */
155 message_register(MSG_SMB_UNLOCK, received_unlock_msg);
159 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with "
160 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
161 length, (unsigned int)blr->expire_time.tv_sec,
162 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
163 blr->fsp->fnum, blr->fsp->fsp_name ));
165 /* Push the MID of this packet on the signing queue. */
166 srv_defer_sign_response(SVAL(inbuf,smb_mid));
171 /****************************************************************************
172 Return a smd with a given size.
173 *****************************************************************************/
175 static void send_blocking_reply(char *outbuf, int outsize)
178 smb_setlen(outbuf,outsize - 4);
180 if (!send_smb(smbd_server_fd(),outbuf))
181 exit_server("send_blocking_reply: send_smb failed.");
184 /****************************************************************************
185 Return a lockingX success SMB.
186 *****************************************************************************/
188 static void reply_lockingX_success(blocking_lock_record *blr)
190 char *outbuf = get_OutBuffer();
191 int bufsize = BUFFER_SIZE;
192 char *inbuf = blr->inbuf;
195 construct_reply_common(inbuf, outbuf);
196 set_message(outbuf,2,0,True);
199 * As this message is a lockingX call we must handle
200 * any following chained message correctly.
201 * This is normally handled in construct_reply(),
202 * but as that calls switch_message, we can't use
203 * that here and must set up the chain info manually.
206 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
208 outsize += chain_size;
210 send_blocking_reply(outbuf,outsize);
213 /****************************************************************************
214 Return a generic lock fail error blocking call.
215 *****************************************************************************/
217 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
219 char *outbuf = get_OutBuffer();
220 char *inbuf = blr->inbuf;
221 construct_reply_common(inbuf, outbuf);
223 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
224 FILE_LOCK_CONFLICT! (tridge) */
225 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
226 status = NT_STATUS_FILE_LOCK_CONFLICT;
229 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
230 /* Store the last lock error. */
231 files_struct *fsp = blr->fsp;
233 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
234 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
235 fsp->last_lock_failure.context.pid = procid_self();
236 fsp->last_lock_failure.start = blr->offset;
237 fsp->last_lock_failure.size = blr->count;
238 fsp->last_lock_failure.fnum = fsp->fnum;
239 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
240 fsp->last_lock_failure.lock_flav = blr->lock_flav;
244 if (!send_smb(smbd_server_fd(),outbuf)) {
245 exit_server("generic_blocking_lock_error: send_smb failed.");
249 /****************************************************************************
250 Return a lock fail error for a lockingX call. Undo all the locks we have
252 *****************************************************************************/
254 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
256 char *inbuf = blr->inbuf;
257 files_struct *fsp = blr->fsp;
258 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
259 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
261 unsigned char locktype = CVAL(inbuf,smb_vwv3);
262 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
266 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
269 * Data now points at the beginning of the list
270 * of smb_lkrng structs.
274 * Ensure we don't do a remove on the lock that just failed,
275 * as under POSIX rules, if we have a lock already there, we
276 * will delete it (and we shouldn't) .....
279 for(i = blr->lock_num - 1; i >= 0; i--) {
282 lock_pid = get_lock_pid( data, i, large_file_format);
283 count = get_lock_count( data, i, large_file_format);
284 offset = get_lock_offset( data, i, large_file_format, &err);
287 * We know err cannot be set as if it was the lock
288 * request would never have been queued. JRA.
298 generic_blocking_lock_error(blr, status);
301 /****************************************************************************
302 Return a lock fail error.
303 *****************************************************************************/
305 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
307 switch(blr->com_type) {
309 reply_lockingX_error(blr, status);
314 char *outbuf = get_OutBuffer();
315 char *inbuf = blr->inbuf;
316 construct_reply_common(inbuf, outbuf);
317 /* construct_reply_common has done us the favor to pre-fill the
318 * command field with SMBtranss2 which is wrong :-)
320 SCVAL(outbuf,smb_com,SMBtrans2);
322 if (!send_smb(smbd_server_fd(),outbuf)) {
323 exit_server("blocking_lock_reply_error: send_smb failed.");
328 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
329 exit_server("PANIC - unknown type on blocking lock queue");
333 /****************************************************************************
334 Attempt to finish off getting all pending blocking locks for a lockingX call.
335 Returns True if we want to be removed from the list.
336 *****************************************************************************/
338 static BOOL process_lockingX(blocking_lock_record *blr)
340 char *inbuf = blr->inbuf;
341 unsigned char locktype = CVAL(inbuf,smb_vwv3);
342 files_struct *fsp = blr->fsp;
343 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
344 uint16 num_locks = SVAL(inbuf,smb_vwv7);
345 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
347 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
349 NTSTATUS status = NT_STATUS_OK;
351 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
354 * Data now points at the beginning of the list
355 * of smb_lkrng structs.
358 for(; blr->lock_num < num_locks; blr->lock_num++) {
359 struct byte_range_lock *br_lck = NULL;
362 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
363 count = get_lock_count( data, blr->lock_num, large_file_format);
364 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
367 * We know err cannot be set as if it was the lock
368 * request would never have been queued. JRA.
371 br_lck = do_lock(fsp,
375 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
376 READ_LOCK : WRITE_LOCK),
383 if (NT_STATUS_IS_ERR(status)) {
388 if(blr->lock_num == num_locks) {
390 * Success - we got all the locks.
393 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
394 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
396 reply_lockingX_success(blr);
398 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
399 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
401 * We have other than a "can't get lock"
402 * error. Free any locks we had and return an error.
403 * Return True so we get dequeued.
406 blocking_lock_reply_error(blr, status);
411 * Still can't get all the locks - keep waiting.
414 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
416 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
421 /****************************************************************************
422 Attempt to get the posix lock request from a SMBtrans2 call.
423 Returns True if we want to be removed from the list.
424 *****************************************************************************/
426 static BOOL process_trans2(blocking_lock_record *blr)
429 char *inbuf = blr->inbuf;
433 struct byte_range_lock *br_lck = do_lock(blr->fsp,
443 if (!NT_STATUS_IS_OK(status)) {
444 if (ERROR_WAS_LOCK_DENIED(status)) {
445 /* Still can't get the lock, just keep waiting. */
449 * We have other than a "can't get lock"
450 * error. Send an error and return True so we get dequeued.
452 blocking_lock_reply_error(blr, status);
456 /* We finally got the lock, return success. */
457 outbuf = get_OutBuffer();
458 construct_reply_common(inbuf, outbuf);
459 SCVAL(outbuf,smb_com,SMBtrans2);
461 /* Fake up max_data_bytes here - we know it fits. */
462 send_trans2_replies(outbuf, max_send, params, 2, NULL, 0, 0xffff);
467 /****************************************************************************
468 Process a blocking lock SMB.
469 Returns True if we want to be removed from the list.
470 *****************************************************************************/
472 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
474 switch(blr->com_type) {
476 return process_lockingX(blr);
479 return process_trans2(blr);
481 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
482 exit_server("PANIC - unknown type on blocking lock queue");
484 return False; /* Keep compiler happy. */
487 /****************************************************************************
488 Cancel entries by fnum from the blocking lock pending queue.
489 *****************************************************************************/
491 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
493 blocking_lock_record *blr, *next = NULL;
495 for(blr = blocking_lock_queue; blr; blr = next) {
497 if(blr->fsp->fnum == fsp->fnum) {
498 unsigned char locktype = 0;
500 if (blr->com_type == SMBlockingX) {
501 locktype = CVAL(blr->inbuf,smb_vwv3);
505 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
506 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
508 brl_lock_cancel(br_lck,
515 blocking_lock_cancel(fsp,
521 NT_STATUS_RANGE_NOT_LOCKED);
527 /****************************************************************************
528 Delete entries by mid from the blocking lock pending queue. Always send reply.
529 *****************************************************************************/
531 void remove_pending_lock_requests_by_mid(int mid)
533 blocking_lock_record *blr, *next = NULL;
535 for(blr = blocking_lock_queue; blr; blr = next) {
537 if(SVAL(blr->inbuf,smb_mid) == mid) {
538 files_struct *fsp = blr->fsp;
539 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
542 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
543 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
545 brl_lock_cancel(br_lck,
554 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
555 DLIST_REMOVE(blocking_lock_queue, blr);
556 free_blocking_lock_record(blr);
561 /****************************************************************************
562 Set a flag as an unlock request affects one of our pending locks.
563 *****************************************************************************/
565 static void received_unlock_msg(int msg_type, struct process_id src,
566 void *buf, size_t len)
568 DEBUG(10,("received_unlock_msg\n"));
569 process_blocking_lock_queue();
572 /****************************************************************************
573 Return the number of milliseconds to the next blocking locks timeout, or default_timeout
574 *****************************************************************************/
576 unsigned int blocking_locks_timeout_ms(unsigned int default_timeout_ms)
578 unsigned int timeout_ms = default_timeout_ms;
579 struct timeval tv_curr;
580 SMB_BIG_INT min_tv_dif_us = 0x7FFFFFFF; /* A large +ve number. */
581 blocking_lock_record *blr = blocking_lock_queue;
583 /* note that we avoid the GetTimeOfDay() syscall if there are no blocking locks */
588 tv_curr = timeval_current();
590 for (; blr; blr = blr->next) {
591 SMB_BIG_INT tv_dif_us;
593 if (timeval_is_zero(&blr->expire_time)) {
594 continue; /* Never timeout. */
597 tv_dif_us = usec_time_diff(&blr->expire_time, &tv_curr);
598 min_tv_dif_us = MIN(min_tv_dif_us, tv_dif_us);
601 if (min_tv_dif_us < 0) {
605 timeout_ms = (unsigned int)(min_tv_dif_us / (SMB_BIG_INT)1000);
607 if (timeout_ms < 1) {
611 DEBUG(10,("blocking_locks_timeout_ms: returning %u\n", timeout_ms));
616 /****************************************************************************
617 Process the blocking lock queue. Note that this is only called as root.
618 *****************************************************************************/
620 void process_blocking_lock_queue(void)
622 struct timeval tv_curr = timeval_current();
623 blocking_lock_record *blr, *next = NULL;
626 * Go through the queue and see if we can get any of the locks.
629 for (blr = blocking_lock_queue; blr; blr = next) {
630 connection_struct *conn = NULL;
632 files_struct *fsp = NULL;
637 * Ensure we don't have any old chain_fsp values
644 conn = conn_find(SVAL(blr->inbuf,smb_tid));
645 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
646 SVAL(blr->inbuf,smb_uid);
648 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
649 fsp->fnum, fsp->fsp_name ));
651 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
652 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
655 * Lock expired - throw away all previously
656 * obtained locks and return lock error.
660 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
661 fsp->fnum, fsp->fsp_name ));
663 brl_lock_cancel(br_lck,
672 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
673 DLIST_REMOVE(blocking_lock_queue, blr);
674 free_blocking_lock_record(blr);
678 if(!change_to_user(conn,vuid)) {
679 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
682 * Remove the entry and return an error to the client.
686 brl_lock_cancel(br_lck,
695 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
697 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
698 DLIST_REMOVE(blocking_lock_queue, blr);
699 free_blocking_lock_record(blr);
703 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
704 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
707 * Remove the entry and return an error to the client.
711 brl_lock_cancel(br_lck,
720 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
721 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
722 DLIST_REMOVE(blocking_lock_queue, blr);
723 free_blocking_lock_record(blr);
724 change_to_root_user();
729 * Go through the remaining locks and try and obtain them.
730 * The call returns True if all locks were obtained successfully
731 * and False if we still need to wait.
734 if(blocking_lock_record_process(blr)) {
735 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
738 brl_lock_cancel(br_lck,
747 DLIST_REMOVE(blocking_lock_queue, blr);
748 free_blocking_lock_record(blr);
750 change_to_root_user();
754 /****************************************************************************
755 Handle a cancel message. Lock already moved onto the cancel queue.
756 *****************************************************************************/
758 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
760 static void process_blocking_lock_cancel_message(int msg_type, struct process_id src,
761 void *buf, size_t len)
764 const char *msg = (const char *)buf;
765 blocking_lock_record *blr;
768 smb_panic("process_blocking_lock_cancel_message: null msg\n");
771 if (len != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
772 DEBUG(0, ("process_blocking_lock_cancel_message: "
773 "Got invalid msg len %d\n", (int)len));
774 smb_panic("process_blocking_lock_cancel_message: bad msg\n");
777 memcpy(&blr, msg, sizeof(blr));
778 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
780 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
783 blocking_lock_reply_error(blr, err);
784 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
785 free_blocking_lock_record(blr);
788 /****************************************************************************
789 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
790 *****************************************************************************/
792 BOOL blocking_lock_cancel(files_struct *fsp,
796 enum brl_flavour lock_flav,
797 unsigned char locktype,
800 static BOOL initialized;
801 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
802 blocking_lock_record *blr;
805 /* Register our message. */
806 message_register(MSG_SMB_BLOCKING_LOCK_CANCEL,
807 process_blocking_lock_cancel_message);
812 for (blr = blocking_lock_queue; blr; blr = blr->next) {
813 if (fsp == blr->fsp &&
814 lock_pid == blr->lock_pid &&
815 offset == blr->offset &&
816 count == blr->count &&
817 lock_flav == blr->lock_flav) {
826 /* Check the flags are right. */
827 if (blr->com_type == SMBlockingX &&
828 (locktype & LOCKING_ANDX_LARGE_FILES) !=
829 (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
833 /* Move to cancelled queue. */
834 DLIST_REMOVE(blocking_lock_queue, blr);
835 DLIST_ADD(blocking_lock_cancelled_queue, blr);
837 /* Create the message. */
838 memcpy(msg, &blr, sizeof(blr));
839 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
841 message_send_pid(pid_to_procid(sys_getpid()),
842 MSG_SMB_BLOCKING_LOCK_CANCEL,
843 &msg, sizeof(msg), True);