s3:smbd: s/struct fd_event/struct tevent_fd
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_write.c
blobf9cfbfcf057dde658025d12c56c11d44cc6a7e67
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 NTSTATUS status;
42 const uint8_t *inbody;
43 uint16_t in_data_offset;
44 uint32_t in_data_length;
45 DATA_BLOB in_data_buffer;
46 uint64_t in_offset;
47 uint64_t in_file_id_persistent;
48 uint64_t in_file_id_volatile;
49 struct files_struct *in_fsp;
50 uint32_t in_flags;
51 struct tevent_req *subreq;
53 status = smbd_smb2_request_verify_sizes(req, 0x31);
54 if (!NT_STATUS_IS_OK(status)) {
55 return smbd_smb2_request_error(req, status);
57 inbody = SMBD_SMB2_IN_BODY_PTR(req);
59 in_data_offset = SVAL(inbody, 0x02);
60 in_data_length = IVAL(inbody, 0x04);
61 in_offset = BVAL(inbody, 0x08);
62 in_file_id_persistent = BVAL(inbody, 0x10);
63 in_file_id_volatile = BVAL(inbody, 0x18);
64 in_flags = IVAL(inbody, 0x2C);
66 if (in_data_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
67 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
70 if (in_data_length > SMBD_SMB2_IN_DYN_LEN(req)) {
71 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
74 /* check the max write size */
75 if (in_data_length > req->sconn->smb2.max_write) {
76 DEBUG(2,("smbd_smb2_request_process_write : "
77 "client ignored max write :%s: 0x%08X: 0x%08X\n",
78 __location__, in_data_length, req->sconn->smb2.max_write));
79 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
82 in_data_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
83 in_data_buffer.length = in_data_length;
85 status = smbd_smb2_request_verify_creditcharge(req, in_data_length);
86 if (!NT_STATUS_IS_OK(status)) {
87 return smbd_smb2_request_error(req, status);
90 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
91 if (in_fsp == NULL) {
92 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
95 subreq = smbd_smb2_write_send(req, req->sconn->ev_ctx,
96 req, in_fsp,
97 in_data_buffer,
98 in_offset,
99 in_flags);
100 if (subreq == NULL) {
101 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
103 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
105 return smbd_smb2_request_pending_queue(req, subreq, 500);
108 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
110 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
111 struct smbd_smb2_request);
112 DATA_BLOB outbody;
113 DATA_BLOB outdyn;
114 uint32_t out_count = 0;
115 NTSTATUS status;
116 NTSTATUS error; /* transport error */
118 status = smbd_smb2_write_recv(subreq, &out_count);
119 TALLOC_FREE(subreq);
120 if (!NT_STATUS_IS_OK(status)) {
121 error = smbd_smb2_request_error(req, status);
122 if (!NT_STATUS_IS_OK(error)) {
123 smbd_server_connection_terminate(req->sconn,
124 nt_errstr(error));
125 return;
127 return;
130 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
131 if (outbody.data == NULL) {
132 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
133 if (!NT_STATUS_IS_OK(error)) {
134 smbd_server_connection_terminate(req->sconn,
135 nt_errstr(error));
136 return;
138 return;
141 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
142 SSVAL(outbody.data, 0x02, 0); /* reserved */
143 SIVAL(outbody.data, 0x04, out_count); /* count */
144 SIVAL(outbody.data, 0x08, 0); /* remaining */
145 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
146 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
148 outdyn = data_blob_const(NULL, 0);
150 error = smbd_smb2_request_done(req, outbody, &outdyn);
151 if (!NT_STATUS_IS_OK(error)) {
152 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
153 return;
157 struct smbd_smb2_write_state {
158 struct smbd_smb2_request *smb2req;
159 struct smb_request *smbreq;
160 files_struct *fsp;
161 bool write_through;
162 uint32_t in_length;
163 uint64_t in_offset;
164 uint32_t out_count;
167 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
169 static NTSTATUS smb2_write_complete_internal(struct tevent_req *req,
170 ssize_t nwritten, int err,
171 bool do_sync)
173 NTSTATUS status;
174 struct smbd_smb2_write_state *state = tevent_req_data(req,
175 struct smbd_smb2_write_state);
176 files_struct *fsp = state->fsp;
178 if (nwritten == -1) {
179 status = map_nt_error_from_unix(err);
181 DEBUG(2, ("smb2_write failed: %s, file %s, "
182 "length=%lu offset=%lu nwritten=-1: %s\n",
183 fsp_fnum_dbg(fsp),
184 fsp_str_dbg(fsp),
185 (unsigned long)state->in_length,
186 (unsigned long)state->in_offset,
187 nt_errstr(status)));
189 return status;
192 DEBUG(3,("smb2: %s, file %s, "
193 "length=%lu offset=%lu wrote=%lu\n",
194 fsp_fnum_dbg(fsp),
195 fsp_str_dbg(fsp),
196 (unsigned long)state->in_length,
197 (unsigned long)state->in_offset,
198 (unsigned long)nwritten));
200 if ((nwritten == 0) && (state->in_length != 0)) {
201 DEBUG(5,("smb2: write [%s] disk full\n",
202 fsp_str_dbg(fsp)));
203 return NT_STATUS_DISK_FULL;
206 if (do_sync) {
207 status = sync_file(fsp->conn, fsp, state->write_through);
208 if (!NT_STATUS_IS_OK(status)) {
209 DEBUG(5,("smb2: sync_file for %s returned %s\n",
210 fsp_str_dbg(fsp),
211 nt_errstr(status)));
212 return status;
216 state->out_count = nwritten;
218 return NT_STATUS_OK;
221 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
223 return smb2_write_complete_internal(req, nwritten, err, true);
226 NTSTATUS smb2_write_complete_nosync(struct tevent_req *req, ssize_t nwritten,
227 int err)
229 return smb2_write_complete_internal(req, nwritten, err, false);
233 static bool smbd_smb2_write_cancel(struct tevent_req *req)
235 struct smbd_smb2_write_state *state =
236 tevent_req_data(req,
237 struct smbd_smb2_write_state);
239 return cancel_smb2_aio(state->smbreq);
242 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
243 struct tevent_context *ev,
244 struct smbd_smb2_request *smb2req,
245 struct files_struct *fsp,
246 DATA_BLOB in_data,
247 uint64_t in_offset,
248 uint32_t in_flags)
250 NTSTATUS status;
251 struct tevent_req *req = NULL;
252 struct smbd_smb2_write_state *state = NULL;
253 struct smb_request *smbreq = NULL;
254 connection_struct *conn = smb2req->tcon->compat;
255 ssize_t nwritten;
256 struct lock_struct lock;
258 req = tevent_req_create(mem_ctx, &state,
259 struct smbd_smb2_write_state);
260 if (req == NULL) {
261 return NULL;
263 state->smb2req = smb2req;
264 if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
265 state->write_through = true;
267 state->in_length = in_data.length;
268 state->out_count = 0;
270 DEBUG(10,("smbd_smb2_write: %s - %s\n",
271 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
273 smbreq = smbd_smb2_fake_smb_request(smb2req);
274 if (tevent_req_nomem(smbreq, req)) {
275 return tevent_req_post(req, ev);
277 state->smbreq = smbreq;
279 state->fsp = fsp;
281 if (IS_IPC(smbreq->conn)) {
282 struct tevent_req *subreq = NULL;
284 if (!fsp_is_np(fsp)) {
285 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
286 return tevent_req_post(req, ev);
289 subreq = np_write_send(state, ev,
290 fsp->fake_file_handle,
291 in_data.data,
292 in_data.length);
293 if (tevent_req_nomem(subreq, req)) {
294 return tevent_req_post(req, ev);
296 tevent_req_set_callback(subreq,
297 smbd_smb2_write_pipe_done,
298 req);
299 return req;
302 if (!CHECK_WRITE(fsp)) {
303 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
304 return tevent_req_post(req, ev);
307 /* Try and do an asynchronous write. */
308 status = schedule_aio_smb2_write(conn,
309 smbreq,
310 fsp,
311 in_offset,
312 in_data,
313 state->write_through);
315 if (NT_STATUS_IS_OK(status)) {
317 * Doing an async write, allow this
318 * request to be canceled
320 tevent_req_set_cancel_fn(req, smbd_smb2_write_cancel);
321 return req;
324 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
325 /* Real error in setting up aio. Fail. */
326 tevent_req_nterror(req, status);
327 return tevent_req_post(req, ev);
330 /* Fallback to synchronous. */
331 init_strict_lock_struct(fsp,
332 fsp->op->global->open_persistent_id,
333 in_offset,
334 in_data.length,
335 WRITE_LOCK,
336 &lock);
338 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
339 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
340 return tevent_req_post(req, ev);
343 nwritten = write_file(smbreq, fsp,
344 (const char *)in_data.data,
345 in_offset,
346 in_data.length);
348 status = smb2_write_complete(req, nwritten, errno);
350 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
352 DEBUG(10,("smb2: write on "
353 "file %s, offset %.0f, requested %u, written = %u\n",
354 fsp_str_dbg(fsp),
355 (double)in_offset,
356 (unsigned int)in_data.length,
357 (unsigned int)nwritten ));
359 if (!NT_STATUS_IS_OK(status)) {
360 tevent_req_nterror(req, status);
361 } else {
362 /* Success. */
363 tevent_req_done(req);
366 return tevent_req_post(req, ev);
369 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
371 struct tevent_req *req = tevent_req_callback_data(subreq,
372 struct tevent_req);
373 struct smbd_smb2_write_state *state = tevent_req_data(req,
374 struct smbd_smb2_write_state);
375 NTSTATUS status;
376 ssize_t nwritten = -1;
378 status = np_write_recv(subreq, &nwritten);
379 TALLOC_FREE(subreq);
380 if (!NT_STATUS_IS_OK(status)) {
381 NTSTATUS old = status;
382 status = nt_status_np_pipe(old);
383 tevent_req_nterror(req, status);
384 return;
387 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
388 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
389 return;
392 state->out_count = nwritten;
394 tevent_req_done(req);
397 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
398 uint32_t *out_count)
400 NTSTATUS status;
401 struct smbd_smb2_write_state *state = tevent_req_data(req,
402 struct smbd_smb2_write_state);
404 if (tevent_req_is_nterror(req, &status)) {
405 tevent_req_received(req);
406 return status;
409 *out_count = state->out_count;
411 tevent_req_received(req);
412 return NT_STATUS_OK;