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 /****************************************************************************
24 This is the structure to queue to implement blocking locks.
25 notify. It consists of the requesting SMB and the expiry time.
26 *****************************************************************************/
28 typedef struct _blocking_lock_record {
29 struct _blocking_lock_record *next;
30 struct _blocking_lock_record *prev;
40 } blocking_lock_record;
42 static blocking_lock_record *blocking_lock_queue;
44 /****************************************************************************
45 Destructor for the above structure.
46 ****************************************************************************/
48 static void free_blocking_lock_record(blocking_lock_record *blr)
50 DLIST_REMOVE(blocking_lock_queue, blr);
51 SAFE_FREE(blr->inbuf);
55 /****************************************************************************
56 Get the files_struct given a particular queued SMB.
57 *****************************************************************************/
59 static files_struct *get_fsp_from_pkt(char *inbuf)
61 switch(CVAL(inbuf,smb_com)) {
64 return file_fsp(inbuf,smb_vwv0);
66 return file_fsp(inbuf,smb_vwv2);
68 DEBUG(0,("get_fsp_from_pkt: PANIC - unknown type on blocking lock queue - exiting.!\n"));
69 exit_server("PANIC - unknown type on blocking lock queue");
71 return NULL; /* Keep compiler happy. */
74 /****************************************************************************
75 Determine if this is a secondary element of a chained SMB.
76 **************************************************************************/
78 static BOOL in_chained_smb(void)
80 return (chain_size != 0);
83 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len);
85 /****************************************************************************
86 Function to push a blocking lock request onto the lock queue.
87 ****************************************************************************/
89 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout,
90 int lock_num, uint16 lock_pid, SMB_BIG_UINT offset, SMB_BIG_UINT count)
92 static BOOL set_lock_msg;
93 blocking_lock_record *blr, *tmp;
94 BOOL my_lock_ctx = False;
97 if(in_chained_smb() ) {
98 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
103 * Now queue an entry on the blocking lock queue. We setup
104 * the expiration time here.
107 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
108 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
112 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
113 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
118 blr->com_type = CVAL(inbuf,smb_com);
119 blr->fsp = get_fsp_from_pkt(inbuf);
120 blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
121 blr->lock_num = lock_num;
122 blr->lock_pid = lock_pid;
123 blr->offset = offset;
125 memcpy(blr->inbuf, inbuf, length);
126 blr->length = length;
128 /* Add a pending lock record for this. */
129 status = brl_lock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
130 lock_pid, sys_getpid(), blr->fsp->conn->cnum,
131 offset, count, PENDING_LOCK, &my_lock_ctx);
133 if (!NT_STATUS_IS_OK(status)) {
134 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
135 free_blocking_lock_record(blr);
139 DLIST_ADD_END(blocking_lock_queue, blr, tmp);
141 /* Ensure we'll receive messages when this is unlocked. */
143 message_register(MSG_SMB_UNLOCK, received_unlock_msg);
147 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
148 for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
149 blr->fsp->fnum, blr->fsp->fsp_name ));
151 /* Push the MID of this packet on the signing queue. */
152 srv_defer_sign_response(SVAL(inbuf,smb_mid));
157 /****************************************************************************
158 Return a smd with a given size.
159 *****************************************************************************/
161 static void send_blocking_reply(char *outbuf, int outsize)
164 smb_setlen(outbuf,outsize - 4);
166 if (!send_smb(smbd_server_fd(),outbuf))
167 exit_server("send_blocking_reply: send_smb failed.");
170 /****************************************************************************
171 Return a lockingX success SMB.
172 *****************************************************************************/
174 static void reply_lockingX_success(blocking_lock_record *blr)
176 char *outbuf = get_OutBuffer();
177 int bufsize = BUFFER_SIZE;
178 char *inbuf = blr->inbuf;
181 construct_reply_common(inbuf, outbuf);
182 set_message(outbuf,2,0,True);
185 * As this message is a lockingX call we must handle
186 * any following chained message correctly.
187 * This is normally handled in construct_reply(),
188 * but as that calls switch_message, we can't use
189 * that here and must set up the chain info manually.
192 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
194 outsize += chain_size;
196 send_blocking_reply(outbuf,outsize);
199 /****************************************************************************
200 Return a generic lock fail error blocking call.
201 *****************************************************************************/
203 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
205 char *outbuf = get_OutBuffer();
206 char *inbuf = blr->inbuf;
207 construct_reply_common(inbuf, outbuf);
209 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
210 FILE_LOCK_CONFLICT! (tridge) */
211 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
212 status = NT_STATUS_FILE_LOCK_CONFLICT;
216 if (!send_smb(smbd_server_fd(),outbuf))
217 exit_server("generic_blocking_lock_error: send_smb failed.");
220 /****************************************************************************
221 Return a lock fail error for a lockingX call. Undo all the locks we have
223 *****************************************************************************/
225 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
227 char *inbuf = blr->inbuf;
228 files_struct *fsp = blr->fsp;
229 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
230 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
231 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
233 unsigned char locktype = CVAL(inbuf,smb_vwv3);
234 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
238 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
241 * Data now points at the beginning of the list
242 * of smb_lkrng structs.
246 * Ensure we don't do a remove on the lock that just failed,
247 * as under POSIX rules, if we have a lock already there, we
248 * will delete it (and we shouldn't) .....
251 for(i = blr->lock_num - 1; i >= 0; i--) {
254 lock_pid = get_lock_pid( data, i, large_file_format);
255 count = get_lock_count( data, i, large_file_format);
256 offset = get_lock_offset( data, i, large_file_format, &err);
259 * We know err cannot be set as if it was the lock
260 * request would never have been queued. JRA.
263 do_unlock(fsp,conn,lock_pid,count,offset);
266 generic_blocking_lock_error(blr, status);
269 /****************************************************************************
270 Return a lock fail error.
271 *****************************************************************************/
273 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
275 switch(blr->com_type) {
278 generic_blocking_lock_error(blr, status);
281 reply_lockingX_error(blr, status);
284 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
285 exit_server("PANIC - unknown type on blocking lock queue");
289 /****************************************************************************
290 Attempt to finish off getting all pending blocking locks for a lockread call.
291 Returns True if we want to be removed from the list.
292 *****************************************************************************/
294 static BOOL process_lockread(blocking_lock_record *blr)
296 char *outbuf = get_OutBuffer();
297 char *inbuf = blr->inbuf;
301 SMB_BIG_UINT startpos;
304 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
305 files_struct *fsp = blr->fsp;
306 BOOL my_lock_ctx = False;
308 numtoread = SVAL(inbuf,smb_vwv1);
309 startpos = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv2);
311 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
312 data = smb_buf(outbuf) + 3;
314 status = do_lock_spin( fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtoread, startpos, READ_LOCK, &my_lock_ctx);
315 if (NT_STATUS_V(status)) {
316 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
317 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
319 * We have other than a "can't get lock"
320 * error. Send an error.
321 * Return True so we get dequeued.
323 generic_blocking_lock_error(blr, status);
328 * Still waiting for lock....
331 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
336 nread = read_file(fsp,data,startpos,numtoread);
339 generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
343 construct_reply_common(inbuf, outbuf);
344 outsize = set_message(outbuf,5,0,True);
347 SSVAL(outbuf,smb_vwv0,nread);
348 SSVAL(outbuf,smb_vwv5,nread+3);
351 SSVAL(p,0,nread); p += 2;
352 set_message_end(outbuf, p+nread);
354 DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
355 fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
357 send_blocking_reply(outbuf,outsize);
361 /****************************************************************************
362 Attempt to finish off getting all pending blocking locks for a lock call.
363 Returns True if we want to be removed from the list.
364 *****************************************************************************/
366 static BOOL process_lock(blocking_lock_record *blr)
368 char *outbuf = get_OutBuffer();
369 char *inbuf = blr->inbuf;
371 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
373 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
374 files_struct *fsp = blr->fsp;
375 BOOL my_lock_ctx = False;
377 count = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
378 offset = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
381 status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK, &my_lock_ctx);
382 if (NT_STATUS_IS_ERR(status)) {
383 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
384 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
386 * We have other than a "can't get lock"
387 * error. Send an error.
388 * Return True so we get dequeued.
391 blocking_lock_reply_error(blr, status);
395 * Still can't get the lock - keep waiting.
397 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
403 * Success - we got the lock.
406 DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
407 fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
409 construct_reply_common(inbuf, outbuf);
410 outsize = set_message(outbuf,0,0,True);
411 send_blocking_reply(outbuf,outsize);
415 /****************************************************************************
416 Attempt to finish off getting all pending blocking locks for a lockingX call.
417 Returns True if we want to be removed from the list.
418 *****************************************************************************/
420 static BOOL process_lockingX(blocking_lock_record *blr)
422 char *inbuf = blr->inbuf;
423 unsigned char locktype = CVAL(inbuf,smb_vwv3);
424 files_struct *fsp = blr->fsp;
425 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
426 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
427 uint16 num_locks = SVAL(inbuf,smb_vwv7);
428 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
430 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
432 BOOL my_lock_ctx = False;
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++) {
445 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
446 count = get_lock_count( data, blr->lock_num, large_file_format);
447 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
450 * We know err cannot be set as if it was the lock
451 * request would never have been queued. JRA.
454 status = do_lock_spin(fsp,conn,lock_pid,count,offset,
455 ((locktype & 1) ? READ_LOCK : WRITE_LOCK), &my_lock_ctx);
456 if (NT_STATUS_IS_ERR(status)) break;
459 if(blr->lock_num == num_locks) {
461 * Success - we got all the locks.
464 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
465 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
467 reply_lockingX_success(blr);
469 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
470 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
472 * We have other than a "can't get lock"
473 * error. Free any locks we had and return an error.
474 * Return True so we get dequeued.
477 blocking_lock_reply_error(blr, status);
482 * Still can't get all the locks - keep waiting.
485 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
487 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
492 /****************************************************************************
493 Process a blocking lock SMB.
494 Returns True if we want to be removed from the list.
495 *****************************************************************************/
497 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
499 switch(blr->com_type) {
501 return process_lock(blr);
503 return process_lockread(blr);
505 return process_lockingX(blr);
507 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
508 exit_server("PANIC - unknown type on blocking lock queue");
510 return False; /* Keep compiler happy. */
513 /****************************************************************************
514 Delete entries by fnum from the blocking lock pending queue.
515 *****************************************************************************/
517 void remove_pending_lock_requests_by_fid(files_struct *fsp)
519 blocking_lock_record *blr, *next = NULL;
521 for(blr = blocking_lock_queue; blr; blr = next) {
523 if(blr->fsp->fnum == fsp->fnum) {
525 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
526 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
528 brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
529 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
530 blr->offset, blr->count, True, NULL, NULL);
532 free_blocking_lock_record(blr);
537 /****************************************************************************
538 Delete entries by mid from the blocking lock pending queue. Always send reply.
539 *****************************************************************************/
541 void remove_pending_lock_requests_by_mid(int mid)
543 blocking_lock_record *blr, *next = NULL;
545 for(blr = blocking_lock_queue; blr; blr = next) {
547 if(SVAL(blr->inbuf,smb_mid) == mid) {
548 files_struct *fsp = blr->fsp;
550 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
551 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
553 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
554 brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
555 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
556 blr->offset, blr->count, True, NULL, NULL);
557 free_blocking_lock_record(blr);
562 /****************************************************************************
563 Set a flag as an unlock request affects one of our pending locks.
564 *****************************************************************************/
566 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len)
568 DEBUG(10,("received_unlock_msg\n"));
569 process_blocking_lock_queue(time(NULL));
572 /****************************************************************************
573 Return the number of seconds to the next blocking locks timeout, or default_timeout
574 *****************************************************************************/
576 unsigned blocking_locks_timeout(unsigned default_timeout)
578 unsigned timeout = default_timeout;
580 blocking_lock_record *blr = blocking_lock_queue;
582 /* note that we avoid the time() syscall if there are no blocking locks */
588 for (; blr; blr = blr->next) {
589 if ((blr->expire_time != (time_t)-1) &&
590 (timeout > (blr->expire_time - t))) {
591 timeout = blr->expire_time - t;
601 /****************************************************************************
602 Process the blocking lock queue. Note that this is only called as root.
603 *****************************************************************************/
605 void process_blocking_lock_queue(time_t t)
607 blocking_lock_record *blr, *next = NULL;
610 * Go through the queue and see if we can get any of the locks.
613 for (blr = blocking_lock_queue; blr; blr = next) {
614 connection_struct *conn = NULL;
616 files_struct *fsp = NULL;
621 * Ensure we don't have any old chain_fsp values
628 conn = conn_find(SVAL(blr->inbuf,smb_tid));
629 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
630 SVAL(blr->inbuf,smb_uid);
632 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
633 fsp->fnum, fsp->fsp_name ));
635 if((blr->expire_time != -1) && (blr->expire_time <= t)) {
637 * Lock expired - throw away all previously
638 * obtained locks and return lock error.
640 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
641 fsp->fnum, fsp->fsp_name ));
643 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
644 blr->lock_pid, sys_getpid(), conn->cnum,
645 blr->offset, blr->count, True, NULL, NULL);
647 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
648 free_blocking_lock_record(blr);
652 if(!change_to_user(conn,vuid)) {
653 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
656 * Remove the entry and return an error to the client.
658 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
660 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
661 blr->lock_pid, sys_getpid(), conn->cnum,
662 blr->offset, blr->count, True, NULL, NULL);
664 free_blocking_lock_record(blr);
668 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
669 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
671 * Remove the entry and return an error to the client.
673 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
675 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
676 blr->lock_pid, sys_getpid(), conn->cnum,
677 blr->offset, blr->count, True, NULL, NULL);
679 free_blocking_lock_record(blr);
680 change_to_root_user();
685 * Go through the remaining locks and try and obtain them.
686 * The call returns True if all locks were obtained successfully
687 * and False if we still need to wait.
690 if(blocking_lock_record_process(blr)) {
692 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
693 blr->lock_pid, sys_getpid(), conn->cnum,
694 blr->offset, blr->count, True, NULL, NULL);
696 free_blocking_lock_record(blr);
698 change_to_root_user();