1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libeio@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann <libeio@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
208 | #endif |
208 | #endif |
209 | |
209 | |
210 | #define D_NAME(entp) entp->d_name |
210 | #define D_NAME(entp) entp->d_name |
211 | |
211 | |
212 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
212 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
213 | #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
213 | #if __FreeBSD__ || __NetBSD__ || __OpenBSD__ |
214 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
214 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
215 | #define D_INO(de) (de)->d_fileno |
215 | #define D_INO(de) (de)->d_fileno |
216 | #define D_NAMLEN(de) (de)->d_namlen |
216 | #define D_NAMLEN(de) (de)->d_namlen |
217 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
217 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
218 | #define D_INO(de) (de)->d_ino |
218 | #define D_INO(de) (de)->d_ino |
… | |
… | |
409 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
409 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
410 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
410 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
411 | |
411 | |
412 | /* worker threads management */ |
412 | /* worker threads management */ |
413 | |
413 | |
414 | static void ecb_cold |
414 | static void |
415 | etp_worker_clear (etp_worker *wrk) |
415 | etp_worker_clear (etp_worker *wrk) |
416 | { |
416 | { |
417 | } |
417 | } |
418 | |
418 | |
419 | static void ecb_cold |
419 | static void ecb_cold |
… | |
… | |
1032 | int res; |
1032 | int res; |
1033 | |
1033 | |
1034 | #if HAVE_SYS_SYNCFS |
1034 | #if HAVE_SYS_SYNCFS |
1035 | res = (int)syscall (__NR_syncfs, (int)(fd)); |
1035 | res = (int)syscall (__NR_syncfs, (int)(fd)); |
1036 | #else |
1036 | #else |
1037 | res = -1; |
1037 | res = EIO_ENOSYS (); |
1038 | errno = ENOSYS; |
|
|
1039 | #endif |
1038 | #endif |
1040 | |
1039 | |
1041 | if (res < 0 && errno == ENOSYS && fd >= 0) |
1040 | if (res < 0 && errno == ENOSYS && fd >= 0) |
1042 | sync (); |
1041 | sync (); |
1043 | |
1042 | |
… | |
… | |
1073 | } |
1072 | } |
1074 | |
1073 | |
1075 | static int |
1074 | static int |
1076 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
1075 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
1077 | { |
1076 | { |
1078 | #if HAVE_FALLOCATE |
1077 | #if HAVE_LINUX_FALLOCATE |
1079 | return fallocate (fd, mode, offset, len); |
1078 | return fallocate (fd, mode, offset, len); |
1080 | #else |
1079 | #else |
1081 | errno = ENOSYS; |
1080 | return EIO_ENOSYS (); |
1082 | return -1; |
|
|
1083 | #endif |
1081 | #endif |
1084 | } |
1082 | } |
1085 | |
1083 | |
1086 | #if !HAVE_READAHEAD |
1084 | #if !HAVE_READAHEAD |
1087 | # undef readahead |
1085 | # undef readahead |
… | |
… | |
1102 | todo -= len; |
1100 | todo -= len; |
1103 | } |
1101 | } |
1104 | |
1102 | |
1105 | FUBd; |
1103 | FUBd; |
1106 | |
1104 | |
1107 | errno = 0; |
1105 | /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */ |
|
|
1106 | /* but not for e.g. EIO or eof, so we also never fail */ |
1108 | return count; |
1107 | return 0; |
1109 | } |
1108 | } |
1110 | |
1109 | |
1111 | #endif |
1110 | #endif |
1112 | |
1111 | |
1113 | /* sendfile always needs emulation */ |
1112 | /* sendfile always needs emulation */ |
… | |
… | |
1148 | |
1147 | |
1149 | /* according to source inspection, this is correct, and useful behaviour */ |
1148 | /* according to source inspection, this is correct, and useful behaviour */ |
1150 | if (sbytes) |
1149 | if (sbytes) |
1151 | res = sbytes; |
1150 | res = sbytes; |
1152 | |
1151 | |
1153 | # elif defined (__APPLE__) |
1152 | # elif defined __APPLE__ |
1154 | off_t sbytes = count; |
1153 | off_t sbytes = count; |
1155 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1154 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1156 | |
1155 | |
1157 | /* according to the manpage, sbytes is always valid */ |
1156 | /* according to the manpage, sbytes is always valid */ |
1158 | if (sbytes) |
1157 | if (sbytes) |
… | |
… | |
1185 | HANDLE h = TO_SOCKET (ifd); |
1184 | HANDLE h = TO_SOCKET (ifd); |
1186 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
1185 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
1187 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
1186 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
1188 | |
1187 | |
1189 | #else |
1188 | #else |
1190 | res = -1; |
1189 | res = EIO_ENOSYS (); |
1191 | errno = ENOSYS; |
|
|
1192 | #endif |
1190 | #endif |
1193 | |
1191 | |
1194 | /* we assume sendfile can copy at least 128mb in one go */ |
1192 | /* we assume sendfile can copy at least 128mb in one go */ |
1195 | if (res <= 128 * 1024 * 1024) |
1193 | if (res <= 128 * 1024 * 1024) |
1196 | { |
1194 | { |
… | |
… | |
1382 | } |
1380 | } |
1383 | |
1381 | |
1384 | /*****************************************************************************/ |
1382 | /*****************************************************************************/ |
1385 | /* requests implemented outside eio_execute, because they are so large */ |
1383 | /* requests implemented outside eio_execute, because they are so large */ |
1386 | |
1384 | |
|
|
1385 | static void |
|
|
1386 | eio__lseek (eio_req *req) |
|
|
1387 | { |
|
|
1388 | /* this usually gets optimised away completely, or your compiler sucks, */ |
|
|
1389 | /* or the whence constants really are not 0, 1, 2 */ |
|
|
1390 | int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET |
|
|
1391 | : req->int2 == EIO_SEEK_CUR ? SEEK_CUR |
|
|
1392 | : req->int2 == EIO_SEEK_END ? SEEK_END |
|
|
1393 | : req->int2; |
|
|
1394 | |
|
|
1395 | req->offs = lseek (req->int1, req->offs, whence); |
|
|
1396 | req->result = req->offs == (off_t)-1 ? -1 : 0; |
|
|
1397 | } |
|
|
1398 | |
1387 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
1399 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
1388 | static int |
1400 | static int |
1389 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1401 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1390 | { |
1402 | { |
1391 | const char *rel = path; |
1403 | const char *rel = path; |
… | |
… | |
1487 | } |
1499 | } |
1488 | } |
1500 | } |
1489 | |
1501 | |
1490 | errno = ENAMETOOLONG; |
1502 | errno = ENAMETOOLONG; |
1491 | if (res + 1 + len + 1 >= tmp1) |
1503 | if (res + 1 + len + 1 >= tmp1) |
1492 | return; |
1504 | return -1; |
1493 | |
1505 | |
1494 | /* copy one component */ |
1506 | /* copy one component */ |
1495 | *res = '/'; |
1507 | *res = '/'; |
1496 | memcpy (res + 1, beg, len); |
1508 | memcpy (res + 1, beg, len); |
1497 | |
1509 | |
… | |
… | |
1966 | { |
1978 | { |
1967 | if (ent->type == EIO_DT_UNKNOWN) |
1979 | if (ent->type == EIO_DT_UNKNOWN) |
1968 | { |
1980 | { |
1969 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1981 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1970 | ent->score = 1; |
1982 | ent->score = 1; |
1971 | else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ |
1983 | else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */ |
1972 | ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ |
1984 | ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ |
1973 | } |
1985 | } |
1974 | else if (ent->type == EIO_DT_DIR) |
1986 | else if (ent->type == EIO_DT_DIR) |
1975 | ent->score = 0; |
1987 | ent->score = 0; |
1976 | } |
1988 | } |
… | |
… | |
2133 | req->result = -1; \ |
2145 | req->result = -1; \ |
2134 | break; \ |
2146 | break; \ |
2135 | } \ |
2147 | } \ |
2136 | } |
2148 | } |
2137 | |
2149 | |
|
|
2150 | static void ecb_noinline ecb_cold |
|
|
2151 | etp_proc_init (void) |
|
|
2152 | { |
|
|
2153 | #if HAVE_PRCTL_SET_NAME |
|
|
2154 | /* provide a more sensible "thread name" */ |
|
|
2155 | char name[16 + 1]; |
|
|
2156 | const int namelen = sizeof (name) - 1; |
|
|
2157 | int len; |
|
|
2158 | |
|
|
2159 | prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
2160 | name [namelen] = 0; |
|
|
2161 | len = strlen (name); |
|
|
2162 | strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio"); |
|
|
2163 | prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
2164 | #endif |
|
|
2165 | } |
|
|
2166 | |
2138 | X_THREAD_PROC (etp_proc) |
2167 | X_THREAD_PROC (etp_proc) |
2139 | { |
2168 | { |
2140 | ETP_REQ *req; |
2169 | ETP_REQ *req; |
2141 | struct timespec ts; |
2170 | struct timespec ts; |
2142 | etp_worker *self = (etp_worker *)thr_arg; |
2171 | etp_worker *self = (etp_worker *)thr_arg; |
2143 | |
2172 | |
2144 | #if HAVE_PRCTL_SET_NAME |
2173 | etp_proc_init (); |
2145 | prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0); |
|
|
2146 | #endif |
|
|
2147 | |
2174 | |
2148 | /* try to distribute timeouts somewhat evenly */ |
2175 | /* try to distribute timeouts somewhat evenly */ |
2149 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
2176 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
2150 | |
2177 | |
2151 | for (;;) |
2178 | for (;;) |
… | |
… | |
2213 | free (req); |
2240 | free (req); |
2214 | |
2241 | |
2215 | X_LOCK (wrklock); |
2242 | X_LOCK (wrklock); |
2216 | etp_worker_free (self); |
2243 | etp_worker_free (self); |
2217 | X_UNLOCK (wrklock); |
2244 | X_UNLOCK (wrklock); |
2218 | |
|
|
2219 | return 0; |
|
|
2220 | } |
2245 | } |
2221 | |
2246 | |
2222 | /*****************************************************************************/ |
2247 | /*****************************************************************************/ |
2223 | |
2248 | |
2224 | int ecb_cold |
2249 | int ecb_cold |
… | |
… | |
2299 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2324 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2300 | break; |
2325 | break; |
2301 | case EIO_WD_CLOSE: req->result = 0; |
2326 | case EIO_WD_CLOSE: req->result = 0; |
2302 | eio_wd_close_sync (req->wd); break; |
2327 | eio_wd_close_sync (req->wd); break; |
2303 | |
2328 | |
|
|
2329 | case EIO_SEEK: eio__lseek (req); break; |
2304 | case EIO_READ: ALLOC (req->size); |
2330 | case EIO_READ: ALLOC (req->size); |
2305 | req->result = req->offs >= 0 |
2331 | req->result = req->offs >= 0 |
2306 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2332 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2307 | : read (req->int1, req->ptr2, req->size); break; |
2333 | : read (req->int1, req->ptr2, req->size); break; |
2308 | case EIO_WRITE: req->result = req->offs >= 0 |
2334 | case EIO_WRITE: req->result = req->offs >= 0 |
… | |
… | |
2464 | case EIO_CUSTOM: |
2490 | case EIO_CUSTOM: |
2465 | req->feed (req); |
2491 | req->feed (req); |
2466 | break; |
2492 | break; |
2467 | |
2493 | |
2468 | default: |
2494 | default: |
2469 | errno = ENOSYS; |
|
|
2470 | req->result = -1; |
2495 | req->result = EIO_ENOSYS (); |
2471 | break; |
2496 | break; |
2472 | } |
2497 | } |
2473 | |
2498 | |
2474 | req->errorno = errno; |
2499 | req->errorno = errno; |
2475 | } |
2500 | } |
2476 | |
2501 | |
2477 | #ifndef EIO_NO_WRAPPERS |
2502 | #ifndef EIO_NO_WRAPPERS |
2478 | |
2503 | |
|
|
2504 | eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data) |
|
|
2505 | { |
|
|
2506 | REQ (EIO_WD_OPEN); PATH; SEND; |
|
|
2507 | } |
|
|
2508 | |
|
|
2509 | eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data) |
|
|
2510 | { |
|
|
2511 | REQ (EIO_WD_CLOSE); req->wd = wd; SEND; |
|
|
2512 | } |
|
|
2513 | |
2479 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
2514 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
2480 | { |
2515 | { |
2481 | REQ (EIO_NOP); SEND; |
2516 | REQ (EIO_NOP); SEND; |
2482 | } |
2517 | } |
2483 | |
2518 | |
… | |
… | |
2542 | } |
2577 | } |
2543 | |
2578 | |
2544 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2579 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2545 | { |
2580 | { |
2546 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
2581 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
|
|
2582 | } |
|
|
2583 | |
|
|
2584 | eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data) |
|
|
2585 | { |
|
|
2586 | REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND; |
2547 | } |
2587 | } |
2548 | |
2588 | |
2549 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2589 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2550 | { |
2590 | { |
2551 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2591 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |