--- libeio/eio.c 2011/07/26 11:07:08 1.99 +++ libeio/eio.c 2018/06/15 02:57:36 1.150 @@ -1,19 +1,19 @@ /* * libeio implementation * - * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann + * Copyright (c) 2007,2008,2009,2010,2011,2012,2013,2016,2017 Marc Alexander Lehmann * All rights reserved. * * Redistribution and use in source and binary forms, with or without modifica- * tion, are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO @@ -44,11 +44,6 @@ #include "eio.h" #include "ecb.h" -#ifdef EIO_STACKSIZE -# define XTHREAD_STACKSIZE EIO_STACKSIZE -#endif -#include "xthread.h" - #include #include #include @@ -107,7 +102,10 @@ #undef PAGESIZE #define PAGESIZE 4096 /* GetSystemInfo? */ + /* TODO: look at how perl does stat (non-sloppy), unlink (ro-files), utime, link */ + #ifdef EIO_STRUCT_STATI64 + /* look at perl's non-sloppy stat */ #define stat(path,buf) _stati64 (path,buf) #define fstat(fd,buf) _fstati64 (fd,buf) #endif @@ -119,7 +117,10 @@ #define chmod(path,mode) _chmod (path, mode) #define dup(fd) _dup (fd) #define dup2(fd1,fd2) _dup2 (fd1, fd2) + #define pipe(fds) _pipe (fds, 4096, O_BINARY) + #define fcntl(fd,cmd,arg) EIO_ENOSYS () + #define ioctl(fd,cmd,arg) EIO_ENOSYS () #define fchmod(fd,mode) EIO_ENOSYS () #define chown(path,uid,gid) EIO_ENOSYS () #define fchown(fd,uid,gid) EIO_ENOSYS () @@ -131,6 +132,41 @@ #define statvfs(path,buf) EIO_ENOSYS () #define fstatvfs(fd,buf) EIO_ENOSYS () + #define pread(fd,buf,count,offset) eio__pread (fd, buf, count, offset) + #define pwrite(fd,buf,count,offset) eio__pwrite (fd, buf, count, offset) + + #if __GNUC__ + typedef long long eio_off_t; /* signed for compatibility to msvc */ + #else + typedef __int64 eio_off_t; /* unsigned not supported by msvc */ + #endif + + static eio_ssize_t + eio__pread (int fd, void *buf, eio_ssize_t count, eio_off_t offset) + { + OVERLAPPED o = { 0 }; + DWORD got; + + o.Offset = offset; + o.OffsetHigh = offset >> 32; + + return ReadFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o) + ? got : -1; + } + + static eio_ssize_t + eio__pwrite (int fd, void *buf, eio_ssize_t count, eio_off_t offset) + { + OVERLAPPED o = { 0 }; + DWORD got; + + o.Offset = offset; + o.OffsetHigh = offset >> 32; + + return WriteFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o) + ? got : -1; + } + /* rename() uses MoveFile, which fails to overwrite */ #define rename(old,neu) eio__rename (old, neu) @@ -167,19 +203,23 @@ symlink (const char *old, const char *neu) { #if WINVER >= 0x0600 - if (CreateSymbolicLink (neu, old, 1)) - return 0; + int flags; - if (CreateSymbolicLink (neu, old, 0)) - return 0; + /* This tries out all combinations of SYMBOLIC_LINK_FLAG_DIRECTORY + * and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE, + * with directory first. + */ + for (flags = 3; flags >= 0; --flags) + if (CreateSymbolicLink (neu, old, flags)) + return 0; #endif return EIO_ERRNO (ENOENT, -1); } - /* POSIX API only */ - #define CreateHardLink(neu,old,flags) 0 - #define CreateSymbolicLink(neu,old,flags) 0 + /* POSIX API only, causing trouble for win32 apps */ + #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */ + #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */ struct statvfs { @@ -193,13 +233,22 @@ #else + #include #include #include - #include #include #include #include + #ifdef ANDROID + #include + #define statvfs statfs + #define fstatvfs fstatfs + #include /* supposedly limits.h does #define PAGESIZE PAGESIZE */ + #else + #include + #endif + #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES #include #endif @@ -207,7 +256,7 @@ #define D_NAME(entp) entp->d_name /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ - #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ + #if __FreeBSD__ || __NetBSD__ || __OpenBSD__ #define _DIRENT_HAVE_D_TYPE /* sigh */ #define D_INO(de) (de)->d_fileno #define D_NAMLEN(de) (de)->d_namlen @@ -238,10 +287,6 @@ # include #endif -#if HAVE_SYS_PRCTL_H -# include -#endif - #if HAVE_SENDFILE # if __linux # include @@ -257,6 +302,11 @@ # endif #endif +#if HAVE_RENAMEAT2 +# include +# include +#endif + #ifndef D_TYPE # define D_TYPE(de) 0 #endif @@ -274,13 +324,23 @@ /* used for readlink etc. */ #ifndef PATH_MAX -# define PATH_MAX 4096 +# define PATH_MAX 0 #endif +#ifndef O_CLOEXEC + #define O_CLOEXEC 0 +#endif + +#ifndef EIO_PATH_MIN +# define EIO_PATH_MIN 8160 +#endif + +#define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX) + /* buffer size for various temporary buffers */ #define EIO_BUFSIZE 65536 -#define dBUF \ +#define dBUF \ char *eio_buf = malloc (EIO_BUFSIZE); \ errno = ENOMEM; \ if (!eio_buf) \ @@ -289,454 +349,57 @@ #define FUBd \ free (eio_buf) -#define EIO_TICKS ((1000000 + 1023) >> 10) - -#define ETP_PRI_MIN EIO_PRI_MIN -#define ETP_PRI_MAX EIO_PRI_MAX - -struct etp_worker; - -#define ETP_REQ eio_req -#define ETP_DESTROY(req) eio_destroy (req) -static int eio_finish (eio_req *req); -#define ETP_FINISH(req) eio_finish (req) -static void eio_execute (struct etp_worker *self, eio_req *req); -#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) - /*****************************************************************************/ -#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) - -/* calculate time difference in ~1/EIO_TICKS of a second */ -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); -} - -static unsigned int started, idle, wanted = 4; - -static void (*want_poll_cb) (void); -static void (*done_poll_cb) (void); - -static unsigned int max_poll_time; /* reslock */ -static unsigned int max_poll_reqs; /* reslock */ - -static volatile unsigned int nreqs; /* reqlock */ -static volatile unsigned int nready; /* reqlock */ -static volatile unsigned int npending; /* reqlock */ -static volatile unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ -static volatile unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ +struct etp_tmpbuf; -static xmutex_t wrklock; -static xmutex_t reslock; -static xmutex_t reqlock; -static xcond_t reqwait; - -#if !HAVE_PREADWRITE -/* - * make our pread/pwrite emulation safe against themselves, but not against - * normal read/write by using a mutex. slows down execution a lot, - * but that's your problem, not mine. - */ -static xmutex_t preadwritelock; +#if _POSIX_VERSION >= 200809L + #define HAVE_AT 1 + #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) + #ifndef O_SEARCH + #define O_SEARCH O_RDONLY + #endif +#else + #define HAVE_AT 0 + static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path); #endif -typedef struct etp_worker +struct eio_pwd { - /* locked by wrklock */ - struct etp_worker *prev, *next; - - xthread_t tid; - - /* locked by reslock, reqlock or wrklock */ - ETP_REQ *req; /* currently processed request */ - -#ifdef ETP_WORKER_COMMON - ETP_WORKER_COMMON +#if HAVE_AT + int fd; #endif -} etp_worker; - -static etp_worker wrk_first; /* NOT etp */ - -#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) -#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) - -/* worker threads management */ - -static void ecb_cold -etp_worker_clear (etp_worker *wrk) -{ -} - -static void ecb_cold -etp_worker_free (etp_worker *wrk) -{ - wrk->next->prev = wrk->prev; - wrk->prev->next = wrk->next; - - free (wrk); -} - -static unsigned int -etp_nreqs (void) -{ - int retval; - if (WORDACCESS_UNSAFE) X_LOCK (reqlock); - retval = nreqs; - if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); - return retval; -} + int len; + char str[1]; /* actually, a 0-terminated canonical path */ +}; -static unsigned int -etp_nready (void) -{ - unsigned int retval; - - if (WORDACCESS_UNSAFE) X_LOCK (reqlock); - retval = nready; - if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); - - return retval; -} - -static unsigned int -etp_npending (void) -{ - unsigned int retval; - - if (WORDACCESS_UNSAFE) X_LOCK (reqlock); - retval = npending; - if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); - - return retval; -} - -static unsigned int -etp_nthreads (void) -{ - unsigned int retval; - - if (WORDACCESS_UNSAFE) X_LOCK (reqlock); - retval = started; - if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); - - return retval; -} - -/* - * a somewhat faster data structure might be nice, but - * with 8 priorities this actually needs <20 insns - * per shift, the most expensive operation. - */ -typedef struct { - ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */ - int size; -} etp_reqq; - -static etp_reqq req_queue; -static etp_reqq res_queue; - -static void ecb_noinline ecb_cold -reqq_init (etp_reqq *q) -{ - int pri; - - for (pri = 0; pri < ETP_NUM_PRI; ++pri) - q->qs[pri] = q->qe[pri] = 0; - - q->size = 0; -} - -static int ecb_noinline -reqq_push (etp_reqq *q, ETP_REQ *req) -{ - int pri = req->pri; - req->next = 0; - - if (q->qe[pri]) - { - q->qe[pri]->next = req; - q->qe[pri] = req; - } - else - q->qe[pri] = q->qs[pri] = req; - - return q->size++; -} - -static ETP_REQ * ecb_noinline -reqq_shift (etp_reqq *q) -{ - int pri; - - if (!q->size) - return 0; - - --q->size; - - for (pri = ETP_NUM_PRI; pri--; ) - { - eio_req *req = q->qs[pri]; - - if (req) - { - if (!(q->qs[pri] = (eio_req *)req->next)) - q->qe[pri] = 0; - - return req; - } - } - - abort (); -} - -static int ecb_cold -etp_init (void (*want_poll)(void), void (*done_poll)(void)) -{ - X_MUTEX_CREATE (wrklock); - X_MUTEX_CREATE (reslock); - X_MUTEX_CREATE (reqlock); - X_COND_CREATE (reqwait); - - reqq_init (&req_queue); - reqq_init (&res_queue); - - wrk_first.next = - wrk_first.prev = &wrk_first; - - started = 0; - idle = 0; - nreqs = 0; - nready = 0; - npending = 0; - - want_poll_cb = want_poll; - done_poll_cb = done_poll; - - return 0; -} - -X_THREAD_PROC (etp_proc); - -static void ecb_cold -etp_start_thread (void) -{ - etp_worker *wrk = calloc (1, sizeof (etp_worker)); - - /*TODO*/ - assert (("unable to allocate worker thread data", wrk)); - - X_LOCK (wrklock); - - if (thread_create (&wrk->tid, etp_proc, (void *)wrk)) - { - wrk->prev = &wrk_first; - wrk->next = wrk_first.next; - wrk_first.next->prev = wrk; - wrk_first.next = wrk; - ++started; - } - else - free (wrk); - - X_UNLOCK (wrklock); -} - -static void -etp_maybe_start_thread (void) -{ - if (ecb_expect_true (etp_nthreads () >= wanted)) - return; - - /* todo: maybe use idle here, but might be less exact */ - if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) - return; - - etp_start_thread (); -} - -static void ecb_cold -etp_end_thread (void) -{ - eio_req *req = calloc (1, sizeof (eio_req)); - - req->type = -1; - req->pri = ETP_PRI_MAX - ETP_PRI_MIN; - - X_LOCK (reqlock); - reqq_push (&req_queue, req); - X_COND_SIGNAL (reqwait); - X_UNLOCK (reqlock); - - X_LOCK (wrklock); - --started; - X_UNLOCK (wrklock); -} - -static int -etp_poll (void) -{ - unsigned int maxreqs; - unsigned int maxtime; - struct timeval tv_start, tv_now; - - X_LOCK (reslock); - maxreqs = max_poll_reqs; - maxtime = max_poll_time; - X_UNLOCK (reslock); - - if (maxtime) - gettimeofday (&tv_start, 0); - - for (;;) - { - ETP_REQ *req; - - etp_maybe_start_thread (); - - X_LOCK (reslock); - req = reqq_shift (&res_queue); - - if (req) - { - --npending; - - if (!res_queue.size && done_poll_cb) - done_poll_cb (); - } - - X_UNLOCK (reslock); - - if (!req) - return 0; - - X_LOCK (reqlock); - --nreqs; - X_UNLOCK (reqlock); - - if (ecb_expect_false (req->type == EIO_GROUP && req->size)) - { - req->int1 = 1; /* mark request as delayed */ - continue; - } - else - { - int res = ETP_FINISH (req); - if (ecb_expect_false (res)) - return res; - } - - if (ecb_expect_false (maxreqs && !--maxreqs)) - break; - - if (maxtime) - { - gettimeofday (&tv_now, 0); - - if (tvdiff (&tv_start, &tv_now) >= maxtime) - break; - } - } - - errno = EAGAIN; - return -1; -} - -static void -etp_cancel (ETP_REQ *req) -{ - req->cancelled = 1; - - eio_grp_cancel (req); -} - -static void -etp_submit (ETP_REQ *req) -{ - req->pri -= 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 (ecb_expect_false (req->type == EIO_GROUP)) - { - /* I hope this is worth it :/ */ - X_LOCK (reqlock); - ++nreqs; - X_UNLOCK (reqlock); - - X_LOCK (reslock); - - ++npending; - - if (!reqq_push (&res_queue, req) && want_poll_cb) - want_poll_cb (); - - X_UNLOCK (reslock); - } - else - { - X_LOCK (reqlock); - ++nreqs; - ++nready; - reqq_push (&req_queue, req); - X_COND_SIGNAL (reqwait); - X_UNLOCK (reqlock); - - etp_maybe_start_thread (); - } -} +/*****************************************************************************/ -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); -} +#define ETP_PRI_MIN EIO_PRI_MIN +#define ETP_PRI_MAX EIO_PRI_MAX -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); -} +#define ETP_TYPE_QUIT -1 +#define ETP_TYPE_GROUP EIO_GROUP -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 eio_nop_callback (void) { } +static void (*eio_want_poll_cb)(void) = eio_nop_callback; +static void (*eio_done_poll_cb)(void) = eio_nop_callback; -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); -} +#define ETP_WANT_POLL(pool) eio_want_poll_cb () +#define ETP_DONE_POLL(pool) eio_done_poll_cb () -static void ecb_cold -etp_set_min_parallel (unsigned int nthreads) -{ - if (wanted < nthreads) - wanted = nthreads; -} +struct etp_worker; +#define ETP_REQ eio_req +#define ETP_DESTROY(req) eio_destroy (req) +static int eio_finish (eio_req *req); +#define ETP_FINISH(req) eio_finish (req) +static void eio_execute (struct etp_worker *self, eio_req *req); +#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) -static void ecb_cold -etp_set_max_parallel (unsigned int nthreads) -{ - if (wanted > nthreads) - wanted = nthreads; +#include "etp.c" - while (started > wanted) - etp_end_thread (); -} +static struct etp_pool eio_pool; +#define EIO_POOL (&eio_pool) /*****************************************************************************/ @@ -745,12 +408,12 @@ { while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) { - grp->flags &= ~EIO_FLAG_GROUPADD; + grp->flags &= ~ETP_FLAG_GROUPADD; EIO_FEED (grp); /* stop if no progress has been made */ - if (!(grp->flags & EIO_FLAG_GROUPADD)) + if (!(grp->flags & ETP_FLAG_GROUPADD)) { grp->feed = 0; break; @@ -767,7 +430,7 @@ grp_try_feed (grp); /* finish, if done */ - if (!grp->size && grp->int1) + if (!grp->size && grp->flags & ETP_FLAG_DELAYED) return eio_finish (grp); else return 0; @@ -813,128 +476,103 @@ void eio_grp_cancel (eio_req *grp) { - for (grp = grp->grp_first; grp; grp = grp->grp_next) - eio_cancel (grp); + etp_grp_cancel (EIO_POOL, grp); } void eio_cancel (eio_req *req) { - etp_cancel (req); + etp_cancel (EIO_POOL, req); } void eio_submit (eio_req *req) { - etp_submit (req); + etp_submit (EIO_POOL, req); } unsigned int eio_nreqs (void) { - return etp_nreqs (); + return etp_nreqs (EIO_POOL); } unsigned int eio_nready (void) { - return etp_nready (); + return etp_nready (EIO_POOL); } unsigned int eio_npending (void) { - return etp_npending (); + return etp_npending (EIO_POOL); } unsigned int ecb_cold eio_nthreads (void) { - return etp_nthreads (); + return etp_nthreads (EIO_POOL); } void ecb_cold eio_set_max_poll_time (double nseconds) { - etp_set_max_poll_time (nseconds); + etp_set_max_poll_time (EIO_POOL, nseconds); } void ecb_cold eio_set_max_poll_reqs (unsigned int maxreqs) { - etp_set_max_poll_reqs (maxreqs); + etp_set_max_poll_reqs (EIO_POOL, maxreqs); } void ecb_cold eio_set_max_idle (unsigned int nthreads) { - etp_set_max_idle (nthreads); + etp_set_max_idle (EIO_POOL, nthreads); } void ecb_cold eio_set_idle_timeout (unsigned int seconds) { - etp_set_idle_timeout (seconds); + etp_set_idle_timeout (EIO_POOL, seconds); } void ecb_cold eio_set_min_parallel (unsigned int nthreads) { - etp_set_min_parallel (nthreads); + etp_set_min_parallel (EIO_POOL, nthreads); } void ecb_cold eio_set_max_parallel (unsigned int nthreads) { - etp_set_max_parallel (nthreads); + etp_set_max_parallel (EIO_POOL, nthreads); } int eio_poll (void) { - return etp_poll (); + return etp_poll (EIO_POOL); } /*****************************************************************************/ /* work around various missing functions */ -#if !HAVE_PREADWRITE -# undef pread -# undef pwrite -# define pread eio__pread -# define pwrite eio__pwrite - -static eio_ssize_t -eio__pread (int fd, void *buf, size_t count, off_t offset) -{ - eio_ssize_t res; - off_t ooffset; - - X_LOCK (preadwritelock); - ooffset = lseek (fd, 0, SEEK_CUR); - lseek (fd, offset, SEEK_SET); - res = read (fd, buf, count); - lseek (fd, ooffset, SEEK_SET); - X_UNLOCK (preadwritelock); - - return res; -} +#if HAVE_POSIX_CLOSE && !__linux +# define eio__close(fd) posix_close (fd, 0) +#else +# define eio__close(fd) close (fd) +#endif -static eio_ssize_t -eio__pwrite (int fd, void *buf, size_t count, off_t offset) +/* close() without disturbing errno */ +static void +silent_close (int fd) { - eio_ssize_t res; - off_t ooffset; - - X_LOCK (preadwritelock); - ooffset = lseek (fd, 0, SEEK_CUR); - lseek (fd, offset, SEEK_SET); - res = write (fd, buf, count); - lseek (fd, ooffset, SEEK_SET); - X_UNLOCK (preadwritelock); - - return res; + int saved_errno = errno; + eio__close (fd); + errno = saved_errno; } -#endif #ifndef HAVE_UTIMES @@ -986,8 +624,7 @@ #if HAVE_SYS_SYNCFS res = (int)syscall (__NR_syncfs, (int)(fd)); #else - res = -1; - errno = ENOSYS; + res = EIO_ENOSYS (); #endif if (res < 0 && errno == ENOSYS && fd >= 0) @@ -1027,11 +664,10 @@ static int eio__fallocate (int fd, int mode, off_t offset, size_t len) { -#if HAVE_FALLOCATE +#if HAVE_LINUX_FALLOCATE return fallocate (fd, mode, offset, len); #else - errno = ENOSYS; - return -1; + return EIO_ENOSYS (); #endif } @@ -1056,8 +692,9 @@ FUBd; - errno = 0; - return count; + /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */ + /* but not for e.g. EIO or eof, so we also never fail */ + return 0; } #endif @@ -1102,7 +739,7 @@ if (sbytes) res = sbytes; -# elif defined (__APPLE__) +# elif defined __APPLE__ off_t sbytes = count; res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); @@ -1139,8 +776,7 @@ res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); #else - res = -1; - errno = ENOSYS; + res = EIO_ENOSYS (); #endif /* we assume sendfile can copy at least 128mb in one go */ @@ -1325,9 +961,9 @@ if (addr < end) if (flags & EIO_MT_MODIFY) /* modify */ - do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); + do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < end && !EIO_CANCELLED (req)); else - do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); + do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < end && !EIO_CANCELLED (req)); } return 0; @@ -1337,10 +973,25 @@ /* requests implemented outside eio_execute, because they are so large */ static void -eio__realpath (eio_req *req, etp_worker *self) +eio__lseek (eio_req *req) +{ + /* this usually gets optimised away completely, or your compiler sucks, */ + /* or the whence constants really are not 0, 1, 2 */ + int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET + : req->int2 == EIO_SEEK_CUR ? SEEK_CUR + : req->int2 == EIO_SEEK_END ? SEEK_END + : req->int2; + + req->offs = lseek (req->int1, req->offs, whence); + req->result = req->offs == (off_t)-1 ? -1 : 0; +} + +/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ +static int +eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) { - char *rel = req->ptr1; char *res; + const char *rel = path; char *tmp1, *tmp2; #if SYMLOOP_MAX > 32 int symlinks = SYMLOOP_MAX; @@ -1348,31 +999,34 @@ int symlinks = 32; #endif - req->result = -1; - errno = EINVAL; if (!rel) - return; + return -1; errno = ENOENT; if (!*rel) - return; + return -1; - 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 = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3); +#ifdef _WIN32 + if (_access (rel, 4) != 0) + return -1; - res = req->ptr2; - tmp1 = res + PATH_MAX; - tmp2 = tmp1 + PATH_MAX; + symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0); + + errno = ENAMETOOLONG; + if (symlinks >= EIO_PATH_MAX * 3) + return -1; + + errno = EIO; + if (symlinks <= 0) + return -1; + + return symlinks; + +#else + tmp1 = res + EIO_PATH_MAX; + tmp2 = tmp1 + EIO_PATH_MAX; #if 0 /* disabled, the musl way to do things is just too racy */ #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) @@ -1383,33 +1037,45 @@ if (fd >= 0) { sprintf (tmp1, "/proc/self/fd/%d", fd); - req->result = readlink (tmp1, res, PATH_MAX); - close (fd); - + req->result = readlink (tmp1, res, EIO_PATH_MAX); /* here we should probably stat the open file and the disk file, to make sure they still match */ + eio__close (fd); if (req->result > 0) goto done; } else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) - return; + return -1; } #endif #endif if (*rel != '/') { - if (!getcwd (res, PATH_MAX)) - return; + int len; + + errno = ENOENT; + if (wd == EIO_INVALID_WD) + return -1; + + if (wd == EIO_CWD) + { + if (!getcwd (res, EIO_PATH_MAX)) + return -1; + + len = strlen (res); + } + else + memcpy (res, wd->str, len = wd->len); if (res [1]) /* only use if not / */ - res += strlen (res); + res += len; } while (*rel) { eio_ssize_t len, linklen; - char *beg = rel; + const char *beg = rel; while (*rel && *rel != '/') ++rel; @@ -1431,7 +1097,7 @@ { /* .. - back up one component, if possible */ - while (res != req->ptr2) + while (res != tmpbuf->ptr) if (*--res == '/') break; @@ -1441,7 +1107,7 @@ errno = ENAMETOOLONG; if (res + 1 + len + 1 >= tmp1) - return; + return -1; /* copy one component */ *res = '/'; @@ -1451,12 +1117,12 @@ res [len + 1] = 0; /* now check if it's a symlink */ - linklen = readlink (req->ptr2, tmp1, PATH_MAX); + linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX); if (linklen < 0) { if (errno != EINVAL) - return; + return -1; /* it's a normal directory. hopefully */ res += len + 1; @@ -1467,15 +1133,15 @@ int rellen = strlen (rel); errno = ENAMETOOLONG; - if (linklen + 1 + rellen >= PATH_MAX) - return; + if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */ + return -1; errno = ELOOP; if (!--symlinks) - return; + return -1; if (*tmp1 == '/') - res = req->ptr2; /* symlink resolves to an absolute path */ + res = tmpbuf->ptr; /* 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); @@ -1487,13 +1153,11 @@ } /* special case for the lone root path */ - if (res == req->ptr2) + if (res == tmpbuf->ptr) *res++ = '/'; - req->result = res - (char *)req->ptr2; - -done: - req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */ + return res - (char *)tmpbuf->ptr; +#endif } static signed char @@ -1691,47 +1355,68 @@ int len = strlen ((const char *)req->ptr1); char *path = malloc (MAX_PATH); const char *fmt; + const char *reqpath = wd_expand (&self->tmpbuf, req->wd, req->ptr1); if (!len) fmt = "./*"; - else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\') + else if (reqpath[len - 1] == '/' || reqpath[len - 1] == '\\') fmt = "%s*"; else fmt = "%s/*"; - _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1); + _snprintf (path, MAX_PATH, fmt, reqpath); dirp = FindFirstFile (path, &entp); free (path); if (dirp == INVALID_HANDLE_VALUE) { - dirp = 0; + /* should steal _dosmaperr */ + switch (GetLastError ()) + { + case ERROR_FILE_NOT_FOUND: + req->result = 0; + break; + + case ERROR_INVALID_NAME: + case ERROR_PATH_NOT_FOUND: + case ERROR_NO_MORE_FILES: + errno = ENOENT; + break; + + case ERROR_NOT_ENOUGH_MEMORY: + errno = ENOMEM; + break; + + default: + errno = EINVAL; + break; + } - /* should steal _dosmaperr */ - switch (GetLastError ()) - { - case ERROR_FILE_NOT_FOUND: - req->result = 0; - break; - - case ERROR_INVALID_NAME: - case ERROR_PATH_NOT_FOUND: - case ERROR_NO_MORE_FILES: - errno = ENOENT; - break; - - case ERROR_NOT_ENOUGH_MEMORY: - errno = ENOMEM; - break; - - default: - errno = EINVAL; - break; - } + return; } } #else - dirp = opendir (req->ptr1); + #if HAVE_AT + if (req->wd) + { + int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY); + + if (fd < 0) + return; + + dirp = fdopendir (fd); + + if (!dirp) + silent_close (fd); + } + else + dirp = opendir (req->ptr1); + #else + dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); + #endif + + if (!dirp) + return; #endif if (req->flags & EIO_FLAG_PTR1_FREE) @@ -1741,303 +1426,383 @@ req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; req->ptr2 = names = malloc (namesalloc); - if (dirp && names && (!flags || dents)) - for (;;) - { - int done; + if (!names || (flags && !dents)) + return; + + for (;;) + { + int done; #ifdef _WIN32 - done = !dirp; + done = !dirp; #else - errno = 0; - entp = readdir (dirp); - done = !entp; + errno = 0; + entp = readdir (dirp); + done = !entp; #endif - if (done) - { + if (done) + { #ifndef _WIN32 - int old_errno = errno; - closedir (dirp); - errno = old_errno; + int old_errno = errno; + closedir (dirp); + errno = old_errno; - if (errno) - break; + if (errno) + break; #endif - /* sort etc. */ - req->int1 = flags; - req->result = dentoffs; - - if (flags & EIO_READDIR_STAT_ORDER) - eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits); - else if (flags & EIO_READDIR_DIRS_FIRST) - if (flags & EIO_READDIR_FOUND_UNKNOWN) - eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ - else - { - /* in this case, all is known, and we just put dirs first and sort them */ - eio_dirent *oth = dents + dentoffs; - eio_dirent *dir = dents; - - /* now partition dirs to the front, and non-dirs to the back */ - /* by walking from both sides and swapping if necessary */ - while (oth > dir) - { - if (dir->type == EIO_DT_DIR) - ++dir; - else if ((--oth)->type == EIO_DT_DIR) - { - eio_dirent tmp = *dir; *dir = *oth; *oth = tmp; + /* sort etc. */ + req->int1 = flags; + req->result = dentoffs; + + if (flags & EIO_READDIR_STAT_ORDER) + eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits); + else if (flags & EIO_READDIR_DIRS_FIRST) + if (flags & EIO_READDIR_FOUND_UNKNOWN) + eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ + else + { + /* in this case, all is known, and we just put dirs first and sort them */ + eio_dirent *oth = dents + dentoffs; + eio_dirent *dir = dents; + + /* now partition dirs to the front, and non-dirs to the back */ + /* by walking from both sides and swapping if necessary */ + while (oth > dir) + { + if (dir->type == EIO_DT_DIR) + ++dir; + else if ((--oth)->type == EIO_DT_DIR) + { + eio_dirent tmp = *dir; *dir = *oth; *oth = tmp; - ++dir; - } - } + ++dir; + } + } - /* now sort the dirs only (dirs all have the same score) */ - eio_dent_sort (dents, dir - dents, 0, inode_bits); - } + /* now sort the dirs only (dirs all have the same score) */ + eio_dent_sort (dents, dir - dents, 0, inode_bits); + } - break; - } + break; + } - /* now add the entry to our list(s) */ - name = D_NAME (entp); + /* now add the entry to our list(s) */ + name = D_NAME (entp); - /* skip . and .. entries */ - if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) - { - int len = D_NAMLEN (entp) + 1; + /* skip . and .. entries */ + if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) + { + int len = D_NAMLEN (entp) + 1; - while (ecb_expect_false (namesoffs + len > namesalloc)) - { - namesalloc *= 2; - req->ptr2 = names = realloc (names, namesalloc); + while (ecb_expect_false (namesoffs + len > namesalloc)) + { + namesalloc *= 2; + req->ptr2 = names = realloc (names, namesalloc); - if (!names) - break; - } + if (!names) + break; + } - memcpy (names + namesoffs, name, len); + memcpy (names + namesoffs, name, len); - if (dents) - { - struct eio_dirent *ent; + if (dents) + { + struct eio_dirent *ent; - if (ecb_expect_false (dentoffs == dentalloc)) - { - dentalloc *= 2; - req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); + if (ecb_expect_false (dentoffs == dentalloc)) + { + dentalloc *= 2; + req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); - if (!dents) - break; - } + if (!dents) + break; + } - ent = dents + dentoffs; + ent = dents + dentoffs; - ent->nameofs = namesoffs; /* rather dirtily we store the offset in the pointer */ - ent->namelen = len - 1; - ent->inode = D_INO (entp); + ent->nameofs = namesoffs; /* rather dirtily we store the offset in the pointer */ + ent->namelen = len - 1; + ent->inode = D_INO (entp); - inode_bits |= ent->inode; + inode_bits |= ent->inode; - switch (D_TYPE (entp)) - { - default: - ent->type = EIO_DT_UNKNOWN; - flags |= EIO_READDIR_FOUND_UNKNOWN; - break; - - #ifdef DT_FIFO - case DT_FIFO: ent->type = EIO_DT_FIFO; break; - #endif - #ifdef DT_CHR - case DT_CHR: ent->type = EIO_DT_CHR; break; - #endif - #ifdef DT_MPC - case DT_MPC: ent->type = EIO_DT_MPC; break; - #endif - #ifdef DT_DIR - case DT_DIR: ent->type = EIO_DT_DIR; break; - #endif - #ifdef DT_NAM - case DT_NAM: ent->type = EIO_DT_NAM; break; - #endif - #ifdef DT_BLK - case DT_BLK: ent->type = EIO_DT_BLK; break; - #endif - #ifdef DT_MPB - case DT_MPB: ent->type = EIO_DT_MPB; break; - #endif - #ifdef DT_REG - case DT_REG: ent->type = EIO_DT_REG; break; - #endif - #ifdef DT_NWK - case DT_NWK: ent->type = EIO_DT_NWK; break; - #endif - #ifdef DT_CMP - case DT_CMP: ent->type = EIO_DT_CMP; break; - #endif - #ifdef DT_LNK - case DT_LNK: ent->type = EIO_DT_LNK; break; - #endif - #ifdef DT_SOCK - case DT_SOCK: ent->type = EIO_DT_SOCK; break; - #endif - #ifdef DT_DOOR - case DT_DOOR: ent->type = EIO_DT_DOOR; break; - #endif - #ifdef DT_WHT - case DT_WHT: ent->type = EIO_DT_WHT; break; - #endif - } + switch (D_TYPE (entp)) + { + default: + ent->type = EIO_DT_UNKNOWN; + flags |= EIO_READDIR_FOUND_UNKNOWN; + break; + + #ifdef DT_FIFO + case DT_FIFO: ent->type = EIO_DT_FIFO; break; + #endif + #ifdef DT_CHR + case DT_CHR: ent->type = EIO_DT_CHR; break; + #endif + #ifdef DT_MPC + case DT_MPC: ent->type = EIO_DT_MPC; break; + #endif + #ifdef DT_DIR + case DT_DIR: ent->type = EIO_DT_DIR; break; + #endif + #ifdef DT_NAM + case DT_NAM: ent->type = EIO_DT_NAM; break; + #endif + #ifdef DT_BLK + case DT_BLK: ent->type = EIO_DT_BLK; break; + #endif + #ifdef DT_MPB + case DT_MPB: ent->type = EIO_DT_MPB; break; + #endif + #ifdef DT_REG + case DT_REG: ent->type = EIO_DT_REG; break; + #endif + #ifdef DT_NWK + case DT_NWK: ent->type = EIO_DT_NWK; break; + #endif + #ifdef DT_CMP + case DT_CMP: ent->type = EIO_DT_CMP; break; + #endif + #ifdef DT_LNK + case DT_LNK: ent->type = EIO_DT_LNK; break; + #endif + #ifdef DT_SOCK + case DT_SOCK: ent->type = EIO_DT_SOCK; break; + #endif + #ifdef DT_DOOR + case DT_DOOR: ent->type = EIO_DT_DOOR; break; + #endif + #ifdef DT_WHT + case DT_WHT: ent->type = EIO_DT_WHT; break; + #endif + } - ent->score = 7; + ent->score = 7; - if (flags & EIO_READDIR_DIRS_FIRST) - { - if (ent->type == EIO_DT_UNKNOWN) - { - if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ - ent->score = 1; - else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ - ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ - } - else if (ent->type == EIO_DT_DIR) - ent->score = 0; - } - } + if (flags & EIO_READDIR_DIRS_FIRST) + { + if (ent->type == EIO_DT_UNKNOWN) + { + if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ + ent->score = 1; + else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */ + ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ + } + else if (ent->type == EIO_DT_DIR) + ent->score = 0; + } + } - namesoffs += len; - ++dentoffs; - } + namesoffs += len; + ++dentoffs; + } - if (EIO_CANCELLED (req)) - { - errno = ECANCELED; - break; - } + if (EIO_CANCELLED (req)) + { + errno = ECANCELED; + break; + } #ifdef _WIN32 - if (!FindNextFile (dirp, &entp)) - { - FindClose (dirp); - dirp = 0; - } + if (!FindNextFile (dirp, &entp)) + { + FindClose (dirp); + dirp = 0; + } #endif - } + } } /*****************************************************************************/ +/* working directory stuff */ +/* various deficiencies in the posix 2008 api force us to */ +/* keep the absolute path in string form at all times */ +/* fuck yeah. */ + +#if !HAVE_AT + +/* a bit like realpath, but usually faster because it doesn'T have to return */ +/* an absolute or canonical path */ +static const char * +wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) +{ + if (!wd || *path == '/') + return path; -#define ALLOC(len) \ - if (!req->ptr2) \ - { \ - X_LOCK (wrklock); \ - req->flags |= EIO_FLAG_PTR2_FREE; \ - X_UNLOCK (wrklock); \ - req->ptr2 = malloc (len); \ - if (!req->ptr2) \ - { \ - errno = ENOMEM; \ - req->result = -1; \ - break; \ - } \ - } + if (path [0] == '.' && !path [1]) + return wd->str; + + { + int l1 = wd->len; + int l2 = strlen (path); + + char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2); + + memcpy (res, wd->str, l1); + res [l1] = '/'; + memcpy (res + l1 + 1, path, l2 + 1); + + return res; + } +} + +#endif -X_THREAD_PROC (etp_proc) +static eio_wd +eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) { - ETP_REQ *req; - struct timespec ts; - etp_worker *self = (etp_worker *)thr_arg; + int fd; + eio_wd res; + int len = eio__realpath (tmpbuf, wd, path); -#if HAVE_PRCTL_SET_NAME - prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0); + if (len < 0) + return EIO_INVALID_WD; + +#if HAVE_AT + fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); + + if (fd < 0) + return EIO_INVALID_WD; #endif - /* try to distribute timeouts somewhat evenly */ - ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); + res = malloc (sizeof (*res) + len); /* one extra 0-byte */ - for (;;) +#if HAVE_AT + res->fd = fd; +#endif + + res->len = len; + memcpy (res->str, tmpbuf->ptr, len); + res->str [len] = 0; + + return res; +} + +eio_wd +eio_wd_open_sync (eio_wd wd, const char *path) +{ + struct etp_tmpbuf tmpbuf = { }; + wd = eio__wd_open_sync (&tmpbuf, wd, path); + free (tmpbuf.ptr); + + return wd; +} + +void +eio_wd_close_sync (eio_wd wd) +{ + if (wd != EIO_INVALID_WD && wd != EIO_CWD) { - ts.tv_sec = 0; + #if HAVE_AT + eio__close (wd->fd); + #endif + free (wd); + } +} - X_LOCK (reqlock); +#if HAVE_AT - for (;;) - { - self->req = req = reqq_shift (&req_queue); +static int +eio__renameat2 (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags) +{ +#if HAVE_RENAMEAT2 + return syscall (SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags); +#else + if (flags) + return EIO_ENOSYS (); - if (req) - break; + return renameat (olddirfd, oldpath, newdirfd, newpath); +#endif +} - 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; - } +/* they forgot these */ - ++idle; +static int +eio__truncateat (int dirfd, const char *path, off_t length) +{ + int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); + int res; - if (idle <= max_idle) - /* we are allowed to idle, so do so without any timeout */ - X_COND_WAIT (reqwait, reqlock); - else - { - /* initialise timeout once */ - if (!ts.tv_sec) - ts.tv_sec = time (0) + idle_timeout; + if (fd < 0) + return fd; - if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) - ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ - } + res = ftruncate (fd, length); + silent_close (fd); + return res; +} - --idle; - } +static int +eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) +{ + int fd = openat (dirfd, path, O_SEARCH | O_CLOEXEC); + int res; - --nready; + if (fd < 0) + return fd; - X_UNLOCK (reqlock); - - if (req->type < 0) - goto quit; + res = fstatvfs (fd, buf); + silent_close (fd); + return res; +} - ETP_EXECUTE (self, req); +#endif - X_LOCK (reslock); +/*****************************************************************************/ - ++npending; +#define ALLOC(len) \ + if (!req->ptr2) \ + { \ + X_LOCK (EIO_POOL->wrklock); \ + req->flags |= EIO_FLAG_PTR2_FREE; \ + X_UNLOCK (EIO_POOL->wrklock); \ + req->ptr2 = malloc (len); \ + if (!req->ptr2) \ + { \ + errno = ENOMEM; \ + req->result = -1; \ + goto alloc_fail; \ + } \ + } + +/*****************************************************************************/ - if (!reqq_push (&res_queue, req) && want_poll_cb) - want_poll_cb (); +static void +eio__slurp (int fd, eio_req *req) +{ + req->result = fd; + + if (fd < 0) + return; + + if (req->offs < 0 || !req->size) /* do we need the size? */ + { + off_t size = lseek (fd, 0, SEEK_END); - self->req = 0; - etp_worker_clear (self); + if (req->offs < 0) + req->offs += size; - X_UNLOCK (reslock); + if (!req->size) + req->size = size - req->offs; } -quit: - X_LOCK (wrklock); - etp_worker_free (self); - X_UNLOCK (wrklock); + ALLOC (req->size); + req->result = pread (fd, req->ptr2, req->size, req->offs); - return 0; -} + silent_close (fd); -/*****************************************************************************/ +alloc_fail: + ; +} int ecb_cold eio_init (void (*want_poll)(void), void (*done_poll)(void)) { -#if !HAVE_PREADWRITE - X_MUTEX_CREATE (preadwritelock); -#endif + eio_want_poll_cb = want_poll; + eio_done_poll_cb = done_poll; - return etp_init (want_poll, done_poll); + return etp_init (EIO_POOL, 0, 0, 0); } ecb_inline void @@ -2046,7 +1811,7 @@ free (req); } -#define REQ(rtype) \ +#define REQ(rtype) \ eio_req *req; \ \ req = (eio_req *)calloc (1, sizeof *req); \ @@ -2070,9 +1835,17 @@ return 0; \ } +#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)]) + static void eio_execute (etp_worker *self, eio_req *req) { +#if HAVE_AT + int dirfd; +#else + const char *path; +#endif + if (ecb_expect_false (EIO_CANCELLED (req))) { req->result = -1; @@ -2080,8 +1853,31 @@ return; } + if (ecb_expect_false (req->wd == EIO_INVALID_WD)) + { + req->result = -1; + req->errorno = ENOENT; + return; + } + + if (req->type >= EIO_OPEN) + { + #if HAVE_AT + dirfd = WD2FD (req->wd); + #else + path = wd_expand (&self->tmpbuf, req->wd, req->ptr1); + #endif + } + switch (req->type) { + case EIO_WD_OPEN: req->wd = eio__wd_open_sync (&self->tmpbuf, req->wd, req->ptr1); + req->result = req->wd == EIO_INVALID_WD ? -1 : 0; + break; + case EIO_WD_CLOSE: req->result = 0; + eio_wd_close_sync (req->wd); break; + + case EIO_SEEK: eio__lseek (req); break; case EIO_READ: ALLOC (req->size); req->result = req->offs >= 0 ? pread (req->int1, req->ptr2, req->size, req->offs) @@ -2090,44 +1886,151 @@ ? pwrite (req->int1, req->ptr2, req->size, req->offs) : write (req->int1, req->ptr2, req->size); break; + case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break; + case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break; + case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; +#if HAVE_AT + case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); - req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; + req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, 0); break; case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); - req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; + req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; + case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; + case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; + case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; + case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; + case EIO_SLURP: eio__slurp ( openat (dirfd, req->ptr1, O_RDONLY | O_CLOEXEC), req); break; + + case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; + case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */ + req->result = req->wd && SINGLEDOT (req->ptr1) + ? rmdir (req->wd->str) + : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; + case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; + case EIO_RENAME: req->result = eio__renameat2 ( + dirfd, + /* complications arise because "." cannot be renamed, so we might have to expand */ + req->wd && SINGLEDOT (req->ptr1) ? req->wd->str : req->ptr1, + WD2FD ((eio_wd)req->int3), + req->ptr2, + req->int2 + ); + break; + case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; + case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; + case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; + case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); + req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; + case EIO_READLINK: ALLOC (EIO_PATH_MAX); + req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX); + if (req->result == EIO_PATH_MAX) + { + req->result = -1; + errno = ENAMETOOLONG; + } + break; + case EIO_UTIME: + case EIO_FUTIME: + { + struct timespec ts[2]; + struct timespec *times; + + if (req->nv1 != -1. || req->nv2 != -1.) + { + ts[0].tv_sec = req->nv1; + ts[0].tv_nsec = (req->nv1 - ts[0].tv_sec) * 1e9; + ts[1].tv_sec = req->nv2; + ts[1].tv_nsec = (req->nv2 - ts[1].tv_sec) * 1e9; + + times = ts; + } + else + times = 0; + + req->result = req->type == EIO_FUTIME + ? futimens (req->int1, times) + : utimensat (dirfd, req->ptr1, times, 0); + } + break; + +#else + + case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); + req->result = stat (path , (EIO_STRUCT_STAT *)req->ptr2); break; + case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); + req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; + case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; + case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break; + case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; + case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; + case EIO_SLURP: eio__slurp ( open (path , O_RDONLY | O_CLOEXEC), req); break; + + case EIO_UNLINK: req->result = unlink (path ); break; + case EIO_RMDIR: req->result = rmdir (path ); break; + case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; + case EIO_RENAME: req->result = req->int2 ? EIO_ENOSYS () : rename (path, req->ptr2); break; + case EIO_LINK: req->result = link (path , req->ptr2); break; + case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; + case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; + case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); + req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; + case EIO_READLINK: ALLOC (EIO_PATH_MAX); + req->result = readlink (path, req->ptr2, EIO_PATH_MAX); + if (req->result == EIO_PATH_MAX) + { + req->result = -1; + errno = ENAMETOOLONG; + } + break; + + case EIO_UTIME: + case EIO_FUTIME: + { + struct timeval tv[2]; + struct timeval *times; + + if (req->nv1 != -1. || req->nv2 != -1.) + { + tv[0].tv_sec = req->nv1; + tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1e6; + tv[1].tv_sec = req->nv2; + tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1e6; + + times = tv; + } + else + times = 0; + + req->result = req->type == EIO_FUTIME + ? futimes (req->int1, times) + : utimes (req->ptr1, times); + } + break; + +#endif + + case EIO_REALPATH: if (0 <= (req->result = eio__realpath (&self->tmpbuf, req->wd, req->ptr1))) + { + ALLOC (req->result); + memcpy (req->ptr2, self->tmpbuf.ptr, req->result); + } + break; + case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; - case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); - req->result = statvfs (req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; case EIO_FSTATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); req->result = fstatvfs (req->int1, (EIO_STRUCT_STATVFS *)req->ptr2); break; - case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; - case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break; case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; - case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; - case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break; - case EIO_CLOSE: req->result = close (req->int1); break; + case EIO_CLOSE: req->result = eio__close (req->int1); break; case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; - case EIO_UNLINK: req->result = unlink (req->ptr1); break; - case EIO_RMDIR: req->result = rmdir (req->ptr1); break; - case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; - case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; - case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; - 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; - case EIO_SYNC: req->result = 0; sync (); break; case EIO_FSYNC: req->result = fsync (req->int1); break; case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; @@ -2156,32 +2059,10 @@ #endif break; - case EIO_UTIME: - case EIO_FUTIME: - { - struct timeval tv[2]; - struct timeval *times; - - if (req->nv1 != -1. || req->nv2 != -1.) - { - tv[0].tv_sec = req->nv1; - tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; - tv[1].tv_sec = req->nv2; - tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; - - times = tv; - } - else - times = 0; - - req->result = req->type == EIO_FUTIME - ? futimes (req->int1, times) - : utimes (req->ptr1, times); - } - break; - +#if 0 case EIO_GROUP: abort (); /* handled in eio_request */ +#endif case EIO_NOP: req->result = 0; @@ -2192,16 +2073,26 @@ break; default: - errno = ENOSYS; - req->result = -1; + req->result = EIO_ENOSYS (); break; } +alloc_fail: req->errorno = errno; } #ifndef EIO_NO_WRAPPERS +eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data) +{ + REQ (EIO_WD_OPEN); PATH; SEND; +} + +eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_WD_CLOSE); req->wd = wd; SEND; +} + eio_req *eio_nop (int pri, eio_cb cb, void *data) { REQ (EIO_NOP); SEND; @@ -2272,6 +2163,11 @@ REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; } +eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND; +} + eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) { REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; @@ -2282,6 +2178,16 @@ REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; } +eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data) +{ + REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND; +} + +eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data) +{ + REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND; +} + eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) { REQ (EIO_FSTAT); req->int1 = fd; SEND; @@ -2434,6 +2340,11 @@ return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); } +eio_req *eio_slurp (const char *path, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SLURP); PATH; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; +} + eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) { REQ (EIO_CUSTOM); req->feed = execute; SEND; @@ -2477,7 +2388,7 @@ { assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); - grp->flags |= EIO_FLAG_GROUPADD; + grp->flags |= ETP_FLAG_GROUPADD; ++grp->size; req->grp = grp;