Fix bug 5686 - libsmbclient segfaults with more than one SMBCCTX.
[Samba/gebeck_regimport.git] / source3 / smbd / aio.c
blob2889e3c13f82a0b04e12aa86e8a0e5520b03d82b
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 #define RT_SIGNAL_AIO (SIGRTMIN+3)
28 #endif
30 /****************************************************************************
31 The buffer we keep around whilst an aio request is in process.
32 *****************************************************************************/
34 struct aio_extra {
35 struct aio_extra *next, *prev;
36 SMB_STRUCT_AIOCB acb;
37 files_struct *fsp;
38 bool read_req;
39 uint16 mid;
40 char *inbuf;
41 char *outbuf;
44 static struct aio_extra *aio_list_head;
46 /****************************************************************************
47 Create the extended aio struct we must keep around for the lifetime
48 of the aio_read call.
49 *****************************************************************************/
51 static struct aio_extra *create_aio_ex_read(files_struct *fsp, size_t buflen,
52 uint16 mid)
54 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
56 if (!aio_ex) {
57 return NULL;
59 ZERO_STRUCTP(aio_ex);
60 /* The output buffer stored in the aio_ex is the start of
61 the smb return buffer. The buffer used in the acb
62 is the start of the reply data portion of that buffer. */
63 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, buflen);
64 if (!aio_ex->outbuf) {
65 SAFE_FREE(aio_ex);
66 return NULL;
68 DLIST_ADD(aio_list_head, aio_ex);
69 aio_ex->fsp = fsp;
70 aio_ex->read_req = True;
71 aio_ex->mid = mid;
72 return aio_ex;
75 /****************************************************************************
76 Create the extended aio struct we must keep around for the lifetime
77 of the aio_write call.
78 *****************************************************************************/
80 static struct aio_extra *create_aio_ex_write(files_struct *fsp,
81 size_t inbuflen,
82 size_t outbuflen,
83 uint16 mid)
85 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
87 if (!aio_ex) {
88 return NULL;
90 ZERO_STRUCTP(aio_ex);
92 /* We need space for an output reply of outbuflen bytes. */
93 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
94 if (!aio_ex->outbuf) {
95 SAFE_FREE(aio_ex);
96 return NULL;
99 if (!(aio_ex->inbuf = SMB_MALLOC_ARRAY(char, inbuflen))) {
100 SAFE_FREE(aio_ex->outbuf);
101 SAFE_FREE(aio_ex);
102 return NULL;
105 DLIST_ADD(aio_list_head, aio_ex);
106 aio_ex->fsp = fsp;
107 aio_ex->read_req = False;
108 aio_ex->mid = mid;
109 return aio_ex;
112 /****************************************************************************
113 Delete the extended aio struct.
114 *****************************************************************************/
116 static void delete_aio_ex(struct aio_extra *aio_ex)
118 DLIST_REMOVE(aio_list_head, aio_ex);
119 SAFE_FREE(aio_ex->inbuf);
120 SAFE_FREE(aio_ex->outbuf);
121 SAFE_FREE(aio_ex);
124 /****************************************************************************
125 Given the aiocb struct find the extended aio struct containing it.
126 *****************************************************************************/
128 static struct aio_extra *find_aio_ex(uint16 mid)
130 struct aio_extra *p;
132 for( p = aio_list_head; p; p = p->next) {
133 if (mid == p->mid) {
134 return p;
137 return NULL;
140 /****************************************************************************
141 We can have these many aio buffers in flight.
142 *****************************************************************************/
144 static int aio_pending_size;
145 static sig_atomic_t signals_received;
146 static int outstanding_aio_calls;
147 static uint16 *aio_pending_array;
149 /****************************************************************************
150 Signal handler when an aio request completes.
151 *****************************************************************************/
153 void aio_request_done(uint16_t mid)
155 if (signals_received < aio_pending_size) {
156 aio_pending_array[signals_received] = mid;
157 signals_received++;
159 /* Else signal is lost. */
162 static void signal_handler(int sig, siginfo_t *info, void *unused)
164 aio_request_done(info->si_value.sival_int);
165 sys_select_signal(RT_SIGNAL_AIO);
168 /****************************************************************************
169 Is there a signal waiting ?
170 *****************************************************************************/
172 bool aio_finished(void)
174 return (signals_received != 0);
177 /****************************************************************************
178 Initialize the signal handler for aio read/write.
179 *****************************************************************************/
181 void initialize_async_io_handler(void)
183 struct sigaction act;
185 aio_pending_size = lp_maxmux();
186 aio_pending_array = SMB_MALLOC_ARRAY(uint16, aio_pending_size);
187 SMB_ASSERT(aio_pending_array != NULL);
189 ZERO_STRUCT(act);
190 act.sa_sigaction = signal_handler;
191 act.sa_flags = SA_SIGINFO;
192 sigemptyset( &act.sa_mask );
193 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
194 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
197 /* the signal can start off blocked due to a bug in bash */
198 BlockSignals(False, RT_SIGNAL_AIO);
201 /****************************************************************************
202 Set up an aio request from a SMBreadX call.
203 *****************************************************************************/
205 bool schedule_aio_read_and_X(connection_struct *conn,
206 struct smb_request *req,
207 files_struct *fsp, SMB_OFF_T startpos,
208 size_t smb_maxcnt)
210 struct aio_extra *aio_ex;
211 SMB_STRUCT_AIOCB *a;
212 size_t bufsize;
213 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
215 if (fsp->base_fsp != NULL) {
216 /* No AIO on streams yet */
217 DEBUG(10, ("AIO on streams not yet supported\n"));
218 return false;
221 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
222 && !SMB_VFS_AIO_FORCE(fsp)) {
223 /* Too small a read for aio request. */
224 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
225 "for minimum aio_read of %u\n",
226 (unsigned int)smb_maxcnt,
227 (unsigned int)min_aio_read_size ));
228 return False;
231 /* Only do this on non-chained and non-chaining reads not using the
232 * write cache. */
233 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
234 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
235 return False;
238 if (outstanding_aio_calls >= aio_pending_size) {
239 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
240 "activities outstanding.\n",
241 outstanding_aio_calls ));
242 return False;
245 /* The following is safe from integer wrap as we've already checked
246 smb_maxcnt is 128k or less. Wct is 12 for read replies */
248 bufsize = smb_size + 12 * 2 + smb_maxcnt;
250 if ((aio_ex = create_aio_ex_read(fsp, bufsize, req->mid)) == NULL) {
251 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
252 return False;
255 construct_reply_common((char *)req->inbuf, aio_ex->outbuf);
256 srv_set_message(aio_ex->outbuf, 12, 0, True);
257 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
259 a = &aio_ex->acb;
261 /* Now set up the aio record for the read call. */
263 a->aio_fildes = fsp->fh->fd;
264 a->aio_buf = smb_buf(aio_ex->outbuf);
265 a->aio_nbytes = smb_maxcnt;
266 a->aio_offset = startpos;
267 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
268 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
269 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
271 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
272 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
273 "Error %s\n", strerror(errno) ));
274 delete_aio_ex(aio_ex);
275 return False;
278 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
279 "offset %.0f, len = %u (mid = %u)\n",
280 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
281 (unsigned int)aio_ex->mid ));
283 srv_defer_sign_response(aio_ex->mid);
284 outstanding_aio_calls++;
285 return True;
288 /****************************************************************************
289 Set up an aio request from a SMBwriteX call.
290 *****************************************************************************/
292 bool schedule_aio_write_and_X(connection_struct *conn,
293 struct smb_request *req,
294 files_struct *fsp, char *data,
295 SMB_OFF_T startpos,
296 size_t numtowrite)
298 struct aio_extra *aio_ex;
299 SMB_STRUCT_AIOCB *a;
300 size_t inbufsize, outbufsize;
301 bool write_through = BITSETW(req->inbuf+smb_vwv7,0);
302 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
304 if (fsp->base_fsp != NULL) {
305 /* No AIO on streams yet */
306 DEBUG(10, ("AIO on streams not yet supported\n"));
307 return false;
310 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
311 && !SMB_VFS_AIO_FORCE(fsp)) {
312 /* Too small a write for aio request. */
313 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
314 "small for minimum aio_write of %u\n",
315 (unsigned int)numtowrite,
316 (unsigned int)min_aio_write_size ));
317 return False;
320 /* Only do this on non-chained and non-chaining reads not using the
321 * write cache. */
322 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
323 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
324 return False;
327 if (outstanding_aio_calls >= aio_pending_size) {
328 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
329 "activities outstanding.\n",
330 outstanding_aio_calls ));
331 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
332 "aio_write for file %s, offset %.0f, len = %u "
333 "(mid = %u)\n",
334 fsp->fsp_name, (double)startpos,
335 (unsigned int)numtowrite,
336 (unsigned int)req->mid ));
337 return False;
340 inbufsize = smb_len(req->inbuf) + 4;
341 reply_outbuf(req, 6, 0);
342 outbufsize = smb_len(req->outbuf) + 4;
343 if (!(aio_ex = create_aio_ex_write(fsp, inbufsize, outbufsize,
344 req->mid))) {
345 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
346 return False;
349 /* Copy the SMB header already setup in outbuf. */
350 memcpy(aio_ex->inbuf, req->inbuf, inbufsize);
352 /* Copy the SMB header already setup in outbuf. */
353 memcpy(aio_ex->outbuf, req->outbuf, outbufsize);
354 TALLOC_FREE(req->outbuf);
355 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
357 a = &aio_ex->acb;
359 /* Now set up the aio record for the write call. */
361 a->aio_fildes = fsp->fh->fd;
362 a->aio_buf = aio_ex->inbuf + (PTR_DIFF(data, req->inbuf));
363 a->aio_nbytes = numtowrite;
364 a->aio_offset = startpos;
365 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
366 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
367 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
369 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
370 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
371 "Error %s\n", strerror(errno) ));
372 delete_aio_ex(aio_ex);
373 return False;
376 release_level_2_oplocks_on_change(fsp);
378 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
379 && fsp->aio_write_behind) {
380 /* Lie to the client and immediately claim we finished the
381 * write. */
382 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
383 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
384 show_msg(aio_ex->outbuf);
385 if (!srv_send_smb(smbd_server_fd(),aio_ex->outbuf,
386 IS_CONN_ENCRYPTED(fsp->conn))) {
387 exit_server_cleanly("handle_aio_write: srv_send_smb "
388 "failed.");
390 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
391 "behind for file %s\n", fsp->fsp_name ));
392 } else {
393 srv_defer_sign_response(aio_ex->mid);
395 outstanding_aio_calls++;
397 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
398 "%s, offset %.0f, len = %u (mid = %u) "
399 "outstanding_aio_calls = %d\n",
400 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
401 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
403 return True;
407 /****************************************************************************
408 Complete the read and return the data or error back to the client.
409 Returns errno or zero if all ok.
410 *****************************************************************************/
412 static int handle_aio_read_complete(struct aio_extra *aio_ex)
414 int ret = 0;
415 int outsize;
416 char *outbuf = aio_ex->outbuf;
417 char *data = smb_buf(outbuf);
418 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
420 if (nread < 0) {
421 /* We're relying here on the fact that if the fd is
422 closed then the aio will complete and aio_return
423 will return an error. Hopefully this is
424 true.... JRA. */
426 /* If errno is ECANCELED then don't return anything to the
427 * client. */
428 if (errno == ECANCELED) {
429 srv_cancel_sign_response(aio_ex->mid);
430 return 0;
433 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
434 "Error = %s\n",
435 aio_ex->fsp->fsp_name, strerror(errno) ));
437 ret = errno;
438 ERROR_NT(map_nt_error_from_unix(ret));
439 outsize = srv_set_message(outbuf,0,0,true);
440 } else {
441 outsize = srv_set_message(outbuf,12,nread,False);
442 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
443 SSVAL(outbuf,smb_vwv5,nread);
444 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
445 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
446 SSVAL(smb_buf(outbuf),-2,nread);
448 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
449 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
451 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
452 "nread=%d\n",
453 aio_ex->fsp->fsp_name,
454 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
457 smb_setlen(outbuf,outsize - 4);
458 show_msg(outbuf);
459 if (!srv_send_smb(smbd_server_fd(),outbuf,
460 IS_CONN_ENCRYPTED(aio_ex->fsp->conn))) {
461 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
462 "failed.");
465 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
466 "for file %s, offset %.0f, len = %u\n",
467 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
468 (unsigned int)nread ));
470 return ret;
473 /****************************************************************************
474 Complete the write and return the data or error back to the client.
475 Returns errno or zero if all ok.
476 *****************************************************************************/
478 static int handle_aio_write_complete(struct aio_extra *aio_ex)
480 int ret = 0;
481 files_struct *fsp = aio_ex->fsp;
482 char *outbuf = aio_ex->outbuf;
483 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
484 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
486 if (fsp->aio_write_behind) {
487 if (nwritten != numtowrite) {
488 if (nwritten == -1) {
489 DEBUG(5,("handle_aio_write_complete: "
490 "aio_write_behind failed ! File %s "
491 "is corrupt ! Error %s\n",
492 fsp->fsp_name, strerror(errno) ));
493 ret = errno;
494 } else {
495 DEBUG(0,("handle_aio_write_complete: "
496 "aio_write_behind failed ! File %s "
497 "is corrupt ! Wanted %u bytes but "
498 "only wrote %d\n", fsp->fsp_name,
499 (unsigned int)numtowrite,
500 (int)nwritten ));
501 ret = EIO;
503 } else {
504 DEBUG(10,("handle_aio_write_complete: "
505 "aio_write_behind completed for file %s\n",
506 fsp->fsp_name ));
508 return 0;
511 /* We don't need outsize or set_message here as we've already set the
512 fixed size length when we set up the aio call. */
514 if(nwritten == -1) {
515 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
516 "nwritten == %d. Error = %s\n",
517 fsp->fsp_name, (unsigned int)numtowrite,
518 (int)nwritten, strerror(errno) ));
520 /* If errno is ECANCELED then don't return anything to the
521 * client. */
522 if (errno == ECANCELED) {
523 srv_cancel_sign_response(aio_ex->mid);
524 return 0;
527 ret = errno;
528 ERROR_BOTH(map_nt_error_from_unix(ret), ERRHRD, ERRdiskfull);
529 srv_set_message(outbuf,0,0,true);
530 } else {
531 bool write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
532 NTSTATUS status;
534 SSVAL(outbuf,smb_vwv2,nwritten);
535 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
536 if (nwritten < (ssize_t)numtowrite) {
537 SCVAL(outbuf,smb_rcls,ERRHRD);
538 SSVAL(outbuf,smb_err,ERRdiskfull);
541 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
542 fsp->fnum, (int)numtowrite, (int)nwritten));
543 status = sync_file(fsp->conn,fsp, write_through);
544 if (!NT_STATUS_IS_OK(status)) {
545 ret = errno;
546 ERROR_BOTH(map_nt_error_from_unix(ret),
547 ERRHRD, ERRdiskfull);
548 srv_set_message(outbuf,0,0,true);
549 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
550 fsp->fsp_name, nt_errstr(status) ));
553 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
556 show_msg(outbuf);
557 if (!srv_send_smb(smbd_server_fd(),outbuf,IS_CONN_ENCRYPTED(fsp->conn))) {
558 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
561 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
562 "for file %s, offset %.0f, requested %u, written = %u\n",
563 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
564 (unsigned int)numtowrite, (unsigned int)nwritten ));
566 return ret;
569 /****************************************************************************
570 Handle any aio completion. Returns True if finished (and sets *perr if err
571 was non-zero), False if not.
572 *****************************************************************************/
574 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
576 int err;
578 /* Ensure the operation has really completed. */
579 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
580 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
581 "process for file %s\n",
582 aio_ex->mid, aio_ex->fsp->fsp_name ));
583 return False;
586 if (aio_ex->read_req) {
587 err = handle_aio_read_complete(aio_ex);
588 } else {
589 err = handle_aio_write_complete(aio_ex);
592 if (err) {
593 *perr = err; /* Only save non-zero errors. */
596 return True;
599 /****************************************************************************
600 Handle any aio completion inline.
601 Returns non-zero errno if fail or zero if all ok.
602 *****************************************************************************/
604 int process_aio_queue(void)
606 int i;
607 int ret = 0;
609 BlockSignals(True, RT_SIGNAL_AIO);
611 DEBUG(10,("process_aio_queue: signals_received = %d\n",
612 (int)signals_received));
613 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
614 outstanding_aio_calls));
616 if (!signals_received) {
617 BlockSignals(False, RT_SIGNAL_AIO);
618 return 0;
621 /* Drain all the complete aio_reads. */
622 for (i = 0; i < signals_received; i++) {
623 uint16 mid = aio_pending_array[i];
624 files_struct *fsp = NULL;
625 struct aio_extra *aio_ex = find_aio_ex(mid);
627 if (!aio_ex) {
628 DEBUG(3,("process_aio_queue: Can't find record to "
629 "match mid %u.\n", (unsigned int)mid));
630 srv_cancel_sign_response(mid);
631 continue;
634 fsp = aio_ex->fsp;
635 if (fsp == NULL) {
636 /* file was closed whilst I/O was outstanding. Just
637 * ignore. */
638 DEBUG( 3,( "process_aio_queue: file closed whilst "
639 "aio outstanding.\n"));
640 srv_cancel_sign_response(mid);
641 continue;
644 if (!handle_aio_completed(aio_ex, &ret)) {
645 continue;
648 delete_aio_ex(aio_ex);
651 outstanding_aio_calls -= signals_received;
652 signals_received = 0;
653 BlockSignals(False, RT_SIGNAL_AIO);
654 return ret;
657 /****************************************************************************
658 We're doing write behind and the client closed the file. Wait up to 30
659 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
660 completed, errno to return if not.
661 *****************************************************************************/
663 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
665 int wait_for_aio_completion(files_struct *fsp)
667 struct aio_extra *aio_ex;
668 const SMB_STRUCT_AIOCB **aiocb_list;
669 int aio_completion_count = 0;
670 time_t start_time = time(NULL);
671 int seconds_left;
673 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
674 seconds_left >= 0;) {
675 int err = 0;
676 int i;
677 struct timespec ts;
679 aio_completion_count = 0;
680 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
681 if (aio_ex->fsp == fsp) {
682 aio_completion_count++;
686 if (!aio_completion_count) {
687 return 0;
690 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
691 "to complete.\n", aio_completion_count ));
693 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
694 aio_completion_count);
695 if (!aiocb_list) {
696 return ENOMEM;
699 for( i = 0, aio_ex = aio_list_head;
700 aio_ex;
701 aio_ex = aio_ex->next) {
702 if (aio_ex->fsp == fsp) {
703 aiocb_list[i++] = &aio_ex->acb;
707 /* Now wait up to seconds_left for completion. */
708 ts.tv_sec = seconds_left;
709 ts.tv_nsec = 0;
711 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
712 "of %d seconds.\n",
713 aio_completion_count, seconds_left ));
715 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
716 aio_completion_count, &ts);
718 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
719 "errno = %s\n", err, strerror(errno) ));
721 if (err == -1 && errno == EAGAIN) {
722 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
723 "out waiting for %d events after a wait of "
724 "%d seconds\n", aio_completion_count,
725 seconds_left));
726 /* Timeout. */
727 cancel_aio_by_fsp(fsp);
728 SAFE_FREE(aiocb_list);
729 return EIO;
732 /* One or more events might have completed - process them if
733 * so. */
734 for( i = 0; i < aio_completion_count; i++) {
735 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
737 aio_ex = find_aio_ex(mid);
739 if (!aio_ex) {
740 DEBUG(0, ("wait_for_aio_completion: mid %u "
741 "doesn't match an aio record\n",
742 (unsigned int)mid ));
743 continue;
746 if (!handle_aio_completed(aio_ex, &err)) {
747 continue;
749 delete_aio_ex(aio_ex);
752 SAFE_FREE(aiocb_list);
753 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
754 - (time(NULL) - start_time);
757 /* We timed out - we don't know why. Return ret if already an error,
758 * else EIO. */
759 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
760 "for %d events\n",
761 aio_completion_count));
763 return EIO;
766 /****************************************************************************
767 Cancel any outstanding aio requests. The client doesn't care about the reply.
768 *****************************************************************************/
770 void cancel_aio_by_fsp(files_struct *fsp)
772 struct aio_extra *aio_ex;
774 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
775 if (aio_ex->fsp == fsp) {
776 /* Don't delete the aio_extra record as we may have
777 completed and don't yet know it. Just do the
778 aio_cancel call and return. */
779 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
780 aio_ex->fsp = NULL; /* fsp will be closed when we
781 * return. */
786 #else
787 bool aio_finished(void)
789 return False;
792 void initialize_async_io_handler(void)
796 int process_aio_queue(void)
798 return False;
801 bool schedule_aio_read_and_X(connection_struct *conn,
802 struct smb_request *req,
803 files_struct *fsp, SMB_OFF_T startpos,
804 size_t smb_maxcnt)
806 return False;
809 bool schedule_aio_write_and_X(connection_struct *conn,
810 struct smb_request *req,
811 files_struct *fsp, char *data,
812 SMB_OFF_T startpos,
813 size_t numtowrite)
815 return False;
818 void cancel_aio_by_fsp(files_struct *fsp)
822 int wait_for_aio_completion(files_struct *fsp)
824 return ENOSYS;
826 #endif