… | |
… | |
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 form 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 |
69 | |
69 | |
|
|
70 | #ifndef ECANCELED |
|
|
71 | # define ECANCELED EDOM |
|
|
72 | #endif |
|
|
73 | |
|
|
74 | static void eio_destroy (eio_req *req); |
|
|
75 | |
70 | #ifndef EIO_FINISH |
76 | #ifndef EIO_FINISH |
71 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
77 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
72 | #endif |
78 | #endif |
73 | |
79 | |
74 | #ifndef EIO_DESTROY |
80 | #ifndef EIO_DESTROY |
… | |
… | |
77 | |
83 | |
78 | #ifndef EIO_FEED |
84 | #ifndef EIO_FEED |
79 | # 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) |
80 | #endif |
86 | #endif |
81 | |
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 | |
82 | #ifdef _WIN32 |
99 | #ifdef _WIN32 |
83 | |
100 | |
84 | /*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 | |
85 | #else |
130 | #else |
86 | |
131 | |
87 | # include <sys/time.h> |
132 | #include <sys/time.h> |
88 | # include <sys/select.h> |
133 | #include <sys/select.h> |
|
|
134 | #include <sys/statvfs.h> |
89 | # include <unistd.h> |
135 | #include <unistd.h> |
90 | # include <utime.h> |
136 | #include <utime.h> |
91 | # include <signal.h> |
137 | #include <signal.h> |
92 | # include <dirent.h> |
138 | #include <dirent.h> |
93 | |
139 | |
94 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
140 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
95 | # include <sys/mman.h> |
141 | #include <sys/mman.h> |
96 | #endif |
142 | #endif |
97 | |
143 | |
98 | /* 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 */ |
99 | # if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
145 | #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
100 | # define _DIRENT_HAVE_D_TYPE /* sigh */ |
146 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
101 | # define D_INO(de) (de)->d_fileno |
147 | #define D_INO(de) (de)->d_fileno |
102 | # define D_NAMLEN(de) (de)->d_namlen |
148 | #define D_NAMLEN(de) (de)->d_namlen |
103 | # elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
149 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
104 | # define D_INO(de) (de)->d_ino |
150 | #define D_INO(de) (de)->d_ino |
105 | # endif |
151 | #endif |
106 | |
152 | |
107 | #ifdef _D_EXACT_NAMLEN |
153 | #ifdef _D_EXACT_NAMLEN |
108 | # undef D_NAMLEN |
154 | #undef D_NAMLEN |
109 | # define D_NAMLEN(de) _D_EXACT_NAMLEN (de) |
155 | #define D_NAMLEN(de) _D_EXACT_NAMLEN (de) |
110 | #endif |
156 | #endif |
111 | |
157 | |
112 | # ifdef _DIRENT_HAVE_D_TYPE |
158 | #ifdef _DIRENT_HAVE_D_TYPE |
113 | # define D_TYPE(de) (de)->d_type |
159 | #define D_TYPE(de) (de)->d_type |
114 | # endif |
160 | #endif |
115 | |
161 | |
116 | # ifndef EIO_STRUCT_DIRENT |
162 | #ifndef EIO_STRUCT_DIRENT |
117 | # define EIO_STRUCT_DIRENT struct dirent |
163 | #define EIO_STRUCT_DIRENT struct dirent |
118 | # endif |
164 | #endif |
119 | |
165 | |
120 | #endif |
166 | #endif |
121 | |
167 | |
122 | #if HAVE_SENDFILE |
168 | #if HAVE_SENDFILE |
123 | # if __linux |
169 | # if __linux |
… | |
… | |
375 | } |
421 | } |
376 | |
422 | |
377 | static void ecb_cold |
423 | static void ecb_cold |
378 | etp_thread_init (void) |
424 | etp_thread_init (void) |
379 | { |
425 | { |
|
|
426 | #if !HAVE_PREADWRITE |
|
|
427 | X_MUTEX_CREATE (preadwritelock); |
|
|
428 | #endif |
380 | X_MUTEX_CREATE (wrklock); |
429 | X_MUTEX_CREATE (wrklock); |
381 | X_MUTEX_CREATE (reslock); |
430 | X_MUTEX_CREATE (reslock); |
382 | X_MUTEX_CREATE (reqlock); |
431 | X_MUTEX_CREATE (reqlock); |
383 | X_COND_CREATE (reqwait); |
432 | X_COND_CREATE (reqwait); |
384 | } |
433 | } |
385 | |
434 | |
386 | static void ecb_cold |
435 | static void ecb_cold |
387 | etp_atfork_prepare (void) |
436 | etp_atfork_prepare (void) |
388 | { |
437 | { |
389 | X_LOCK (wrklock); |
|
|
390 | X_LOCK (reqlock); |
|
|
391 | X_LOCK (reslock); |
|
|
392 | #if !HAVE_PREADWRITE |
|
|
393 | X_LOCK (preadwritelock); |
|
|
394 | #endif |
|
|
395 | } |
438 | } |
396 | |
439 | |
397 | static void ecb_cold |
440 | static void ecb_cold |
398 | etp_atfork_parent (void) |
441 | etp_atfork_parent (void) |
399 | { |
442 | { |
400 | #if !HAVE_PREADWRITE |
|
|
401 | X_UNLOCK (preadwritelock); |
|
|
402 | #endif |
|
|
403 | X_UNLOCK (reslock); |
|
|
404 | X_UNLOCK (reqlock); |
|
|
405 | X_UNLOCK (wrklock); |
|
|
406 | } |
443 | } |
407 | |
444 | |
408 | static void ecb_cold |
445 | static void ecb_cold |
409 | etp_atfork_child (void) |
446 | etp_atfork_child (void) |
410 | { |
447 | { |
… | |
… | |
583 | } |
620 | } |
584 | |
621 | |
585 | static void |
622 | static void |
586 | etp_cancel (ETP_REQ *req) |
623 | etp_cancel (ETP_REQ *req) |
587 | { |
624 | { |
588 | X_LOCK (wrklock); |
625 | req->cancelled = 1; |
589 | req->flags |= EIO_FLAG_CANCELLED; |
|
|
590 | X_UNLOCK (wrklock); |
|
|
591 | |
626 | |
592 | eio_grp_cancel (req); |
627 | eio_grp_cancel (req); |
593 | } |
628 | } |
594 | |
629 | |
595 | static void |
630 | static void |
… | |
… | |
711 | return eio_finish (grp); |
746 | return eio_finish (grp); |
712 | else |
747 | else |
713 | return 0; |
748 | return 0; |
714 | } |
749 | } |
715 | |
750 | |
716 | void |
751 | static void |
717 | eio_destroy (eio_req *req) |
752 | eio_destroy (eio_req *req) |
718 | { |
753 | { |
719 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
754 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
720 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
755 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
721 | |
756 | |
… | |
… | |
739 | if (grp->grp_first == req) |
774 | if (grp->grp_first == req) |
740 | grp->grp_first = req->grp_next; |
775 | grp->grp_first = req->grp_next; |
741 | |
776 | |
742 | res2 = grp_dec (grp); |
777 | res2 = grp_dec (grp); |
743 | |
778 | |
744 | if (!res && res2) |
779 | if (!res) |
745 | res = res2; |
780 | res = res2; |
746 | } |
781 | } |
747 | |
782 | |
748 | eio_destroy (req); |
783 | eio_destroy (req); |
749 | |
784 | |
… | |
… | |
944 | /* 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 |
945 | * call fdatasync, as that matches the expectation of its users best */ |
980 | * call fdatasync, as that matches the expectation of its users best */ |
946 | return fdatasync (fd); |
981 | return fdatasync (fd); |
947 | } |
982 | } |
948 | |
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 | |
949 | #if !HAVE_READAHEAD |
995 | #if !HAVE_READAHEAD |
950 | # undef readahead |
996 | # undef readahead |
951 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
997 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
952 | |
998 | |
953 | static ssize_t |
999 | static ssize_t |
… | |
… | |
981 | if (!count) |
1027 | if (!count) |
982 | return 0; |
1028 | return 0; |
983 | |
1029 | |
984 | for (;;) |
1030 | for (;;) |
985 | { |
1031 | { |
|
|
1032 | #ifdef __APPLE__ |
|
|
1033 | # undef HAVE_SENDFILE /* broken, as everything on os x */ |
|
|
1034 | #endif |
986 | #if HAVE_SENDFILE |
1035 | #if HAVE_SENDFILE |
987 | # if __linux |
1036 | # if __linux |
988 | off_t soffset = offset; |
1037 | off_t soffset = offset; |
989 | res = sendfile (ofd, ifd, &soffset, count); |
1038 | res = sendfile (ofd, ifd, &soffset, count); |
990 | |
1039 | |
… | |
… | |
1033 | if (res < 0 && sbytes) |
1082 | if (res < 0 && sbytes) |
1034 | res = sbytes; |
1083 | res = sbytes; |
1035 | |
1084 | |
1036 | # endif |
1085 | # endif |
1037 | |
1086 | |
1038 | #elif defined (_WIN32) |
1087 | #elif defined (_WIN32) && 0 |
1039 | /* 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 */ |
1040 | /* 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, */ |
1041 | /* libeio guarantees that the file offset does not change, and windows */ |
1090 | /* libeio guarantees that the file offset does not change, and windows */ |
1042 | /* 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 */ |
1043 | HANDLE h = TO_SOCKET (ifd); |
1092 | HANDLE h = TO_SOCKET (ifd); |
… | |
… | |
1115 | count -= cnt; |
1164 | count -= cnt; |
1116 | } |
1165 | } |
1117 | } |
1166 | } |
1118 | |
1167 | |
1119 | return res; |
1168 | return res; |
|
|
1169 | } |
|
|
1170 | |
|
|
1171 | #ifdef PAGESIZE |
|
|
1172 | # define eio_pagesize() PAGESIZE |
|
|
1173 | #else |
|
|
1174 | static intptr_t |
|
|
1175 | eio_pagesize (void) |
|
|
1176 | { |
|
|
1177 | static intptr_t page; |
|
|
1178 | |
|
|
1179 | if (!page) |
|
|
1180 | page = sysconf (_SC_PAGESIZE); |
|
|
1181 | |
|
|
1182 | return page; |
|
|
1183 | } |
|
|
1184 | #endif |
|
|
1185 | |
|
|
1186 | static void |
|
|
1187 | eio_page_align (void **addr, size_t *length) |
|
|
1188 | { |
|
|
1189 | intptr_t mask = eio_pagesize () - 1; |
|
|
1190 | |
|
|
1191 | /* round down addr */ |
|
|
1192 | intptr_t adj = mask & (intptr_t)*addr; |
|
|
1193 | |
|
|
1194 | *addr = (void *)((intptr_t)*addr - adj); |
|
|
1195 | *length += adj; |
|
|
1196 | |
|
|
1197 | /* round up length */ |
|
|
1198 | *length = (*length + mask) & ~mask; |
|
|
1199 | } |
|
|
1200 | |
|
|
1201 | #if !_POSIX_MEMLOCK |
|
|
1202 | # define eio__mlockall(a) eio_nosyscall() |
|
|
1203 | #else |
|
|
1204 | |
|
|
1205 | static int |
|
|
1206 | eio__mlockall (int flags) |
|
|
1207 | { |
|
|
1208 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
|
|
1209 | extern int mallopt (int, int); |
|
|
1210 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
|
|
1211 | #endif |
|
|
1212 | |
|
|
1213 | if (EIO_MCL_CURRENT != MCL_CURRENT |
|
|
1214 | || EIO_MCL_FUTURE != MCL_FUTURE) |
|
|
1215 | { |
|
|
1216 | flags = 0 |
|
|
1217 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
|
|
1218 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
|
|
1219 | } |
|
|
1220 | |
|
|
1221 | return mlockall (flags); |
|
|
1222 | } |
|
|
1223 | #endif |
|
|
1224 | |
|
|
1225 | #if !_POSIX_MEMLOCK_RANGE |
|
|
1226 | # define eio__mlock(a,b) EIO_ENOSYS () |
|
|
1227 | #else |
|
|
1228 | |
|
|
1229 | static int |
|
|
1230 | eio__mlock (void *addr, size_t length) |
|
|
1231 | { |
|
|
1232 | eio_page_align (&addr, &length); |
|
|
1233 | |
|
|
1234 | return mlock (addr, length); |
|
|
1235 | } |
|
|
1236 | |
|
|
1237 | #endif |
|
|
1238 | |
|
|
1239 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
|
|
1240 | # define eio__msync(a,b,c) EIO_ENOSYS () |
|
|
1241 | #else |
|
|
1242 | |
|
|
1243 | static int |
|
|
1244 | eio__msync (void *mem, size_t len, int flags) |
|
|
1245 | { |
|
|
1246 | eio_page_align (&mem, &len); |
|
|
1247 | |
|
|
1248 | if (EIO_MS_ASYNC != MS_SYNC |
|
|
1249 | || EIO_MS_INVALIDATE != MS_INVALIDATE |
|
|
1250 | || EIO_MS_SYNC != MS_SYNC) |
|
|
1251 | { |
|
|
1252 | flags = 0 |
|
|
1253 | | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) |
|
|
1254 | | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) |
|
|
1255 | | (flags & EIO_MS_SYNC ? MS_SYNC : 0); |
|
|
1256 | } |
|
|
1257 | |
|
|
1258 | return msync (mem, len, flags); |
|
|
1259 | } |
|
|
1260 | |
|
|
1261 | #endif |
|
|
1262 | |
|
|
1263 | static int |
|
|
1264 | eio__mtouch (eio_req *req) |
|
|
1265 | { |
|
|
1266 | void *mem = req->ptr2; |
|
|
1267 | size_t len = req->size; |
|
|
1268 | int flags = req->int1; |
|
|
1269 | |
|
|
1270 | eio_page_align (&mem, &len); |
|
|
1271 | |
|
|
1272 | { |
|
|
1273 | intptr_t addr = (intptr_t)mem; |
|
|
1274 | intptr_t end = addr + len; |
|
|
1275 | intptr_t page = eio_pagesize (); |
|
|
1276 | |
|
|
1277 | if (addr < end) |
|
|
1278 | if (flags & EIO_MT_MODIFY) /* modify */ |
|
|
1279 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1280 | else |
|
|
1281 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1282 | } |
|
|
1283 | |
|
|
1284 | return 0; |
|
|
1285 | } |
|
|
1286 | |
|
|
1287 | /*****************************************************************************/ |
|
|
1288 | /* requests implemented outside eio_execute, because they are so large */ |
|
|
1289 | |
|
|
1290 | static void |
|
|
1291 | eio__realpath (eio_req *req, etp_worker *self) |
|
|
1292 | { |
|
|
1293 | char *rel = req->ptr1; |
|
|
1294 | char *res; |
|
|
1295 | char *tmp1, *tmp2; |
|
|
1296 | #if SYMLOOP_MAX > 32 |
|
|
1297 | int symlinks = SYMLOOP_MAX; |
|
|
1298 | #else |
|
|
1299 | int symlinks = 32; |
|
|
1300 | #endif |
|
|
1301 | |
|
|
1302 | req->result = -1; |
|
|
1303 | |
|
|
1304 | errno = EINVAL; |
|
|
1305 | if (!rel) |
|
|
1306 | return; |
|
|
1307 | |
|
|
1308 | errno = ENOENT; |
|
|
1309 | if (!*rel) |
|
|
1310 | return; |
|
|
1311 | |
|
|
1312 | if (!req->ptr2) |
|
|
1313 | { |
|
|
1314 | X_LOCK (wrklock); |
|
|
1315 | req->flags |= EIO_FLAG_PTR2_FREE; |
|
|
1316 | X_UNLOCK (wrklock); |
|
|
1317 | req->ptr2 = malloc (PATH_MAX * 3); |
|
|
1318 | |
|
|
1319 | errno = ENOMEM; |
|
|
1320 | if (!req->ptr2) |
|
|
1321 | return; |
|
|
1322 | } |
|
|
1323 | |
|
|
1324 | res = req->ptr2; |
|
|
1325 | tmp1 = res + PATH_MAX; |
|
|
1326 | tmp2 = tmp1 + PATH_MAX; |
|
|
1327 | |
|
|
1328 | #if 0 /* disabled, the musl way to do things is just too racy */ |
|
|
1329 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
|
|
1330 | /* on linux we may be able to ask the kernel */ |
|
|
1331 | { |
|
|
1332 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
|
|
1333 | |
|
|
1334 | if (fd >= 0) |
|
|
1335 | { |
|
|
1336 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
|
|
1337 | req->result = readlink (tmp1, res, PATH_MAX); |
|
|
1338 | close (fd); |
|
|
1339 | |
|
|
1340 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
|
|
1341 | |
|
|
1342 | if (req->result > 0) |
|
|
1343 | goto done; |
|
|
1344 | } |
|
|
1345 | else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) |
|
|
1346 | return; |
|
|
1347 | } |
|
|
1348 | #endif |
|
|
1349 | #endif |
|
|
1350 | |
|
|
1351 | if (*rel != '/') |
|
|
1352 | { |
|
|
1353 | if (!getcwd (res, PATH_MAX)) |
|
|
1354 | return; |
|
|
1355 | |
|
|
1356 | if (res [1]) /* only use if not / */ |
|
|
1357 | res += strlen (res); |
|
|
1358 | } |
|
|
1359 | |
|
|
1360 | while (*rel) |
|
|
1361 | { |
|
|
1362 | ssize_t len, linklen; |
|
|
1363 | char *beg = rel; |
|
|
1364 | |
|
|
1365 | while (*rel && *rel != '/') |
|
|
1366 | ++rel; |
|
|
1367 | |
|
|
1368 | len = rel - beg; |
|
|
1369 | |
|
|
1370 | if (!len) /* skip slashes */ |
|
|
1371 | { |
|
|
1372 | ++rel; |
|
|
1373 | continue; |
|
|
1374 | } |
|
|
1375 | |
|
|
1376 | if (beg [0] == '.') |
|
|
1377 | { |
|
|
1378 | if (len == 1) |
|
|
1379 | continue; /* . - nop */ |
|
|
1380 | |
|
|
1381 | if (beg [1] == '.' && len == 2) |
|
|
1382 | { |
|
|
1383 | /* .. - back up one component, if possible */ |
|
|
1384 | |
|
|
1385 | while (res != req->ptr2) |
|
|
1386 | if (*--res == '/') |
|
|
1387 | break; |
|
|
1388 | |
|
|
1389 | continue; |
|
|
1390 | } |
|
|
1391 | } |
|
|
1392 | |
|
|
1393 | errno = ENAMETOOLONG; |
|
|
1394 | if (res + 1 + len + 1 >= tmp1) |
|
|
1395 | return; |
|
|
1396 | |
|
|
1397 | /* copy one component */ |
|
|
1398 | *res = '/'; |
|
|
1399 | memcpy (res + 1, beg, len); |
|
|
1400 | |
|
|
1401 | /* zero-terminate, for readlink */ |
|
|
1402 | res [len + 1] = 0; |
|
|
1403 | |
|
|
1404 | /* now check if it's a symlink */ |
|
|
1405 | linklen = readlink (req->ptr2, tmp1, PATH_MAX); |
|
|
1406 | |
|
|
1407 | if (linklen < 0) |
|
|
1408 | { |
|
|
1409 | if (errno != EINVAL) |
|
|
1410 | return; |
|
|
1411 | |
|
|
1412 | /* it's a normal directory. hopefully */ |
|
|
1413 | res += len + 1; |
|
|
1414 | } |
|
|
1415 | else |
|
|
1416 | { |
|
|
1417 | /* yay, it was a symlink - build new path in tmp2 */ |
|
|
1418 | int rellen = strlen (rel); |
|
|
1419 | |
|
|
1420 | errno = ENAMETOOLONG; |
|
|
1421 | if (linklen + 1 + rellen >= PATH_MAX) |
|
|
1422 | return; |
|
|
1423 | |
|
|
1424 | errno = ELOOP; |
|
|
1425 | if (!--symlinks) |
|
|
1426 | return; |
|
|
1427 | |
|
|
1428 | if (*tmp1 == '/') |
|
|
1429 | res = req->ptr2; /* symlink resolves to an absolute path */ |
|
|
1430 | |
|
|
1431 | /* we need to be careful, as rel might point into tmp2 already */ |
|
|
1432 | memmove (tmp2 + linklen + 1, rel, rellen + 1); |
|
|
1433 | tmp2 [linklen] = '/'; |
|
|
1434 | memcpy (tmp2, tmp1, linklen); |
|
|
1435 | |
|
|
1436 | rel = tmp2; |
|
|
1437 | } |
|
|
1438 | } |
|
|
1439 | |
|
|
1440 | /* special case for the lone root path */ |
|
|
1441 | if (res == req->ptr2) |
|
|
1442 | *res++ = '/'; |
|
|
1443 | |
|
|
1444 | req->result = res - (char *)req->ptr2; |
|
|
1445 | |
|
|
1446 | done: |
|
|
1447 | req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */ |
1120 | } |
1448 | } |
1121 | |
1449 | |
1122 | static signed char |
1450 | static signed char |
1123 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1451 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1124 | { |
1452 | { |
… | |
… | |
1484 | break; |
1812 | break; |
1485 | } |
1813 | } |
1486 | } |
1814 | } |
1487 | } |
1815 | } |
1488 | |
1816 | |
1489 | #ifdef PAGESIZE |
|
|
1490 | # define eio_pagesize() PAGESIZE |
|
|
1491 | #else |
|
|
1492 | static intptr_t |
|
|
1493 | eio_pagesize (void) |
|
|
1494 | { |
|
|
1495 | static intptr_t page; |
|
|
1496 | |
|
|
1497 | if (!page) |
|
|
1498 | page = sysconf (_SC_PAGESIZE); |
|
|
1499 | |
|
|
1500 | return page; |
|
|
1501 | } |
|
|
1502 | #endif |
|
|
1503 | |
|
|
1504 | static void |
|
|
1505 | eio_page_align (void **addr, size_t *length) |
|
|
1506 | { |
|
|
1507 | intptr_t mask = eio_pagesize () - 1; |
|
|
1508 | |
|
|
1509 | /* round down addr */ |
|
|
1510 | intptr_t adj = mask & (intptr_t)*addr; |
|
|
1511 | |
|
|
1512 | *addr = (void *)((intptr_t)*addr - adj); |
|
|
1513 | *length += adj; |
|
|
1514 | |
|
|
1515 | /* round up length */ |
|
|
1516 | *length = (*length + mask) & ~mask; |
|
|
1517 | } |
|
|
1518 | |
|
|
1519 | #if !_POSIX_MEMLOCK |
|
|
1520 | # define eio__mlockall(a) ((errno = ENOSYS), -1) |
|
|
1521 | #else |
|
|
1522 | |
|
|
1523 | static int |
|
|
1524 | eio__mlockall (int flags) |
|
|
1525 | { |
|
|
1526 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
|
|
1527 | extern int mallopt (int, int); |
|
|
1528 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
|
|
1529 | #endif |
|
|
1530 | |
|
|
1531 | if (EIO_MCL_CURRENT != MCL_CURRENT |
|
|
1532 | || EIO_MCL_FUTURE != MCL_FUTURE) |
|
|
1533 | { |
|
|
1534 | flags = 0 |
|
|
1535 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
|
|
1536 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
|
|
1537 | } |
|
|
1538 | |
|
|
1539 | return mlockall (flags); |
|
|
1540 | } |
|
|
1541 | #endif |
|
|
1542 | |
|
|
1543 | #if !_POSIX_MEMLOCK_RANGE |
|
|
1544 | # define eio__mlock(a,b) ((errno = ENOSYS), -1) |
|
|
1545 | #else |
|
|
1546 | |
|
|
1547 | static int |
|
|
1548 | eio__mlock (void *addr, size_t length) |
|
|
1549 | { |
|
|
1550 | eio_page_align (&addr, &length); |
|
|
1551 | |
|
|
1552 | return mlock (addr, length); |
|
|
1553 | } |
|
|
1554 | |
|
|
1555 | #endif |
|
|
1556 | |
|
|
1557 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
|
|
1558 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
|
|
1559 | #else |
|
|
1560 | |
|
|
1561 | static int |
|
|
1562 | eio__msync (void *mem, size_t len, int flags) |
|
|
1563 | { |
|
|
1564 | eio_page_align (&mem, &len); |
|
|
1565 | |
|
|
1566 | if (EIO_MS_ASYNC != MS_SYNC |
|
|
1567 | || EIO_MS_INVALIDATE != MS_INVALIDATE |
|
|
1568 | || EIO_MS_SYNC != MS_SYNC) |
|
|
1569 | { |
|
|
1570 | flags = 0 |
|
|
1571 | | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) |
|
|
1572 | | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) |
|
|
1573 | | (flags & EIO_MS_SYNC ? MS_SYNC : 0); |
|
|
1574 | } |
|
|
1575 | |
|
|
1576 | return msync (mem, len, flags); |
|
|
1577 | } |
|
|
1578 | |
|
|
1579 | #endif |
|
|
1580 | |
|
|
1581 | static int |
|
|
1582 | eio__mtouch (eio_req *req) |
|
|
1583 | { |
|
|
1584 | void *mem = req->ptr2; |
|
|
1585 | size_t len = req->size; |
|
|
1586 | int flags = req->int1; |
|
|
1587 | |
|
|
1588 | eio_page_align (&mem, &len); |
|
|
1589 | |
|
|
1590 | { |
|
|
1591 | intptr_t addr = (intptr_t)mem; |
|
|
1592 | intptr_t end = addr + len; |
|
|
1593 | intptr_t page = eio_pagesize (); |
|
|
1594 | |
|
|
1595 | if (addr < end) |
|
|
1596 | if (flags & EIO_MT_MODIFY) /* modify */ |
|
|
1597 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1598 | else |
|
|
1599 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1600 | } |
|
|
1601 | |
|
|
1602 | return 0; |
|
|
1603 | } |
|
|
1604 | |
|
|
1605 | /*****************************************************************************/ |
1817 | /*****************************************************************************/ |
1606 | |
1818 | |
1607 | #define ALLOC(len) \ |
1819 | #define ALLOC(len) \ |
1608 | if (!req->ptr2) \ |
1820 | if (!req->ptr2) \ |
1609 | { \ |
1821 | { \ |
… | |
… | |
1622 | X_THREAD_PROC (etp_proc) |
1834 | X_THREAD_PROC (etp_proc) |
1623 | { |
1835 | { |
1624 | ETP_REQ *req; |
1836 | ETP_REQ *req; |
1625 | struct timespec ts; |
1837 | struct timespec ts; |
1626 | etp_worker *self = (etp_worker *)thr_arg; |
1838 | etp_worker *self = (etp_worker *)thr_arg; |
|
|
1839 | int timeout; |
1627 | |
1840 | |
1628 | /* try to distribute timeouts somewhat randomly */ |
1841 | /* try to distribute timeouts somewhat evenly */ |
1629 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1842 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1630 | |
1843 | |
1631 | for (;;) |
1844 | for (;;) |
1632 | { |
1845 | { |
|
|
1846 | ts.tv_sec = 0; |
|
|
1847 | |
1633 | X_LOCK (reqlock); |
1848 | X_LOCK (reqlock); |
1634 | |
1849 | |
1635 | for (;;) |
1850 | for (;;) |
1636 | { |
1851 | { |
1637 | self->req = req = reqq_shift (&req_queue); |
1852 | self->req = req = reqq_shift (&req_queue); |
1638 | |
1853 | |
1639 | if (req) |
1854 | if (req) |
1640 | break; |
1855 | break; |
1641 | |
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 | |
1642 | ++idle; |
1866 | ++idle; |
1643 | |
1867 | |
1644 | ts.tv_sec = time (0) + idle_timeout; |
1868 | if (idle <= max_idle) |
1645 | 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 |
1646 | { |
1872 | { |
1647 | if (idle > max_idle) |
1873 | /* initialise timeout once */ |
1648 | { |
1874 | if (!ts.tv_sec) |
1649 | --idle; |
1875 | ts.tv_sec = time (0) + idle_timeout; |
1650 | X_UNLOCK (reqlock); |
|
|
1651 | X_LOCK (wrklock); |
|
|
1652 | --started; |
|
|
1653 | X_UNLOCK (wrklock); |
|
|
1654 | goto quit; |
|
|
1655 | } |
|
|
1656 | |
1876 | |
1657 | /* we are allowed to idle, so do so without any timeout */ |
|
|
1658 | 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.,.. */ |
1659 | } |
1879 | } |
1660 | |
1880 | |
1661 | --idle; |
1881 | --idle; |
1662 | } |
1882 | } |
1663 | |
1883 | |
… | |
… | |
1666 | X_UNLOCK (reqlock); |
1886 | X_UNLOCK (reqlock); |
1667 | |
1887 | |
1668 | if (req->type < 0) |
1888 | if (req->type < 0) |
1669 | goto quit; |
1889 | goto quit; |
1670 | |
1890 | |
1671 | if (!EIO_CANCELLED (req)) |
|
|
1672 | ETP_EXECUTE (self, req); |
1891 | ETP_EXECUTE (self, req); |
1673 | |
1892 | |
1674 | X_LOCK (reslock); |
1893 | X_LOCK (reslock); |
1675 | |
1894 | |
1676 | ++npending; |
1895 | ++npending; |
1677 | |
1896 | |
… | |
… | |
1731 | } |
1950 | } |
1732 | |
1951 | |
1733 | static void |
1952 | static void |
1734 | eio_execute (etp_worker *self, eio_req *req) |
1953 | eio_execute (etp_worker *self, eio_req *req) |
1735 | { |
1954 | { |
|
|
1955 | if (ecb_expect_false (EIO_CANCELLED (req))) |
|
|
1956 | { |
|
|
1957 | req->result = -1; |
|
|
1958 | req->errorno = ECANCELED; |
|
|
1959 | return; |
|
|
1960 | } |
|
|
1961 | |
1736 | switch (req->type) |
1962 | switch (req->type) |
1737 | { |
1963 | { |
1738 | case EIO_READ: ALLOC (req->size); |
1964 | case EIO_READ: ALLOC (req->size); |
1739 | req->result = req->offs >= 0 |
1965 | req->result = req->offs >= 0 |
1740 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1966 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
… | |
… | |
1774 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
2000 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
1775 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
2001 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
1776 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
2002 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
1777 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
2003 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1778 | |
2004 | |
|
|
2005 | case EIO_REALPATH: eio__realpath (req, self); break; |
|
|
2006 | |
1779 | case EIO_READLINK: ALLOC (PATH_MAX); |
2007 | case EIO_READLINK: ALLOC (PATH_MAX); |
1780 | req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; |
2008 | req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; |
1781 | |
2009 | |
1782 | case EIO_SYNC: req->result = 0; sync (); break; |
2010 | case EIO_SYNC: req->result = 0; sync (); break; |
1783 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
2011 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
… | |
… | |
1785 | 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; |
1786 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
2014 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
1787 | 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; |
1788 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
2016 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
1789 | 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; |
1790 | |
2019 | |
1791 | case EIO_READDIR: eio__scandir (req, self); break; |
2020 | case EIO_READDIR: eio__scandir (req, self); break; |
1792 | |
2021 | |
1793 | case EIO_BUSY: |
2022 | case EIO_BUSY: |
1794 | #ifdef _WIN32 |
2023 | #ifdef _WIN32 |
… | |
… | |
1894 | 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) |
1895 | { |
2124 | { |
1896 | 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; |
1897 | } |
2126 | } |
1898 | |
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 | |
1899 | 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) |
1900 | { |
2134 | { |
1901 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
2135 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
1902 | } |
2136 | } |
1903 | |
2137 | |
… | |
… | |
1998 | } |
2232 | } |
1999 | |
2233 | |
2000 | eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) |
2234 | eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) |
2001 | { |
2235 | { |
2002 | return eio__1path (EIO_READLINK, path, pri, cb, data); |
2236 | return eio__1path (EIO_READLINK, path, pri, cb, data); |
|
|
2237 | } |
|
|
2238 | |
|
|
2239 | eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data) |
|
|
2240 | { |
|
|
2241 | return eio__1path (EIO_REALPATH, path, pri, cb, data); |
2003 | } |
2242 | } |
2004 | |
2243 | |
2005 | eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) |
2244 | eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) |
2006 | { |
2245 | { |
2007 | return eio__1path (EIO_STAT, path, pri, cb, data); |
2246 | return eio__1path (EIO_STAT, path, pri, cb, data); |