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