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