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 *****************************************************************************/
30 typedef struct _blocking_lock_record {
31 struct _blocking_lock_record *next;
32 struct _blocking_lock_record *prev;
42 } blocking_lock_record;
44 static blocking_lock_record *blocking_lock_queue;
46 /****************************************************************************
47 Destructor for the above structure.
48 ****************************************************************************/
50 static void free_blocking_lock_record(blocking_lock_record *blr)
52 SAFE_FREE(blr->inbuf);
56 /****************************************************************************
57 Get the files_struct given a particular queued SMB.
58 *****************************************************************************/
60 static files_struct *get_fsp_from_pkt(char *inbuf)
62 switch(CVAL(inbuf,smb_com)) {
65 return file_fsp(inbuf,smb_vwv0);
67 return file_fsp(inbuf,smb_vwv2);
69 DEBUG(0,("get_fsp_from_pkt: PANIC - unknown type on blocking lock queue - exiting.!\n"));
70 exit_server("PANIC - unknown type on blocking lock queue");
72 return NULL; /* Keep compiler happy. */
75 /****************************************************************************
76 Determine if this is a secondary element of a chained SMB.
77 **************************************************************************/
79 static BOOL in_chained_smb(void)
81 return (chain_size != 0);
84 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len);
86 /****************************************************************************
87 Function to push a blocking lock request onto the lock queue.
88 ****************************************************************************/
90 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout,
91 int lock_num, uint16 lock_pid, SMB_BIG_UINT offset, SMB_BIG_UINT count)
93 static BOOL set_lock_msg;
94 blocking_lock_record *blr, *tmp;
95 BOOL my_lock_ctx = False;
98 if(in_chained_smb() ) {
99 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
104 * Now queue an entry on the blocking lock queue. We setup
105 * the expiration time here.
108 if((blr = (blocking_lock_record *)malloc(sizeof(blocking_lock_record))) == NULL) {
109 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
113 if((blr->inbuf = (char *)malloc(length)) == NULL) {
114 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
119 blr->com_type = CVAL(inbuf,smb_com);
120 blr->fsp = get_fsp_from_pkt(inbuf);
121 blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
122 blr->lock_num = lock_num;
123 blr->lock_pid = lock_pid;
124 blr->offset = offset;
126 memcpy(blr->inbuf, inbuf, length);
127 blr->length = length;
129 /* Add a pending lock record for this. */
130 status = brl_lock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
131 lock_pid, sys_getpid(), blr->fsp->conn->cnum,
132 offset, count, PENDING_LOCK, &my_lock_ctx);
134 if (!NT_STATUS_IS_OK(status)) {
135 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
136 free_blocking_lock_record(blr);
140 DLIST_ADD_END(blocking_lock_queue, blr, tmp);
142 /* Ensure we'll receive messages when this is unlocked. */
144 message_register(MSG_SMB_UNLOCK, received_unlock_msg);
148 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
149 for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
150 blr->fsp->fnum, blr->fsp->fsp_name ));
152 /* Push the MID of this packet on the signing queue. */
153 srv_defer_sign_response(SVAL(inbuf,smb_mid));
158 /****************************************************************************
159 Return a smd with a given size.
160 *****************************************************************************/
162 static void send_blocking_reply(char *outbuf, int outsize)
165 smb_setlen(outbuf,outsize - 4);
167 if (!send_smb(smbd_server_fd(),outbuf))
168 exit_server("send_blocking_reply: send_smb failed.");
171 /****************************************************************************
172 Return a lockingX success SMB.
173 *****************************************************************************/
175 static void reply_lockingX_success(blocking_lock_record *blr)
177 char *outbuf = OutBuffer;
178 int bufsize = BUFFER_SIZE;
179 char *inbuf = blr->inbuf;
182 construct_reply_common(inbuf, outbuf);
183 set_message(outbuf,2,0,True);
186 * As this message is a lockingX call we must handle
187 * any following chained message correctly.
188 * This is normally handled in construct_reply(),
189 * but as that calls switch_message, we can't use
190 * that here and must set up the chain info manually.
193 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
195 outsize += chain_size;
197 send_blocking_reply(outbuf,outsize);
200 /****************************************************************************
201 Return a generic lock fail error blocking call.
202 *****************************************************************************/
204 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
206 char *outbuf = OutBuffer;
207 char *inbuf = blr->inbuf;
208 construct_reply_common(inbuf, outbuf);
210 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
211 FILE_LOCK_CONFLICT! (tridge) */
212 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
213 status = NT_STATUS_FILE_LOCK_CONFLICT;
217 if (!send_smb(smbd_server_fd(),outbuf))
218 exit_server("generic_blocking_lock_error: send_smb failed.");
221 /****************************************************************************
222 Return a lock fail error for a lockingX call. Undo all the locks we have
224 *****************************************************************************/
226 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
228 char *inbuf = blr->inbuf;
229 files_struct *fsp = blr->fsp;
230 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
231 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
232 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
234 unsigned char locktype = CVAL(inbuf,smb_vwv3);
235 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
239 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
242 * Data now points at the beginning of the list
243 * of smb_lkrng structs.
247 * Ensure we don't do a remove on the lock that just failed,
248 * as under POSIX rules, if we have a lock already there, we
249 * will delete it (and we shouldn't) .....
252 for(i = blr->lock_num - 1; i >= 0; i--) {
255 lock_pid = get_lock_pid( data, i, large_file_format);
256 count = get_lock_count( data, i, large_file_format);
257 offset = get_lock_offset( data, i, large_file_format, &err);
260 * We know err cannot be set as if it was the lock
261 * request would never have been queued. JRA.
264 do_unlock(fsp,conn,lock_pid,count,offset);
267 generic_blocking_lock_error(blr, status);
270 /****************************************************************************
271 Return a lock fail error.
272 *****************************************************************************/
274 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
276 switch(blr->com_type) {
279 generic_blocking_lock_error(blr, status);
282 reply_lockingX_error(blr, status);
285 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
286 exit_server("PANIC - unknown type on blocking lock queue");
290 /****************************************************************************
291 Attempt to finish off getting all pending blocking locks for a lockread call.
292 Returns True if we want to be removed from the list.
293 *****************************************************************************/
295 static BOOL process_lockread(blocking_lock_record *blr)
297 char *outbuf = OutBuffer;
298 char *inbuf = blr->inbuf;
302 SMB_BIG_UINT startpos;
305 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
306 files_struct *fsp = blr->fsp;
307 BOOL my_lock_ctx = False;
309 numtoread = SVAL(inbuf,smb_vwv1);
310 startpos = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv2);
312 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
313 data = smb_buf(outbuf) + 3;
315 status = do_lock_spin( fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtoread, startpos, READ_LOCK, &my_lock_ctx);
316 if (NT_STATUS_V(status)) {
317 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
318 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
320 * We have other than a "can't get lock"
321 * error. Send an error.
322 * Return True so we get dequeued.
324 generic_blocking_lock_error(blr, status);
329 * Still waiting for lock....
332 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
337 nread = read_file(fsp,data,startpos,numtoread);
340 generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
344 construct_reply_common(inbuf, outbuf);
345 outsize = set_message(outbuf,5,0,True);
348 SSVAL(outbuf,smb_vwv0,nread);
349 SSVAL(outbuf,smb_vwv5,nread+3);
352 SSVAL(p,0,nread); p += 2;
353 set_message_end(outbuf, p+nread);
355 DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
356 fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
358 send_blocking_reply(outbuf,outsize);
362 /****************************************************************************
363 Attempt to finish off getting all pending blocking locks for a lock call.
364 Returns True if we want to be removed from the list.
365 *****************************************************************************/
367 static BOOL process_lock(blocking_lock_record *blr)
369 char *outbuf = OutBuffer;
370 char *inbuf = blr->inbuf;
372 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
374 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
375 files_struct *fsp = blr->fsp;
376 BOOL my_lock_ctx = False;
378 count = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
379 offset = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
382 status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK, &my_lock_ctx);
383 if (NT_STATUS_IS_ERR(status)) {
384 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
385 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
387 * We have other than a "can't get lock"
388 * error. Send an error.
389 * Return True so we get dequeued.
392 blocking_lock_reply_error(blr, status);
396 * Still can't get the lock - keep waiting.
398 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
404 * Success - we got the lock.
407 DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
408 fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
410 construct_reply_common(inbuf, outbuf);
411 outsize = set_message(outbuf,0,0,True);
412 send_blocking_reply(outbuf,outsize);
416 /****************************************************************************
417 Attempt to finish off getting all pending blocking locks for a lockingX call.
418 Returns True if we want to be removed from the list.
419 *****************************************************************************/
421 static BOOL process_lockingX(blocking_lock_record *blr)
423 char *inbuf = blr->inbuf;
424 unsigned char locktype = CVAL(inbuf,smb_vwv3);
425 files_struct *fsp = blr->fsp;
426 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
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;
431 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
433 BOOL my_lock_ctx = False;
434 NTSTATUS status = NT_STATUS_OK;
436 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
439 * Data now points at the beginning of the list
440 * of smb_lkrng structs.
443 for(; blr->lock_num < num_locks; blr->lock_num++) {
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.
455 status = do_lock_spin(fsp,conn,lock_pid,count,offset,
456 ((locktype & 1) ? READ_LOCK : WRITE_LOCK), &my_lock_ctx);
457 if (NT_STATUS_IS_ERR(status)) break;
460 if(blr->lock_num == num_locks) {
462 * Success - we got all the locks.
465 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
466 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
468 reply_lockingX_success(blr);
470 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
471 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
473 * We have other than a "can't get lock"
474 * error. Free any locks we had and return an error.
475 * Return True so we get dequeued.
478 blocking_lock_reply_error(blr, status);
483 * Still can't get all the locks - keep waiting.
486 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
488 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
493 /****************************************************************************
494 Process a blocking lock SMB.
495 Returns True if we want to be removed from the list.
496 *****************************************************************************/
498 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
500 switch(blr->com_type) {
502 return process_lock(blr);
504 return process_lockread(blr);
506 return process_lockingX(blr);
508 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
509 exit_server("PANIC - unknown type on blocking lock queue");
511 return False; /* Keep compiler happy. */
514 /****************************************************************************
515 Delete entries by fnum from the blocking lock pending queue.
516 *****************************************************************************/
518 void remove_pending_lock_requests_by_fid(files_struct *fsp)
520 blocking_lock_record *blr, *next = NULL;
522 for(blr = blocking_lock_queue; blr; blr = next) {
524 if(blr->fsp->fnum == fsp->fnum) {
526 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
527 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
529 brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
530 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
531 blr->offset, blr->count, True, NULL, NULL);
533 free_blocking_lock_record(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, *next = NULL;
546 for(blr = blocking_lock_queue; blr; blr = next) {
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_FILE_LOCK_CONFLICT);
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(blr);
563 /****************************************************************************
564 Set a flag as an unlock request affects one of our pending locks.
565 *****************************************************************************/
567 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len)
569 DEBUG(10,("received_unlock_msg\n"));
570 process_blocking_lock_queue(time(NULL));
573 /****************************************************************************
574 Return the number of seconds to the next blocking locks timeout, or default_timeout
575 *****************************************************************************/
577 unsigned blocking_locks_timeout(unsigned default_timeout)
579 unsigned timeout = default_timeout;
581 blocking_lock_record *blr = blocking_lock_queue;
583 /* note that we avoid the time() syscall if there are no blocking locks */
589 for (; blr; blr = blr->next) {
590 if ((blr->expire_time != (time_t)-1) &&
591 (timeout > (blr->expire_time - t))) {
592 timeout = blr->expire_time - t;
602 /****************************************************************************
603 Process the blocking lock queue. Note that this is only called as root.
604 *****************************************************************************/
606 void process_blocking_lock_queue(time_t t)
608 blocking_lock_record *blr, *next = NULL;
611 * Go through the queue and see if we can get any of the locks.
614 for (blr = blocking_lock_queue; blr; blr = next) {
615 connection_struct *conn = NULL;
617 files_struct *fsp = NULL;
622 * Ensure we don't have any old chain_fsp values
629 conn = conn_find(SVAL(blr->inbuf,smb_tid));
630 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
631 SVAL(blr->inbuf,smb_uid);
633 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
634 fsp->fnum, fsp->fsp_name ));
636 if((blr->expire_time != -1) && (blr->expire_time <= t)) {
638 * Lock expired - throw away all previously
639 * obtained locks and return lock error.
641 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
642 fsp->fnum, fsp->fsp_name ));
644 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
645 blr->lock_pid, sys_getpid(), conn->cnum,
646 blr->offset, blr->count, True, NULL, NULL);
648 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
649 free_blocking_lock_record(blr);
653 if(!change_to_user(conn,vuid)) {
654 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
657 * Remove the entry and return an error to the client.
659 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
661 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
662 blr->lock_pid, sys_getpid(), conn->cnum,
663 blr->offset, blr->count, True, NULL, NULL);
665 free_blocking_lock_record(blr);
669 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
670 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
672 * Remove the entry and return an error to the client.
674 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
676 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
677 blr->lock_pid, sys_getpid(), conn->cnum,
678 blr->offset, blr->count, True, NULL, NULL);
680 free_blocking_lock_record(blr);
681 change_to_root_user();
686 * Go through the remaining locks and try and obtain them.
687 * The call returns True if all locks were obtained successfully
688 * and False if we still need to wait.
691 if(blocking_lock_record_process(blr)) {
693 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
694 blr->lock_pid, sys_getpid(), conn->cnum,
695 blr->offset, blr->count, True, NULL, NULL);
697 free_blocking_lock_record(blr);
699 change_to_root_user();