s3/vfs: wrap async io function args inside struct vfs_aio_state
[kai/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 #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(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                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
246                 TALLOC_FREE(aio_ex);
247                 return NT_STATUS_RETRY;
248         }
249         tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
250
251         if (!aio_add_req_to_fsp(fsp, req)) {
252                 DEBUG(1, ("Could not add req to fsp\n"));
253                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
254                 TALLOC_FREE(aio_ex);
255                 return NT_STATUS_RETRY;
256         }
257
258         aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
259
260         DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
261                   "offset %.0f, len = %u (mid = %u)\n",
262                   fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
263                   (unsigned int)aio_ex->smbreq->mid ));
264
265         return NT_STATUS_OK;
266 }
267
268 static void aio_pread_smb1_done(struct tevent_req *req)
269 {
270         struct aio_extra *aio_ex = tevent_req_callback_data(
271                 req, struct aio_extra);
272         files_struct *fsp = aio_ex->fsp;
273         int outsize;
274         char *outbuf = (char *)aio_ex->outbuf.data;
275         char *data = smb_buf(outbuf) + 1 /* padding byte */;
276         ssize_t nread;
277         struct vfs_aio_state vfs_aio_state;
278
279         nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
280         TALLOC_FREE(req);
281
282         DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
283                    (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
284
285         if (fsp == NULL) {
286                 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
287                            "aio outstanding (mid[%llu]).\n",
288                            (unsigned long long)aio_ex->smbreq->mid));
289                 TALLOC_FREE(aio_ex);
290                 return;
291         }
292
293         /* Unlock now we're done. */
294         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
295
296         if (nread < 0) {
297                 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
298                            "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
299                            strerror(vfs_aio_state.error)));
300
301                 ERROR_NT(map_nt_error_from_unix(vfs_aio_state.error));
302                 outsize = srv_set_message(outbuf,0,0,true);
303         } else {
304                 outsize = srv_set_message(outbuf, 12,
305                                           nread + 1 /* padding byte */, false);
306                 SSVAL(outbuf,smb_vwv2, 0xFFFF); /* Remaining - must be * -1. */
307                 SSVAL(outbuf,smb_vwv5, nread);
308                 SSVAL(outbuf,smb_vwv6, smb_offset(data,outbuf));
309                 SSVAL(outbuf,smb_vwv7, ((nread >> 16) & 1));
310                 SSVAL(smb_buf(outbuf), -2, nread);
311
312                 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
313                 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
314
315                 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
316                            "nread=%d\n", fsp_str_dbg(fsp),
317                            (int)aio_ex->nbyte, (int)nread ) );
318
319         }
320         smb_setlen(outbuf, outsize - 4);
321         show_msg(outbuf);
322         if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
323                           true, aio_ex->smbreq->seqnum+1,
324                           IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
325                 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
326                                     "failed.");
327         }
328
329         DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
330                    "for file %s, offset %.0f, len = %u\n",
331                    fsp_str_dbg(fsp), (double)aio_ex->offset,
332                    (unsigned int)nread));
333
334         TALLOC_FREE(aio_ex);
335 }
336
337 struct pwrite_fsync_state {
338         struct tevent_context *ev;
339         files_struct *fsp;
340         bool write_through;
341         ssize_t nwritten;
342 };
343
344 static void pwrite_fsync_write_done(struct tevent_req *subreq);
345 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
346
347 static struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
348                                             struct tevent_context *ev,
349                                             struct files_struct *fsp,
350                                             const void *data,
351                                             size_t n, off_t offset,
352                                             bool write_through)
353 {
354         struct tevent_req *req, *subreq;
355         struct pwrite_fsync_state *state;
356
357         req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
358         if (req == NULL) {
359                 return NULL;
360         }
361         state->ev = ev;
362         state->fsp = fsp;
363         state->write_through = write_through;
364
365         subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
366         if (tevent_req_nomem(subreq, req)) {
367                 return tevent_req_post(req, ev);
368         }
369         tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
370         return req;
371 }
372
373 static void pwrite_fsync_write_done(struct tevent_req *subreq)
374 {
375         struct tevent_req *req = tevent_req_callback_data(
376                 subreq, struct tevent_req);
377         struct pwrite_fsync_state *state = tevent_req_data(
378                 req, struct pwrite_fsync_state);
379         connection_struct *conn = state->fsp->conn;
380         bool do_sync;
381         struct vfs_aio_state vfs_aio_state;
382
383         state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &vfs_aio_state);
384         TALLOC_FREE(subreq);
385         if (state->nwritten == -1) {
386                 tevent_req_error(req, vfs_aio_state.error);
387                 return;
388         }
389
390         do_sync = (lp_strict_sync(SNUM(conn)) &&
391                    (lp_sync_always(SNUM(conn)) || state->write_through));
392         if (!do_sync) {
393                 tevent_req_done(req);
394                 return;
395         }
396
397         subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
398         if (tevent_req_nomem(subreq, req)) {
399                 return;
400         }
401         tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
402 }
403
404 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
405 {
406         struct tevent_req *req = tevent_req_callback_data(
407                 subreq, struct tevent_req);
408         int ret;
409         struct vfs_aio_state vfs_aio_state;
410
411         ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
412         TALLOC_FREE(subreq);
413         if (ret == -1) {
414                 tevent_req_error(req, vfs_aio_state.error);
415                 return;
416         }
417         tevent_req_done(req);
418 }
419
420 static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
421 {
422         struct pwrite_fsync_state *state = tevent_req_data(
423                 req, struct pwrite_fsync_state);
424
425         if (tevent_req_is_unix_error(req, perr)) {
426                 return -1;
427         }
428         return state->nwritten;
429 }
430
431 static void aio_pwrite_smb1_done(struct tevent_req *req);
432
433 /****************************************************************************
434  Set up an aio request from a SMBwriteX call.
435 *****************************************************************************/
436
437 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
438                               struct smb_request *smbreq,
439                               files_struct *fsp, const char *data,
440                               off_t startpos,
441                               size_t numtowrite)
442 {
443         struct aio_extra *aio_ex;
444         size_t bufsize;
445         size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
446         struct tevent_req *req;
447
448         if (fsp->base_fsp != NULL) {
449                 /* No AIO on streams yet */
450                 DEBUG(10, ("AIO on streams not yet supported\n"));
451                 return NT_STATUS_RETRY;
452         }
453
454         if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
455             && !SMB_VFS_AIO_FORCE(fsp)) {
456                 /* Too small a write for aio request. */
457                 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
458                           "small for minimum aio_write of %u\n",
459                           (unsigned int)numtowrite,
460                           (unsigned int)min_aio_write_size ));
461                 return NT_STATUS_RETRY;
462         }
463
464         /* Only do this on non-chained and non-chaining writes not using the
465          * write cache. */
466         if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
467                 return NT_STATUS_RETRY;
468         }
469
470         bufsize = smb_size + 6*2;
471
472         if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
473                 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
474                 return NT_STATUS_NO_MEMORY;
475         }
476         aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
477
478         construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
479         srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
480         SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
481
482         init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
483                 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
484                 &aio_ex->lock);
485
486         /* Take the lock until the AIO completes. */
487         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
488                 TALLOC_FREE(aio_ex);
489                 return NT_STATUS_FILE_LOCK_CONFLICT;
490         }
491
492         aio_ex->nbyte = numtowrite;
493         aio_ex->offset = startpos;
494
495         req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
496                                 data, numtowrite, startpos,
497                                 aio_ex->write_through);
498         if (req == NULL) {
499                 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
500                          "Error %s\n", strerror(errno) ));
501                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
502                 TALLOC_FREE(aio_ex);
503                 return NT_STATUS_RETRY;
504         }
505         tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
506
507         if (!aio_add_req_to_fsp(fsp, req)) {
508                 DEBUG(1, ("Could not add req to fsp\n"));
509                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
510                 TALLOC_FREE(aio_ex);
511                 return NT_STATUS_RETRY;
512         }
513
514         aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
515
516         /* This should actually be improved to span the write. */
517         contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
518         contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
519
520         if (!aio_ex->write_through && !lp_sync_always(SNUM(fsp->conn))
521             && fsp->aio_write_behind) {
522                 /* Lie to the client and immediately claim we finished the
523                  * write. */
524                 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
525                 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
526                 show_msg((char *)aio_ex->outbuf.data);
527                 if (!srv_send_smb(aio_ex->smbreq->xconn,
528                                 (char *)aio_ex->outbuf.data,
529                                 true, aio_ex->smbreq->seqnum+1,
530                                 IS_CONN_ENCRYPTED(fsp->conn),
531                                 &aio_ex->smbreq->pcd)) {
532                         exit_server_cleanly("schedule_aio_write_and_X: "
533                                             "srv_send_smb failed.");
534                 }
535                 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
536                           "behind for file %s\n", fsp_str_dbg(fsp)));
537         }
538
539         DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
540                   "%s, offset %.0f, len = %u (mid = %u) "
541                   "outstanding_aio_calls = %d\n",
542                   fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
543                   (unsigned int)aio_ex->smbreq->mid,
544                   get_outstanding_aio_calls() ));
545
546         return NT_STATUS_OK;
547 }
548
549 static void aio_pwrite_smb1_done(struct tevent_req *req)
550 {
551         struct aio_extra *aio_ex = tevent_req_callback_data(
552                 req, struct aio_extra);
553         files_struct *fsp = aio_ex->fsp;
554         char *outbuf = (char *)aio_ex->outbuf.data;
555         ssize_t numtowrite = aio_ex->nbyte;
556         ssize_t nwritten;
557         int err;
558
559         nwritten = pwrite_fsync_recv(req, &err);
560         TALLOC_FREE(req);
561
562         DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
563                    (nwritten == -1) ? strerror(err) : "no error"));
564
565         if (fsp == NULL) {
566                 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
567                            "aio outstanding (mid[%llu]).\n",
568                            (unsigned long long)aio_ex->smbreq->mid));
569                 TALLOC_FREE(aio_ex);
570                 return;
571         }
572
573         /* Unlock now we're done. */
574         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
575
576         mark_file_modified(fsp);
577
578         if (fsp->aio_write_behind) {
579
580                 if (nwritten != numtowrite) {
581                         if (nwritten == -1) {
582                                 DEBUG(5,("handle_aio_write_complete: "
583                                          "aio_write_behind failed ! File %s "
584                                          "is corrupt ! Error %s\n",
585                                          fsp_str_dbg(fsp), strerror(err)));
586                         } else {
587                                 DEBUG(0,("handle_aio_write_complete: "
588                                          "aio_write_behind failed ! File %s "
589                                          "is corrupt ! Wanted %u bytes but "
590                                          "only wrote %d\n", fsp_str_dbg(fsp),
591                                          (unsigned int)numtowrite,
592                                          (int)nwritten ));
593                         }
594                 } else {
595                         DEBUG(10,("handle_aio_write_complete: "
596                                   "aio_write_behind completed for file %s\n",
597                                   fsp_str_dbg(fsp)));
598                 }
599                 /* TODO: should no return success in case of an error !!! */
600                 TALLOC_FREE(aio_ex);
601                 return;
602         }
603
604         /* We don't need outsize or set_message here as we've already set the
605            fixed size length when we set up the aio call. */
606
607         if (nwritten == -1) {
608                 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
609                           "nwritten == %d. Error = %s\n",
610                           fsp_str_dbg(fsp), (unsigned int)numtowrite,
611                           (int)nwritten, strerror(err)));
612
613                 ERROR_NT(map_nt_error_from_unix(err));
614                 srv_set_message(outbuf,0,0,true);
615         } else {
616                 SSVAL(outbuf,smb_vwv2,nwritten);
617                 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
618                 if (nwritten < (ssize_t)numtowrite) {
619                         SCVAL(outbuf,smb_rcls,ERRHRD);
620                         SSVAL(outbuf,smb_err,ERRdiskfull);
621                 }
622
623                 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
624                          fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
625
626                 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
627         }
628
629         show_msg(outbuf);
630         if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
631                           true, aio_ex->smbreq->seqnum+1,
632                           IS_CONN_ENCRYPTED(fsp->conn),
633                           NULL)) {
634                 exit_server_cleanly("handle_aio_write_complete: "
635                                     "srv_send_smb failed.");
636         }
637
638         DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
639                    "for file %s, offset %.0f, requested %u, written = %u\n",
640                    fsp_str_dbg(fsp), (double)aio_ex->offset,
641                    (unsigned int)numtowrite, (unsigned int)nwritten));
642
643         TALLOC_FREE(aio_ex);
644 }
645
646 bool cancel_smb2_aio(struct smb_request *smbreq)
647 {
648         struct smbd_smb2_request *smb2req = smbreq->smb2req;
649         struct aio_extra *aio_ex = NULL;
650
651         if (smb2req) {
652                 aio_ex = talloc_get_type(smbreq->async_priv,
653                                          struct aio_extra);
654         }
655
656         if (aio_ex == NULL) {
657                 return false;
658         }
659
660         if (aio_ex->fsp == NULL) {
661                 return false;
662         }
663
664         /*
665          * We let the aio request run. Setting fsp to NULL has the
666          * effect that the _done routines don't send anything out.
667          */
668
669         aio_ex->fsp = NULL;
670         return true;
671 }
672
673 static void aio_pread_smb2_done(struct tevent_req *req);
674
675 /****************************************************************************
676  Set up an aio request from a SMB2 read call.
677 *****************************************************************************/
678
679 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
680                                 struct smb_request *smbreq,
681                                 files_struct *fsp,
682                                 TALLOC_CTX *ctx,
683                                 DATA_BLOB *preadbuf,
684                                 off_t startpos,
685                                 size_t smb_maxcnt)
686 {
687         struct aio_extra *aio_ex;
688         size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
689         struct tevent_req *req;
690
691         if (fsp->base_fsp != NULL) {
692                 /* No AIO on streams yet */
693                 DEBUG(10, ("AIO on streams not yet supported\n"));
694                 return NT_STATUS_RETRY;
695         }
696
697         if (fsp->op == NULL) {
698                 /* No AIO on internal opens. */
699                 return NT_STATUS_RETRY;
700         }
701
702         if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
703             && !SMB_VFS_AIO_FORCE(fsp)) {
704                 /* Too small a read for aio request. */
705                 DEBUG(10,("smb2: read size (%u) too small "
706                         "for minimum aio_read of %u\n",
707                         (unsigned int)smb_maxcnt,
708                         (unsigned int)min_aio_read_size ));
709                 return NT_STATUS_RETRY;
710         }
711
712         /* Only do this on reads not using the write cache. */
713         if (lp_write_cache_size(SNUM(conn)) != 0) {
714                 return NT_STATUS_RETRY;
715         }
716
717         /* Create the out buffer. */
718         *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
719         if (preadbuf->data == NULL) {
720                 return NT_STATUS_NO_MEMORY;
721         }
722
723         if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
724                 return NT_STATUS_NO_MEMORY;
725         }
726
727         init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
728                 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
729                 &aio_ex->lock);
730
731         /* Take the lock until the AIO completes. */
732         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
733                 TALLOC_FREE(aio_ex);
734                 return NT_STATUS_FILE_LOCK_CONFLICT;
735         }
736
737         aio_ex->nbyte = smb_maxcnt;
738         aio_ex->offset = startpos;
739
740         req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
741                                  preadbuf->data, smb_maxcnt, startpos);
742         if (req == NULL) {
743                 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
744                           "Error %s\n", strerror(errno)));
745                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
746                 TALLOC_FREE(aio_ex);
747                 return NT_STATUS_RETRY;
748         }
749         tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
750
751         if (!aio_add_req_to_fsp(fsp, req)) {
752                 DEBUG(1, ("Could not add req to fsp\n"));
753                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
754                 TALLOC_FREE(aio_ex);
755                 return NT_STATUS_RETRY;
756         }
757
758         /* We don't need talloc_move here as both aio_ex and
759          * smbreq are children of smbreq->smb2req. */
760         aio_ex->smbreq = smbreq;
761         smbreq->async_priv = aio_ex;
762
763         DEBUG(10,("smb2: scheduled aio_read for file %s, "
764                 "offset %.0f, len = %u (mid = %u)\n",
765                 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
766                 (unsigned int)aio_ex->smbreq->mid ));
767
768         return NT_STATUS_OK;
769 }
770
771 static void aio_pread_smb2_done(struct tevent_req *req)
772 {
773         struct aio_extra *aio_ex = tevent_req_callback_data(
774                 req, struct aio_extra);
775         struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
776         files_struct *fsp = aio_ex->fsp;
777         NTSTATUS status;
778         ssize_t nread;
779         struct vfs_aio_state vfs_aio_state = { 0 };
780
781         nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
782         TALLOC_FREE(req);
783
784         DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
785                    (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
786
787         if (fsp == NULL) {
788                 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
789                           __func__, (uintmax_t)aio_ex->smbreq->mid));
790                 TALLOC_FREE(aio_ex);
791                 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
792                 return;
793         }
794
795         /* Unlock now we're done. */
796         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
797
798         /* Common error or success code processing for async or sync
799            read returns. */
800
801         status = smb2_read_complete(subreq, nread, vfs_aio_state.error);
802
803         if (nread > 0) {
804                 fsp->fh->pos = aio_ex->offset + nread;
805                 fsp->fh->position_information = fsp->fh->pos;
806         }
807
808         DEBUG(10, ("smb2: scheduled aio_read completed "
809                    "for file %s, offset %.0f, len = %u "
810                    "(errcode = %d, NTSTATUS = %s)\n",
811                    fsp_str_dbg(aio_ex->fsp),
812                    (double)aio_ex->offset,
813                    (unsigned int)nread,
814                    vfs_aio_state.error, nt_errstr(status)));
815
816         if (!NT_STATUS_IS_OK(status)) {
817                 tevent_req_nterror(subreq, status);
818                 return;
819         }
820         tevent_req_done(subreq);
821 }
822
823 static void aio_pwrite_smb2_done(struct tevent_req *req);
824
825 /****************************************************************************
826  Set up an aio request from a SMB2write call.
827 *****************************************************************************/
828
829 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
830                                 struct smb_request *smbreq,
831                                 files_struct *fsp,
832                                 uint64_t in_offset,
833                                 DATA_BLOB in_data,
834                                 bool write_through)
835 {
836         struct aio_extra *aio_ex = NULL;
837         size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
838         struct tevent_req *req;
839
840         if (fsp->base_fsp != NULL) {
841                 /* No AIO on streams yet */
842                 DEBUG(10, ("AIO on streams not yet supported\n"));
843                 return NT_STATUS_RETRY;
844         }
845
846         if (fsp->op == NULL) {
847                 /* No AIO on internal opens. */
848                 return NT_STATUS_RETRY;
849         }
850
851         if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
852             && !SMB_VFS_AIO_FORCE(fsp)) {
853                 /* Too small a write for aio request. */
854                 DEBUG(10,("smb2: write size (%u) too "
855                         "small for minimum aio_write of %u\n",
856                         (unsigned int)in_data.length,
857                         (unsigned int)min_aio_write_size ));
858                 return NT_STATUS_RETRY;
859         }
860
861         /* Only do this on writes not using the write cache. */
862         if (lp_write_cache_size(SNUM(conn)) != 0) {
863                 return NT_STATUS_RETRY;
864         }
865
866         if (smbreq->unread_bytes) {
867                 /* Can't do async with recvfile. */
868                 return NT_STATUS_RETRY;
869         }
870
871         if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
872                 return NT_STATUS_NO_MEMORY;
873         }
874
875         aio_ex->write_through = write_through;
876
877         init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
878                 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
879                 &aio_ex->lock);
880
881         /* Take the lock until the AIO completes. */
882         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
883                 TALLOC_FREE(aio_ex);
884                 return NT_STATUS_FILE_LOCK_CONFLICT;
885         }
886
887         aio_ex->nbyte = in_data.length;
888         aio_ex->offset = in_offset;
889
890         req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
891                                 in_data.data, in_data.length, in_offset,
892                                 write_through);
893         if (req == NULL) {
894                 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
895                           "Error %s\n", strerror(errno)));
896                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
897                 TALLOC_FREE(aio_ex);
898                 return NT_STATUS_RETRY;
899         }
900         tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
901
902         if (!aio_add_req_to_fsp(fsp, req)) {
903                 DEBUG(1, ("Could not add req to fsp\n"));
904                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
905                 TALLOC_FREE(aio_ex);
906                 return NT_STATUS_RETRY;
907         }
908
909         /* We don't need talloc_move here as both aio_ex and
910         * smbreq are children of smbreq->smb2req. */
911         aio_ex->smbreq = smbreq;
912         smbreq->async_priv = aio_ex;
913
914         /* This should actually be improved to span the write. */
915         contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
916         contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
917
918         /*
919          * We don't want to do write behind due to ownership
920          * issues of the request structs. Maybe add it if I
921          * figure those out. JRA.
922          */
923
924         DEBUG(10,("smb2: scheduled aio_write for file "
925                 "%s, offset %.0f, len = %u (mid = %u) "
926                 "outstanding_aio_calls = %d\n",
927                 fsp_str_dbg(fsp),
928                 (double)in_offset,
929                 (unsigned int)in_data.length,
930                 (unsigned int)aio_ex->smbreq->mid,
931                 get_outstanding_aio_calls() ));
932
933         return NT_STATUS_OK;
934 }
935
936 static void aio_pwrite_smb2_done(struct tevent_req *req)
937 {
938         struct aio_extra *aio_ex = tevent_req_callback_data(
939                 req, struct aio_extra);
940         ssize_t numtowrite = aio_ex->nbyte;
941         struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
942         files_struct *fsp = aio_ex->fsp;
943         NTSTATUS status;
944         ssize_t nwritten;
945         int err = 0;
946
947         nwritten = pwrite_fsync_recv(req, &err);
948         TALLOC_FREE(req);
949
950         DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
951                    (nwritten == -1) ? strerror(err) : "no error"));
952
953         if (fsp == NULL) {
954                 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
955                           __func__, (uintmax_t)aio_ex->smbreq->mid));
956                 TALLOC_FREE(aio_ex);
957                 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
958                 return;
959         }
960
961         /* Unlock now we're done. */
962         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
963
964         mark_file_modified(fsp);
965
966         status = smb2_write_complete_nosync(subreq, nwritten, err);
967
968         DEBUG(10, ("smb2: scheduled aio_write completed "
969                    "for file %s, offset %.0f, requested %u, "
970                    "written = %u (errcode = %d, NTSTATUS = %s)\n",
971                    fsp_str_dbg(fsp),
972                    (double)aio_ex->offset,
973                    (unsigned int)numtowrite,
974                    (unsigned int)nwritten,
975                    err, nt_errstr(status)));
976
977         if (!NT_STATUS_IS_OK(status)) {
978                 tevent_req_nterror(subreq, status);
979                 return;
980         }
981         tevent_req_done(subreq);
982 }