… | |
… | |
54 | #include <stdlib.h> |
54 | #include <stdlib.h> |
55 | #include <string.h> |
55 | #include <string.h> |
56 | #include <errno.h> |
56 | #include <errno.h> |
57 | #include <sys/types.h> |
57 | #include <sys/types.h> |
58 | #include <sys/stat.h> |
58 | #include <sys/stat.h> |
59 | #include <sys/statvfs.h> |
|
|
60 | #include <limits.h> |
59 | #include <limits.h> |
61 | #include <fcntl.h> |
60 | #include <fcntl.h> |
62 | #include <assert.h> |
61 | #include <assert.h> |
63 | |
62 | |
|
|
63 | #include <sys/statvfs.h> |
64 | /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ |
64 | /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ |
65 | /* intptr_t only comes from stdint.h, says idiot openbsd coder */ |
65 | /* intptr_t only comes from stdint.h, says idiot openbsd coder */ |
66 | #if HAVE_STDINT_H |
66 | #if HAVE_STDINT_H |
67 | # include <stdint.h> |
67 | # include <stdint.h> |
68 | #endif |
68 | #endif |
… | |
… | |
83 | |
83 | |
84 | #ifndef EIO_FEED |
84 | #ifndef EIO_FEED |
85 | # define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0) |
85 | # define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0) |
86 | #endif |
86 | #endif |
87 | |
87 | |
|
|
88 | #ifndef EIO_FD_TO_WIN32_HANDLE |
|
|
89 | # define EIO_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) |
|
|
90 | #endif |
|
|
91 | #ifndef EIO_WIN32_HANDLE_TO_FD |
|
|
92 | # define EIO_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0) |
|
|
93 | #endif |
|
|
94 | |
|
|
95 | #define EIO_ERRNO(errval,retval) ((errno = errval), retval) |
|
|
96 | |
|
|
97 | #define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1) |
|
|
98 | |
88 | #ifdef _WIN32 |
99 | #ifdef _WIN32 |
89 | |
100 | |
90 | /*doh*/ |
101 | #define PAGESIZE 4096 /* GetSystemInfo? */ |
|
|
102 | |
|
|
103 | #define stat(path,buf) _stati64 (path,buf) |
|
|
104 | #define lstat(path,buf) stat (path,buf) |
|
|
105 | #define fstat(fd,buf) _fstati64 (path,buf) |
|
|
106 | #define fsync(fd) (FlushFileBuffers (EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1)) |
|
|
107 | #define mkdir(path,mode) _mkdir (path) |
|
|
108 | #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) |
|
|
109 | |
|
|
110 | #define chown(path,uid,gid) EIO_ENOSYS () |
|
|
111 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
|
|
112 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
|
|
113 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
|
|
114 | #define mknod(path,mode,dev) EIO_ENOSYS () |
|
|
115 | #define sync() EIO_ENOSYS () |
|
|
116 | |
|
|
117 | /* we could even stat and see if it exists */ |
|
|
118 | static int |
|
|
119 | symlink (const char *old, const char *neu) |
|
|
120 | { |
|
|
121 | if (CreateSymbolicLink (neu, old, 1)) |
|
|
122 | return 0; |
|
|
123 | |
|
|
124 | if (CreateSymbolicLink (neu, old, 0)) |
|
|
125 | return 0; |
|
|
126 | |
|
|
127 | return EIO_ERRNO (ENOENT, -1); |
|
|
128 | } |
|
|
129 | |
91 | #else |
130 | #else |
92 | |
131 | |
93 | # include <sys/time.h> |
132 | #include <sys/time.h> |
94 | # include <sys/select.h> |
133 | #include <sys/select.h> |
|
|
134 | #include <sys/statvfs.h> |
95 | # include <unistd.h> |
135 | #include <unistd.h> |
96 | # include <utime.h> |
136 | #include <utime.h> |
97 | # include <signal.h> |
137 | #include <signal.h> |
98 | # include <dirent.h> |
138 | #include <dirent.h> |
99 | |
139 | |
100 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
140 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
101 | # include <sys/mman.h> |
141 | #include <sys/mman.h> |
102 | #endif |
142 | #endif |
103 | |
143 | |
104 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
144 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
105 | # if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
145 | #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
106 | # define _DIRENT_HAVE_D_TYPE /* sigh */ |
146 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
107 | # define D_INO(de) (de)->d_fileno |
147 | #define D_INO(de) (de)->d_fileno |
108 | # define D_NAMLEN(de) (de)->d_namlen |
148 | #define D_NAMLEN(de) (de)->d_namlen |
109 | # elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
149 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
110 | # define D_INO(de) (de)->d_ino |
150 | #define D_INO(de) (de)->d_ino |
111 | # endif |
151 | #endif |
112 | |
152 | |
113 | #ifdef _D_EXACT_NAMLEN |
153 | #ifdef _D_EXACT_NAMLEN |
114 | # undef D_NAMLEN |
154 | #undef D_NAMLEN |
115 | # define D_NAMLEN(de) _D_EXACT_NAMLEN (de) |
155 | #define D_NAMLEN(de) _D_EXACT_NAMLEN (de) |
116 | #endif |
156 | #endif |
117 | |
157 | |
118 | # ifdef _DIRENT_HAVE_D_TYPE |
158 | #ifdef _DIRENT_HAVE_D_TYPE |
119 | # define D_TYPE(de) (de)->d_type |
159 | #define D_TYPE(de) (de)->d_type |
120 | # endif |
160 | #endif |
121 | |
161 | |
122 | # ifndef EIO_STRUCT_DIRENT |
162 | #ifndef EIO_STRUCT_DIRENT |
123 | # define EIO_STRUCT_DIRENT struct dirent |
163 | #define EIO_STRUCT_DIRENT struct dirent |
124 | # endif |
164 | #endif |
125 | |
165 | |
126 | #endif |
166 | #endif |
127 | |
167 | |
128 | #if HAVE_SENDFILE |
168 | #if HAVE_SENDFILE |
129 | # if __linux |
169 | # if __linux |
… | |
… | |
381 | } |
421 | } |
382 | |
422 | |
383 | static void ecb_cold |
423 | static void ecb_cold |
384 | etp_thread_init (void) |
424 | etp_thread_init (void) |
385 | { |
425 | { |
|
|
426 | #if !HAVE_PREADWRITE |
|
|
427 | X_MUTEX_CREATE (preadwritelock); |
|
|
428 | #endif |
386 | X_MUTEX_CREATE (wrklock); |
429 | X_MUTEX_CREATE (wrklock); |
387 | X_MUTEX_CREATE (reslock); |
430 | X_MUTEX_CREATE (reslock); |
388 | X_MUTEX_CREATE (reqlock); |
431 | X_MUTEX_CREATE (reqlock); |
389 | X_COND_CREATE (reqwait); |
432 | X_COND_CREATE (reqwait); |
390 | } |
433 | } |
391 | |
434 | |
392 | static void ecb_cold |
435 | static void ecb_cold |
393 | etp_atfork_prepare (void) |
436 | etp_atfork_prepare (void) |
394 | { |
437 | { |
395 | X_LOCK (wrklock); |
|
|
396 | X_LOCK (reqlock); |
|
|
397 | X_LOCK (reslock); |
|
|
398 | #if !HAVE_PREADWRITE |
|
|
399 | X_LOCK (preadwritelock); |
|
|
400 | #endif |
|
|
401 | } |
438 | } |
402 | |
439 | |
403 | static void ecb_cold |
440 | static void ecb_cold |
404 | etp_atfork_parent (void) |
441 | etp_atfork_parent (void) |
405 | { |
442 | { |
406 | #if !HAVE_PREADWRITE |
|
|
407 | X_UNLOCK (preadwritelock); |
|
|
408 | #endif |
|
|
409 | X_UNLOCK (reslock); |
|
|
410 | X_UNLOCK (reqlock); |
|
|
411 | X_UNLOCK (wrklock); |
|
|
412 | } |
443 | } |
413 | |
444 | |
414 | static void ecb_cold |
445 | static void ecb_cold |
415 | etp_atfork_child (void) |
446 | etp_atfork_child (void) |
416 | { |
447 | { |
… | |
… | |
589 | } |
620 | } |
590 | |
621 | |
591 | static void |
622 | static void |
592 | etp_cancel (ETP_REQ *req) |
623 | etp_cancel (ETP_REQ *req) |
593 | { |
624 | { |
594 | X_LOCK (wrklock); |
625 | req->cancelled = 1; |
595 | req->flags |= EIO_FLAG_CANCELLED; |
|
|
596 | X_UNLOCK (wrklock); |
|
|
597 | |
626 | |
598 | eio_grp_cancel (req); |
627 | eio_grp_cancel (req); |
599 | } |
628 | } |
600 | |
629 | |
601 | static void |
630 | static void |
… | |
… | |
950 | /* even though we could play tricks with the flags, it's better to always |
979 | /* even though we could play tricks with the flags, it's better to always |
951 | * call fdatasync, as that matches the expectation of its users best */ |
980 | * call fdatasync, as that matches the expectation of its users best */ |
952 | return fdatasync (fd); |
981 | return fdatasync (fd); |
953 | } |
982 | } |
954 | |
983 | |
|
|
984 | static int |
|
|
985 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
|
|
986 | { |
|
|
987 | #if HAVE_FALLOCATE |
|
|
988 | return fallocate (fd, mode, offset, len); |
|
|
989 | #else |
|
|
990 | errno = ENOSYS; |
|
|
991 | return -1; |
|
|
992 | #endif |
|
|
993 | } |
|
|
994 | |
955 | #if !HAVE_READAHEAD |
995 | #if !HAVE_READAHEAD |
956 | # undef readahead |
996 | # undef readahead |
957 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
997 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
958 | |
998 | |
959 | static ssize_t |
999 | static ssize_t |
… | |
… | |
987 | if (!count) |
1027 | if (!count) |
988 | return 0; |
1028 | return 0; |
989 | |
1029 | |
990 | for (;;) |
1030 | for (;;) |
991 | { |
1031 | { |
|
|
1032 | #ifdef __APPLE__ |
|
|
1033 | # undef HAVE_SENDFILE /* broken, as everything on os x */ |
|
|
1034 | #endif |
992 | #if HAVE_SENDFILE |
1035 | #if HAVE_SENDFILE |
993 | # if __linux |
1036 | # if __linux |
994 | off_t soffset = offset; |
1037 | off_t soffset = offset; |
995 | res = sendfile (ofd, ifd, &soffset, count); |
1038 | res = sendfile (ofd, ifd, &soffset, count); |
996 | |
1039 | |
… | |
… | |
1012 | |
1055 | |
1013 | /* according to source inspection, this is correct, and useful behaviour */ |
1056 | /* according to source inspection, this is correct, and useful behaviour */ |
1014 | if (sbytes) |
1057 | if (sbytes) |
1015 | res = sbytes; |
1058 | res = sbytes; |
1016 | |
1059 | |
1017 | # elif defined (__APPLE__) && 0 /* broken, as everything on os x */ |
1060 | # elif defined (__APPLE__) |
1018 | off_t sbytes = count; |
1061 | off_t sbytes = count; |
1019 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1062 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1020 | |
1063 | |
1021 | /* according to the manpage, sbytes is always valid */ |
1064 | /* according to the manpage, sbytes is always valid */ |
1022 | if (sbytes) |
1065 | if (sbytes) |
… | |
… | |
1039 | if (res < 0 && sbytes) |
1082 | if (res < 0 && sbytes) |
1040 | res = sbytes; |
1083 | res = sbytes; |
1041 | |
1084 | |
1042 | # endif |
1085 | # endif |
1043 | |
1086 | |
1044 | #elif defined (_WIN32) |
1087 | #elif defined (_WIN32) && 0 |
1045 | /* does not work, just for documentation of what would need to be done */ |
1088 | /* does not work, just for documentation of what would need to be done */ |
1046 | /* actually, cannot be done like this, as TransmitFile changes the file offset, */ |
1089 | /* actually, cannot be done like this, as TransmitFile changes the file offset, */ |
1047 | /* libeio guarantees that the file offset does not change, and windows */ |
1090 | /* libeio guarantees that the file offset does not change, and windows */ |
1048 | /* has no way to get an independent handle to the same file description */ |
1091 | /* has no way to get an independent handle to the same file description */ |
1049 | HANDLE h = TO_SOCKET (ifd); |
1092 | HANDLE h = TO_SOCKET (ifd); |
… | |
… | |
1154 | /* round up length */ |
1197 | /* round up length */ |
1155 | *length = (*length + mask) & ~mask; |
1198 | *length = (*length + mask) & ~mask; |
1156 | } |
1199 | } |
1157 | |
1200 | |
1158 | #if !_POSIX_MEMLOCK |
1201 | #if !_POSIX_MEMLOCK |
1159 | # define eio__mlockall(a) ((errno = ENOSYS), -1) |
1202 | # define eio__mlockall(a) eio_nosyscall() |
1160 | #else |
1203 | #else |
1161 | |
1204 | |
1162 | static int |
1205 | static int |
1163 | eio__mlockall (int flags) |
1206 | eio__mlockall (int flags) |
1164 | { |
1207 | { |
… | |
… | |
1178 | return mlockall (flags); |
1221 | return mlockall (flags); |
1179 | } |
1222 | } |
1180 | #endif |
1223 | #endif |
1181 | |
1224 | |
1182 | #if !_POSIX_MEMLOCK_RANGE |
1225 | #if !_POSIX_MEMLOCK_RANGE |
1183 | # define eio__mlock(a,b) ((errno = ENOSYS), -1) |
1226 | # define eio__mlock(a,b) EIO_ENOSYS () |
1184 | #else |
1227 | #else |
1185 | |
1228 | |
1186 | static int |
1229 | static int |
1187 | eio__mlock (void *addr, size_t length) |
1230 | eio__mlock (void *addr, size_t length) |
1188 | { |
1231 | { |
… | |
… | |
1192 | } |
1235 | } |
1193 | |
1236 | |
1194 | #endif |
1237 | #endif |
1195 | |
1238 | |
1196 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
1239 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
1197 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
1240 | # define eio__msync(a,b,c) EIO_ENOSYS () |
1198 | #else |
1241 | #else |
1199 | |
1242 | |
1200 | static int |
1243 | static int |
1201 | eio__msync (void *mem, size_t len, int flags) |
1244 | eio__msync (void *mem, size_t len, int flags) |
1202 | { |
1245 | { |
… | |
… | |
1791 | X_THREAD_PROC (etp_proc) |
1834 | X_THREAD_PROC (etp_proc) |
1792 | { |
1835 | { |
1793 | ETP_REQ *req; |
1836 | ETP_REQ *req; |
1794 | struct timespec ts; |
1837 | struct timespec ts; |
1795 | etp_worker *self = (etp_worker *)thr_arg; |
1838 | etp_worker *self = (etp_worker *)thr_arg; |
|
|
1839 | int timeout; |
1796 | |
1840 | |
1797 | /* try to distribute timeouts somewhat randomly */ |
1841 | /* try to distribute timeouts somewhat evenly */ |
1798 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1842 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1799 | |
1843 | |
1800 | for (;;) |
1844 | for (;;) |
1801 | { |
1845 | { |
|
|
1846 | ts.tv_sec = 0; |
|
|
1847 | |
1802 | X_LOCK (reqlock); |
1848 | X_LOCK (reqlock); |
1803 | |
1849 | |
1804 | for (;;) |
1850 | for (;;) |
1805 | { |
1851 | { |
1806 | self->req = req = reqq_shift (&req_queue); |
1852 | self->req = req = reqq_shift (&req_queue); |
1807 | |
1853 | |
1808 | if (req) |
1854 | if (req) |
1809 | break; |
1855 | break; |
1810 | |
1856 | |
|
|
1857 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
|
|
1858 | { |
|
|
1859 | X_UNLOCK (reqlock); |
|
|
1860 | X_LOCK (wrklock); |
|
|
1861 | --started; |
|
|
1862 | X_UNLOCK (wrklock); |
|
|
1863 | goto quit; |
|
|
1864 | } |
|
|
1865 | |
1811 | ++idle; |
1866 | ++idle; |
1812 | |
1867 | |
1813 | ts.tv_sec = time (0) + idle_timeout; |
1868 | if (idle <= max_idle) |
1814 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
1869 | /* we are allowed to idle, so do so without any timeout */ |
|
|
1870 | X_COND_WAIT (reqwait, reqlock); |
|
|
1871 | else |
1815 | { |
1872 | { |
1816 | if (idle > max_idle) |
1873 | /* initialise timeout once */ |
1817 | { |
1874 | if (!ts.tv_sec) |
1818 | --idle; |
1875 | ts.tv_sec = time (0) + idle_timeout; |
1819 | X_UNLOCK (reqlock); |
|
|
1820 | X_LOCK (wrklock); |
|
|
1821 | --started; |
|
|
1822 | X_UNLOCK (wrklock); |
|
|
1823 | goto quit; |
|
|
1824 | } |
|
|
1825 | |
1876 | |
1826 | /* we are allowed to idle, so do so without any timeout */ |
|
|
1827 | X_COND_WAIT (reqwait, reqlock); |
1877 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
|
|
1878 | ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ |
1828 | } |
1879 | } |
1829 | |
1880 | |
1830 | --idle; |
1881 | --idle; |
1831 | } |
1882 | } |
1832 | |
1883 | |
… | |
… | |
1962 | case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; |
2013 | case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; |
1963 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
2014 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
1964 | case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; |
2015 | case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; |
1965 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
2016 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
1966 | case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; |
2017 | case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; |
|
|
2018 | case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; |
1967 | |
2019 | |
1968 | case EIO_READDIR: eio__scandir (req, self); break; |
2020 | case EIO_READDIR: eio__scandir (req, self); break; |
1969 | |
2021 | |
1970 | case EIO_BUSY: |
2022 | case EIO_BUSY: |
1971 | #ifdef _WIN32 |
2023 | #ifdef _WIN32 |
… | |
… | |
2071 | eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
2123 | eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
2072 | { |
2124 | { |
2073 | REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; |
2125 | REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; |
2074 | } |
2126 | } |
2075 | |
2127 | |
|
|
2128 | eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data) |
|
|
2129 | { |
|
|
2130 | REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND; |
|
|
2131 | } |
|
|
2132 | |
2076 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
2133 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
2077 | { |
2134 | { |
2078 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
2135 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
2079 | } |
2136 | } |
2080 | |
2137 | |