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 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(server_event_context(),
86 server_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
);
108 outstanding_aio_calls
--;
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
);
141 outstanding_aio_calls
++;
145 /****************************************************************************
146 Set up an aio request from a SMBreadX call.
147 *****************************************************************************/
149 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
150 struct smb_request
*smbreq
,
151 files_struct
*fsp
, off_t startpos
,
154 struct aio_extra
*aio_ex
;
157 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
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 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 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
342 /* This should actually be improved to span the write. */
343 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
344 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
346 if (!aio_ex
->write_through
&& !lp_syncalways(SNUM(fsp
->conn
))
347 && fsp
->aio_write_behind
) {
348 /* Lie to the client and immediately claim we finished the
350 SSVAL(aio_ex
->outbuf
.data
,smb_vwv2
,numtowrite
);
351 SSVAL(aio_ex
->outbuf
.data
,smb_vwv4
,(numtowrite
>>16)&1);
352 show_msg((char *)aio_ex
->outbuf
.data
);
353 if (!srv_send_smb(aio_ex
->smbreq
->sconn
,
354 (char *)aio_ex
->outbuf
.data
,
355 true, aio_ex
->smbreq
->seqnum
+1,
356 IS_CONN_ENCRYPTED(fsp
->conn
),
357 &aio_ex
->smbreq
->pcd
)) {
358 exit_server_cleanly("schedule_aio_write_and_X: "
359 "srv_send_smb failed.");
361 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
362 "behind for file %s\n", fsp_str_dbg(fsp
)));
365 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
366 "%s, offset %.0f, len = %u (mid = %u) "
367 "outstanding_aio_calls = %d\n",
368 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)numtowrite
,
369 (unsigned int)aio_ex
->smbreq
->mid
, outstanding_aio_calls
));
374 bool cancel_smb2_aio(struct smb_request
*smbreq
)
376 struct smbd_smb2_request
*smb2req
= smbreq
->smb2req
;
377 struct aio_extra
*aio_ex
= NULL
;
381 aio_ex
= talloc_get_type(smbreq
->async_priv
,
385 if (aio_ex
== NULL
) {
389 if (aio_ex
->fsp
== NULL
) {
393 ret
= SMB_VFS_AIO_CANCEL(aio_ex
->fsp
, &aio_ex
->acb
);
394 if (ret
!= AIO_CANCELED
) {
401 /****************************************************************************
402 Set up an aio request from a SMB2 read call.
403 *****************************************************************************/
405 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
406 struct smb_request
*smbreq
,
413 struct aio_extra
*aio_ex
;
415 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
418 if (fsp
->base_fsp
!= NULL
) {
419 /* No AIO on streams yet */
420 DEBUG(10, ("AIO on streams not yet supported\n"));
421 return NT_STATUS_RETRY
;
424 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
425 && !SMB_VFS_AIO_FORCE(fsp
)) {
426 /* Too small a read for aio request. */
427 DEBUG(10,("smb2: read size (%u) too small "
428 "for minimum aio_read of %u\n",
429 (unsigned int)smb_maxcnt
,
430 (unsigned int)min_aio_read_size
));
431 return NT_STATUS_RETRY
;
434 /* Only do this on reads not using the write cache. */
435 if (lp_write_cache_size(SNUM(conn
)) != 0) {
436 return NT_STATUS_RETRY
;
439 if (outstanding_aio_calls
>= aio_pending_size
) {
440 DEBUG(10,("smb2: Already have %d aio "
441 "activities outstanding.\n",
442 outstanding_aio_calls
));
443 return NT_STATUS_RETRY
;
446 /* Create the out buffer. */
447 *preadbuf
= data_blob_talloc(ctx
, NULL
, smb_maxcnt
);
448 if (preadbuf
->data
== NULL
) {
449 return NT_STATUS_NO_MEMORY
;
452 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
453 return NT_STATUS_NO_MEMORY
;
455 aio_ex
->handle_completion
= handle_aio_smb2_read_complete
;
457 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
458 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
461 /* Take the lock until the AIO completes. */
462 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
464 return NT_STATUS_FILE_LOCK_CONFLICT
;
469 /* Now set up the aio record for the read call. */
471 a
->aio_fildes
= fsp
->fh
->fd
;
472 a
->aio_buf
= preadbuf
->data
;
473 a
->aio_nbytes
= smb_maxcnt
;
474 a
->aio_offset
= startpos
;
475 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
476 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
477 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
479 ret
= SMB_VFS_AIO_READ(fsp
, a
);
481 DEBUG(0,("smb2: aio_read failed. "
482 "Error %s\n", strerror(errno
) ));
483 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
485 return NT_STATUS_RETRY
;
488 /* We don't need talloc_move here as both aio_ex and
489 * smbreq are children of smbreq->smb2req. */
490 aio_ex
->smbreq
= smbreq
;
491 smbreq
->async_priv
= aio_ex
;
493 DEBUG(10,("smb2: scheduled aio_read for file %s, "
494 "offset %.0f, len = %u (mid = %u)\n",
495 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
496 (unsigned int)aio_ex
->smbreq
->mid
));
501 /****************************************************************************
502 Set up an aio request from a SMB2write call.
503 *****************************************************************************/
505 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
506 struct smb_request
*smbreq
,
512 struct aio_extra
*aio_ex
= NULL
;
513 SMB_STRUCT_AIOCB
*a
= NULL
;
514 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
517 if (fsp
->base_fsp
!= NULL
) {
518 /* No AIO on streams yet */
519 DEBUG(10, ("AIO on streams not yet supported\n"));
520 return NT_STATUS_RETRY
;
523 if ((!min_aio_write_size
|| (in_data
.length
< min_aio_write_size
))
524 && !SMB_VFS_AIO_FORCE(fsp
)) {
525 /* Too small a write for aio request. */
526 DEBUG(10,("smb2: write size (%u) too "
527 "small for minimum aio_write of %u\n",
528 (unsigned int)in_data
.length
,
529 (unsigned int)min_aio_write_size
));
530 return NT_STATUS_RETRY
;
533 /* Only do this on writes not using the write cache. */
534 if (lp_write_cache_size(SNUM(conn
)) != 0) {
535 return NT_STATUS_RETRY
;
538 if (outstanding_aio_calls
>= aio_pending_size
) {
539 DEBUG(3,("smb2: Already have %d aio "
540 "activities outstanding.\n",
541 outstanding_aio_calls
));
542 return NT_STATUS_RETRY
;
545 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
546 return NT_STATUS_NO_MEMORY
;
549 aio_ex
->handle_completion
= handle_aio_smb2_write_complete
;
550 aio_ex
->write_through
= write_through
;
552 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
553 in_offset
, (uint64_t)in_data
.length
, WRITE_LOCK
,
556 /* Take the lock until the AIO completes. */
557 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
559 return NT_STATUS_FILE_LOCK_CONFLICT
;
564 /* Now set up the aio record for the write call. */
566 a
->aio_fildes
= fsp
->fh
->fd
;
567 a
->aio_buf
= in_data
.data
;
568 a
->aio_nbytes
= in_data
.length
;
569 a
->aio_offset
= in_offset
;
570 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
571 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
572 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
574 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
576 DEBUG(3,("smb2: aio_write failed. "
577 "Error %s\n", strerror(errno
) ));
578 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
580 return NT_STATUS_RETRY
;
583 /* We don't need talloc_move here as both aio_ex and
584 * smbreq are children of smbreq->smb2req. */
585 aio_ex
->smbreq
= smbreq
;
586 smbreq
->async_priv
= aio_ex
;
588 /* This should actually be improved to span the write. */
589 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
590 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
593 * We don't want to do write behind due to ownership
594 * issues of the request structs. Maybe add it if I
595 * figure those out. JRA.
598 DEBUG(10,("smb2: scheduled aio_write for file "
599 "%s, offset %.0f, len = %u (mid = %u) "
600 "outstanding_aio_calls = %d\n",
603 (unsigned int)in_data
.length
,
604 (unsigned int)aio_ex
->smbreq
->mid
,
605 outstanding_aio_calls
));
610 /****************************************************************************
611 Complete the read and return the data or error back to the client.
612 Returns errno or zero if all ok.
613 *****************************************************************************/
615 static int handle_aio_read_complete(struct aio_extra
*aio_ex
, int errcode
)
618 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
619 char *data
= smb_buf(outbuf
);
620 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
623 /* We're relying here on the fact that if the fd is
624 closed then the aio will complete and aio_return
625 will return an error. Hopefully this is
628 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
630 fsp_str_dbg(aio_ex
->fsp
), (int)nread
, strerror(errcode
)));
632 ERROR_NT(map_nt_error_from_unix(errcode
));
633 outsize
= srv_set_message(outbuf
,0,0,true);
635 outsize
= srv_set_message(outbuf
,12,nread
,False
);
636 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be * -1. */
637 SSVAL(outbuf
,smb_vwv5
,nread
);
638 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
639 SSVAL(outbuf
,smb_vwv7
,((nread
>> 16) & 1));
640 SSVAL(smb_buf(outbuf
),-2,nread
);
642 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
643 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
645 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
647 fsp_str_dbg(aio_ex
->fsp
),
648 (int)aio_ex
->acb
.aio_nbytes
, (int)nread
) );
651 smb_setlen(outbuf
,outsize
- 4);
653 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
654 true, aio_ex
->smbreq
->seqnum
+1,
655 IS_CONN_ENCRYPTED(aio_ex
->fsp
->conn
), NULL
)) {
656 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
660 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
661 "for file %s, offset %.0f, len = %u\n",
662 fsp_str_dbg(aio_ex
->fsp
), (double)aio_ex
->acb
.aio_offset
,
663 (unsigned int)nread
));
668 /****************************************************************************
669 Complete the write and return the data or error back to the client.
670 Returns error code or zero if all ok.
671 *****************************************************************************/
673 static int handle_aio_write_complete(struct aio_extra
*aio_ex
, int errcode
)
675 files_struct
*fsp
= aio_ex
->fsp
;
676 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
677 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
678 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
680 if (fsp
->aio_write_behind
) {
681 if (nwritten
!= numtowrite
) {
682 if (nwritten
== -1) {
683 DEBUG(5,("handle_aio_write_complete: "
684 "aio_write_behind failed ! File %s "
685 "is corrupt ! Error %s\n",
686 fsp_str_dbg(fsp
), strerror(errcode
)));
688 DEBUG(0,("handle_aio_write_complete: "
689 "aio_write_behind failed ! File %s "
690 "is corrupt ! Wanted %u bytes but "
691 "only wrote %d\n", fsp_str_dbg(fsp
),
692 (unsigned int)numtowrite
,
697 DEBUG(10,("handle_aio_write_complete: "
698 "aio_write_behind completed for file %s\n",
701 /* TODO: should no return 0 in case of an error !!! */
705 /* We don't need outsize or set_message here as we've already set the
706 fixed size length when we set up the aio call. */
709 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
710 "nwritten == %d. Error = %s\n",
711 fsp_str_dbg(fsp
), (unsigned int)numtowrite
,
712 (int)nwritten
, strerror(errcode
) ));
714 ERROR_NT(map_nt_error_from_unix(errcode
));
715 srv_set_message(outbuf
,0,0,true);
719 SSVAL(outbuf
,smb_vwv2
,nwritten
);
720 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
721 if (nwritten
< (ssize_t
)numtowrite
) {
722 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
723 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
726 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
727 fsp_fnum_dbg(fsp
), (int)numtowrite
, (int)nwritten
));
728 status
= sync_file(fsp
->conn
,fsp
, aio_ex
->write_through
);
729 if (!NT_STATUS_IS_OK(status
)) {
731 ERROR_BOTH(map_nt_error_from_unix(errcode
),
732 ERRHRD
, ERRdiskfull
);
733 srv_set_message(outbuf
,0,0,true);
734 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
735 fsp_str_dbg(fsp
), nt_errstr(status
)));
738 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nwritten
;
742 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
743 true, aio_ex
->smbreq
->seqnum
+1,
744 IS_CONN_ENCRYPTED(fsp
->conn
),
746 exit_server_cleanly("handle_aio_write_complete: "
747 "srv_send_smb failed.");
750 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
751 "for file %s, offset %.0f, requested %u, written = %u\n",
752 fsp_str_dbg(fsp
), (double)aio_ex
->acb
.aio_offset
,
753 (unsigned int)numtowrite
, (unsigned int)nwritten
));
758 /****************************************************************************
759 Complete the read and return the data or error back to the client.
760 Returns errno or zero if all ok.
761 *****************************************************************************/
763 static int handle_aio_smb2_read_complete(struct aio_extra
*aio_ex
, int errcode
)
766 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
767 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
769 /* Common error or success code processing for async or sync
772 status
= smb2_read_complete(subreq
, nread
, errcode
);
775 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
776 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
779 DEBUG(10,("smb2: scheduled aio_read completed "
780 "for file %s, offset %.0f, len = %u "
781 "(errcode = %d, NTSTATUS = %s)\n",
782 fsp_str_dbg(aio_ex
->fsp
),
783 (double)aio_ex
->acb
.aio_offset
,
786 nt_errstr(status
) ));
788 if (!NT_STATUS_IS_OK(status
)) {
789 tevent_req_nterror(subreq
, status
);
793 tevent_req_done(subreq
);
797 /****************************************************************************
798 Complete the SMB2 write and return the data or error back to the client.
799 Returns error code or zero if all ok.
800 *****************************************************************************/
802 static int handle_aio_smb2_write_complete(struct aio_extra
*aio_ex
, int errcode
)
804 files_struct
*fsp
= aio_ex
->fsp
;
805 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
806 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
807 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
810 status
= smb2_write_complete(subreq
, nwritten
, errcode
);
812 DEBUG(10,("smb2: scheduled aio_write completed "
813 "for file %s, offset %.0f, requested %u, "
814 "written = %u (errcode = %d, NTSTATUS = %s)\n",
816 (double)aio_ex
->acb
.aio_offset
,
817 (unsigned int)numtowrite
,
818 (unsigned int)nwritten
,
820 nt_errstr(status
) ));
822 if (!NT_STATUS_IS_OK(status
)) {
823 tevent_req_nterror(subreq
, status
);
827 tevent_req_done(subreq
);
831 /****************************************************************************
832 Handle any aio completion. Returns True if finished (and sets *perr if err
833 was non-zero), False if not.
834 *****************************************************************************/
836 static bool handle_aio_completed(struct aio_extra
*aio_ex
, int *perr
)
838 files_struct
*fsp
= NULL
;
842 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
847 DEBUG(3, ("handle_aio_completed: aio_ex->fsp == NULL\n"));
853 /* Ensure the operation has really completed. */
854 err
= SMB_VFS_AIO_ERROR(fsp
, &aio_ex
->acb
);
855 if (err
== EINPROGRESS
) {
856 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
857 "process for file %s\n",
858 (unsigned long long)aio_ex
->smbreq
->mid
,
859 fsp_str_dbg(aio_ex
->fsp
)));
863 if (err
== ECANCELED
) {
864 DEBUG(10,( "handle_aio_completed: operation mid %llu canceled "
866 (unsigned long long)aio_ex
->smbreq
->mid
,
867 fsp_str_dbg(aio_ex
->fsp
)));
870 /* Unlock now we're done. */
871 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
873 err
= aio_ex
->handle_completion(aio_ex
, err
);
875 *perr
= err
; /* Only save non-zero errors. */
881 /****************************************************************************
882 Handle any aio completion inline.
883 *****************************************************************************/
885 void smbd_aio_complete_aio_ex(struct aio_extra
*aio_ex
)
887 files_struct
*fsp
= NULL
;
890 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
891 (unsigned long long)aio_ex
->smbreq
->mid
));
895 /* file was closed whilst I/O was outstanding. Just
897 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
898 "aio outstanding (mid[%llu]).\n",
899 (unsigned long long)aio_ex
->smbreq
->mid
));
903 if (!handle_aio_completed(aio_ex
, &ret
)) {
908 /****************************************************************************
909 We're doing write behind and the client closed the file. Wait up to 45
910 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
911 completed, errno to return if not.
912 *****************************************************************************/
914 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 45
916 int wait_for_aio_completion(files_struct
*fsp
)
918 struct aio_extra
*aio_ex
;
919 const SMB_STRUCT_AIOCB
**aiocb_list
;
920 int aio_completion_count
= 0;
921 time_t start_time
= time_mono(NULL
);
924 for (seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
;
925 seconds_left
>= 0;) {
930 aio_completion_count
= 0;
931 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
932 if (aio_ex
->fsp
== fsp
) {
933 aio_completion_count
++;
937 if (!aio_completion_count
) {
941 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
942 "to complete.\n", aio_completion_count
));
944 aiocb_list
= SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB
*,
945 aio_completion_count
);
950 for( i
= 0, aio_ex
= aio_list_head
;
952 aio_ex
= aio_ex
->next
) {
953 if (aio_ex
->fsp
== fsp
) {
954 aiocb_list
[i
++] = &aio_ex
->acb
;
958 /* Now wait up to seconds_left for completion. */
959 ts
.tv_sec
= seconds_left
;
962 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
964 aio_completion_count
, seconds_left
));
966 err
= SMB_VFS_AIO_SUSPEND(fsp
, aiocb_list
,
967 aio_completion_count
, &ts
);
969 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
970 "errno = %s\n", err
, strerror(errno
) ));
972 if (err
== -1 && errno
== EAGAIN
) {
973 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
974 "out waiting for %d events after a wait of "
975 "%d seconds\n", aio_completion_count
,
978 SAFE_FREE(aiocb_list
);
979 /* We're hosed here - IO may complete
980 and trample over memory if we free
981 the aio_ex struct, but if we don't
982 we leak IO requests. I think smb_panic()
983 if the right thing to do here. JRA.
985 smb_panic("AIO suspend timed out - cannot continue.");
989 /* One or more events might have completed - process them if
991 for( i
= 0; i
< aio_completion_count
; i
++) {
992 aio_ex
= (struct aio_extra
*)aiocb_list
[i
]->aio_sigevent
.sigev_value
.sival_ptr
;
994 if (!handle_aio_completed(aio_ex
, &err
)) {
1000 SAFE_FREE(aiocb_list
);
1001 seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
1002 - (time_mono(NULL
) - start_time
);
1005 /* We timed out - we don't know why. Return ret if already an error,
1007 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
1009 aio_completion_count
));
1016 bool initialize_async_io_handler(void)
1021 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
1022 struct smb_request
*smbreq
,
1023 files_struct
*fsp
, off_t startpos
,
1026 return NT_STATUS_RETRY
;
1029 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
1030 struct smb_request
*smbreq
,
1031 files_struct
*fsp
, const char *data
,
1035 return NT_STATUS_RETRY
;
1038 bool cancel_smb2_aio(struct smb_request
*smbreq
)
1043 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
1044 struct smb_request
*smbreq
,
1047 DATA_BLOB
*preadbuf
,
1051 return NT_STATUS_RETRY
;
1054 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
1055 struct smb_request
*smbreq
,
1061 return NT_STATUS_RETRY
;
1064 int wait_for_aio_completion(files_struct
*fsp
)
1069 void smbd_aio_complete_mid(uint64_t mid
);