e143999a785d2d38c7f3dcafcdf01740a35df2ae
[abartlet/samba.git/.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 _blocking_lock_record {
31         struct _blocking_lock_record *next;
32         struct _blocking_lock_record *prev;
33         int com_type;
34         files_struct *fsp;
35         time_t expire_time;
36         int lock_num;
37         SMB_BIG_UINT offset;
38         SMB_BIG_UINT count;
39         uint16 lock_pid;
40         char *inbuf;
41         int length;
42 } blocking_lock_record;
43
44 static blocking_lock_record *blocking_lock_queue;
45
46 /****************************************************************************
47  Destructor for the above structure.
48 ****************************************************************************/
49
50 static void free_blocking_lock_record(blocking_lock_record *blr)
51 {
52         DLIST_REMOVE(blocking_lock_queue, blr);
53         SAFE_FREE(blr->inbuf);
54         SAFE_FREE(blr);
55 }
56
57 /****************************************************************************
58  Get the files_struct given a particular queued SMB.
59 *****************************************************************************/
60
61 static files_struct *get_fsp_from_pkt(char *inbuf)
62 {
63         switch(CVAL(inbuf,smb_com)) {
64                 case SMBlock:
65                 case SMBlockread:
66                         return file_fsp(inbuf,smb_vwv0);
67                 case SMBlockingX:
68                         return file_fsp(inbuf,smb_vwv2);
69                 default:
70                         DEBUG(0,("get_fsp_from_pkt: PANIC - unknown type on blocking lock queue - exiting.!\n"));
71                         exit_server("PANIC - unknown type on blocking lock queue");
72         }
73         return NULL; /* Keep compiler happy. */
74 }
75
76 /****************************************************************************
77  Determine if this is a secondary element of a chained SMB.
78   **************************************************************************/
79
80 static BOOL in_chained_smb(void)
81 {
82         return (chain_size != 0);
83 }
84
85 static void received_unlock_msg(int msg_type, pid_t src, void *buf, size_t len);
86
87 /****************************************************************************
88  Function to push a blocking lock request onto the lock queue.
89 ****************************************************************************/
90
91 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout,
92                 int lock_num, uint16 lock_pid, SMB_BIG_UINT offset, SMB_BIG_UINT count)
93 {
94         static BOOL set_lock_msg;
95         blocking_lock_record *blr, *tmp;
96         BOOL my_lock_ctx = False;
97         NTSTATUS status;
98
99         if(in_chained_smb() ) {
100                 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
101                 return False;
102         }
103
104         /*
105          * Now queue an entry on the blocking lock queue. We setup
106          * the expiration time here.
107          */
108
109         if((blr = (blocking_lock_record *)malloc(sizeof(blocking_lock_record))) == NULL) {
110                 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
111                 return False;
112         }
113
114         if((blr->inbuf = (char *)malloc(length)) == NULL) {
115                 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
116                 SAFE_FREE(blr);
117                 return False;
118         }
119
120         blr->com_type = CVAL(inbuf,smb_com);
121         blr->fsp = get_fsp_from_pkt(inbuf);
122         blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
123         blr->lock_num = lock_num;
124         blr->lock_pid = lock_pid;
125         blr->offset = offset;
126         blr->count = count;
127         memcpy(blr->inbuf, inbuf, length);
128         blr->length = length;
129
130         /* Add a pending lock record for this. */
131         status = brl_lock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
132                         lock_pid, sys_getpid(), blr->fsp->conn->cnum,
133                         offset, count, PENDING_LOCK, &my_lock_ctx);
134
135         if (!NT_STATUS_IS_OK(status)) {
136                 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
137                 free_blocking_lock_record(blr);
138                 return False;
139         }
140
141         DLIST_ADD_END(blocking_lock_queue, blr, tmp);
142
143         /* Ensure we'll receive messages when this is unlocked. */
144         if (!set_lock_msg) {
145                 message_register(MSG_SMB_UNLOCK, received_unlock_msg);
146                 set_lock_msg = True;
147         }
148
149         DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
150 for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
151                 blr->fsp->fnum, blr->fsp->fsp_name ));
152
153         /* Push the MID of this packet on the signing queue. */
154         srv_defer_sign_response(SVAL(inbuf,smb_mid));
155
156         return True;
157 }
158
159 /****************************************************************************
160  Return a smd with a given size.
161 *****************************************************************************/
162
163 static void send_blocking_reply(char *outbuf, int outsize)
164 {
165         if(outsize > 4)
166                 smb_setlen(outbuf,outsize - 4);
167
168         if (!send_smb(smbd_server_fd(),outbuf))
169                 exit_server("send_blocking_reply: send_smb failed.");
170 }
171
172 /****************************************************************************
173  Return a lockingX success SMB.
174 *****************************************************************************/
175
176 static void reply_lockingX_success(blocking_lock_record *blr)
177 {
178         char *outbuf = OutBuffer;
179         int bufsize = BUFFER_SIZE;
180         char *inbuf = blr->inbuf;
181         int outsize = 0;
182
183         construct_reply_common(inbuf, outbuf);
184         set_message(outbuf,2,0,True);
185
186         /*
187          * As this message is a lockingX call we must handle
188          * any following chained message correctly.
189          * This is normally handled in construct_reply(),
190          * but as that calls switch_message, we can't use
191          * that here and must set up the chain info manually.
192          */
193
194         outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
195
196         outsize += chain_size;
197
198         send_blocking_reply(outbuf,outsize);
199 }
200
201 /****************************************************************************
202  Return a generic lock fail error blocking call.
203 *****************************************************************************/
204
205 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
206 {
207         char *outbuf = OutBuffer;
208         char *inbuf = blr->inbuf;
209         construct_reply_common(inbuf, outbuf);
210
211         /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
212            FILE_LOCK_CONFLICT! (tridge) */
213         if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
214                 status = NT_STATUS_FILE_LOCK_CONFLICT;
215         }
216
217         ERROR_NT(status);
218         if (!send_smb(smbd_server_fd(),outbuf))
219                 exit_server("generic_blocking_lock_error: send_smb failed.");
220 }
221
222 /****************************************************************************
223  Return a lock fail error for a lockingX call. Undo all the locks we have 
224  obtained first.
225 *****************************************************************************/
226
227 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
228 {
229         char *inbuf = blr->inbuf;
230         files_struct *fsp = blr->fsp;
231         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
232         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
233         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
234         uint16 lock_pid;
235         unsigned char locktype = CVAL(inbuf,smb_vwv3);
236         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
237         char *data;
238         int i;
239
240         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
241         
242         /* 
243          * Data now points at the beginning of the list
244          * of smb_lkrng structs.
245          */
246
247         /*
248          * Ensure we don't do a remove on the lock that just failed,
249          * as under POSIX rules, if we have a lock already there, we
250          * will delete it (and we shouldn't) .....
251          */
252         
253         for(i = blr->lock_num - 1; i >= 0; i--) {
254                 BOOL err;
255                 
256                 lock_pid = get_lock_pid( data, i, large_file_format);
257                 count = get_lock_count( data, i, large_file_format);
258                 offset = get_lock_offset( data, i, large_file_format, &err);
259                 
260                 /*
261                  * We know err cannot be set as if it was the lock
262                  * request would never have been queued. JRA.
263                  */
264                 
265                 do_unlock(fsp,conn,lock_pid,count,offset);
266         }
267         
268         generic_blocking_lock_error(blr, status);
269 }
270
271 /****************************************************************************
272  Return a lock fail error.
273 *****************************************************************************/
274
275 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
276 {
277         switch(blr->com_type) {
278         case SMBlock:
279         case SMBlockread:
280                 generic_blocking_lock_error(blr, status);
281                 break;
282         case SMBlockingX:
283                 reply_lockingX_error(blr, status);
284                 break;
285         default:
286                 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
287                 exit_server("PANIC - unknown type on blocking lock queue");
288         }
289 }
290
291 /****************************************************************************
292  Attempt to finish off getting all pending blocking locks for a lockread call.
293  Returns True if we want to be removed from the list.
294 *****************************************************************************/
295
296 static BOOL process_lockread(blocking_lock_record *blr)
297 {
298         char *outbuf = OutBuffer;
299         char *inbuf = blr->inbuf;
300         ssize_t nread = -1;
301         char *data, *p;
302         int outsize = 0;
303         SMB_BIG_UINT startpos;
304         size_t numtoread;
305         NTSTATUS status;
306         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
307         files_struct *fsp = blr->fsp;
308         BOOL my_lock_ctx = False;
309
310         numtoread = SVAL(inbuf,smb_vwv1);
311         startpos = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv2);
312         
313         numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
314         data = smb_buf(outbuf) + 3;
315  
316         status = do_lock_spin( fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtoread, startpos, READ_LOCK, &my_lock_ctx);
317         if (NT_STATUS_V(status)) {
318                 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
319                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
320                         /*
321                          * We have other than a "can't get lock"
322                          * error. Send an error.
323                          * Return True so we get dequeued.
324                          */
325                         generic_blocking_lock_error(blr, status);
326                         return True;
327                 }
328
329                 /*
330                  * Still waiting for lock....
331                  */
332                 
333                 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
334                           fsp->fsp_name));
335                 return False;
336         }
337
338         nread = read_file(fsp,data,startpos,numtoread);
339
340         if (nread < 0) {
341                 generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
342                 return True;
343         }
344         
345         construct_reply_common(inbuf, outbuf);
346         outsize = set_message(outbuf,5,0,True);
347         
348         outsize += nread;
349         SSVAL(outbuf,smb_vwv0,nread);
350         SSVAL(outbuf,smb_vwv5,nread+3);
351         p = smb_buf(outbuf);
352         *p++ = 1;
353         SSVAL(p,0,nread); p += 2;
354         set_message_end(outbuf, p+nread);
355         
356         DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
357                    fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
358         
359         send_blocking_reply(outbuf,outsize);
360         return True;
361 }
362
363 /****************************************************************************
364  Attempt to finish off getting all pending blocking locks for a lock call.
365  Returns True if we want to be removed from the list.
366 *****************************************************************************/
367
368 static BOOL process_lock(blocking_lock_record *blr)
369 {
370         char *outbuf = OutBuffer;
371         char *inbuf = blr->inbuf;
372         int outsize;
373         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
374         NTSTATUS status;
375         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
376         files_struct *fsp = blr->fsp;
377         BOOL my_lock_ctx = False;
378
379         count = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
380         offset = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
381
382         errno = 0;
383         status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK, &my_lock_ctx);
384         if (NT_STATUS_IS_ERR(status)) {
385                 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
386                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
387                         /*
388                          * We have other than a "can't get lock"
389                          * error. Send an error.
390                          * Return True so we get dequeued.
391                          */
392                         
393                         blocking_lock_reply_error(blr, status);
394                         return True;
395                 }
396                 /*
397                  * Still can't get the lock - keep waiting.
398                  */
399                 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
400                           fsp->fsp_name));
401                 return False;
402         }
403
404         /*
405          * Success - we got the lock.
406          */
407         
408         DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
409                  fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
410         
411         construct_reply_common(inbuf, outbuf);
412         outsize = set_message(outbuf,0,0,True);
413         send_blocking_reply(outbuf,outsize);
414         return True;
415 }
416
417 /****************************************************************************
418  Attempt to finish off getting all pending blocking locks for a lockingX call.
419  Returns True if we want to be removed from the list.
420 *****************************************************************************/
421
422 static BOOL process_lockingX(blocking_lock_record *blr)
423 {
424         char *inbuf = blr->inbuf;
425         unsigned char locktype = CVAL(inbuf,smb_vwv3);
426         files_struct *fsp = blr->fsp;
427         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
428         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
429         uint16 num_locks = SVAL(inbuf,smb_vwv7);
430         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
431         uint16 lock_pid;
432         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
433         char *data;
434         BOOL my_lock_ctx = False;
435         NTSTATUS status = NT_STATUS_OK;
436
437         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
438
439         /* 
440          * Data now points at the beginning of the list
441          * of smb_lkrng structs.
442          */
443         
444         for(; blr->lock_num < num_locks; blr->lock_num++) {
445                 BOOL err;
446
447                 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
448                 count = get_lock_count( data, blr->lock_num, large_file_format);
449                 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
450                 
451                 /*
452                  * We know err cannot be set as if it was the lock
453                  * request would never have been queued. JRA.
454                  */
455                 errno = 0;
456                 status = do_lock_spin(fsp,conn,lock_pid,count,offset, 
457                                  ((locktype & 1) ? READ_LOCK : WRITE_LOCK), &my_lock_ctx);
458                 if (NT_STATUS_IS_ERR(status)) break;
459         }
460
461         if(blr->lock_num == num_locks) {
462                 /*
463                  * Success - we got all the locks.
464                  */
465                 
466                 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
467                          fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
468
469                 reply_lockingX_success(blr);
470                 return True;
471         } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
472                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
473                         /*
474                          * We have other than a "can't get lock"
475                          * error. Free any locks we had and return an error.
476                          * Return True so we get dequeued.
477                          */
478                 
479                 blocking_lock_reply_error(blr, status);
480                 return True;
481         }
482
483         /*
484          * Still can't get all the locks - keep waiting.
485          */
486         
487         DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
488 Waiting....\n", 
489                   blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
490         
491         return False;
492 }
493
494 /****************************************************************************
495  Process a blocking lock SMB.
496  Returns True if we want to be removed from the list.
497 *****************************************************************************/
498
499 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
500 {
501         switch(blr->com_type) {
502                 case SMBlock:
503                         return process_lock(blr);
504                 case SMBlockread:
505                         return process_lockread(blr);
506                 case SMBlockingX:
507                         return process_lockingX(blr);
508                 default:
509                         DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
510                         exit_server("PANIC - unknown type on blocking lock queue");
511         }
512         return False; /* Keep compiler happy. */
513 }
514
515 /****************************************************************************
516  Delete entries by fnum from the blocking lock pending queue.
517 *****************************************************************************/
518
519 void remove_pending_lock_requests_by_fid(files_struct *fsp)
520 {
521         blocking_lock_record *blr, *next = NULL;
522
523         for(blr = blocking_lock_queue; blr; blr = next) {
524                 next = blr->next;
525                 if(blr->fsp->fnum == fsp->fnum) {
526
527                         DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
528 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
529
530                         brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
531                                 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
532                                 blr->offset, blr->count, True, NULL, NULL);
533
534                         free_blocking_lock_record(blr);
535                 }
536         }
537 }
538
539 /****************************************************************************
540  Delete entries by mid from the blocking lock pending queue. Always send reply.
541 *****************************************************************************/
542
543 void remove_pending_lock_requests_by_mid(int mid)
544 {
545         blocking_lock_record *blr, *next = NULL;
546
547         for(blr = blocking_lock_queue; blr; blr = next) {
548                 next = blr->next;
549                 if(SVAL(blr->inbuf,smb_mid) == mid) {
550                         files_struct *fsp = blr->fsp;
551
552                         DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
553 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
554
555                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
556                         brl_unlock(blr->fsp->dev, blr->fsp->inode, blr->fsp->fnum,
557                                 blr->lock_pid, sys_getpid(), blr->fsp->conn->cnum,
558                                 blr->offset, blr->count, True, NULL, NULL);
559                         free_blocking_lock_record(blr);
560                 }
561         }
562 }
563
564 /****************************************************************************
565   Set a flag as an unlock request affects one of our pending locks.
566 *****************************************************************************/
567
568 static void received_unlock_msg(int msg_type, pid_t src, 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, sys_getpid(), 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, sys_getpid(), 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, sys_getpid(), 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, sys_getpid(), 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 }