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