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/globals.h"
26 /* The signal we'll use to signify aio done. */
28 #define RT_SIGNAL_AIO (SIGRTMIN+3)
31 #ifndef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIVAL_PTR
32 #ifdef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIGVAL_PTR
33 #define sival_int sigval_int
34 #define sival_ptr sigval_ptr
38 /****************************************************************************
39 The buffer we keep around whilst an aio request is in process.
40 *****************************************************************************/
43 struct aio_extra
*next
, *prev
;
46 struct smb_request
*smbreq
;
48 struct lock_struct lock
;
50 int (*handle_completion
)(struct aio_extra
*ex
, int errcode
);
53 /****************************************************************************
54 Initialize the signal handler for aio read/write.
55 *****************************************************************************/
57 static void smbd_aio_signal_handler(struct tevent_context
*ev_ctx
,
58 struct tevent_signal
*se
,
59 int signum
, int count
,
60 void *_info
, void *private_data
)
62 siginfo_t
*info
= (siginfo_t
*)_info
;
63 struct aio_extra
*aio_ex
= (struct aio_extra
*)
64 info
->si_value
.sival_ptr
;
66 smbd_aio_complete_aio_ex(aio_ex
);
70 static bool initialize_async_io_handler(void)
72 static bool tried_signal_setup
= false;
74 if (aio_signal_event
) {
77 if (tried_signal_setup
) {
80 tried_signal_setup
= true;
82 aio_signal_event
= tevent_add_signal(smbd_event_context(),
84 RT_SIGNAL_AIO
, SA_SIGINFO
,
85 smbd_aio_signal_handler
,
87 if (!aio_signal_event
) {
88 DEBUG(10, ("Failed to setup RT_SIGNAL_AIO handler\n"));
92 /* tevent supports 100 signal with SA_SIGINFO */
93 aio_pending_size
= 100;
97 static int handle_aio_read_complete(struct aio_extra
*aio_ex
, int errcode
);
98 static int handle_aio_write_complete(struct aio_extra
*aio_ex
, int errcode
);
99 static int handle_aio_smb2_read_complete(struct aio_extra
*aio_ex
, int errcode
);
100 static int handle_aio_smb2_write_complete(struct aio_extra
*aio_ex
, int errcode
);
102 static int aio_extra_destructor(struct aio_extra
*aio_ex
)
104 DLIST_REMOVE(aio_list_head
, aio_ex
);
108 /****************************************************************************
109 Create the extended aio struct we must keep around for the lifetime
111 *****************************************************************************/
113 static struct aio_extra
*create_aio_extra(TALLOC_CTX
*mem_ctx
,
117 struct aio_extra
*aio_ex
= TALLOC_ZERO_P(mem_ctx
, struct aio_extra
);
123 /* The output buffer stored in the aio_ex is the start of
124 the smb return buffer. The buffer used in the acb
125 is the start of the reply data portion of that buffer. */
128 aio_ex
->outbuf
= data_blob_talloc(aio_ex
, NULL
, buflen
);
129 if (!aio_ex
->outbuf
.data
) {
134 DLIST_ADD(aio_list_head
, aio_ex
);
135 talloc_set_destructor(aio_ex
, aio_extra_destructor
);
140 /****************************************************************************
141 Set up an aio request from a SMBreadX call.
142 *****************************************************************************/
144 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
145 struct smb_request
*smbreq
,
146 files_struct
*fsp
, SMB_OFF_T startpos
,
149 struct aio_extra
*aio_ex
;
152 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
155 /* Ensure aio is initialized. */
156 if (!initialize_async_io_handler()) {
157 return NT_STATUS_RETRY
;
160 if (fsp
->base_fsp
!= NULL
) {
161 /* No AIO on streams yet */
162 DEBUG(10, ("AIO on streams not yet supported\n"));
163 return NT_STATUS_RETRY
;
166 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
167 && !SMB_VFS_AIO_FORCE(fsp
)) {
168 /* Too small a read for aio request. */
169 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
170 "for minimum aio_read of %u\n",
171 (unsigned int)smb_maxcnt
,
172 (unsigned int)min_aio_read_size
));
173 return NT_STATUS_RETRY
;
176 /* Only do this on non-chained and non-chaining reads not using the
178 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
179 return NT_STATUS_RETRY
;
182 if (outstanding_aio_calls
>= aio_pending_size
) {
183 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
184 "activities outstanding.\n",
185 outstanding_aio_calls
));
186 return NT_STATUS_RETRY
;
189 /* The following is safe from integer wrap as we've already checked
190 smb_maxcnt is 128k or less. Wct is 12 for read replies */
192 bufsize
= smb_size
+ 12 * 2 + smb_maxcnt
;
194 if ((aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
)) == NULL
) {
195 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
196 return NT_STATUS_NO_MEMORY
;
198 aio_ex
->handle_completion
= handle_aio_read_complete
;
200 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
201 srv_set_message((char *)aio_ex
->outbuf
.data
, 12, 0, True
);
202 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
204 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
205 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
208 /* Take the lock until the AIO completes. */
209 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
211 return NT_STATUS_FILE_LOCK_CONFLICT
;
216 /* Now set up the aio record for the read call. */
218 a
->aio_fildes
= fsp
->fh
->fd
;
219 a
->aio_buf
= smb_buf(aio_ex
->outbuf
.data
);
220 a
->aio_nbytes
= smb_maxcnt
;
221 a
->aio_offset
= startpos
;
222 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
223 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
224 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
226 ret
= SMB_VFS_AIO_READ(fsp
, a
);
228 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
229 "Error %s\n", strerror(errno
) ));
230 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
232 return NT_STATUS_RETRY
;
235 outstanding_aio_calls
++;
236 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
238 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
239 "offset %.0f, len = %u (mid = %u)\n",
240 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
241 (unsigned int)aio_ex
->smbreq
->mid
));
246 /****************************************************************************
247 Set up an aio request from a SMBwriteX call.
248 *****************************************************************************/
250 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
251 struct smb_request
*smbreq
,
252 files_struct
*fsp
, char *data
,
256 struct aio_extra
*aio_ex
;
259 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
262 /* Ensure aio is initialized. */
263 if (!initialize_async_io_handler()) {
264 return NT_STATUS_RETRY
;
267 if (fsp
->base_fsp
!= NULL
) {
268 /* No AIO on streams yet */
269 DEBUG(10, ("AIO on streams not yet supported\n"));
270 return NT_STATUS_RETRY
;
273 if ((!min_aio_write_size
|| (numtowrite
< min_aio_write_size
))
274 && !SMB_VFS_AIO_FORCE(fsp
)) {
275 /* Too small a write for aio request. */
276 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
277 "small for minimum aio_write of %u\n",
278 (unsigned int)numtowrite
,
279 (unsigned int)min_aio_write_size
));
280 return NT_STATUS_RETRY
;
283 /* Only do this on non-chained and non-chaining writes not using the
285 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
286 return NT_STATUS_RETRY
;
289 if (outstanding_aio_calls
>= aio_pending_size
) {
290 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
291 "activities outstanding.\n",
292 outstanding_aio_calls
));
293 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
294 "aio_write for file %s, offset %.0f, len = %u "
296 fsp_str_dbg(fsp
), (double)startpos
,
297 (unsigned int)numtowrite
,
298 (unsigned int)smbreq
->mid
));
299 return NT_STATUS_RETRY
;
302 bufsize
= smb_size
+ 6*2;
304 if (!(aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
))) {
305 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
306 return NT_STATUS_NO_MEMORY
;
308 aio_ex
->handle_completion
= handle_aio_write_complete
;
309 aio_ex
->write_through
= BITSETW(smbreq
->vwv
+7,0);
311 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
312 srv_set_message((char *)aio_ex
->outbuf
.data
, 6, 0, True
);
313 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
315 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
316 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
319 /* Take the lock until the AIO completes. */
320 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
322 return NT_STATUS_FILE_LOCK_CONFLICT
;
327 /* Now set up the aio record for the write call. */
329 a
->aio_fildes
= fsp
->fh
->fd
;
331 a
->aio_nbytes
= numtowrite
;
332 a
->aio_offset
= startpos
;
333 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
334 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
335 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
337 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
339 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
340 "Error %s\n", strerror(errno
) ));
341 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
343 return NT_STATUS_RETRY
;
346 outstanding_aio_calls
++;
347 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
349 /* This should actually be improved to span the write. */
350 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
351 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
353 if (!aio_ex
->write_through
&& !lp_syncalways(SNUM(fsp
->conn
))
354 && fsp
->aio_write_behind
) {
355 /* Lie to the client and immediately claim we finished the
357 SSVAL(aio_ex
->outbuf
.data
,smb_vwv2
,numtowrite
);
358 SSVAL(aio_ex
->outbuf
.data
,smb_vwv4
,(numtowrite
>>16)&1);
359 show_msg((char *)aio_ex
->outbuf
.data
);
360 if (!srv_send_smb(aio_ex
->smbreq
->sconn
,
361 (char *)aio_ex
->outbuf
.data
,
362 true, aio_ex
->smbreq
->seqnum
+1,
363 IS_CONN_ENCRYPTED(fsp
->conn
),
364 &aio_ex
->smbreq
->pcd
)) {
365 exit_server_cleanly("schedule_aio_write_and_X: "
366 "srv_send_smb failed.");
368 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
369 "behind for file %s\n", fsp_str_dbg(fsp
)));
372 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
373 "%s, offset %.0f, len = %u (mid = %u) "
374 "outstanding_aio_calls = %d\n",
375 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)numtowrite
,
376 (unsigned int)aio_ex
->smbreq
->mid
, outstanding_aio_calls
));
381 /****************************************************************************
382 Set up an aio request from a SMB2 read call.
383 *****************************************************************************/
385 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
386 struct smb_request
*smbreq
,
392 struct aio_extra
*aio_ex
;
394 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
397 /* Ensure aio is initialized. */
398 if (!initialize_async_io_handler()) {
399 return NT_STATUS_RETRY
;
402 if (fsp
->base_fsp
!= NULL
) {
403 /* No AIO on streams yet */
404 DEBUG(10, ("AIO on streams not yet supported\n"));
405 return NT_STATUS_RETRY
;
408 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
409 && !SMB_VFS_AIO_FORCE(fsp
)) {
410 /* Too small a read for aio request. */
411 DEBUG(10,("smb2: read size (%u) too small "
412 "for minimum aio_read of %u\n",
413 (unsigned int)smb_maxcnt
,
414 (unsigned int)min_aio_read_size
));
415 return NT_STATUS_RETRY
;
418 /* Only do this on reads not using the write cache. */
419 if (lp_write_cache_size(SNUM(conn
)) != 0) {
420 return NT_STATUS_RETRY
;
423 if (outstanding_aio_calls
>= aio_pending_size
) {
424 DEBUG(10,("smb2: Already have %d aio "
425 "activities outstanding.\n",
426 outstanding_aio_calls
));
427 return NT_STATUS_RETRY
;
430 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
431 return NT_STATUS_NO_MEMORY
;
433 aio_ex
->handle_completion
= handle_aio_smb2_read_complete
;
435 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
436 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
439 /* Take the lock until the AIO completes. */
440 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
442 return NT_STATUS_FILE_LOCK_CONFLICT
;
447 /* Now set up the aio record for the read call. */
449 a
->aio_fildes
= fsp
->fh
->fd
;
451 a
->aio_nbytes
= smb_maxcnt
;
452 a
->aio_offset
= startpos
;
453 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
454 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
455 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
457 ret
= SMB_VFS_AIO_READ(fsp
, a
);
459 DEBUG(0,("smb2: aio_read failed. "
460 "Error %s\n", strerror(errno
) ));
461 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
463 return NT_STATUS_RETRY
;
466 outstanding_aio_calls
++;
467 /* We don't need talloc_move here as both aio_ex and
468 * smbreq are children of smbreq->smb2req. */
469 aio_ex
->smbreq
= smbreq
;
471 DEBUG(10,("smb2: scheduled aio_read for file %s, "
472 "offset %.0f, len = %u (mid = %u)\n",
473 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
474 (unsigned int)aio_ex
->smbreq
->mid
));
479 /****************************************************************************
480 Set up an aio request from a SMB2write call.
481 *****************************************************************************/
483 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
484 struct smb_request
*smbreq
,
490 struct aio_extra
*aio_ex
= NULL
;
491 SMB_STRUCT_AIOCB
*a
= NULL
;
492 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
495 /* Ensure aio is initialized. */
496 if (!initialize_async_io_handler()) {
497 return NT_STATUS_RETRY
;
500 if (fsp
->base_fsp
!= NULL
) {
501 /* No AIO on streams yet */
502 DEBUG(10, ("AIO on streams not yet supported\n"));
503 return NT_STATUS_RETRY
;
506 if ((!min_aio_write_size
|| (in_data
.length
< min_aio_write_size
))
507 && !SMB_VFS_AIO_FORCE(fsp
)) {
508 /* Too small a write for aio request. */
509 DEBUG(10,("smb2: write size (%u) too "
510 "small for minimum aio_write of %u\n",
511 (unsigned int)in_data
.length
,
512 (unsigned int)min_aio_write_size
));
513 return NT_STATUS_RETRY
;
516 /* Only do this on writes not using the write cache. */
517 if (lp_write_cache_size(SNUM(conn
)) != 0) {
518 return NT_STATUS_RETRY
;
521 if (outstanding_aio_calls
>= aio_pending_size
) {
522 DEBUG(3,("smb2: Already have %d aio "
523 "activities outstanding.\n",
524 outstanding_aio_calls
));
525 return NT_STATUS_RETRY
;
528 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
529 return NT_STATUS_NO_MEMORY
;
532 aio_ex
->handle_completion
= handle_aio_smb2_write_complete
;
533 aio_ex
->write_through
= write_through
;
535 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
536 in_offset
, (uint64_t)in_data
.length
, WRITE_LOCK
,
539 /* Take the lock until the AIO completes. */
540 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
542 return NT_STATUS_FILE_LOCK_CONFLICT
;
547 /* Now set up the aio record for the write call. */
549 a
->aio_fildes
= fsp
->fh
->fd
;
550 a
->aio_buf
= in_data
.data
;
551 a
->aio_nbytes
= in_data
.length
;
552 a
->aio_offset
= in_offset
;
553 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
554 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
555 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
557 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
559 DEBUG(3,("smb2: aio_write failed. "
560 "Error %s\n", strerror(errno
) ));
561 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
563 return NT_STATUS_RETRY
;
566 outstanding_aio_calls
++;
567 /* We don't need talloc_move here as both aio_ex and
568 * smbreq are children of smbreq->smb2req. */
569 aio_ex
->smbreq
= smbreq
;
571 /* This should actually be improved to span the write. */
572 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
573 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
576 * We don't want to do write behind due to ownership
577 * issues of the request structs. Maybe add it if I
578 * figure those out. JRA.
581 DEBUG(10,("smb2: scheduled aio_write for file "
582 "%s, offset %.0f, len = %u (mid = %u) "
583 "outstanding_aio_calls = %d\n",
586 (unsigned int)in_data
.length
,
587 (unsigned int)aio_ex
->smbreq
->mid
,
588 outstanding_aio_calls
));
593 /****************************************************************************
594 Complete the read and return the data or error back to the client.
595 Returns errno or zero if all ok.
596 *****************************************************************************/
598 static int handle_aio_read_complete(struct aio_extra
*aio_ex
, int errcode
)
601 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
602 char *data
= smb_buf(outbuf
);
603 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
606 /* We're relying here on the fact that if the fd is
607 closed then the aio will complete and aio_return
608 will return an error. Hopefully this is
611 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
613 fsp_str_dbg(aio_ex
->fsp
), (int)nread
, strerror(errcode
)));
615 ERROR_NT(map_nt_error_from_unix(errcode
));
616 outsize
= srv_set_message(outbuf
,0,0,true);
618 outsize
= srv_set_message(outbuf
,12,nread
,False
);
619 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be * -1. */
620 SSVAL(outbuf
,smb_vwv5
,nread
);
621 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
622 SSVAL(outbuf
,smb_vwv7
,((nread
>> 16) & 1));
623 SSVAL(smb_buf(outbuf
),-2,nread
);
625 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
626 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
628 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
630 fsp_str_dbg(aio_ex
->fsp
),
631 (int)aio_ex
->acb
.aio_nbytes
, (int)nread
) );
634 smb_setlen(outbuf
,outsize
- 4);
636 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
637 true, aio_ex
->smbreq
->seqnum
+1,
638 IS_CONN_ENCRYPTED(aio_ex
->fsp
->conn
), NULL
)) {
639 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
643 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
644 "for file %s, offset %.0f, len = %u\n",
645 fsp_str_dbg(aio_ex
->fsp
), (double)aio_ex
->acb
.aio_offset
,
646 (unsigned int)nread
));
651 /****************************************************************************
652 Complete the write and return the data or error back to the client.
653 Returns error code or zero if all ok.
654 *****************************************************************************/
656 static int handle_aio_write_complete(struct aio_extra
*aio_ex
, int errcode
)
658 files_struct
*fsp
= aio_ex
->fsp
;
659 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
660 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
661 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
663 if (fsp
->aio_write_behind
) {
664 if (nwritten
!= numtowrite
) {
665 if (nwritten
== -1) {
666 DEBUG(5,("handle_aio_write_complete: "
667 "aio_write_behind failed ! File %s "
668 "is corrupt ! Error %s\n",
669 fsp_str_dbg(fsp
), strerror(errcode
)));
671 DEBUG(0,("handle_aio_write_complete: "
672 "aio_write_behind failed ! File %s "
673 "is corrupt ! Wanted %u bytes but "
674 "only wrote %d\n", fsp_str_dbg(fsp
),
675 (unsigned int)numtowrite
,
680 DEBUG(10,("handle_aio_write_complete: "
681 "aio_write_behind completed for file %s\n",
684 /* TODO: should no return 0 in case of an error !!! */
688 /* We don't need outsize or set_message here as we've already set the
689 fixed size length when we set up the aio call. */
692 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
693 "nwritten == %d. Error = %s\n",
694 fsp_str_dbg(fsp
), (unsigned int)numtowrite
,
695 (int)nwritten
, strerror(errcode
) ));
697 ERROR_NT(map_nt_error_from_unix(errcode
));
698 srv_set_message(outbuf
,0,0,true);
702 SSVAL(outbuf
,smb_vwv2
,nwritten
);
703 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
704 if (nwritten
< (ssize_t
)numtowrite
) {
705 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
706 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
709 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
710 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
711 status
= sync_file(fsp
->conn
,fsp
, aio_ex
->write_through
);
712 if (!NT_STATUS_IS_OK(status
)) {
714 ERROR_BOTH(map_nt_error_from_unix(errcode
),
715 ERRHRD
, ERRdiskfull
);
716 srv_set_message(outbuf
,0,0,true);
717 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
718 fsp_str_dbg(fsp
), nt_errstr(status
)));
721 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nwritten
;
725 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
726 true, aio_ex
->smbreq
->seqnum
+1,
727 IS_CONN_ENCRYPTED(fsp
->conn
),
729 exit_server_cleanly("handle_aio_write_complete: "
730 "srv_send_smb failed.");
733 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
734 "for file %s, offset %.0f, requested %u, written = %u\n",
735 fsp_str_dbg(fsp
), (double)aio_ex
->acb
.aio_offset
,
736 (unsigned int)numtowrite
, (unsigned int)nwritten
));
741 /****************************************************************************
742 Complete the read and return the data or error back to the client.
743 Returns errno or zero if all ok.
744 *****************************************************************************/
746 static int handle_aio_smb2_read_complete(struct aio_extra
*aio_ex
, int errcode
)
749 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
750 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
752 /* Common error or success code processing for async or sync
755 status
= smb2_read_complete(subreq
, nread
, errcode
);
758 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
759 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
762 DEBUG(10,("smb2: scheduled aio_read completed "
763 "for file %s, offset %.0f, len = %u "
764 "(errcode = %d, NTSTATUS = %s)\n",
765 fsp_str_dbg(aio_ex
->fsp
),
766 (double)aio_ex
->acb
.aio_offset
,
769 nt_errstr(status
) ));
771 if (!NT_STATUS_IS_OK(status
)) {
772 tevent_req_nterror(subreq
, status
);
775 tevent_req_done(subreq
);
779 /****************************************************************************
780 Complete the SMB2 write and return the data or error back to the client.
781 Returns error code or zero if all ok.
782 *****************************************************************************/
784 static int handle_aio_smb2_write_complete(struct aio_extra
*aio_ex
, int errcode
)
786 files_struct
*fsp
= aio_ex
->fsp
;
787 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
788 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
789 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
792 status
= smb2_write_complete(subreq
, nwritten
, errcode
);
794 DEBUG(10,("smb2: scheduled aio_write completed "
795 "for file %s, offset %.0f, requested %u, "
796 "written = %u (errcode = %d, NTSTATUS = %s)\n",
798 (double)aio_ex
->acb
.aio_offset
,
799 (unsigned int)numtowrite
,
800 (unsigned int)nwritten
,
802 nt_errstr(status
) ));
804 if (!NT_STATUS_IS_OK(status
)) {
805 tevent_req_nterror(subreq
, status
);
808 tevent_req_done(subreq
);
812 /****************************************************************************
813 Handle any aio completion. Returns True if finished (and sets *perr if err
814 was non-zero), False if not.
815 *****************************************************************************/
817 static bool handle_aio_completed(struct aio_extra
*aio_ex
, int *perr
)
819 files_struct
*fsp
= NULL
;
823 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
829 /* Ensure the operation has really completed. */
830 err
= SMB_VFS_AIO_ERROR(fsp
, &aio_ex
->acb
);
831 if (err
== EINPROGRESS
) {
832 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
833 "process for file %s\n",
834 (unsigned long long)aio_ex
->smbreq
->mid
,
835 fsp_str_dbg(aio_ex
->fsp
)));
839 /* Unlock now we're done. */
840 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
842 if (err
== ECANCELED
) {
843 /* If error is ECANCELED then don't return anything to the
845 DEBUG(10,( "handle_aio_completed: operation mid %llu"
847 (unsigned long long)aio_ex
->smbreq
->mid
));
851 err
= aio_ex
->handle_completion(aio_ex
, err
);
853 *perr
= err
; /* Only save non-zero errors. */
859 /****************************************************************************
860 Handle any aio completion inline.
861 *****************************************************************************/
863 void smbd_aio_complete_aio_ex(struct aio_extra
*aio_ex
)
865 files_struct
*fsp
= NULL
;
868 outstanding_aio_calls
--;
870 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
871 (unsigned long long)aio_ex
->smbreq
->mid
));
875 /* file was closed whilst I/O was outstanding. Just
877 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
878 "aio outstanding (mid[%llu]).\n",
879 (unsigned long long)aio_ex
->smbreq
->mid
));
883 if (!handle_aio_completed(aio_ex
, &ret
)) {
890 /****************************************************************************
891 We're doing write behind and the client closed the file. Wait up to 30
892 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
893 completed, errno to return if not.
894 *****************************************************************************/
896 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
898 int wait_for_aio_completion(files_struct
*fsp
)
900 struct aio_extra
*aio_ex
;
901 const SMB_STRUCT_AIOCB
**aiocb_list
;
902 int aio_completion_count
= 0;
903 time_t start_time
= time_mono(NULL
);
906 for (seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
;
907 seconds_left
>= 0;) {
912 aio_completion_count
= 0;
913 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
914 if (aio_ex
->fsp
== fsp
) {
915 aio_completion_count
++;
919 if (!aio_completion_count
) {
923 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
924 "to complete.\n", aio_completion_count
));
926 aiocb_list
= SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB
*,
927 aio_completion_count
);
932 for( i
= 0, aio_ex
= aio_list_head
;
934 aio_ex
= aio_ex
->next
) {
935 if (aio_ex
->fsp
== fsp
) {
936 aiocb_list
[i
++] = &aio_ex
->acb
;
940 /* Now wait up to seconds_left for completion. */
941 ts
.tv_sec
= seconds_left
;
944 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
946 aio_completion_count
, seconds_left
));
948 err
= SMB_VFS_AIO_SUSPEND(fsp
, aiocb_list
,
949 aio_completion_count
, &ts
);
951 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
952 "errno = %s\n", err
, strerror(errno
) ));
954 if (err
== -1 && errno
== EAGAIN
) {
955 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
956 "out waiting for %d events after a wait of "
957 "%d seconds\n", aio_completion_count
,
960 cancel_aio_by_fsp(fsp
);
961 SAFE_FREE(aiocb_list
);
965 /* One or more events might have completed - process them if
967 for( i
= 0; i
< aio_completion_count
; i
++) {
968 aio_ex
= (struct aio_extra
*)aiocb_list
[i
]->aio_sigevent
.sigev_value
.sival_ptr
;
970 if (!handle_aio_completed(aio_ex
, &err
)) {
976 SAFE_FREE(aiocb_list
);
977 seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
978 - (time_mono(NULL
) - start_time
);
981 /* We timed out - we don't know why. Return ret if already an error,
983 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
985 aio_completion_count
));
990 /****************************************************************************
991 Cancel any outstanding aio requests. The client doesn't care about the reply.
992 *****************************************************************************/
994 void cancel_aio_by_fsp(files_struct
*fsp
)
996 struct aio_extra
*aio_ex
;
998 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
999 if (aio_ex
->fsp
== fsp
) {
1000 /* Unlock now we're done. */
1001 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
1003 /* Don't delete the aio_extra record as we may have
1004 completed and don't yet know it. Just do the
1005 aio_cancel call and return. */
1006 SMB_VFS_AIO_CANCEL(fsp
, &aio_ex
->acb
);
1007 aio_ex
->fsp
= NULL
; /* fsp will be closed when we
1014 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
1015 struct smb_request
*smbreq
,
1016 files_struct
*fsp
, SMB_OFF_T startpos
,
1019 return NT_STATUS_RETRY
;
1022 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
1023 struct smb_request
*smbreq
,
1024 files_struct
*fsp
, char *data
,
1028 return NT_STATUS_RETRY
;
1031 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
1032 struct smb_request
*smbreq
,
1038 return NT_STATUS_RETRY
;
1041 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
1042 struct smb_request
*smbreq
,
1048 return NT_STATUS_RETRY
;
1051 void cancel_aio_by_fsp(files_struct
*fsp
)
1055 int wait_for_aio_completion(files_struct
*fsp
)
1060 void smbd_aio_complete_mid(uint64_t mid
);