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