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(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) ));
245 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
247 return NT_STATUS_RETRY;
249 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
251 if (!aio_add_req_to_fsp(fsp, req)) {
252 DEBUG(1, ("Could not add req to fsp\n"));
253 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
255 return NT_STATUS_RETRY;
258 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
260 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
261 "offset %.0f, len = %u (mid = %u)\n",
262 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
263 (unsigned int)aio_ex->smbreq->mid ));
268 static void aio_pread_smb1_done(struct tevent_req *req)
270 struct aio_extra *aio_ex = tevent_req_callback_data(
271 req, struct aio_extra);
272 files_struct *fsp = aio_ex->fsp;
274 char *outbuf = (char *)aio_ex->outbuf.data;
275 char *data = smb_buf(outbuf) + 1 /* padding byte */;
277 struct vfs_aio_state vfs_aio_state;
279 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
282 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
283 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
286 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
287 "aio outstanding (mid[%llu]).\n",
288 (unsigned long long)aio_ex->smbreq->mid));
293 /* Unlock now we're done. */
294 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
297 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
298 "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
299 strerror(vfs_aio_state.error)));
301 ERROR_NT(map_nt_error_from_unix(vfs_aio_state.error));
302 outsize = srv_set_message(outbuf,0,0,true);
304 outsize = srv_set_message(outbuf, 12,
305 nread + 1 /* padding byte */, false);
306 SSVAL(outbuf,smb_vwv2, 0xFFFF); /* Remaining - must be * -1. */
307 SSVAL(outbuf,smb_vwv5, nread);
308 SSVAL(outbuf,smb_vwv6, smb_offset(data,outbuf));
309 SSVAL(outbuf,smb_vwv7, ((nread >> 16) & 1));
310 SSVAL(smb_buf(outbuf), -2, nread);
312 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
313 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
315 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
316 "nread=%d\n", fsp_str_dbg(fsp),
317 (int)aio_ex->nbyte, (int)nread ) );
320 smb_setlen(outbuf, outsize - 4);
322 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
323 true, aio_ex->smbreq->seqnum+1,
324 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
325 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
329 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
330 "for file %s, offset %.0f, len = %u\n",
331 fsp_str_dbg(fsp), (double)aio_ex->offset,
332 (unsigned int)nread));
337 struct pwrite_fsync_state {
338 struct tevent_context *ev;
344 static void pwrite_fsync_write_done(struct tevent_req *subreq);
345 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
347 static struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
348 struct tevent_context *ev,
349 struct files_struct *fsp,
351 size_t n, off_t offset,
354 struct tevent_req *req, *subreq;
355 struct pwrite_fsync_state *state;
357 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
363 state->write_through = write_through;
365 subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
366 if (tevent_req_nomem(subreq, req)) {
367 return tevent_req_post(req, ev);
369 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
373 static void pwrite_fsync_write_done(struct tevent_req *subreq)
375 struct tevent_req *req = tevent_req_callback_data(
376 subreq, struct tevent_req);
377 struct pwrite_fsync_state *state = tevent_req_data(
378 req, struct pwrite_fsync_state);
379 connection_struct *conn = state->fsp->conn;
381 struct vfs_aio_state vfs_aio_state;
383 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &vfs_aio_state);
385 if (state->nwritten == -1) {
386 tevent_req_error(req, vfs_aio_state.error);
390 do_sync = (lp_strict_sync(SNUM(conn)) &&
391 (lp_sync_always(SNUM(conn)) || state->write_through));
393 tevent_req_done(req);
397 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
398 if (tevent_req_nomem(subreq, req)) {
401 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
404 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
406 struct tevent_req *req = tevent_req_callback_data(
407 subreq, struct tevent_req);
409 struct vfs_aio_state vfs_aio_state;
411 ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
414 tevent_req_error(req, vfs_aio_state.error);
417 tevent_req_done(req);
420 static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
422 struct pwrite_fsync_state *state = tevent_req_data(
423 req, struct pwrite_fsync_state);
425 if (tevent_req_is_unix_error(req, perr)) {
428 return state->nwritten;
431 static void aio_pwrite_smb1_done(struct tevent_req *req);
433 /****************************************************************************
434 Set up an aio request from a SMBwriteX call.
435 *****************************************************************************/
437 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
438 struct smb_request *smbreq,
439 files_struct *fsp, const char *data,
443 struct aio_extra *aio_ex;
445 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
446 struct tevent_req *req;
448 if (fsp->base_fsp != NULL) {
449 /* No AIO on streams yet */
450 DEBUG(10, ("AIO on streams not yet supported\n"));
451 return NT_STATUS_RETRY;
454 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
455 && !SMB_VFS_AIO_FORCE(fsp)) {
456 /* Too small a write for aio request. */
457 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
458 "small for minimum aio_write of %u\n",
459 (unsigned int)numtowrite,
460 (unsigned int)min_aio_write_size ));
461 return NT_STATUS_RETRY;
464 /* Only do this on non-chained and non-chaining writes not using the
466 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
467 return NT_STATUS_RETRY;
470 bufsize = smb_size + 6*2;
472 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
473 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
474 return NT_STATUS_NO_MEMORY;
476 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
478 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
479 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
480 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
482 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
483 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
486 /* Take the lock until the AIO completes. */
487 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
489 return NT_STATUS_FILE_LOCK_CONFLICT;
492 aio_ex->nbyte = numtowrite;
493 aio_ex->offset = startpos;
495 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
496 data, numtowrite, startpos,
497 aio_ex->write_through);
499 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
500 "Error %s\n", strerror(errno) ));
501 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
503 return NT_STATUS_RETRY;
505 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
507 if (!aio_add_req_to_fsp(fsp, req)) {
508 DEBUG(1, ("Could not add req to fsp\n"));
509 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
511 return NT_STATUS_RETRY;
514 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
516 /* This should actually be improved to span the write. */
517 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
518 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
520 if (!aio_ex->write_through && !lp_sync_always(SNUM(fsp->conn))
521 && fsp->aio_write_behind) {
522 /* Lie to the client and immediately claim we finished the
524 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
525 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
526 show_msg((char *)aio_ex->outbuf.data);
527 if (!srv_send_smb(aio_ex->smbreq->xconn,
528 (char *)aio_ex->outbuf.data,
529 true, aio_ex->smbreq->seqnum+1,
530 IS_CONN_ENCRYPTED(fsp->conn),
531 &aio_ex->smbreq->pcd)) {
532 exit_server_cleanly("schedule_aio_write_and_X: "
533 "srv_send_smb failed.");
535 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
536 "behind for file %s\n", fsp_str_dbg(fsp)));
539 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
540 "%s, offset %.0f, len = %u (mid = %u) "
541 "outstanding_aio_calls = %d\n",
542 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
543 (unsigned int)aio_ex->smbreq->mid,
544 get_outstanding_aio_calls() ));
549 static void aio_pwrite_smb1_done(struct tevent_req *req)
551 struct aio_extra *aio_ex = tevent_req_callback_data(
552 req, struct aio_extra);
553 files_struct *fsp = aio_ex->fsp;
554 char *outbuf = (char *)aio_ex->outbuf.data;
555 ssize_t numtowrite = aio_ex->nbyte;
559 nwritten = pwrite_fsync_recv(req, &err);
562 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
563 (nwritten == -1) ? strerror(err) : "no error"));
566 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
567 "aio outstanding (mid[%llu]).\n",
568 (unsigned long long)aio_ex->smbreq->mid));
573 /* Unlock now we're done. */
574 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
576 mark_file_modified(fsp);
578 if (fsp->aio_write_behind) {
580 if (nwritten != numtowrite) {
581 if (nwritten == -1) {
582 DEBUG(5,("handle_aio_write_complete: "
583 "aio_write_behind failed ! File %s "
584 "is corrupt ! Error %s\n",
585 fsp_str_dbg(fsp), strerror(err)));
587 DEBUG(0,("handle_aio_write_complete: "
588 "aio_write_behind failed ! File %s "
589 "is corrupt ! Wanted %u bytes but "
590 "only wrote %d\n", fsp_str_dbg(fsp),
591 (unsigned int)numtowrite,
595 DEBUG(10,("handle_aio_write_complete: "
596 "aio_write_behind completed for file %s\n",
599 /* TODO: should no return success in case of an error !!! */
604 /* We don't need outsize or set_message here as we've already set the
605 fixed size length when we set up the aio call. */
607 if (nwritten == -1) {
608 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
609 "nwritten == %d. Error = %s\n",
610 fsp_str_dbg(fsp), (unsigned int)numtowrite,
611 (int)nwritten, strerror(err)));
613 ERROR_NT(map_nt_error_from_unix(err));
614 srv_set_message(outbuf,0,0,true);
616 SSVAL(outbuf,smb_vwv2,nwritten);
617 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
618 if (nwritten < (ssize_t)numtowrite) {
619 SCVAL(outbuf,smb_rcls,ERRHRD);
620 SSVAL(outbuf,smb_err,ERRdiskfull);
623 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
624 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
626 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
630 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
631 true, aio_ex->smbreq->seqnum+1,
632 IS_CONN_ENCRYPTED(fsp->conn),
634 exit_server_cleanly("handle_aio_write_complete: "
635 "srv_send_smb failed.");
638 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
639 "for file %s, offset %.0f, requested %u, written = %u\n",
640 fsp_str_dbg(fsp), (double)aio_ex->offset,
641 (unsigned int)numtowrite, (unsigned int)nwritten));
646 bool cancel_smb2_aio(struct smb_request *smbreq)
648 struct smbd_smb2_request *smb2req = smbreq->smb2req;
649 struct aio_extra *aio_ex = NULL;
652 aio_ex = talloc_get_type(smbreq->async_priv,
656 if (aio_ex == NULL) {
660 if (aio_ex->fsp == NULL) {
665 * We let the aio request run. Setting fsp to NULL has the
666 * effect that the _done routines don't send anything out.
673 static void aio_pread_smb2_done(struct tevent_req *req);
675 /****************************************************************************
676 Set up an aio request from a SMB2 read call.
677 *****************************************************************************/
679 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
680 struct smb_request *smbreq,
687 struct aio_extra *aio_ex;
688 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
689 struct tevent_req *req;
691 if (fsp->base_fsp != NULL) {
692 /* No AIO on streams yet */
693 DEBUG(10, ("AIO on streams not yet supported\n"));
694 return NT_STATUS_RETRY;
697 if (fsp->op == NULL) {
698 /* No AIO on internal opens. */
699 return NT_STATUS_RETRY;
702 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
703 && !SMB_VFS_AIO_FORCE(fsp)) {
704 /* Too small a read for aio request. */
705 DEBUG(10,("smb2: read size (%u) too small "
706 "for minimum aio_read of %u\n",
707 (unsigned int)smb_maxcnt,
708 (unsigned int)min_aio_read_size ));
709 return NT_STATUS_RETRY;
712 /* Only do this on reads not using the write cache. */
713 if (lp_write_cache_size(SNUM(conn)) != 0) {
714 return NT_STATUS_RETRY;
717 /* Create the out buffer. */
718 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
719 if (preadbuf->data == NULL) {
720 return NT_STATUS_NO_MEMORY;
723 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
724 return NT_STATUS_NO_MEMORY;
727 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
728 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
731 /* Take the lock until the AIO completes. */
732 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
734 return NT_STATUS_FILE_LOCK_CONFLICT;
737 aio_ex->nbyte = smb_maxcnt;
738 aio_ex->offset = startpos;
740 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
741 preadbuf->data, smb_maxcnt, startpos);
743 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
744 "Error %s\n", strerror(errno)));
745 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
747 return NT_STATUS_RETRY;
749 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
751 if (!aio_add_req_to_fsp(fsp, req)) {
752 DEBUG(1, ("Could not add req to fsp\n"));
753 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
755 return NT_STATUS_RETRY;
758 /* We don't need talloc_move here as both aio_ex and
759 * smbreq are children of smbreq->smb2req. */
760 aio_ex->smbreq = smbreq;
761 smbreq->async_priv = aio_ex;
763 DEBUG(10,("smb2: scheduled aio_read for file %s, "
764 "offset %.0f, len = %u (mid = %u)\n",
765 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
766 (unsigned int)aio_ex->smbreq->mid ));
771 static void aio_pread_smb2_done(struct tevent_req *req)
773 struct aio_extra *aio_ex = tevent_req_callback_data(
774 req, struct aio_extra);
775 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
776 files_struct *fsp = aio_ex->fsp;
779 struct vfs_aio_state vfs_aio_state = { 0 };
781 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
784 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
785 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
788 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
789 __func__, (uintmax_t)aio_ex->smbreq->mid));
791 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
795 /* Unlock now we're done. */
796 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
798 /* Common error or success code processing for async or sync
801 status = smb2_read_complete(subreq, nread, vfs_aio_state.error);
804 fsp->fh->pos = aio_ex->offset + nread;
805 fsp->fh->position_information = fsp->fh->pos;
808 DEBUG(10, ("smb2: scheduled aio_read completed "
809 "for file %s, offset %.0f, len = %u "
810 "(errcode = %d, NTSTATUS = %s)\n",
811 fsp_str_dbg(aio_ex->fsp),
812 (double)aio_ex->offset,
814 vfs_aio_state.error, nt_errstr(status)));
816 if (!NT_STATUS_IS_OK(status)) {
817 tevent_req_nterror(subreq, status);
820 tevent_req_done(subreq);
823 static void aio_pwrite_smb2_done(struct tevent_req *req);
825 /****************************************************************************
826 Set up an aio request from a SMB2write call.
827 *****************************************************************************/
829 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
830 struct smb_request *smbreq,
836 struct aio_extra *aio_ex = NULL;
837 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
838 struct tevent_req *req;
840 if (fsp->base_fsp != NULL) {
841 /* No AIO on streams yet */
842 DEBUG(10, ("AIO on streams not yet supported\n"));
843 return NT_STATUS_RETRY;
846 if (fsp->op == NULL) {
847 /* No AIO on internal opens. */
848 return NT_STATUS_RETRY;
851 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
852 && !SMB_VFS_AIO_FORCE(fsp)) {
853 /* Too small a write for aio request. */
854 DEBUG(10,("smb2: write size (%u) too "
855 "small for minimum aio_write of %u\n",
856 (unsigned int)in_data.length,
857 (unsigned int)min_aio_write_size ));
858 return NT_STATUS_RETRY;
861 /* Only do this on writes not using the write cache. */
862 if (lp_write_cache_size(SNUM(conn)) != 0) {
863 return NT_STATUS_RETRY;
866 if (smbreq->unread_bytes) {
867 /* Can't do async with recvfile. */
868 return NT_STATUS_RETRY;
871 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
872 return NT_STATUS_NO_MEMORY;
875 aio_ex->write_through = write_through;
877 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
878 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
881 /* Take the lock until the AIO completes. */
882 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
884 return NT_STATUS_FILE_LOCK_CONFLICT;
887 aio_ex->nbyte = in_data.length;
888 aio_ex->offset = in_offset;
890 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
891 in_data.data, in_data.length, in_offset,
894 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
895 "Error %s\n", strerror(errno)));
896 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
898 return NT_STATUS_RETRY;
900 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
902 if (!aio_add_req_to_fsp(fsp, req)) {
903 DEBUG(1, ("Could not add req to fsp\n"));
904 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
906 return NT_STATUS_RETRY;
909 /* We don't need talloc_move here as both aio_ex and
910 * smbreq are children of smbreq->smb2req. */
911 aio_ex->smbreq = smbreq;
912 smbreq->async_priv = aio_ex;
914 /* This should actually be improved to span the write. */
915 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
916 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
919 * We don't want to do write behind due to ownership
920 * issues of the request structs. Maybe add it if I
921 * figure those out. JRA.
924 DEBUG(10,("smb2: scheduled aio_write for file "
925 "%s, offset %.0f, len = %u (mid = %u) "
926 "outstanding_aio_calls = %d\n",
929 (unsigned int)in_data.length,
930 (unsigned int)aio_ex->smbreq->mid,
931 get_outstanding_aio_calls() ));
936 static void aio_pwrite_smb2_done(struct tevent_req *req)
938 struct aio_extra *aio_ex = tevent_req_callback_data(
939 req, struct aio_extra);
940 ssize_t numtowrite = aio_ex->nbyte;
941 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
942 files_struct *fsp = aio_ex->fsp;
947 nwritten = pwrite_fsync_recv(req, &err);
950 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
951 (nwritten == -1) ? strerror(err) : "no error"));
954 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
955 __func__, (uintmax_t)aio_ex->smbreq->mid));
957 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
961 /* Unlock now we're done. */
962 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
964 mark_file_modified(fsp);
966 status = smb2_write_complete_nosync(subreq, nwritten, err);
968 DEBUG(10, ("smb2: scheduled aio_write completed "
969 "for file %s, offset %.0f, requested %u, "
970 "written = %u (errcode = %d, NTSTATUS = %s)\n",
972 (double)aio_ex->offset,
973 (unsigned int)numtowrite,
974 (unsigned int)nwritten,
975 err, nt_errstr(status)));
977 if (!NT_STATUS_IS_OK(status)) {
978 tevent_req_nterror(subreq, status);
981 tevent_req_done(subreq);