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 static int aio_extra_destructor(struct aio_extra
*aio_ex
)
53 outstanding_aio_calls
--;
57 /****************************************************************************
58 Create the extended aio struct we must keep around for the lifetime
60 *****************************************************************************/
62 static struct aio_extra
*create_aio_extra(TALLOC_CTX
*mem_ctx
,
66 struct aio_extra
*aio_ex
= talloc_zero(mem_ctx
, struct aio_extra
);
72 /* The output buffer stored in the aio_ex is the start of
73 the smb return buffer. The buffer used in the acb
74 is the start of the reply data portion of that buffer. */
77 aio_ex
->outbuf
= data_blob_talloc(aio_ex
, NULL
, buflen
);
78 if (!aio_ex
->outbuf
.data
) {
83 talloc_set_destructor(aio_ex
, aio_extra_destructor
);
85 outstanding_aio_calls
++;
89 struct aio_req_fsp_link
{
91 struct tevent_req
*req
;
94 static int aio_del_req_from_fsp(struct aio_req_fsp_link
*lnk
)
97 files_struct
*fsp
= lnk
->fsp
;
98 struct tevent_req
*req
= lnk
->req
;
100 for (i
=0; i
<fsp
->num_aio_requests
; i
++) {
101 if (fsp
->aio_requests
[i
] == req
) {
105 if (i
== fsp
->num_aio_requests
) {
106 DEBUG(1, ("req %p not found in fsp %p\n", req
, fsp
));
109 fsp
->num_aio_requests
-= 1;
110 fsp
->aio_requests
[i
] = fsp
->aio_requests
[fsp
->num_aio_requests
];
112 if (fsp
->num_aio_requests
== 0) {
113 tevent_wait_done(fsp
->deferred_close
);
118 static bool aio_add_req_to_fsp(files_struct
*fsp
, struct tevent_req
*req
)
121 struct aio_req_fsp_link
*lnk
;
123 lnk
= talloc(req
, struct aio_req_fsp_link
);
128 array_len
= talloc_array_length(fsp
->aio_requests
);
129 if (array_len
<= fsp
->num_aio_requests
) {
130 struct tevent_req
**tmp
;
132 tmp
= talloc_realloc(
133 fsp
, fsp
->aio_requests
, struct tevent_req
*,
134 fsp
->num_aio_requests
+1);
139 fsp
->aio_requests
= tmp
;
141 fsp
->aio_requests
[fsp
->num_aio_requests
] = req
;
142 fsp
->num_aio_requests
+= 1;
146 talloc_set_destructor(lnk
, aio_del_req_from_fsp
);
151 static void aio_pread_smb1_done(struct tevent_req
*req
);
153 /****************************************************************************
154 Set up an aio request from a SMBreadX call.
155 *****************************************************************************/
157 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
158 struct smb_request
*smbreq
,
159 files_struct
*fsp
, off_t startpos
,
162 struct aio_extra
*aio_ex
;
164 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
165 struct tevent_req
*req
;
167 if (fsp
->base_fsp
!= NULL
) {
168 /* No AIO on streams yet */
169 DEBUG(10, ("AIO on streams not yet supported\n"));
170 return NT_STATUS_RETRY
;
173 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
174 && !SMB_VFS_AIO_FORCE(fsp
)) {
175 /* Too small a read for aio request. */
176 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
177 "for minimum aio_read of %u\n",
178 (unsigned int)smb_maxcnt
,
179 (unsigned int)min_aio_read_size
));
180 return NT_STATUS_RETRY
;
183 /* Only do this on non-chained and non-chaining reads not using the
185 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
186 return NT_STATUS_RETRY
;
189 if (outstanding_aio_calls
>= aio_pending_size
) {
190 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
191 "activities outstanding.\n",
192 outstanding_aio_calls
));
193 return NT_STATUS_RETRY
;
196 /* The following is safe from integer wrap as we've already checked
197 smb_maxcnt is 128k or less. Wct is 12 for read replies */
199 bufsize
= smb_size
+ 12 * 2 + smb_maxcnt
;
201 if ((aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
)) == NULL
) {
202 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
203 return NT_STATUS_NO_MEMORY
;
206 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
207 srv_set_message((char *)aio_ex
->outbuf
.data
, 12, 0, True
);
208 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
210 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
211 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
214 /* Take the lock until the AIO completes. */
215 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
217 return NT_STATUS_FILE_LOCK_CONFLICT
;
220 aio_ex
->nbyte
= smb_maxcnt
;
221 aio_ex
->offset
= startpos
;
223 req
= SMB_VFS_PREAD_SEND(aio_ex
, fsp
->conn
->sconn
->ev_ctx
,
224 fsp
, smb_buf(aio_ex
->outbuf
.data
),
225 smb_maxcnt
, startpos
);
227 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
228 "Error %s\n", strerror(errno
) ));
229 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
231 return NT_STATUS_RETRY
;
233 tevent_req_set_callback(req
, aio_pread_smb1_done
, aio_ex
);
235 if (!aio_add_req_to_fsp(fsp
, req
)) {
236 DEBUG(1, ("Could not add req to fsp\n"));
237 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
239 return NT_STATUS_RETRY
;
242 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
244 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
245 "offset %.0f, len = %u (mid = %u)\n",
246 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
247 (unsigned int)aio_ex
->smbreq
->mid
));
252 static void aio_pread_smb1_done(struct tevent_req
*req
)
254 struct aio_extra
*aio_ex
= tevent_req_callback_data(
255 req
, struct aio_extra
);
256 files_struct
*fsp
= aio_ex
->fsp
;
258 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
259 char *data
= smb_buf(outbuf
);
263 nread
= SMB_VFS_PREAD_RECV(req
, &err
);
266 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread
,
267 (nread
== -1) ? strerror(err
) : "no error"));
270 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
271 "aio outstanding (mid[%llu]).\n",
272 (unsigned long long)aio_ex
->smbreq
->mid
));
277 /* Unlock now we're done. */
278 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
281 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
282 "Error = %s\n", fsp_str_dbg(fsp
), (int)nread
,
285 ERROR_NT(map_nt_error_from_unix(err
));
286 outsize
= srv_set_message(outbuf
,0,0,true);
288 outsize
= srv_set_message(outbuf
, 12, nread
, False
);
289 SSVAL(outbuf
,smb_vwv2
, 0xFFFF); /* Remaining - must be * -1. */
290 SSVAL(outbuf
,smb_vwv5
, nread
);
291 SSVAL(outbuf
,smb_vwv6
, smb_offset(data
,outbuf
));
292 SSVAL(outbuf
,smb_vwv7
, ((nread
>> 16) & 1));
293 SSVAL(smb_buf(outbuf
), -2, nread
);
295 aio_ex
->fsp
->fh
->pos
= aio_ex
->offset
+ nread
;
296 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
298 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
299 "nread=%d\n", fsp_str_dbg(fsp
),
300 (int)aio_ex
->nbyte
, (int)nread
) );
303 smb_setlen(outbuf
, outsize
- 4);
305 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
306 true, aio_ex
->smbreq
->seqnum
+1,
307 IS_CONN_ENCRYPTED(fsp
->conn
), NULL
)) {
308 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
312 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
313 "for file %s, offset %.0f, len = %u\n",
314 fsp_str_dbg(fsp
), (double)aio_ex
->offset
,
315 (unsigned int)nread
));
320 struct pwrite_fsync_state
{
321 struct tevent_context
*ev
;
327 static void pwrite_fsync_write_done(struct tevent_req
*subreq
);
328 static void pwrite_fsync_sync_done(struct tevent_req
*subreq
);
330 static struct tevent_req
*pwrite_fsync_send(TALLOC_CTX
*mem_ctx
,
331 struct tevent_context
*ev
,
332 struct files_struct
*fsp
,
334 size_t n
, off_t offset
,
337 struct tevent_req
*req
, *subreq
;
338 struct pwrite_fsync_state
*state
;
340 req
= tevent_req_create(mem_ctx
, &state
, struct pwrite_fsync_state
);
346 state
->write_through
= write_through
;
348 subreq
= SMB_VFS_PWRITE_SEND(state
, ev
, fsp
, data
, n
, offset
);
349 if (tevent_req_nomem(subreq
, req
)) {
350 return tevent_req_post(req
, ev
);
352 tevent_req_set_callback(subreq
, pwrite_fsync_write_done
, req
);
356 static void pwrite_fsync_write_done(struct tevent_req
*subreq
)
358 struct tevent_req
*req
= tevent_req_callback_data(
359 subreq
, struct tevent_req
);
360 struct pwrite_fsync_state
*state
= tevent_req_data(
361 req
, struct pwrite_fsync_state
);
362 connection_struct
*conn
= state
->fsp
->conn
;
366 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &err
);
368 if (state
->nwritten
== -1) {
369 tevent_req_error(req
, err
);
373 do_sync
= (lp_strict_sync(SNUM(conn
)) &&
374 (lp_syncalways(SNUM(conn
)) || state
->write_through
));
376 tevent_req_done(req
);
380 subreq
= SMB_VFS_FSYNC_SEND(state
, state
->ev
, state
->fsp
);
381 if (tevent_req_nomem(subreq
, req
)) {
384 tevent_req_set_callback(subreq
, pwrite_fsync_sync_done
, req
);
387 static void pwrite_fsync_sync_done(struct tevent_req
*subreq
)
389 struct tevent_req
*req
= tevent_req_callback_data(
390 subreq
, struct tevent_req
);
393 ret
= SMB_VFS_FSYNC_RECV(subreq
, &err
);
396 tevent_req_error(req
, err
);
399 tevent_req_done(req
);
402 static ssize_t
pwrite_fsync_recv(struct tevent_req
*req
, int *perr
)
404 struct pwrite_fsync_state
*state
= tevent_req_data(
405 req
, struct pwrite_fsync_state
);
407 if (tevent_req_is_unix_error(req
, perr
)) {
410 return state
->nwritten
;
413 static void aio_pwrite_smb1_done(struct tevent_req
*req
);
415 /****************************************************************************
416 Set up an aio request from a SMBwriteX call.
417 *****************************************************************************/
419 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
420 struct smb_request
*smbreq
,
421 files_struct
*fsp
, const char *data
,
425 struct aio_extra
*aio_ex
;
427 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
428 struct tevent_req
*req
;
430 if (fsp
->base_fsp
!= NULL
) {
431 /* No AIO on streams yet */
432 DEBUG(10, ("AIO on streams not yet supported\n"));
433 return NT_STATUS_RETRY
;
436 if ((!min_aio_write_size
|| (numtowrite
< min_aio_write_size
))
437 && !SMB_VFS_AIO_FORCE(fsp
)) {
438 /* Too small a write for aio request. */
439 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
440 "small for minimum aio_write of %u\n",
441 (unsigned int)numtowrite
,
442 (unsigned int)min_aio_write_size
));
443 return NT_STATUS_RETRY
;
446 /* Only do this on non-chained and non-chaining writes not using the
448 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
449 return NT_STATUS_RETRY
;
452 if (outstanding_aio_calls
>= aio_pending_size
) {
453 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
454 "activities outstanding.\n",
455 outstanding_aio_calls
));
456 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
457 "aio_write for file %s, offset %.0f, len = %u "
459 fsp_str_dbg(fsp
), (double)startpos
,
460 (unsigned int)numtowrite
,
461 (unsigned int)smbreq
->mid
));
462 return NT_STATUS_RETRY
;
465 bufsize
= smb_size
+ 6*2;
467 if (!(aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
))) {
468 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
469 return NT_STATUS_NO_MEMORY
;
471 aio_ex
->write_through
= BITSETW(smbreq
->vwv
+7,0);
473 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
474 srv_set_message((char *)aio_ex
->outbuf
.data
, 6, 0, True
);
475 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
477 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
478 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
481 /* Take the lock until the AIO completes. */
482 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
484 return NT_STATUS_FILE_LOCK_CONFLICT
;
487 aio_ex
->nbyte
= numtowrite
;
488 aio_ex
->offset
= startpos
;
490 req
= pwrite_fsync_send(aio_ex
, fsp
->conn
->sconn
->ev_ctx
, fsp
,
491 data
, numtowrite
, startpos
,
492 aio_ex
->write_through
);
494 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
495 "Error %s\n", strerror(errno
) ));
496 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
498 return NT_STATUS_RETRY
;
500 tevent_req_set_callback(req
, aio_pwrite_smb1_done
, aio_ex
);
502 if (!aio_add_req_to_fsp(fsp
, req
)) {
503 DEBUG(1, ("Could not add req to fsp\n"));
504 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
506 return NT_STATUS_RETRY
;
509 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
511 /* This should actually be improved to span the write. */
512 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
513 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
515 if (!aio_ex
->write_through
&& !lp_syncalways(SNUM(fsp
->conn
))
516 && fsp
->aio_write_behind
) {
517 /* Lie to the client and immediately claim we finished the
519 SSVAL(aio_ex
->outbuf
.data
,smb_vwv2
,numtowrite
);
520 SSVAL(aio_ex
->outbuf
.data
,smb_vwv4
,(numtowrite
>>16)&1);
521 show_msg((char *)aio_ex
->outbuf
.data
);
522 if (!srv_send_smb(aio_ex
->smbreq
->sconn
,
523 (char *)aio_ex
->outbuf
.data
,
524 true, aio_ex
->smbreq
->seqnum
+1,
525 IS_CONN_ENCRYPTED(fsp
->conn
),
526 &aio_ex
->smbreq
->pcd
)) {
527 exit_server_cleanly("schedule_aio_write_and_X: "
528 "srv_send_smb failed.");
530 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
531 "behind for file %s\n", fsp_str_dbg(fsp
)));
534 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
535 "%s, offset %.0f, len = %u (mid = %u) "
536 "outstanding_aio_calls = %d\n",
537 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)numtowrite
,
538 (unsigned int)aio_ex
->smbreq
->mid
, outstanding_aio_calls
));
543 static void aio_pwrite_smb1_done(struct tevent_req
*req
)
545 struct aio_extra
*aio_ex
= tevent_req_callback_data(
546 req
, struct aio_extra
);
547 files_struct
*fsp
= aio_ex
->fsp
;
548 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
549 ssize_t numtowrite
= aio_ex
->nbyte
;
553 nwritten
= pwrite_fsync_recv(req
, &err
);
556 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten
,
557 (nwritten
== -1) ? strerror(err
) : "no error"));
560 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
561 "aio outstanding (mid[%llu]).\n",
562 (unsigned long long)aio_ex
->smbreq
->mid
));
567 /* Unlock now we're done. */
568 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
570 mark_file_modified(fsp
);
572 if (fsp
->aio_write_behind
) {
574 if (nwritten
!= numtowrite
) {
575 if (nwritten
== -1) {
576 DEBUG(5,("handle_aio_write_complete: "
577 "aio_write_behind failed ! File %s "
578 "is corrupt ! Error %s\n",
579 fsp_str_dbg(fsp
), strerror(err
)));
581 DEBUG(0,("handle_aio_write_complete: "
582 "aio_write_behind failed ! File %s "
583 "is corrupt ! Wanted %u bytes but "
584 "only wrote %d\n", fsp_str_dbg(fsp
),
585 (unsigned int)numtowrite
,
589 DEBUG(10,("handle_aio_write_complete: "
590 "aio_write_behind completed for file %s\n",
593 /* TODO: should no return success in case of an error !!! */
598 /* We don't need outsize or set_message here as we've already set the
599 fixed size length when we set up the aio call. */
601 if (nwritten
== -1) {
602 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
603 "nwritten == %d. Error = %s\n",
604 fsp_str_dbg(fsp
), (unsigned int)numtowrite
,
605 (int)nwritten
, strerror(err
)));
607 ERROR_NT(map_nt_error_from_unix(err
));
608 srv_set_message(outbuf
,0,0,true);
610 SSVAL(outbuf
,smb_vwv2
,nwritten
);
611 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
612 if (nwritten
< (ssize_t
)numtowrite
) {
613 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
614 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
617 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
618 fsp_fnum_dbg(fsp
), (int)numtowrite
, (int)nwritten
));
620 aio_ex
->fsp
->fh
->pos
= aio_ex
->offset
+ nwritten
;
624 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
625 true, aio_ex
->smbreq
->seqnum
+1,
626 IS_CONN_ENCRYPTED(fsp
->conn
),
628 exit_server_cleanly("handle_aio_write_complete: "
629 "srv_send_smb failed.");
632 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
633 "for file %s, offset %.0f, requested %u, written = %u\n",
634 fsp_str_dbg(fsp
), (double)aio_ex
->offset
,
635 (unsigned int)numtowrite
, (unsigned int)nwritten
));
640 bool cancel_smb2_aio(struct smb_request
*smbreq
)
642 struct smbd_smb2_request
*smb2req
= smbreq
->smb2req
;
643 struct aio_extra
*aio_ex
= NULL
;
646 aio_ex
= talloc_get_type(smbreq
->async_priv
,
650 if (aio_ex
== NULL
) {
654 if (aio_ex
->fsp
== NULL
) {
659 * We let the aio request run. Setting fsp to NULL has the
660 * effect that the _done routines don't send anything out.
667 static void aio_pread_smb2_done(struct tevent_req
*req
);
669 /****************************************************************************
670 Set up an aio request from a SMB2 read call.
671 *****************************************************************************/
673 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
674 struct smb_request
*smbreq
,
681 struct aio_extra
*aio_ex
;
682 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
683 struct tevent_req
*req
;
685 if (fsp
->base_fsp
!= NULL
) {
686 /* No AIO on streams yet */
687 DEBUG(10, ("AIO on streams not yet supported\n"));
688 return NT_STATUS_RETRY
;
691 if (fsp
->op
== NULL
) {
692 /* No AIO on internal opens. */
693 return NT_STATUS_RETRY
;
696 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
697 && !SMB_VFS_AIO_FORCE(fsp
)) {
698 /* Too small a read for aio request. */
699 DEBUG(10,("smb2: read size (%u) too small "
700 "for minimum aio_read of %u\n",
701 (unsigned int)smb_maxcnt
,
702 (unsigned int)min_aio_read_size
));
703 return NT_STATUS_RETRY
;
706 /* Only do this on reads not using the write cache. */
707 if (lp_write_cache_size(SNUM(conn
)) != 0) {
708 return NT_STATUS_RETRY
;
711 if (outstanding_aio_calls
>= aio_pending_size
) {
712 DEBUG(10,("smb2: Already have %d aio "
713 "activities outstanding.\n",
714 outstanding_aio_calls
));
715 return NT_STATUS_RETRY
;
718 /* Create the out buffer. */
719 *preadbuf
= data_blob_talloc(ctx
, NULL
, smb_maxcnt
);
720 if (preadbuf
->data
== NULL
) {
721 return NT_STATUS_NO_MEMORY
;
724 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
725 return NT_STATUS_NO_MEMORY
;
728 init_strict_lock_struct(fsp
, fsp
->op
->global
->open_persistent_id
,
729 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
732 /* Take the lock until the AIO completes. */
733 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
735 return NT_STATUS_FILE_LOCK_CONFLICT
;
738 aio_ex
->nbyte
= smb_maxcnt
;
739 aio_ex
->offset
= startpos
;
741 req
= SMB_VFS_PREAD_SEND(aio_ex
, fsp
->conn
->sconn
->ev_ctx
, fsp
,
742 preadbuf
->data
, smb_maxcnt
, startpos
);
744 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
745 "Error %s\n", strerror(errno
)));
746 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
748 return NT_STATUS_RETRY
;
750 tevent_req_set_callback(req
, aio_pread_smb2_done
, aio_ex
);
752 if (!aio_add_req_to_fsp(fsp
, req
)) {
753 DEBUG(1, ("Could not add req to fsp\n"));
754 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
756 return NT_STATUS_RETRY
;
759 /* We don't need talloc_move here as both aio_ex and
760 * smbreq are children of smbreq->smb2req. */
761 aio_ex
->smbreq
= smbreq
;
762 smbreq
->async_priv
= aio_ex
;
764 DEBUG(10,("smb2: scheduled aio_read for file %s, "
765 "offset %.0f, len = %u (mid = %u)\n",
766 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
767 (unsigned int)aio_ex
->smbreq
->mid
));
772 static void aio_pread_smb2_done(struct tevent_req
*req
)
774 struct aio_extra
*aio_ex
= tevent_req_callback_data(
775 req
, struct aio_extra
);
776 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
777 files_struct
*fsp
= aio_ex
->fsp
;
782 nread
= SMB_VFS_PREAD_RECV(req
, &err
);
785 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread
,
786 (nread
== -1) ? strerror(err
) : "no error"));
789 DEBUG( 3, ("aio_pread_smb2_done: file closed whilst "
790 "aio outstanding (mid[%llu]).\n",
791 (unsigned long long)aio_ex
->smbreq
->mid
));
796 /* Unlock now we're done. */
797 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
799 /* Common error or success code processing for async or sync
802 status
= smb2_read_complete(subreq
, nread
, err
);
805 fsp
->fh
->pos
= aio_ex
->offset
+ nread
;
806 fsp
->fh
->position_information
= fsp
->fh
->pos
;
809 DEBUG(10, ("smb2: scheduled aio_read completed "
810 "for file %s, offset %.0f, len = %u "
811 "(errcode = %d, NTSTATUS = %s)\n",
812 fsp_str_dbg(aio_ex
->fsp
),
813 (double)aio_ex
->offset
,
815 err
, nt_errstr(status
)));
817 if (!NT_STATUS_IS_OK(status
)) {
818 tevent_req_nterror(subreq
, status
);
821 tevent_req_done(subreq
);
824 static void aio_pwrite_smb2_done(struct tevent_req
*req
);
826 /****************************************************************************
827 Set up an aio request from a SMB2write call.
828 *****************************************************************************/
830 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
831 struct smb_request
*smbreq
,
837 struct aio_extra
*aio_ex
= NULL
;
838 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
839 struct tevent_req
*req
;
841 if (fsp
->base_fsp
!= NULL
) {
842 /* No AIO on streams yet */
843 DEBUG(10, ("AIO on streams not yet supported\n"));
844 return NT_STATUS_RETRY
;
847 if (fsp
->op
== NULL
) {
848 /* No AIO on internal opens. */
849 return NT_STATUS_RETRY
;
852 if ((!min_aio_write_size
|| (in_data
.length
< min_aio_write_size
))
853 && !SMB_VFS_AIO_FORCE(fsp
)) {
854 /* Too small a write for aio request. */
855 DEBUG(10,("smb2: write size (%u) too "
856 "small for minimum aio_write of %u\n",
857 (unsigned int)in_data
.length
,
858 (unsigned int)min_aio_write_size
));
859 return NT_STATUS_RETRY
;
862 /* Only do this on writes not using the write cache. */
863 if (lp_write_cache_size(SNUM(conn
)) != 0) {
864 return NT_STATUS_RETRY
;
867 if (outstanding_aio_calls
>= aio_pending_size
) {
868 DEBUG(3,("smb2: Already have %d aio "
869 "activities outstanding.\n",
870 outstanding_aio_calls
));
871 return NT_STATUS_RETRY
;
874 if (smbreq
->unread_bytes
) {
875 /* Can't do async with recvfile. */
876 return NT_STATUS_RETRY
;
879 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
880 return NT_STATUS_NO_MEMORY
;
883 aio_ex
->write_through
= write_through
;
885 init_strict_lock_struct(fsp
, fsp
->op
->global
->open_persistent_id
,
886 in_offset
, (uint64_t)in_data
.length
, WRITE_LOCK
,
889 /* Take the lock until the AIO completes. */
890 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
892 return NT_STATUS_FILE_LOCK_CONFLICT
;
895 aio_ex
->nbyte
= in_data
.length
;
896 aio_ex
->offset
= in_offset
;
898 req
= pwrite_fsync_send(aio_ex
, fsp
->conn
->sconn
->ev_ctx
, fsp
,
899 in_data
.data
, in_data
.length
, in_offset
,
902 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
903 "Error %s\n", strerror(errno
)));
904 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
906 return NT_STATUS_RETRY
;
908 tevent_req_set_callback(req
, aio_pwrite_smb2_done
, aio_ex
);
910 if (!aio_add_req_to_fsp(fsp
, req
)) {
911 DEBUG(1, ("Could not add req to fsp\n"));
912 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
914 return NT_STATUS_RETRY
;
917 /* We don't need talloc_move here as both aio_ex and
918 * smbreq are children of smbreq->smb2req. */
919 aio_ex
->smbreq
= smbreq
;
920 smbreq
->async_priv
= aio_ex
;
922 /* This should actually be improved to span the write. */
923 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
924 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
927 * We don't want to do write behind due to ownership
928 * issues of the request structs. Maybe add it if I
929 * figure those out. JRA.
932 DEBUG(10,("smb2: scheduled aio_write for file "
933 "%s, offset %.0f, len = %u (mid = %u) "
934 "outstanding_aio_calls = %d\n",
937 (unsigned int)in_data
.length
,
938 (unsigned int)aio_ex
->smbreq
->mid
,
939 outstanding_aio_calls
));
944 static void aio_pwrite_smb2_done(struct tevent_req
*req
)
946 struct aio_extra
*aio_ex
= tevent_req_callback_data(
947 req
, struct aio_extra
);
948 ssize_t numtowrite
= aio_ex
->nbyte
;
949 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
950 files_struct
*fsp
= aio_ex
->fsp
;
955 nwritten
= pwrite_fsync_recv(req
, &err
);
958 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten
,
959 (nwritten
== -1) ? strerror(err
) : "no error"));
962 DEBUG( 3, ("aio_pwrite_smb2_done: file closed whilst "
963 "aio outstanding (mid[%llu]).\n",
964 (unsigned long long)aio_ex
->smbreq
->mid
));
969 /* Unlock now we're done. */
970 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
972 status
= smb2_write_complete_nosync(subreq
, nwritten
, err
);
974 DEBUG(10, ("smb2: scheduled aio_write completed "
975 "for file %s, offset %.0f, requested %u, "
976 "written = %u (errcode = %d, NTSTATUS = %s)\n",
978 (double)aio_ex
->offset
,
979 (unsigned int)numtowrite
,
980 (unsigned int)nwritten
,
981 err
, nt_errstr(status
)));
983 if (!NT_STATUS_IS_OK(status
)) {
984 tevent_req_nterror(subreq
, status
);
987 tevent_req_done(subreq
);