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
;
53 int (*handle_completion
)(struct aio_extra
*ex
, int errcode
);
56 /****************************************************************************
57 Initialize the signal handler for aio read/write.
58 *****************************************************************************/
60 static void smbd_aio_signal_handler(struct tevent_context
*ev_ctx
,
61 struct tevent_signal
*se
,
62 int signum
, int count
,
63 void *_info
, void *private_data
)
65 siginfo_t
*info
= (siginfo_t
*)_info
;
66 struct aio_extra
*aio_ex
= (struct aio_extra
*)
67 info
->si_value
.sival_ptr
;
69 smbd_aio_complete_aio_ex(aio_ex
);
74 bool initialize_async_io_handler(void)
76 static bool tried_signal_setup
= false;
78 if (aio_signal_event
) {
81 if (tried_signal_setup
) {
84 tried_signal_setup
= true;
86 aio_signal_event
= tevent_add_signal(server_event_context(),
87 server_event_context(),
88 RT_SIGNAL_AIO
, SA_SIGINFO
,
89 smbd_aio_signal_handler
,
91 if (!aio_signal_event
) {
92 DEBUG(10, ("Failed to setup RT_SIGNAL_AIO handler\n"));
96 /* tevent supports 100 signal with SA_SIGINFO */
97 aio_pending_size
= 100;
101 static int handle_aio_read_complete(struct aio_extra
*aio_ex
, int errcode
);
102 static int handle_aio_write_complete(struct aio_extra
*aio_ex
, int errcode
);
103 static int handle_aio_smb2_read_complete(struct aio_extra
*aio_ex
, int errcode
);
104 static int handle_aio_smb2_write_complete(struct aio_extra
*aio_ex
, int errcode
);
106 static int aio_extra_destructor(struct aio_extra
*aio_ex
)
108 DLIST_REMOVE(aio_list_head
, aio_ex
);
112 /****************************************************************************
113 Create the extended aio struct we must keep around for the lifetime
115 *****************************************************************************/
117 static struct aio_extra
*create_aio_extra(TALLOC_CTX
*mem_ctx
,
121 struct aio_extra
*aio_ex
= talloc_zero(mem_ctx
, struct aio_extra
);
127 /* The output buffer stored in the aio_ex is the start of
128 the smb return buffer. The buffer used in the acb
129 is the start of the reply data portion of that buffer. */
132 aio_ex
->outbuf
= data_blob_talloc(aio_ex
, NULL
, buflen
);
133 if (!aio_ex
->outbuf
.data
) {
138 DLIST_ADD(aio_list_head
, aio_ex
);
139 talloc_set_destructor(aio_ex
, aio_extra_destructor
);
144 /****************************************************************************
145 Set up an aio request from a SMBreadX call.
146 *****************************************************************************/
148 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
149 struct smb_request
*smbreq
,
150 files_struct
*fsp
, off_t startpos
,
153 struct aio_extra
*aio_ex
;
156 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
159 if (fsp
->base_fsp
!= NULL
) {
160 /* No AIO on streams yet */
161 DEBUG(10, ("AIO on streams not yet supported\n"));
162 return NT_STATUS_RETRY
;
165 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
166 && !SMB_VFS_AIO_FORCE(fsp
)) {
167 /* Too small a read for aio request. */
168 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
169 "for minimum aio_read of %u\n",
170 (unsigned int)smb_maxcnt
,
171 (unsigned int)min_aio_read_size
));
172 return NT_STATUS_RETRY
;
175 /* Only do this on non-chained and non-chaining reads not using the
177 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
178 return NT_STATUS_RETRY
;
181 if (outstanding_aio_calls
>= aio_pending_size
) {
182 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
183 "activities outstanding.\n",
184 outstanding_aio_calls
));
185 return NT_STATUS_RETRY
;
188 /* The following is safe from integer wrap as we've already checked
189 smb_maxcnt is 128k or less. Wct is 12 for read replies */
191 bufsize
= smb_size
+ 12 * 2 + smb_maxcnt
;
193 if ((aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
)) == NULL
) {
194 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
195 return NT_STATUS_NO_MEMORY
;
197 aio_ex
->handle_completion
= handle_aio_read_complete
;
199 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
200 srv_set_message((char *)aio_ex
->outbuf
.data
, 12, 0, True
);
201 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
203 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
204 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
207 /* Take the lock until the AIO completes. */
208 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
210 return NT_STATUS_FILE_LOCK_CONFLICT
;
215 /* Now set up the aio record for the read call. */
217 a
->aio_fildes
= fsp
->fh
->fd
;
218 a
->aio_buf
= smb_buf(aio_ex
->outbuf
.data
);
219 a
->aio_nbytes
= smb_maxcnt
;
220 a
->aio_offset
= startpos
;
221 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
222 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
223 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
225 ret
= SMB_VFS_AIO_READ(fsp
, a
);
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
;
234 outstanding_aio_calls
++;
235 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
237 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
238 "offset %.0f, len = %u (mid = %u)\n",
239 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
240 (unsigned int)aio_ex
->smbreq
->mid
));
245 /****************************************************************************
246 Set up an aio request from a SMBwriteX call.
247 *****************************************************************************/
249 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
250 struct smb_request
*smbreq
,
251 files_struct
*fsp
, const char *data
,
255 struct aio_extra
*aio_ex
;
258 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
261 if (fsp
->base_fsp
!= NULL
) {
262 /* No AIO on streams yet */
263 DEBUG(10, ("AIO on streams not yet supported\n"));
264 return NT_STATUS_RETRY
;
267 if ((!min_aio_write_size
|| (numtowrite
< min_aio_write_size
))
268 && !SMB_VFS_AIO_FORCE(fsp
)) {
269 /* Too small a write for aio request. */
270 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
271 "small for minimum aio_write of %u\n",
272 (unsigned int)numtowrite
,
273 (unsigned int)min_aio_write_size
));
274 return NT_STATUS_RETRY
;
277 /* Only do this on non-chained and non-chaining writes not using the
279 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
280 return NT_STATUS_RETRY
;
283 if (outstanding_aio_calls
>= aio_pending_size
) {
284 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
285 "activities outstanding.\n",
286 outstanding_aio_calls
));
287 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
288 "aio_write for file %s, offset %.0f, len = %u "
290 fsp_str_dbg(fsp
), (double)startpos
,
291 (unsigned int)numtowrite
,
292 (unsigned int)smbreq
->mid
));
293 return NT_STATUS_RETRY
;
296 bufsize
= smb_size
+ 6*2;
298 if (!(aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
))) {
299 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
300 return NT_STATUS_NO_MEMORY
;
302 aio_ex
->handle_completion
= handle_aio_write_complete
;
303 aio_ex
->write_through
= BITSETW(smbreq
->vwv
+7,0);
305 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
306 srv_set_message((char *)aio_ex
->outbuf
.data
, 6, 0, True
);
307 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
309 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
310 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
313 /* Take the lock until the AIO completes. */
314 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
316 return NT_STATUS_FILE_LOCK_CONFLICT
;
321 /* Now set up the aio record for the write call. */
323 a
->aio_fildes
= fsp
->fh
->fd
;
324 a
->aio_buf
= discard_const_p(char, data
);
325 a
->aio_nbytes
= numtowrite
;
326 a
->aio_offset
= startpos
;
327 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
328 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
329 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
331 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
333 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
334 "Error %s\n", strerror(errno
) ));
335 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
337 return NT_STATUS_RETRY
;
340 outstanding_aio_calls
++;
341 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
343 /* This should actually be improved to span the write. */
344 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
345 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
347 if (!aio_ex
->write_through
&& !lp_syncalways(SNUM(fsp
->conn
))
348 && fsp
->aio_write_behind
) {
349 /* Lie to the client and immediately claim we finished the
351 SSVAL(aio_ex
->outbuf
.data
,smb_vwv2
,numtowrite
);
352 SSVAL(aio_ex
->outbuf
.data
,smb_vwv4
,(numtowrite
>>16)&1);
353 show_msg((char *)aio_ex
->outbuf
.data
);
354 if (!srv_send_smb(aio_ex
->smbreq
->sconn
,
355 (char *)aio_ex
->outbuf
.data
,
356 true, aio_ex
->smbreq
->seqnum
+1,
357 IS_CONN_ENCRYPTED(fsp
->conn
),
358 &aio_ex
->smbreq
->pcd
)) {
359 exit_server_cleanly("schedule_aio_write_and_X: "
360 "srv_send_smb failed.");
362 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
363 "behind for file %s\n", fsp_str_dbg(fsp
)));
366 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
367 "%s, offset %.0f, len = %u (mid = %u) "
368 "outstanding_aio_calls = %d\n",
369 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)numtowrite
,
370 (unsigned int)aio_ex
->smbreq
->mid
, outstanding_aio_calls
));
375 bool cancel_smb2_aio(struct smb_request
*smbreq
)
377 struct smbd_smb2_request
*smb2req
= smbreq
->smb2req
;
378 struct aio_extra
*aio_ex
= NULL
;
382 aio_ex
= talloc_get_type(smbreq
->async_priv
,
386 if (aio_ex
== NULL
) {
390 if (aio_ex
->fsp
== NULL
) {
394 ret
= SMB_VFS_AIO_CANCEL(aio_ex
->fsp
, &aio_ex
->acb
);
395 if (ret
!= AIO_CANCELED
) {
402 /****************************************************************************
403 Set up an aio request from a SMB2 read call.
404 *****************************************************************************/
406 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
407 struct smb_request
*smbreq
,
414 struct aio_extra
*aio_ex
;
416 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
419 if (fsp
->base_fsp
!= NULL
) {
420 /* No AIO on streams yet */
421 DEBUG(10, ("AIO on streams not yet supported\n"));
422 return NT_STATUS_RETRY
;
425 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
426 && !SMB_VFS_AIO_FORCE(fsp
)) {
427 /* Too small a read for aio request. */
428 DEBUG(10,("smb2: read size (%u) too small "
429 "for minimum aio_read of %u\n",
430 (unsigned int)smb_maxcnt
,
431 (unsigned int)min_aio_read_size
));
432 return NT_STATUS_RETRY
;
435 /* Only do this on reads not using the write cache. */
436 if (lp_write_cache_size(SNUM(conn
)) != 0) {
437 return NT_STATUS_RETRY
;
440 if (outstanding_aio_calls
>= aio_pending_size
) {
441 DEBUG(10,("smb2: Already have %d aio "
442 "activities outstanding.\n",
443 outstanding_aio_calls
));
444 return NT_STATUS_RETRY
;
447 /* Create the out buffer. */
448 *preadbuf
= data_blob_talloc(ctx
, NULL
, smb_maxcnt
);
449 if (preadbuf
->data
== NULL
) {
450 return NT_STATUS_NO_MEMORY
;
453 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
454 return NT_STATUS_NO_MEMORY
;
456 aio_ex
->handle_completion
= handle_aio_smb2_read_complete
;
457 aio_ex
->pass_cancel
= true;
459 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
460 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
463 /* Take the lock until the AIO completes. */
464 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
466 return NT_STATUS_FILE_LOCK_CONFLICT
;
471 /* Now set up the aio record for the read call. */
473 a
->aio_fildes
= fsp
->fh
->fd
;
474 a
->aio_buf
= preadbuf
->data
;
475 a
->aio_nbytes
= smb_maxcnt
;
476 a
->aio_offset
= startpos
;
477 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
478 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
479 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
481 ret
= SMB_VFS_AIO_READ(fsp
, a
);
483 DEBUG(0,("smb2: aio_read failed. "
484 "Error %s\n", strerror(errno
) ));
485 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
487 return NT_STATUS_RETRY
;
490 outstanding_aio_calls
++;
491 /* We don't need talloc_move here as both aio_ex and
492 * smbreq are children of smbreq->smb2req. */
493 aio_ex
->smbreq
= smbreq
;
494 smbreq
->async_priv
= aio_ex
;
496 DEBUG(10,("smb2: scheduled aio_read for file %s, "
497 "offset %.0f, len = %u (mid = %u)\n",
498 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
499 (unsigned int)aio_ex
->smbreq
->mid
));
504 /****************************************************************************
505 Set up an aio request from a SMB2write call.
506 *****************************************************************************/
508 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
509 struct smb_request
*smbreq
,
515 struct aio_extra
*aio_ex
= NULL
;
516 SMB_STRUCT_AIOCB
*a
= NULL
;
517 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
520 if (fsp
->base_fsp
!= NULL
) {
521 /* No AIO on streams yet */
522 DEBUG(10, ("AIO on streams not yet supported\n"));
523 return NT_STATUS_RETRY
;
526 if ((!min_aio_write_size
|| (in_data
.length
< min_aio_write_size
))
527 && !SMB_VFS_AIO_FORCE(fsp
)) {
528 /* Too small a write for aio request. */
529 DEBUG(10,("smb2: write size (%u) too "
530 "small for minimum aio_write of %u\n",
531 (unsigned int)in_data
.length
,
532 (unsigned int)min_aio_write_size
));
533 return NT_STATUS_RETRY
;
536 /* Only do this on writes not using the write cache. */
537 if (lp_write_cache_size(SNUM(conn
)) != 0) {
538 return NT_STATUS_RETRY
;
541 if (outstanding_aio_calls
>= aio_pending_size
) {
542 DEBUG(3,("smb2: Already have %d aio "
543 "activities outstanding.\n",
544 outstanding_aio_calls
));
545 return NT_STATUS_RETRY
;
548 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
549 return NT_STATUS_NO_MEMORY
;
552 aio_ex
->handle_completion
= handle_aio_smb2_write_complete
;
553 aio_ex
->write_through
= write_through
;
554 aio_ex
->pass_cancel
= true;
556 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
557 in_offset
, (uint64_t)in_data
.length
, WRITE_LOCK
,
560 /* Take the lock until the AIO completes. */
561 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
563 return NT_STATUS_FILE_LOCK_CONFLICT
;
568 /* Now set up the aio record for the write call. */
570 a
->aio_fildes
= fsp
->fh
->fd
;
571 a
->aio_buf
= in_data
.data
;
572 a
->aio_nbytes
= in_data
.length
;
573 a
->aio_offset
= in_offset
;
574 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
575 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
576 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
578 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
580 DEBUG(3,("smb2: aio_write failed. "
581 "Error %s\n", strerror(errno
) ));
582 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
584 return NT_STATUS_RETRY
;
587 outstanding_aio_calls
++;
588 /* We don't need talloc_move here as both aio_ex and
589 * smbreq are children of smbreq->smb2req. */
590 aio_ex
->smbreq
= smbreq
;
591 smbreq
->async_priv
= aio_ex
;
593 /* This should actually be improved to span the write. */
594 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
595 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
598 * We don't want to do write behind due to ownership
599 * issues of the request structs. Maybe add it if I
600 * figure those out. JRA.
603 DEBUG(10,("smb2: scheduled aio_write for file "
604 "%s, offset %.0f, len = %u (mid = %u) "
605 "outstanding_aio_calls = %d\n",
608 (unsigned int)in_data
.length
,
609 (unsigned int)aio_ex
->smbreq
->mid
,
610 outstanding_aio_calls
));
615 /****************************************************************************
616 Complete the read and return the data or error back to the client.
617 Returns errno or zero if all ok.
618 *****************************************************************************/
620 static int handle_aio_read_complete(struct aio_extra
*aio_ex
, int errcode
)
623 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
624 char *data
= smb_buf(outbuf
);
625 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
628 /* We're relying here on the fact that if the fd is
629 closed then the aio will complete and aio_return
630 will return an error. Hopefully this is
633 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
635 fsp_str_dbg(aio_ex
->fsp
), (int)nread
, strerror(errcode
)));
637 ERROR_NT(map_nt_error_from_unix(errcode
));
638 outsize
= srv_set_message(outbuf
,0,0,true);
640 outsize
= srv_set_message(outbuf
,12,nread
,False
);
641 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be * -1. */
642 SSVAL(outbuf
,smb_vwv5
,nread
);
643 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
644 SSVAL(outbuf
,smb_vwv7
,((nread
>> 16) & 1));
645 SSVAL(smb_buf(outbuf
),-2,nread
);
647 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
648 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
650 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
652 fsp_str_dbg(aio_ex
->fsp
),
653 (int)aio_ex
->acb
.aio_nbytes
, (int)nread
) );
656 smb_setlen(outbuf
,outsize
- 4);
658 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
659 true, aio_ex
->smbreq
->seqnum
+1,
660 IS_CONN_ENCRYPTED(aio_ex
->fsp
->conn
), NULL
)) {
661 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
665 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
666 "for file %s, offset %.0f, len = %u\n",
667 fsp_str_dbg(aio_ex
->fsp
), (double)aio_ex
->acb
.aio_offset
,
668 (unsigned int)nread
));
673 /****************************************************************************
674 Complete the write and return the data or error back to the client.
675 Returns error code or zero if all ok.
676 *****************************************************************************/
678 static int handle_aio_write_complete(struct aio_extra
*aio_ex
, int errcode
)
680 files_struct
*fsp
= aio_ex
->fsp
;
681 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
682 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
683 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
685 if (fsp
->aio_write_behind
) {
686 if (nwritten
!= numtowrite
) {
687 if (nwritten
== -1) {
688 DEBUG(5,("handle_aio_write_complete: "
689 "aio_write_behind failed ! File %s "
690 "is corrupt ! Error %s\n",
691 fsp_str_dbg(fsp
), strerror(errcode
)));
693 DEBUG(0,("handle_aio_write_complete: "
694 "aio_write_behind failed ! File %s "
695 "is corrupt ! Wanted %u bytes but "
696 "only wrote %d\n", fsp_str_dbg(fsp
),
697 (unsigned int)numtowrite
,
702 DEBUG(10,("handle_aio_write_complete: "
703 "aio_write_behind completed for file %s\n",
706 /* TODO: should no return 0 in case of an error !!! */
710 /* We don't need outsize or set_message here as we've already set the
711 fixed size length when we set up the aio call. */
714 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
715 "nwritten == %d. Error = %s\n",
716 fsp_str_dbg(fsp
), (unsigned int)numtowrite
,
717 (int)nwritten
, strerror(errcode
) ));
719 ERROR_NT(map_nt_error_from_unix(errcode
));
720 srv_set_message(outbuf
,0,0,true);
724 SSVAL(outbuf
,smb_vwv2
,nwritten
);
725 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
726 if (nwritten
< (ssize_t
)numtowrite
) {
727 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
728 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
731 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
732 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
733 status
= sync_file(fsp
->conn
,fsp
, aio_ex
->write_through
);
734 if (!NT_STATUS_IS_OK(status
)) {
736 ERROR_BOTH(map_nt_error_from_unix(errcode
),
737 ERRHRD
, ERRdiskfull
);
738 srv_set_message(outbuf
,0,0,true);
739 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
740 fsp_str_dbg(fsp
), nt_errstr(status
)));
743 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nwritten
;
747 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
748 true, aio_ex
->smbreq
->seqnum
+1,
749 IS_CONN_ENCRYPTED(fsp
->conn
),
751 exit_server_cleanly("handle_aio_write_complete: "
752 "srv_send_smb failed.");
755 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
756 "for file %s, offset %.0f, requested %u, written = %u\n",
757 fsp_str_dbg(fsp
), (double)aio_ex
->acb
.aio_offset
,
758 (unsigned int)numtowrite
, (unsigned int)nwritten
));
763 /****************************************************************************
764 Complete the read and return the data or error back to the client.
765 Returns errno or zero if all ok.
766 *****************************************************************************/
768 static int handle_aio_smb2_read_complete(struct aio_extra
*aio_ex
, int errcode
)
771 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
772 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
774 /* Common error or success code processing for async or sync
777 status
= smb2_read_complete(subreq
, nread
, errcode
);
780 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
781 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
784 DEBUG(10,("smb2: scheduled aio_read completed "
785 "for file %s, offset %.0f, len = %u "
786 "(errcode = %d, NTSTATUS = %s)\n",
787 fsp_str_dbg(aio_ex
->fsp
),
788 (double)aio_ex
->acb
.aio_offset
,
791 nt_errstr(status
) ));
793 if (!NT_STATUS_IS_OK(status
)) {
794 tevent_req_nterror(subreq
, status
);
798 tevent_req_done(subreq
);
802 /****************************************************************************
803 Complete the SMB2 write and return the data or error back to the client.
804 Returns error code or zero if all ok.
805 *****************************************************************************/
807 static int handle_aio_smb2_write_complete(struct aio_extra
*aio_ex
, int errcode
)
809 files_struct
*fsp
= aio_ex
->fsp
;
810 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
811 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
812 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
815 status
= smb2_write_complete(subreq
, nwritten
, errcode
);
817 DEBUG(10,("smb2: scheduled aio_write completed "
818 "for file %s, offset %.0f, requested %u, "
819 "written = %u (errcode = %d, NTSTATUS = %s)\n",
821 (double)aio_ex
->acb
.aio_offset
,
822 (unsigned int)numtowrite
,
823 (unsigned int)nwritten
,
825 nt_errstr(status
) ));
827 if (!NT_STATUS_IS_OK(status
)) {
828 tevent_req_nterror(subreq
, status
);
832 tevent_req_done(subreq
);
836 /****************************************************************************
837 Handle any aio completion. Returns True if finished (and sets *perr if err
838 was non-zero), False if not.
839 *****************************************************************************/
841 static bool handle_aio_completed(struct aio_extra
*aio_ex
, int *perr
)
843 files_struct
*fsp
= NULL
;
847 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
852 DEBUG(3, ("handle_aio_completed: aio_ex->fsp == NULL\n"));
858 /* Ensure the operation has really completed. */
859 err
= SMB_VFS_AIO_ERROR(fsp
, &aio_ex
->acb
);
860 if (err
== EINPROGRESS
) {
861 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
862 "process for file %s\n",
863 (unsigned long long)aio_ex
->smbreq
->mid
,
864 fsp_str_dbg(aio_ex
->fsp
)));
868 /* Unlock now we're done. */
869 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
871 if (!aio_ex
->pass_cancel
&& err
== ECANCELED
) {
872 /* If error is ECANCELED then don't return anything to the
874 DEBUG(10,( "handle_aio_completed: operation mid %llu"
876 (unsigned long long)aio_ex
->smbreq
->mid
));
880 err
= aio_ex
->handle_completion(aio_ex
, err
);
882 *perr
= err
; /* Only save non-zero errors. */
888 /****************************************************************************
889 Handle any aio completion inline.
890 *****************************************************************************/
892 void smbd_aio_complete_aio_ex(struct aio_extra
*aio_ex
)
894 files_struct
*fsp
= NULL
;
897 outstanding_aio_calls
--;
899 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
900 (unsigned long long)aio_ex
->smbreq
->mid
));
904 /* file was closed whilst I/O was outstanding. Just
906 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
907 "aio outstanding (mid[%llu]).\n",
908 (unsigned long long)aio_ex
->smbreq
->mid
));
912 if (!handle_aio_completed(aio_ex
, &ret
)) {
917 /****************************************************************************
918 We're doing write behind and the client closed the file. Wait up to 30
919 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
920 completed, errno to return if not.
921 *****************************************************************************/
923 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
925 int wait_for_aio_completion(files_struct
*fsp
)
927 struct aio_extra
*aio_ex
;
928 const SMB_STRUCT_AIOCB
**aiocb_list
;
929 int aio_completion_count
= 0;
930 time_t start_time
= time_mono(NULL
);
933 for (seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
;
934 seconds_left
>= 0;) {
939 aio_completion_count
= 0;
940 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
941 if (aio_ex
->fsp
== fsp
) {
942 aio_completion_count
++;
946 if (!aio_completion_count
) {
950 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
951 "to complete.\n", aio_completion_count
));
953 aiocb_list
= SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB
*,
954 aio_completion_count
);
959 for( i
= 0, aio_ex
= aio_list_head
;
961 aio_ex
= aio_ex
->next
) {
962 if (aio_ex
->fsp
== fsp
) {
963 aiocb_list
[i
++] = &aio_ex
->acb
;
967 /* Now wait up to seconds_left for completion. */
968 ts
.tv_sec
= seconds_left
;
971 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
973 aio_completion_count
, seconds_left
));
975 err
= SMB_VFS_AIO_SUSPEND(fsp
, aiocb_list
,
976 aio_completion_count
, &ts
);
978 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
979 "errno = %s\n", err
, strerror(errno
) ));
981 if (err
== -1 && errno
== EAGAIN
) {
982 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
983 "out waiting for %d events after a wait of "
984 "%d seconds\n", aio_completion_count
,
987 cancel_aio_by_fsp(fsp
);
988 SAFE_FREE(aiocb_list
);
992 /* One or more events might have completed - process them if
994 for( i
= 0; i
< aio_completion_count
; i
++) {
995 aio_ex
= (struct aio_extra
*)aiocb_list
[i
]->aio_sigevent
.sigev_value
.sival_ptr
;
997 if (!handle_aio_completed(aio_ex
, &err
)) {
1000 TALLOC_FREE(aio_ex
);
1003 SAFE_FREE(aiocb_list
);
1004 seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
1005 - (time_mono(NULL
) - start_time
);
1008 /* We timed out - we don't know why. Return ret if already an error,
1010 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
1012 aio_completion_count
));
1017 /****************************************************************************
1018 Cancel any outstanding aio requests. The client doesn't care about the reply.
1019 *****************************************************************************/
1021 void cancel_aio_by_fsp(files_struct
*fsp
)
1023 struct aio_extra
*aio_ex
;
1025 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
1026 if (aio_ex
->fsp
== fsp
) {
1027 /* Unlock now we're done. */
1028 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
1030 /* Don't delete the aio_extra record as we may have
1031 completed and don't yet know it. Just do the
1032 aio_cancel call and return. */
1033 SMB_VFS_AIO_CANCEL(fsp
, &aio_ex
->acb
);
1034 aio_ex
->fsp
= NULL
; /* fsp will be closed when we
1042 bool initialize_async_io_handler(void)
1047 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
1048 struct smb_request
*smbreq
,
1049 files_struct
*fsp
, off_t startpos
,
1052 return NT_STATUS_RETRY
;
1055 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
1056 struct smb_request
*smbreq
,
1057 files_struct
*fsp
, const char *data
,
1061 return NT_STATUS_RETRY
;
1064 bool cancel_smb2_aio(struct smb_request
*smbreq
)
1069 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
1070 struct smb_request
*smbreq
,
1073 DATA_BLOB
*preadbuf
,
1077 return NT_STATUS_RETRY
;
1080 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
1081 struct smb_request
*smbreq
,
1087 return NT_STATUS_RETRY
;
1090 void cancel_aio_by_fsp(files_struct
*fsp
)
1094 int wait_for_aio_completion(files_struct
*fsp
)
1099 void smbd_aio_complete_mid(uint64_t mid
);