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