smbd: add vfs_valid_{pread,pwrite}_range() checks where needed
[Samba.git] / source3 / smbd / aio.c
blobaf8a01461a7e975885d86b952c4b30837f87c47b
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 async_io read handling using POSIX async io.
5 Copyright (C) Jeremy Allison 2005.
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 "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/tevent_wait.h"
28 /****************************************************************************
29 The buffer we keep around whilst an aio request is in process.
30 *****************************************************************************/
32 struct aio_extra {
33 files_struct *fsp;
34 struct smb_request *smbreq;
35 DATA_BLOB outbuf;
36 struct lock_struct lock;
37 size_t nbyte;
38 off_t offset;
39 bool write_through;
42 /****************************************************************************
43 Accessor function to return write_through state.
44 *****************************************************************************/
46 bool aio_write_through_requested(struct aio_extra *aio_ex)
48 return aio_ex->write_through;
51 /****************************************************************************
52 Create the extended aio struct we must keep around for the lifetime
53 of the aio call.
54 *****************************************************************************/
56 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
57 files_struct *fsp,
58 size_t buflen)
60 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
62 if (!aio_ex) {
63 return NULL;
66 /* The output buffer stored in the aio_ex is the start of
67 the smb return buffer. The buffer used in the acb
68 is the start of the reply data portion of that buffer. */
70 if (buflen) {
71 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
72 if (!aio_ex->outbuf.data) {
73 TALLOC_FREE(aio_ex);
74 return NULL;
77 aio_ex->fsp = fsp;
78 return aio_ex;
81 struct aio_req_fsp_link {
82 files_struct *fsp;
83 struct tevent_req *req;
86 static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
88 unsigned i;
89 files_struct *fsp = lnk->fsp;
90 struct tevent_req *req = lnk->req;
92 for (i=0; i<fsp->num_aio_requests; i++) {
93 if (fsp->aio_requests[i] == req) {
94 break;
97 if (i == fsp->num_aio_requests) {
98 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
99 return 0;
101 fsp->num_aio_requests -= 1;
102 fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
104 if (fsp->num_aio_requests == 0) {
105 tevent_wait_done(fsp->deferred_close);
106 TALLOC_FREE(fsp->aio_requests);
108 return 0;
111 bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
113 size_t array_len;
114 struct aio_req_fsp_link *lnk;
116 lnk = talloc(req, struct aio_req_fsp_link);
117 if (lnk == NULL) {
118 return false;
121 array_len = talloc_array_length(fsp->aio_requests);
122 if (array_len <= fsp->num_aio_requests) {
123 struct tevent_req **tmp;
125 if (fsp->num_aio_requests + 10 < 10) {
126 /* Integer wrap. */
127 TALLOC_FREE(lnk);
128 return false;
132 * Allocate in blocks of 10 so we don't allocate
133 * on every aio request.
135 tmp = talloc_realloc(
136 fsp, fsp->aio_requests, struct tevent_req *,
137 fsp->num_aio_requests+10);
138 if (tmp == NULL) {
139 TALLOC_FREE(lnk);
140 return false;
142 fsp->aio_requests = tmp;
144 fsp->aio_requests[fsp->num_aio_requests] = req;
145 fsp->num_aio_requests += 1;
147 lnk->fsp = fsp;
148 lnk->req = req;
149 talloc_set_destructor(lnk, aio_del_req_from_fsp);
151 return true;
154 static void aio_pread_smb1_done(struct tevent_req *req);
156 /****************************************************************************
157 Set up an aio request from a SMBreadX call.
158 *****************************************************************************/
160 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
161 struct smb_request *smbreq,
162 files_struct *fsp, off_t startpos,
163 size_t smb_maxcnt)
165 struct aio_extra *aio_ex;
166 size_t bufsize;
167 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
168 struct tevent_req *req;
169 bool ok;
171 ok = vfs_valid_pread_range(startpos, smb_maxcnt);
172 if (!ok) {
173 return NT_STATUS_INVALID_PARAMETER;
176 if (fsp->base_fsp != NULL) {
177 /* No AIO on streams yet */
178 DEBUG(10, ("AIO on streams not yet supported\n"));
179 return NT_STATUS_RETRY;
182 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
183 && !SMB_VFS_AIO_FORCE(fsp)) {
184 /* Too small a read for aio request. */
185 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
186 "for minimum aio_read of %u\n",
187 (unsigned int)smb_maxcnt,
188 (unsigned int)min_aio_read_size ));
189 return NT_STATUS_RETRY;
192 /* Only do this on non-chained and non-chaining reads */
193 if (req_is_in_chain(smbreq)) {
194 return NT_STATUS_RETRY;
197 /* The following is safe from integer wrap as we've already checked
198 smb_maxcnt is 128k or less. Wct is 12 for read replies */
200 bufsize = smb_size + 12 * 2 + smb_maxcnt + 1 /* padding byte */;
202 if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
203 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
204 return NT_STATUS_NO_MEMORY;
207 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
208 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
209 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
210 SCVAL(smb_buf(aio_ex->outbuf.data), 0, 0); /* padding byte */
212 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
213 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
214 &aio_ex->lock);
216 /* Take the lock until the AIO completes. */
217 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
218 TALLOC_FREE(aio_ex);
219 return NT_STATUS_FILE_LOCK_CONFLICT;
222 aio_ex->nbyte = smb_maxcnt;
223 aio_ex->offset = startpos;
225 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx,
226 fsp,
227 smb_buf(aio_ex->outbuf.data) + 1 /* pad */,
228 smb_maxcnt, startpos);
229 if (req == NULL) {
230 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
231 "Error %s\n", strerror(errno) ));
232 TALLOC_FREE(aio_ex);
233 return NT_STATUS_RETRY;
235 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
237 if (!aio_add_req_to_fsp(fsp, req)) {
238 DEBUG(1, ("Could not add req to fsp\n"));
239 TALLOC_FREE(aio_ex);
240 return NT_STATUS_RETRY;
243 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
245 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
246 "offset %.0f, len = %u (mid = %u)\n",
247 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
248 (unsigned int)aio_ex->smbreq->mid ));
250 return NT_STATUS_OK;
253 static void aio_pread_smb1_done(struct tevent_req *req)
255 struct aio_extra *aio_ex = tevent_req_callback_data(
256 req, struct aio_extra);
257 files_struct *fsp = aio_ex->fsp;
258 size_t outsize;
259 char *outbuf = (char *)aio_ex->outbuf.data;
260 ssize_t nread;
261 struct vfs_aio_state vfs_aio_state;
263 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
264 TALLOC_FREE(req);
266 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
267 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
269 if (fsp == NULL) {
270 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
271 "aio outstanding (mid[%llu]).\n",
272 (unsigned long long)aio_ex->smbreq->mid));
273 TALLOC_FREE(aio_ex);
274 return;
277 if (nread < 0) {
278 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
279 "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
280 strerror(vfs_aio_state.error)));
282 ERROR_NT(map_nt_error_from_unix(vfs_aio_state.error));
283 outsize = srv_set_message(outbuf,0,0,true);
284 } else {
285 outsize = setup_readX_header(outbuf, nread);
287 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
288 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
290 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
291 "nread=%d\n", fsp_str_dbg(fsp),
292 (int)aio_ex->nbyte, (int)nread ) );
296 if (outsize <= 4) {
297 DBG_INFO("Invalid outsize (%zu)\n", outsize);
298 TALLOC_FREE(aio_ex);
299 return;
301 outsize -= 4;
302 _smb_setlen_large(outbuf, outsize);
304 show_msg(outbuf);
305 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
306 true, aio_ex->smbreq->seqnum+1,
307 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
308 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
309 "failed.");
312 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
313 "for file %s, offset %.0f, len = %u\n",
314 fsp_str_dbg(fsp), (double)aio_ex->offset,
315 (unsigned int)nread));
317 TALLOC_FREE(aio_ex);
320 struct pwrite_fsync_state {
321 struct tevent_context *ev;
322 files_struct *fsp;
323 bool write_through;
324 ssize_t nwritten;
327 static void pwrite_fsync_write_done(struct tevent_req *subreq);
328 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
330 static struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
331 struct tevent_context *ev,
332 struct files_struct *fsp,
333 const void *data,
334 size_t n, off_t offset,
335 bool write_through)
337 struct tevent_req *req, *subreq;
338 struct pwrite_fsync_state *state;
339 bool ok;
341 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
342 if (req == NULL) {
343 return NULL;
345 state->ev = ev;
346 state->fsp = fsp;
347 state->write_through = write_through;
349 ok = vfs_valid_pwrite_range(offset, n);
350 if (!ok) {
351 tevent_req_error(req, EINVAL);
352 return tevent_req_post(req, ev);
355 if (n == 0) {
356 tevent_req_done(req);
357 return tevent_req_post(req, ev);
360 subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
361 if (tevent_req_nomem(subreq, req)) {
362 return tevent_req_post(req, ev);
364 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
365 return req;
368 static void pwrite_fsync_write_done(struct tevent_req *subreq)
370 struct tevent_req *req = tevent_req_callback_data(
371 subreq, struct tevent_req);
372 struct pwrite_fsync_state *state = tevent_req_data(
373 req, struct pwrite_fsync_state);
374 connection_struct *conn = state->fsp->conn;
375 bool do_sync;
376 struct vfs_aio_state vfs_aio_state;
378 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &vfs_aio_state);
379 TALLOC_FREE(subreq);
380 if (state->nwritten == -1) {
381 tevent_req_error(req, vfs_aio_state.error);
382 return;
385 do_sync = (lp_strict_sync(SNUM(conn)) &&
386 (lp_sync_always(SNUM(conn)) || state->write_through));
387 if (!do_sync) {
388 tevent_req_done(req);
389 return;
392 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
393 if (tevent_req_nomem(subreq, req)) {
394 return;
396 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
399 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
401 struct tevent_req *req = tevent_req_callback_data(
402 subreq, struct tevent_req);
403 int ret;
404 struct vfs_aio_state vfs_aio_state;
406 ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
407 TALLOC_FREE(subreq);
408 if (ret == -1) {
409 tevent_req_error(req, vfs_aio_state.error);
410 return;
412 tevent_req_done(req);
415 static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
417 struct pwrite_fsync_state *state = tevent_req_data(
418 req, struct pwrite_fsync_state);
420 if (tevent_req_is_unix_error(req, perr)) {
421 return -1;
423 return state->nwritten;
426 static void aio_pwrite_smb1_done(struct tevent_req *req);
428 /****************************************************************************
429 Set up an aio request from a SMBwriteX call.
430 *****************************************************************************/
432 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
433 struct smb_request *smbreq,
434 files_struct *fsp, const char *data,
435 off_t startpos,
436 size_t numtowrite)
438 struct aio_extra *aio_ex;
439 size_t bufsize;
440 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
441 struct tevent_req *req;
443 if (fsp->base_fsp != NULL) {
444 /* No AIO on streams yet */
445 DEBUG(10, ("AIO on streams not yet supported\n"));
446 return NT_STATUS_RETRY;
449 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
450 && !SMB_VFS_AIO_FORCE(fsp)) {
451 /* Too small a write for aio request. */
452 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
453 "small for minimum aio_write of %u\n",
454 (unsigned int)numtowrite,
455 (unsigned int)min_aio_write_size ));
456 return NT_STATUS_RETRY;
459 /* Only do this on non-chained and non-chaining writes */
460 if (req_is_in_chain(smbreq)) {
461 return NT_STATUS_RETRY;
464 bufsize = smb_size + 6*2;
466 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
467 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
468 return NT_STATUS_NO_MEMORY;
470 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
472 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
473 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
474 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
476 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
477 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
478 &aio_ex->lock);
480 /* Take the lock until the AIO completes. */
481 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
482 TALLOC_FREE(aio_ex);
483 return NT_STATUS_FILE_LOCK_CONFLICT;
486 aio_ex->nbyte = numtowrite;
487 aio_ex->offset = startpos;
489 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
490 data, numtowrite, startpos,
491 aio_ex->write_through);
492 if (req == NULL) {
493 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
494 "Error %s\n", strerror(errno) ));
495 TALLOC_FREE(aio_ex);
496 return NT_STATUS_RETRY;
498 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
500 if (!aio_add_req_to_fsp(fsp, req)) {
501 DEBUG(1, ("Could not add req to fsp\n"));
502 TALLOC_FREE(aio_ex);
503 return NT_STATUS_RETRY;
506 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
508 /* This should actually be improved to span the write. */
509 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
510 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
512 if (!aio_ex->write_through && !lp_sync_always(SNUM(fsp->conn))
513 && fsp->aio_write_behind) {
514 /* Lie to the client and immediately claim we finished the
515 * write. */
516 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
517 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
518 show_msg((char *)aio_ex->outbuf.data);
519 if (!srv_send_smb(aio_ex->smbreq->xconn,
520 (char *)aio_ex->outbuf.data,
521 true, aio_ex->smbreq->seqnum+1,
522 IS_CONN_ENCRYPTED(fsp->conn),
523 &aio_ex->smbreq->pcd)) {
524 exit_server_cleanly("schedule_aio_write_and_X: "
525 "srv_send_smb failed.");
527 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
528 "behind for file %s\n", fsp_str_dbg(fsp)));
531 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
532 "%s, offset %.0f, len = %u (mid = %u)\n",
533 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
534 (unsigned int)aio_ex->smbreq->mid));
536 return NT_STATUS_OK;
539 static void aio_pwrite_smb1_done(struct tevent_req *req)
541 struct aio_extra *aio_ex = tevent_req_callback_data(
542 req, struct aio_extra);
543 files_struct *fsp = aio_ex->fsp;
544 char *outbuf = (char *)aio_ex->outbuf.data;
545 ssize_t numtowrite = aio_ex->nbyte;
546 ssize_t nwritten;
547 int err;
549 nwritten = pwrite_fsync_recv(req, &err);
550 TALLOC_FREE(req);
552 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
553 (nwritten == -1) ? strerror(err) : "no error"));
555 if (fsp == NULL) {
556 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
557 "aio outstanding (mid[%llu]).\n",
558 (unsigned long long)aio_ex->smbreq->mid));
559 TALLOC_FREE(aio_ex);
560 return;
563 mark_file_modified(fsp);
565 if (fsp->aio_write_behind) {
567 if (nwritten != numtowrite) {
568 if (nwritten == -1) {
569 DEBUG(5,("handle_aio_write_complete: "
570 "aio_write_behind failed ! File %s "
571 "is corrupt ! Error %s\n",
572 fsp_str_dbg(fsp), strerror(err)));
573 } else {
574 DEBUG(0,("handle_aio_write_complete: "
575 "aio_write_behind failed ! File %s "
576 "is corrupt ! Wanted %u bytes but "
577 "only wrote %d\n", fsp_str_dbg(fsp),
578 (unsigned int)numtowrite,
579 (int)nwritten ));
581 } else {
582 DEBUG(10,("handle_aio_write_complete: "
583 "aio_write_behind completed for file %s\n",
584 fsp_str_dbg(fsp)));
586 /* TODO: should no return success in case of an error !!! */
587 TALLOC_FREE(aio_ex);
588 return;
591 /* We don't need outsize or set_message here as we've already set the
592 fixed size length when we set up the aio call. */
594 if (nwritten == -1) {
595 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
596 "nwritten == %d. Error = %s\n",
597 fsp_str_dbg(fsp), (unsigned int)numtowrite,
598 (int)nwritten, strerror(err)));
600 ERROR_NT(map_nt_error_from_unix(err));
601 srv_set_message(outbuf,0,0,true);
602 } else {
603 SSVAL(outbuf,smb_vwv2,nwritten);
604 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
605 if (nwritten < (ssize_t)numtowrite) {
606 SCVAL(outbuf,smb_rcls,ERRHRD);
607 SSVAL(outbuf,smb_err,ERRdiskfull);
610 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
611 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
613 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
616 show_msg(outbuf);
617 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
618 true, aio_ex->smbreq->seqnum+1,
619 IS_CONN_ENCRYPTED(fsp->conn),
620 NULL)) {
621 exit_server_cleanly("handle_aio_write_complete: "
622 "srv_send_smb failed.");
625 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
626 "for file %s, offset %.0f, requested %u, written = %u\n",
627 fsp_str_dbg(fsp), (double)aio_ex->offset,
628 (unsigned int)numtowrite, (unsigned int)nwritten));
630 TALLOC_FREE(aio_ex);
633 bool cancel_smb2_aio(struct smb_request *smbreq)
635 struct smbd_smb2_request *smb2req = smbreq->smb2req;
636 struct aio_extra *aio_ex = NULL;
638 if (smb2req) {
639 aio_ex = talloc_get_type(smbreq->async_priv,
640 struct aio_extra);
643 if (aio_ex == NULL) {
644 return false;
647 if (aio_ex->fsp == NULL) {
648 return false;
652 * We let the aio request run and don't try to cancel it which means
653 * processing of the SMB2 request must continue as normal, cf MS-SMB2
654 * 3.3.5.16:
656 * If the target request is not successfully canceled, processing of
657 * the target request MUST continue and no response is sent to the
658 * cancel request.
661 return false;
664 static void aio_pread_smb2_done(struct tevent_req *req);
666 /****************************************************************************
667 Set up an aio request from a SMB2 read call.
668 *****************************************************************************/
670 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
671 struct smb_request *smbreq,
672 files_struct *fsp,
673 TALLOC_CTX *ctx,
674 DATA_BLOB *preadbuf,
675 off_t startpos,
676 size_t smb_maxcnt)
678 struct aio_extra *aio_ex;
679 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
680 struct tevent_req *req;
681 bool ok;
683 ok = vfs_valid_pread_range(startpos, smb_maxcnt);
684 if (!ok) {
685 return NT_STATUS_INVALID_PARAMETER;
688 if (fsp->base_fsp != NULL) {
689 /* No AIO on streams yet */
690 DEBUG(10, ("AIO on streams not yet supported\n"));
691 return NT_STATUS_RETRY;
694 if (fsp->op == NULL) {
695 /* No AIO on internal opens. */
696 return NT_STATUS_RETRY;
699 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
700 && !SMB_VFS_AIO_FORCE(fsp)) {
701 /* Too small a read for aio request. */
702 DEBUG(10,("smb2: read size (%u) too small "
703 "for minimum aio_read of %u\n",
704 (unsigned int)smb_maxcnt,
705 (unsigned int)min_aio_read_size ));
706 return NT_STATUS_RETRY;
709 if (smbd_smb2_is_compound(smbreq->smb2req)) {
710 return NT_STATUS_RETRY;
713 /* Create the out buffer. */
714 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
715 if (preadbuf->data == NULL) {
716 return NT_STATUS_NO_MEMORY;
719 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
720 return NT_STATUS_NO_MEMORY;
723 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
724 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
725 &aio_ex->lock);
727 /* Take the lock until the AIO completes. */
728 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
729 TALLOC_FREE(aio_ex);
730 return NT_STATUS_FILE_LOCK_CONFLICT;
733 aio_ex->nbyte = smb_maxcnt;
734 aio_ex->offset = startpos;
736 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
737 preadbuf->data, smb_maxcnt, startpos);
738 if (req == NULL) {
739 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
740 "Error %s\n", strerror(errno)));
741 TALLOC_FREE(aio_ex);
742 return NT_STATUS_RETRY;
744 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
746 if (!aio_add_req_to_fsp(fsp, req)) {
747 DEBUG(1, ("Could not add req to fsp\n"));
748 TALLOC_FREE(aio_ex);
749 return NT_STATUS_RETRY;
752 /* We don't need talloc_move here as both aio_ex and
753 * smbreq are children of smbreq->smb2req. */
754 aio_ex->smbreq = smbreq;
755 smbreq->async_priv = aio_ex;
757 DEBUG(10,("smb2: scheduled aio_read for file %s, "
758 "offset %.0f, len = %u (mid = %u)\n",
759 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
760 (unsigned int)aio_ex->smbreq->mid ));
762 return NT_STATUS_OK;
765 static void aio_pread_smb2_done(struct tevent_req *req)
767 struct aio_extra *aio_ex = tevent_req_callback_data(
768 req, struct aio_extra);
769 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
770 files_struct *fsp = aio_ex->fsp;
771 NTSTATUS status;
772 ssize_t nread;
773 struct vfs_aio_state vfs_aio_state = { 0 };
775 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
776 TALLOC_FREE(req);
778 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
779 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
781 /* Common error or success code processing for async or sync
782 read returns. */
784 status = smb2_read_complete(subreq, nread, vfs_aio_state.error);
786 if (nread > 0) {
787 fsp->fh->pos = aio_ex->offset + nread;
788 fsp->fh->position_information = fsp->fh->pos;
791 DEBUG(10, ("smb2: scheduled aio_read completed "
792 "for file %s, offset %.0f, len = %u "
793 "(errcode = %d, NTSTATUS = %s)\n",
794 fsp_str_dbg(aio_ex->fsp),
795 (double)aio_ex->offset,
796 (unsigned int)nread,
797 vfs_aio_state.error, nt_errstr(status)));
799 if (!NT_STATUS_IS_OK(status)) {
800 tevent_req_nterror(subreq, status);
801 return;
803 tevent_req_done(subreq);
806 static void aio_pwrite_smb2_done(struct tevent_req *req);
808 /****************************************************************************
809 Set up an aio request from a SMB2write call.
810 *****************************************************************************/
812 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
813 struct smb_request *smbreq,
814 files_struct *fsp,
815 uint64_t in_offset,
816 DATA_BLOB in_data,
817 bool write_through)
819 struct aio_extra *aio_ex = NULL;
820 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
821 struct tevent_req *req;
823 if (fsp->base_fsp != NULL) {
824 /* No AIO on streams yet */
825 DEBUG(10, ("AIO on streams not yet supported\n"));
826 return NT_STATUS_RETRY;
829 if (fsp->op == NULL) {
830 /* No AIO on internal opens. */
831 return NT_STATUS_RETRY;
834 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
835 && !SMB_VFS_AIO_FORCE(fsp)) {
836 /* Too small a write for aio request. */
837 DEBUG(10,("smb2: write size (%u) too "
838 "small for minimum aio_write of %u\n",
839 (unsigned int)in_data.length,
840 (unsigned int)min_aio_write_size ));
841 return NT_STATUS_RETRY;
844 if (smbd_smb2_is_compound(smbreq->smb2req)) {
845 return NT_STATUS_RETRY;
848 if (smbreq->unread_bytes) {
849 /* Can't do async with recvfile. */
850 return NT_STATUS_RETRY;
853 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
854 return NT_STATUS_NO_MEMORY;
857 aio_ex->write_through = write_through;
859 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
860 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
861 &aio_ex->lock);
863 /* Take the lock until the AIO completes. */
864 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
865 TALLOC_FREE(aio_ex);
866 return NT_STATUS_FILE_LOCK_CONFLICT;
869 aio_ex->nbyte = in_data.length;
870 aio_ex->offset = in_offset;
872 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
873 in_data.data, in_data.length, in_offset,
874 write_through);
875 if (req == NULL) {
876 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
877 "Error %s\n", strerror(errno)));
878 TALLOC_FREE(aio_ex);
879 return NT_STATUS_RETRY;
881 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
883 if (!aio_add_req_to_fsp(fsp, req)) {
884 DEBUG(1, ("Could not add req to fsp\n"));
885 TALLOC_FREE(aio_ex);
886 return NT_STATUS_RETRY;
889 /* We don't need talloc_move here as both aio_ex and
890 * smbreq are children of smbreq->smb2req. */
891 aio_ex->smbreq = smbreq;
892 smbreq->async_priv = aio_ex;
894 /* This should actually be improved to span the write. */
895 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
896 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
899 * We don't want to do write behind due to ownership
900 * issues of the request structs. Maybe add it if I
901 * figure those out. JRA.
904 DEBUG(10,("smb2: scheduled aio_write for file "
905 "%s, offset %.0f, len = %u (mid = %u)\n",
906 fsp_str_dbg(fsp),
907 (double)in_offset,
908 (unsigned int)in_data.length,
909 (unsigned int)aio_ex->smbreq->mid));
911 return NT_STATUS_OK;
914 static void aio_pwrite_smb2_done(struct tevent_req *req)
916 struct aio_extra *aio_ex = tevent_req_callback_data(
917 req, struct aio_extra);
918 ssize_t numtowrite = aio_ex->nbyte;
919 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
920 files_struct *fsp = aio_ex->fsp;
921 NTSTATUS status;
922 ssize_t nwritten;
923 int err = 0;
925 nwritten = pwrite_fsync_recv(req, &err);
926 TALLOC_FREE(req);
928 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
929 (nwritten == -1) ? strerror(err) : "no error"));
931 mark_file_modified(fsp);
933 status = smb2_write_complete_nosync(subreq, nwritten, err);
935 DEBUG(10, ("smb2: scheduled aio_write completed "
936 "for file %s, offset %.0f, requested %u, "
937 "written = %u (errcode = %d, NTSTATUS = %s)\n",
938 fsp_str_dbg(fsp),
939 (double)aio_ex->offset,
940 (unsigned int)numtowrite,
941 (unsigned int)nwritten,
942 err, nt_errstr(status)));
944 if (!NT_STATUS_IS_OK(status)) {
945 tevent_req_nterror(subreq, status);
946 return;
948 tevent_req_done(subreq);