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. */
27 #define RT_SIGNAL_AIO (SIGRTMIN+3)
30 /****************************************************************************
31 The buffer we keep around whilst an aio request is in process.
32 *****************************************************************************/
35 struct aio_extra *next, *prev;
44 static struct aio_extra *aio_list_head;
46 /****************************************************************************
47 Create the extended aio struct we must keep around for the lifetime
49 *****************************************************************************/
51 static struct aio_extra *create_aio_ex_read(files_struct *fsp, size_t buflen,
54 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
60 /* The output buffer stored in the aio_ex is the start of
61 the smb return buffer. The buffer used in the acb
62 is the start of the reply data portion of that buffer. */
63 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, buflen);
64 if (!aio_ex->outbuf) {
68 DLIST_ADD(aio_list_head, aio_ex);
70 aio_ex->read_req = True;
75 /****************************************************************************
76 Create the extended aio struct we must keep around for the lifetime
77 of the aio_write call.
78 *****************************************************************************/
80 static struct aio_extra *create_aio_ex_write(files_struct *fsp,
85 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
92 /* We need space for an output reply of outbuflen bytes. */
93 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
94 if (!aio_ex->outbuf) {
99 if (!(aio_ex->inbuf = SMB_MALLOC_ARRAY(char, inbuflen))) {
100 SAFE_FREE(aio_ex->outbuf);
105 DLIST_ADD(aio_list_head, aio_ex);
107 aio_ex->read_req = False;
112 /****************************************************************************
113 Delete the extended aio struct.
114 *****************************************************************************/
116 static void delete_aio_ex(struct aio_extra *aio_ex)
118 DLIST_REMOVE(aio_list_head, aio_ex);
119 SAFE_FREE(aio_ex->inbuf);
120 SAFE_FREE(aio_ex->outbuf);
124 /****************************************************************************
125 Given the aiocb struct find the extended aio struct containing it.
126 *****************************************************************************/
128 static struct aio_extra *find_aio_ex(uint16 mid)
132 for( p = aio_list_head; p; p = p->next) {
140 /****************************************************************************
141 We can have these many aio buffers in flight.
142 *****************************************************************************/
144 static int aio_pending_size;
145 static sig_atomic_t signals_received;
146 static int outstanding_aio_calls;
147 static uint16 *aio_pending_array;
149 /****************************************************************************
150 Signal handler when an aio request completes.
151 *****************************************************************************/
153 void aio_request_done(uint16_t mid)
155 if (signals_received < aio_pending_size) {
156 aio_pending_array[signals_received] = mid;
159 /* Else signal is lost. */
162 static void signal_handler(int sig, siginfo_t *info, void *unused)
164 aio_request_done(info->si_value.sival_int);
165 sys_select_signal(RT_SIGNAL_AIO);
168 /****************************************************************************
169 Is there a signal waiting ?
170 *****************************************************************************/
172 bool aio_finished(void)
174 return (signals_received != 0);
177 /****************************************************************************
178 Initialize the signal handler for aio read/write.
179 *****************************************************************************/
181 void initialize_async_io_handler(void)
183 struct sigaction act;
185 aio_pending_size = lp_maxmux();
186 aio_pending_array = SMB_MALLOC_ARRAY(uint16, aio_pending_size);
187 SMB_ASSERT(aio_pending_array != NULL);
190 act.sa_sigaction = signal_handler;
191 act.sa_flags = SA_SIGINFO;
192 sigemptyset( &act.sa_mask );
193 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
194 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
197 /* the signal can start off blocked due to a bug in bash */
198 BlockSignals(False, RT_SIGNAL_AIO);
201 /****************************************************************************
202 Set up an aio request from a SMBreadX call.
203 *****************************************************************************/
205 bool schedule_aio_read_and_X(connection_struct *conn,
206 struct smb_request *req,
207 files_struct *fsp, SMB_OFF_T startpos,
210 struct aio_extra *aio_ex;
213 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
215 if (fsp->base_fsp != NULL) {
216 /* No AIO on streams yet */
217 DEBUG(10, ("AIO on streams not yet supported\n"));
221 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
222 && !SMB_VFS_AIO_FORCE(fsp)) {
223 /* Too small a read for aio request. */
224 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
225 "for minimum aio_read of %u\n",
226 (unsigned int)smb_maxcnt,
227 (unsigned int)min_aio_read_size ));
231 /* Only do this on non-chained and non-chaining reads not using the
233 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
234 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
238 if (outstanding_aio_calls >= aio_pending_size) {
239 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
240 "activities outstanding.\n",
241 outstanding_aio_calls ));
245 /* The following is safe from integer wrap as we've already checked
246 smb_maxcnt is 128k or less. Wct is 12 for read replies */
248 bufsize = smb_size + 12 * 2 + smb_maxcnt;
250 if ((aio_ex = create_aio_ex_read(fsp, bufsize, req->mid)) == NULL) {
251 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
255 construct_reply_common((char *)req->inbuf, aio_ex->outbuf);
256 srv_set_message(aio_ex->outbuf, 12, 0, True);
257 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
261 /* Now set up the aio record for the read call. */
263 a->aio_fildes = fsp->fh->fd;
264 a->aio_buf = smb_buf(aio_ex->outbuf);
265 a->aio_nbytes = smb_maxcnt;
266 a->aio_offset = startpos;
267 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
268 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
269 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
272 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
273 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
274 "Error %s\n", strerror(errno) ));
275 delete_aio_ex(aio_ex);
281 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
282 "offset %.0f, len = %u (mid = %u)\n",
283 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
284 (unsigned int)aio_ex->mid ));
286 srv_defer_sign_response(aio_ex->mid);
287 outstanding_aio_calls++;
291 /****************************************************************************
292 Set up an aio request from a SMBwriteX call.
293 *****************************************************************************/
295 bool schedule_aio_write_and_X(connection_struct *conn,
296 struct smb_request *req,
297 files_struct *fsp, char *data,
301 struct aio_extra *aio_ex;
303 size_t inbufsize, outbufsize;
304 bool write_through = BITSETW(req->inbuf+smb_vwv7,0);
305 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
307 if (fsp->base_fsp != NULL) {
308 /* No AIO on streams yet */
309 DEBUG(10, ("AIO on streams not yet supported\n"));
313 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
314 && !SMB_VFS_AIO_FORCE(fsp)) {
315 /* Too small a write for aio request. */
316 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
317 "small for minimum aio_write of %u\n",
318 (unsigned int)numtowrite,
319 (unsigned int)min_aio_write_size ));
323 /* Only do this on non-chained and non-chaining reads not using the
325 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
326 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
330 if (outstanding_aio_calls >= aio_pending_size) {
331 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
332 "activities outstanding.\n",
333 outstanding_aio_calls ));
334 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
335 "aio_write for file %s, offset %.0f, len = %u "
337 fsp->fsp_name, (double)startpos,
338 (unsigned int)numtowrite,
339 (unsigned int)req->mid ));
343 inbufsize = smb_len(req->inbuf) + 4;
344 reply_outbuf(req, 6, 0);
345 outbufsize = smb_len(req->outbuf) + 4;
346 if (!(aio_ex = create_aio_ex_write(fsp, inbufsize, outbufsize,
348 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
352 /* Copy the SMB header already setup in outbuf. */
353 memcpy(aio_ex->inbuf, req->inbuf, inbufsize);
355 /* Copy the SMB header already setup in outbuf. */
356 memcpy(aio_ex->outbuf, req->outbuf, outbufsize);
357 TALLOC_FREE(req->outbuf);
358 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
362 /* Now set up the aio record for the write call. */
364 a->aio_fildes = fsp->fh->fd;
365 a->aio_buf = aio_ex->inbuf + (PTR_DIFF(data, req->inbuf));
366 a->aio_nbytes = numtowrite;
367 a->aio_offset = startpos;
368 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
369 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
370 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
373 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
374 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
375 "Error %s\n", strerror(errno) ));
376 delete_aio_ex(aio_ex);
382 release_level_2_oplocks_on_change(fsp);
384 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
385 && fsp->aio_write_behind) {
386 /* Lie to the client and immediately claim we finished the
388 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
389 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
390 show_msg(aio_ex->outbuf);
391 if (!srv_send_smb(smbd_server_fd(),aio_ex->outbuf,
392 IS_CONN_ENCRYPTED(fsp->conn))) {
393 exit_server_cleanly("handle_aio_write: srv_send_smb "
396 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
397 "behind for file %s\n", fsp->fsp_name ));
399 srv_defer_sign_response(aio_ex->mid);
401 outstanding_aio_calls++;
403 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
404 "%s, offset %.0f, len = %u (mid = %u) "
405 "outstanding_aio_calls = %d\n",
406 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
407 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
413 /****************************************************************************
414 Complete the read and return the data or error back to the client.
415 Returns errno or zero if all ok.
416 *****************************************************************************/
418 static int handle_aio_read_complete(struct aio_extra *aio_ex)
422 char *outbuf = aio_ex->outbuf;
423 char *data = smb_buf(outbuf);
424 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
427 /* We're relying here on the fact that if the fd is
428 closed then the aio will complete and aio_return
429 will return an error. Hopefully this is
432 /* If errno is ECANCELED then don't return anything to the
434 if (errno == ECANCELED) {
435 srv_cancel_sign_response(aio_ex->mid);
439 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
441 aio_ex->fsp->fsp_name, strerror(errno) ));
444 ERROR_NT(map_nt_error_from_unix(ret));
445 outsize = srv_set_message(outbuf,0,0,true);
447 outsize = srv_set_message(outbuf,12,nread,False);
448 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
449 SSVAL(outbuf,smb_vwv5,nread);
450 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
451 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
452 SSVAL(smb_buf(outbuf),-2,nread);
454 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
455 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
457 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
459 aio_ex->fsp->fsp_name,
460 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
463 smb_setlen(outbuf,outsize - 4);
465 if (!srv_send_smb(smbd_server_fd(),outbuf,
466 IS_CONN_ENCRYPTED(aio_ex->fsp->conn))) {
467 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
471 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
472 "for file %s, offset %.0f, len = %u\n",
473 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
474 (unsigned int)nread ));
479 /****************************************************************************
480 Complete the write and return the data or error back to the client.
481 Returns errno or zero if all ok.
482 *****************************************************************************/
484 static int handle_aio_write_complete(struct aio_extra *aio_ex)
487 files_struct *fsp = aio_ex->fsp;
488 char *outbuf = aio_ex->outbuf;
489 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
490 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
492 if (fsp->aio_write_behind) {
493 if (nwritten != numtowrite) {
494 if (nwritten == -1) {
495 DEBUG(5,("handle_aio_write_complete: "
496 "aio_write_behind failed ! File %s "
497 "is corrupt ! Error %s\n",
498 fsp->fsp_name, strerror(errno) ));
501 DEBUG(0,("handle_aio_write_complete: "
502 "aio_write_behind failed ! File %s "
503 "is corrupt ! Wanted %u bytes but "
504 "only wrote %d\n", fsp->fsp_name,
505 (unsigned int)numtowrite,
510 DEBUG(10,("handle_aio_write_complete: "
511 "aio_write_behind completed for file %s\n",
517 /* We don't need outsize or set_message here as we've already set the
518 fixed size length when we set up the aio call. */
521 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
522 "nwritten == %d. Error = %s\n",
523 fsp->fsp_name, (unsigned int)numtowrite,
524 (int)nwritten, strerror(errno) ));
526 /* If errno is ECANCELED then don't return anything to the
528 if (errno == ECANCELED) {
529 srv_cancel_sign_response(aio_ex->mid);
534 ERROR_BOTH(map_nt_error_from_unix(ret), ERRHRD, ERRdiskfull);
535 srv_set_message(outbuf,0,0,true);
537 bool write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
540 SSVAL(outbuf,smb_vwv2,nwritten);
541 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
542 if (nwritten < (ssize_t)numtowrite) {
543 SCVAL(outbuf,smb_rcls,ERRHRD);
544 SSVAL(outbuf,smb_err,ERRdiskfull);
547 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
548 fsp->fnum, (int)numtowrite, (int)nwritten));
549 status = sync_file(fsp->conn,fsp, write_through);
550 if (!NT_STATUS_IS_OK(status)) {
552 ERROR_BOTH(map_nt_error_from_unix(ret),
553 ERRHRD, ERRdiskfull);
554 srv_set_message(outbuf,0,0,true);
555 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
556 fsp->fsp_name, nt_errstr(status) ));
559 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
563 if (!srv_send_smb(smbd_server_fd(),outbuf,IS_CONN_ENCRYPTED(fsp->conn))) {
564 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
567 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
568 "for file %s, offset %.0f, requested %u, written = %u\n",
569 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
570 (unsigned int)numtowrite, (unsigned int)nwritten ));
575 /****************************************************************************
576 Handle any aio completion. Returns True if finished (and sets *perr if err
577 was non-zero), False if not.
578 *****************************************************************************/
580 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
584 /* Ensure the operation has really completed. */
585 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
586 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
587 "process for file %s\n",
588 aio_ex->mid, aio_ex->fsp->fsp_name ));
592 if (aio_ex->read_req) {
593 err = handle_aio_read_complete(aio_ex);
595 err = handle_aio_write_complete(aio_ex);
599 *perr = err; /* Only save non-zero errors. */
605 /****************************************************************************
606 Handle any aio completion inline.
607 Returns non-zero errno if fail or zero if all ok.
608 *****************************************************************************/
610 int process_aio_queue(void)
615 BlockSignals(True, RT_SIGNAL_AIO);
617 DEBUG(10,("process_aio_queue: signals_received = %d\n",
618 (int)signals_received));
619 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
620 outstanding_aio_calls));
622 if (!signals_received) {
623 BlockSignals(False, RT_SIGNAL_AIO);
627 /* Drain all the complete aio_reads. */
628 for (i = 0; i < signals_received; i++) {
629 uint16 mid = aio_pending_array[i];
630 files_struct *fsp = NULL;
631 struct aio_extra *aio_ex = find_aio_ex(mid);
634 DEBUG(3,("process_aio_queue: Can't find record to "
635 "match mid %u.\n", (unsigned int)mid));
636 srv_cancel_sign_response(mid);
642 /* file was closed whilst I/O was outstanding. Just
644 DEBUG( 3,( "process_aio_queue: file closed whilst "
645 "aio outstanding.\n"));
646 srv_cancel_sign_response(mid);
650 if (!handle_aio_completed(aio_ex, &ret)) {
654 delete_aio_ex(aio_ex);
657 outstanding_aio_calls -= signals_received;
658 signals_received = 0;
659 BlockSignals(False, RT_SIGNAL_AIO);
663 /****************************************************************************
664 We're doing write behind and the client closed the file. Wait up to 30
665 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
666 completed, errno to return if not.
667 *****************************************************************************/
669 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
671 int wait_for_aio_completion(files_struct *fsp)
673 struct aio_extra *aio_ex;
674 const SMB_STRUCT_AIOCB **aiocb_list;
675 int aio_completion_count = 0;
676 time_t start_time = time(NULL);
679 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
680 seconds_left >= 0;) {
685 aio_completion_count = 0;
686 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
687 if (aio_ex->fsp == fsp) {
688 aio_completion_count++;
692 if (!aio_completion_count) {
696 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
697 "to complete.\n", aio_completion_count ));
699 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
700 aio_completion_count);
705 for( i = 0, aio_ex = aio_list_head;
707 aio_ex = aio_ex->next) {
708 if (aio_ex->fsp == fsp) {
709 aiocb_list[i++] = &aio_ex->acb;
713 /* Now wait up to seconds_left for completion. */
714 ts.tv_sec = seconds_left;
717 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
719 aio_completion_count, seconds_left ));
721 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
722 aio_completion_count, &ts);
724 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
725 "errno = %s\n", err, strerror(errno) ));
727 if (err == -1 && errno == EAGAIN) {
728 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
729 "out waiting for %d events after a wait of "
730 "%d seconds\n", aio_completion_count,
733 cancel_aio_by_fsp(fsp);
734 SAFE_FREE(aiocb_list);
738 /* One or more events might have completed - process them if
740 for( i = 0; i < aio_completion_count; i++) {
741 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
743 aio_ex = find_aio_ex(mid);
746 DEBUG(0, ("wait_for_aio_completion: mid %u "
747 "doesn't match an aio record\n",
748 (unsigned int)mid ));
752 if (!handle_aio_completed(aio_ex, &err)) {
755 delete_aio_ex(aio_ex);
758 SAFE_FREE(aiocb_list);
759 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
760 - (time(NULL) - start_time);
763 /* We timed out - we don't know why. Return ret if already an error,
765 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
767 aio_completion_count));
772 /****************************************************************************
773 Cancel any outstanding aio requests. The client doesn't care about the reply.
774 *****************************************************************************/
776 void cancel_aio_by_fsp(files_struct *fsp)
778 struct aio_extra *aio_ex;
780 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
781 if (aio_ex->fsp == fsp) {
782 /* Don't delete the aio_extra record as we may have
783 completed and don't yet know it. Just do the
784 aio_cancel call and return. */
785 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
786 aio_ex->fsp = NULL; /* fsp will be closed when we
793 bool aio_finished(void)
798 void initialize_async_io_handler(void)
802 int process_aio_queue(void)
807 bool schedule_aio_read_and_X(connection_struct *conn,
808 struct smb_request *req,
809 files_struct *fsp, SMB_OFF_T startpos,
815 bool schedule_aio_write_and_X(connection_struct *conn,
816 struct smb_request *req,
817 files_struct *fsp, char *data,
824 void cancel_aio_by_fsp(files_struct *fsp)
828 int wait_for_aio_completion(files_struct *fsp)