72d021d4e64b2f53f0289a488535c5cd6a740681
[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 /****************************************************************************
24  This is the structure to queue to implement blocking locks.
25  notify. It consists of the requesting SMB and the expiry time.
26 *****************************************************************************/
27
28 typedef struct _blocking_lock_record {
29         struct _blocking_lock_record *next;
30         struct _blocking_lock_record *prev;
31         int com_type;
32         files_struct *fsp;
33         time_t expire_time;
34         int lock_num;
35         SMB_BIG_UINT offset;
36         SMB_BIG_UINT count;
37         uint16 lock_pid;
38         char *inbuf;
39         int length;
40 } blocking_lock_record;
41
42 static blocking_lock_record *blocking_lock_queue;
43
44 /****************************************************************************
45  Destructor for the above structure.
46 ****************************************************************************/
47
48 static void free_blocking_lock_record(blocking_lock_record *blr)
49 {
50         DLIST_REMOVE(blocking_lock_queue, blr);
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, *tmp;
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 = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
108                 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
109                 return False;
110         }
111
112         if((blr->inbuf = (char *)SMB_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         DLIST_ADD_END(blocking_lock_queue, blr, tmp);
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 = get_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 = get_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 = get_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 = get_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, *next = NULL;
520
521         for(blr = blocking_lock_queue; blr; blr = next) {
522                 next = blr->next;
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(blr);
533                 }
534         }
535 }
536
537 /****************************************************************************
538  Delete entries by mid from the blocking lock pending queue. Always send reply.
539 *****************************************************************************/
540
541 void remove_pending_lock_requests_by_mid(int mid)
542 {
543         blocking_lock_record *blr, *next = NULL;
544
545         for(blr = blocking_lock_queue; blr; blr = next) {
546                 next = blr->next;
547                 if(SVAL(blr->inbuf,smb_mid) == mid) {
548                         files_struct *fsp = blr->fsp;
549
550                         DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
551 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
552
553                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
554                         brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
555                                 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
556                                 blr->offset, blr->count, True, NULL, NULL);
557                         free_blocking_lock_record(blr);
558                 }
559         }
560 }
561
562 /****************************************************************************
563   Set a flag as an unlock request affects one of our pending locks.
564 *****************************************************************************/
565
566 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len)
567 {
568         DEBUG(10,("received_unlock_msg\n"));
569         process_blocking_lock_queue(time(NULL));
570 }
571
572 /****************************************************************************
573  Return the number of seconds to the next blocking locks timeout, or default_timeout
574 *****************************************************************************/
575
576 unsigned blocking_locks_timeout(unsigned default_timeout)
577 {
578         unsigned timeout = default_timeout;
579         time_t t;
580         blocking_lock_record *blr = blocking_lock_queue;
581
582         /* note that we avoid the time() syscall if there are no blocking locks */
583         if (!blr)
584                 return timeout;
585
586         t = time(NULL);
587
588         for (; blr; blr = blr->next) {
589                 if ((blr->expire_time != (time_t)-1) &&
590                                         (timeout > (blr->expire_time - t))) {
591                         timeout = blr->expire_time - t;
592                 }
593         }
594
595         if (timeout < 1)
596                 timeout = 1;
597
598         return timeout;
599 }
600
601 /****************************************************************************
602  Process the blocking lock queue. Note that this is only called as root.
603 *****************************************************************************/
604
605 void process_blocking_lock_queue(time_t t)
606 {
607         blocking_lock_record *blr, *next = NULL;
608
609         /*
610          * Go through the queue and see if we can get any of the locks.
611          */
612
613         for (blr = blocking_lock_queue; blr; blr = next) {
614                 connection_struct *conn = NULL;
615                 uint16 vuid;
616                 files_struct *fsp = NULL;
617
618                 next = blr->next;
619
620                 /*
621                  * Ensure we don't have any old chain_fsp values
622                  * sitting around....
623                  */
624                 chain_size = 0;
625                 file_chain_reset();
626                 fsp = blr->fsp;
627
628                 conn = conn_find(SVAL(blr->inbuf,smb_tid));
629                 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
630                                 SVAL(blr->inbuf,smb_uid);
631
632                 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
633                         fsp->fnum, fsp->fsp_name ));
634
635                 if((blr->expire_time != -1) && (blr->expire_time <= t)) {
636                         /*
637                          * Lock expired - throw away all previously
638                          * obtained locks and return lock error.
639                          */
640                         DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
641                                 fsp->fnum, fsp->fsp_name ));
642
643                         brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
644                                 blr->lock_pid, sys_getpid(), conn->cnum,
645                                 blr->offset, blr->count, True, NULL, NULL);
646
647                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
648                         free_blocking_lock_record(blr);
649                         continue;
650                 }
651
652                 if(!change_to_user(conn,vuid)) {
653                         DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
654                                 vuid ));
655                         /*
656                          * Remove the entry and return an error to the client.
657                          */
658                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
659
660                         brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
661                                         blr->lock_pid, sys_getpid(), conn->cnum,
662                                         blr->offset, blr->count, True, NULL, NULL);
663
664                         free_blocking_lock_record(blr);
665                         continue;
666                 }
667
668                 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
669                         DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
670                         /*
671                          * Remove the entry and return an error to the client.
672                          */
673                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
674
675                         brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
676                                         blr->lock_pid, sys_getpid(), conn->cnum,
677                                         blr->offset, blr->count, True, NULL, NULL);
678
679                         free_blocking_lock_record(blr);
680                         change_to_root_user();
681                         continue;
682                 }
683
684                 /*
685                  * Go through the remaining locks and try and obtain them.
686                  * The call returns True if all locks were obtained successfully
687                  * and False if we still need to wait.
688                  */
689
690                 if(blocking_lock_record_process(blr)) {
691
692                         brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
693                                         blr->lock_pid, sys_getpid(), conn->cnum,
694                                         blr->offset, blr->count, True, NULL, NULL);
695
696                         free_blocking_lock_record(blr);
697                 }
698                 change_to_root_user();
699         }
700 }