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/>.
25 /* 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 *req;
50 int (*handle_completion)(struct aio_extra *ex);
53 static int handle_aio_read_complete(struct aio_extra *aio_ex);
54 static int handle_aio_write_complete(struct aio_extra *aio_ex);
56 static struct aio_extra *aio_list_head;
58 static int aio_extra_destructor(struct aio_extra *aio_ex)
60 DLIST_REMOVE(aio_list_head, aio_ex);
64 /****************************************************************************
65 Create the extended aio struct we must keep around for the lifetime
67 *****************************************************************************/
69 static struct aio_extra *create_aio_extra(files_struct *fsp, size_t buflen)
71 struct aio_extra *aio_ex = TALLOC_ZERO_P(NULL, struct aio_extra);
77 /* The output buffer stored in the aio_ex is the start of
78 the smb return buffer. The buffer used in the acb
79 is the start of the reply data portion of that buffer. */
81 aio_ex->outbuf = TALLOC_ARRAY(aio_ex, char, buflen);
82 if (!aio_ex->outbuf) {
86 DLIST_ADD(aio_list_head, aio_ex);
87 talloc_set_destructor(aio_ex, aio_extra_destructor);
92 /****************************************************************************
93 Given the mid find the extended aio struct containing it.
94 *****************************************************************************/
96 static struct aio_extra *find_aio_ex(uint16 mid)
100 for( p = aio_list_head; p; p = p->next) {
101 if (mid == p->req->mid) {
108 /****************************************************************************
109 We can have these many aio buffers in flight.
110 *****************************************************************************/
112 static int aio_pending_size;
113 static sig_atomic_t signals_received;
114 static int outstanding_aio_calls;
115 static uint16 *aio_pending_array;
117 /****************************************************************************
118 Signal handler when an aio request completes.
119 *****************************************************************************/
121 void aio_request_done(uint16_t mid)
123 if (signals_received < aio_pending_size) {
124 aio_pending_array[signals_received] = mid;
127 /* Else signal is lost. */
130 static void signal_handler(int sig, siginfo_t *info, void *unused)
132 aio_request_done(info->si_value.sival_int);
133 sys_select_signal(RT_SIGNAL_AIO);
136 /****************************************************************************
137 Is there a signal waiting ?
138 *****************************************************************************/
140 bool aio_finished(void)
142 return (signals_received != 0);
145 /****************************************************************************
146 Initialize the signal handler for aio read/write.
147 *****************************************************************************/
149 void initialize_async_io_handler(void)
151 struct sigaction act;
153 aio_pending_size = lp_maxmux();
154 aio_pending_array = SMB_MALLOC_ARRAY(uint16, aio_pending_size);
155 SMB_ASSERT(aio_pending_array != NULL);
158 act.sa_sigaction = signal_handler;
159 act.sa_flags = SA_SIGINFO;
160 sigemptyset( &act.sa_mask );
161 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
162 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
165 /* the signal can start off blocked due to a bug in bash */
166 BlockSignals(False, RT_SIGNAL_AIO);
169 /****************************************************************************
170 Set up an aio request from a SMBreadX call.
171 *****************************************************************************/
173 bool schedule_aio_read_and_X(connection_struct *conn,
174 struct smb_request *req,
175 files_struct *fsp, SMB_OFF_T startpos,
178 struct aio_extra *aio_ex;
181 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
184 if (fsp->base_fsp != NULL) {
185 /* No AIO on streams yet */
186 DEBUG(10, ("AIO on streams not yet supported\n"));
190 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
191 && !SMB_VFS_AIO_FORCE(fsp)) {
192 /* Too small a read for aio request. */
193 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
194 "for minimum aio_read of %u\n",
195 (unsigned int)smb_maxcnt,
196 (unsigned int)min_aio_read_size ));
200 /* Only do this on non-chained and non-chaining reads not using the
202 if (chain_size !=0 || (CVAL(req->vwv+0, 0) != 0xFF)
203 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
207 if (outstanding_aio_calls >= aio_pending_size) {
208 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
209 "activities outstanding.\n",
210 outstanding_aio_calls ));
214 /* The following is safe from integer wrap as we've already checked
215 smb_maxcnt is 128k or less. Wct is 12 for read replies */
217 bufsize = smb_size + 12 * 2 + smb_maxcnt;
219 if ((aio_ex = create_aio_extra(fsp, bufsize)) == NULL) {
220 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
223 aio_ex->handle_completion = handle_aio_read_complete;
225 construct_reply_common_req(req, aio_ex->outbuf);
226 srv_set_message(aio_ex->outbuf, 12, 0, True);
227 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
231 /* Now set up the aio record for the read call. */
233 a->aio_fildes = fsp->fh->fd;
234 a->aio_buf = smb_buf(aio_ex->outbuf);
235 a->aio_nbytes = smb_maxcnt;
236 a->aio_offset = startpos;
237 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
238 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
239 a->aio_sigevent.sigev_value.sival_int = req->mid;
242 ret = SMB_VFS_AIO_READ(fsp, a);
246 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
247 "Error %s\n", strerror(errno) ));
252 aio_ex->req = talloc_move(aio_ex, &req);
254 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
255 "offset %.0f, len = %u (mid = %u)\n",
256 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
257 (unsigned int)aio_ex->req->mid ));
259 srv_defer_sign_response(aio_ex->req->mid);
260 outstanding_aio_calls++;
264 /****************************************************************************
265 Set up an aio request from a SMBwriteX call.
266 *****************************************************************************/
268 bool schedule_aio_write_and_X(connection_struct *conn,
269 struct smb_request *req,
270 files_struct *fsp, char *data,
274 struct aio_extra *aio_ex;
277 bool write_through = BITSETW(req->vwv+7,0);
278 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
281 if (fsp->base_fsp != NULL) {
282 /* No AIO on streams yet */
283 DEBUG(10, ("AIO on streams not yet supported\n"));
287 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
288 && !SMB_VFS_AIO_FORCE(fsp)) {
289 /* Too small a write for aio request. */
290 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
291 "small for minimum aio_write of %u\n",
292 (unsigned int)numtowrite,
293 (unsigned int)min_aio_write_size ));
297 /* Only do this on non-chained and non-chaining reads not using the
299 if (chain_size !=0 || (CVAL(req->vwv+0, 0) != 0xFF)
300 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
304 if (outstanding_aio_calls >= aio_pending_size) {
305 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
306 "activities outstanding.\n",
307 outstanding_aio_calls ));
308 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
309 "aio_write for file %s, offset %.0f, len = %u "
311 fsp->fsp_name, (double)startpos,
312 (unsigned int)numtowrite,
313 (unsigned int)req->mid ));
317 bufsize = smb_size + 6*2;
319 if (!(aio_ex = create_aio_extra(fsp, bufsize))) {
320 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
323 aio_ex->handle_completion = handle_aio_write_complete;
325 construct_reply_common_req(req, aio_ex->outbuf);
326 srv_set_message(aio_ex->outbuf, 6, 0, True);
327 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
331 /* Now set up the aio record for the write call. */
333 a->aio_fildes = fsp->fh->fd;
335 a->aio_nbytes = numtowrite;
336 a->aio_offset = startpos;
337 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
338 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
339 a->aio_sigevent.sigev_value.sival_int = req->mid;
342 ret = SMB_VFS_AIO_WRITE(fsp, a);
346 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
347 "Error %s\n", strerror(errno) ));
352 aio_ex->req = talloc_move(aio_ex, &req);
354 release_level_2_oplocks_on_change(fsp);
356 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
357 && fsp->aio_write_behind) {
358 /* Lie to the client and immediately claim we finished the
360 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
361 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
362 show_msg(aio_ex->outbuf);
363 if (!srv_send_smb(smbd_server_fd(),aio_ex->outbuf,
364 IS_CONN_ENCRYPTED(fsp->conn))) {
365 exit_server_cleanly("handle_aio_write: srv_send_smb "
368 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
369 "behind for file %s\n", fsp->fsp_name ));
371 srv_defer_sign_response(aio_ex->req->mid);
373 outstanding_aio_calls++;
375 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
376 "%s, offset %.0f, len = %u (mid = %u) "
377 "outstanding_aio_calls = %d\n",
378 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
379 (unsigned int)aio_ex->req->mid, outstanding_aio_calls ));
385 /****************************************************************************
386 Complete the read and return the data or error back to the client.
387 Returns errno or zero if all ok.
388 *****************************************************************************/
390 static int handle_aio_read_complete(struct aio_extra *aio_ex)
394 char *outbuf = aio_ex->outbuf;
395 char *data = smb_buf(outbuf);
396 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
399 /* We're relying here on the fact that if the fd is
400 closed then the aio will complete and aio_return
401 will return an error. Hopefully this is
404 /* If errno is ECANCELED then don't return anything to the
406 if (errno == ECANCELED) {
407 srv_cancel_sign_response(aio_ex->req->mid);
411 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
413 aio_ex->fsp->fsp_name, strerror(errno) ));
416 ERROR_NT(map_nt_error_from_unix(ret));
417 outsize = srv_set_message(outbuf,0,0,true);
419 outsize = srv_set_message(outbuf,12,nread,False);
420 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
421 SSVAL(outbuf,smb_vwv5,nread);
422 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
423 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
424 SSVAL(smb_buf(outbuf),-2,nread);
426 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
427 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
429 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
431 aio_ex->fsp->fsp_name,
432 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
435 smb_setlen(outbuf,outsize - 4);
437 if (!srv_send_smb(smbd_server_fd(),outbuf,
438 IS_CONN_ENCRYPTED(aio_ex->fsp->conn))) {
439 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
443 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
444 "for file %s, offset %.0f, len = %u\n",
445 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
446 (unsigned int)nread ));
451 /****************************************************************************
452 Complete the write and return the data or error back to the client.
453 Returns errno or zero if all ok.
454 *****************************************************************************/
456 static int handle_aio_write_complete(struct aio_extra *aio_ex)
459 files_struct *fsp = aio_ex->fsp;
460 char *outbuf = aio_ex->outbuf;
461 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
462 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
464 if (fsp->aio_write_behind) {
465 if (nwritten != numtowrite) {
466 if (nwritten == -1) {
467 DEBUG(5,("handle_aio_write_complete: "
468 "aio_write_behind failed ! File %s "
469 "is corrupt ! Error %s\n",
470 fsp->fsp_name, strerror(errno) ));
473 DEBUG(0,("handle_aio_write_complete: "
474 "aio_write_behind failed ! File %s "
475 "is corrupt ! Wanted %u bytes but "
476 "only wrote %d\n", fsp->fsp_name,
477 (unsigned int)numtowrite,
482 DEBUG(10,("handle_aio_write_complete: "
483 "aio_write_behind completed for file %s\n",
489 /* We don't need outsize or set_message here as we've already set the
490 fixed size length when we set up the aio call. */
493 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
494 "nwritten == %d. Error = %s\n",
495 fsp->fsp_name, (unsigned int)numtowrite,
496 (int)nwritten, strerror(errno) ));
498 /* If errno is ECANCELED then don't return anything to the
500 if (errno == ECANCELED) {
501 srv_cancel_sign_response(aio_ex->req->mid);
506 ERROR_BOTH(map_nt_error_from_unix(ret), ERRHRD, ERRdiskfull);
507 srv_set_message(outbuf,0,0,true);
509 bool write_through = BITSETW(aio_ex->req->vwv+7,0);
512 SSVAL(outbuf,smb_vwv2,nwritten);
513 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
514 if (nwritten < (ssize_t)numtowrite) {
515 SCVAL(outbuf,smb_rcls,ERRHRD);
516 SSVAL(outbuf,smb_err,ERRdiskfull);
519 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
520 fsp->fnum, (int)numtowrite, (int)nwritten));
521 status = sync_file(fsp->conn,fsp, write_through);
522 if (!NT_STATUS_IS_OK(status)) {
524 ERROR_BOTH(map_nt_error_from_unix(ret),
525 ERRHRD, ERRdiskfull);
526 srv_set_message(outbuf,0,0,true);
527 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
528 fsp->fsp_name, nt_errstr(status) ));
531 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
535 if (!srv_send_smb(smbd_server_fd(),outbuf,IS_CONN_ENCRYPTED(fsp->conn))) {
536 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
539 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
540 "for file %s, offset %.0f, requested %u, written = %u\n",
541 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
542 (unsigned int)numtowrite, (unsigned int)nwritten ));
547 /****************************************************************************
548 Handle any aio completion. Returns True if finished (and sets *perr if err
549 was non-zero), False if not.
550 *****************************************************************************/
552 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
557 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
561 /* Ensure the operation has really completed. */
562 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
563 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
564 "process for file %s\n",
565 aio_ex->req->mid, aio_ex->fsp->fsp_name ));
569 err = aio_ex->handle_completion(aio_ex);
571 *perr = err; /* Only save non-zero errors. */
577 /****************************************************************************
578 Handle any aio completion inline.
579 Returns non-zero errno if fail or zero if all ok.
580 *****************************************************************************/
582 int process_aio_queue(void)
587 BlockSignals(True, RT_SIGNAL_AIO);
589 DEBUG(10,("process_aio_queue: signals_received = %d\n",
590 (int)signals_received));
591 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
592 outstanding_aio_calls));
594 if (!signals_received) {
595 BlockSignals(False, RT_SIGNAL_AIO);
599 /* Drain all the complete aio_reads. */
600 for (i = 0; i < signals_received; i++) {
601 uint16 mid = aio_pending_array[i];
602 files_struct *fsp = NULL;
603 struct aio_extra *aio_ex = find_aio_ex(mid);
606 DEBUG(3,("process_aio_queue: Can't find record to "
607 "match mid %u.\n", (unsigned int)mid));
608 srv_cancel_sign_response(mid);
614 /* file was closed whilst I/O was outstanding. Just
616 DEBUG( 3,( "process_aio_queue: file closed whilst "
617 "aio outstanding.\n"));
618 srv_cancel_sign_response(mid);
622 if (!handle_aio_completed(aio_ex, &ret)) {
629 outstanding_aio_calls -= signals_received;
630 signals_received = 0;
631 BlockSignals(False, RT_SIGNAL_AIO);
635 /****************************************************************************
636 We're doing write behind and the client closed the file. Wait up to 30
637 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
638 completed, errno to return if not.
639 *****************************************************************************/
641 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
643 int wait_for_aio_completion(files_struct *fsp)
645 struct aio_extra *aio_ex;
646 const SMB_STRUCT_AIOCB **aiocb_list;
647 int aio_completion_count = 0;
648 time_t start_time = time(NULL);
651 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
652 seconds_left >= 0;) {
657 aio_completion_count = 0;
658 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
659 if (aio_ex->fsp == fsp) {
660 aio_completion_count++;
664 if (!aio_completion_count) {
668 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
669 "to complete.\n", aio_completion_count ));
671 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
672 aio_completion_count);
677 for( i = 0, aio_ex = aio_list_head;
679 aio_ex = aio_ex->next) {
680 if (aio_ex->fsp == fsp) {
681 aiocb_list[i++] = &aio_ex->acb;
685 /* Now wait up to seconds_left for completion. */
686 ts.tv_sec = seconds_left;
689 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
691 aio_completion_count, seconds_left ));
693 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
694 aio_completion_count, &ts);
696 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
697 "errno = %s\n", err, strerror(errno) ));
699 if (err == -1 && errno == EAGAIN) {
700 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
701 "out waiting for %d events after a wait of "
702 "%d seconds\n", aio_completion_count,
705 cancel_aio_by_fsp(fsp);
706 SAFE_FREE(aiocb_list);
710 /* One or more events might have completed - process them if
712 for( i = 0; i < aio_completion_count; i++) {
713 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
715 aio_ex = find_aio_ex(mid);
718 DEBUG(0, ("wait_for_aio_completion: mid %u "
719 "doesn't match an aio record\n",
720 (unsigned int)mid ));
724 if (!handle_aio_completed(aio_ex, &err)) {
730 SAFE_FREE(aiocb_list);
731 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
732 - (time(NULL) - start_time);
735 /* We timed out - we don't know why. Return ret if already an error,
737 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
739 aio_completion_count));
744 /****************************************************************************
745 Cancel any outstanding aio requests. The client doesn't care about the reply.
746 *****************************************************************************/
748 void cancel_aio_by_fsp(files_struct *fsp)
750 struct aio_extra *aio_ex;
752 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
753 if (aio_ex->fsp == fsp) {
754 /* Don't delete the aio_extra record as we may have
755 completed and don't yet know it. Just do the
756 aio_cancel call and return. */
757 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
758 aio_ex->fsp = NULL; /* fsp will be closed when we
765 bool aio_finished(void)
770 void initialize_async_io_handler(void)
774 int process_aio_queue(void)
779 bool schedule_aio_read_and_X(connection_struct *conn,
780 struct smb_request *req,
781 files_struct *fsp, SMB_OFF_T startpos,
787 bool schedule_aio_write_and_X(connection_struct *conn,
788 struct smb_request *req,
789 files_struct *fsp, char *data,
796 void cancel_aio_by_fsp(files_struct *fsp)
800 int wait_for_aio_completion(files_struct *fsp)