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 The buffer we keep around whilst an aio request is in process.
40 *****************************************************************************/
43 struct aio_extra *next, *prev;
46 struct smb_request *smbreq;
48 struct lock_struct lock;
50 int (*handle_completion)(struct aio_extra *ex, int errcode);
53 /****************************************************************************
54 Initialize the signal handler for aio read/write.
55 *****************************************************************************/
57 static void smbd_aio_signal_handler(struct tevent_context *ev_ctx,
58 struct tevent_signal *se,
59 int signum, int count,
60 void *_info, void *private_data)
62 siginfo_t *info = (siginfo_t *)_info;
63 struct aio_extra *aio_ex = (struct aio_extra *)
64 info->si_value.sival_ptr;
66 smbd_aio_complete_aio_ex(aio_ex);
70 static void initialize_async_io_handler(void)
72 if (aio_signal_event) {
76 aio_signal_event = tevent_add_signal(smbd_event_context(),
78 RT_SIGNAL_AIO, SA_SIGINFO,
79 smbd_aio_signal_handler,
81 if (!aio_signal_event) {
82 exit_server("Failed to setup RT_SIGNAL_AIO handler");
85 /* tevent supports 100 signal with SA_SIGINFO */
86 aio_pending_size = 100;
89 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode);
90 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode);
92 static int aio_extra_destructor(struct aio_extra *aio_ex)
94 DLIST_REMOVE(aio_list_head, aio_ex);
98 /****************************************************************************
99 Create the extended aio struct we must keep around for the lifetime
101 *****************************************************************************/
103 static struct aio_extra *create_aio_extra(files_struct *fsp, size_t buflen)
105 struct aio_extra *aio_ex = TALLOC_ZERO_P(NULL, struct aio_extra);
111 /* The output buffer stored in the aio_ex is the start of
112 the smb return buffer. The buffer used in the acb
113 is the start of the reply data portion of that buffer. */
115 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
116 if (!aio_ex->outbuf.data) {
120 DLIST_ADD(aio_list_head, aio_ex);
121 talloc_set_destructor(aio_ex, aio_extra_destructor);
126 /****************************************************************************
127 Set up an aio request from a SMBreadX call.
128 *****************************************************************************/
130 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
131 struct smb_request *smbreq,
132 files_struct *fsp, SMB_OFF_T startpos,
135 struct aio_extra *aio_ex;
138 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
141 /* Ensure aio is initialized. */
142 initialize_async_io_handler();
144 if (fsp->base_fsp != NULL) {
145 /* No AIO on streams yet */
146 DEBUG(10, ("AIO on streams not yet supported\n"));
147 return NT_STATUS_RETRY;
150 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
151 && !SMB_VFS_AIO_FORCE(fsp)) {
152 /* Too small a read for aio request. */
153 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
154 "for minimum aio_read of %u\n",
155 (unsigned int)smb_maxcnt,
156 (unsigned int)min_aio_read_size ));
157 return NT_STATUS_RETRY;
160 /* Only do this on non-chained and non-chaining reads not using the
162 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
163 return NT_STATUS_RETRY;
166 if (outstanding_aio_calls >= aio_pending_size) {
167 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
168 "activities outstanding.\n",
169 outstanding_aio_calls ));
170 return NT_STATUS_RETRY;
173 /* The following is safe from integer wrap as we've already checked
174 smb_maxcnt is 128k or less. Wct is 12 for read replies */
176 bufsize = smb_size + 12 * 2 + smb_maxcnt;
178 if ((aio_ex = create_aio_extra(fsp, bufsize)) == NULL) {
179 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
180 return NT_STATUS_NO_MEMORY;
182 aio_ex->handle_completion = handle_aio_read_complete;
184 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
185 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
186 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
188 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
189 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
192 /* Take the lock until the AIO completes. */
193 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
195 return NT_STATUS_FILE_LOCK_CONFLICT;
200 /* Now set up the aio record for the read call. */
202 a->aio_fildes = fsp->fh->fd;
203 a->aio_buf = smb_buf(aio_ex->outbuf.data);
204 a->aio_nbytes = smb_maxcnt;
205 a->aio_offset = startpos;
206 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
207 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
208 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
210 ret = SMB_VFS_AIO_READ(fsp, a);
212 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
213 "Error %s\n", strerror(errno) ));
214 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
216 return NT_STATUS_RETRY;
219 outstanding_aio_calls++;
220 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
222 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
223 "offset %.0f, len = %u (mid = %u)\n",
224 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
225 (unsigned int)aio_ex->smbreq->mid ));
230 /****************************************************************************
231 Set up an aio request from a SMBwriteX call.
232 *****************************************************************************/
234 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
235 struct smb_request *smbreq,
236 files_struct *fsp, char *data,
240 struct aio_extra *aio_ex;
243 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
246 /* Ensure aio is initialized. */
247 initialize_async_io_handler();
249 if (fsp->base_fsp != NULL) {
250 /* No AIO on streams yet */
251 DEBUG(10, ("AIO on streams not yet supported\n"));
252 return NT_STATUS_RETRY;
255 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
256 && !SMB_VFS_AIO_FORCE(fsp)) {
257 /* Too small a write for aio request. */
258 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
259 "small for minimum aio_write of %u\n",
260 (unsigned int)numtowrite,
261 (unsigned int)min_aio_write_size ));
262 return NT_STATUS_RETRY;
265 /* Only do this on non-chained and non-chaining writes not using the
267 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
268 return NT_STATUS_RETRY;
271 if (outstanding_aio_calls >= aio_pending_size) {
272 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
273 "activities outstanding.\n",
274 outstanding_aio_calls ));
275 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
276 "aio_write for file %s, offset %.0f, len = %u "
278 fsp_str_dbg(fsp), (double)startpos,
279 (unsigned int)numtowrite,
280 (unsigned int)smbreq->mid ));
281 return NT_STATUS_RETRY;
284 bufsize = smb_size + 6*2;
286 if (!(aio_ex = create_aio_extra(fsp, bufsize))) {
287 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
288 return NT_STATUS_NO_MEMORY;
290 aio_ex->handle_completion = handle_aio_write_complete;
291 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
293 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
294 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
295 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
297 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
298 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
301 /* Take the lock until the AIO completes. */
302 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
304 return NT_STATUS_FILE_LOCK_CONFLICT;
309 /* Now set up the aio record for the write call. */
311 a->aio_fildes = fsp->fh->fd;
313 a->aio_nbytes = numtowrite;
314 a->aio_offset = startpos;
315 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
316 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
317 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
319 ret = SMB_VFS_AIO_WRITE(fsp, a);
321 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
322 "Error %s\n", strerror(errno) ));
323 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
325 return NT_STATUS_RETRY;
328 outstanding_aio_calls++;
329 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
331 /* This should actually be improved to span the write. */
332 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
333 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
335 if (!aio_ex->write_through && !lp_syncalways(SNUM(fsp->conn))
336 && fsp->aio_write_behind) {
337 /* Lie to the client and immediately claim we finished the
339 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
340 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
341 show_msg((char *)aio_ex->outbuf.data);
342 if (!srv_send_smb(smbd_server_fd(),(char *)aio_ex->outbuf.data,
343 true, aio_ex->smbreq->seqnum+1,
344 IS_CONN_ENCRYPTED(fsp->conn),
345 &aio_ex->smbreq->pcd)) {
346 exit_server_cleanly("handle_aio_write: srv_send_smb "
349 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
350 "behind for file %s\n", fsp_str_dbg(fsp)));
353 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
354 "%s, offset %.0f, len = %u (mid = %u) "
355 "outstanding_aio_calls = %d\n",
356 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
357 (unsigned int)aio_ex->smbreq->mid, outstanding_aio_calls ));
362 /****************************************************************************
363 Complete the read and return the data or error back to the client.
364 Returns errno or zero if all ok.
365 *****************************************************************************/
367 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
370 char *outbuf = (char *)aio_ex->outbuf.data;
371 char *data = smb_buf(outbuf);
372 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
375 /* We're relying here on the fact that if the fd is
376 closed then the aio will complete and aio_return
377 will return an error. Hopefully this is
380 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
382 fsp_str_dbg(aio_ex->fsp), (int)nread, strerror(errcode)));
384 ERROR_NT(map_nt_error_from_unix(errcode));
385 outsize = srv_set_message(outbuf,0,0,true);
387 outsize = srv_set_message(outbuf,12,nread,False);
388 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
389 SSVAL(outbuf,smb_vwv5,nread);
390 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
391 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
392 SSVAL(smb_buf(outbuf),-2,nread);
394 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
395 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
397 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
399 fsp_str_dbg(aio_ex->fsp),
400 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
403 smb_setlen(outbuf,outsize - 4);
405 if (!srv_send_smb(smbd_server_fd(),outbuf,
406 true, aio_ex->smbreq->seqnum+1,
407 IS_CONN_ENCRYPTED(aio_ex->fsp->conn), NULL)) {
408 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
412 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
413 "for file %s, offset %.0f, len = %u\n",
414 fsp_str_dbg(aio_ex->fsp), (double)aio_ex->acb.aio_offset,
415 (unsigned int)nread ));
420 /****************************************************************************
421 Complete the write and return the data or error back to the client.
422 Returns error code or zero if all ok.
423 *****************************************************************************/
425 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
427 files_struct *fsp = aio_ex->fsp;
428 char *outbuf = (char *)aio_ex->outbuf.data;
429 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
430 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
432 if (fsp->aio_write_behind) {
433 if (nwritten != numtowrite) {
434 if (nwritten == -1) {
435 DEBUG(5,("handle_aio_write_complete: "
436 "aio_write_behind failed ! File %s "
437 "is corrupt ! Error %s\n",
438 fsp_str_dbg(fsp), strerror(errcode)));
440 DEBUG(0,("handle_aio_write_complete: "
441 "aio_write_behind failed ! File %s "
442 "is corrupt ! Wanted %u bytes but "
443 "only wrote %d\n", fsp_str_dbg(fsp),
444 (unsigned int)numtowrite,
449 DEBUG(10,("handle_aio_write_complete: "
450 "aio_write_behind completed for file %s\n",
453 /* TODO: should no return 0 in case of an error !!! */
457 /* We don't need outsize or set_message here as we've already set the
458 fixed size length when we set up the aio call. */
461 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
462 "nwritten == %d. Error = %s\n",
463 fsp_str_dbg(fsp), (unsigned int)numtowrite,
464 (int)nwritten, strerror(errcode) ));
466 ERROR_NT(map_nt_error_from_unix(errcode));
467 srv_set_message(outbuf,0,0,true);
471 SSVAL(outbuf,smb_vwv2,nwritten);
472 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
473 if (nwritten < (ssize_t)numtowrite) {
474 SCVAL(outbuf,smb_rcls,ERRHRD);
475 SSVAL(outbuf,smb_err,ERRdiskfull);
478 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
479 fsp->fnum, (int)numtowrite, (int)nwritten));
480 status = sync_file(fsp->conn,fsp, aio_ex->write_through);
481 if (!NT_STATUS_IS_OK(status)) {
483 ERROR_BOTH(map_nt_error_from_unix(errcode),
484 ERRHRD, ERRdiskfull);
485 srv_set_message(outbuf,0,0,true);
486 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
487 fsp_str_dbg(fsp), nt_errstr(status)));
490 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
494 if (!srv_send_smb(smbd_server_fd(),outbuf,
495 true, aio_ex->smbreq->seqnum+1,
496 IS_CONN_ENCRYPTED(fsp->conn),
498 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
501 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
502 "for file %s, offset %.0f, requested %u, written = %u\n",
503 fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
504 (unsigned int)numtowrite, (unsigned int)nwritten ));
509 /****************************************************************************
510 Handle any aio completion. Returns True if finished (and sets *perr if err
511 was non-zero), False if not.
512 *****************************************************************************/
514 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
516 files_struct *fsp = NULL;
520 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
526 /* Ensure the operation has really completed. */
527 err = SMB_VFS_AIO_ERROR(fsp, &aio_ex->acb);
528 if (err == EINPROGRESS) {
529 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
530 "process for file %s\n",
531 (unsigned long long)aio_ex->smbreq->mid,
532 fsp_str_dbg(aio_ex->fsp)));
536 /* Unlock now we're done. */
537 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
539 if (err == ECANCELED) {
540 /* If error is ECANCELED then don't return anything to the
542 DEBUG(10,( "handle_aio_completed: operation mid %llu"
544 (unsigned long long)aio_ex->smbreq->mid));
548 err = aio_ex->handle_completion(aio_ex, err);
550 *perr = err; /* Only save non-zero errors. */
556 /****************************************************************************
557 Handle any aio completion inline.
558 *****************************************************************************/
560 void smbd_aio_complete_aio_ex(struct aio_extra *aio_ex)
562 files_struct *fsp = NULL;
565 outstanding_aio_calls--;
567 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
568 (unsigned long long)aio_ex->smbreq->mid));
572 /* file was closed whilst I/O was outstanding. Just
574 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
575 "aio outstanding (mid[%llu]).\n",
576 (unsigned long long)aio_ex->smbreq->mid));
580 if (!handle_aio_completed(aio_ex, &ret)) {
587 /****************************************************************************
588 We're doing write behind and the client closed the file. Wait up to 30
589 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
590 completed, errno to return if not.
591 *****************************************************************************/
593 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
595 int wait_for_aio_completion(files_struct *fsp)
597 struct aio_extra *aio_ex;
598 const SMB_STRUCT_AIOCB **aiocb_list;
599 int aio_completion_count = 0;
600 time_t start_time = time(NULL);
603 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
604 seconds_left >= 0;) {
609 aio_completion_count = 0;
610 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
611 if (aio_ex->fsp == fsp) {
612 aio_completion_count++;
616 if (!aio_completion_count) {
620 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
621 "to complete.\n", aio_completion_count ));
623 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
624 aio_completion_count);
629 for( i = 0, aio_ex = aio_list_head;
631 aio_ex = aio_ex->next) {
632 if (aio_ex->fsp == fsp) {
633 aiocb_list[i++] = &aio_ex->acb;
637 /* Now wait up to seconds_left for completion. */
638 ts.tv_sec = seconds_left;
641 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
643 aio_completion_count, seconds_left ));
645 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
646 aio_completion_count, &ts);
648 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
649 "errno = %s\n", err, strerror(errno) ));
651 if (err == -1 && errno == EAGAIN) {
652 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
653 "out waiting for %d events after a wait of "
654 "%d seconds\n", aio_completion_count,
657 cancel_aio_by_fsp(fsp);
658 SAFE_FREE(aiocb_list);
662 /* One or more events might have completed - process them if
664 for( i = 0; i < aio_completion_count; i++) {
665 aio_ex = (struct aio_extra *)aiocb_list[i]->aio_sigevent.sigev_value.sival_ptr;
667 if (!handle_aio_completed(aio_ex, &err)) {
673 SAFE_FREE(aiocb_list);
674 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
675 - (time(NULL) - start_time);
678 /* We timed out - we don't know why. Return ret if already an error,
680 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
682 aio_completion_count));
687 /****************************************************************************
688 Cancel any outstanding aio requests. The client doesn't care about the reply.
689 *****************************************************************************/
691 void cancel_aio_by_fsp(files_struct *fsp)
693 struct aio_extra *aio_ex;
695 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
696 if (aio_ex->fsp == fsp) {
697 /* Unlock now we're done. */
698 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
700 /* Don't delete the aio_extra record as we may have
701 completed and don't yet know it. Just do the
702 aio_cancel call and return. */
703 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
704 aio_ex->fsp = NULL; /* fsp will be closed when we
711 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
712 struct smb_request *smbreq,
713 files_struct *fsp, SMB_OFF_T startpos,
716 return NT_STATUS_RETRY;
719 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
720 struct smb_request *smbreq,
721 files_struct *fsp, char *data,
725 return NT_STATUS_RETRY;
728 void cancel_aio_by_fsp(files_struct *fsp)
732 int wait_for_aio_completion(files_struct *fsp)
737 void smbd_aio_complete_mid(uint64_t mid);