s3: libsmb: Make a comment note that cli_set_ea() needs some internal changes before...
[Samba.git] / source3 / smbd / aio.c
blobff1be13824634f7978cee68ef20e501394b8e973
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(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 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
246 TALLOC_FREE(aio_ex);
247 return NT_STATUS_RETRY;
249 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
251 if (!aio_add_req_to_fsp(fsp, req)) {
252 DEBUG(1, ("Could not add req to fsp\n"));
253 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
254 TALLOC_FREE(aio_ex);
255 return NT_STATUS_RETRY;
258 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
260 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
261 "offset %.0f, len = %u (mid = %u)\n",
262 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
263 (unsigned int)aio_ex->smbreq->mid ));
265 return NT_STATUS_OK;
268 static void aio_pread_smb1_done(struct tevent_req *req)
270 struct aio_extra *aio_ex = tevent_req_callback_data(
271 req, struct aio_extra);
272 files_struct *fsp = aio_ex->fsp;
273 int outsize;
274 char *outbuf = (char *)aio_ex->outbuf.data;
275 ssize_t nread;
276 struct vfs_aio_state vfs_aio_state;
278 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
279 TALLOC_FREE(req);
281 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
282 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
284 if (fsp == NULL) {
285 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
286 "aio outstanding (mid[%llu]).\n",
287 (unsigned long long)aio_ex->smbreq->mid));
288 TALLOC_FREE(aio_ex);
289 return;
292 /* Unlock now we're done. */
293 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
295 if (nread < 0) {
296 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
297 "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
298 strerror(vfs_aio_state.error)));
300 ERROR_NT(map_nt_error_from_unix(vfs_aio_state.error));
301 outsize = srv_set_message(outbuf,0,0,true);
302 } else {
303 outsize = setup_readX_header(outbuf, nread);
305 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
306 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
308 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
309 "nread=%d\n", fsp_str_dbg(fsp),
310 (int)aio_ex->nbyte, (int)nread ) );
313 smb_setlen(outbuf, outsize - 4);
314 show_msg(outbuf);
315 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
316 true, aio_ex->smbreq->seqnum+1,
317 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
318 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
319 "failed.");
322 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
323 "for file %s, offset %.0f, len = %u\n",
324 fsp_str_dbg(fsp), (double)aio_ex->offset,
325 (unsigned int)nread));
327 TALLOC_FREE(aio_ex);
330 struct pwrite_fsync_state {
331 struct tevent_context *ev;
332 files_struct *fsp;
333 bool write_through;
334 ssize_t nwritten;
337 static void pwrite_fsync_write_done(struct tevent_req *subreq);
338 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
340 static struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
341 struct tevent_context *ev,
342 struct files_struct *fsp,
343 const void *data,
344 size_t n, off_t offset,
345 bool write_through)
347 struct tevent_req *req, *subreq;
348 struct pwrite_fsync_state *state;
350 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
351 if (req == NULL) {
352 return NULL;
354 state->ev = ev;
355 state->fsp = fsp;
356 state->write_through = write_through;
358 subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
359 if (tevent_req_nomem(subreq, req)) {
360 return tevent_req_post(req, ev);
362 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
363 return req;
366 static void pwrite_fsync_write_done(struct tevent_req *subreq)
368 struct tevent_req *req = tevent_req_callback_data(
369 subreq, struct tevent_req);
370 struct pwrite_fsync_state *state = tevent_req_data(
371 req, struct pwrite_fsync_state);
372 connection_struct *conn = state->fsp->conn;
373 bool do_sync;
374 struct vfs_aio_state vfs_aio_state;
376 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &vfs_aio_state);
377 TALLOC_FREE(subreq);
378 if (state->nwritten == -1) {
379 tevent_req_error(req, vfs_aio_state.error);
380 return;
383 do_sync = (lp_strict_sync(SNUM(conn)) &&
384 (lp_sync_always(SNUM(conn)) || state->write_through));
385 if (!do_sync) {
386 tevent_req_done(req);
387 return;
390 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
391 if (tevent_req_nomem(subreq, req)) {
392 return;
394 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
397 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
399 struct tevent_req *req = tevent_req_callback_data(
400 subreq, struct tevent_req);
401 int ret;
402 struct vfs_aio_state vfs_aio_state;
404 ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
405 TALLOC_FREE(subreq);
406 if (ret == -1) {
407 tevent_req_error(req, vfs_aio_state.error);
408 return;
410 tevent_req_done(req);
413 static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
415 struct pwrite_fsync_state *state = tevent_req_data(
416 req, struct pwrite_fsync_state);
418 if (tevent_req_is_unix_error(req, perr)) {
419 return -1;
421 return state->nwritten;
424 static void aio_pwrite_smb1_done(struct tevent_req *req);
426 /****************************************************************************
427 Set up an aio request from a SMBwriteX call.
428 *****************************************************************************/
430 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
431 struct smb_request *smbreq,
432 files_struct *fsp, const char *data,
433 off_t startpos,
434 size_t numtowrite)
436 struct aio_extra *aio_ex;
437 size_t bufsize;
438 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
439 struct tevent_req *req;
441 if (fsp->base_fsp != NULL) {
442 /* No AIO on streams yet */
443 DEBUG(10, ("AIO on streams not yet supported\n"));
444 return NT_STATUS_RETRY;
447 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
448 && !SMB_VFS_AIO_FORCE(fsp)) {
449 /* Too small a write for aio request. */
450 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
451 "small for minimum aio_write of %u\n",
452 (unsigned int)numtowrite,
453 (unsigned int)min_aio_write_size ));
454 return NT_STATUS_RETRY;
457 /* Only do this on non-chained and non-chaining writes not using the
458 * write cache. */
459 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
460 return NT_STATUS_RETRY;
463 bufsize = smb_size + 6*2;
465 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
466 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
467 return NT_STATUS_NO_MEMORY;
469 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
471 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
472 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
473 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
475 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
476 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
477 &aio_ex->lock);
479 /* Take the lock until the AIO completes. */
480 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
481 TALLOC_FREE(aio_ex);
482 return NT_STATUS_FILE_LOCK_CONFLICT;
485 aio_ex->nbyte = numtowrite;
486 aio_ex->offset = startpos;
488 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
489 data, numtowrite, startpos,
490 aio_ex->write_through);
491 if (req == NULL) {
492 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
493 "Error %s\n", strerror(errno) ));
494 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
495 TALLOC_FREE(aio_ex);
496 return NT_STATUS_RETRY;
498 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
500 if (!aio_add_req_to_fsp(fsp, req)) {
501 DEBUG(1, ("Could not add req to fsp\n"));
502 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
503 TALLOC_FREE(aio_ex);
504 return NT_STATUS_RETRY;
507 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
509 /* This should actually be improved to span the write. */
510 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
511 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
513 if (!aio_ex->write_through && !lp_sync_always(SNUM(fsp->conn))
514 && fsp->aio_write_behind) {
515 /* Lie to the client and immediately claim we finished the
516 * write. */
517 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
518 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
519 show_msg((char *)aio_ex->outbuf.data);
520 if (!srv_send_smb(aio_ex->smbreq->xconn,
521 (char *)aio_ex->outbuf.data,
522 true, aio_ex->smbreq->seqnum+1,
523 IS_CONN_ENCRYPTED(fsp->conn),
524 &aio_ex->smbreq->pcd)) {
525 exit_server_cleanly("schedule_aio_write_and_X: "
526 "srv_send_smb failed.");
528 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
529 "behind for file %s\n", fsp_str_dbg(fsp)));
532 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
533 "%s, offset %.0f, len = %u (mid = %u) "
534 "outstanding_aio_calls = %d\n",
535 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
536 (unsigned int)aio_ex->smbreq->mid,
537 get_outstanding_aio_calls() ));
539 return NT_STATUS_OK;
542 static void aio_pwrite_smb1_done(struct tevent_req *req)
544 struct aio_extra *aio_ex = tevent_req_callback_data(
545 req, struct aio_extra);
546 files_struct *fsp = aio_ex->fsp;
547 char *outbuf = (char *)aio_ex->outbuf.data;
548 ssize_t numtowrite = aio_ex->nbyte;
549 ssize_t nwritten;
550 int err;
552 nwritten = pwrite_fsync_recv(req, &err);
553 TALLOC_FREE(req);
555 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
556 (nwritten == -1) ? strerror(err) : "no error"));
558 if (fsp == NULL) {
559 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
560 "aio outstanding (mid[%llu]).\n",
561 (unsigned long long)aio_ex->smbreq->mid));
562 TALLOC_FREE(aio_ex);
563 return;
566 /* Unlock now we're done. */
567 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
569 mark_file_modified(fsp);
571 if (fsp->aio_write_behind) {
573 if (nwritten != numtowrite) {
574 if (nwritten == -1) {
575 DEBUG(5,("handle_aio_write_complete: "
576 "aio_write_behind failed ! File %s "
577 "is corrupt ! Error %s\n",
578 fsp_str_dbg(fsp), strerror(err)));
579 } else {
580 DEBUG(0,("handle_aio_write_complete: "
581 "aio_write_behind failed ! File %s "
582 "is corrupt ! Wanted %u bytes but "
583 "only wrote %d\n", fsp_str_dbg(fsp),
584 (unsigned int)numtowrite,
585 (int)nwritten ));
587 } else {
588 DEBUG(10,("handle_aio_write_complete: "
589 "aio_write_behind completed for file %s\n",
590 fsp_str_dbg(fsp)));
592 /* TODO: should no return success in case of an error !!! */
593 TALLOC_FREE(aio_ex);
594 return;
597 /* We don't need outsize or set_message here as we've already set the
598 fixed size length when we set up the aio call. */
600 if (nwritten == -1) {
601 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
602 "nwritten == %d. Error = %s\n",
603 fsp_str_dbg(fsp), (unsigned int)numtowrite,
604 (int)nwritten, strerror(err)));
606 ERROR_NT(map_nt_error_from_unix(err));
607 srv_set_message(outbuf,0,0,true);
608 } else {
609 SSVAL(outbuf,smb_vwv2,nwritten);
610 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
611 if (nwritten < (ssize_t)numtowrite) {
612 SCVAL(outbuf,smb_rcls,ERRHRD);
613 SSVAL(outbuf,smb_err,ERRdiskfull);
616 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
617 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
619 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
622 show_msg(outbuf);
623 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
624 true, aio_ex->smbreq->seqnum+1,
625 IS_CONN_ENCRYPTED(fsp->conn),
626 NULL)) {
627 exit_server_cleanly("handle_aio_write_complete: "
628 "srv_send_smb failed.");
631 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
632 "for file %s, offset %.0f, requested %u, written = %u\n",
633 fsp_str_dbg(fsp), (double)aio_ex->offset,
634 (unsigned int)numtowrite, (unsigned int)nwritten));
636 TALLOC_FREE(aio_ex);
639 bool cancel_smb2_aio(struct smb_request *smbreq)
641 struct smbd_smb2_request *smb2req = smbreq->smb2req;
642 struct aio_extra *aio_ex = NULL;
644 if (smb2req) {
645 aio_ex = talloc_get_type(smbreq->async_priv,
646 struct aio_extra);
649 if (aio_ex == NULL) {
650 return false;
653 if (aio_ex->fsp == NULL) {
654 return false;
658 * We let the aio request run. Setting fsp to NULL has the
659 * effect that the _done routines don't send anything out.
662 aio_ex->fsp = NULL;
663 return true;
666 static void aio_pread_smb2_done(struct tevent_req *req);
668 /****************************************************************************
669 Set up an aio request from a SMB2 read call.
670 *****************************************************************************/
672 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
673 struct smb_request *smbreq,
674 files_struct *fsp,
675 TALLOC_CTX *ctx,
676 DATA_BLOB *preadbuf,
677 off_t startpos,
678 size_t smb_maxcnt)
680 struct aio_extra *aio_ex;
681 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
682 struct tevent_req *req;
684 if (fsp->base_fsp != NULL) {
685 /* No AIO on streams yet */
686 DEBUG(10, ("AIO on streams not yet supported\n"));
687 return NT_STATUS_RETRY;
690 if (fsp->op == NULL) {
691 /* No AIO on internal opens. */
692 return NT_STATUS_RETRY;
695 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
696 && !SMB_VFS_AIO_FORCE(fsp)) {
697 /* Too small a read for aio request. */
698 DEBUG(10,("smb2: read size (%u) too small "
699 "for minimum aio_read of %u\n",
700 (unsigned int)smb_maxcnt,
701 (unsigned int)min_aio_read_size ));
702 return NT_STATUS_RETRY;
705 /* Only do this on reads not using the write cache. */
706 if (lp_write_cache_size(SNUM(conn)) != 0) {
707 return NT_STATUS_RETRY;
710 /* Create the out buffer. */
711 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
712 if (preadbuf->data == NULL) {
713 return NT_STATUS_NO_MEMORY;
716 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
717 return NT_STATUS_NO_MEMORY;
720 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
721 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
722 &aio_ex->lock);
724 /* Take the lock until the AIO completes. */
725 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
726 TALLOC_FREE(aio_ex);
727 return NT_STATUS_FILE_LOCK_CONFLICT;
730 aio_ex->nbyte = smb_maxcnt;
731 aio_ex->offset = startpos;
733 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
734 preadbuf->data, smb_maxcnt, startpos);
735 if (req == NULL) {
736 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
737 "Error %s\n", strerror(errno)));
738 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
739 TALLOC_FREE(aio_ex);
740 return NT_STATUS_RETRY;
742 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
744 if (!aio_add_req_to_fsp(fsp, req)) {
745 DEBUG(1, ("Could not add req to fsp\n"));
746 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
747 TALLOC_FREE(aio_ex);
748 return NT_STATUS_RETRY;
751 /* We don't need talloc_move here as both aio_ex and
752 * smbreq are children of smbreq->smb2req. */
753 aio_ex->smbreq = smbreq;
754 smbreq->async_priv = aio_ex;
756 DEBUG(10,("smb2: scheduled aio_read for file %s, "
757 "offset %.0f, len = %u (mid = %u)\n",
758 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
759 (unsigned int)aio_ex->smbreq->mid ));
761 return NT_STATUS_OK;
764 static void aio_pread_smb2_done(struct tevent_req *req)
766 struct aio_extra *aio_ex = tevent_req_callback_data(
767 req, struct aio_extra);
768 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
769 files_struct *fsp = aio_ex->fsp;
770 NTSTATUS status;
771 ssize_t nread;
772 struct vfs_aio_state vfs_aio_state = { 0 };
774 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
775 TALLOC_FREE(req);
777 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
778 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
780 if (fsp == NULL) {
781 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
782 __func__, (uintmax_t)aio_ex->smbreq->mid));
783 TALLOC_FREE(aio_ex);
784 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
785 return;
788 /* Unlock now we're done. */
789 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
791 /* Common error or success code processing for async or sync
792 read returns. */
794 status = smb2_read_complete(subreq, nread, vfs_aio_state.error);
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 vfs_aio_state.error, 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 (fsp->op == NULL) {
840 /* No AIO on internal opens. */
841 return NT_STATUS_RETRY;
844 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
845 && !SMB_VFS_AIO_FORCE(fsp)) {
846 /* Too small a write for aio request. */
847 DEBUG(10,("smb2: write size (%u) too "
848 "small for minimum aio_write of %u\n",
849 (unsigned int)in_data.length,
850 (unsigned int)min_aio_write_size ));
851 return NT_STATUS_RETRY;
854 /* Only do this on writes not using the write cache. */
855 if (lp_write_cache_size(SNUM(conn)) != 0) {
856 return NT_STATUS_RETRY;
859 if (smbreq->unread_bytes) {
860 /* Can't do async with recvfile. */
861 return NT_STATUS_RETRY;
864 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
865 return NT_STATUS_NO_MEMORY;
868 aio_ex->write_through = write_through;
870 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
871 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
872 &aio_ex->lock);
874 /* Take the lock until the AIO completes. */
875 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
876 TALLOC_FREE(aio_ex);
877 return NT_STATUS_FILE_LOCK_CONFLICT;
880 aio_ex->nbyte = in_data.length;
881 aio_ex->offset = in_offset;
883 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
884 in_data.data, in_data.length, in_offset,
885 write_through);
886 if (req == NULL) {
887 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
888 "Error %s\n", strerror(errno)));
889 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
890 TALLOC_FREE(aio_ex);
891 return NT_STATUS_RETRY;
893 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
895 if (!aio_add_req_to_fsp(fsp, req)) {
896 DEBUG(1, ("Could not add req to fsp\n"));
897 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
898 TALLOC_FREE(aio_ex);
899 return NT_STATUS_RETRY;
902 /* We don't need talloc_move here as both aio_ex and
903 * smbreq are children of smbreq->smb2req. */
904 aio_ex->smbreq = smbreq;
905 smbreq->async_priv = aio_ex;
907 /* This should actually be improved to span the write. */
908 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
909 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
912 * We don't want to do write behind due to ownership
913 * issues of the request structs. Maybe add it if I
914 * figure those out. JRA.
917 DEBUG(10,("smb2: scheduled aio_write for file "
918 "%s, offset %.0f, len = %u (mid = %u) "
919 "outstanding_aio_calls = %d\n",
920 fsp_str_dbg(fsp),
921 (double)in_offset,
922 (unsigned int)in_data.length,
923 (unsigned int)aio_ex->smbreq->mid,
924 get_outstanding_aio_calls() ));
926 return NT_STATUS_OK;
929 static void aio_pwrite_smb2_done(struct tevent_req *req)
931 struct aio_extra *aio_ex = tevent_req_callback_data(
932 req, struct aio_extra);
933 ssize_t numtowrite = aio_ex->nbyte;
934 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
935 files_struct *fsp = aio_ex->fsp;
936 NTSTATUS status;
937 ssize_t nwritten;
938 int err = 0;
940 nwritten = pwrite_fsync_recv(req, &err);
941 TALLOC_FREE(req);
943 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
944 (nwritten == -1) ? strerror(err) : "no error"));
946 if (fsp == NULL) {
947 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
948 __func__, (uintmax_t)aio_ex->smbreq->mid));
949 TALLOC_FREE(aio_ex);
950 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
951 return;
954 /* Unlock now we're done. */
955 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
957 mark_file_modified(fsp);
959 status = smb2_write_complete_nosync(subreq, nwritten, err);
961 DEBUG(10, ("smb2: scheduled aio_write completed "
962 "for file %s, offset %.0f, requested %u, "
963 "written = %u (errcode = %d, NTSTATUS = %s)\n",
964 fsp_str_dbg(fsp),
965 (double)aio_ex->offset,
966 (unsigned int)numtowrite,
967 (unsigned int)nwritten,
968 err, nt_errstr(status)));
970 if (!NT_STATUS_IS_OK(status)) {
971 tevent_req_nterror(subreq, status);
972 return;
974 tevent_req_done(subreq);