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. */
31 #define RT_SIGNAL_AIO (SIGRTMIN+3)
34 #ifndef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIVAL_PTR
35 #ifdef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIGVAL_PTR
36 #define sival_int sigval_int
37 #define sival_ptr sigval_ptr
41 /****************************************************************************
42 The buffer we keep around whilst an aio request is in process.
43 *****************************************************************************/
46 struct aio_extra
*next
, *prev
;
49 struct smb_request
*req
;
51 int (*handle_completion
)(struct aio_extra
*ex
);
54 static int handle_aio_read_complete(struct aio_extra
*aio_ex
);
55 static int handle_aio_write_complete(struct aio_extra
*aio_ex
);
57 static int aio_extra_destructor(struct aio_extra
*aio_ex
)
59 DLIST_REMOVE(aio_list_head
, aio_ex
);
63 /****************************************************************************
64 Create the extended aio struct we must keep around for the lifetime
66 *****************************************************************************/
68 static struct aio_extra
*create_aio_extra(files_struct
*fsp
, size_t buflen
)
70 struct aio_extra
*aio_ex
= TALLOC_ZERO_P(NULL
, struct aio_extra
);
76 /* The output buffer stored in the aio_ex is the start of
77 the smb return buffer. The buffer used in the acb
78 is the start of the reply data portion of that buffer. */
80 aio_ex
->outbuf
= TALLOC_ARRAY(aio_ex
, char, buflen
);
81 if (!aio_ex
->outbuf
) {
85 DLIST_ADD(aio_list_head
, aio_ex
);
86 talloc_set_destructor(aio_ex
, aio_extra_destructor
);
91 /****************************************************************************
92 Given the mid find the extended aio struct containing it.
93 *****************************************************************************/
95 static struct aio_extra
*find_aio_ex(uint16 mid
)
99 for( p
= aio_list_head
; p
; p
= p
->next
) {
100 if (mid
== p
->req
->mid
) {
107 /****************************************************************************
108 We can have these many aio buffers in flight.
109 *****************************************************************************/
111 /****************************************************************************
112 Set up an aio request from a SMBreadX call.
113 *****************************************************************************/
115 bool schedule_aio_read_and_X(connection_struct
*conn
,
116 struct smb_request
*req
,
117 files_struct
*fsp
, SMB_OFF_T startpos
,
120 struct aio_extra
*aio_ex
;
123 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
126 if (fsp
->base_fsp
!= NULL
) {
127 /* No AIO on streams yet */
128 DEBUG(10, ("AIO on streams not yet supported\n"));
132 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
133 && !SMB_VFS_AIO_FORCE(fsp
)) {
134 /* Too small a read for aio request. */
135 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
136 "for minimum aio_read of %u\n",
137 (unsigned int)smb_maxcnt
,
138 (unsigned int)min_aio_read_size
));
142 /* Only do this on non-chained and non-chaining reads not using the
144 if (req_is_in_chain(req
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
148 if (outstanding_aio_calls
>= aio_pending_size
) {
149 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
150 "activities outstanding.\n",
151 outstanding_aio_calls
));
155 /* The following is safe from integer wrap as we've already checked
156 smb_maxcnt is 128k or less. Wct is 12 for read replies */
158 bufsize
= smb_size
+ 12 * 2 + smb_maxcnt
;
160 if ((aio_ex
= create_aio_extra(fsp
, bufsize
)) == NULL
) {
161 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
164 aio_ex
->handle_completion
= handle_aio_read_complete
;
166 construct_reply_common_req(req
, aio_ex
->outbuf
);
167 srv_set_message(aio_ex
->outbuf
, 12, 0, True
);
168 SCVAL(aio_ex
->outbuf
,smb_vwv0
,0xFF); /* Never a chained reply. */
172 /* Now set up the aio record for the read call. */
174 a
->aio_fildes
= fsp
->fh
->fd
;
175 a
->aio_buf
= smb_buf(aio_ex
->outbuf
);
176 a
->aio_nbytes
= smb_maxcnt
;
177 a
->aio_offset
= startpos
;
178 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
179 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
180 a
->aio_sigevent
.sigev_value
.sival_int
= req
->mid
;
183 ret
= SMB_VFS_AIO_READ(fsp
, a
);
187 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
188 "Error %s\n", strerror(errno
) ));
193 aio_ex
->req
= talloc_move(aio_ex
, &req
);
195 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
196 "offset %.0f, len = %u (mid = %u)\n",
197 fsp
->fsp_name
, (double)startpos
, (unsigned int)smb_maxcnt
,
198 (unsigned int)aio_ex
->req
->mid
));
200 srv_defer_sign_response(aio_ex
->req
->mid
);
201 outstanding_aio_calls
++;
205 /****************************************************************************
206 Set up an aio request from a SMBwriteX call.
207 *****************************************************************************/
209 bool schedule_aio_write_and_X(connection_struct
*conn
,
210 struct smb_request
*req
,
211 files_struct
*fsp
, char *data
,
215 struct aio_extra
*aio_ex
;
218 bool write_through
= BITSETW(req
->vwv
+7,0);
219 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
222 if (fsp
->base_fsp
!= NULL
) {
223 /* No AIO on streams yet */
224 DEBUG(10, ("AIO on streams not yet supported\n"));
228 if ((!min_aio_write_size
|| (numtowrite
< min_aio_write_size
))
229 && !SMB_VFS_AIO_FORCE(fsp
)) {
230 /* Too small a write for aio request. */
231 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
232 "small for minimum aio_write of %u\n",
233 (unsigned int)numtowrite
,
234 (unsigned int)min_aio_write_size
));
238 /* Only do this on non-chained and non-chaining reads not using the
240 if (req_is_in_chain(req
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
244 if (outstanding_aio_calls
>= aio_pending_size
) {
245 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
246 "activities outstanding.\n",
247 outstanding_aio_calls
));
248 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
249 "aio_write for file %s, offset %.0f, len = %u "
251 fsp
->fsp_name
, (double)startpos
,
252 (unsigned int)numtowrite
,
253 (unsigned int)req
->mid
));
257 bufsize
= smb_size
+ 6*2;
259 if (!(aio_ex
= create_aio_extra(fsp
, bufsize
))) {
260 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
263 aio_ex
->handle_completion
= handle_aio_write_complete
;
265 construct_reply_common_req(req
, aio_ex
->outbuf
);
266 srv_set_message(aio_ex
->outbuf
, 6, 0, True
);
267 SCVAL(aio_ex
->outbuf
,smb_vwv0
,0xFF); /* Never a chained reply. */
271 /* Now set up the aio record for the write call. */
273 a
->aio_fildes
= fsp
->fh
->fd
;
275 a
->aio_nbytes
= numtowrite
;
276 a
->aio_offset
= startpos
;
277 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
278 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
279 a
->aio_sigevent
.sigev_value
.sival_int
= req
->mid
;
282 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
286 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
287 "Error %s\n", strerror(errno
) ));
292 aio_ex
->req
= talloc_move(aio_ex
, &req
);
294 /* This should actually be improved to span the write. */
295 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
296 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
298 if (!write_through
&& !lp_syncalways(SNUM(fsp
->conn
))
299 && fsp
->aio_write_behind
) {
300 /* Lie to the client and immediately claim we finished the
302 SSVAL(aio_ex
->outbuf
,smb_vwv2
,numtowrite
);
303 SSVAL(aio_ex
->outbuf
,smb_vwv4
,(numtowrite
>>16)&1);
304 show_msg(aio_ex
->outbuf
);
305 if (!srv_send_smb(smbd_server_fd(),aio_ex
->outbuf
,
306 IS_CONN_ENCRYPTED(fsp
->conn
),
308 exit_server_cleanly("handle_aio_write: srv_send_smb "
311 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
312 "behind for file %s\n", fsp
->fsp_name
));
314 srv_defer_sign_response(aio_ex
->req
->mid
);
316 outstanding_aio_calls
++;
318 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
319 "%s, offset %.0f, len = %u (mid = %u) "
320 "outstanding_aio_calls = %d\n",
321 fsp
->fsp_name
, (double)startpos
, (unsigned int)numtowrite
,
322 (unsigned int)aio_ex
->req
->mid
, outstanding_aio_calls
));
328 /****************************************************************************
329 Complete the read and return the data or error back to the client.
330 Returns errno or zero if all ok.
331 *****************************************************************************/
333 static int handle_aio_read_complete(struct aio_extra
*aio_ex
)
337 char *outbuf
= aio_ex
->outbuf
;
338 char *data
= smb_buf(outbuf
);
339 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
342 /* We're relying here on the fact that if the fd is
343 closed then the aio will complete and aio_return
344 will return an error. Hopefully this is
347 /* If errno is ECANCELED then don't return anything to the
349 if (errno
== ECANCELED
) {
350 srv_cancel_sign_response(aio_ex
->req
->mid
, false);
354 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
356 aio_ex
->fsp
->fsp_name
, strerror(errno
) ));
359 ERROR_NT(map_nt_error_from_unix(ret
));
360 outsize
= srv_set_message(outbuf
,0,0,true);
362 outsize
= srv_set_message(outbuf
,12,nread
,False
);
363 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be * -1. */
364 SSVAL(outbuf
,smb_vwv5
,nread
);
365 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
366 SSVAL(outbuf
,smb_vwv7
,((nread
>> 16) & 1));
367 SSVAL(smb_buf(outbuf
),-2,nread
);
369 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
370 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
372 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
374 aio_ex
->fsp
->fsp_name
,
375 (int)aio_ex
->acb
.aio_nbytes
, (int)nread
) );
378 smb_setlen(outbuf
,outsize
- 4);
380 if (!srv_send_smb(smbd_server_fd(),outbuf
,
381 IS_CONN_ENCRYPTED(aio_ex
->fsp
->conn
), NULL
)) {
382 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
386 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
387 "for file %s, offset %.0f, len = %u\n",
388 aio_ex
->fsp
->fsp_name
, (double)aio_ex
->acb
.aio_offset
,
389 (unsigned int)nread
));
394 /****************************************************************************
395 Complete the write and return the data or error back to the client.
396 Returns errno or zero if all ok.
397 *****************************************************************************/
399 static int handle_aio_write_complete(struct aio_extra
*aio_ex
)
402 files_struct
*fsp
= aio_ex
->fsp
;
403 char *outbuf
= aio_ex
->outbuf
;
404 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
405 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
407 if (fsp
->aio_write_behind
) {
408 if (nwritten
!= numtowrite
) {
409 if (nwritten
== -1) {
410 DEBUG(5,("handle_aio_write_complete: "
411 "aio_write_behind failed ! File %s "
412 "is corrupt ! Error %s\n",
413 fsp
->fsp_name
, strerror(errno
) ));
416 DEBUG(0,("handle_aio_write_complete: "
417 "aio_write_behind failed ! File %s "
418 "is corrupt ! Wanted %u bytes but "
419 "only wrote %d\n", fsp
->fsp_name
,
420 (unsigned int)numtowrite
,
425 DEBUG(10,("handle_aio_write_complete: "
426 "aio_write_behind completed for file %s\n",
432 /* We don't need outsize or set_message here as we've already set the
433 fixed size length when we set up the aio call. */
436 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
437 "nwritten == %d. Error = %s\n",
438 fsp
->fsp_name
, (unsigned int)numtowrite
,
439 (int)nwritten
, strerror(errno
) ));
441 /* If errno is ECANCELED then don't return anything to the
443 if (errno
== ECANCELED
) {
444 srv_cancel_sign_response(aio_ex
->req
->mid
, false);
449 ERROR_BOTH(map_nt_error_from_unix(ret
), ERRHRD
, ERRdiskfull
);
450 srv_set_message(outbuf
,0,0,true);
452 bool write_through
= BITSETW(aio_ex
->req
->vwv
+7,0);
455 SSVAL(outbuf
,smb_vwv2
,nwritten
);
456 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
457 if (nwritten
< (ssize_t
)numtowrite
) {
458 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
459 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
462 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
463 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
464 status
= sync_file(fsp
->conn
,fsp
, write_through
);
465 if (!NT_STATUS_IS_OK(status
)) {
467 ERROR_BOTH(map_nt_error_from_unix(ret
),
468 ERRHRD
, ERRdiskfull
);
469 srv_set_message(outbuf
,0,0,true);
470 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
471 fsp
->fsp_name
, nt_errstr(status
) ));
474 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nwritten
;
478 if (!srv_send_smb(smbd_server_fd(),outbuf
,IS_CONN_ENCRYPTED(fsp
->conn
),
480 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
483 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
484 "for file %s, offset %.0f, requested %u, written = %u\n",
485 fsp
->fsp_name
, (double)aio_ex
->acb
.aio_offset
,
486 (unsigned int)numtowrite
, (unsigned int)nwritten
));
491 /****************************************************************************
492 Handle any aio completion. Returns True if finished (and sets *perr if err
493 was non-zero), False if not.
494 *****************************************************************************/
496 static bool handle_aio_completed(struct aio_extra
*aio_ex
, int *perr
)
501 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
505 /* Ensure the operation has really completed. */
506 if (SMB_VFS_AIO_ERROR(aio_ex
->fsp
, &aio_ex
->acb
) == EINPROGRESS
) {
507 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
508 "process for file %s\n",
509 aio_ex
->req
->mid
, aio_ex
->fsp
->fsp_name
));
513 err
= aio_ex
->handle_completion(aio_ex
);
515 *perr
= err
; /* Only save non-zero errors. */
521 /****************************************************************************
522 Handle any aio completion inline.
523 Returns non-zero errno if fail or zero if all ok.
524 *****************************************************************************/
526 void smbd_aio_complete_mid(unsigned int mid
)
528 files_struct
*fsp
= NULL
;
529 struct aio_extra
*aio_ex
= find_aio_ex(mid
);
532 DEBUG(10,("smbd_aio_complete_mid: mid[%u]\n", mid
));
535 DEBUG(3,("smbd_aio_complete_mid: Can't find record to "
536 "match mid %u.\n", mid
));
537 srv_cancel_sign_response(mid
, false);
543 /* file was closed whilst I/O was outstanding. Just
545 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
546 "aio outstanding (mid[%u]).\n", mid
));
547 srv_cancel_sign_response(mid
, false);
551 if (!handle_aio_completed(aio_ex
, &ret
)) {
558 static void smbd_aio_signal_handler(struct tevent_context
*ev_ctx
,
559 struct tevent_signal
*se
,
560 int signum
, int count
,
561 void *_info
, void *private_data
)
563 siginfo_t
*info
= (siginfo_t
*)_info
;
564 unsigned int mid
= (unsigned int)info
->si_value
.sival_int
;
566 smbd_aio_complete_mid(mid
);
569 /****************************************************************************
570 We're doing write behind and the client closed the file. Wait up to 30
571 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
572 completed, errno to return if not.
573 *****************************************************************************/
575 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
577 int wait_for_aio_completion(files_struct
*fsp
)
579 struct aio_extra
*aio_ex
;
580 const SMB_STRUCT_AIOCB
**aiocb_list
;
581 int aio_completion_count
= 0;
582 time_t start_time
= time(NULL
);
585 for (seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
;
586 seconds_left
>= 0;) {
591 aio_completion_count
= 0;
592 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
593 if (aio_ex
->fsp
== fsp
) {
594 aio_completion_count
++;
598 if (!aio_completion_count
) {
602 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
603 "to complete.\n", aio_completion_count
));
605 aiocb_list
= SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB
*,
606 aio_completion_count
);
611 for( i
= 0, aio_ex
= aio_list_head
;
613 aio_ex
= aio_ex
->next
) {
614 if (aio_ex
->fsp
== fsp
) {
615 aiocb_list
[i
++] = &aio_ex
->acb
;
619 /* Now wait up to seconds_left for completion. */
620 ts
.tv_sec
= seconds_left
;
623 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
625 aio_completion_count
, seconds_left
));
627 err
= SMB_VFS_AIO_SUSPEND(fsp
, aiocb_list
,
628 aio_completion_count
, &ts
);
630 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
631 "errno = %s\n", err
, strerror(errno
) ));
633 if (err
== -1 && errno
== EAGAIN
) {
634 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
635 "out waiting for %d events after a wait of "
636 "%d seconds\n", aio_completion_count
,
639 cancel_aio_by_fsp(fsp
);
640 SAFE_FREE(aiocb_list
);
644 /* One or more events might have completed - process them if
646 for( i
= 0; i
< aio_completion_count
; i
++) {
647 uint16 mid
= aiocb_list
[i
]->aio_sigevent
.sigev_value
.sival_int
;
649 aio_ex
= find_aio_ex(mid
);
652 DEBUG(0, ("wait_for_aio_completion: mid %u "
653 "doesn't match an aio record\n",
654 (unsigned int)mid
));
658 if (!handle_aio_completed(aio_ex
, &err
)) {
664 SAFE_FREE(aiocb_list
);
665 seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
666 - (time(NULL
) - start_time
);
669 /* We timed out - we don't know why. Return ret if already an error,
671 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
673 aio_completion_count
));
678 /****************************************************************************
679 Cancel any outstanding aio requests. The client doesn't care about the reply.
680 *****************************************************************************/
682 void cancel_aio_by_fsp(files_struct
*fsp
)
684 struct aio_extra
*aio_ex
;
686 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
687 if (aio_ex
->fsp
== fsp
) {
688 /* Don't delete the aio_extra record as we may have
689 completed and don't yet know it. Just do the
690 aio_cancel call and return. */
691 SMB_VFS_AIO_CANCEL(fsp
, &aio_ex
->acb
);
692 aio_ex
->fsp
= NULL
; /* fsp will be closed when we
698 /****************************************************************************
699 Initialize the signal handler for aio read/write.
700 *****************************************************************************/
702 void initialize_async_io_handler(void)
704 aio_signal_event
= tevent_add_signal(smbd_event_context(),
705 smbd_event_context(),
706 RT_SIGNAL_AIO
, SA_SIGINFO
,
707 smbd_aio_signal_handler
,
709 if (!aio_signal_event
) {
710 exit_server("Failed to setup RT_SIGNAL_AIO handler");
713 /* tevent supports 100 signal with SA_SIGINFO */
714 aio_pending_size
= 100;
718 void initialize_async_io_handler(void)
722 bool schedule_aio_read_and_X(connection_struct
*conn
,
723 struct smb_request
*req
,
724 files_struct
*fsp
, SMB_OFF_T startpos
,
730 bool schedule_aio_write_and_X(connection_struct
*conn
,
731 struct smb_request
*req
,
732 files_struct
*fsp
, char *data
,
739 void cancel_aio_by_fsp(files_struct
*fsp
)
743 int wait_for_aio_completion(files_struct
*fsp
)
748 void smbd_aio_complete_mid(unsigned int mid
);