From 74999f8f9974970e4d71a0df521e178e3149f3df Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 1 Oct 2012 00:57:45 +0200 Subject: [PATCH] unix: port fs and work api to new thread pool --- config-unix.mk | 8 +- include/uv-private/uv-unix.h | 23 +- src/unix/fs.c | 1251 ++++++++++++++++------------------ src/unix/internal.h | 6 - src/unix/loop.c | 1 - src/unix/uv-eio.c | 107 --- src/unix/uv-eio.h | 13 - uv.gyp | 5 +- 8 files changed, 616 insertions(+), 798 deletions(-) delete mode 100644 src/unix/uv-eio.c delete mode 100644 src/unix/uv-eio.h diff --git a/config-unix.mk b/config-unix.mk index 28478c6c..30d3a17d 100644 --- a/config-unix.mk +++ b/config-unix.mk @@ -51,7 +51,7 @@ ifeq (SunOS,$(uname_S)) EV_CONFIG=config_sunos.h EIO_CONFIG=config_sunos.h CPPFLAGS += -D__EXTENSIONS__ -D_XOPEN_SOURCE=500 -LINKFLAGS+=-lsocket -lnsl -lkstat +LINKFLAGS+=-lkstat -lnsl -lsendfile -lsocket OBJS += src/unix/sunos.o endif @@ -137,7 +137,7 @@ endif RUNNER_LIBS= RUNNER_SRC=test/runner-unix.c -uv.a: $(OBJS) src/fs-poll.o src/inet.o src/uv-common.o src/unix/ev/ev.o src/unix/uv-eio.o src/unix/eio/eio.o +uv.a: $(OBJS) src/fs-poll.o src/inet.o src/uv-common.o src/unix/ev/ev.o src/unix/eio/eio.o $(AR) rcs uv.a $^ src/%.o: src/%.c include/uv.h include/uv-private/uv-unix.h @@ -158,10 +158,6 @@ EIO_CPPFLAGS += -D_GNU_SOURCE src/unix/eio/eio.o: src/unix/eio/eio.c $(CC) $(EIO_CPPFLAGS) $(CFLAGS) -c src/unix/eio/eio.c -o src/unix/eio/eio.o -src/unix/uv-eio.o: src/unix/uv-eio.c - $(CC) $(CPPFLAGS) -Isrc/unix/eio/ $(CSTDFLAG) $(CFLAGS) -c src/unix/uv-eio.c -o src/unix/uv-eio.o - - clean-platform: -rm -f src/unix/*.o -rm -f src/unix/ev/*.o diff --git a/include/uv-private/uv-unix.h b/include/uv-private/uv-unix.h index 4adebc61..7f810a96 100644 --- a/include/uv-private/uv-unix.h +++ b/include/uv-private/uv-unix.h @@ -25,7 +25,6 @@ #include "ngx-queue.h" #include "ev.h" -#include "eio.h" #include #include @@ -122,13 +121,7 @@ typedef struct { #define UV_LOOP_PRIVATE_FIELDS \ unsigned long flags; \ - /* Poll result queue */ \ - eio_channel uv_eio_channel; \ struct ev_loop* ev; \ - /* Various thing for libeio. */ \ - uv_async_t uv_eio_want_poll_notifier; \ - uv_async_t uv_eio_done_poll_notifier; \ - uv_idle_t uv_eio_poller; \ ngx_queue_t wq; \ uv_mutex_t wq_mutex; \ uv_async_t wq_async; \ @@ -256,12 +249,22 @@ typedef struct { int errorno; \ #define UV_FS_PRIVATE_FIELDS \ - struct stat statbuf; \ + const char *new_path; \ uv_file file; \ - eio_req* eio; \ + int flags; \ + mode_t mode; \ + void* buf; \ + size_t len; \ + off_t off; \ + uid_t uid; \ + gid_t gid; \ + double atime; \ + double mtime; \ + struct uv__work work_req; \ + struct stat statbuf; \ #define UV_WORK_PRIVATE_FIELDS \ - eio_req* eio; + struct uv__work work_req; #define UV_TTY_PRIVATE_FIELDS \ struct termios orig_termios; \ diff --git a/src/unix/fs.c b/src/unix/fs.c index 875f0b94..96670db9 100644 --- a/src/unix/fs.c +++ b/src/unix/fs.c @@ -21,537 +21,116 @@ #include "uv.h" #include "internal.h" -#include "eio.h" +#include #include #include #include -#include -#include -#include -#include -#include -#include + +#include +#include #include - - -#define ARGS1(a) (a) -#define ARGS2(a,b) (a), (b) -#define ARGS3(a,b,c) (a), (b), (c) -#define ARGS4(a,b,c,d) (a), (b), (c), (d) - -#define WRAP_EIO(type, eiofunc, func, args) \ - uv_fs_req_init(loop, req, type, path, cb); \ - if (cb) { \ - /* async */ \ - req->eio = eiofunc(args, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel); \ - if (!req->eio) { \ - uv__set_sys_error(loop, ENOMEM); \ - return -1; \ - } \ - } else { \ - /* sync */ \ - req->result = func(args); \ - if (req->result) { \ - uv__set_sys_error(loop, errno); \ - } \ - return req->result; \ - } \ - return 0; - - -static void uv_fs_req_init(uv_loop_t* loop, uv_fs_t* req, uv_fs_type fs_type, - const char* path, uv_fs_cb cb) { - /* Make sure the thread pool is initialized. */ - uv_eio_init(loop); - - uv__req_init(loop, req, UV_FS); - req->loop = loop; - req->fs_type = fs_type; - req->cb = cb; - req->result = 0; - req->ptr = NULL; - req->path = path ? strdup(path) : NULL; - req->file = -1; - req->errorno = 0; - req->eio = NULL; - - /* synchronous requests don't increase the reference count */ - if (!req->cb) - uv__req_unregister(req->loop, req); -} - - -void uv_fs_req_cleanup(uv_fs_t* req) { - if (req->cb) - uv__req_unregister(req->loop, req); - - free((void*)req->path); - req->path = NULL; - - switch (req->fs_type) { - case UV_FS_READDIR: - assert(req->result > 0 ? (req->ptr != NULL) : (req->ptr == NULL)); - free(req->ptr); - req->ptr = NULL; - break; - - case UV_FS_STAT: - case UV_FS_LSTAT: - req->ptr = NULL; - break; - - default: - break; - } -} - - -static int uv__fs_after(eio_req* eio) { - char* name; - int namelen; - int buflen = 0; - uv_fs_t* req = eio->data; - int i; - - assert(req->cb); - - req->result = req->eio->result; - req->errorno = uv_translate_sys_error(req->eio->errorno); - - switch (req->fs_type) { - case UV_FS_READDIR: - /* - * XXX This is pretty bad. - * We alloc and copy the large null terminated string list from libeio. - * This is done because libeio is going to free eio->ptr2 after this - * callback. We must keep it until uv_fs_req_cleanup. If we get rid of - * libeio this can be avoided. - */ - buflen = 0; - name = req->eio->ptr2; - - for (i = 0; i < req->result; i++) { - namelen = strlen(name); - buflen += namelen + 1; - name += namelen; - assert(*name == '\0'); - name++; - } - - if (buflen) { - if ((req->ptr = malloc(buflen))) - memcpy(req->ptr, req->eio->ptr2, buflen); - else - uv__set_sys_error(req->loop, ENOMEM); - } - break; - - case UV_FS_STAT: - case UV_FS_LSTAT: - case UV_FS_FSTAT: - req->ptr = req->eio->ptr2; - break; - - case UV_FS_READLINK: - if (req->result == -1) { - req->ptr = NULL; - break; - } - assert(req->result > 0); - - /* Make zero-terminated copy of req->eio->ptr2 */ - if ((req->ptr = name = malloc(req->result + 1))) { - memcpy(name, req->eio->ptr2, req->result); - name[req->result] = '\0'; - req->result = 0; - } - else { - req->errorno = ENOMEM; - req->result = -1; - } - break; - - default: - break; - } - - req->eio = NULL; /* Freed by libeio */ - req->cb(req); - - return 0; -} - - -int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { - char* path = NULL; - WRAP_EIO(UV_FS_CLOSE, eio_close, close, ARGS1(file)); -} - - -int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, - int mode, uv_fs_cb cb) { - uv_fs_req_init(loop, req, UV_FS_OPEN, path, cb); - - if (cb) { - /* async */ - req->eio = eio_open(path, flags, mode, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel); - if (!req->eio) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - - } else { - /* sync */ - req->result = open(path, flags, mode); - if (req->result < 0) { - uv__set_sys_error(loop, errno); - return -1; - } - - uv__cloexec(req->result, 1); - - return req->result; - } - - return 0; -} - - -int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf, - size_t length, int64_t offset, uv_fs_cb cb) { - uv_fs_req_init(loop, req, UV_FS_READ, NULL, cb); - - if (cb) { - /* async */ - req->eio = eio_read(fd, buf, length, offset, EIO_PRI_DEFAULT, - uv__fs_after, req, &loop->uv_eio_channel); - - if (!req->eio) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - - } else { - /* sync */ - req->result = offset < 0 ? - read(fd, buf, length) : - pread(fd, buf, length, offset); - - if (req->result < 0) { - uv__set_sys_error(loop, errno); - return -1; - } - - return req->result; - } - - return 0; -} - - -int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { - WRAP_EIO(UV_FS_UNLINK, eio_unlink, unlink, ARGS1(path)) -} - - -int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file, void* buf, - size_t length, int64_t offset, uv_fs_cb cb) { - uv_fs_req_init(loop, req, UV_FS_WRITE, NULL, cb); - - if (cb) { - /* async */ - req->eio = eio_write(file, buf, length, offset, EIO_PRI_DEFAULT, - uv__fs_after, req, &loop->uv_eio_channel); - if (!req->eio) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - - } else { - /* sync */ - req->result = offset < 0 ? - write(file, buf, length) : - pwrite(file, buf, length, offset); - - if (req->result < 0) { - uv__set_sys_error(loop, errno); - return -1; - } - - return req->result; - } - - return 0; -} - - -int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode, - uv_fs_cb cb) { - WRAP_EIO(UV_FS_MKDIR, eio_mkdir, mkdir, ARGS2(path, mode)) -} - - -int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { - WRAP_EIO(UV_FS_RMDIR, eio_rmdir, rmdir, ARGS1(path)) -} - - -int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, - uv_fs_cb cb) { - int r; - struct dirent* entry; - size_t size = 0; - size_t d_namlen = 0; - - uv_fs_req_init(loop, req, UV_FS_READDIR, path, cb); - - if (cb) { - /* async */ - req->eio = eio_readdir(path, flags, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel); - if (!req->eio) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - - } else { - /* sync */ - DIR* dir = opendir(path); - if (!dir) { - uv__set_sys_error(loop, errno); - req->result = -1; - return -1; - } - - /* req->result stores number of entries */ - req->result = 0; - - while ((entry = readdir(dir))) { - d_namlen = strlen(entry->d_name); - - /* Skip . and .. */ - if ((d_namlen == 1 && entry->d_name[0] == '.') || - (d_namlen == 2 && entry->d_name[0] == '.' && - entry->d_name[1] == '.')) { - continue; - } - - req->ptr = realloc(req->ptr, size + d_namlen + 1); - /* TODO check ENOMEM */ - memcpy((char*)req->ptr + size, entry->d_name, d_namlen); - size += d_namlen; - ((char*)req->ptr)[size] = '\0'; - size++; - req->result++; - } - - r = closedir(dir); - if (r) { - uv__set_sys_error(loop, errno); - req->result = -1; - return -1; - } - - return req->result; - } - - return 0; -} - - -int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { - char* pathdup; - int pathlen; - - uv_fs_req_init(loop, req, UV_FS_STAT, path, cb); - - /* TODO do this without duplicating the string. */ - /* TODO security */ - pathdup = strdup(path); - pathlen = strlen(path); - - if (pathlen > 0 && path[pathlen - 1] == '\\') { - /* TODO do not modify input string */ - pathdup[pathlen - 1] = '\0'; - } - - if (cb) { - /* async */ - req->eio = eio_stat(pathdup, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel); - - free(pathdup); - - if (!req->eio) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - - } else { - /* sync */ - req->result = stat(pathdup, &req->statbuf); - - free(pathdup); - - if (req->result < 0) { - uv__set_sys_error(loop, errno); - return -1; - } - - req->ptr = &req->statbuf; - return req->result; - } - - return 0; -} - - -int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { - uv_fs_req_init(loop, req, UV_FS_FSTAT, NULL, cb); - - if (cb) { - /* async */ - req->eio = eio_fstat(file, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel); - - if (!req->eio) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - - } else { - /* sync */ - req->result = fstat(file, &req->statbuf); - - if (req->result < 0) { - uv__set_sys_error(loop, errno); - return -1; - } - - req->ptr = &req->statbuf; - return req->result; - } - - return 0; -} - - -int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* new_path, - uv_fs_cb cb) { - WRAP_EIO(UV_FS_RENAME, eio_rename, rename, ARGS2(path, new_path)) -} - - -int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { - char* path = NULL; - WRAP_EIO(UV_FS_FSYNC, eio_fsync, fsync, ARGS1(file)) -} - - +#include +#include +#include +#include +#include + +#if defined(__linux__) || defined(__sun) +# include +#elif defined(__APPLE__) || defined(__FreeBSD__) +# include +# include +#endif + +#define INIT(type) \ + do { \ + uv__req_init((loop), (req), UV_FS_ ## type); \ + (req)->fs_type = UV_FS_ ## type; \ + (req)->errorno = 0; \ + (req)->result = 0; \ + (req)->ptr = NULL; \ + (req)->loop = loop; \ + (req)->path = NULL; \ + (req)->new_path = NULL; \ + (req)->cb = (cb); \ + } \ + while (0) + +#define PATH \ + do { \ + if (NULL == ((req)->path = strdup((path)))) \ + return uv__set_sys_error((loop), ENOMEM); \ + } \ + while (0) + +#define PATH2 \ + do { \ + size_t path_len; \ + size_t new_path_len; \ + \ + path_len = strlen(path) + 1; \ + new_path_len = strlen(new_path) + 1; \ + \ + if (NULL == ((req)->path = malloc(path_len + new_path_len))) \ + return uv__set_sys_error((loop), ENOMEM); \ + \ + (req)->new_path = (req)->path + new_path_len; \ + memcpy((void*) (req)->path, (path), path_len); \ + memcpy((void*) (req)->new_path, (new_path), new_path_len); \ + } \ + while (0) + +#define PATH_STAT_FIXUP \ + do { \ + size_t len; \ + PATH; \ + len = strlen((req)->path); \ + if (len != 0 && (req)->path[len - 1] == '\\') \ + ((char*) (req)->path)[len - 1] = '\0'; /* Windows compatibility... */ \ + } \ + while (0) + +#define POST \ + do { \ + if ((cb) != NULL) \ + uv__work_submit((loop), &(req)->work_req, uv__fs_work, uv__fs_done); \ + else { \ + uv__fs_work(&(req)->work_req); \ + uv__fs_done(&(req)->work_req); \ + } \ + return (req)->result; \ + } \ + while (0) + + +static ssize_t uv__fs_fdatasync(uv_fs_t* req) { #if defined(__APPLE__) && defined(F_FULLFSYNC) -ssize_t uv__fs_fdatasync(uv_file file) { - return fcntl(file, F_FULLFSYNC); -} - - -void uv__fs_fdatasync_work(eio_req* eio) { - uv_fs_t* req = eio->data; - - eio->result = uv__fs_fdatasync(req->file); -} -#endif - - -int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { - char* path = NULL; -#if defined(__FreeBSD__) \ - || (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1060) - /* freebsd and pre-10.6 darwin don't have fdatasync, - * do a full fsync instead. - */ - WRAP_EIO(UV_FS_FDATASYNC, eio_fdatasync, fsync, ARGS1(file)) -#elif defined(__APPLE__) && defined(F_FULLFSYNC) - /* OSX >= 10.6 does have fdatasync, but better use fcntl anyway */ - uv_fs_req_init(loop, req, UV_FS_FDATASYNC, path, cb); - req->file = file; - - if (cb) { - /* async */ - req->eio = eio_custom(uv__fs_fdatasync_work, - EIO_PRI_DEFAULT, - uv__fs_after, - req, - &loop->uv_eio_channel); - if (req->eio == NULL) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - } else { - /* sync */ - req->result = uv__fs_fdatasync(file); - if (req->result) { - uv__set_sys_error(loop, errno); - } - return req->result; - } - return 0; + return fcntl(req->file, F_FULLFSYNC); #else - WRAP_EIO(UV_FS_FDATASYNC, eio_fdatasync, fdatasync, ARGS1(file)) + return fdatasync(req->file); #endif } -int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file, int64_t offset, - uv_fs_cb cb) { - char* path = NULL; - WRAP_EIO(UV_FS_FTRUNCATE, eio_ftruncate, ftruncate, ARGS2(file, offset)) -} - - -int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd, uv_file in_fd, - int64_t in_offset, size_t length, uv_fs_cb cb) { - char* path = NULL; - WRAP_EIO(UV_FS_SENDFILE, eio_sendfile, eio_sendfile_sync, - ARGS4(out_fd, in_fd, in_offset, length)) -} - - -int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode, - uv_fs_cb cb) { - WRAP_EIO(UV_FS_CHMOD, eio_chmod, chmod, ARGS2(path, mode)) -} - - -static int _utime(const char* path, double atime, double mtime) { - struct utimbuf buf; - buf.actime = atime; - buf.modtime = mtime; - return utime(path, &buf); -} - - -int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path, double atime, - double mtime, uv_fs_cb cb) { - WRAP_EIO(UV_FS_UTIME, eio_utime, _utime, ARGS3(path, atime, mtime)) -} - - -static int _futime(const uv_file fd, double atime, double mtime) { -#if __linux__ +static ssize_t uv__fs_futime(uv_fs_t* req) { +#if defined(__linux__) /* utimesat() has nanosecond resolution but we stick to microseconds * for the sake of consistency with other platforms. */ struct timespec ts[2]; - ts[0].tv_sec = atime; - ts[0].tv_nsec = (unsigned long)(atime * 1000000) % 1000000 * 1000; - ts[1].tv_sec = mtime; - ts[1].tv_nsec = (unsigned long)(mtime * 1000000) % 1000000 * 1000; - return uv__utimesat(fd, NULL, ts, 0); + ts[0].tv_sec = req->atime; + ts[0].tv_nsec = (unsigned long)(req->atime * 1000000) % 1000000 * 1000; + ts[1].tv_sec = req->mtime; + ts[1].tv_nsec = (unsigned long)(req->mtime * 1000000) % 1000000 * 1000; + return uv__utimesat(req->file, NULL, ts, 0); #elif HAVE_FUTIMES struct timeval tv[2]; - tv[0].tv_sec = atime; - tv[0].tv_usec = (unsigned long)(atime * 1000000) % 1000000; - tv[1].tv_sec = mtime; - tv[1].tv_usec = (unsigned long)(mtime * 1000000) % 1000000; - return futimes(fd, tv); + tv[0].tv_sec = req->atime; + tv[0].tv_usec = (unsigned long)(req->atime * 1000000) % 1000000; + tv[1].tv_sec = req->mtime; + tv[1].tv_usec = (unsigned long)(req->mtime * 1000000) % 1000000; + return futimes(req->file, tv); #else /* !HAVE_FUTIMES */ errno = ENOSYS; return -1; @@ -559,181 +138,549 @@ static int _futime(const uv_file fd, double atime, double mtime) { } -int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file, double atime, - double mtime, uv_fs_cb cb) { - const char* path = NULL; - WRAP_EIO(UV_FS_FUTIME, eio_futime, _futime, ARGS3(file, atime, mtime)) +static ssize_t uv__fs_pwrite(uv_fs_t* req) { +#if defined(__APPLE__) + /* Serialize writes on OS X, concurrent pwrite() calls result in data loss. + * We can't use a per-file descriptor lock, the descriptor may be a dup(). + */ + static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; + ssize_t r; + + pthread_mutex_lock(&lock); + r = pwrite(req->file, req->buf, req->len, req->off); + pthread_mutex_unlock(&lock); + + return r; +#else + return pwrite(req->file, req->buf, req->len, req->off); +#endif +} + +static ssize_t uv__fs_read(uv_fs_t* req) { + if (req->off < 0) + return read(req->file, req->buf, req->len); + else + return pread(req->file, req->buf, req->len, req->off); +} + + +static int uv__fs_readdir_filter(const struct dirent* dent) { + return strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0; +} + + +/* This should have been called uv__fs_scandir(). */ +static ssize_t uv__fs_readdir(uv_fs_t* req) { + struct dirent **dents; + int saved_errno; + size_t off; + size_t len; + char *buf; + int i; + int n; + + n = scandir(req->path, &dents, uv__fs_readdir_filter, alphasort); + + if (n == -1 || n == 0) + return n; + + len = 0; + + for (i = 0; i < n; i++) + len += strlen(dents[i]->d_name) + 1; + + buf = malloc(len); + + if (buf == NULL) { + errno = ENOMEM; + n = -1; + goto out; + } + + off = 0; + + for (i = 0; i < n; i++) { + len = strlen(dents[i]->d_name) + 1; + memcpy(buf + off, dents[i]->d_name, len); + off += len; + } + + req->ptr = buf; + +out: + saved_errno = errno; + { + for (i = 0; i < n; i++) + free(dents[i]); + free(dents); + } + errno = saved_errno; + + return n; +} + + +static ssize_t uv__fs_readlink(uv_fs_t* req) { + ssize_t len; + char* buf; + + len = pathconf(req->path, _PC_PATH_MAX); + + if (len == -1) { +#if defined(PATH_MAX) + len = PATH_MAX; +#else + len = 4096; +#endif + } + + buf = malloc(len + 1); + + if (buf == NULL) { + errno = ENOMEM; + return -1; + } + + len = readlink(req->path, buf, len); + + if (len == -1) { + free(buf); + return -1; + } + + buf[len] = '\0'; + req->ptr = buf; + + return 0; +} + + +static ssize_t uv__fs_sendfile(uv_fs_t* req) { + /* req->file is the out_fd, req->flags the in_fd */ +#if defined(__linux__) || defined(__sun) + return sendfile(req->file, req->flags, &req->off, req->len); +#elif defined(__APPLE__) || defined(__FreeBSD__) + return sendfile(req->flags, + req->file, + req->off, + req->len, + NULL, + &req->off, + 0); +#else + errno = ENOSYS; + return -1; +#endif +} + + +static ssize_t uv__fs_utime(uv_fs_t* req) { + struct utimbuf buf; + buf.actime = req->atime; + buf.modtime = req->mtime; + return utime(req->path, &buf); /* TODO use utimes() where available */ +} + + +static ssize_t uv__fs_write(uv_fs_t* req) { + if (req->off < 0) + return write(req->file, req->buf, req->len); + else + return uv__fs_pwrite(req); +} + + +static void uv__fs_work(struct uv__work* w) { + int retry_on_eintr; + uv_fs_t* req; + ssize_t r; + + req = container_of(w, uv_fs_t, work_req); + retry_on_eintr = !(req->fs_type == UV_FS_CLOSE); + + do { + errno = 0; + +#define X(type, action) \ + case UV_FS_ ## type: \ + r = action; \ + break; + + switch (req->fs_type) { + X(CHMOD, chmod(req->path, req->mode)); + X(CHOWN, chown(req->path, req->uid, req->gid)); + X(CLOSE, close(req->file)); + X(FCHMOD, fchmod(req->file, req->mode)); + X(FCHOWN, fchown(req->file, req->uid, req->gid)); + X(FDATASYNC, uv__fs_fdatasync(req)); + X(FSTAT, fstat(req->file, &req->statbuf)); + X(FSYNC, fsync(req->file)); + X(FTRUNCATE, ftruncate(req->file, req->off)); + X(FUTIME, uv__fs_futime(req)); + X(LSTAT, lstat(req->path, &req->statbuf)); + X(LINK, link(req->path, req->new_path)); + X(MKDIR, mkdir(req->path, req->mode)); + X(OPEN, open(req->path, req->flags, req->mode)); + X(READ, uv__fs_read(req)); + X(READDIR, uv__fs_readdir(req)); + X(READLINK, uv__fs_readlink(req)); + X(RENAME, rename(req->path, req->new_path)); + X(RMDIR, rmdir(req->path)); + X(SENDFILE, uv__fs_sendfile(req)); + X(STAT, stat(req->path, &req->statbuf)); + X(SYMLINK, symlink(req->path, req->new_path)); + X(UNLINK, unlink(req->path)); + X(UTIME, uv__fs_utime(req)); + X(WRITE, uv__fs_write(req)); + default: abort(); + } + +#undef X + } + while (r == -1 && errno == EINTR && retry_on_eintr); + + req->errorno = errno; + req->result = r; + + if (r == 0 && (req->fs_type == UV_FS_STAT || + req->fs_type == UV_FS_FSTAT || + req->fs_type == UV_FS_LSTAT)) { + req->ptr = &req->statbuf; + } +} + + +static void uv__fs_done(struct uv__work* w) { + uv_fs_t* req; + + req = container_of(w, uv_fs_t, work_req); + uv__req_unregister(req->loop, req); + + if (req->errorno != 0) { + req->errorno = uv_translate_sys_error(req->errorno); + uv__set_artificial_error(req->loop, req->errorno); + } + + if (req->cb != NULL) + req->cb(req); +} + + +int uv_fs_chmod(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + int mode, + uv_fs_cb cb) { + INIT(CHMOD); + PATH; + req->mode = mode; + POST; +} + + +int uv_fs_chown(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + int uid, + int gid, + uv_fs_cb cb) { + INIT(CHOWN); + PATH; + req->uid = uid; + req->gid = gid; + POST; +} + + +int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { + INIT(CLOSE); + req->file = file; + POST; +} + + +int uv_fs_fchmod(uv_loop_t* loop, + uv_fs_t* req, + uv_file file, + int mode, + uv_fs_cb cb) { + INIT(FCHMOD); + req->file = file; + req->mode = mode; + POST; +} + + +int uv_fs_fchown(uv_loop_t* loop, + uv_fs_t* req, + uv_file file, + int uid, + int gid, + uv_fs_cb cb) { + INIT(FCHOWN); + req->file = file; + req->uid = uid; + req->gid = gid; + POST; +} + + +int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { + INIT(FDATASYNC); + req->file = file; + POST; +} + + +int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { + INIT(FSTAT); + req->file = file; + POST; +} + + +int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { + INIT(FSYNC); + req->file = file; + POST; +} + + +int uv_fs_ftruncate(uv_loop_t* loop, + uv_fs_t* req, + uv_file file, + int64_t off, + uv_fs_cb cb) { + INIT(FTRUNCATE); + req->file = file; + req->off = off; + POST; +} + + +int uv_fs_futime(uv_loop_t* loop, + uv_fs_t* req, + uv_file file, + double atime, + double mtime, + uv_fs_cb cb) { + INIT(FUTIME); + req->file = file; + req->atime = atime; + req->mtime = mtime; + POST; } int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { - char* pathdup; - int pathlen; - - uv_fs_req_init(loop, req, UV_FS_LSTAT, path, cb); - - /* TODO do this without duplicating the string. */ - /* TODO security */ - pathdup = strdup(path); - pathlen = strlen(path); - - if (pathlen > 0 && path[pathlen - 1] == '\\') { - /* TODO do not modify input string */ - pathdup[pathlen - 1] = '\0'; - } - - if (cb) { - /* async */ - req->eio = eio_lstat(pathdup, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel); - - free(pathdup); - - if (!req->eio) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - - } else { - /* sync */ - req->result = lstat(pathdup, &req->statbuf); - - free(pathdup); - - if (req->result < 0) { - uv__set_sys_error(loop, errno); - return -1; - } - - req->ptr = &req->statbuf; - return req->result; - } - - return 0; + INIT(LSTAT); + PATH_STAT_FIXUP; + POST; } -int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path, - const char* new_path, uv_fs_cb cb) { - WRAP_EIO(UV_FS_LINK, eio_link, link, ARGS2(path, new_path)) +int uv_fs_link(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + const char* new_path, + uv_fs_cb cb) { + INIT(LINK); + PATH2; + POST; } -int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path, - const char* new_path, int flags, uv_fs_cb cb) { - WRAP_EIO(UV_FS_SYMLINK, eio_symlink, symlink, ARGS2(path, new_path)) +int uv_fs_mkdir(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + int mode, + uv_fs_cb cb) { + INIT(MKDIR); + PATH; + req->mode = mode; + POST; } -int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path, - uv_fs_cb cb) { - ssize_t size; - char* buf; - - uv_fs_req_init(loop, req, UV_FS_READLINK, path, cb); - - if (cb) { - if ((req->eio = eio_readlink(path, EIO_PRI_DEFAULT, uv__fs_after, req, &loop->uv_eio_channel))) { - return 0; - } else { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - } else { - /* pathconf(_PC_PATH_MAX) may return -1 to signify that path - * lengths have no upper limit or aren't suitable for malloc'ing. - */ - if ((size = pathconf(path, _PC_PATH_MAX)) == -1) { -#if defined(PATH_MAX) - size = PATH_MAX; -#else - size = 4096; -#endif - } - - if ((buf = malloc(size + 1)) == NULL) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - - if ((size = readlink(path, buf, size)) == -1) { - req->errorno = errno; - req->result = -1; - free(buf); - } else { - /* Cannot conceivably fail since it shrinks the buffer. */ - buf = realloc(buf, size + 1); - buf[size] = '\0'; - req->result = 0; - req->ptr = buf; - } - - return req->result; - } - - assert(0 && "unreachable"); +int uv_fs_open(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + int flags, + int mode, + uv_fs_cb cb) { + INIT(OPEN); + PATH; + req->flags = flags; + req->mode = mode; + POST; } -int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file, int mode, - uv_fs_cb cb) { - char* path = NULL; - WRAP_EIO(UV_FS_FCHMOD, eio_fchmod, fchmod, ARGS2(file, mode)) +int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, + uv_file file, + void* buf, + size_t len, + int64_t off, + uv_fs_cb cb) { + INIT(READ); + req->file = file; + req->buf = buf; + req->len = len; + req->off = off; + POST; } -int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path, int uid, - int gid, uv_fs_cb cb) { - WRAP_EIO(UV_FS_CHOWN, eio_chown, chown, ARGS3(path, uid, gid)) +int uv_fs_readdir(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + int flags, + uv_fs_cb cb) { + INIT(READDIR); + PATH; + req->flags = flags; + POST; } -int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file, int uid, int gid, - uv_fs_cb cb) { - char* path = NULL; - WRAP_EIO(UV_FS_FCHOWN, eio_fchown, fchown, ARGS3(file, uid, gid)) +int uv_fs_readlink(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + uv_fs_cb cb) { + INIT(READLINK); + PATH; + POST; } -static void uv__work(eio_req* eio) { - uv_work_t* req = eio->data; - if (req->work_cb) { +int uv_fs_rename(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + const char* new_path, + uv_fs_cb cb) { + INIT(RENAME); + PATH2; + POST; +} + + +int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { + INIT(RMDIR); + PATH; + POST; +} + + +int uv_fs_sendfile(uv_loop_t* loop, + uv_fs_t* req, + uv_file out_fd, + uv_file in_fd, + int64_t off, + size_t len, + uv_fs_cb cb) { + INIT(SENDFILE); + req->flags = in_fd; /* hack */ + req->file = out_fd; + req->off = off; + req->len = len; + POST; +} + + +int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { + INIT(STAT); + PATH_STAT_FIXUP; + POST; +} + + +int uv_fs_symlink(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + const char* new_path, + int flags, + uv_fs_cb cb) { + INIT(SYMLINK); + PATH2; + req->flags = flags; + POST; +} + + +int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { + INIT(UNLINK); + PATH; + POST; +} + + +int uv_fs_utime(uv_loop_t* loop, + uv_fs_t* req, + const char* path, + double atime, + double mtime, + uv_fs_cb cb) { + INIT(UTIME); + PATH; + req->atime = atime; + req->mtime = mtime; + POST; +} + + +int uv_fs_write(uv_loop_t* loop, + uv_fs_t* req, + uv_file file, + void* buf, + size_t len, + int64_t off, + uv_fs_cb cb) { + INIT(WRITE); + req->file = file; + req->buf = buf; + req->len = len; + req->off = off; + POST; +} + + +void uv_fs_req_cleanup(uv_fs_t* req) { + free((void*) req->path); + req->path = NULL; + req->new_path = NULL; + + if (req->ptr != &req->statbuf) + free(req->ptr); + req->ptr = NULL; +} + + +static void uv__queue_work(struct uv__work* w) { + uv_work_t* req = container_of(w, uv_work_t, work_req); + + if (req->work_cb) req->work_cb(req); - } } -static int uv__after_work(eio_req *eio) { - uv_work_t* req = eio->data; +static void uv__queue_done(struct uv__work* w) { + uv_work_t* req = container_of(w, uv_work_t, work_req); + uv__req_unregister(req->loop, req); - if (req->after_work_cb) { + + if (req->after_work_cb) req->after_work_cb(req); - } - return 0; } -int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb, - uv_after_work_cb after_work_cb) { - void* data = req->data; - - uv_eio_init(loop); - +int uv_queue_work(uv_loop_t* loop, + uv_work_t* req, + uv_work_cb work_cb, + uv_after_work_cb after_work_cb) { uv__req_init(loop, req, UV_WORK); req->loop = loop; - req->data = data; req->work_cb = work_cb; req->after_work_cb = after_work_cb; - - req->eio = eio_custom(uv__work, - EIO_PRI_DEFAULT, - uv__after_work, - req, - &loop->uv_eio_channel); - - if (!req->eio) { - uv__set_sys_error(loop, ENOMEM); - return -1; - } - + uv__work_submit(loop, &req->work_req, uv__queue_work, uv__queue_done); return 0; } diff --git a/src/unix/internal.h b/src/unix/internal.h index 20f12bcd..5685e1a8 100644 --- a/src/unix/internal.h +++ b/src/unix/internal.h @@ -23,7 +23,6 @@ #define UV_UNIX_INTERNAL_H_ #include "uv-common.h" -#include "uv-eio.h" #include #include /* abort */ @@ -101,11 +100,6 @@ enum { UV_TCP_SINGLE_ACCEPT = 0x400 /* Only accept() when idle. */ }; -/* loop flags */ -enum { - UV_LOOP_EIO_INITIALIZED = 1 -}; - __attribute__((unused)) __attribute__((always_inline)) static void uv__req_init(uv_loop_t* loop, uv_req_t* req, uv_req_type type) { diff --git a/src/unix/loop.c b/src/unix/loop.c index d757a718..7d41c005 100644 --- a/src/unix/loop.c +++ b/src/unix/loop.c @@ -54,7 +54,6 @@ int uv__loop_init(uv_loop_t* loop, int default_loop) { loop->emfile_fd = -1; loop->ev = (default_loop ? ev_default_loop : ev_loop_new)(flags); ev_set_userdata(loop->ev, loop); - eio_channel_init(&loop->uv_eio_channel, loop); uv_signal_init(loop, &loop->child_watcher); uv__handle_unref(&loop->child_watcher); diff --git a/src/unix/uv-eio.c b/src/unix/uv-eio.c deleted file mode 100644 index e5ba2f27..00000000 --- a/src/unix/uv-eio.c +++ /dev/null @@ -1,107 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -/* This file integrates the libuv event loop with the libeio thread pool */ - -#include "uv.h" -#include "eio.h" -#include "internal.h" - -#include -#include - - -static uv_once_t uv__eio_init_once_guard = UV_ONCE_INIT; - - -static void uv_eio_do_poll(uv_idle_t* watcher, int status) { - uv_loop_t* loop = watcher->loop; - assert(watcher == &loop->uv_eio_poller); - if (eio_poll(&loop->uv_eio_channel) != -1) - uv_idle_stop(watcher); -} - - -/* Called from the main thread. */ -static void uv_eio_want_poll_notifier_cb(uv_async_t* watcher, int status) { - uv_loop_t* loop = watcher->loop; - assert(watcher == &loop->uv_eio_want_poll_notifier); - if (eio_poll(&loop->uv_eio_channel) == -1) - uv_idle_start(&loop->uv_eio_poller, uv_eio_do_poll); -} - - -static void uv_eio_done_poll_notifier_cb(uv_async_t* watcher, int revents) { - uv_loop_t* loop = watcher->loop; - assert(watcher == &loop->uv_eio_done_poll_notifier); - if (eio_poll(&loop->uv_eio_channel) != -1) - uv_idle_stop(&loop->uv_eio_poller); -} - - -/* - * uv_eio_want_poll() is called from the EIO thread pool each time an EIO - * request (that is, one of the node.fs.* functions) has completed. - */ -static void uv_eio_want_poll(eio_channel *channel) { - /* Signal the main thread that eio_poll need to be processed. */ - uv_loop_t* loop = channel->data; - uv_async_send(&loop->uv_eio_want_poll_notifier); -} - - -static void uv_eio_done_poll(eio_channel *channel) { - /* - * Signal the main thread that we should stop calling eio_poll(). - * from the idle watcher. - */ - uv_loop_t* loop = channel->data; - uv_async_send(&loop->uv_eio_done_poll_notifier); -} - - -static void uv__eio_init(void) { - eio_init(uv_eio_want_poll, uv_eio_done_poll); -} - - -void uv_eio_init(uv_loop_t* loop) { - if (loop->flags & UV_LOOP_EIO_INITIALIZED) return; - loop->flags |= UV_LOOP_EIO_INITIALIZED; - - uv_idle_init(loop, &loop->uv_eio_poller); - uv_idle_start(&loop->uv_eio_poller, uv_eio_do_poll); - loop->uv_eio_poller.flags |= UV__HANDLE_INTERNAL; - - loop->uv_eio_want_poll_notifier.data = loop; - uv_async_init(loop, - &loop->uv_eio_want_poll_notifier, - uv_eio_want_poll_notifier_cb); - loop->uv_eio_want_poll_notifier.flags |= UV__HANDLE_INTERNAL; - uv__handle_unref(&loop->uv_eio_want_poll_notifier); - - uv_async_init(loop, - &loop->uv_eio_done_poll_notifier, - uv_eio_done_poll_notifier_cb); - loop->uv_eio_done_poll_notifier.flags |= UV__HANDLE_INTERNAL; - uv__handle_unref(&loop->uv_eio_done_poll_notifier); - - uv_once(&uv__eio_init_once_guard, uv__eio_init); -} diff --git a/src/unix/uv-eio.h b/src/unix/uv-eio.h deleted file mode 100644 index 711d0cf2..00000000 --- a/src/unix/uv-eio.h +++ /dev/null @@ -1,13 +0,0 @@ -/* This header is private to libuv */ -#ifndef UV_EIO_H_ -#define UV_EIO_H_ - -#include "eio.h" - -/* - * Call this function to integrate libeio into the libuv event loop. It is - * safe to call more than once. - * TODO: uv_eio_deinit - */ -void uv_eio_init(uv_loop_t*); -#endif diff --git a/uv.gyp b/uv.gyp index 4d7f5494..6e23eed1 100644 --- a/uv.gyp +++ b/uv.gyp @@ -142,8 +142,6 @@ 'src/unix/timer.c', 'src/unix/tty.c', 'src/unix/udp.c', - 'src/unix/uv-eio.c', - 'src/unix/uv-eio.h', ], 'include_dirs': [ 'src/unix/ev', ], 'libraries': [ '-lm' ] @@ -187,8 +185,9 @@ 'direct_dependent_settings': { 'libraries': [ '-lkstat', - '-lsocket', '-lnsl', + '-lsendfile', + '-lsocket', ], }, }],