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