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