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