--- libeio/eio.c 2011/07/18 01:27:03 1.93 +++ libeio/eio.c 2011/09/15 13:20:54 1.103 @@ -45,7 +45,7 @@ #include "ecb.h" #ifdef EIO_STACKSIZE -# define XTHREAD_STACKSIZE EIO_STACKSIZE +# define X_STACKSIZE EIO_STACKSIZE #endif #include "xthread.h" @@ -73,6 +73,10 @@ # define ELOOP EDOM #endif +#if !defined(ENOTSOCK) && defined(WSAENOTSOCK) +# define ENOTSOCK WSAENOTSOCK +#endif + static void eio_destroy (eio_req *req); #ifndef EIO_FINISH @@ -103,9 +107,12 @@ #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 (path,buf) + #define fstat(fd,buf) _fstati64 (fd,buf) #endif #define lstat(path,buf) stat (path,buf) #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1)) @@ -127,6 +134,37 @@ #define statvfs(path,buf) EIO_ENOSYS () #define fstatvfs(fd,buf) EIO_ENOSYS () + /* rename() uses MoveFile, which fails to overwrite */ + #define rename(old,neu) eio__rename (old, neu) + + static int + eio__rename (const char *old, const char *neu) + { + if (MoveFileEx (old, neu, MOVEFILE_REPLACE_EXISTING)) + return 0; + + /* should steal _dosmaperr */ + switch (GetLastError ()) + { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_DRIVE: + case ERROR_NO_MORE_FILES: + case ERROR_BAD_NETPATH: + case ERROR_BAD_NET_NAME: + case ERROR_BAD_PATHNAME: + case ERROR_FILENAME_EXCED_RANGE: + errno = ENOENT; + break; + + default: + errno = EACCES; + break; + } + + return -1; + } + /* we could even stat and see if it exists */ static int symlink (const char *old, const char *neu) @@ -162,7 +200,6 @@ #include #include #include - #include #include #include @@ -196,6 +233,18 @@ #endif +#if HAVE_UTIMES +# include +#endif + +#if HAVE_SYS_SYSCALL_H +# include +#endif + +#if HAVE_SYS_PRCTL_H +# include +#endif + #if HAVE_SENDFILE # if __linux # include @@ -235,13 +284,13 @@ #define EIO_BUFSIZE 65536 #define dBUF \ - char *eio_buf; \ - ETP_WORKER_LOCK (self); \ - self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \ - ETP_WORKER_UNLOCK (self); \ + char *eio_buf = malloc (EIO_BUFSIZE); \ errno = ENOMEM; \ if (!eio_buf) \ - return -1; + return -1 + +#define FUBd \ + free (eio_buf) #define EIO_TICKS ((1000000 + 1023) >> 10) @@ -257,16 +306,6 @@ static void eio_execute (struct etp_worker *self, eio_req *req); #define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) -#define ETP_WORKER_CLEAR(req) \ - if (wrk->dbuf) \ - { \ - free (wrk->dbuf); \ - wrk->dbuf = 0; \ - } - -#define ETP_WORKER_COMMON \ - void *dbuf; - /*****************************************************************************/ #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) @@ -287,11 +326,11 @@ 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 */ +static unsigned int nreqs; /* reqlock */ +static unsigned int nready; /* reqlock */ +static unsigned int npending; /* reqlock */ +static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ +static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ static xmutex_t wrklock; static xmutex_t reslock; @@ -317,7 +356,9 @@ /* locked by reslock, reqlock or wrklock */ ETP_REQ *req; /* currently processed request */ +#ifdef ETP_WORKER_COMMON ETP_WORKER_COMMON +#endif } etp_worker; static etp_worker wrk_first; /* NOT etp */ @@ -330,7 +371,6 @@ static void ecb_cold etp_worker_clear (etp_worker *wrk) { - ETP_WORKER_CLEAR (wrk); } static void ecb_cold @@ -941,6 +981,24 @@ # define fdatasync(fd) fsync (fd) #endif +static int +eio__syncfs (int fd) +{ + int res; + +#if HAVE_SYS_SYNCFS + res = (int)syscall (__NR_syncfs, (int)(fd)); +#else + res = -1; + errno = ENOSYS; +#endif + + if (res < 0 && errno == ENOSYS && fd >= 0) + sync (); + + return res; +} + /* sync_file_range always needs emulation */ static int eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) @@ -999,6 +1057,8 @@ todo -= len; } + FUBd; + errno = 0; return count; } @@ -1007,7 +1067,7 @@ /* sendfile always needs emulation */ static eio_ssize_t -eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) +eio__sendfile (int ofd, int ifd, off_t offset, size_t count) { eio_ssize_t written = 0; eio_ssize_t res; @@ -1153,6 +1213,8 @@ res += cnt; count -= cnt; } + + FUBd; } return res; @@ -1646,32 +1708,36 @@ if (dirp == INVALID_HANDLE_VALUE) { - dirp = 0; - - 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; + /* 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; + } - case ERROR_NOT_ENOUGH_MEMORY: - errno = ENOMEM; - break; - - default: - errno = EINVAL; - break; - } + return; } } #else dirp = opendir (req->ptr1); + + if (!dirp) + return; #endif if (req->flags & EIO_FLAG_PTR1_FREE) @@ -1681,191 +1747,193 @@ 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, '.')) /* 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; + } + } - 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 - } + } } /*****************************************************************************/ @@ -1891,6 +1959,10 @@ 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 + /* try to distribute timeouts somewhat evenly */ ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); @@ -2027,7 +2099,7 @@ : write (req->int1, req->ptr2, req->size); 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, self); break; + case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; @@ -2067,11 +2139,12 @@ 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; + case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; + case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; case EIO_MTOUCH: req->result = eio__mtouch (req); break; case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; - case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; case EIO_READDIR: eio__scandir (req, self); break; @@ -2162,6 +2235,21 @@ REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; } +eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FDATASYNC); req->int1 = fd; SEND; +} + +eio_req *eio_syncfs (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SYNCFS); req->int1 = fd; SEND; +} + +eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; +} + eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) { REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; @@ -2177,21 +2265,11 @@ REQ (EIO_MLOCKALL); req->int1 = flags; SEND; } -eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) -{ - REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; -} - eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data) { REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND; } -eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) -{ - REQ (EIO_FDATASYNC); req->int1 = fd; SEND; -} - eio_req *eio_close (int fd, int pri, eio_cb cb, void *data) { REQ (EIO_CLOSE); req->int1 = fd; SEND; @@ -2427,16 +2505,6 @@ eio_ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) { - etp_worker wrk; - eio_ssize_t ret; - - wrk.dbuf = 0; - - ret = eio__sendfile (ofd, ifd, offset, count, &wrk); - - if (wrk.dbuf) - free (wrk.dbuf); - - return ret; + return eio__sendfile (ofd, ifd, offset, count); }