s3: Lift the server_messaging_context from notify_job_status
[Samba/gbeck.git] / source3 / smbd / smb2_write.c
blob7db0e7e6a567e579783455b12e1c94c52a96ca13
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/globals.h"
23 #include "../libcli/smb/smb_common.h"
25 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
26 struct tevent_context *ev,
27 struct smbd_smb2_request *smb2req,
28 uint32_t in_smbpid,
29 uint64_t in_file_id_volatile,
30 DATA_BLOB in_data,
31 uint64_t in_offset,
32 uint32_t in_flags);
33 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
34 uint32_t *out_count);
36 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
37 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
39 const uint8_t *inhdr;
40 const uint8_t *inbody;
41 int i = req->current_idx;
42 size_t expected_body_size = 0x31;
43 size_t body_size;
44 uint32_t in_smbpid;
45 uint16_t in_data_offset;
46 uint32_t in_data_length;
47 DATA_BLOB in_data_buffer;
48 uint64_t in_offset;
49 uint64_t in_file_id_persistent;
50 uint64_t in_file_id_volatile;
51 uint32_t in_flags;
52 struct tevent_req *subreq;
54 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
55 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
56 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
59 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
61 body_size = SVAL(inbody, 0x00);
62 if (body_size != expected_body_size) {
63 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
66 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
68 in_data_offset = SVAL(inbody, 0x02);
69 in_data_length = IVAL(inbody, 0x04);
70 in_offset = BVAL(inbody, 0x08);
71 in_file_id_persistent = BVAL(inbody, 0x10);
72 in_file_id_volatile = BVAL(inbody, 0x18);
73 in_flags = IVAL(inbody, 0x2C);
75 if (in_data_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
76 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
79 if (in_data_length > req->in.vector[i+2].iov_len) {
80 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83 /* check the max write size */
84 if (in_data_length > lp_smb2_max_write()) {
85 /* This is a warning. */
86 DEBUG(2,("smbd_smb2_request_process_write : "
87 "client ignored max write :%s: 0x%08X: 0x%08X\n",
88 __location__, in_data_length, lp_smb2_max_write()));
89 #if 0
90 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91 #endif
94 in_data_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
95 in_data_buffer.length = in_data_length;
97 if (req->compat_chain_fsp) {
98 /* skip check */
99 } else if (in_file_id_persistent != in_file_id_volatile) {
100 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
103 subreq = smbd_smb2_write_send(req,
104 req->sconn->smb2.event_ctx,
105 req,
106 in_smbpid,
107 in_file_id_volatile,
108 in_data_buffer,
109 in_offset,
110 in_flags);
111 if (subreq == NULL) {
112 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
114 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
116 return smbd_smb2_request_pending_queue(req, subreq);
119 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
121 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
122 struct smbd_smb2_request);
123 int i = req->current_idx;
124 uint8_t *outhdr;
125 DATA_BLOB outbody;
126 DATA_BLOB outdyn;
127 uint32_t out_count = 0;
128 NTSTATUS status;
129 NTSTATUS error; /* transport error */
131 status = smbd_smb2_write_recv(subreq, &out_count);
132 TALLOC_FREE(subreq);
133 if (!NT_STATUS_IS_OK(status)) {
134 error = smbd_smb2_request_error(req, status);
135 if (!NT_STATUS_IS_OK(error)) {
136 smbd_server_connection_terminate(req->sconn,
137 nt_errstr(error));
138 return;
140 return;
143 outhdr = (uint8_t *)req->out.vector[i].iov_base;
145 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
146 if (outbody.data == NULL) {
147 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
148 if (!NT_STATUS_IS_OK(error)) {
149 smbd_server_connection_terminate(req->sconn,
150 nt_errstr(error));
151 return;
153 return;
156 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
157 SSVAL(outbody.data, 0x02, 0); /* reserved */
158 SIVAL(outbody.data, 0x04, out_count); /* count */
159 SIVAL(outbody.data, 0x08, 0); /* remaining */
160 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
161 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
163 outdyn = data_blob_const(NULL, 0);
165 error = smbd_smb2_request_done(req, outbody, &outdyn);
166 if (!NT_STATUS_IS_OK(error)) {
167 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
168 return;
172 struct smbd_smb2_write_state {
173 struct smbd_smb2_request *smb2req;
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 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
185 NTSTATUS status;
186 struct smbd_smb2_write_state *state = tevent_req_data(req,
187 struct smbd_smb2_write_state);
188 files_struct *fsp = state->fsp;
190 DEBUG(3,("smb2: fnum=[%d/%s] "
191 "length=%lu offset=%lu wrote=%lu\n",
192 fsp->fnum,
193 fsp_str_dbg(fsp),
194 (unsigned long)state->in_length,
195 (unsigned long)state->in_offset,
196 (unsigned long)nwritten));
198 if (nwritten == -1) {
199 return map_nt_error_from_unix(err);
202 if ((nwritten == 0) && (state->in_length != 0)) {
203 DEBUG(5,("smb2: write [%s] disk full\n",
204 fsp_str_dbg(fsp)));
205 return NT_STATUS_DISK_FULL;
208 status = sync_file(fsp->conn, fsp, state->write_through);
209 if (!NT_STATUS_IS_OK(status)) {
210 DEBUG(5,("smb2: sync_file for %s returned %s\n",
211 fsp_str_dbg(fsp),
212 nt_errstr(status)));
213 return status;
216 state->out_count = nwritten;
218 return NT_STATUS_OK;
221 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
222 struct tevent_context *ev,
223 struct smbd_smb2_request *smb2req,
224 uint32_t in_smbpid,
225 uint64_t in_file_id_volatile,
226 DATA_BLOB in_data,
227 uint64_t in_offset,
228 uint32_t in_flags)
230 NTSTATUS status;
231 struct tevent_req *req = NULL;
232 struct smbd_smb2_write_state *state = NULL;
233 struct smb_request *smbreq = NULL;
234 connection_struct *conn = smb2req->tcon->compat_conn;
235 files_struct *fsp = NULL;
236 ssize_t nwritten;
237 struct lock_struct lock;
239 req = tevent_req_create(mem_ctx, &state,
240 struct smbd_smb2_write_state);
241 if (req == NULL) {
242 return NULL;
244 state->smb2req = smb2req;
245 if (in_flags & 0x00000001) {
246 state->write_through = true;
248 state->in_length = in_data.length;
249 state->out_count = 0;
251 DEBUG(10,("smbd_smb2_write: file_id[0x%016llX]\n",
252 (unsigned long long)in_file_id_volatile));
254 smbreq = smbd_smb2_fake_smb_request(smb2req);
255 if (tevent_req_nomem(smbreq, req)) {
256 return tevent_req_post(req, ev);
259 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
260 if (fsp == NULL) {
261 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
262 return tevent_req_post(req, ev);
264 if (conn != fsp->conn) {
265 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
266 return tevent_req_post(req, ev);
268 if (smb2req->session->vuid != fsp->vuid) {
269 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
270 return tevent_req_post(req, ev);
273 state->fsp = fsp;
275 if (IS_IPC(smbreq->conn)) {
276 struct tevent_req *subreq = NULL;
278 if (!fsp_is_np(fsp)) {
279 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
280 return tevent_req_post(req, ev);
283 subreq = np_write_send(state, smbd_event_context(),
284 fsp->fake_file_handle,
285 in_data.data,
286 in_data.length);
287 if (tevent_req_nomem(subreq, req)) {
288 return tevent_req_post(req, ev);
290 tevent_req_set_callback(subreq,
291 smbd_smb2_write_pipe_done,
292 req);
293 return req;
296 if (!CHECK_WRITE(fsp)) {
297 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
298 return tevent_req_post(req, ev);
301 /* Try and do an asynchronous write. */
302 status = schedule_aio_smb2_write(conn,
303 smbreq,
304 fsp,
305 in_offset,
306 in_data,
307 state->write_through);
309 if (NT_STATUS_IS_OK(status)) {
311 * Doing an async write. Don't
312 * send a "gone async" message
313 * as we expect this to be less
314 * than the client timeout period.
315 * JRA. FIXME for offline files..
316 * FIXME - add cancel code..
318 smb2req->async = true;
319 return req;
322 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
323 /* Real error in setting up aio. Fail. */
324 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
325 return tevent_req_post(req, ev);
328 /* Fallback to synchronous. */
329 init_strict_lock_struct(fsp,
330 in_file_id_volatile,
331 in_offset,
332 in_data.length,
333 WRITE_LOCK,
334 &lock);
336 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
337 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
338 return tevent_req_post(req, ev);
341 nwritten = write_file(smbreq, fsp,
342 (const char *)in_data.data,
343 in_offset,
344 in_data.length);
346 status = smb2_write_complete(req, nwritten, errno);
348 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
350 DEBUG(10,("smb2: write on "
351 "file %s, offset %.0f, requested %u, written = %u\n",
352 fsp_str_dbg(fsp),
353 (double)in_offset,
354 (unsigned int)in_data.length,
355 (unsigned int)nwritten ));
357 if (!NT_STATUS_IS_OK(status)) {
358 tevent_req_nterror(req, status);
359 } else {
360 /* Success. */
361 tevent_req_done(req);
364 return tevent_req_post(req, ev);
367 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
369 struct tevent_req *req = tevent_req_callback_data(subreq,
370 struct tevent_req);
371 struct smbd_smb2_write_state *state = tevent_req_data(req,
372 struct smbd_smb2_write_state);
373 NTSTATUS status;
374 ssize_t nwritten = -1;
376 status = np_write_recv(subreq, &nwritten);
377 TALLOC_FREE(subreq);
378 if (!NT_STATUS_IS_OK(status)) {
379 tevent_req_nterror(req, status);
380 return;
383 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
384 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
385 return;
388 state->out_count = nwritten;
390 tevent_req_done(req);
393 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
394 uint32_t *out_count)
396 NTSTATUS status;
397 struct smbd_smb2_write_state *state = tevent_req_data(req,
398 struct smbd_smb2_write_state);
400 if (tevent_req_is_nterror(req, &status)) {
401 tevent_req_received(req);
402 return status;
405 *out_count = state->out_count;
407 tevent_req_received(req);
408 return NT_STATUS_OK;