s3:smb2_write: pass fsp->fnum to init_strict_lock_struct()
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_write.c
bloba149cb84dd39aef263a1aecb03835095f0c33517
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 uint32_t in_smbpid,
32 uint64_t in_file_id_volatile,
33 DATA_BLOB in_data,
34 uint64_t in_offset,
35 uint32_t in_flags);
36 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
37 uint32_t *out_count);
39 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
40 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
42 NTSTATUS status;
43 const uint8_t *inhdr;
44 const uint8_t *inbody;
45 int i = req->current_idx;
46 uint32_t in_smbpid;
47 uint16_t in_data_offset;
48 uint32_t in_data_length;
49 DATA_BLOB in_data_buffer;
50 uint64_t in_offset;
51 uint64_t in_file_id_persistent;
52 uint64_t in_file_id_volatile;
53 uint32_t in_flags;
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 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
61 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
63 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
65 in_data_offset = SVAL(inbody, 0x02);
66 in_data_length = IVAL(inbody, 0x04);
67 in_offset = BVAL(inbody, 0x08);
68 in_file_id_persistent = BVAL(inbody, 0x10);
69 in_file_id_volatile = BVAL(inbody, 0x18);
70 in_flags = IVAL(inbody, 0x2C);
72 if (in_data_offset != (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
73 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
76 if (in_data_length > req->in.vector[i+2].iov_len) {
77 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
80 /* check the max write size */
81 if (in_data_length > req->sconn->smb2.max_write) {
82 DEBUG(2,("smbd_smb2_request_process_write : "
83 "client ignored max write :%s: 0x%08X: 0x%08X\n",
84 __location__, in_data_length, req->sconn->smb2.max_write));
85 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
88 in_data_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
89 in_data_buffer.length = in_data_length;
91 status = smbd_smb2_request_verify_creditcharge(req, in_data_length);
92 if (!NT_STATUS_IS_OK(status)) {
93 return smbd_smb2_request_error(req, status);
96 if (req->compat_chain_fsp) {
97 /* skip check */
98 } else if (in_file_id_persistent != in_file_id_volatile) {
99 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
102 subreq = smbd_smb2_write_send(req,
103 req->sconn->ev_ctx,
104 req,
105 in_smbpid,
106 in_file_id_volatile,
107 in_data_buffer,
108 in_offset,
109 in_flags);
110 if (subreq == NULL) {
111 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
113 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
115 return smbd_smb2_request_pending_queue(req, subreq, 500);
118 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
120 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
121 struct smbd_smb2_request);
122 DATA_BLOB outbody;
123 DATA_BLOB outdyn;
124 uint32_t out_count = 0;
125 NTSTATUS status;
126 NTSTATUS error; /* transport error */
128 status = smbd_smb2_write_recv(subreq, &out_count);
129 TALLOC_FREE(subreq);
130 if (!NT_STATUS_IS_OK(status)) {
131 error = smbd_smb2_request_error(req, status);
132 if (!NT_STATUS_IS_OK(error)) {
133 smbd_server_connection_terminate(req->sconn,
134 nt_errstr(error));
135 return;
137 return;
140 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
141 if (outbody.data == NULL) {
142 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
143 if (!NT_STATUS_IS_OK(error)) {
144 smbd_server_connection_terminate(req->sconn,
145 nt_errstr(error));
146 return;
148 return;
151 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
152 SSVAL(outbody.data, 0x02, 0); /* reserved */
153 SIVAL(outbody.data, 0x04, out_count); /* count */
154 SIVAL(outbody.data, 0x08, 0); /* remaining */
155 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
156 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
158 outdyn = data_blob_const(NULL, 0);
160 error = smbd_smb2_request_done(req, outbody, &outdyn);
161 if (!NT_STATUS_IS_OK(error)) {
162 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
163 return;
167 struct smbd_smb2_write_state {
168 struct smbd_smb2_request *smb2req;
169 struct smb_request *smbreq;
170 files_struct *fsp;
171 bool write_through;
172 uint32_t in_length;
173 uint64_t in_offset;
174 uint32_t out_count;
177 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
179 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
181 NTSTATUS status;
182 struct smbd_smb2_write_state *state = tevent_req_data(req,
183 struct smbd_smb2_write_state);
184 files_struct *fsp = state->fsp;
186 if (nwritten == -1) {
187 status = map_nt_error_from_unix(err);
189 DEBUG(2, ("smb2_write failed: fnum=[%d/%s] "
190 "length=%lu offset=%lu nwritten=-1: %s\n",
191 fsp->fnum,
192 fsp_str_dbg(fsp),
193 (unsigned long)state->in_length,
194 (unsigned long)state->in_offset,
195 nt_errstr(status)));
197 return status;
200 DEBUG(3,("smb2: fnum=[%d/%s] "
201 "length=%lu offset=%lu wrote=%lu\n",
202 fsp->fnum,
203 fsp_str_dbg(fsp),
204 (unsigned long)state->in_length,
205 (unsigned long)state->in_offset,
206 (unsigned long)nwritten));
208 if ((nwritten == 0) && (state->in_length != 0)) {
209 DEBUG(5,("smb2: write [%s] disk full\n",
210 fsp_str_dbg(fsp)));
211 return NT_STATUS_DISK_FULL;
214 status = sync_file(fsp->conn, fsp, state->write_through);
215 if (!NT_STATUS_IS_OK(status)) {
216 DEBUG(5,("smb2: sync_file for %s returned %s\n",
217 fsp_str_dbg(fsp),
218 nt_errstr(status)));
219 return status;
222 state->out_count = nwritten;
224 return NT_STATUS_OK;
227 static bool smbd_smb2_write_cancel(struct tevent_req *req)
229 struct smbd_smb2_write_state *state =
230 tevent_req_data(req,
231 struct smbd_smb2_write_state);
233 state->smb2req->cancelled = true;
235 return cancel_smb2_aio(state->smbreq);
238 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
239 struct tevent_context *ev,
240 struct smbd_smb2_request *smb2req,
241 uint32_t in_smbpid,
242 uint64_t in_file_id_volatile,
243 DATA_BLOB in_data,
244 uint64_t in_offset,
245 uint32_t in_flags)
247 NTSTATUS status;
248 struct tevent_req *req = NULL;
249 struct smbd_smb2_write_state *state = NULL;
250 struct smb_request *smbreq = NULL;
251 connection_struct *conn = smb2req->tcon->compat_conn;
252 files_struct *fsp = NULL;
253 ssize_t nwritten;
254 struct lock_struct lock;
256 req = tevent_req_create(mem_ctx, &state,
257 struct smbd_smb2_write_state);
258 if (req == NULL) {
259 return NULL;
261 state->smb2req = smb2req;
262 if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
263 state->write_through = true;
265 state->in_length = in_data.length;
266 state->out_count = 0;
268 DEBUG(10,("smbd_smb2_write: file_id[0x%016llX]\n",
269 (unsigned long long)in_file_id_volatile));
271 smbreq = smbd_smb2_fake_smb_request(smb2req);
272 if (tevent_req_nomem(smbreq, req)) {
273 return tevent_req_post(req, ev);
275 state->smbreq = smbreq;
277 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
278 if (fsp == NULL) {
279 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
280 return tevent_req_post(req, ev);
282 if (conn != fsp->conn) {
283 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
284 return tevent_req_post(req, ev);
286 if (smb2req->session->vuid != fsp->vuid) {
287 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
288 return tevent_req_post(req, ev);
291 state->fsp = fsp;
293 if (IS_IPC(smbreq->conn)) {
294 struct tevent_req *subreq = NULL;
296 if (!fsp_is_np(fsp)) {
297 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
298 return tevent_req_post(req, ev);
301 subreq = np_write_send(state, ev,
302 fsp->fake_file_handle,
303 in_data.data,
304 in_data.length);
305 if (tevent_req_nomem(subreq, req)) {
306 return tevent_req_post(req, ev);
308 tevent_req_set_callback(subreq,
309 smbd_smb2_write_pipe_done,
310 req);
311 return req;
314 if (!CHECK_WRITE(fsp)) {
315 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
316 return tevent_req_post(req, ev);
319 /* Try and do an asynchronous write. */
320 status = schedule_aio_smb2_write(conn,
321 smbreq,
322 fsp,
323 in_offset,
324 in_data,
325 state->write_through);
327 if (NT_STATUS_IS_OK(status)) {
329 * Doing an async write, allow this
330 * request to be canceled
332 tevent_req_set_cancel_fn(req, smbd_smb2_write_cancel);
333 return req;
336 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
337 /* Real error in setting up aio. Fail. */
338 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
339 return tevent_req_post(req, ev);
342 /* Fallback to synchronous. */
343 init_strict_lock_struct(fsp,
344 fsp->fnum,
345 in_offset,
346 in_data.length,
347 WRITE_LOCK,
348 &lock);
350 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
351 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
352 return tevent_req_post(req, ev);
355 nwritten = write_file(smbreq, fsp,
356 (const char *)in_data.data,
357 in_offset,
358 in_data.length);
360 status = smb2_write_complete(req, nwritten, errno);
362 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
364 DEBUG(10,("smb2: write on "
365 "file %s, offset %.0f, requested %u, written = %u\n",
366 fsp_str_dbg(fsp),
367 (double)in_offset,
368 (unsigned int)in_data.length,
369 (unsigned int)nwritten ));
371 if (!NT_STATUS_IS_OK(status)) {
372 tevent_req_nterror(req, status);
373 } else {
374 /* Success. */
375 tevent_req_done(req);
378 return tevent_req_post(req, ev);
381 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
383 struct tevent_req *req = tevent_req_callback_data(subreq,
384 struct tevent_req);
385 struct smbd_smb2_write_state *state = tevent_req_data(req,
386 struct smbd_smb2_write_state);
387 NTSTATUS status;
388 ssize_t nwritten = -1;
390 status = np_write_recv(subreq, &nwritten);
391 TALLOC_FREE(subreq);
392 if (!NT_STATUS_IS_OK(status)) {
393 NTSTATUS old = status;
394 status = nt_status_np_pipe(old);
395 tevent_req_nterror(req, status);
396 return;
399 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
400 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
401 return;
404 state->out_count = nwritten;
406 tevent_req_done(req);
409 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
410 uint32_t *out_count)
412 NTSTATUS status;
413 struct smbd_smb2_write_state *state = tevent_req_data(req,
414 struct smbd_smb2_write_state);
416 if (tevent_req_is_nterror(req, &status)) {
417 tevent_req_received(req);
418 return status;
421 *out_count = state->out_count;
423 tevent_req_received(req);
424 return NT_STATUS_OK;