Commit an updated configure, but the build_farm should run autoconf!
[Samba.git] / source3 / smbd / blocking.c
blob48d3c8a24a8da19333e5a4ffe017ccc3984a4a7a
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 extern int DEBUGLEVEL;
24 extern char *OutBuffer;
26 /****************************************************************************
27 This is the structure to queue to implement blocking locks.
28 notify. It consists of the requesting SMB and the expiry time.
29 *****************************************************************************/
31 typedef struct {
32 ubi_slNode msg_next;
33 int com_type;
34 files_struct *fsp;
35 time_t expire_time;
36 int lock_num;
37 char *inbuf;
38 int length;
39 } blocking_lock_record;
41 static ubi_slList blocking_lock_queue = { NULL, (ubi_slNodePtr)&blocking_lock_queue, 0};
43 /****************************************************************************
44 Destructor for the above structure.
45 ****************************************************************************/
47 static void free_blocking_lock_record(blocking_lock_record *blr)
49 free(blr->inbuf);
50 free((char *)blr);
53 /****************************************************************************
54 Get the files_struct given a particular queued SMB.
55 *****************************************************************************/
57 static files_struct *get_fsp_from_pkt(char *inbuf)
59 switch(CVAL(inbuf,smb_com)) {
60 case SMBlock:
61 case SMBlockread:
62 return file_fsp(inbuf,smb_vwv0);
63 case SMBlockingX:
64 return file_fsp(inbuf,smb_vwv2);
65 default:
66 DEBUG(0,("get_fsp_from_pkt: PANIC - unknown type on blocking lock queue - exiting.!\n"));
67 exit_server("PANIC - unknown type on blocking lock queue");
69 return NULL; /* Keep compiler happy. */
72 /****************************************************************************
73 Determine if this is a secondary element of a chained SMB.
74 **************************************************************************/
76 static BOOL in_chained_smb(void)
78 return (chain_size != 0);
81 /****************************************************************************
82 Function to push a blocking lock request onto the lock queue.
83 ****************************************************************************/
85 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout, int lock_num)
87 blocking_lock_record *blr;
89 if(in_chained_smb() ) {
90 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
91 return False;
95 * Now queue an entry on the blocking lock queue. We setup
96 * the expiration time here.
99 if((blr = (blocking_lock_record *)malloc(sizeof(blocking_lock_record))) == NULL) {
100 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
101 return False;
104 if((blr->inbuf = (char *)malloc(length)) == NULL) {
105 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
106 free((char *)blr);
107 return False;
110 blr->com_type = CVAL(inbuf,smb_com);
111 blr->fsp = get_fsp_from_pkt(inbuf);
112 blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
113 blr->lock_num = lock_num;
114 memcpy(blr->inbuf, inbuf, length);
115 blr->length = length;
117 ubi_slAddTail(&blocking_lock_queue, blr);
120 DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
121 for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
122 blr->fsp->fnum, blr->fsp->fsp_name ));
124 return True;
127 /****************************************************************************
128 Return a smd with a given size.
129 *****************************************************************************/
131 static void send_blocking_reply(char *outbuf, int outsize)
133 if(outsize > 4)
134 smb_setlen(outbuf,outsize - 4);
136 if (!send_smb(smbd_server_fd(),outbuf))
137 exit_server("send_blocking_reply: send_smb failed.\n");
140 /****************************************************************************
141 Return a lockingX success SMB.
142 *****************************************************************************/
144 static void reply_lockingX_success(blocking_lock_record *blr)
146 char *outbuf = OutBuffer;
147 int bufsize = BUFFER_SIZE;
148 char *inbuf = blr->inbuf;
149 int outsize = 0;
151 construct_reply_common(inbuf, outbuf);
152 set_message(outbuf,2,0,True);
155 * As this message is a lockingX call we must handle
156 * any following chained message correctly.
157 * This is normally handled in construct_reply(),
158 * but as that calls switch_message, we can't use
159 * that here and must set up the chain info manually.
162 outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
164 outsize += chain_size;
166 send_blocking_reply(outbuf,outsize);
169 /****************************************************************************
170 Return a generic lock fail error blocking call.
171 *****************************************************************************/
173 static void generic_blocking_lock_error(blocking_lock_record *blr, int eclass, int32 ecode)
175 char *outbuf = OutBuffer;
176 char *inbuf = blr->inbuf;
177 construct_reply_common(inbuf, outbuf);
179 if(eclass == 0) /* NT Error. */
180 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
182 ERROR(eclass,ecode);
183 if (!send_smb(smbd_server_fd(),outbuf))
184 exit_server("generic_blocking_lock_error: send_smb failed.\n");
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 uint16 lock_pid;
200 unsigned char locktype = CVAL(inbuf,smb_vwv3);
201 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
202 char *data;
203 int i;
205 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
208 * Data now points at the beginning of the list
209 * of smb_lkrng structs.
213 * Ensure we don't do a remove on the lock that just failed,
214 * as under POSIX rules, if we have a lock already there, we
215 * will delete it (and we shouldn't) .....
218 for(i = blr->lock_num - 1; i >= 0; i--) {
219 int dummy1;
220 uint32 dummy2;
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,&dummy1,&dummy2);
235 generic_blocking_lock_error(blr, eclass, ecode);
238 /****************************************************************************
239 Return a lock fail error.
240 *****************************************************************************/
242 static void blocking_lock_reply_error(blocking_lock_record *blr, int eclass, int32 ecode)
244 switch(blr->com_type) {
245 case SMBlock:
246 generic_blocking_lock_error(blr, eclass, ecode);
247 break;
248 case SMBlockread:
249 generic_blocking_lock_error(blr, eclass, ecode);
250 break;
251 case SMBlockingX:
252 reply_lockingX_error(blr, eclass, ecode);
253 break;
254 default:
255 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
256 exit_server("PANIC - unknown type on blocking lock queue");
260 /****************************************************************************
261 Attempt to finish off getting all pending blocking locks for a lockread call.
262 Returns True if we want to be removed from the list.
263 *****************************************************************************/
265 static BOOL process_lockread(blocking_lock_record *blr)
267 char *outbuf = OutBuffer;
268 char *inbuf = blr->inbuf;
269 ssize_t nread = -1;
270 char *data, *p;
271 int outsize = 0;
272 SMB_OFF_T startpos;
273 size_t numtoread;
274 int eclass;
275 uint32 ecode;
276 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
277 files_struct *fsp = blr->fsp;
279 numtoread = SVAL(inbuf,smb_vwv1);
280 startpos = IVAL(inbuf,smb_vwv2);
282 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
283 data = smb_buf(outbuf) + 3;
285 if(!do_lock( fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtoread, (SMB_BIG_UINT)startpos, READ_LOCK, &eclass, &ecode)) {
286 if((errno != EACCES) && (errno != EAGAIN)) {
288 * We have other than a "can't get lock" POSIX
289 * error. Send an error.
290 * Return True so we get dequeued.
293 generic_blocking_lock_error(blr, eclass, ecode);
294 return True;
298 * Still waiting for lock....
301 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
302 fsp->fsp_name));
303 return False;
306 nread = read_file(fsp,data,startpos,numtoread);
308 if (nread < 0) {
309 generic_blocking_lock_error(blr,ERRDOS,ERRnoaccess);
310 return True;
313 construct_reply_common(inbuf, outbuf);
314 outsize = set_message(outbuf,5,0,True);
316 outsize += nread;
317 SSVAL(outbuf,smb_vwv0,nread);
318 SSVAL(outbuf,smb_vwv5,nread+3);
319 p = smb_buf(outbuf);
320 *p++ = 1;
321 SSVAL(p,0,nread); p += 2;
322 set_message_end(outbuf, p+nread);
324 DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
325 fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
327 send_blocking_reply(outbuf,outsize);
328 return True;
331 /****************************************************************************
332 Attempt to finish off getting all pending blocking locks for a lock call.
333 Returns True if we want to be removed from the list.
334 *****************************************************************************/
336 static BOOL process_lock(blocking_lock_record *blr)
338 char *outbuf = OutBuffer;
339 char *inbuf = blr->inbuf;
340 int outsize;
341 SMB_OFF_T count = 0, offset = 0;
342 int eclass;
343 uint32 ecode;
344 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
345 files_struct *fsp = blr->fsp;
347 count = IVAL(inbuf,smb_vwv1);
348 offset = IVAL(inbuf,smb_vwv3);
350 errno = 0;
351 if (!do_lock(fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)count, (SMB_BIG_UINT)offset, WRITE_LOCK, &eclass, &ecode)) {
352 if((errno != EACCES) && (errno != EAGAIN)) {
355 * We have other than a "can't get lock" POSIX
356 * error. Send an error.
357 * Return True so we get dequeued.
360 blocking_lock_reply_error(blr, eclass, ecode);
361 return True;
365 * Still can't get the lock - keep waiting.
368 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
369 fsp->fsp_name));
370 return False;
374 * Success - we got the lock.
377 DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
378 fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
380 construct_reply_common(inbuf, outbuf);
381 outsize = set_message(outbuf,0,0,True);
382 send_blocking_reply(outbuf,outsize);
383 return True;
386 /****************************************************************************
387 Attempt to finish off getting all pending blocking locks for a lockingX call.
388 Returns True if we want to be removed from the list.
389 *****************************************************************************/
391 static BOOL process_lockingX(blocking_lock_record *blr)
393 char *inbuf = blr->inbuf;
394 unsigned char locktype = CVAL(inbuf,smb_vwv3);
395 files_struct *fsp = blr->fsp;
396 connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
397 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
398 uint16 num_locks = SVAL(inbuf,smb_vwv7);
399 SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
400 uint16 lock_pid;
401 BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
402 char *data;
403 int eclass=0;
404 uint32 ecode=0;
406 data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
409 * Data now points at the beginning of the list
410 * of smb_lkrng structs.
413 for(; blr->lock_num < num_locks; blr->lock_num++) {
414 BOOL err;
416 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
417 count = get_lock_count( data, blr->lock_num, large_file_format);
418 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
421 * We know err cannot be set as if it was the lock
422 * request would never have been queued. JRA.
424 errno = 0;
425 if(!do_lock(fsp,conn,count,lock_pid,offset, ((locktype & 1) ? READ_LOCK : WRITE_LOCK),
426 &eclass, &ecode))
427 break;
430 if(blr->lock_num == num_locks) {
433 * Success - we got all the locks.
436 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
437 fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
439 reply_lockingX_success(blr);
440 return True;
442 } else if((errno != EACCES) && (errno != EAGAIN)) {
445 * We have other than a "can't get lock" POSIX
446 * error. Free any locks we had and return an error.
447 * Return True so we get dequeued.
450 blocking_lock_reply_error(blr, eclass, ecode);
451 return True;
455 * Still can't get all the locks - keep waiting.
458 DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
459 Waiting....\n", blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
461 return False;
464 /****************************************************************************
465 Process a blocking lock SMB.
466 Returns True if we want to be removed from the list.
467 *****************************************************************************/
469 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
471 switch(blr->com_type) {
472 case SMBlock:
473 return process_lock(blr);
474 case SMBlockread:
475 return process_lockread(blr);
476 case SMBlockingX:
477 return process_lockingX(blr);
478 default:
479 DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
480 exit_server("PANIC - unknown type on blocking lock queue");
482 return False; /* Keep compiler happy. */
485 /****************************************************************************
486 Delete entries by fnum from the blocking lock pending queue.
487 *****************************************************************************/
489 void remove_pending_lock_requests_by_fid(files_struct *fsp)
491 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
492 blocking_lock_record *prev = NULL;
494 while(blr != NULL) {
495 if(blr->fsp->fnum == fsp->fnum) {
497 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
498 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
500 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
501 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
502 continue;
505 prev = blr;
506 blr = (blocking_lock_record *)ubi_slNext(blr);
510 /****************************************************************************
511 Delete entries by mid from the blocking lock pending queue. Always send reply.
512 *****************************************************************************/
514 void remove_pending_lock_requests_by_mid(int mid)
516 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
517 blocking_lock_record *prev = NULL;
519 while(blr != NULL) {
520 if(SVAL(blr->inbuf,smb_mid) == mid) {
521 files_struct *fsp = blr->fsp;
523 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
524 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
526 blocking_lock_reply_error(blr,0,NT_STATUS_CANCELLED);
527 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
528 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
529 continue;
532 prev = blr;
533 blr = (blocking_lock_record *)ubi_slNext(blr);
537 /****************************************************************************
538 Return True if the blocking lock queue has entries.
539 *****************************************************************************/
541 BOOL blocking_locks_pending(void)
543 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
544 return (blr == NULL ? False : True);
547 /****************************************************************************
548 Process the blocking lock queue. Note that this is only called as root.
549 *****************************************************************************/
551 void process_blocking_lock_queue(time_t t)
553 blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
554 blocking_lock_record *prev = NULL;
556 if(blr == NULL)
557 return;
560 * Go through the queue and see if we can get any of the locks.
563 while(blr != NULL) {
564 connection_struct *conn = NULL;
565 uint16 vuid;
566 files_struct *fsp = NULL;
569 * Ensure we don't have any old chain_fsp values
570 * sitting around....
572 chain_size = 0;
573 file_chain_reset();
574 fsp = blr->fsp;
576 conn = conn_find(SVAL(blr->inbuf,smb_tid));
577 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
578 SVAL(blr->inbuf,smb_uid);
580 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
581 fsp->fnum, fsp->fsp_name ));
583 if((blr->expire_time != -1) && (blr->expire_time > t)) {
585 * Lock expired - throw away all previously
586 * obtained locks and return lock error.
588 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
589 fsp->fnum, fsp->fsp_name ));
591 blocking_lock_reply_error(blr,ERRSRV,ERRaccess);
592 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
593 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
594 continue;
597 if(!become_user(conn,vuid)) {
598 DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
599 vuid ));
601 * Remove the entry and return an error to the client.
603 blocking_lock_reply_error(blr,ERRSRV,ERRaccess);
604 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
605 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
606 continue;
609 if(!become_service(conn,True)) {
610 DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
612 * Remove the entry and return an error to the client.
614 blocking_lock_reply_error(blr,ERRSRV,ERRaccess);
615 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
616 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
617 unbecome_user();
618 continue;
622 * Go through the remaining locks and try and obtain them.
623 * The call returns True if all locks were obtained successfully
624 * and False if we still need to wait.
627 if(blocking_lock_record_process(blr)) {
628 free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
629 blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
630 unbecome_user();
631 continue;
634 unbecome_user();
637 * Move to the next in the list.
639 prev = blr;
640 blr = (blocking_lock_record *)ubi_slNext(blr);