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