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"
27 /****************************************************************************
28 The buffer we keep around whilst an aio request is in process.
29 *****************************************************************************/
33 struct smb_request
*smbreq
;
35 struct lock_struct lock
;
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
--;
56 /****************************************************************************
57 Create the extended aio struct we must keep around for the lifetime
59 *****************************************************************************/
61 static struct aio_extra
*create_aio_extra(TALLOC_CTX
*mem_ctx
,
65 struct aio_extra
*aio_ex
= talloc_zero(mem_ctx
, struct aio_extra
);
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. */
76 aio_ex
->outbuf
= data_blob_talloc(aio_ex
, NULL
, buflen
);
77 if (!aio_ex
->outbuf
.data
) {
82 talloc_set_destructor(aio_ex
, aio_extra_destructor
);
84 outstanding_aio_calls
++;
88 struct aio_req_fsp_link
{
90 struct tevent_req
*req
;
93 static int aio_del_req_from_fsp(struct aio_req_fsp_link
*lnk
)
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
) {
104 if (i
== fsp
->num_aio_requests
) {
105 DEBUG(1, ("req %p not found in fsp %p\n", req
, fsp
));
108 fsp
->num_aio_requests
-= 1;
109 fsp
->aio_requests
[i
] = fsp
->aio_requests
[fsp
->num_aio_requests
];
113 static bool aio_add_req_to_fsp(files_struct
*fsp
, struct tevent_req
*req
)
116 struct aio_req_fsp_link
*lnk
;
118 lnk
= talloc(req
, struct aio_req_fsp_link
);
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);
134 fsp
->aio_requests
= tmp
;
136 fsp
->aio_requests
[fsp
->num_aio_requests
] = req
;
137 fsp
->num_aio_requests
+= 1;
141 talloc_set_destructor(lnk
, aio_del_req_from_fsp
);
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
,
157 struct aio_extra
*aio_ex
;
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
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
,
209 /* Take the lock until the AIO completes. */
210 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
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
);
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
);
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
);
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
));
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
;
253 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
254 char *data
= smb_buf(outbuf
);
258 nread
= SMB_VFS_PREAD_RECV(req
, &err
);
261 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread
,
262 (nread
== -1) ? strerror(err
) : "no error"));
265 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
266 "aio outstanding (mid[%llu]).\n",
267 (unsigned long long)aio_ex
->smbreq
->mid
));
272 /* Unlock now we're done. */
273 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
276 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
277 "Error = %s\n", fsp_str_dbg(fsp
), (int)nread
,
280 ERROR_NT(map_nt_error_from_unix(err
));
281 outsize
= srv_set_message(outbuf
,0,0,true);
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);
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 "
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
));
315 struct pwrite_fsync_state
{
316 struct tevent_context
*ev
;
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
,
329 size_t n
, off_t offset
,
332 struct tevent_req
*req
, *subreq
;
333 struct pwrite_fsync_state
*state
;
335 req
= tevent_req_create(mem_ctx
, &state
, struct pwrite_fsync_state
);
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
);
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
;
361 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &err
);
363 if (state
->nwritten
== -1) {
364 tevent_req_error(req
, err
);
368 do_sync
= (lp_strict_sync(SNUM(conn
)) &&
369 (lp_syncalways(SNUM(conn
)) || state
->write_through
));
371 tevent_req_done(req
);
375 subreq
= SMB_VFS_FSYNC_SEND(state
, state
->ev
, state
->fsp
);
376 if (tevent_req_nomem(subreq
, req
)) {
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
);
388 ret
= SMB_VFS_FSYNC_RECV(subreq
, &err
);
391 tevent_req_error(req
, err
);
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
)) {
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
,
420 struct aio_extra
*aio_ex
;
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
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 "
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
,
476 /* Take the lock until the AIO completes. */
477 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
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
);
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
);
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
);
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
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
));
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
;
548 nwritten
= pwrite_fsync_recv(req
, &err
);
551 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten
,
552 (nwritten
== -1) ? strerror(err
) : "no error"));
555 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
556 "aio outstanding (mid[%llu]).\n",
557 (unsigned long long)aio_ex
->smbreq
->mid
));
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
)));
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
,
584 DEBUG(10,("handle_aio_write_complete: "
585 "aio_write_behind completed for file %s\n",
588 /* TODO: should no return success in case of an error !!! */
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);
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
;
619 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
620 true, aio_ex
->smbreq
->seqnum
+1,
621 IS_CONN_ENCRYPTED(fsp
->conn
),
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
));
635 bool cancel_smb2_aio(struct smb_request
*smbreq
)
637 struct smbd_smb2_request
*smb2req
= smbreq
->smb2req
;
638 struct aio_extra
*aio_ex
= NULL
;
641 aio_ex
= talloc_get_type(smbreq
->async_priv
,
645 if (aio_ex
== NULL
) {
649 if (aio_ex
->fsp
== NULL
) {
654 * We let the aio request run. Setting fsp to NULL has the
655 * effect that the _done routines don't send anything out.
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
,
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
,
722 /* Take the lock until the AIO completes. */
723 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
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
);
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
);
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
);
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
));
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
;
772 nread
= SMB_VFS_PREAD_RECV(req
, &err
);
775 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread
,
776 (nread
== -1) ? strerror(err
) : "no error"));
779 DEBUG( 3, ("aio_pread_smb2_done: file closed whilst "
780 "aio outstanding (mid[%llu]).\n",
781 (unsigned long long)aio_ex
->smbreq
->mid
));
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
794 status
= smb2_read_complete(subreq
, nread
, err
);
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
,
807 err
, nt_errstr(status
)));
809 if (!NT_STATUS_IS_OK(status
)) {
810 tevent_req_nterror(subreq
, status
);
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
,
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
,
871 /* Take the lock until the AIO completes. */
872 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
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
,
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
);
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
);
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",
919 (unsigned int)in_data
.length
,
920 (unsigned int)aio_ex
->smbreq
->mid
,
921 outstanding_aio_calls
));
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
;
937 nwritten
= pwrite_fsync_recv(req
, &err
);
940 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten
,
941 (nwritten
== -1) ? strerror(err
) : "no error"));
944 DEBUG( 3, ("aio_pwrite_smb2_done: file closed whilst "
945 "aio outstanding (mid[%llu]).\n",
946 (unsigned long long)aio_ex
->smbreq
->mid
));
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",
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
);
969 tevent_req_done(subreq
);
972 /****************************************************************************
973 Handle any aio completion inline.
974 *****************************************************************************/
976 void aio_fsp_close(files_struct
*fsp
)
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
);