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/>.
22 #include "smbd/globals.h"
26 /* The signal we'll use to signify aio done. */
28 #define RT_SIGNAL_AIO (SIGRTMIN+3)
31 #ifndef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIVAL_PTR
32 #ifdef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIGVAL_PTR
33 #define sival_int sigval_int
34 #define sival_ptr sigval_ptr
38 /****************************************************************************
39 Initialize the signal handler for aio read/write.
40 *****************************************************************************/
42 static void smbd_aio_signal_handler(struct tevent_context *ev_ctx,
43 struct tevent_signal *se,
44 int signum, int count,
45 void *_info, void *private_data)
47 siginfo_t *info = (siginfo_t *)_info;
48 unsigned int mid = (unsigned int)info->si_value.sival_int;
50 smbd_aio_complete_mid(mid);
54 static void initialize_async_io_handler(void)
56 if (aio_signal_event) {
60 aio_signal_event = tevent_add_signal(smbd_event_context(),
62 RT_SIGNAL_AIO, SA_SIGINFO,
63 smbd_aio_signal_handler,
65 if (!aio_signal_event) {
66 exit_server("Failed to setup RT_SIGNAL_AIO handler");
69 /* tevent supports 100 signal with SA_SIGINFO */
70 aio_pending_size = 100;
74 /****************************************************************************
75 The buffer we keep around whilst an aio request is in process.
76 *****************************************************************************/
79 struct aio_extra *next, *prev;
82 struct smb_request *req;
84 int (*handle_completion)(struct aio_extra *ex, int errcode);
87 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode);
88 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode);
90 static int aio_extra_destructor(struct aio_extra *aio_ex)
92 DLIST_REMOVE(aio_list_head, aio_ex);
96 /****************************************************************************
97 Create the extended aio struct we must keep around for the lifetime
99 *****************************************************************************/
101 static struct aio_extra *create_aio_extra(files_struct *fsp, size_t buflen)
103 struct aio_extra *aio_ex = TALLOC_ZERO_P(NULL, struct aio_extra);
109 /* The output buffer stored in the aio_ex is the start of
110 the smb return buffer. The buffer used in the acb
111 is the start of the reply data portion of that buffer. */
113 aio_ex->outbuf = TALLOC_ARRAY(aio_ex, char, buflen);
114 if (!aio_ex->outbuf) {
118 DLIST_ADD(aio_list_head, aio_ex);
119 talloc_set_destructor(aio_ex, aio_extra_destructor);
124 /****************************************************************************
125 Given the mid 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) {
133 if (mid == p->req->mid) {
140 /****************************************************************************
141 We can have these many aio buffers in flight.
142 *****************************************************************************/
144 /****************************************************************************
145 Set up an aio request from a SMBreadX call.
146 *****************************************************************************/
148 bool schedule_aio_read_and_X(connection_struct *conn,
149 struct smb_request *req,
150 files_struct *fsp, SMB_OFF_T startpos,
153 struct aio_extra *aio_ex;
156 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
159 if (fsp->base_fsp != NULL) {
160 /* No AIO on streams yet */
161 DEBUG(10, ("AIO on streams not yet supported\n"));
165 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
166 && !SMB_VFS_AIO_FORCE(fsp)) {
167 /* Too small a read for aio request. */
168 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
169 "for minimum aio_read of %u\n",
170 (unsigned int)smb_maxcnt,
171 (unsigned int)min_aio_read_size ));
175 /* Only do this on non-chained and non-chaining reads not using the
177 if (req_is_in_chain(req) || (lp_write_cache_size(SNUM(conn)) != 0)) {
181 if (outstanding_aio_calls >= aio_pending_size) {
182 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
183 "activities outstanding.\n",
184 outstanding_aio_calls ));
188 /* The following is safe from integer wrap as we've already checked
189 smb_maxcnt is 128k or less. Wct is 12 for read replies */
191 bufsize = smb_size + 12 * 2 + smb_maxcnt;
193 /* Ensure aio is initialized. */
194 initialize_async_io_handler();
196 if ((aio_ex = create_aio_extra(fsp, bufsize)) == NULL) {
197 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
200 aio_ex->handle_completion = handle_aio_read_complete;
202 construct_reply_common_req(req, aio_ex->outbuf);
203 srv_set_message(aio_ex->outbuf, 12, 0, True);
204 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
208 /* Now set up the aio record for the read call. */
210 a->aio_fildes = fsp->fh->fd;
211 a->aio_buf = smb_buf(aio_ex->outbuf);
212 a->aio_nbytes = smb_maxcnt;
213 a->aio_offset = startpos;
214 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
215 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
216 a->aio_sigevent.sigev_value.sival_int = req->mid;
218 ret = SMB_VFS_AIO_READ(fsp, a);
220 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
221 "Error %s\n", strerror(errno) ));
226 outstanding_aio_calls++;
227 aio_ex->req = talloc_move(aio_ex, &req);
229 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
230 "offset %.0f, len = %u (mid = %u)\n",
231 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
232 (unsigned int)aio_ex->req->mid ));
237 /****************************************************************************
238 Set up an aio request from a SMBwriteX call.
239 *****************************************************************************/
241 bool schedule_aio_write_and_X(connection_struct *conn,
242 struct smb_request *req,
243 files_struct *fsp, char *data,
247 struct aio_extra *aio_ex;
250 bool write_through = BITSETW(req->vwv+7,0);
251 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
254 if (fsp->base_fsp != NULL) {
255 /* No AIO on streams yet */
256 DEBUG(10, ("AIO on streams not yet supported\n"));
260 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
261 && !SMB_VFS_AIO_FORCE(fsp)) {
262 /* Too small a write for aio request. */
263 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
264 "small for minimum aio_write of %u\n",
265 (unsigned int)numtowrite,
266 (unsigned int)min_aio_write_size ));
270 /* Only do this on non-chained and non-chaining reads not using the
272 if (req_is_in_chain(req) || (lp_write_cache_size(SNUM(conn)) != 0)) {
276 if (outstanding_aio_calls >= aio_pending_size) {
277 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
278 "activities outstanding.\n",
279 outstanding_aio_calls ));
280 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
281 "aio_write for file %s, offset %.0f, len = %u "
283 fsp_str_dbg(fsp), (double)startpos,
284 (unsigned int)numtowrite,
285 (unsigned int)req->mid ));
289 /* Ensure aio is initialized. */
290 initialize_async_io_handler();
292 bufsize = smb_size + 6*2;
294 if (!(aio_ex = create_aio_extra(fsp, bufsize))) {
295 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
298 aio_ex->handle_completion = handle_aio_write_complete;
300 construct_reply_common_req(req, aio_ex->outbuf);
301 srv_set_message(aio_ex->outbuf, 6, 0, True);
302 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
306 /* Now set up the aio record for the write call. */
308 a->aio_fildes = fsp->fh->fd;
310 a->aio_nbytes = numtowrite;
311 a->aio_offset = startpos;
312 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
313 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
314 a->aio_sigevent.sigev_value.sival_int = req->mid;
316 ret = SMB_VFS_AIO_WRITE(fsp, a);
318 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
319 "Error %s\n", strerror(errno) ));
324 outstanding_aio_calls++;
325 aio_ex->req = talloc_move(aio_ex, &req);
327 /* This should actually be improved to span the write. */
328 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
329 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
331 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
332 && fsp->aio_write_behind) {
333 /* Lie to the client and immediately claim we finished the
335 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
336 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
337 show_msg(aio_ex->outbuf);
338 if (!srv_send_smb(smbd_server_fd(),aio_ex->outbuf,
339 true, aio_ex->req->seqnum+1,
340 IS_CONN_ENCRYPTED(fsp->conn),
341 &aio_ex->req->pcd)) {
342 exit_server_cleanly("handle_aio_write: srv_send_smb "
345 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
346 "behind for file %s\n", fsp_str_dbg(fsp)));
349 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
350 "%s, offset %.0f, len = %u (mid = %u) "
351 "outstanding_aio_calls = %d\n",
352 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
353 (unsigned int)aio_ex->req->mid, outstanding_aio_calls ));
359 /****************************************************************************
360 Complete the read and return the data or error back to the client.
361 Returns errno or zero if all ok.
362 *****************************************************************************/
364 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
367 char *outbuf = aio_ex->outbuf;
368 char *data = smb_buf(outbuf);
369 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
372 /* We're relying here on the fact that if the fd is
373 closed then the aio will complete and aio_return
374 will return an error. Hopefully this is
377 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
379 fsp_str_dbg(aio_ex->fsp), (int)nread, strerror(errcode)));
381 ERROR_NT(map_nt_error_from_unix(errcode));
382 outsize = srv_set_message(outbuf,0,0,true);
384 outsize = srv_set_message(outbuf,12,nread,False);
385 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
386 SSVAL(outbuf,smb_vwv5,nread);
387 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
388 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
389 SSVAL(smb_buf(outbuf),-2,nread);
391 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
392 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
394 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
396 fsp_str_dbg(aio_ex->fsp),
397 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
400 smb_setlen(outbuf,outsize - 4);
402 if (!srv_send_smb(smbd_server_fd(),outbuf,
403 true, aio_ex->req->seqnum+1,
404 IS_CONN_ENCRYPTED(aio_ex->fsp->conn), NULL)) {
405 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
409 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
410 "for file %s, offset %.0f, len = %u\n",
411 fsp_str_dbg(aio_ex->fsp), (double)aio_ex->acb.aio_offset,
412 (unsigned int)nread ));
417 /****************************************************************************
418 Complete the write and return the data or error back to the client.
419 Returns error code or zero if all ok.
420 *****************************************************************************/
422 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
424 files_struct *fsp = aio_ex->fsp;
425 char *outbuf = aio_ex->outbuf;
426 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
427 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
429 if (fsp->aio_write_behind) {
430 if (nwritten != numtowrite) {
431 if (nwritten == -1) {
432 DEBUG(5,("handle_aio_write_complete: "
433 "aio_write_behind failed ! File %s "
434 "is corrupt ! Error %s\n",
435 fsp_str_dbg(fsp), strerror(errcode)));
437 DEBUG(0,("handle_aio_write_complete: "
438 "aio_write_behind failed ! File %s "
439 "is corrupt ! Wanted %u bytes but "
440 "only wrote %d\n", fsp_str_dbg(fsp),
441 (unsigned int)numtowrite,
446 DEBUG(10,("handle_aio_write_complete: "
447 "aio_write_behind completed for file %s\n",
450 /* TODO: should no return 0 in case of an error !!! */
454 /* We don't need outsize or set_message here as we've already set the
455 fixed size length when we set up the aio call. */
458 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
459 "nwritten == %d. Error = %s\n",
460 fsp_str_dbg(fsp), (unsigned int)numtowrite,
461 (int)nwritten, strerror(errcode) ));
463 ERROR_NT(map_nt_error_from_unix(errcode));
464 srv_set_message(outbuf,0,0,true);
466 bool write_through = BITSETW(aio_ex->req->vwv+7,0);
469 SSVAL(outbuf,smb_vwv2,nwritten);
470 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
471 if (nwritten < (ssize_t)numtowrite) {
472 SCVAL(outbuf,smb_rcls,ERRHRD);
473 SSVAL(outbuf,smb_err,ERRdiskfull);
476 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
477 fsp->fnum, (int)numtowrite, (int)nwritten));
478 status = sync_file(fsp->conn,fsp, write_through);
479 if (!NT_STATUS_IS_OK(status)) {
481 ERROR_BOTH(map_nt_error_from_unix(errcode),
482 ERRHRD, ERRdiskfull);
483 srv_set_message(outbuf,0,0,true);
484 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
485 fsp_str_dbg(fsp), nt_errstr(status)));
488 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
492 if (!srv_send_smb(smbd_server_fd(),outbuf,
493 true, aio_ex->req->seqnum+1,
494 IS_CONN_ENCRYPTED(fsp->conn),
496 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
499 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
500 "for file %s, offset %.0f, requested %u, written = %u\n",
501 fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
502 (unsigned int)numtowrite, (unsigned int)nwritten ));
507 /****************************************************************************
508 Handle any aio completion. Returns True if finished (and sets *perr if err
509 was non-zero), False if not.
510 *****************************************************************************/
512 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
517 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
521 /* Ensure the operation has really completed. */
522 err = SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb);
523 if (err == EINPROGRESS) {
524 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
525 "process for file %s\n",
526 aio_ex->req->mid, fsp_str_dbg(aio_ex->fsp)));
528 } else if (err == ECANCELED) {
529 /* If error is ECANCELED then don't return anything to the
531 DEBUG(10,( "handle_aio_completed: operation mid %u"
532 " canceled\n", aio_ex->req->mid));
536 err = aio_ex->handle_completion(aio_ex, err);
538 *perr = err; /* Only save non-zero errors. */
544 /****************************************************************************
545 Handle any aio completion inline.
546 *****************************************************************************/
548 void smbd_aio_complete_mid(unsigned int mid)
550 files_struct *fsp = NULL;
551 struct aio_extra *aio_ex = find_aio_ex(mid);
554 outstanding_aio_calls--;
556 DEBUG(10,("smbd_aio_complete_mid: mid[%u]\n", mid));
559 DEBUG(3,("smbd_aio_complete_mid: Can't find record to "
560 "match mid %u.\n", mid));
566 /* file was closed whilst I/O was outstanding. Just
568 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
569 "aio outstanding (mid[%u]).\n", mid));
573 if (!handle_aio_completed(aio_ex, &ret)) {
580 /****************************************************************************
581 We're doing write behind and the client closed the file. Wait up to 30
582 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
583 completed, errno to return if not.
584 *****************************************************************************/
586 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
588 int wait_for_aio_completion(files_struct *fsp)
590 struct aio_extra *aio_ex;
591 const SMB_STRUCT_AIOCB **aiocb_list;
592 int aio_completion_count = 0;
593 time_t start_time = time(NULL);
596 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
597 seconds_left >= 0;) {
602 aio_completion_count = 0;
603 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
604 if (aio_ex->fsp == fsp) {
605 aio_completion_count++;
609 if (!aio_completion_count) {
613 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
614 "to complete.\n", aio_completion_count ));
616 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
617 aio_completion_count);
622 for( i = 0, aio_ex = aio_list_head;
624 aio_ex = aio_ex->next) {
625 if (aio_ex->fsp == fsp) {
626 aiocb_list[i++] = &aio_ex->acb;
630 /* Now wait up to seconds_left for completion. */
631 ts.tv_sec = seconds_left;
634 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
636 aio_completion_count, seconds_left ));
638 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
639 aio_completion_count, &ts);
641 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
642 "errno = %s\n", err, strerror(errno) ));
644 if (err == -1 && errno == EAGAIN) {
645 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
646 "out waiting for %d events after a wait of "
647 "%d seconds\n", aio_completion_count,
650 cancel_aio_by_fsp(fsp);
651 SAFE_FREE(aiocb_list);
655 /* One or more events might have completed - process them if
657 for( i = 0; i < aio_completion_count; i++) {
658 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
660 aio_ex = find_aio_ex(mid);
663 DEBUG(0, ("wait_for_aio_completion: mid %u "
664 "doesn't match an aio record\n",
665 (unsigned int)mid ));
669 if (!handle_aio_completed(aio_ex, &err)) {
675 SAFE_FREE(aiocb_list);
676 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
677 - (time(NULL) - start_time);
680 /* We timed out - we don't know why. Return ret if already an error,
682 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
684 aio_completion_count));
689 /****************************************************************************
690 Cancel any outstanding aio requests. The client doesn't care about the reply.
691 *****************************************************************************/
693 void cancel_aio_by_fsp(files_struct *fsp)
695 struct aio_extra *aio_ex;
697 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
698 if (aio_ex->fsp == fsp) {
699 /* Don't delete the aio_extra record as we may have
700 completed and don't yet know it. Just do the
701 aio_cancel call and return. */
702 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
703 aio_ex->fsp = NULL; /* fsp will be closed when we
710 bool schedule_aio_read_and_X(connection_struct *conn,
711 struct smb_request *req,
712 files_struct *fsp, SMB_OFF_T startpos,
718 bool schedule_aio_write_and_X(connection_struct *conn,
719 struct smb_request *req,
720 files_struct *fsp, char *data,
727 void cancel_aio_by_fsp(files_struct *fsp)
731 int wait_for_aio_completion(files_struct *fsp)
736 void smbd_aio_complete_mid(unsigned int mid);