s3: Fix a "Invalid (state->nread >= 0)" warning
[Samba/id10ts.git] / source3 / smbd / smb2_flush.c
blobfecce45fb4e1c376d4278aa2fd01ac992b1a9118
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
27 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
28 struct tevent_context *ev,
29 struct smbd_smb2_request *smb2req,
30 uint64_t in_file_id_volatile);
31 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req);
33 static void smbd_smb2_request_flush_done(struct tevent_req *subreq);
34 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req)
36 NTSTATUS status;
37 const uint8_t *inbody;
38 int i = req->current_idx;
39 uint64_t in_file_id_persistent;
40 uint64_t in_file_id_volatile;
41 struct tevent_req *subreq;
43 status = smbd_smb2_request_verify_sizes(req, 0x18);
44 if (!NT_STATUS_IS_OK(status)) {
45 return smbd_smb2_request_error(req, status);
47 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
49 in_file_id_persistent = BVAL(inbody, 0x08);
50 in_file_id_volatile = BVAL(inbody, 0x10);
52 if (req->compat_chain_fsp) {
53 /* skip check */
54 } else if (in_file_id_persistent != in_file_id_volatile) {
55 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
58 subreq = smbd_smb2_flush_send(req,
59 req->sconn->ev_ctx,
60 req,
61 in_file_id_volatile);
62 if (subreq == NULL) {
63 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
65 tevent_req_set_callback(subreq, smbd_smb2_request_flush_done, req);
67 return smbd_smb2_request_pending_queue(req, subreq, 500);
70 static void smbd_smb2_request_flush_done(struct tevent_req *subreq)
72 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
73 struct smbd_smb2_request);
74 DATA_BLOB outbody;
75 NTSTATUS status;
76 NTSTATUS error; /* transport error */
78 status = smbd_smb2_flush_recv(subreq);
79 TALLOC_FREE(subreq);
80 if (!NT_STATUS_IS_OK(status)) {
81 error = smbd_smb2_request_error(req, status);
82 if (!NT_STATUS_IS_OK(error)) {
83 smbd_server_connection_terminate(req->sconn,
84 nt_errstr(error));
85 return;
87 return;
90 outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
91 if (outbody.data == NULL) {
92 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
93 if (!NT_STATUS_IS_OK(error)) {
94 smbd_server_connection_terminate(req->sconn,
95 nt_errstr(error));
96 return;
98 return;
101 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
102 SSVAL(outbody.data, 0x02, 0); /* reserved */
104 error = smbd_smb2_request_done(req, outbody, NULL);
105 if (!NT_STATUS_IS_OK(error)) {
106 smbd_server_connection_terminate(req->sconn,
107 nt_errstr(error));
108 return;
112 struct smbd_smb2_flush_state {
113 struct smbd_smb2_request *smb2req;
116 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
117 struct tevent_context *ev,
118 struct smbd_smb2_request *smb2req,
119 uint64_t in_file_id_volatile)
121 struct tevent_req *req;
122 struct smbd_smb2_flush_state *state;
123 NTSTATUS status;
124 struct smb_request *smbreq;
125 files_struct *fsp;
127 req = tevent_req_create(mem_ctx, &state,
128 struct smbd_smb2_flush_state);
129 if (req == NULL) {
130 return NULL;
132 state->smb2req = smb2req;
134 DEBUG(10,("smbd_smb2_flush: file_id[0x%016llX]\n",
135 (unsigned long long)in_file_id_volatile));
137 smbreq = smbd_smb2_fake_smb_request(smb2req);
138 if (tevent_req_nomem(smbreq, req)) {
139 return tevent_req_post(req, ev);
142 if (IS_IPC(smbreq->conn)) {
143 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
144 return tevent_req_post(req, ev);
147 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
148 if (fsp == NULL) {
149 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
150 return tevent_req_post(req, ev);
152 if (smbreq->conn != fsp->conn) {
153 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
154 return tevent_req_post(req, ev);
156 if (smb2req->session->vuid != fsp->vuid) {
157 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
158 return tevent_req_post(req, ev);
161 if (!CHECK_WRITE(fsp)) {
162 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
163 return tevent_req_post(req, ev);
166 status = sync_file(smbreq->conn, fsp, true);
167 if (!NT_STATUS_IS_OK(status)) {
168 DEBUG(5,("smbd_smb2_flush: sync_file for %s returned %s\n",
169 fsp_str_dbg(fsp), nt_errstr(status)));
170 tevent_req_nterror(req, status);
171 return tevent_req_post(req, ev);
174 tevent_req_done(req);
175 return tevent_req_post(req, ev);
178 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req)
180 NTSTATUS status;
182 if (tevent_req_is_nterror(req, &status)) {
183 tevent_req_received(req);
184 return status;
187 tevent_req_received(req);
188 return NT_STATUS_OK;