r8402: allow to build with --enable-aio-support.
[tprouty/samba.git] / source / smbd / aio.c
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.
6
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 2 of the License, or
10    (at your option) any later version.
11
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.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #if defined(WITH_AIO)
25
26 /* The signal we'll use to signify aio done. */
27 #ifndef RT_SIGNAL_AIO
28 #define RT_SIGNAL_AIO (SIGRTMIN+3)
29 #endif
30
31 /****************************************************************************
32  The buffer we keep around whilst an aio request is in process.
33 *****************************************************************************/
34
35 struct aio_extra {
36         struct aio_extra *next, *prev;
37         SMB_STRUCT_AIOCB acb;
38         files_struct *fsp;
39         BOOL read_req;
40         uint16 mid;
41         char *inbuf;
42         char *outbuf;
43 };
44
45 static struct aio_extra *aio_list_head;
46
47 /****************************************************************************
48  Create the extended aio struct we must keep around for the lifetime
49  of the aio_read call.
50 *****************************************************************************/
51
52 static struct aio_extra *create_aio_ex_read(files_struct *fsp, size_t buflen, uint16 mid)
53 {
54         struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
55
56         if (!aio_ex) {
57                 return NULL;
58         }
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;
67         }
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;
73 }
74
75 /****************************************************************************
76  Create the extended aio struct we must keep around for the lifetime
77  of the aio_write call.
78 *****************************************************************************/
79
80 static struct aio_extra *create_aio_ex_write(files_struct *fsp, size_t outbuflen, uint16 mid)
81 {
82         struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
83
84         if (!aio_ex) {
85                 return NULL;
86         }
87         ZERO_STRUCTP(aio_ex);
88
89         /* We need space for an output reply of outbuflen bytes. */
90         aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
91         if (!aio_ex->outbuf) {
92                 SAFE_FREE(aio_ex);
93                 return NULL;
94         }
95         /* Steal the input buffer containing the write data from the main SMB call. */
96         /* We must re-allocate a new one here. */
97         if (NewInBuffer(&aio_ex->inbuf) == NULL) {
98                 SAFE_FREE(aio_ex->outbuf);
99                 SAFE_FREE(aio_ex);
100                 return NULL;
101         }
102
103         /* aio_ex->inbuf now contains the stolen old InBuf containing the data to write. */
104
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;
110 }
111
112 /****************************************************************************
113  Delete the extended aio struct.
114 *****************************************************************************/
115
116 static void delete_aio_ex(struct aio_extra *aio_ex)
117 {
118         DLIST_REMOVE(aio_list_head, aio_ex);
119         /* Safe to do as we've removed ourselves from the in use list first. */
120         free_InBuffer(aio_ex->inbuf);
121
122         SAFE_FREE(aio_ex->outbuf);
123         SAFE_FREE(aio_ex);
124 }
125
126 /****************************************************************************
127  Given the aiocb struct find the extended aio struct containing it.
128 *****************************************************************************/
129
130 static struct aio_extra *find_aio_ex(uint16 mid)
131 {
132         struct aio_extra *p;
133
134         for( p = aio_list_head; p; p = p->next) {
135                 if (mid == p->mid) {
136                         return p;
137                 }
138         }
139         return NULL;
140 }
141
142 /****************************************************************************
143  We can have these many aio buffers in flight.
144 *****************************************************************************/
145
146 #define AIO_PENDING_SIZE 10
147 static sig_atomic_t signals_received;
148 static int outstanding_aio_calls;
149 static uint16 aio_pending_array[AIO_PENDING_SIZE];
150
151 /****************************************************************************
152  Signal handler when an aio request completes.
153 *****************************************************************************/
154
155 static void signal_handler(int sig, siginfo_t *info, void *unused)
156 {
157         if (signals_received < AIO_PENDING_SIZE - 1) {
158                 aio_pending_array[signals_received] = *(uint16 *)(info->si_value.sival_ptr);
159                 signals_received++;
160         } /* Else signal is lost. */
161         sys_select_signal(RT_SIGNAL_AIO);
162 }
163
164 /****************************************************************************
165  Is there a signal waiting ?
166 *****************************************************************************/
167
168 BOOL aio_finished(void)
169 {
170         return (signals_received != 0);
171 }
172
173 /****************************************************************************
174  Initialize the signal handler for aio read/write.
175 *****************************************************************************/
176
177 void initialize_async_io_handler(void)
178 {
179         struct sigaction act;
180
181         ZERO_STRUCT(act);
182         act.sa_sigaction = signal_handler;
183         act.sa_flags = SA_SIGINFO;
184         sigemptyset( &act.sa_mask );
185         if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
186                 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
187         }
188
189         /* the signal can start off blocked due to a bug in bash */
190         BlockSignals(False, RT_SIGNAL_AIO);
191 }
192
193 /****************************************************************************
194  Set up an aio request from a SMBreadX call.
195 *****************************************************************************/
196
197 BOOL schedule_aio_read_and_X(connection_struct *conn,
198                              char *inbuf, char *outbuf,
199                              int length, int len_outbuf,
200                              files_struct *fsp, SMB_OFF_T startpos,
201                              size_t smb_maxcnt)
202 {
203         struct aio_extra *aio_ex;
204         SMB_STRUCT_AIOCB *a;
205         size_t bufsize;
206         size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
207
208         if (!min_aio_read_size || (smb_maxcnt < min_aio_read_size)) {
209                 /* Too small a read for aio request. */
210                 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
211                           "for minimum aio_read of %u\n",
212                           (unsigned int)smb_maxcnt,
213                           (unsigned int)min_aio_read_size ));
214                 return False;
215         }
216
217         /* Only do this on non-chained and non-chaining reads not using the write cache. */
218         if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF) || (lp_write_cache_size(SNUM(conn)) != 0) ) {
219                 return False;
220         }
221
222         if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
223                 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio activities outstanding.\n",
224                           outstanding_aio_calls ));
225                 return False;
226         }
227
228         /* The following is safe from integer wrap as we've already
229            checked smb_maxcnt is 128k or less. */
230         bufsize = PTR_DIFF(smb_buf(outbuf),outbuf) + smb_maxcnt;
231
232         if ((aio_ex = create_aio_ex_read(fsp, bufsize, SVAL(inbuf,smb_mid))) == NULL) {
233                 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
234                 return False;
235         }
236
237         /* Copy the SMB header already setup in outbuf. */
238         memcpy(aio_ex->outbuf, outbuf, smb_buf(outbuf) - outbuf);
239         SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
240
241         a = &aio_ex->acb;
242
243         /* Now set up the aio record for the read call. */
244         
245         a->aio_fildes = fsp->fh->fd;
246         a->aio_buf = smb_buf(aio_ex->outbuf);
247         a->aio_nbytes = smb_maxcnt;
248         a->aio_offset = startpos;
249         a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
250         a->aio_sigevent.sigev_signo  = RT_SIGNAL_AIO;
251         a->aio_sigevent.sigev_value.sival_ptr = (void *)&aio_ex->mid;
252
253         if (SMB_VFS_AIO_READ(fsp,a) == -1) {
254                 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. Error %s\n",
255                         strerror(errno) ));
256                 delete_aio_ex(aio_ex);
257                 return False;
258         }
259
260         DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, offset %.0f, len = %u (mid = %u)\n",
261                 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt, (unsigned int)aio_ex->mid ));
262
263         srv_defer_sign_response(aio_ex->mid);
264         outstanding_aio_calls++;
265         return True;
266 }
267
268 /****************************************************************************
269  Set up an aio request from a SMBwriteX call.
270 *****************************************************************************/
271
272 BOOL schedule_aio_write_and_X(connection_struct *conn,
273                                 char *inbuf, char *outbuf,
274                                 int length, int len_outbuf,
275                                 files_struct *fsp, char *data,
276                                 SMB_OFF_T startpos,
277                                 size_t numtowrite)
278 {
279         struct aio_extra *aio_ex;
280         SMB_STRUCT_AIOCB *a;
281         size_t outbufsize;
282         BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
283         size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
284
285         if (!min_aio_write_size || (numtowrite < min_aio_write_size)) {
286                 /* Too small a write for aio request. */
287                 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too small "
288                           "for minimum aio_write of %u\n",
289                           (unsigned int)numtowrite,
290                           (unsigned int)min_aio_write_size ));
291                 return False;
292         }
293
294         /* Only do this on non-chained and non-chaining reads not using the write cache. */
295         if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF) || (lp_write_cache_size(SNUM(conn)) != 0) ) {
296                 return False;
297         }
298
299         if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
300                 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio activities outstanding.\n",
301                           outstanding_aio_calls ));
302                 DEBUG(10,("schedule_aio_write_and_X: failed to schedule aio_write for file %s, offset %.0f, len = %u (mid = %u)\n",
303                         fsp->fsp_name, (double)startpos, (unsigned int)numtowrite, (unsigned int)SVAL(inbuf,smb_mid) ));
304                 return False;
305         }
306
307         outbufsize = smb_len(outbuf) + 4;
308         if ((aio_ex = create_aio_ex_write(fsp, outbufsize, SVAL(inbuf,smb_mid))) == NULL) {
309                 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
310                 return False;
311         }
312
313         /* Paranioa.... */
314         SMB_ASSERT(aio_ex->inbuf == inbuf);
315
316         /* Copy the SMB header already setup in outbuf. */
317         memcpy(aio_ex->outbuf, outbuf, outbufsize);
318         SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
319
320         a = &aio_ex->acb;
321
322         /* Now set up the aio record for the write call. */
323         
324         a->aio_fildes = fsp->fh->fd;
325         a->aio_buf = data; /* As we've stolen inbuf this points within inbuf. */
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 = (void *)&aio_ex->mid;
331
332         if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
333                 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. Error %s\n",
334                         strerror(errno) ));
335                 /* Replace global InBuf as we're going to do a normal write. */
336                 set_InBuffer(aio_ex->inbuf);
337                 aio_ex->inbuf = NULL;
338                 delete_aio_ex(aio_ex);
339                 return False;
340         }
341
342         if (!write_through && !lp_syncalways(SNUM(fsp->conn)) && fsp->aio_write_behind) {
343                 /* Lie to the client and immediately claim we finished the write. */
344                 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
345                 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
346                 show_msg(aio_ex->outbuf);
347                 if (!send_smb(smbd_server_fd(),aio_ex->outbuf)) {
348                         exit_server("handle_aio_write: send_smb failed.");
349                 }
350                 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write behind for file %s\n",
351                         fsp->fsp_name ));
352         } else {
353                 srv_defer_sign_response(aio_ex->mid);
354         }
355         outstanding_aio_calls++;
356
357         DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file %s, \
358 offset %.0f, len = %u (mid = %u) outstanding_aio_calls = %d\n",
359                 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite, (unsigned int)aio_ex->mid, outstanding_aio_calls ));
360
361         return True;
362 }
363
364
365 /****************************************************************************
366  Complete the read and return the data or error back to the client.
367  Returns errno or zero if all ok.
368 *****************************************************************************/
369
370 static int handle_aio_read_complete(struct aio_extra *aio_ex)
371 {
372         int ret = 0;
373         int outsize;
374         char *outbuf = aio_ex->outbuf;
375         char *data = smb_buf(outbuf);
376         ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
377
378         if (nread < 0) {
379                 /* We're relying here on the fact that if the fd is
380                    closed then the aio will complete and aio_return
381                    will return an error. Hopefully this is
382                    true.... JRA. */
383
384                 /* If errno is ECANCELED then don't return anything to the client. */
385                 if (errno == ECANCELED) {
386                         srv_cancel_sign_response(aio_ex->mid);
387                         return 0;
388                 }
389
390                 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. Error = %s\n",
391                         aio_ex->fsp->fsp_name, strerror(errno) ));
392
393                 outsize = (UNIXERROR(ERRDOS,ERRnoaccess));
394                 ret = errno;
395         } else {
396                 outsize = set_message(outbuf,12,nread,False);
397                 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
398                 SSVAL(outbuf,smb_vwv5,nread);
399                 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
400                 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
401                 SSVAL(smb_buf(outbuf),-2,nread);
402
403                 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d nread=%d\n",
404                         aio_ex->fsp->fsp_name,
405                         aio_ex->acb.aio_nbytes, (int)nread ) );
406
407         }
408         smb_setlen(outbuf,outsize - 4);
409         show_msg(outbuf);
410         if (!send_smb(smbd_server_fd(),outbuf)) {
411                 exit_server("handle_aio_read_complete: send_smb failed.");
412         }
413
414         DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed for file %s, offset %.0f, len = %u\n",
415                 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset, (unsigned int)nread ));
416
417         return ret;
418 }
419
420 /****************************************************************************
421  Complete the write and return the data or error back to the client.
422  Returns errno or zero if all ok.
423 *****************************************************************************/
424
425 static int handle_aio_write_complete(struct aio_extra *aio_ex)
426 {
427         int ret = 0;
428         files_struct *fsp = aio_ex->fsp;
429         char *outbuf = aio_ex->outbuf;
430         ssize_t numtowrite = aio_ex->acb.aio_nbytes;
431         ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
432
433         if (fsp->aio_write_behind) {
434                 if (nwritten != numtowrite) {
435                         if (nwritten == -1) {
436                                 DEBUG(5,("handle_aio_write_complete: aio_write_behind failed ! File %s is corrupt ! Error %s\n",
437                                         fsp->fsp_name, strerror(errno) ));
438                                 ret = errno;
439                         } else {
440                                 DEBUG(0,("handle_aio_write_complete: aio_write_behind failed ! File %s is corrupt ! \
441 Wanted %u bytes but only wrote %d\n", fsp->fsp_name, (unsigned int)numtowrite, (int)nwritten ));
442                                 ret = EIO;
443                         }
444                 } else {
445                         DEBUG(10,("handle_aio_write_complete: aio_write_behind completed for file %s\n",
446                                 fsp->fsp_name ));
447                 }
448                 return 0;
449         }
450
451         /* We don't need outsize or set_message here as we've already set the
452            fixed size length when we set up the aio call. */
453
454         if(nwritten == -1) {
455                 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. nwritten == %d. Error = %s\n",
456                         fsp->fsp_name, (unsigned int)numtowrite,
457                         (int)nwritten, strerror(errno) ));
458
459                 /* If errno is ECANCELED then don't return anything to the client. */
460                 if (errno == ECANCELED) {
461                         srv_cancel_sign_response(aio_ex->mid);
462                         return 0;
463                 }
464
465                 UNIXERROR(ERRHRD,ERRdiskfull);
466                 ret = errno;
467         } else {
468                 BOOL write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
469
470                 SSVAL(outbuf,smb_vwv2,nwritten);
471                 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
472                 if (nwritten < (ssize_t)numtowrite) {
473                         SCVAL(outbuf,smb_rcls,ERRHRD);
474                         SSVAL(outbuf,smb_err,ERRdiskfull);
475                 }
476                                                                                                                                   
477                 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
478                 if (lp_syncalways(SNUM(fsp->conn)) || write_through) {
479                         sync_file(fsp->conn,fsp);
480                 }
481         }
482
483         show_msg(outbuf);
484         if (!send_smb(smbd_server_fd(),outbuf)) {
485                 exit_server("handle_aio_write: send_smb failed.");
486         }
487
488         DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed for file %s, offset %.0f, requested %u, written = %u\n",
489                 fsp->fsp_name, (double)aio_ex->acb.aio_offset, (unsigned int)numtowrite, (unsigned int)nwritten ));
490
491         return ret;
492 }
493
494 /****************************************************************************
495  Handle any aio completion. Returns True if finished (and sets *perr if err was non-zero),
496  False if not.
497 *****************************************************************************/
498
499 static BOOL handle_aio_completed(struct aio_extra *aio_ex, int *perr)
500 {
501         int err;
502
503         /* Ensure the operation has really completed. */
504         if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
505                 DEBUG(10,( "handle_aio_completed: operation mid %u still in process for file %s\n",
506                         aio_ex->mid, aio_ex->fsp->fsp_name ));
507                 return False;
508         }
509
510         if (aio_ex->read_req) {
511                 err = handle_aio_read_complete(aio_ex);
512         } else {
513                 err = handle_aio_write_complete(aio_ex);
514         }
515
516         if (err) {
517                 *perr = err; /* Only save non-zero errors. */
518         }
519
520         return True;
521 }
522
523 /****************************************************************************
524  Handle any aio completion inline.
525  Returns non-zero errno if fail or zero if all ok.
526 *****************************************************************************/
527
528 int process_aio_queue(void)
529 {
530         int i;
531         int ret = 0;
532
533         BlockSignals(True, RT_SIGNAL_AIO);
534
535         DEBUG(10,("process_aio_queue: signals_received = %d\n", (int)signals_received));
536         DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n", outstanding_aio_calls));
537
538         if (!signals_received) {
539                 BlockSignals(False, RT_SIGNAL_AIO);
540                 return 0;
541         }
542
543         /* Drain all the complete aio_reads. */
544         for (i = 0; i < signals_received; i++) {
545                 uint16 mid = aio_pending_array[i];
546                 files_struct *fsp = NULL;
547                 struct aio_extra *aio_ex = find_aio_ex(mid);
548
549                 if (!aio_ex) {
550                         DEBUG(3,("process_aio_queue: Can't find record to match mid %u.\n",
551                                 (unsigned int)mid));
552                         srv_cancel_sign_response(mid);
553                         continue;
554                 }
555
556                 fsp = aio_ex->fsp;
557                 if (fsp == NULL) {
558                         /* file was closed whilst I/O was outstanding. Just ignore. */
559                         DEBUG( 3,( "process_aio_queue: file closed whilst aio outstanding.\n"));
560                         srv_cancel_sign_response(mid);
561                         continue;
562                 }
563
564                 if (!handle_aio_completed(aio_ex, &ret)) {
565                         continue;
566                 }
567
568                 delete_aio_ex(aio_ex);
569         }
570
571         outstanding_aio_calls -= signals_received;
572         signals_received = 0;
573         BlockSignals(False, RT_SIGNAL_AIO);
574         return ret;
575 }
576
577 /****************************************************************************
578  We're doing write behind and the client closed the file. Wait up to 30 seconds
579  (my arbitrary choice) for the aio to complete. Return 0 if all writes completed,
580  errno to return if not.
581 *****************************************************************************/
582
583 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
584
585 BOOL wait_for_aio_completion(files_struct *fsp)
586 {
587         struct aio_extra *aio_ex;
588         const SMB_STRUCT_AIOCB **aiocb_list;
589         int aio_completion_count = 0;
590         time_t start_time = time(NULL);
591         int seconds_left;
592         int ret = 0;
593
594         for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT; seconds_left >= 0;) {
595                 int err = 0;
596                 int i;
597                 struct timespec ts;
598
599                 aio_completion_count = 0;
600                 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
601                         if (aio_ex->fsp == fsp) {
602                                 aio_completion_count++;
603                         }
604                 }
605
606                 if (!aio_completion_count) {
607                         return ret;
608                 }
609
610                 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events to complete.\n",
611                         aio_completion_count ));
612
613                 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *, aio_completion_count);
614                 if (!aiocb_list) {
615                         return False;
616                 }
617
618                 for( i = 0, aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
619                         if (aio_ex->fsp == fsp) {
620                                 aiocb_list[i++] = &aio_ex->acb;
621                         }
622                 }
623
624                 /* Now wait up to seconds_left for completion. */
625                 ts.tv_sec = seconds_left;
626                 ts.tv_nsec = 0;
627
628                 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait of %d seconds.\n",
629                         aio_completion_count, seconds_left ));
630
631                 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list, aio_completion_count, &ts);
632
633                 DEBUG(10,("wait_for_aio_completion: returned err = %d, errno = %s\n",
634                         err, strerror(errno) ));
635                 
636                 if (err == -1 && errno == EAGAIN) {
637                         DEBUG(0,("wait_for_aio_completion: aio_suspend timed out waiting for %d events after a wait of %d seconds\n",
638                                         aio_completion_count, seconds_left));
639                         /* Timeout. */
640                         cancel_aio_by_fsp(fsp);
641                         SAFE_FREE(aiocb_list);
642                         return ret ? ret : EIO;
643                 }
644
645                 /* One or more events might have completed - process them if so. */
646                 for( i = 0; i < aio_completion_count; i++) {
647                         uint16 mid = *(uint16 *)aiocb_list[i]->aio_sigevent.sigev_value.sival_ptr;
648
649                         aio_ex = find_aio_ex(mid);
650
651                         if (!handle_aio_completed(aio_ex, &err)) {
652                                 continue;
653                         }
654                         delete_aio_ex(aio_ex);
655                 }
656
657                 SAFE_FREE(aiocb_list);
658                 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT - (time(NULL) - start_time);
659         }
660
661         /* We timed out - we don't know why. Return ret if already an error, else EIO. */
662         DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting for %d events\n",
663                         aio_completion_count));
664
665         return ret ? ret : EIO;
666 }
667
668 /****************************************************************************
669  Cancel any outstanding aio requests. The client doesn't care about the reply.
670 *****************************************************************************/
671
672 void cancel_aio_by_fsp(files_struct *fsp)
673 {
674         struct aio_extra *aio_ex;
675
676         for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
677                 if (aio_ex->fsp == fsp) {
678                         /* Don't delete the aio_extra record as we may have completed
679                            and don't yet know it. Just do the aio_cancel call and return. */
680                         SMB_VFS_AIO_CANCEL(fsp,fsp->fh->fd, &aio_ex->acb);
681                         aio_ex->fsp = NULL; /* fsp will be closed when we return. */
682                 }
683         }
684 }
685
686 /****************************************************************************
687  Check if a buffer was stolen for aio use.
688 *****************************************************************************/
689
690 BOOL aio_inbuffer_in_use(char *inbuf)
691 {
692         struct aio_extra *aio_ex;
693
694         for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
695                 if (aio_ex->inbuf == inbuf) {
696                         return True;
697                 }
698         }
699         return False;
700 }
701 #else
702 BOOL aio_finished(void)
703 {
704         return False;
705 }
706
707 void initialize_async_io_handler(void)
708 {
709 }
710
711 int process_aio_queue(void)
712 {
713         return False;
714 }
715
716 BOOL schedule_aio_read_and_X(connection_struct *conn,
717                              char *inbuf, char *outbuf,
718                              int length, int len_outbuf,
719                              files_struct *fsp, SMB_OFF_T startpos,
720                              size_t smb_maxcnt)
721 {
722         return False;
723 }
724
725 BOOL schedule_aio_write_and_X(connection_struct *conn,
726                                 char *inbuf, char *outbuf,
727                                 int length, int len_outbuf,
728                                 files_struct *fsp, char *data,
729                                 SMB_OFF_T startpos,
730                                 size_t numtowrite)
731 {
732         return False;
733 }
734
735 void cancel_aio_by_fsp(files_struct *fsp)
736 {
737 }
738
739 BOOL wait_for_aio_completion(files_struct *fsp)
740 {
741         return True;
742 }
743
744 BOOL aio_inbuffer_in_use(char *ptr)
745 {
746         return False;
747 }
748 #endif