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 #define AIO_PENDING_SIZE 10
145 static sig_atomic_t signals_received;
146 static int outstanding_aio_calls;
147 static uint16 aio_pending_array[AIO_PENDING_SIZE];
149 /****************************************************************************
150 Signal handler when an aio request completes.
151 *****************************************************************************/
153 static void signal_handler(int sig, siginfo_t *info, void *unused)
155 if (signals_received < AIO_PENDING_SIZE) {
156 aio_pending_array[signals_received] = info->si_value.sival_int;
158 } /* Else signal is lost. */
159 sys_select_signal(RT_SIGNAL_AIO);
162 /****************************************************************************
163 Is there a signal waiting ?
164 *****************************************************************************/
166 bool aio_finished(void)
168 return (signals_received != 0);
171 /****************************************************************************
172 Initialize the signal handler for aio read/write.
173 *****************************************************************************/
175 void initialize_async_io_handler(void)
177 struct sigaction act;
180 act.sa_sigaction = signal_handler;
181 act.sa_flags = SA_SIGINFO;
182 sigemptyset( &act.sa_mask );
183 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
184 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
187 /* the signal can start off blocked due to a bug in bash */
188 BlockSignals(False, RT_SIGNAL_AIO);
191 /****************************************************************************
192 Set up an aio request from a SMBreadX call.
193 *****************************************************************************/
195 bool schedule_aio_read_and_X(connection_struct *conn,
196 struct smb_request *req,
197 files_struct *fsp, SMB_OFF_T startpos,
200 struct aio_extra *aio_ex;
203 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
205 if (!min_aio_read_size || (smb_maxcnt < min_aio_read_size)) {
206 /* Too small a read for aio request. */
207 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
208 "for minimum aio_read of %u\n",
209 (unsigned int)smb_maxcnt,
210 (unsigned int)min_aio_read_size ));
214 /* Only do this on non-chained and non-chaining reads not using the
216 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
217 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
221 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
222 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
223 "activities outstanding.\n",
224 outstanding_aio_calls ));
228 /* The following is safe from integer wrap as we've already checked
229 smb_maxcnt is 128k or less. Wct is 12 for read replies */
231 bufsize = smb_size + 12 * 2 + smb_maxcnt;
233 if ((aio_ex = create_aio_ex_read(fsp, bufsize, req->mid)) == NULL) {
234 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
238 construct_reply_common((char *)req->inbuf, aio_ex->outbuf);
239 set_message(aio_ex->outbuf, 12, 0, True);
240 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
244 /* Now set up the aio record for the read call. */
246 a->aio_fildes = fsp->fh->fd;
247 a->aio_buf = smb_buf(aio_ex->outbuf);
248 a->aio_nbytes = smb_maxcnt;
249 a->aio_offset = startpos;
250 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
251 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
252 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
254 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
255 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
256 "Error %s\n", strerror(errno) ));
257 delete_aio_ex(aio_ex);
261 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
262 "offset %.0f, len = %u (mid = %u)\n",
263 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
264 (unsigned int)aio_ex->mid ));
266 srv_defer_sign_response(aio_ex->mid);
267 outstanding_aio_calls++;
271 /****************************************************************************
272 Set up an aio request from a SMBwriteX call.
273 *****************************************************************************/
275 bool schedule_aio_write_and_X(connection_struct *conn,
276 struct smb_request *req,
277 files_struct *fsp, char *data,
281 struct aio_extra *aio_ex;
283 size_t inbufsize, outbufsize;
284 bool write_through = BITSETW(req->inbuf+smb_vwv7,0);
285 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
287 if (!min_aio_write_size || (numtowrite < min_aio_write_size)) {
288 /* Too small a write for aio request. */
289 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
290 "small for minimum aio_write of %u\n",
291 (unsigned int)numtowrite,
292 (unsigned int)min_aio_write_size ));
296 /* Only do this on non-chained and non-chaining reads not using the
298 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
299 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
303 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
304 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
305 "activities outstanding.\n",
306 outstanding_aio_calls ));
307 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
308 "aio_write for file %s, offset %.0f, len = %u "
310 fsp->fsp_name, (double)startpos,
311 (unsigned int)numtowrite,
312 (unsigned int)req->mid ));
316 inbufsize = smb_len(req->inbuf) + 4;
317 reply_outbuf(req, 6, 0);
318 outbufsize = smb_len(req->outbuf) + 4;
319 if (!(aio_ex = create_aio_ex_write(fsp, inbufsize, outbufsize,
321 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
325 /* Copy the SMB header already setup in outbuf. */
326 memcpy(aio_ex->inbuf, req->inbuf, inbufsize);
328 /* Copy the SMB header already setup in outbuf. */
329 memcpy(aio_ex->outbuf, req->outbuf, outbufsize);
330 TALLOC_FREE(req->outbuf);
331 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
335 /* Now set up the aio record for the write call. */
337 a->aio_fildes = fsp->fh->fd;
338 a->aio_buf = aio_ex->inbuf + (PTR_DIFF(data, req->inbuf));
339 a->aio_nbytes = numtowrite;
340 a->aio_offset = startpos;
341 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
342 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
343 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
345 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
346 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
347 "Error %s\n", strerror(errno) ));
348 delete_aio_ex(aio_ex);
352 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
353 && fsp->aio_write_behind) {
354 /* Lie to the client and immediately claim we finished the
356 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
357 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
358 show_msg(aio_ex->outbuf);
359 if (!send_smb(smbd_server_fd(),aio_ex->outbuf)) {
360 exit_server_cleanly("handle_aio_write: send_smb "
363 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
364 "behind for file %s\n", fsp->fsp_name ));
366 srv_defer_sign_response(aio_ex->mid);
368 outstanding_aio_calls++;
370 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
371 "%s, offset %.0f, len = %u (mid = %u) "
372 "outstanding_aio_calls = %d\n",
373 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
374 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
380 /****************************************************************************
381 Complete the read and return the data or error back to the client.
382 Returns errno or zero if all ok.
383 *****************************************************************************/
385 static int handle_aio_read_complete(struct aio_extra *aio_ex)
389 char *outbuf = aio_ex->outbuf;
390 char *data = smb_buf(outbuf);
391 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
394 /* We're relying here on the fact that if the fd is
395 closed then the aio will complete and aio_return
396 will return an error. Hopefully this is
399 /* If errno is ECANCELED then don't return anything to the
401 if (errno == ECANCELED) {
402 srv_cancel_sign_response(aio_ex->mid);
406 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
408 aio_ex->fsp->fsp_name, strerror(errno) ));
410 outsize = (UNIXERROR(ERRDOS,ERRnoaccess));
413 outsize = set_message(outbuf,12,nread,False);
414 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
415 SSVAL(outbuf,smb_vwv5,nread);
416 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
417 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
418 SSVAL(smb_buf(outbuf),-2,nread);
420 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
422 aio_ex->fsp->fsp_name,
423 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
426 smb_setlen(outbuf,outsize - 4);
428 if (!send_smb(smbd_server_fd(),outbuf)) {
429 exit_server_cleanly("handle_aio_read_complete: send_smb "
433 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
434 "for file %s, offset %.0f, len = %u\n",
435 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
436 (unsigned int)nread ));
441 /****************************************************************************
442 Complete the write and return the data or error back to the client.
443 Returns errno or zero if all ok.
444 *****************************************************************************/
446 static int handle_aio_write_complete(struct aio_extra *aio_ex)
449 files_struct *fsp = aio_ex->fsp;
450 char *outbuf = aio_ex->outbuf;
451 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
452 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
454 if (fsp->aio_write_behind) {
455 if (nwritten != numtowrite) {
456 if (nwritten == -1) {
457 DEBUG(5,("handle_aio_write_complete: "
458 "aio_write_behind failed ! File %s "
459 "is corrupt ! Error %s\n",
460 fsp->fsp_name, strerror(errno) ));
463 DEBUG(0,("handle_aio_write_complete: "
464 "aio_write_behind failed ! File %s "
465 "is corrupt ! Wanted %u bytes but "
466 "only wrote %d\n", fsp->fsp_name,
467 (unsigned int)numtowrite,
472 DEBUG(10,("handle_aio_write_complete: "
473 "aio_write_behind completed for file %s\n",
479 /* We don't need outsize or set_message here as we've already set the
480 fixed size length when we set up the aio call. */
483 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
484 "nwritten == %d. Error = %s\n",
485 fsp->fsp_name, (unsigned int)numtowrite,
486 (int)nwritten, strerror(errno) ));
488 /* If errno is ECANCELED then don't return anything to the
490 if (errno == ECANCELED) {
491 srv_cancel_sign_response(aio_ex->mid);
495 UNIXERROR(ERRHRD,ERRdiskfull);
498 bool write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
501 SSVAL(outbuf,smb_vwv2,nwritten);
502 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
503 if (nwritten < (ssize_t)numtowrite) {
504 SCVAL(outbuf,smb_rcls,ERRHRD);
505 SSVAL(outbuf,smb_err,ERRdiskfull);
508 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
509 fsp->fnum, (int)numtowrite, (int)nwritten));
510 status = sync_file(fsp->conn,fsp, write_through);
511 if (!NT_STATUS_IS_OK(status)) {
512 UNIXERROR(ERRHRD,ERRdiskfull);
514 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
515 fsp->fsp_name, nt_errstr(status) ));
520 if (!send_smb(smbd_server_fd(),outbuf)) {
521 exit_server_cleanly("handle_aio_write: send_smb failed.");
524 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
525 "for file %s, offset %.0f, requested %u, written = %u\n",
526 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
527 (unsigned int)numtowrite, (unsigned int)nwritten ));
532 /****************************************************************************
533 Handle any aio completion. Returns True if finished (and sets *perr if err
534 was non-zero), False if not.
535 *****************************************************************************/
537 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
541 /* Ensure the operation has really completed. */
542 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
543 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
544 "process for file %s\n",
545 aio_ex->mid, aio_ex->fsp->fsp_name ));
549 if (aio_ex->read_req) {
550 err = handle_aio_read_complete(aio_ex);
552 err = handle_aio_write_complete(aio_ex);
556 *perr = err; /* Only save non-zero errors. */
562 /****************************************************************************
563 Handle any aio completion inline.
564 Returns non-zero errno if fail or zero if all ok.
565 *****************************************************************************/
567 int process_aio_queue(void)
572 BlockSignals(True, RT_SIGNAL_AIO);
574 DEBUG(10,("process_aio_queue: signals_received = %d\n",
575 (int)signals_received));
576 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
577 outstanding_aio_calls));
579 if (!signals_received) {
580 BlockSignals(False, RT_SIGNAL_AIO);
584 /* Drain all the complete aio_reads. */
585 for (i = 0; i < signals_received; i++) {
586 uint16 mid = aio_pending_array[i];
587 files_struct *fsp = NULL;
588 struct aio_extra *aio_ex = find_aio_ex(mid);
591 DEBUG(3,("process_aio_queue: Can't find record to "
592 "match mid %u.\n", (unsigned int)mid));
593 srv_cancel_sign_response(mid);
599 /* file was closed whilst I/O was outstanding. Just
601 DEBUG( 3,( "process_aio_queue: file closed whilst "
602 "aio outstanding.\n"));
603 srv_cancel_sign_response(mid);
607 if (!handle_aio_completed(aio_ex, &ret)) {
611 delete_aio_ex(aio_ex);
614 outstanding_aio_calls -= signals_received;
615 signals_received = 0;
616 BlockSignals(False, RT_SIGNAL_AIO);
620 /****************************************************************************
621 We're doing write behind and the client closed the file. Wait up to 30
622 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
623 completed, errno to return if not.
624 *****************************************************************************/
626 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
628 int wait_for_aio_completion(files_struct *fsp)
630 struct aio_extra *aio_ex;
631 const SMB_STRUCT_AIOCB **aiocb_list;
632 int aio_completion_count = 0;
633 time_t start_time = time(NULL);
636 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
637 seconds_left >= 0;) {
642 aio_completion_count = 0;
643 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
644 if (aio_ex->fsp == fsp) {
645 aio_completion_count++;
649 if (!aio_completion_count) {
653 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
654 "to complete.\n", aio_completion_count ));
656 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
657 aio_completion_count);
662 for( i = 0, aio_ex = aio_list_head;
664 aio_ex = aio_ex->next) {
665 if (aio_ex->fsp == fsp) {
666 aiocb_list[i++] = &aio_ex->acb;
670 /* Now wait up to seconds_left for completion. */
671 ts.tv_sec = seconds_left;
674 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
676 aio_completion_count, seconds_left ));
678 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
679 aio_completion_count, &ts);
681 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
682 "errno = %s\n", err, strerror(errno) ));
684 if (err == -1 && errno == EAGAIN) {
685 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
686 "out waiting for %d events after a wait of "
687 "%d seconds\n", aio_completion_count,
690 cancel_aio_by_fsp(fsp);
691 SAFE_FREE(aiocb_list);
695 /* One or more events might have completed - process them if
697 for( i = 0; i < aio_completion_count; i++) {
698 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
700 aio_ex = find_aio_ex(mid);
703 DEBUG(0, ("wait_for_aio_completion: mid %u "
704 "doesn't match an aio record\n",
705 (unsigned int)mid ));
709 if (!handle_aio_completed(aio_ex, &err)) {
712 delete_aio_ex(aio_ex);
715 SAFE_FREE(aiocb_list);
716 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
717 - (time(NULL) - start_time);
720 /* We timed out - we don't know why. Return ret if already an error,
722 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
724 aio_completion_count));
729 /****************************************************************************
730 Cancel any outstanding aio requests. The client doesn't care about the reply.
731 *****************************************************************************/
733 void cancel_aio_by_fsp(files_struct *fsp)
735 struct aio_extra *aio_ex;
737 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
738 if (aio_ex->fsp == fsp) {
739 /* Don't delete the aio_extra record as we may have
740 completed and don't yet know it. Just do the
741 aio_cancel call and return. */
742 SMB_VFS_AIO_CANCEL(fsp,fsp->fh->fd, &aio_ex->acb);
743 aio_ex->fsp = NULL; /* fsp will be closed when we
750 bool aio_finished(void)
755 void initialize_async_io_handler(void)
759 int process_aio_queue(void)
764 bool schedule_aio_read_and_X(connection_struct *conn,
765 struct smb_request *req,
766 files_struct *fsp, SMB_OFF_T startpos,
772 bool schedule_aio_write_and_X(connection_struct *conn,
773 struct smb_request *req,
774 files_struct *fsp, char *data,
781 void cancel_aio_by_fsp(files_struct *fsp)
785 int wait_for_aio_completion(files_struct *fsp)