s3:smb2_create: don't take 'state->te' as indication for "was_deferred" (bug #9196)
[Samba.git] / source3 / smbd / smb2_read.c
blob2890f8674531cc5b3c294c78fbd3299ca194bd5f
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 "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "libcli/security/security.h"
27 #include "../lib/util/tevent_ntstatus.h"
28 #include "rpc_server/srv_pipe_hnd.h"
30 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
31 struct tevent_context *ev,
32 struct smbd_smb2_request *smb2req,
33 struct files_struct *in_fsp,
34 uint32_t in_length,
35 uint64_t in_offset,
36 uint32_t in_minimum,
37 uint32_t in_remaining);
38 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
39 TALLOC_CTX *mem_ctx,
40 DATA_BLOB *out_data,
41 uint32_t *out_remaining);
43 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
44 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
46 NTSTATUS status;
47 const uint8_t *inbody;
48 uint32_t in_length;
49 uint64_t in_offset;
50 uint64_t in_file_id_persistent;
51 uint64_t in_file_id_volatile;
52 struct files_struct *in_fsp;
53 uint32_t in_minimum_count;
54 uint32_t in_remaining_bytes;
55 struct tevent_req *subreq;
57 status = smbd_smb2_request_verify_sizes(req, 0x31);
58 if (!NT_STATUS_IS_OK(status)) {
59 return smbd_smb2_request_error(req, status);
61 inbody = SMBD_SMB2_IN_BODY_PTR(req);
63 in_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_minimum_count = IVAL(inbody, 0x20);
68 in_remaining_bytes = IVAL(inbody, 0x28);
70 /* check the max read size */
71 if (in_length > req->sconn->smb2.max_read) {
72 DEBUG(2,("smbd_smb2_request_process_read: "
73 "client ignored max read: %s: 0x%08X: 0x%08X\n",
74 __location__, in_length, req->sconn->smb2.max_read));
75 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
78 status = smbd_smb2_request_verify_creditcharge(req, in_length);
79 if (!NT_STATUS_IS_OK(status)) {
80 return smbd_smb2_request_error(req, status);
83 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
84 if (in_fsp == NULL) {
85 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
88 subreq = smbd_smb2_read_send(req, req->sconn->ev_ctx,
89 req, in_fsp,
90 in_length,
91 in_offset,
92 in_minimum_count,
93 in_remaining_bytes);
94 if (subreq == NULL) {
95 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
97 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
99 return smbd_smb2_request_pending_queue(req, subreq, 500);
102 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
104 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
105 struct smbd_smb2_request);
106 DATA_BLOB outbody;
107 DATA_BLOB outdyn;
108 uint8_t out_data_offset;
109 DATA_BLOB out_data_buffer = data_blob_null;
110 uint32_t out_data_remaining = 0;
111 NTSTATUS status;
112 NTSTATUS error; /* transport error */
114 status = smbd_smb2_read_recv(subreq,
115 req,
116 &out_data_buffer,
117 &out_data_remaining);
118 TALLOC_FREE(subreq);
119 if (!NT_STATUS_IS_OK(status)) {
120 error = smbd_smb2_request_error(req, status);
121 if (!NT_STATUS_IS_OK(error)) {
122 smbd_server_connection_terminate(req->sconn,
123 nt_errstr(error));
124 return;
126 return;
129 out_data_offset = SMB2_HDR_BODY + 0x10;
131 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
132 if (outbody.data == NULL) {
133 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
134 if (!NT_STATUS_IS_OK(error)) {
135 smbd_server_connection_terminate(req->sconn,
136 nt_errstr(error));
137 return;
139 return;
142 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
143 SCVAL(outbody.data, 0x02,
144 out_data_offset); /* data offset */
145 SCVAL(outbody.data, 0x03, 0); /* reserved */
146 SIVAL(outbody.data, 0x04,
147 out_data_buffer.length); /* data length */
148 SIVAL(outbody.data, 0x08,
149 out_data_remaining); /* data remaining */
150 SIVAL(outbody.data, 0x0C, 0); /* reserved */
152 outdyn = out_data_buffer;
154 error = smbd_smb2_request_done(req, outbody, &outdyn);
155 if (!NT_STATUS_IS_OK(error)) {
156 smbd_server_connection_terminate(req->sconn,
157 nt_errstr(error));
158 return;
162 struct smbd_smb2_read_state {
163 struct smbd_smb2_request *smb2req;
164 struct smb_request *smbreq;
165 files_struct *fsp;
166 uint32_t in_length;
167 uint64_t in_offset;
168 uint32_t in_minimum;
169 DATA_BLOB out_data;
170 uint32_t out_remaining;
173 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
174 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
176 struct lock_struct lock;
177 uint32_t in_length = state->in_length;
178 uint64_t in_offset = state->in_offset;
179 files_struct *fsp = state->fsp;
180 ssize_t nread;
182 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
183 fsp,
184 NULL,
185 in_offset,
186 in_length);
187 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
188 (int)nread,
189 fsp_str_dbg(fsp) ));
191 if (nread == -1) {
192 if (errno == ENOSYS || errno == EINTR) {
194 * Special hack for broken systems with no working
195 * sendfile. Fake this up by doing read/write calls.
197 set_use_sendfile(SNUM(fsp->conn), false);
198 nread = fake_sendfile(fsp, in_offset, in_length);
199 if (nread == -1) {
200 DEBUG(0,("smb2_sendfile_send_data: "
201 "fake_sendfile failed for "
202 "file %s (%s).\n",
203 fsp_str_dbg(fsp),
204 strerror(errno)));
205 exit_server_cleanly("smb2_sendfile_send_data: "
206 "fake_sendfile failed");
208 goto out;
211 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
212 "%s (%s). Terminating\n",
213 fsp_str_dbg(fsp),
214 strerror(errno)));
215 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
216 } else if (nread == 0) {
218 * Some sendfile implementations return 0 to indicate
219 * that there was a short read, but nothing was
220 * actually written to the socket. In this case,
221 * fallback to the normal read path so the header gets
222 * the correct byte count.
224 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
225 "falling back to the normal read: %s\n",
226 fsp_str_dbg(fsp)));
228 nread = fake_sendfile(fsp, in_offset, in_length);
229 if (nread == -1) {
230 DEBUG(0,("smb2_sendfile_send_data: "
231 "fake_sendfile failed for file "
232 "%s (%s). Terminating\n",
233 fsp_str_dbg(fsp),
234 strerror(errno)));
235 exit_server_cleanly("smb2_sendfile_send_data: "
236 "fake_sendfile failed");
240 out:
242 if (nread < in_length) {
243 sendfile_short_send(fsp, nread, 0, in_length);
246 init_strict_lock_struct(fsp,
247 fsp->op->global->open_persistent_id,
248 in_offset,
249 in_length,
250 READ_LOCK,
251 &lock);
253 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
254 return 0;
257 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
258 struct smbd_smb2_read_state *state)
260 struct smbd_smb2_read_state *state_copy = NULL;
261 files_struct *fsp = state->fsp;
264 * We cannot use sendfile if...
265 * We were not configured to do so OR
266 * Signing is active OR
267 * This is a compound SMB2 operation OR
268 * fsp is a STREAM file OR
269 * We're using a write cache OR
270 * It's not a regular file OR
271 * Requested offset is greater than file size OR
272 * there's not enough data in the file.
273 * Phew :-). Luckily this means most
274 * reads on most normal files. JRA.
277 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
278 smb2req->do_signing ||
279 smb2req->do_encryption ||
280 smb2req->in.vector_count < (2*SMBD_SMB2_NUM_IOV_PER_REQ) ||
281 (fsp->base_fsp != NULL) ||
282 (fsp->wcp != NULL) ||
283 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
284 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
285 (fsp->fsp_name->st.st_ex_size < state->in_offset + state->in_length))
287 return NT_STATUS_RETRY;
290 /* We've already checked there's this amount of data
291 to read. */
292 state->out_data.length = state->in_length;
293 state->out_remaining = 0;
295 /* Make a copy of state attached to the smb2req. Attach
296 the destructor here as this will trigger the sendfile
297 call when the request is destroyed. */
298 state_copy = talloc(smb2req, struct smbd_smb2_read_state);
299 if (!state_copy) {
300 return NT_STATUS_NO_MEMORY;
302 *state_copy = *state;
303 talloc_set_destructor(state_copy, smb2_sendfile_send_data);
304 return NT_STATUS_OK;
307 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
309 /*******************************************************************
310 Common read complete processing function for both synchronous and
311 asynchronous reads.
312 *******************************************************************/
314 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
316 struct smbd_smb2_read_state *state = tevent_req_data(req,
317 struct smbd_smb2_read_state);
318 files_struct *fsp = state->fsp;
320 if (nread < 0) {
321 NTSTATUS status = map_nt_error_from_unix(err);
323 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
324 "Error = %s (NTSTATUS %s)\n",
325 fsp_str_dbg(fsp),
326 (int)nread,
327 strerror(err),
328 nt_errstr(status)));
330 return status;
332 if (nread == 0 && state->in_length != 0) {
333 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
334 fsp_str_dbg(fsp)));
335 return NT_STATUS_END_OF_FILE;
338 if (nread < state->in_minimum) {
339 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
340 "minimum requested %u. Returning end of file\n",
341 fsp_str_dbg(fsp),
342 (int)nread,
343 (unsigned int)state->in_minimum));
344 return NT_STATUS_END_OF_FILE;
347 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
348 fsp_fnum_dbg(fsp),
349 fsp_str_dbg(fsp),
350 (unsigned long)state->in_length,
351 (unsigned long)state->in_offset,
352 (unsigned long)nread));
354 state->out_data.length = nread;
355 state->out_remaining = 0;
357 return NT_STATUS_OK;
360 static bool smbd_smb2_read_cancel(struct tevent_req *req)
362 struct smbd_smb2_read_state *state =
363 tevent_req_data(req,
364 struct smbd_smb2_read_state);
366 return cancel_smb2_aio(state->smbreq);
369 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
370 struct tevent_context *ev,
371 struct smbd_smb2_request *smb2req,
372 struct files_struct *fsp,
373 uint32_t in_length,
374 uint64_t in_offset,
375 uint32_t in_minimum,
376 uint32_t in_remaining)
378 NTSTATUS status;
379 struct tevent_req *req = NULL;
380 struct smbd_smb2_read_state *state = NULL;
381 struct smb_request *smbreq = NULL;
382 connection_struct *conn = smb2req->tcon->compat;
383 ssize_t nread = -1;
384 struct lock_struct lock;
385 int saved_errno;
387 req = tevent_req_create(mem_ctx, &state,
388 struct smbd_smb2_read_state);
389 if (req == NULL) {
390 return NULL;
392 state->smb2req = smb2req;
393 state->in_length = in_length;
394 state->in_offset = in_offset;
395 state->in_minimum = in_minimum;
396 state->out_data = data_blob_null;
397 state->out_remaining = 0;
399 DEBUG(10,("smbd_smb2_read: %s - %s\n",
400 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
402 smbreq = smbd_smb2_fake_smb_request(smb2req);
403 if (tevent_req_nomem(smbreq, req)) {
404 return tevent_req_post(req, ev);
406 state->smbreq = smbreq;
408 if (fsp->is_directory) {
409 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
410 return tevent_req_post(req, ev);
413 state->fsp = fsp;
415 if (IS_IPC(smbreq->conn)) {
416 struct tevent_req *subreq = NULL;
418 state->out_data = data_blob_talloc(state, NULL, in_length);
419 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
420 return tevent_req_post(req, ev);
423 if (!fsp_is_np(fsp)) {
424 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
425 return tevent_req_post(req, ev);
428 subreq = np_read_send(state, ev,
429 fsp->fake_file_handle,
430 state->out_data.data,
431 state->out_data.length);
432 if (tevent_req_nomem(subreq, req)) {
433 return tevent_req_post(req, ev);
435 tevent_req_set_callback(subreq,
436 smbd_smb2_read_pipe_done,
437 req);
438 return req;
441 if (!CHECK_READ(fsp, smbreq)) {
442 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
443 return tevent_req_post(req, ev);
446 status = schedule_smb2_aio_read(fsp->conn,
447 smbreq,
448 fsp,
449 state,
450 &state->out_data,
451 (off_t)in_offset,
452 (size_t)in_length);
454 if (NT_STATUS_IS_OK(status)) {
456 * Doing an async read, allow this
457 * request to be canceled
459 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
460 return req;
463 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
464 /* Real error in setting up aio. Fail. */
465 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
466 return tevent_req_post(req, ev);
469 /* Fallback to synchronous. */
471 init_strict_lock_struct(fsp,
472 fsp->op->global->open_persistent_id,
473 in_offset,
474 in_length,
475 READ_LOCK,
476 &lock);
478 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
479 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
480 return tevent_req_post(req, ev);
483 /* Try sendfile in preference. */
484 status = schedule_smb2_sendfile_read(smb2req, state);
485 if (NT_STATUS_IS_OK(status)) {
486 tevent_req_done(req);
487 return tevent_req_post(req, ev);
488 } else {
489 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
490 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
491 tevent_req_nterror(req, status);
492 return tevent_req_post(req, ev);
496 /* Ok, read into memory. Allocate the out buffer. */
497 state->out_data = data_blob_talloc(state, NULL, in_length);
498 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
499 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
500 return tevent_req_post(req, ev);
503 nread = read_file(fsp,
504 (char *)state->out_data.data,
505 in_offset,
506 in_length);
508 saved_errno = errno;
510 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
512 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
513 "len=%llu returned %lld\n",
514 fsp_str_dbg(fsp),
515 fsp_fnum_dbg(fsp),
516 (unsigned long long)in_offset,
517 (unsigned long long)in_length,
518 (long long)nread));
520 status = smb2_read_complete(req, nread, saved_errno);
521 if (!NT_STATUS_IS_OK(status)) {
522 tevent_req_nterror(req, status);
523 } else {
524 /* Success. */
525 tevent_req_done(req);
527 return tevent_req_post(req, ev);
530 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
532 struct tevent_req *req = tevent_req_callback_data(subreq,
533 struct tevent_req);
534 struct smbd_smb2_read_state *state = tevent_req_data(req,
535 struct smbd_smb2_read_state);
536 NTSTATUS status;
537 ssize_t nread = -1;
538 bool is_data_outstanding;
540 status = np_read_recv(subreq, &nread, &is_data_outstanding);
541 TALLOC_FREE(subreq);
542 if (!NT_STATUS_IS_OK(status)) {
543 NTSTATUS old = status;
544 status = nt_status_np_pipe(old);
545 tevent_req_nterror(req, status);
546 return;
549 if (nread == 0 && state->out_data.length != 0) {
550 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
551 return;
554 state->out_data.length = nread;
555 state->out_remaining = 0;
558 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
559 * handle it in SMB1 pipe_read_andx_done().
562 tevent_req_done(req);
565 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
566 TALLOC_CTX *mem_ctx,
567 DATA_BLOB *out_data,
568 uint32_t *out_remaining)
570 NTSTATUS status;
571 struct smbd_smb2_read_state *state = tevent_req_data(req,
572 struct smbd_smb2_read_state);
574 if (tevent_req_is_nterror(req, &status)) {
575 tevent_req_received(req);
576 return status;
579 *out_data = state->out_data;
580 talloc_steal(mem_ctx, out_data->data);
581 *out_remaining = state->out_remaining;
583 tevent_req_received(req);
584 return NT_STATUS_OK;