80ce2bc8c12689b021b0f6c3eb34d2746468ad88
[amitay/samba.git] / source3 / smbd / smb2_lock.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) Jeremy Allison 2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "librpc/gen_ndr/messaging.h"
26
27 struct smbd_smb2_lock_element {
28         uint64_t offset;
29         uint64_t length;
30         uint32_t flags;
31 };
32
33 struct smbd_smb2_lock_state {
34         struct smbd_smb2_request *smb2req;
35         struct smb_request *smb1req;
36         struct blocking_lock_record *blr;
37         uint16_t lock_count;
38         struct smbd_lock_element *locks;
39 };
40
41 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
42                                 struct blocking_lock_record *blr);
43
44 static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
45                                                  struct tevent_context *ev,
46                                                  struct smbd_smb2_request *smb2req,
47                                                  uint32_t in_smbpid,
48                                                  uint64_t in_file_id_volatile,
49                                                  uint16_t in_lock_count,
50                                                  struct smbd_smb2_lock_element *in_locks);
51 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req);
52
53 static void smbd_smb2_request_lock_done(struct tevent_req *subreq);
54 NTSTATUS smbd_smb2_request_process_lock(struct smbd_smb2_request *req)
55 {
56         const uint8_t *inhdr;
57         const uint8_t *inbody;
58         const int i = req->current_idx;
59         size_t expected_body_size = 0x30;
60         size_t body_size;
61         uint32_t in_smbpid;
62         uint16_t in_lock_count;
63         uint64_t in_file_id_persistent;
64         uint64_t in_file_id_volatile;
65         struct smbd_smb2_lock_element *in_locks;
66         struct tevent_req *subreq;
67         const uint8_t *lock_buffer;
68         uint16_t l;
69
70         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
71         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
72                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
73         }
74
75         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
76
77         body_size = SVAL(inbody, 0x00);
78         if (body_size != expected_body_size) {
79                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
80         }
81
82         in_smbpid                       = IVAL(inhdr, SMB2_HDR_PID);
83
84         in_lock_count                   = CVAL(inbody, 0x02);
85         /* 0x04 - 4 bytes reserved */
86         in_file_id_persistent           = BVAL(inbody, 0x08);
87         in_file_id_volatile             = BVAL(inbody, 0x10);
88
89         if (in_lock_count < 1) {
90                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91         }
92
93         if (((in_lock_count - 1) * 0x18) > req->in.vector[i+2].iov_len) {
94                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
95         }
96
97         if (req->compat_chain_fsp) {
98                 /* skip check */
99         } else if (in_file_id_persistent != in_file_id_volatile) {
100                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
101         }
102
103         in_locks = talloc_array(req, struct smbd_smb2_lock_element,
104                                 in_lock_count);
105         if (in_locks == NULL) {
106                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
107         }
108
109         l = 0;
110         lock_buffer = inbody + 0x18;
111
112         in_locks[l].offset      = BVAL(lock_buffer, 0x00);
113         in_locks[l].length      = BVAL(lock_buffer, 0x08);
114         in_locks[l].flags       = IVAL(lock_buffer, 0x10);
115         /* 0x14 - 4 reserved bytes */
116
117         lock_buffer = (const uint8_t *)req->in.vector[i+2].iov_base;
118
119         for (l=1; l < in_lock_count; l++) {
120                 in_locks[l].offset      = BVAL(lock_buffer, 0x00);
121                 in_locks[l].length      = BVAL(lock_buffer, 0x08);
122                 in_locks[l].flags       = IVAL(lock_buffer, 0x10);
123                 /* 0x14 - 4 reserved bytes */
124
125                 lock_buffer += 0x18;
126         }
127
128         subreq = smbd_smb2_lock_send(req,
129                                      req->sconn->smb2.event_ctx,
130                                      req,
131                                      in_smbpid,
132                                      in_file_id_volatile,
133                                      in_lock_count,
134                                      in_locks);
135         if (subreq == NULL) {
136                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
137         }
138         tevent_req_set_callback(subreq, smbd_smb2_request_lock_done, req);
139
140         return smbd_smb2_request_pending_queue(req, subreq);
141 }
142
143 static void smbd_smb2_request_lock_done(struct tevent_req *subreq)
144 {
145         struct smbd_smb2_request *smb2req = tevent_req_callback_data(subreq,
146                                         struct smbd_smb2_request);
147         DATA_BLOB outbody;
148         NTSTATUS status;
149         NTSTATUS error; /* transport error */
150
151         if (smb2req->cancelled) {
152                 const uint8_t *inhdr = (const uint8_t *)
153                         smb2req->in.vector[smb2req->current_idx].iov_base;
154                 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
155                 struct smbd_smb2_lock_state *state;
156
157                 DEBUG(10,("smbd_smb2_request_lock_done: cancelled mid %llu\n",
158                         (unsigned long long)mid ));
159
160                 state = tevent_req_data(smb2req->subreq,
161                                 struct smbd_smb2_lock_state);
162
163                 SMB_ASSERT(state);
164                 SMB_ASSERT(state->blr);
165
166                 remove_pending_lock(state, state->blr);
167
168                 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
169                 if (!NT_STATUS_IS_OK(error)) {
170                         smbd_server_connection_terminate(smb2req->sconn,
171                                 nt_errstr(error));
172                         return;
173                 }
174                 return;
175         }
176
177         status = smbd_smb2_lock_recv(subreq);
178         TALLOC_FREE(subreq);
179         if (!NT_STATUS_IS_OK(status)) {
180                 error = smbd_smb2_request_error(smb2req, status);
181                 if (!NT_STATUS_IS_OK(error)) {
182                         smbd_server_connection_terminate(smb2req->sconn,
183                                                          nt_errstr(error));
184                         return;
185                 }
186                 return;
187         }
188
189         outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x04);
190         if (outbody.data == NULL) {
191                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
192                 if (!NT_STATUS_IS_OK(error)) {
193                         smbd_server_connection_terminate(smb2req->sconn,
194                                                          nt_errstr(error));
195                         return;
196                 }
197                 return;
198         }
199
200         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
201         SSVAL(outbody.data, 0x02, 0);           /* reserved */
202
203         error = smbd_smb2_request_done(smb2req, outbody, NULL);
204         if (!NT_STATUS_IS_OK(error)) {
205                 smbd_server_connection_terminate(smb2req->sconn,
206                                                  nt_errstr(error));
207                 return;
208         }
209 }
210
211 static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
212                                                  struct tevent_context *ev,
213                                                  struct smbd_smb2_request *smb2req,
214                                                  uint32_t in_smbpid,
215                                                  uint64_t in_file_id_volatile,
216                                                  uint16_t in_lock_count,
217                                                  struct smbd_smb2_lock_element *in_locks)
218 {
219         struct tevent_req *req;
220         struct smbd_smb2_lock_state *state;
221         struct smb_request *smb1req;
222         connection_struct *conn = smb2req->tcon->compat_conn;
223         files_struct *fsp;
224         int32_t timeout = -1;
225         bool isunlock = false;
226         uint16_t i;
227         struct smbd_lock_element *locks;
228         NTSTATUS status;
229         bool async = false;
230
231         req = tevent_req_create(mem_ctx, &state,
232                         struct smbd_smb2_lock_state);
233         if (req == NULL) {
234                 return NULL;
235         }
236         state->smb2req = smb2req;
237         smb2req->subreq = req; /* So we can find this when going async. */
238
239         smb1req = smbd_smb2_fake_smb_request(smb2req);
240         if (tevent_req_nomem(smb1req, req)) {
241                 return tevent_req_post(req, ev);
242         }
243         state->smb1req = smb1req;
244
245         DEBUG(10,("smbd_smb2_lock_send: file_id[0x%016llX]\n",
246                   (unsigned long long)in_file_id_volatile));
247
248         fsp = file_fsp(smb1req, (uint16_t)in_file_id_volatile);
249         if (fsp == NULL) {
250                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
251                 return tevent_req_post(req, ev);
252         }
253         if (conn != fsp->conn) {
254                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
255                 return tevent_req_post(req, ev);
256         }
257         if (smb2req->session->vuid != fsp->vuid) {
258                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
259                 return tevent_req_post(req, ev);
260         }
261
262         locks = talloc_array(state, struct smbd_lock_element, in_lock_count);
263         if (locks == NULL) {
264                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
265                 return tevent_req_post(req, ev);
266         }
267
268         switch (in_locks[0].flags) {
269         case SMB2_LOCK_FLAG_SHARED:
270         case SMB2_LOCK_FLAG_EXCLUSIVE:
271                 if (in_lock_count > 1) {
272                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
273                         return tevent_req_post(req, ev);
274                 }
275                 timeout = -1;
276                 break;
277
278         case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
279         case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
280                 timeout = 0;
281                 break;
282
283         case SMB2_LOCK_FLAG_UNLOCK:
284                 /* only the first lock gives the UNLOCK bit - see
285                    MS-SMB2 3.3.5.14 */
286                 isunlock = true;
287                 timeout = 0;
288                 break;
289
290         default:
291                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
292                 return tevent_req_post(req, ev);
293         }
294
295         for (i=0; i<in_lock_count; i++) {
296                 bool invalid = false;
297
298                 switch (in_locks[i].flags) {
299                 case SMB2_LOCK_FLAG_SHARED:
300                 case SMB2_LOCK_FLAG_EXCLUSIVE:
301                         if (isunlock) {
302                                 invalid = true;
303                                 break;
304                         }
305                         if (i > 0) {
306                                 tevent_req_nterror(req,
307                                                    NT_STATUS_INVALID_PARAMETER);
308                                 return tevent_req_post(req, ev);
309                         }
310                         break;
311
312                 case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
313                 case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
314                         if (isunlock) {
315                                 invalid = true;
316                         }
317                         break;
318
319                 case SMB2_LOCK_FLAG_UNLOCK:
320                         if (!isunlock) {
321                                 tevent_req_nterror(req,
322                                                    NT_STATUS_INVALID_PARAMETER);
323                                 return tevent_req_post(req, ev);
324                         }
325                         break;
326
327                 default:
328                         if (isunlock) {
329                                 /*
330                                  * is the first element was a UNLOCK
331                                  * we need to deferr the error response
332                                  * to the backend, because we need to process
333                                  * all unlock elements before
334                                  */
335                                 invalid = true;
336                                 break;
337                         }
338                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
339                         return tevent_req_post(req, ev);
340                 }
341
342                 locks[i].smblctx = in_file_id_volatile;
343                 locks[i].offset = in_locks[i].offset;
344                 locks[i].count  = in_locks[i].length;
345
346                 if (in_locks[i].flags & SMB2_LOCK_FLAG_EXCLUSIVE) {
347                         locks[i].brltype = WRITE_LOCK;
348                 } else if (in_locks[i].flags & SMB2_LOCK_FLAG_SHARED) {
349                         locks[i].brltype = READ_LOCK;
350                 } else if (invalid) {
351                         /*
352                          * this is an invalid UNLOCK element
353                          * and the backend needs to test for
354                          * brltype != UNLOCK_LOCK and return
355                          * NT_STATUS_INVALID_PARAMER
356                          */
357                         locks[i].brltype = READ_LOCK;
358                 } else {
359                         locks[i].brltype = UNLOCK_LOCK;
360                 }
361
362                 DEBUG(10,("smbd_smb2_lock_send: index %d offset=%llu, count=%llu, "
363                         "smblctx = %llu type %d\n",
364                         i,
365                         (unsigned long long)locks[i].offset,
366                         (unsigned long long)locks[i].count,
367                         (unsigned long long)locks[i].smblctx,
368                         (int)locks[i].brltype ));
369         }
370
371         state->locks = locks;
372         state->lock_count = in_lock_count;
373
374         if (isunlock) {
375                 status = smbd_do_locking(smb1req, fsp,
376                                          0,
377                                          timeout,
378                                          in_lock_count,
379                                          locks,
380                                          0,
381                                          NULL,
382                                          &async);
383         } else {
384                 status = smbd_do_locking(smb1req, fsp,
385                                          0,
386                                          timeout,
387                                          0,
388                                          NULL,
389                                          in_lock_count,
390                                          locks,
391                                          &async);
392         }
393         if (!NT_STATUS_IS_OK(status)) {
394                 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
395                        status = NT_STATUS_LOCK_NOT_GRANTED;
396                 }
397                 tevent_req_nterror(req, status);
398                 return tevent_req_post(req, ev);
399         }
400
401         if (async) {
402                 return req;
403         }
404
405         tevent_req_done(req);
406         return tevent_req_post(req, ev);
407 }
408
409 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req)
410 {
411         NTSTATUS status;
412
413         if (tevent_req_is_nterror(req, &status)) {
414                 tevent_req_received(req);
415                 return status;
416         }
417
418         tevent_req_received(req);
419         return NT_STATUS_OK;
420 }
421
422 /****************************************************************
423  Cancel an outstanding blocking lock request.
424 *****************************************************************/
425
426 static bool smbd_smb2_lock_cancel(struct tevent_req *req)
427 {
428         struct smbd_smb2_request *smb2req = NULL;
429         struct smbd_smb2_lock_state *state = tevent_req_data(req,
430                                 struct smbd_smb2_lock_state);
431         if (!state) {
432                 return false;
433         }
434
435         if (!state->smb2req) {
436                 return false;
437         }
438
439         smb2req = state->smb2req;
440         smb2req->cancelled = true;
441
442         tevent_req_done(req);
443         return true;
444 }
445
446 /****************************************************************
447  Got a message saying someone unlocked a file. Re-schedule all
448  blocking lock requests as we don't know if anything overlapped.
449 *****************************************************************/
450
451 static void received_unlock_msg(struct messaging_context *msg,
452                                 void *private_data,
453                                 uint32_t msg_type,
454                                 struct server_id server_id,
455                                 DATA_BLOB *data)
456 {
457         struct smbd_server_connection *sconn;
458
459         DEBUG(10,("received_unlock_msg (SMB2)\n"));
460
461         sconn = msg_ctx_to_sconn(msg);
462         if (sconn == NULL) {
463                 DEBUG(1, ("could not find sconn\n"));
464                 return;
465         }
466         process_blocking_lock_queue_smb2(sconn, timeval_current());
467 }
468
469 /****************************************************************
470  Function to get the blr on a pending record.
471 *****************************************************************/
472
473 struct blocking_lock_record *get_pending_smb2req_blr(struct smbd_smb2_request *smb2req)
474 {
475         struct smbd_smb2_lock_state *state = NULL;
476         const uint8_t *inhdr;
477
478         if (!smb2req) {
479                 return NULL;
480         }
481         if (smb2req->subreq == NULL) {
482                 return NULL;
483         }
484         if (!tevent_req_is_in_progress(smb2req->subreq)) {
485                 return NULL;
486         }
487         inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
488         if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
489                 return NULL;
490         }
491         state = tevent_req_data(smb2req->subreq,
492                         struct smbd_smb2_lock_state);
493         if (!state) {
494                 return NULL;
495         }
496         return state->blr;
497 }
498 /****************************************************************
499  Set up the next brl timeout.
500 *****************************************************************/
501
502 static bool recalc_smb2_brl_timeout(struct smbd_server_connection *sconn)
503 {
504         struct smbd_smb2_request *smb2req;
505         struct timeval next_timeout = timeval_zero();
506         int max_brl_timeout = lp_parm_int(-1, "brl", "recalctime", 5);
507
508         TALLOC_FREE(sconn->smb2.locks.brl_timeout);
509
510         for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
511                 struct blocking_lock_record *blr =
512                         get_pending_smb2req_blr(smb2req);
513                 if (!blr) {
514                         continue;
515                 }
516                 if (timeval_is_zero(&blr->expire_time)) {
517                         /*
518                          * If we're blocked on pid 0xFFFFFFFFFFFFFFFFLL this is
519                          * a POSIX lock, so calculate a timeout of
520                          * 10 seconds into the future.
521                          */
522                         if (blr->blocking_smblctx == 0xFFFFFFFFFFFFFFFFLL) {
523                                 struct timeval psx_to = timeval_current_ofs(10, 0);
524                                 next_timeout = timeval_brl_min(&next_timeout, &psx_to);
525                         }
526
527                         continue;
528                 }
529
530                 next_timeout = timeval_brl_min(&next_timeout, &blr->expire_time);
531         }
532
533         if (timeval_is_zero(&next_timeout)) {
534                 DEBUG(10, ("recalc_smb2_brl_timeout:Next "
535                         "timeout = Infinite.\n"));
536                 return true;
537         }
538
539         /*
540          * To account for unclean shutdowns by clients we need a
541          * maximum timeout that we use for checking pending locks. If
542          * we have any pending locks at all, then check if the pending
543          * lock can continue at least every brl:recalctime seconds
544          * (default 5 seconds).
545          *
546          * This saves us needing to do a message_send_all() in the
547          * SIGCHLD handler in the parent daemon. That
548          * message_send_all() caused O(n^2) work to be done when IP
549          * failovers happened in clustered Samba, which could make the
550          * entire system unusable for many minutes.
551          */
552
553         if (max_brl_timeout > 0) {
554                 struct timeval min_to = timeval_current_ofs(max_brl_timeout, 0);
555                 next_timeout = timeval_brl_min(&next_timeout, &min_to);
556         }
557
558         if (DEBUGLVL(10)) {
559                 struct timeval cur, from_now;
560
561                 cur = timeval_current();
562                 from_now = timeval_until(&cur, &next_timeout);
563                 DEBUG(10, ("recalc_smb2_brl_timeout: Next "
564                         "timeout = %d.%d seconds from now.\n",
565                         (int)from_now.tv_sec, (int)from_now.tv_usec));
566         }
567
568         sconn->smb2.locks.brl_timeout = event_add_timed(
569                                 smbd_event_context(),
570                                 NULL,
571                                 next_timeout,
572                                 brl_timeout_fn,
573                                 NULL);
574         if (!sconn->smb2.locks.brl_timeout) {
575                 return false;
576         }
577         return true;
578 }
579
580 /****************************************************************
581  Get an SMB2 lock reqeust to go async. lock_timeout should
582  always be -1 here.
583 *****************************************************************/
584
585 bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck,
586                                 struct smb_request *smb1req,
587                                 files_struct *fsp,
588                                 int lock_timeout,
589                                 int lock_num,
590                                 uint64_t smblctx,
591                                 enum brl_type lock_type,
592                                 enum brl_flavour lock_flav,
593                                 uint64_t offset,
594                                 uint64_t count,
595                                 uint64_t blocking_smblctx)
596 {
597         struct smbd_server_connection *sconn = smb1req->sconn;
598         struct smbd_smb2_request *smb2req = smb1req->smb2req;
599         struct tevent_req *req = NULL;
600         struct smbd_smb2_lock_state *state = NULL;
601         struct blocking_lock_record *blr = NULL;
602         NTSTATUS status = NT_STATUS_OK;
603
604         if (!smb2req) {
605                 return false;
606         }
607         req = smb2req->subreq;
608         if (!req) {
609                 return false;
610         }
611         if (!tevent_req_is_in_progress(smb2req->subreq)) {
612                 return false;
613         }
614         state = tevent_req_data(req, struct smbd_smb2_lock_state);
615         if (!state) {
616                 return false;
617         }
618
619         blr = talloc_zero(state, struct blocking_lock_record);
620         if (!blr) {
621                 return false;
622         }
623         blr->fsp = fsp;
624
625         if (lock_timeout == -1) {
626                 blr->expire_time.tv_sec = 0;
627                 blr->expire_time.tv_usec = 0; /* Never expire. */
628         } else {
629                 blr->expire_time = timeval_current_ofs(
630                         lock_timeout/1000,
631                         (lock_timeout % 1000) * 1000);
632         }
633
634         blr->lock_num = lock_num;
635         blr->smblctx = smblctx;
636         blr->blocking_smblctx = blocking_smblctx;
637         blr->lock_flav = lock_flav;
638         blr->lock_type = lock_type;
639         blr->offset = offset;
640         blr->count = count;
641
642         /* Specific brl_lock() implementations can fill this in. */
643         blr->blr_private = NULL;
644
645         /* Add a pending lock record for this. */
646         status = brl_lock(sconn->msg_ctx,
647                         br_lck,
648                         smblctx,
649                         sconn_server_id(sconn),
650                         offset,
651                         count,
652                         lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
653                         blr->lock_flav,
654                         true,
655                         NULL,
656                         blr);
657
658         if (!NT_STATUS_IS_OK(status)) {
659                 DEBUG(0,("push_blocking_lock_request_smb2: "
660                         "failed to add PENDING_LOCK record.\n"));
661                 TALLOC_FREE(blr);
662                 return false;
663         }
664         state->blr = blr;
665
666         DEBUG(10,("push_blocking_lock_request_smb2: file %s timeout %d\n",
667                 fsp_str_dbg(fsp),
668                 lock_timeout ));
669
670         recalc_smb2_brl_timeout(sconn);
671
672         /* Ensure we'll receive messages when this is unlocked. */
673         if (!sconn->smb2.locks.blocking_lock_unlock_state) {
674                 messaging_register(sconn->msg_ctx, NULL,
675                                 MSG_SMB_UNLOCK, received_unlock_msg);
676                 sconn->smb2.locks.blocking_lock_unlock_state = true;
677         }
678
679         /* allow this request to be canceled */
680         tevent_req_set_cancel_fn(req, smbd_smb2_lock_cancel);
681
682         return true;
683 }
684
685 /****************************************************************
686  Remove a pending lock record under lock.
687 *****************************************************************/
688
689 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
690                         struct blocking_lock_record *blr)
691 {
692         int i;
693         struct byte_range_lock *br_lck = brl_get_locks(
694                                 state, blr->fsp);
695
696         DEBUG(10, ("remove_pending_lock: BLR = %p\n", blr));
697
698         if (br_lck) {
699                 brl_lock_cancel(br_lck,
700                                 blr->smblctx,
701                                 sconn_server_id(blr->fsp->conn->sconn),
702                                 blr->offset,
703                                 blr->count,
704                                 blr->lock_flav,
705                                 blr);
706                 TALLOC_FREE(br_lck);
707         }
708
709         /* Remove the locks we already got. */
710
711         for(i = blr->lock_num - 1; i >= 0; i--) {
712                 struct smbd_lock_element *e = &state->locks[i];
713
714                 do_unlock(blr->fsp->conn->sconn->msg_ctx,
715                         blr->fsp,
716                         e->smblctx,
717                         e->count,
718                         e->offset,
719                         WINDOWS_LOCK);
720         }
721 }
722
723 /****************************************************************
724  Re-proccess a blocking lock request.
725  This is equivalent to process_lockingX() inside smbd/blocking.c
726 *****************************************************************/
727
728 static void reprocess_blocked_smb2_lock(struct smbd_smb2_request *smb2req,
729                                 struct timeval tv_curr)
730 {
731         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
732         struct blocking_lock_record *blr = NULL;
733         struct smbd_smb2_lock_state *state = NULL;
734         files_struct *fsp = NULL;
735
736         if (!smb2req->subreq) {
737                 return;
738         }
739         state = tevent_req_data(smb2req->subreq, struct smbd_smb2_lock_state);
740         if (!state) {
741                 return;
742         }
743
744         blr = state->blr;
745         fsp = blr->fsp;
746
747         /* Try and finish off getting all the outstanding locks. */
748
749         for (; blr->lock_num < state->lock_count; blr->lock_num++) {
750                 struct byte_range_lock *br_lck = NULL;
751                 struct smbd_lock_element *e = &state->locks[blr->lock_num];
752
753                 br_lck = do_lock(fsp->conn->sconn->msg_ctx,
754                                 fsp,
755                                 e->smblctx,
756                                 e->count,
757                                 e->offset,
758                                 e->brltype,
759                                 WINDOWS_LOCK,
760                                 true,
761                                 &status,
762                                 &blr->blocking_smblctx,
763                                 blr);
764
765                 TALLOC_FREE(br_lck);
766
767                 if (NT_STATUS_IS_ERR(status)) {
768                         break;
769                 }
770         }
771
772         if(blr->lock_num == state->lock_count) {
773                 /*
774                  * Success - we got all the locks.
775                  */
776
777                 DEBUG(3,("reprocess_blocked_smb2_lock SUCCESS file = %s, "
778                         "fnum=%d num_locks=%d\n",
779                         fsp_str_dbg(fsp),
780                         fsp->fnum,
781                         (int)state->lock_count));
782
783                 tevent_req_done(smb2req->subreq);
784                 return;
785         }
786
787         if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
788                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
789                 /*
790                  * We have other than a "can't get lock"
791                  * error. Return an error.
792                  */
793                 remove_pending_lock(state, blr);
794                 tevent_req_nterror(smb2req->subreq, status);
795                 return;
796         }
797
798         /*
799          * We couldn't get the locks for this record on the list.
800          * If the time has expired, return a lock error.
801          */
802
803         if (!timeval_is_zero(&blr->expire_time) &&
804                         timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
805                 remove_pending_lock(state, blr);
806                 tevent_req_nterror(smb2req->subreq, NT_STATUS_LOCK_NOT_GRANTED);
807                 return;
808         }
809
810         /*
811          * Still can't get all the locks - keep waiting.
812          */
813
814         DEBUG(10,("reprocess_blocked_smb2_lock: only got %d locks of %d needed "
815                 "for file %s, fnum = %d. Still waiting....\n",
816                 (int)blr->lock_num,
817                 (int)state->lock_count,
818                 fsp_str_dbg(fsp),
819                 (int)fsp->fnum));
820
821         return;
822
823 }
824
825 /****************************************************************
826  Attempt to proccess all outstanding blocking locks pending on
827  the request queue.
828 *****************************************************************/
829
830 void process_blocking_lock_queue_smb2(
831         struct smbd_server_connection *sconn, struct timeval tv_curr)
832 {
833         struct smbd_smb2_request *smb2req, *nextreq;
834
835         for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
836                 const uint8_t *inhdr;
837
838                 nextreq = smb2req->next;
839
840                 if (smb2req->subreq == NULL) {
841                         /* This message has been processed. */
842                         continue;
843                 }
844                 if (!tevent_req_is_in_progress(smb2req->subreq)) {
845                         /* This message has been processed. */
846                         continue;
847                 }
848
849                 inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
850                 if (SVAL(inhdr, SMB2_HDR_OPCODE) == SMB2_OP_LOCK) {
851                         reprocess_blocked_smb2_lock(smb2req, tv_curr);
852                 }
853         }
854
855         recalc_smb2_brl_timeout(sconn);
856 }
857
858 /****************************************************************************
859  Remove any locks on this fd. Called from file_close().
860 ****************************************************************************/
861
862 void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
863                         struct byte_range_lock *br_lck,
864                         enum file_close_type close_type)
865 {
866         struct smbd_server_connection *sconn = fsp->conn->sconn;
867         struct smbd_smb2_request *smb2req, *nextreq;
868
869         for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
870                 struct smbd_smb2_lock_state *state = NULL;
871                 files_struct *fsp_curr = NULL;
872                 int i = smb2req->current_idx;
873                 uint64_t in_file_id_volatile;
874                 struct blocking_lock_record *blr = NULL;
875                 const uint8_t *inhdr;
876                 const uint8_t *inbody;
877
878                 nextreq = smb2req->next;
879
880                 if (smb2req->subreq == NULL) {
881                         /* This message has been processed. */
882                         continue;
883                 }
884                 if (!tevent_req_is_in_progress(smb2req->subreq)) {
885                         /* This message has been processed. */
886                         continue;
887                 }
888
889                 inhdr = (const uint8_t *)smb2req->in.vector[i].iov_base;
890                 if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
891                         /* Not a lock call. */
892                         continue;
893                 }
894
895                 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
896                 in_file_id_volatile = BVAL(inbody, 0x10);
897
898                 state = tevent_req_data(smb2req->subreq,
899                                 struct smbd_smb2_lock_state);
900                 if (!state) {
901                         /* Strange - is this even possible ? */
902                         continue;
903                 }
904
905                 fsp_curr = file_fsp(state->smb1req, (uint16_t)in_file_id_volatile);
906                 if (fsp_curr == NULL) {
907                         /* Strange - is this even possible ? */
908                         continue;
909                 }
910
911                 if (fsp_curr != fsp) {
912                         /* It's not our fid */
913                         continue;
914                 }
915
916                 blr = state->blr;
917
918                 /* Remove the entries from the lock db. */
919                 brl_lock_cancel(br_lck,
920                                 blr->smblctx,
921                                 sconn_server_id(sconn),
922                                 blr->offset,
923                                 blr->count,
924                                 blr->lock_flav,
925                                 blr);
926
927                 /* Finally end the request. */
928                 if (close_type == SHUTDOWN_CLOSE) {
929                         tevent_req_done(smb2req->subreq);
930                 } else {
931                         tevent_req_nterror(smb2req->subreq,
932                                 NT_STATUS_RANGE_NOT_LOCKED);
933                 }
934         }
935 }