import HEAD into svn+ssh://svn.samba.org/home/svn/samba/trunk
[metze/old/v3-2-winbind-ndr.git] / source / smbd / blocking.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Blocking Locking functions
4    Copyright (C) Jeremy Allison 1998-2003
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23 extern char *OutBuffer;
24
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 *****************************************************************************/
29
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         SMB_BIG_UINT offset;
37         SMB_BIG_UINT count;
38         uint16 lock_pid;
39         char *inbuf;
40         int length;
41 } blocking_lock_record;
42
43 static ubi_slList blocking_lock_queue = { NULL, (ubi_slNodePtr)&blocking_lock_queue, 0};
44
45 /****************************************************************************
46  Destructor for the above structure.
47 ****************************************************************************/
48
49 static void free_blocking_lock_record(blocking_lock_record *blr)
50 {
51         SAFE_FREE(blr->inbuf);
52         SAFE_FREE(blr);
53 }
54
55 /****************************************************************************
56  Get the files_struct given a particular queued SMB.
57 *****************************************************************************/
58
59 static files_struct *get_fsp_from_pkt(char *inbuf)
60 {
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");
70         }
71         return NULL; /* Keep compiler happy. */
72 }
73
74 /****************************************************************************
75  Determine if this is a secondary element of a chained SMB.
76   **************************************************************************/
77
78 static BOOL in_chained_smb(void)
79 {
80         return (chain_size != 0);
81 }
82
83 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len);
84
85 /****************************************************************************
86  Function to push a blocking lock request onto the lock queue.
87 ****************************************************************************/
88
89 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout,
90                 int lock_num, uint16 lock_pid, SMB_BIG_UINT offset, SMB_BIG_UINT count)
91 {
92         static BOOL set_lock_msg;
93         blocking_lock_record *blr;
94         BOOL my_lock_ctx = False;
95         NTSTATUS status;
96
97         if(in_chained_smb() ) {
98                 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
99                 return False;
100         }
101
102         /*
103          * Now queue an entry on the blocking lock queue. We setup
104          * the expiration time here.
105          */
106
107         if((blr = (blocking_lock_record *)malloc(sizeof(blocking_lock_record))) == NULL) {
108                 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
109                 return False;
110         }
111
112         if((blr->inbuf = (char *)malloc(length)) == NULL) {
113                 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
114                 SAFE_FREE(blr);
115                 return False;
116         }
117
118         blr->com_type = CVAL(inbuf,smb_com);
119         blr->fsp = get_fsp_from_pkt(inbuf);
120         blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
121         blr->lock_num = lock_num;
122         blr->lock_pid = lock_pid;
123         blr->offset = offset;
124         blr->count = count;
125         memcpy(blr->inbuf, inbuf, length);
126         blr->length = length;
127
128         /* Add a pending lock record for this. */
129         status = brl_lock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
130                         lock_pid, sys_getpid(), blr->fsp->conn->cnum,
131                         offset, count, PENDING_LOCK, &my_lock_ctx);
132
133         if (!NT_STATUS_IS_OK(status)) {
134                 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
135                 free_blocking_lock_record(blr);
136                 return False;
137         }
138
139         ubi_slAddTail(&blocking_lock_queue, blr);
140
141         /* Ensure we'll receive messages when this is unlocked. */
142         if (!set_lock_msg) {
143                 message_register(MSG_SMB_UNLOCK, received_unlock_msg);
144                 set_lock_msg = True;
145         }
146
147         DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
148 for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
149                 blr->fsp->fnum, blr->fsp->fsp_name ));
150
151         /* Push the MID of this packet on the signing queue. */
152         srv_defer_sign_response(SVAL(inbuf,smb_mid));
153
154         return True;
155 }
156
157 /****************************************************************************
158  Return a smd with a given size.
159 *****************************************************************************/
160
161 static void send_blocking_reply(char *outbuf, int outsize)
162 {
163         if(outsize > 4)
164                 smb_setlen(outbuf,outsize - 4);
165
166         if (!send_smb(smbd_server_fd(),outbuf))
167                 exit_server("send_blocking_reply: send_smb failed.");
168 }
169
170 /****************************************************************************
171  Return a lockingX success SMB.
172 *****************************************************************************/
173
174 static void reply_lockingX_success(blocking_lock_record *blr)
175 {
176         char *outbuf = OutBuffer;
177         int bufsize = BUFFER_SIZE;
178         char *inbuf = blr->inbuf;
179         int outsize = 0;
180
181         construct_reply_common(inbuf, outbuf);
182         set_message(outbuf,2,0,True);
183
184         /*
185          * As this message is a lockingX call we must handle
186          * any following chained message correctly.
187          * This is normally handled in construct_reply(),
188          * but as that calls switch_message, we can't use
189          * that here and must set up the chain info manually.
190          */
191
192         outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
193
194         outsize += chain_size;
195
196         send_blocking_reply(outbuf,outsize);
197 }
198
199 /****************************************************************************
200  Return a generic lock fail error blocking call.
201 *****************************************************************************/
202
203 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
204 {
205         char *outbuf = OutBuffer;
206         char *inbuf = blr->inbuf;
207         construct_reply_common(inbuf, outbuf);
208
209         /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
210            FILE_LOCK_CONFLICT! (tridge) */
211         if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
212                 status = NT_STATUS_FILE_LOCK_CONFLICT;
213         }
214
215         ERROR_NT(status);
216         if (!send_smb(smbd_server_fd(),outbuf))
217                 exit_server("generic_blocking_lock_error: send_smb failed.");
218 }
219
220 /****************************************************************************
221  Return a lock fail error for a lockingX call. Undo all the locks we have 
222  obtained first.
223 *****************************************************************************/
224
225 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
226 {
227         char *inbuf = blr->inbuf;
228         files_struct *fsp = blr->fsp;
229         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
230         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
231         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
232         uint16 lock_pid;
233         unsigned char locktype = CVAL(inbuf,smb_vwv3);
234         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
235         char *data;
236         int i;
237
238         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
239         
240         /* 
241          * Data now points at the beginning of the list
242          * of smb_lkrng structs.
243          */
244
245         /*
246          * Ensure we don't do a remove on the lock that just failed,
247          * as under POSIX rules, if we have a lock already there, we
248          * will delete it (and we shouldn't) .....
249          */
250         
251         for(i = blr->lock_num - 1; i >= 0; i--) {
252                 BOOL err;
253                 
254                 lock_pid = get_lock_pid( data, i, large_file_format);
255                 count = get_lock_count( data, i, large_file_format);
256                 offset = get_lock_offset( data, i, large_file_format, &err);
257                 
258                 /*
259                  * We know err cannot be set as if it was the lock
260                  * request would never have been queued. JRA.
261                  */
262                 
263                 do_unlock(fsp,conn,lock_pid,count,offset);
264         }
265         
266         generic_blocking_lock_error(blr, status);
267 }
268
269 /****************************************************************************
270  Return a lock fail error.
271 *****************************************************************************/
272
273 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
274 {
275         switch(blr->com_type) {
276         case SMBlock:
277         case SMBlockread:
278                 generic_blocking_lock_error(blr, status);
279                 break;
280         case SMBlockingX:
281                 reply_lockingX_error(blr, status);
282                 break;
283         default:
284                 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
285                 exit_server("PANIC - unknown type on blocking lock queue");
286         }
287 }
288
289 /****************************************************************************
290  Attempt to finish off getting all pending blocking locks for a lockread call.
291  Returns True if we want to be removed from the list.
292 *****************************************************************************/
293
294 static BOOL process_lockread(blocking_lock_record *blr)
295 {
296         char *outbuf = OutBuffer;
297         char *inbuf = blr->inbuf;
298         ssize_t nread = -1;
299         char *data, *p;
300         int outsize = 0;
301         SMB_BIG_UINT startpos;
302         size_t numtoread;
303         NTSTATUS status;
304         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
305         files_struct *fsp = blr->fsp;
306         BOOL my_lock_ctx = False;
307
308         numtoread = SVAL(inbuf,smb_vwv1);
309         startpos = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv2);
310         
311         numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
312         data = smb_buf(outbuf) + 3;
313  
314         status = do_lock_spin( fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtoread, startpos, READ_LOCK, &my_lock_ctx);
315         if (NT_STATUS_V(status)) {
316                 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
317                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
318                         /*
319                          * We have other than a "can't get lock"
320                          * error. Send an error.
321                          * Return True so we get dequeued.
322                          */
323                         generic_blocking_lock_error(blr, status);
324                         return True;
325                 }
326
327                 /*
328                  * Still waiting for lock....
329                  */
330                 
331                 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
332                           fsp->fsp_name));
333                 return False;
334         }
335
336         nread = read_file(fsp,data,startpos,numtoread);
337
338         if (nread < 0) {
339                 generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
340                 return True;
341         }
342         
343         construct_reply_common(inbuf, outbuf);
344         outsize = set_message(outbuf,5,0,True);
345         
346         outsize += nread;
347         SSVAL(outbuf,smb_vwv0,nread);
348         SSVAL(outbuf,smb_vwv5,nread+3);
349         p = smb_buf(outbuf);
350         *p++ = 1;
351         SSVAL(p,0,nread); p += 2;
352         set_message_end(outbuf, p+nread);
353         
354         DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
355                    fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
356         
357         send_blocking_reply(outbuf,outsize);
358         return True;
359 }
360
361 /****************************************************************************
362  Attempt to finish off getting all pending blocking locks for a lock call.
363  Returns True if we want to be removed from the list.
364 *****************************************************************************/
365
366 static BOOL process_lock(blocking_lock_record *blr)
367 {
368         char *outbuf = OutBuffer;
369         char *inbuf = blr->inbuf;
370         int outsize;
371         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
372         NTSTATUS status;
373         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
374         files_struct *fsp = blr->fsp;
375         BOOL my_lock_ctx = False;
376
377         count = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
378         offset = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
379
380         errno = 0;
381         status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK, &my_lock_ctx);
382         if (NT_STATUS_IS_ERR(status)) {
383                 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
384                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
385                         /*
386                          * We have other than a "can't get lock"
387                          * error. Send an error.
388                          * Return True so we get dequeued.
389                          */
390                         
391                         blocking_lock_reply_error(blr, status);
392                         return True;
393                 }
394                 /*
395                  * Still can't get the lock - keep waiting.
396                  */
397                 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
398                           fsp->fsp_name));
399                 return False;
400         }
401
402         /*
403          * Success - we got the lock.
404          */
405         
406         DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
407                  fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
408         
409         construct_reply_common(inbuf, outbuf);
410         outsize = set_message(outbuf,0,0,True);
411         send_blocking_reply(outbuf,outsize);
412         return True;
413 }
414
415 /****************************************************************************
416  Attempt to finish off getting all pending blocking locks for a lockingX call.
417  Returns True if we want to be removed from the list.
418 *****************************************************************************/
419
420 static BOOL process_lockingX(blocking_lock_record *blr)
421 {
422         char *inbuf = blr->inbuf;
423         unsigned char locktype = CVAL(inbuf,smb_vwv3);
424         files_struct *fsp = blr->fsp;
425         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
426         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
427         uint16 num_locks = SVAL(inbuf,smb_vwv7);
428         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
429         uint16 lock_pid;
430         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
431         char *data;
432         BOOL my_lock_ctx = False;
433         NTSTATUS status = NT_STATUS_OK;
434
435         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
436
437         /* 
438          * Data now points at the beginning of the list
439          * of smb_lkrng structs.
440          */
441         
442         for(; blr->lock_num < num_locks; blr->lock_num++) {
443                 BOOL err;
444
445                 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
446                 count = get_lock_count( data, blr->lock_num, large_file_format);
447                 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
448                 
449                 /*
450                  * We know err cannot be set as if it was the lock
451                  * request would never have been queued. JRA.
452                  */
453                 errno = 0;
454                 status = do_lock_spin(fsp,conn,lock_pid,count,offset, 
455                                  ((locktype & 1) ? READ_LOCK : WRITE_LOCK), &my_lock_ctx);
456                 if (NT_STATUS_IS_ERR(status)) break;
457         }
458
459         if(blr->lock_num == num_locks) {
460                 /*
461                  * Success - we got all the locks.
462                  */
463                 
464                 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
465                          fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
466
467                 reply_lockingX_success(blr);
468                 return True;
469         } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
470                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
471                         /*
472                          * We have other than a "can't get lock"
473                          * error. Free any locks we had and return an error.
474                          * Return True so we get dequeued.
475                          */
476                 
477                 blocking_lock_reply_error(blr, status);
478                 return True;
479         }
480
481         /*
482          * Still can't get all the locks - keep waiting.
483          */
484         
485         DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
486 Waiting....\n", 
487                   blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
488         
489         return False;
490 }
491
492 /****************************************************************************
493  Process a blocking lock SMB.
494  Returns True if we want to be removed from the list.
495 *****************************************************************************/
496
497 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
498 {
499         switch(blr->com_type) {
500                 case SMBlock:
501                         return process_lock(blr);
502                 case SMBlockread:
503                         return process_lockread(blr);
504                 case SMBlockingX:
505                         return process_lockingX(blr);
506                 default:
507                         DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
508                         exit_server("PANIC - unknown type on blocking lock queue");
509         }
510         return False; /* Keep compiler happy. */
511 }
512
513 /****************************************************************************
514  Delete entries by fnum from the blocking lock pending queue.
515 *****************************************************************************/
516
517 void remove_pending_lock_requests_by_fid(files_struct *fsp)
518 {
519         blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
520         blocking_lock_record *prev = NULL;
521
522         while(blr != NULL) {
523                 if(blr->fsp->fnum == fsp->fnum) {
524
525                         DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
526 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
527
528                         brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
529                                 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
530                                 blr->offset, blr->count, True, NULL, NULL);
531
532                         free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
533                         blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
534                         continue;
535                 }
536
537                 prev = blr;
538                 blr = (blocking_lock_record *)ubi_slNext(blr);
539         }
540 }
541
542 /****************************************************************************
543  Delete entries by mid from the blocking lock pending queue. Always send reply.
544 *****************************************************************************/
545
546 void remove_pending_lock_requests_by_mid(int mid)
547 {
548         blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
549         blocking_lock_record *prev = NULL;
550
551         while(blr != NULL) {
552                 if(SVAL(blr->inbuf,smb_mid) == mid) {
553                         files_struct *fsp = blr->fsp;
554
555                         DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
556 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
557
558                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
559                         brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
560                                 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
561                                 blr->offset, blr->count, True, NULL, NULL);
562                         free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
563                         blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
564                         continue;
565                 }
566
567                 prev = blr;
568                 blr = (blocking_lock_record *)ubi_slNext(blr);
569         }
570 }
571
572 /****************************************************************************
573   Set a flag as an unlock request affects one of our pending locks.
574 *****************************************************************************/
575
576 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len)
577 {
578         DEBUG(10,("received_unlock_msg\n"));
579         process_blocking_lock_queue(time(NULL));
580 }
581
582 /****************************************************************************
583  Return the number of seconds to the next blocking locks timeout, or default_timeout
584 *****************************************************************************/
585
586 unsigned blocking_locks_timeout(unsigned default_timeout)
587 {
588         unsigned timeout = default_timeout;
589         time_t t;
590         blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst(&blocking_lock_queue);
591
592         /* note that we avoid the time() syscall if there are no blocking locks */
593         if (!blr)
594                 return timeout;
595
596         t = time(NULL);
597
598         while (blr) {
599                 if ((blr->expire_time != (time_t)-1) &&
600                                         (timeout > (blr->expire_time - t))) {
601                         timeout = blr->expire_time - t;
602                 }
603                 blr = (blocking_lock_record *)ubi_slNext(blr);
604         }
605
606         if (timeout < 1)
607                 timeout = 1;
608
609         return timeout;
610 }
611
612 /****************************************************************************
613  Process the blocking lock queue. Note that this is only called as root.
614 *****************************************************************************/
615
616 void process_blocking_lock_queue(time_t t)
617 {
618         blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
619         blocking_lock_record *prev = NULL;
620
621         if(blr == NULL)
622                 return;
623
624         /*
625          * Go through the queue and see if we can get any of the locks.
626          */
627
628         while(blr != NULL) {
629                 connection_struct *conn = NULL;
630                 uint16 vuid;
631                 files_struct *fsp = NULL;
632
633                 /*
634                  * Ensure we don't have any old chain_fsp values
635                  * sitting around....
636                  */
637                 chain_size = 0;
638                 file_chain_reset();
639                 fsp = blr->fsp;
640
641                 conn = conn_find(SVAL(blr->inbuf,smb_tid));
642                 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
643                                 SVAL(blr->inbuf,smb_uid);
644
645                 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
646                         fsp->fnum, fsp->fsp_name ));
647
648                 if((blr->expire_time != -1) && (blr->expire_time <= t)) {
649                         /*
650                          * Lock expired - throw away all previously
651                          * obtained locks and return lock error.
652                          */
653                         DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
654                                 fsp->fnum, fsp->fsp_name ));
655
656                         brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
657                                 blr->lock_pid, sys_getpid(), conn->cnum,
658                                 blr->offset, blr->count, True, NULL, NULL);
659
660                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
661                         free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
662                         blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
663                         continue;
664                 }
665
666                 if(!change_to_user(conn,vuid)) {
667                         DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
668                                 vuid ));
669                         /*
670                          * Remove the entry and return an error to the client.
671                          */
672                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
673
674                         brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
675                                         blr->lock_pid, sys_getpid(), conn->cnum,
676                                         blr->offset, blr->count, True, NULL, NULL);
677
678                         free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
679                         blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
680                         continue;
681                 }
682
683                 if(!set_current_service(conn,True)) {
684                         DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
685                         /*
686                          * Remove the entry and return an error to the client.
687                          */
688                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
689
690                         brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
691                                         blr->lock_pid, sys_getpid(), conn->cnum,
692                                         blr->offset, blr->count, True, NULL, NULL);
693
694                         free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
695                         blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
696                         change_to_root_user();
697                         continue;
698                 }
699
700                 /*
701                  * Go through the remaining locks and try and obtain them.
702                  * The call returns True if all locks were obtained successfully
703                  * and False if we still need to wait.
704                  */
705
706                 if(blocking_lock_record_process(blr)) {
707
708                         brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
709                                         blr->lock_pid, sys_getpid(), conn->cnum,
710                                         blr->offset, blr->count, True, NULL, NULL);
711
712                         free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
713                         blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
714                         change_to_root_user();
715                         continue;
716                 }
717
718                 change_to_root_user();
719
720                 /*
721                  * Move to the next in the list.
722                  */
723                 prev = blr;
724                 blr = (blocking_lock_record *)ubi_slNext(blr);
725         }
726 }