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 extern char *OutBuffer
;
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 *****************************************************************************/
41 } blocking_lock_record
;
43 static ubi_slList blocking_lock_queue
= { NULL
, (ubi_slNodePtr
)&blocking_lock_queue
, 0};
45 /****************************************************************************
46 Destructor for the above structure.
47 ****************************************************************************/
49 static void free_blocking_lock_record(blocking_lock_record
*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
;
96 if(in_chained_smb() ) {
97 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
102 * Now queue an entry on the blocking lock queue. We setup
103 * the expiration time here.
106 if((blr
= (blocking_lock_record
*)malloc(sizeof(blocking_lock_record
))) == NULL
) {
107 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
111 if((blr
->inbuf
= (char *)malloc(length
)) == NULL
) {
112 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
117 blr
->com_type
= CVAL(inbuf
,smb_com
);
118 blr
->fsp
= get_fsp_from_pkt(inbuf
);
119 blr
->expire_time
= (lock_timeout
== -1) ? (time_t)-1 : time(NULL
) + (time_t)lock_timeout
;
120 blr
->lock_num
= lock_num
;
121 blr
->lock_pid
= lock_pid
;
122 blr
->offset
= offset
;
124 memcpy(blr
->inbuf
, inbuf
, length
);
125 blr
->length
= length
;
127 /* Add a pending lock record for this. */
128 status
= brl_lock(blr
->fsp
->dev
, blr
->fsp
->inode
, blr
->fsp
->fnum
,
129 lock_pid
, sys_getpid(), blr
->fsp
->conn
->cnum
,
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 ubi_slAddTail(&blocking_lock_queue
, blr
);
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
));
154 /****************************************************************************
155 Return a smd with a given size.
156 *****************************************************************************/
158 static void send_blocking_reply(char *outbuf
, int outsize
)
161 smb_setlen(outbuf
,outsize
- 4);
163 if (!send_smb(smbd_server_fd(),outbuf
))
164 exit_server("send_blocking_reply: send_smb failed.");
167 /****************************************************************************
168 Return a lockingX success SMB.
169 *****************************************************************************/
171 static void reply_lockingX_success(blocking_lock_record
*blr
)
173 char *outbuf
= OutBuffer
;
174 int bufsize
= BUFFER_SIZE
;
175 char *inbuf
= blr
->inbuf
;
178 construct_reply_common(inbuf
, outbuf
);
179 set_message(outbuf
,2,0,True
);
182 * As this message is a lockingX call we must handle
183 * any following chained message correctly.
184 * This is normally handled in construct_reply(),
185 * but as that calls switch_message, we can't use
186 * that here and must set up the chain info manually.
189 outsize
= chain_reply(inbuf
,outbuf
,blr
->length
,bufsize
);
191 outsize
+= chain_size
;
193 send_blocking_reply(outbuf
,outsize
);
196 /****************************************************************************
197 Return a generic lock fail error blocking call.
198 *****************************************************************************/
200 static void generic_blocking_lock_error(blocking_lock_record
*blr
, NTSTATUS status
)
202 char *outbuf
= OutBuffer
;
203 char *inbuf
= blr
->inbuf
;
204 construct_reply_common(inbuf
, outbuf
);
206 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
207 FILE_LOCK_CONFLICT! (tridge) */
208 if (NT_STATUS_EQUAL(status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
209 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
213 if (!send_smb(smbd_server_fd(),outbuf
))
214 exit_server("generic_blocking_lock_error: send_smb failed.");
217 /****************************************************************************
218 Return a lock fail error for a lockingX call. Undo all the locks we have
220 *****************************************************************************/
222 static void reply_lockingX_error(blocking_lock_record
*blr
, NTSTATUS status
)
224 char *inbuf
= blr
->inbuf
;
225 files_struct
*fsp
= blr
->fsp
;
226 connection_struct
*conn
= conn_find(SVAL(inbuf
,smb_tid
));
227 uint16 num_ulocks
= SVAL(inbuf
,smb_vwv6
);
228 SMB_BIG_UINT count
= (SMB_BIG_UINT
)0, offset
= (SMB_BIG_UINT
) 0;
230 unsigned char locktype
= CVAL(inbuf
,smb_vwv3
);
231 BOOL large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
);
235 data
= smb_buf(inbuf
) + ((large_file_format
? 20 : 10)*num_ulocks
);
238 * Data now points at the beginning of the list
239 * of smb_lkrng structs.
243 * Ensure we don't do a remove on the lock that just failed,
244 * as under POSIX rules, if we have a lock already there, we
245 * will delete it (and we shouldn't) .....
248 for(i
= blr
->lock_num
- 1; i
>= 0; i
--) {
251 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
252 count
= get_lock_count( data
, i
, large_file_format
);
253 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
256 * We know err cannot be set as if it was the lock
257 * request would never have been queued. JRA.
260 do_unlock(fsp
,conn
,lock_pid
,count
,offset
);
263 generic_blocking_lock_error(blr
, status
);
266 /****************************************************************************
267 Return a lock fail error.
268 *****************************************************************************/
270 static void blocking_lock_reply_error(blocking_lock_record
*blr
, NTSTATUS status
)
272 switch(blr
->com_type
) {
275 generic_blocking_lock_error(blr
, status
);
278 reply_lockingX_error(blr
, status
);
281 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
282 exit_server("PANIC - unknown type on blocking lock queue");
286 /****************************************************************************
287 Attempt to finish off getting all pending blocking locks for a lockread call.
288 Returns True if we want to be removed from the list.
289 *****************************************************************************/
291 static BOOL
process_lockread(blocking_lock_record
*blr
)
293 char *outbuf
= OutBuffer
;
294 char *inbuf
= blr
->inbuf
;
298 SMB_BIG_UINT startpos
;
301 connection_struct
*conn
= conn_find(SVAL(inbuf
,smb_tid
));
302 files_struct
*fsp
= blr
->fsp
;
304 numtoread
= SVAL(inbuf
,smb_vwv1
);
305 startpos
= (SMB_BIG_UINT
)IVAL(inbuf
,smb_vwv2
);
307 numtoread
= MIN(BUFFER_SIZE
-outsize
,numtoread
);
308 data
= smb_buf(outbuf
) + 3;
310 status
= do_lock_spin( fsp
, conn
, SVAL(inbuf
,smb_pid
), (SMB_BIG_UINT
)numtoread
,
311 startpos
, READ_LOCK
);
312 if (NT_STATUS_V(status
)) {
313 if (!NT_STATUS_EQUAL(status
,NT_STATUS_LOCK_NOT_GRANTED
) &&
314 !NT_STATUS_EQUAL(status
,NT_STATUS_FILE_LOCK_CONFLICT
)) {
316 * We have other than a "can't get lock"
317 * error. Send an error.
318 * Return True so we get dequeued.
320 generic_blocking_lock_error(blr
, status
);
325 * Still waiting for lock....
328 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
333 nread
= read_file(fsp
,data
,startpos
,numtoread
);
336 generic_blocking_lock_error(blr
,NT_STATUS_ACCESS_DENIED
);
340 construct_reply_common(inbuf
, outbuf
);
341 outsize
= set_message(outbuf
,5,0,True
);
344 SSVAL(outbuf
,smb_vwv0
,nread
);
345 SSVAL(outbuf
,smb_vwv5
,nread
+3);
348 SSVAL(p
,0,nread
); p
+= 2;
349 set_message_end(outbuf
, p
+nread
);
351 DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
352 fsp
->fsp_name
, fsp
->fnum
, (int)numtoread
, (int)nread
) );
354 send_blocking_reply(outbuf
,outsize
);
358 /****************************************************************************
359 Attempt to finish off getting all pending blocking locks for a lock call.
360 Returns True if we want to be removed from the list.
361 *****************************************************************************/
363 static BOOL
process_lock(blocking_lock_record
*blr
)
365 char *outbuf
= OutBuffer
;
366 char *inbuf
= blr
->inbuf
;
368 SMB_BIG_UINT count
= (SMB_BIG_UINT
)0, offset
= (SMB_BIG_UINT
)0;
370 connection_struct
*conn
= conn_find(SVAL(inbuf
,smb_tid
));
371 files_struct
*fsp
= blr
->fsp
;
373 count
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv1
);
374 offset
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv3
);
377 status
= do_lock_spin(fsp
, conn
, SVAL(inbuf
,smb_pid
), count
,
379 if (NT_STATUS_IS_ERR(status
)) {
380 if (!NT_STATUS_EQUAL(status
,NT_STATUS_LOCK_NOT_GRANTED
) &&
381 !NT_STATUS_EQUAL(status
,NT_STATUS_FILE_LOCK_CONFLICT
)) {
383 * We have other than a "can't get lock"
384 * error. Send an error.
385 * Return True so we get dequeued.
388 blocking_lock_reply_error(blr
, status
);
392 * Still can't get the lock - keep waiting.
394 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
400 * Success - we got the lock.
403 DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
404 fsp
->fsp_name
, fsp
->fnum
, (double)offset
, (double)count
));
406 construct_reply_common(inbuf
, outbuf
);
407 outsize
= set_message(outbuf
,0,0,True
);
408 send_blocking_reply(outbuf
,outsize
);
412 /****************************************************************************
413 Attempt to finish off getting all pending blocking locks for a lockingX call.
414 Returns True if we want to be removed from the list.
415 *****************************************************************************/
417 static BOOL
process_lockingX(blocking_lock_record
*blr
)
419 char *inbuf
= blr
->inbuf
;
420 unsigned char locktype
= CVAL(inbuf
,smb_vwv3
);
421 files_struct
*fsp
= blr
->fsp
;
422 connection_struct
*conn
= conn_find(SVAL(inbuf
,smb_tid
));
423 uint16 num_ulocks
= SVAL(inbuf
,smb_vwv6
);
424 uint16 num_locks
= SVAL(inbuf
,smb_vwv7
);
425 SMB_BIG_UINT count
= (SMB_BIG_UINT
)0, offset
= (SMB_BIG_UINT
)0;
427 BOOL large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
);
429 NTSTATUS status
= NT_STATUS_OK
;
431 data
= smb_buf(inbuf
) + ((large_file_format
? 20 : 10)*num_ulocks
);
434 * Data now points at the beginning of the list
435 * of smb_lkrng structs.
438 for(; blr
->lock_num
< num_locks
; blr
->lock_num
++) {
441 lock_pid
= get_lock_pid( data
, blr
->lock_num
, large_file_format
);
442 count
= get_lock_count( data
, blr
->lock_num
, large_file_format
);
443 offset
= get_lock_offset( data
, blr
->lock_num
, large_file_format
, &err
);
446 * We know err cannot be set as if it was the lock
447 * request would never have been queued. JRA.
450 status
= do_lock_spin(fsp
,conn
,lock_pid
,count
,offset
,
451 ((locktype
& 1) ? READ_LOCK
: WRITE_LOCK
));
452 if (NT_STATUS_IS_ERR(status
)) break;
455 if(blr
->lock_num
== num_locks
) {
457 * Success - we got all the locks.
460 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
461 fsp
->fsp_name
, fsp
->fnum
, (unsigned int)locktype
, num_locks
) );
463 reply_lockingX_success(blr
);
465 } else if (!NT_STATUS_EQUAL(status
,NT_STATUS_LOCK_NOT_GRANTED
) &&
466 !NT_STATUS_EQUAL(status
,NT_STATUS_FILE_LOCK_CONFLICT
)) {
468 * We have other than a "can't get lock"
469 * error. Free any locks we had and return an error.
470 * Return True so we get dequeued.
473 blocking_lock_reply_error(blr
, status
);
478 * Still can't get all the locks - keep waiting.
481 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
483 blr
->lock_num
, num_locks
, fsp
->fsp_name
, fsp
->fnum
));
488 /****************************************************************************
489 Process a blocking lock SMB.
490 Returns True if we want to be removed from the list.
491 *****************************************************************************/
493 static BOOL
blocking_lock_record_process(blocking_lock_record
*blr
)
495 switch(blr
->com_type
) {
497 return process_lock(blr
);
499 return process_lockread(blr
);
501 return process_lockingX(blr
);
503 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
504 exit_server("PANIC - unknown type on blocking lock queue");
506 return False
; /* Keep compiler happy. */
509 /****************************************************************************
510 Delete entries by fnum from the blocking lock pending queue.
511 *****************************************************************************/
513 void remove_pending_lock_requests_by_fid(files_struct
*fsp
)
515 blocking_lock_record
*blr
= (blocking_lock_record
*)ubi_slFirst( &blocking_lock_queue
);
516 blocking_lock_record
*prev
= NULL
;
519 if(blr
->fsp
->fnum
== fsp
->fnum
) {
521 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
522 file %s fnum = %d\n", blr
->com_type
, fsp
->fsp_name
, fsp
->fnum
));
524 brl_unlock(blr
->fsp
->dev
, blr
->fsp
->inode
, blr
->fsp
->fnum
,
525 blr
->lock_pid
, sys_getpid(), blr
->fsp
->conn
->cnum
,
526 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
528 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
529 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
534 blr
= (blocking_lock_record
*)ubi_slNext(blr
);
538 /****************************************************************************
539 Delete entries by mid from the blocking lock pending queue. Always send reply.
540 *****************************************************************************/
542 void remove_pending_lock_requests_by_mid(int mid
)
544 blocking_lock_record
*blr
= (blocking_lock_record
*)ubi_slFirst( &blocking_lock_queue
);
545 blocking_lock_record
*prev
= NULL
;
548 if(SVAL(blr
->inbuf
,smb_mid
) == mid
) {
549 files_struct
*fsp
= blr
->fsp
;
551 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
552 file %s fnum = %d\n", blr
->com_type
, fsp
->fsp_name
, fsp
->fnum
));
554 blocking_lock_reply_error(blr
,NT_STATUS_CANCELLED
);
555 brl_unlock(blr
->fsp
->dev
, blr
->fsp
->inode
, blr
->fsp
->fnum
,
556 blr
->lock_pid
, sys_getpid(), blr
->fsp
->conn
->cnum
,
557 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
558 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
559 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
564 blr
= (blocking_lock_record
*)ubi_slNext(blr
);
568 /****************************************************************************
569 Set a flag as an unlock request affects one of our pending locks.
570 *****************************************************************************/
572 static void received_unlock_msg(int msg_type
, pid_t src
, void *buf
, size_t len
)
574 DEBUG(10,("received_unlock_msg\n"));
575 process_blocking_lock_queue(time(NULL
));
578 /****************************************************************************
579 Return the number of seconds to the next blocking locks timeout, or default_timeout
580 *****************************************************************************/
582 unsigned blocking_locks_timeout(unsigned default_timeout
)
584 unsigned timeout
= default_timeout
;
586 blocking_lock_record
*blr
= (blocking_lock_record
*)ubi_slFirst(&blocking_lock_queue
);
588 /* note that we avoid the time() syscall if there are no blocking locks */
595 if ((blr
->expire_time
!= (time_t)-1) &&
596 (timeout
> (blr
->expire_time
- t
))) {
597 timeout
= blr
->expire_time
- t
;
599 blr
= (blocking_lock_record
*)ubi_slNext(blr
);
608 /****************************************************************************
609 Process the blocking lock queue. Note that this is only called as root.
610 *****************************************************************************/
612 void process_blocking_lock_queue(time_t t
)
614 blocking_lock_record
*blr
= (blocking_lock_record
*)ubi_slFirst( &blocking_lock_queue
);
615 blocking_lock_record
*prev
= NULL
;
621 * Go through the queue and see if we can get any of the locks.
625 connection_struct
*conn
= NULL
;
627 files_struct
*fsp
= NULL
;
630 * Ensure we don't have any old chain_fsp values
637 conn
= conn_find(SVAL(blr
->inbuf
,smb_tid
));
638 vuid
= (lp_security() == SEC_SHARE
) ? UID_FIELD_INVALID
:
639 SVAL(blr
->inbuf
,smb_uid
);
641 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
642 fsp
->fnum
, fsp
->fsp_name
));
644 if((blr
->expire_time
!= -1) && (blr
->expire_time
<= t
)) {
646 * Lock expired - throw away all previously
647 * obtained locks and return lock error.
649 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
650 fsp
->fnum
, fsp
->fsp_name
));
652 brl_unlock(fsp
->dev
, fsp
->inode
, fsp
->fnum
,
653 blr
->lock_pid
, sys_getpid(), conn
->cnum
,
654 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
656 blocking_lock_reply_error(blr
,NT_STATUS_FILE_LOCK_CONFLICT
);
657 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
658 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
662 if(!change_to_user(conn
,vuid
)) {
663 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
666 * Remove the entry and return an error to the client.
668 blocking_lock_reply_error(blr
,NT_STATUS_ACCESS_DENIED
);
670 brl_unlock(fsp
->dev
, fsp
->inode
, fsp
->fnum
,
671 blr
->lock_pid
, sys_getpid(), conn
->cnum
,
672 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
674 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
675 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
679 if(!set_current_service(conn
,True
)) {
680 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno
) ));
682 * Remove the entry and return an error to the client.
684 blocking_lock_reply_error(blr
,NT_STATUS_ACCESS_DENIED
);
686 brl_unlock(fsp
->dev
, fsp
->inode
, fsp
->fnum
,
687 blr
->lock_pid
, sys_getpid(), conn
->cnum
,
688 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
690 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
691 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
692 change_to_root_user();
697 * Go through the remaining locks and try and obtain them.
698 * The call returns True if all locks were obtained successfully
699 * and False if we still need to wait.
702 if(blocking_lock_record_process(blr
)) {
704 brl_unlock(fsp
->dev
, fsp
->inode
, fsp
->fnum
,
705 blr
->lock_pid
, sys_getpid(), conn
->cnum
,
706 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
708 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
709 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
710 change_to_root_user();
714 change_to_root_user();
717 * Move to the next in the list.
720 blr
= (blocking_lock_record
*)ubi_slNext(blr
);