Change smbd_aio_complete_mid() -> smbd_aio_complete_aio_ex(). Simplifies
[Samba/ekacnet.git] / source3 / smbd / aio.c
blobc7a70b3e5432c9727733bc1cf8f1aa22b22c6fd6
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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/>.
21 #include "includes.h"
22 #include "smbd/globals.h"
24 #if defined(WITH_AIO)
26 /* The signal we'll use to signify aio done. */
27 #ifndef RT_SIGNAL_AIO
28 #define RT_SIGNAL_AIO (SIGRTMIN+3)
29 #endif
31 #ifndef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIVAL_PTR
32 #ifdef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIGVAL_PTR
33 #define sival_int sigval_int
34 #define sival_ptr sigval_ptr
35 #endif
36 #endif
38 /****************************************************************************
39 The buffer we keep around whilst an aio request is in process.
40 *****************************************************************************/
42 struct aio_extra {
43 struct aio_extra *next, *prev;
44 SMB_STRUCT_AIOCB acb;
45 files_struct *fsp;
46 struct smb_request *req;
47 DATA_BLOB outbuf;
48 struct lock_struct lock;
49 int (*handle_completion)(struct aio_extra *ex, int errcode);
52 /****************************************************************************
53 Initialize the signal handler for aio read/write.
54 *****************************************************************************/
56 static void smbd_aio_signal_handler(struct tevent_context *ev_ctx,
57 struct tevent_signal *se,
58 int signum, int count,
59 void *_info, void *private_data)
61 siginfo_t *info = (siginfo_t *)_info;
62 struct aio_extra *aio_ex = (struct aio_extra *)
63 info->si_value.sival_ptr;
65 smbd_aio_complete_aio_ex(aio_ex);
69 static void initialize_async_io_handler(void)
71 if (aio_signal_event) {
72 return;
75 aio_signal_event = tevent_add_signal(smbd_event_context(),
76 smbd_event_context(),
77 RT_SIGNAL_AIO, SA_SIGINFO,
78 smbd_aio_signal_handler,
79 NULL);
80 if (!aio_signal_event) {
81 exit_server("Failed to setup RT_SIGNAL_AIO handler");
84 /* tevent supports 100 signal with SA_SIGINFO */
85 aio_pending_size = 100;
88 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode);
89 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode);
91 static int aio_extra_destructor(struct aio_extra *aio_ex)
93 DLIST_REMOVE(aio_list_head, aio_ex);
94 return 0;
97 /****************************************************************************
98 Create the extended aio struct we must keep around for the lifetime
99 of the aio call.
100 *****************************************************************************/
102 static struct aio_extra *create_aio_extra(files_struct *fsp, size_t buflen)
104 struct aio_extra *aio_ex = TALLOC_ZERO_P(NULL, struct aio_extra);
106 if (!aio_ex) {
107 return NULL;
110 /* The output buffer stored in the aio_ex is the start of
111 the smb return buffer. The buffer used in the acb
112 is the start of the reply data portion of that buffer. */
114 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
115 if (!aio_ex->outbuf.data) {
116 TALLOC_FREE(aio_ex);
117 return NULL;
119 DLIST_ADD(aio_list_head, aio_ex);
120 talloc_set_destructor(aio_ex, aio_extra_destructor);
121 aio_ex->fsp = fsp;
122 return aio_ex;
125 /****************************************************************************
126 Set up an aio request from a SMBreadX call.
127 *****************************************************************************/
129 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
130 struct smb_request *req,
131 files_struct *fsp, SMB_OFF_T startpos,
132 size_t smb_maxcnt)
134 struct aio_extra *aio_ex;
135 SMB_STRUCT_AIOCB *a;
136 size_t bufsize;
137 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
138 int ret;
140 /* Ensure aio is initialized. */
141 initialize_async_io_handler();
143 if (fsp->base_fsp != NULL) {
144 /* No AIO on streams yet */
145 DEBUG(10, ("AIO on streams not yet supported\n"));
146 return NT_STATUS_RETRY;
149 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
150 && !SMB_VFS_AIO_FORCE(fsp)) {
151 /* Too small a read for aio request. */
152 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
153 "for minimum aio_read of %u\n",
154 (unsigned int)smb_maxcnt,
155 (unsigned int)min_aio_read_size ));
156 return NT_STATUS_RETRY;
159 /* Only do this on non-chained and non-chaining reads not using the
160 * write cache. */
161 if (req_is_in_chain(req) || (lp_write_cache_size(SNUM(conn)) != 0)) {
162 return NT_STATUS_RETRY;
165 if (outstanding_aio_calls >= aio_pending_size) {
166 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
167 "activities outstanding.\n",
168 outstanding_aio_calls ));
169 return NT_STATUS_RETRY;
172 /* The following is safe from integer wrap as we've already checked
173 smb_maxcnt is 128k or less. Wct is 12 for read replies */
175 bufsize = smb_size + 12 * 2 + smb_maxcnt;
177 if ((aio_ex = create_aio_extra(fsp, bufsize)) == NULL) {
178 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
179 return NT_STATUS_NO_MEMORY;
181 aio_ex->handle_completion = handle_aio_read_complete;
183 construct_reply_common_req(req, (char *)aio_ex->outbuf.data);
184 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
185 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
187 init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
188 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
189 &aio_ex->lock);
191 /* Take the lock until the AIO completes. */
192 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
193 TALLOC_FREE(aio_ex);
194 return NT_STATUS_FILE_LOCK_CONFLICT;
197 a = &aio_ex->acb;
199 /* Now set up the aio record for the read call. */
201 a->aio_fildes = fsp->fh->fd;
202 a->aio_buf = smb_buf(aio_ex->outbuf.data);
203 a->aio_nbytes = smb_maxcnt;
204 a->aio_offset = startpos;
205 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
206 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
207 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
209 ret = SMB_VFS_AIO_READ(fsp, a);
210 if (ret == -1) {
211 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
212 "Error %s\n", strerror(errno) ));
213 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
214 TALLOC_FREE(aio_ex);
215 return NT_STATUS_RETRY;
218 outstanding_aio_calls++;
219 aio_ex->req = talloc_move(aio_ex, &req);
221 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
222 "offset %.0f, len = %u (mid = %u)\n",
223 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
224 (unsigned int)aio_ex->req->mid ));
226 return NT_STATUS_OK;
229 /****************************************************************************
230 Set up an aio request from a SMBwriteX call.
231 *****************************************************************************/
233 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
234 struct smb_request *req,
235 files_struct *fsp, char *data,
236 SMB_OFF_T startpos,
237 size_t numtowrite)
239 struct aio_extra *aio_ex;
240 SMB_STRUCT_AIOCB *a;
241 size_t bufsize;
242 bool write_through = BITSETW(req->vwv+7,0);
243 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
244 int ret;
246 /* Ensure aio is initialized. */
247 initialize_async_io_handler();
249 if (fsp->base_fsp != NULL) {
250 /* No AIO on streams yet */
251 DEBUG(10, ("AIO on streams not yet supported\n"));
252 return NT_STATUS_RETRY;
255 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
256 && !SMB_VFS_AIO_FORCE(fsp)) {
257 /* Too small a write for aio request. */
258 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
259 "small for minimum aio_write of %u\n",
260 (unsigned int)numtowrite,
261 (unsigned int)min_aio_write_size ));
262 return NT_STATUS_RETRY;
265 /* Only do this on non-chained and non-chaining reads not using the
266 * write cache. */
267 if (req_is_in_chain(req) || (lp_write_cache_size(SNUM(conn)) != 0)) {
268 return NT_STATUS_RETRY;
271 if (outstanding_aio_calls >= aio_pending_size) {
272 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
273 "activities outstanding.\n",
274 outstanding_aio_calls ));
275 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
276 "aio_write for file %s, offset %.0f, len = %u "
277 "(mid = %u)\n",
278 fsp_str_dbg(fsp), (double)startpos,
279 (unsigned int)numtowrite,
280 (unsigned int)req->mid ));
281 return NT_STATUS_RETRY;
284 bufsize = smb_size + 6*2;
286 if (!(aio_ex = create_aio_extra(fsp, bufsize))) {
287 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
288 return NT_STATUS_NO_MEMORY;
290 aio_ex->handle_completion = handle_aio_write_complete;
292 construct_reply_common_req(req, (char *)aio_ex->outbuf.data);
293 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
294 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
296 init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
297 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
298 &aio_ex->lock);
300 /* Take the lock until the AIO completes. */
301 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
302 TALLOC_FREE(aio_ex);
303 return NT_STATUS_FILE_LOCK_CONFLICT;
306 a = &aio_ex->acb;
308 /* Now set up the aio record for the write call. */
310 a->aio_fildes = fsp->fh->fd;
311 a->aio_buf = data;
312 a->aio_nbytes = numtowrite;
313 a->aio_offset = startpos;
314 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
315 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
316 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
318 ret = SMB_VFS_AIO_WRITE(fsp, a);
319 if (ret == -1) {
320 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
321 "Error %s\n", strerror(errno) ));
322 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
323 TALLOC_FREE(aio_ex);
324 return NT_STATUS_RETRY;
327 outstanding_aio_calls++;
328 aio_ex->req = talloc_move(aio_ex, &req);
330 /* This should actually be improved to span the write. */
331 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
332 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
334 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
335 && fsp->aio_write_behind) {
336 /* Lie to the client and immediately claim we finished the
337 * write. */
338 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
339 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
340 show_msg((char *)aio_ex->outbuf.data);
341 if (!srv_send_smb(smbd_server_fd(),(char *)aio_ex->outbuf.data,
342 true, aio_ex->req->seqnum+1,
343 IS_CONN_ENCRYPTED(fsp->conn),
344 &aio_ex->req->pcd)) {
345 exit_server_cleanly("handle_aio_write: srv_send_smb "
346 "failed.");
348 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
349 "behind for file %s\n", fsp_str_dbg(fsp)));
352 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
353 "%s, offset %.0f, len = %u (mid = %u) "
354 "outstanding_aio_calls = %d\n",
355 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
356 (unsigned int)aio_ex->req->mid, outstanding_aio_calls ));
358 return NT_STATUS_OK;
361 /****************************************************************************
362 Complete the read and return the data or error back to the client.
363 Returns errno or zero if all ok.
364 *****************************************************************************/
366 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
368 int outsize;
369 char *outbuf = (char *)aio_ex->outbuf.data;
370 char *data = smb_buf(outbuf);
371 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
373 if (nread < 0) {
374 /* We're relying here on the fact that if the fd is
375 closed then the aio will complete and aio_return
376 will return an error. Hopefully this is
377 true.... JRA. */
379 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
380 "Error = %s\n",
381 fsp_str_dbg(aio_ex->fsp), (int)nread, strerror(errcode)));
383 ERROR_NT(map_nt_error_from_unix(errcode));
384 outsize = srv_set_message(outbuf,0,0,true);
385 } else {
386 outsize = srv_set_message(outbuf,12,nread,False);
387 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
388 SSVAL(outbuf,smb_vwv5,nread);
389 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
390 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
391 SSVAL(smb_buf(outbuf),-2,nread);
393 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
394 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
396 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
397 "nread=%d\n",
398 fsp_str_dbg(aio_ex->fsp),
399 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
402 smb_setlen(outbuf,outsize - 4);
403 show_msg(outbuf);
404 if (!srv_send_smb(smbd_server_fd(),outbuf,
405 true, aio_ex->req->seqnum+1,
406 IS_CONN_ENCRYPTED(aio_ex->fsp->conn), NULL)) {
407 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
408 "failed.");
411 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
412 "for file %s, offset %.0f, len = %u\n",
413 fsp_str_dbg(aio_ex->fsp), (double)aio_ex->acb.aio_offset,
414 (unsigned int)nread ));
416 return errcode;
419 /****************************************************************************
420 Complete the write and return the data or error back to the client.
421 Returns error code or zero if all ok.
422 *****************************************************************************/
424 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
426 files_struct *fsp = aio_ex->fsp;
427 char *outbuf = (char *)aio_ex->outbuf.data;
428 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
429 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
431 if (fsp->aio_write_behind) {
432 if (nwritten != numtowrite) {
433 if (nwritten == -1) {
434 DEBUG(5,("handle_aio_write_complete: "
435 "aio_write_behind failed ! File %s "
436 "is corrupt ! Error %s\n",
437 fsp_str_dbg(fsp), strerror(errcode)));
438 } else {
439 DEBUG(0,("handle_aio_write_complete: "
440 "aio_write_behind failed ! File %s "
441 "is corrupt ! Wanted %u bytes but "
442 "only wrote %d\n", fsp_str_dbg(fsp),
443 (unsigned int)numtowrite,
444 (int)nwritten ));
445 errcode = EIO;
447 } else {
448 DEBUG(10,("handle_aio_write_complete: "
449 "aio_write_behind completed for file %s\n",
450 fsp_str_dbg(fsp)));
452 /* TODO: should no return 0 in case of an error !!! */
453 return 0;
456 /* We don't need outsize or set_message here as we've already set the
457 fixed size length when we set up the aio call. */
459 if(nwritten == -1) {
460 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
461 "nwritten == %d. Error = %s\n",
462 fsp_str_dbg(fsp), (unsigned int)numtowrite,
463 (int)nwritten, strerror(errcode) ));
465 ERROR_NT(map_nt_error_from_unix(errcode));
466 srv_set_message(outbuf,0,0,true);
467 } else {
468 bool write_through = BITSETW(aio_ex->req->vwv+7,0);
469 NTSTATUS status;
471 SSVAL(outbuf,smb_vwv2,nwritten);
472 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
473 if (nwritten < (ssize_t)numtowrite) {
474 SCVAL(outbuf,smb_rcls,ERRHRD);
475 SSVAL(outbuf,smb_err,ERRdiskfull);
478 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
479 fsp->fnum, (int)numtowrite, (int)nwritten));
480 status = sync_file(fsp->conn,fsp, write_through);
481 if (!NT_STATUS_IS_OK(status)) {
482 errcode = errno;
483 ERROR_BOTH(map_nt_error_from_unix(errcode),
484 ERRHRD, ERRdiskfull);
485 srv_set_message(outbuf,0,0,true);
486 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
487 fsp_str_dbg(fsp), nt_errstr(status)));
490 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
493 show_msg(outbuf);
494 if (!srv_send_smb(smbd_server_fd(),outbuf,
495 true, aio_ex->req->seqnum+1,
496 IS_CONN_ENCRYPTED(fsp->conn),
497 NULL)) {
498 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
501 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
502 "for file %s, offset %.0f, requested %u, written = %u\n",
503 fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
504 (unsigned int)numtowrite, (unsigned int)nwritten ));
506 return errcode;
509 /****************************************************************************
510 Handle any aio completion. Returns True if finished (and sets *perr if err
511 was non-zero), False if not.
512 *****************************************************************************/
514 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
516 files_struct *fsp = NULL;
517 int err;
519 if(!aio_ex) {
520 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
521 return false;
524 fsp = aio_ex->fsp;
526 /* Ensure the operation has really completed. */
527 err = SMB_VFS_AIO_ERROR(fsp, &aio_ex->acb);
528 if (err == EINPROGRESS) {
529 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
530 "process for file %s\n",
531 (unsigned long long)aio_ex->req->mid,
532 fsp_str_dbg(aio_ex->fsp)));
533 return False;
536 /* Unlock now we're done. */
537 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
539 if (err == ECANCELED) {
540 /* If error is ECANCELED then don't return anything to the
541 * client. */
542 DEBUG(10,( "handle_aio_completed: operation mid %llu"
543 " canceled\n",
544 (unsigned long long)aio_ex->req->mid));
545 return True;
548 err = aio_ex->handle_completion(aio_ex, err);
549 if (err) {
550 *perr = err; /* Only save non-zero errors. */
553 return True;
556 /****************************************************************************
557 Handle any aio completion inline.
558 *****************************************************************************/
560 void smbd_aio_complete_aio_ex(struct aio_extra *aio_ex)
562 files_struct *fsp = NULL;
563 int ret = 0;
565 outstanding_aio_calls--;
567 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
568 (unsigned long long)aio_ex->req->mid));
570 fsp = aio_ex->fsp;
571 if (fsp == NULL) {
572 /* file was closed whilst I/O was outstanding. Just
573 * ignore. */
574 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
575 "aio outstanding (mid[%llu]).\n",
576 (unsigned long long)aio_ex->req->mid));
577 return;
580 if (!handle_aio_completed(aio_ex, &ret)) {
581 return;
584 TALLOC_FREE(aio_ex);
587 /****************************************************************************
588 We're doing write behind and the client closed the file. Wait up to 30
589 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
590 completed, errno to return if not.
591 *****************************************************************************/
593 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
595 int wait_for_aio_completion(files_struct *fsp)
597 struct aio_extra *aio_ex;
598 const SMB_STRUCT_AIOCB **aiocb_list;
599 int aio_completion_count = 0;
600 time_t start_time = time(NULL);
601 int seconds_left;
603 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
604 seconds_left >= 0;) {
605 int err = 0;
606 int i;
607 struct timespec ts;
609 aio_completion_count = 0;
610 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
611 if (aio_ex->fsp == fsp) {
612 aio_completion_count++;
616 if (!aio_completion_count) {
617 return 0;
620 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
621 "to complete.\n", aio_completion_count ));
623 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
624 aio_completion_count);
625 if (!aiocb_list) {
626 return ENOMEM;
629 for( i = 0, aio_ex = aio_list_head;
630 aio_ex;
631 aio_ex = aio_ex->next) {
632 if (aio_ex->fsp == fsp) {
633 aiocb_list[i++] = &aio_ex->acb;
637 /* Now wait up to seconds_left for completion. */
638 ts.tv_sec = seconds_left;
639 ts.tv_nsec = 0;
641 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
642 "of %d seconds.\n",
643 aio_completion_count, seconds_left ));
645 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
646 aio_completion_count, &ts);
648 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
649 "errno = %s\n", err, strerror(errno) ));
651 if (err == -1 && errno == EAGAIN) {
652 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
653 "out waiting for %d events after a wait of "
654 "%d seconds\n", aio_completion_count,
655 seconds_left));
656 /* Timeout. */
657 cancel_aio_by_fsp(fsp);
658 SAFE_FREE(aiocb_list);
659 return EIO;
662 /* One or more events might have completed - process them if
663 * so. */
664 for( i = 0; i < aio_completion_count; i++) {
665 aio_ex = (struct aio_extra *)aiocb_list[i]->aio_sigevent.sigev_value.sival_ptr;
667 if (!handle_aio_completed(aio_ex, &err)) {
668 continue;
670 TALLOC_FREE(aio_ex);
673 SAFE_FREE(aiocb_list);
674 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
675 - (time(NULL) - start_time);
678 /* We timed out - we don't know why. Return ret if already an error,
679 * else EIO. */
680 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
681 "for %d events\n",
682 aio_completion_count));
684 return EIO;
687 /****************************************************************************
688 Cancel any outstanding aio requests. The client doesn't care about the reply.
689 *****************************************************************************/
691 void cancel_aio_by_fsp(files_struct *fsp)
693 struct aio_extra *aio_ex;
695 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
696 if (aio_ex->fsp == fsp) {
697 /* Unlock now we're done. */
698 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
700 /* Don't delete the aio_extra record as we may have
701 completed and don't yet know it. Just do the
702 aio_cancel call and return. */
703 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
704 aio_ex->fsp = NULL; /* fsp will be closed when we
705 * return. */
710 #else
711 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
712 struct smb_request *req,
713 files_struct *fsp, SMB_OFF_T startpos,
714 size_t smb_maxcnt)
716 return NT_STATUS_RETRY;
719 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
720 struct smb_request *req,
721 files_struct *fsp, char *data,
722 SMB_OFF_T startpos,
723 size_t numtowrite)
725 return NT_STATUS_RETRY;
728 void cancel_aio_by_fsp(files_struct *fsp)
732 int wait_for_aio_completion(files_struct *fsp)
734 return ENOSYS;
737 void smbd_aio_complete_mid(uint64_t mid);
739 #endif