Initial import
[samba] / 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] = info->si_value.sival_int;
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_int = 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_int = 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                 sync_file(fsp->conn,fsp, write_through);
479         }
480
481         show_msg(outbuf);
482         if (!send_smb(smbd_server_fd(),outbuf)) {
483                 exit_server("handle_aio_write: send_smb failed.");
484         }
485
486         DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed for file %s, offset %.0f, requested %u, written = %u\n",
487                 fsp->fsp_name, (double)aio_ex->acb.aio_offset, (unsigned int)numtowrite, (unsigned int)nwritten ));
488
489         return ret;
490 }
491
492 /****************************************************************************
493  Handle any aio completion. Returns True if finished (and sets *perr if err was non-zero),
494  False if not.
495 *****************************************************************************/
496
497 static BOOL handle_aio_completed(struct aio_extra *aio_ex, int *perr)
498 {
499         int err;
500
501         /* Ensure the operation has really completed. */
502         if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
503                 DEBUG(10,( "handle_aio_completed: operation mid %u still in process for file %s\n",
504                         aio_ex->mid, aio_ex->fsp->fsp_name ));
505                 return False;
506         }
507
508         if (aio_ex->read_req) {
509                 err = handle_aio_read_complete(aio_ex);
510         } else {
511                 err = handle_aio_write_complete(aio_ex);
512         }
513
514         if (err) {
515                 *perr = err; /* Only save non-zero errors. */
516         }
517
518         return True;
519 }
520
521 /****************************************************************************
522  Handle any aio completion inline.
523  Returns non-zero errno if fail or zero if all ok.
524 *****************************************************************************/
525
526 int process_aio_queue(void)
527 {
528         int i;
529         int ret = 0;
530
531         BlockSignals(True, RT_SIGNAL_AIO);
532
533         DEBUG(10,("process_aio_queue: signals_received = %d\n", (int)signals_received));
534         DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n", outstanding_aio_calls));
535
536         if (!signals_received) {
537                 BlockSignals(False, RT_SIGNAL_AIO);
538                 return 0;
539         }
540
541         /* Drain all the complete aio_reads. */
542         for (i = 0; i < signals_received; i++) {
543                 uint16 mid = aio_pending_array[i];
544                 files_struct *fsp = NULL;
545                 struct aio_extra *aio_ex = find_aio_ex(mid);
546
547                 if (!aio_ex) {
548                         DEBUG(3,("process_aio_queue: Can't find record to match mid %u.\n",
549                                 (unsigned int)mid));
550                         srv_cancel_sign_response(mid);
551                         continue;
552                 }
553
554                 fsp = aio_ex->fsp;
555                 if (fsp == NULL) {
556                         /* file was closed whilst I/O was outstanding. Just ignore. */
557                         DEBUG( 3,( "process_aio_queue: file closed whilst aio outstanding.\n"));
558                         srv_cancel_sign_response(mid);
559                         continue;
560                 }
561
562                 if (!handle_aio_completed(aio_ex, &ret)) {
563                         continue;
564                 }
565
566                 delete_aio_ex(aio_ex);
567         }
568
569         outstanding_aio_calls -= signals_received;
570         signals_received = 0;
571         BlockSignals(False, RT_SIGNAL_AIO);
572         return ret;
573 }
574
575 /****************************************************************************
576  We're doing write behind and the client closed the file. Wait up to 30 seconds
577  (my arbitrary choice) for the aio to complete. Return 0 if all writes completed,
578  errno to return if not.
579 *****************************************************************************/
580
581 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
582
583 BOOL wait_for_aio_completion(files_struct *fsp)
584 {
585         struct aio_extra *aio_ex;
586         const SMB_STRUCT_AIOCB **aiocb_list;
587         int aio_completion_count = 0;
588         time_t start_time = time(NULL);
589         int seconds_left;
590         int ret = 0;
591
592         for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT; seconds_left >= 0;) {
593                 int err = 0;
594                 int i;
595                 struct timespec ts;
596
597                 aio_completion_count = 0;
598                 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
599                         if (aio_ex->fsp == fsp) {
600                                 aio_completion_count++;
601                         }
602                 }
603
604                 if (!aio_completion_count) {
605                         return ret;
606                 }
607
608                 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events to complete.\n",
609                         aio_completion_count ));
610
611                 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *, aio_completion_count);
612                 if (!aiocb_list) {
613                         return False;
614                 }
615
616                 for( i = 0, aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
617                         if (aio_ex->fsp == fsp) {
618                                 aiocb_list[i++] = &aio_ex->acb;
619                         }
620                 }
621
622                 /* Now wait up to seconds_left for completion. */
623                 ts.tv_sec = seconds_left;
624                 ts.tv_nsec = 0;
625
626                 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait of %d seconds.\n",
627                         aio_completion_count, seconds_left ));
628
629                 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list, aio_completion_count, &ts);
630
631                 DEBUG(10,("wait_for_aio_completion: returned err = %d, errno = %s\n",
632                         err, strerror(errno) ));
633                 
634                 if (err == -1 && errno == EAGAIN) {
635                         DEBUG(0,("wait_for_aio_completion: aio_suspend timed out waiting for %d events after a wait of %d seconds\n",
636                                         aio_completion_count, seconds_left));
637                         /* Timeout. */
638                         cancel_aio_by_fsp(fsp);
639                         SAFE_FREE(aiocb_list);
640                         return ret ? ret : EIO;
641                 }
642
643                 /* One or more events might have completed - process them if so. */
644                 for( i = 0; i < aio_completion_count; i++) {
645                         uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
646
647                         aio_ex = find_aio_ex(mid);
648
649                         if (!aio_ex) {
650                                 DEBUG(0, ("wait_for_aio_completion: mid %u doesn't match an aio record\n",
651                                         (unsigned int)mid ));
652                                 continue;
653                         }
654
655                         if (!handle_aio_completed(aio_ex, &err)) {
656                                 continue;
657                         }
658                         delete_aio_ex(aio_ex);
659                 }
660
661                 SAFE_FREE(aiocb_list);
662                 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT - (time(NULL) - start_time);
663         }
664
665         /* We timed out - we don't know why. Return ret if already an error, else EIO. */
666         DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting for %d events\n",
667                         aio_completion_count));
668
669         return ret ? ret : EIO;
670 }
671
672 /****************************************************************************
673  Cancel any outstanding aio requests. The client doesn't care about the reply.
674 *****************************************************************************/
675
676 void cancel_aio_by_fsp(files_struct *fsp)
677 {
678         struct aio_extra *aio_ex;
679
680         for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
681                 if (aio_ex->fsp == fsp) {
682                         /* Don't delete the aio_extra record as we may have completed
683                            and don't yet know it. Just do the aio_cancel call and return. */
684                         SMB_VFS_AIO_CANCEL(fsp,fsp->fh->fd, &aio_ex->acb);
685                         aio_ex->fsp = NULL; /* fsp will be closed when we return. */
686                 }
687         }
688 }
689
690 /****************************************************************************
691  Check if a buffer was stolen for aio use.
692 *****************************************************************************/
693
694 BOOL aio_inbuffer_in_use(char *inbuf)
695 {
696         struct aio_extra *aio_ex;
697
698         for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
699                 if (aio_ex->inbuf == inbuf) {
700                         return True;
701                 }
702         }
703         return False;
704 }
705 #else
706 BOOL aio_finished(void)
707 {
708         return False;
709 }
710
711 void initialize_async_io_handler(void)
712 {
713 }
714
715 int process_aio_queue(void)
716 {
717         return False;
718 }
719
720 BOOL schedule_aio_read_and_X(connection_struct *conn,
721                              char *inbuf, char *outbuf,
722                              int length, int len_outbuf,
723                              files_struct *fsp, SMB_OFF_T startpos,
724                              size_t smb_maxcnt)
725 {
726         return False;
727 }
728
729 BOOL schedule_aio_write_and_X(connection_struct *conn,
730                                 char *inbuf, char *outbuf,
731                                 int length, int len_outbuf,
732                                 files_struct *fsp, char *data,
733                                 SMB_OFF_T startpos,
734                                 size_t numtowrite)
735 {
736         return False;
737 }
738
739 void cancel_aio_by_fsp(files_struct *fsp)
740 {
741 }
742
743 BOOL wait_for_aio_completion(files_struct *fsp)
744 {
745         return True;
746 }
747
748 BOOL aio_inbuffer_in_use(char *ptr)
749 {
750         return False;
751 }
752 #endif