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 DLIST_REMOVE(blocking_lock_queue, blr);
53 SAFE_FREE(blr->inbuf);
57 /****************************************************************************
58 Get the files_struct given a particular queued SMB.
59 *****************************************************************************/
61 static files_struct *get_fsp_from_pkt(char *inbuf)
63 switch(CVAL(inbuf,smb_com)) {
66 return file_fsp(inbuf,smb_vwv0);
68 return file_fsp(inbuf,smb_vwv2);
70 DEBUG(0,("get_fsp_from_pkt: PANIC - unknown type on blocking lock queue - exiting.!\n"));
71 exit_server("PANIC - unknown type on blocking lock queue");
73 return NULL; /* Keep compiler happy. */
76 /****************************************************************************
77 Determine if this is a secondary element of a chained SMB.
78 **************************************************************************/
80 static BOOL in_chained_smb(void)
82 return (chain_size != 0);
85 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len);
87 /****************************************************************************
88 Function to push a blocking lock request onto the lock queue.
89 ****************************************************************************/
91 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout,
92 int lock_num, uint16 lock_pid, SMB_BIG_UINT offset, SMB_BIG_UINT count)
94 static BOOL set_lock_msg;
95 blocking_lock_record *blr, *tmp;
96 BOOL my_lock_ctx = False;
99 if(in_chained_smb() ) {
100 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
105 * Now queue an entry on the blocking lock queue. We setup
106 * the expiration time here.
109 if((blr = (blocking_lock_record *)malloc(sizeof(blocking_lock_record))) == NULL) {
110 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
114 if((blr->inbuf = (char *)malloc(length)) == NULL) {
115 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
120 blr->com_type = CVAL(inbuf,smb_com);
121 blr->fsp = get_fsp_from_pkt(inbuf);
122 blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
123 blr->lock_num = lock_num;
124 blr->lock_pid = lock_pid;
125 blr->offset = offset;
127 memcpy(blr->inbuf, inbuf, length);
128 blr->length = length;
130 /* Add a pending lock record for this. */
131 status = brl_lock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
132 lock_pid, sys_getpid(), blr->fsp->conn->cnum,
133 offset, count, PENDING_LOCK, &my_lock_ctx);
135 if (!NT_STATUS_IS_OK(status)) {
136 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
137 free_blocking_lock_record(blr);
141 DLIST_ADD_END(blocking_lock_queue, blr, tmp);
143 /* Ensure we'll receive messages when this is unlocked. */
145 message_register(MSG_SMB_UNLOCK, received_unlock_msg);
149 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
150 for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
151 blr->fsp->fnum, blr->fsp->fsp_name ));
153 /* Push the MID of this packet on the signing queue. */
154 srv_defer_sign_response(SVAL(inbuf,smb_mid));
159 /****************************************************************************
160 Return a smd with a given size.
161 *****************************************************************************/
163 static void send_blocking_reply(char *outbuf, int outsize)
166 smb_setlen(outbuf,outsize - 4);
168 if (!send_smb(smbd_server_fd(),outbuf))
169 exit_server("send_blocking_reply: send_smb failed.");
172 /****************************************************************************
173 Return a lockingX success SMB.
174 *****************************************************************************/
176 static void reply_lockingX_success(blocking_lock_record *blr)
178 char *outbuf = OutBuffer;
179 int bufsize = BUFFER_SIZE;
180 char *inbuf = blr->inbuf;
183 construct_reply_common(inbuf, outbuf);
184 set_message(outbuf,2,0,True);
187 * As this message is a lockingX call we must handle
188 * any following chained message correctly.
189 * This is normally handled in construct_reply(),
190 * but as that calls switch_message, we can't use
191 * that here and must set up the chain info manually.
194 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
196 outsize += chain_size;
198 send_blocking_reply(outbuf,outsize);
201 /****************************************************************************
202 Return a generic lock fail error blocking call.
203 *****************************************************************************/
205 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
207 char *outbuf = OutBuffer;
208 char *inbuf = blr->inbuf;
209 construct_reply_common(inbuf, outbuf);
211 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
212 FILE_LOCK_CONFLICT! (tridge) */
213 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
214 status = NT_STATUS_FILE_LOCK_CONFLICT;
218 if (!send_smb(smbd_server_fd(),outbuf))
219 exit_server("generic_blocking_lock_error: send_smb failed.");
222 /****************************************************************************
223 Return a lock fail error for a lockingX call. Undo all the locks we have
225 *****************************************************************************/
227 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
229 char *inbuf = blr->inbuf;
230 files_struct *fsp = blr->fsp;
231 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
232 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
233 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
235 unsigned char locktype = CVAL(inbuf,smb_vwv3);
236 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
240 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
243 * Data now points at the beginning of the list
244 * of smb_lkrng structs.
248 * Ensure we don't do a remove on the lock that just failed,
249 * as under POSIX rules, if we have a lock already there, we
250 * will delete it (and we shouldn't) .....
253 for(i = blr->lock_num - 1; i >= 0; i--) {
256 lock_pid = get_lock_pid( data, i, large_file_format);
257 count = get_lock_count( data, i, large_file_format);
258 offset = get_lock_offset( data, i, large_file_format, &err);
261 * We know err cannot be set as if it was the lock
262 * request would never have been queued. JRA.
265 do_unlock(fsp,conn,lock_pid,count,offset);
268 generic_blocking_lock_error(blr, status);
271 /****************************************************************************
272 Return a lock fail error.
273 *****************************************************************************/
275 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
277 switch(blr->com_type) {
280 generic_blocking_lock_error(blr, status);
283 reply_lockingX_error(blr, status);
286 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
287 exit_server("PANIC - unknown type on blocking lock queue");
291 /****************************************************************************
292 Attempt to finish off getting all pending blocking locks for a lockread call.
293 Returns True if we want to be removed from the list.
294 *****************************************************************************/
296 static BOOL process_lockread(blocking_lock_record *blr)
298 char *outbuf = OutBuffer;
299 char *inbuf = blr->inbuf;
303 SMB_BIG_UINT startpos;
306 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
307 files_struct *fsp = blr->fsp;
308 BOOL my_lock_ctx = False;
310 numtoread = SVAL(inbuf,smb_vwv1);
311 startpos = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv2);
313 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
314 data = smb_buf(outbuf) + 3;
316 status = do_lock_spin( fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtoread, startpos, READ_LOCK, &my_lock_ctx);
317 if (NT_STATUS_V(status)) {
318 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
319 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
321 * We have other than a "can't get lock"
322 * error. Send an error.
323 * Return True so we get dequeued.
325 generic_blocking_lock_error(blr, status);
330 * Still waiting for lock....
333 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
338 nread = read_file(fsp,data,startpos,numtoread);
341 generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
345 construct_reply_common(inbuf, outbuf);
346 outsize = set_message(outbuf,5,0,True);
349 SSVAL(outbuf,smb_vwv0,nread);
350 SSVAL(outbuf,smb_vwv5,nread+3);
353 SSVAL(p,0,nread); p += 2;
354 set_message_end(outbuf, p+nread);
356 DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
357 fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
359 send_blocking_reply(outbuf,outsize);
363 /****************************************************************************
364 Attempt to finish off getting all pending blocking locks for a lock call.
365 Returns True if we want to be removed from the list.
366 *****************************************************************************/
368 static BOOL process_lock(blocking_lock_record *blr)
370 char *outbuf = OutBuffer;
371 char *inbuf = blr->inbuf;
373 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
375 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
376 files_struct *fsp = blr->fsp;
377 BOOL my_lock_ctx = False;
379 count = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
380 offset = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
383 status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK, &my_lock_ctx);
384 if (NT_STATUS_IS_ERR(status)) {
385 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
386 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
388 * We have other than a "can't get lock"
389 * error. Send an error.
390 * Return True so we get dequeued.
393 blocking_lock_reply_error(blr, status);
397 * Still can't get the lock - keep waiting.
399 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
405 * Success - we got the lock.
408 DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
409 fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
411 construct_reply_common(inbuf, outbuf);
412 outsize = set_message(outbuf,0,0,True);
413 send_blocking_reply(outbuf,outsize);
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 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
428 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
429 uint16 num_locks = SVAL(inbuf,smb_vwv7);
430 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
432 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
434 BOOL my_lock_ctx = False;
435 NTSTATUS status = NT_STATUS_OK;
437 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
440 * Data now points at the beginning of the list
441 * of smb_lkrng structs.
444 for(; blr->lock_num < num_locks; blr->lock_num++) {
447 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
448 count = get_lock_count( data, blr->lock_num, large_file_format);
449 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
452 * We know err cannot be set as if it was the lock
453 * request would never have been queued. JRA.
456 status = do_lock_spin(fsp,conn,lock_pid,count,offset,
457 ((locktype & 1) ? READ_LOCK : WRITE_LOCK), &my_lock_ctx);
458 if (NT_STATUS_IS_ERR(status)) break;
461 if(blr->lock_num == num_locks) {
463 * Success - we got all the locks.
466 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
467 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
469 reply_lockingX_success(blr);
471 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
472 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
474 * We have other than a "can't get lock"
475 * error. Free any locks we had and return an error.
476 * Return True so we get dequeued.
479 blocking_lock_reply_error(blr, status);
484 * Still can't get all the locks - keep waiting.
487 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
489 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
494 /****************************************************************************
495 Process a blocking lock SMB.
496 Returns True if we want to be removed from the list.
497 *****************************************************************************/
499 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
501 switch(blr->com_type) {
503 return process_lock(blr);
505 return process_lockread(blr);
507 return process_lockingX(blr);
509 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
510 exit_server("PANIC - unknown type on blocking lock queue");
512 return False; /* Keep compiler happy. */
515 /****************************************************************************
516 Delete entries by fnum from the blocking lock pending queue.
517 *****************************************************************************/
519 void remove_pending_lock_requests_by_fid(files_struct *fsp)
521 blocking_lock_record *blr, *next = NULL;
523 for(blr = blocking_lock_queue; blr; blr = next) {
525 if(blr->fsp->fnum == fsp->fnum) {
527 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
528 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
530 brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
531 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
532 blr->offset, blr->count, True, NULL, NULL);
534 free_blocking_lock_record(blr);
539 /****************************************************************************
540 Delete entries by mid from the blocking lock pending queue. Always send reply.
541 *****************************************************************************/
543 void remove_pending_lock_requests_by_mid(int mid)
545 blocking_lock_record *blr, *next = NULL;
547 for(blr = blocking_lock_queue; blr; blr = next) {
549 if(SVAL(blr->inbuf,smb_mid) == mid) {
550 files_struct *fsp = blr->fsp;
552 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
553 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
555 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
556 brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
557 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
558 blr->offset, blr->count, True, NULL, NULL);
559 free_blocking_lock_record(blr);
564 /****************************************************************************
565 Set a flag as an unlock request affects one of our pending locks.
566 *****************************************************************************/
568 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len)
570 DEBUG(10,("received_unlock_msg\n"));
571 process_blocking_lock_queue(time(NULL));
574 /****************************************************************************
575 Return the number of seconds to the next blocking locks timeout, or default_timeout
576 *****************************************************************************/
578 unsigned blocking_locks_timeout(unsigned default_timeout)
580 unsigned timeout = default_timeout;
582 blocking_lock_record *blr = blocking_lock_queue;
584 /* note that we avoid the time() syscall if there are no blocking locks */
590 for (; blr; blr = blr->next) {
591 if ((blr->expire_time != (time_t)-1) &&
592 (timeout > (blr->expire_time - t))) {
593 timeout = blr->expire_time - t;
603 /****************************************************************************
604 Process the blocking lock queue. Note that this is only called as root.
605 *****************************************************************************/
607 void process_blocking_lock_queue(time_t t)
609 blocking_lock_record *blr, *next = NULL;
612 * Go through the queue and see if we can get any of the locks.
615 for (blr = blocking_lock_queue; blr; blr = next) {
616 connection_struct *conn = NULL;
618 files_struct *fsp = NULL;
623 * Ensure we don't have any old chain_fsp values
630 conn = conn_find(SVAL(blr->inbuf,smb_tid));
631 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
632 SVAL(blr->inbuf,smb_uid);
634 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
635 fsp->fnum, fsp->fsp_name ));
637 if((blr->expire_time != -1) && (blr->expire_time <= t)) {
639 * Lock expired - throw away all previously
640 * obtained locks and return lock error.
642 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
643 fsp->fnum, fsp->fsp_name ));
645 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
646 blr->lock_pid, sys_getpid(), conn->cnum,
647 blr->offset, blr->count, True, NULL, NULL);
649 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
650 free_blocking_lock_record(blr);
654 if(!change_to_user(conn,vuid)) {
655 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
658 * Remove the entry and return an error to the client.
660 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
662 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
663 blr->lock_pid, sys_getpid(), conn->cnum,
664 blr->offset, blr->count, True, NULL, NULL);
666 free_blocking_lock_record(blr);
670 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
671 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
673 * Remove the entry and return an error to the client.
675 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
677 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
678 blr->lock_pid, sys_getpid(), conn->cnum,
679 blr->offset, blr->count, True, NULL, NULL);
681 free_blocking_lock_record(blr);
682 change_to_root_user();
687 * Go through the remaining locks and try and obtain them.
688 * The call returns True if all locks were obtained successfully
689 * and False if we still need to wait.
692 if(blocking_lock_record_process(blr)) {
694 brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
695 blr->lock_pid, sys_getpid(), conn->cnum,
696 blr->offset, blr->count, True, NULL, NULL);
698 free_blocking_lock_record(blr);
700 change_to_root_user();