[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / smbd / blocking.c
blobb9e7c14470576b49ed670b11a7f9580318256392
1 /*
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.
21 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_LOCKING
25 extern int max_send;
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;
35 int com_type;
36 files_struct *fsp;
37 struct timeval expire_time;
38 int lock_num;
39 SMB_BIG_UINT offset;
40 SMB_BIG_UINT count;
41 uint32 lock_pid;
42 uint32 blocking_pid; /* PID that blocks us. */
43 enum brl_flavour lock_flav;
44 enum brl_type lock_type;
45 char *inbuf;
46 int length;
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);
62 SAFE_FREE(blr);
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,
76 void *private_data);
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,
84 files_struct *fsp,
85 int lock_timeout,
86 int lock_num,
87 uint32 lock_pid,
88 enum brl_type lock_type,
89 enum brl_flavour lock_flav,
90 SMB_BIG_UINT offset,
91 SMB_BIG_UINT count,
92 uint32 blocking_pid)
94 static BOOL set_lock_msg;
95 blocking_lock_record *blr;
96 NTSTATUS status;
98 if(in_chained_smb() ) {
99 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
100 return False;
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" ));
110 return False;
113 blr->next = NULL;
114 blr->prev = NULL;
116 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
117 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
118 SAFE_FREE(blr);
119 return False;
122 blr->com_type = CVAL(inbuf,smb_com);
123 blr->fsp = fsp;
124 if (lock_timeout == -1) {
125 blr->expire_time.tv_sec = 0;
126 blr->expire_time.tv_usec = 0; /* Never expire. */
127 } else {
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;
137 blr->count = count;
138 memcpy(blr->inbuf, inbuf, length);
139 blr->length = length;
141 /* Add a pending lock record for this. */
142 status = brl_lock(br_lck,
143 lock_pid,
144 procid_self(),
145 offset,
146 count,
147 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
148 blr->lock_flav,
149 lock_timeout ? True : False, /* blocking_lock. */
150 NULL);
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);
156 return False;
159 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
161 /* Ensure we'll receive messages when this is unlocked. */
162 if (!set_lock_msg) {
163 message_register(MSG_SMB_UNLOCK, received_unlock_msg,
164 NULL);
165 set_lock_msg = True;
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));
177 return True;
180 /****************************************************************************
181 Return a smd with a given size.
182 *****************************************************************************/
184 static void send_blocking_reply(char *outbuf, int outsize)
186 if(outsize > 4)
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;
202 int outsize = 0;
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;
252 ERROR_NT(status);
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
260 obtained first.
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;
269 uint32 lock_pid;
270 unsigned char locktype = CVAL(inbuf,smb_vwv3);
271 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
272 char *data;
273 int i;
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--) {
289 BOOL err;
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.
300 do_unlock(fsp,
301 lock_pid,
302 count,
303 offset,
304 WINDOWS_LOCK);
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) {
317 case SMBlockingX:
318 reply_lockingX_error(blr, status);
319 break;
320 case SMBtrans2:
321 case SMBtranss2:
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);
330 ERROR_NT(status);
331 if (!send_smb(smbd_server_fd(),outbuf)) {
332 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
334 break;
336 default:
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;
355 uint32 lock_pid;
356 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
357 char *data;
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;
369 BOOL err;
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.
379 errno = 0;
380 br_lck = do_lock(fsp,
381 lock_pid,
382 count,
383 offset,
384 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
385 READ_LOCK : WRITE_LOCK),
386 WINDOWS_LOCK,
387 True,
388 &status,
389 &blr->blocking_pid);
391 TALLOC_FREE(br_lck);
393 if (NT_STATUS_IS_ERR(status)) {
394 break;
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);
407 return True;
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);
417 return True;
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. \
425 Waiting....\n",
426 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
428 return False;
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;
439 char *outbuf;
440 char params[2];
441 NTSTATUS status;
442 struct byte_range_lock *br_lck = do_lock(blr->fsp,
443 blr->lock_pid,
444 blr->count,
445 blr->offset,
446 blr->lock_type,
447 blr->lock_flav,
448 True,
449 &status,
450 &blr->blocking_pid);
451 TALLOC_FREE(br_lck);
453 if (!NT_STATUS_IS_OK(status)) {
454 if (ERROR_WAS_LOCK_DENIED(status)) {
455 /* Still can't get the lock, just keep waiting. */
456 return False;
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);
463 return True;
466 /* We finally got the lock, return success. */
467 outbuf = get_OutBuffer();
468 construct_reply_common(inbuf, outbuf);
469 SCVAL(outbuf,smb_com,SMBtrans2);
470 SSVAL(params,0,0);
471 /* Fake up max_data_bytes here - we know it fits. */
472 send_trans2_replies(outbuf, max_send, params, 2, NULL, 0, 0xffff);
473 return True;
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) {
485 case SMBlockingX:
486 return process_lockingX(blr);
487 case SMBtrans2:
488 case SMBtranss2:
489 return process_trans2(blr);
490 default:
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) {
506 next = 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);
514 if (br_lck) {
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,
519 blr->lock_pid,
520 procid_self(),
521 blr->offset,
522 blr->count,
523 blr->lock_flav);
525 blocking_lock_cancel(fsp,
526 blr->lock_pid,
527 blr->offset,
528 blr->count,
529 blr->lock_flav,
530 locktype,
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) {
546 next = 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);
551 if (br_lck) {
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,
556 blr->lock_pid,
557 procid_self(),
558 blr->offset,
559 blr->count,
560 blr->lock_flav);
561 TALLOC_FREE(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) {
580 next = blr->next;
581 if(SVAL(blr->inbuf,smb_mid) == mid) {
582 return True;
585 return False;
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,
594 void *private_data)
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 */
612 if (!blr) {
613 return timeout_ms;
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
625 * 10 seconds.
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) {
639 min_tv_dif_us = 0;
642 timeout_ms = (unsigned int)(min_tv_dif_us / (SMB_BIG_INT)1000);
644 if (timeout_ms < 1) {
645 timeout_ms = 1;
648 DEBUG(10,("blocking_locks_timeout_ms: returning %u\n", timeout_ms));
650 return 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;
668 uint16 vuid;
669 files_struct *fsp = NULL;
671 next = blr->next;
674 * Ensure we don't have any old chain_fsp values
675 * sitting around....
677 chain_size = 0;
678 file_chain_reset();
679 fsp = blr->fsp;
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(!change_to_user(conn,vuid)) {
689 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
692 * Remove the entry and return an error to the client.
695 if (br_lck) {
696 brl_lock_cancel(br_lck,
697 blr->lock_pid,
698 procid_self(),
699 blr->offset,
700 blr->count,
701 blr->lock_flav);
702 TALLOC_FREE(br_lck);
705 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
706 vuid ));
707 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
708 DLIST_REMOVE(blocking_lock_queue, blr);
709 free_blocking_lock_record(blr);
710 continue;
713 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
714 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
717 * Remove the entry and return an error to the client.
720 if (br_lck) {
721 brl_lock_cancel(br_lck,
722 blr->lock_pid,
723 procid_self(),
724 blr->offset,
725 blr->count,
726 blr->lock_flav);
727 TALLOC_FREE(br_lck);
730 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
731 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
732 DLIST_REMOVE(blocking_lock_queue, blr);
733 free_blocking_lock_record(blr);
734 change_to_root_user();
735 continue;
739 * Go through the remaining locks and try and obtain them.
740 * The call returns True if all locks were obtained successfully
741 * and False if we still need to wait.
744 if(blocking_lock_record_process(blr)) {
745 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
747 if (br_lck) {
748 brl_lock_cancel(br_lck,
749 blr->lock_pid,
750 procid_self(),
751 blr->offset,
752 blr->count,
753 blr->lock_flav);
754 TALLOC_FREE(br_lck);
757 DLIST_REMOVE(blocking_lock_queue, blr);
758 free_blocking_lock_record(blr);
759 change_to_root_user();
760 continue;
763 change_to_root_user();
766 * We couldn't get the locks for this record on the list.
767 * If the time has expired, return a lock error.
770 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
771 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
774 * Lock expired - throw away all previously
775 * obtained locks and return lock error.
778 if (br_lck) {
779 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
780 fsp->fnum, fsp->fsp_name ));
782 brl_lock_cancel(br_lck,
783 blr->lock_pid,
784 procid_self(),
785 blr->offset,
786 blr->count,
787 blr->lock_flav);
788 TALLOC_FREE(br_lck);
791 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
792 DLIST_REMOVE(blocking_lock_queue, blr);
793 free_blocking_lock_record(blr);
794 continue;
800 /****************************************************************************
801 Handle a cancel message. Lock already moved onto the cancel queue.
802 *****************************************************************************/
804 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
806 static void process_blocking_lock_cancel_message(int msg_type,
807 struct process_id src,
808 void *buf, size_t len,
809 void *private_data)
811 NTSTATUS err;
812 const char *msg = (const char *)buf;
813 blocking_lock_record *blr;
815 if (buf == NULL) {
816 smb_panic("process_blocking_lock_cancel_message: null msg\n");
819 if (len != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
820 DEBUG(0, ("process_blocking_lock_cancel_message: "
821 "Got invalid msg len %d\n", (int)len));
822 smb_panic("process_blocking_lock_cancel_message: bad msg\n");
825 memcpy(&blr, msg, sizeof(blr));
826 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
828 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
829 nt_errstr(err) ));
831 blocking_lock_reply_error(blr, err);
832 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
833 free_blocking_lock_record(blr);
836 /****************************************************************************
837 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
838 *****************************************************************************/
840 BOOL blocking_lock_cancel(files_struct *fsp,
841 uint32 lock_pid,
842 SMB_BIG_UINT offset,
843 SMB_BIG_UINT count,
844 enum brl_flavour lock_flav,
845 unsigned char locktype,
846 NTSTATUS err)
848 static BOOL initialized;
849 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
850 blocking_lock_record *blr;
852 if (!initialized) {
853 /* Register our message. */
854 message_register(MSG_SMB_BLOCKING_LOCK_CANCEL,
855 process_blocking_lock_cancel_message,
856 NULL);
858 initialized = True;
861 for (blr = blocking_lock_queue; blr; blr = blr->next) {
862 if (fsp == blr->fsp &&
863 lock_pid == blr->lock_pid &&
864 offset == blr->offset &&
865 count == blr->count &&
866 lock_flav == blr->lock_flav) {
867 break;
871 if (!blr) {
872 return False;
875 /* Check the flags are right. */
876 if (blr->com_type == SMBlockingX &&
877 (locktype & LOCKING_ANDX_LARGE_FILES) !=
878 (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
879 return False;
882 /* Move to cancelled queue. */
883 DLIST_REMOVE(blocking_lock_queue, blr);
884 DLIST_ADD(blocking_lock_cancelled_queue, blr);
886 /* Create the message. */
887 memcpy(msg, &blr, sizeof(blr));
888 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
890 message_send_pid(pid_to_procid(sys_getpid()),
891 MSG_SMB_BLOCKING_LOCK_CANCEL,
892 &msg, sizeof(msg), True);
894 return True;