2 Unix SMB/Netbios implementation.
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/>.
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 The buffer we keep around whilst an aio request is in process.
30 *****************************************************************************/
34 struct smb_request
*smbreq
;
36 struct lock_struct lock
;
42 /****************************************************************************
43 Accessor function to return write_through state.
44 *****************************************************************************/
46 bool aio_write_through_requested(struct aio_extra
*aio_ex
)
48 return aio_ex
->write_through
;
51 /****************************************************************************
52 Create the extended aio struct we must keep around for the lifetime
54 *****************************************************************************/
56 static struct aio_extra
*create_aio_extra(TALLOC_CTX
*mem_ctx
,
60 struct aio_extra
*aio_ex
= talloc_zero(mem_ctx
, struct aio_extra
);
66 /* The output buffer stored in the aio_ex is the start of
67 the smb return buffer. The buffer used in the acb
68 is the start of the reply data portion of that buffer. */
71 aio_ex
->outbuf
= data_blob_talloc(aio_ex
, NULL
, buflen
);
72 if (!aio_ex
->outbuf
.data
) {
81 struct aio_req_fsp_link
{
83 struct tevent_req
*req
;
86 static int aio_del_req_from_fsp(struct aio_req_fsp_link
*lnk
)
89 files_struct
*fsp
= lnk
->fsp
;
90 struct tevent_req
*req
= lnk
->req
;
92 for (i
=0; i
<fsp
->num_aio_requests
; i
++) {
93 if (fsp
->aio_requests
[i
] == req
) {
97 if (i
== fsp
->num_aio_requests
) {
98 DEBUG(1, ("req %p not found in fsp %p\n", req
, fsp
));
101 fsp
->num_aio_requests
-= 1;
102 fsp
->aio_requests
[i
] = fsp
->aio_requests
[fsp
->num_aio_requests
];
104 if (fsp
->num_aio_requests
== 0) {
105 tevent_wait_done(fsp
->deferred_close
);
110 bool aio_add_req_to_fsp(files_struct
*fsp
, struct tevent_req
*req
)
113 struct aio_req_fsp_link
*lnk
;
115 lnk
= talloc(req
, struct aio_req_fsp_link
);
120 array_len
= talloc_array_length(fsp
->aio_requests
);
121 if (array_len
<= fsp
->num_aio_requests
) {
122 struct tevent_req
**tmp
;
124 tmp
= talloc_realloc(
125 fsp
, fsp
->aio_requests
, struct tevent_req
*,
126 fsp
->num_aio_requests
+1);
131 fsp
->aio_requests
= tmp
;
133 fsp
->aio_requests
[fsp
->num_aio_requests
] = req
;
134 fsp
->num_aio_requests
+= 1;
138 talloc_set_destructor(lnk
, aio_del_req_from_fsp
);
143 static void aio_pread_smb1_done(struct tevent_req
*req
);
145 /****************************************************************************
146 Set up an aio request from a SMBreadX call.
147 *****************************************************************************/
149 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
150 struct smb_request
*smbreq
,
151 files_struct
*fsp
, off_t startpos
,
154 struct aio_extra
*aio_ex
;
156 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
157 struct tevent_req
*req
;
159 if (fsp
->base_fsp
!= NULL
) {
160 /* No AIO on streams yet */
161 DEBUG(10, ("AIO on streams not yet supported\n"));
162 return NT_STATUS_RETRY
;
165 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
166 && !SMB_VFS_AIO_FORCE(fsp
)) {
167 /* Too small a read for aio request. */
168 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
169 "for minimum aio_read of %u\n",
170 (unsigned int)smb_maxcnt
,
171 (unsigned int)min_aio_read_size
));
172 return NT_STATUS_RETRY
;
175 /* Only do this on non-chained and non-chaining reads not using the
177 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
178 return NT_STATUS_RETRY
;
181 /* The following is safe from integer wrap as we've already checked
182 smb_maxcnt is 128k or less. Wct is 12 for read replies */
184 bufsize
= smb_size
+ 12 * 2 + smb_maxcnt
+ 1 /* padding byte */;
186 if ((aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
)) == NULL
) {
187 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
188 return NT_STATUS_NO_MEMORY
;
191 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
192 srv_set_message((char *)aio_ex
->outbuf
.data
, 12, 0, True
);
193 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
194 SCVAL(smb_buf(aio_ex
->outbuf
.data
), 0, 0); /* padding byte */
196 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
197 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
200 /* Take the lock until the AIO completes. */
201 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &aio_ex
->lock
)) {
203 return NT_STATUS_FILE_LOCK_CONFLICT
;
206 aio_ex
->nbyte
= smb_maxcnt
;
207 aio_ex
->offset
= startpos
;
209 req
= SMB_VFS_PREAD_SEND(aio_ex
, smbreq
->ev_ctx
,
211 smb_buf(aio_ex
->outbuf
.data
) + 1 /* pad */,
212 smb_maxcnt
, startpos
);
214 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
215 "Error %s\n", strerror(errno
) ));
217 return NT_STATUS_RETRY
;
219 tevent_req_set_callback(req
, aio_pread_smb1_done
, aio_ex
);
221 if (!aio_add_req_to_fsp(fsp
, req
)) {
222 DEBUG(1, ("Could not add req to fsp\n"));
224 return NT_STATUS_RETRY
;
227 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
229 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
230 "offset %.0f, len = %u (mid = %u)\n",
231 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
232 (unsigned int)aio_ex
->smbreq
->mid
));
237 static void aio_pread_smb1_done(struct tevent_req
*req
)
239 struct aio_extra
*aio_ex
= tevent_req_callback_data(
240 req
, struct aio_extra
);
241 files_struct
*fsp
= aio_ex
->fsp
;
243 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
245 struct vfs_aio_state vfs_aio_state
;
247 nread
= SMB_VFS_PREAD_RECV(req
, &vfs_aio_state
);
250 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread
,
251 (nread
== -1) ? strerror(vfs_aio_state
.error
) : "no error"));
254 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
255 "aio outstanding (mid[%llu]).\n",
256 (unsigned long long)aio_ex
->smbreq
->mid
));
262 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
263 "Error = %s\n", fsp_str_dbg(fsp
), (int)nread
,
264 strerror(vfs_aio_state
.error
)));
266 ERROR_NT(map_nt_error_from_unix(vfs_aio_state
.error
));
267 outsize
= srv_set_message(outbuf
,0,0,true);
269 outsize
= setup_readX_header(outbuf
, nread
);
271 aio_ex
->fsp
->fh
->pos
= aio_ex
->offset
+ nread
;
272 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
274 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
275 "nread=%d\n", fsp_str_dbg(fsp
),
276 (int)aio_ex
->nbyte
, (int)nread
) );
281 DBG_INFO("Invalid outsize (%zu)\n", outsize
);
286 _smb_setlen_large(outbuf
, outsize
);
289 if (!srv_send_smb(aio_ex
->smbreq
->xconn
, outbuf
,
290 true, aio_ex
->smbreq
->seqnum
+1,
291 IS_CONN_ENCRYPTED(fsp
->conn
), NULL
)) {
292 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
296 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
297 "for file %s, offset %.0f, len = %u\n",
298 fsp_str_dbg(fsp
), (double)aio_ex
->offset
,
299 (unsigned int)nread
));
304 struct pwrite_fsync_state
{
305 struct tevent_context
*ev
;
311 static void pwrite_fsync_write_done(struct tevent_req
*subreq
);
312 static void pwrite_fsync_sync_done(struct tevent_req
*subreq
);
314 static struct tevent_req
*pwrite_fsync_send(TALLOC_CTX
*mem_ctx
,
315 struct tevent_context
*ev
,
316 struct files_struct
*fsp
,
318 size_t n
, off_t offset
,
321 struct tevent_req
*req
, *subreq
;
322 struct pwrite_fsync_state
*state
;
324 req
= tevent_req_create(mem_ctx
, &state
, struct pwrite_fsync_state
);
330 state
->write_through
= write_through
;
332 subreq
= SMB_VFS_PWRITE_SEND(state
, ev
, fsp
, data
, n
, offset
);
333 if (tevent_req_nomem(subreq
, req
)) {
334 return tevent_req_post(req
, ev
);
336 tevent_req_set_callback(subreq
, pwrite_fsync_write_done
, req
);
340 static void pwrite_fsync_write_done(struct tevent_req
*subreq
)
342 struct tevent_req
*req
= tevent_req_callback_data(
343 subreq
, struct tevent_req
);
344 struct pwrite_fsync_state
*state
= tevent_req_data(
345 req
, struct pwrite_fsync_state
);
346 connection_struct
*conn
= state
->fsp
->conn
;
348 struct vfs_aio_state vfs_aio_state
;
350 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &vfs_aio_state
);
352 if (state
->nwritten
== -1) {
353 tevent_req_error(req
, vfs_aio_state
.error
);
357 do_sync
= (lp_strict_sync(SNUM(conn
)) &&
358 (lp_sync_always(SNUM(conn
)) || state
->write_through
));
360 tevent_req_done(req
);
364 subreq
= SMB_VFS_FSYNC_SEND(state
, state
->ev
, state
->fsp
);
365 if (tevent_req_nomem(subreq
, req
)) {
368 tevent_req_set_callback(subreq
, pwrite_fsync_sync_done
, req
);
371 static void pwrite_fsync_sync_done(struct tevent_req
*subreq
)
373 struct tevent_req
*req
= tevent_req_callback_data(
374 subreq
, struct tevent_req
);
376 struct vfs_aio_state vfs_aio_state
;
378 ret
= SMB_VFS_FSYNC_RECV(subreq
, &vfs_aio_state
);
381 tevent_req_error(req
, vfs_aio_state
.error
);
384 tevent_req_done(req
);
387 static ssize_t
pwrite_fsync_recv(struct tevent_req
*req
, int *perr
)
389 struct pwrite_fsync_state
*state
= tevent_req_data(
390 req
, struct pwrite_fsync_state
);
392 if (tevent_req_is_unix_error(req
, perr
)) {
395 return state
->nwritten
;
398 static void aio_pwrite_smb1_done(struct tevent_req
*req
);
400 /****************************************************************************
401 Set up an aio request from a SMBwriteX call.
402 *****************************************************************************/
404 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
405 struct smb_request
*smbreq
,
406 files_struct
*fsp
, const char *data
,
410 struct aio_extra
*aio_ex
;
412 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
413 struct tevent_req
*req
;
415 if (fsp
->base_fsp
!= NULL
) {
416 /* No AIO on streams yet */
417 DEBUG(10, ("AIO on streams not yet supported\n"));
418 return NT_STATUS_RETRY
;
421 if ((!min_aio_write_size
|| (numtowrite
< min_aio_write_size
))
422 && !SMB_VFS_AIO_FORCE(fsp
)) {
423 /* Too small a write for aio request. */
424 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
425 "small for minimum aio_write of %u\n",
426 (unsigned int)numtowrite
,
427 (unsigned int)min_aio_write_size
));
428 return NT_STATUS_RETRY
;
431 /* Only do this on non-chained and non-chaining writes not using the
433 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
434 return NT_STATUS_RETRY
;
437 bufsize
= smb_size
+ 6*2;
439 if (!(aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
))) {
440 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
441 return NT_STATUS_NO_MEMORY
;
443 aio_ex
->write_through
= BITSETW(smbreq
->vwv
+7,0);
445 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
446 srv_set_message((char *)aio_ex
->outbuf
.data
, 6, 0, True
);
447 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
449 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
450 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
453 /* Take the lock until the AIO completes. */
454 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &aio_ex
->lock
)) {
456 return NT_STATUS_FILE_LOCK_CONFLICT
;
459 aio_ex
->nbyte
= numtowrite
;
460 aio_ex
->offset
= startpos
;
462 req
= pwrite_fsync_send(aio_ex
, smbreq
->ev_ctx
, fsp
,
463 data
, numtowrite
, startpos
,
464 aio_ex
->write_through
);
466 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
467 "Error %s\n", strerror(errno
) ));
469 return NT_STATUS_RETRY
;
471 tevent_req_set_callback(req
, aio_pwrite_smb1_done
, aio_ex
);
473 if (!aio_add_req_to_fsp(fsp
, req
)) {
474 DEBUG(1, ("Could not add req to fsp\n"));
476 return NT_STATUS_RETRY
;
479 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
481 /* This should actually be improved to span the write. */
482 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
483 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
485 if (!aio_ex
->write_through
&& !lp_sync_always(SNUM(fsp
->conn
))
486 && fsp
->aio_write_behind
) {
487 /* Lie to the client and immediately claim we finished the
489 SSVAL(aio_ex
->outbuf
.data
,smb_vwv2
,numtowrite
);
490 SSVAL(aio_ex
->outbuf
.data
,smb_vwv4
,(numtowrite
>>16)&1);
491 show_msg((char *)aio_ex
->outbuf
.data
);
492 if (!srv_send_smb(aio_ex
->smbreq
->xconn
,
493 (char *)aio_ex
->outbuf
.data
,
494 true, aio_ex
->smbreq
->seqnum
+1,
495 IS_CONN_ENCRYPTED(fsp
->conn
),
496 &aio_ex
->smbreq
->pcd
)) {
497 exit_server_cleanly("schedule_aio_write_and_X: "
498 "srv_send_smb failed.");
500 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
501 "behind for file %s\n", fsp_str_dbg(fsp
)));
504 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
505 "%s, offset %.0f, len = %u (mid = %u)\n",
506 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)numtowrite
,
507 (unsigned int)aio_ex
->smbreq
->mid
));
512 static void aio_pwrite_smb1_done(struct tevent_req
*req
)
514 struct aio_extra
*aio_ex
= tevent_req_callback_data(
515 req
, struct aio_extra
);
516 files_struct
*fsp
= aio_ex
->fsp
;
517 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
518 ssize_t numtowrite
= aio_ex
->nbyte
;
522 nwritten
= pwrite_fsync_recv(req
, &err
);
525 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten
,
526 (nwritten
== -1) ? strerror(err
) : "no error"));
529 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
530 "aio outstanding (mid[%llu]).\n",
531 (unsigned long long)aio_ex
->smbreq
->mid
));
536 mark_file_modified(fsp
);
538 if (fsp
->aio_write_behind
) {
540 if (nwritten
!= numtowrite
) {
541 if (nwritten
== -1) {
542 DEBUG(5,("handle_aio_write_complete: "
543 "aio_write_behind failed ! File %s "
544 "is corrupt ! Error %s\n",
545 fsp_str_dbg(fsp
), strerror(err
)));
547 DEBUG(0,("handle_aio_write_complete: "
548 "aio_write_behind failed ! File %s "
549 "is corrupt ! Wanted %u bytes but "
550 "only wrote %d\n", fsp_str_dbg(fsp
),
551 (unsigned int)numtowrite
,
555 DEBUG(10,("handle_aio_write_complete: "
556 "aio_write_behind completed for file %s\n",
559 /* TODO: should no return success in case of an error !!! */
564 /* We don't need outsize or set_message here as we've already set the
565 fixed size length when we set up the aio call. */
567 if (nwritten
== -1) {
568 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
569 "nwritten == %d. Error = %s\n",
570 fsp_str_dbg(fsp
), (unsigned int)numtowrite
,
571 (int)nwritten
, strerror(err
)));
573 ERROR_NT(map_nt_error_from_unix(err
));
574 srv_set_message(outbuf
,0,0,true);
576 SSVAL(outbuf
,smb_vwv2
,nwritten
);
577 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
578 if (nwritten
< (ssize_t
)numtowrite
) {
579 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
580 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
583 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
584 fsp_fnum_dbg(fsp
), (int)numtowrite
, (int)nwritten
));
586 aio_ex
->fsp
->fh
->pos
= aio_ex
->offset
+ nwritten
;
590 if (!srv_send_smb(aio_ex
->smbreq
->xconn
, outbuf
,
591 true, aio_ex
->smbreq
->seqnum
+1,
592 IS_CONN_ENCRYPTED(fsp
->conn
),
594 exit_server_cleanly("handle_aio_write_complete: "
595 "srv_send_smb failed.");
598 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
599 "for file %s, offset %.0f, requested %u, written = %u\n",
600 fsp_str_dbg(fsp
), (double)aio_ex
->offset
,
601 (unsigned int)numtowrite
, (unsigned int)nwritten
));
606 bool cancel_smb2_aio(struct smb_request
*smbreq
)
608 struct smbd_smb2_request
*smb2req
= smbreq
->smb2req
;
609 struct aio_extra
*aio_ex
= NULL
;
612 aio_ex
= talloc_get_type(smbreq
->async_priv
,
616 if (aio_ex
== NULL
) {
620 if (aio_ex
->fsp
== NULL
) {
625 * We let the aio request run. Setting fsp to NULL has the
626 * effect that the _done routines don't send anything out.
633 static void aio_pread_smb2_done(struct tevent_req
*req
);
635 /****************************************************************************
636 Set up an aio request from a SMB2 read call.
637 *****************************************************************************/
639 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
640 struct smb_request
*smbreq
,
647 struct aio_extra
*aio_ex
;
648 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
649 struct tevent_req
*req
;
651 if (fsp
->base_fsp
!= NULL
) {
652 /* No AIO on streams yet */
653 DEBUG(10, ("AIO on streams not yet supported\n"));
654 return NT_STATUS_RETRY
;
657 if (fsp
->op
== NULL
) {
658 /* No AIO on internal opens. */
659 return NT_STATUS_RETRY
;
662 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
663 && !SMB_VFS_AIO_FORCE(fsp
)) {
664 /* Too small a read for aio request. */
665 DEBUG(10,("smb2: read size (%u) too small "
666 "for minimum aio_read of %u\n",
667 (unsigned int)smb_maxcnt
,
668 (unsigned int)min_aio_read_size
));
669 return NT_STATUS_RETRY
;
672 /* Only do this on reads not using the write cache. */
673 if (lp_write_cache_size(SNUM(conn
)) != 0) {
674 return NT_STATUS_RETRY
;
677 if (smbd_smb2_is_compound(smbreq
->smb2req
)) {
678 return NT_STATUS_RETRY
;
681 /* Create the out buffer. */
682 *preadbuf
= data_blob_talloc(ctx
, NULL
, smb_maxcnt
);
683 if (preadbuf
->data
== NULL
) {
684 return NT_STATUS_NO_MEMORY
;
687 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
688 return NT_STATUS_NO_MEMORY
;
691 init_strict_lock_struct(fsp
, fsp
->op
->global
->open_persistent_id
,
692 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
695 /* Take the lock until the AIO completes. */
696 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &aio_ex
->lock
)) {
698 return NT_STATUS_FILE_LOCK_CONFLICT
;
701 aio_ex
->nbyte
= smb_maxcnt
;
702 aio_ex
->offset
= startpos
;
704 req
= SMB_VFS_PREAD_SEND(aio_ex
, smbreq
->ev_ctx
, fsp
,
705 preadbuf
->data
, smb_maxcnt
, startpos
);
707 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
708 "Error %s\n", strerror(errno
)));
710 return NT_STATUS_RETRY
;
712 tevent_req_set_callback(req
, aio_pread_smb2_done
, aio_ex
);
714 if (!aio_add_req_to_fsp(fsp
, req
)) {
715 DEBUG(1, ("Could not add req to fsp\n"));
717 return NT_STATUS_RETRY
;
720 /* We don't need talloc_move here as both aio_ex and
721 * smbreq are children of smbreq->smb2req. */
722 aio_ex
->smbreq
= smbreq
;
723 smbreq
->async_priv
= aio_ex
;
725 DEBUG(10,("smb2: scheduled aio_read for file %s, "
726 "offset %.0f, len = %u (mid = %u)\n",
727 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
728 (unsigned int)aio_ex
->smbreq
->mid
));
733 static void aio_pread_smb2_done(struct tevent_req
*req
)
735 struct aio_extra
*aio_ex
= tevent_req_callback_data(
736 req
, struct aio_extra
);
737 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
738 files_struct
*fsp
= aio_ex
->fsp
;
741 struct vfs_aio_state vfs_aio_state
= { 0 };
743 nread
= SMB_VFS_PREAD_RECV(req
, &vfs_aio_state
);
746 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread
,
747 (nread
== -1) ? strerror(vfs_aio_state
.error
) : "no error"));
750 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
751 __func__
, (uintmax_t)aio_ex
->smbreq
->mid
));
753 tevent_req_nterror(subreq
, NT_STATUS_INTERNAL_ERROR
);
757 /* Common error or success code processing for async or sync
760 status
= smb2_read_complete(subreq
, nread
, vfs_aio_state
.error
);
763 fsp
->fh
->pos
= aio_ex
->offset
+ nread
;
764 fsp
->fh
->position_information
= fsp
->fh
->pos
;
767 DEBUG(10, ("smb2: scheduled aio_read completed "
768 "for file %s, offset %.0f, len = %u "
769 "(errcode = %d, NTSTATUS = %s)\n",
770 fsp_str_dbg(aio_ex
->fsp
),
771 (double)aio_ex
->offset
,
773 vfs_aio_state
.error
, nt_errstr(status
)));
775 if (!NT_STATUS_IS_OK(status
)) {
776 tevent_req_nterror(subreq
, status
);
779 tevent_req_done(subreq
);
782 static void aio_pwrite_smb2_done(struct tevent_req
*req
);
784 /****************************************************************************
785 Set up an aio request from a SMB2write call.
786 *****************************************************************************/
788 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
789 struct smb_request
*smbreq
,
795 struct aio_extra
*aio_ex
= NULL
;
796 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
797 struct tevent_req
*req
;
799 if (fsp
->base_fsp
!= NULL
) {
800 /* No AIO on streams yet */
801 DEBUG(10, ("AIO on streams not yet supported\n"));
802 return NT_STATUS_RETRY
;
805 if (fsp
->op
== NULL
) {
806 /* No AIO on internal opens. */
807 return NT_STATUS_RETRY
;
810 if ((!min_aio_write_size
|| (in_data
.length
< min_aio_write_size
))
811 && !SMB_VFS_AIO_FORCE(fsp
)) {
812 /* Too small a write for aio request. */
813 DEBUG(10,("smb2: write size (%u) too "
814 "small for minimum aio_write of %u\n",
815 (unsigned int)in_data
.length
,
816 (unsigned int)min_aio_write_size
));
817 return NT_STATUS_RETRY
;
820 /* Only do this on writes not using the write cache. */
821 if (lp_write_cache_size(SNUM(conn
)) != 0) {
822 return NT_STATUS_RETRY
;
825 if (smbd_smb2_is_compound(smbreq
->smb2req
)) {
826 return NT_STATUS_RETRY
;
829 if (smbreq
->unread_bytes
) {
830 /* Can't do async with recvfile. */
831 return NT_STATUS_RETRY
;
834 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
835 return NT_STATUS_NO_MEMORY
;
838 aio_ex
->write_through
= write_through
;
840 init_strict_lock_struct(fsp
, fsp
->op
->global
->open_persistent_id
,
841 in_offset
, (uint64_t)in_data
.length
, WRITE_LOCK
,
844 /* Take the lock until the AIO completes. */
845 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &aio_ex
->lock
)) {
847 return NT_STATUS_FILE_LOCK_CONFLICT
;
850 aio_ex
->nbyte
= in_data
.length
;
851 aio_ex
->offset
= in_offset
;
853 req
= pwrite_fsync_send(aio_ex
, smbreq
->ev_ctx
, fsp
,
854 in_data
.data
, in_data
.length
, in_offset
,
857 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
858 "Error %s\n", strerror(errno
)));
860 return NT_STATUS_RETRY
;
862 tevent_req_set_callback(req
, aio_pwrite_smb2_done
, aio_ex
);
864 if (!aio_add_req_to_fsp(fsp
, req
)) {
865 DEBUG(1, ("Could not add req to fsp\n"));
867 return NT_STATUS_RETRY
;
870 /* We don't need talloc_move here as both aio_ex and
871 * smbreq are children of smbreq->smb2req. */
872 aio_ex
->smbreq
= smbreq
;
873 smbreq
->async_priv
= aio_ex
;
875 /* This should actually be improved to span the write. */
876 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
877 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
880 * We don't want to do write behind due to ownership
881 * issues of the request structs. Maybe add it if I
882 * figure those out. JRA.
885 DEBUG(10,("smb2: scheduled aio_write for file "
886 "%s, offset %.0f, len = %u (mid = %u)\n",
889 (unsigned int)in_data
.length
,
890 (unsigned int)aio_ex
->smbreq
->mid
));
895 static void aio_pwrite_smb2_done(struct tevent_req
*req
)
897 struct aio_extra
*aio_ex
= tevent_req_callback_data(
898 req
, struct aio_extra
);
899 ssize_t numtowrite
= aio_ex
->nbyte
;
900 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
901 files_struct
*fsp
= aio_ex
->fsp
;
906 nwritten
= pwrite_fsync_recv(req
, &err
);
909 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten
,
910 (nwritten
== -1) ? strerror(err
) : "no error"));
913 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
914 __func__
, (uintmax_t)aio_ex
->smbreq
->mid
));
916 tevent_req_nterror(subreq
, NT_STATUS_INTERNAL_ERROR
);
920 mark_file_modified(fsp
);
922 status
= smb2_write_complete_nosync(subreq
, nwritten
, err
);
924 DEBUG(10, ("smb2: scheduled aio_write completed "
925 "for file %s, offset %.0f, requested %u, "
926 "written = %u (errcode = %d, NTSTATUS = %s)\n",
928 (double)aio_ex
->offset
,
929 (unsigned int)numtowrite
,
930 (unsigned int)nwritten
,
931 err
, nt_errstr(status
)));
933 if (!NT_STATUS_IS_OK(status
)) {
934 tevent_req_nterror(subreq
, status
);
937 tevent_req_done(subreq
);