s4/selftest: Enable samba.tests.samba_tool.join for py3
[Samba.git] / source3 / smbd / smb2_lock.c
blobda5a54df623349c15e38ea829ff0384559c84a1e
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
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.
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.
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/>.
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"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_SMB2
32 struct smbd_smb2_lock_element {
33 uint64_t offset;
34 uint64_t length;
35 uint32_t flags;
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;
46 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
47 struct blocking_lock_record *blr);
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);
57 static void smbd_smb2_request_lock_done(struct tevent_req *subreq);
58 NTSTATUS smbd_smb2_request_process_lock(struct smbd_smb2_request *req)
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;
71 status = smbd_smb2_request_verify_sizes(req, 0x30);
72 if (!NT_STATUS_IS_OK(status)) {
73 return smbd_smb2_request_error(req, status);
75 inbody = SMBD_SMB2_IN_BODY_PTR(req);
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);
82 if (in_lock_count < 1) {
83 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
86 if (((in_lock_count - 1) * 0x18) > SMBD_SMB2_IN_DYN_LEN(req)) {
87 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
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);
96 l = 0;
97 lock_buffer = inbody + 0x18;
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 */
104 status = req->session->status;
105 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
107 * We need to catch NT_STATUS_NETWORK_SESSION_EXPIRED
108 * for lock requests only.
110 * Unlock requests still need to be processed!
112 * This means smbd_smb2_request_check_session()
113 * can't handle the difference and always
114 * allows SMB2_OP_LOCK.
116 if (in_locks[0].flags != SMB2_LOCK_FLAG_UNLOCK) {
117 return smbd_smb2_request_error(req, status);
121 lock_buffer = SMBD_SMB2_IN_DYN_PTR(req);
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 */
129 lock_buffer += 0x18;
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);
137 subreq = smbd_smb2_lock_send(req, req->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);
144 tevent_req_set_callback(subreq, smbd_smb2_request_lock_done, req);
146 return smbd_smb2_request_pending_queue(req, subreq, 500);
149 static void smbd_smb2_request_lock_done(struct tevent_req *subreq)
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 */
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;
166 return;
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;
177 return;
180 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
181 SSVAL(outbody.data, 0x02, 0); /* reserved */
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;
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)
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;
208 req = tevent_req_create(mem_ctx, &state,
209 struct smbd_smb2_lock_state);
210 if (req == NULL) {
211 return NULL;
213 state->smb2req = smb2req;
214 smb2req->subreq = req; /* So we can find this when going async. */
216 smb1req = smbd_smb2_fake_smb_request(smb2req);
217 if (tevent_req_nomem(smb1req, req)) {
218 return tevent_req_post(req, ev);
220 state->smb1req = smb1req;
222 DEBUG(10,("smbd_smb2_lock_send: %s - %s\n",
223 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
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);
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);
238 timeout = -1;
239 break;
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;
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;
253 default:
254 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
255 return tevent_req_post(req, ev);
258 if (!isunlock && (in_lock_count > 1)) {
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.
265 for (i=0; i<in_lock_count; i++) {
266 uint32_t flags = in_locks[i].flags;
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);
276 for (i=0; i<in_lock_count; i++) {
277 bool invalid = false;
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;
286 break;
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;
293 break;
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);
301 break;
303 default:
304 if (isunlock) {
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
311 invalid = true;
312 break;
314 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
315 return tevent_req_post(req, ev);
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;
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) {
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
333 locks[i].brltype = READ_LOCK;
334 } else {
335 locks[i].brltype = UNLOCK_LOCK;
338 DEBUG(10,("smbd_smb2_lock_send: index %d offset=%llu, count=%llu, "
339 "smblctx = %llu type %d\n",
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 ));
347 state->locks = locks;
348 state->lock_count = in_lock_count;
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,
357 timeout,
358 in_lock_count,
359 locks,
360 &async);
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;
366 tevent_req_nterror(req, status);
367 return tevent_req_post(req, ev);
370 if (async) {
371 tevent_req_defer_callback(req, smb2req->ev_ctx);
372 SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
373 return req;
376 tevent_req_done(req);
377 return tevent_req_post(req, ev);
380 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req)
382 NTSTATUS status;
384 if (tevent_req_is_nterror(req, &status)) {
385 tevent_req_received(req);
386 return status;
389 tevent_req_received(req);
390 return NT_STATUS_OK;
393 /****************************************************************
394 Cancel an outstanding blocking lock request.
395 *****************************************************************/
397 static bool smbd_smb2_lock_cancel(struct tevent_req *req)
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;
406 if (!state->smb2req) {
407 return false;
410 smb2req = state->smb2req;
412 remove_pending_lock(state, state->blr);
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.
419 * Note that the close case is handled in
420 * cancel_pending_lock_requests_by_fid_smb2(SHUTDOWN_CLOSE)
421 * for now.
423 if (!NT_STATUS_IS_OK(smb2req->session->status)) {
424 tevent_req_nterror(req, NT_STATUS_RANGE_NOT_LOCKED);
425 return true;
428 if (!NT_STATUS_IS_OK(smb2req->tcon->status)) {
429 tevent_req_nterror(req, NT_STATUS_RANGE_NOT_LOCKED);
430 return true;
433 tevent_req_nterror(req, NT_STATUS_CANCELLED);
434 return true;
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 *****************************************************************/
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)
448 struct smbd_server_connection *sconn =
449 talloc_get_type_abort(private_data,
450 struct smbd_server_connection);
452 DEBUG(10,("received_unlock_msg (SMB2)\n"));
454 process_blocking_lock_queue_smb2(sconn, timeval_current());
457 /****************************************************************
458 Function to get the blr on a pending record.
459 *****************************************************************/
461 struct blocking_lock_record *get_pending_smb2req_blr(struct smbd_smb2_request *smb2req)
463 struct smbd_smb2_lock_state *state = NULL;
464 const uint8_t *inhdr;
466 if (!smb2req) {
467 return NULL;
469 if (smb2req->subreq == NULL) {
470 return NULL;
472 if (!tevent_req_is_in_progress(smb2req->subreq)) {
473 return NULL;
475 inhdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
476 if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
477 return NULL;
479 state = tevent_req_data(smb2req->subreq,
480 struct smbd_smb2_lock_state);
481 if (!state) {
482 return NULL;
484 return state->blr;
486 /****************************************************************
487 Set up the next brl timeout.
488 *****************************************************************/
490 static bool recalc_smb2_brl_timeout(struct smbd_server_connection *sconn)
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);
496 TALLOC_FREE(sconn->smb2.locks.brl_timeout);
498 if (sconn->client != NULL) {
499 xconn = sconn->client->connections;
502 for (; xconn != NULL; xconn = xconn->next) {
503 struct smbd_smb2_request *smb2req, *nextreq;
505 for (smb2req = xconn->smb2.requests; smb2req; smb2req = nextreq) {
506 struct blocking_lock_record *blr =
507 get_pending_smb2req_blr(smb2req);
509 nextreq = smb2req->next;
511 if (blr == NULL) {
512 continue;
515 if (!timeval_is_zero(&blr->expire_time)) {
516 next_timeout = timeval_brl_min(&next_timeout,
517 &blr->expire_time);
518 continue;
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.
526 if (blr->blocking_smblctx == 0xFFFFFFFFFFFFFFFFLL) {
527 struct timeval psx_to;
529 psx_to = timeval_current_ofs(10, 0);
530 next_timeout = timeval_brl_min(&next_timeout,
531 &psx_to);
536 if (timeval_is_zero(&next_timeout)) {
537 DEBUG(10, ("recalc_smb2_brl_timeout:Next "
538 "timeout = Infinite.\n"));
539 return true;
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).
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.
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);
561 if (DEBUGLVL(10)) {
562 struct timeval cur, from_now;
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));
572 * brl_timeout_fn() calls change_to_root_user()
573 * so we can use sconn->root_ev_ctx.
575 sconn->smb2.locks.brl_timeout = tevent_add_timer(
576 sconn->root_ev_ctx,
577 NULL,
578 next_timeout,
579 brl_timeout_fn,
580 sconn);
581 if (!sconn->smb2.locks.brl_timeout) {
582 return false;
584 return true;
587 /****************************************************************
588 Get an SMB2 lock request to go async. lock_timeout should
589 always be -1 here.
590 *****************************************************************/
592 bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck,
593 struct smb_request *smb1req,
594 files_struct *fsp,
595 int lock_timeout,
596 int lock_num,
597 uint64_t smblctx,
598 enum brl_type lock_type,
599 enum brl_flavour lock_flav,
600 uint64_t offset,
601 uint64_t count,
602 uint64_t blocking_smblctx)
604 struct smbd_server_connection *sconn = smb1req->sconn;
605 struct smbd_smb2_request *smb2req = smb1req->smb2req;
606 struct tevent_req *req = NULL;
607 struct smbd_smb2_lock_state *state = NULL;
608 struct blocking_lock_record *blr = NULL;
609 NTSTATUS status = NT_STATUS_OK;
611 if (!smb2req) {
612 return false;
614 req = smb2req->subreq;
615 if (!req) {
616 return false;
618 if (!tevent_req_is_in_progress(smb2req->subreq)) {
619 return false;
621 state = tevent_req_data(req, struct smbd_smb2_lock_state);
622 if (!state) {
623 return false;
626 blr = talloc_zero(state, struct blocking_lock_record);
627 if (!blr) {
628 return false;
630 blr->fsp = fsp;
632 if (lock_timeout == -1) {
633 blr->expire_time.tv_sec = 0;
634 blr->expire_time.tv_usec = 0; /* Never expire. */
635 } else {
636 blr->expire_time = timeval_current_ofs_msec(lock_timeout);
639 blr->lock_num = lock_num;
640 blr->smblctx = smblctx;
641 blr->blocking_smblctx = blocking_smblctx;
642 blr->lock_flav = lock_flav;
643 blr->lock_type = lock_type;
644 blr->offset = offset;
645 blr->count = count;
647 /* Specific brl_lock() implementations can fill this in. */
648 blr->blr_private = NULL;
650 /* Add a pending lock record for this. */
651 status = brl_lock(sconn->msg_ctx,
652 br_lck,
653 smblctx,
654 messaging_server_id(sconn->msg_ctx),
655 offset,
656 count,
657 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
658 blr->lock_flav,
659 true,
660 NULL);
662 if (!NT_STATUS_IS_OK(status)) {
663 DEBUG(0,("push_blocking_lock_request_smb2: "
664 "failed to add PENDING_LOCK record.\n"));
665 TALLOC_FREE(blr);
666 return false;
668 state->blr = blr;
670 DEBUG(10,("push_blocking_lock_request_smb2: file %s timeout %d\n",
671 fsp_str_dbg(fsp),
672 lock_timeout ));
674 recalc_smb2_brl_timeout(sconn);
676 /* Ensure we'll receive messages when this is unlocked. */
677 if (!sconn->smb2.locks.blocking_lock_unlock_state) {
678 messaging_register(sconn->msg_ctx, sconn,
679 MSG_SMB_UNLOCK, received_unlock_msg);
680 sconn->smb2.locks.blocking_lock_unlock_state = true;
683 /* allow this request to be canceled */
684 tevent_req_set_cancel_fn(req, smbd_smb2_lock_cancel);
686 return true;
689 /****************************************************************
690 Remove a pending lock record under lock.
691 *****************************************************************/
693 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
694 struct blocking_lock_record *blr)
696 struct byte_range_lock *br_lck = brl_get_locks(
697 state, blr->fsp);
699 DEBUG(10, ("remove_pending_lock: BLR = %p\n", blr));
701 if (br_lck) {
702 brl_lock_cancel(br_lck,
703 blr->smblctx,
704 messaging_server_id(blr->fsp->conn->sconn->msg_ctx),
705 blr->offset,
706 blr->count,
707 blr->lock_flav);
708 TALLOC_FREE(br_lck);
712 /****************************************************************
713 Re-proccess a blocking lock request.
714 This is equivalent to process_lockingX() inside smbd/blocking.c
715 *****************************************************************/
717 static void reprocess_blocked_smb2_lock(struct smbd_smb2_request *smb2req,
718 struct timeval tv_curr)
720 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
721 struct blocking_lock_record *blr = NULL;
722 struct smbd_smb2_lock_state *state = NULL;
723 struct byte_range_lock *br_lck = NULL;
724 struct smbd_lock_element *e = NULL;
725 files_struct *fsp = NULL;
727 if (!smb2req->subreq) {
728 return;
730 SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(smb2req->profile);
732 state = tevent_req_data(smb2req->subreq, struct smbd_smb2_lock_state);
733 if (!state) {
734 return;
737 blr = state->blr;
738 fsp = blr->fsp;
740 /* We can only have one blocked lock in SMB2. */
741 SMB_ASSERT(state->lock_count == 1);
742 SMB_ASSERT(blr->lock_num == 0);
744 /* Try and get the outstanding lock. */
745 e = &state->locks[blr->lock_num];
747 br_lck = do_lock(fsp->conn->sconn->msg_ctx,
748 fsp,
749 e->smblctx,
750 e->count,
751 e->offset,
752 e->brltype,
753 WINDOWS_LOCK,
754 true,
755 &status,
756 &blr->blocking_smblctx);
758 TALLOC_FREE(br_lck);
760 if (NT_STATUS_IS_OK(status)) {
762 * Success - we got the lock.
765 DEBUG(3,("reprocess_blocked_smb2_lock SUCCESS file = %s, "
766 "%s, num_locks=%d\n",
767 fsp_str_dbg(fsp),
768 fsp_fnum_dbg(fsp),
769 (int)state->lock_count));
771 remove_pending_lock(state, blr);
772 tevent_req_done(smb2req->subreq);
773 return;
776 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
777 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
779 * We have other than a "can't get lock"
780 * error. Return an error.
782 remove_pending_lock(state, blr);
783 tevent_req_nterror(smb2req->subreq, status);
784 return;
788 * We couldn't get the lock for this record.
789 * If the time has expired, return a lock error.
792 if (!timeval_is_zero(&blr->expire_time) &&
793 timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
794 remove_pending_lock(state, blr);
795 tevent_req_nterror(smb2req->subreq, NT_STATUS_LOCK_NOT_GRANTED);
796 return;
800 * Still can't get the lock - keep waiting.
803 DEBUG(10,("reprocess_blocked_smb2_lock: failed to get lock "
804 "for file %s, %s. Still waiting....\n",
805 fsp_str_dbg(fsp),
806 fsp_fnum_dbg(fsp)));
808 SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
809 return;
812 /****************************************************************
813 Attempt to proccess all outstanding blocking locks pending on
814 the request queue.
815 *****************************************************************/
817 void process_blocking_lock_queue_smb2(
818 struct smbd_server_connection *sconn, struct timeval tv_curr)
820 struct smbXsrv_connection *xconn = NULL;
822 if (sconn != NULL && sconn->client != NULL) {
823 xconn = sconn->client->connections;
826 for (; xconn != NULL; xconn = xconn->next) {
827 struct smbd_smb2_request *smb2req, *nextreq;
829 for (smb2req = xconn->smb2.requests; smb2req; smb2req = nextreq) {
830 const uint8_t *inhdr;
832 nextreq = smb2req->next;
834 if (smb2req->subreq == NULL) {
835 /* This message has been processed. */
836 continue;
838 if (!tevent_req_is_in_progress(smb2req->subreq)) {
839 /* This message has been processed. */
840 continue;
843 inhdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
844 if (SVAL(inhdr, SMB2_HDR_OPCODE) == SMB2_OP_LOCK) {
845 reprocess_blocked_smb2_lock(smb2req, tv_curr);
850 recalc_smb2_brl_timeout(sconn);
853 /****************************************************************************
854 Remove any locks on this fd. Called from file_close().
855 ****************************************************************************/
857 void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
858 struct byte_range_lock *br_lck,
859 enum file_close_type close_type)
861 struct smbd_server_connection *sconn = fsp->conn->sconn;
862 struct smbXsrv_connection *xconn = NULL;
864 if (sconn != NULL && sconn->client != NULL) {
865 xconn = sconn->client->connections;
868 for (; xconn != NULL; xconn = xconn->next) {
869 struct smbd_smb2_request *smb2req, *nextreq;
871 for (smb2req = xconn->smb2.requests; smb2req; smb2req = nextreq) {
872 struct smbd_smb2_lock_state *state = NULL;
873 files_struct *fsp_curr = NULL;
874 struct blocking_lock_record *blr = NULL;
875 const uint8_t *inhdr;
877 nextreq = smb2req->next;
879 if (smb2req->subreq == NULL) {
880 /* This message has been processed. */
881 continue;
883 if (!tevent_req_is_in_progress(smb2req->subreq)) {
884 /* This message has been processed. */
885 continue;
888 inhdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
889 if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
890 /* Not a lock call. */
891 continue;
894 state = tevent_req_data(smb2req->subreq,
895 struct smbd_smb2_lock_state);
896 if (!state) {
897 /* Strange - is this even possible ? */
898 continue;
901 fsp_curr = smb2req->compat_chain_fsp;
902 if (fsp_curr == NULL) {
903 /* Strange - is this even possible ? */
904 continue;
907 if (fsp_curr != fsp) {
908 /* It's not our fid */
909 continue;
912 blr = state->blr;
914 /* Remove the entries from the lock db. */
915 brl_lock_cancel(br_lck,
916 blr->smblctx,
917 messaging_server_id(sconn->msg_ctx),
918 blr->offset,
919 blr->count,
920 blr->lock_flav);
922 /* Finally end the request. */
923 if (close_type == SHUTDOWN_CLOSE) {
924 tevent_req_done(smb2req->subreq);
925 } else {
926 tevent_req_nterror(smb2req->subreq,
927 NT_STATUS_RANGE_NOT_LOCKED);