s3:smbd: change smb2_lock.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_lock.c
blob68ad9e6e37eb411cace1e253b9a10406b759165f
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 struct smbd_smb2_lock_element {
30 uint64_t offset;
31 uint64_t length;
32 uint32_t flags;
35 struct smbd_smb2_lock_state {
36 struct smbd_smb2_request *smb2req;
37 struct smb_request *smb1req;
38 struct blocking_lock_record *blr;
39 uint16_t lock_count;
40 struct smbd_lock_element *locks;
43 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
44 struct blocking_lock_record *blr);
46 static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
47 struct tevent_context *ev,
48 struct smbd_smb2_request *smb2req,
49 struct files_struct *in_fsp,
50 uint32_t in_smbpid,
51 uint16_t in_lock_count,
52 struct smbd_smb2_lock_element *in_locks);
53 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req);
55 static void smbd_smb2_request_lock_done(struct tevent_req *subreq);
56 NTSTATUS smbd_smb2_request_process_lock(struct smbd_smb2_request *req)
58 const uint8_t *inhdr;
59 const uint8_t *inbody;
60 const int i = req->current_idx;
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 files_struct *in_fsp;
66 struct smbd_smb2_lock_element *in_locks;
67 struct tevent_req *subreq;
68 const uint8_t *lock_buffer;
69 uint16_t l;
70 NTSTATUS status;
72 status = smbd_smb2_request_verify_sizes(req, 0x30);
73 if (!NT_STATUS_IS_OK(status)) {
74 return smbd_smb2_request_error(req, status);
76 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
77 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
79 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
81 in_lock_count = CVAL(inbody, 0x02);
82 /* 0x04 - 4 bytes reserved */
83 in_file_id_persistent = BVAL(inbody, 0x08);
84 in_file_id_volatile = BVAL(inbody, 0x10);
86 if (in_lock_count < 1) {
87 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
90 if (((in_lock_count - 1) * 0x18) > req->in.vector[i+2].iov_len) {
91 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
94 in_locks = talloc_array(req, struct smbd_smb2_lock_element,
95 in_lock_count);
96 if (in_locks == NULL) {
97 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
100 l = 0;
101 lock_buffer = inbody + 0x18;
103 in_locks[l].offset = BVAL(lock_buffer, 0x00);
104 in_locks[l].length = BVAL(lock_buffer, 0x08);
105 in_locks[l].flags = IVAL(lock_buffer, 0x10);
106 /* 0x14 - 4 reserved bytes */
108 lock_buffer = (const uint8_t *)req->in.vector[i+2].iov_base;
110 for (l=1; l < in_lock_count; l++) {
111 in_locks[l].offset = BVAL(lock_buffer, 0x00);
112 in_locks[l].length = BVAL(lock_buffer, 0x08);
113 in_locks[l].flags = IVAL(lock_buffer, 0x10);
114 /* 0x14 - 4 reserved bytes */
116 lock_buffer += 0x18;
119 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
120 if (in_fsp == NULL) {
121 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
124 subreq = smbd_smb2_lock_send(req, req->sconn->ev_ctx,
125 req, in_fsp,
126 in_smbpid,
127 in_lock_count,
128 in_locks);
129 if (subreq == NULL) {
130 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
132 tevent_req_set_callback(subreq, smbd_smb2_request_lock_done, req);
134 return smbd_smb2_request_pending_queue(req, subreq, 500);
137 static void smbd_smb2_request_lock_done(struct tevent_req *subreq)
139 struct smbd_smb2_request *smb2req = tevent_req_callback_data(subreq,
140 struct smbd_smb2_request);
141 DATA_BLOB outbody;
142 NTSTATUS status;
143 NTSTATUS error; /* transport error */
145 if (smb2req->cancelled) {
146 const uint8_t *inhdr = (const uint8_t *)
147 smb2req->in.vector[smb2req->current_idx].iov_base;
148 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
149 struct smbd_smb2_lock_state *state;
151 DEBUG(10,("smbd_smb2_request_lock_done: cancelled mid %llu\n",
152 (unsigned long long)mid ));
154 state = tevent_req_data(smb2req->subreq,
155 struct smbd_smb2_lock_state);
157 SMB_ASSERT(state);
158 SMB_ASSERT(state->blr);
160 remove_pending_lock(state, state->blr);
162 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
163 if (!NT_STATUS_IS_OK(error)) {
164 smbd_server_connection_terminate(smb2req->sconn,
165 nt_errstr(error));
166 return;
168 return;
171 status = smbd_smb2_lock_recv(subreq);
172 TALLOC_FREE(subreq);
173 if (!NT_STATUS_IS_OK(status)) {
174 error = smbd_smb2_request_error(smb2req, status);
175 if (!NT_STATUS_IS_OK(error)) {
176 smbd_server_connection_terminate(smb2req->sconn,
177 nt_errstr(error));
178 return;
180 return;
183 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x04);
184 if (outbody.data == NULL) {
185 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
186 if (!NT_STATUS_IS_OK(error)) {
187 smbd_server_connection_terminate(smb2req->sconn,
188 nt_errstr(error));
189 return;
191 return;
194 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
195 SSVAL(outbody.data, 0x02, 0); /* reserved */
197 error = smbd_smb2_request_done(smb2req, outbody, NULL);
198 if (!NT_STATUS_IS_OK(error)) {
199 smbd_server_connection_terminate(smb2req->sconn,
200 nt_errstr(error));
201 return;
205 static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
206 struct tevent_context *ev,
207 struct smbd_smb2_request *smb2req,
208 struct files_struct *fsp,
209 uint32_t in_smbpid,
210 uint16_t in_lock_count,
211 struct smbd_smb2_lock_element *in_locks)
213 struct tevent_req *req;
214 struct smbd_smb2_lock_state *state;
215 struct smb_request *smb1req;
216 int32_t timeout = -1;
217 bool isunlock = false;
218 uint16_t i;
219 struct smbd_lock_element *locks;
220 NTSTATUS status;
221 bool async = false;
223 req = tevent_req_create(mem_ctx, &state,
224 struct smbd_smb2_lock_state);
225 if (req == NULL) {
226 return NULL;
228 state->smb2req = smb2req;
229 smb2req->subreq = req; /* So we can find this when going async. */
231 smb1req = smbd_smb2_fake_smb_request(smb2req);
232 if (tevent_req_nomem(smb1req, req)) {
233 return tevent_req_post(req, ev);
235 state->smb1req = smb1req;
237 DEBUG(10,("smbd_smb2_lock_send: %s - %s\n",
238 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
240 locks = talloc_array(state, struct smbd_lock_element, in_lock_count);
241 if (locks == NULL) {
242 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
243 return tevent_req_post(req, ev);
246 switch (in_locks[0].flags) {
247 case SMB2_LOCK_FLAG_SHARED:
248 case SMB2_LOCK_FLAG_EXCLUSIVE:
249 if (in_lock_count > 1) {
250 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
251 return tevent_req_post(req, ev);
253 timeout = -1;
254 break;
256 case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
257 case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
258 timeout = 0;
259 break;
261 case SMB2_LOCK_FLAG_UNLOCK:
262 /* only the first lock gives the UNLOCK bit - see
263 MS-SMB2 3.3.5.14 */
264 isunlock = true;
265 timeout = 0;
266 break;
268 default:
269 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
270 return tevent_req_post(req, ev);
273 for (i=0; i<in_lock_count; i++) {
274 bool invalid = false;
276 switch (in_locks[i].flags) {
277 case SMB2_LOCK_FLAG_SHARED:
278 case SMB2_LOCK_FLAG_EXCLUSIVE:
279 if (isunlock) {
280 invalid = true;
281 break;
283 if (i > 0) {
284 tevent_req_nterror(req,
285 NT_STATUS_INVALID_PARAMETER);
286 return tevent_req_post(req, ev);
288 break;
290 case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
291 case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
292 if (isunlock) {
293 invalid = true;
295 break;
297 case SMB2_LOCK_FLAG_UNLOCK:
298 if (!isunlock) {
299 tevent_req_nterror(req,
300 NT_STATUS_INVALID_PARAMETER);
301 return tevent_req_post(req, ev);
303 break;
305 default:
306 if (isunlock) {
308 * is the first element was a UNLOCK
309 * we need to deferr the error response
310 * to the backend, because we need to process
311 * all unlock elements before
313 invalid = true;
314 break;
316 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
317 return tevent_req_post(req, ev);
320 locks[i].smblctx = fsp->fnum;
321 locks[i].offset = in_locks[i].offset;
322 locks[i].count = in_locks[i].length;
324 if (in_locks[i].flags & SMB2_LOCK_FLAG_EXCLUSIVE) {
325 locks[i].brltype = WRITE_LOCK;
326 } else if (in_locks[i].flags & SMB2_LOCK_FLAG_SHARED) {
327 locks[i].brltype = READ_LOCK;
328 } else if (invalid) {
330 * this is an invalid UNLOCK element
331 * and the backend needs to test for
332 * brltype != UNLOCK_LOCK and return
333 * NT_STATUS_INVALID_PARAMER
335 locks[i].brltype = READ_LOCK;
336 } else {
337 locks[i].brltype = UNLOCK_LOCK;
340 DEBUG(10,("smbd_smb2_lock_send: index %d offset=%llu, count=%llu, "
341 "smblctx = %llu type %d\n",
343 (unsigned long long)locks[i].offset,
344 (unsigned long long)locks[i].count,
345 (unsigned long long)locks[i].smblctx,
346 (int)locks[i].brltype ));
349 state->locks = locks;
350 state->lock_count = in_lock_count;
352 if (isunlock) {
353 status = smbd_do_locking(smb1req, fsp,
355 timeout,
356 in_lock_count,
357 locks,
359 NULL,
360 &async);
361 } else {
362 status = smbd_do_locking(smb1req, fsp,
364 timeout,
366 NULL,
367 in_lock_count,
368 locks,
369 &async);
371 if (!NT_STATUS_IS_OK(status)) {
372 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
373 status = NT_STATUS_LOCK_NOT_GRANTED;
375 tevent_req_nterror(req, status);
376 return tevent_req_post(req, ev);
379 if (async) {
380 return req;
383 tevent_req_done(req);
384 return tevent_req_post(req, ev);
387 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req)
389 NTSTATUS status;
391 if (tevent_req_is_nterror(req, &status)) {
392 tevent_req_received(req);
393 return status;
396 tevent_req_received(req);
397 return NT_STATUS_OK;
400 /****************************************************************
401 Cancel an outstanding blocking lock request.
402 *****************************************************************/
404 static bool smbd_smb2_lock_cancel(struct tevent_req *req)
406 struct smbd_smb2_request *smb2req = NULL;
407 struct smbd_smb2_lock_state *state = tevent_req_data(req,
408 struct smbd_smb2_lock_state);
409 if (!state) {
410 return false;
413 if (!state->smb2req) {
414 return false;
417 smb2req = state->smb2req;
418 smb2req->cancelled = true;
420 tevent_req_done(req);
421 return true;
424 /****************************************************************
425 Got a message saying someone unlocked a file. Re-schedule all
426 blocking lock requests as we don't know if anything overlapped.
427 *****************************************************************/
429 static void received_unlock_msg(struct messaging_context *msg,
430 void *private_data,
431 uint32_t msg_type,
432 struct server_id server_id,
433 DATA_BLOB *data)
435 struct smbd_server_connection *sconn =
436 talloc_get_type_abort(private_data,
437 struct smbd_server_connection);
439 DEBUG(10,("received_unlock_msg (SMB2)\n"));
441 process_blocking_lock_queue_smb2(sconn, timeval_current());
444 /****************************************************************
445 Function to get the blr on a pending record.
446 *****************************************************************/
448 struct blocking_lock_record *get_pending_smb2req_blr(struct smbd_smb2_request *smb2req)
450 struct smbd_smb2_lock_state *state = NULL;
451 const uint8_t *inhdr;
453 if (!smb2req) {
454 return NULL;
456 if (smb2req->subreq == NULL) {
457 return NULL;
459 if (!tevent_req_is_in_progress(smb2req->subreq)) {
460 return NULL;
462 inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
463 if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
464 return NULL;
466 state = tevent_req_data(smb2req->subreq,
467 struct smbd_smb2_lock_state);
468 if (!state) {
469 return NULL;
471 return state->blr;
473 /****************************************************************
474 Set up the next brl timeout.
475 *****************************************************************/
477 static bool recalc_smb2_brl_timeout(struct smbd_server_connection *sconn)
479 struct smbd_smb2_request *smb2req;
480 struct timeval next_timeout = timeval_zero();
481 int max_brl_timeout = lp_parm_int(-1, "brl", "recalctime", 5);
483 TALLOC_FREE(sconn->smb2.locks.brl_timeout);
485 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
486 struct blocking_lock_record *blr =
487 get_pending_smb2req_blr(smb2req);
488 if (!blr) {
489 continue;
491 if (timeval_is_zero(&blr->expire_time)) {
493 * If we're blocked on pid 0xFFFFFFFFFFFFFFFFLL this is
494 * a POSIX lock, so calculate a timeout of
495 * 10 seconds into the future.
497 if (blr->blocking_smblctx == 0xFFFFFFFFFFFFFFFFLL) {
498 struct timeval psx_to = timeval_current_ofs(10, 0);
499 next_timeout = timeval_brl_min(&next_timeout, &psx_to);
502 continue;
505 next_timeout = timeval_brl_min(&next_timeout, &blr->expire_time);
508 if (timeval_is_zero(&next_timeout)) {
509 DEBUG(10, ("recalc_smb2_brl_timeout:Next "
510 "timeout = Infinite.\n"));
511 return true;
515 * To account for unclean shutdowns by clients we need a
516 * maximum timeout that we use for checking pending locks. If
517 * we have any pending locks at all, then check if the pending
518 * lock can continue at least every brl:recalctime seconds
519 * (default 5 seconds).
521 * This saves us needing to do a message_send_all() in the
522 * SIGCHLD handler in the parent daemon. That
523 * message_send_all() caused O(n^2) work to be done when IP
524 * failovers happened in clustered Samba, which could make the
525 * entire system unusable for many minutes.
528 if (max_brl_timeout > 0) {
529 struct timeval min_to = timeval_current_ofs(max_brl_timeout, 0);
530 next_timeout = timeval_brl_min(&next_timeout, &min_to);
533 if (DEBUGLVL(10)) {
534 struct timeval cur, from_now;
536 cur = timeval_current();
537 from_now = timeval_until(&cur, &next_timeout);
538 DEBUG(10, ("recalc_smb2_brl_timeout: Next "
539 "timeout = %d.%d seconds from now.\n",
540 (int)from_now.tv_sec, (int)from_now.tv_usec));
543 sconn->smb2.locks.brl_timeout = tevent_add_timer(
544 sconn->ev_ctx,
545 NULL,
546 next_timeout,
547 brl_timeout_fn,
548 NULL);
549 if (!sconn->smb2.locks.brl_timeout) {
550 return false;
552 return true;
555 /****************************************************************
556 Get an SMB2 lock reqeust to go async. lock_timeout should
557 always be -1 here.
558 *****************************************************************/
560 bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck,
561 struct smb_request *smb1req,
562 files_struct *fsp,
563 int lock_timeout,
564 int lock_num,
565 uint64_t smblctx,
566 enum brl_type lock_type,
567 enum brl_flavour lock_flav,
568 uint64_t offset,
569 uint64_t count,
570 uint64_t blocking_smblctx)
572 struct smbd_server_connection *sconn = smb1req->sconn;
573 struct smbd_smb2_request *smb2req = smb1req->smb2req;
574 struct tevent_req *req = NULL;
575 struct smbd_smb2_lock_state *state = NULL;
576 struct blocking_lock_record *blr = NULL;
577 NTSTATUS status = NT_STATUS_OK;
579 if (!smb2req) {
580 return false;
582 req = smb2req->subreq;
583 if (!req) {
584 return false;
586 if (!tevent_req_is_in_progress(smb2req->subreq)) {
587 return false;
589 state = tevent_req_data(req, struct smbd_smb2_lock_state);
590 if (!state) {
591 return false;
594 blr = talloc_zero(state, struct blocking_lock_record);
595 if (!blr) {
596 return false;
598 blr->fsp = fsp;
600 if (lock_timeout == -1) {
601 blr->expire_time.tv_sec = 0;
602 blr->expire_time.tv_usec = 0; /* Never expire. */
603 } else {
604 blr->expire_time = timeval_current_ofs_msec(lock_timeout);
607 blr->lock_num = lock_num;
608 blr->smblctx = smblctx;
609 blr->blocking_smblctx = blocking_smblctx;
610 blr->lock_flav = lock_flav;
611 blr->lock_type = lock_type;
612 blr->offset = offset;
613 blr->count = count;
615 /* Specific brl_lock() implementations can fill this in. */
616 blr->blr_private = NULL;
618 /* Add a pending lock record for this. */
619 status = brl_lock(sconn->msg_ctx,
620 br_lck,
621 smblctx,
622 messaging_server_id(sconn->msg_ctx),
623 offset,
624 count,
625 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
626 blr->lock_flav,
627 true,
628 NULL,
629 blr);
631 if (!NT_STATUS_IS_OK(status)) {
632 DEBUG(0,("push_blocking_lock_request_smb2: "
633 "failed to add PENDING_LOCK record.\n"));
634 TALLOC_FREE(blr);
635 return false;
637 state->blr = blr;
639 DEBUG(10,("push_blocking_lock_request_smb2: file %s timeout %d\n",
640 fsp_str_dbg(fsp),
641 lock_timeout ));
643 recalc_smb2_brl_timeout(sconn);
645 /* Ensure we'll receive messages when this is unlocked. */
646 if (!sconn->smb2.locks.blocking_lock_unlock_state) {
647 messaging_register(sconn->msg_ctx, sconn,
648 MSG_SMB_UNLOCK, received_unlock_msg);
649 sconn->smb2.locks.blocking_lock_unlock_state = true;
652 /* allow this request to be canceled */
653 tevent_req_set_cancel_fn(req, smbd_smb2_lock_cancel);
655 return true;
658 /****************************************************************
659 Remove a pending lock record under lock.
660 *****************************************************************/
662 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
663 struct blocking_lock_record *blr)
665 int i;
666 struct byte_range_lock *br_lck = brl_get_locks(
667 state, blr->fsp);
669 DEBUG(10, ("remove_pending_lock: BLR = %p\n", blr));
671 if (br_lck) {
672 brl_lock_cancel(br_lck,
673 blr->smblctx,
674 messaging_server_id(blr->fsp->conn->sconn->msg_ctx),
675 blr->offset,
676 blr->count,
677 blr->lock_flav,
678 blr);
679 TALLOC_FREE(br_lck);
682 /* Remove the locks we already got. */
684 for(i = blr->lock_num - 1; i >= 0; i--) {
685 struct smbd_lock_element *e = &state->locks[i];
687 do_unlock(blr->fsp->conn->sconn->msg_ctx,
688 blr->fsp,
689 e->smblctx,
690 e->count,
691 e->offset,
692 WINDOWS_LOCK);
696 /****************************************************************
697 Re-proccess a blocking lock request.
698 This is equivalent to process_lockingX() inside smbd/blocking.c
699 *****************************************************************/
701 static void reprocess_blocked_smb2_lock(struct smbd_smb2_request *smb2req,
702 struct timeval tv_curr)
704 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
705 struct blocking_lock_record *blr = NULL;
706 struct smbd_smb2_lock_state *state = NULL;
707 files_struct *fsp = NULL;
709 if (!smb2req->subreq) {
710 return;
712 state = tevent_req_data(smb2req->subreq, struct smbd_smb2_lock_state);
713 if (!state) {
714 return;
717 blr = state->blr;
718 fsp = blr->fsp;
720 /* Try and finish off getting all the outstanding locks. */
722 for (; blr->lock_num < state->lock_count; blr->lock_num++) {
723 struct byte_range_lock *br_lck = NULL;
724 struct smbd_lock_element *e = &state->locks[blr->lock_num];
726 br_lck = do_lock(fsp->conn->sconn->msg_ctx,
727 fsp,
728 e->smblctx,
729 e->count,
730 e->offset,
731 e->brltype,
732 WINDOWS_LOCK,
733 true,
734 &status,
735 &blr->blocking_smblctx,
736 blr);
738 TALLOC_FREE(br_lck);
740 if (NT_STATUS_IS_ERR(status)) {
741 break;
745 if(blr->lock_num == state->lock_count) {
747 * Success - we got all the locks.
750 DEBUG(3,("reprocess_blocked_smb2_lock SUCCESS file = %s, "
751 "%s, num_locks=%d\n",
752 fsp_str_dbg(fsp),
753 fsp_fnum_dbg(fsp),
754 (int)state->lock_count));
756 tevent_req_done(smb2req->subreq);
757 return;
760 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
761 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
763 * We have other than a "can't get lock"
764 * error. Return an error.
766 remove_pending_lock(state, blr);
767 tevent_req_nterror(smb2req->subreq, status);
768 return;
772 * We couldn't get the locks for this record on the list.
773 * If the time has expired, return a lock error.
776 if (!timeval_is_zero(&blr->expire_time) &&
777 timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
778 remove_pending_lock(state, blr);
779 tevent_req_nterror(smb2req->subreq, NT_STATUS_LOCK_NOT_GRANTED);
780 return;
784 * Still can't get all the locks - keep waiting.
787 DEBUG(10,("reprocess_blocked_smb2_lock: only got %d locks of %d needed "
788 "for file %s, %s. Still waiting....\n",
789 (int)blr->lock_num,
790 (int)state->lock_count,
791 fsp_str_dbg(fsp),
792 fsp_fnum_dbg(fsp)));
794 return;
798 /****************************************************************
799 Attempt to proccess all outstanding blocking locks pending on
800 the request queue.
801 *****************************************************************/
803 void process_blocking_lock_queue_smb2(
804 struct smbd_server_connection *sconn, struct timeval tv_curr)
806 struct smbd_smb2_request *smb2req, *nextreq;
808 for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
809 const uint8_t *inhdr;
811 nextreq = smb2req->next;
813 if (smb2req->subreq == NULL) {
814 /* This message has been processed. */
815 continue;
817 if (!tevent_req_is_in_progress(smb2req->subreq)) {
818 /* This message has been processed. */
819 continue;
822 inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
823 if (SVAL(inhdr, SMB2_HDR_OPCODE) == SMB2_OP_LOCK) {
824 reprocess_blocked_smb2_lock(smb2req, tv_curr);
828 recalc_smb2_brl_timeout(sconn);
831 /****************************************************************************
832 Remove any locks on this fd. Called from file_close().
833 ****************************************************************************/
835 void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
836 struct byte_range_lock *br_lck,
837 enum file_close_type close_type)
839 struct smbd_server_connection *sconn = fsp->conn->sconn;
840 struct smbd_smb2_request *smb2req, *nextreq;
842 for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
843 struct smbd_smb2_lock_state *state = NULL;
844 files_struct *fsp_curr = NULL;
845 int i = smb2req->current_idx;
846 struct blocking_lock_record *blr = NULL;
847 const uint8_t *inhdr;
849 nextreq = smb2req->next;
851 if (smb2req->subreq == NULL) {
852 /* This message has been processed. */
853 continue;
855 if (!tevent_req_is_in_progress(smb2req->subreq)) {
856 /* This message has been processed. */
857 continue;
860 inhdr = (const uint8_t *)smb2req->in.vector[i].iov_base;
861 if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
862 /* Not a lock call. */
863 continue;
866 state = tevent_req_data(smb2req->subreq,
867 struct smbd_smb2_lock_state);
868 if (!state) {
869 /* Strange - is this even possible ? */
870 continue;
873 fsp_curr = smb2req->compat_chain_fsp;
874 if (fsp_curr == NULL) {
875 /* Strange - is this even possible ? */
876 continue;
879 if (fsp_curr != fsp) {
880 /* It's not our fid */
881 continue;
884 blr = state->blr;
886 /* Remove the entries from the lock db. */
887 brl_lock_cancel(br_lck,
888 blr->smblctx,
889 messaging_server_id(sconn->msg_ctx),
890 blr->offset,
891 blr->count,
892 blr->lock_flav,
893 blr);
895 /* Finally end the request. */
896 if (close_type == SHUTDOWN_CLOSE) {
897 tevent_req_done(smb2req->subreq);
898 } else {
899 tevent_req_nterror(smb2req->subreq,
900 NT_STATUS_RANGE_NOT_LOCKED);