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 aio_pending_size = 100; /* tevent supports 100 signals SA_SIGINFO */
34 int get_aio_pending_size(void)
36 return aio_pending_size;
39 void set_aio_pending_size(int newsize)
41 aio_pending_size = newsize;
44 /****************************************************************************
45 The buffer we keep around whilst an aio request is in process.
46 *****************************************************************************/
50 struct smb_request *smbreq;
52 struct lock_struct lock;
58 /****************************************************************************
59 Accessor function to return write_through state.
60 *****************************************************************************/
62 bool aio_write_through_requested(struct aio_extra *aio_ex)
64 return aio_ex->write_through;
67 static int aio_extra_destructor(struct aio_extra *aio_ex)
69 outstanding_aio_calls--;
73 /****************************************************************************
74 Create the extended aio struct we must keep around for the lifetime
76 *****************************************************************************/
78 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
82 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
88 /* The output buffer stored in the aio_ex is the start of
89 the smb return buffer. The buffer used in the acb
90 is the start of the reply data portion of that buffer. */
93 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
94 if (!aio_ex->outbuf.data) {
99 talloc_set_destructor(aio_ex, aio_extra_destructor);
101 outstanding_aio_calls++;
105 struct aio_req_fsp_link {
107 struct tevent_req *req;
110 static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
113 files_struct *fsp = lnk->fsp;
114 struct tevent_req *req = lnk->req;
116 for (i=0; i<fsp->num_aio_requests; i++) {
117 if (fsp->aio_requests[i] == req) {
121 if (i == fsp->num_aio_requests) {
122 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
125 fsp->num_aio_requests -= 1;
126 fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
128 if (fsp->num_aio_requests == 0) {
129 tevent_wait_done(fsp->deferred_close);
134 bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
137 struct aio_req_fsp_link *lnk;
139 lnk = talloc(req, struct aio_req_fsp_link);
144 array_len = talloc_array_length(fsp->aio_requests);
145 if (array_len <= fsp->num_aio_requests) {
146 struct tevent_req **tmp;
148 tmp = talloc_realloc(
149 fsp, fsp->aio_requests, struct tevent_req *,
150 fsp->num_aio_requests+1);
155 fsp->aio_requests = tmp;
157 fsp->aio_requests[fsp->num_aio_requests] = req;
158 fsp->num_aio_requests += 1;
162 talloc_set_destructor(lnk, aio_del_req_from_fsp);
167 static void aio_pread_smb1_done(struct tevent_req *req);
169 /****************************************************************************
170 Set up an aio request from a SMBreadX call.
171 *****************************************************************************/
173 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
174 struct smb_request *smbreq,
175 files_struct *fsp, off_t startpos,
178 struct aio_extra *aio_ex;
180 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
181 struct tevent_req *req;
183 if (fsp->base_fsp != NULL) {
184 /* No AIO on streams yet */
185 DEBUG(10, ("AIO on streams not yet supported\n"));
186 return NT_STATUS_RETRY;
189 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
190 && !SMB_VFS_AIO_FORCE(fsp)) {
191 /* Too small a read for aio request. */
192 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
193 "for minimum aio_read of %u\n",
194 (unsigned int)smb_maxcnt,
195 (unsigned int)min_aio_read_size ));
196 return NT_STATUS_RETRY;
199 /* Only do this on non-chained and non-chaining reads not using the
201 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
202 return NT_STATUS_RETRY;
205 if (outstanding_aio_calls >= get_aio_pending_size()) {
206 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
207 "activities outstanding.\n",
208 outstanding_aio_calls ));
209 return NT_STATUS_RETRY;
212 /* The following is safe from integer wrap as we've already checked
213 smb_maxcnt is 128k or less. Wct is 12 for read replies */
215 bufsize = smb_size + 12 * 2 + smb_maxcnt + 1 /* padding byte */;
217 if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
218 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
219 return NT_STATUS_NO_MEMORY;
222 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
223 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
224 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
225 SCVAL(smb_buf(aio_ex->outbuf.data), 0, 0); /* padding byte */
227 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
228 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
231 /* Take the lock until the AIO completes. */
232 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
234 return NT_STATUS_FILE_LOCK_CONFLICT;
237 aio_ex->nbyte = smb_maxcnt;
238 aio_ex->offset = startpos;
240 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx,
242 smb_buf(aio_ex->outbuf.data) + 1 /* pad */,
243 smb_maxcnt, startpos);
245 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
246 "Error %s\n", strerror(errno) ));
247 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
249 return NT_STATUS_RETRY;
251 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
253 if (!aio_add_req_to_fsp(fsp, req)) {
254 DEBUG(1, ("Could not add req to fsp\n"));
255 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
257 return NT_STATUS_RETRY;
260 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
262 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
263 "offset %.0f, len = %u (mid = %u)\n",
264 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
265 (unsigned int)aio_ex->smbreq->mid ));
270 static void aio_pread_smb1_done(struct tevent_req *req)
272 struct aio_extra *aio_ex = tevent_req_callback_data(
273 req, struct aio_extra);
274 files_struct *fsp = aio_ex->fsp;
276 char *outbuf = (char *)aio_ex->outbuf.data;
277 char *data = smb_buf(outbuf) + 1 /* padding byte */;
281 nread = SMB_VFS_PREAD_RECV(req, &err);
284 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
285 (nread == -1) ? strerror(err) : "no error"));
288 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
289 "aio outstanding (mid[%llu]).\n",
290 (unsigned long long)aio_ex->smbreq->mid));
295 /* Unlock now we're done. */
296 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
299 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
300 "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
303 ERROR_NT(map_nt_error_from_unix(err));
304 outsize = srv_set_message(outbuf,0,0,true);
306 outsize = srv_set_message(outbuf, 12,
307 nread + 1 /* padding byte */, false);
308 SSVAL(outbuf,smb_vwv2, 0xFFFF); /* Remaining - must be * -1. */
309 SSVAL(outbuf,smb_vwv5, nread);
310 SSVAL(outbuf,smb_vwv6, smb_offset(data,outbuf));
311 SSVAL(outbuf,smb_vwv7, ((nread >> 16) & 1));
312 SSVAL(smb_buf(outbuf), -2, nread);
314 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
315 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
317 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
318 "nread=%d\n", fsp_str_dbg(fsp),
319 (int)aio_ex->nbyte, (int)nread ) );
322 smb_setlen(outbuf, outsize - 4);
324 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
325 true, aio_ex->smbreq->seqnum+1,
326 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
327 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
331 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
332 "for file %s, offset %.0f, len = %u\n",
333 fsp_str_dbg(fsp), (double)aio_ex->offset,
334 (unsigned int)nread));
339 struct pwrite_fsync_state {
340 struct tevent_context *ev;
346 static void pwrite_fsync_write_done(struct tevent_req *subreq);
347 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
349 static struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
350 struct tevent_context *ev,
351 struct files_struct *fsp,
353 size_t n, off_t offset,
356 struct tevent_req *req, *subreq;
357 struct pwrite_fsync_state *state;
359 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
365 state->write_through = write_through;
367 subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
368 if (tevent_req_nomem(subreq, req)) {
369 return tevent_req_post(req, ev);
371 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
375 static void pwrite_fsync_write_done(struct tevent_req *subreq)
377 struct tevent_req *req = tevent_req_callback_data(
378 subreq, struct tevent_req);
379 struct pwrite_fsync_state *state = tevent_req_data(
380 req, struct pwrite_fsync_state);
381 connection_struct *conn = state->fsp->conn;
385 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &err);
387 if (state->nwritten == -1) {
388 tevent_req_error(req, err);
392 do_sync = (lp_strict_sync(SNUM(conn)) &&
393 (lp_sync_always(SNUM(conn)) || state->write_through));
395 tevent_req_done(req);
399 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
400 if (tevent_req_nomem(subreq, req)) {
403 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
406 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
408 struct tevent_req *req = tevent_req_callback_data(
409 subreq, struct tevent_req);
412 ret = SMB_VFS_FSYNC_RECV(subreq, &err);
415 tevent_req_error(req, err);
418 tevent_req_done(req);
421 static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
423 struct pwrite_fsync_state *state = tevent_req_data(
424 req, struct pwrite_fsync_state);
426 if (tevent_req_is_unix_error(req, perr)) {
429 return state->nwritten;
432 static void aio_pwrite_smb1_done(struct tevent_req *req);
434 /****************************************************************************
435 Set up an aio request from a SMBwriteX call.
436 *****************************************************************************/
438 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
439 struct smb_request *smbreq,
440 files_struct *fsp, const char *data,
444 struct aio_extra *aio_ex;
446 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
447 struct tevent_req *req;
449 if (fsp->base_fsp != NULL) {
450 /* No AIO on streams yet */
451 DEBUG(10, ("AIO on streams not yet supported\n"));
452 return NT_STATUS_RETRY;
455 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
456 && !SMB_VFS_AIO_FORCE(fsp)) {
457 /* Too small a write for aio request. */
458 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
459 "small for minimum aio_write of %u\n",
460 (unsigned int)numtowrite,
461 (unsigned int)min_aio_write_size ));
462 return NT_STATUS_RETRY;
465 /* Only do this on non-chained and non-chaining writes not using the
467 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
468 return NT_STATUS_RETRY;
471 if (outstanding_aio_calls >= get_aio_pending_size()) {
472 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
473 "activities outstanding.\n",
474 outstanding_aio_calls ));
475 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
476 "aio_write for file %s, offset %.0f, len = %u "
478 fsp_str_dbg(fsp), (double)startpos,
479 (unsigned int)numtowrite,
480 (unsigned int)smbreq->mid ));
481 return NT_STATUS_RETRY;
484 bufsize = smb_size + 6*2;
486 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
487 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
488 return NT_STATUS_NO_MEMORY;
490 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
492 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
493 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
494 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
496 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
497 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
500 /* Take the lock until the AIO completes. */
501 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
503 return NT_STATUS_FILE_LOCK_CONFLICT;
506 aio_ex->nbyte = numtowrite;
507 aio_ex->offset = startpos;
509 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
510 data, numtowrite, startpos,
511 aio_ex->write_through);
513 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
514 "Error %s\n", strerror(errno) ));
515 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
517 return NT_STATUS_RETRY;
519 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
521 if (!aio_add_req_to_fsp(fsp, req)) {
522 DEBUG(1, ("Could not add req to fsp\n"));
523 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
525 return NT_STATUS_RETRY;
528 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
530 /* This should actually be improved to span the write. */
531 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
532 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
534 if (!aio_ex->write_through && !lp_sync_always(SNUM(fsp->conn))
535 && fsp->aio_write_behind) {
536 /* Lie to the client and immediately claim we finished the
538 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
539 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
540 show_msg((char *)aio_ex->outbuf.data);
541 if (!srv_send_smb(aio_ex->smbreq->xconn,
542 (char *)aio_ex->outbuf.data,
543 true, aio_ex->smbreq->seqnum+1,
544 IS_CONN_ENCRYPTED(fsp->conn),
545 &aio_ex->smbreq->pcd)) {
546 exit_server_cleanly("schedule_aio_write_and_X: "
547 "srv_send_smb failed.");
549 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
550 "behind for file %s\n", fsp_str_dbg(fsp)));
553 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
554 "%s, offset %.0f, len = %u (mid = %u) "
555 "outstanding_aio_calls = %d\n",
556 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
557 (unsigned int)aio_ex->smbreq->mid, outstanding_aio_calls ));
562 static void aio_pwrite_smb1_done(struct tevent_req *req)
564 struct aio_extra *aio_ex = tevent_req_callback_data(
565 req, struct aio_extra);
566 files_struct *fsp = aio_ex->fsp;
567 char *outbuf = (char *)aio_ex->outbuf.data;
568 ssize_t numtowrite = aio_ex->nbyte;
572 nwritten = pwrite_fsync_recv(req, &err);
575 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
576 (nwritten == -1) ? strerror(err) : "no error"));
579 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
580 "aio outstanding (mid[%llu]).\n",
581 (unsigned long long)aio_ex->smbreq->mid));
586 /* Unlock now we're done. */
587 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
589 mark_file_modified(fsp);
591 if (fsp->aio_write_behind) {
593 if (nwritten != numtowrite) {
594 if (nwritten == -1) {
595 DEBUG(5,("handle_aio_write_complete: "
596 "aio_write_behind failed ! File %s "
597 "is corrupt ! Error %s\n",
598 fsp_str_dbg(fsp), strerror(err)));
600 DEBUG(0,("handle_aio_write_complete: "
601 "aio_write_behind failed ! File %s "
602 "is corrupt ! Wanted %u bytes but "
603 "only wrote %d\n", fsp_str_dbg(fsp),
604 (unsigned int)numtowrite,
608 DEBUG(10,("handle_aio_write_complete: "
609 "aio_write_behind completed for file %s\n",
612 /* TODO: should no return success in case of an error !!! */
617 /* We don't need outsize or set_message here as we've already set the
618 fixed size length when we set up the aio call. */
620 if (nwritten == -1) {
621 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
622 "nwritten == %d. Error = %s\n",
623 fsp_str_dbg(fsp), (unsigned int)numtowrite,
624 (int)nwritten, strerror(err)));
626 ERROR_NT(map_nt_error_from_unix(err));
627 srv_set_message(outbuf,0,0,true);
629 SSVAL(outbuf,smb_vwv2,nwritten);
630 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
631 if (nwritten < (ssize_t)numtowrite) {
632 SCVAL(outbuf,smb_rcls,ERRHRD);
633 SSVAL(outbuf,smb_err,ERRdiskfull);
636 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
637 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
639 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
643 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
644 true, aio_ex->smbreq->seqnum+1,
645 IS_CONN_ENCRYPTED(fsp->conn),
647 exit_server_cleanly("handle_aio_write_complete: "
648 "srv_send_smb failed.");
651 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
652 "for file %s, offset %.0f, requested %u, written = %u\n",
653 fsp_str_dbg(fsp), (double)aio_ex->offset,
654 (unsigned int)numtowrite, (unsigned int)nwritten));
659 bool cancel_smb2_aio(struct smb_request *smbreq)
661 struct smbd_smb2_request *smb2req = smbreq->smb2req;
662 struct aio_extra *aio_ex = NULL;
665 aio_ex = talloc_get_type(smbreq->async_priv,
669 if (aio_ex == NULL) {
673 if (aio_ex->fsp == NULL) {
678 * We let the aio request run. Setting fsp to NULL has the
679 * effect that the _done routines don't send anything out.
686 static void aio_pread_smb2_done(struct tevent_req *req);
688 /****************************************************************************
689 Set up an aio request from a SMB2 read call.
690 *****************************************************************************/
692 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
693 struct smb_request *smbreq,
700 struct aio_extra *aio_ex;
701 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
702 struct tevent_req *req;
704 if (fsp->base_fsp != NULL) {
705 /* No AIO on streams yet */
706 DEBUG(10, ("AIO on streams not yet supported\n"));
707 return NT_STATUS_RETRY;
710 if (fsp->op == NULL) {
711 /* No AIO on internal opens. */
712 return NT_STATUS_RETRY;
715 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
716 && !SMB_VFS_AIO_FORCE(fsp)) {
717 /* Too small a read for aio request. */
718 DEBUG(10,("smb2: read size (%u) too small "
719 "for minimum aio_read of %u\n",
720 (unsigned int)smb_maxcnt,
721 (unsigned int)min_aio_read_size ));
722 return NT_STATUS_RETRY;
725 /* Only do this on reads not using the write cache. */
726 if (lp_write_cache_size(SNUM(conn)) != 0) {
727 return NT_STATUS_RETRY;
730 if (outstanding_aio_calls >= get_aio_pending_size()) {
731 DEBUG(10,("smb2: Already have %d aio "
732 "activities outstanding.\n",
733 outstanding_aio_calls ));
734 return NT_STATUS_RETRY;
737 /* Create the out buffer. */
738 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
739 if (preadbuf->data == NULL) {
740 return NT_STATUS_NO_MEMORY;
743 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
744 return NT_STATUS_NO_MEMORY;
747 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
748 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
751 /* Take the lock until the AIO completes. */
752 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
754 return NT_STATUS_FILE_LOCK_CONFLICT;
757 aio_ex->nbyte = smb_maxcnt;
758 aio_ex->offset = startpos;
760 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
761 preadbuf->data, smb_maxcnt, startpos);
763 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
764 "Error %s\n", strerror(errno)));
765 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
767 return NT_STATUS_RETRY;
769 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
771 if (!aio_add_req_to_fsp(fsp, req)) {
772 DEBUG(1, ("Could not add req to fsp\n"));
773 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
775 return NT_STATUS_RETRY;
778 /* We don't need talloc_move here as both aio_ex and
779 * smbreq are children of smbreq->smb2req. */
780 aio_ex->smbreq = smbreq;
781 smbreq->async_priv = aio_ex;
783 DEBUG(10,("smb2: scheduled aio_read for file %s, "
784 "offset %.0f, len = %u (mid = %u)\n",
785 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
786 (unsigned int)aio_ex->smbreq->mid ));
791 static void aio_pread_smb2_done(struct tevent_req *req)
793 struct aio_extra *aio_ex = tevent_req_callback_data(
794 req, struct aio_extra);
795 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
796 files_struct *fsp = aio_ex->fsp;
801 nread = SMB_VFS_PREAD_RECV(req, &err);
804 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
805 (nread == -1) ? strerror(err) : "no error"));
808 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
809 __func__, (uintmax_t)aio_ex->smbreq->mid));
811 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
815 /* Unlock now we're done. */
816 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
818 /* Common error or success code processing for async or sync
821 status = smb2_read_complete(subreq, nread, err);
824 fsp->fh->pos = aio_ex->offset + nread;
825 fsp->fh->position_information = fsp->fh->pos;
828 DEBUG(10, ("smb2: scheduled aio_read completed "
829 "for file %s, offset %.0f, len = %u "
830 "(errcode = %d, NTSTATUS = %s)\n",
831 fsp_str_dbg(aio_ex->fsp),
832 (double)aio_ex->offset,
834 err, nt_errstr(status)));
836 if (!NT_STATUS_IS_OK(status)) {
837 tevent_req_nterror(subreq, status);
840 tevent_req_done(subreq);
843 static void aio_pwrite_smb2_done(struct tevent_req *req);
845 /****************************************************************************
846 Set up an aio request from a SMB2write call.
847 *****************************************************************************/
849 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
850 struct smb_request *smbreq,
856 struct aio_extra *aio_ex = NULL;
857 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
858 struct tevent_req *req;
860 if (fsp->base_fsp != NULL) {
861 /* No AIO on streams yet */
862 DEBUG(10, ("AIO on streams not yet supported\n"));
863 return NT_STATUS_RETRY;
866 if (fsp->op == NULL) {
867 /* No AIO on internal opens. */
868 return NT_STATUS_RETRY;
871 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
872 && !SMB_VFS_AIO_FORCE(fsp)) {
873 /* Too small a write for aio request. */
874 DEBUG(10,("smb2: write size (%u) too "
875 "small for minimum aio_write of %u\n",
876 (unsigned int)in_data.length,
877 (unsigned int)min_aio_write_size ));
878 return NT_STATUS_RETRY;
881 /* Only do this on writes not using the write cache. */
882 if (lp_write_cache_size(SNUM(conn)) != 0) {
883 return NT_STATUS_RETRY;
886 if (outstanding_aio_calls >= get_aio_pending_size()) {
887 DEBUG(3,("smb2: Already have %d aio "
888 "activities outstanding.\n",
889 outstanding_aio_calls ));
890 return NT_STATUS_RETRY;
893 if (smbreq->unread_bytes) {
894 /* Can't do async with recvfile. */
895 return NT_STATUS_RETRY;
898 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
899 return NT_STATUS_NO_MEMORY;
902 aio_ex->write_through = write_through;
904 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
905 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
908 /* Take the lock until the AIO completes. */
909 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
911 return NT_STATUS_FILE_LOCK_CONFLICT;
914 aio_ex->nbyte = in_data.length;
915 aio_ex->offset = in_offset;
917 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
918 in_data.data, in_data.length, in_offset,
921 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
922 "Error %s\n", strerror(errno)));
923 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
925 return NT_STATUS_RETRY;
927 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
929 if (!aio_add_req_to_fsp(fsp, req)) {
930 DEBUG(1, ("Could not add req to fsp\n"));
931 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
933 return NT_STATUS_RETRY;
936 /* We don't need talloc_move here as both aio_ex and
937 * smbreq are children of smbreq->smb2req. */
938 aio_ex->smbreq = smbreq;
939 smbreq->async_priv = aio_ex;
941 /* This should actually be improved to span the write. */
942 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
943 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
946 * We don't want to do write behind due to ownership
947 * issues of the request structs. Maybe add it if I
948 * figure those out. JRA.
951 DEBUG(10,("smb2: scheduled aio_write for file "
952 "%s, offset %.0f, len = %u (mid = %u) "
953 "outstanding_aio_calls = %d\n",
956 (unsigned int)in_data.length,
957 (unsigned int)aio_ex->smbreq->mid,
958 outstanding_aio_calls ));
963 static void aio_pwrite_smb2_done(struct tevent_req *req)
965 struct aio_extra *aio_ex = tevent_req_callback_data(
966 req, struct aio_extra);
967 ssize_t numtowrite = aio_ex->nbyte;
968 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
969 files_struct *fsp = aio_ex->fsp;
974 nwritten = pwrite_fsync_recv(req, &err);
977 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
978 (nwritten == -1) ? strerror(err) : "no error"));
981 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
982 __func__, (uintmax_t)aio_ex->smbreq->mid));
984 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
988 /* Unlock now we're done. */
989 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
991 mark_file_modified(fsp);
993 status = smb2_write_complete_nosync(subreq, nwritten, err);
995 DEBUG(10, ("smb2: scheduled aio_write completed "
996 "for file %s, offset %.0f, requested %u, "
997 "written = %u (errcode = %d, NTSTATUS = %s)\n",
999 (double)aio_ex->offset,
1000 (unsigned int)numtowrite,
1001 (unsigned int)nwritten,
1002 err, nt_errstr(status)));
1004 if (!NT_STATUS_IS_OK(status)) {
1005 tevent_req_nterror(subreq, status);
1008 tevent_req_done(subreq);