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 |
… | |
… | |
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 | { |
… | |
… | |
1381 | return 0; |
1379 | return 0; |
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 */ |
|
|
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 | } |
1386 | |
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 | { |
… | |
… | |
2228 | free (req); |
2240 | free (req); |
2229 | |
2241 | |
2230 | X_LOCK (wrklock); |
2242 | X_LOCK (wrklock); |
2231 | etp_worker_free (self); |
2243 | etp_worker_free (self); |
2232 | X_UNLOCK (wrklock); |
2244 | X_UNLOCK (wrklock); |
2233 | |
|
|
2234 | return 0; |
|
|
2235 | } |
2245 | } |
2236 | |
2246 | |
2237 | /*****************************************************************************/ |
2247 | /*****************************************************************************/ |
2238 | |
2248 | |
2239 | int ecb_cold |
2249 | int ecb_cold |
… | |
… | |
2314 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2324 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2315 | break; |
2325 | break; |
2316 | case EIO_WD_CLOSE: req->result = 0; |
2326 | case EIO_WD_CLOSE: req->result = 0; |
2317 | eio_wd_close_sync (req->wd); break; |
2327 | eio_wd_close_sync (req->wd); break; |
2318 | |
2328 | |
|
|
2329 | case EIO_SEEK: eio__lseek (req); break; |
2319 | case EIO_READ: ALLOC (req->size); |
2330 | case EIO_READ: ALLOC (req->size); |
2320 | req->result = req->offs >= 0 |
2331 | req->result = req->offs >= 0 |
2321 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2332 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2322 | : read (req->int1, req->ptr2, req->size); break; |
2333 | : read (req->int1, req->ptr2, req->size); break; |
2323 | case EIO_WRITE: req->result = req->offs >= 0 |
2334 | case EIO_WRITE: req->result = req->offs >= 0 |
… | |
… | |
2479 | case EIO_CUSTOM: |
2490 | case EIO_CUSTOM: |
2480 | req->feed (req); |
2491 | req->feed (req); |
2481 | break; |
2492 | break; |
2482 | |
2493 | |
2483 | default: |
2494 | default: |
2484 | errno = ENOSYS; |
|
|
2485 | req->result = -1; |
2495 | req->result = EIO_ENOSYS (); |
2486 | break; |
2496 | break; |
2487 | } |
2497 | } |
2488 | |
2498 | |
2489 | req->errorno = errno; |
2499 | req->errorno = errno; |
2490 | } |
2500 | } |
… | |
… | |
2567 | } |
2577 | } |
2568 | |
2578 | |
2569 | 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) |
2570 | { |
2580 | { |
2571 | 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; |
2572 | } |
2587 | } |
2573 | |
2588 | |
2574 | 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) |
2575 | { |
2590 | { |
2576 | 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; |