--- libeio/eio.c 2011/06/10 06:50:42 1.69 +++ libeio/eio.c 2011/07/14 18:30:10 1.86 @@ -42,6 +42,7 @@ #endif #include "eio.h" +#include "ecb.h" #ifdef EIO_STACKSIZE # define XTHREAD_STACKSIZE EIO_STACKSIZE @@ -55,17 +56,23 @@ #include #include #include -#include #include #include #include +#include /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ -/* intptr_t only comes form stdint.h, says idiot openbsd coder */ +/* intptr_t only comes from stdint.h, says idiot openbsd coder */ #if HAVE_STDINT_H # include #endif +#ifndef ECANCELED +# define ECANCELED EDOM +#endif + +static void eio_destroy (eio_req *req); + #ifndef EIO_FINISH # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 #endif @@ -78,43 +85,83 @@ # define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0) #endif +#ifndef EIO_FD_TO_WIN32_HANDLE +# define EIO_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) +#endif +#ifndef EIO_WIN32_HANDLE_TO_FD +# define EIO_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0) +#endif + +#define EIO_ERRNO(errval,retval) ((errno = errval), retval) + +#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1) + #ifdef _WIN32 - /*doh*/ + #define PAGESIZE 4096 /* GetSystemInfo? */ + + #define stat(path,buf) _stati64 (path,buf) + #define lstat(path,buf) stat (path,buf) + #define fstat(fd,buf) _fstati64 (path,buf) + #define fsync(fd) (FlushFileBuffers (EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1)) + #define mkdir(path,mode) _mkdir (path) + #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) + + #define chown(path,uid,gid) EIO_ENOSYS () + #define fchown(fd,uid,gid) EIO_ENOSYS () + #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ + #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ + #define mknod(path,mode,dev) EIO_ENOSYS () + #define sync() EIO_ENOSYS () + + /* we could even stat and see if it exists */ + static int + symlink (const char *old, const char *neu) + { + if (CreateSymbolicLink (neu, old, 1)) + return 0; + + if (CreateSymbolicLink (neu, old, 0)) + return 0; + + return EIO_ERRNO (ENOENT, -1); + } + #else -# include -# include -# include -# include -# include -# include - -#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES -# include -#endif - -/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ -# if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ -# define _DIRENT_HAVE_D_TYPE /* sigh */ -# define D_INO(de) (de)->d_fileno -# define D_NAMLEN(de) (de)->d_namlen -# elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 -# define D_INO(de) (de)->d_ino -# endif + #include + #include + #include + #include + #include + #include + #include -#ifdef _D_EXACT_NAMLEN -# undef D_NAMLEN -# define D_NAMLEN(de) _D_EXACT_NAMLEN (de) -#endif + #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES + #include + #endif -# ifdef _DIRENT_HAVE_D_TYPE -# define D_TYPE(de) (de)->d_type -# endif + /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ + #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ + #define _DIRENT_HAVE_D_TYPE /* sigh */ + #define D_INO(de) (de)->d_fileno + #define D_NAMLEN(de) (de)->d_namlen + #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 + #define D_INO(de) (de)->d_ino + #endif -# ifndef EIO_STRUCT_DIRENT -# define EIO_STRUCT_DIRENT struct dirent -# endif + #ifdef _D_EXACT_NAMLEN + #undef D_NAMLEN + #define D_NAMLEN(de) _D_EXACT_NAMLEN (de) + #endif + + #ifdef _DIRENT_HAVE_D_TYPE + #define D_TYPE(de) (de)->d_type + #endif + + #ifndef EIO_STRUCT_DIRENT + #define EIO_STRUCT_DIRENT struct dirent + #endif #endif @@ -167,19 +214,6 @@ #define EIO_TICKS ((1000000 + 1023) >> 10) -/*****************************************************************************/ - -#if __GNUC__ >= 3 -# define expect(expr,value) __builtin_expect ((expr),(value)) -#else -# define expect(expr,value) (expr) -#endif - -#define expect_false(expr) expect ((expr) != 0, 0) -#define expect_true(expr) expect ((expr) != 0, 1) - -/*****************************************************************************/ - #define ETP_PRI_MIN EIO_PRI_MIN #define ETP_PRI_MAX EIO_PRI_MAX @@ -214,7 +248,8 @@ #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) /* calculate time difference in ~1/EIO_TICKS of a second */ -static int tvdiff (struct timeval *tv1, struct timeval *tv2) +ecb_inline int +tvdiff (struct timeval *tv1, struct timeval *tv2) { return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS + ((tv2->tv_usec - tv1->tv_usec) >> 10); @@ -268,12 +303,14 @@ /* worker threads management */ -static void etp_worker_clear (etp_worker *wrk) +static void ecb_cold +etp_worker_clear (etp_worker *wrk) { ETP_WORKER_CLEAR (wrk); } -static void etp_worker_free (etp_worker *wrk) +static void ecb_cold +etp_worker_free (etp_worker *wrk) { wrk->next->prev = wrk->prev; wrk->prev->next = wrk->next; @@ -281,7 +318,8 @@ free (wrk); } -static unsigned int etp_nreqs (void) +static unsigned int +etp_nreqs (void) { int retval; if (WORDACCESS_UNSAFE) X_LOCK (reqlock); @@ -290,7 +328,8 @@ return retval; } -static unsigned int etp_nready (void) +static unsigned int +etp_nready (void) { unsigned int retval; @@ -301,7 +340,8 @@ return retval; } -static unsigned int etp_npending (void) +static unsigned int +etp_npending (void) { unsigned int retval; @@ -312,7 +352,8 @@ return retval; } -static unsigned int etp_nthreads (void) +static unsigned int +etp_nthreads (void) { unsigned int retval; @@ -336,7 +377,8 @@ static etp_reqq req_queue; static etp_reqq res_queue; -static int reqq_push (etp_reqq *q, ETP_REQ *req) +static int ecb_noinline +reqq_push (etp_reqq *q, ETP_REQ *req) { int pri = req->pri; req->next = 0; @@ -352,7 +394,8 @@ return q->size++; } -static ETP_REQ *reqq_shift (etp_reqq *q) +static ETP_REQ * ecb_noinline +reqq_shift (etp_reqq *q) { int pri; @@ -377,35 +420,30 @@ abort (); } -static void etp_thread_init (void) +static void ecb_cold +etp_thread_init (void) { +#if !HAVE_PREADWRITE + X_MUTEX_CREATE (preadwritelock); +#endif X_MUTEX_CREATE (wrklock); X_MUTEX_CREATE (reslock); X_MUTEX_CREATE (reqlock); X_COND_CREATE (reqwait); } -static void etp_atfork_prepare (void) +static void ecb_cold +etp_atfork_prepare (void) { - X_LOCK (wrklock); - X_LOCK (reqlock); - X_LOCK (reslock); -#if !HAVE_PREADWRITE - X_LOCK (preadwritelock); -#endif } -static void etp_atfork_parent (void) +static void ecb_cold +etp_atfork_parent (void) { -#if !HAVE_PREADWRITE - X_UNLOCK (preadwritelock); -#endif - X_UNLOCK (reslock); - X_UNLOCK (reqlock); - X_UNLOCK (wrklock); } -static void etp_atfork_child (void) +static void ecb_cold +etp_atfork_child (void) { ETP_REQ *prv; @@ -435,14 +473,14 @@ etp_thread_init (); } -static void +static void ecb_cold etp_once_init (void) { etp_thread_init (); X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); } -static int +static int ecb_cold etp_init (void (*want_poll)(void), void (*done_poll)(void)) { static pthread_once_t doinit = PTHREAD_ONCE_INIT; @@ -457,7 +495,8 @@ X_THREAD_PROC (etp_proc); -static void etp_start_thread (void) +static void ecb_cold +etp_start_thread (void) { etp_worker *wrk = calloc (1, sizeof (etp_worker)); @@ -480,19 +519,21 @@ X_UNLOCK (wrklock); } -static void etp_maybe_start_thread (void) +static void +etp_maybe_start_thread (void) { - if (expect_true (etp_nthreads () >= wanted)) + if (ecb_expect_true (etp_nthreads () >= wanted)) return; /* todo: maybe use idle here, but might be less exact */ - if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) + if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) return; etp_start_thread (); } -static void etp_end_thread (void) +static void ecb_cold +etp_end_thread (void) { eio_req *req = calloc (1, sizeof (eio_req)); @@ -509,7 +550,8 @@ X_UNLOCK (wrklock); } -static int etp_poll (void) +static int +etp_poll (void) { unsigned int maxreqs; unsigned int maxtime; @@ -549,7 +591,7 @@ --nreqs; X_UNLOCK (reqlock); - if (expect_false (req->type == EIO_GROUP && req->size)) + if (ecb_expect_false (req->type == EIO_GROUP && req->size)) { req->int1 = 1; /* mark request as delayed */ continue; @@ -557,11 +599,11 @@ else { int res = ETP_FINISH (req); - if (expect_false (res)) + if (ecb_expect_false (res)) return res; } - if (expect_false (maxreqs && !--maxreqs)) + if (ecb_expect_false (maxreqs && !--maxreqs)) break; if (maxtime) @@ -577,23 +619,23 @@ return -1; } -static void etp_cancel (ETP_REQ *req) +static void +etp_cancel (ETP_REQ *req) { - X_LOCK (wrklock); - req->flags |= EIO_FLAG_CANCELLED; - X_UNLOCK (wrklock); + req->cancelled = 1; eio_grp_cancel (req); } -static void etp_submit (ETP_REQ *req) +static void +etp_submit (ETP_REQ *req) { req->pri -= ETP_PRI_MIN; - if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; - if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; + if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; + if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; - if (expect_false (req->type == EIO_GROUP)) + if (ecb_expect_false (req->type == EIO_GROUP)) { /* I hope this is worth it :/ */ X_LOCK (reqlock); @@ -622,41 +664,47 @@ } } -static void etp_set_max_poll_time (double nseconds) +static void ecb_cold +etp_set_max_poll_time (double nseconds) { if (WORDACCESS_UNSAFE) X_LOCK (reslock); max_poll_time = nseconds * EIO_TICKS; if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); } -static void etp_set_max_poll_reqs (unsigned int maxreqs) +static void ecb_cold +etp_set_max_poll_reqs (unsigned int maxreqs) { if (WORDACCESS_UNSAFE) X_LOCK (reslock); max_poll_reqs = maxreqs; if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); } -static void etp_set_max_idle (unsigned int nthreads) +static void ecb_cold +etp_set_max_idle (unsigned int nthreads) { if (WORDACCESS_UNSAFE) X_LOCK (reqlock); max_idle = nthreads; if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); } -static void etp_set_idle_timeout (unsigned int seconds) +static void ecb_cold +etp_set_idle_timeout (unsigned int seconds) { if (WORDACCESS_UNSAFE) X_LOCK (reqlock); idle_timeout = seconds; if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); } -static void etp_set_min_parallel (unsigned int nthreads) +static void ecb_cold +etp_set_min_parallel (unsigned int nthreads) { if (wanted < nthreads) wanted = nthreads; } -static void etp_set_max_parallel (unsigned int nthreads) +static void ecb_cold +etp_set_max_parallel (unsigned int nthreads) { if (wanted > nthreads) wanted = nthreads; @@ -667,7 +715,8 @@ /*****************************************************************************/ -static void grp_try_feed (eio_req *grp) +static void +grp_try_feed (eio_req *grp) { while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) { @@ -684,7 +733,8 @@ } } -static int grp_dec (eio_req *grp) +static int +grp_dec (eio_req *grp) { --grp->size; @@ -698,7 +748,8 @@ return 0; } -void eio_destroy (eio_req *req) +static void +eio_destroy (eio_req *req) { if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); @@ -706,7 +757,8 @@ EIO_DESTROY (req); } -static int eio_finish (eio_req *req) +static int +eio_finish (eio_req *req) { int res = EIO_FINISH (req); @@ -724,7 +776,7 @@ res2 = grp_dec (grp); - if (!res && res2) + if (!res) res = res2; } @@ -733,68 +785,81 @@ return res; } -void eio_grp_cancel (eio_req *grp) +void +eio_grp_cancel (eio_req *grp) { for (grp = grp->grp_first; grp; grp = grp->grp_next) eio_cancel (grp); } -void eio_cancel (eio_req *req) +void +eio_cancel (eio_req *req) { etp_cancel (req); } -void eio_submit (eio_req *req) +void +eio_submit (eio_req *req) { etp_submit (req); } -unsigned int eio_nreqs (void) +unsigned int +eio_nreqs (void) { return etp_nreqs (); } -unsigned int eio_nready (void) +unsigned int +eio_nready (void) { return etp_nready (); } -unsigned int eio_npending (void) +unsigned int +eio_npending (void) { return etp_npending (); } -unsigned int eio_nthreads (void) +unsigned int ecb_cold +eio_nthreads (void) { return etp_nthreads (); } -void eio_set_max_poll_time (double nseconds) +void ecb_cold +eio_set_max_poll_time (double nseconds) { etp_set_max_poll_time (nseconds); } -void eio_set_max_poll_reqs (unsigned int maxreqs) +void ecb_cold +eio_set_max_poll_reqs (unsigned int maxreqs) { etp_set_max_poll_reqs (maxreqs); } -void eio_set_max_idle (unsigned int nthreads) +void ecb_cold +eio_set_max_idle (unsigned int nthreads) { etp_set_max_idle (nthreads); } -void eio_set_idle_timeout (unsigned int seconds) +void ecb_cold +eio_set_idle_timeout (unsigned int seconds) { etp_set_idle_timeout (seconds); } -void eio_set_min_parallel (unsigned int nthreads) +void ecb_cold +eio_set_min_parallel (unsigned int nthreads) { etp_set_min_parallel (nthreads); } -void eio_set_max_parallel (unsigned int nthreads) +void ecb_cold +eio_set_max_parallel (unsigned int nthreads) { etp_set_max_parallel (nthreads); } @@ -874,7 +939,8 @@ # undef futimes # define futimes(fd,times) eio__futimes (fd, times) -static int eio__futimes (int fd, const struct timeval tv[2]) +static int +eio__futimes (int fd, const struct timeval tv[2]) { errno = ENOSYS; return -1; @@ -888,7 +954,7 @@ #endif /* sync_file_range always needs emulation */ -int +static int eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) { #if HAVE_SYNC_FILE_RANGE @@ -915,6 +981,17 @@ return fdatasync (fd); } +static int +eio__fallocate (int fd, int mode, off_t offset, size_t len) +{ +#if HAVE_FALLOCATE + return fallocate (fd, mode, offset, len); +#else + errno = ENOSYS; + return -1; +#endif +} + #if !HAVE_READAHEAD # undef readahead # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) @@ -952,6 +1029,9 @@ for (;;) { +#ifdef __APPLE__ +# undef HAVE_SENDFILE /* broken, as everything on os x */ +#endif #if HAVE_SENDFILE # if __linux off_t soffset = offset; @@ -1004,8 +1084,11 @@ # endif -#elif defined (_WIN32) +#elif defined (_WIN32) && 0 /* does not work, just for documentation of what would need to be done */ + /* actually, cannot be done like this, as TransmitFile changes the file offset, */ + /* libeio guarantees that the file offset does not change, and windows */ + /* has no way to get an independent handle to the same file description */ HANDLE h = TO_SOCKET (ifd); SetFilePointer (h, offset, 0, FILE_BEGIN); res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); @@ -1085,6 +1168,285 @@ return res; } +#ifdef PAGESIZE +# define eio_pagesize() PAGESIZE +#else +static intptr_t +eio_pagesize (void) +{ + static intptr_t page; + + if (!page) + page = sysconf (_SC_PAGESIZE); + + return page; +} +#endif + +static void +eio_page_align (void **addr, size_t *length) +{ + intptr_t mask = eio_pagesize () - 1; + + /* round down addr */ + intptr_t adj = mask & (intptr_t)*addr; + + *addr = (void *)((intptr_t)*addr - adj); + *length += adj; + + /* round up length */ + *length = (*length + mask) & ~mask; +} + +#if !_POSIX_MEMLOCK +# define eio__mlockall(a) eio_nosyscall() +#else + +static int +eio__mlockall (int flags) +{ + #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 + extern int mallopt (int, int); + mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ + #endif + + if (EIO_MCL_CURRENT != MCL_CURRENT + || EIO_MCL_FUTURE != MCL_FUTURE) + { + flags = 0 + | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) + | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); + } + + return mlockall (flags); +} +#endif + +#if !_POSIX_MEMLOCK_RANGE +# define eio__mlock(a,b) EIO_ENOSYS () +#else + +static int +eio__mlock (void *addr, size_t length) +{ + eio_page_align (&addr, &length); + + return mlock (addr, length); +} + +#endif + +#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) +# define eio__msync(a,b,c) EIO_ENOSYS () +#else + +static int +eio__msync (void *mem, size_t len, int flags) +{ + eio_page_align (&mem, &len); + + if (EIO_MS_ASYNC != MS_SYNC + || EIO_MS_INVALIDATE != MS_INVALIDATE + || EIO_MS_SYNC != MS_SYNC) + { + flags = 0 + | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) + | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) + | (flags & EIO_MS_SYNC ? MS_SYNC : 0); + } + + return msync (mem, len, flags); +} + +#endif + +static int +eio__mtouch (eio_req *req) +{ + void *mem = req->ptr2; + size_t len = req->size; + int flags = req->int1; + + eio_page_align (&mem, &len); + + { + intptr_t addr = (intptr_t)mem; + intptr_t end = addr + len; + intptr_t page = eio_pagesize (); + + if (addr < end) + if (flags & EIO_MT_MODIFY) /* modify */ + do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); + else + do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); + } + + return 0; +} + +/*****************************************************************************/ +/* requests implemented outside eio_execute, because they are so large */ + +static void +eio__realpath (eio_req *req, etp_worker *self) +{ + char *rel = req->ptr1; + char *res; + char *tmp1, *tmp2; +#if SYMLOOP_MAX > 32 + int symlinks = SYMLOOP_MAX; +#else + int symlinks = 32; +#endif + + req->result = -1; + + errno = EINVAL; + if (!rel) + return; + + errno = ENOENT; + if (!*rel) + return; + + if (!req->ptr2) + { + X_LOCK (wrklock); + req->flags |= EIO_FLAG_PTR2_FREE; + X_UNLOCK (wrklock); + req->ptr2 = malloc (PATH_MAX * 3); + + errno = ENOMEM; + if (!req->ptr2) + return; + } + + res = req->ptr2; + tmp1 = res + PATH_MAX; + tmp2 = tmp1 + PATH_MAX; + +#if 0 /* disabled, the musl way to do things is just too racy */ +#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) + /* on linux we may be able to ask the kernel */ + { + int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); + + if (fd >= 0) + { + sprintf (tmp1, "/proc/self/fd/%d", fd); + req->result = readlink (tmp1, res, PATH_MAX); + close (fd); + + /* here we should probably stat the open file and the disk file, to make sure they still match */ + + if (req->result > 0) + goto done; + } + else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) + return; + } +#endif +#endif + + if (*rel != '/') + { + if (!getcwd (res, PATH_MAX)) + return; + + if (res [1]) /* only use if not / */ + res += strlen (res); + } + + while (*rel) + { + ssize_t len, linklen; + char *beg = rel; + + while (*rel && *rel != '/') + ++rel; + + len = rel - beg; + + if (!len) /* skip slashes */ + { + ++rel; + continue; + } + + if (beg [0] == '.') + { + if (len == 1) + continue; /* . - nop */ + + if (beg [1] == '.' && len == 2) + { + /* .. - back up one component, if possible */ + + while (res != req->ptr2) + if (*--res == '/') + break; + + continue; + } + } + + errno = ENAMETOOLONG; + if (res + 1 + len + 1 >= tmp1) + return; + + /* copy one component */ + *res = '/'; + memcpy (res + 1, beg, len); + + /* zero-terminate, for readlink */ + res [len + 1] = 0; + + /* now check if it's a symlink */ + linklen = readlink (req->ptr2, tmp1, PATH_MAX); + + if (linklen < 0) + { + if (errno != EINVAL) + return; + + /* it's a normal directory. hopefully */ + res += len + 1; + } + else + { + /* yay, it was a symlink - build new path in tmp2 */ + int rellen = strlen (rel); + + errno = ENAMETOOLONG; + if (linklen + 1 + rellen >= PATH_MAX) + return; + + errno = ELOOP; + if (!--symlinks) + return; + + if (*tmp1 == '/') + res = req->ptr2; /* symlink resolves to an absolute path */ + + /* we need to be careful, as rel might point into tmp2 already */ + memmove (tmp2 + linklen + 1, rel, rellen + 1); + tmp2 [linklen] = '/'; + memcpy (tmp2, tmp1, linklen); + + rel = tmp2; + } + } + + /* special case for the lone root path */ + if (res == req->ptr2) + *res++ = '/'; + + req->result = res - (char *)req->ptr2; + +done: + req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */ +} + static signed char eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) { @@ -1274,6 +1636,9 @@ /* the corresponding closedir is in ETP_WORKER_CLEAR */ self->dirp = dirp = opendir (req->ptr1); + if (req->flags & EIO_FLAG_PTR1_FREE) + free (req->ptr1); + req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; req->ptr2 = names = malloc (namesalloc); @@ -1334,7 +1699,7 @@ { int len = D_NAMLEN (entp) + 1; - while (expect_false (namesoffs + len > namesalloc)) + while (ecb_expect_false (namesoffs + len > namesalloc)) { namesalloc *= 2; X_LOCK (wrklock); @@ -1351,7 +1716,7 @@ { struct eio_dirent *ent; - if (expect_false (dentoffs == dentalloc)) + if (ecb_expect_false (dentoffs == dentalloc)) { dentalloc *= 2; X_LOCK (wrklock); @@ -1449,122 +1814,6 @@ } } -#ifdef PAGESIZE -# define eio_pagesize() PAGESIZE -#else -static intptr_t -eio_pagesize (void) -{ - static intptr_t page; - - if (!page) - page = sysconf (_SC_PAGESIZE); - - return page; -} -#endif - -static void -eio_page_align (void **addr, size_t *length) -{ - intptr_t mask = eio_pagesize () - 1; - - /* round down addr */ - intptr_t adj = mask & (intptr_t)*addr; - - *addr = (void *)((intptr_t)*addr - adj); - *length += adj; - - /* round up length */ - *length = (*length + mask) & ~mask; -} - -#if !_POSIX_MEMLOCK -# define eio__mlockall(a) ((errno = ENOSYS), -1) -#else - -static int -eio__mlockall (int flags) -{ - #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 - extern int mallopt (int, int); - mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ - #endif - - if (EIO_MCL_CURRENT != MCL_CURRENT - || EIO_MCL_FUTURE != MCL_FUTURE) - { - flags = 0 - | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) - | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); - } - - return mlockall (flags); -} -#endif - -#if !_POSIX_MEMLOCK_RANGE -# define eio__mlock(a,b) ((errno = ENOSYS), -1) -#else - -static int -eio__mlock (void *addr, size_t length) -{ - eio_page_align (&addr, &length); - - return mlock (addr, length); -} - -#endif - -#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) -# define eio__msync(a,b,c) ((errno = ENOSYS), -1) -#else - -int -eio__msync (void *mem, size_t len, int flags) -{ - eio_page_align (&mem, &len); - - if (EIO_MS_ASYNC != MS_SYNC - || EIO_MS_INVALIDATE != MS_INVALIDATE - || EIO_MS_SYNC != MS_SYNC) - { - flags = 0 - | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) - | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) - | (flags & EIO_MS_SYNC ? MS_SYNC : 0); - } - - return msync (mem, len, flags); -} - -#endif - -int -eio__mtouch (eio_req *req) -{ - void *mem = req->ptr2; - size_t len = req->size; - int flags = req->int1; - - eio_page_align (&mem, &len); - - { - intptr_t addr = (intptr_t)mem; - intptr_t end = addr + len; - intptr_t page = eio_pagesize (); - - if (addr < end) - if (flags & EIO_MT_MODIFY) /* modify */ - do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); - else - do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); - } - - return 0; -} - /*****************************************************************************/ #define ALLOC(len) \ @@ -1587,12 +1836,15 @@ ETP_REQ *req; struct timespec ts; etp_worker *self = (etp_worker *)thr_arg; + int timeout; - /* try to distribute timeouts somewhat randomly */ + /* try to distribute timeouts somewhat evenly */ ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); for (;;) { + ts.tv_sec = 0; + X_LOCK (reqlock); for (;;) @@ -1602,23 +1854,28 @@ if (req) break; + if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ + { + X_UNLOCK (reqlock); + X_LOCK (wrklock); + --started; + X_UNLOCK (wrklock); + goto quit; + } + ++idle; - ts.tv_sec = time (0) + idle_timeout; - if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) + if (idle <= max_idle) + /* we are allowed to idle, so do so without any timeout */ + X_COND_WAIT (reqwait, reqlock); + else { - if (idle > max_idle) - { - --idle; - X_UNLOCK (reqlock); - X_LOCK (wrklock); - --started; - X_UNLOCK (wrklock); - goto quit; - } + /* initialise timeout once */ + if (!ts.tv_sec) + ts.tv_sec = time (0) + idle_timeout; - /* we are allowed to idle, so do so without any timeout */ - X_COND_WAIT (reqwait, reqlock); + if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) + ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ } --idle; @@ -1631,8 +1888,7 @@ if (req->type < 0) goto quit; - if (!EIO_CANCELLED (req)) - ETP_EXECUTE (self, req); + ETP_EXECUTE (self, req); X_LOCK (reslock); @@ -1657,12 +1913,14 @@ /*****************************************************************************/ -int eio_init (void (*want_poll)(void), void (*done_poll)(void)) +int ecb_cold +eio_init (void (*want_poll)(void), void (*done_poll)(void)) { return etp_init (want_poll, done_poll); } -static void eio_api_destroy (eio_req *req) +ecb_inline void +eio_api_destroy (eio_req *req) { free (req); } @@ -1691,8 +1949,16 @@ return 0; \ } -static void eio_execute (etp_worker *self, eio_req *req) +static void +eio_execute (etp_worker *self, eio_req *req) { + if (ecb_expect_false (EIO_CANCELLED (req))) + { + req->result = -1; + req->errorno = ECANCELED; + return; + } + switch (req->type) { case EIO_READ: ALLOC (req->size); @@ -1736,6 +2002,8 @@ case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; + case EIO_REALPATH: eio__realpath (req, self); break; + case EIO_READLINK: ALLOC (PATH_MAX); req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; @@ -1747,6 +2015,7 @@ case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; + case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; case EIO_READDIR: eio__scandir (req, self); break; @@ -1856,6 +2125,11 @@ REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; } +eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND; +} + eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) { REQ (EIO_FDATASYNC); req->int1 = fd; SEND; @@ -1962,6 +2236,11 @@ return eio__1path (EIO_READLINK, path, pri, cb, data); } +eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data) +{ + return eio__1path (EIO_REALPATH, path, pri, cb, data); +} + eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) { return eio__1path (EIO_STAT, path, pri, cb, data); @@ -2028,7 +2307,7 @@ return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); } -eio_req *eio_custom (void (*)(eio_req *) execute, int pri, eio_cb cb, void *data); +eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) { REQ (EIO_CUSTOM); req->feed = execute; SEND; } @@ -2049,7 +2328,8 @@ /*****************************************************************************/ /* grp functions */ -void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) +void +eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) { grp->int2 = limit; grp->feed = feed; @@ -2057,14 +2337,16 @@ grp_try_feed (grp); } -void eio_grp_limit (eio_req *grp, int limit) +void +eio_grp_limit (eio_req *grp, int limit) { grp->int2 = limit; grp_try_feed (grp); } -void eio_grp_add (eio_req *grp, eio_req *req) +void +eio_grp_add (eio_req *grp, eio_req *req) { assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); @@ -2085,7 +2367,8 @@ /*****************************************************************************/ /* misc garbage */ -ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) +ssize_t +eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) { etp_worker wrk; ssize_t ret;