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