--- libeio/eio.c 2011/09/26 18:23:18 1.105 +++ libeio/eio.c 2012/04/24 18:47:50 1.120 @@ -1,7 +1,7 @@ /* * libeio implementation * - * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann + * Copyright (c) 2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann * All rights reserved. * * Redistribution and use in source and binary forms, with or without modifica- @@ -210,7 +210,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 @@ -314,6 +314,28 @@ return buf->ptr; } +struct tmpbuf; + +#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 tmpbuf *tmpbuf, eio_wd wd, const char *path); +#endif + +struct eio_pwd +{ +#if HAVE_AT + int fd; +#endif + int len; + char str[1]; /* actually, a 0-terminated canonical path */ +}; + /*****************************************************************************/ #define ETP_PRI_MIN EIO_PRI_MIN @@ -389,7 +411,7 @@ /* worker threads management */ -static void ecb_cold +static void etp_worker_clear (etp_worker *wrk) { } @@ -1012,8 +1034,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) @@ -1053,11 +1074,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 } @@ -1082,8 +1102,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 @@ -1128,7 +1149,7 @@ if (sbytes) res = sbytes; -# elif defined (__APPLE__) +# elif defined __APPLE__ off_t sbytes = count; res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); @@ -1165,8 +1186,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 */ @@ -1362,19 +1382,18 @@ /*****************************************************************************/ /* requests implemented outside eio_execute, because they are so large */ -/* copies some absolute path to tmpbuf */ -static char * -eio__getwd (struct tmpbuf *tmpbuf, eio_wd wd) +static void +eio__lseek (eio_req *req) { - if (wd == EIO_CWD) - return getcwd (tmpbuf->ptr, PATH_MAX); + /* 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; -#if HAVE_AT - abort (); /*TODO*/ -#else - strcpy (tmpbuf->ptr, wd); -#endif - return tmpbuf->ptr; + 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 */ @@ -1390,8 +1409,6 @@ int symlinks = 32; #endif - /*D*/ /*TODO: wd ignored */ - errno = EINVAL; if (!rel) return -1; @@ -1429,11 +1446,24 @@ if (*rel != '/') { - if (!eio__getwd (tmpbuf, wd)) + int len; + + errno = ENOENT; + if (wd == EIO_INVALID_WD) return -1; + + if (wd == EIO_CWD) + { + if (!getcwd (res, 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) @@ -1471,7 +1501,7 @@ errno = ENAMETOOLONG; if (res + 1 + len + 1 >= tmp1) - return; + return -1; /* copy one component */ *res = '/'; @@ -1690,7 +1720,7 @@ /* read a full directory */ static void -eio__scandir (eio_req *req) +eio__scandir (eio_req *req, etp_worker *self) { char *name, *names; int namesalloc = 4096 - sizeof (void *) * 4; @@ -1718,15 +1748,16 @@ 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); @@ -1758,7 +1789,24 @@ } } #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) + close (fd); + } + else + dirp = opendir (req->ptr1); + #else + dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); + #endif if (!dirp) return; @@ -1932,7 +1980,7 @@ { 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 */ + 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) @@ -1962,38 +2010,95 @@ /*****************************************************************************/ /* 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 +#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 tmpbuf *tmpbuf, eio_wd wd, const char *path) +{ + if (!wd || *path == '/') + return path; -#define WD2FD(wd) ((wd) ? ((int)(long)(wd)) - 1 : AT_FDCWD) + if (path [0] == '.' && !path [1]) + return wd->str; -#ifndef O_SEARCH -# define O_SEARCH O_RDONLY -#endif + { + int l1 = wd->len; + int l2 = strlen (path); -eio_wd -eio_wd_open_sync (eio_wd wd, const char *path) -{ - int fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); + char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); + + memcpy (res, wd->str, l1); + res [l1] = '/'; + memcpy (res + l1 + 1, path, l2 + 1); - return fd >= 0 ? (eio_wd)(long)(fd + 1) : EIO_INVALID_WD; + return res; + } } +#endif + static eio_wd eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) { - return eio_wd_open_sync (wd, path); + int fd; + eio_wd res; + int len = eio__realpath (tmpbuf, wd, path); + + 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 + + res = malloc (sizeof (*res) + len); /* one extra 0-byte */ + +#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 tmpbuf tmpbuf = { 0 }; + wd = eio__wd_open_sync (&tmpbuf, wd, path); + free (tmpbuf.ptr); + + return wd; } void eio_wd_close_sync (eio_wd wd) { - int fd = WD2FD (wd); - - if (fd >= 0) - close (fd); + if (wd != EIO_INVALID_WD && wd != EIO_CWD) + { + #if HAVE_AT + close (wd->fd); + #endif + free (wd); + } } +#if HAVE_AT + +/* they forgot these */ + static int eio__truncateat (int dirfd, const char *path, off_t length) { @@ -2023,73 +2128,6 @@ } -#else - -/* on legacy systems, we represent the working directories simply by their path strings */ - -static const char * -wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) -{ - if (!wd || *path == '/') - return path; - - { - int l1 = strlen ((const char *)wd); - int l2 = strlen (path); - - char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); - - memcpy (res, wd, l1); - res [l1] = '/'; - memcpy (res + l1 + 1, path, l2 + 1); - - return res; - } -} - -static eio_wd -eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) -{ - if (*path == '/') /* absolute paths ignore wd */ - path = strdup (path); - else if (path [0] == '.' && !path [1]) /* special case '.', as it is common */ - return wd; - else - { - int len = eio__realpath (tmpbuf, wd, path); - - path = EIO_INVALID_WD; - - if (len >= 0) - { - ((char *)tmpbuf->ptr)[len] = 0; - path = strdup (tmpbuf->ptr); - } - } - - if (!path) - path = EIO_INVALID_WD; - - return (eio_wd)path; -} - -eio_wd -eio_wd_open_sync (eio_wd wd, const char *path) -{ - struct tmpbuf tmpbuf = { 0 }; - 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) - free (wd); -} - #endif /*****************************************************************************/ @@ -2109,15 +2147,30 @@ } \ } +static void ecb_noinline ecb_cold +etp_proc_init (void) +{ +#if HAVE_PRCTL_SET_NAME + /* provide a more sensible "thread name" */ + char name[16 + 1]; + const int namelen = sizeof (name) - 1; + int len; + + prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); + name [namelen] = 0; + len = strlen (name); + strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio"); + prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0); +#endif +} + X_THREAD_PROC (etp_proc) { ETP_REQ *req; struct timespec ts; etp_worker *self = (etp_worker *)thr_arg; -#if HAVE_PRCTL_SET_NAME - prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0); -#endif + etp_proc_init (); /* try to distribute timeouts somewhat evenly */ ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); @@ -2189,8 +2242,6 @@ X_LOCK (wrklock); etp_worker_free (self); X_UNLOCK (wrklock); - - return 0; } /*****************************************************************************/ @@ -2272,8 +2323,10 @@ 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: eio_wd_close_sync (req->wd); 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) @@ -2286,6 +2339,7 @@ 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 = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, 0); break; case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); @@ -2298,15 +2352,40 @@ case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; case EIO_RMDIR: req->result = 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 = renameat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2); break; - case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2, 0); break; + case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); 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_READLINK: ALLOC (PATH_MAX); req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); 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)); @@ -2327,6 +2406,31 @@ req->result = readlink (path, req->ptr2, PATH_MAX); break; case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); 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))) @@ -2359,7 +2463,7 @@ case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; - case EIO_READDIR: eio__scandir (req); break; + case EIO_READDIR: eio__scandir (req, self); break; case EIO_BUSY: #ifdef _WIN32 @@ -2376,30 +2480,6 @@ #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; - case EIO_GROUP: abort (); /* handled in eio_request */ @@ -2412,8 +2492,7 @@ break; default: - errno = ENOSYS; - req->result = -1; + req->result = EIO_ENOSYS (); break; } @@ -2422,6 +2501,16 @@ #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; @@ -2492,6 +2581,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;