ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libeio/eio.c
(Generate patch)

Comparing cvsroot/libeio/eio.c (file contents):
Revision 1.106 by root, Mon Sep 26 20:19:08 2011 UTC vs.
Revision 1.121 by root, Sat Jun 2 20:13:26 2012 UTC

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 *
58#include <sys/stat.h> 58#include <sys/stat.h>
59#include <limits.h> 59#include <limits.h>
60#include <fcntl.h> 60#include <fcntl.h>
61#include <assert.h> 61#include <assert.h>
62 62
63#if _POSIX_VERSION >= 200809L
64# define HAVE_AT 1
65#else
66# define HAVE_AT 0
67#endif
68
69/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
70/* intptr_t only comes from stdint.h, says idiot openbsd coder */ 64/* intptr_t only comes from stdint.h, says idiot openbsd coder */
71#if HAVE_STDINT_H 65#if HAVE_STDINT_H
72# include <stdint.h> 66# include <stdint.h>
73#endif 67#endif
214 #endif 208 #endif
215 209
216 #define D_NAME(entp) entp->d_name 210 #define D_NAME(entp) entp->d_name
217 211
218 /* 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 */
219 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 213 #if __FreeBSD__ || __NetBSD__ || __OpenBSD__
220 #define _DIRENT_HAVE_D_TYPE /* sigh */ 214 #define _DIRENT_HAVE_D_TYPE /* sigh */
221 #define D_INO(de) (de)->d_fileno 215 #define D_INO(de) (de)->d_fileno
222 #define D_NAMLEN(de) (de)->d_namlen 216 #define D_NAMLEN(de) (de)->d_namlen
223 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 217 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
224 #define D_INO(de) (de)->d_ino 218 #define D_INO(de) (de)->d_ino
318 } 312 }
319 313
320 return buf->ptr; 314 return buf->ptr;
321} 315}
322 316
317struct tmpbuf;
318
319#if _POSIX_VERSION >= 200809L
320 #define HAVE_AT 1
321 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
322 #ifndef O_SEARCH
323 #define O_SEARCH O_RDONLY
324 #endif
325#else
326 #define HAVE_AT 0
327 static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path);
328#endif
329
330struct eio_pwd
331{
332#if HAVE_AT
333 int fd;
334#endif
335 int len;
336 char str[1]; /* actually, a 0-terminated canonical path */
337};
338
323/*****************************************************************************/ 339/*****************************************************************************/
324 340
325#define ETP_PRI_MIN EIO_PRI_MIN 341#define ETP_PRI_MIN EIO_PRI_MIN
326#define ETP_PRI_MAX EIO_PRI_MAX 342#define ETP_PRI_MAX EIO_PRI_MAX
327 343
363static xmutex_t wrklock; 379static xmutex_t wrklock;
364static xmutex_t reslock; 380static xmutex_t reslock;
365static xmutex_t reqlock; 381static xmutex_t reqlock;
366static xcond_t reqwait; 382static xcond_t reqwait;
367 383
368#if !HAVE_PREADWRITE
369/*
370 * make our pread/pwrite emulation safe against themselves, but not against
371 * normal read/write by using a mutex. slows down execution a lot,
372 * but that's your problem, not mine.
373 */
374static xmutex_t preadwritelock;
375#endif
376
377typedef struct etp_worker 384typedef struct etp_worker
378{ 385{
379 struct tmpbuf tmpbuf; 386 struct tmpbuf tmpbuf;
380 387
381 /* locked by wrklock */ 388 /* locked by wrklock */
393#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 400#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
394#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 401#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
395 402
396/* worker threads management */ 403/* worker threads management */
397 404
398static void ecb_cold 405static void
399etp_worker_clear (etp_worker *wrk) 406etp_worker_clear (etp_worker *wrk)
400{ 407{
401} 408}
402 409
403static void ecb_cold 410static void ecb_cold
927} 934}
928 935
929/*****************************************************************************/ 936/*****************************************************************************/
930/* work around various missing functions */ 937/* work around various missing functions */
931 938
932#if !HAVE_PREADWRITE
933# undef pread
934# undef pwrite
935# define pread eio__pread
936# define pwrite eio__pwrite
937
938static eio_ssize_t
939eio__pread (int fd, void *buf, size_t count, off_t offset)
940{
941 eio_ssize_t res;
942 off_t ooffset;
943
944 X_LOCK (preadwritelock);
945 ooffset = lseek (fd, 0, SEEK_CUR);
946 lseek (fd, offset, SEEK_SET);
947 res = read (fd, buf, count);
948 lseek (fd, ooffset, SEEK_SET);
949 X_UNLOCK (preadwritelock);
950
951 return res;
952}
953
954static eio_ssize_t
955eio__pwrite (int fd, void *buf, size_t count, off_t offset)
956{
957 eio_ssize_t res;
958 off_t ooffset;
959
960 X_LOCK (preadwritelock);
961 ooffset = lseek (fd, 0, SEEK_CUR);
962 lseek (fd, offset, SEEK_SET);
963 res = write (fd, buf, count);
964 lseek (fd, ooffset, SEEK_SET);
965 X_UNLOCK (preadwritelock);
966
967 return res;
968}
969#endif
970
971#ifndef HAVE_UTIMES 939#ifndef HAVE_UTIMES
972 940
973# undef utimes 941# undef utimes
974# define utimes(path,times) eio__utimes (path, times) 942# define utimes(path,times) eio__utimes (path, times)
975 943
1016 int res; 984 int res;
1017 985
1018#if HAVE_SYS_SYNCFS 986#if HAVE_SYS_SYNCFS
1019 res = (int)syscall (__NR_syncfs, (int)(fd)); 987 res = (int)syscall (__NR_syncfs, (int)(fd));
1020#else 988#else
1021 res = -1; 989 res = EIO_ENOSYS ();
1022 errno = ENOSYS;
1023#endif 990#endif
1024 991
1025 if (res < 0 && errno == ENOSYS && fd >= 0) 992 if (res < 0 && errno == ENOSYS && fd >= 0)
1026 sync (); 993 sync ();
1027 994
1057} 1024}
1058 1025
1059static int 1026static int
1060eio__fallocate (int fd, int mode, off_t offset, size_t len) 1027eio__fallocate (int fd, int mode, off_t offset, size_t len)
1061{ 1028{
1062#if HAVE_FALLOCATE 1029#if HAVE_LINUX_FALLOCATE
1063 return fallocate (fd, mode, offset, len); 1030 return fallocate (fd, mode, offset, len);
1064#else 1031#else
1065 errno = ENOSYS; 1032 return EIO_ENOSYS ();
1066 return -1;
1067#endif 1033#endif
1068} 1034}
1069 1035
1070#if !HAVE_READAHEAD 1036#if !HAVE_READAHEAD
1071# undef readahead 1037# undef readahead
1086 todo -= len; 1052 todo -= len;
1087 } 1053 }
1088 1054
1089 FUBd; 1055 FUBd;
1090 1056
1091 errno = 0; 1057 /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */
1058 /* but not for e.g. EIO or eof, so we also never fail */
1092 return count; 1059 return 0;
1093} 1060}
1094 1061
1095#endif 1062#endif
1096 1063
1097/* sendfile always needs emulation */ 1064/* sendfile always needs emulation */
1132 1099
1133 /* according to source inspection, this is correct, and useful behaviour */ 1100 /* according to source inspection, this is correct, and useful behaviour */
1134 if (sbytes) 1101 if (sbytes)
1135 res = sbytes; 1102 res = sbytes;
1136 1103
1137# elif defined (__APPLE__) 1104# elif defined __APPLE__
1138 off_t sbytes = count; 1105 off_t sbytes = count;
1139 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1106 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
1140 1107
1141 /* according to the manpage, sbytes is always valid */ 1108 /* according to the manpage, sbytes is always valid */
1142 if (sbytes) 1109 if (sbytes)
1169 HANDLE h = TO_SOCKET (ifd); 1136 HANDLE h = TO_SOCKET (ifd);
1170 SetFilePointer (h, offset, 0, FILE_BEGIN); 1137 SetFilePointer (h, offset, 0, FILE_BEGIN);
1171 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1138 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1172 1139
1173#else 1140#else
1174 res = -1; 1141 res = EIO_ENOSYS ();
1175 errno = ENOSYS;
1176#endif 1142#endif
1177 1143
1178 /* we assume sendfile can copy at least 128mb in one go */ 1144 /* we assume sendfile can copy at least 128mb in one go */
1179 if (res <= 128 * 1024 * 1024) 1145 if (res <= 128 * 1024 * 1024)
1180 { 1146 {
1366} 1332}
1367 1333
1368/*****************************************************************************/ 1334/*****************************************************************************/
1369/* requests implemented outside eio_execute, because they are so large */ 1335/* requests implemented outside eio_execute, because they are so large */
1370 1336
1371/* copies some absolute path to tmpbuf */ 1337static void
1372static char * 1338eio__lseek (eio_req *req)
1373eio__getwd (struct tmpbuf *tmpbuf, eio_wd wd)
1374{ 1339{
1375 if (wd == EIO_CWD) 1340 /* this usually gets optimised away completely, or your compiler sucks, */
1376 return getcwd (tmpbuf->ptr, PATH_MAX); 1341 /* or the whence constants really are not 0, 1, 2 */
1342 int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET
1343 : req->int2 == EIO_SEEK_CUR ? SEEK_CUR
1344 : req->int2 == EIO_SEEK_END ? SEEK_END
1345 : req->int2;
1377 1346
1378#if HAVE_AT 1347 req->offs = lseek (req->int1, req->offs, whence);
1379 abort (); /*TODO*/ 1348 req->result = req->offs == (off_t)-1 ? -1 : 0;
1380#else
1381 strcpy (tmpbuf->ptr, wd);
1382#endif
1383 return tmpbuf->ptr;
1384} 1349}
1385 1350
1386/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 1351/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1387static int 1352static int
1388eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1353eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
1394 int symlinks = SYMLOOP_MAX; 1359 int symlinks = SYMLOOP_MAX;
1395#else 1360#else
1396 int symlinks = 32; 1361 int symlinks = 32;
1397#endif 1362#endif
1398 1363
1399 /*D*/ /*TODO: wd ignored */
1400
1401 errno = EINVAL; 1364 errno = EINVAL;
1402 if (!rel) 1365 if (!rel)
1403 return -1; 1366 return -1;
1404 1367
1405 errno = ENOENT; 1368 errno = ENOENT;
1433#endif 1396#endif
1434#endif 1397#endif
1435 1398
1436 if (*rel != '/') 1399 if (*rel != '/')
1437 { 1400 {
1438 if (!eio__getwd (tmpbuf, wd)) 1401 int len;
1402
1403 errno = ENOENT;
1404 if (wd == EIO_INVALID_WD)
1439 return -1; 1405 return -1;
1406
1407 if (wd == EIO_CWD)
1408 {
1409 if (!getcwd (res, PATH_MAX))
1410 return -1;
1411
1412 len = strlen (res);
1413 }
1414 else
1415 memcpy (res, wd->str, len = wd->len);
1440 1416
1441 if (res [1]) /* only use if not / */ 1417 if (res [1]) /* only use if not / */
1442 res += strlen (res); 1418 res += len;
1443 } 1419 }
1444 1420
1445 while (*rel) 1421 while (*rel)
1446 { 1422 {
1447 eio_ssize_t len, linklen; 1423 eio_ssize_t len, linklen;
1475 } 1451 }
1476 } 1452 }
1477 1453
1478 errno = ENAMETOOLONG; 1454 errno = ENAMETOOLONG;
1479 if (res + 1 + len + 1 >= tmp1) 1455 if (res + 1 + len + 1 >= tmp1)
1480 return; 1456 return -1;
1481 1457
1482 /* copy one component */ 1458 /* copy one component */
1483 *res = '/'; 1459 *res = '/';
1484 memcpy (res + 1, beg, len); 1460 memcpy (res + 1, beg, len);
1485 1461
1694 eio_dent_insertion_sort (dents, size); 1670 eio_dent_insertion_sort (dents, size);
1695} 1671}
1696 1672
1697/* read a full directory */ 1673/* read a full directory */
1698static void 1674static void
1699eio__scandir (eio_req *req) 1675eio__scandir (eio_req *req, etp_worker *self)
1700{ 1676{
1701 char *name, *names; 1677 char *name, *names;
1702 int namesalloc = 4096 - sizeof (void *) * 4; 1678 int namesalloc = 4096 - sizeof (void *) * 4;
1703 int namesoffs = 0; 1679 int namesoffs = 0;
1704 int flags = req->int1; 1680 int flags = req->int1;
1722#ifdef _WIN32 1698#ifdef _WIN32
1723 { 1699 {
1724 int len = strlen ((const char *)req->ptr1); 1700 int len = strlen ((const char *)req->ptr1);
1725 char *path = malloc (MAX_PATH); 1701 char *path = malloc (MAX_PATH);
1726 const char *fmt; 1702 const char *fmt;
1703 const char *reqpath = wd_expand (&self->tmpbuf, req->wd, req->ptr1);
1727 1704
1728 if (!len) 1705 if (!len)
1729 fmt = "./*"; 1706 fmt = "./*";
1730 else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\') 1707 else if (reqpath[len - 1] == '/' || reqpath[len - 1] == '\\')
1731 fmt = "%s*"; 1708 fmt = "%s*";
1732 else 1709 else
1733 fmt = "%s/*"; 1710 fmt = "%s/*";
1734 1711
1735 _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1); 1712 _snprintf (path, MAX_PATH, fmt, reqpath);
1736 dirp = FindFirstFile (path, &entp); 1713 dirp = FindFirstFile (path, &entp);
1737 free (path); 1714 free (path);
1738 1715
1739 if (dirp == INVALID_HANDLE_VALUE) 1716 if (dirp == INVALID_HANDLE_VALUE)
1740 { 1717 {
1762 1739
1763 return; 1740 return;
1764 } 1741 }
1765 } 1742 }
1766#else 1743#else
1744 #if HAVE_AT
1745 if (req->wd)
1746 {
1747 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY);
1748
1749 if (fd < 0)
1750 return;
1751
1752 dirp = fdopendir (fd);
1753
1754 if (!dirp)
1755 close (fd);
1756 }
1757 else
1767 dirp = opendir (req->ptr1); 1758 dirp = opendir (req->ptr1);
1759 #else
1760 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
1761 #endif
1768 1762
1769 if (!dirp) 1763 if (!dirp)
1770 return; 1764 return;
1771#endif 1765#endif
1772 1766
1936 { 1930 {
1937 if (ent->type == EIO_DT_UNKNOWN) 1931 if (ent->type == EIO_DT_UNKNOWN)
1938 { 1932 {
1939 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ 1933 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1940 ent->score = 1; 1934 ent->score = 1;
1941 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ 1935 else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */
1942 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ 1936 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */
1943 } 1937 }
1944 else if (ent->type == EIO_DT_DIR) 1938 else if (ent->type == EIO_DT_DIR)
1945 ent->score = 0; 1939 ent->score = 0;
1946 } 1940 }
1966 } 1960 }
1967} 1961}
1968 1962
1969/*****************************************************************************/ 1963/*****************************************************************************/
1970/* working directory stuff */ 1964/* working directory stuff */
1965/* various deficiencies in the posix 2008 api force us to */
1966/* keep the absolute path in string form at all times */
1967/* fuck yeah. */
1968
1969#if !HAVE_AT
1970
1971/* a bit like realpath, but usually faster because it doesn'T have to return */
1972/* an absolute or canonical path */
1973static const char *
1974wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
1975{
1976 if (!wd || *path == '/')
1977 return path;
1978
1979 if (path [0] == '.' && !path [1])
1980 return wd->str;
1981
1982 {
1983 int l1 = wd->len;
1984 int l2 = strlen (path);
1985
1986 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2);
1987
1988 memcpy (res, wd->str, l1);
1989 res [l1] = '/';
1990 memcpy (res + l1 + 1, path, l2 + 1);
1991
1992 return res;
1993 }
1994}
1995
1996#endif
1997
1998static eio_wd
1999eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2000{
2001 int fd;
2002 eio_wd res;
2003 int len = eio__realpath (tmpbuf, wd, path);
2004
2005 if (len < 0)
2006 return EIO_INVALID_WD;
1971 2007
1972#if HAVE_AT 2008#if HAVE_AT
2009 fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY);
1973 2010
1974#define WD2FD(wd) ((wd) ? ((int)(long)(wd)) - 1 : AT_FDCWD) 2011 if (fd < 0)
1975 2012 return EIO_INVALID_WD;
1976#ifndef O_SEARCH
1977# define O_SEARCH O_RDONLY
1978#endif 2013#endif
2014
2015 res = malloc (sizeof (*res) + len); /* one extra 0-byte */
2016
2017#if HAVE_AT
2018 res->fd = fd;
2019#endif
2020
2021 res->len = len;
2022 memcpy (res->str, tmpbuf->ptr, len);
2023 res->str [len] = 0;
2024
2025 return res;
2026}
1979 2027
1980eio_wd 2028eio_wd
1981eio_wd_open_sync (eio_wd wd, const char *path) 2029eio_wd_open_sync (eio_wd wd, const char *path)
1982{ 2030{
1983 int fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 2031 struct tmpbuf tmpbuf = { 0 };
1984
1985 return fd >= 0 ? (eio_wd)(long)(fd + 1) : EIO_INVALID_WD;
1986}
1987
1988static eio_wd
1989eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
1990{
1991 return eio_wd_open_sync (wd, path); 2032 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2033 free (tmpbuf.ptr);
2034
2035 return wd;
1992} 2036}
1993 2037
1994void 2038void
1995eio_wd_close_sync (eio_wd wd) 2039eio_wd_close_sync (eio_wd wd)
1996{ 2040{
1997 int fd = WD2FD (wd); 2041 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
1998 2042 {
1999 if (fd >= 0) 2043 #if HAVE_AT
2000 close (fd); 2044 close (wd->fd);
2045 #endif
2046 free (wd);
2047 }
2001} 2048}
2049
2050#if HAVE_AT
2051
2052/* they forgot these */
2002 2053
2003static int 2054static int
2004eio__truncateat (int dirfd, const char *path, off_t length) 2055eio__truncateat (int dirfd, const char *path, off_t length)
2005{ 2056{
2006 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); 2057 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC);
2025 2076
2026 res = fstatvfs (fd, buf); 2077 res = fstatvfs (fd, buf);
2027 close (fd); 2078 close (fd);
2028 return res; 2079 return res;
2029 2080
2030}
2031
2032#else
2033
2034/* on legacy systems, we represent the working directories simply by their path strings */
2035
2036static const char *
2037wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2038{
2039 if (!wd || *path == '/')
2040 return path;
2041
2042 {
2043 int l1 = strlen ((const char *)wd);
2044 int l2 = strlen (path);
2045
2046 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2);
2047
2048 memcpy (res, wd, l1);
2049 res [l1] = '/';
2050 memcpy (res + l1 + 1, path, l2 + 1);
2051
2052 return res;
2053 }
2054}
2055
2056static eio_wd
2057eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2058{
2059 if (*path == '/') /* absolute paths ignore wd */
2060 path = strdup (path);
2061 else if (path [0] == '.' && !path [1]) /* special case '.', as it is common */
2062 return wd;
2063 else
2064 {
2065 int len = eio__realpath (tmpbuf, wd, path);
2066
2067 path = EIO_INVALID_WD;
2068
2069 if (len >= 0)
2070 {
2071 ((char *)tmpbuf->ptr)[len] = 0;
2072 path = strdup (tmpbuf->ptr);
2073 }
2074 }
2075
2076 if (!path)
2077 path = EIO_INVALID_WD;
2078
2079 return (eio_wd)path;
2080}
2081
2082eio_wd
2083eio_wd_open_sync (eio_wd wd, const char *path)
2084{
2085 struct tmpbuf tmpbuf = { 0 };
2086 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2087 free (tmpbuf.ptr);
2088
2089 return wd;
2090}
2091
2092void
2093eio_wd_close_sync (eio_wd wd)
2094{
2095 if (wd != EIO_INVALID_WD)
2096 free (wd);
2097} 2081}
2098 2082
2099#endif 2083#endif
2100 2084
2101/*****************************************************************************/ 2085/*****************************************************************************/
2113 req->result = -1; \ 2097 req->result = -1; \
2114 break; \ 2098 break; \
2115 } \ 2099 } \
2116 } 2100 }
2117 2101
2102static void ecb_noinline ecb_cold
2103etp_proc_init (void)
2104{
2105#if HAVE_PRCTL_SET_NAME
2106 /* provide a more sensible "thread name" */
2107 char name[16 + 1];
2108 const int namelen = sizeof (name) - 1;
2109 int len;
2110
2111 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0);
2112 name [namelen] = 0;
2113 len = strlen (name);
2114 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2115 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2116#endif
2117}
2118
2118X_THREAD_PROC (etp_proc) 2119X_THREAD_PROC (etp_proc)
2119{ 2120{
2120 ETP_REQ *req; 2121 ETP_REQ *req;
2121 struct timespec ts; 2122 struct timespec ts;
2122 etp_worker *self = (etp_worker *)thr_arg; 2123 etp_worker *self = (etp_worker *)thr_arg;
2123 2124
2124#if HAVE_PRCTL_SET_NAME 2125 etp_proc_init ();
2125 prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0);
2126#endif
2127 2126
2128 /* try to distribute timeouts somewhat evenly */ 2127 /* try to distribute timeouts somewhat evenly */
2129 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 2128 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2130 2129
2131 for (;;) 2130 for (;;)
2193 free (req); 2192 free (req);
2194 2193
2195 X_LOCK (wrklock); 2194 X_LOCK (wrklock);
2196 etp_worker_free (self); 2195 etp_worker_free (self);
2197 X_UNLOCK (wrklock); 2196 X_UNLOCK (wrklock);
2198
2199 return 0;
2200} 2197}
2201 2198
2202/*****************************************************************************/ 2199/*****************************************************************************/
2203 2200
2204int ecb_cold 2201int ecb_cold
2205eio_init (void (*want_poll)(void), void (*done_poll)(void)) 2202eio_init (void (*want_poll)(void), void (*done_poll)(void))
2206{ 2203{
2207#if !HAVE_PREADWRITE
2208 X_MUTEX_CREATE (preadwritelock);
2209#endif
2210
2211 return etp_init (want_poll, done_poll); 2204 return etp_init (want_poll, done_poll);
2212} 2205}
2213 2206
2214ecb_inline void 2207ecb_inline void
2215eio_api_destroy (eio_req *req) 2208eio_api_destroy (eio_req *req)
2216{ 2209{
2217 free (req); 2210 free (req);
2218} 2211}
2219 2212
2220#define REQ(rtype) \ 2213#define REQ(rtype) \
2221 eio_req *req; \ 2214 eio_req *req; \
2222 \ 2215 \
2223 req = (eio_req *)calloc (1, sizeof *req); \ 2216 req = (eio_req *)calloc (1, sizeof *req); \
2224 if (!req) \ 2217 if (!req) \
2225 return 0; \ 2218 return 0; \
2279 req->result = req->wd == EIO_INVALID_WD ? -1 : 0; 2272 req->result = req->wd == EIO_INVALID_WD ? -1 : 0;
2280 break; 2273 break;
2281 case EIO_WD_CLOSE: req->result = 0; 2274 case EIO_WD_CLOSE: req->result = 0;
2282 eio_wd_close_sync (req->wd); break; 2275 eio_wd_close_sync (req->wd); break;
2283 2276
2277 case EIO_SEEK: eio__lseek (req); break;
2284 case EIO_READ: ALLOC (req->size); 2278 case EIO_READ: ALLOC (req->size);
2285 req->result = req->offs >= 0 2279 req->result = req->offs >= 0
2286 ? pread (req->int1, req->ptr2, req->size, req->offs) 2280 ? pread (req->int1, req->ptr2, req->size, req->offs)
2287 : read (req->int1, req->ptr2, req->size); break; 2281 : read (req->int1, req->ptr2, req->size); break;
2288 case EIO_WRITE: req->result = req->offs >= 0 2282 case EIO_WRITE: req->result = req->offs >= 0
2304 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; 2298 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
2305 2299
2306 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 2300 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2307 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 2301 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2308 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 2302 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
2309 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2); break; 2303 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break;
2310 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2, 0); break; 2304 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break;
2311 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; 2305 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break;
2312 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 2306 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
2313 case EIO_READLINK: ALLOC (PATH_MAX); 2307 case EIO_READLINK: ALLOC (PATH_MAX);
2314 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; 2308 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2315 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 2309 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2415 case EIO_MTOUCH: req->result = eio__mtouch (req); break; 2409 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
2416 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 2410 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
2417 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 2411 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
2418 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; 2412 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
2419 2413
2420 case EIO_READDIR: eio__scandir (req); break; 2414 case EIO_READDIR: eio__scandir (req, self); break;
2421 2415
2422 case EIO_BUSY: 2416 case EIO_BUSY:
2423#ifdef _WIN32 2417#ifdef _WIN32
2424 Sleep (req->nv1 * 1e3); 2418 Sleep (req->nv1 * 1e3);
2425#else 2419#else
2444 case EIO_CUSTOM: 2438 case EIO_CUSTOM:
2445 req->feed (req); 2439 req->feed (req);
2446 break; 2440 break;
2447 2441
2448 default: 2442 default:
2449 errno = ENOSYS;
2450 req->result = -1; 2443 req->result = EIO_ENOSYS ();
2451 break; 2444 break;
2452 } 2445 }
2453 2446
2454 req->errorno = errno; 2447 req->errorno = errno;
2455} 2448}
2456 2449
2457#ifndef EIO_NO_WRAPPERS 2450#ifndef EIO_NO_WRAPPERS
2458 2451
2452eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data)
2453{
2454 REQ (EIO_WD_OPEN); PATH; SEND;
2455}
2456
2457eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data)
2458{
2459 REQ (EIO_WD_CLOSE); req->wd = wd; SEND;
2460}
2461
2459eio_req *eio_nop (int pri, eio_cb cb, void *data) 2462eio_req *eio_nop (int pri, eio_cb cb, void *data)
2460{ 2463{
2461 REQ (EIO_NOP); SEND; 2464 REQ (EIO_NOP); SEND;
2462} 2465}
2463 2466
2522} 2525}
2523 2526
2524eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) 2527eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
2525{ 2528{
2526 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; 2529 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
2530}
2531
2532eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data)
2533{
2534 REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND;
2527} 2535}
2528 2536
2529eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2537eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2530{ 2538{
2531 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2539 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines