r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / smbd / blocking.c
blobed1977e3bec9297a0c1da5550a64a4c47cc74afc
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 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #undef DBGC_CLASS
22 #define DBGC_CLASS DBGC_LOCKING
24 extern int max_send;
26 /****************************************************************************
27 This is the structure to queue to implement blocking locks.
28 notify. It consists of the requesting SMB and the expiry time.
29 *****************************************************************************/
31 typedef struct _blocking_lock_record {
32 struct _blocking_lock_record *next;
33 struct _blocking_lock_record *prev;
34 int com_type;
35 files_struct *fsp;
36 struct timeval expire_time;
37 int lock_num;
38 SMB_BIG_UINT offset;
39 SMB_BIG_UINT count;
40 uint32 lock_pid;
41 uint32 blocking_pid; /* PID that blocks us. */
42 enum brl_flavour lock_flav;
43 enum brl_type lock_type;
44 char *inbuf;
45 int length;
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 /* The event that makes us process our blocking lock queue */
55 static struct timed_event *brl_timeout;
57 /****************************************************************************
58 Destructor for the above structure.
59 ****************************************************************************/
61 static void free_blocking_lock_record(blocking_lock_record *blr)
63 SAFE_FREE(blr->inbuf);
64 SAFE_FREE(blr);
67 /****************************************************************************
68 Determine if this is a secondary element of a chained SMB.
69 **************************************************************************/
71 static BOOL in_chained_smb(void)
73 return (chain_size != 0);
76 static void received_unlock_msg(struct messaging_context *msg,
77 void *private_data,
78 uint32_t msg_type,
79 struct server_id server_id,
80 DATA_BLOB *data);
81 static void process_blocking_lock_queue(void);
83 static void brl_timeout_fn(struct event_context *event_ctx,
84 struct timed_event *te,
85 const struct timeval *now,
86 void *private_data)
88 SMB_ASSERT(brl_timeout == te);
89 TALLOC_FREE(brl_timeout);
91 change_to_root_user(); /* TODO: Possibly run all timed events as
92 * root */
94 process_blocking_lock_queue();
97 /****************************************************************************
98 After a change to blocking_lock_queue, recalculate the timed_event for the
99 next processing.
100 ****************************************************************************/
102 static BOOL recalc_brl_timeout(void)
104 blocking_lock_record *brl;
105 struct timeval next_timeout;
107 TALLOC_FREE(brl_timeout);
109 next_timeout = timeval_zero();
111 for (brl = blocking_lock_queue; brl; brl = brl->next) {
112 if (timeval_is_zero(&brl->expire_time)) {
114 * If we're blocked on pid 0xFFFFFFFF this is
115 * a POSIX lock, so calculate a timeout of
116 * 10 seconds into the future.
118 if (brl->blocking_pid == 0xFFFFFFFF) {
119 struct timeval psx_to = timeval_current_ofs(10, 0);
120 next_timeout = timeval_min(&next_timeout, &psx_to);
123 continue;
126 if (timeval_is_zero(&next_timeout)) {
127 next_timeout = brl->expire_time;
129 else {
130 next_timeout = timeval_min(&next_timeout,
131 &brl->expire_time);
135 if (timeval_is_zero(&next_timeout)) {
136 return True;
139 if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
140 next_timeout, "brl_timeout",
141 brl_timeout_fn, NULL))) {
142 return False;
145 return True;
149 /****************************************************************************
150 Function to push a blocking lock request onto the lock queue.
151 ****************************************************************************/
153 BOOL push_blocking_lock_request( struct byte_range_lock *br_lck,
154 char *inbuf, int length,
155 files_struct *fsp,
156 int lock_timeout,
157 int lock_num,
158 uint32 lock_pid,
159 enum brl_type lock_type,
160 enum brl_flavour lock_flav,
161 SMB_BIG_UINT offset,
162 SMB_BIG_UINT count,
163 uint32 blocking_pid)
165 static BOOL set_lock_msg;
166 blocking_lock_record *blr;
167 NTSTATUS status;
169 if(in_chained_smb() ) {
170 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
171 return False;
175 * Now queue an entry on the blocking lock queue. We setup
176 * the expiration time here.
179 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
180 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
181 return False;
184 blr->next = NULL;
185 blr->prev = NULL;
187 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
188 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
189 SAFE_FREE(blr);
190 return False;
193 blr->com_type = CVAL(inbuf,smb_com);
194 blr->fsp = fsp;
195 if (lock_timeout == -1) {
196 blr->expire_time.tv_sec = 0;
197 blr->expire_time.tv_usec = 0; /* Never expire. */
198 } else {
199 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
200 (lock_timeout % 1000) * 1000);
202 blr->lock_num = lock_num;
203 blr->lock_pid = lock_pid;
204 blr->blocking_pid = blocking_pid;
205 blr->lock_flav = lock_flav;
206 blr->lock_type = lock_type;
207 blr->offset = offset;
208 blr->count = count;
209 memcpy(blr->inbuf, inbuf, length);
210 blr->length = length;
212 /* Add a pending lock record for this. */
213 status = brl_lock(smbd_messaging_context(), br_lck,
214 lock_pid,
215 procid_self(),
216 offset,
217 count,
218 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
219 blr->lock_flav,
220 lock_timeout ? True : False, /* blocking_lock. */
221 NULL);
223 if (!NT_STATUS_IS_OK(status)) {
224 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
225 DLIST_REMOVE(blocking_lock_queue, blr);
226 free_blocking_lock_record(blr);
227 return False;
230 DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
231 recalc_brl_timeout();
233 /* Ensure we'll receive messages when this is unlocked. */
234 if (!set_lock_msg) {
235 messaging_register(smbd_messaging_context(), NULL,
236 MSG_SMB_UNLOCK, received_unlock_msg);
237 set_lock_msg = True;
240 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with "
241 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
242 length, (unsigned int)blr->expire_time.tv_sec,
243 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
244 blr->fsp->fnum, blr->fsp->fsp_name ));
246 /* Push the MID of this packet on the signing queue. */
247 srv_defer_sign_response(SVAL(inbuf,smb_mid));
249 return True;
252 /****************************************************************************
253 Return a smd with a given size.
254 *****************************************************************************/
256 static void send_blocking_reply(char *outbuf, int outsize, const char *inbuf)
258 if(outsize > 4) {
259 smb_setlen(inbuf, outbuf,outsize - 4);
262 if (!send_smb(smbd_server_fd(),outbuf)) {
263 exit_server_cleanly("send_blocking_reply: send_smb failed.");
267 /****************************************************************************
268 Return a lockingX success SMB.
269 *****************************************************************************/
271 static void reply_lockingX_success(blocking_lock_record *blr)
273 char *outbuf = get_OutBuffer();
274 int bufsize = BUFFER_SIZE;
275 char *inbuf = blr->inbuf;
276 int outsize = 0;
278 construct_reply_common(inbuf, outbuf);
279 set_message(inbuf,outbuf,2,0,True);
282 * As this message is a lockingX call we must handle
283 * any following chained message correctly.
284 * This is normally handled in construct_reply(),
285 * but as that calls switch_message, we can't use
286 * that here and must set up the chain info manually.
289 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
291 outsize += chain_size;
293 send_blocking_reply(outbuf,outsize,inbuf);
296 /****************************************************************************
297 Return a generic lock fail error blocking call.
298 *****************************************************************************/
300 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
302 char *outbuf = get_OutBuffer();
303 char *inbuf = blr->inbuf;
304 construct_reply_common(inbuf, outbuf);
306 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
307 FILE_LOCK_CONFLICT! (tridge) */
308 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
309 status = NT_STATUS_FILE_LOCK_CONFLICT;
312 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
313 /* Store the last lock error. */
314 files_struct *fsp = blr->fsp;
316 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
317 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
318 fsp->last_lock_failure.context.pid = procid_self();
319 fsp->last_lock_failure.start = blr->offset;
320 fsp->last_lock_failure.size = blr->count;
321 fsp->last_lock_failure.fnum = fsp->fnum;
322 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
323 fsp->last_lock_failure.lock_flav = blr->lock_flav;
326 ERROR_NT(status);
327 if (!send_smb(smbd_server_fd(),outbuf)) {
328 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
332 /****************************************************************************
333 Return a lock fail error for a lockingX call. Undo all the locks we have
334 obtained first.
335 *****************************************************************************/
337 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
339 char *inbuf = blr->inbuf;
340 files_struct *fsp = blr->fsp;
341 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
342 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
343 uint32 lock_pid;
344 unsigned char locktype = CVAL(inbuf,smb_vwv3);
345 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
346 char *data;
347 int i;
349 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
352 * Data now points at the beginning of the list
353 * of smb_lkrng structs.
357 * Ensure we don't do a remove on the lock that just failed,
358 * as under POSIX rules, if we have a lock already there, we
359 * will delete it (and we shouldn't) .....
362 for(i = blr->lock_num - 1; i >= 0; i--) {
363 BOOL err;
365 lock_pid = get_lock_pid( data, i, large_file_format);
366 count = get_lock_count( data, i, large_file_format);
367 offset = get_lock_offset( data, i, large_file_format, &err);
370 * We know err cannot be set as if it was the lock
371 * request would never have been queued. JRA.
374 do_unlock(smbd_messaging_context(),
375 fsp,
376 lock_pid,
377 count,
378 offset,
379 WINDOWS_LOCK);
382 generic_blocking_lock_error(blr, status);
385 /****************************************************************************
386 Return a lock fail error.
387 *****************************************************************************/
389 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
391 switch(blr->com_type) {
392 case SMBlockingX:
393 reply_lockingX_error(blr, status);
394 break;
395 case SMBtrans2:
396 case SMBtranss2:
398 char *outbuf = get_OutBuffer();
399 char *inbuf = blr->inbuf;
400 construct_reply_common(inbuf, outbuf);
401 /* construct_reply_common has done us the favor to pre-fill the
402 * command field with SMBtranss2 which is wrong :-)
404 SCVAL(outbuf,smb_com,SMBtrans2);
405 ERROR_NT(status);
406 if (!send_smb(smbd_server_fd(),outbuf)) {
407 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
409 break;
411 default:
412 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
413 exit_server("PANIC - unknown type on blocking lock queue");
417 /****************************************************************************
418 Attempt to finish off getting all pending blocking locks for a lockingX call.
419 Returns True if we want to be removed from the list.
420 *****************************************************************************/
422 static BOOL process_lockingX(blocking_lock_record *blr)
424 char *inbuf = blr->inbuf;
425 unsigned char locktype = CVAL(inbuf,smb_vwv3);
426 files_struct *fsp = blr->fsp;
427 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
428 uint16 num_locks = SVAL(inbuf,smb_vwv7);
429 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
430 uint32 lock_pid;
431 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
432 char *data;
433 NTSTATUS status = NT_STATUS_OK;
435 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
438 * Data now points at the beginning of the list
439 * of smb_lkrng structs.
442 for(; blr->lock_num < num_locks; blr->lock_num++) {
443 struct byte_range_lock *br_lck = NULL;
444 BOOL err;
446 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
447 count = get_lock_count( data, blr->lock_num, large_file_format);
448 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
451 * We know err cannot be set as if it was the lock
452 * request would never have been queued. JRA.
454 errno = 0;
455 br_lck = do_lock(smbd_messaging_context(),
456 fsp,
457 lock_pid,
458 count,
459 offset,
460 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
461 READ_LOCK : WRITE_LOCK),
462 WINDOWS_LOCK,
463 True,
464 &status,
465 &blr->blocking_pid);
467 TALLOC_FREE(br_lck);
469 if (NT_STATUS_IS_ERR(status)) {
470 break;
474 if(blr->lock_num == num_locks) {
476 * Success - we got all the locks.
479 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
480 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
482 reply_lockingX_success(blr);
483 return True;
484 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
485 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
487 * We have other than a "can't get lock"
488 * error. Free any locks we had and return an error.
489 * Return True so we get dequeued.
492 blocking_lock_reply_error(blr, status);
493 return True;
497 * Still can't get all the locks - keep waiting.
500 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
501 Waiting....\n",
502 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
504 return False;
507 /****************************************************************************
508 Attempt to get the posix lock request from a SMBtrans2 call.
509 Returns True if we want to be removed from the list.
510 *****************************************************************************/
512 static BOOL process_trans2(blocking_lock_record *blr)
514 char *inbuf = blr->inbuf;
515 char *outbuf;
516 char params[2];
517 NTSTATUS status;
518 struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
519 blr->fsp,
520 blr->lock_pid,
521 blr->count,
522 blr->offset,
523 blr->lock_type,
524 blr->lock_flav,
525 True,
526 &status,
527 &blr->blocking_pid);
528 TALLOC_FREE(br_lck);
530 if (!NT_STATUS_IS_OK(status)) {
531 if (ERROR_WAS_LOCK_DENIED(status)) {
532 /* Still can't get the lock, just keep waiting. */
533 return False;
536 * We have other than a "can't get lock"
537 * error. Send an error and return True so we get dequeued.
539 blocking_lock_reply_error(blr, status);
540 return True;
543 /* We finally got the lock, return success. */
544 outbuf = get_OutBuffer();
545 construct_reply_common(inbuf, outbuf);
546 SCVAL(outbuf,smb_com,SMBtrans2);
547 SSVAL(params,0,0);
548 /* Fake up max_data_bytes here - we know it fits. */
549 send_trans2_replies(inbuf, outbuf, max_send, params, 2, NULL, 0, 0xffff);
550 return True;
554 /****************************************************************************
555 Process a blocking lock SMB.
556 Returns True if we want to be removed from the list.
557 *****************************************************************************/
559 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
561 switch(blr->com_type) {
562 case SMBlockingX:
563 return process_lockingX(blr);
564 case SMBtrans2:
565 case SMBtranss2:
566 return process_trans2(blr);
567 default:
568 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
569 exit_server("PANIC - unknown type on blocking lock queue");
571 return False; /* Keep compiler happy. */
574 /****************************************************************************
575 Cancel entries by fnum from the blocking lock pending queue.
576 *****************************************************************************/
578 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
580 blocking_lock_record *blr, *next = NULL;
582 for(blr = blocking_lock_queue; blr; blr = next) {
583 next = blr->next;
584 if(blr->fsp->fnum == fsp->fnum) {
585 unsigned char locktype = 0;
587 if (blr->com_type == SMBlockingX) {
588 locktype = CVAL(blr->inbuf,smb_vwv3);
591 if (br_lck) {
592 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
593 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
595 brl_lock_cancel(br_lck,
596 blr->lock_pid,
597 procid_self(),
598 blr->offset,
599 blr->count,
600 blr->lock_flav);
602 blocking_lock_cancel(fsp,
603 blr->lock_pid,
604 blr->offset,
605 blr->count,
606 blr->lock_flav,
607 locktype,
608 NT_STATUS_RANGE_NOT_LOCKED);
614 /****************************************************************************
615 Delete entries by mid from the blocking lock pending queue. Always send reply.
616 *****************************************************************************/
618 void remove_pending_lock_requests_by_mid(int mid)
620 blocking_lock_record *blr, *next = NULL;
622 for(blr = blocking_lock_queue; blr; blr = next) {
623 next = blr->next;
624 if(SVAL(blr->inbuf,smb_mid) == mid) {
625 files_struct *fsp = blr->fsp;
626 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
628 if (br_lck) {
629 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
630 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
632 brl_lock_cancel(br_lck,
633 blr->lock_pid,
634 procid_self(),
635 blr->offset,
636 blr->count,
637 blr->lock_flav);
638 TALLOC_FREE(br_lck);
641 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
642 DLIST_REMOVE(blocking_lock_queue, blr);
643 free_blocking_lock_record(blr);
648 /****************************************************************************
649 Is this mid a blocking lock request on the queue ?
650 *****************************************************************************/
652 BOOL blocking_lock_was_deferred(int mid)
654 blocking_lock_record *blr, *next = NULL;
656 for(blr = blocking_lock_queue; blr; blr = next) {
657 next = blr->next;
658 if(SVAL(blr->inbuf,smb_mid) == mid) {
659 return True;
662 return False;
665 /****************************************************************************
666 Set a flag as an unlock request affects one of our pending locks.
667 *****************************************************************************/
669 static void received_unlock_msg(struct messaging_context *msg,
670 void *private_data,
671 uint32_t msg_type,
672 struct server_id server_id,
673 DATA_BLOB *data)
675 DEBUG(10,("received_unlock_msg\n"));
676 process_blocking_lock_queue();
679 /****************************************************************************
680 Process the blocking lock queue. Note that this is only called as root.
681 *****************************************************************************/
683 static void process_blocking_lock_queue(void)
685 struct timeval tv_curr = timeval_current();
686 blocking_lock_record *blr, *next = NULL;
687 BOOL recalc_timeout = False;
690 * Go through the queue and see if we can get any of the locks.
693 for (blr = blocking_lock_queue; blr; blr = next) {
694 connection_struct *conn = NULL;
695 uint16 vuid;
696 files_struct *fsp = NULL;
698 next = blr->next;
701 * Ensure we don't have any old chain_fsp values
702 * sitting around....
704 chain_size = 0;
705 file_chain_reset();
706 fsp = blr->fsp;
708 conn = conn_find(SVAL(blr->inbuf,smb_tid));
709 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
710 SVAL(blr->inbuf,smb_uid);
712 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
713 fsp->fnum, fsp->fsp_name ));
715 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
716 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
719 * Lock expired - throw away all previously
720 * obtained locks and return lock error.
723 if (br_lck) {
724 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
725 fsp->fnum, fsp->fsp_name ));
727 brl_lock_cancel(br_lck,
728 blr->lock_pid,
729 procid_self(),
730 blr->offset,
731 blr->count,
732 blr->lock_flav);
733 TALLOC_FREE(br_lck);
736 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
737 DLIST_REMOVE(blocking_lock_queue, blr);
738 free_blocking_lock_record(blr);
739 recalc_timeout = True;
740 continue;
743 if(!change_to_user(conn,vuid)) {
744 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
747 * Remove the entry and return an error to the client.
750 if (br_lck) {
751 brl_lock_cancel(br_lck,
752 blr->lock_pid,
753 procid_self(),
754 blr->offset,
755 blr->count,
756 blr->lock_flav);
757 TALLOC_FREE(br_lck);
760 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
761 vuid ));
762 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
763 DLIST_REMOVE(blocking_lock_queue, blr);
764 free_blocking_lock_record(blr);
765 recalc_timeout = True;
766 continue;
769 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
770 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
773 * Remove the entry and return an error to the client.
776 if (br_lck) {
777 brl_lock_cancel(br_lck,
778 blr->lock_pid,
779 procid_self(),
780 blr->offset,
781 blr->count,
782 blr->lock_flav);
783 TALLOC_FREE(br_lck);
786 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
787 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
788 DLIST_REMOVE(blocking_lock_queue, blr);
789 free_blocking_lock_record(blr);
790 recalc_timeout = True;
791 change_to_root_user();
792 continue;
796 * Go through the remaining locks and try and obtain them.
797 * The call returns True if all locks were obtained successfully
798 * and False if we still need to wait.
801 if(blocking_lock_record_process(blr)) {
802 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
804 if (br_lck) {
805 brl_lock_cancel(br_lck,
806 blr->lock_pid,
807 procid_self(),
808 blr->offset,
809 blr->count,
810 blr->lock_flav);
811 TALLOC_FREE(br_lck);
814 DLIST_REMOVE(blocking_lock_queue, blr);
815 free_blocking_lock_record(blr);
816 recalc_timeout = True;
818 change_to_root_user();
821 if (recalc_timeout) {
822 recalc_brl_timeout();
826 /****************************************************************************
827 Handle a cancel message. Lock already moved onto the cancel queue.
828 *****************************************************************************/
830 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
832 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
833 void *private_data,
834 uint32_t msg_type,
835 struct server_id server_id,
836 DATA_BLOB *data)
838 NTSTATUS err;
839 const char *msg = (const char *)data->data;
840 blocking_lock_record *blr;
842 if (data->data == NULL) {
843 smb_panic("process_blocking_lock_cancel_message: null msg");
846 if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
847 DEBUG(0, ("process_blocking_lock_cancel_message: "
848 "Got invalid msg len %d\n", (int)data->length));
849 smb_panic("process_blocking_lock_cancel_message: bad msg");
852 memcpy(&blr, msg, sizeof(blr));
853 memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
855 DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
856 nt_errstr(err) ));
858 blocking_lock_reply_error(blr, err);
859 DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
860 free_blocking_lock_record(blr);
863 /****************************************************************************
864 Send ourselves a blocking lock cancelled message. Handled asynchronously above.
865 *****************************************************************************/
867 BOOL blocking_lock_cancel(files_struct *fsp,
868 uint32 lock_pid,
869 SMB_BIG_UINT offset,
870 SMB_BIG_UINT count,
871 enum brl_flavour lock_flav,
872 unsigned char locktype,
873 NTSTATUS err)
875 static BOOL initialized;
876 char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
877 blocking_lock_record *blr;
879 if (!initialized) {
880 /* Register our message. */
881 messaging_register(smbd_messaging_context(), NULL,
882 MSG_SMB_BLOCKING_LOCK_CANCEL,
883 process_blocking_lock_cancel_message);
885 initialized = True;
888 for (blr = blocking_lock_queue; blr; blr = blr->next) {
889 if (fsp == blr->fsp &&
890 lock_pid == blr->lock_pid &&
891 offset == blr->offset &&
892 count == blr->count &&
893 lock_flav == blr->lock_flav) {
894 break;
898 if (!blr) {
899 return False;
902 /* Check the flags are right. */
903 if (blr->com_type == SMBlockingX &&
904 (locktype & LOCKING_ANDX_LARGE_FILES) !=
905 (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
906 return False;
909 /* Move to cancelled queue. */
910 DLIST_REMOVE(blocking_lock_queue, blr);
911 DLIST_ADD(blocking_lock_cancelled_queue, blr);
913 /* Create the message. */
914 memcpy(msg, &blr, sizeof(blr));
915 memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
917 messaging_send_buf(smbd_messaging_context(), procid_self(),
918 MSG_SMB_BLOCKING_LOCK_CANCEL,
919 (uint8 *)&msg, sizeof(msg));
921 return True;