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