s3/vfs: rename SMB_VFS_STRICT_LOCK to SMB_VFS_STRICT_LOCK_CHECK
[nivanova/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_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(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         /* Create the out buffer. */
701         *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
702         if (preadbuf->data == NULL) {
703                 return NT_STATUS_NO_MEMORY;
704         }
705
706         if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
707                 return NT_STATUS_NO_MEMORY;
708         }
709
710         init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
711                 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
712                 &aio_ex->lock);
713
714         /* Take the lock until the AIO completes. */
715         if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
716                 TALLOC_FREE(aio_ex);
717                 return NT_STATUS_FILE_LOCK_CONFLICT;
718         }
719
720         aio_ex->nbyte = smb_maxcnt;
721         aio_ex->offset = startpos;
722
723         req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
724                                  preadbuf->data, smb_maxcnt, startpos);
725         if (req == NULL) {
726                 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
727                           "Error %s\n", strerror(errno)));
728                 TALLOC_FREE(aio_ex);
729                 return NT_STATUS_RETRY;
730         }
731         tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
732
733         if (!aio_add_req_to_fsp(fsp, req)) {
734                 DEBUG(1, ("Could not add req to fsp\n"));
735                 TALLOC_FREE(aio_ex);
736                 return NT_STATUS_RETRY;
737         }
738
739         /* We don't need talloc_move here as both aio_ex and
740          * smbreq are children of smbreq->smb2req. */
741         aio_ex->smbreq = smbreq;
742         smbreq->async_priv = aio_ex;
743
744         DEBUG(10,("smb2: scheduled aio_read for file %s, "
745                 "offset %.0f, len = %u (mid = %u)\n",
746                 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
747                 (unsigned int)aio_ex->smbreq->mid ));
748
749         return NT_STATUS_OK;
750 }
751
752 static void aio_pread_smb2_done(struct tevent_req *req)
753 {
754         struct aio_extra *aio_ex = tevent_req_callback_data(
755                 req, struct aio_extra);
756         struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
757         files_struct *fsp = aio_ex->fsp;
758         NTSTATUS status;
759         ssize_t nread;
760         struct vfs_aio_state vfs_aio_state = { 0 };
761
762         nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
763         TALLOC_FREE(req);
764
765         DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
766                    (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
767
768         if (fsp == NULL) {
769                 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
770                           __func__, (uintmax_t)aio_ex->smbreq->mid));
771                 TALLOC_FREE(aio_ex);
772                 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
773                 return;
774         }
775
776         /* Common error or success code processing for async or sync
777            read returns. */
778
779         status = smb2_read_complete(subreq, nread, vfs_aio_state.error);
780
781         if (nread > 0) {
782                 fsp->fh->pos = aio_ex->offset + nread;
783                 fsp->fh->position_information = fsp->fh->pos;
784         }
785
786         DEBUG(10, ("smb2: scheduled aio_read completed "
787                    "for file %s, offset %.0f, len = %u "
788                    "(errcode = %d, NTSTATUS = %s)\n",
789                    fsp_str_dbg(aio_ex->fsp),
790                    (double)aio_ex->offset,
791                    (unsigned int)nread,
792                    vfs_aio_state.error, nt_errstr(status)));
793
794         if (!NT_STATUS_IS_OK(status)) {
795                 tevent_req_nterror(subreq, status);
796                 return;
797         }
798         tevent_req_done(subreq);
799 }
800
801 static void aio_pwrite_smb2_done(struct tevent_req *req);
802
803 /****************************************************************************
804  Set up an aio request from a SMB2write call.
805 *****************************************************************************/
806
807 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
808                                 struct smb_request *smbreq,
809                                 files_struct *fsp,
810                                 uint64_t in_offset,
811                                 DATA_BLOB in_data,
812                                 bool write_through)
813 {
814         struct aio_extra *aio_ex = NULL;
815         size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
816         struct tevent_req *req;
817
818         if (fsp->base_fsp != NULL) {
819                 /* No AIO on streams yet */
820                 DEBUG(10, ("AIO on streams not yet supported\n"));
821                 return NT_STATUS_RETRY;
822         }
823
824         if (fsp->op == NULL) {
825                 /* No AIO on internal opens. */
826                 return NT_STATUS_RETRY;
827         }
828
829         if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
830             && !SMB_VFS_AIO_FORCE(fsp)) {
831                 /* Too small a write for aio request. */
832                 DEBUG(10,("smb2: write size (%u) too "
833                         "small for minimum aio_write of %u\n",
834                         (unsigned int)in_data.length,
835                         (unsigned int)min_aio_write_size ));
836                 return NT_STATUS_RETRY;
837         }
838
839         /* Only do this on writes not using the write cache. */
840         if (lp_write_cache_size(SNUM(conn)) != 0) {
841                 return NT_STATUS_RETRY;
842         }
843
844         if (smbreq->unread_bytes) {
845                 /* Can't do async with recvfile. */
846                 return NT_STATUS_RETRY;
847         }
848
849         if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
850                 return NT_STATUS_NO_MEMORY;
851         }
852
853         aio_ex->write_through = write_through;
854
855         init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
856                 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
857                 &aio_ex->lock);
858
859         /* Take the lock until the AIO completes. */
860         if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
861                 TALLOC_FREE(aio_ex);
862                 return NT_STATUS_FILE_LOCK_CONFLICT;
863         }
864
865         aio_ex->nbyte = in_data.length;
866         aio_ex->offset = in_offset;
867
868         req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
869                                 in_data.data, in_data.length, in_offset,
870                                 write_through);
871         if (req == NULL) {
872                 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
873                           "Error %s\n", strerror(errno)));
874                 TALLOC_FREE(aio_ex);
875                 return NT_STATUS_RETRY;
876         }
877         tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
878
879         if (!aio_add_req_to_fsp(fsp, req)) {
880                 DEBUG(1, ("Could not add req to fsp\n"));
881                 TALLOC_FREE(aio_ex);
882                 return NT_STATUS_RETRY;
883         }
884
885         /* We don't need talloc_move here as both aio_ex and
886         * smbreq are children of smbreq->smb2req. */
887         aio_ex->smbreq = smbreq;
888         smbreq->async_priv = aio_ex;
889
890         /* This should actually be improved to span the write. */
891         contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
892         contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
893
894         /*
895          * We don't want to do write behind due to ownership
896          * issues of the request structs. Maybe add it if I
897          * figure those out. JRA.
898          */
899
900         DEBUG(10,("smb2: scheduled aio_write for file "
901                 "%s, offset %.0f, len = %u (mid = %u) "
902                 "outstanding_aio_calls = %d\n",
903                 fsp_str_dbg(fsp),
904                 (double)in_offset,
905                 (unsigned int)in_data.length,
906                 (unsigned int)aio_ex->smbreq->mid,
907                 get_outstanding_aio_calls() ));
908
909         return NT_STATUS_OK;
910 }
911
912 static void aio_pwrite_smb2_done(struct tevent_req *req)
913 {
914         struct aio_extra *aio_ex = tevent_req_callback_data(
915                 req, struct aio_extra);
916         ssize_t numtowrite = aio_ex->nbyte;
917         struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
918         files_struct *fsp = aio_ex->fsp;
919         NTSTATUS status;
920         ssize_t nwritten;
921         int err = 0;
922
923         nwritten = pwrite_fsync_recv(req, &err);
924         TALLOC_FREE(req);
925
926         DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
927                    (nwritten == -1) ? strerror(err) : "no error"));
928
929         if (fsp == NULL) {
930                 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
931                           __func__, (uintmax_t)aio_ex->smbreq->mid));
932                 TALLOC_FREE(aio_ex);
933                 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
934                 return;
935         }
936
937         mark_file_modified(fsp);
938
939         status = smb2_write_complete_nosync(subreq, nwritten, err);
940
941         DEBUG(10, ("smb2: scheduled aio_write completed "
942                    "for file %s, offset %.0f, requested %u, "
943                    "written = %u (errcode = %d, NTSTATUS = %s)\n",
944                    fsp_str_dbg(fsp),
945                    (double)aio_ex->offset,
946                    (unsigned int)numtowrite,
947                    (unsigned int)nwritten,
948                    err, nt_errstr(status)));
949
950         if (!NT_STATUS_IS_OK(status)) {
951                 tevent_req_nterror(subreq, status);
952                 return;
953         }
954         tevent_req_done(subreq);
955 }