This commit was manufactured by cvs2svn to create branch 'SAMBA_TNG'.
[Samba.git] / source / smbd / blocking.c
blob255e6453485913ce9128bf4e9b2ff62e123242bc
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Blocking Locking functions
5 Copyright (C) Jeremy Allison 1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #include "nterr.h"
25 extern int DEBUGLEVEL;
26 extern char *OutBuffer;
28 /****************************************************************************
29 This is the structure to queue to implement blocking locks.
30 notify. It consists of the requesting SMB and the expiry time.
31 *****************************************************************************/
33 typedef struct {
34 ubi_slNode msg_next;
35 int com_type;
36 files_struct *fsp;
37 time_t expire_time;
38 int lock_num;
39 char *inbuf;
40 int length;
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 free(blr->inbuf);
52 free((char *)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 /****************************************************************************
84 Function to push a blocking lock request onto the lock queue.
85 ****************************************************************************/
87 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout, int lock_num)
89 blocking_lock_record *blr;
91 if(in_chained_smb() ) {
92 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
93 return False;
97 * Now queue an entry on the blocking lock queue. We setup
98 * the expiration time here.
101 if((blr = (blocking_lock_record *)malloc(sizeof(blocking_lock_record))) == NULL) {
102 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
103 return False;
106 if((blr->inbuf = (char *)malloc(length)) == NULL) {
107 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
108 free((char *)blr);
109 return False;
112 blr->com_type = CVAL(inbuf,smb_com);
113 blr->fsp = get_fsp_from_pkt(inbuf);
114 blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
115 blr->lock_num = lock_num;
116 memcpy(blr->inbuf, inbuf, length);
117 blr->length = length;
119 ubi_slAddTail(&blocking_lock_queue, blr);
122 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d \
123 for fnum = %d, name = %s\n", length, (int)blr->expire_time,
124 blr->fsp->fnum, blr->fsp->fsp_name ));
126 return True;
129 /****************************************************************************
130 Return a smd with a given size.
131 *****************************************************************************/
133 static void send_blocking_reply(char *outbuf, int outsize)
135 if(outsize > 4)
136 smb_setlen(outbuf,outsize - 4);
138 send_smb(smbd_server_fd(),outbuf);
141 /****************************************************************************
142 Return a lockingX success SMB.
143 *****************************************************************************/
145 static void reply_lockingX_success(blocking_lock_record *blr)
147 char *outbuf = OutBuffer;
148 int bufsize = BUFFER_SIZE;
149 char *inbuf = blr->inbuf;
150 int outsize = 0;
152 construct_reply_common(inbuf, outbuf);
153 set_message(outbuf,2,0,True);
156 * As this message is a lockingX call we must handle
157 * any following chained message correctly.
158 * This is normally handled in construct_reply(),
159 * but as that calls switch_message, we can't use
160 * that here and must set up the chain info manually.
163 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
165 outsize += chain_size;
167 send_blocking_reply(outbuf,outsize);
170 /****************************************************************************
171 Return a generic lock fail error blocking call.
172 *****************************************************************************/
174 static void generic_blocking_lock_error(blocking_lock_record *blr, int eclass, int32 ecode)
176 char *outbuf = OutBuffer;
177 char *inbuf = blr->inbuf;
178 construct_reply_common(inbuf, outbuf);
180 if(eclass == 0) /* NT Error. */
181 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
183 ERROR(eclass,ecode);
184 send_smb(smbd_server_fd(),outbuf);
187 /****************************************************************************
188 Return a lock fail error for a lockingX call. Undo all the locks we have
189 obtained first.
190 *****************************************************************************/
192 static void reply_lockingX_error(blocking_lock_record *blr, int eclass, int32 ecode)
194 char *inbuf = blr->inbuf;
195 files_struct *fsp = blr->fsp;
196 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
197 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
198 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
199 unsigned char locktype = CVAL(inbuf,smb_vwv3);
200 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
201 char *data;
202 int i;
204 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
207 * Data now points at the beginning of the list
208 * of smb_lkrng structs.
212 * Ensure we don't do a remove on the lock that just failed,
213 * as under POSIX rules, if we have a lock already there, we
214 * will delete it (and we shouldn't) .....
217 for(i = blr->lock_num - 1; i >= 0; i--) {
218 int dummy1;
219 uint32 dummy2;
220 BOOL err;
222 count = get_lock_count( data, i, large_file_format);
223 offset = get_lock_offset( data, i, large_file_format, &err);
226 * We know err cannot be set as if it was the lock
227 * request would never have been queued. JRA.
230 do_unlock(fsp,conn,count,offset,&dummy1,&dummy2);
233 generic_blocking_lock_error(blr, eclass, ecode);
236 /****************************************************************************
237 Return a lock fail error.
238 *****************************************************************************/
240 static void blocking_lock_reply_error(blocking_lock_record *blr, int eclass, int32 ecode)
242 switch(blr->com_type) {
243 case SMBlock:
244 generic_blocking_lock_error(blr, eclass, ecode);
245 break;
246 case SMBlockread:
247 generic_blocking_lock_error(blr, eclass, ecode);
248 break;
249 case SMBlockingX:
250 reply_lockingX_error(blr, eclass, ecode);
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;
269 int outsize = 0;
270 SMB_OFF_T startpos;
271 size_t numtoread;
272 int eclass;
273 uint32 ecode;
274 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
275 files_struct *fsp = blr->fsp;
277 numtoread = SVAL(inbuf,smb_vwv1);
278 startpos = IVAL(inbuf,smb_vwv2);
280 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
281 data = smb_buf(outbuf) + 3;
283 if(!do_lock( fsp, conn, (SMB_BIG_UINT)numtoread, (SMB_BIG_UINT)startpos, READ_LOCK, &eclass, &ecode)) {
284 if((errno != EACCES) && (errno != EAGAIN)) {
286 * We have other than a "can't get lock" POSIX
287 * error. Send an error.
288 * Return True so we get dequeued.
291 generic_blocking_lock_error(blr, eclass, ecode);
292 return True;
296 * Still waiting for lock....
299 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
300 fsp->fsp_name));
301 return False;
304 nread = read_file(fsp,data,startpos,numtoread);
306 if (nread < 0) {
307 generic_blocking_lock_error(blr,ERRDOS,ERRnoaccess);
308 return True;
311 construct_reply_common(inbuf, outbuf);
312 outsize = set_message(outbuf,5,3,True);
314 outsize += nread;
315 SSVAL(outbuf,smb_vwv0,nread);
316 SSVAL(outbuf,smb_vwv5,nread+3);
317 SSVAL(smb_buf(outbuf),1,nread);
319 DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
320 fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
322 send_blocking_reply(outbuf,outsize);
323 return True;
326 /****************************************************************************
327 Attempt to finish off getting all pending blocking locks for a lock call.
328 Returns True if we want to be removed from the list.
329 *****************************************************************************/
331 static BOOL process_lock(blocking_lock_record *blr)
333 char *outbuf = OutBuffer;
334 char *inbuf = blr->inbuf;
335 int outsize;
336 SMB_OFF_T count = 0, offset = 0;
337 int eclass;
338 uint32 ecode;
339 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
340 files_struct *fsp = blr->fsp;
342 count = IVAL(inbuf,smb_vwv1);
343 offset = IVAL(inbuf,smb_vwv3);
345 errno = 0;
346 if (!do_lock(fsp, conn, (SMB_BIG_UINT)count, (SMB_BIG_UINT)offset, WRITE_LOCK, &eclass, &ecode)) {
347 if((errno != EACCES) && (errno != EAGAIN)) {
350 * We have other than a "can't get lock" POSIX
351 * error. Send an error.
352 * Return True so we get dequeued.
355 blocking_lock_reply_error(blr, eclass, ecode);
356 return True;
360 * Still can't get the lock - keep waiting.
363 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
364 fsp->fsp_name));
365 return False;
369 * Success - we got the lock.
372 DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
373 fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
375 construct_reply_common(inbuf, outbuf);
376 outsize = set_message(outbuf,0,0,True);
377 send_blocking_reply(outbuf,outsize);
378 return True;
381 /****************************************************************************
382 Attempt to finish off getting all pending blocking locks for a lockingX call.
383 Returns True if we want to be removed from the list.
384 *****************************************************************************/
386 static BOOL process_lockingX(blocking_lock_record *blr)
388 char *inbuf = blr->inbuf;
389 unsigned char locktype = CVAL(inbuf,smb_vwv3);
390 files_struct *fsp = blr->fsp;
391 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
392 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
393 uint16 num_locks = SVAL(inbuf,smb_vwv7);
394 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
395 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
396 char *data;
397 int eclass=0;
398 uint32 ecode=0;
400 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
403 * Data now points at the beginning of the list
404 * of smb_lkrng structs.
407 for(; blr->lock_num < num_locks; blr->lock_num++) {
408 BOOL err;
410 count = get_lock_count( data, blr->lock_num, large_file_format);
411 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
414 * We know err cannot be set as if it was the lock
415 * request would never have been queued. JRA.
417 errno = 0;
418 if(!do_lock(fsp,conn,count,offset, ((locktype & 1) ? READ_LOCK : WRITE_LOCK),
419 &eclass, &ecode))
420 break;
423 if(blr->lock_num == num_locks) {
426 * Success - we got all the locks.
429 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
430 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
432 reply_lockingX_success(blr);
433 return True;
435 } else if((errno != EACCES) && (errno != EAGAIN)) {
438 * We have other than a "can't get lock" POSIX
439 * error. Free any locks we had and return an error.
440 * Return True so we get dequeued.
443 blocking_lock_reply_error(blr, eclass, ecode);
444 return True;
448 * Still can't get all the locks - keep waiting.
451 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
452 Waiting....\n", blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
454 return False;
457 /****************************************************************************
458 Process a blocking lock SMB.
459 Returns True if we want to be removed from the list.
460 *****************************************************************************/
462 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
464 switch(blr->com_type) {
465 case SMBlock:
466 return process_lock(blr);
467 case SMBlockread:
468 return process_lockread(blr);
469 case SMBlockingX:
470 return process_lockingX(blr);
471 default:
472 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
473 exit_server("PANIC - unknown type on blocking lock queue");
475 return False; /* Keep compiler happy. */
478 /****************************************************************************
479 Delete entries by fnum from the blocking lock pending queue.
480 *****************************************************************************/
482 void remove_pending_lock_requests_by_fid(files_struct *fsp)
484 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
485 blocking_lock_record *prev = NULL;
487 while(blr != NULL) {
488 if(blr->fsp->fnum == fsp->fnum) {
490 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
491 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
493 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
494 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
495 continue;
498 prev = blr;
499 blr = (blocking_lock_record *)ubi_slNext(blr);
503 /****************************************************************************
504 Delete entries by mid from the blocking lock pending queue. Always send reply.
505 *****************************************************************************/
507 void remove_pending_lock_requests_by_mid(int mid)
509 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
510 blocking_lock_record *prev = NULL;
512 while(blr != NULL) {
513 if(SVAL(blr->inbuf,smb_mid) == mid) {
514 files_struct *fsp = blr->fsp;
516 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
517 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
519 blocking_lock_reply_error(blr,0,NT_STATUS_CANCELLED);
520 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
521 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
522 continue;
525 prev = blr;
526 blr = (blocking_lock_record *)ubi_slNext(blr);
530 /****************************************************************************
531 Return True if the blocking lock queue has entries.
532 *****************************************************************************/
534 BOOL blocking_locks_pending(void)
536 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
537 return (blr == NULL ? False : True);
540 /****************************************************************************
541 Process the blocking lock queue. Note that this is only called as root.
542 *****************************************************************************/
544 void process_blocking_lock_queue(time_t t)
546 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
547 blocking_lock_record *prev = NULL;
549 if(blr == NULL)
550 return;
553 * Go through the queue and see if we can get any of the locks.
556 while(blr != NULL) {
557 connection_struct *conn = NULL;
558 uint16 vuid;
559 files_struct *fsp = NULL;
562 * Ensure we don't have any old chain_fsp values
563 * sitting around....
565 chain_size = 0;
566 file_chain_reset();
567 fsp = blr->fsp;
569 conn = conn_find(SVAL(blr->inbuf,smb_tid));
570 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
571 SVAL(blr->inbuf,smb_uid);
573 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
574 fsp->fnum, fsp->fsp_name ));
576 if((blr->expire_time != -1) && (blr->expire_time > t)) {
578 * Lock expired - throw away all previously
579 * obtained locks and return lock error.
581 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
582 fsp->fnum, fsp->fsp_name ));
584 blocking_lock_reply_error(blr,ERRSRV,ERRaccess);
585 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
586 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
587 continue;
590 if(!become_user(conn,vuid)) {
591 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
592 vuid ));
594 * Remove the entry and return an error to the client.
596 blocking_lock_reply_error(blr,ERRSRV,ERRaccess);
597 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
598 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
599 continue;
602 if(!become_service(conn,True)) {
603 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
605 * Remove the entry and return an error to the client.
607 blocking_lock_reply_error(blr,ERRSRV,ERRaccess);
608 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
609 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
610 unbecome_user();
611 continue;
615 * Go through the remaining locks and try and obtain them.
616 * The call returns True if all locks were obtained successfully
617 * and False if we still need to wait.
620 if(blocking_lock_record_process(blr)) {
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 unbecome_user();
624 continue;
627 unbecome_user();
630 * Move to the next in the list.
632 prev = blr;
633 blr = (blocking_lock_record *)ubi_slNext(blr);