2 Unix SMB/CIFS implementation.
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/>.
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../lib/util/tevent_ntstatus.h"
29 struct smbd_smb2_lock_element {
35 struct smbd_smb2_lock_state {
36 struct smbd_smb2_request *smb2req;
37 struct smb_request *smb1req;
38 struct blocking_lock_record *blr;
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,
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)
59 const uint8_t *inbody;
60 const int i = req->current_idx;
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;
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,
96 if (in_locks == NULL) {
97 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
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 */
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->smb2.event_ctx,
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);
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);
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);
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,
171 status = smbd_smb2_lock_recv(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,
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,
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,
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,
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;
219 struct smbd_lock_element *locks;
223 req = tevent_req_create(mem_ctx, &state,
224 struct smbd_smb2_lock_state);
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 - fnum[%d]\n",
238 fsp_str_dbg(fsp), fsp->fnum));
240 locks = talloc_array(state, struct smbd_lock_element, in_lock_count);
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);
256 case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
257 case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
261 case SMB2_LOCK_FLAG_UNLOCK:
262 /* only the first lock gives the UNLOCK bit - see
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:
284 tevent_req_nterror(req,
285 NT_STATUS_INVALID_PARAMETER);
286 return tevent_req_post(req, ev);
290 case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
291 case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
297 case SMB2_LOCK_FLAG_UNLOCK:
299 tevent_req_nterror(req,
300 NT_STATUS_INVALID_PARAMETER);
301 return tevent_req_post(req, ev);
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
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;
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;
353 status = smbd_do_locking(smb1req, fsp,
362 status = smbd_do_locking(smb1req, fsp,
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);
383 tevent_req_done(req);
384 return tevent_req_post(req, ev);
387 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req)
391 if (tevent_req_is_nterror(req, &status)) {
392 tevent_req_received(req);
396 tevent_req_received(req);
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);
413 if (!state->smb2req) {
417 smb2req = state->smb2req;
418 smb2req->cancelled = true;
420 tevent_req_done(req);
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,
432 struct server_id server_id,
435 struct smbd_server_connection *sconn;
437 DEBUG(10,("received_unlock_msg (SMB2)\n"));
439 sconn = msg_ctx_to_sconn(msg);
441 DEBUG(1, ("could not find sconn\n"));
444 process_blocking_lock_queue_smb2(sconn, timeval_current());
447 /****************************************************************
448 Function to get the blr on a pending record.
449 *****************************************************************/
451 struct blocking_lock_record *get_pending_smb2req_blr(struct smbd_smb2_request *smb2req)
453 struct smbd_smb2_lock_state *state = NULL;
454 const uint8_t *inhdr;
459 if (smb2req->subreq == NULL) {
462 if (!tevent_req_is_in_progress(smb2req->subreq)) {
465 inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
466 if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
469 state = tevent_req_data(smb2req->subreq,
470 struct smbd_smb2_lock_state);
476 /****************************************************************
477 Set up the next brl timeout.
478 *****************************************************************/
480 static bool recalc_smb2_brl_timeout(struct smbd_server_connection *sconn)
482 struct smbd_smb2_request *smb2req;
483 struct timeval next_timeout = timeval_zero();
484 int max_brl_timeout = lp_parm_int(-1, "brl", "recalctime", 5);
486 TALLOC_FREE(sconn->smb2.locks.brl_timeout);
488 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
489 struct blocking_lock_record *blr =
490 get_pending_smb2req_blr(smb2req);
494 if (timeval_is_zero(&blr->expire_time)) {
496 * If we're blocked on pid 0xFFFFFFFFFFFFFFFFLL this is
497 * a POSIX lock, so calculate a timeout of
498 * 10 seconds into the future.
500 if (blr->blocking_smblctx == 0xFFFFFFFFFFFFFFFFLL) {
501 struct timeval psx_to = timeval_current_ofs(10, 0);
502 next_timeout = timeval_brl_min(&next_timeout, &psx_to);
508 next_timeout = timeval_brl_min(&next_timeout, &blr->expire_time);
511 if (timeval_is_zero(&next_timeout)) {
512 DEBUG(10, ("recalc_smb2_brl_timeout:Next "
513 "timeout = Infinite.\n"));
518 * To account for unclean shutdowns by clients we need a
519 * maximum timeout that we use for checking pending locks. If
520 * we have any pending locks at all, then check if the pending
521 * lock can continue at least every brl:recalctime seconds
522 * (default 5 seconds).
524 * This saves us needing to do a message_send_all() in the
525 * SIGCHLD handler in the parent daemon. That
526 * message_send_all() caused O(n^2) work to be done when IP
527 * failovers happened in clustered Samba, which could make the
528 * entire system unusable for many minutes.
531 if (max_brl_timeout > 0) {
532 struct timeval min_to = timeval_current_ofs(max_brl_timeout, 0);
533 next_timeout = timeval_brl_min(&next_timeout, &min_to);
537 struct timeval cur, from_now;
539 cur = timeval_current();
540 from_now = timeval_until(&cur, &next_timeout);
541 DEBUG(10, ("recalc_smb2_brl_timeout: Next "
542 "timeout = %d.%d seconds from now.\n",
543 (int)from_now.tv_sec, (int)from_now.tv_usec));
546 sconn->smb2.locks.brl_timeout = event_add_timed(
547 smbd_event_context(),
552 if (!sconn->smb2.locks.brl_timeout) {
558 /****************************************************************
559 Get an SMB2 lock reqeust to go async. lock_timeout should
561 *****************************************************************/
563 bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck,
564 struct smb_request *smb1req,
569 enum brl_type lock_type,
570 enum brl_flavour lock_flav,
573 uint64_t blocking_smblctx)
575 struct smbd_server_connection *sconn = smb1req->sconn;
576 struct smbd_smb2_request *smb2req = smb1req->smb2req;
577 struct tevent_req *req = NULL;
578 struct smbd_smb2_lock_state *state = NULL;
579 struct blocking_lock_record *blr = NULL;
580 NTSTATUS status = NT_STATUS_OK;
585 req = smb2req->subreq;
589 if (!tevent_req_is_in_progress(smb2req->subreq)) {
592 state = tevent_req_data(req, struct smbd_smb2_lock_state);
597 blr = talloc_zero(state, struct blocking_lock_record);
603 if (lock_timeout == -1) {
604 blr->expire_time.tv_sec = 0;
605 blr->expire_time.tv_usec = 0; /* Never expire. */
607 blr->expire_time = timeval_current_ofs(
609 (lock_timeout % 1000) * 1000);
612 blr->lock_num = lock_num;
613 blr->smblctx = smblctx;
614 blr->blocking_smblctx = blocking_smblctx;
615 blr->lock_flav = lock_flav;
616 blr->lock_type = lock_type;
617 blr->offset = offset;
620 /* Specific brl_lock() implementations can fill this in. */
621 blr->blr_private = NULL;
623 /* Add a pending lock record for this. */
624 status = brl_lock(sconn->msg_ctx,
627 sconn_server_id(sconn),
630 lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
636 if (!NT_STATUS_IS_OK(status)) {
637 DEBUG(0,("push_blocking_lock_request_smb2: "
638 "failed to add PENDING_LOCK record.\n"));
644 DEBUG(10,("push_blocking_lock_request_smb2: file %s timeout %d\n",
648 recalc_smb2_brl_timeout(sconn);
650 /* Ensure we'll receive messages when this is unlocked. */
651 if (!sconn->smb2.locks.blocking_lock_unlock_state) {
652 messaging_register(sconn->msg_ctx, NULL,
653 MSG_SMB_UNLOCK, received_unlock_msg);
654 sconn->smb2.locks.blocking_lock_unlock_state = true;
657 /* allow this request to be canceled */
658 tevent_req_set_cancel_fn(req, smbd_smb2_lock_cancel);
663 /****************************************************************
664 Remove a pending lock record under lock.
665 *****************************************************************/
667 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
668 struct blocking_lock_record *blr)
671 struct byte_range_lock *br_lck = brl_get_locks(
674 DEBUG(10, ("remove_pending_lock: BLR = %p\n", blr));
677 brl_lock_cancel(br_lck,
679 sconn_server_id(blr->fsp->conn->sconn),
687 /* Remove the locks we already got. */
689 for(i = blr->lock_num - 1; i >= 0; i--) {
690 struct smbd_lock_element *e = &state->locks[i];
692 do_unlock(blr->fsp->conn->sconn->msg_ctx,
701 /****************************************************************
702 Re-proccess a blocking lock request.
703 This is equivalent to process_lockingX() inside smbd/blocking.c
704 *****************************************************************/
706 static void reprocess_blocked_smb2_lock(struct smbd_smb2_request *smb2req,
707 struct timeval tv_curr)
709 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
710 struct blocking_lock_record *blr = NULL;
711 struct smbd_smb2_lock_state *state = NULL;
712 files_struct *fsp = NULL;
714 if (!smb2req->subreq) {
717 state = tevent_req_data(smb2req->subreq, struct smbd_smb2_lock_state);
725 /* Try and finish off getting all the outstanding locks. */
727 for (; blr->lock_num < state->lock_count; blr->lock_num++) {
728 struct byte_range_lock *br_lck = NULL;
729 struct smbd_lock_element *e = &state->locks[blr->lock_num];
731 br_lck = do_lock(fsp->conn->sconn->msg_ctx,
740 &blr->blocking_smblctx,
745 if (NT_STATUS_IS_ERR(status)) {
750 if(blr->lock_num == state->lock_count) {
752 * Success - we got all the locks.
755 DEBUG(3,("reprocess_blocked_smb2_lock SUCCESS file = %s, "
756 "fnum=%d num_locks=%d\n",
759 (int)state->lock_count));
761 tevent_req_done(smb2req->subreq);
765 if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
766 !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
768 * We have other than a "can't get lock"
769 * error. Return an error.
771 remove_pending_lock(state, blr);
772 tevent_req_nterror(smb2req->subreq, status);
777 * We couldn't get the locks for this record on the list.
778 * If the time has expired, return a lock error.
781 if (!timeval_is_zero(&blr->expire_time) &&
782 timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
783 remove_pending_lock(state, blr);
784 tevent_req_nterror(smb2req->subreq, NT_STATUS_LOCK_NOT_GRANTED);
789 * Still can't get all the locks - keep waiting.
792 DEBUG(10,("reprocess_blocked_smb2_lock: only got %d locks of %d needed "
793 "for file %s, fnum = %d. Still waiting....\n",
795 (int)state->lock_count,
803 /****************************************************************
804 Attempt to proccess all outstanding blocking locks pending on
806 *****************************************************************/
808 void process_blocking_lock_queue_smb2(
809 struct smbd_server_connection *sconn, struct timeval tv_curr)
811 struct smbd_smb2_request *smb2req, *nextreq;
813 for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
814 const uint8_t *inhdr;
816 nextreq = smb2req->next;
818 if (smb2req->subreq == NULL) {
819 /* This message has been processed. */
822 if (!tevent_req_is_in_progress(smb2req->subreq)) {
823 /* This message has been processed. */
827 inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
828 if (SVAL(inhdr, SMB2_HDR_OPCODE) == SMB2_OP_LOCK) {
829 reprocess_blocked_smb2_lock(smb2req, tv_curr);
833 recalc_smb2_brl_timeout(sconn);
836 /****************************************************************************
837 Remove any locks on this fd. Called from file_close().
838 ****************************************************************************/
840 void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
841 struct byte_range_lock *br_lck,
842 enum file_close_type close_type)
844 struct smbd_server_connection *sconn = fsp->conn->sconn;
845 struct smbd_smb2_request *smb2req, *nextreq;
847 for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
848 struct smbd_smb2_lock_state *state = NULL;
849 files_struct *fsp_curr = NULL;
850 int i = smb2req->current_idx;
851 struct blocking_lock_record *blr = NULL;
852 const uint8_t *inhdr;
854 nextreq = smb2req->next;
856 if (smb2req->subreq == NULL) {
857 /* This message has been processed. */
860 if (!tevent_req_is_in_progress(smb2req->subreq)) {
861 /* This message has been processed. */
865 inhdr = (const uint8_t *)smb2req->in.vector[i].iov_base;
866 if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
867 /* Not a lock call. */
871 state = tevent_req_data(smb2req->subreq,
872 struct smbd_smb2_lock_state);
874 /* Strange - is this even possible ? */
878 fsp_curr = smb2req->compat_chain_fsp;
879 if (fsp_curr == NULL) {
880 /* Strange - is this even possible ? */
884 if (fsp_curr != fsp) {
885 /* It's not our fid */
891 /* Remove the entries from the lock db. */
892 brl_lock_cancel(br_lck,
894 sconn_server_id(sconn),
900 /* Finally end the request. */
901 if (close_type == SHUTDOWN_CLOSE) {
902 tevent_req_done(smb2req->subreq);
904 tevent_req_nterror(smb2req->subreq,
905 NT_STATUS_RANGE_NOT_LOCKED);