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
,
130 offset
, count
, PENDING_LOCK
);
132 if (!NT_STATUS_IS_OK(status
)) {
133 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
134 free_blocking_lock_record(blr
);
138 ubi_slAddTail(&blocking_lock_queue
, blr
);
140 /* Ensure we'll receive messages when this is unlocked. */
142 message_register(MSG_SMB_UNLOCK
, received_unlock_msg
);
146 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
147 for fnum = %d, name = %s\n", length
, (int)blr
->expire_time
, lock_timeout
,
148 blr
->fsp
->fnum
, blr
->fsp
->fsp_name
));
150 /* Push the MID of this packet on the signing queue. */
151 srv_defer_sign_response(SVAL(inbuf
,smb_mid
), True
);
156 /****************************************************************************
157 Return a smd with a given size.
158 *****************************************************************************/
160 static void send_blocking_reply(char *outbuf
, int outsize
)
163 smb_setlen(outbuf
,outsize
- 4);
165 if (!send_smb(smbd_server_fd(),outbuf
))
166 exit_server("send_blocking_reply: send_smb failed.");
169 /****************************************************************************
170 Return a lockingX success SMB.
171 *****************************************************************************/
173 static void reply_lockingX_success(blocking_lock_record
*blr
)
175 char *outbuf
= OutBuffer
;
176 int bufsize
= BUFFER_SIZE
;
177 char *inbuf
= blr
->inbuf
;
180 construct_reply_common(inbuf
, outbuf
);
181 set_message(outbuf
,2,0,True
);
184 * As this message is a lockingX call we must handle
185 * any following chained message correctly.
186 * This is normally handled in construct_reply(),
187 * but as that calls switch_message, we can't use
188 * that here and must set up the chain info manually.
191 outsize
= chain_reply(inbuf
,outbuf
,blr
->length
,bufsize
);
193 outsize
+= chain_size
;
195 send_blocking_reply(outbuf
,outsize
);
198 /****************************************************************************
199 Return a generic lock fail error blocking call.
200 *****************************************************************************/
202 static void generic_blocking_lock_error(blocking_lock_record
*blr
, NTSTATUS status
)
204 char *outbuf
= OutBuffer
;
205 char *inbuf
= blr
->inbuf
;
206 construct_reply_common(inbuf
, outbuf
);
208 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
209 FILE_LOCK_CONFLICT! (tridge) */
210 if (NT_STATUS_EQUAL(status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
211 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
215 if (!send_smb(smbd_server_fd(),outbuf
))
216 exit_server("generic_blocking_lock_error: send_smb failed.");
219 /****************************************************************************
220 Return a lock fail error for a lockingX call. Undo all the locks we have
222 *****************************************************************************/
224 static void reply_lockingX_error(blocking_lock_record
*blr
, NTSTATUS status
)
226 char *inbuf
= blr
->inbuf
;
227 files_struct
*fsp
= blr
->fsp
;
228 connection_struct
*conn
= conn_find(SVAL(inbuf
,smb_tid
));
229 uint16 num_ulocks
= SVAL(inbuf
,smb_vwv6
);
230 SMB_BIG_UINT count
= (SMB_BIG_UINT
)0, offset
= (SMB_BIG_UINT
) 0;
232 unsigned char locktype
= CVAL(inbuf
,smb_vwv3
);
233 BOOL large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
);
237 data
= smb_buf(inbuf
) + ((large_file_format
? 20 : 10)*num_ulocks
);
240 * Data now points at the beginning of the list
241 * of smb_lkrng structs.
245 * Ensure we don't do a remove on the lock that just failed,
246 * as under POSIX rules, if we have a lock already there, we
247 * will delete it (and we shouldn't) .....
250 for(i
= blr
->lock_num
- 1; i
>= 0; i
--) {
253 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
254 count
= get_lock_count( data
, i
, large_file_format
);
255 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
258 * We know err cannot be set as if it was the lock
259 * request would never have been queued. JRA.
262 do_unlock(fsp
,conn
,lock_pid
,count
,offset
);
265 generic_blocking_lock_error(blr
, status
);
268 /****************************************************************************
269 Return a lock fail error.
270 *****************************************************************************/
272 static void blocking_lock_reply_error(blocking_lock_record
*blr
, NTSTATUS status
)
274 switch(blr
->com_type
) {
277 generic_blocking_lock_error(blr
, status
);
280 reply_lockingX_error(blr
, status
);
283 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
284 exit_server("PANIC - unknown type on blocking lock queue");
288 /****************************************************************************
289 Attempt to finish off getting all pending blocking locks for a lockread call.
290 Returns True if we want to be removed from the list.
291 *****************************************************************************/
293 static BOOL
process_lockread(blocking_lock_record
*blr
)
295 char *outbuf
= OutBuffer
;
296 char *inbuf
= blr
->inbuf
;
300 SMB_BIG_UINT startpos
;
303 connection_struct
*conn
= conn_find(SVAL(inbuf
,smb_tid
));
304 files_struct
*fsp
= blr
->fsp
;
306 numtoread
= SVAL(inbuf
,smb_vwv1
);
307 startpos
= (SMB_BIG_UINT
)IVAL(inbuf
,smb_vwv2
);
309 numtoread
= MIN(BUFFER_SIZE
-outsize
,numtoread
);
310 data
= smb_buf(outbuf
) + 3;
312 status
= do_lock_spin( fsp
, conn
, SVAL(inbuf
,smb_pid
), (SMB_BIG_UINT
)numtoread
,
313 startpos
, READ_LOCK
);
314 if (NT_STATUS_V(status
)) {
315 if (!NT_STATUS_EQUAL(status
,NT_STATUS_LOCK_NOT_GRANTED
) &&
316 !NT_STATUS_EQUAL(status
,NT_STATUS_FILE_LOCK_CONFLICT
)) {
318 * We have other than a "can't get lock"
319 * error. Send an error.
320 * Return True so we get dequeued.
322 generic_blocking_lock_error(blr
, status
);
327 * Still waiting for lock....
330 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
335 nread
= read_file(fsp
,data
,startpos
,numtoread
);
338 generic_blocking_lock_error(blr
,NT_STATUS_ACCESS_DENIED
);
342 construct_reply_common(inbuf
, outbuf
);
343 outsize
= set_message(outbuf
,5,0,True
);
346 SSVAL(outbuf
,smb_vwv0
,nread
);
347 SSVAL(outbuf
,smb_vwv5
,nread
+3);
350 SSVAL(p
,0,nread
); p
+= 2;
351 set_message_end(outbuf
, p
+nread
);
353 DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
354 fsp
->fsp_name
, fsp
->fnum
, (int)numtoread
, (int)nread
) );
356 send_blocking_reply(outbuf
,outsize
);
360 /****************************************************************************
361 Attempt to finish off getting all pending blocking locks for a lock call.
362 Returns True if we want to be removed from the list.
363 *****************************************************************************/
365 static BOOL
process_lock(blocking_lock_record
*blr
)
367 char *outbuf
= OutBuffer
;
368 char *inbuf
= blr
->inbuf
;
370 SMB_BIG_UINT count
= (SMB_BIG_UINT
)0, offset
= (SMB_BIG_UINT
)0;
372 connection_struct
*conn
= conn_find(SVAL(inbuf
,smb_tid
));
373 files_struct
*fsp
= blr
->fsp
;
375 count
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv1
);
376 offset
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv3
);
379 status
= do_lock_spin(fsp
, conn
, SVAL(inbuf
,smb_pid
), count
,
381 if (NT_STATUS_IS_ERR(status
)) {
382 if (!NT_STATUS_EQUAL(status
,NT_STATUS_LOCK_NOT_GRANTED
) &&
383 !NT_STATUS_EQUAL(status
,NT_STATUS_FILE_LOCK_CONFLICT
)) {
385 * We have other than a "can't get lock"
386 * error. Send an error.
387 * Return True so we get dequeued.
390 blocking_lock_reply_error(blr
, status
);
394 * Still can't get the lock - keep waiting.
396 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
402 * Success - we got the lock.
405 DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
406 fsp
->fsp_name
, fsp
->fnum
, (double)offset
, (double)count
));
408 construct_reply_common(inbuf
, outbuf
);
409 outsize
= set_message(outbuf
,0,0,True
);
410 send_blocking_reply(outbuf
,outsize
);
414 /****************************************************************************
415 Attempt to finish off getting all pending blocking locks for a lockingX call.
416 Returns True if we want to be removed from the list.
417 *****************************************************************************/
419 static BOOL
process_lockingX(blocking_lock_record
*blr
)
421 char *inbuf
= blr
->inbuf
;
422 unsigned char locktype
= CVAL(inbuf
,smb_vwv3
);
423 files_struct
*fsp
= blr
->fsp
;
424 connection_struct
*conn
= conn_find(SVAL(inbuf
,smb_tid
));
425 uint16 num_ulocks
= SVAL(inbuf
,smb_vwv6
);
426 uint16 num_locks
= SVAL(inbuf
,smb_vwv7
);
427 SMB_BIG_UINT count
= (SMB_BIG_UINT
)0, offset
= (SMB_BIG_UINT
)0;
429 BOOL large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
);
431 NTSTATUS status
= NT_STATUS_OK
;
433 data
= smb_buf(inbuf
) + ((large_file_format
? 20 : 10)*num_ulocks
);
436 * Data now points at the beginning of the list
437 * of smb_lkrng structs.
440 for(; blr
->lock_num
< num_locks
; blr
->lock_num
++) {
443 lock_pid
= get_lock_pid( data
, blr
->lock_num
, large_file_format
);
444 count
= get_lock_count( data
, blr
->lock_num
, large_file_format
);
445 offset
= get_lock_offset( data
, blr
->lock_num
, large_file_format
, &err
);
448 * We know err cannot be set as if it was the lock
449 * request would never have been queued. JRA.
452 status
= do_lock_spin(fsp
,conn
,lock_pid
,count
,offset
,
453 ((locktype
& 1) ? READ_LOCK
: WRITE_LOCK
));
454 if (NT_STATUS_IS_ERR(status
)) break;
457 if(blr
->lock_num
== num_locks
) {
459 * Success - we got all the locks.
462 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
463 fsp
->fsp_name
, fsp
->fnum
, (unsigned int)locktype
, num_locks
) );
465 reply_lockingX_success(blr
);
467 } else if (!NT_STATUS_EQUAL(status
,NT_STATUS_LOCK_NOT_GRANTED
) &&
468 !NT_STATUS_EQUAL(status
,NT_STATUS_FILE_LOCK_CONFLICT
)) {
470 * We have other than a "can't get lock"
471 * error. Free any locks we had and return an error.
472 * Return True so we get dequeued.
475 blocking_lock_reply_error(blr
, status
);
480 * Still can't get all the locks - keep waiting.
483 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
485 blr
->lock_num
, num_locks
, fsp
->fsp_name
, fsp
->fnum
));
490 /****************************************************************************
491 Process a blocking lock SMB.
492 Returns True if we want to be removed from the list.
493 *****************************************************************************/
495 static BOOL
blocking_lock_record_process(blocking_lock_record
*blr
)
497 switch(blr
->com_type
) {
499 return process_lock(blr
);
501 return process_lockread(blr
);
503 return process_lockingX(blr
);
505 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
506 exit_server("PANIC - unknown type on blocking lock queue");
508 return False
; /* Keep compiler happy. */
511 /****************************************************************************
512 Delete entries by fnum from the blocking lock pending queue.
513 *****************************************************************************/
515 void remove_pending_lock_requests_by_fid(files_struct
*fsp
)
517 blocking_lock_record
*blr
= (blocking_lock_record
*)ubi_slFirst( &blocking_lock_queue
);
518 blocking_lock_record
*prev
= NULL
;
521 if(blr
->fsp
->fnum
== fsp
->fnum
) {
523 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
524 file %s fnum = %d\n", blr
->com_type
, fsp
->fsp_name
, fsp
->fnum
));
526 brl_unlock(blr
->fsp
->dev
, blr
->fsp
->inode
, blr
->fsp
->fnum
,
527 blr
->lock_pid
, sys_getpid(), blr
->fsp
->conn
->cnum
,
528 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
530 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
531 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
536 blr
= (blocking_lock_record
*)ubi_slNext(blr
);
540 /****************************************************************************
541 Delete entries by mid from the blocking lock pending queue. Always send reply.
542 *****************************************************************************/
544 void remove_pending_lock_requests_by_mid(int mid
)
546 blocking_lock_record
*blr
= (blocking_lock_record
*)ubi_slFirst( &blocking_lock_queue
);
547 blocking_lock_record
*prev
= NULL
;
550 if(SVAL(blr
->inbuf
,smb_mid
) == mid
) {
551 files_struct
*fsp
= blr
->fsp
;
553 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
554 file %s fnum = %d\n", blr
->com_type
, fsp
->fsp_name
, fsp
->fnum
));
556 blocking_lock_reply_error(blr
,NT_STATUS_CANCELLED
);
557 brl_unlock(blr
->fsp
->dev
, blr
->fsp
->inode
, blr
->fsp
->fnum
,
558 blr
->lock_pid
, sys_getpid(), blr
->fsp
->conn
->cnum
,
559 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
560 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
561 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
566 blr
= (blocking_lock_record
*)ubi_slNext(blr
);
570 /****************************************************************************
571 Set a flag as an unlock request affects one of our pending locks.
572 *****************************************************************************/
574 static void received_unlock_msg(int msg_type
, pid_t src
, void *buf
, size_t len
)
576 DEBUG(10,("received_unlock_msg\n"));
577 process_blocking_lock_queue(time(NULL
));
580 /****************************************************************************
581 Return the number of seconds to the next blocking locks timeout, or default_timeout
582 *****************************************************************************/
584 unsigned blocking_locks_timeout(unsigned default_timeout
)
586 unsigned timeout
= default_timeout
;
588 blocking_lock_record
*blr
= (blocking_lock_record
*)ubi_slFirst(&blocking_lock_queue
);
590 /* note that we avoid the time() syscall if there are no blocking locks */
597 if ((blr
->expire_time
!= (time_t)-1) &&
598 (timeout
> (blr
->expire_time
- t
))) {
599 timeout
= blr
->expire_time
- t
;
601 blr
= (blocking_lock_record
*)ubi_slNext(blr
);
610 /****************************************************************************
611 Process the blocking lock queue. Note that this is only called as root.
612 *****************************************************************************/
614 void process_blocking_lock_queue(time_t t
)
616 blocking_lock_record
*blr
= (blocking_lock_record
*)ubi_slFirst( &blocking_lock_queue
);
617 blocking_lock_record
*prev
= NULL
;
623 * Go through the queue and see if we can get any of the locks.
627 connection_struct
*conn
= NULL
;
629 files_struct
*fsp
= NULL
;
632 * Ensure we don't have any old chain_fsp values
639 conn
= conn_find(SVAL(blr
->inbuf
,smb_tid
));
640 vuid
= (lp_security() == SEC_SHARE
) ? UID_FIELD_INVALID
:
641 SVAL(blr
->inbuf
,smb_uid
);
643 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
644 fsp
->fnum
, fsp
->fsp_name
));
646 if((blr
->expire_time
!= -1) && (blr
->expire_time
<= t
)) {
648 * Lock expired - throw away all previously
649 * obtained locks and return lock error.
651 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
652 fsp
->fnum
, fsp
->fsp_name
));
654 brl_unlock(fsp
->dev
, fsp
->inode
, fsp
->fnum
,
655 blr
->lock_pid
, sys_getpid(), conn
->cnum
,
656 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
658 blocking_lock_reply_error(blr
,NT_STATUS_FILE_LOCK_CONFLICT
);
659 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
660 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
664 if(!change_to_user(conn
,vuid
)) {
665 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
668 * Remove the entry and return an error to the client.
670 blocking_lock_reply_error(blr
,NT_STATUS_ACCESS_DENIED
);
672 brl_unlock(fsp
->dev
, fsp
->inode
, fsp
->fnum
,
673 blr
->lock_pid
, sys_getpid(), conn
->cnum
,
674 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
676 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
677 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
681 if(!set_current_service(conn
,True
)) {
682 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno
) ));
684 * Remove the entry and return an error to the client.
686 blocking_lock_reply_error(blr
,NT_STATUS_ACCESS_DENIED
);
688 brl_unlock(fsp
->dev
, fsp
->inode
, fsp
->fnum
,
689 blr
->lock_pid
, sys_getpid(), conn
->cnum
,
690 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
692 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
693 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
694 change_to_root_user();
699 * Go through the remaining locks and try and obtain them.
700 * The call returns True if all locks were obtained successfully
701 * and False if we still need to wait.
704 if(blocking_lock_record_process(blr
)) {
706 brl_unlock(fsp
->dev
, fsp
->inode
, fsp
->fnum
,
707 blr
->lock_pid
, sys_getpid(), conn
->cnum
,
708 blr
->offset
, blr
->count
, True
, NULL
, NULL
);
710 free_blocking_lock_record((blocking_lock_record
*)ubi_slRemNext( &blocking_lock_queue
, prev
));
711 blr
= (blocking_lock_record
*)(prev
? ubi_slNext(prev
) : ubi_slFirst(&blocking_lock_queue
));
712 change_to_root_user();
716 change_to_root_user();
719 * Move to the next in the list.
722 blr
= (blocking_lock_record
*)ubi_slNext(blr
);