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