From f2f55d703d0dd549a83809d3e5cc5151569b48d6 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 27 Jun 2005 22:53:56 +0000 Subject: r7963: Add aio support to 3.0. Jeremy. (This used to be commit 1de27da47051af08790317f5b48b02719d6b9934) --- source3/smbd/aio.c | 748 +++++++++++++++++++++++++++++++++++++++++++++++ source3/smbd/blocking.c | 10 +- source3/smbd/close.c | 14 + source3/smbd/conn.c | 1 + source3/smbd/notify.c | 1 + source3/smbd/open.c | 4 + source3/smbd/oplock.c | 26 +- source3/smbd/process.c | 111 ++++++- source3/smbd/reply.c | 7 - source3/smbd/server.c | 5 +- source3/smbd/service.c | 1 + source3/smbd/sesssetup.c | 1 + source3/smbd/trans2.c | 3 + source3/smbd/vfs-wrap.c | 35 +++ source3/smbd/vfs.c | 39 ++- 15 files changed, 958 insertions(+), 48 deletions(-) create mode 100644 source3/smbd/aio.c (limited to 'source3/smbd') diff --git a/source3/smbd/aio.c b/source3/smbd/aio.c new file mode 100644 index 0000000000..7910ee5a7a --- /dev/null +++ b/source3/smbd/aio.c @@ -0,0 +1,748 @@ +/* + Unix SMB/Netbios implementation. + Version 3.0 + async_io read handling using POSIX async io. + Copyright (C) Jeremy Allison 2005. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +#if defined(WITH_AIO) + +/* The signal we'll use to signify aio done. */ +#ifndef RT_SIGNAL_AIO +#define RT_SIGNAL_AIO (SIGRTMIN+3) +#endif + +/**************************************************************************** + The buffer we keep around whilst an aio request is in process. +*****************************************************************************/ + +struct aio_extra { + struct aio_extra *next, *prev; + SMB_STRUCT_AIOCB acb; + files_struct *fsp; + BOOL read_req; + uint16 mid; + char *inbuf; + char *outbuf; +}; + +static struct aio_extra *aio_list_head; + +/**************************************************************************** + Create the extended aio struct we must keep around for the lifetime + of the aio_read call. +*****************************************************************************/ + +static struct aio_extra *create_aio_ex_read(files_struct *fsp, size_t buflen, uint16 mid) +{ + struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra); + + if (!aio_ex) { + return NULL; + } + ZERO_STRUCTP(aio_ex); + /* The output buffer stored in the aio_ex is the start of + the smb return buffer. The buffer used in the acb + is the start of the reply data portion of that buffer. */ + aio_ex->outbuf = SMB_MALLOC_ARRAY(char, buflen); + if (!aio_ex->outbuf) { + SAFE_FREE(aio_ex); + return NULL; + } + DLIST_ADD(aio_list_head, aio_ex); + aio_ex->fsp = fsp; + aio_ex->read_req = True; + aio_ex->mid = mid; + return aio_ex; +} + +/**************************************************************************** + Create the extended aio struct we must keep around for the lifetime + of the aio_write call. +*****************************************************************************/ + +static struct aio_extra *create_aio_ex_write(files_struct *fsp, size_t outbuflen, uint16 mid) +{ + struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra); + + if (!aio_ex) { + return NULL; + } + ZERO_STRUCTP(aio_ex); + + /* We need space for an output reply of outbuflen bytes. */ + aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen); + if (!aio_ex->outbuf) { + SAFE_FREE(aio_ex); + return NULL; + } + /* Steal the input buffer containing the write data from the main SMB call. */ + /* We must re-allocate a new one here. */ + if (NewInBuffer(&aio_ex->inbuf) == NULL) { + SAFE_FREE(aio_ex->outbuf); + SAFE_FREE(aio_ex); + return NULL; + } + + /* aio_ex->inbuf now contains the stolen old InBuf containing the data to write. */ + + DLIST_ADD(aio_list_head, aio_ex); + aio_ex->fsp = fsp; + aio_ex->read_req = False; + aio_ex->mid = mid; + return aio_ex; +} + +/**************************************************************************** + Delete the extended aio struct. +*****************************************************************************/ + +static void delete_aio_ex(struct aio_extra *aio_ex) +{ + DLIST_REMOVE(aio_list_head, aio_ex); + /* Safe to do as we've removed ourselves from the in use list first. */ + free_InBuffer(aio_ex->inbuf); + + SAFE_FREE(aio_ex->outbuf); + SAFE_FREE(aio_ex); +} + +/**************************************************************************** + Given the aiocb struct find the extended aio struct containing it. +*****************************************************************************/ + +static struct aio_extra *find_aio_ex(uint16 mid) +{ + struct aio_extra *p; + + for( p = aio_list_head; p; p = p->next) { + if (mid == p->mid) { + return p; + } + } + return NULL; +} + +/**************************************************************************** + We can have these many aio buffers in flight. +*****************************************************************************/ + +#define AIO_PENDING_SIZE 10 +static sig_atomic_t signals_received; +static int outstanding_aio_calls; +static uint16 aio_pending_array[AIO_PENDING_SIZE]; + +/**************************************************************************** + Signal handler when an aio request completes. +*****************************************************************************/ + +static void signal_handler(int sig, siginfo_t *info, void *unused) +{ + if (signals_received < AIO_PENDING_SIZE - 1) { + aio_pending_array[signals_received] = *(uint16 *)(info->si_value.sival_ptr); + signals_received++; + } /* Else signal is lost. */ + sys_select_signal(RT_SIGNAL_AIO); +} + +/**************************************************************************** + Is there a signal waiting ? +*****************************************************************************/ + +BOOL aio_finished(void) +{ + return (signals_received != 0); +} + +/**************************************************************************** + Initialize the signal handler for aio read/write. +*****************************************************************************/ + +void initialize_async_io_handler(void) +{ + struct sigaction act; + + ZERO_STRUCT(act); + act.sa_sigaction = signal_handler; + act.sa_flags = SA_SIGINFO; + sigemptyset( &act.sa_mask ); + if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) { + DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n")); + } + + /* the signal can start off blocked due to a bug in bash */ + BlockSignals(False, RT_SIGNAL_AIO); +} + +/**************************************************************************** + Set up an aio request from a SMBreadX call. +*****************************************************************************/ + +BOOL schedule_aio_read_and_X(connection_struct *conn, + char *inbuf, char *outbuf, + int length, int len_outbuf, + files_struct *fsp, SMB_OFF_T startpos, + size_t smb_maxcnt) +{ + struct aio_extra *aio_ex; + SMB_STRUCT_AIOCB *a; + size_t bufsize; + size_t min_aio_read_size = lp_aio_read_size(SNUM(conn)); + + if (!min_aio_read_size || (smb_maxcnt < min_aio_read_size)) { + /* Too small a read for aio request. */ + DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small " + "for minimum aio_read of %u\n", + (unsigned int)smb_maxcnt, + (unsigned int)min_aio_read_size )); + return False; + } + + /* Only do this on non-chained and non-chaining reads not using the write cache. */ + if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF) || (lp_write_cache_size(SNUM(conn)) != 0) ) { + return False; + } + + if (outstanding_aio_calls >= AIO_PENDING_SIZE) { + DEBUG(10,("schedule_aio_read_and_X: Already have %d aio activities outstanding.\n", + outstanding_aio_calls )); + return False; + } + + /* The following is safe from integer wrap as we've already + checked smb_maxcnt is 128k or less. */ + bufsize = PTR_DIFF(smb_buf(outbuf),outbuf) + smb_maxcnt; + + if ((aio_ex = create_aio_ex_read(fsp, bufsize, SVAL(inbuf,smb_mid))) == NULL) { + DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n")); + return False; + } + + /* Copy the SMB header already setup in outbuf. */ + memcpy(aio_ex->outbuf, outbuf, smb_buf(outbuf) - outbuf); + SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */ + + a = &aio_ex->acb; + + /* Now set up the aio record for the read call. */ + + a->aio_fildes = fsp->fd; + a->aio_buf = smb_buf(aio_ex->outbuf); + a->aio_nbytes = smb_maxcnt; + a->aio_offset = startpos; + a->aio_sigevent.sigev_notify = SIGEV_SIGNAL; + a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO; + a->aio_sigevent.sigev_value.sival_ptr = (void *)&aio_ex->mid; + + if (SMB_VFS_AIO_READ(fsp,a) == -1) { + DEBUG(0,("schedule_aio_read_and_X: aio_read failed. Error %s\n", + strerror(errno) )); + delete_aio_ex(aio_ex); + return False; + } + + DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, offset %.0f, len = %u (mid = %u)\n", + fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt, (unsigned int)aio_ex->mid )); + + srv_defer_sign_response(aio_ex->mid); + outstanding_aio_calls++; + return True; +} + +/**************************************************************************** + Set up an aio request from a SMBwriteX call. +*****************************************************************************/ + +BOOL schedule_aio_write_and_X(connection_struct *conn, + char *inbuf, char *outbuf, + int length, int len_outbuf, + files_struct *fsp, char *data, + SMB_OFF_T startpos, + size_t numtowrite) +{ + struct aio_extra *aio_ex; + SMB_STRUCT_AIOCB *a; + size_t outbufsize; + BOOL write_through = BITSETW(inbuf+smb_vwv7,0); + size_t min_aio_write_size = lp_aio_write_size(SNUM(conn)); + + if (!min_aio_write_size || (numtowrite < min_aio_write_size)) { + /* Too small a write for aio request. */ + DEBUG(10,("schedule_aio_write_and_X: write size (%u) too small " + "for minimum aio_write of %u\n", + (unsigned int)numtowrite, + (unsigned int)min_aio_write_size )); + return False; + } + + /* Only do this on non-chained and non-chaining reads not using the write cache. */ + if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF) || (lp_write_cache_size(SNUM(conn)) != 0) ) { + return False; + } + + if (outstanding_aio_calls >= AIO_PENDING_SIZE) { + DEBUG(3,("schedule_aio_write_and_X: Already have %d aio activities outstanding.\n", + outstanding_aio_calls )); + DEBUG(10,("schedule_aio_write_and_X: failed to schedule aio_write for file %s, offset %.0f, len = %u (mid = %u)\n", + fsp->fsp_name, (double)startpos, (unsigned int)numtowrite, (unsigned int)SVAL(inbuf,smb_mid) )); + return False; + } + + outbufsize = smb_len(outbuf) + 4; + if ((aio_ex = create_aio_ex_write(fsp, outbufsize, SVAL(inbuf,smb_mid))) == NULL) { + DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n")); + return False; + } + + /* Paranioa.... */ + SMB_ASSERT(aio_ex->inbuf == inbuf); + + /* Copy the SMB header already setup in outbuf. */ + memcpy(aio_ex->outbuf, outbuf, outbufsize); + SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */ + + a = &aio_ex->acb; + + /* Now set up the aio record for the write call. */ + + a->aio_fildes = fsp->fd; + a->aio_buf = data; /* As we've stolen inbuf this points within inbuf. */ + a->aio_nbytes = numtowrite; + a->aio_offset = startpos; + a->aio_sigevent.sigev_notify = SIGEV_SIGNAL; + a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO; + a->aio_sigevent.sigev_value.sival_ptr = (void *)&aio_ex->mid; + + if (SMB_VFS_AIO_WRITE(fsp,a) == -1) { + DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. Error %s\n", + strerror(errno) )); + /* Replace global InBuf as we're going to do a normal write. */ + set_InBuffer(aio_ex->inbuf); + aio_ex->inbuf = NULL; + delete_aio_ex(aio_ex); + return False; + } + + if (!write_through && !lp_syncalways(SNUM(fsp->conn)) && fsp->aio_write_behind) { + /* Lie to the client and immediately claim we finished the write. */ + SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite); + SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1); + show_msg(aio_ex->outbuf); + if (!send_smb(smbd_server_fd(),aio_ex->outbuf)) { + exit_server("handle_aio_write: send_smb failed."); + } + DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write behind for file %s\n", + fsp->fsp_name )); + } else { + srv_defer_sign_response(aio_ex->mid); + } + outstanding_aio_calls++; + + DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file %s, \ +offset %.0f, len = %u (mid = %u) outstanding_aio_calls = %d\n", + fsp->fsp_name, (double)startpos, (unsigned int)numtowrite, (unsigned int)aio_ex->mid, outstanding_aio_calls )); + + return True; +} + + +/**************************************************************************** + Complete the read and return the data or error back to the client. + Returns errno or zero if all ok. +*****************************************************************************/ + +static int handle_aio_read_complete(struct aio_extra *aio_ex) +{ + int ret = 0; + int outsize; + char *outbuf = aio_ex->outbuf; + char *data = smb_buf(outbuf); + ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb); + + if (nread < 0) { + /* We're relying here on the fact that if the fd is + closed then the aio will complete and aio_return + will return an error. Hopefully this is + true.... JRA. */ + + /* If errno is ECANCELED then don't return anything to the client. */ + if (errno == ECANCELED) { + srv_cancel_sign_response(aio_ex->mid); + return 0; + } + + DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. Error = %s\n", + aio_ex->fsp->fsp_name, strerror(errno) )); + + outsize = (UNIXERROR(ERRDOS,ERRnoaccess)); + ret = errno; + } else { + outsize = set_message(outbuf,12,nread,False); + SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */ + SSVAL(outbuf,smb_vwv5,nread); + SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf)); + SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1)); + SSVAL(smb_buf(outbuf),-2,nread); + + DEBUG( 3, ( "handle_aio_read_complete file %s max=%d nread=%d\n", + aio_ex->fsp->fsp_name, + aio_ex->acb.aio_nbytes, (int)nread ) ); + + } + smb_setlen(outbuf,outsize - 4); + show_msg(outbuf); + if (!send_smb(smbd_server_fd(),outbuf)) { + exit_server("handle_aio_read_complete: send_smb failed."); + } + + DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed for file %s, offset %.0f, len = %u\n", + aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset, (unsigned int)nread )); + + return ret; +} + +/**************************************************************************** + Complete the write and return the data or error back to the client. + Returns errno or zero if all ok. +*****************************************************************************/ + +static int handle_aio_write_complete(struct aio_extra *aio_ex) +{ + int ret = 0; + files_struct *fsp = aio_ex->fsp; + char *outbuf = aio_ex->outbuf; + ssize_t numtowrite = aio_ex->acb.aio_nbytes; + ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb); + + if (fsp->aio_write_behind) { + if (nwritten != numtowrite) { + if (nwritten == -1) { + DEBUG(5,("handle_aio_write_complete: aio_write_behind failed ! File %s is corrupt ! Error %s\n", + fsp->fsp_name, strerror(errno) )); + ret = errno; + } else { + DEBUG(0,("handle_aio_write_complete: aio_write_behind failed ! File %s is corrupt ! \ +Wanted %u bytes but only wrote %d\n", fsp->fsp_name, (unsigned int)numtowrite, (int)nwritten )); + ret = EIO; + } + } else { + DEBUG(10,("handle_aio_write_complete: aio_write_behind completed for file %s\n", + fsp->fsp_name )); + } + return 0; + } + + /* We don't need outsize or set_message here as we've already set the + fixed size length when we set up the aio call. */ + + if(nwritten == -1) { + DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. nwritten == %d. Error = %s\n", + fsp->fsp_name, (unsigned int)numtowrite, + (int)nwritten, strerror(errno) )); + + /* If errno is ECANCELED then don't return anything to the client. */ + if (errno == ECANCELED) { + srv_cancel_sign_response(aio_ex->mid); + return 0; + } + + UNIXERROR(ERRHRD,ERRdiskfull); + ret = errno; + } else { + BOOL write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0); + + SSVAL(outbuf,smb_vwv2,nwritten); + SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1); + if (nwritten < (ssize_t)numtowrite) { + SCVAL(outbuf,smb_rcls,ERRHRD); + SSVAL(outbuf,smb_err,ERRdiskfull); + } + + DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten)); + if (lp_syncalways(SNUM(fsp->conn)) || write_through) { + sync_file(fsp->conn,fsp); + } + } + + show_msg(outbuf); + if (!send_smb(smbd_server_fd(),outbuf)) { + exit_server("handle_aio_write: send_smb failed."); + } + + DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed for file %s, offset %.0f, requested %u, written = %u\n", + fsp->fsp_name, (double)aio_ex->acb.aio_offset, (unsigned int)numtowrite, (unsigned int)nwritten )); + + return ret; +} + +/**************************************************************************** + Handle any aio completion. Returns True if finished (and sets *perr if err was non-zero), + False if not. +*****************************************************************************/ + +static BOOL handle_aio_completed(struct aio_extra *aio_ex, int *perr) +{ + int err; + + /* Ensure the operation has really completed. */ + if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) { + DEBUG(10,( "handle_aio_completed: operation mid %u still in process for file %s\n", + aio_ex->mid, aio_ex->fsp->fsp_name )); + return False; + } + + if (aio_ex->read_req) { + err = handle_aio_read_complete(aio_ex); + } else { + err = handle_aio_write_complete(aio_ex); + } + + if (err) { + *perr = err; /* Only save non-zero errors. */ + } + + return True; +} + +/**************************************************************************** + Handle any aio completion inline. + Returns non-zero errno if fail or zero if all ok. +*****************************************************************************/ + +int process_aio_queue(void) +{ + int i; + int ret = 0; + + BlockSignals(True, RT_SIGNAL_AIO); + + DEBUG(10,("process_aio_queue: signals_received = %d\n", (int)signals_received)); + DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n", outstanding_aio_calls)); + + if (!signals_received) { + BlockSignals(False, RT_SIGNAL_AIO); + return 0; + } + + /* Drain all the complete aio_reads. */ + for (i = 0; i < signals_received; i++) { + uint16 mid = aio_pending_array[i]; + files_struct *fsp = NULL; + struct aio_extra *aio_ex = find_aio_ex(mid); + + if (!aio_ex) { + DEBUG(3,("process_aio_queue: Can't find record to match mid %u.\n", + (unsigned int)mid)); + srv_cancel_sign_response(mid); + continue; + } + + fsp = aio_ex->fsp; + if (fsp == NULL) { + /* file was closed whilst I/O was outstanding. Just ignore. */ + DEBUG( 3,( "process_aio_queue: file closed whilst aio outstanding.\n")); + srv_cancel_sign_response(mid); + continue; + } + + if (!handle_aio_completed(aio_ex, &ret)) { + continue; + } + + delete_aio_ex(aio_ex); + } + + outstanding_aio_calls -= signals_received; + signals_received = 0; + BlockSignals(False, RT_SIGNAL_AIO); + return ret; +} + +/**************************************************************************** + We're doing write behind and the client closed the file. Wait up to 30 seconds + (my arbitrary choice) for the aio to complete. Return 0 if all writes completed, + errno to return if not. +*****************************************************************************/ + +#define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29 + +BOOL wait_for_aio_completion(files_struct *fsp) +{ + struct aio_extra *aio_ex; + const SMB_STRUCT_AIOCB **aiocb_list; + int aio_completion_count = 0; + time_t start_time = time(NULL); + int seconds_left; + int ret = 0; + + for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT; seconds_left >= 0;) { + int err = 0; + int i; + struct timespec ts; + + aio_completion_count = 0; + for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) { + if (aio_ex->fsp == fsp) { + aio_completion_count++; + } + } + + if (!aio_completion_count) { + return ret; + } + + DEBUG(3,("wait_for_aio_completion: waiting for %d aio events to complete.\n", + aio_completion_count )); + + aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *, aio_completion_count); + if (!aiocb_list) { + return False; + } + + for( i = 0, aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) { + if (aio_ex->fsp == fsp) { + aiocb_list[i++] = &aio_ex->acb; + } + } + + /* Now wait up to seconds_left for completion. */ + ts.tv_sec = seconds_left; + ts.tv_nsec = 0; + + DEBUG(10,("wait_for_aio_completion: %d events, doing a wait of %d seconds.\n", + aio_completion_count, seconds_left )); + + err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list, aio_completion_count, &ts); + + DEBUG(10,("wait_for_aio_completion: returned err = %d, errno = %s\n", + err, strerror(errno) )); + + if (err == -1 && errno == EAGAIN) { + DEBUG(0,("wait_for_aio_completion: aio_suspend timed out waiting for %d events after a wait of %d seconds\n", + aio_completion_count, seconds_left)); + /* Timeout. */ + cancel_aio_by_fsp(fsp); + SAFE_FREE(aiocb_list); + return ret ? ret : EIO; + } + + /* One or more events might have completed - process them if so. */ + for( i = 0; i < aio_completion_count; i++) { + uint16 mid = *(uint16 *)aiocb_list[i]->aio_sigevent.sigev_value.sival_ptr; + + aio_ex = find_aio_ex(mid); + + if (!handle_aio_completed(aio_ex, &err)) { + continue; + } + delete_aio_ex(aio_ex); + } + + SAFE_FREE(aiocb_list); + seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT - (time(NULL) - start_time); + } + + /* We timed out - we don't know why. Return ret if already an error, else EIO. */ + DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting for %d events\n", + aio_completion_count)); + + return ret ? ret : EIO; +} + +/**************************************************************************** + Cancel any outstanding aio requests. The client doesn't care about the reply. +*****************************************************************************/ + +void cancel_aio_by_fsp(files_struct *fsp) +{ + struct aio_extra *aio_ex; + + for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) { + if (aio_ex->fsp == fsp) { + /* Don't delete the aio_extra record as we may have completed + and don't yet know it. Just do the aio_cancel call and return. */ + SMB_VFS_AIO_CANCEL(fsp,fsp->fd, &aio_ex->acb); + aio_ex->fsp = NULL; /* fsp will be closed when we return. */ + } + } +} + +/**************************************************************************** + Check if a buffer was stolen for aio use. +*****************************************************************************/ + +BOOL aio_inbuffer_in_use(char *inbuf) +{ + struct aio_extra *aio_ex; + + for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) { + if (aio_ex->inbuf == inbuf) { + return True; + } + } + return False; +} +#else +BOOL aio_finished(void) +{ + return False; +} + +void initialize_async_io_handler(void) +{ +} + +int process_aio_queue(void) +{ + return False; +} + +BOOL schedule_aio_read_and_X(connection_struct *conn, + char *inbuf, char *outbuf, + int length, int len_outbuf, + files_struct *fsp, SMB_OFF_T startpos, + size_t smb_maxcnt) +{ + return False; +} + +BOOL schedule_aio_write_and_X(connection_struct *conn, + char *inbuf, char *outbuf, + int length, int len_outbuf, + files_struct *fsp, char *data, + SMB_OFF_T startpos, + size_t numtowrite) +{ + return False; +} + +void cancel_aio_by_fsp(files_struct *fsp) +{ +} + +BOOL wait_for_aio_completion(files_struct *fsp) +{ + return True; +} + +BOOL aio_inbuffer_in_use(char *ptr) +{ + return False; +} +#endif diff --git a/source3/smbd/blocking.c b/source3/smbd/blocking.c index 0e71174a2e..72d021d4e6 100644 --- a/source3/smbd/blocking.c +++ b/source3/smbd/blocking.c @@ -20,8 +20,6 @@ #include "includes.h" -extern char *OutBuffer; - /**************************************************************************** This is the structure to queue to implement blocking locks. notify. It consists of the requesting SMB and the expiry time. @@ -175,7 +173,7 @@ static void send_blocking_reply(char *outbuf, int outsize) static void reply_lockingX_success(blocking_lock_record *blr) { - char *outbuf = OutBuffer; + char *outbuf = get_OutBuffer(); int bufsize = BUFFER_SIZE; char *inbuf = blr->inbuf; int outsize = 0; @@ -204,7 +202,7 @@ static void reply_lockingX_success(blocking_lock_record *blr) static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status) { - char *outbuf = OutBuffer; + char *outbuf = get_OutBuffer(); char *inbuf = blr->inbuf; construct_reply_common(inbuf, outbuf); @@ -295,7 +293,7 @@ static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status static BOOL process_lockread(blocking_lock_record *blr) { - char *outbuf = OutBuffer; + char *outbuf = get_OutBuffer(); char *inbuf = blr->inbuf; ssize_t nread = -1; char *data, *p; @@ -367,7 +365,7 @@ static BOOL process_lockread(blocking_lock_record *blr) static BOOL process_lock(blocking_lock_record *blr) { - char *outbuf = OutBuffer; + char *outbuf = get_OutBuffer(); char *inbuf = blr->inbuf; int outsize; SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0; diff --git a/source3/smbd/close.c b/source3/smbd/close.c index b3244432ff..b7649bcce4 100644 --- a/source3/smbd/close.c +++ b/source3/smbd/close.c @@ -156,6 +156,20 @@ static int close_normal_file(files_struct *fsp, BOOL normal_close) remove_pending_lock_requests_by_fid(fsp); + if (fsp->aio_write_behind) { + /* + * If we're finishing write behind on a close we can get a write + * error here, we must remember this. + */ + int ret = wait_for_aio_completion(fsp); + if (ret) { + saved_errno = ret; + err1 = -1; + } + } else { + cancel_aio_by_fsp(fsp); + } + /* * If we're flushing on a close we can get a write * error here, we must remember this. diff --git a/source3/smbd/conn.c b/source3/smbd/conn.c index dc7dec4e97..534a3367d4 100644 --- a/source3/smbd/conn.c +++ b/source3/smbd/conn.c @@ -257,6 +257,7 @@ void conn_free(connection_struct *conn) free_namearray(conn->veto_list); free_namearray(conn->hide_list); free_namearray(conn->veto_oplock_list); + free_namearray(conn->aio_write_behind_list); string_free(&conn->user); string_free(&conn->dirpath); diff --git a/source3/smbd/notify.c b/source3/smbd/notify.c index 92b86f350c..ad49dc0a21 100644 --- a/source3/smbd/notify.c +++ b/source3/smbd/notify.c @@ -60,6 +60,7 @@ static void change_notify_reply_packet(char *inbuf, NTSTATUS error_code) */ set_message(outbuf,18,0,False); + show_msg(outbuf); if (!send_smb(smbd_server_fd(),outbuf)) exit_server("change_notify_reply_packet: send_smb failed."); } diff --git a/source3/smbd/open.c b/source3/smbd/open.c index 8b30776fdd..9da53a5057 100644 --- a/source3/smbd/open.c +++ b/source3/smbd/open.c @@ -316,6 +316,10 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn, fsp->is_directory = False; fsp->is_stat = False; fsp->directory_delete_on_close = False; + if (conn->aio_write_behind_list && is_in_path(fname, conn->aio_write_behind_list, conn->case_sensitive)) { + fsp->aio_write_behind = True; + } + string_set(&fsp->fsp_name,fname); fsp->wcp = NULL; /* Write cache pointer. */ diff --git a/source3/smbd/oplock.c b/source3/smbd/oplock.c index 9b8df98fd5..8208fbebe3 100644 --- a/source3/smbd/oplock.c +++ b/source3/smbd/oplock.c @@ -634,6 +634,7 @@ static BOOL oplock_break_level2(files_struct *fsp, BOOL local_request) /* Save the server smb signing state. */ sign_state = srv_oplock_set_signing(False); + show_msg(outbuf); if (!send_smb(smbd_server_fd(), outbuf)) exit_server("oplock_break_level2: send_smb failed."); @@ -677,7 +678,9 @@ static BOOL oplock_break_level2(files_struct *fsp, BOOL local_request) static BOOL oplock_break(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id, BOOL local_request) { char *inbuf = NULL; + char *saved_inbuf = NULL; char *outbuf = NULL; + char *saved_outbuf = NULL; files_struct *fsp = NULL; time_t start_time; BOOL shutdown_server = False; @@ -740,14 +743,15 @@ static BOOL oplock_break(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id, * messages crossing on the wire. */ - if((inbuf = (char *)SMB_MALLOC(BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE + SAFETY_MARGIN))==NULL) { + if((inbuf = NewInBuffer(&saved_inbuf))==NULL) { DEBUG(0,("oplock_break: malloc fail for input buffer.\n")); return False; } - if((outbuf = (char *)SMB_MALLOC(BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE + SAFETY_MARGIN))==NULL) { + if((outbuf = NewOutBuffer(&saved_outbuf))==NULL) { DEBUG(0,("oplock_break: malloc fail for output buffer.\n")); - SAFE_FREE(inbuf); + set_InBuffer(saved_inbuf); + free_InBuffer(inbuf); return False; } @@ -778,6 +782,7 @@ static BOOL oplock_break(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id, /* Save the server smb signing state. */ sign_state = srv_oplock_set_signing(False); + show_msg(outbuf); if (!send_smb(smbd_server_fd(), outbuf)) { srv_oplock_set_signing(sign_state); exit_server("oplock_break: send_smb failed."); @@ -823,11 +828,16 @@ static BOOL oplock_break(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id, * From Charles Hoch . If the break processing * code closes the file (as it often does), then the fsp pointer here * points to free()'d memory. We *must* revalidate fsp each time - * around the loop. + * around the loop. With async I/O, write calls may steal the global InBuffer, + * so ensure we're using the correct one each time around the loop. */ while((fsp = initial_break_processing(dev, inode, file_id)) && OPEN_FSP(fsp) && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) { + + inbuf = get_InBuffer(); + outbuf = get_OutBuffer(); + if(receive_smb(smbd_server_fd(),inbuf, timeout) == False) { /* * Die if we got an error. @@ -899,9 +909,13 @@ static BOOL oplock_break(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id, /* Restore the chain fnum. */ file_chain_restore(); + /* Restore the global In/Out buffers. */ + set_InBuffer(saved_inbuf); + set_OutBuffer(saved_outbuf); + /* Free the buffers we've been using to recurse. */ - SAFE_FREE(inbuf); - SAFE_FREE(outbuf); + free_InBuffer(inbuf); + free_OutBuffer(outbuf); /* We need this in case a readraw crossed on the wire. */ if(global_oplock_break) diff --git a/source3/smbd/process.c b/source3/smbd/process.c index c4c1debbf3..94d4b8d903 100644 --- a/source3/smbd/process.c +++ b/source3/smbd/process.c @@ -28,8 +28,7 @@ extern int smb_echo_count; struct timeval smb_last_time; static char *InBuffer = NULL; -char *OutBuffer = NULL; -char *last_inbuf = NULL; +static char *OutBuffer = NULL; /* * Size of data we can send to client. Set @@ -287,11 +286,17 @@ static void async_processing(char *buffer, int buffer_len) { DEBUG(10,("async_processing: Doing async processing.\n")); + process_aio_queue(); + /* check for oplock messages (both UDP and kernel) */ if (receive_local_message(buffer, buffer_len, 1)) { process_local_message(buffer, buffer_len); } + /* Do the aio check again after receive_local_message as it does a select + and may have eaten our signal. */ + process_aio_queue(); + if (got_sig_term) { exit_server("Caught TERM signal"); } @@ -981,8 +986,6 @@ static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize !check_access(smbd_server_fd(), lp_hostsallow(-1), lp_hostsdeny(-1)))) return(ERROR_DOS(ERRSRV,ERRaccess)); - last_inbuf = inbuf; - outsize = smb_messages[type].fn(conn, inbuf,outbuf,size,bufsize); } @@ -1511,24 +1514,106 @@ machine %s in domain %s.\n", global_myname(), lp_workgroup())); } /**************************************************************************** - process commands from the client + Accessor functions for InBuffer, OutBuffer. ****************************************************************************/ -void smbd_process(void) +char *get_InBuffer(void) { - time_t last_timeout_processing_time = time(NULL); - unsigned int num_smbs = 0; - const size_t total_buffer_size = BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE + SAFETY_MARGIN; + return InBuffer; +} - InBuffer = (char *)SMB_MALLOC(total_buffer_size); - OutBuffer = (char *)SMB_MALLOC(total_buffer_size); - if ((InBuffer == NULL) || (OutBuffer == NULL)) - return; +void set_InBuffer(char *new_inbuf) +{ + InBuffer = new_inbuf; +} + +char *get_OutBuffer(void) +{ + return OutBuffer; +} + +void set_OutBuffer(char *new_outbuf) +{ + OutBuffer = new_outbuf; +} + +/**************************************************************************** + Free an InBuffer. Checks if not in use by aio system. + Must have been allocated by NewInBuffer. +****************************************************************************/ + +void free_InBuffer(char *inbuf) +{ + if (!aio_inbuffer_in_use(inbuf)) { + SAFE_FREE(inbuf); + } +} + +/**************************************************************************** + Free an OutBuffer. No outbuffers currently stolen by aio system. + Must have been allocated by NewInBuffer. +****************************************************************************/ + +void free_OutBuffer(char *outbuf) +{ + SAFE_FREE(outbuf); +} + +const int total_buffer_size = (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE + SAFETY_MARGIN); +/**************************************************************************** + Allocate a new InBuffer. Returns the new and old ones. +****************************************************************************/ + +char *NewInBuffer(char **old_inbuf) +{ + char *new_inbuf = (char *)SMB_MALLOC(total_buffer_size); + if (!new_inbuf) { + return NULL; + } + if (old_inbuf) { + *old_inbuf = InBuffer; + } + InBuffer = new_inbuf; #if defined(DEVELOPER) clobber_region(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, InBuffer, total_buffer_size); +#endif + return InBuffer; +} + +/**************************************************************************** + Allocate a new OutBuffer. Returns the new and old ones. +****************************************************************************/ + +char *NewOutBuffer(char **old_outbuf) +{ + char *new_outbuf = (char *)SMB_MALLOC(total_buffer_size); + if (!new_outbuf) { + return NULL; + } + if (old_outbuf) { + *old_outbuf = OutBuffer; + } + OutBuffer = new_outbuf; +#if defined(DEVELOPER) clobber_region(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, OutBuffer, total_buffer_size); #endif + return OutBuffer; +} + +/**************************************************************************** + Process commands from the client +****************************************************************************/ + +void smbd_process(void) +{ + time_t last_timeout_processing_time = time(NULL); + unsigned int num_smbs = 0; + + /* Allocate the primary Inbut/Output buffers. */ + + if ((NewInBuffer(NULL) == NULL) || (NewOutBuffer(NULL) == NULL)) + return; max_recv = MIN(lp_maxxmit(),BUFFER_SIZE); diff --git a/source3/smbd/reply.c b/source3/smbd/reply.c index 9a7c22320c..312a3ace23 100644 --- a/source3/smbd/reply.c +++ b/source3/smbd/reply.c @@ -2575,13 +2575,10 @@ int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt return ERROR_DOS(ERRDOS,ERRlock); } -#if 0 - /* Enable when the AIO code is moved over. JRA. */ if (schedule_aio_read_and_X(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt)) { END_PROFILE(SMBreadX); return -1; } -#endif nread = send_file_readX(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt); if (nread != -1) @@ -2952,15 +2949,11 @@ int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int leng nwritten = 0; } else { -#if 0 - /* Enable when AIO code is moved over. JRA. */ - if (schedule_aio_write_and_X(conn, inbuf, outbuf, length, bufsize, fsp,data,startpos,numtowrite)) { END_PROFILE(SMBwriteX); return -1; } -#endif nwritten = write_file(fsp,data,startpos,numtowrite); } diff --git a/source3/smbd/server.c b/source3/smbd/server.c index b40a8267cc..4217d821f4 100644 --- a/source3/smbd/server.c +++ b/source3/smbd/server.c @@ -30,7 +30,6 @@ int last_message = -1; /* a useful macro to debug the last message processed */ #define LAST_MESSAGE() smb_fn_name(last_message) -extern char *last_inbuf; extern struct auth_context *negprot_global_auth_context; extern pstring user_socket_options; extern SIG_ATOMIC_T got_sig_term; @@ -635,6 +634,7 @@ void exit_server(const char *reason) if (!reason) { int oldlevel = DEBUGLEVEL; + char *last_inbuf = get_InBuffer(); DEBUGLEVEL = 10; DEBUG(0,("Last message was %s\n",smb_fn_name(last_message))); if (last_inbuf) @@ -951,6 +951,9 @@ void build_options(BOOL screen); if (!init_change_notify()) exit(1); + /* Setup aio signal handler. */ + initialize_async_io_handler(); + /* re-initialise the timezone */ TimeInit(); diff --git a/source3/smbd/service.c b/source3/smbd/service.c index b53d6e3ad9..d330e847e2 100644 --- a/source3/smbd/service.c +++ b/source3/smbd/service.c @@ -379,6 +379,7 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser, conn->veto_list = NULL; conn->hide_list = NULL; conn->veto_oplock_list = NULL; + conn->aio_write_behind_list = NULL; string_set(&conn->dirpath,""); string_set(&conn->user,user); conn->nt_user_token = NULL; diff --git a/source3/smbd/sesssetup.c b/source3/smbd/sesssetup.c index 95fe571cff..1ddd6256b3 100644 --- a/source3/smbd/sesssetup.c +++ b/source3/smbd/sesssetup.c @@ -96,6 +96,7 @@ static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf, set_message_end(outbuf,p); + show_msg(outbuf); return send_smb(smbd_server_fd(),outbuf); } diff --git a/source3/smbd/trans2.c b/source3/smbd/trans2.c index 5bf53fca8a..d4daf1fd69 100644 --- a/source3/smbd/trans2.c +++ b/source3/smbd/trans2.c @@ -591,6 +591,7 @@ static int send_trans2_replies(char *outbuf, /* If there genuinely are no parameters or data to send just send the empty packet */ if(params_to_send == 0 && data_to_send == 0) { + show_msg(outbuf); if (!send_smb(smbd_server_fd(),outbuf)) exit_server("send_trans2_replies: send_smb failed."); return 0; @@ -685,6 +686,7 @@ static int send_trans2_replies(char *outbuf, params_to_send, data_to_send, paramsize, datasize)); /* Send the packet */ + show_msg(outbuf); if (!send_smb(smbd_server_fd(),outbuf)) exit_server("send_trans2_replies: send_smb failed."); @@ -4912,6 +4914,7 @@ int reply_trans2(connection_struct *conn, of the parameter/data bytes */ outsize = set_message(outbuf,0,0,True); srv_signing_trans_stop(); + show_msg(outbuf); if (!send_smb(smbd_server_fd(),outbuf)) exit_server("reply_trans2: send_smb failed."); diff --git a/source3/smbd/vfs-wrap.c b/source3/smbd/vfs-wrap.c index 3260cce9aa..1d205e5977 100644 --- a/source3/smbd/vfs-wrap.c +++ b/source3/smbd/vfs-wrap.c @@ -1052,3 +1052,38 @@ int vfswrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp { return sys_fsetxattr(fd, name, value, size, flags); } + +int vfswrap_aio_read(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb) +{ + return sys_aio_read(aiocb); +} + +int vfswrap_aio_write(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb) +{ + return sys_aio_write(aiocb); +} + +int vfswrap_aio_return(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb) +{ + return sys_aio_return(aiocb); +} + +int vfswrap_aio_cancel(struct vfs_handle_struct *handle, struct files_struct *fsp, int fd, SMB_STRUCT_AIOCB *aiocb) +{ + return sys_aio_cancel(fd, aiocb); +} + +int vfswrap_aio_error(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb) +{ + return sys_aio_error(aiocb); +} + +int vfswrap_aio_fsync(struct vfs_handle_struct *handle, struct files_struct *fsp, int op, SMB_STRUCT_AIOCB *aiocb) +{ + return sys_aio_fsync(op, aiocb); +} + +int vfswrap_aio_suspend(struct vfs_handle_struct *handle, struct files_struct *fsp, const SMB_STRUCT_AIOCB * const aiocb[], int n, const struct timespec *timeout) +{ + return sys_aio_suspend(aiocb, n, timeout); +} diff --git a/source3/smbd/vfs.c b/source3/smbd/vfs.c index b09935eaa9..11adfed694 100644 --- a/source3/smbd/vfs.c +++ b/source3/smbd/vfs.c @@ -145,7 +145,16 @@ static struct vfs_ops default_vfs = { vfswrap_fremovexattr, vfswrap_setxattr, vfswrap_lsetxattr, - vfswrap_fsetxattr + vfswrap_fsetxattr, + + /* AIO operations. */ + vfswrap_aio_read, + vfswrap_aio_write, + vfswrap_aio_return, + vfswrap_aio_cancel, + vfswrap_aio_error, + vfswrap_aio_fsync, + vfswrap_aio_suspend } }; @@ -277,20 +286,20 @@ BOOL vfs_init_custom(connection_struct *conn, const char *vfs_object) DLIST_ADD(conn->vfs_handles, handle); for(i=0; ops[i].op != NULL; i++) { - DEBUG(5, ("Checking operation #%d (type %d, layer %d)\n", i, ops[i].type, ops[i].layer)); - if(ops[i].layer == SMB_VFS_LAYER_OPAQUE) { - /* Check whether this operation was already made opaque by different module */ - if(((void**)&conn->vfs_opaque.ops)[ops[i].type] == ((void**)&default_vfs.ops)[ops[i].type]) { - /* No, it isn't overloaded yet. Overload. */ - DEBUGADD(5, ("Making operation type %d opaque [module %s]\n", ops[i].type, vfs_object)); - ((void**)&conn->vfs_opaque.ops)[ops[i].type] = ops[i].op; - ((vfs_handle_struct **)&conn->vfs_opaque.handles)[ops[i].type] = handle; - } - } - /* Change current VFS disposition*/ - DEBUGADD(5, ("Accepting operation type %d from module %s\n", ops[i].type, vfs_object)); - ((void**)&conn->vfs.ops)[ops[i].type] = ops[i].op; - ((vfs_handle_struct **)&conn->vfs.handles)[ops[i].type] = handle; + DEBUG(5, ("Checking operation #%d (type %d, layer %d)\n", i, ops[i].type, ops[i].layer)); + if(ops[i].layer == SMB_VFS_LAYER_OPAQUE) { + /* Check whether this operation was already made opaque by different module */ + if(((void**)&conn->vfs_opaque.ops)[ops[i].type] == ((void**)&default_vfs.ops)[ops[i].type]) { + /* No, it isn't overloaded yet. Overload. */ + DEBUGADD(5, ("Making operation type %d opaque [module %s]\n", ops[i].type, vfs_object)); + ((void**)&conn->vfs_opaque.ops)[ops[i].type] = ops[i].op; + ((vfs_handle_struct **)&conn->vfs_opaque.handles)[ops[i].type] = handle; + } + } + /* Change current VFS disposition*/ + DEBUGADD(5, ("Accepting operation type %d from module %s\n", ops[i].type, vfs_object)); + ((void**)&conn->vfs.ops)[ops[i].type] = ops[i].op; + ((vfs_handle_struct **)&conn->vfs.handles)[ops[i].type] = handle; } SAFE_FREE(module_name); -- cgit