nfs4acls: Use talloc_realloc()
[Samba.git] / source3 / smbd / smb2_write.c
blobda583c9de8e144fb823d41b3c72fbe8bcd8fc01e
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 (smb2req->xconn->protocol >= PROTOCOL_SMB3_02) {
279 if (in_flags & SMB2_WRITEFLAG_WRITE_UNBUFFERED) {
280 state->write_through = true;
283 if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
284 state->write_through = true;
286 state->in_length = in_data.length;
287 state->out_count = 0;
289 DEBUG(10,("smbd_smb2_write: %s - %s\n",
290 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
292 smbreq = smbd_smb2_fake_smb_request(smb2req);
293 if (tevent_req_nomem(smbreq, req)) {
294 return tevent_req_post(req, ev);
296 state->smbreq = smbreq;
298 state->fsp = fsp;
300 if (IS_IPC(smbreq->conn)) {
301 struct tevent_req *subreq = NULL;
303 if (!fsp_is_np(fsp)) {
304 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
305 return tevent_req_post(req, ev);
308 subreq = np_write_send(state, ev,
309 fsp->fake_file_handle,
310 in_data.data,
311 in_data.length);
312 if (tevent_req_nomem(subreq, req)) {
313 return tevent_req_post(req, ev);
315 tevent_req_set_callback(subreq,
316 smbd_smb2_write_pipe_done,
317 req);
318 return req;
321 if (!CHECK_WRITE(fsp)) {
322 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
323 return tevent_req_post(req, ev);
326 /* Try and do an asynchronous write. */
327 status = schedule_aio_smb2_write(conn,
328 smbreq,
329 fsp,
330 in_offset,
331 in_data,
332 state->write_through);
334 if (NT_STATUS_IS_OK(status)) {
336 * Doing an async write, allow this
337 * request to be canceled
339 tevent_req_set_cancel_fn(req, smbd_smb2_write_cancel);
340 return req;
343 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
344 /* Real error in setting up aio. Fail. */
345 tevent_req_nterror(req, status);
346 return tevent_req_post(req, ev);
349 /* Fallback to synchronous. */
350 init_strict_lock_struct(fsp,
351 fsp->op->global->open_persistent_id,
352 in_offset,
353 in_data.length,
354 WRITE_LOCK,
355 &lock);
357 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
358 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
359 return tevent_req_post(req, ev);
363 * Note: in_data.data is NULL for the recvfile case.
365 nwritten = write_file(smbreq, fsp,
366 (const char *)in_data.data,
367 in_offset,
368 in_data.length);
370 status = smb2_write_complete(req, nwritten, errno);
372 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
374 DEBUG(10,("smb2: write on "
375 "file %s, offset %.0f, requested %u, written = %u\n",
376 fsp_str_dbg(fsp),
377 (double)in_offset,
378 (unsigned int)in_data.length,
379 (unsigned int)nwritten ));
381 if (!NT_STATUS_IS_OK(status)) {
382 tevent_req_nterror(req, status);
383 } else {
384 /* Success. */
385 tevent_req_done(req);
388 return tevent_req_post(req, ev);
391 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
393 struct tevent_req *req = tevent_req_callback_data(subreq,
394 struct tevent_req);
395 struct smbd_smb2_write_state *state = tevent_req_data(req,
396 struct smbd_smb2_write_state);
397 NTSTATUS status;
398 ssize_t nwritten = -1;
400 status = np_write_recv(subreq, &nwritten);
401 TALLOC_FREE(subreq);
402 if (!NT_STATUS_IS_OK(status)) {
403 NTSTATUS old = status;
404 status = nt_status_np_pipe(old);
405 tevent_req_nterror(req, status);
406 return;
409 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
410 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
411 return;
414 state->out_count = nwritten;
416 tevent_req_done(req);
419 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
420 uint32_t *out_count)
422 NTSTATUS status;
423 struct smbd_smb2_write_state *state = tevent_req_data(req,
424 struct smbd_smb2_write_state);
426 if (tevent_req_is_nterror(req, &status)) {
427 tevent_req_received(req);
428 return status;
431 *out_count = state->out_count;
433 tevent_req_received(req);
434 return NT_STATUS_OK;