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"
28 /* The signal we'll use to signify aio done. */
30 #define RT_SIGNAL_AIO (SIGRTMIN+3)
33 #ifndef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIVAL_PTR
34 #ifdef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIGVAL_PTR
35 #define sival_int sigval_int
36 #define sival_ptr sigval_ptr
40 /****************************************************************************
41 The buffer we keep around whilst an aio request is in process.
42 *****************************************************************************/
45 struct aio_extra
*next
, *prev
;
48 struct smb_request
*smbreq
;
50 struct lock_struct lock
;
52 int (*handle_completion
)(struct aio_extra
*ex
, int errcode
);
55 /****************************************************************************
56 Initialize the signal handler for aio read/write.
57 *****************************************************************************/
59 static void smbd_aio_signal_handler(struct tevent_context
*ev_ctx
,
60 struct tevent_signal
*se
,
61 int signum
, int count
,
62 void *_info
, void *private_data
)
64 siginfo_t
*info
= (siginfo_t
*)_info
;
65 struct aio_extra
*aio_ex
= (struct aio_extra
*)
66 info
->si_value
.sival_ptr
;
68 smbd_aio_complete_aio_ex(aio_ex
);
73 static bool initialize_async_io_handler(void)
75 static bool tried_signal_setup
= false;
77 if (aio_signal_event
) {
80 if (tried_signal_setup
) {
83 tried_signal_setup
= true;
85 aio_signal_event
= tevent_add_signal(smbd_event_context(),
87 RT_SIGNAL_AIO
, SA_SIGINFO
,
88 smbd_aio_signal_handler
,
90 if (!aio_signal_event
) {
91 DEBUG(10, ("Failed to setup RT_SIGNAL_AIO handler\n"));
95 /* tevent supports 100 signal with SA_SIGINFO */
96 aio_pending_size
= 100;
100 static int handle_aio_read_complete(struct aio_extra
*aio_ex
, int errcode
);
101 static int handle_aio_write_complete(struct aio_extra
*aio_ex
, int errcode
);
102 static int handle_aio_smb2_read_complete(struct aio_extra
*aio_ex
, int errcode
);
103 static int handle_aio_smb2_write_complete(struct aio_extra
*aio_ex
, int errcode
);
105 static int aio_extra_destructor(struct aio_extra
*aio_ex
)
107 DLIST_REMOVE(aio_list_head
, aio_ex
);
111 /****************************************************************************
112 Create the extended aio struct we must keep around for the lifetime
114 *****************************************************************************/
116 static struct aio_extra
*create_aio_extra(TALLOC_CTX
*mem_ctx
,
120 struct aio_extra
*aio_ex
= TALLOC_ZERO_P(mem_ctx
, struct aio_extra
);
126 /* The output buffer stored in the aio_ex is the start of
127 the smb return buffer. The buffer used in the acb
128 is the start of the reply data portion of that buffer. */
131 aio_ex
->outbuf
= data_blob_talloc(aio_ex
, NULL
, buflen
);
132 if (!aio_ex
->outbuf
.data
) {
137 DLIST_ADD(aio_list_head
, aio_ex
);
138 talloc_set_destructor(aio_ex
, aio_extra_destructor
);
143 /****************************************************************************
144 Set up an aio request from a SMBreadX call.
145 *****************************************************************************/
147 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
148 struct smb_request
*smbreq
,
149 files_struct
*fsp
, SMB_OFF_T startpos
,
152 struct aio_extra
*aio_ex
;
155 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
158 /* Ensure aio is initialized. */
159 if (!initialize_async_io_handler()) {
160 return NT_STATUS_RETRY
;
163 if (fsp
->base_fsp
!= NULL
) {
164 /* No AIO on streams yet */
165 DEBUG(10, ("AIO on streams not yet supported\n"));
166 return NT_STATUS_RETRY
;
169 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
170 && !SMB_VFS_AIO_FORCE(fsp
)) {
171 /* Too small a read for aio request. */
172 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
173 "for minimum aio_read of %u\n",
174 (unsigned int)smb_maxcnt
,
175 (unsigned int)min_aio_read_size
));
176 return NT_STATUS_RETRY
;
179 /* Only do this on non-chained and non-chaining reads not using the
181 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
182 return NT_STATUS_RETRY
;
185 if (outstanding_aio_calls
>= aio_pending_size
) {
186 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
187 "activities outstanding.\n",
188 outstanding_aio_calls
));
189 return NT_STATUS_RETRY
;
192 /* The following is safe from integer wrap as we've already checked
193 smb_maxcnt is 128k or less. Wct is 12 for read replies */
195 bufsize
= smb_size
+ 12 * 2 + smb_maxcnt
;
197 if ((aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
)) == NULL
) {
198 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
199 return NT_STATUS_NO_MEMORY
;
201 aio_ex
->handle_completion
= handle_aio_read_complete
;
203 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
204 srv_set_message((char *)aio_ex
->outbuf
.data
, 12, 0, True
);
205 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
207 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
208 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
211 /* Take the lock until the AIO completes. */
212 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
214 return NT_STATUS_FILE_LOCK_CONFLICT
;
219 /* Now set up the aio record for the read call. */
221 a
->aio_fildes
= fsp
->fh
->fd
;
222 a
->aio_buf
= smb_buf(aio_ex
->outbuf
.data
);
223 a
->aio_nbytes
= smb_maxcnt
;
224 a
->aio_offset
= startpos
;
225 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
226 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
227 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
229 ret
= SMB_VFS_AIO_READ(fsp
, a
);
231 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
232 "Error %s\n", strerror(errno
) ));
233 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
235 return NT_STATUS_RETRY
;
238 outstanding_aio_calls
++;
239 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
241 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
242 "offset %.0f, len = %u (mid = %u)\n",
243 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
244 (unsigned int)aio_ex
->smbreq
->mid
));
249 /****************************************************************************
250 Set up an aio request from a SMBwriteX call.
251 *****************************************************************************/
253 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
254 struct smb_request
*smbreq
,
255 files_struct
*fsp
, char *data
,
259 struct aio_extra
*aio_ex
;
262 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
265 /* Ensure aio is initialized. */
266 if (!initialize_async_io_handler()) {
267 return NT_STATUS_RETRY
;
270 if (fsp
->base_fsp
!= NULL
) {
271 /* No AIO on streams yet */
272 DEBUG(10, ("AIO on streams not yet supported\n"));
273 return NT_STATUS_RETRY
;
276 if ((!min_aio_write_size
|| (numtowrite
< min_aio_write_size
))
277 && !SMB_VFS_AIO_FORCE(fsp
)) {
278 /* Too small a write for aio request. */
279 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
280 "small for minimum aio_write of %u\n",
281 (unsigned int)numtowrite
,
282 (unsigned int)min_aio_write_size
));
283 return NT_STATUS_RETRY
;
286 /* Only do this on non-chained and non-chaining writes not using the
288 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
289 return NT_STATUS_RETRY
;
292 if (outstanding_aio_calls
>= aio_pending_size
) {
293 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
294 "activities outstanding.\n",
295 outstanding_aio_calls
));
296 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
297 "aio_write for file %s, offset %.0f, len = %u "
299 fsp_str_dbg(fsp
), (double)startpos
,
300 (unsigned int)numtowrite
,
301 (unsigned int)smbreq
->mid
));
302 return NT_STATUS_RETRY
;
305 bufsize
= smb_size
+ 6*2;
307 if (!(aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
))) {
308 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
309 return NT_STATUS_NO_MEMORY
;
311 aio_ex
->handle_completion
= handle_aio_write_complete
;
312 aio_ex
->write_through
= BITSETW(smbreq
->vwv
+7,0);
314 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
315 srv_set_message((char *)aio_ex
->outbuf
.data
, 6, 0, True
);
316 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
318 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
319 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
322 /* Take the lock until the AIO completes. */
323 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
325 return NT_STATUS_FILE_LOCK_CONFLICT
;
330 /* Now set up the aio record for the write call. */
332 a
->aio_fildes
= fsp
->fh
->fd
;
334 a
->aio_nbytes
= numtowrite
;
335 a
->aio_offset
= startpos
;
336 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
337 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
338 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
340 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
342 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
343 "Error %s\n", strerror(errno
) ));
344 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
346 return NT_STATUS_RETRY
;
349 outstanding_aio_calls
++;
350 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
352 /* This should actually be improved to span the write. */
353 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
354 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
356 if (!aio_ex
->write_through
&& !lp_syncalways(SNUM(fsp
->conn
))
357 && fsp
->aio_write_behind
) {
358 /* Lie to the client and immediately claim we finished the
360 SSVAL(aio_ex
->outbuf
.data
,smb_vwv2
,numtowrite
);
361 SSVAL(aio_ex
->outbuf
.data
,smb_vwv4
,(numtowrite
>>16)&1);
362 show_msg((char *)aio_ex
->outbuf
.data
);
363 if (!srv_send_smb(aio_ex
->smbreq
->sconn
,
364 (char *)aio_ex
->outbuf
.data
,
365 true, aio_ex
->smbreq
->seqnum
+1,
366 IS_CONN_ENCRYPTED(fsp
->conn
),
367 &aio_ex
->smbreq
->pcd
)) {
368 exit_server_cleanly("schedule_aio_write_and_X: "
369 "srv_send_smb failed.");
371 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
372 "behind for file %s\n", fsp_str_dbg(fsp
)));
375 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
376 "%s, offset %.0f, len = %u (mid = %u) "
377 "outstanding_aio_calls = %d\n",
378 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)numtowrite
,
379 (unsigned int)aio_ex
->smbreq
->mid
, outstanding_aio_calls
));
384 /****************************************************************************
385 Set up an aio request from a SMB2 read call.
386 *****************************************************************************/
388 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
389 struct smb_request
*smbreq
,
396 struct aio_extra
*aio_ex
;
398 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
401 /* Ensure aio is initialized. */
402 if (!initialize_async_io_handler()) {
403 return NT_STATUS_RETRY
;
406 if (fsp
->base_fsp
!= NULL
) {
407 /* No AIO on streams yet */
408 DEBUG(10, ("AIO on streams not yet supported\n"));
409 return NT_STATUS_RETRY
;
412 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
413 && !SMB_VFS_AIO_FORCE(fsp
)) {
414 /* Too small a read for aio request. */
415 DEBUG(10,("smb2: read size (%u) too small "
416 "for minimum aio_read of %u\n",
417 (unsigned int)smb_maxcnt
,
418 (unsigned int)min_aio_read_size
));
419 return NT_STATUS_RETRY
;
422 /* Only do this on reads not using the write cache. */
423 if (lp_write_cache_size(SNUM(conn
)) != 0) {
424 return NT_STATUS_RETRY
;
427 if (outstanding_aio_calls
>= aio_pending_size
) {
428 DEBUG(10,("smb2: Already have %d aio "
429 "activities outstanding.\n",
430 outstanding_aio_calls
));
431 return NT_STATUS_RETRY
;
434 /* Create the out buffer. */
435 *preadbuf
= data_blob_talloc(ctx
, NULL
, smb_maxcnt
);
436 if (preadbuf
->data
== NULL
) {
437 return NT_STATUS_NO_MEMORY
;
440 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
441 return NT_STATUS_NO_MEMORY
;
443 aio_ex
->handle_completion
= handle_aio_smb2_read_complete
;
445 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
446 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
449 /* Take the lock until the AIO completes. */
450 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
452 return NT_STATUS_FILE_LOCK_CONFLICT
;
457 /* Now set up the aio record for the read call. */
459 a
->aio_fildes
= fsp
->fh
->fd
;
460 a
->aio_buf
= preadbuf
->data
;
461 a
->aio_nbytes
= smb_maxcnt
;
462 a
->aio_offset
= startpos
;
463 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
464 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
465 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
467 ret
= SMB_VFS_AIO_READ(fsp
, a
);
469 DEBUG(0,("smb2: aio_read failed. "
470 "Error %s\n", strerror(errno
) ));
471 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
473 return NT_STATUS_RETRY
;
476 outstanding_aio_calls
++;
477 /* We don't need talloc_move here as both aio_ex and
478 * smbreq are children of smbreq->smb2req. */
479 aio_ex
->smbreq
= smbreq
;
481 DEBUG(10,("smb2: scheduled aio_read for file %s, "
482 "offset %.0f, len = %u (mid = %u)\n",
483 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
484 (unsigned int)aio_ex
->smbreq
->mid
));
489 /****************************************************************************
490 Set up an aio request from a SMB2write call.
491 *****************************************************************************/
493 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
494 struct smb_request
*smbreq
,
500 struct aio_extra
*aio_ex
= NULL
;
501 SMB_STRUCT_AIOCB
*a
= NULL
;
502 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
505 /* Ensure aio is initialized. */
506 if (!initialize_async_io_handler()) {
507 return NT_STATUS_RETRY
;
510 if (fsp
->base_fsp
!= NULL
) {
511 /* No AIO on streams yet */
512 DEBUG(10, ("AIO on streams not yet supported\n"));
513 return NT_STATUS_RETRY
;
516 if ((!min_aio_write_size
|| (in_data
.length
< min_aio_write_size
))
517 && !SMB_VFS_AIO_FORCE(fsp
)) {
518 /* Too small a write for aio request. */
519 DEBUG(10,("smb2: write size (%u) too "
520 "small for minimum aio_write of %u\n",
521 (unsigned int)in_data
.length
,
522 (unsigned int)min_aio_write_size
));
523 return NT_STATUS_RETRY
;
526 /* Only do this on writes not using the write cache. */
527 if (lp_write_cache_size(SNUM(conn
)) != 0) {
528 return NT_STATUS_RETRY
;
531 if (outstanding_aio_calls
>= aio_pending_size
) {
532 DEBUG(3,("smb2: Already have %d aio "
533 "activities outstanding.\n",
534 outstanding_aio_calls
));
535 return NT_STATUS_RETRY
;
538 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
539 return NT_STATUS_NO_MEMORY
;
542 aio_ex
->handle_completion
= handle_aio_smb2_write_complete
;
543 aio_ex
->write_through
= write_through
;
545 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
546 in_offset
, (uint64_t)in_data
.length
, WRITE_LOCK
,
549 /* Take the lock until the AIO completes. */
550 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
552 return NT_STATUS_FILE_LOCK_CONFLICT
;
557 /* Now set up the aio record for the write call. */
559 a
->aio_fildes
= fsp
->fh
->fd
;
560 a
->aio_buf
= in_data
.data
;
561 a
->aio_nbytes
= in_data
.length
;
562 a
->aio_offset
= in_offset
;
563 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
564 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
565 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
567 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
569 DEBUG(3,("smb2: aio_write failed. "
570 "Error %s\n", strerror(errno
) ));
571 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
573 return NT_STATUS_RETRY
;
576 outstanding_aio_calls
++;
577 /* We don't need talloc_move here as both aio_ex and
578 * smbreq are children of smbreq->smb2req. */
579 aio_ex
->smbreq
= smbreq
;
581 /* This should actually be improved to span the write. */
582 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
583 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
586 * We don't want to do write behind due to ownership
587 * issues of the request structs. Maybe add it if I
588 * figure those out. JRA.
591 DEBUG(10,("smb2: scheduled aio_write for file "
592 "%s, offset %.0f, len = %u (mid = %u) "
593 "outstanding_aio_calls = %d\n",
596 (unsigned int)in_data
.length
,
597 (unsigned int)aio_ex
->smbreq
->mid
,
598 outstanding_aio_calls
));
603 /****************************************************************************
604 Complete the read and return the data or error back to the client.
605 Returns errno or zero if all ok.
606 *****************************************************************************/
608 static int handle_aio_read_complete(struct aio_extra
*aio_ex
, int errcode
)
611 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
612 char *data
= smb_buf(outbuf
);
613 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
616 /* We're relying here on the fact that if the fd is
617 closed then the aio will complete and aio_return
618 will return an error. Hopefully this is
621 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
623 fsp_str_dbg(aio_ex
->fsp
), (int)nread
, strerror(errcode
)));
625 ERROR_NT(map_nt_error_from_unix(errcode
));
626 outsize
= srv_set_message(outbuf
,0,0,true);
628 outsize
= srv_set_message(outbuf
,12,nread
,False
);
629 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be * -1. */
630 SSVAL(outbuf
,smb_vwv5
,nread
);
631 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
632 SSVAL(outbuf
,smb_vwv7
,((nread
>> 16) & 1));
633 SSVAL(smb_buf(outbuf
),-2,nread
);
635 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
636 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
638 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
640 fsp_str_dbg(aio_ex
->fsp
),
641 (int)aio_ex
->acb
.aio_nbytes
, (int)nread
) );
644 smb_setlen(outbuf
,outsize
- 4);
646 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
647 true, aio_ex
->smbreq
->seqnum
+1,
648 IS_CONN_ENCRYPTED(aio_ex
->fsp
->conn
), NULL
)) {
649 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
653 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
654 "for file %s, offset %.0f, len = %u\n",
655 fsp_str_dbg(aio_ex
->fsp
), (double)aio_ex
->acb
.aio_offset
,
656 (unsigned int)nread
));
661 /****************************************************************************
662 Complete the write and return the data or error back to the client.
663 Returns error code or zero if all ok.
664 *****************************************************************************/
666 static int handle_aio_write_complete(struct aio_extra
*aio_ex
, int errcode
)
668 files_struct
*fsp
= aio_ex
->fsp
;
669 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
670 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
671 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
673 if (fsp
->aio_write_behind
) {
674 if (nwritten
!= numtowrite
) {
675 if (nwritten
== -1) {
676 DEBUG(5,("handle_aio_write_complete: "
677 "aio_write_behind failed ! File %s "
678 "is corrupt ! Error %s\n",
679 fsp_str_dbg(fsp
), strerror(errcode
)));
681 DEBUG(0,("handle_aio_write_complete: "
682 "aio_write_behind failed ! File %s "
683 "is corrupt ! Wanted %u bytes but "
684 "only wrote %d\n", fsp_str_dbg(fsp
),
685 (unsigned int)numtowrite
,
690 DEBUG(10,("handle_aio_write_complete: "
691 "aio_write_behind completed for file %s\n",
694 /* TODO: should no return 0 in case of an error !!! */
698 /* We don't need outsize or set_message here as we've already set the
699 fixed size length when we set up the aio call. */
702 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
703 "nwritten == %d. Error = %s\n",
704 fsp_str_dbg(fsp
), (unsigned int)numtowrite
,
705 (int)nwritten
, strerror(errcode
) ));
707 ERROR_NT(map_nt_error_from_unix(errcode
));
708 srv_set_message(outbuf
,0,0,true);
712 SSVAL(outbuf
,smb_vwv2
,nwritten
);
713 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
714 if (nwritten
< (ssize_t
)numtowrite
) {
715 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
716 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
719 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
720 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
721 status
= sync_file(fsp
->conn
,fsp
, aio_ex
->write_through
);
722 if (!NT_STATUS_IS_OK(status
)) {
724 ERROR_BOTH(map_nt_error_from_unix(errcode
),
725 ERRHRD
, ERRdiskfull
);
726 srv_set_message(outbuf
,0,0,true);
727 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
728 fsp_str_dbg(fsp
), nt_errstr(status
)));
731 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nwritten
;
735 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
736 true, aio_ex
->smbreq
->seqnum
+1,
737 IS_CONN_ENCRYPTED(fsp
->conn
),
739 exit_server_cleanly("handle_aio_write_complete: "
740 "srv_send_smb failed.");
743 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
744 "for file %s, offset %.0f, requested %u, written = %u\n",
745 fsp_str_dbg(fsp
), (double)aio_ex
->acb
.aio_offset
,
746 (unsigned int)numtowrite
, (unsigned int)nwritten
));
751 /****************************************************************************
752 Complete the read and return the data or error back to the client.
753 Returns errno or zero if all ok.
754 *****************************************************************************/
756 static int handle_aio_smb2_read_complete(struct aio_extra
*aio_ex
, int errcode
)
759 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
760 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
762 /* Common error or success code processing for async or sync
765 status
= smb2_read_complete(subreq
, nread
, errcode
);
768 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
769 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
772 DEBUG(10,("smb2: scheduled aio_read completed "
773 "for file %s, offset %.0f, len = %u "
774 "(errcode = %d, NTSTATUS = %s)\n",
775 fsp_str_dbg(aio_ex
->fsp
),
776 (double)aio_ex
->acb
.aio_offset
,
779 nt_errstr(status
) ));
781 if (!NT_STATUS_IS_OK(status
)) {
782 tevent_req_nterror(subreq
, status
);
786 tevent_req_done(subreq
);
790 /****************************************************************************
791 Complete the SMB2 write and return the data or error back to the client.
792 Returns error code or zero if all ok.
793 *****************************************************************************/
795 static int handle_aio_smb2_write_complete(struct aio_extra
*aio_ex
, int errcode
)
797 files_struct
*fsp
= aio_ex
->fsp
;
798 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
799 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
800 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
803 status
= smb2_write_complete(subreq
, nwritten
, errcode
);
805 DEBUG(10,("smb2: scheduled aio_write completed "
806 "for file %s, offset %.0f, requested %u, "
807 "written = %u (errcode = %d, NTSTATUS = %s)\n",
809 (double)aio_ex
->acb
.aio_offset
,
810 (unsigned int)numtowrite
,
811 (unsigned int)nwritten
,
813 nt_errstr(status
) ));
815 if (!NT_STATUS_IS_OK(status
)) {
816 tevent_req_nterror(subreq
, status
);
820 tevent_req_done(subreq
);
824 /****************************************************************************
825 Handle any aio completion. Returns True if finished (and sets *perr if err
826 was non-zero), False if not.
827 *****************************************************************************/
829 static bool handle_aio_completed(struct aio_extra
*aio_ex
, int *perr
)
831 files_struct
*fsp
= NULL
;
835 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
841 /* Ensure the operation has really completed. */
842 err
= SMB_VFS_AIO_ERROR(fsp
, &aio_ex
->acb
);
843 if (err
== EINPROGRESS
) {
844 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
845 "process for file %s\n",
846 (unsigned long long)aio_ex
->smbreq
->mid
,
847 fsp_str_dbg(aio_ex
->fsp
)));
851 /* Unlock now we're done. */
852 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
854 if (err
== ECANCELED
) {
855 /* If error is ECANCELED then don't return anything to the
857 DEBUG(10,( "handle_aio_completed: operation mid %llu"
859 (unsigned long long)aio_ex
->smbreq
->mid
));
863 err
= aio_ex
->handle_completion(aio_ex
, err
);
865 *perr
= err
; /* Only save non-zero errors. */
871 /****************************************************************************
872 Handle any aio completion inline.
873 *****************************************************************************/
875 void smbd_aio_complete_aio_ex(struct aio_extra
*aio_ex
)
877 files_struct
*fsp
= NULL
;
880 outstanding_aio_calls
--;
882 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
883 (unsigned long long)aio_ex
->smbreq
->mid
));
887 /* file was closed whilst I/O was outstanding. Just
889 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
890 "aio outstanding (mid[%llu]).\n",
891 (unsigned long long)aio_ex
->smbreq
->mid
));
895 if (!handle_aio_completed(aio_ex
, &ret
)) {
900 /****************************************************************************
901 We're doing write behind and the client closed the file. Wait up to 30
902 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
903 completed, errno to return if not.
904 *****************************************************************************/
906 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
908 int wait_for_aio_completion(files_struct
*fsp
)
910 struct aio_extra
*aio_ex
;
911 const SMB_STRUCT_AIOCB
**aiocb_list
;
912 int aio_completion_count
= 0;
913 time_t start_time
= time_mono(NULL
);
916 for (seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
;
917 seconds_left
>= 0;) {
922 aio_completion_count
= 0;
923 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
924 if (aio_ex
->fsp
== fsp
) {
925 aio_completion_count
++;
929 if (!aio_completion_count
) {
933 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
934 "to complete.\n", aio_completion_count
));
936 aiocb_list
= SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB
*,
937 aio_completion_count
);
942 for( i
= 0, aio_ex
= aio_list_head
;
944 aio_ex
= aio_ex
->next
) {
945 if (aio_ex
->fsp
== fsp
) {
946 aiocb_list
[i
++] = &aio_ex
->acb
;
950 /* Now wait up to seconds_left for completion. */
951 ts
.tv_sec
= seconds_left
;
954 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
956 aio_completion_count
, seconds_left
));
958 err
= SMB_VFS_AIO_SUSPEND(fsp
, aiocb_list
,
959 aio_completion_count
, &ts
);
961 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
962 "errno = %s\n", err
, strerror(errno
) ));
964 if (err
== -1 && errno
== EAGAIN
) {
965 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
966 "out waiting for %d events after a wait of "
967 "%d seconds\n", aio_completion_count
,
970 cancel_aio_by_fsp(fsp
);
971 SAFE_FREE(aiocb_list
);
975 /* One or more events might have completed - process them if
977 for( i
= 0; i
< aio_completion_count
; i
++) {
978 aio_ex
= (struct aio_extra
*)aiocb_list
[i
]->aio_sigevent
.sigev_value
.sival_ptr
;
980 if (!handle_aio_completed(aio_ex
, &err
)) {
986 SAFE_FREE(aiocb_list
);
987 seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
988 - (time_mono(NULL
) - start_time
);
991 /* We timed out - we don't know why. Return ret if already an error,
993 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
995 aio_completion_count
));
1000 /****************************************************************************
1001 Cancel any outstanding aio requests. The client doesn't care about the reply.
1002 *****************************************************************************/
1004 void cancel_aio_by_fsp(files_struct
*fsp
)
1006 struct aio_extra
*aio_ex
;
1008 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
1009 if (aio_ex
->fsp
== fsp
) {
1010 /* Unlock now we're done. */
1011 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
1013 /* Don't delete the aio_extra record as we may have
1014 completed and don't yet know it. Just do the
1015 aio_cancel call and return. */
1016 SMB_VFS_AIO_CANCEL(fsp
, &aio_ex
->acb
);
1017 aio_ex
->fsp
= NULL
; /* fsp will be closed when we
1024 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
1025 struct smb_request
*smbreq
,
1026 files_struct
*fsp
, SMB_OFF_T startpos
,
1029 return NT_STATUS_RETRY
;
1032 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
1033 struct smb_request
*smbreq
,
1034 files_struct
*fsp
, char *data
,
1038 return NT_STATUS_RETRY
;
1041 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
1042 struct smb_request
*smbreq
,
1045 DATA_BLOB
*preadbuf
,
1049 return NT_STATUS_RETRY
;
1052 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
1053 struct smb_request
*smbreq
,
1059 return NT_STATUS_RETRY
;
1062 void cancel_aio_by_fsp(files_struct
*fsp
)
1066 int wait_for_aio_completion(files_struct
*fsp
)
1071 void smbd_aio_complete_mid(uint64_t mid
);