--- libeio/eio.c 2011/07/18 01:27:03 1.93 +++ libeio/eio.c 2011/08/03 15:25:38 1.101 @@ -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; @@ -1648,6 +1710,7 @@ { dirp = 0; + /* should steal _dosmaperr */ switch (GetLastError ()) { case ERROR_FILE_NOT_FOUND: @@ -1891,6 +1954,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 +2094,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 +2134,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 +2230,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 +2260,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 +2500,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); }