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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* The signal we'll use to signify aio done. */
28 #define RT_SIGNAL_AIO (SIGRTMIN+3)
31 /****************************************************************************
32 The buffer we keep around whilst an aio request is in process.
33 *****************************************************************************/
36 struct aio_extra *next, *prev;
45 static struct aio_extra *aio_list_head;
47 /****************************************************************************
48 Create the extended aio struct we must keep around for the lifetime
50 *****************************************************************************/
52 static struct aio_extra *create_aio_ex_read(files_struct *fsp, size_t buflen,
55 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
61 /* The output buffer stored in the aio_ex is the start of
62 the smb return buffer. The buffer used in the acb
63 is the start of the reply data portion of that buffer. */
64 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, buflen);
65 if (!aio_ex->outbuf) {
69 DLIST_ADD(aio_list_head, aio_ex);
71 aio_ex->read_req = True;
76 /****************************************************************************
77 Create the extended aio struct we must keep around for the lifetime
78 of the aio_write call.
79 *****************************************************************************/
81 static struct aio_extra *create_aio_ex_write(files_struct *fsp,
82 size_t outbuflen, uint16 mid)
84 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
91 /* We need space for an output reply of outbuflen bytes. */
92 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
93 if (!aio_ex->outbuf) {
97 /* Steal the input buffer containing the write data from the main SMB
99 /* We must re-allocate a new one here. */
100 if (NewInBuffer(&aio_ex->inbuf) == NULL) {
101 SAFE_FREE(aio_ex->outbuf);
106 /* aio_ex->inbuf now contains the stolen old InBuf containing the data
109 DLIST_ADD(aio_list_head, aio_ex);
111 aio_ex->read_req = False;
116 /****************************************************************************
117 Delete the extended aio struct.
118 *****************************************************************************/
120 static void delete_aio_ex(struct aio_extra *aio_ex)
122 DLIST_REMOVE(aio_list_head, aio_ex);
123 /* Safe to do as we've removed ourselves from the in use list first. */
124 free_InBuffer(aio_ex->inbuf);
126 SAFE_FREE(aio_ex->outbuf);
130 /****************************************************************************
131 Given the aiocb struct find the extended aio struct containing it.
132 *****************************************************************************/
134 static struct aio_extra *find_aio_ex(uint16 mid)
138 for( p = aio_list_head; p; p = p->next) {
146 /****************************************************************************
147 We can have these many aio buffers in flight.
148 *****************************************************************************/
150 #define AIO_PENDING_SIZE 10
151 static sig_atomic_t signals_received;
152 static int outstanding_aio_calls;
153 static uint16 aio_pending_array[AIO_PENDING_SIZE];
155 /****************************************************************************
156 Signal handler when an aio request completes.
157 *****************************************************************************/
159 static void signal_handler(int sig, siginfo_t *info, void *unused)
161 if (signals_received < AIO_PENDING_SIZE - 1) {
162 aio_pending_array[signals_received] = info->si_value.sival_int;
164 } /* Else signal is lost. */
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;
186 act.sa_sigaction = signal_handler;
187 act.sa_flags = SA_SIGINFO;
188 sigemptyset( &act.sa_mask );
189 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
190 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
193 /* the signal can start off blocked due to a bug in bash */
194 BlockSignals(False, RT_SIGNAL_AIO);
197 /****************************************************************************
198 Set up an aio request from a SMBreadX call.
199 *****************************************************************************/
201 BOOL schedule_aio_read_and_X(connection_struct *conn,
202 char *inbuf, char *outbuf,
203 int length, int len_outbuf,
204 files_struct *fsp, SMB_OFF_T startpos,
207 struct aio_extra *aio_ex;
210 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
212 if (!min_aio_read_size || (smb_maxcnt < min_aio_read_size)) {
213 /* Too small a read for aio request. */
214 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
215 "for minimum aio_read of %u\n",
216 (unsigned int)smb_maxcnt,
217 (unsigned int)min_aio_read_size ));
221 /* Only do this on non-chained and non-chaining reads not using the
223 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF)
224 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
228 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
229 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
230 "activities outstanding.\n",
231 outstanding_aio_calls ));
235 /* The following is safe from integer wrap as we've already
236 checked smb_maxcnt is 128k or less. */
237 bufsize = PTR_DIFF(smb_buf(outbuf),outbuf) + smb_maxcnt;
239 if ((aio_ex = create_aio_ex_read(fsp, bufsize,
240 SVAL(inbuf,smb_mid))) == NULL) {
241 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
245 /* Copy the SMB header already setup in outbuf. */
246 memcpy(aio_ex->outbuf, outbuf, smb_buf(outbuf) - outbuf);
247 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
251 /* Now set up the aio record for the read call. */
253 a->aio_fildes = fsp->fh->fd;
254 a->aio_buf = smb_buf(aio_ex->outbuf);
255 a->aio_nbytes = smb_maxcnt;
256 a->aio_offset = startpos;
257 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
258 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
259 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
261 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
262 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
263 "Error %s\n", strerror(errno) ));
264 delete_aio_ex(aio_ex);
268 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
269 "offset %.0f, len = %u (mid = %u)\n",
270 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
271 (unsigned int)aio_ex->mid ));
273 srv_defer_sign_response(aio_ex->mid);
274 outstanding_aio_calls++;
278 /****************************************************************************
279 Set up an aio request from a SMBwriteX call.
280 *****************************************************************************/
282 BOOL schedule_aio_write_and_X(connection_struct *conn,
283 char *inbuf, char *outbuf,
284 int length, int len_outbuf,
285 files_struct *fsp, char *data,
289 struct aio_extra *aio_ex;
292 BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
293 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
295 if (!min_aio_write_size || (numtowrite < min_aio_write_size)) {
296 /* Too small a write for aio request. */
297 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
298 "small for minimum aio_write of %u\n",
299 (unsigned int)numtowrite,
300 (unsigned int)min_aio_write_size ));
304 /* Only do this on non-chained and non-chaining reads not using the
306 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF)
307 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
311 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
312 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
313 "activities outstanding.\n",
314 outstanding_aio_calls ));
315 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
316 "aio_write for file %s, offset %.0f, len = %u "
318 fsp->fsp_name, (double)startpos,
319 (unsigned int)numtowrite,
320 (unsigned int)SVAL(inbuf,smb_mid) ));
324 outbufsize = smb_len(outbuf) + 4;
325 if ((aio_ex = create_aio_ex_write(fsp, outbufsize,
326 SVAL(inbuf,smb_mid))) == NULL) {
327 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
332 SMB_ASSERT(aio_ex->inbuf == inbuf);
334 /* Copy the SMB header already setup in outbuf. */
335 memcpy(aio_ex->outbuf, outbuf, outbufsize);
336 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
340 /* Now set up the aio record for the write call. */
342 a->aio_fildes = fsp->fh->fd;
343 a->aio_buf = data; /* As we've stolen inbuf this points within
345 a->aio_nbytes = numtowrite;
346 a->aio_offset = startpos;
347 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
348 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
349 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
351 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
352 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
353 "Error %s\n", strerror(errno) ));
354 /* Replace global InBuf as we're going to do a normal write. */
355 set_InBuffer(aio_ex->inbuf);
356 aio_ex->inbuf = NULL;
357 delete_aio_ex(aio_ex);
361 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
362 && fsp->aio_write_behind) {
363 /* Lie to the client and immediately claim we finished the
365 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
366 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
367 show_msg(aio_ex->outbuf);
368 if (!send_smb(smbd_server_fd(),aio_ex->outbuf)) {
369 exit_server_cleanly("handle_aio_write: send_smb "
372 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
373 "behind for file %s\n", fsp->fsp_name ));
375 srv_defer_sign_response(aio_ex->mid);
377 outstanding_aio_calls++;
379 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
380 "%s, offset %.0f, len = %u (mid = %u) "
381 "outstanding_aio_calls = %d\n",
382 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
383 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
389 /****************************************************************************
390 Complete the read and return the data or error back to the client.
391 Returns errno or zero if all ok.
392 *****************************************************************************/
394 static int handle_aio_read_complete(struct aio_extra *aio_ex)
398 char *outbuf = aio_ex->outbuf;
399 char *data = smb_buf(outbuf);
400 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
403 /* We're relying here on the fact that if the fd is
404 closed then the aio will complete and aio_return
405 will return an error. Hopefully this is
408 /* If errno is ECANCELED then don't return anything to the
410 if (errno == ECANCELED) {
411 srv_cancel_sign_response(aio_ex->mid);
415 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
417 aio_ex->fsp->fsp_name, strerror(errno) ));
419 outsize = (UNIXERROR(ERRDOS,ERRnoaccess));
422 outsize = set_message(outbuf,12,nread,False);
423 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
424 SSVAL(outbuf,smb_vwv5,nread);
425 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
426 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
427 SSVAL(smb_buf(outbuf),-2,nread);
429 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
431 aio_ex->fsp->fsp_name,
432 aio_ex->acb.aio_nbytes, (int)nread ) );
435 smb_setlen(outbuf,outsize - 4);
437 if (!send_smb(smbd_server_fd(),outbuf)) {
438 exit_server_cleanly("handle_aio_read_complete: send_smb "
442 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
443 "for file %s, offset %.0f, len = %u\n",
444 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
445 (unsigned int)nread ));
450 /****************************************************************************
451 Complete the write and return the data or error back to the client.
452 Returns errno or zero if all ok.
453 *****************************************************************************/
455 static int handle_aio_write_complete(struct aio_extra *aio_ex)
458 files_struct *fsp = aio_ex->fsp;
459 char *outbuf = aio_ex->outbuf;
460 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
461 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
463 if (fsp->aio_write_behind) {
464 if (nwritten != numtowrite) {
465 if (nwritten == -1) {
466 DEBUG(5,("handle_aio_write_complete: "
467 "aio_write_behind failed ! File %s "
468 "is corrupt ! Error %s\n",
469 fsp->fsp_name, strerror(errno) ));
472 DEBUG(0,("handle_aio_write_complete: "
473 "aio_write_behind failed ! File %s "
474 "is corrupt ! Wanted %u bytes but "
475 "only wrote %d\n", fsp->fsp_name,
476 (unsigned int)numtowrite,
481 DEBUG(10,("handle_aio_write_complete: "
482 "aio_write_behind completed for file %s\n",
488 /* We don't need outsize or set_message here as we've already set the
489 fixed size length when we set up the aio call. */
492 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
493 "nwritten == %d. Error = %s\n",
494 fsp->fsp_name, (unsigned int)numtowrite,
495 (int)nwritten, strerror(errno) ));
497 /* If errno is ECANCELED then don't return anything to the
499 if (errno == ECANCELED) {
500 srv_cancel_sign_response(aio_ex->mid);
504 UNIXERROR(ERRHRD,ERRdiskfull);
507 BOOL write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
509 SSVAL(outbuf,smb_vwv2,nwritten);
510 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
511 if (nwritten < (ssize_t)numtowrite) {
512 SCVAL(outbuf,smb_rcls,ERRHRD);
513 SSVAL(outbuf,smb_err,ERRdiskfull);
516 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
517 fsp->fnum, (int)numtowrite, (int)nwritten));
518 sync_file(fsp->conn,fsp, write_through);
522 if (!send_smb(smbd_server_fd(),outbuf)) {
523 exit_server_cleanly("handle_aio_write: send_smb failed.");
526 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
527 "for file %s, offset %.0f, requested %u, written = %u\n",
528 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
529 (unsigned int)numtowrite, (unsigned int)nwritten ));
534 /****************************************************************************
535 Handle any aio completion. Returns True if finished (and sets *perr if err
536 was non-zero), False if not.
537 *****************************************************************************/
539 static BOOL handle_aio_completed(struct aio_extra *aio_ex, int *perr)
543 /* Ensure the operation has really completed. */
544 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
545 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
546 "process for file %s\n",
547 aio_ex->mid, aio_ex->fsp->fsp_name ));
551 if (aio_ex->read_req) {
552 err = handle_aio_read_complete(aio_ex);
554 err = handle_aio_write_complete(aio_ex);
558 *perr = err; /* Only save non-zero errors. */
564 /****************************************************************************
565 Handle any aio completion inline.
566 Returns non-zero errno if fail or zero if all ok.
567 *****************************************************************************/
569 int process_aio_queue(void)
574 BlockSignals(True, RT_SIGNAL_AIO);
576 DEBUG(10,("process_aio_queue: signals_received = %d\n",
577 (int)signals_received));
578 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
579 outstanding_aio_calls));
581 if (!signals_received) {
582 BlockSignals(False, RT_SIGNAL_AIO);
586 /* Drain all the complete aio_reads. */
587 for (i = 0; i < signals_received; i++) {
588 uint16 mid = aio_pending_array[i];
589 files_struct *fsp = NULL;
590 struct aio_extra *aio_ex = find_aio_ex(mid);
593 DEBUG(3,("process_aio_queue: Can't find record to "
594 "match mid %u.\n", (unsigned int)mid));
595 srv_cancel_sign_response(mid);
601 /* file was closed whilst I/O was outstanding. Just
603 DEBUG( 3,( "process_aio_queue: file closed whilst "
604 "aio outstanding.\n"));
605 srv_cancel_sign_response(mid);
609 if (!handle_aio_completed(aio_ex, &ret)) {
613 delete_aio_ex(aio_ex);
616 outstanding_aio_calls -= signals_received;
617 signals_received = 0;
618 BlockSignals(False, RT_SIGNAL_AIO);
622 /****************************************************************************
623 We're doing write behind and the client closed the file. Wait up to 30
624 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
625 completed, errno to return if not.
626 *****************************************************************************/
628 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
630 int wait_for_aio_completion(files_struct *fsp)
632 struct aio_extra *aio_ex;
633 const SMB_STRUCT_AIOCB **aiocb_list;
634 int aio_completion_count = 0;
635 time_t start_time = time(NULL);
638 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
639 seconds_left >= 0;) {
644 aio_completion_count = 0;
645 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
646 if (aio_ex->fsp == fsp) {
647 aio_completion_count++;
651 if (!aio_completion_count) {
655 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
656 "to complete.\n", aio_completion_count ));
658 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
659 aio_completion_count);
664 for( i = 0, aio_ex = aio_list_head;
666 aio_ex = aio_ex->next) {
667 if (aio_ex->fsp == fsp) {
668 aiocb_list[i++] = &aio_ex->acb;
672 /* Now wait up to seconds_left for completion. */
673 ts.tv_sec = seconds_left;
676 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
678 aio_completion_count, seconds_left ));
680 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
681 aio_completion_count, &ts);
683 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
684 "errno = %s\n", err, strerror(errno) ));
686 if (err == -1 && errno == EAGAIN) {
687 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
688 "out waiting for %d events after a wait of "
689 "%d seconds\n", aio_completion_count,
692 cancel_aio_by_fsp(fsp);
693 SAFE_FREE(aiocb_list);
697 /* One or more events might have completed - process them if
699 for( i = 0; i < aio_completion_count; i++) {
700 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
702 aio_ex = find_aio_ex(mid);
705 DEBUG(0, ("wait_for_aio_completion: mid %u "
706 "doesn't match an aio record\n",
707 (unsigned int)mid ));
711 if (!handle_aio_completed(aio_ex, &err)) {
714 delete_aio_ex(aio_ex);
717 SAFE_FREE(aiocb_list);
718 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
719 - (time(NULL) - start_time);
722 /* We timed out - we don't know why. Return ret if already an error,
724 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
726 aio_completion_count));
731 /****************************************************************************
732 Cancel any outstanding aio requests. The client doesn't care about the reply.
733 *****************************************************************************/
735 void cancel_aio_by_fsp(files_struct *fsp)
737 struct aio_extra *aio_ex;
739 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
740 if (aio_ex->fsp == fsp) {
741 /* Don't delete the aio_extra record as we may have
742 completed and don't yet know it. Just do the
743 aio_cancel call and return. */
744 SMB_VFS_AIO_CANCEL(fsp,fsp->fh->fd, &aio_ex->acb);
745 aio_ex->fsp = NULL; /* fsp will be closed when we
751 /****************************************************************************
752 Check if a buffer was stolen for aio use.
753 *****************************************************************************/
755 BOOL aio_inbuffer_in_use(char *inbuf)
757 struct aio_extra *aio_ex;
759 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
760 if (aio_ex->inbuf == inbuf) {
767 BOOL aio_finished(void)
772 void initialize_async_io_handler(void)
776 int process_aio_queue(void)
781 BOOL schedule_aio_read_and_X(connection_struct *conn,
782 char *inbuf, char *outbuf,
783 int length, int len_outbuf,
784 files_struct *fsp, SMB_OFF_T startpos,
790 BOOL schedule_aio_write_and_X(connection_struct *conn,
791 char *inbuf, char *outbuf,
792 int length, int len_outbuf,
793 files_struct *fsp, char *data,
800 void cancel_aio_by_fsp(files_struct *fsp)
804 BOOL wait_for_aio_completion(files_struct *fsp)
809 BOOL aio_inbuffer_in_use(char *ptr)