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, uint16 mid)
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, size_t outbuflen, uint16 mid)
82 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
89 /* We need space for an output reply of outbuflen bytes. */
90 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
91 if (!aio_ex->outbuf) {
95 /* Steal the input buffer containing the write data from the main SMB call. */
96 /* We must re-allocate a new one here. */
97 if (NewInBuffer(&aio_ex->inbuf) == NULL) {
98 SAFE_FREE(aio_ex->outbuf);
103 /* aio_ex->inbuf now contains the stolen old InBuf containing the data to write. */
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 to do as we've removed ourselves from the in use list first. */
120 free_InBuffer(aio_ex->inbuf);
122 SAFE_FREE(aio_ex->outbuf);
126 /****************************************************************************
127 Given the aiocb struct find the extended aio struct containing it.
128 *****************************************************************************/
130 static struct aio_extra *find_aio_ex(uint16 mid)
134 for( p = aio_list_head; p; p = p->next) {
142 /****************************************************************************
143 We can have these many aio buffers in flight.
144 *****************************************************************************/
146 #define AIO_PENDING_SIZE 10
147 static sig_atomic_t signals_received;
148 static int outstanding_aio_calls;
149 static uint16 aio_pending_array[AIO_PENDING_SIZE];
151 /****************************************************************************
152 Signal handler when an aio request completes.
153 *****************************************************************************/
155 static void signal_handler(int sig, siginfo_t *info, void *unused)
157 if (signals_received < AIO_PENDING_SIZE - 1) {
158 aio_pending_array[signals_received] = info->si_value.sival_int;
160 } /* Else signal is lost. */
161 sys_select_signal(RT_SIGNAL_AIO);
164 /****************************************************************************
165 Is there a signal waiting ?
166 *****************************************************************************/
168 BOOL aio_finished(void)
170 return (signals_received != 0);
173 /****************************************************************************
174 Initialize the signal handler for aio read/write.
175 *****************************************************************************/
177 void initialize_async_io_handler(void)
179 struct sigaction act;
182 act.sa_sigaction = signal_handler;
183 act.sa_flags = SA_SIGINFO;
184 sigemptyset( &act.sa_mask );
185 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
186 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
189 /* the signal can start off blocked due to a bug in bash */
190 BlockSignals(False, RT_SIGNAL_AIO);
193 /****************************************************************************
194 Set up an aio request from a SMBreadX call.
195 *****************************************************************************/
197 BOOL schedule_aio_read_and_X(connection_struct *conn,
198 char *inbuf, char *outbuf,
199 int length, int len_outbuf,
200 files_struct *fsp, SMB_OFF_T startpos,
203 struct aio_extra *aio_ex;
206 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
208 if (!min_aio_read_size || (smb_maxcnt < min_aio_read_size)) {
209 /* Too small a read for aio request. */
210 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
211 "for minimum aio_read of %u\n",
212 (unsigned int)smb_maxcnt,
213 (unsigned int)min_aio_read_size ));
217 /* Only do this on non-chained and non-chaining reads not using the write cache. */
218 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF) || (lp_write_cache_size(SNUM(conn)) != 0) ) {
222 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
223 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio activities outstanding.\n",
224 outstanding_aio_calls ));
228 /* The following is safe from integer wrap as we've already
229 checked smb_maxcnt is 128k or less. */
230 bufsize = PTR_DIFF(smb_buf(outbuf),outbuf) + smb_maxcnt;
232 if ((aio_ex = create_aio_ex_read(fsp, bufsize, SVAL(inbuf,smb_mid))) == NULL) {
233 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
237 /* Copy the SMB header already setup in outbuf. */
238 memcpy(aio_ex->outbuf, outbuf, smb_buf(outbuf) - outbuf);
239 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
243 /* Now set up the aio record for the read call. */
245 a->aio_fildes = fsp->fh->fd;
246 a->aio_buf = smb_buf(aio_ex->outbuf);
247 a->aio_nbytes = smb_maxcnt;
248 a->aio_offset = startpos;
249 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
250 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
251 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
253 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
254 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. Error %s\n",
256 delete_aio_ex(aio_ex);
260 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, offset %.0f, len = %u (mid = %u)\n",
261 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt, (unsigned int)aio_ex->mid ));
263 srv_defer_sign_response(aio_ex->mid);
264 outstanding_aio_calls++;
268 /****************************************************************************
269 Set up an aio request from a SMBwriteX call.
270 *****************************************************************************/
272 BOOL schedule_aio_write_and_X(connection_struct *conn,
273 char *inbuf, char *outbuf,
274 int length, int len_outbuf,
275 files_struct *fsp, char *data,
279 struct aio_extra *aio_ex;
282 BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
283 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
285 if (!min_aio_write_size || (numtowrite < min_aio_write_size)) {
286 /* Too small a write for aio request. */
287 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too small "
288 "for minimum aio_write of %u\n",
289 (unsigned int)numtowrite,
290 (unsigned int)min_aio_write_size ));
294 /* Only do this on non-chained and non-chaining reads not using the write cache. */
295 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF) || (lp_write_cache_size(SNUM(conn)) != 0) ) {
299 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
300 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio activities outstanding.\n",
301 outstanding_aio_calls ));
302 DEBUG(10,("schedule_aio_write_and_X: failed to schedule aio_write for file %s, offset %.0f, len = %u (mid = %u)\n",
303 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite, (unsigned int)SVAL(inbuf,smb_mid) ));
307 outbufsize = smb_len(outbuf) + 4;
308 if ((aio_ex = create_aio_ex_write(fsp, outbufsize, SVAL(inbuf,smb_mid))) == NULL) {
309 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
314 SMB_ASSERT(aio_ex->inbuf == inbuf);
316 /* Copy the SMB header already setup in outbuf. */
317 memcpy(aio_ex->outbuf, outbuf, outbufsize);
318 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
322 /* Now set up the aio record for the write call. */
324 a->aio_fildes = fsp->fh->fd;
325 a->aio_buf = data; /* As we've stolen inbuf this points within inbuf. */
326 a->aio_nbytes = numtowrite;
327 a->aio_offset = startpos;
328 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
329 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
330 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
332 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
333 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. Error %s\n",
335 /* Replace global InBuf as we're going to do a normal write. */
336 set_InBuffer(aio_ex->inbuf);
337 aio_ex->inbuf = NULL;
338 delete_aio_ex(aio_ex);
342 if (!write_through && !lp_syncalways(SNUM(fsp->conn)) && fsp->aio_write_behind) {
343 /* Lie to the client and immediately claim we finished the write. */
344 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
345 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
346 show_msg(aio_ex->outbuf);
347 if (!send_smb(smbd_server_fd(),aio_ex->outbuf)) {
348 exit_server_cleanly("handle_aio_write: send_smb failed.");
350 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write behind for file %s\n",
353 srv_defer_sign_response(aio_ex->mid);
355 outstanding_aio_calls++;
357 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file %s, \
358 offset %.0f, len = %u (mid = %u) outstanding_aio_calls = %d\n",
359 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite, (unsigned int)aio_ex->mid, outstanding_aio_calls ));
365 /****************************************************************************
366 Complete the read and return the data or error back to the client.
367 Returns errno or zero if all ok.
368 *****************************************************************************/
370 static int handle_aio_read_complete(struct aio_extra *aio_ex)
374 char *outbuf = aio_ex->outbuf;
375 char *data = smb_buf(outbuf);
376 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
379 /* We're relying here on the fact that if the fd is
380 closed then the aio will complete and aio_return
381 will return an error. Hopefully this is
384 /* If errno is ECANCELED then don't return anything to the client. */
385 if (errno == ECANCELED) {
386 srv_cancel_sign_response(aio_ex->mid);
390 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. Error = %s\n",
391 aio_ex->fsp->fsp_name, strerror(errno) ));
393 outsize = (UNIXERROR(ERRDOS,ERRnoaccess));
396 outsize = set_message(outbuf,12,nread,False);
397 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
398 SSVAL(outbuf,smb_vwv5,nread);
399 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
400 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
401 SSVAL(smb_buf(outbuf),-2,nread);
403 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d nread=%d\n",
404 aio_ex->fsp->fsp_name,
405 aio_ex->acb.aio_nbytes, (int)nread ) );
408 smb_setlen(outbuf,outsize - 4);
410 if (!send_smb(smbd_server_fd(),outbuf)) {
411 exit_server_cleanly("handle_aio_read_complete: send_smb failed.");
414 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed for file %s, offset %.0f, len = %u\n",
415 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset, (unsigned int)nread ));
420 /****************************************************************************
421 Complete the write and return the data or error back to the client.
422 Returns errno or zero if all ok.
423 *****************************************************************************/
425 static int handle_aio_write_complete(struct aio_extra *aio_ex)
428 files_struct *fsp = aio_ex->fsp;
429 char *outbuf = aio_ex->outbuf;
430 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
431 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
433 if (fsp->aio_write_behind) {
434 if (nwritten != numtowrite) {
435 if (nwritten == -1) {
436 DEBUG(5,("handle_aio_write_complete: aio_write_behind failed ! File %s is corrupt ! Error %s\n",
437 fsp->fsp_name, strerror(errno) ));
440 DEBUG(0,("handle_aio_write_complete: aio_write_behind failed ! File %s is corrupt ! \
441 Wanted %u bytes but only wrote %d\n", fsp->fsp_name, (unsigned int)numtowrite, (int)nwritten ));
445 DEBUG(10,("handle_aio_write_complete: aio_write_behind completed for file %s\n",
451 /* We don't need outsize or set_message here as we've already set the
452 fixed size length when we set up the aio call. */
455 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. nwritten == %d. Error = %s\n",
456 fsp->fsp_name, (unsigned int)numtowrite,
457 (int)nwritten, strerror(errno) ));
459 /* If errno is ECANCELED then don't return anything to the client. */
460 if (errno == ECANCELED) {
461 srv_cancel_sign_response(aio_ex->mid);
465 UNIXERROR(ERRHRD,ERRdiskfull);
468 BOOL write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
470 SSVAL(outbuf,smb_vwv2,nwritten);
471 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
472 if (nwritten < (ssize_t)numtowrite) {
473 SCVAL(outbuf,smb_rcls,ERRHRD);
474 SSVAL(outbuf,smb_err,ERRdiskfull);
477 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
478 sync_file(fsp->conn,fsp, write_through);
482 if (!send_smb(smbd_server_fd(),outbuf)) {
483 exit_server_cleanly("handle_aio_write: send_smb failed.");
486 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed for file %s, offset %.0f, requested %u, written = %u\n",
487 fsp->fsp_name, (double)aio_ex->acb.aio_offset, (unsigned int)numtowrite, (unsigned int)nwritten ));
492 /****************************************************************************
493 Handle any aio completion. Returns True if finished (and sets *perr if err was non-zero),
495 *****************************************************************************/
497 static BOOL handle_aio_completed(struct aio_extra *aio_ex, int *perr)
501 /* Ensure the operation has really completed. */
502 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
503 DEBUG(10,( "handle_aio_completed: operation mid %u still in process for file %s\n",
504 aio_ex->mid, aio_ex->fsp->fsp_name ));
508 if (aio_ex->read_req) {
509 err = handle_aio_read_complete(aio_ex);
511 err = handle_aio_write_complete(aio_ex);
515 *perr = err; /* Only save non-zero errors. */
521 /****************************************************************************
522 Handle any aio completion inline.
523 Returns non-zero errno if fail or zero if all ok.
524 *****************************************************************************/
526 int process_aio_queue(void)
531 BlockSignals(True, RT_SIGNAL_AIO);
533 DEBUG(10,("process_aio_queue: signals_received = %d\n", (int)signals_received));
534 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n", outstanding_aio_calls));
536 if (!signals_received) {
537 BlockSignals(False, RT_SIGNAL_AIO);
541 /* Drain all the complete aio_reads. */
542 for (i = 0; i < signals_received; i++) {
543 uint16 mid = aio_pending_array[i];
544 files_struct *fsp = NULL;
545 struct aio_extra *aio_ex = find_aio_ex(mid);
548 DEBUG(3,("process_aio_queue: Can't find record to match mid %u.\n",
550 srv_cancel_sign_response(mid);
556 /* file was closed whilst I/O was outstanding. Just ignore. */
557 DEBUG( 3,( "process_aio_queue: file closed whilst aio outstanding.\n"));
558 srv_cancel_sign_response(mid);
562 if (!handle_aio_completed(aio_ex, &ret)) {
566 delete_aio_ex(aio_ex);
569 outstanding_aio_calls -= signals_received;
570 signals_received = 0;
571 BlockSignals(False, RT_SIGNAL_AIO);
575 /****************************************************************************
576 We're doing write behind and the client closed the file. Wait up to 30 seconds
577 (my arbitrary choice) for the aio to complete. Return 0 if all writes completed,
578 errno to return if not.
579 *****************************************************************************/
581 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
583 BOOL wait_for_aio_completion(files_struct *fsp)
585 struct aio_extra *aio_ex;
586 const SMB_STRUCT_AIOCB **aiocb_list;
587 int aio_completion_count = 0;
588 time_t start_time = time(NULL);
592 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT; seconds_left >= 0;) {
597 aio_completion_count = 0;
598 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
599 if (aio_ex->fsp == fsp) {
600 aio_completion_count++;
604 if (!aio_completion_count) {
608 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events to complete.\n",
609 aio_completion_count ));
611 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *, aio_completion_count);
616 for( i = 0, aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
617 if (aio_ex->fsp == fsp) {
618 aiocb_list[i++] = &aio_ex->acb;
622 /* Now wait up to seconds_left for completion. */
623 ts.tv_sec = seconds_left;
626 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait of %d seconds.\n",
627 aio_completion_count, seconds_left ));
629 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list, aio_completion_count, &ts);
631 DEBUG(10,("wait_for_aio_completion: returned err = %d, errno = %s\n",
632 err, strerror(errno) ));
634 if (err == -1 && errno == EAGAIN) {
635 DEBUG(0,("wait_for_aio_completion: aio_suspend timed out waiting for %d events after a wait of %d seconds\n",
636 aio_completion_count, seconds_left));
638 cancel_aio_by_fsp(fsp);
639 SAFE_FREE(aiocb_list);
640 return ret ? ret : EIO;
643 /* One or more events might have completed - process them if so. */
644 for( i = 0; i < aio_completion_count; i++) {
645 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
647 aio_ex = find_aio_ex(mid);
650 DEBUG(0, ("wait_for_aio_completion: mid %u doesn't match an aio record\n",
651 (unsigned int)mid ));
655 if (!handle_aio_completed(aio_ex, &err)) {
658 delete_aio_ex(aio_ex);
661 SAFE_FREE(aiocb_list);
662 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT - (time(NULL) - start_time);
665 /* We timed out - we don't know why. Return ret if already an error, else EIO. */
666 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting for %d events\n",
667 aio_completion_count));
669 return ret ? ret : EIO;
672 /****************************************************************************
673 Cancel any outstanding aio requests. The client doesn't care about the reply.
674 *****************************************************************************/
676 void cancel_aio_by_fsp(files_struct *fsp)
678 struct aio_extra *aio_ex;
680 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
681 if (aio_ex->fsp == fsp) {
682 /* Don't delete the aio_extra record as we may have completed
683 and don't yet know it. Just do the aio_cancel call and return. */
684 SMB_VFS_AIO_CANCEL(fsp,fsp->fh->fd, &aio_ex->acb);
685 aio_ex->fsp = NULL; /* fsp will be closed when we return. */
690 /****************************************************************************
691 Check if a buffer was stolen for aio use.
692 *****************************************************************************/
694 BOOL aio_inbuffer_in_use(char *inbuf)
696 struct aio_extra *aio_ex;
698 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
699 if (aio_ex->inbuf == inbuf) {
706 BOOL aio_finished(void)
711 void initialize_async_io_handler(void)
715 int process_aio_queue(void)
720 BOOL schedule_aio_read_and_X(connection_struct *conn,
721 char *inbuf, char *outbuf,
722 int length, int len_outbuf,
723 files_struct *fsp, SMB_OFF_T startpos,
729 BOOL schedule_aio_write_and_X(connection_struct *conn,
730 char *inbuf, char *outbuf,
731 int length, int len_outbuf,
732 files_struct *fsp, char *data,
739 void cancel_aio_by_fsp(files_struct *fsp)
743 BOOL wait_for_aio_completion(files_struct *fsp)
748 BOOL aio_inbuffer_in_use(char *ptr)