s3: Remove the unused completion handling from aio.c
[ambi/samba-autobuild/.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/smbd.h"
23 #include "smbd/globals.h"
24 #include "../lib/util/tevent_ntstatus.h"
25
26 #if defined(HAVE_AIO)
27
28 /* The signal we'll use to signify aio done. */
29 #ifndef RT_SIGNAL_AIO
30 #define RT_SIGNAL_AIO   (SIGRTMIN+3)
31 #endif
32
33 #ifndef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIVAL_PTR
34 #ifdef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIGVAL_PTR
35 #define sival_int       sigval_int
36 #define sival_ptr       sigval_ptr
37 #endif
38 #endif
39
40 /****************************************************************************
41  The buffer we keep around whilst an aio request is in process.
42 *****************************************************************************/
43
44 struct aio_extra {
45         struct aio_extra *next, *prev;
46         SMB_STRUCT_AIOCB acb;
47         files_struct *fsp;
48         struct smb_request *smbreq;
49         DATA_BLOB outbuf;
50         struct lock_struct lock;
51         bool write_through;
52 };
53
54 /****************************************************************************
55  Accessor function to return write_through state.
56 *****************************************************************************/
57
58 bool aio_write_through_requested(struct aio_extra *aio_ex)
59 {
60         return aio_ex->write_through;
61 }
62
63 bool initialize_async_io_handler(void)
64 {
65         return true;
66 }
67
68 static int aio_extra_destructor(struct aio_extra *aio_ex)
69 {
70         outstanding_aio_calls--;
71         return 0;
72 }
73
74 /****************************************************************************
75  Create the extended aio struct we must keep around for the lifetime
76  of the aio call.
77 *****************************************************************************/
78
79 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
80                                         files_struct *fsp,
81                                         size_t buflen)
82 {
83         struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
84
85         if (!aio_ex) {
86                 return NULL;
87         }
88
89         /* The output buffer stored in the aio_ex is the start of
90            the smb return buffer. The buffer used in the acb
91            is the start of the reply data portion of that buffer. */
92
93         if (buflen) {
94                 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
95                 if (!aio_ex->outbuf.data) {
96                         TALLOC_FREE(aio_ex);
97                         return NULL;
98                 }
99         }
100         talloc_set_destructor(aio_ex, aio_extra_destructor);
101         aio_ex->fsp = fsp;
102         outstanding_aio_calls++;
103         return aio_ex;
104 }
105
106 struct aio_req_fsp_link {
107         files_struct *fsp;
108         struct tevent_req *req;
109 };
110
111 static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
112 {
113         unsigned i;
114         files_struct *fsp = lnk->fsp;
115         struct tevent_req *req = lnk->req;
116
117         for (i=0; i<fsp->num_aio_requests; i++) {
118                 if (fsp->aio_requests[i] == req) {
119                         break;
120                 }
121         }
122         if (i == fsp->num_aio_requests) {
123                 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
124                 return 0;
125         }
126         fsp->num_aio_requests -= 1;
127         fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
128         return 0;
129 }
130
131 static bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
132 {
133         size_t array_len;
134         struct aio_req_fsp_link *lnk;
135
136         lnk = talloc(req, struct aio_req_fsp_link);
137         if (lnk == NULL) {
138                 return false;
139         }
140
141         array_len = talloc_array_length(fsp->aio_requests);
142         if (array_len <= fsp->num_aio_requests) {
143                 struct tevent_req **tmp;
144
145                 tmp = talloc_realloc(
146                         fsp, fsp->aio_requests, struct tevent_req *,
147                         fsp->num_aio_requests+1);
148                 if (tmp == NULL) {
149                         TALLOC_FREE(lnk);
150                         return false;
151                 }
152                 fsp->aio_requests = tmp;
153         }
154         fsp->aio_requests[fsp->num_aio_requests] = req;
155         fsp->num_aio_requests += 1;
156
157         lnk->fsp = fsp;
158         lnk->req = req;
159         talloc_set_destructor(lnk, aio_del_req_from_fsp);
160
161         return true;
162 }
163
164 static void aio_pread_smb1_done(struct tevent_req *req);
165
166 /****************************************************************************
167  Set up an aio request from a SMBreadX call.
168 *****************************************************************************/
169
170 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
171                              struct smb_request *smbreq,
172                              files_struct *fsp, off_t startpos,
173                              size_t smb_maxcnt)
174 {
175         struct aio_extra *aio_ex;
176         SMB_STRUCT_AIOCB *a;
177         size_t bufsize;
178         size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
179         struct tevent_req *req;
180
181         if (fsp->base_fsp != NULL) {
182                 /* No AIO on streams yet */
183                 DEBUG(10, ("AIO on streams not yet supported\n"));
184                 return NT_STATUS_RETRY;
185         }
186
187         if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
188             && !SMB_VFS_AIO_FORCE(fsp)) {
189                 /* Too small a read for aio request. */
190                 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
191                           "for minimum aio_read of %u\n",
192                           (unsigned int)smb_maxcnt,
193                           (unsigned int)min_aio_read_size ));
194                 return NT_STATUS_RETRY;
195         }
196
197         /* Only do this on non-chained and non-chaining reads not using the
198          * write cache. */
199         if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
200                 return NT_STATUS_RETRY;
201         }
202
203         if (outstanding_aio_calls >= aio_pending_size) {
204                 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
205                           "activities outstanding.\n",
206                           outstanding_aio_calls ));
207                 return NT_STATUS_RETRY;
208         }
209
210         /* The following is safe from integer wrap as we've already checked
211            smb_maxcnt is 128k or less. Wct is 12 for read replies */
212
213         bufsize = smb_size + 12 * 2 + smb_maxcnt;
214
215         if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
216                 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
217                 return NT_STATUS_NO_MEMORY;
218         }
219
220         construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
221         srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
222         SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
223
224         init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
225                 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
226                 &aio_ex->lock);
227
228         /* Take the lock until the AIO completes. */
229         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
230                 TALLOC_FREE(aio_ex);
231                 return NT_STATUS_FILE_LOCK_CONFLICT;
232         }
233
234         a = &aio_ex->acb;
235
236         /* Now set up the aio record for the read call. */
237
238         a->aio_fildes = fsp->fh->fd;
239         a->aio_buf = smb_buf(aio_ex->outbuf.data);
240         a->aio_nbytes = smb_maxcnt;
241         a->aio_offset = startpos;
242         a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
243         a->aio_sigevent.sigev_signo  = RT_SIGNAL_AIO;
244         a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
245
246         req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx,
247                                  fsp, smb_buf(aio_ex->outbuf.data),
248                                  smb_maxcnt, startpos);
249         if (req == NULL) {
250                 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
251                          "Error %s\n", strerror(errno) ));
252                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
253                 TALLOC_FREE(aio_ex);
254                 return NT_STATUS_RETRY;
255         }
256         tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
257
258         if (!aio_add_req_to_fsp(fsp, req)) {
259                 DEBUG(1, ("Could not add req to fsp\n"));
260                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
261                 TALLOC_FREE(aio_ex);
262                 return NT_STATUS_RETRY;
263         }
264
265         aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
266
267         DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
268                   "offset %.0f, len = %u (mid = %u)\n",
269                   fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
270                   (unsigned int)aio_ex->smbreq->mid ));
271
272         return NT_STATUS_OK;
273 }
274
275 static void aio_pread_smb1_done(struct tevent_req *req)
276 {
277         struct aio_extra *aio_ex = tevent_req_callback_data(
278                 req, struct aio_extra);
279         files_struct *fsp = aio_ex->fsp;
280         int outsize;
281         char *outbuf = (char *)aio_ex->outbuf.data;
282         char *data = smb_buf(outbuf);
283         ssize_t nread;
284         int err;
285
286         nread = SMB_VFS_PREAD_RECV(req, &err);
287         TALLOC_FREE(req);
288
289         DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
290                    (nread == -1) ? strerror(err) : "no error"));
291
292         if (fsp == NULL) {
293                 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
294                            "aio outstanding (mid[%llu]).\n",
295                            (unsigned long long)aio_ex->smbreq->mid));
296                 TALLOC_FREE(aio_ex);
297                 return;
298         }
299
300         /* Unlock now we're done. */
301         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
302
303         if (nread < 0) {
304                 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
305                            "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
306                            strerror(err)));
307
308                 ERROR_NT(map_nt_error_from_unix(err));
309                 outsize = srv_set_message(outbuf,0,0,true);
310         } else {
311                 outsize = srv_set_message(outbuf, 12, nread, False);
312                 SSVAL(outbuf,smb_vwv2, 0xFFFF); /* Remaining - must be * -1. */
313                 SSVAL(outbuf,smb_vwv5, nread);
314                 SSVAL(outbuf,smb_vwv6, smb_offset(data,outbuf));
315                 SSVAL(outbuf,smb_vwv7, ((nread >> 16) & 1));
316                 SSVAL(smb_buf(outbuf), -2, nread);
317
318                 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
319                 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
320
321                 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
322                            "nread=%d\n", fsp_str_dbg(fsp),
323                            (int)aio_ex->acb.aio_nbytes, (int)nread ) );
324
325         }
326         smb_setlen(outbuf, outsize - 4);
327         show_msg(outbuf);
328         if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
329                           true, aio_ex->smbreq->seqnum+1,
330                           IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
331                 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
332                                     "failed.");
333         }
334
335         DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
336                    "for file %s, offset %.0f, len = %u\n",
337                    fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
338                    (unsigned int)nread));
339
340         TALLOC_FREE(aio_ex);
341 }
342
343 static void aio_pwrite_smb1_done(struct tevent_req *req);
344
345 /****************************************************************************
346  Set up an aio request from a SMBwriteX call.
347 *****************************************************************************/
348
349 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
350                               struct smb_request *smbreq,
351                               files_struct *fsp, const char *data,
352                               off_t startpos,
353                               size_t numtowrite)
354 {
355         struct aio_extra *aio_ex;
356         SMB_STRUCT_AIOCB *a;
357         size_t bufsize;
358         size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
359         struct tevent_req *req;
360
361         if (fsp->base_fsp != NULL) {
362                 /* No AIO on streams yet */
363                 DEBUG(10, ("AIO on streams not yet supported\n"));
364                 return NT_STATUS_RETRY;
365         }
366
367         if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
368             && !SMB_VFS_AIO_FORCE(fsp)) {
369                 /* Too small a write for aio request. */
370                 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
371                           "small for minimum aio_write of %u\n",
372                           (unsigned int)numtowrite,
373                           (unsigned int)min_aio_write_size ));
374                 return NT_STATUS_RETRY;
375         }
376
377         /* Only do this on non-chained and non-chaining writes not using the
378          * write cache. */
379         if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
380                 return NT_STATUS_RETRY;
381         }
382
383         if (outstanding_aio_calls >= aio_pending_size) {
384                 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
385                          "activities outstanding.\n",
386                           outstanding_aio_calls ));
387                 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
388                           "aio_write for file %s, offset %.0f, len = %u "
389                           "(mid = %u)\n",
390                           fsp_str_dbg(fsp), (double)startpos,
391                           (unsigned int)numtowrite,
392                           (unsigned int)smbreq->mid ));
393                 return NT_STATUS_RETRY;
394         }
395
396         bufsize = smb_size + 6*2;
397
398         if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
399                 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
400                 return NT_STATUS_NO_MEMORY;
401         }
402         aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
403
404         construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
405         srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
406         SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
407
408         init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
409                 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
410                 &aio_ex->lock);
411
412         /* Take the lock until the AIO completes. */
413         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
414                 TALLOC_FREE(aio_ex);
415                 return NT_STATUS_FILE_LOCK_CONFLICT;
416         }
417
418         a = &aio_ex->acb;
419
420         /* Now set up the aio record for the write call. */
421
422         a->aio_fildes = fsp->fh->fd;
423         a->aio_buf = discard_const_p(char, data);
424         a->aio_nbytes = numtowrite;
425         a->aio_offset = startpos;
426         a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
427         a->aio_sigevent.sigev_signo  = RT_SIGNAL_AIO;
428         a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
429
430         req = SMB_VFS_PWRITE_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
431                                   data, numtowrite, startpos);
432         if (req == NULL) {
433                 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
434                          "Error %s\n", strerror(errno) ));
435                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
436                 TALLOC_FREE(aio_ex);
437                 return NT_STATUS_RETRY;
438         }
439         tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
440
441         if (!aio_add_req_to_fsp(fsp, req)) {
442                 DEBUG(1, ("Could not add req to fsp\n"));
443                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
444                 TALLOC_FREE(aio_ex);
445                 return NT_STATUS_RETRY;
446         }
447
448         aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
449
450         /* This should actually be improved to span the write. */
451         contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
452         contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
453
454         if (!aio_ex->write_through && !lp_syncalways(SNUM(fsp->conn))
455             && fsp->aio_write_behind) {
456                 /* Lie to the client and immediately claim we finished the
457                  * write. */
458                 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
459                 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
460                 show_msg((char *)aio_ex->outbuf.data);
461                 if (!srv_send_smb(aio_ex->smbreq->sconn,
462                                 (char *)aio_ex->outbuf.data,
463                                 true, aio_ex->smbreq->seqnum+1,
464                                 IS_CONN_ENCRYPTED(fsp->conn),
465                                 &aio_ex->smbreq->pcd)) {
466                         exit_server_cleanly("schedule_aio_write_and_X: "
467                                             "srv_send_smb failed.");
468                 }
469                 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
470                           "behind for file %s\n", fsp_str_dbg(fsp)));
471         }
472
473         DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
474                   "%s, offset %.0f, len = %u (mid = %u) "
475                   "outstanding_aio_calls = %d\n",
476                   fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
477                   (unsigned int)aio_ex->smbreq->mid, outstanding_aio_calls ));
478
479         return NT_STATUS_OK;
480 }
481
482 static void aio_pwrite_smb1_done(struct tevent_req *req)
483 {
484         struct aio_extra *aio_ex = tevent_req_callback_data(
485                 req, struct aio_extra);
486         files_struct *fsp = aio_ex->fsp;
487         char *outbuf = (char *)aio_ex->outbuf.data;
488         ssize_t numtowrite = aio_ex->acb.aio_nbytes;
489         ssize_t nwritten;
490         int err;
491
492         nwritten = SMB_VFS_PWRITE_RECV(req, &err);
493         TALLOC_FREE(req);
494
495         DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
496                    (nwritten == -1) ? strerror(err) : "no error"));
497
498         if (fsp == NULL) {
499                 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
500                            "aio outstanding (mid[%llu]).\n",
501                            (unsigned long long)aio_ex->smbreq->mid));
502                 TALLOC_FREE(aio_ex);
503                 return;
504         }
505
506         /* Unlock now we're done. */
507         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
508
509         if (fsp->aio_write_behind) {
510
511                 if (nwritten != numtowrite) {
512                         if (nwritten == -1) {
513                                 DEBUG(5,("handle_aio_write_complete: "
514                                          "aio_write_behind failed ! File %s "
515                                          "is corrupt ! Error %s\n",
516                                          fsp_str_dbg(fsp), strerror(err)));
517                         } else {
518                                 DEBUG(0,("handle_aio_write_complete: "
519                                          "aio_write_behind failed ! File %s "
520                                          "is corrupt ! Wanted %u bytes but "
521                                          "only wrote %d\n", fsp_str_dbg(fsp),
522                                          (unsigned int)numtowrite,
523                                          (int)nwritten ));
524                         }
525                 } else {
526                         DEBUG(10,("handle_aio_write_complete: "
527                                   "aio_write_behind completed for file %s\n",
528                                   fsp_str_dbg(fsp)));
529                 }
530                 /* TODO: should no return success in case of an error !!! */
531                 TALLOC_FREE(aio_ex);
532                 return;
533         }
534
535         /* We don't need outsize or set_message here as we've already set the
536            fixed size length when we set up the aio call. */
537
538         if (nwritten == -1) {
539                 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
540                           "nwritten == %d. Error = %s\n",
541                           fsp_str_dbg(fsp), (unsigned int)numtowrite,
542                           (int)nwritten, strerror(err)));
543
544                 ERROR_NT(map_nt_error_from_unix(err));
545                 srv_set_message(outbuf,0,0,true);
546         } else {
547                 NTSTATUS status;
548
549                 SSVAL(outbuf,smb_vwv2,nwritten);
550                 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
551                 if (nwritten < (ssize_t)numtowrite) {
552                         SCVAL(outbuf,smb_rcls,ERRHRD);
553                         SSVAL(outbuf,smb_err,ERRdiskfull);
554                 }
555
556                 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
557                          fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
558                 status = sync_file(fsp->conn,fsp, aio_ex->write_through);
559                 if (!NT_STATUS_IS_OK(status)) {
560                         ERROR_BOTH(map_nt_error_from_unix(errno),
561                                    ERRHRD, ERRdiskfull);
562                         srv_set_message(outbuf,0,0,true);
563                         DEBUG(5, ("handle_aio_write: sync_file for %s "
564                                   "returned %s\n",
565                                   fsp_str_dbg(fsp), nt_errstr(status)));
566                 }
567
568                 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
569         }
570
571         show_msg(outbuf);
572         if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
573                           true, aio_ex->smbreq->seqnum+1,
574                           IS_CONN_ENCRYPTED(fsp->conn),
575                           NULL)) {
576                 exit_server_cleanly("handle_aio_write_complete: "
577                                     "srv_send_smb failed.");
578         }
579
580         DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
581                    "for file %s, offset %.0f, requested %u, written = %u\n",
582                    fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
583                    (unsigned int)numtowrite, (unsigned int)nwritten));
584
585         TALLOC_FREE(aio_ex);
586 }
587
588 bool cancel_smb2_aio(struct smb_request *smbreq)
589 {
590         struct smbd_smb2_request *smb2req = smbreq->smb2req;
591         struct aio_extra *aio_ex = NULL;
592
593         if (smb2req) {
594                 aio_ex = talloc_get_type(smbreq->async_priv,
595                                          struct aio_extra);
596         }
597
598         if (aio_ex == NULL) {
599                 return false;
600         }
601
602         if (aio_ex->fsp == NULL) {
603                 return false;
604         }
605
606         /*
607          * We let the aio request run. Setting fsp to NULL has the
608          * effect that the _done routines don't send anything out.
609          */
610
611         aio_ex->fsp = NULL;
612         return true;
613 }
614
615 static void aio_pread_smb2_done(struct tevent_req *req);
616
617 /****************************************************************************
618  Set up an aio request from a SMB2 read call.
619 *****************************************************************************/
620
621 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
622                                 struct smb_request *smbreq,
623                                 files_struct *fsp,
624                                 TALLOC_CTX *ctx,
625                                 DATA_BLOB *preadbuf,
626                                 off_t startpos,
627                                 size_t smb_maxcnt)
628 {
629         struct aio_extra *aio_ex;
630         SMB_STRUCT_AIOCB *a;
631         size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
632         struct tevent_req *req;
633
634         if (fsp->base_fsp != NULL) {
635                 /* No AIO on streams yet */
636                 DEBUG(10, ("AIO on streams not yet supported\n"));
637                 return NT_STATUS_RETRY;
638         }
639
640         if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
641             && !SMB_VFS_AIO_FORCE(fsp)) {
642                 /* Too small a read for aio request. */
643                 DEBUG(10,("smb2: read size (%u) too small "
644                         "for minimum aio_read of %u\n",
645                         (unsigned int)smb_maxcnt,
646                         (unsigned int)min_aio_read_size ));
647                 return NT_STATUS_RETRY;
648         }
649
650         /* Only do this on reads not using the write cache. */
651         if (lp_write_cache_size(SNUM(conn)) != 0) {
652                 return NT_STATUS_RETRY;
653         }
654
655         if (outstanding_aio_calls >= aio_pending_size) {
656                 DEBUG(10,("smb2: Already have %d aio "
657                         "activities outstanding.\n",
658                         outstanding_aio_calls ));
659                 return NT_STATUS_RETRY;
660         }
661
662         /* Create the out buffer. */
663         *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
664         if (preadbuf->data == NULL) {
665                 return NT_STATUS_NO_MEMORY;
666         }
667
668         if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
669                 return NT_STATUS_NO_MEMORY;
670         }
671
672         init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
673                 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
674                 &aio_ex->lock);
675
676         /* Take the lock until the AIO completes. */
677         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
678                 TALLOC_FREE(aio_ex);
679                 return NT_STATUS_FILE_LOCK_CONFLICT;
680         }
681
682         a = &aio_ex->acb;
683
684         /* Now set up the aio record for the read call. */
685
686         a->aio_fildes = fsp->fh->fd;
687         a->aio_buf = preadbuf->data;
688         a->aio_nbytes = smb_maxcnt;
689         a->aio_offset = startpos;
690         a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
691         a->aio_sigevent.sigev_signo  = RT_SIGNAL_AIO;
692         a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
693
694         req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
695                                  preadbuf->data, smb_maxcnt, startpos);
696         if (req == NULL) {
697                 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
698                           "Error %s\n", strerror(errno)));
699                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
700                 TALLOC_FREE(aio_ex);
701                 return NT_STATUS_RETRY;
702         }
703         tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
704
705         if (!aio_add_req_to_fsp(fsp, req)) {
706                 DEBUG(1, ("Could not add req to fsp\n"));
707                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
708                 TALLOC_FREE(aio_ex);
709                 return NT_STATUS_RETRY;
710         }
711
712         /* We don't need talloc_move here as both aio_ex and
713          * smbreq are children of smbreq->smb2req. */
714         aio_ex->smbreq = smbreq;
715         smbreq->async_priv = aio_ex;
716
717         DEBUG(10,("smb2: scheduled aio_read for file %s, "
718                 "offset %.0f, len = %u (mid = %u)\n",
719                 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
720                 (unsigned int)aio_ex->smbreq->mid ));
721
722         return NT_STATUS_OK;
723 }
724
725 static void aio_pread_smb2_done(struct tevent_req *req)
726 {
727         struct aio_extra *aio_ex = tevent_req_callback_data(
728                 req, struct aio_extra);
729         struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
730         files_struct *fsp = aio_ex->fsp;
731         NTSTATUS status;
732         ssize_t nread;
733         int err = 0;
734
735         nread = SMB_VFS_PREAD_RECV(req, &err);
736         TALLOC_FREE(req);
737
738         DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
739                    (nread == -1) ? strerror(err) : "no error"));
740
741         if (fsp == NULL) {
742                 DEBUG( 3, ("aio_pread_smb2_done: file closed whilst "
743                            "aio outstanding (mid[%llu]).\n",
744                            (unsigned long long)aio_ex->smbreq->mid));
745                 TALLOC_FREE(aio_ex);
746                 return;
747         }
748
749         /* Unlock now we're done. */
750         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
751
752         /* Common error or success code processing for async or sync
753            read returns. */
754
755         status = smb2_read_complete(subreq, nread, err);
756
757         if (nread > 0) {
758                 fsp->fh->pos = aio_ex->acb.aio_offset + nread;
759                 fsp->fh->position_information = fsp->fh->pos;
760         }
761
762         DEBUG(10, ("smb2: scheduled aio_read completed "
763                    "for file %s, offset %.0f, len = %u "
764                    "(errcode = %d, NTSTATUS = %s)\n",
765                    fsp_str_dbg(aio_ex->fsp),
766                    (double)aio_ex->acb.aio_offset,
767                    (unsigned int)nread,
768                    err, nt_errstr(status)));
769
770         if (!NT_STATUS_IS_OK(status)) {
771                 tevent_req_nterror(subreq, status);
772                 return;
773         }
774         tevent_req_done(subreq);
775 }
776
777 static void aio_pwrite_smb2_done(struct tevent_req *req);
778
779 /****************************************************************************
780  Set up an aio request from a SMB2write call.
781 *****************************************************************************/
782
783 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
784                                 struct smb_request *smbreq,
785                                 files_struct *fsp,
786                                 uint64_t in_offset,
787                                 DATA_BLOB in_data,
788                                 bool write_through)
789 {
790         struct aio_extra *aio_ex = NULL;
791         SMB_STRUCT_AIOCB *a = NULL;
792         size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
793         struct tevent_req *req;
794
795         if (fsp->base_fsp != NULL) {
796                 /* No AIO on streams yet */
797                 DEBUG(10, ("AIO on streams not yet supported\n"));
798                 return NT_STATUS_RETRY;
799         }
800
801         if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
802             && !SMB_VFS_AIO_FORCE(fsp)) {
803                 /* Too small a write for aio request. */
804                 DEBUG(10,("smb2: write size (%u) too "
805                         "small for minimum aio_write of %u\n",
806                         (unsigned int)in_data.length,
807                         (unsigned int)min_aio_write_size ));
808                 return NT_STATUS_RETRY;
809         }
810
811         /* Only do this on writes not using the write cache. */
812         if (lp_write_cache_size(SNUM(conn)) != 0) {
813                 return NT_STATUS_RETRY;
814         }
815
816         if (outstanding_aio_calls >= aio_pending_size) {
817                 DEBUG(3,("smb2: Already have %d aio "
818                         "activities outstanding.\n",
819                         outstanding_aio_calls ));
820                 return NT_STATUS_RETRY;
821         }
822
823         if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
824                 return NT_STATUS_NO_MEMORY;
825         }
826
827         aio_ex->write_through = write_through;
828
829         init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
830                 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
831                 &aio_ex->lock);
832
833         /* Take the lock until the AIO completes. */
834         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
835                 TALLOC_FREE(aio_ex);
836                 return NT_STATUS_FILE_LOCK_CONFLICT;
837         }
838
839         a = &aio_ex->acb;
840
841         /* Now set up the aio record for the write call. */
842
843         a->aio_fildes = fsp->fh->fd;
844         a->aio_buf = in_data.data;
845         a->aio_nbytes = in_data.length;
846         a->aio_offset = in_offset;
847         a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
848         a->aio_sigevent.sigev_signo  = RT_SIGNAL_AIO;
849         a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
850
851         req = SMB_VFS_PWRITE_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
852                                   in_data.data, in_data.length, in_offset);
853         if (req == NULL) {
854                 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
855                           "Error %s\n", strerror(errno)));
856                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
857                 TALLOC_FREE(aio_ex);
858                 return NT_STATUS_RETRY;
859         }
860         tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
861
862         if (!aio_add_req_to_fsp(fsp, req)) {
863                 DEBUG(1, ("Could not add req to fsp\n"));
864                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
865                 TALLOC_FREE(aio_ex);
866                 return NT_STATUS_RETRY;
867         }
868
869         /* We don't need talloc_move here as both aio_ex and
870         * smbreq are children of smbreq->smb2req. */
871         aio_ex->smbreq = smbreq;
872         smbreq->async_priv = aio_ex;
873
874         /* This should actually be improved to span the write. */
875         contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
876         contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
877
878         /*
879          * We don't want to do write behind due to ownership
880          * issues of the request structs. Maybe add it if I
881          * figure those out. JRA.
882          */
883
884         DEBUG(10,("smb2: scheduled aio_write for file "
885                 "%s, offset %.0f, len = %u (mid = %u) "
886                 "outstanding_aio_calls = %d\n",
887                 fsp_str_dbg(fsp),
888                 (double)in_offset,
889                 (unsigned int)in_data.length,
890                 (unsigned int)aio_ex->smbreq->mid,
891                 outstanding_aio_calls ));
892
893         return NT_STATUS_OK;
894 }
895
896 static void aio_pwrite_smb2_done(struct tevent_req *req)
897 {
898         struct aio_extra *aio_ex = tevent_req_callback_data(
899                 req, struct aio_extra);
900         ssize_t numtowrite = aio_ex->acb.aio_nbytes;
901         struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
902         files_struct *fsp = aio_ex->fsp;
903         NTSTATUS status;
904         ssize_t nwritten;
905         int err = 0;
906
907         nwritten = SMB_VFS_PWRITE_RECV(req, &err);
908         TALLOC_FREE(req);
909
910         DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
911                    (nwritten == -1) ? strerror(err) : "no error"));
912
913         if (fsp == NULL) {
914                 DEBUG( 3, ("aio_pwrite_smb2_done: file closed whilst "
915                            "aio outstanding (mid[%llu]).\n",
916                            (unsigned long long)aio_ex->smbreq->mid));
917                 TALLOC_FREE(aio_ex);
918                 return;
919         }
920
921         /* Unlock now we're done. */
922         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
923
924         status = smb2_write_complete(subreq, nwritten, err);
925
926         DEBUG(10, ("smb2: scheduled aio_write completed "
927                    "for file %s, offset %.0f, requested %u, "
928                    "written = %u (errcode = %d, NTSTATUS = %s)\n",
929                    fsp_str_dbg(fsp),
930                    (double)aio_ex->acb.aio_offset,
931                    (unsigned int)numtowrite,
932                    (unsigned int)nwritten,
933                    err, nt_errstr(status)));
934
935         if (!NT_STATUS_IS_OK(status)) {
936                 tevent_req_nterror(subreq, status);
937                 return;
938         }
939         tevent_req_done(subreq);
940 }
941
942 /****************************************************************************
943  Handle any aio completion inline.
944 *****************************************************************************/
945
946 void smbd_aio_complete_aio_ex(struct aio_extra *aio_ex)
947 {
948         return;
949 }
950
951 void aio_fsp_close(files_struct *fsp)
952 {
953         unsigned i;
954
955         for (i=0; i<fsp->num_aio_requests; i++) {
956                 struct tevent_req *req = fsp->aio_requests[i];
957                 struct aio_extra *aio_ex = tevent_req_callback_data(
958                         req, struct aio_extra);
959                 aio_ex->fsp = NULL;
960         }
961 }
962
963 #else
964
965 bool initialize_async_io_handler(void)
966 {
967         return false;
968 }
969
970 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
971                              struct smb_request *smbreq,
972                              files_struct *fsp, off_t startpos,
973                              size_t smb_maxcnt)
974 {
975         return NT_STATUS_RETRY;
976 }
977
978 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
979                               struct smb_request *smbreq,
980                               files_struct *fsp, const char *data,
981                               off_t startpos,
982                               size_t numtowrite)
983 {
984         return NT_STATUS_RETRY;
985 }
986
987 bool cancel_smb2_aio(struct smb_request *smbreq)
988 {
989         return false;
990 }
991
992 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
993                                 struct smb_request *smbreq,
994                                 files_struct *fsp,
995                                 TALLOC_CTX *ctx,
996                                 DATA_BLOB *preadbuf,
997                                 off_t startpos,
998                                 size_t smb_maxcnt)
999 {
1000         return NT_STATUS_RETRY;
1001 }
1002
1003 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
1004                                 struct smb_request *smbreq,
1005                                 files_struct *fsp,
1006                                 uint64_t in_offset,
1007                                 DATA_BLOB in_data,
1008                                 bool write_through)
1009 {
1010         return NT_STATUS_RETRY;
1011 }
1012
1013 void aio_fsp_close(files_struct *fsp)
1014 {
1015         return;
1016 }
1017
1018 int wait_for_aio_completion(files_struct *fsp)
1019 {
1020         return 0;
1021 }
1022
1023 void smbd_aio_complete_mid(uint64_t mid);
1024
1025 #endif