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"));
97 static int handle_aio_read_complete(struct aio_extra
*aio_ex
, int errcode
);
98 static int handle_aio_write_complete(struct aio_extra
*aio_ex
, int errcode
);
99 static int handle_aio_smb2_read_complete(struct aio_extra
*aio_ex
, int errcode
);
100 static int handle_aio_smb2_write_complete(struct aio_extra
*aio_ex
, int errcode
);
102 static int aio_extra_destructor(struct aio_extra
*aio_ex
)
104 DLIST_REMOVE(aio_list_head
, aio_ex
);
105 outstanding_aio_calls
--;
109 /****************************************************************************
110 Create the extended aio struct we must keep around for the lifetime
112 *****************************************************************************/
114 static struct aio_extra
*create_aio_extra(TALLOC_CTX
*mem_ctx
,
118 struct aio_extra
*aio_ex
= talloc_zero(mem_ctx
, struct aio_extra
);
124 /* The output buffer stored in the aio_ex is the start of
125 the smb return buffer. The buffer used in the acb
126 is the start of the reply data portion of that buffer. */
129 aio_ex
->outbuf
= data_blob_talloc(aio_ex
, NULL
, buflen
);
130 if (!aio_ex
->outbuf
.data
) {
135 DLIST_ADD(aio_list_head
, aio_ex
);
136 talloc_set_destructor(aio_ex
, aio_extra_destructor
);
138 outstanding_aio_calls
++;
142 /****************************************************************************
143 Set up an aio request from a SMBreadX call.
144 *****************************************************************************/
146 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
147 struct smb_request
*smbreq
,
148 files_struct
*fsp
, off_t startpos
,
151 struct aio_extra
*aio_ex
;
154 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
157 if (fsp
->base_fsp
!= NULL
) {
158 /* No AIO on streams yet */
159 DEBUG(10, ("AIO on streams not yet supported\n"));
160 return NT_STATUS_RETRY
;
163 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
164 && !SMB_VFS_AIO_FORCE(fsp
)) {
165 /* Too small a read for aio request. */
166 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
167 "for minimum aio_read of %u\n",
168 (unsigned int)smb_maxcnt
,
169 (unsigned int)min_aio_read_size
));
170 return NT_STATUS_RETRY
;
173 /* Only do this on non-chained and non-chaining reads not using the
175 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
176 return NT_STATUS_RETRY
;
179 if (outstanding_aio_calls
>= aio_pending_size
) {
180 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
181 "activities outstanding.\n",
182 outstanding_aio_calls
));
183 return NT_STATUS_RETRY
;
186 /* The following is safe from integer wrap as we've already checked
187 smb_maxcnt is 128k or less. Wct is 12 for read replies */
189 bufsize
= smb_size
+ 12 * 2 + smb_maxcnt
;
191 if ((aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
)) == NULL
) {
192 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
193 return NT_STATUS_NO_MEMORY
;
195 aio_ex
->handle_completion
= handle_aio_read_complete
;
197 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
198 srv_set_message((char *)aio_ex
->outbuf
.data
, 12, 0, True
);
199 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
201 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
202 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
205 /* Take the lock until the AIO completes. */
206 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
208 return NT_STATUS_FILE_LOCK_CONFLICT
;
213 /* Now set up the aio record for the read call. */
215 a
->aio_fildes
= fsp
->fh
->fd
;
216 a
->aio_buf
= smb_buf(aio_ex
->outbuf
.data
);
217 a
->aio_nbytes
= smb_maxcnt
;
218 a
->aio_offset
= startpos
;
219 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
220 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
221 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
223 ret
= SMB_VFS_AIO_READ(fsp
, a
);
225 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
226 "Error %s\n", strerror(errno
) ));
227 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
229 return NT_STATUS_RETRY
;
232 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
234 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
235 "offset %.0f, len = %u (mid = %u)\n",
236 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
237 (unsigned int)aio_ex
->smbreq
->mid
));
242 /****************************************************************************
243 Set up an aio request from a SMBwriteX call.
244 *****************************************************************************/
246 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
247 struct smb_request
*smbreq
,
248 files_struct
*fsp
, const char *data
,
252 struct aio_extra
*aio_ex
;
255 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
258 if (fsp
->base_fsp
!= NULL
) {
259 /* No AIO on streams yet */
260 DEBUG(10, ("AIO on streams not yet supported\n"));
261 return NT_STATUS_RETRY
;
264 if ((!min_aio_write_size
|| (numtowrite
< min_aio_write_size
))
265 && !SMB_VFS_AIO_FORCE(fsp
)) {
266 /* Too small a write for aio request. */
267 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
268 "small for minimum aio_write of %u\n",
269 (unsigned int)numtowrite
,
270 (unsigned int)min_aio_write_size
));
271 return NT_STATUS_RETRY
;
274 /* Only do this on non-chained and non-chaining writes not using the
276 if (req_is_in_chain(smbreq
) || (lp_write_cache_size(SNUM(conn
)) != 0)) {
277 return NT_STATUS_RETRY
;
280 if (outstanding_aio_calls
>= aio_pending_size
) {
281 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
282 "activities outstanding.\n",
283 outstanding_aio_calls
));
284 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
285 "aio_write for file %s, offset %.0f, len = %u "
287 fsp_str_dbg(fsp
), (double)startpos
,
288 (unsigned int)numtowrite
,
289 (unsigned int)smbreq
->mid
));
290 return NT_STATUS_RETRY
;
293 bufsize
= smb_size
+ 6*2;
295 if (!(aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
))) {
296 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
297 return NT_STATUS_NO_MEMORY
;
299 aio_ex
->handle_completion
= handle_aio_write_complete
;
300 aio_ex
->write_through
= BITSETW(smbreq
->vwv
+7,0);
302 construct_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
303 srv_set_message((char *)aio_ex
->outbuf
.data
, 6, 0, True
);
304 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
306 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
307 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
310 /* Take the lock until the AIO completes. */
311 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
313 return NT_STATUS_FILE_LOCK_CONFLICT
;
318 /* Now set up the aio record for the write call. */
320 a
->aio_fildes
= fsp
->fh
->fd
;
321 a
->aio_buf
= discard_const_p(char, data
);
322 a
->aio_nbytes
= numtowrite
;
323 a
->aio_offset
= startpos
;
324 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
325 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
326 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
328 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
330 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
331 "Error %s\n", strerror(errno
) ));
332 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
334 return NT_STATUS_RETRY
;
337 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
339 /* This should actually be improved to span the write. */
340 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
341 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
343 if (!aio_ex
->write_through
&& !lp_syncalways(SNUM(fsp
->conn
))
344 && fsp
->aio_write_behind
) {
345 /* Lie to the client and immediately claim we finished the
347 SSVAL(aio_ex
->outbuf
.data
,smb_vwv2
,numtowrite
);
348 SSVAL(aio_ex
->outbuf
.data
,smb_vwv4
,(numtowrite
>>16)&1);
349 show_msg((char *)aio_ex
->outbuf
.data
);
350 if (!srv_send_smb(aio_ex
->smbreq
->sconn
,
351 (char *)aio_ex
->outbuf
.data
,
352 true, aio_ex
->smbreq
->seqnum
+1,
353 IS_CONN_ENCRYPTED(fsp
->conn
),
354 &aio_ex
->smbreq
->pcd
)) {
355 exit_server_cleanly("schedule_aio_write_and_X: "
356 "srv_send_smb failed.");
358 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
359 "behind for file %s\n", fsp_str_dbg(fsp
)));
362 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
363 "%s, offset %.0f, len = %u (mid = %u) "
364 "outstanding_aio_calls = %d\n",
365 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)numtowrite
,
366 (unsigned int)aio_ex
->smbreq
->mid
, outstanding_aio_calls
));
371 bool cancel_smb2_aio(struct smb_request
*smbreq
)
373 struct smbd_smb2_request
*smb2req
= smbreq
->smb2req
;
374 struct aio_extra
*aio_ex
= NULL
;
378 aio_ex
= talloc_get_type(smbreq
->async_priv
,
382 if (aio_ex
== NULL
) {
386 if (aio_ex
->fsp
== NULL
) {
390 ret
= SMB_VFS_AIO_CANCEL(aio_ex
->fsp
, &aio_ex
->acb
);
391 if (ret
!= AIO_CANCELED
) {
398 /****************************************************************************
399 Set up an aio request from a SMB2 read call.
400 *****************************************************************************/
402 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
403 struct smb_request
*smbreq
,
410 struct aio_extra
*aio_ex
;
412 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
415 if (fsp
->base_fsp
!= NULL
) {
416 /* No AIO on streams yet */
417 DEBUG(10, ("AIO on streams not yet supported\n"));
418 return NT_STATUS_RETRY
;
421 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
422 && !SMB_VFS_AIO_FORCE(fsp
)) {
423 /* Too small a read for aio request. */
424 DEBUG(10,("smb2: read size (%u) too small "
425 "for minimum aio_read of %u\n",
426 (unsigned int)smb_maxcnt
,
427 (unsigned int)min_aio_read_size
));
428 return NT_STATUS_RETRY
;
431 /* Only do this on reads not using the write cache. */
432 if (lp_write_cache_size(SNUM(conn
)) != 0) {
433 return NT_STATUS_RETRY
;
436 if (outstanding_aio_calls
>= aio_pending_size
) {
437 DEBUG(10,("smb2: Already have %d aio "
438 "activities outstanding.\n",
439 outstanding_aio_calls
));
440 return NT_STATUS_RETRY
;
443 /* Create the out buffer. */
444 *preadbuf
= data_blob_talloc(ctx
, NULL
, smb_maxcnt
);
445 if (preadbuf
->data
== NULL
) {
446 return NT_STATUS_NO_MEMORY
;
449 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
450 return NT_STATUS_NO_MEMORY
;
452 aio_ex
->handle_completion
= handle_aio_smb2_read_complete
;
454 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
455 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
458 /* Take the lock until the AIO completes. */
459 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
461 return NT_STATUS_FILE_LOCK_CONFLICT
;
466 /* Now set up the aio record for the read call. */
468 a
->aio_fildes
= fsp
->fh
->fd
;
469 a
->aio_buf
= preadbuf
->data
;
470 a
->aio_nbytes
= smb_maxcnt
;
471 a
->aio_offset
= startpos
;
472 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
473 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
474 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
476 ret
= SMB_VFS_AIO_READ(fsp
, a
);
478 DEBUG(0,("smb2: aio_read failed. "
479 "Error %s\n", strerror(errno
) ));
480 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
482 return NT_STATUS_RETRY
;
485 /* We don't need talloc_move here as both aio_ex and
486 * smbreq are children of smbreq->smb2req. */
487 aio_ex
->smbreq
= smbreq
;
488 smbreq
->async_priv
= aio_ex
;
490 DEBUG(10,("smb2: scheduled aio_read for file %s, "
491 "offset %.0f, len = %u (mid = %u)\n",
492 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
493 (unsigned int)aio_ex
->smbreq
->mid
));
498 /****************************************************************************
499 Set up an aio request from a SMB2write call.
500 *****************************************************************************/
502 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
503 struct smb_request
*smbreq
,
509 struct aio_extra
*aio_ex
= NULL
;
510 SMB_STRUCT_AIOCB
*a
= NULL
;
511 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
514 if (fsp
->base_fsp
!= NULL
) {
515 /* No AIO on streams yet */
516 DEBUG(10, ("AIO on streams not yet supported\n"));
517 return NT_STATUS_RETRY
;
520 if ((!min_aio_write_size
|| (in_data
.length
< min_aio_write_size
))
521 && !SMB_VFS_AIO_FORCE(fsp
)) {
522 /* Too small a write for aio request. */
523 DEBUG(10,("smb2: write size (%u) too "
524 "small for minimum aio_write of %u\n",
525 (unsigned int)in_data
.length
,
526 (unsigned int)min_aio_write_size
));
527 return NT_STATUS_RETRY
;
530 /* Only do this on writes not using the write cache. */
531 if (lp_write_cache_size(SNUM(conn
)) != 0) {
532 return NT_STATUS_RETRY
;
535 if (outstanding_aio_calls
>= aio_pending_size
) {
536 DEBUG(3,("smb2: Already have %d aio "
537 "activities outstanding.\n",
538 outstanding_aio_calls
));
539 return NT_STATUS_RETRY
;
542 if (!(aio_ex
= create_aio_extra(smbreq
->smb2req
, fsp
, 0))) {
543 return NT_STATUS_NO_MEMORY
;
546 aio_ex
->handle_completion
= handle_aio_smb2_write_complete
;
547 aio_ex
->write_through
= write_through
;
549 init_strict_lock_struct(fsp
, (uint64_t)smbreq
->smbpid
,
550 in_offset
, (uint64_t)in_data
.length
, WRITE_LOCK
,
553 /* Take the lock until the AIO completes. */
554 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &aio_ex
->lock
)) {
556 return NT_STATUS_FILE_LOCK_CONFLICT
;
561 /* Now set up the aio record for the write call. */
563 a
->aio_fildes
= fsp
->fh
->fd
;
564 a
->aio_buf
= in_data
.data
;
565 a
->aio_nbytes
= in_data
.length
;
566 a
->aio_offset
= in_offset
;
567 a
->aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
568 a
->aio_sigevent
.sigev_signo
= RT_SIGNAL_AIO
;
569 a
->aio_sigevent
.sigev_value
.sival_ptr
= aio_ex
;
571 ret
= SMB_VFS_AIO_WRITE(fsp
, a
);
573 DEBUG(3,("smb2: aio_write failed. "
574 "Error %s\n", strerror(errno
) ));
575 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &aio_ex
->lock
);
577 return NT_STATUS_RETRY
;
580 /* We don't need talloc_move here as both aio_ex and
581 * smbreq are children of smbreq->smb2req. */
582 aio_ex
->smbreq
= smbreq
;
583 smbreq
->async_priv
= aio_ex
;
585 /* This should actually be improved to span the write. */
586 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
587 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
590 * We don't want to do write behind due to ownership
591 * issues of the request structs. Maybe add it if I
592 * figure those out. JRA.
595 DEBUG(10,("smb2: scheduled aio_write for file "
596 "%s, offset %.0f, len = %u (mid = %u) "
597 "outstanding_aio_calls = %d\n",
600 (unsigned int)in_data
.length
,
601 (unsigned int)aio_ex
->smbreq
->mid
,
602 outstanding_aio_calls
));
607 /****************************************************************************
608 Complete the read and return the data or error back to the client.
609 Returns errno or zero if all ok.
610 *****************************************************************************/
612 static int handle_aio_read_complete(struct aio_extra
*aio_ex
, int errcode
)
615 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
616 char *data
= smb_buf(outbuf
);
617 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
620 /* We're relying here on the fact that if the fd is
621 closed then the aio will complete and aio_return
622 will return an error. Hopefully this is
625 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
627 fsp_str_dbg(aio_ex
->fsp
), (int)nread
, strerror(errcode
)));
629 ERROR_NT(map_nt_error_from_unix(errcode
));
630 outsize
= srv_set_message(outbuf
,0,0,true);
632 outsize
= srv_set_message(outbuf
,12,nread
,False
);
633 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be * -1. */
634 SSVAL(outbuf
,smb_vwv5
,nread
);
635 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
636 SSVAL(outbuf
,smb_vwv7
,((nread
>> 16) & 1));
637 SSVAL(smb_buf(outbuf
),-2,nread
);
639 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
640 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
642 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
644 fsp_str_dbg(aio_ex
->fsp
),
645 (int)aio_ex
->acb
.aio_nbytes
, (int)nread
) );
648 smb_setlen(outbuf
,outsize
- 4);
650 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
651 true, aio_ex
->smbreq
->seqnum
+1,
652 IS_CONN_ENCRYPTED(aio_ex
->fsp
->conn
), NULL
)) {
653 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
657 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
658 "for file %s, offset %.0f, len = %u\n",
659 fsp_str_dbg(aio_ex
->fsp
), (double)aio_ex
->acb
.aio_offset
,
660 (unsigned int)nread
));
665 /****************************************************************************
666 Complete the write and return the data or error back to the client.
667 Returns error code or zero if all ok.
668 *****************************************************************************/
670 static int handle_aio_write_complete(struct aio_extra
*aio_ex
, int errcode
)
672 files_struct
*fsp
= aio_ex
->fsp
;
673 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
674 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
675 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
677 if (fsp
->aio_write_behind
) {
678 if (nwritten
!= numtowrite
) {
679 if (nwritten
== -1) {
680 DEBUG(5,("handle_aio_write_complete: "
681 "aio_write_behind failed ! File %s "
682 "is corrupt ! Error %s\n",
683 fsp_str_dbg(fsp
), strerror(errcode
)));
685 DEBUG(0,("handle_aio_write_complete: "
686 "aio_write_behind failed ! File %s "
687 "is corrupt ! Wanted %u bytes but "
688 "only wrote %d\n", fsp_str_dbg(fsp
),
689 (unsigned int)numtowrite
,
694 DEBUG(10,("handle_aio_write_complete: "
695 "aio_write_behind completed for file %s\n",
698 /* TODO: should no return 0 in case of an error !!! */
702 /* We don't need outsize or set_message here as we've already set the
703 fixed size length when we set up the aio call. */
706 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
707 "nwritten == %d. Error = %s\n",
708 fsp_str_dbg(fsp
), (unsigned int)numtowrite
,
709 (int)nwritten
, strerror(errcode
) ));
711 ERROR_NT(map_nt_error_from_unix(errcode
));
712 srv_set_message(outbuf
,0,0,true);
716 SSVAL(outbuf
,smb_vwv2
,nwritten
);
717 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
718 if (nwritten
< (ssize_t
)numtowrite
) {
719 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
720 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
723 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
724 fsp_fnum_dbg(fsp
), (int)numtowrite
, (int)nwritten
));
725 status
= sync_file(fsp
->conn
,fsp
, aio_ex
->write_through
);
726 if (!NT_STATUS_IS_OK(status
)) {
728 ERROR_BOTH(map_nt_error_from_unix(errcode
),
729 ERRHRD
, ERRdiskfull
);
730 srv_set_message(outbuf
,0,0,true);
731 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
732 fsp_str_dbg(fsp
), nt_errstr(status
)));
735 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nwritten
;
739 if (!srv_send_smb(aio_ex
->smbreq
->sconn
, outbuf
,
740 true, aio_ex
->smbreq
->seqnum
+1,
741 IS_CONN_ENCRYPTED(fsp
->conn
),
743 exit_server_cleanly("handle_aio_write_complete: "
744 "srv_send_smb failed.");
747 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
748 "for file %s, offset %.0f, requested %u, written = %u\n",
749 fsp_str_dbg(fsp
), (double)aio_ex
->acb
.aio_offset
,
750 (unsigned int)numtowrite
, (unsigned int)nwritten
));
755 /****************************************************************************
756 Complete the read and return the data or error back to the client.
757 Returns errno or zero if all ok.
758 *****************************************************************************/
760 static int handle_aio_smb2_read_complete(struct aio_extra
*aio_ex
, int errcode
)
763 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
764 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
766 /* Common error or success code processing for async or sync
769 status
= smb2_read_complete(subreq
, nread
, errcode
);
772 aio_ex
->fsp
->fh
->pos
= aio_ex
->acb
.aio_offset
+ nread
;
773 aio_ex
->fsp
->fh
->position_information
= aio_ex
->fsp
->fh
->pos
;
776 DEBUG(10,("smb2: scheduled aio_read completed "
777 "for file %s, offset %.0f, len = %u "
778 "(errcode = %d, NTSTATUS = %s)\n",
779 fsp_str_dbg(aio_ex
->fsp
),
780 (double)aio_ex
->acb
.aio_offset
,
783 nt_errstr(status
) ));
785 if (!NT_STATUS_IS_OK(status
)) {
786 tevent_req_nterror(subreq
, status
);
790 tevent_req_done(subreq
);
794 /****************************************************************************
795 Complete the SMB2 write and return the data or error back to the client.
796 Returns error code or zero if all ok.
797 *****************************************************************************/
799 static int handle_aio_smb2_write_complete(struct aio_extra
*aio_ex
, int errcode
)
801 files_struct
*fsp
= aio_ex
->fsp
;
802 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
803 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
804 struct tevent_req
*subreq
= aio_ex
->smbreq
->smb2req
->subreq
;
807 status
= smb2_write_complete(subreq
, nwritten
, errcode
);
809 DEBUG(10,("smb2: scheduled aio_write completed "
810 "for file %s, offset %.0f, requested %u, "
811 "written = %u (errcode = %d, NTSTATUS = %s)\n",
813 (double)aio_ex
->acb
.aio_offset
,
814 (unsigned int)numtowrite
,
815 (unsigned int)nwritten
,
817 nt_errstr(status
) ));
819 if (!NT_STATUS_IS_OK(status
)) {
820 tevent_req_nterror(subreq
, status
);
824 tevent_req_done(subreq
);
828 /****************************************************************************
829 Handle any aio completion. Returns True if finished (and sets *perr if err
830 was non-zero), False if not.
831 *****************************************************************************/
833 static bool handle_aio_completed(struct aio_extra
*aio_ex
, int *perr
)
835 files_struct
*fsp
= NULL
;
839 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
844 DEBUG(3, ("handle_aio_completed: aio_ex->fsp == NULL\n"));
850 /* Ensure the operation has really completed. */
851 err
= SMB_VFS_AIO_ERROR(fsp
, &aio_ex
->acb
);
852 if (err
== EINPROGRESS
) {
853 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
854 "process for file %s\n",
855 (unsigned long long)aio_ex
->smbreq
->mid
,
856 fsp_str_dbg(aio_ex
->fsp
)));
860 if (err
== ECANCELED
) {
861 DEBUG(10,( "handle_aio_completed: operation mid %llu canceled "
863 (unsigned long long)aio_ex
->smbreq
->mid
,
864 fsp_str_dbg(aio_ex
->fsp
)));
867 /* Unlock now we're done. */
868 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &aio_ex
->lock
);
870 err
= aio_ex
->handle_completion(aio_ex
, err
);
872 *perr
= err
; /* Only save non-zero errors. */
878 /****************************************************************************
879 Handle any aio completion inline.
880 *****************************************************************************/
882 void smbd_aio_complete_aio_ex(struct aio_extra
*aio_ex
)
884 files_struct
*fsp
= NULL
;
887 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
888 (unsigned long long)aio_ex
->smbreq
->mid
));
892 /* file was closed whilst I/O was outstanding. Just
894 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
895 "aio outstanding (mid[%llu]).\n",
896 (unsigned long long)aio_ex
->smbreq
->mid
));
900 if (!handle_aio_completed(aio_ex
, &ret
)) {
905 /****************************************************************************
906 We're doing write behind and the client closed the file. Wait up to 45
907 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
908 completed, errno to return if not.
909 *****************************************************************************/
911 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 45
913 int wait_for_aio_completion(files_struct
*fsp
)
915 struct aio_extra
*aio_ex
;
916 const SMB_STRUCT_AIOCB
**aiocb_list
;
917 int aio_completion_count
= 0;
918 time_t start_time
= time_mono(NULL
);
921 for (seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
;
922 seconds_left
>= 0;) {
927 aio_completion_count
= 0;
928 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
929 if (aio_ex
->fsp
== fsp
) {
930 aio_completion_count
++;
934 if (!aio_completion_count
) {
938 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
939 "to complete.\n", aio_completion_count
));
941 aiocb_list
= SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB
*,
942 aio_completion_count
);
947 for( i
= 0, aio_ex
= aio_list_head
;
949 aio_ex
= aio_ex
->next
) {
950 if (aio_ex
->fsp
== fsp
) {
951 aiocb_list
[i
++] = &aio_ex
->acb
;
955 /* Now wait up to seconds_left for completion. */
956 ts
.tv_sec
= seconds_left
;
959 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
961 aio_completion_count
, seconds_left
));
963 err
= SMB_VFS_AIO_SUSPEND(fsp
, aiocb_list
,
964 aio_completion_count
, &ts
);
966 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
967 "errno = %s\n", err
, strerror(errno
) ));
969 if (err
== -1 && errno
== EAGAIN
) {
970 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
971 "out waiting for %d events after a wait of "
972 "%d seconds\n", aio_completion_count
,
975 SAFE_FREE(aiocb_list
);
976 /* We're hosed here - IO may complete
977 and trample over memory if we free
978 the aio_ex struct, but if we don't
979 we leak IO requests. I think smb_panic()
980 if the right thing to do here. JRA.
982 smb_panic("AIO suspend timed out - cannot continue.");
986 /* One or more events might have completed - process them if
988 for( i
= 0; i
< aio_completion_count
; i
++) {
989 aio_ex
= (struct aio_extra
*)aiocb_list
[i
]->aio_sigevent
.sigev_value
.sival_ptr
;
991 if (!handle_aio_completed(aio_ex
, &err
)) {
997 SAFE_FREE(aiocb_list
);
998 seconds_left
= SMB_TIME_FOR_AIO_COMPLETE_WAIT
999 - (time_mono(NULL
) - start_time
);
1002 /* We timed out - we don't know why. Return ret if already an error,
1004 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
1006 aio_completion_count
));
1013 bool initialize_async_io_handler(void)
1018 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
1019 struct smb_request
*smbreq
,
1020 files_struct
*fsp
, off_t startpos
,
1023 return NT_STATUS_RETRY
;
1026 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
1027 struct smb_request
*smbreq
,
1028 files_struct
*fsp
, const char *data
,
1032 return NT_STATUS_RETRY
;
1035 bool cancel_smb2_aio(struct smb_request
*smbreq
)
1040 NTSTATUS
schedule_smb2_aio_read(connection_struct
*conn
,
1041 struct smb_request
*smbreq
,
1044 DATA_BLOB
*preadbuf
,
1048 return NT_STATUS_RETRY
;
1051 NTSTATUS
schedule_aio_smb2_write(connection_struct
*conn
,
1052 struct smb_request
*smbreq
,
1058 return NT_STATUS_RETRY
;
1061 int wait_for_aio_completion(files_struct
*fsp
)
1066 void smbd_aio_complete_mid(uint64_t mid
);