r14507: Re-disable accidentially re-enabled paranoia check. This should make
[Samba/gebeck_regimport.git] / source / smbd / blocking.c
blob805e45f6ea33f2d870e3905d88dd615b297eb882
1 /*
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.
21 #include "includes.h"
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;
31 int com_type;
32 files_struct *fsp;
33 time_t expire_time;
34 int lock_num;
35 SMB_BIG_UINT offset;
36 SMB_BIG_UINT count;
37 uint16 lock_pid;
38 char *inbuf;
39 int length;
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);
52 SAFE_FREE(blr);
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)) {
62 case SMBlock:
63 case SMBlockread:
64 return file_fsp(inbuf,smb_vwv0);
65 case SMBlockingX:
66 return file_fsp(inbuf,smb_vwv2);
67 default:
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, struct process_id src,
84 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;
96 NTSTATUS status;
98 if(in_chained_smb() ) {
99 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
100 return False;
104 * Now queue an entry on the blocking lock queue. We setup
105 * the expiration time here.
108 if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
109 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
110 return False;
113 if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
114 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
115 SAFE_FREE(blr);
116 return False;
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;
125 blr->count = count;
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, procid_self(), 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);
137 return False;
140 DLIST_ADD_END(blocking_lock_queue, blr, tmp);
142 /* Ensure we'll receive messages when this is unlocked. */
143 if (!set_lock_msg) {
144 message_register(MSG_SMB_UNLOCK, received_unlock_msg);
145 set_lock_msg = True;
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));
155 return True;
158 /****************************************************************************
159 Return a smd with a given size.
160 *****************************************************************************/
162 static void send_blocking_reply(char *outbuf, int outsize)
164 if(outsize > 4)
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 = get_OutBuffer();
178 int bufsize = BUFFER_SIZE;
179 char *inbuf = blr->inbuf;
180 int outsize = 0;
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 = get_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;
216 ERROR_NT(status);
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
223 obtained first.
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;
233 uint16 lock_pid;
234 unsigned char locktype = CVAL(inbuf,smb_vwv3);
235 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
236 char *data;
237 int i;
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--) {
253 BOOL err;
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) {
277 case SMBlock:
278 case SMBlockread:
279 generic_blocking_lock_error(blr, status);
280 break;
281 case SMBlockingX:
282 reply_lockingX_error(blr, status);
283 break;
284 default:
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 = get_OutBuffer();
298 char *inbuf = blr->inbuf;
299 ssize_t nread = -1;
300 char *data, *p;
301 int outsize = 0;
302 SMB_BIG_UINT startpos;
303 size_t numtoread;
304 NTSTATUS status;
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);
325 return True;
329 * Still waiting for lock....
332 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
333 fsp->fsp_name));
334 return False;
337 nread = read_file(fsp,data,startpos,numtoread);
339 if (nread < 0) {
340 generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
341 return True;
344 construct_reply_common(inbuf, outbuf);
345 outsize = set_message(outbuf,5,0,True);
347 outsize += nread;
348 SSVAL(outbuf,smb_vwv0,nread);
349 SSVAL(outbuf,smb_vwv5,nread+3);
350 p = smb_buf(outbuf);
351 *p++ = 1;
352 SSVAL(p,0,nread); p += 2;
353 set_message_end(outbuf, p+nread);
355 DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%lu nread=%ld\n",
356 fsp->fsp_name, fsp->fnum, (unsigned long)numtoread, (long)nread ) );
358 send_blocking_reply(outbuf,outsize);
359 return True;
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 = get_OutBuffer();
370 char *inbuf = blr->inbuf;
371 int outsize;
372 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
373 NTSTATUS status;
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);
381 errno = 0;
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);
393 return True;
396 * Still can't get the lock - keep waiting.
398 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
399 fsp->fsp_name));
400 return False;
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);
413 return True;
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;
430 uint16 lock_pid;
431 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
432 char *data;
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++) {
444 BOOL err;
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.
454 errno = 0;
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);
469 return True;
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);
479 return True;
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. \
487 Waiting....\n",
488 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
490 return False;
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) {
501 case SMBlock:
502 return process_lock(blr);
503 case SMBlockread:
504 return process_lockread(blr);
505 case SMBlockingX:
506 return process_lockingX(blr);
507 default:
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) {
523 next = 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, procid_self(), 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) {
547 next = 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, procid_self(), 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, struct process_id src,
568 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;
581 time_t t;
582 blocking_lock_record *blr = blocking_lock_queue;
584 /* note that we avoid the time() syscall if there are no blocking locks */
585 if (!blr)
586 return timeout;
588 t = time(NULL);
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;
597 if (timeout < 1)
598 timeout = 1;
600 return timeout;
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;
617 uint16 vuid;
618 files_struct *fsp = NULL;
620 next = blr->next;
623 * Ensure we don't have any old chain_fsp values
624 * sitting around....
626 chain_size = 0;
627 file_chain_reset();
628 fsp = blr->fsp;
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, procid_self(), 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);
651 continue;
654 if(!change_to_user(conn,vuid)) {
655 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
656 vuid ));
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, procid_self(), conn->cnum,
664 blr->offset, blr->count, True, NULL, NULL);
666 free_blocking_lock_record(blr);
667 continue;
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, procid_self(), conn->cnum,
679 blr->offset, blr->count, True, NULL, NULL);
681 free_blocking_lock_record(blr);
682 change_to_root_user();
683 continue;
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, procid_self(), conn->cnum,
696 blr->offset, blr->count, True, NULL, NULL);
698 free_blocking_lock_record(blr);
700 change_to_root_user();