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"
26 #include "lib/tevent_wait.h"
28 /****************************************************************************
29 Statics plus accessor functions.
30 *****************************************************************************/
32 static int outstanding_aio_calls;
34 int get_outstanding_aio_calls(void)
36 return outstanding_aio_calls;
39 void increment_outstanding_aio_calls(void)
41 outstanding_aio_calls++;
44 void decrement_outstanding_aio_calls(void)
46 outstanding_aio_calls--;
49 /****************************************************************************
50 The buffer we keep around whilst an aio request is in process.
51 *****************************************************************************/
55 struct smb_request *smbreq;
57 struct lock_struct lock;
63 /****************************************************************************
64 Accessor function to return write_through state.
65 *****************************************************************************/
67 bool aio_write_through_requested(struct aio_extra *aio_ex)
69 return aio_ex->write_through;
72 static int aio_extra_destructor(struct aio_extra *aio_ex)
74 decrement_outstanding_aio_calls();
78 /****************************************************************************
79 Create the extended aio struct we must keep around for the lifetime
81 *****************************************************************************/
83 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
87 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
93 /* The output buffer stored in the aio_ex is the start of
94 the smb return buffer. The buffer used in the acb
95 is the start of the reply data portion of that buffer. */
98 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
99 if (!aio_ex->outbuf.data) {
104 talloc_set_destructor(aio_ex, aio_extra_destructor);
106 increment_outstanding_aio_calls();
110 struct aio_req_fsp_link {
112 struct tevent_req *req;
115 static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
118 files_struct *fsp = lnk->fsp;
119 struct tevent_req *req = lnk->req;
121 for (i=0; i<fsp->num_aio_requests; i++) {
122 if (fsp->aio_requests[i] == req) {
126 if (i == fsp->num_aio_requests) {
127 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
130 fsp->num_aio_requests -= 1;
131 fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
133 if (fsp->num_aio_requests == 0) {
134 tevent_wait_done(fsp->deferred_close);
139 bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
142 struct aio_req_fsp_link *lnk;
144 lnk = talloc(req, struct aio_req_fsp_link);
149 array_len = talloc_array_length(fsp->aio_requests);
150 if (array_len <= fsp->num_aio_requests) {
151 struct tevent_req **tmp;
153 tmp = talloc_realloc(
154 fsp, fsp->aio_requests, struct tevent_req *,
155 fsp->num_aio_requests+1);
160 fsp->aio_requests = tmp;
162 fsp->aio_requests[fsp->num_aio_requests] = req;
163 fsp->num_aio_requests += 1;
167 talloc_set_destructor(lnk, aio_del_req_from_fsp);
172 static void aio_pread_smb1_done(struct tevent_req *req);
174 /****************************************************************************
175 Set up an aio request from a SMBreadX call.
176 *****************************************************************************/
178 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
179 struct smb_request *smbreq,
180 files_struct *fsp, off_t startpos,
183 struct aio_extra *aio_ex;
185 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
186 struct tevent_req *req;
188 if (fsp->base_fsp != NULL) {
189 /* No AIO on streams yet */
190 DEBUG(10, ("AIO on streams not yet supported\n"));
191 return NT_STATUS_RETRY;
194 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
195 && !SMB_VFS_AIO_FORCE(fsp)) {
196 /* Too small a read for aio request. */
197 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
198 "for minimum aio_read of %u\n",
199 (unsigned int)smb_maxcnt,
200 (unsigned int)min_aio_read_size ));
201 return NT_STATUS_RETRY;
204 /* Only do this on non-chained and non-chaining reads not using the
206 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
207 return NT_STATUS_RETRY;
210 /* The following is safe from integer wrap as we've already checked
211 smb_maxcnt is 128k or less. Wct is 12 for read replies */
213 bufsize = smb_size + 12 * 2 + smb_maxcnt + 1 /* padding byte */;
215 if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
216 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
217 return NT_STATUS_NO_MEMORY;
220 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
221 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
222 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
223 SCVAL(smb_buf(aio_ex->outbuf.data), 0, 0); /* padding byte */
225 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
226 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
229 /* Take the lock until the AIO completes. */
230 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
232 return NT_STATUS_FILE_LOCK_CONFLICT;
235 aio_ex->nbyte = smb_maxcnt;
236 aio_ex->offset = startpos;
238 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx,
240 smb_buf(aio_ex->outbuf.data) + 1 /* pad */,
241 smb_maxcnt, startpos);
243 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
244 "Error %s\n", strerror(errno) ));
246 return NT_STATUS_RETRY;
248 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
250 if (!aio_add_req_to_fsp(fsp, req)) {
251 DEBUG(1, ("Could not add req to fsp\n"));
253 return NT_STATUS_RETRY;
256 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
258 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
259 "offset %.0f, len = %u (mid = %u)\n",
260 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
261 (unsigned int)aio_ex->smbreq->mid ));
266 static void aio_pread_smb1_done(struct tevent_req *req)
268 struct aio_extra *aio_ex = tevent_req_callback_data(
269 req, struct aio_extra);
270 files_struct *fsp = aio_ex->fsp;
272 char *outbuf = (char *)aio_ex->outbuf.data;
274 struct vfs_aio_state vfs_aio_state;
276 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
279 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
280 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
283 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
284 "aio outstanding (mid[%llu]).\n",
285 (unsigned long long)aio_ex->smbreq->mid));
291 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
292 "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
293 strerror(vfs_aio_state.error)));
295 ERROR_NT(map_nt_error_from_unix(vfs_aio_state.error));
296 outsize = srv_set_message(outbuf,0,0,true);
298 outsize = setup_readX_header(outbuf, nread);
300 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
301 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
303 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
304 "nread=%d\n", fsp_str_dbg(fsp),
305 (int)aio_ex->nbyte, (int)nread ) );
308 _smb_setlen_large(outbuf, outsize - 4);
310 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
311 true, aio_ex->smbreq->seqnum+1,
312 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
313 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
317 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
318 "for file %s, offset %.0f, len = %u\n",
319 fsp_str_dbg(fsp), (double)aio_ex->offset,
320 (unsigned int)nread));
325 struct pwrite_fsync_state {
326 struct tevent_context *ev;
332 static void pwrite_fsync_write_done(struct tevent_req *subreq);
333 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
335 static struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
336 struct tevent_context *ev,
337 struct files_struct *fsp,
339 size_t n, off_t offset,
342 struct tevent_req *req, *subreq;
343 struct pwrite_fsync_state *state;
345 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
351 state->write_through = write_through;
353 subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
354 if (tevent_req_nomem(subreq, req)) {
355 return tevent_req_post(req, ev);
357 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
361 static void pwrite_fsync_write_done(struct tevent_req *subreq)
363 struct tevent_req *req = tevent_req_callback_data(
364 subreq, struct tevent_req);
365 struct pwrite_fsync_state *state = tevent_req_data(
366 req, struct pwrite_fsync_state);
367 connection_struct *conn = state->fsp->conn;
369 struct vfs_aio_state vfs_aio_state;
371 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &vfs_aio_state);
373 if (state->nwritten == -1) {
374 tevent_req_error(req, vfs_aio_state.error);
378 do_sync = (lp_strict_sync(SNUM(conn)) &&
379 (lp_sync_always(SNUM(conn)) || state->write_through));
381 tevent_req_done(req);
385 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
386 if (tevent_req_nomem(subreq, req)) {
389 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
392 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
394 struct tevent_req *req = tevent_req_callback_data(
395 subreq, struct tevent_req);
397 struct vfs_aio_state vfs_aio_state;
399 ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
402 tevent_req_error(req, vfs_aio_state.error);
405 tevent_req_done(req);
408 static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
410 struct pwrite_fsync_state *state = tevent_req_data(
411 req, struct pwrite_fsync_state);
413 if (tevent_req_is_unix_error(req, perr)) {
416 return state->nwritten;
419 static void aio_pwrite_smb1_done(struct tevent_req *req);
421 /****************************************************************************
422 Set up an aio request from a SMBwriteX call.
423 *****************************************************************************/
425 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
426 struct smb_request *smbreq,
427 files_struct *fsp, const char *data,
431 struct aio_extra *aio_ex;
433 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
434 struct tevent_req *req;
436 if (fsp->base_fsp != NULL) {
437 /* No AIO on streams yet */
438 DEBUG(10, ("AIO on streams not yet supported\n"));
439 return NT_STATUS_RETRY;
442 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
443 && !SMB_VFS_AIO_FORCE(fsp)) {
444 /* Too small a write for aio request. */
445 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
446 "small for minimum aio_write of %u\n",
447 (unsigned int)numtowrite,
448 (unsigned int)min_aio_write_size ));
449 return NT_STATUS_RETRY;
452 /* Only do this on non-chained and non-chaining writes not using the
454 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
455 return NT_STATUS_RETRY;
458 bufsize = smb_size + 6*2;
460 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
461 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
462 return NT_STATUS_NO_MEMORY;
464 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
466 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
467 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
468 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
470 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
471 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
474 /* Take the lock until the AIO completes. */
475 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
477 return NT_STATUS_FILE_LOCK_CONFLICT;
480 aio_ex->nbyte = numtowrite;
481 aio_ex->offset = startpos;
483 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
484 data, numtowrite, startpos,
485 aio_ex->write_through);
487 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
488 "Error %s\n", strerror(errno) ));
490 return NT_STATUS_RETRY;
492 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
494 if (!aio_add_req_to_fsp(fsp, req)) {
495 DEBUG(1, ("Could not add req to fsp\n"));
497 return NT_STATUS_RETRY;
500 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
502 /* This should actually be improved to span the write. */
503 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
504 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
506 if (!aio_ex->write_through && !lp_sync_always(SNUM(fsp->conn))
507 && fsp->aio_write_behind) {
508 /* Lie to the client and immediately claim we finished the
510 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
511 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
512 show_msg((char *)aio_ex->outbuf.data);
513 if (!srv_send_smb(aio_ex->smbreq->xconn,
514 (char *)aio_ex->outbuf.data,
515 true, aio_ex->smbreq->seqnum+1,
516 IS_CONN_ENCRYPTED(fsp->conn),
517 &aio_ex->smbreq->pcd)) {
518 exit_server_cleanly("schedule_aio_write_and_X: "
519 "srv_send_smb failed.");
521 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
522 "behind for file %s\n", fsp_str_dbg(fsp)));
525 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
526 "%s, offset %.0f, len = %u (mid = %u) "
527 "outstanding_aio_calls = %d\n",
528 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
529 (unsigned int)aio_ex->smbreq->mid,
530 get_outstanding_aio_calls() ));
535 static void aio_pwrite_smb1_done(struct tevent_req *req)
537 struct aio_extra *aio_ex = tevent_req_callback_data(
538 req, struct aio_extra);
539 files_struct *fsp = aio_ex->fsp;
540 char *outbuf = (char *)aio_ex->outbuf.data;
541 ssize_t numtowrite = aio_ex->nbyte;
545 nwritten = pwrite_fsync_recv(req, &err);
548 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
549 (nwritten == -1) ? strerror(err) : "no error"));
552 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
553 "aio outstanding (mid[%llu]).\n",
554 (unsigned long long)aio_ex->smbreq->mid));
559 mark_file_modified(fsp);
561 if (fsp->aio_write_behind) {
563 if (nwritten != numtowrite) {
564 if (nwritten == -1) {
565 DEBUG(5,("handle_aio_write_complete: "
566 "aio_write_behind failed ! File %s "
567 "is corrupt ! Error %s\n",
568 fsp_str_dbg(fsp), strerror(err)));
570 DEBUG(0,("handle_aio_write_complete: "
571 "aio_write_behind failed ! File %s "
572 "is corrupt ! Wanted %u bytes but "
573 "only wrote %d\n", fsp_str_dbg(fsp),
574 (unsigned int)numtowrite,
578 DEBUG(10,("handle_aio_write_complete: "
579 "aio_write_behind completed for file %s\n",
582 /* TODO: should no return success in case of an error !!! */
587 /* We don't need outsize or set_message here as we've already set the
588 fixed size length when we set up the aio call. */
590 if (nwritten == -1) {
591 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
592 "nwritten == %d. Error = %s\n",
593 fsp_str_dbg(fsp), (unsigned int)numtowrite,
594 (int)nwritten, strerror(err)));
596 ERROR_NT(map_nt_error_from_unix(err));
597 srv_set_message(outbuf,0,0,true);
599 SSVAL(outbuf,smb_vwv2,nwritten);
600 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
601 if (nwritten < (ssize_t)numtowrite) {
602 SCVAL(outbuf,smb_rcls,ERRHRD);
603 SSVAL(outbuf,smb_err,ERRdiskfull);
606 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
607 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
609 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
613 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
614 true, aio_ex->smbreq->seqnum+1,
615 IS_CONN_ENCRYPTED(fsp->conn),
617 exit_server_cleanly("handle_aio_write_complete: "
618 "srv_send_smb failed.");
621 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
622 "for file %s, offset %.0f, requested %u, written = %u\n",
623 fsp_str_dbg(fsp), (double)aio_ex->offset,
624 (unsigned int)numtowrite, (unsigned int)nwritten));
629 bool cancel_smb2_aio(struct smb_request *smbreq)
631 struct smbd_smb2_request *smb2req = smbreq->smb2req;
632 struct aio_extra *aio_ex = NULL;
635 aio_ex = talloc_get_type(smbreq->async_priv,
639 if (aio_ex == NULL) {
643 if (aio_ex->fsp == NULL) {
648 * We let the aio request run. Setting fsp to NULL has the
649 * effect that the _done routines don't send anything out.
656 static void aio_pread_smb2_done(struct tevent_req *req);
658 /****************************************************************************
659 Set up an aio request from a SMB2 read call.
660 *****************************************************************************/
662 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
663 struct smb_request *smbreq,
670 struct aio_extra *aio_ex;
671 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
672 struct tevent_req *req;
674 if (fsp->base_fsp != NULL) {
675 /* No AIO on streams yet */
676 DEBUG(10, ("AIO on streams not yet supported\n"));
677 return NT_STATUS_RETRY;
680 if (fsp->op == NULL) {
681 /* No AIO on internal opens. */
682 return NT_STATUS_RETRY;
685 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
686 && !SMB_VFS_AIO_FORCE(fsp)) {
687 /* Too small a read for aio request. */
688 DEBUG(10,("smb2: read size (%u) too small "
689 "for minimum aio_read of %u\n",
690 (unsigned int)smb_maxcnt,
691 (unsigned int)min_aio_read_size ));
692 return NT_STATUS_RETRY;
695 /* Only do this on reads not using the write cache. */
696 if (lp_write_cache_size(SNUM(conn)) != 0) {
697 return NT_STATUS_RETRY;
700 if (smbd_smb2_is_compound(smbreq->smb2req)) {
701 return NT_STATUS_RETRY;
704 /* Create the out buffer. */
705 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
706 if (preadbuf->data == NULL) {
707 return NT_STATUS_NO_MEMORY;
710 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
711 return NT_STATUS_NO_MEMORY;
714 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
715 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
718 /* Take the lock until the AIO completes. */
719 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
721 return NT_STATUS_FILE_LOCK_CONFLICT;
724 aio_ex->nbyte = smb_maxcnt;
725 aio_ex->offset = startpos;
727 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
728 preadbuf->data, smb_maxcnt, startpos);
730 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
731 "Error %s\n", strerror(errno)));
733 return NT_STATUS_RETRY;
735 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
737 if (!aio_add_req_to_fsp(fsp, req)) {
738 DEBUG(1, ("Could not add req to fsp\n"));
740 return NT_STATUS_RETRY;
743 /* We don't need talloc_move here as both aio_ex and
744 * smbreq are children of smbreq->smb2req. */
745 aio_ex->smbreq = smbreq;
746 smbreq->async_priv = aio_ex;
748 DEBUG(10,("smb2: scheduled aio_read for file %s, "
749 "offset %.0f, len = %u (mid = %u)\n",
750 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
751 (unsigned int)aio_ex->smbreq->mid ));
756 static void aio_pread_smb2_done(struct tevent_req *req)
758 struct aio_extra *aio_ex = tevent_req_callback_data(
759 req, struct aio_extra);
760 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
761 files_struct *fsp = aio_ex->fsp;
764 struct vfs_aio_state vfs_aio_state = { 0 };
766 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
769 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
770 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
773 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
774 __func__, (uintmax_t)aio_ex->smbreq->mid));
776 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
780 /* Common error or success code processing for async or sync
783 status = smb2_read_complete(subreq, nread, vfs_aio_state.error);
786 fsp->fh->pos = aio_ex->offset + nread;
787 fsp->fh->position_information = fsp->fh->pos;
790 DEBUG(10, ("smb2: scheduled aio_read completed "
791 "for file %s, offset %.0f, len = %u "
792 "(errcode = %d, NTSTATUS = %s)\n",
793 fsp_str_dbg(aio_ex->fsp),
794 (double)aio_ex->offset,
796 vfs_aio_state.error, nt_errstr(status)));
798 if (!NT_STATUS_IS_OK(status)) {
799 tevent_req_nterror(subreq, status);
802 tevent_req_done(subreq);
805 static void aio_pwrite_smb2_done(struct tevent_req *req);
807 /****************************************************************************
808 Set up an aio request from a SMB2write call.
809 *****************************************************************************/
811 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
812 struct smb_request *smbreq,
818 struct aio_extra *aio_ex = NULL;
819 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
820 struct tevent_req *req;
822 if (fsp->base_fsp != NULL) {
823 /* No AIO on streams yet */
824 DEBUG(10, ("AIO on streams not yet supported\n"));
825 return NT_STATUS_RETRY;
828 if (fsp->op == NULL) {
829 /* No AIO on internal opens. */
830 return NT_STATUS_RETRY;
833 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
834 && !SMB_VFS_AIO_FORCE(fsp)) {
835 /* Too small a write for aio request. */
836 DEBUG(10,("smb2: write size (%u) too "
837 "small for minimum aio_write of %u\n",
838 (unsigned int)in_data.length,
839 (unsigned int)min_aio_write_size ));
840 return NT_STATUS_RETRY;
843 /* Only do this on writes not using the write cache. */
844 if (lp_write_cache_size(SNUM(conn)) != 0) {
845 return NT_STATUS_RETRY;
848 if (smbd_smb2_is_compound(smbreq->smb2req)) {
849 return NT_STATUS_RETRY;
852 if (smbreq->unread_bytes) {
853 /* Can't do async with recvfile. */
854 return NT_STATUS_RETRY;
857 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
858 return NT_STATUS_NO_MEMORY;
861 aio_ex->write_through = write_through;
863 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
864 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
867 /* Take the lock until the AIO completes. */
868 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
870 return NT_STATUS_FILE_LOCK_CONFLICT;
873 aio_ex->nbyte = in_data.length;
874 aio_ex->offset = in_offset;
876 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
877 in_data.data, in_data.length, in_offset,
880 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
881 "Error %s\n", strerror(errno)));
883 return NT_STATUS_RETRY;
885 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
887 if (!aio_add_req_to_fsp(fsp, req)) {
888 DEBUG(1, ("Could not add req to fsp\n"));
890 return NT_STATUS_RETRY;
893 /* We don't need talloc_move here as both aio_ex and
894 * smbreq are children of smbreq->smb2req. */
895 aio_ex->smbreq = smbreq;
896 smbreq->async_priv = aio_ex;
898 /* This should actually be improved to span the write. */
899 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
900 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
903 * We don't want to do write behind due to ownership
904 * issues of the request structs. Maybe add it if I
905 * figure those out. JRA.
908 DEBUG(10,("smb2: scheduled aio_write for file "
909 "%s, offset %.0f, len = %u (mid = %u) "
910 "outstanding_aio_calls = %d\n",
913 (unsigned int)in_data.length,
914 (unsigned int)aio_ex->smbreq->mid,
915 get_outstanding_aio_calls() ));
920 static void aio_pwrite_smb2_done(struct tevent_req *req)
922 struct aio_extra *aio_ex = tevent_req_callback_data(
923 req, struct aio_extra);
924 ssize_t numtowrite = aio_ex->nbyte;
925 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
926 files_struct *fsp = aio_ex->fsp;
931 nwritten = pwrite_fsync_recv(req, &err);
934 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
935 (nwritten == -1) ? strerror(err) : "no error"));
938 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
939 __func__, (uintmax_t)aio_ex->smbreq->mid));
941 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
945 mark_file_modified(fsp);
947 status = smb2_write_complete_nosync(subreq, nwritten, err);
949 DEBUG(10, ("smb2: scheduled aio_write completed "
950 "for file %s, offset %.0f, requested %u, "
951 "written = %u (errcode = %d, NTSTATUS = %s)\n",
953 (double)aio_ex->offset,
954 (unsigned int)numtowrite,
955 (unsigned int)nwritten,
956 err, nt_errstr(status)));
958 if (!NT_STATUS_IS_OK(status)) {
959 tevent_req_nterror(subreq, status);
962 tevent_req_done(subreq);