2 Unix SMB/Netbios implementation.
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/>.
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
27 /****************************************************************************
28 The buffer we keep around whilst an aio request is in process.
29 *****************************************************************************/
33 struct smb_request *smbreq;
35 struct lock_struct lock;
41 /****************************************************************************
42 Accessor function to return write_through state.
43 *****************************************************************************/
45 bool aio_write_through_requested(struct aio_extra *aio_ex)
47 return aio_ex->write_through;
50 /****************************************************************************
51 Create the extended aio struct we must keep around for the lifetime
53 *****************************************************************************/
55 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
59 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
65 /* The output buffer stored in the aio_ex is the start of
66 the smb return buffer. The buffer used in the acb
67 is the start of the reply data portion of that buffer. */
70 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
71 if (!aio_ex->outbuf.data) {
80 struct aio_req_fsp_link {
82 struct tevent_req *req;
85 static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
88 files_struct *fsp = lnk->fsp;
89 struct tevent_req *req = lnk->req;
91 for (i=0; i<fsp->num_aio_requests; i++) {
92 if (fsp->aio_requests[i] == req) {
96 if (i == fsp->num_aio_requests) {
97 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
100 fsp->num_aio_requests -= 1;
101 fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
103 if (fsp->num_aio_requests == 0) {
104 TALLOC_FREE(fsp->aio_requests);
109 bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
112 struct aio_req_fsp_link *lnk;
114 lnk = talloc(req, struct aio_req_fsp_link);
119 array_len = talloc_array_length(fsp->aio_requests);
120 if (array_len <= fsp->num_aio_requests) {
121 struct tevent_req **tmp;
123 if (fsp->num_aio_requests + 10 < 10) {
130 * Allocate in blocks of 10 so we don't allocate
131 * on every aio request.
133 tmp = talloc_realloc(
134 fsp, fsp->aio_requests, struct tevent_req *,
135 fsp->num_aio_requests+10);
140 fsp->aio_requests = tmp;
142 fsp->aio_requests[fsp->num_aio_requests] = req;
143 fsp->num_aio_requests += 1;
147 talloc_set_destructor(lnk, aio_del_req_from_fsp);
152 static void aio_pread_smb1_done(struct tevent_req *req);
154 /****************************************************************************
155 Set up an aio request from a SMBreadX call.
156 *****************************************************************************/
158 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
159 struct smb_request *smbreq,
160 files_struct *fsp, off_t startpos,
163 struct aio_extra *aio_ex;
165 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
166 struct tevent_req *req;
168 if (fsp->base_fsp != NULL) {
169 /* No AIO on streams yet */
170 DEBUG(10, ("AIO on streams not yet supported\n"));
171 return NT_STATUS_RETRY;
174 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
175 && !SMB_VFS_AIO_FORCE(fsp)) {
176 /* Too small a read for aio request. */
177 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
178 "for minimum aio_read of %u\n",
179 (unsigned int)smb_maxcnt,
180 (unsigned int)min_aio_read_size ));
181 return NT_STATUS_RETRY;
184 /* Only do this on non-chained and non-chaining reads */
185 if (req_is_in_chain(smbreq)) {
186 return NT_STATUS_RETRY;
189 /* The following is safe from integer wrap as we've already checked
190 smb_maxcnt is 128k or less. Wct is 12 for read replies */
192 bufsize = smb_size + 12 * 2 + smb_maxcnt + 1 /* padding byte */;
194 if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
195 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
196 return NT_STATUS_NO_MEMORY;
199 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
200 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
201 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
202 SCVAL(smb_buf(aio_ex->outbuf.data), 0, 0); /* padding byte */
204 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
205 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
208 /* Take the lock until the AIO completes. */
209 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
211 return NT_STATUS_FILE_LOCK_CONFLICT;
214 aio_ex->nbyte = smb_maxcnt;
215 aio_ex->offset = startpos;
217 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx,
219 smb_buf(aio_ex->outbuf.data) + 1 /* pad */,
220 smb_maxcnt, startpos);
222 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
223 "Error %s\n", strerror(errno) ));
225 return NT_STATUS_RETRY;
227 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
229 if (!aio_add_req_to_fsp(fsp, req)) {
230 DEBUG(1, ("Could not add req to fsp\n"));
232 return NT_STATUS_RETRY;
235 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
237 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
238 "offset %.0f, len = %u (mid = %u)\n",
239 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
240 (unsigned int)aio_ex->smbreq->mid ));
245 static void aio_pread_smb1_done(struct tevent_req *req)
247 struct aio_extra *aio_ex = tevent_req_callback_data(
248 req, struct aio_extra);
249 files_struct *fsp = aio_ex->fsp;
251 char *outbuf = (char *)aio_ex->outbuf.data;
253 struct vfs_aio_state vfs_aio_state;
255 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
258 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
259 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
262 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
263 "aio outstanding (mid[%llu]).\n",
264 (unsigned long long)aio_ex->smbreq->mid));
270 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
271 "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
272 strerror(vfs_aio_state.error)));
274 ERROR_NT(map_nt_error_from_unix(vfs_aio_state.error));
275 outsize = srv_set_message(outbuf,0,0,true);
277 outsize = setup_readX_header(outbuf, nread);
279 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
280 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
282 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
283 "nread=%d\n", fsp_str_dbg(fsp),
284 (int)aio_ex->nbyte, (int)nread ) );
289 DBG_INFO("Invalid outsize (%zu)\n", outsize);
294 _smb_setlen_large(outbuf, outsize);
297 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
298 true, aio_ex->smbreq->seqnum+1,
299 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
300 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
304 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
305 "for file %s, offset %.0f, len = %u\n",
306 fsp_str_dbg(fsp), (double)aio_ex->offset,
307 (unsigned int)nread));
312 struct pwrite_fsync_state {
313 struct tevent_context *ev;
319 static void pwrite_fsync_write_done(struct tevent_req *subreq);
320 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
322 static struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
323 struct tevent_context *ev,
324 struct files_struct *fsp,
326 size_t n, off_t offset,
329 struct tevent_req *req, *subreq;
330 struct pwrite_fsync_state *state;
332 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
338 state->write_through = write_through;
340 subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
341 if (tevent_req_nomem(subreq, req)) {
342 return tevent_req_post(req, ev);
344 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
348 static void pwrite_fsync_write_done(struct tevent_req *subreq)
350 struct tevent_req *req = tevent_req_callback_data(
351 subreq, struct tevent_req);
352 struct pwrite_fsync_state *state = tevent_req_data(
353 req, struct pwrite_fsync_state);
354 connection_struct *conn = state->fsp->conn;
356 struct vfs_aio_state vfs_aio_state;
358 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &vfs_aio_state);
360 if (state->nwritten == -1) {
361 tevent_req_error(req, vfs_aio_state.error);
365 do_sync = (lp_strict_sync(SNUM(conn)) &&
366 (lp_sync_always(SNUM(conn)) || state->write_through));
368 tevent_req_done(req);
372 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
373 if (tevent_req_nomem(subreq, req)) {
376 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
379 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
381 struct tevent_req *req = tevent_req_callback_data(
382 subreq, struct tevent_req);
384 struct vfs_aio_state vfs_aio_state;
386 ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
389 tevent_req_error(req, vfs_aio_state.error);
392 tevent_req_done(req);
395 static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
397 struct pwrite_fsync_state *state = tevent_req_data(
398 req, struct pwrite_fsync_state);
400 if (tevent_req_is_unix_error(req, perr)) {
403 return state->nwritten;
406 static void aio_pwrite_smb1_done(struct tevent_req *req);
408 /****************************************************************************
409 Set up an aio request from a SMBwriteX call.
410 *****************************************************************************/
412 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
413 struct smb_request *smbreq,
414 files_struct *fsp, const char *data,
418 struct aio_extra *aio_ex;
420 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
421 struct tevent_req *req;
423 if (fsp->base_fsp != NULL) {
424 /* No AIO on streams yet */
425 DEBUG(10, ("AIO on streams not yet supported\n"));
426 return NT_STATUS_RETRY;
429 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
430 && !SMB_VFS_AIO_FORCE(fsp)) {
431 /* Too small a write for aio request. */
432 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
433 "small for minimum aio_write of %u\n",
434 (unsigned int)numtowrite,
435 (unsigned int)min_aio_write_size ));
436 return NT_STATUS_RETRY;
439 /* Only do this on non-chained and non-chaining writes */
440 if (req_is_in_chain(smbreq)) {
441 return NT_STATUS_RETRY;
444 bufsize = smb_size + 6*2;
446 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
447 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
448 return NT_STATUS_NO_MEMORY;
450 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
452 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
453 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
454 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
456 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
457 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
460 /* Take the lock until the AIO completes. */
461 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
463 return NT_STATUS_FILE_LOCK_CONFLICT;
466 aio_ex->nbyte = numtowrite;
467 aio_ex->offset = startpos;
469 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
470 data, numtowrite, startpos,
471 aio_ex->write_through);
473 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
474 "Error %s\n", strerror(errno) ));
476 return NT_STATUS_RETRY;
478 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
480 if (!aio_add_req_to_fsp(fsp, req)) {
481 DEBUG(1, ("Could not add req to fsp\n"));
483 return NT_STATUS_RETRY;
486 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
488 /* This should actually be improved to span the write. */
489 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
490 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
492 if (!aio_ex->write_through && !lp_sync_always(SNUM(fsp->conn))
493 && fsp->fsp_flags.aio_write_behind)
495 /* Lie to the client and immediately claim we finished the
497 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
498 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
499 show_msg((char *)aio_ex->outbuf.data);
500 if (!srv_send_smb(aio_ex->smbreq->xconn,
501 (char *)aio_ex->outbuf.data,
502 true, aio_ex->smbreq->seqnum+1,
503 IS_CONN_ENCRYPTED(fsp->conn),
504 &aio_ex->smbreq->pcd)) {
505 exit_server_cleanly("schedule_aio_write_and_X: "
506 "srv_send_smb failed.");
508 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
509 "behind for file %s\n", fsp_str_dbg(fsp)));
512 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
513 "%s, offset %.0f, len = %u (mid = %u)\n",
514 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
515 (unsigned int)aio_ex->smbreq->mid));
520 static void aio_pwrite_smb1_done(struct tevent_req *req)
522 struct aio_extra *aio_ex = tevent_req_callback_data(
523 req, struct aio_extra);
524 files_struct *fsp = aio_ex->fsp;
525 char *outbuf = (char *)aio_ex->outbuf.data;
526 ssize_t numtowrite = aio_ex->nbyte;
530 nwritten = pwrite_fsync_recv(req, &err);
533 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
534 (nwritten == -1) ? strerror(err) : "no error"));
537 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
538 "aio outstanding (mid[%llu]).\n",
539 (unsigned long long)aio_ex->smbreq->mid));
544 mark_file_modified(fsp);
546 if (fsp->fsp_flags.aio_write_behind) {
548 if (nwritten != numtowrite) {
549 if (nwritten == -1) {
550 DEBUG(5,("handle_aio_write_complete: "
551 "aio_write_behind failed ! File %s "
552 "is corrupt ! Error %s\n",
553 fsp_str_dbg(fsp), strerror(err)));
555 DEBUG(0,("handle_aio_write_complete: "
556 "aio_write_behind failed ! File %s "
557 "is corrupt ! Wanted %u bytes but "
558 "only wrote %d\n", fsp_str_dbg(fsp),
559 (unsigned int)numtowrite,
563 DEBUG(10,("handle_aio_write_complete: "
564 "aio_write_behind completed for file %s\n",
567 /* TODO: should no return success in case of an error !!! */
572 /* We don't need outsize or set_message here as we've already set the
573 fixed size length when we set up the aio call. */
575 if (nwritten == -1) {
576 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
577 "nwritten == %d. Error = %s\n",
578 fsp_str_dbg(fsp), (unsigned int)numtowrite,
579 (int)nwritten, strerror(err)));
581 ERROR_NT(map_nt_error_from_unix(err));
582 srv_set_message(outbuf,0,0,true);
584 SSVAL(outbuf,smb_vwv2,nwritten);
585 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
586 if (nwritten < (ssize_t)numtowrite) {
587 SCVAL(outbuf,smb_rcls,ERRHRD);
588 SSVAL(outbuf,smb_err,ERRdiskfull);
591 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
592 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
594 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
598 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
599 true, aio_ex->smbreq->seqnum+1,
600 IS_CONN_ENCRYPTED(fsp->conn),
602 exit_server_cleanly("handle_aio_write_complete: "
603 "srv_send_smb failed.");
606 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
607 "for file %s, offset %.0f, requested %u, written = %u\n",
608 fsp_str_dbg(fsp), (double)aio_ex->offset,
609 (unsigned int)numtowrite, (unsigned int)nwritten));
614 bool cancel_smb2_aio(struct smb_request *smbreq)
616 struct smbd_smb2_request *smb2req = smbreq->smb2req;
617 struct aio_extra *aio_ex = NULL;
620 aio_ex = talloc_get_type(smbreq->async_priv,
624 if (aio_ex == NULL) {
628 if (aio_ex->fsp == NULL) {
633 * We let the aio request run and don't try to cancel it which means
634 * processing of the SMB2 request must continue as normal, cf MS-SMB2
637 * If the target request is not successfully canceled, processing of
638 * the target request MUST continue and no response is sent to the
645 static void aio_pread_smb2_done(struct tevent_req *req);
647 /****************************************************************************
648 Set up an aio request from a SMB2 read call.
649 *****************************************************************************/
651 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
652 struct smb_request *smbreq,
659 struct aio_extra *aio_ex;
660 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
661 struct tevent_req *req;
663 if (fsp->base_fsp != NULL) {
664 /* No AIO on streams yet */
665 DEBUG(10, ("AIO on streams not yet supported\n"));
666 return NT_STATUS_RETRY;
669 if (fsp->op == NULL) {
670 /* No AIO on internal opens. */
671 return NT_STATUS_RETRY;
674 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
675 && !SMB_VFS_AIO_FORCE(fsp)) {
676 /* Too small a read for aio request. */
677 DEBUG(10,("smb2: read size (%u) too small "
678 "for minimum aio_read of %u\n",
679 (unsigned int)smb_maxcnt,
680 (unsigned int)min_aio_read_size ));
681 return NT_STATUS_RETRY;
684 if (smbd_smb2_is_compound(smbreq->smb2req)) {
685 return NT_STATUS_RETRY;
688 /* Create the out buffer. */
689 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
690 if (preadbuf->data == NULL) {
691 return NT_STATUS_NO_MEMORY;
694 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
695 return NT_STATUS_NO_MEMORY;
698 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
699 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
702 /* Take the lock until the AIO completes. */
703 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
705 return NT_STATUS_FILE_LOCK_CONFLICT;
708 aio_ex->nbyte = smb_maxcnt;
709 aio_ex->offset = startpos;
711 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
712 preadbuf->data, smb_maxcnt, startpos);
714 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
715 "Error %s\n", strerror(errno)));
717 return NT_STATUS_RETRY;
719 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
721 if (!aio_add_req_to_fsp(fsp, req)) {
722 DEBUG(1, ("Could not add req to fsp\n"));
724 return NT_STATUS_RETRY;
727 /* We don't need talloc_move here as both aio_ex and
728 * smbreq are children of smbreq->smb2req. */
729 aio_ex->smbreq = smbreq;
730 smbreq->async_priv = aio_ex;
732 DEBUG(10,("smb2: scheduled aio_read for file %s, "
733 "offset %.0f, len = %u (mid = %u)\n",
734 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
735 (unsigned int)aio_ex->smbreq->mid ));
740 static void aio_pread_smb2_done(struct tevent_req *req)
742 struct aio_extra *aio_ex = tevent_req_callback_data(
743 req, struct aio_extra);
744 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
745 files_struct *fsp = aio_ex->fsp;
748 struct vfs_aio_state vfs_aio_state = { 0 };
750 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
753 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
754 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
756 /* Common error or success code processing for async or sync
759 status = smb2_read_complete(subreq, nread, vfs_aio_state.error);
762 fsp->fh->pos = aio_ex->offset + nread;
763 fsp->fh->position_information = fsp->fh->pos;
766 DEBUG(10, ("smb2: scheduled aio_read completed "
767 "for file %s, offset %.0f, len = %u "
768 "(errcode = %d, NTSTATUS = %s)\n",
769 fsp_str_dbg(aio_ex->fsp),
770 (double)aio_ex->offset,
772 vfs_aio_state.error, nt_errstr(status)));
774 if (!NT_STATUS_IS_OK(status)) {
775 tevent_req_nterror(subreq, status);
778 tevent_req_done(subreq);
781 static void aio_pwrite_smb2_done(struct tevent_req *req);
783 /****************************************************************************
784 Set up an aio request from a SMB2write call.
785 *****************************************************************************/
787 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
788 struct smb_request *smbreq,
794 struct aio_extra *aio_ex = NULL;
795 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
796 struct tevent_req *req;
798 if (fsp->base_fsp != NULL) {
799 /* No AIO on streams yet */
800 DEBUG(10, ("AIO on streams not yet supported\n"));
801 return NT_STATUS_RETRY;
804 if (fsp->op == NULL) {
805 /* No AIO on internal opens. */
806 return NT_STATUS_RETRY;
809 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
810 && !SMB_VFS_AIO_FORCE(fsp)) {
811 /* Too small a write for aio request. */
812 DEBUG(10,("smb2: write size (%u) too "
813 "small for minimum aio_write of %u\n",
814 (unsigned int)in_data.length,
815 (unsigned int)min_aio_write_size ));
816 return NT_STATUS_RETRY;
819 if (smbd_smb2_is_compound(smbreq->smb2req)) {
820 return NT_STATUS_RETRY;
823 if (smbreq->unread_bytes) {
824 /* Can't do async with recvfile. */
825 return NT_STATUS_RETRY;
828 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
829 return NT_STATUS_NO_MEMORY;
832 aio_ex->write_through = write_through;
834 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
835 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
838 /* Take the lock until the AIO completes. */
839 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
841 return NT_STATUS_FILE_LOCK_CONFLICT;
844 aio_ex->nbyte = in_data.length;
845 aio_ex->offset = in_offset;
847 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
848 in_data.data, in_data.length, in_offset,
851 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
852 "Error %s\n", strerror(errno)));
854 return NT_STATUS_RETRY;
856 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
858 if (!aio_add_req_to_fsp(fsp, req)) {
859 DEBUG(1, ("Could not add req to fsp\n"));
861 return NT_STATUS_RETRY;
864 /* We don't need talloc_move here as both aio_ex and
865 * smbreq are children of smbreq->smb2req. */
866 aio_ex->smbreq = smbreq;
867 smbreq->async_priv = aio_ex;
869 /* This should actually be improved to span the write. */
870 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
871 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
874 * We don't want to do write behind due to ownership
875 * issues of the request structs. Maybe add it if I
876 * figure those out. JRA.
879 DEBUG(10,("smb2: scheduled aio_write for file "
880 "%s, offset %.0f, len = %u (mid = %u)\n",
883 (unsigned int)in_data.length,
884 (unsigned int)aio_ex->smbreq->mid));
889 static void aio_pwrite_smb2_done(struct tevent_req *req)
891 struct aio_extra *aio_ex = tevent_req_callback_data(
892 req, struct aio_extra);
893 ssize_t numtowrite = aio_ex->nbyte;
894 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
895 files_struct *fsp = aio_ex->fsp;
900 nwritten = pwrite_fsync_recv(req, &err);
903 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
904 (nwritten == -1) ? strerror(err) : "no error"));
906 mark_file_modified(fsp);
908 status = smb2_write_complete_nosync(subreq, nwritten, err);
910 DEBUG(10, ("smb2: scheduled aio_write completed "
911 "for file %s, offset %.0f, requested %u, "
912 "written = %u (errcode = %d, NTSTATUS = %s)\n",
914 (double)aio_ex->offset,
915 (unsigned int)numtowrite,
916 (unsigned int)nwritten,
917 err, nt_errstr(status)));
919 if (!NT_STATUS_IS_OK(status)) {
920 tevent_req_nterror(subreq, status);
923 tevent_req_done(subreq);