s3:smbd: use new simplified snb_signing code in the server
[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, blr->fsp->fsp_name ));
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 num_locks=%d\n",
422                          fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
423
424                 reply_lockingX_success(blr);
425                 return True;
426         }
427
428         if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
429             !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
430                 /*
431                  * We have other than a "can't get lock"
432                  * error. Free any locks we had and return an error.
433                  * Return True so we get dequeued.
434                  */
435                 blocking_lock_reply_error(blr, status);
436                 return True;
437         }
438
439         /*
440          * Still can't get all the locks - keep waiting.
441          */
442
443         DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
444 Waiting....\n", 
445                   blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
446
447         return False;
448 }
449
450 /****************************************************************************
451  Attempt to get the posix lock request from a SMBtrans2 call.
452  Returns True if we want to be removed from the list.
453 *****************************************************************************/
454
455 static bool process_trans2(struct blocking_lock_record *blr)
456 {
457         char params[2];
458         NTSTATUS status;
459         struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
460                                                 blr->fsp,
461                                                 blr->lock_pid,
462                                                 blr->count,
463                                                 blr->offset,
464                                                 blr->lock_type,
465                                                 blr->lock_flav,
466                                                 True,
467                                                 &status,
468                                                 &blr->blocking_pid,
469                                                 blr);
470         TALLOC_FREE(br_lck);
471
472         if (!NT_STATUS_IS_OK(status)) {
473                 if (ERROR_WAS_LOCK_DENIED(status)) {
474                         /* Still can't get the lock, just keep waiting. */
475                         return False;
476                 }       
477                 /*
478                  * We have other than a "can't get lock"
479                  * error. Send an error and return True so we get dequeued.
480                  */
481                 blocking_lock_reply_error(blr, status);
482                 return True;
483         }
484
485         /* We finally got the lock, return success. */
486
487         SSVAL(params,0,0);
488         /* Fake up max_data_bytes here - we know it fits. */
489         send_trans2_replies(blr->fsp->conn, blr->req, params, 2, NULL, 0, 0xffff);
490         return True;
491 }
492
493
494 /****************************************************************************
495  Process a blocking lock SMB.
496  Returns True if we want to be removed from the list.
497 *****************************************************************************/
498
499 static bool blocking_lock_record_process(struct blocking_lock_record *blr)
500 {
501         switch(blr->req->cmd) {
502                 case SMBlockingX:
503                         return process_lockingX(blr);
504                 case SMBtrans2:
505                 case SMBtranss2:
506                         return process_trans2(blr);
507                 default:
508                         DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
509                         exit_server("PANIC - unknown type on blocking lock queue");
510         }
511         return False; /* Keep compiler happy. */
512 }
513
514 /****************************************************************************
515  Cancel entries by fnum from the blocking lock pending queue.
516 *****************************************************************************/
517
518 void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
519 {
520         struct blocking_lock_record *blr, *blr_cancelled, *next = NULL;
521
522         for(blr = blocking_lock_queue; blr; blr = next) {
523                 unsigned char locktype = 0;
524
525                 next = blr->next;
526                 if (blr->fsp->fnum != fsp->fnum) {
527                         continue;
528                 }
529
530                 if (blr->req->cmd == SMBlockingX) {
531                         locktype = CVAL(blr->req->vwv+3, 0);
532                 }
533
534                 DEBUG(10, ("remove_pending_lock_requests_by_fid - removing "
535                            "request type %d for file %s fnum = %d\n",
536                            blr->req->cmd, fsp->fsp_name, fsp->fnum));
537
538                 blr_cancelled = blocking_lock_cancel(fsp,
539                                      blr->lock_pid,
540                                      blr->offset,
541                                      blr->count,
542                                      blr->lock_flav,
543                                      locktype,
544                                      NT_STATUS_RANGE_NOT_LOCKED);
545
546                 SMB_ASSERT(blr_cancelled == blr);
547
548                 brl_lock_cancel(br_lck,
549                                 blr->lock_pid,
550                                 procid_self(),
551                                 blr->offset,
552                                 blr->count,
553                                 blr->lock_flav,
554                                 blr);
555
556                 /* We're closing the file fsp here, so ensure
557                  * we don't have a dangling pointer. */
558                 blr->fsp = NULL;
559         }
560 }
561
562 /****************************************************************************
563  Delete entries by mid from the blocking lock pending queue. Always send reply.
564 *****************************************************************************/
565
566 void remove_pending_lock_requests_by_mid(int mid)
567 {
568         struct blocking_lock_record *blr, *next = NULL;
569
570         for(blr = blocking_lock_queue; blr; blr = next) {
571                 files_struct *fsp;
572                 struct byte_range_lock *br_lck;
573
574                 next = blr->next;
575
576                 if (blr->req->mid != mid) {
577                         continue;
578                 }
579
580                 fsp = blr->fsp;
581                 br_lck = brl_get_locks(talloc_tos(), fsp);
582
583                 if (br_lck) {
584                         DEBUG(10, ("remove_pending_lock_requests_by_mid - "
585                                    "removing request type %d for file %s fnum "
586                                    "= %d\n", blr->req->cmd, fsp->fsp_name,
587                                    fsp->fnum ));
588
589                         brl_lock_cancel(br_lck,
590                                         blr->lock_pid,
591                                         procid_self(),
592                                         blr->offset,
593                                         blr->count,
594                                         blr->lock_flav,
595                                         blr);
596                         TALLOC_FREE(br_lck);
597                 }
598
599                 blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
600                 DLIST_REMOVE(blocking_lock_queue, blr);
601                 TALLOC_FREE(blr);
602         }
603 }
604
605 /****************************************************************************
606  Is this mid a blocking lock request on the queue ?
607 *****************************************************************************/
608
609 bool blocking_lock_was_deferred(int mid)
610 {
611         struct blocking_lock_record *blr, *next = NULL;
612
613         for(blr = blocking_lock_queue; blr; blr = next) {
614                 next = blr->next;
615                 if(blr->req->mid == mid) {
616                         return True;
617                 }
618         }
619         return False;
620 }
621
622 /****************************************************************************
623   Set a flag as an unlock request affects one of our pending locks.
624 *****************************************************************************/
625
626 static void received_unlock_msg(struct messaging_context *msg,
627                                 void *private_data,
628                                 uint32_t msg_type,
629                                 struct server_id server_id,
630                                 DATA_BLOB *data)
631 {
632         DEBUG(10,("received_unlock_msg\n"));
633         process_blocking_lock_queue();
634 }
635
636 /****************************************************************************
637  Process the blocking lock queue. Note that this is only called as root.
638 *****************************************************************************/
639
640 void process_blocking_lock_queue(void)
641 {
642         struct timeval tv_curr = timeval_current();
643         struct blocking_lock_record *blr, *next = NULL;
644         bool recalc_timeout = False;
645
646         /*
647          * Go through the queue and see if we can get any of the locks.
648          */
649
650         for (blr = blocking_lock_queue; blr; blr = next) {
651
652                 next = blr->next;
653
654                 /*
655                  * Go through the remaining locks and try and obtain them.
656                  * The call returns True if all locks were obtained successfully
657                  * and False if we still need to wait.
658                  */
659
660                 DEBUG(10, ("Processing BLR = %p\n", blr));
661
662                 if(blocking_lock_record_process(blr)) {
663                         struct byte_range_lock *br_lck = brl_get_locks(
664                                 talloc_tos(), blr->fsp);
665
666                         DEBUG(10, ("BLR_process returned true: cancelling and "
667                             "removing lock. BLR = %p\n", blr));
668
669                         if (br_lck) {
670                                 brl_lock_cancel(br_lck,
671                                         blr->lock_pid,
672                                         procid_self(),
673                                         blr->offset,
674                                         blr->count,
675                                         blr->lock_flav,
676                                         blr);
677                                 TALLOC_FREE(br_lck);
678                         }
679
680                         DLIST_REMOVE(blocking_lock_queue, blr);
681                         TALLOC_FREE(blr);
682                         recalc_timeout = True;
683                         continue;
684                 }
685
686                 /*
687                  * We couldn't get the locks for this record on the list.
688                  * If the time has expired, return a lock error.
689                  */
690
691                 if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
692                         struct byte_range_lock *br_lck = brl_get_locks(
693                                 talloc_tos(), blr->fsp);
694
695                         DEBUG(10, ("Lock timed out! BLR = %p\n", blr));
696
697                         /*
698                          * Lock expired - throw away all previously
699                          * obtained locks and return lock error.
700                          */
701
702                         if (br_lck) {
703                                 DEBUG(5,("process_blocking_lock_queue: "
704                                          "pending lock fnum = %d for file %s "
705                                          "timed out.\n", blr->fsp->fnum,
706                                          blr->fsp->fsp_name ));
707
708                                 brl_lock_cancel(br_lck,
709                                         blr->lock_pid,
710                                         procid_self(),
711                                         blr->offset,
712                                         blr->count,
713                                         blr->lock_flav,
714                                         blr);
715                                 TALLOC_FREE(br_lck);
716                         }
717
718                         blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
719                         DLIST_REMOVE(blocking_lock_queue, blr);
720                         TALLOC_FREE(blr);
721                         recalc_timeout = True;
722                 }
723         }
724
725         if (recalc_timeout) {
726                 recalc_brl_timeout();
727         }
728 }
729
730 /****************************************************************************
731  Handle a cancel message. Lock already moved onto the cancel queue.
732 *****************************************************************************/
733
734 #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(struct blocking_lock_record *) + sizeof(NTSTATUS))
735
736 static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
737                                                  void *private_data,
738                                                  uint32_t msg_type,
739                                                  struct server_id server_id,
740                                                  DATA_BLOB *data)
741 {
742         NTSTATUS err;
743         const char *msg = (const char *)data->data;
744         struct blocking_lock_record *blr;
745
746         if (data->data == NULL) {
747                 smb_panic("process_blocking_lock_cancel_message: null msg");
748         }
749
750         if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
751                 DEBUG(0, ("process_blocking_lock_cancel_message: "
752                           "Got invalid msg len %d\n", (int)data->length));
753                 smb_panic("process_blocking_lock_cancel_message: bad msg");
754         }
755
756         memcpy(&blr, msg, sizeof(blr));
757         memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
758
759         DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
760                 nt_errstr(err) ));
761
762         blocking_lock_reply_error(blr, err);
763         DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
764         TALLOC_FREE(blr);
765 }
766
767 /****************************************************************************
768  Send ourselves a blocking lock cancelled message. Handled asynchronously above.
769  Returns the blocking_lock_record that is being cancelled.
770 *****************************************************************************/
771
772 struct blocking_lock_record *blocking_lock_cancel(files_struct *fsp,
773                         uint32 lock_pid,
774                         uint64_t offset,
775                         uint64_t count,
776                         enum brl_flavour lock_flav,
777                         unsigned char locktype,
778                         NTSTATUS err)
779 {
780         char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
781         struct blocking_lock_record *blr;
782
783         if (!blocking_lock_cancel_state) {
784                 /* Register our message. */
785                 messaging_register(smbd_messaging_context(), NULL,
786                                    MSG_SMB_BLOCKING_LOCK_CANCEL,
787                                    process_blocking_lock_cancel_message);
788
789                 blocking_lock_cancel_state = True;
790         }
791
792         for (blr = blocking_lock_queue; blr; blr = blr->next) {
793                 if (fsp == blr->fsp &&
794                                 lock_pid == blr->lock_pid &&
795                                 offset == blr->offset &&
796                                 count == blr->count &&
797                                 lock_flav == blr->lock_flav) {
798                         break;
799                 }
800         }
801
802         if (!blr) {
803                 return NULL;
804         }
805
806         /* Check the flags are right. */
807         if (blr->req->cmd == SMBlockingX &&
808                 (locktype & LOCKING_ANDX_LARGE_FILES) !=
809                         (CVAL(blr->req->vwv+3, 0) & LOCKING_ANDX_LARGE_FILES)) {
810                 return NULL;
811         }
812
813         /* Move to cancelled queue. */
814         DLIST_REMOVE(blocking_lock_queue, blr);
815         DLIST_ADD(blocking_lock_cancelled_queue, blr);
816
817         /* Create the message. */
818         memcpy(msg, &blr, sizeof(blr));
819         memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
820
821         messaging_send_buf(smbd_messaging_context(), procid_self(),
822                            MSG_SMB_BLOCKING_LOCK_CANCEL,
823                            (uint8 *)&msg, sizeof(msg));
824
825         return blr;
826 }