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;
271 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
272 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
273 "Error %s\n", strerror(errno) ));
274 delete_aio_ex(aio_ex);
278 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
279 "offset %.0f, len = %u (mid = %u)\n",
280 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
281 (unsigned int)aio_ex->mid ));
283 srv_defer_sign_response(aio_ex->mid);
284 outstanding_aio_calls++;
288 /****************************************************************************
289 Set up an aio request from a SMBwriteX call.
290 *****************************************************************************/
292 bool schedule_aio_write_and_X(connection_struct *conn,
293 struct smb_request *req,
294 files_struct *fsp, char *data,
298 struct aio_extra *aio_ex;
300 size_t inbufsize, outbufsize;
301 bool write_through = BITSETW(req->inbuf+smb_vwv7,0);
302 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
304 if (fsp->base_fsp != NULL) {
305 /* No AIO on streams yet */
306 DEBUG(10, ("AIO on streams not yet supported\n"));
310 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
311 && !SMB_VFS_AIO_FORCE(fsp)) {
312 /* Too small a write for aio request. */
313 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
314 "small for minimum aio_write of %u\n",
315 (unsigned int)numtowrite,
316 (unsigned int)min_aio_write_size ));
320 /* Only do this on non-chained and non-chaining reads not using the
322 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
323 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
327 if (outstanding_aio_calls >= aio_pending_size) {
328 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
329 "activities outstanding.\n",
330 outstanding_aio_calls ));
331 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
332 "aio_write for file %s, offset %.0f, len = %u "
334 fsp->fsp_name, (double)startpos,
335 (unsigned int)numtowrite,
336 (unsigned int)req->mid ));
340 inbufsize = smb_len(req->inbuf) + 4;
341 reply_outbuf(req, 6, 0);
342 outbufsize = smb_len(req->outbuf) + 4;
343 if (!(aio_ex = create_aio_ex_write(fsp, inbufsize, outbufsize,
345 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
349 /* Copy the SMB header already setup in outbuf. */
350 memcpy(aio_ex->inbuf, req->inbuf, inbufsize);
352 /* Copy the SMB header already setup in outbuf. */
353 memcpy(aio_ex->outbuf, req->outbuf, outbufsize);
354 TALLOC_FREE(req->outbuf);
355 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
359 /* Now set up the aio record for the write call. */
361 a->aio_fildes = fsp->fh->fd;
362 a->aio_buf = aio_ex->inbuf + (PTR_DIFF(data, req->inbuf));
363 a->aio_nbytes = numtowrite;
364 a->aio_offset = startpos;
365 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
366 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
367 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
369 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
370 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
371 "Error %s\n", strerror(errno) ));
372 delete_aio_ex(aio_ex);
376 release_level_2_oplocks_on_change(fsp);
378 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
379 && fsp->aio_write_behind) {
380 /* Lie to the client and immediately claim we finished the
382 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
383 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
384 show_msg(aio_ex->outbuf);
385 if (!srv_send_smb(smbd_server_fd(),aio_ex->outbuf,
386 IS_CONN_ENCRYPTED(fsp->conn))) {
387 exit_server_cleanly("handle_aio_write: srv_send_smb "
390 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
391 "behind for file %s\n", fsp->fsp_name ));
393 srv_defer_sign_response(aio_ex->mid);
395 outstanding_aio_calls++;
397 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
398 "%s, offset %.0f, len = %u (mid = %u) "
399 "outstanding_aio_calls = %d\n",
400 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
401 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
407 /****************************************************************************
408 Complete the read and return the data or error back to the client.
409 Returns errno or zero if all ok.
410 *****************************************************************************/
412 static int handle_aio_read_complete(struct aio_extra *aio_ex)
416 char *outbuf = aio_ex->outbuf;
417 char *data = smb_buf(outbuf);
418 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
421 /* We're relying here on the fact that if the fd is
422 closed then the aio will complete and aio_return
423 will return an error. Hopefully this is
426 /* If errno is ECANCELED then don't return anything to the
428 if (errno == ECANCELED) {
429 srv_cancel_sign_response(aio_ex->mid);
433 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
435 aio_ex->fsp->fsp_name, strerror(errno) ));
438 ERROR_NT(map_nt_error_from_unix(ret));
439 outsize = srv_set_message(outbuf,0,0,true);
441 outsize = srv_set_message(outbuf,12,nread,False);
442 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
443 SSVAL(outbuf,smb_vwv5,nread);
444 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
445 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
446 SSVAL(smb_buf(outbuf),-2,nread);
448 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
449 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
451 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
453 aio_ex->fsp->fsp_name,
454 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
457 smb_setlen(outbuf,outsize - 4);
459 if (!srv_send_smb(smbd_server_fd(),outbuf,
460 IS_CONN_ENCRYPTED(aio_ex->fsp->conn))) {
461 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
465 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
466 "for file %s, offset %.0f, len = %u\n",
467 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
468 (unsigned int)nread ));
473 /****************************************************************************
474 Complete the write and return the data or error back to the client.
475 Returns errno or zero if all ok.
476 *****************************************************************************/
478 static int handle_aio_write_complete(struct aio_extra *aio_ex)
481 files_struct *fsp = aio_ex->fsp;
482 char *outbuf = aio_ex->outbuf;
483 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
484 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
486 if (fsp->aio_write_behind) {
487 if (nwritten != numtowrite) {
488 if (nwritten == -1) {
489 DEBUG(5,("handle_aio_write_complete: "
490 "aio_write_behind failed ! File %s "
491 "is corrupt ! Error %s\n",
492 fsp->fsp_name, strerror(errno) ));
495 DEBUG(0,("handle_aio_write_complete: "
496 "aio_write_behind failed ! File %s "
497 "is corrupt ! Wanted %u bytes but "
498 "only wrote %d\n", fsp->fsp_name,
499 (unsigned int)numtowrite,
504 DEBUG(10,("handle_aio_write_complete: "
505 "aio_write_behind completed for file %s\n",
511 /* We don't need outsize or set_message here as we've already set the
512 fixed size length when we set up the aio call. */
515 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
516 "nwritten == %d. Error = %s\n",
517 fsp->fsp_name, (unsigned int)numtowrite,
518 (int)nwritten, strerror(errno) ));
520 /* If errno is ECANCELED then don't return anything to the
522 if (errno == ECANCELED) {
523 srv_cancel_sign_response(aio_ex->mid);
528 ERROR_BOTH(map_nt_error_from_unix(ret), ERRHRD, ERRdiskfull);
529 srv_set_message(outbuf,0,0,true);
531 bool write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
534 SSVAL(outbuf,smb_vwv2,nwritten);
535 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
536 if (nwritten < (ssize_t)numtowrite) {
537 SCVAL(outbuf,smb_rcls,ERRHRD);
538 SSVAL(outbuf,smb_err,ERRdiskfull);
541 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
542 fsp->fnum, (int)numtowrite, (int)nwritten));
543 status = sync_file(fsp->conn,fsp, write_through);
544 if (!NT_STATUS_IS_OK(status)) {
546 ERROR_BOTH(map_nt_error_from_unix(ret),
547 ERRHRD, ERRdiskfull);
548 srv_set_message(outbuf,0,0,true);
549 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
550 fsp->fsp_name, nt_errstr(status) ));
553 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
557 if (!srv_send_smb(smbd_server_fd(),outbuf,IS_CONN_ENCRYPTED(fsp->conn))) {
558 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
561 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
562 "for file %s, offset %.0f, requested %u, written = %u\n",
563 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
564 (unsigned int)numtowrite, (unsigned int)nwritten ));
569 /****************************************************************************
570 Handle any aio completion. Returns True if finished (and sets *perr if err
571 was non-zero), False if not.
572 *****************************************************************************/
574 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
578 /* Ensure the operation has really completed. */
579 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
580 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
581 "process for file %s\n",
582 aio_ex->mid, aio_ex->fsp->fsp_name ));
586 if (aio_ex->read_req) {
587 err = handle_aio_read_complete(aio_ex);
589 err = handle_aio_write_complete(aio_ex);
593 *perr = err; /* Only save non-zero errors. */
599 /****************************************************************************
600 Handle any aio completion inline.
601 Returns non-zero errno if fail or zero if all ok.
602 *****************************************************************************/
604 int process_aio_queue(void)
609 BlockSignals(True, RT_SIGNAL_AIO);
611 DEBUG(10,("process_aio_queue: signals_received = %d\n",
612 (int)signals_received));
613 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
614 outstanding_aio_calls));
616 if (!signals_received) {
617 BlockSignals(False, RT_SIGNAL_AIO);
621 /* Drain all the complete aio_reads. */
622 for (i = 0; i < signals_received; i++) {
623 uint16 mid = aio_pending_array[i];
624 files_struct *fsp = NULL;
625 struct aio_extra *aio_ex = find_aio_ex(mid);
628 DEBUG(3,("process_aio_queue: Can't find record to "
629 "match mid %u.\n", (unsigned int)mid));
630 srv_cancel_sign_response(mid);
636 /* file was closed whilst I/O was outstanding. Just
638 DEBUG( 3,( "process_aio_queue: file closed whilst "
639 "aio outstanding.\n"));
640 srv_cancel_sign_response(mid);
644 if (!handle_aio_completed(aio_ex, &ret)) {
648 delete_aio_ex(aio_ex);
651 outstanding_aio_calls -= signals_received;
652 signals_received = 0;
653 BlockSignals(False, RT_SIGNAL_AIO);
657 /****************************************************************************
658 We're doing write behind and the client closed the file. Wait up to 30
659 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
660 completed, errno to return if not.
661 *****************************************************************************/
663 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
665 int wait_for_aio_completion(files_struct *fsp)
667 struct aio_extra *aio_ex;
668 const SMB_STRUCT_AIOCB **aiocb_list;
669 int aio_completion_count = 0;
670 time_t start_time = time(NULL);
673 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
674 seconds_left >= 0;) {
679 aio_completion_count = 0;
680 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
681 if (aio_ex->fsp == fsp) {
682 aio_completion_count++;
686 if (!aio_completion_count) {
690 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
691 "to complete.\n", aio_completion_count ));
693 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
694 aio_completion_count);
699 for( i = 0, aio_ex = aio_list_head;
701 aio_ex = aio_ex->next) {
702 if (aio_ex->fsp == fsp) {
703 aiocb_list[i++] = &aio_ex->acb;
707 /* Now wait up to seconds_left for completion. */
708 ts.tv_sec = seconds_left;
711 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
713 aio_completion_count, seconds_left ));
715 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
716 aio_completion_count, &ts);
718 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
719 "errno = %s\n", err, strerror(errno) ));
721 if (err == -1 && errno == EAGAIN) {
722 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
723 "out waiting for %d events after a wait of "
724 "%d seconds\n", aio_completion_count,
727 cancel_aio_by_fsp(fsp);
728 SAFE_FREE(aiocb_list);
732 /* One or more events might have completed - process them if
734 for( i = 0; i < aio_completion_count; i++) {
735 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
737 aio_ex = find_aio_ex(mid);
740 DEBUG(0, ("wait_for_aio_completion: mid %u "
741 "doesn't match an aio record\n",
742 (unsigned int)mid ));
746 if (!handle_aio_completed(aio_ex, &err)) {
749 delete_aio_ex(aio_ex);
752 SAFE_FREE(aiocb_list);
753 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
754 - (time(NULL) - start_time);
757 /* We timed out - we don't know why. Return ret if already an error,
759 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
761 aio_completion_count));
766 /****************************************************************************
767 Cancel any outstanding aio requests. The client doesn't care about the reply.
768 *****************************************************************************/
770 void cancel_aio_by_fsp(files_struct *fsp)
772 struct aio_extra *aio_ex;
774 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
775 if (aio_ex->fsp == fsp) {
776 /* Don't delete the aio_extra record as we may have
777 completed and don't yet know it. Just do the
778 aio_cancel call and return. */
779 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
780 aio_ex->fsp = NULL; /* fsp will be closed when we
787 bool aio_finished(void)
792 void initialize_async_io_handler(void)
796 int process_aio_queue(void)
801 bool schedule_aio_read_and_X(connection_struct *conn,
802 struct smb_request *req,
803 files_struct *fsp, SMB_OFF_T startpos,
809 bool schedule_aio_write_and_X(connection_struct *conn,
810 struct smb_request *req,
811 files_struct *fsp, char *data,
818 void cancel_aio_by_fsp(files_struct *fsp)
822 int wait_for_aio_completion(files_struct *fsp)