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