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;
53 int (*handle_completion)(struct aio_extra *ex, int errcode);
56 /****************************************************************************
57 Initialize the signal handler for aio read/write.
58 *****************************************************************************/
60 static void smbd_aio_signal_handler(struct tevent_context *ev_ctx,
61 struct tevent_signal *se,
62 int signum, int count,
63 void *_info, void *private_data)
65 siginfo_t *info = (siginfo_t *)_info;
66 struct aio_extra *aio_ex = (struct aio_extra *)
67 info->si_value.sival_ptr;
69 smbd_aio_complete_aio_ex(aio_ex);
74 bool initialize_async_io_handler(void)
76 static bool tried_signal_setup = false;
78 if (aio_signal_event) {
81 if (tried_signal_setup) {
84 tried_signal_setup = true;
86 aio_signal_event = tevent_add_signal(server_event_context(),
87 server_event_context(),
88 RT_SIGNAL_AIO, SA_SIGINFO,
89 smbd_aio_signal_handler,
91 if (!aio_signal_event) {
92 DEBUG(10, ("Failed to setup RT_SIGNAL_AIO handler\n"));
96 /* tevent supports 100 signal with SA_SIGINFO */
97 aio_pending_size = 100;
101 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode);
102 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode);
103 static int handle_aio_smb2_read_complete(struct aio_extra *aio_ex, int errcode);
104 static int handle_aio_smb2_write_complete(struct aio_extra *aio_ex, int errcode);
106 static int aio_extra_destructor(struct aio_extra *aio_ex)
108 DLIST_REMOVE(aio_list_head, aio_ex);
109 outstanding_aio_calls--;
113 /****************************************************************************
114 Create the extended aio struct we must keep around for the lifetime
116 *****************************************************************************/
118 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
122 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
128 /* The output buffer stored in the aio_ex is the start of
129 the smb return buffer. The buffer used in the acb
130 is the start of the reply data portion of that buffer. */
133 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
134 if (!aio_ex->outbuf.data) {
139 DLIST_ADD(aio_list_head, aio_ex);
140 talloc_set_destructor(aio_ex, aio_extra_destructor);
142 outstanding_aio_calls++;
146 /****************************************************************************
147 Set up an aio request from a SMBreadX call.
148 *****************************************************************************/
150 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
151 struct smb_request *smbreq,
152 files_struct *fsp, off_t startpos,
155 struct aio_extra *aio_ex;
158 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
161 if (fsp->base_fsp != NULL) {
162 /* No AIO on streams yet */
163 DEBUG(10, ("AIO on streams not yet supported\n"));
164 return NT_STATUS_RETRY;
167 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
168 && !SMB_VFS_AIO_FORCE(fsp)) {
169 /* Too small a read for aio request. */
170 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
171 "for minimum aio_read of %u\n",
172 (unsigned int)smb_maxcnt,
173 (unsigned int)min_aio_read_size ));
174 return NT_STATUS_RETRY;
177 /* Only do this on non-chained and non-chaining reads not using the
179 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
180 return NT_STATUS_RETRY;
183 if (outstanding_aio_calls >= aio_pending_size) {
184 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
185 "activities outstanding.\n",
186 outstanding_aio_calls ));
187 return NT_STATUS_RETRY;
190 /* The following is safe from integer wrap as we've already checked
191 smb_maxcnt is 128k or less. Wct is 12 for read replies */
193 bufsize = smb_size + 12 * 2 + smb_maxcnt;
195 if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
196 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
197 return NT_STATUS_NO_MEMORY;
199 aio_ex->handle_completion = handle_aio_read_complete;
201 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
202 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
203 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
205 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
206 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
209 /* Take the lock until the AIO completes. */
210 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
212 return NT_STATUS_FILE_LOCK_CONFLICT;
217 /* Now set up the aio record for the read call. */
219 a->aio_fildes = fsp->fh->fd;
220 a->aio_buf = smb_buf(aio_ex->outbuf.data);
221 a->aio_nbytes = smb_maxcnt;
222 a->aio_offset = startpos;
223 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
224 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
225 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
227 ret = SMB_VFS_AIO_READ(fsp, a);
229 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
230 "Error %s\n", strerror(errno) ));
231 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
233 return NT_STATUS_RETRY;
236 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
238 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
239 "offset %.0f, len = %u (mid = %u)\n",
240 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
241 (unsigned int)aio_ex->smbreq->mid ));
246 /****************************************************************************
247 Set up an aio request from a SMBwriteX call.
248 *****************************************************************************/
250 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
251 struct smb_request *smbreq,
252 files_struct *fsp, const char *data,
256 struct aio_extra *aio_ex;
259 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
262 if (fsp->base_fsp != NULL) {
263 /* No AIO on streams yet */
264 DEBUG(10, ("AIO on streams not yet supported\n"));
265 return NT_STATUS_RETRY;
268 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
269 && !SMB_VFS_AIO_FORCE(fsp)) {
270 /* Too small a write for aio request. */
271 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
272 "small for minimum aio_write of %u\n",
273 (unsigned int)numtowrite,
274 (unsigned int)min_aio_write_size ));
275 return NT_STATUS_RETRY;
278 /* Only do this on non-chained and non-chaining writes not using the
280 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
281 return NT_STATUS_RETRY;
284 if (outstanding_aio_calls >= aio_pending_size) {
285 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
286 "activities outstanding.\n",
287 outstanding_aio_calls ));
288 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
289 "aio_write for file %s, offset %.0f, len = %u "
291 fsp_str_dbg(fsp), (double)startpos,
292 (unsigned int)numtowrite,
293 (unsigned int)smbreq->mid ));
294 return NT_STATUS_RETRY;
297 bufsize = smb_size + 6*2;
299 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
300 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
301 return NT_STATUS_NO_MEMORY;
303 aio_ex->handle_completion = handle_aio_write_complete;
304 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
306 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
307 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
308 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
310 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
311 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
314 /* Take the lock until the AIO completes. */
315 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
317 return NT_STATUS_FILE_LOCK_CONFLICT;
322 /* Now set up the aio record for the write call. */
324 a->aio_fildes = fsp->fh->fd;
325 a->aio_buf = discard_const_p(char, data);
326 a->aio_nbytes = numtowrite;
327 a->aio_offset = startpos;
328 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
329 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
330 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
332 ret = SMB_VFS_AIO_WRITE(fsp, a);
334 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
335 "Error %s\n", strerror(errno) ));
336 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
338 return NT_STATUS_RETRY;
341 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
343 /* This should actually be improved to span the write. */
344 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
345 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
347 if (!aio_ex->write_through && !lp_syncalways(SNUM(fsp->conn))
348 && fsp->aio_write_behind) {
349 /* Lie to the client and immediately claim we finished the
351 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
352 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
353 show_msg((char *)aio_ex->outbuf.data);
354 if (!srv_send_smb(aio_ex->smbreq->sconn,
355 (char *)aio_ex->outbuf.data,
356 true, aio_ex->smbreq->seqnum+1,
357 IS_CONN_ENCRYPTED(fsp->conn),
358 &aio_ex->smbreq->pcd)) {
359 exit_server_cleanly("schedule_aio_write_and_X: "
360 "srv_send_smb failed.");
362 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
363 "behind for file %s\n", fsp_str_dbg(fsp)));
366 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
367 "%s, offset %.0f, len = %u (mid = %u) "
368 "outstanding_aio_calls = %d\n",
369 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
370 (unsigned int)aio_ex->smbreq->mid, outstanding_aio_calls ));
375 bool cancel_smb2_aio(struct smb_request *smbreq)
377 struct smbd_smb2_request *smb2req = smbreq->smb2req;
378 struct aio_extra *aio_ex = NULL;
382 aio_ex = talloc_get_type(smbreq->async_priv,
386 if (aio_ex == NULL) {
390 if (aio_ex->fsp == NULL) {
394 ret = SMB_VFS_AIO_CANCEL(aio_ex->fsp, &aio_ex->acb);
395 if (ret != AIO_CANCELED) {
402 /****************************************************************************
403 Set up an aio request from a SMB2 read call.
404 *****************************************************************************/
406 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
407 struct smb_request *smbreq,
414 struct aio_extra *aio_ex;
416 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
419 if (fsp->base_fsp != NULL) {
420 /* No AIO on streams yet */
421 DEBUG(10, ("AIO on streams not yet supported\n"));
422 return NT_STATUS_RETRY;
425 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
426 && !SMB_VFS_AIO_FORCE(fsp)) {
427 /* Too small a read for aio request. */
428 DEBUG(10,("smb2: read size (%u) too small "
429 "for minimum aio_read of %u\n",
430 (unsigned int)smb_maxcnt,
431 (unsigned int)min_aio_read_size ));
432 return NT_STATUS_RETRY;
435 /* Only do this on reads not using the write cache. */
436 if (lp_write_cache_size(SNUM(conn)) != 0) {
437 return NT_STATUS_RETRY;
440 if (outstanding_aio_calls >= aio_pending_size) {
441 DEBUG(10,("smb2: Already have %d aio "
442 "activities outstanding.\n",
443 outstanding_aio_calls ));
444 return NT_STATUS_RETRY;
447 /* Create the out buffer. */
448 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
449 if (preadbuf->data == NULL) {
450 return NT_STATUS_NO_MEMORY;
453 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
454 return NT_STATUS_NO_MEMORY;
456 aio_ex->handle_completion = handle_aio_smb2_read_complete;
457 aio_ex->pass_cancel = true;
459 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
460 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
463 /* Take the lock until the AIO completes. */
464 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
466 return NT_STATUS_FILE_LOCK_CONFLICT;
471 /* Now set up the aio record for the read call. */
473 a->aio_fildes = fsp->fh->fd;
474 a->aio_buf = preadbuf->data;
475 a->aio_nbytes = smb_maxcnt;
476 a->aio_offset = startpos;
477 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
478 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
479 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
481 ret = SMB_VFS_AIO_READ(fsp, a);
483 DEBUG(0,("smb2: aio_read failed. "
484 "Error %s\n", strerror(errno) ));
485 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
487 return NT_STATUS_RETRY;
490 /* We don't need talloc_move here as both aio_ex and
491 * smbreq are children of smbreq->smb2req. */
492 aio_ex->smbreq = smbreq;
493 smbreq->async_priv = aio_ex;
495 DEBUG(10,("smb2: scheduled aio_read for file %s, "
496 "offset %.0f, len = %u (mid = %u)\n",
497 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
498 (unsigned int)aio_ex->smbreq->mid ));
503 /****************************************************************************
504 Set up an aio request from a SMB2write call.
505 *****************************************************************************/
507 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
508 struct smb_request *smbreq,
514 struct aio_extra *aio_ex = NULL;
515 SMB_STRUCT_AIOCB *a = NULL;
516 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
519 if (fsp->base_fsp != NULL) {
520 /* No AIO on streams yet */
521 DEBUG(10, ("AIO on streams not yet supported\n"));
522 return NT_STATUS_RETRY;
525 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
526 && !SMB_VFS_AIO_FORCE(fsp)) {
527 /* Too small a write for aio request. */
528 DEBUG(10,("smb2: write size (%u) too "
529 "small for minimum aio_write of %u\n",
530 (unsigned int)in_data.length,
531 (unsigned int)min_aio_write_size ));
532 return NT_STATUS_RETRY;
535 /* Only do this on writes not using the write cache. */
536 if (lp_write_cache_size(SNUM(conn)) != 0) {
537 return NT_STATUS_RETRY;
540 if (outstanding_aio_calls >= aio_pending_size) {
541 DEBUG(3,("smb2: Already have %d aio "
542 "activities outstanding.\n",
543 outstanding_aio_calls ));
544 return NT_STATUS_RETRY;
547 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
548 return NT_STATUS_NO_MEMORY;
551 aio_ex->handle_completion = handle_aio_smb2_write_complete;
552 aio_ex->write_through = write_through;
553 aio_ex->pass_cancel = true;
555 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
556 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
559 /* Take the lock until the AIO completes. */
560 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
562 return NT_STATUS_FILE_LOCK_CONFLICT;
567 /* Now set up the aio record for the write call. */
569 a->aio_fildes = fsp->fh->fd;
570 a->aio_buf = in_data.data;
571 a->aio_nbytes = in_data.length;
572 a->aio_offset = in_offset;
573 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
574 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
575 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
577 ret = SMB_VFS_AIO_WRITE(fsp, a);
579 DEBUG(3,("smb2: aio_write failed. "
580 "Error %s\n", strerror(errno) ));
581 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
583 return NT_STATUS_RETRY;
586 /* We don't need talloc_move here as both aio_ex and
587 * smbreq are children of smbreq->smb2req. */
588 aio_ex->smbreq = smbreq;
589 smbreq->async_priv = aio_ex;
591 /* This should actually be improved to span the write. */
592 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
593 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
596 * We don't want to do write behind due to ownership
597 * issues of the request structs. Maybe add it if I
598 * figure those out. JRA.
601 DEBUG(10,("smb2: scheduled aio_write for file "
602 "%s, offset %.0f, len = %u (mid = %u) "
603 "outstanding_aio_calls = %d\n",
606 (unsigned int)in_data.length,
607 (unsigned int)aio_ex->smbreq->mid,
608 outstanding_aio_calls ));
613 /****************************************************************************
614 Complete the read and return the data or error back to the client.
615 Returns errno or zero if all ok.
616 *****************************************************************************/
618 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
621 char *outbuf = (char *)aio_ex->outbuf.data;
622 char *data = smb_buf(outbuf);
623 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
626 /* We're relying here on the fact that if the fd is
627 closed then the aio will complete and aio_return
628 will return an error. Hopefully this is
631 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
633 fsp_str_dbg(aio_ex->fsp), (int)nread, strerror(errcode)));
635 ERROR_NT(map_nt_error_from_unix(errcode));
636 outsize = srv_set_message(outbuf,0,0,true);
638 outsize = srv_set_message(outbuf,12,nread,False);
639 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
640 SSVAL(outbuf,smb_vwv5,nread);
641 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
642 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
643 SSVAL(smb_buf(outbuf),-2,nread);
645 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
646 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
648 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
650 fsp_str_dbg(aio_ex->fsp),
651 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
654 smb_setlen(outbuf,outsize - 4);
656 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
657 true, aio_ex->smbreq->seqnum+1,
658 IS_CONN_ENCRYPTED(aio_ex->fsp->conn), NULL)) {
659 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
663 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
664 "for file %s, offset %.0f, len = %u\n",
665 fsp_str_dbg(aio_ex->fsp), (double)aio_ex->acb.aio_offset,
666 (unsigned int)nread ));
671 /****************************************************************************
672 Complete the write and return the data or error back to the client.
673 Returns error code or zero if all ok.
674 *****************************************************************************/
676 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
678 files_struct *fsp = aio_ex->fsp;
679 char *outbuf = (char *)aio_ex->outbuf.data;
680 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
681 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
683 if (fsp->aio_write_behind) {
684 if (nwritten != numtowrite) {
685 if (nwritten == -1) {
686 DEBUG(5,("handle_aio_write_complete: "
687 "aio_write_behind failed ! File %s "
688 "is corrupt ! Error %s\n",
689 fsp_str_dbg(fsp), strerror(errcode)));
691 DEBUG(0,("handle_aio_write_complete: "
692 "aio_write_behind failed ! File %s "
693 "is corrupt ! Wanted %u bytes but "
694 "only wrote %d\n", fsp_str_dbg(fsp),
695 (unsigned int)numtowrite,
700 DEBUG(10,("handle_aio_write_complete: "
701 "aio_write_behind completed for file %s\n",
704 /* TODO: should no return 0 in case of an error !!! */
708 /* We don't need outsize or set_message here as we've already set the
709 fixed size length when we set up the aio call. */
712 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
713 "nwritten == %d. Error = %s\n",
714 fsp_str_dbg(fsp), (unsigned int)numtowrite,
715 (int)nwritten, strerror(errcode) ));
717 ERROR_NT(map_nt_error_from_unix(errcode));
718 srv_set_message(outbuf,0,0,true);
722 SSVAL(outbuf,smb_vwv2,nwritten);
723 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
724 if (nwritten < (ssize_t)numtowrite) {
725 SCVAL(outbuf,smb_rcls,ERRHRD);
726 SSVAL(outbuf,smb_err,ERRdiskfull);
729 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
730 fsp->fnum, (int)numtowrite, (int)nwritten));
731 status = sync_file(fsp->conn,fsp, aio_ex->write_through);
732 if (!NT_STATUS_IS_OK(status)) {
734 ERROR_BOTH(map_nt_error_from_unix(errcode),
735 ERRHRD, ERRdiskfull);
736 srv_set_message(outbuf,0,0,true);
737 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
738 fsp_str_dbg(fsp), nt_errstr(status)));
741 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
745 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
746 true, aio_ex->smbreq->seqnum+1,
747 IS_CONN_ENCRYPTED(fsp->conn),
749 exit_server_cleanly("handle_aio_write_complete: "
750 "srv_send_smb failed.");
753 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
754 "for file %s, offset %.0f, requested %u, written = %u\n",
755 fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
756 (unsigned int)numtowrite, (unsigned int)nwritten ));
761 /****************************************************************************
762 Complete the read and return the data or error back to the client.
763 Returns errno or zero if all ok.
764 *****************************************************************************/
766 static int handle_aio_smb2_read_complete(struct aio_extra *aio_ex, int errcode)
769 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
770 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
772 /* Common error or success code processing for async or sync
775 status = smb2_read_complete(subreq, nread, errcode);
778 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
779 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
782 DEBUG(10,("smb2: scheduled aio_read completed "
783 "for file %s, offset %.0f, len = %u "
784 "(errcode = %d, NTSTATUS = %s)\n",
785 fsp_str_dbg(aio_ex->fsp),
786 (double)aio_ex->acb.aio_offset,
789 nt_errstr(status) ));
791 if (!NT_STATUS_IS_OK(status)) {
792 tevent_req_nterror(subreq, status);
796 tevent_req_done(subreq);
800 /****************************************************************************
801 Complete the SMB2 write and return the data or error back to the client.
802 Returns error code or zero if all ok.
803 *****************************************************************************/
805 static int handle_aio_smb2_write_complete(struct aio_extra *aio_ex, int errcode)
807 files_struct *fsp = aio_ex->fsp;
808 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
809 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
810 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
813 status = smb2_write_complete(subreq, nwritten, errcode);
815 DEBUG(10,("smb2: scheduled aio_write completed "
816 "for file %s, offset %.0f, requested %u, "
817 "written = %u (errcode = %d, NTSTATUS = %s)\n",
819 (double)aio_ex->acb.aio_offset,
820 (unsigned int)numtowrite,
821 (unsigned int)nwritten,
823 nt_errstr(status) ));
825 if (!NT_STATUS_IS_OK(status)) {
826 tevent_req_nterror(subreq, status);
830 tevent_req_done(subreq);
834 /****************************************************************************
835 Handle any aio completion. Returns True if finished (and sets *perr if err
836 was non-zero), False if not.
837 *****************************************************************************/
839 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
841 files_struct *fsp = NULL;
845 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
850 DEBUG(3, ("handle_aio_completed: aio_ex->fsp == NULL\n"));
856 /* Ensure the operation has really completed. */
857 err = SMB_VFS_AIO_ERROR(fsp, &aio_ex->acb);
858 if (err == EINPROGRESS) {
859 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
860 "process for file %s\n",
861 (unsigned long long)aio_ex->smbreq->mid,
862 fsp_str_dbg(aio_ex->fsp)));
866 /* Unlock now we're done. */
867 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
869 if (!aio_ex->pass_cancel && err == ECANCELED) {
870 /* If error is ECANCELED then don't return anything to the
872 DEBUG(10,( "handle_aio_completed: operation mid %llu"
874 (unsigned long long)aio_ex->smbreq->mid));
878 err = aio_ex->handle_completion(aio_ex, err);
880 *perr = err; /* Only save non-zero errors. */
886 /****************************************************************************
887 Handle any aio completion inline.
888 *****************************************************************************/
890 void smbd_aio_complete_aio_ex(struct aio_extra *aio_ex)
892 files_struct *fsp = NULL;
895 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
896 (unsigned long long)aio_ex->smbreq->mid));
900 /* file was closed whilst I/O was outstanding. Just
902 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
903 "aio outstanding (mid[%llu]).\n",
904 (unsigned long long)aio_ex->smbreq->mid));
908 if (!handle_aio_completed(aio_ex, &ret)) {
913 /****************************************************************************
914 We're doing write behind and the client closed the file. Wait up to 45
915 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
916 completed, errno to return if not.
917 *****************************************************************************/
919 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 45
921 int wait_for_aio_completion(files_struct *fsp)
923 struct aio_extra *aio_ex;
924 const SMB_STRUCT_AIOCB **aiocb_list;
925 int aio_completion_count = 0;
926 time_t start_time = time_mono(NULL);
929 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
930 seconds_left >= 0;) {
935 aio_completion_count = 0;
936 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
937 if (aio_ex->fsp == fsp) {
938 aio_completion_count++;
942 if (!aio_completion_count) {
946 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
947 "to complete.\n", aio_completion_count ));
949 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
950 aio_completion_count);
955 for( i = 0, aio_ex = aio_list_head;
957 aio_ex = aio_ex->next) {
958 if (aio_ex->fsp == fsp) {
959 aiocb_list[i++] = &aio_ex->acb;
963 /* Now wait up to seconds_left for completion. */
964 ts.tv_sec = seconds_left;
967 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
969 aio_completion_count, seconds_left ));
971 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
972 aio_completion_count, &ts);
974 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
975 "errno = %s\n", err, strerror(errno) ));
977 if (err == -1 && errno == EAGAIN) {
978 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
979 "out waiting for %d events after a wait of "
980 "%d seconds\n", aio_completion_count,
983 SAFE_FREE(aiocb_list);
984 /* We're hosed here - IO may complete
985 and trample over memory if we free
986 the aio_ex struct, but if we don't
987 we leak IO requests. I think smb_panic()
988 if the right thing to do here. JRA.
990 smb_panic("AIO suspend timed out - cannot continue.");
994 /* One or more events might have completed - process them if
996 for( i = 0; i < aio_completion_count; i++) {
997 aio_ex = (struct aio_extra *)aiocb_list[i]->aio_sigevent.sigev_value.sival_ptr;
999 if (!handle_aio_completed(aio_ex, &err)) {
1002 TALLOC_FREE(aio_ex);
1005 SAFE_FREE(aiocb_list);
1006 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
1007 - (time_mono(NULL) - start_time);
1010 /* We timed out - we don't know why. Return ret if already an error,
1012 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
1014 aio_completion_count));
1021 bool initialize_async_io_handler(void)
1026 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
1027 struct smb_request *smbreq,
1028 files_struct *fsp, off_t startpos,
1031 return NT_STATUS_RETRY;
1034 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
1035 struct smb_request *smbreq,
1036 files_struct *fsp, const char *data,
1040 return NT_STATUS_RETRY;
1043 bool cancel_smb2_aio(struct smb_request *smbreq)
1048 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
1049 struct smb_request *smbreq,
1052 DATA_BLOB *preadbuf,
1056 return NT_STATUS_RETRY;
1059 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
1060 struct smb_request *smbreq,
1066 return NT_STATUS_RETRY;
1069 int wait_for_aio_completion(files_struct *fsp)
1074 void smbd_aio_complete_mid(uint64_t mid);