oops. Use -r, not -f
[Samba.git] / source / smbd / blocking.c
blob96235dea3a597afb557fdd0f14c2be16c7d4852d
1 /*
2 Unix SMB/CIFS implementation.
3 Blocking Locking functions
4 Copyright (C) Jeremy Allison 1998
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 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 {
31 ubi_slNode msg_next;
32 int com_type;
33 files_struct *fsp;
34 time_t expire_time;
35 int lock_num;
36 char *inbuf;
37 int length;
38 } blocking_lock_record;
40 static ubi_slList blocking_lock_queue = { NULL, (ubi_slNodePtr)&blocking_lock_queue, 0};
42 /****************************************************************************
43 Destructor for the above structure.
44 ****************************************************************************/
46 static void free_blocking_lock_record(blocking_lock_record *blr)
48 SAFE_FREE(blr->inbuf);
49 SAFE_FREE(blr);
52 /****************************************************************************
53 Get the files_struct given a particular queued SMB.
54 *****************************************************************************/
56 static files_struct *get_fsp_from_pkt(char *inbuf)
58 switch(CVAL(inbuf,smb_com)) {
59 case SMBlock:
60 case SMBlockread:
61 return file_fsp(inbuf,smb_vwv0);
62 case SMBlockingX:
63 return file_fsp(inbuf,smb_vwv2);
64 default:
65 DEBUG(0,("get_fsp_from_pkt: PANIC - unknown type on blocking lock queue - exiting.!\n"));
66 exit_server("PANIC - unknown type on blocking lock queue");
68 return NULL; /* Keep compiler happy. */
71 /****************************************************************************
72 Determine if this is a secondary element of a chained SMB.
73 **************************************************************************/
75 static BOOL in_chained_smb(void)
77 return (chain_size != 0);
80 /****************************************************************************
81 Function to push a blocking lock request onto the lock queue.
82 ****************************************************************************/
84 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout, int lock_num)
86 blocking_lock_record *blr;
88 if(in_chained_smb() ) {
89 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
90 return False;
94 * Now queue an entry on the blocking lock queue. We setup
95 * the expiration time here.
98 if((blr = (blocking_lock_record *)malloc(sizeof(blocking_lock_record))) == NULL) {
99 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
100 return False;
103 if((blr->inbuf = (char *)malloc(length)) == NULL) {
104 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
105 SAFE_FREE(blr);
106 return False;
109 blr->com_type = CVAL(inbuf,smb_com);
110 blr->fsp = get_fsp_from_pkt(inbuf);
111 blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
112 blr->lock_num = lock_num;
113 memcpy(blr->inbuf, inbuf, length);
114 blr->length = length;
116 ubi_slAddTail(&blocking_lock_queue, blr);
119 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
120 for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
121 blr->fsp->fnum, blr->fsp->fsp_name ));
123 return True;
126 /****************************************************************************
127 Return a smd with a given size.
128 *****************************************************************************/
130 static void send_blocking_reply(char *outbuf, int outsize)
132 if(outsize > 4)
133 smb_setlen(outbuf,outsize - 4);
135 if (!send_smb(smbd_server_fd(),outbuf))
136 exit_server("send_blocking_reply: send_smb failed.");
139 /****************************************************************************
140 Return a lockingX success SMB.
141 *****************************************************************************/
143 static void reply_lockingX_success(blocking_lock_record *blr)
145 char *outbuf = OutBuffer;
146 int bufsize = BUFFER_SIZE;
147 char *inbuf = blr->inbuf;
148 int outsize = 0;
150 construct_reply_common(inbuf, outbuf);
151 set_message(outbuf,2,0,True);
154 * As this message is a lockingX call we must handle
155 * any following chained message correctly.
156 * This is normally handled in construct_reply(),
157 * but as that calls switch_message, we can't use
158 * that here and must set up the chain info manually.
161 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
163 outsize += chain_size;
165 send_blocking_reply(outbuf,outsize);
168 /****************************************************************************
169 Return a generic lock fail error blocking call.
170 *****************************************************************************/
172 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
174 char *outbuf = OutBuffer;
175 char *inbuf = blr->inbuf;
176 construct_reply_common(inbuf, outbuf);
178 /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
179 FILE_LOCK_CONFLICT! (tridge) */
180 if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
181 status = NT_STATUS_FILE_LOCK_CONFLICT;
184 ERROR_NT(status);
185 if (!send_smb(smbd_server_fd(),outbuf))
186 exit_server("generic_blocking_lock_error: send_smb failed.");
189 /****************************************************************************
190 Return a lock fail error for a lockingX call. Undo all the locks we have
191 obtained first.
192 *****************************************************************************/
194 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
196 char *inbuf = blr->inbuf;
197 files_struct *fsp = blr->fsp;
198 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
199 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
200 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
201 uint16 lock_pid;
202 unsigned char locktype = CVAL(inbuf,smb_vwv3);
203 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
204 char *data;
205 int i;
207 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
210 * Data now points at the beginning of the list
211 * of smb_lkrng structs.
215 * Ensure we don't do a remove on the lock that just failed,
216 * as under POSIX rules, if we have a lock already there, we
217 * will delete it (and we shouldn't) .....
220 for(i = blr->lock_num - 1; i >= 0; i--) {
221 BOOL err;
223 lock_pid = get_lock_pid( data, i, large_file_format);
224 count = get_lock_count( data, i, large_file_format);
225 offset = get_lock_offset( data, i, large_file_format, &err);
228 * We know err cannot be set as if it was the lock
229 * request would never have been queued. JRA.
232 do_unlock(fsp,conn,lock_pid,count,offset);
235 generic_blocking_lock_error(blr, status);
238 /****************************************************************************
239 Return a lock fail error.
240 *****************************************************************************/
242 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
244 switch(blr->com_type) {
245 case SMBlock:
246 case SMBlockread:
247 generic_blocking_lock_error(blr, status);
248 break;
249 case SMBlockingX:
250 reply_lockingX_error(blr, status);
251 break;
252 default:
253 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
254 exit_server("PANIC - unknown type on blocking lock queue");
258 /****************************************************************************
259 Attempt to finish off getting all pending blocking locks for a lockread call.
260 Returns True if we want to be removed from the list.
261 *****************************************************************************/
263 static BOOL process_lockread(blocking_lock_record *blr)
265 char *outbuf = OutBuffer;
266 char *inbuf = blr->inbuf;
267 ssize_t nread = -1;
268 char *data, *p;
269 int outsize = 0;
270 SMB_BIG_UINT startpos;
271 size_t numtoread;
272 NTSTATUS status;
273 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
274 files_struct *fsp = blr->fsp;
276 numtoread = SVAL(inbuf,smb_vwv1);
277 startpos = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv2);
279 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
280 data = smb_buf(outbuf) + 3;
282 status = do_lock_spin( fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtoread,
283 startpos, READ_LOCK);
284 if (NT_STATUS_V(status)) {
285 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
286 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
288 * We have other than a "can't get lock"
289 * error. Send an error.
290 * Return True so we get dequeued.
292 generic_blocking_lock_error(blr, status);
293 return True;
297 * Still waiting for lock....
300 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
301 fsp->fsp_name));
302 return False;
305 nread = read_file(fsp,data,startpos,numtoread);
307 if (nread < 0) {
308 generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
309 return True;
312 construct_reply_common(inbuf, outbuf);
313 outsize = set_message(outbuf,5,0,True);
315 outsize += nread;
316 SSVAL(outbuf,smb_vwv0,nread);
317 SSVAL(outbuf,smb_vwv5,nread+3);
318 p = smb_buf(outbuf);
319 *p++ = 1;
320 SSVAL(p,0,nread); p += 2;
321 set_message_end(outbuf, p+nread);
323 DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
324 fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
326 send_blocking_reply(outbuf,outsize);
327 return True;
330 /****************************************************************************
331 Attempt to finish off getting all pending blocking locks for a lock call.
332 Returns True if we want to be removed from the list.
333 *****************************************************************************/
335 static BOOL process_lock(blocking_lock_record *blr)
337 char *outbuf = OutBuffer;
338 char *inbuf = blr->inbuf;
339 int outsize;
340 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
341 NTSTATUS status;
342 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
343 files_struct *fsp = blr->fsp;
345 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
346 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
348 errno = 0;
349 status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count,
350 offset, WRITE_LOCK);
351 if (NT_STATUS_IS_ERR(status)) {
352 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
353 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
355 * We have other than a "can't get lock"
356 * error. Send an error.
357 * Return True so we get dequeued.
360 blocking_lock_reply_error(blr, status);
361 return True;
364 * Still can't get the lock - keep waiting.
366 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
367 fsp->fsp_name));
368 return False;
372 * Success - we got the lock.
375 DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
376 fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
378 construct_reply_common(inbuf, outbuf);
379 outsize = set_message(outbuf,0,0,True);
380 send_blocking_reply(outbuf,outsize);
381 return True;
384 /****************************************************************************
385 Attempt to finish off getting all pending blocking locks for a lockingX call.
386 Returns True if we want to be removed from the list.
387 *****************************************************************************/
389 static BOOL process_lockingX(blocking_lock_record *blr)
391 char *inbuf = blr->inbuf;
392 unsigned char locktype = CVAL(inbuf,smb_vwv3);
393 files_struct *fsp = blr->fsp;
394 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
395 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
396 uint16 num_locks = SVAL(inbuf,smb_vwv7);
397 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
398 uint16 lock_pid;
399 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
400 char *data;
401 NTSTATUS status = NT_STATUS_OK;
403 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
406 * Data now points at the beginning of the list
407 * of smb_lkrng structs.
410 for(; blr->lock_num < num_locks; blr->lock_num++) {
411 BOOL err;
413 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
414 count = get_lock_count( data, blr->lock_num, large_file_format);
415 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
418 * We know err cannot be set as if it was the lock
419 * request would never have been queued. JRA.
421 errno = 0;
422 status = do_lock_spin(fsp,conn,lock_pid,count,offset,
423 ((locktype & 1) ? READ_LOCK : WRITE_LOCK));
424 if (NT_STATUS_IS_ERR(status)) break;
427 if(blr->lock_num == num_locks) {
429 * Success - we got all the locks.
432 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
433 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
435 reply_lockingX_success(blr);
436 return True;
437 } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
438 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
440 * We have other than a "can't get lock"
441 * error. Free any locks we had and return an error.
442 * Return True so we get dequeued.
445 blocking_lock_reply_error(blr, status);
446 return True;
450 * Still can't get all the locks - keep waiting.
453 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
454 Waiting....\n",
455 blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
457 return False;
460 /****************************************************************************
461 Process a blocking lock SMB.
462 Returns True if we want to be removed from the list.
463 *****************************************************************************/
465 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
467 switch(blr->com_type) {
468 case SMBlock:
469 return process_lock(blr);
470 case SMBlockread:
471 return process_lockread(blr);
472 case SMBlockingX:
473 return process_lockingX(blr);
474 default:
475 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
476 exit_server("PANIC - unknown type on blocking lock queue");
478 return False; /* Keep compiler happy. */
481 /****************************************************************************
482 Delete entries by fnum from the blocking lock pending queue.
483 *****************************************************************************/
485 void remove_pending_lock_requests_by_fid(files_struct *fsp)
487 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
488 blocking_lock_record *prev = NULL;
490 while(blr != NULL) {
491 if(blr->fsp->fnum == fsp->fnum) {
493 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
494 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
496 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
497 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
498 continue;
501 prev = blr;
502 blr = (blocking_lock_record *)ubi_slNext(blr);
506 /****************************************************************************
507 Delete entries by mid from the blocking lock pending queue. Always send reply.
508 *****************************************************************************/
510 void remove_pending_lock_requests_by_mid(int mid)
512 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
513 blocking_lock_record *prev = NULL;
515 while(blr != NULL) {
516 if(SVAL(blr->inbuf,smb_mid) == mid) {
517 files_struct *fsp = blr->fsp;
519 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
520 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
522 blocking_lock_reply_error(blr,NT_STATUS_CANCELLED);
523 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
524 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
525 continue;
528 prev = blr;
529 blr = (blocking_lock_record *)ubi_slNext(blr);
533 /****************************************************************************
534 Return the number of seconds to the next blocking locks timeout, or default_timeout.
535 *****************************************************************************/
537 BOOL blocking_locks_timeout(unsigned default_timeout)
539 unsigned timeout = default_timeout;
540 time_t t;
541 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst(&blocking_lock_queue);
543 /* note that we avoid the time() syscall if there are no blocking locks */
544 if (!blr) {
545 return timeout;
548 t = time(NULL);
550 while (blr) {
551 if (timeout > (blr->expire_time - t)) {
552 timeout = blr->expire_time - t;
554 blr = (blocking_lock_record *)ubi_slNext(blr);
557 if (timeout < 1) {
558 timeout = 1;
561 return timeout;
564 /****************************************************************************
565 Process the blocking lock queue. Note that this is only called as root.
566 *****************************************************************************/
568 void process_blocking_lock_queue(time_t t)
570 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
571 blocking_lock_record *prev = NULL;
573 if(blr == NULL)
574 return;
577 * Go through the queue and see if we can get any of the locks.
580 while(blr != NULL) {
581 connection_struct *conn = NULL;
582 uint16 vuid;
583 files_struct *fsp = NULL;
586 * Ensure we don't have any old chain_fsp values
587 * sitting around....
589 chain_size = 0;
590 file_chain_reset();
591 fsp = blr->fsp;
593 conn = conn_find(SVAL(blr->inbuf,smb_tid));
594 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
595 SVAL(blr->inbuf,smb_uid);
597 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
598 fsp->fnum, fsp->fsp_name ));
600 if((blr->expire_time != -1) && (blr->expire_time <= t)) {
602 * Lock expired - throw away all previously
603 * obtained locks and return lock error.
605 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
606 fsp->fnum, fsp->fsp_name ));
608 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
609 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
610 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
611 continue;
614 if(!change_to_user(conn,vuid)) {
615 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
616 vuid ));
618 * Remove the entry and return an error to the client.
620 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
621 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
622 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
623 continue;
626 if(!set_current_service(conn,True)) {
627 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
629 * Remove the entry and return an error to the client.
631 blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
632 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
633 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
634 change_to_root_user();
635 continue;
639 * Go through the remaining locks and try and obtain them.
640 * The call returns True if all locks were obtained successfully
641 * and False if we still need to wait.
644 if(blocking_lock_record_process(blr)) {
645 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
646 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
647 change_to_root_user();
648 continue;
651 change_to_root_user();
654 * Move to the next in the list.
656 prev = blr;
657 blr = (blocking_lock_record *)ubi_slNext(blr);