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"
28 /* The signal we'll use to signify aio done. */
30 #define RT_SIGNAL_AIO (SIGRTMIN+3)
33 #ifndef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIVAL_PTR
34 #ifdef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIGVAL_PTR
35 #define sival_int sigval_int
36 #define sival_ptr sigval_ptr
40 /****************************************************************************
41 The buffer we keep around whilst an aio request is in process.
42 *****************************************************************************/
45 struct aio_extra *next, *prev;
48 struct smb_request *smbreq;
50 struct lock_struct lock;
54 /****************************************************************************
55 Accessor function to return write_through state.
56 *****************************************************************************/
58 bool aio_write_through_requested(struct aio_extra *aio_ex)
60 return aio_ex->write_through;
63 bool initialize_async_io_handler(void)
68 static int aio_extra_destructor(struct aio_extra *aio_ex)
70 outstanding_aio_calls--;
74 /****************************************************************************
75 Create the extended aio struct we must keep around for the lifetime
77 *****************************************************************************/
79 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
83 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
89 /* The output buffer stored in the aio_ex is the start of
90 the smb return buffer. The buffer used in the acb
91 is the start of the reply data portion of that buffer. */
94 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
95 if (!aio_ex->outbuf.data) {
100 talloc_set_destructor(aio_ex, aio_extra_destructor);
102 outstanding_aio_calls++;
106 struct aio_req_fsp_link {
108 struct tevent_req *req;
111 static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
114 files_struct *fsp = lnk->fsp;
115 struct tevent_req *req = lnk->req;
117 for (i=0; i<fsp->num_aio_requests; i++) {
118 if (fsp->aio_requests[i] == req) {
122 if (i == fsp->num_aio_requests) {
123 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
126 fsp->num_aio_requests -= 1;
127 fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
131 static bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
134 struct aio_req_fsp_link *lnk;
136 lnk = talloc(req, struct aio_req_fsp_link);
141 array_len = talloc_array_length(fsp->aio_requests);
142 if (array_len <= fsp->num_aio_requests) {
143 struct tevent_req **tmp;
145 tmp = talloc_realloc(
146 fsp, fsp->aio_requests, struct tevent_req *,
147 fsp->num_aio_requests+1);
152 fsp->aio_requests = tmp;
154 fsp->aio_requests[fsp->num_aio_requests] = req;
155 fsp->num_aio_requests += 1;
159 talloc_set_destructor(lnk, aio_del_req_from_fsp);
164 static void aio_pread_smb1_done(struct tevent_req *req);
166 /****************************************************************************
167 Set up an aio request from a SMBreadX call.
168 *****************************************************************************/
170 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
171 struct smb_request *smbreq,
172 files_struct *fsp, off_t startpos,
175 struct aio_extra *aio_ex;
178 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
179 struct tevent_req *req;
181 if (fsp->base_fsp != NULL) {
182 /* No AIO on streams yet */
183 DEBUG(10, ("AIO on streams not yet supported\n"));
184 return NT_STATUS_RETRY;
187 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
188 && !SMB_VFS_AIO_FORCE(fsp)) {
189 /* Too small a read for aio request. */
190 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
191 "for minimum aio_read of %u\n",
192 (unsigned int)smb_maxcnt,
193 (unsigned int)min_aio_read_size ));
194 return NT_STATUS_RETRY;
197 /* Only do this on non-chained and non-chaining reads not using the
199 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
200 return NT_STATUS_RETRY;
203 if (outstanding_aio_calls >= aio_pending_size) {
204 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
205 "activities outstanding.\n",
206 outstanding_aio_calls ));
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;
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. */
224 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
225 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
228 /* Take the lock until the AIO completes. */
229 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
231 return NT_STATUS_FILE_LOCK_CONFLICT;
236 /* Now set up the aio record for the read call. */
238 a->aio_fildes = fsp->fh->fd;
239 a->aio_buf = smb_buf(aio_ex->outbuf.data);
240 a->aio_nbytes = smb_maxcnt;
241 a->aio_offset = startpos;
242 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
243 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
244 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
246 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx,
247 fsp, smb_buf(aio_ex->outbuf.data),
248 smb_maxcnt, startpos);
250 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
251 "Error %s\n", strerror(errno) ));
252 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
254 return NT_STATUS_RETRY;
256 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
258 if (!aio_add_req_to_fsp(fsp, req)) {
259 DEBUG(1, ("Could not add req to fsp\n"));
260 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
262 return NT_STATUS_RETRY;
265 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
267 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
268 "offset %.0f, len = %u (mid = %u)\n",
269 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
270 (unsigned int)aio_ex->smbreq->mid ));
275 static void aio_pread_smb1_done(struct tevent_req *req)
277 struct aio_extra *aio_ex = tevent_req_callback_data(
278 req, struct aio_extra);
279 files_struct *fsp = aio_ex->fsp;
281 char *outbuf = (char *)aio_ex->outbuf.data;
282 char *data = smb_buf(outbuf);
286 nread = SMB_VFS_PREAD_RECV(req, &err);
289 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
290 (nread == -1) ? strerror(err) : "no error"));
293 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
294 "aio outstanding (mid[%llu]).\n",
295 (unsigned long long)aio_ex->smbreq->mid));
300 /* Unlock now we're done. */
301 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
304 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
305 "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
308 ERROR_NT(map_nt_error_from_unix(err));
309 outsize = srv_set_message(outbuf,0,0,true);
311 outsize = srv_set_message(outbuf, 12, nread, False);
312 SSVAL(outbuf,smb_vwv2, 0xFFFF); /* Remaining - must be * -1. */
313 SSVAL(outbuf,smb_vwv5, nread);
314 SSVAL(outbuf,smb_vwv6, smb_offset(data,outbuf));
315 SSVAL(outbuf,smb_vwv7, ((nread >> 16) & 1));
316 SSVAL(smb_buf(outbuf), -2, nread);
318 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
319 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
321 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
322 "nread=%d\n", fsp_str_dbg(fsp),
323 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
326 smb_setlen(outbuf, outsize - 4);
328 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
329 true, aio_ex->smbreq->seqnum+1,
330 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
331 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
335 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
336 "for file %s, offset %.0f, len = %u\n",
337 fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
338 (unsigned int)nread));
343 static void aio_pwrite_smb1_done(struct tevent_req *req);
345 /****************************************************************************
346 Set up an aio request from a SMBwriteX call.
347 *****************************************************************************/
349 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
350 struct smb_request *smbreq,
351 files_struct *fsp, const char *data,
355 struct aio_extra *aio_ex;
358 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
359 struct tevent_req *req;
361 if (fsp->base_fsp != NULL) {
362 /* No AIO on streams yet */
363 DEBUG(10, ("AIO on streams not yet supported\n"));
364 return NT_STATUS_RETRY;
367 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
368 && !SMB_VFS_AIO_FORCE(fsp)) {
369 /* Too small a write for aio request. */
370 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
371 "small for minimum aio_write of %u\n",
372 (unsigned int)numtowrite,
373 (unsigned int)min_aio_write_size ));
374 return NT_STATUS_RETRY;
377 /* Only do this on non-chained and non-chaining writes not using the
379 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
380 return NT_STATUS_RETRY;
383 if (outstanding_aio_calls >= aio_pending_size) {
384 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
385 "activities outstanding.\n",
386 outstanding_aio_calls ));
387 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
388 "aio_write for file %s, offset %.0f, len = %u "
390 fsp_str_dbg(fsp), (double)startpos,
391 (unsigned int)numtowrite,
392 (unsigned int)smbreq->mid ));
393 return NT_STATUS_RETRY;
396 bufsize = smb_size + 6*2;
398 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
399 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
400 return NT_STATUS_NO_MEMORY;
402 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
404 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
405 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
406 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
408 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
409 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
412 /* Take the lock until the AIO completes. */
413 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
415 return NT_STATUS_FILE_LOCK_CONFLICT;
420 /* Now set up the aio record for the write call. */
422 a->aio_fildes = fsp->fh->fd;
423 a->aio_buf = discard_const_p(char, data);
424 a->aio_nbytes = numtowrite;
425 a->aio_offset = startpos;
426 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
427 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
428 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
430 req = SMB_VFS_PWRITE_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
431 data, numtowrite, startpos);
433 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
434 "Error %s\n", strerror(errno) ));
435 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
437 return NT_STATUS_RETRY;
439 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
441 if (!aio_add_req_to_fsp(fsp, req)) {
442 DEBUG(1, ("Could not add req to fsp\n"));
443 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
445 return NT_STATUS_RETRY;
448 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
450 /* This should actually be improved to span the write. */
451 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
452 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
454 if (!aio_ex->write_through && !lp_syncalways(SNUM(fsp->conn))
455 && fsp->aio_write_behind) {
456 /* Lie to the client and immediately claim we finished the
458 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
459 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
460 show_msg((char *)aio_ex->outbuf.data);
461 if (!srv_send_smb(aio_ex->smbreq->sconn,
462 (char *)aio_ex->outbuf.data,
463 true, aio_ex->smbreq->seqnum+1,
464 IS_CONN_ENCRYPTED(fsp->conn),
465 &aio_ex->smbreq->pcd)) {
466 exit_server_cleanly("schedule_aio_write_and_X: "
467 "srv_send_smb failed.");
469 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
470 "behind for file %s\n", fsp_str_dbg(fsp)));
473 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
474 "%s, offset %.0f, len = %u (mid = %u) "
475 "outstanding_aio_calls = %d\n",
476 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
477 (unsigned int)aio_ex->smbreq->mid, outstanding_aio_calls ));
482 static void aio_pwrite_smb1_done(struct tevent_req *req)
484 struct aio_extra *aio_ex = tevent_req_callback_data(
485 req, struct aio_extra);
486 files_struct *fsp = aio_ex->fsp;
487 char *outbuf = (char *)aio_ex->outbuf.data;
488 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
492 nwritten = SMB_VFS_PWRITE_RECV(req, &err);
495 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
496 (nwritten == -1) ? strerror(err) : "no error"));
499 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
500 "aio outstanding (mid[%llu]).\n",
501 (unsigned long long)aio_ex->smbreq->mid));
506 /* Unlock now we're done. */
507 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
509 if (fsp->aio_write_behind) {
511 if (nwritten != numtowrite) {
512 if (nwritten == -1) {
513 DEBUG(5,("handle_aio_write_complete: "
514 "aio_write_behind failed ! File %s "
515 "is corrupt ! Error %s\n",
516 fsp_str_dbg(fsp), strerror(err)));
518 DEBUG(0,("handle_aio_write_complete: "
519 "aio_write_behind failed ! File %s "
520 "is corrupt ! Wanted %u bytes but "
521 "only wrote %d\n", fsp_str_dbg(fsp),
522 (unsigned int)numtowrite,
526 DEBUG(10,("handle_aio_write_complete: "
527 "aio_write_behind completed for file %s\n",
530 /* TODO: should no return success in case of an error !!! */
535 /* We don't need outsize or set_message here as we've already set the
536 fixed size length when we set up the aio call. */
538 if (nwritten == -1) {
539 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
540 "nwritten == %d. Error = %s\n",
541 fsp_str_dbg(fsp), (unsigned int)numtowrite,
542 (int)nwritten, strerror(err)));
544 ERROR_NT(map_nt_error_from_unix(err));
545 srv_set_message(outbuf,0,0,true);
549 SSVAL(outbuf,smb_vwv2,nwritten);
550 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
551 if (nwritten < (ssize_t)numtowrite) {
552 SCVAL(outbuf,smb_rcls,ERRHRD);
553 SSVAL(outbuf,smb_err,ERRdiskfull);
556 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
557 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
558 status = sync_file(fsp->conn,fsp, aio_ex->write_through);
559 if (!NT_STATUS_IS_OK(status)) {
560 ERROR_BOTH(map_nt_error_from_unix(errno),
561 ERRHRD, ERRdiskfull);
562 srv_set_message(outbuf,0,0,true);
563 DEBUG(5, ("handle_aio_write: sync_file for %s "
565 fsp_str_dbg(fsp), nt_errstr(status)));
568 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
572 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
573 true, aio_ex->smbreq->seqnum+1,
574 IS_CONN_ENCRYPTED(fsp->conn),
576 exit_server_cleanly("handle_aio_write_complete: "
577 "srv_send_smb failed.");
580 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
581 "for file %s, offset %.0f, requested %u, written = %u\n",
582 fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
583 (unsigned int)numtowrite, (unsigned int)nwritten));
588 bool cancel_smb2_aio(struct smb_request *smbreq)
590 struct smbd_smb2_request *smb2req = smbreq->smb2req;
591 struct aio_extra *aio_ex = NULL;
594 aio_ex = talloc_get_type(smbreq->async_priv,
598 if (aio_ex == NULL) {
602 if (aio_ex->fsp == NULL) {
607 * We let the aio request run. Setting fsp to NULL has the
608 * effect that the _done routines don't send anything out.
615 static void aio_pread_smb2_done(struct tevent_req *req);
617 /****************************************************************************
618 Set up an aio request from a SMB2 read call.
619 *****************************************************************************/
621 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
622 struct smb_request *smbreq,
629 struct aio_extra *aio_ex;
631 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
632 struct tevent_req *req;
634 if (fsp->base_fsp != NULL) {
635 /* No AIO on streams yet */
636 DEBUG(10, ("AIO on streams not yet supported\n"));
637 return NT_STATUS_RETRY;
640 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
641 && !SMB_VFS_AIO_FORCE(fsp)) {
642 /* Too small a read for aio request. */
643 DEBUG(10,("smb2: read size (%u) too small "
644 "for minimum aio_read of %u\n",
645 (unsigned int)smb_maxcnt,
646 (unsigned int)min_aio_read_size ));
647 return NT_STATUS_RETRY;
650 /* Only do this on reads not using the write cache. */
651 if (lp_write_cache_size(SNUM(conn)) != 0) {
652 return NT_STATUS_RETRY;
655 if (outstanding_aio_calls >= aio_pending_size) {
656 DEBUG(10,("smb2: Already have %d aio "
657 "activities outstanding.\n",
658 outstanding_aio_calls ));
659 return NT_STATUS_RETRY;
662 /* Create the out buffer. */
663 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
664 if (preadbuf->data == NULL) {
665 return NT_STATUS_NO_MEMORY;
668 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
669 return NT_STATUS_NO_MEMORY;
672 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
673 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
676 /* Take the lock until the AIO completes. */
677 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
679 return NT_STATUS_FILE_LOCK_CONFLICT;
684 /* Now set up the aio record for the read call. */
686 a->aio_fildes = fsp->fh->fd;
687 a->aio_buf = preadbuf->data;
688 a->aio_nbytes = smb_maxcnt;
689 a->aio_offset = startpos;
690 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
691 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
692 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
694 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
695 preadbuf->data, smb_maxcnt, startpos);
697 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
698 "Error %s\n", strerror(errno)));
699 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
701 return NT_STATUS_RETRY;
703 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
705 if (!aio_add_req_to_fsp(fsp, req)) {
706 DEBUG(1, ("Could not add req to fsp\n"));
707 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
709 return NT_STATUS_RETRY;
712 /* We don't need talloc_move here as both aio_ex and
713 * smbreq are children of smbreq->smb2req. */
714 aio_ex->smbreq = smbreq;
715 smbreq->async_priv = aio_ex;
717 DEBUG(10,("smb2: scheduled aio_read for file %s, "
718 "offset %.0f, len = %u (mid = %u)\n",
719 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
720 (unsigned int)aio_ex->smbreq->mid ));
725 static void aio_pread_smb2_done(struct tevent_req *req)
727 struct aio_extra *aio_ex = tevent_req_callback_data(
728 req, struct aio_extra);
729 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
730 files_struct *fsp = aio_ex->fsp;
735 nread = SMB_VFS_PREAD_RECV(req, &err);
738 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
739 (nread == -1) ? strerror(err) : "no error"));
742 DEBUG( 3, ("aio_pread_smb2_done: file closed whilst "
743 "aio outstanding (mid[%llu]).\n",
744 (unsigned long long)aio_ex->smbreq->mid));
749 /* Unlock now we're done. */
750 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
752 /* Common error or success code processing for async or sync
755 status = smb2_read_complete(subreq, nread, err);
758 fsp->fh->pos = aio_ex->acb.aio_offset + nread;
759 fsp->fh->position_information = fsp->fh->pos;
762 DEBUG(10, ("smb2: scheduled aio_read completed "
763 "for file %s, offset %.0f, len = %u "
764 "(errcode = %d, NTSTATUS = %s)\n",
765 fsp_str_dbg(aio_ex->fsp),
766 (double)aio_ex->acb.aio_offset,
768 err, nt_errstr(status)));
770 if (!NT_STATUS_IS_OK(status)) {
771 tevent_req_nterror(subreq, status);
774 tevent_req_done(subreq);
777 static void aio_pwrite_smb2_done(struct tevent_req *req);
779 /****************************************************************************
780 Set up an aio request from a SMB2write call.
781 *****************************************************************************/
783 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
784 struct smb_request *smbreq,
790 struct aio_extra *aio_ex = NULL;
791 SMB_STRUCT_AIOCB *a = NULL;
792 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
793 struct tevent_req *req;
795 if (fsp->base_fsp != NULL) {
796 /* No AIO on streams yet */
797 DEBUG(10, ("AIO on streams not yet supported\n"));
798 return NT_STATUS_RETRY;
801 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
802 && !SMB_VFS_AIO_FORCE(fsp)) {
803 /* Too small a write for aio request. */
804 DEBUG(10,("smb2: write size (%u) too "
805 "small for minimum aio_write of %u\n",
806 (unsigned int)in_data.length,
807 (unsigned int)min_aio_write_size ));
808 return NT_STATUS_RETRY;
811 /* Only do this on writes not using the write cache. */
812 if (lp_write_cache_size(SNUM(conn)) != 0) {
813 return NT_STATUS_RETRY;
816 if (outstanding_aio_calls >= aio_pending_size) {
817 DEBUG(3,("smb2: Already have %d aio "
818 "activities outstanding.\n",
819 outstanding_aio_calls ));
820 return NT_STATUS_RETRY;
823 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
824 return NT_STATUS_NO_MEMORY;
827 aio_ex->write_through = write_through;
829 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
830 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
833 /* Take the lock until the AIO completes. */
834 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
836 return NT_STATUS_FILE_LOCK_CONFLICT;
841 /* Now set up the aio record for the write call. */
843 a->aio_fildes = fsp->fh->fd;
844 a->aio_buf = in_data.data;
845 a->aio_nbytes = in_data.length;
846 a->aio_offset = in_offset;
847 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
848 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
849 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
851 req = SMB_VFS_PWRITE_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
852 in_data.data, in_data.length, in_offset);
854 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
855 "Error %s\n", strerror(errno)));
856 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
858 return NT_STATUS_RETRY;
860 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
862 if (!aio_add_req_to_fsp(fsp, req)) {
863 DEBUG(1, ("Could not add req to fsp\n"));
864 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
866 return NT_STATUS_RETRY;
869 /* We don't need talloc_move here as both aio_ex and
870 * smbreq are children of smbreq->smb2req. */
871 aio_ex->smbreq = smbreq;
872 smbreq->async_priv = aio_ex;
874 /* This should actually be improved to span the write. */
875 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
876 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
879 * We don't want to do write behind due to ownership
880 * issues of the request structs. Maybe add it if I
881 * figure those out. JRA.
884 DEBUG(10,("smb2: scheduled aio_write for file "
885 "%s, offset %.0f, len = %u (mid = %u) "
886 "outstanding_aio_calls = %d\n",
889 (unsigned int)in_data.length,
890 (unsigned int)aio_ex->smbreq->mid,
891 outstanding_aio_calls ));
896 static void aio_pwrite_smb2_done(struct tevent_req *req)
898 struct aio_extra *aio_ex = tevent_req_callback_data(
899 req, struct aio_extra);
900 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
901 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
902 files_struct *fsp = aio_ex->fsp;
907 nwritten = SMB_VFS_PWRITE_RECV(req, &err);
910 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
911 (nwritten == -1) ? strerror(err) : "no error"));
914 DEBUG( 3, ("aio_pwrite_smb2_done: file closed whilst "
915 "aio outstanding (mid[%llu]).\n",
916 (unsigned long long)aio_ex->smbreq->mid));
921 /* Unlock now we're done. */
922 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
924 status = smb2_write_complete(subreq, nwritten, err);
926 DEBUG(10, ("smb2: scheduled aio_write completed "
927 "for file %s, offset %.0f, requested %u, "
928 "written = %u (errcode = %d, NTSTATUS = %s)\n",
930 (double)aio_ex->acb.aio_offset,
931 (unsigned int)numtowrite,
932 (unsigned int)nwritten,
933 err, nt_errstr(status)));
935 if (!NT_STATUS_IS_OK(status)) {
936 tevent_req_nterror(subreq, status);
939 tevent_req_done(subreq);
942 /****************************************************************************
943 Handle any aio completion inline.
944 *****************************************************************************/
946 void smbd_aio_complete_aio_ex(struct aio_extra *aio_ex)
951 void aio_fsp_close(files_struct *fsp)
955 for (i=0; i<fsp->num_aio_requests; i++) {
956 struct tevent_req *req = fsp->aio_requests[i];
957 struct aio_extra *aio_ex = tevent_req_callback_data(
958 req, struct aio_extra);
965 bool initialize_async_io_handler(void)
970 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
971 struct smb_request *smbreq,
972 files_struct *fsp, off_t startpos,
975 return NT_STATUS_RETRY;
978 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
979 struct smb_request *smbreq,
980 files_struct *fsp, const char *data,
984 return NT_STATUS_RETRY;
987 bool cancel_smb2_aio(struct smb_request *smbreq)
992 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
993 struct smb_request *smbreq,
1000 return NT_STATUS_RETRY;
1003 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
1004 struct smb_request *smbreq,
1010 return NT_STATUS_RETRY;
1013 void aio_fsp_close(files_struct *fsp)
1018 int wait_for_aio_completion(files_struct *fsp)
1023 void smbd_aio_complete_mid(uint64_t mid);