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