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