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