Fix the developer O3 build
[Samba.git] / source3 / smbd / smb2_write.c
blob508c0b362ca2a3bec82c3e015a6d90b8548a4186
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"
26 #include "rpc_server/srv_pipe_hnd.h"
28 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
29 struct tevent_context *ev,
30 struct smbd_smb2_request *smb2req,
31 struct files_struct *in_fsp,
32 DATA_BLOB in_data,
33 uint64_t in_offset,
34 uint32_t in_flags);
35 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
36 uint32_t *out_count);
38 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
39 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
41 struct smbXsrv_connection *xconn = req->xconn;
42 NTSTATUS status;
43 const uint8_t *inbody;
44 uint16_t in_data_offset;
45 uint32_t in_data_length;
46 DATA_BLOB in_data_buffer;
47 uint64_t in_offset;
48 uint64_t in_file_id_persistent;
49 uint64_t in_file_id_volatile;
50 struct files_struct *in_fsp;
51 uint32_t in_flags;
52 size_t in_dyn_len = 0;
53 uint8_t *in_dyn_ptr = NULL;
54 struct tevent_req *subreq;
56 status = smbd_smb2_request_verify_sizes(req, 0x31);
57 if (!NT_STATUS_IS_OK(status)) {
58 return smbd_smb2_request_error(req, status);
60 inbody = SMBD_SMB2_IN_BODY_PTR(req);
62 in_data_offset = SVAL(inbody, 0x02);
63 in_data_length = IVAL(inbody, 0x04);
64 in_offset = BVAL(inbody, 0x08);
65 in_file_id_persistent = BVAL(inbody, 0x10);
66 in_file_id_volatile = BVAL(inbody, 0x18);
67 in_flags = IVAL(inbody, 0x2C);
69 if (in_data_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
70 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
73 if (req->smb1req != NULL && req->smb1req->unread_bytes > 0) {
74 in_dyn_ptr = NULL;
75 in_dyn_len = req->smb1req->unread_bytes;
76 } else {
77 in_dyn_ptr = SMBD_SMB2_IN_DYN_PTR(req);
78 in_dyn_len = SMBD_SMB2_IN_DYN_LEN(req);
81 if (in_data_length > in_dyn_len) {
82 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
85 /* check the max write size */
86 if (in_data_length > xconn->smb2.server.max_write) {
87 DEBUG(2,("smbd_smb2_request_process_write : "
88 "client ignored max write :%s: 0x%08X: 0x%08X\n",
89 __location__, in_data_length, xconn->smb2.server.max_write));
90 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
94 * Note: that in_dyn_ptr is NULL for the recvfile case.
96 in_data_buffer.data = in_dyn_ptr;
97 in_data_buffer.length = in_data_length;
99 status = smbd_smb2_request_verify_creditcharge(req, in_data_length);
100 if (!NT_STATUS_IS_OK(status)) {
101 return smbd_smb2_request_error(req, status);
104 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
105 if (in_fsp == NULL) {
106 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
109 subreq = smbd_smb2_write_send(req, req->sconn->ev_ctx,
110 req, in_fsp,
111 in_data_buffer,
112 in_offset,
113 in_flags);
114 if (subreq == NULL) {
115 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
117 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
119 return smbd_smb2_request_pending_queue(req, subreq, 500);
122 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
124 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
125 struct smbd_smb2_request);
126 DATA_BLOB outbody;
127 DATA_BLOB outdyn;
128 uint32_t out_count = 0;
129 NTSTATUS status;
130 NTSTATUS error; /* transport error */
132 status = smbd_smb2_write_recv(subreq, &out_count);
133 TALLOC_FREE(subreq);
134 if (!NT_STATUS_IS_OK(status)) {
135 error = smbd_smb2_request_error(req, status);
136 if (!NT_STATUS_IS_OK(error)) {
137 smbd_server_connection_terminate(req->xconn,
138 nt_errstr(error));
139 return;
141 return;
144 outbody = smbd_smb2_generate_outbody(req, 0x10);
145 if (outbody.data == NULL) {
146 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
147 if (!NT_STATUS_IS_OK(error)) {
148 smbd_server_connection_terminate(req->xconn,
149 nt_errstr(error));
150 return;
152 return;
155 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
156 SSVAL(outbody.data, 0x02, 0); /* reserved */
157 SIVAL(outbody.data, 0x04, out_count); /* count */
158 SIVAL(outbody.data, 0x08, 0); /* remaining */
159 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
160 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
162 outdyn = data_blob_const(NULL, 0);
164 error = smbd_smb2_request_done(req, outbody, &outdyn);
165 if (!NT_STATUS_IS_OK(error)) {
166 smbd_server_connection_terminate(req->xconn, nt_errstr(error));
167 return;
171 struct smbd_smb2_write_state {
172 struct smbd_smb2_request *smb2req;
173 struct smb_request *smbreq;
174 files_struct *fsp;
175 bool write_through;
176 uint32_t in_length;
177 uint64_t in_offset;
178 uint32_t out_count;
181 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
183 static NTSTATUS smb2_write_complete_internal(struct tevent_req *req,
184 ssize_t nwritten, int err,
185 bool do_sync)
187 NTSTATUS status;
188 struct smbd_smb2_write_state *state = tevent_req_data(req,
189 struct smbd_smb2_write_state);
190 files_struct *fsp = state->fsp;
192 if (nwritten == -1) {
193 status = map_nt_error_from_unix(err);
195 DEBUG(2, ("smb2_write failed: %s, file %s, "
196 "length=%lu offset=%lu nwritten=-1: %s\n",
197 fsp_fnum_dbg(fsp),
198 fsp_str_dbg(fsp),
199 (unsigned long)state->in_length,
200 (unsigned long)state->in_offset,
201 nt_errstr(status)));
203 return status;
206 DEBUG(3,("smb2: %s, file %s, "
207 "length=%lu offset=%lu wrote=%lu\n",
208 fsp_fnum_dbg(fsp),
209 fsp_str_dbg(fsp),
210 (unsigned long)state->in_length,
211 (unsigned long)state->in_offset,
212 (unsigned long)nwritten));
214 if ((nwritten == 0) && (state->in_length != 0)) {
215 DEBUG(5,("smb2: write [%s] disk full\n",
216 fsp_str_dbg(fsp)));
217 return NT_STATUS_DISK_FULL;
220 if (do_sync) {
221 status = sync_file(fsp->conn, fsp, state->write_through);
222 if (!NT_STATUS_IS_OK(status)) {
223 DEBUG(5,("smb2: sync_file for %s returned %s\n",
224 fsp_str_dbg(fsp),
225 nt_errstr(status)));
226 return status;
230 state->out_count = nwritten;
232 return NT_STATUS_OK;
235 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
237 return smb2_write_complete_internal(req, nwritten, err, true);
240 NTSTATUS smb2_write_complete_nosync(struct tevent_req *req, ssize_t nwritten,
241 int err)
243 return smb2_write_complete_internal(req, nwritten, err, false);
247 static bool smbd_smb2_write_cancel(struct tevent_req *req)
249 struct smbd_smb2_write_state *state =
250 tevent_req_data(req,
251 struct smbd_smb2_write_state);
253 return cancel_smb2_aio(state->smbreq);
256 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
257 struct tevent_context *ev,
258 struct smbd_smb2_request *smb2req,
259 struct files_struct *fsp,
260 DATA_BLOB in_data,
261 uint64_t in_offset,
262 uint32_t in_flags)
264 NTSTATUS status;
265 struct tevent_req *req = NULL;
266 struct smbd_smb2_write_state *state = NULL;
267 struct smb_request *smbreq = NULL;
268 connection_struct *conn = smb2req->tcon->compat;
269 ssize_t nwritten;
270 struct lock_struct lock;
272 req = tevent_req_create(mem_ctx, &state,
273 struct smbd_smb2_write_state);
274 if (req == NULL) {
275 return NULL;
277 state->smb2req = smb2req;
278 if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
279 state->write_through = true;
281 state->in_length = in_data.length;
282 state->out_count = 0;
284 DEBUG(10,("smbd_smb2_write: %s - %s\n",
285 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
287 smbreq = smbd_smb2_fake_smb_request(smb2req);
288 if (tevent_req_nomem(smbreq, req)) {
289 return tevent_req_post(req, ev);
291 state->smbreq = smbreq;
293 state->fsp = fsp;
295 if (IS_IPC(smbreq->conn)) {
296 struct tevent_req *subreq = NULL;
298 if (!fsp_is_np(fsp)) {
299 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
300 return tevent_req_post(req, ev);
303 subreq = np_write_send(state, ev,
304 fsp->fake_file_handle,
305 in_data.data,
306 in_data.length);
307 if (tevent_req_nomem(subreq, req)) {
308 return tevent_req_post(req, ev);
310 tevent_req_set_callback(subreq,
311 smbd_smb2_write_pipe_done,
312 req);
313 return req;
316 if (!CHECK_WRITE(fsp)) {
317 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
318 return tevent_req_post(req, ev);
321 /* Try and do an asynchronous write. */
322 status = schedule_aio_smb2_write(conn,
323 smbreq,
324 fsp,
325 in_offset,
326 in_data,
327 state->write_through);
329 if (NT_STATUS_IS_OK(status)) {
331 * Doing an async write, allow this
332 * request to be canceled
334 tevent_req_set_cancel_fn(req, smbd_smb2_write_cancel);
335 return req;
338 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
339 /* Real error in setting up aio. Fail. */
340 tevent_req_nterror(req, status);
341 return tevent_req_post(req, ev);
344 /* Fallback to synchronous. */
345 init_strict_lock_struct(fsp,
346 fsp->op->global->open_persistent_id,
347 in_offset,
348 in_data.length,
349 WRITE_LOCK,
350 &lock);
352 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
353 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
354 return tevent_req_post(req, ev);
358 * Note: in_data.data is NULL for the recvfile case.
360 nwritten = write_file(smbreq, fsp,
361 (const char *)in_data.data,
362 in_offset,
363 in_data.length);
365 status = smb2_write_complete(req, nwritten, errno);
367 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
369 DEBUG(10,("smb2: write on "
370 "file %s, offset %.0f, requested %u, written = %u\n",
371 fsp_str_dbg(fsp),
372 (double)in_offset,
373 (unsigned int)in_data.length,
374 (unsigned int)nwritten ));
376 if (!NT_STATUS_IS_OK(status)) {
377 tevent_req_nterror(req, status);
378 } else {
379 /* Success. */
380 tevent_req_done(req);
383 return tevent_req_post(req, ev);
386 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
388 struct tevent_req *req = tevent_req_callback_data(subreq,
389 struct tevent_req);
390 struct smbd_smb2_write_state *state = tevent_req_data(req,
391 struct smbd_smb2_write_state);
392 NTSTATUS status;
393 ssize_t nwritten = -1;
395 status = np_write_recv(subreq, &nwritten);
396 TALLOC_FREE(subreq);
397 if (!NT_STATUS_IS_OK(status)) {
398 NTSTATUS old = status;
399 status = nt_status_np_pipe(old);
400 tevent_req_nterror(req, status);
401 return;
404 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
405 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
406 return;
409 state->out_count = nwritten;
411 tevent_req_done(req);
414 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
415 uint32_t *out_count)
417 NTSTATUS status;
418 struct smbd_smb2_write_state *state = tevent_req_data(req,
419 struct smbd_smb2_write_state);
421 if (tevent_req_is_nterror(req, &status)) {
422 tevent_req_received(req);
423 return status;
426 *out_count = state->out_count;
428 tevent_req_received(req);
429 return NT_STATUS_OK;