7fe9f0c3c7f85579995358834a3572f63d5523ea
[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 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_LOCKING
24
25 extern int max_send;
26
27 /****************************************************************************
28  This is the structure to queue to implement blocking locks.
29  notify. It consists of the requesting SMB and the expiry time.
30 *****************************************************************************/
31
32 typedef struct _blocking_lock_record {
33         struct _blocking_lock_record *next;
34         struct _blocking_lock_record *prev;
35         int com_type;
36         files_struct *fsp;
37         struct timeval expire_time;
38         int lock_num;
39         SMB_BIG_UINT offset;
40         SMB_BIG_UINT count;
41         uint32 lock_pid;
42         enum brl_flavour lock_flav;
43         enum brl_type lock_type;
44         char *inbuf;
45         int length;
46 } blocking_lock_record;
47
48 /* dlink list we store pending lock records on. */
49 static blocking_lock_record *blocking_lock_queue;
50
51 /* dlink list we move cancelled lock records onto. */
52 static blocking_lock_record *blocking_lock_cancelled_queue;
53
54 /* The event that makes us process our blocking lock queue */
55 static struct timed_event *brl_timeout;
56
57 /****************************************************************************
58  Destructor for the above structure.
59 ****************************************************************************/
60
61 static void free_blocking_lock_record(blocking_lock_record *blr)
62 {
63         SAFE_FREE(blr->inbuf);
64         SAFE_FREE(blr);
65 }
66
67 /****************************************************************************
68  Determine if this is a secondary element of a chained SMB.
69   **************************************************************************/
70
71 static BOOL in_chained_smb(void)
72 {
73         return (chain_size != 0);
74 }
75
76 static void received_unlock_msg(int msg_type, struct server_id src,
77                                 void *buf, size_t len,
78                                 void *private_data);
79 static void process_blocking_lock_queue(void);
80
81 static void brl_timeout_fn(struct event_context *event_ctx,
82                            struct timed_event *te,
83                            const struct timeval *now,
84                            void *private_data)
85 {
86         SMB_ASSERT(brl_timeout == te);
87         TALLOC_FREE(brl_timeout);
88
89         change_to_root_user();  /* TODO: Possibly run all timed events as
90                                  * root */
91
92         process_blocking_lock_queue();
93 }
94
95 /****************************************************************************
96  After a change to blocking_lock_queue, recalculate the timed_event for the
97  next processing.
98 ****************************************************************************/
99
100 static BOOL recalc_brl_timeout(void)
101 {
102         blocking_lock_record *brl;
103         struct timeval next_timeout;
104
105         TALLOC_FREE(brl_timeout);
106
107         next_timeout = timeval_zero();  
108
109         for (brl = blocking_lock_queue; brl; brl = brl->next) {
110                 if (timeval_is_zero(&brl->expire_time)) {
111                         continue;
112                 }
113
114                 if (timeval_is_zero(&next_timeout)) {
115                         next_timeout = brl->expire_time;
116                 }
117                 else {
118                         next_timeout = timeval_min(&next_timeout,
119                                                    &brl->expire_time);
120                 }
121         }
122
123         if (timeval_is_zero(&next_timeout)) {
124                 return True;
125         }
126
127         if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
128                                             next_timeout, "brl_timeout",
129                                             brl_timeout_fn, NULL))) {
130                 return False;
131         }
132
133         return True;
134 }
135
136
137 /****************************************************************************
138  Function to push a blocking lock request onto the lock queue.
139 ****************************************************************************/
140
141 BOOL push_blocking_lock_request( struct byte_range_lock *br_lck,
142                 char *inbuf, int length,
143                 files_struct *fsp,
144                 int lock_timeout,
145                 int lock_num,
146                 uint32 lock_pid,
147                 enum brl_type lock_type,
148                 enum brl_flavour lock_flav,
149                 SMB_BIG_UINT offset, SMB_BIG_UINT count)
150 {
151         static BOOL set_lock_msg;
152         blocking_lock_record *blr;
153         NTSTATUS status;
154
155         if(in_chained_smb() ) {
156                 DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
157                 return False;
158         }
159
160         /*
161          * Now queue an entry on the blocking lock queue. We setup
162          * the expiration time here.
163          */
164
165         if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
166                 DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
167                 return False;
168         }
169
170         blr->next = NULL;
171         blr->prev = NULL;
172
173         if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
174                 DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
175                 SAFE_FREE(blr);
176                 return False;
177         }
178
179         blr->com_type = CVAL(inbuf,smb_com);
180         blr->fsp = fsp;
181         if (lock_timeout == -1) {
182                 blr->expire_time.tv_sec = 0;
183                 blr->expire_time.tv_usec = 0; /* Never expire. */
184         } else {
185                 blr->expire_time = timeval_current_ofs(lock_timeout/1000,
186                                         (lock_timeout % 1000) * 1000);
187         }
188         blr->lock_num = lock_num;
189         blr->lock_pid = lock_pid;
190         blr->lock_flav = lock_flav;
191         blr->lock_type = lock_type;
192         blr->offset = offset;
193         blr->count = count;
194         memcpy(blr->inbuf, inbuf, length);
195         blr->length = length;
196
197         /* Add a pending lock record for this. */
198         status = brl_lock(br_lck,
199                         lock_pid,
200                         procid_self(),
201                         offset,
202                         count,
203                         lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
204                         blr->lock_flav,
205                         lock_timeout ? True : False); /* blocking_lock. */
206
207         if (!NT_STATUS_IS_OK(status)) {
208                 DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
209                 DLIST_REMOVE(blocking_lock_queue, blr);
210                 free_blocking_lock_record(blr);
211                 return False;
212         }
213
214         DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
215         recalc_brl_timeout();
216
217         /* Ensure we'll receive messages when this is unlocked. */
218         if (!set_lock_msg) {
219                 message_register(MSG_SMB_UNLOCK, received_unlock_msg,
220                                  NULL);
221                 set_lock_msg = True;
222         }
223
224         DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with "
225                 "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
226                 length, (unsigned int)blr->expire_time.tv_sec,
227                 (unsigned int)blr->expire_time.tv_usec, lock_timeout,
228                 blr->fsp->fnum, blr->fsp->fsp_name ));
229
230         /* Push the MID of this packet on the signing queue. */
231         srv_defer_sign_response(SVAL(inbuf,smb_mid));
232
233         return True;
234 }
235
236 /****************************************************************************
237  Return a smd with a given size.
238 *****************************************************************************/
239
240 static void send_blocking_reply(char *outbuf, int outsize, const char *inbuf)
241 {
242         if(outsize > 4) {
243                 smb_setlen(inbuf, outbuf,outsize - 4);
244         }
245
246         if (!send_smb(smbd_server_fd(),outbuf)) {
247                 exit_server_cleanly("send_blocking_reply: send_smb failed.");
248         }
249 }
250
251 /****************************************************************************
252  Return a lockingX success SMB.
253 *****************************************************************************/
254
255 static void reply_lockingX_success(blocking_lock_record *blr)
256 {
257         char *outbuf = get_OutBuffer();
258         int bufsize = BUFFER_SIZE;
259         char *inbuf = blr->inbuf;
260         int outsize = 0;
261
262         construct_reply_common(inbuf, outbuf);
263         set_message(inbuf,outbuf,2,0,True);
264
265         /*
266          * As this message is a lockingX call we must handle
267          * any following chained message correctly.
268          * This is normally handled in construct_reply(),
269          * but as that calls switch_message, we can't use
270          * that here and must set up the chain info manually.
271          */
272
273         outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
274
275         outsize += chain_size;
276
277         send_blocking_reply(outbuf,outsize,inbuf);
278 }
279
280 /****************************************************************************
281  Return a generic lock fail error blocking call.
282 *****************************************************************************/
283
284 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
285 {
286         char *outbuf = get_OutBuffer();
287         char *inbuf = blr->inbuf;
288         construct_reply_common(inbuf, outbuf);
289
290         /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
291            FILE_LOCK_CONFLICT! (tridge) */
292         if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
293                 status = NT_STATUS_FILE_LOCK_CONFLICT;
294         }
295
296         if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
297                 /* Store the last lock error. */
298                 files_struct *fsp = blr->fsp;
299
300                 fsp->last_lock_failure.context.smbpid = blr->lock_pid;
301                 fsp->last_lock_failure.context.tid = fsp->conn->cnum;
302                 fsp->last_lock_failure.context.pid = procid_self();
303                 fsp->last_lock_failure.start = blr->offset;
304                 fsp->last_lock_failure.size = blr->count;
305                 fsp->last_lock_failure.fnum = fsp->fnum;
306                 fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
307                 fsp->last_lock_failure.lock_flav = blr->lock_flav;
308         }
309
310         ERROR_NT(status);
311         if (!send_smb(smbd_server_fd(),outbuf)) {
312                 exit_server_cleanly("generic_blocking_lock_error: send_smb failed.");
313         }
314 }
315
316 /****************************************************************************
317  Return a lock fail error for a lockingX call. Undo all the locks we have 
318  obtained first.
319 *****************************************************************************/
320
321 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
322 {
323         char *inbuf = blr->inbuf;
324         files_struct *fsp = blr->fsp;
325         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
326         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
327         uint32 lock_pid;
328         unsigned char locktype = CVAL(inbuf,smb_vwv3);
329         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
330         char *data;
331         int i;
332
333         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
334         
335         /* 
336          * Data now points at the beginning of the list
337          * of smb_lkrng structs.
338          */
339
340         /*
341          * Ensure we don't do a remove on the lock that just failed,
342          * as under POSIX rules, if we have a lock already there, we
343          * will delete it (and we shouldn't) .....
344          */
345         
346         for(i = blr->lock_num - 1; i >= 0; i--) {
347                 BOOL err;
348                 
349                 lock_pid = get_lock_pid( data, i, large_file_format);
350                 count = get_lock_count( data, i, large_file_format);
351                 offset = get_lock_offset( data, i, large_file_format, &err);
352                 
353                 /*
354                  * We know err cannot be set as if it was the lock
355                  * request would never have been queued. JRA.
356                  */
357                 
358                 do_unlock(fsp,
359                         lock_pid,
360                         count,
361                         offset,
362                         WINDOWS_LOCK);
363         }
364         
365         generic_blocking_lock_error(blr, status);
366 }
367
368 /****************************************************************************
369  Return a lock fail error.
370 *****************************************************************************/
371
372 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
373 {
374         switch(blr->com_type) {
375         case SMBlockingX:
376                 reply_lockingX_error(blr, status);
377                 break;
378         case SMBtrans2:
379         case SMBtranss2:
380                 {
381                         char *outbuf = get_OutBuffer();
382                         char *inbuf = blr->inbuf;
383                         construct_reply_common(inbuf, outbuf);
384                         /* construct_reply_common has done us the favor to pre-fill the
385                          * command field with SMBtranss2 which is wrong :-)
386                          */
387                         SCVAL(outbuf,smb_com,SMBtrans2);
388                         ERROR_NT(status);
389                         if (!send_smb(smbd_server_fd(),outbuf)) {
390                                 exit_server_cleanly("blocking_lock_reply_error: send_smb failed.");
391                         }
392                         break;
393                 }
394         default:
395                 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
396                 exit_server("PANIC - unknown type on blocking lock queue");
397         }
398 }
399
400 /****************************************************************************
401  Attempt to finish off getting all pending blocking locks for a lockingX call.
402  Returns True if we want to be removed from the list.
403 *****************************************************************************/
404
405 static BOOL process_lockingX(blocking_lock_record *blr)
406 {
407         char *inbuf = blr->inbuf;
408         unsigned char locktype = CVAL(inbuf,smb_vwv3);
409         files_struct *fsp = blr->fsp;
410         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
411         uint16 num_locks = SVAL(inbuf,smb_vwv7);
412         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
413         uint32 lock_pid;
414         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
415         char *data;
416         NTSTATUS status = NT_STATUS_OK;
417
418         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
419
420         /* 
421          * Data now points at the beginning of the list
422          * of smb_lkrng structs.
423          */
424
425         for(; blr->lock_num < num_locks; blr->lock_num++) {
426                 struct byte_range_lock *br_lck = NULL;
427                 BOOL err;
428
429                 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
430                 count = get_lock_count( data, blr->lock_num, large_file_format);
431                 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
432                 
433                 /*
434                  * We know err cannot be set as if it was the lock
435                  * request would never have been queued. JRA.
436                  */
437                 errno = 0;
438                 br_lck = do_lock(fsp,
439                                 lock_pid,
440                                 count,
441                                 offset, 
442                                 ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
443                                         READ_LOCK : WRITE_LOCK),
444                                 WINDOWS_LOCK,
445                                 True,
446                                 &status);
447
448                 TALLOC_FREE(br_lck);
449
450                 if (NT_STATUS_IS_ERR(status)) {
451                         break;
452                 }
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  Attempt to get the posix lock request from a SMBtrans2 call.
490  Returns True if we want to be removed from the list.
491 *****************************************************************************/
492
493 static BOOL process_trans2(blocking_lock_record *blr)
494 {
495         char *inbuf = blr->inbuf;
496         char *outbuf;
497         char params[2];
498         NTSTATUS status;
499         struct byte_range_lock *br_lck = do_lock(blr->fsp,
500                                                 blr->lock_pid,
501                                                 blr->count,
502                                                 blr->offset,
503                                                 blr->lock_type,
504                                                 blr->lock_flav,
505                                                 True,
506                                                 &status);
507         TALLOC_FREE(br_lck);
508
509         if (!NT_STATUS_IS_OK(status)) {
510                 if (ERROR_WAS_LOCK_DENIED(status)) {
511                         /* Still can't get the lock, just keep waiting. */
512                         return False;
513                 }       
514                 /*
515                  * We have other than a "can't get lock"
516                  * error. Send an error and return True so we get dequeued.
517                  */
518                 blocking_lock_reply_error(blr, status);
519                 return True;
520         }
521
522         /* We finally got the lock, return success. */
523         outbuf = get_OutBuffer();
524         construct_reply_common(inbuf, outbuf);
525         SCVAL(outbuf,smb_com,SMBtrans2);
526         SSVAL(params,0,0);
527         /* Fake up max_data_bytes here - we know it fits. */
528         send_trans2_replies(inbuf, outbuf, max_send, params, 2, NULL, 0, 0xffff);
529         return True;
530 }
531
532
533 /****************************************************************************
534  Process a blocking lock SMB.
535  Returns True if we want to be removed from the list.
536 *****************************************************************************/
537
538 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
539 {
540         switch(blr->com_type) {
541                 case SMBlockingX:
542                         return process_lockingX(blr);
543                 case SMBtrans2:
544                 case SMBtranss2:
545                         return process_trans2(blr);
546                 default:
547                         DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
548                         exit_server("PANIC - unknown type on blocking lock queue");
549         }
550         return False; /* Keep compiler happy. */
551 }
552
553 /****************************************************************************
554  Cancel entries by fnum from the blocking lock pending queue.
555 *****************************************************************************/
556
557 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
558 {
559         blocking_lock_record *blr, *next = NULL;
560
561         for(blr = blocking_lock_queue; blr; blr = next) {
562                 next = blr->next;
563                 if(blr->fsp->fnum == fsp->fnum) {
564                         unsigned char locktype = 0;
565
566                         if (blr->com_type == SMBlockingX) {
567                                 locktype = CVAL(blr->inbuf,smb_vwv3);
568                         }
569
570                         if (br_lck) {
571                                 DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
572 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
573
574                                 brl_lock_cancel(br_lck,
575                                         blr->lock_pid,
576                                         procid_self(),
577                                         blr->offset,
578                                         blr->count,
579                                         blr->lock_flav);
580
581                                 blocking_lock_cancel(fsp,
582                                         blr->lock_pid,
583                                         blr->offset,
584                                         blr->count,
585                                         blr->lock_flav,
586                                         locktype,
587                                         NT_STATUS_RANGE_NOT_LOCKED);
588                         }
589                 }
590         }
591 }
592
593 /****************************************************************************
594  Delete entries by mid from the blocking lock pending queue. Always send reply.
595 *****************************************************************************/
596
597 void remove_pending_lock_requests_by_mid(int mid)
598 {
599         blocking_lock_record *blr, *next = NULL;
600
601         for(blr = blocking_lock_queue; blr; blr = next) {
602                 next = blr->next;
603                 if(SVAL(blr->inbuf,smb_mid) == mid) {
604                         files_struct *fsp = blr->fsp;
605                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
606
607                         if (br_lck) {
608                                 DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
609 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
610
611                                 brl_lock_cancel(br_lck,
612                                         blr->lock_pid,
613                                         procid_self(),
614                                         blr->offset,
615                                         blr->count,
616                                         blr->lock_flav);
617                                 TALLOC_FREE(br_lck);
618                         }
619
620                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
621                         DLIST_REMOVE(blocking_lock_queue, blr);
622                         free_blocking_lock_record(blr);
623                 }
624         }
625 }
626
627 /****************************************************************************
628  Is this mid a blocking lock request on the queue ?
629 *****************************************************************************/
630
631 BOOL blocking_lock_was_deferred(int mid)
632 {
633         blocking_lock_record *blr, *next = NULL;
634
635         for(blr = blocking_lock_queue; blr; blr = next) {
636                 next = blr->next;
637                 if(SVAL(blr->inbuf,smb_mid) == mid) {
638                         return True;
639                 }
640         }
641         return False;
642 }
643
644 /****************************************************************************
645   Set a flag as an unlock request affects one of our pending locks.
646 *****************************************************************************/
647
648 static void received_unlock_msg(int msg_type, struct server_id src,
649                                 void *buf, size_t len,
650                                 void *private_data)
651 {
652         DEBUG(10,("received_unlock_msg\n"));
653         process_blocking_lock_queue();
654 }
655
656 /****************************************************************************
657  Process the blocking lock queue. Note that this is only called as root.
658 *****************************************************************************/
659
660 static void process_blocking_lock_queue(void)
661 {
662         struct timeval tv_curr = timeval_current();
663         blocking_lock_record *blr, *next = NULL;
664         BOOL recalc_timeout = False;
665
666         /*
667          * Go through the queue and see if we can get any of the locks.
668          */
669
670         for (blr = blocking_lock_queue; blr; blr = next) {
671                 connection_struct *conn = NULL;
672                 uint16 vuid;
673                 files_struct *fsp = NULL;
674
675                 next = blr->next;
676
677                 /*
678                  * Ensure we don't have any old chain_fsp values
679                  * sitting around....
680                  */
681                 chain_size = 0;
682                 file_chain_reset();
683                 fsp = blr->fsp;
684
685                 conn = conn_find(SVAL(blr->inbuf,smb_tid));
686                 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
687                                 SVAL(blr->inbuf,smb_uid);
688
689                 DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
690                         fsp->fnum, fsp->fsp_name ));
691
692                 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
693                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
694
695                         /*
696                          * Lock expired - throw away all previously
697                          * obtained locks and return lock error.
698                          */
699
700                         if (br_lck) {
701                                 DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
702                                         fsp->fnum, fsp->fsp_name ));
703
704                                 brl_lock_cancel(br_lck,
705                                         blr->lock_pid,
706                                         procid_self(),
707                                         blr->offset,
708                                         blr->count,
709                                         blr->lock_flav);
710                                 TALLOC_FREE(br_lck);
711                         }
712
713                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
714                         DLIST_REMOVE(blocking_lock_queue, blr);
715                         free_blocking_lock_record(blr);
716                         recalc_timeout = True;
717                         continue;
718                 }
719
720                 if(!change_to_user(conn,vuid)) {
721                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
722
723                         /*
724                          * Remove the entry and return an error to the client.
725                          */
726
727                         if (br_lck) {
728                                 brl_lock_cancel(br_lck,
729                                         blr->lock_pid,
730                                         procid_self(),
731                                         blr->offset,
732                                         blr->count,
733                                         blr->lock_flav);
734                                 TALLOC_FREE(br_lck);
735                         }
736
737                         DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
738                                 vuid ));
739                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
740                         DLIST_REMOVE(blocking_lock_queue, blr);
741                         free_blocking_lock_record(blr);
742                         recalc_timeout = True;
743                         continue;
744                 }
745
746                 if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
747                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
748
749                         /*
750                          * Remove the entry and return an error to the client.
751                          */
752
753                         if (br_lck) {
754                                 brl_lock_cancel(br_lck,
755                                         blr->lock_pid,
756                                         procid_self(),
757                                         blr->offset,
758                                         blr->count,
759                                         blr->lock_flav);
760                                 TALLOC_FREE(br_lck);
761                         }
762
763                         DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
764                         blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
765                         DLIST_REMOVE(blocking_lock_queue, blr);
766                         free_blocking_lock_record(blr);
767                         recalc_timeout = True;
768                         change_to_root_user();
769                         continue;
770                 }
771
772                 /*
773                  * Go through the remaining locks and try and obtain them.
774                  * The call returns True if all locks were obtained successfully
775                  * and False if we still need to wait.
776                  */
777
778                 if(blocking_lock_record_process(blr)) {
779                         struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
780
781                         if (br_lck) {
782                                 brl_lock_cancel(br_lck,
783                                         blr->lock_pid,
784                                         procid_self(),
785                                         blr->offset,
786                                         blr->count,
787                                         blr->lock_flav);
788                                 TALLOC_FREE(br_lck);
789                         }
790
791                         DLIST_REMOVE(blocking_lock_queue, blr);
792                         free_blocking_lock_record(blr);
793                         recalc_timeout = True;
794                 }
795                 change_to_root_user();
796         }
797
798         if (recalc_timeout) {
799                 recalc_brl_timeout();
800         }
801 }
802
803 /****************************************************************************
804  Handle a cancel message. Lock already moved onto the cancel queue.
805 *****************************************************************************/
806
807 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
808
809 static void process_blocking_lock_cancel_message(int msg_type,
810                                                  struct server_id src,
811                                                  void *buf, size_t len,
812                                                  void *private_data)
813 {
814         NTSTATUS err;
815         const char *msg = (const char *)buf;
816         blocking_lock_record *blr;
817
818         if (buf == NULL) {
819                 smb_panic("process_blocking_lock_cancel_message: null msg\n");
820         }
821
822         if (len != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
823                 DEBUG(0, ("process_blocking_lock_cancel_message: "
824                         "Got invalid msg len %d\n", (int)len));
825                 smb_panic("process_blocking_lock_cancel_message: bad msg\n");
826         }
827
828         memcpy(&blr, msg, sizeof(blr));
829         memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
830
831         DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
832                 nt_errstr(err) ));
833
834         blocking_lock_reply_error(blr, err);
835         DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
836         free_blocking_lock_record(blr);
837 }
838
839 /****************************************************************************
840  Send ourselves a blocking lock cancelled message. Handled asynchronously above.
841 *****************************************************************************/
842
843 BOOL blocking_lock_cancel(files_struct *fsp,
844                         uint32 lock_pid,
845                         SMB_BIG_UINT offset,
846                         SMB_BIG_UINT count,
847                         enum brl_flavour lock_flav,
848                         unsigned char locktype,
849                         NTSTATUS err)
850 {
851         static BOOL initialized;
852         char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
853         blocking_lock_record *blr;
854
855         if (!initialized) {
856                 /* Register our message. */
857                 message_register(MSG_SMB_BLOCKING_LOCK_CANCEL,
858                                  process_blocking_lock_cancel_message,
859                                  NULL);
860
861                 initialized = True;
862         }
863
864         for (blr = blocking_lock_queue; blr; blr = blr->next) {
865                 if (fsp == blr->fsp &&
866                                 lock_pid == blr->lock_pid &&
867                                 offset == blr->offset &&
868                                 count == blr->count &&
869                                 lock_flav == blr->lock_flav) {
870                         break;
871                 }
872         }
873
874         if (!blr) {
875                 return False;
876         }
877
878         /* Check the flags are right. */
879         if (blr->com_type == SMBlockingX &&
880                 (locktype & LOCKING_ANDX_LARGE_FILES) !=
881                         (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
882                 return False;
883         }
884
885         /* Move to cancelled queue. */
886         DLIST_REMOVE(blocking_lock_queue, blr);
887         DLIST_ADD(blocking_lock_cancelled_queue, blr);
888
889         /* Create the message. */
890         memcpy(msg, &blr, sizeof(blr));
891         memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
892
893         message_send_pid(pid_to_procid(sys_getpid()),
894                         MSG_SMB_BLOCKING_LOCK_CANCEL,
895                         &msg, sizeof(msg), True);
896
897         return True;
898 }