s3-build: really fix build of winbind_krb5_locator.
[Samba.git] / source / smbd / aio.c
blobf5aa0a56ee91aee5ff7d5b9e7acc2f8e1a0a5bca
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"
23 #if defined(WITH_AIO)
25 /* The signal we'll use to signify aio done. */
26 #ifndef RT_SIGNAL_AIO
27 #ifndef SIGRTMIN
28 #define SIGRTMIN NSIG
29 #endif
30 #define RT_SIGNAL_AIO (SIGRTMIN+3)
31 #endif
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
37 #endif
38 #endif
40 /****************************************************************************
41 The buffer we keep around whilst an aio request is in process.
42 *****************************************************************************/
44 struct aio_extra {
45 struct aio_extra *next, *prev;
46 SMB_STRUCT_AIOCB acb;
47 files_struct *fsp;
48 bool read_req;
49 uint16 mid;
50 char *inbuf;
51 char *outbuf;
54 static struct aio_extra *aio_list_head;
56 /****************************************************************************
57 Create the extended aio struct we must keep around for the lifetime
58 of the aio_read call.
59 *****************************************************************************/
61 static struct aio_extra *create_aio_ex_read(files_struct *fsp, size_t buflen,
62 uint16 mid)
64 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
66 if (!aio_ex) {
67 return NULL;
69 ZERO_STRUCTP(aio_ex);
70 /* The output buffer stored in the aio_ex is the start of
71 the smb return buffer. The buffer used in the acb
72 is the start of the reply data portion of that buffer. */
73 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, buflen);
74 if (!aio_ex->outbuf) {
75 SAFE_FREE(aio_ex);
76 return NULL;
78 DLIST_ADD(aio_list_head, aio_ex);
79 aio_ex->fsp = fsp;
80 aio_ex->read_req = True;
81 aio_ex->mid = mid;
82 return aio_ex;
85 /****************************************************************************
86 Create the extended aio struct we must keep around for the lifetime
87 of the aio_write call.
88 *****************************************************************************/
90 static struct aio_extra *create_aio_ex_write(files_struct *fsp,
91 size_t inbuflen,
92 size_t outbuflen,
93 uint16 mid)
95 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
97 if (!aio_ex) {
98 return NULL;
100 ZERO_STRUCTP(aio_ex);
102 /* We need space for an output reply of outbuflen bytes. */
103 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
104 if (!aio_ex->outbuf) {
105 SAFE_FREE(aio_ex);
106 return NULL;
109 if (!(aio_ex->inbuf = SMB_MALLOC_ARRAY(char, inbuflen))) {
110 SAFE_FREE(aio_ex->outbuf);
111 SAFE_FREE(aio_ex);
112 return NULL;
115 DLIST_ADD(aio_list_head, aio_ex);
116 aio_ex->fsp = fsp;
117 aio_ex->read_req = False;
118 aio_ex->mid = mid;
119 return aio_ex;
122 /****************************************************************************
123 Delete the extended aio struct.
124 *****************************************************************************/
126 static void delete_aio_ex(struct aio_extra *aio_ex)
128 DLIST_REMOVE(aio_list_head, aio_ex);
129 SAFE_FREE(aio_ex->inbuf);
130 SAFE_FREE(aio_ex->outbuf);
131 SAFE_FREE(aio_ex);
134 /****************************************************************************
135 Given the aiocb struct find the extended aio struct containing it.
136 *****************************************************************************/
138 static struct aio_extra *find_aio_ex(uint16 mid)
140 struct aio_extra *p;
142 for( p = aio_list_head; p; p = p->next) {
143 if (mid == p->mid) {
144 return p;
147 return NULL;
150 /****************************************************************************
151 We can have these many aio buffers in flight.
152 *****************************************************************************/
154 static int aio_pending_size;
155 static sig_atomic_t signals_received;
156 static int outstanding_aio_calls;
157 static uint16 *aio_pending_array;
159 /****************************************************************************
160 Signal handler when an aio request completes.
161 *****************************************************************************/
163 void aio_request_done(uint16_t mid)
165 if (signals_received < aio_pending_size) {
166 aio_pending_array[signals_received] = mid;
167 signals_received++;
169 /* Else signal is lost. */
172 static void signal_handler(int sig, siginfo_t *info, void *unused)
174 aio_request_done(info->si_value.sival_int);
175 sys_select_signal(RT_SIGNAL_AIO);
178 /****************************************************************************
179 Is there a signal waiting ?
180 *****************************************************************************/
182 bool aio_finished(void)
184 return (signals_received != 0);
187 /****************************************************************************
188 Initialize the signal handler for aio read/write.
189 *****************************************************************************/
191 void initialize_async_io_handler(void)
193 struct sigaction act;
195 aio_pending_size = lp_maxmux();
196 aio_pending_array = SMB_MALLOC_ARRAY(uint16, aio_pending_size);
197 SMB_ASSERT(aio_pending_array != NULL);
199 ZERO_STRUCT(act);
200 act.sa_sigaction = signal_handler;
201 act.sa_flags = SA_SIGINFO;
202 sigemptyset( &act.sa_mask );
203 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
204 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
207 /* the signal can start off blocked due to a bug in bash */
208 BlockSignals(False, RT_SIGNAL_AIO);
211 /****************************************************************************
212 Set up an aio request from a SMBreadX call.
213 *****************************************************************************/
215 bool schedule_aio_read_and_X(connection_struct *conn,
216 struct smb_request *req,
217 files_struct *fsp, SMB_OFF_T startpos,
218 size_t smb_maxcnt)
220 struct aio_extra *aio_ex;
221 SMB_STRUCT_AIOCB *a;
222 size_t bufsize;
223 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
225 if (fsp->base_fsp != NULL) {
226 /* No AIO on streams yet */
227 DEBUG(10, ("AIO on streams not yet supported\n"));
228 return false;
231 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
232 && !SMB_VFS_AIO_FORCE(fsp)) {
233 /* Too small a read for aio request. */
234 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
235 "for minimum aio_read of %u\n",
236 (unsigned int)smb_maxcnt,
237 (unsigned int)min_aio_read_size ));
238 return False;
241 /* Only do this on non-chained and non-chaining reads not using the
242 * write cache. */
243 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
244 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
245 return False;
248 if (outstanding_aio_calls >= aio_pending_size) {
249 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
250 "activities outstanding.\n",
251 outstanding_aio_calls ));
252 return False;
255 /* The following is safe from integer wrap as we've already checked
256 smb_maxcnt is 128k or less. Wct is 12 for read replies */
258 bufsize = smb_size + 12 * 2 + smb_maxcnt;
260 if ((aio_ex = create_aio_ex_read(fsp, bufsize, req->mid)) == NULL) {
261 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
262 return False;
265 construct_reply_common((char *)req->inbuf, aio_ex->outbuf);
266 srv_set_message(aio_ex->outbuf, 12, 0, True);
267 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
269 a = &aio_ex->acb;
271 /* Now set up the aio record for the read call. */
273 a->aio_fildes = fsp->fh->fd;
274 a->aio_buf = smb_buf(aio_ex->outbuf);
275 a->aio_nbytes = smb_maxcnt;
276 a->aio_offset = startpos;
277 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
278 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
279 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
281 become_root();
282 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
283 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
284 "Error %s\n", strerror(errno) ));
285 delete_aio_ex(aio_ex);
286 unbecome_root();
287 return False;
289 unbecome_root();
291 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
292 "offset %.0f, len = %u (mid = %u)\n",
293 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
294 (unsigned int)aio_ex->mid ));
296 srv_defer_sign_response(aio_ex->mid);
297 outstanding_aio_calls++;
298 return True;
301 /****************************************************************************
302 Set up an aio request from a SMBwriteX call.
303 *****************************************************************************/
305 bool schedule_aio_write_and_X(connection_struct *conn,
306 struct smb_request *req,
307 files_struct *fsp, char *data,
308 SMB_OFF_T startpos,
309 size_t numtowrite)
311 struct aio_extra *aio_ex;
312 SMB_STRUCT_AIOCB *a;
313 size_t inbufsize, outbufsize;
314 bool write_through = BITSETW(req->inbuf+smb_vwv7,0);
315 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
317 if (fsp->base_fsp != NULL) {
318 /* No AIO on streams yet */
319 DEBUG(10, ("AIO on streams not yet supported\n"));
320 return false;
323 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
324 && !SMB_VFS_AIO_FORCE(fsp)) {
325 /* Too small a write for aio request. */
326 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
327 "small for minimum aio_write of %u\n",
328 (unsigned int)numtowrite,
329 (unsigned int)min_aio_write_size ));
330 return False;
333 /* Only do this on non-chained and non-chaining reads not using the
334 * write cache. */
335 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
336 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
337 return False;
340 if (outstanding_aio_calls >= aio_pending_size) {
341 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
342 "activities outstanding.\n",
343 outstanding_aio_calls ));
344 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
345 "aio_write for file %s, offset %.0f, len = %u "
346 "(mid = %u)\n",
347 fsp->fsp_name, (double)startpos,
348 (unsigned int)numtowrite,
349 (unsigned int)req->mid ));
350 return False;
353 inbufsize = smb_len(req->inbuf) + 4;
354 reply_outbuf(req, 6, 0);
355 outbufsize = smb_len(req->outbuf) + 4;
356 if (!(aio_ex = create_aio_ex_write(fsp, inbufsize, outbufsize,
357 req->mid))) {
358 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
359 return False;
362 /* Copy the SMB header already setup in outbuf. */
363 memcpy(aio_ex->inbuf, req->inbuf, inbufsize);
365 /* Copy the SMB header already setup in outbuf. */
366 memcpy(aio_ex->outbuf, req->outbuf, outbufsize);
367 TALLOC_FREE(req->outbuf);
368 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
370 a = &aio_ex->acb;
372 /* Now set up the aio record for the write call. */
374 a->aio_fildes = fsp->fh->fd;
375 a->aio_buf = aio_ex->inbuf + (PTR_DIFF(data, req->inbuf));
376 a->aio_nbytes = numtowrite;
377 a->aio_offset = startpos;
378 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
379 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
380 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
382 become_root();
383 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
384 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
385 "Error %s\n", strerror(errno) ));
386 delete_aio_ex(aio_ex);
387 unbecome_root();
388 return False;
390 unbecome_root();
392 release_level_2_oplocks_on_change(fsp);
394 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
395 && fsp->aio_write_behind) {
396 /* Lie to the client and immediately claim we finished the
397 * write. */
398 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
399 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
400 show_msg(aio_ex->outbuf);
401 if (!srv_send_smb(smbd_server_fd(),aio_ex->outbuf,
402 IS_CONN_ENCRYPTED(fsp->conn))) {
403 exit_server_cleanly("handle_aio_write: srv_send_smb "
404 "failed.");
406 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
407 "behind for file %s\n", fsp->fsp_name ));
408 } else {
409 srv_defer_sign_response(aio_ex->mid);
411 outstanding_aio_calls++;
413 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
414 "%s, offset %.0f, len = %u (mid = %u) "
415 "outstanding_aio_calls = %d\n",
416 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
417 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
419 return True;
423 /****************************************************************************
424 Complete the read and return the data or error back to the client.
425 Returns errno or zero if all ok.
426 *****************************************************************************/
428 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
430 int outsize;
431 char *outbuf = aio_ex->outbuf;
432 char *data = smb_buf(outbuf);
433 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
435 if (nread < 0) {
436 /* We're relying here on the fact that if the fd is
437 closed then the aio will complete and aio_return
438 will return an error. Hopefully this is
439 true.... JRA. */
441 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
442 "Error = %s\n",
443 aio_ex->fsp->fsp_name, (int)nread, strerror(errcode) ));
445 ERROR_NT(map_nt_error_from_unix(errcode));
446 outsize = srv_set_message(outbuf,0,0,true);
447 } else {
448 outsize = srv_set_message(outbuf,12,nread,False);
449 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
450 SSVAL(outbuf,smb_vwv5,nread);
451 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
452 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
453 SSVAL(smb_buf(outbuf),-2,nread);
455 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
456 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
458 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
459 "nread=%d\n",
460 aio_ex->fsp->fsp_name,
461 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
464 smb_setlen(outbuf,outsize - 4);
465 show_msg(outbuf);
466 if (!srv_send_smb(smbd_server_fd(),outbuf,
467 IS_CONN_ENCRYPTED(aio_ex->fsp->conn))) {
468 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
469 "failed.");
472 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
473 "for file %s, offset %.0f, len = %u\n",
474 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
475 (unsigned int)nread ));
477 return errcode;
480 /****************************************************************************
481 Complete the write and return the data or error back to the client.
482 Returns errno or zero if all ok.
483 *****************************************************************************/
485 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
487 files_struct *fsp = aio_ex->fsp;
488 char *outbuf = aio_ex->outbuf;
489 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
490 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
492 if (fsp->aio_write_behind) {
493 if (nwritten != numtowrite) {
494 if (nwritten == -1) {
495 DEBUG(5,("handle_aio_write_complete: "
496 "aio_write_behind failed ! File %s "
497 "is corrupt ! Error %s\n",
498 fsp->fsp_name, strerror(errcode) ));
499 } else {
500 DEBUG(0,("handle_aio_write_complete: "
501 "aio_write_behind failed ! File %s "
502 "is corrupt ! Wanted %u bytes but "
503 "only wrote %d\n", fsp->fsp_name,
504 (unsigned int)numtowrite,
505 (int)nwritten ));
506 errcode = EIO;
508 } else {
509 DEBUG(10,("handle_aio_write_complete: "
510 "aio_write_behind completed for file %s\n",
511 fsp->fsp_name ));
513 /* TODO: should no return 0 in case of an error !!! */
514 return 0;
517 /* We don't need outsize or set_message here as we've already set the
518 fixed size length when we set up the aio call. */
520 if(nwritten == -1) {
521 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
522 "nwritten == %d. Error = %s\n",
523 fsp->fsp_name, (unsigned int)numtowrite,
524 (int)nwritten, strerror(errno) ));
526 ERROR_NT(map_nt_error_from_unix(errcode));
527 srv_set_message(outbuf,0,0,true);
528 } else {
529 bool write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
530 NTSTATUS status;
532 SSVAL(outbuf,smb_vwv2,nwritten);
533 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
534 if (nwritten < (ssize_t)numtowrite) {
535 SCVAL(outbuf,smb_rcls,ERRHRD);
536 SSVAL(outbuf,smb_err,ERRdiskfull);
539 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
540 fsp->fnum, (int)numtowrite, (int)nwritten));
541 status = sync_file(fsp->conn,fsp, write_through);
542 if (!NT_STATUS_IS_OK(status)) {
543 errcode = errno;
544 ERROR_BOTH(map_nt_error_from_unix(errcode),
545 ERRHRD, ERRdiskfull);
546 srv_set_message(outbuf,0,0,true);
547 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
548 fsp->fsp_name, nt_errstr(status) ));
551 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
554 show_msg(outbuf);
555 if (!srv_send_smb(smbd_server_fd(),outbuf,IS_CONN_ENCRYPTED(fsp->conn))) {
556 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
559 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
560 "for file %s, offset %.0f, requested %u, written = %u\n",
561 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
562 (unsigned int)numtowrite, (unsigned int)nwritten ));
564 return errcode;
567 /****************************************************************************
568 Handle any aio completion. Returns True if finished (and sets *perr if err
569 was non-zero), False if not.
570 *****************************************************************************/
572 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
574 int err;
576 if(!aio_ex) {
577 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
578 return false;
581 /* Ensure the operation has really completed. */
582 err = SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb);
583 if (err == EINPROGRESS) {
584 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
585 "process for file %s\n",
586 aio_ex->mid, aio_ex->fsp->fsp_name ));
587 return False;
588 } else if (err == ECANCELED) {
589 /* If error is ECANCELED then don't return anything to the
590 * client. */
591 DEBUG(10,( "handle_aio_completed: operation mid %u"
592 " canceled\n", aio_ex->mid));
593 srv_cancel_sign_response(aio_ex->mid, false);
594 return True;
598 if (aio_ex->read_req) {
599 err = handle_aio_read_complete(aio_ex, err);
600 } else {
601 err = handle_aio_write_complete(aio_ex, err);
604 if (err) {
605 *perr = err; /* Only save non-zero errors. */
608 return True;
611 /****************************************************************************
612 Handle any aio completion inline.
613 Returns non-zero errno if fail or zero if all ok.
614 *****************************************************************************/
616 int process_aio_queue(void)
618 int i;
619 int ret = 0;
621 BlockSignals(True, RT_SIGNAL_AIO);
623 DEBUG(10,("process_aio_queue: signals_received = %d\n",
624 (int)signals_received));
625 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
626 outstanding_aio_calls));
628 if (!signals_received) {
629 BlockSignals(False, RT_SIGNAL_AIO);
630 return 0;
633 /* Drain all the complete aio_reads. */
634 for (i = 0; i < signals_received; i++) {
635 uint16 mid = aio_pending_array[i];
636 files_struct *fsp = NULL;
637 struct aio_extra *aio_ex = find_aio_ex(mid);
639 if (!aio_ex) {
640 DEBUG(3,("process_aio_queue: Can't find record to "
641 "match mid %u.\n", (unsigned int)mid));
642 srv_cancel_sign_response(mid, false);
643 continue;
646 fsp = aio_ex->fsp;
647 if (fsp == NULL) {
648 /* file was closed whilst I/O was outstanding. Just
649 * ignore. */
650 DEBUG( 3,( "process_aio_queue: file closed whilst "
651 "aio outstanding.\n"));
652 srv_cancel_sign_response(mid, false);
653 continue;
656 if (!handle_aio_completed(aio_ex, &ret)) {
657 continue;
660 delete_aio_ex(aio_ex);
663 outstanding_aio_calls -= signals_received;
664 signals_received = 0;
665 BlockSignals(False, RT_SIGNAL_AIO);
666 return ret;
669 /****************************************************************************
670 We're doing write behind and the client closed the file. Wait up to 30
671 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
672 completed, errno to return if not.
673 *****************************************************************************/
675 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
677 int wait_for_aio_completion(files_struct *fsp)
679 struct aio_extra *aio_ex;
680 const SMB_STRUCT_AIOCB **aiocb_list;
681 int aio_completion_count = 0;
682 time_t start_time = time(NULL);
683 int seconds_left;
685 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
686 seconds_left >= 0;) {
687 int err = 0;
688 int i;
689 struct timespec ts;
691 aio_completion_count = 0;
692 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
693 if (aio_ex->fsp == fsp) {
694 aio_completion_count++;
698 if (!aio_completion_count) {
699 return 0;
702 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
703 "to complete.\n", aio_completion_count ));
705 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
706 aio_completion_count);
707 if (!aiocb_list) {
708 return ENOMEM;
711 for( i = 0, aio_ex = aio_list_head;
712 aio_ex;
713 aio_ex = aio_ex->next) {
714 if (aio_ex->fsp == fsp) {
715 aiocb_list[i++] = &aio_ex->acb;
719 /* Now wait up to seconds_left for completion. */
720 ts.tv_sec = seconds_left;
721 ts.tv_nsec = 0;
723 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
724 "of %d seconds.\n",
725 aio_completion_count, seconds_left ));
727 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
728 aio_completion_count, &ts);
730 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
731 "errno = %s\n", err, strerror(errno) ));
733 if (err == -1 && errno == EAGAIN) {
734 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
735 "out waiting for %d events after a wait of "
736 "%d seconds\n", aio_completion_count,
737 seconds_left));
738 /* Timeout. */
739 cancel_aio_by_fsp(fsp);
740 SAFE_FREE(aiocb_list);
741 return EIO;
744 /* One or more events might have completed - process them if
745 * so. */
746 for( i = 0; i < aio_completion_count; i++) {
747 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
749 aio_ex = find_aio_ex(mid);
751 if (!aio_ex) {
752 DEBUG(0, ("wait_for_aio_completion: mid %u "
753 "doesn't match an aio record\n",
754 (unsigned int)mid ));
755 continue;
758 if (!handle_aio_completed(aio_ex, &err)) {
759 continue;
761 delete_aio_ex(aio_ex);
764 SAFE_FREE(aiocb_list);
765 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
766 - (time(NULL) - start_time);
769 /* We timed out - we don't know why. Return ret if already an error,
770 * else EIO. */
771 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
772 "for %d events\n",
773 aio_completion_count));
775 return EIO;
778 /****************************************************************************
779 Cancel any outstanding aio requests. The client doesn't care about the reply.
780 *****************************************************************************/
782 void cancel_aio_by_fsp(files_struct *fsp)
784 struct aio_extra *aio_ex;
786 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
787 if (aio_ex->fsp == fsp) {
788 /* Don't delete the aio_extra record as we may have
789 completed and don't yet know it. Just do the
790 aio_cancel call and return. */
791 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
792 aio_ex->fsp = NULL; /* fsp will be closed when we
793 * return. */
798 #else
799 bool aio_finished(void)
801 return False;
804 void initialize_async_io_handler(void)
808 int process_aio_queue(void)
810 return False;
813 bool schedule_aio_read_and_X(connection_struct *conn,
814 struct smb_request *req,
815 files_struct *fsp, SMB_OFF_T startpos,
816 size_t smb_maxcnt)
818 return False;
821 bool schedule_aio_write_and_X(connection_struct *conn,
822 struct smb_request *req,
823 files_struct *fsp, char *data,
824 SMB_OFF_T startpos,
825 size_t numtowrite)
827 return False;
830 void cancel_aio_by_fsp(files_struct *fsp)
834 int wait_for_aio_completion(files_struct *fsp)
836 return ENOSYS;
838 #endif