traffic_packets: provision request data for packet_drsuapi_13
[Samba.git] / source3 / smbd / smb2_lock.c
bloba05470e52e4a923dd162afbb8a3a9e85eeacaeec
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->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);
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->sconn->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));
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;
580 return true;
583 /****************************************************************
584 Get an SMB2 lock request to go async. lock_timeout should
585 always be -1 here.
586 *****************************************************************/
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)
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;
607 if (!smb2req) {
608 return false;
610 req = smb2req->subreq;
611 if (!req) {
612 return false;
614 if (!tevent_req_is_in_progress(smb2req->subreq)) {
615 return false;
617 state = tevent_req_data(req, struct smbd_smb2_lock_state);
618 if (!state) {
619 return false;
622 blr = talloc_zero(state, struct blocking_lock_record);
623 if (!blr) {
624 return false;
626 blr->fsp = fsp;
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);
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;
643 /* Specific brl_lock() implementations can fill this in. */
644 blr->blr_private = NULL;
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);
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;
664 state->blr = blr;
666 DEBUG(10,("push_blocking_lock_request_smb2: file %s timeout %d\n",
667 fsp_str_dbg(fsp),
668 lock_timeout ));
670 recalc_smb2_brl_timeout(sconn);
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;
679 /* allow this request to be canceled */
680 tevent_req_set_cancel_fn(req, smbd_smb2_lock_cancel);
682 return true;
685 /****************************************************************
686 Remove a pending lock record under lock.
687 *****************************************************************/
689 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
690 struct blocking_lock_record *blr)
692 struct byte_range_lock *br_lck = brl_get_locks(
693 state, blr->fsp);
695 DEBUG(10, ("remove_pending_lock: BLR = %p\n", blr));
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);
708 /****************************************************************
709 Re-proccess a blocking lock request.
710 This is equivalent to process_lockingX() inside smbd/blocking.c
711 *****************************************************************/
713 static void reprocess_blocked_smb2_lock(struct smbd_smb2_request *smb2req,
714 struct timeval tv_curr)
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;
723 if (!smb2req->subreq) {
724 return;
726 SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(smb2req->profile);
728 state = tevent_req_data(smb2req->subreq, struct smbd_smb2_lock_state);
729 if (!state) {
730 return;
733 blr = state->blr;
734 fsp = blr->fsp;
736 /* We can only have one blocked lock in SMB2. */
737 SMB_ASSERT(state->lock_count == 1);
738 SMB_ASSERT(blr->lock_num == 0);
740 /* Try and get the outstanding lock. */
741 e = &state->locks[blr->lock_num];
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);
754 TALLOC_FREE(br_lck);
756 if (NT_STATUS_IS_OK(status)) {
758 * Success - we got the lock.
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));
767 remove_pending_lock(state, blr);
768 tevent_req_done(smb2req->subreq);
769 return;
772 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
773 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
775 * We have other than a "can't get lock"
776 * error. Return an error.
778 remove_pending_lock(state, blr);
779 tevent_req_nterror(smb2req->subreq, status);
780 return;
784 * We couldn't get the lock for this record.
785 * If the time has expired, return a lock error.
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;
796 * Still can't get the lock - keep waiting.
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)));
804 SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
805 return;
808 /****************************************************************
809 Attempt to proccess all outstanding blocking locks pending on
810 the request queue.
811 *****************************************************************/
813 void process_blocking_lock_queue_smb2(
814 struct smbd_server_connection *sconn, struct timeval tv_curr)
816 struct smbXsrv_connection *xconn = NULL;
818 if (sconn != NULL && sconn->client != NULL) {
819 xconn = sconn->client->connections;
822 for (; xconn != NULL; xconn = xconn->next) {
823 struct smbd_smb2_request *smb2req, *nextreq;
825 for (smb2req = xconn->smb2.requests; smb2req; smb2req = nextreq) {
826 const uint8_t *inhdr;
828 nextreq = smb2req->next;
830 if (smb2req->subreq == NULL) {
831 /* This message has been processed. */
832 continue;
834 if (!tevent_req_is_in_progress(smb2req->subreq)) {
835 /* This message has been processed. */
836 continue;
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);
846 recalc_smb2_brl_timeout(sconn);
849 /****************************************************************************
850 Remove any locks on this fd. Called from file_close().
851 ****************************************************************************/
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)
857 struct smbd_server_connection *sconn = fsp->conn->sconn;
858 struct smbXsrv_connection *xconn = NULL;
860 if (sconn != NULL && sconn->client != NULL) {
861 xconn = sconn->client->connections;
864 for (; xconn != NULL; xconn = xconn->next) {
865 struct smbd_smb2_request *smb2req, *nextreq;
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;
873 nextreq = smb2req->next;
875 if (smb2req->subreq == NULL) {
876 /* This message has been processed. */
877 continue;
879 if (!tevent_req_is_in_progress(smb2req->subreq)) {
880 /* This message has been processed. */
881 continue;
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;
890 state = tevent_req_data(smb2req->subreq,
891 struct smbd_smb2_lock_state);
892 if (!state) {
893 /* Strange - is this even possible ? */
894 continue;
897 fsp_curr = smb2req->compat_chain_fsp;
898 if (fsp_curr == NULL) {
899 /* Strange - is this even possible ? */
900 continue;
903 if (fsp_curr != fsp) {
904 /* It's not our fid */
905 continue;
908 blr = state->blr;
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);
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);