s3/vfs: rename SMB_VFS_STRICT_LOCK to SMB_VFS_STRICT_LOCK_CHECK
[Samba.git] / source3 / smbd / aio.c
blobf455d04266aa44c9d611fd061f9df1d9a5113852
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.
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.
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.
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/>.
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"
28 /****************************************************************************
29 Statics plus accessor functions.
30 *****************************************************************************/
32 static int outstanding_aio_calls;
34 int get_outstanding_aio_calls(void)
36 return outstanding_aio_calls;
39 void increment_outstanding_aio_calls(void)
41 outstanding_aio_calls++;
44 void decrement_outstanding_aio_calls(void)
46 outstanding_aio_calls--;
49 /****************************************************************************
50 The buffer we keep around whilst an aio request is in process.
51 *****************************************************************************/
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;
63 /****************************************************************************
64 Accessor function to return write_through state.
65 *****************************************************************************/
67 bool aio_write_through_requested(struct aio_extra *aio_ex)
69 return aio_ex->write_through;
72 static int aio_extra_destructor(struct aio_extra *aio_ex)
74 decrement_outstanding_aio_calls();
75 return 0;
78 /****************************************************************************
79 Create the extended aio struct we must keep around for the lifetime
80 of the aio call.
81 *****************************************************************************/
83 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
84 files_struct *fsp,
85 size_t buflen)
87 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
89 if (!aio_ex) {
90 return NULL;
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. */
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;
104 talloc_set_destructor(aio_ex, aio_extra_destructor);
105 aio_ex->fsp = fsp;
106 increment_outstanding_aio_calls();
107 return aio_ex;
110 struct aio_req_fsp_link {
111 files_struct *fsp;
112 struct tevent_req *req;
115 static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
117 unsigned i;
118 files_struct *fsp = lnk->fsp;
119 struct tevent_req *req = lnk->req;
121 for (i=0; i<fsp->num_aio_requests; i++) {
122 if (fsp->aio_requests[i] == req) {
123 break;
126 if (i == fsp->num_aio_requests) {
127 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
128 return 0;
130 fsp->num_aio_requests -= 1;
131 fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
133 if (fsp->num_aio_requests == 0) {
134 tevent_wait_done(fsp->deferred_close);
136 return 0;
139 bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
141 size_t array_len;
142 struct aio_req_fsp_link *lnk;
144 lnk = talloc(req, struct aio_req_fsp_link);
145 if (lnk == NULL) {
146 return false;
149 array_len = talloc_array_length(fsp->aio_requests);
150 if (array_len <= fsp->num_aio_requests) {
151 struct tevent_req **tmp;
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;
160 fsp->aio_requests = tmp;
162 fsp->aio_requests[fsp->num_aio_requests] = req;
163 fsp->num_aio_requests += 1;
165 lnk->fsp = fsp;
166 lnk->req = req;
167 talloc_set_destructor(lnk, aio_del_req_from_fsp);
169 return true;
172 static void aio_pread_smb1_done(struct tevent_req *req);
174 /****************************************************************************
175 Set up an aio request from a SMBreadX call.
176 *****************************************************************************/
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)
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;
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;
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;
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;
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 */
213 bufsize = smb_size + 12 * 2 + smb_maxcnt + 1 /* padding byte */;
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;
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 */
225 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
226 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
227 &aio_ex->lock);
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;
235 aio_ex->nbyte = smb_maxcnt;
236 aio_ex->offset = startpos;
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;
248 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
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;
256 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
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 ));
263 return NT_STATUS_OK;
266 static void aio_pread_smb1_done(struct tevent_req *req)
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;
276 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
277 TALLOC_FREE(req);
279 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
280 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
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;
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)));
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);
300 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
301 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
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 ) );
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.");
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));
322 TALLOC_FREE(aio_ex);
325 struct pwrite_fsync_state {
326 struct tevent_context *ev;
327 files_struct *fsp;
328 bool write_through;
329 ssize_t nwritten;
332 static void pwrite_fsync_write_done(struct tevent_req *subreq);
333 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
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)
342 struct tevent_req *req, *subreq;
343 struct pwrite_fsync_state *state;
345 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
346 if (req == NULL) {
347 return NULL;
349 state->ev = ev;
350 state->fsp = fsp;
351 state->write_through = write_through;
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);
357 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
358 return req;
361 static void pwrite_fsync_write_done(struct tevent_req *subreq)
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;
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;
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;
385 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
386 if (tevent_req_nomem(subreq, req)) {
387 return;
389 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
392 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
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;
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;
405 tevent_req_done(req);
408 static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
410 struct pwrite_fsync_state *state = tevent_req_data(
411 req, struct pwrite_fsync_state);
413 if (tevent_req_is_unix_error(req, perr)) {
414 return -1;
416 return state->nwritten;
419 static void aio_pwrite_smb1_done(struct tevent_req *req);
421 /****************************************************************************
422 Set up an aio request from a SMBwriteX call.
423 *****************************************************************************/
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)
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;
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;
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;
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;
458 bufsize = smb_size + 6*2;
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;
464 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
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. */
470 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
471 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
472 &aio_ex->lock);
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;
480 aio_ex->nbyte = numtowrite;
481 aio_ex->offset = startpos;
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;
492 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
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;
500 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
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);
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.");
521 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
522 "behind for file %s\n", fsp_str_dbg(fsp)));
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() ));
532 return NT_STATUS_OK;
535 static void aio_pwrite_smb1_done(struct tevent_req *req)
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;
545 nwritten = pwrite_fsync_recv(req, &err);
546 TALLOC_FREE(req);
548 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
549 (nwritten == -1) ? strerror(err) : "no error"));
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;
559 mark_file_modified(fsp);
561 if (fsp->aio_write_behind) {
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 ));
577 } else {
578 DEBUG(10,("handle_aio_write_complete: "
579 "aio_write_behind completed for file %s\n",
580 fsp_str_dbg(fsp)));
582 /* TODO: should no return success in case of an error !!! */
583 TALLOC_FREE(aio_ex);
584 return;
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. */
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)));
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);
606 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
607 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
609 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
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.");
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));
626 TALLOC_FREE(aio_ex);
629 bool cancel_smb2_aio(struct smb_request *smbreq)
631 struct smbd_smb2_request *smb2req = smbreq->smb2req;
632 struct aio_extra *aio_ex = NULL;
634 if (smb2req) {
635 aio_ex = talloc_get_type(smbreq->async_priv,
636 struct aio_extra);
639 if (aio_ex == NULL) {
640 return false;
643 if (aio_ex->fsp == NULL) {
644 return false;
648 * We let the aio request run. Setting fsp to NULL has the
649 * effect that the _done routines don't send anything out.
652 aio_ex->fsp = NULL;
653 return true;
656 static void aio_pread_smb2_done(struct tevent_req *req);
658 /****************************************************************************
659 Set up an aio request from a SMB2 read call.
660 *****************************************************************************/
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)
670 struct aio_extra *aio_ex;
671 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
672 struct tevent_req *req;
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;
680 if (fsp->op == NULL) {
681 /* No AIO on internal opens. */
682 return NT_STATUS_RETRY;
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;
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;
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;
706 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
707 return NT_STATUS_NO_MEMORY;
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);
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;
720 aio_ex->nbyte = smb_maxcnt;
721 aio_ex->offset = startpos;
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;
731 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
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;
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;
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 ));
749 return NT_STATUS_OK;
752 static void aio_pread_smb2_done(struct tevent_req *req)
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 };
762 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
763 TALLOC_FREE(req);
765 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
766 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
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;
776 /* Common error or success code processing for async or sync
777 read returns. */
779 status = smb2_read_complete(subreq, nread, vfs_aio_state.error);
781 if (nread > 0) {
782 fsp->fh->pos = aio_ex->offset + nread;
783 fsp->fh->position_information = fsp->fh->pos;
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)));
794 if (!NT_STATUS_IS_OK(status)) {
795 tevent_req_nterror(subreq, status);
796 return;
798 tevent_req_done(subreq);
801 static void aio_pwrite_smb2_done(struct tevent_req *req);
803 /****************************************************************************
804 Set up an aio request from a SMB2write call.
805 *****************************************************************************/
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)
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;
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;
824 if (fsp->op == NULL) {
825 /* No AIO on internal opens. */
826 return NT_STATUS_RETRY;
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;
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;
844 if (smbreq->unread_bytes) {
845 /* Can't do async with recvfile. */
846 return NT_STATUS_RETRY;
849 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
850 return NT_STATUS_NO_MEMORY;
853 aio_ex->write_through = write_through;
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);
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;
865 aio_ex->nbyte = in_data.length;
866 aio_ex->offset = in_offset;
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;
877 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
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;
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;
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);
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.
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() ));
909 return NT_STATUS_OK;
912 static void aio_pwrite_smb2_done(struct tevent_req *req)
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;
923 nwritten = pwrite_fsync_recv(req, &err);
924 TALLOC_FREE(req);
926 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
927 (nwritten == -1) ? strerror(err) : "no error"));
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;
937 mark_file_modified(fsp);
939 status = smb2_write_complete_nosync(subreq, nwritten, err);
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)));
950 if (!NT_STATUS_IS_OK(status)) {
951 tevent_req_nterror(subreq, status);
952 return;
954 tevent_req_done(subreq);