s3-aio: Make the strict sync after write async
[Samba/bjacke.git] / source3 / smbd / aio.c
blob1923ef889fce56552ea8cfdcf32cdd5b5b292f4b
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"
27 /****************************************************************************
28 The buffer we keep around whilst an aio request is in process.
29 *****************************************************************************/
31 struct aio_extra {
32 files_struct *fsp;
33 struct smb_request *smbreq;
34 DATA_BLOB outbuf;
35 struct lock_struct lock;
36 size_t nbyte;
37 off_t offset;
38 bool write_through;
41 /****************************************************************************
42 Accessor function to return write_through state.
43 *****************************************************************************/
45 bool aio_write_through_requested(struct aio_extra *aio_ex)
47 return aio_ex->write_through;
50 static int aio_extra_destructor(struct aio_extra *aio_ex)
52 outstanding_aio_calls--;
53 return 0;
56 /****************************************************************************
57 Create the extended aio struct we must keep around for the lifetime
58 of the aio call.
59 *****************************************************************************/
61 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
62 files_struct *fsp,
63 size_t buflen)
65 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
67 if (!aio_ex) {
68 return NULL;
71 /* The output buffer stored in the aio_ex is the start of
72 the smb return buffer. The buffer used in the acb
73 is the start of the reply data portion of that buffer. */
75 if (buflen) {
76 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
77 if (!aio_ex->outbuf.data) {
78 TALLOC_FREE(aio_ex);
79 return NULL;
82 talloc_set_destructor(aio_ex, aio_extra_destructor);
83 aio_ex->fsp = fsp;
84 outstanding_aio_calls++;
85 return aio_ex;
88 struct aio_req_fsp_link {
89 files_struct *fsp;
90 struct tevent_req *req;
93 static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
95 unsigned i;
96 files_struct *fsp = lnk->fsp;
97 struct tevent_req *req = lnk->req;
99 for (i=0; i<fsp->num_aio_requests; i++) {
100 if (fsp->aio_requests[i] == req) {
101 break;
104 if (i == fsp->num_aio_requests) {
105 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
106 return 0;
108 fsp->num_aio_requests -= 1;
109 fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
110 return 0;
113 static bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
115 size_t array_len;
116 struct aio_req_fsp_link *lnk;
118 lnk = talloc(req, struct aio_req_fsp_link);
119 if (lnk == NULL) {
120 return false;
123 array_len = talloc_array_length(fsp->aio_requests);
124 if (array_len <= fsp->num_aio_requests) {
125 struct tevent_req **tmp;
127 tmp = talloc_realloc(
128 fsp, fsp->aio_requests, struct tevent_req *,
129 fsp->num_aio_requests+1);
130 if (tmp == NULL) {
131 TALLOC_FREE(lnk);
132 return false;
134 fsp->aio_requests = tmp;
136 fsp->aio_requests[fsp->num_aio_requests] = req;
137 fsp->num_aio_requests += 1;
139 lnk->fsp = fsp;
140 lnk->req = req;
141 talloc_set_destructor(lnk, aio_del_req_from_fsp);
143 return true;
146 static void aio_pread_smb1_done(struct tevent_req *req);
148 /****************************************************************************
149 Set up an aio request from a SMBreadX call.
150 *****************************************************************************/
152 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
153 struct smb_request *smbreq,
154 files_struct *fsp, off_t startpos,
155 size_t smb_maxcnt)
157 struct aio_extra *aio_ex;
158 size_t bufsize;
159 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
160 struct tevent_req *req;
162 if (fsp->base_fsp != NULL) {
163 /* No AIO on streams yet */
164 DEBUG(10, ("AIO on streams not yet supported\n"));
165 return NT_STATUS_RETRY;
168 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
169 && !SMB_VFS_AIO_FORCE(fsp)) {
170 /* Too small a read for aio request. */
171 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
172 "for minimum aio_read of %u\n",
173 (unsigned int)smb_maxcnt,
174 (unsigned int)min_aio_read_size ));
175 return NT_STATUS_RETRY;
178 /* Only do this on non-chained and non-chaining reads not using the
179 * write cache. */
180 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
181 return NT_STATUS_RETRY;
184 if (outstanding_aio_calls >= aio_pending_size) {
185 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
186 "activities outstanding.\n",
187 outstanding_aio_calls ));
188 return NT_STATUS_RETRY;
191 /* The following is safe from integer wrap as we've already checked
192 smb_maxcnt is 128k or less. Wct is 12 for read replies */
194 bufsize = smb_size + 12 * 2 + smb_maxcnt;
196 if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
197 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
198 return NT_STATUS_NO_MEMORY;
201 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
202 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
203 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
205 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
206 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
207 &aio_ex->lock);
209 /* Take the lock until the AIO completes. */
210 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
211 TALLOC_FREE(aio_ex);
212 return NT_STATUS_FILE_LOCK_CONFLICT;
215 aio_ex->nbyte = smb_maxcnt;
216 aio_ex->offset = startpos;
218 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx,
219 fsp, smb_buf(aio_ex->outbuf.data),
220 smb_maxcnt, startpos);
221 if (req == NULL) {
222 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
223 "Error %s\n", strerror(errno) ));
224 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
225 TALLOC_FREE(aio_ex);
226 return NT_STATUS_RETRY;
228 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
230 if (!aio_add_req_to_fsp(fsp, req)) {
231 DEBUG(1, ("Could not add req to fsp\n"));
232 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
233 TALLOC_FREE(aio_ex);
234 return NT_STATUS_RETRY;
237 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
239 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
240 "offset %.0f, len = %u (mid = %u)\n",
241 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
242 (unsigned int)aio_ex->smbreq->mid ));
244 return NT_STATUS_OK;
247 static void aio_pread_smb1_done(struct tevent_req *req)
249 struct aio_extra *aio_ex = tevent_req_callback_data(
250 req, struct aio_extra);
251 files_struct *fsp = aio_ex->fsp;
252 int outsize;
253 char *outbuf = (char *)aio_ex->outbuf.data;
254 char *data = smb_buf(outbuf);
255 ssize_t nread;
256 int err;
258 nread = SMB_VFS_PREAD_RECV(req, &err);
259 TALLOC_FREE(req);
261 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
262 (nread == -1) ? strerror(err) : "no error"));
264 if (fsp == NULL) {
265 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
266 "aio outstanding (mid[%llu]).\n",
267 (unsigned long long)aio_ex->smbreq->mid));
268 TALLOC_FREE(aio_ex);
269 return;
272 /* Unlock now we're done. */
273 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
275 if (nread < 0) {
276 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
277 "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
278 strerror(err)));
280 ERROR_NT(map_nt_error_from_unix(err));
281 outsize = srv_set_message(outbuf,0,0,true);
282 } else {
283 outsize = srv_set_message(outbuf, 12, nread, False);
284 SSVAL(outbuf,smb_vwv2, 0xFFFF); /* Remaining - must be * -1. */
285 SSVAL(outbuf,smb_vwv5, nread);
286 SSVAL(outbuf,smb_vwv6, smb_offset(data,outbuf));
287 SSVAL(outbuf,smb_vwv7, ((nread >> 16) & 1));
288 SSVAL(smb_buf(outbuf), -2, nread);
290 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
291 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
293 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
294 "nread=%d\n", fsp_str_dbg(fsp),
295 (int)aio_ex->nbyte, (int)nread ) );
298 smb_setlen(outbuf, outsize - 4);
299 show_msg(outbuf);
300 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
301 true, aio_ex->smbreq->seqnum+1,
302 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
303 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
304 "failed.");
307 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
308 "for file %s, offset %.0f, len = %u\n",
309 fsp_str_dbg(fsp), (double)aio_ex->offset,
310 (unsigned int)nread));
312 TALLOC_FREE(aio_ex);
315 struct pwrite_fsync_state {
316 struct tevent_context *ev;
317 files_struct *fsp;
318 bool write_through;
319 ssize_t nwritten;
322 static void pwrite_fsync_write_done(struct tevent_req *subreq);
323 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
325 static struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
326 struct tevent_context *ev,
327 struct files_struct *fsp,
328 const void *data,
329 size_t n, off_t offset,
330 bool write_through)
332 struct tevent_req *req, *subreq;
333 struct pwrite_fsync_state *state;
335 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
336 if (req == NULL) {
337 return NULL;
339 state->ev = ev;
340 state->fsp = fsp;
341 state->write_through = write_through;
343 subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
344 if (tevent_req_nomem(subreq, req)) {
345 return tevent_req_post(req, ev);
347 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
348 return req;
351 static void pwrite_fsync_write_done(struct tevent_req *subreq)
353 struct tevent_req *req = tevent_req_callback_data(
354 subreq, struct tevent_req);
355 struct pwrite_fsync_state *state = tevent_req_data(
356 req, struct pwrite_fsync_state);
357 connection_struct *conn = state->fsp->conn;
358 int err;
359 bool do_sync;
361 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &err);
362 TALLOC_FREE(subreq);
363 if (state->nwritten == -1) {
364 tevent_req_error(req, err);
365 return;
368 do_sync = (lp_strict_sync(SNUM(conn)) &&
369 (lp_syncalways(SNUM(conn)) || state->write_through));
370 if (!do_sync) {
371 tevent_req_done(req);
372 return;
375 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
376 if (tevent_req_nomem(subreq, req)) {
377 return;
379 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
382 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
384 struct tevent_req *req = tevent_req_callback_data(
385 subreq, struct tevent_req);
386 int ret, err;
388 ret = SMB_VFS_FSYNC_RECV(subreq, &err);
389 TALLOC_FREE(subreq);
390 if (ret == -1) {
391 tevent_req_error(req, err);
392 return;
394 tevent_req_done(req);
397 static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
399 struct pwrite_fsync_state *state = tevent_req_data(
400 req, struct pwrite_fsync_state);
402 if (tevent_req_is_unix_error(req, perr)) {
403 return -1;
405 return state->nwritten;
408 static void aio_pwrite_smb1_done(struct tevent_req *req);
410 /****************************************************************************
411 Set up an aio request from a SMBwriteX call.
412 *****************************************************************************/
414 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
415 struct smb_request *smbreq,
416 files_struct *fsp, const char *data,
417 off_t startpos,
418 size_t numtowrite)
420 struct aio_extra *aio_ex;
421 size_t bufsize;
422 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
423 struct tevent_req *req;
425 if (fsp->base_fsp != NULL) {
426 /* No AIO on streams yet */
427 DEBUG(10, ("AIO on streams not yet supported\n"));
428 return NT_STATUS_RETRY;
431 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
432 && !SMB_VFS_AIO_FORCE(fsp)) {
433 /* Too small a write for aio request. */
434 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
435 "small for minimum aio_write of %u\n",
436 (unsigned int)numtowrite,
437 (unsigned int)min_aio_write_size ));
438 return NT_STATUS_RETRY;
441 /* Only do this on non-chained and non-chaining writes not using the
442 * write cache. */
443 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
444 return NT_STATUS_RETRY;
447 if (outstanding_aio_calls >= aio_pending_size) {
448 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
449 "activities outstanding.\n",
450 outstanding_aio_calls ));
451 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
452 "aio_write for file %s, offset %.0f, len = %u "
453 "(mid = %u)\n",
454 fsp_str_dbg(fsp), (double)startpos,
455 (unsigned int)numtowrite,
456 (unsigned int)smbreq->mid ));
457 return NT_STATUS_RETRY;
460 bufsize = smb_size + 6*2;
462 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
463 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
464 return NT_STATUS_NO_MEMORY;
466 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
468 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
469 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
470 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
472 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
473 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
474 &aio_ex->lock);
476 /* Take the lock until the AIO completes. */
477 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
478 TALLOC_FREE(aio_ex);
479 return NT_STATUS_FILE_LOCK_CONFLICT;
482 aio_ex->nbyte = numtowrite;
483 aio_ex->offset = startpos;
485 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
486 data, numtowrite, startpos,
487 aio_ex->write_through);
488 if (req == NULL) {
489 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
490 "Error %s\n", strerror(errno) ));
491 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
492 TALLOC_FREE(aio_ex);
493 return NT_STATUS_RETRY;
495 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
497 if (!aio_add_req_to_fsp(fsp, req)) {
498 DEBUG(1, ("Could not add req to fsp\n"));
499 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
500 TALLOC_FREE(aio_ex);
501 return NT_STATUS_RETRY;
504 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
506 /* This should actually be improved to span the write. */
507 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
508 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
510 if (!aio_ex->write_through && !lp_syncalways(SNUM(fsp->conn))
511 && fsp->aio_write_behind) {
512 /* Lie to the client and immediately claim we finished the
513 * write. */
514 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
515 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
516 show_msg((char *)aio_ex->outbuf.data);
517 if (!srv_send_smb(aio_ex->smbreq->sconn,
518 (char *)aio_ex->outbuf.data,
519 true, aio_ex->smbreq->seqnum+1,
520 IS_CONN_ENCRYPTED(fsp->conn),
521 &aio_ex->smbreq->pcd)) {
522 exit_server_cleanly("schedule_aio_write_and_X: "
523 "srv_send_smb failed.");
525 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
526 "behind for file %s\n", fsp_str_dbg(fsp)));
529 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
530 "%s, offset %.0f, len = %u (mid = %u) "
531 "outstanding_aio_calls = %d\n",
532 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
533 (unsigned int)aio_ex->smbreq->mid, outstanding_aio_calls ));
535 return NT_STATUS_OK;
538 static void aio_pwrite_smb1_done(struct tevent_req *req)
540 struct aio_extra *aio_ex = tevent_req_callback_data(
541 req, struct aio_extra);
542 files_struct *fsp = aio_ex->fsp;
543 char *outbuf = (char *)aio_ex->outbuf.data;
544 ssize_t numtowrite = aio_ex->nbyte;
545 ssize_t nwritten;
546 int err;
548 nwritten = pwrite_fsync_recv(req, &err);
549 TALLOC_FREE(req);
551 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
552 (nwritten == -1) ? strerror(err) : "no error"));
554 if (fsp == NULL) {
555 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
556 "aio outstanding (mid[%llu]).\n",
557 (unsigned long long)aio_ex->smbreq->mid));
558 TALLOC_FREE(aio_ex);
559 return;
562 /* Unlock now we're done. */
563 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
565 mark_file_modified(fsp);
567 if (fsp->aio_write_behind) {
569 if (nwritten != numtowrite) {
570 if (nwritten == -1) {
571 DEBUG(5,("handle_aio_write_complete: "
572 "aio_write_behind failed ! File %s "
573 "is corrupt ! Error %s\n",
574 fsp_str_dbg(fsp), strerror(err)));
575 } else {
576 DEBUG(0,("handle_aio_write_complete: "
577 "aio_write_behind failed ! File %s "
578 "is corrupt ! Wanted %u bytes but "
579 "only wrote %d\n", fsp_str_dbg(fsp),
580 (unsigned int)numtowrite,
581 (int)nwritten ));
583 } else {
584 DEBUG(10,("handle_aio_write_complete: "
585 "aio_write_behind completed for file %s\n",
586 fsp_str_dbg(fsp)));
588 /* TODO: should no return success in case of an error !!! */
589 TALLOC_FREE(aio_ex);
590 return;
593 /* We don't need outsize or set_message here as we've already set the
594 fixed size length when we set up the aio call. */
596 if (nwritten == -1) {
597 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
598 "nwritten == %d. Error = %s\n",
599 fsp_str_dbg(fsp), (unsigned int)numtowrite,
600 (int)nwritten, strerror(err)));
602 ERROR_NT(map_nt_error_from_unix(err));
603 srv_set_message(outbuf,0,0,true);
604 } else {
605 SSVAL(outbuf,smb_vwv2,nwritten);
606 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
607 if (nwritten < (ssize_t)numtowrite) {
608 SCVAL(outbuf,smb_rcls,ERRHRD);
609 SSVAL(outbuf,smb_err,ERRdiskfull);
612 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
613 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
615 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
618 show_msg(outbuf);
619 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
620 true, aio_ex->smbreq->seqnum+1,
621 IS_CONN_ENCRYPTED(fsp->conn),
622 NULL)) {
623 exit_server_cleanly("handle_aio_write_complete: "
624 "srv_send_smb failed.");
627 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
628 "for file %s, offset %.0f, requested %u, written = %u\n",
629 fsp_str_dbg(fsp), (double)aio_ex->offset,
630 (unsigned int)numtowrite, (unsigned int)nwritten));
632 TALLOC_FREE(aio_ex);
635 bool cancel_smb2_aio(struct smb_request *smbreq)
637 struct smbd_smb2_request *smb2req = smbreq->smb2req;
638 struct aio_extra *aio_ex = NULL;
640 if (smb2req) {
641 aio_ex = talloc_get_type(smbreq->async_priv,
642 struct aio_extra);
645 if (aio_ex == NULL) {
646 return false;
649 if (aio_ex->fsp == NULL) {
650 return false;
654 * We let the aio request run. Setting fsp to NULL has the
655 * effect that the _done routines don't send anything out.
658 aio_ex->fsp = NULL;
659 return true;
662 static void aio_pread_smb2_done(struct tevent_req *req);
664 /****************************************************************************
665 Set up an aio request from a SMB2 read call.
666 *****************************************************************************/
668 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
669 struct smb_request *smbreq,
670 files_struct *fsp,
671 TALLOC_CTX *ctx,
672 DATA_BLOB *preadbuf,
673 off_t startpos,
674 size_t smb_maxcnt)
676 struct aio_extra *aio_ex;
677 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
678 struct tevent_req *req;
680 if (fsp->base_fsp != NULL) {
681 /* No AIO on streams yet */
682 DEBUG(10, ("AIO on streams not yet supported\n"));
683 return NT_STATUS_RETRY;
686 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
687 && !SMB_VFS_AIO_FORCE(fsp)) {
688 /* Too small a read for aio request. */
689 DEBUG(10,("smb2: read size (%u) too small "
690 "for minimum aio_read of %u\n",
691 (unsigned int)smb_maxcnt,
692 (unsigned int)min_aio_read_size ));
693 return NT_STATUS_RETRY;
696 /* Only do this on reads not using the write cache. */
697 if (lp_write_cache_size(SNUM(conn)) != 0) {
698 return NT_STATUS_RETRY;
701 if (outstanding_aio_calls >= aio_pending_size) {
702 DEBUG(10,("smb2: Already have %d aio "
703 "activities outstanding.\n",
704 outstanding_aio_calls ));
705 return NT_STATUS_RETRY;
708 /* Create the out buffer. */
709 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
710 if (preadbuf->data == NULL) {
711 return NT_STATUS_NO_MEMORY;
714 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
715 return NT_STATUS_NO_MEMORY;
718 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
719 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
720 &aio_ex->lock);
722 /* Take the lock until the AIO completes. */
723 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
724 TALLOC_FREE(aio_ex);
725 return NT_STATUS_FILE_LOCK_CONFLICT;
728 aio_ex->nbyte = smb_maxcnt;
729 aio_ex->offset = startpos;
731 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
732 preadbuf->data, smb_maxcnt, startpos);
733 if (req == NULL) {
734 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
735 "Error %s\n", strerror(errno)));
736 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
737 TALLOC_FREE(aio_ex);
738 return NT_STATUS_RETRY;
740 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
742 if (!aio_add_req_to_fsp(fsp, req)) {
743 DEBUG(1, ("Could not add req to fsp\n"));
744 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
745 TALLOC_FREE(aio_ex);
746 return NT_STATUS_RETRY;
749 /* We don't need talloc_move here as both aio_ex and
750 * smbreq are children of smbreq->smb2req. */
751 aio_ex->smbreq = smbreq;
752 smbreq->async_priv = aio_ex;
754 DEBUG(10,("smb2: scheduled aio_read for file %s, "
755 "offset %.0f, len = %u (mid = %u)\n",
756 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
757 (unsigned int)aio_ex->smbreq->mid ));
759 return NT_STATUS_OK;
762 static void aio_pread_smb2_done(struct tevent_req *req)
764 struct aio_extra *aio_ex = tevent_req_callback_data(
765 req, struct aio_extra);
766 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
767 files_struct *fsp = aio_ex->fsp;
768 NTSTATUS status;
769 ssize_t nread;
770 int err = 0;
772 nread = SMB_VFS_PREAD_RECV(req, &err);
773 TALLOC_FREE(req);
775 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
776 (nread == -1) ? strerror(err) : "no error"));
778 if (fsp == NULL) {
779 DEBUG( 3, ("aio_pread_smb2_done: file closed whilst "
780 "aio outstanding (mid[%llu]).\n",
781 (unsigned long long)aio_ex->smbreq->mid));
782 TALLOC_FREE(aio_ex);
783 return;
786 /* Unlock now we're done. */
787 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
789 mark_file_modified(fsp);
791 /* Common error or success code processing for async or sync
792 read returns. */
794 status = smb2_read_complete(subreq, nread, err);
796 if (nread > 0) {
797 fsp->fh->pos = aio_ex->offset + nread;
798 fsp->fh->position_information = fsp->fh->pos;
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 err, nt_errstr(status)));
809 if (!NT_STATUS_IS_OK(status)) {
810 tevent_req_nterror(subreq, status);
811 return;
813 tevent_req_done(subreq);
816 static void aio_pwrite_smb2_done(struct tevent_req *req);
818 /****************************************************************************
819 Set up an aio request from a SMB2write call.
820 *****************************************************************************/
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)
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;
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;
839 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
840 && !SMB_VFS_AIO_FORCE(fsp)) {
841 /* Too small a write for aio request. */
842 DEBUG(10,("smb2: write size (%u) too "
843 "small for minimum aio_write of %u\n",
844 (unsigned int)in_data.length,
845 (unsigned int)min_aio_write_size ));
846 return NT_STATUS_RETRY;
849 /* Only do this on writes not using the write cache. */
850 if (lp_write_cache_size(SNUM(conn)) != 0) {
851 return NT_STATUS_RETRY;
854 if (outstanding_aio_calls >= aio_pending_size) {
855 DEBUG(3,("smb2: Already have %d aio "
856 "activities outstanding.\n",
857 outstanding_aio_calls ));
858 return NT_STATUS_RETRY;
861 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
862 return NT_STATUS_NO_MEMORY;
865 aio_ex->write_through = write_through;
867 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
868 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
869 &aio_ex->lock);
871 /* Take the lock until the AIO completes. */
872 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
873 TALLOC_FREE(aio_ex);
874 return NT_STATUS_FILE_LOCK_CONFLICT;
877 aio_ex->nbyte = in_data.length;
878 aio_ex->offset = in_offset;
880 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
881 in_data.data, in_data.length, in_offset,
882 write_through);
883 if (req == NULL) {
884 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
885 "Error %s\n", strerror(errno)));
886 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
887 TALLOC_FREE(aio_ex);
888 return NT_STATUS_RETRY;
890 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
892 if (!aio_add_req_to_fsp(fsp, req)) {
893 DEBUG(1, ("Could not add req to fsp\n"));
894 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
895 TALLOC_FREE(aio_ex);
896 return NT_STATUS_RETRY;
899 /* We don't need talloc_move here as both aio_ex and
900 * smbreq are children of smbreq->smb2req. */
901 aio_ex->smbreq = smbreq;
902 smbreq->async_priv = aio_ex;
904 /* This should actually be improved to span the write. */
905 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
906 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
909 * We don't want to do write behind due to ownership
910 * issues of the request structs. Maybe add it if I
911 * figure those out. JRA.
914 DEBUG(10,("smb2: scheduled aio_write for file "
915 "%s, offset %.0f, len = %u (mid = %u) "
916 "outstanding_aio_calls = %d\n",
917 fsp_str_dbg(fsp),
918 (double)in_offset,
919 (unsigned int)in_data.length,
920 (unsigned int)aio_ex->smbreq->mid,
921 outstanding_aio_calls ));
923 return NT_STATUS_OK;
926 static void aio_pwrite_smb2_done(struct tevent_req *req)
928 struct aio_extra *aio_ex = tevent_req_callback_data(
929 req, struct aio_extra);
930 ssize_t numtowrite = aio_ex->nbyte;
931 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
932 files_struct *fsp = aio_ex->fsp;
933 NTSTATUS status;
934 ssize_t nwritten;
935 int err = 0;
937 nwritten = pwrite_fsync_recv(req, &err);
938 TALLOC_FREE(req);
940 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
941 (nwritten == -1) ? strerror(err) : "no error"));
943 if (fsp == NULL) {
944 DEBUG( 3, ("aio_pwrite_smb2_done: file closed whilst "
945 "aio outstanding (mid[%llu]).\n",
946 (unsigned long long)aio_ex->smbreq->mid));
947 TALLOC_FREE(aio_ex);
948 return;
951 /* Unlock now we're done. */
952 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
954 status = smb2_write_complete_nosync(subreq, nwritten, err);
956 DEBUG(10, ("smb2: scheduled aio_write completed "
957 "for file %s, offset %.0f, requested %u, "
958 "written = %u (errcode = %d, NTSTATUS = %s)\n",
959 fsp_str_dbg(fsp),
960 (double)aio_ex->offset,
961 (unsigned int)numtowrite,
962 (unsigned int)nwritten,
963 err, nt_errstr(status)));
965 if (!NT_STATUS_IS_OK(status)) {
966 tevent_req_nterror(subreq, status);
967 return;
969 tevent_req_done(subreq);
972 /****************************************************************************
973 Handle any aio completion inline.
974 *****************************************************************************/
976 void aio_fsp_close(files_struct *fsp)
978 unsigned i;
980 for (i=0; i<fsp->num_aio_requests; i++) {
981 struct tevent_req *req = fsp->aio_requests[i];
982 struct aio_extra *aio_ex = tevent_req_callback_data(
983 req, struct aio_extra);
984 aio_ex->fsp = NULL;