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 | * |
… | |
… | |
132 | #define sync() EIO_ENOSYS () |
132 | #define sync() EIO_ENOSYS () |
133 | #define readlink(path,buf,s) EIO_ENOSYS () |
133 | #define readlink(path,buf,s) EIO_ENOSYS () |
134 | #define statvfs(path,buf) EIO_ENOSYS () |
134 | #define statvfs(path,buf) EIO_ENOSYS () |
135 | #define fstatvfs(fd,buf) EIO_ENOSYS () |
135 | #define fstatvfs(fd,buf) EIO_ENOSYS () |
136 | |
136 | |
|
|
137 | #define pread(fd,buf,count,offset) eio__pread (fd, buf, count, offset) |
|
|
138 | #define pwrite(fd,buf,count,offset) eio__pwrite (fd, buf, count, offset) |
|
|
139 | |
|
|
140 | #if __GNUC__ |
|
|
141 | typedef long long eio_off_t; /* signed for compatibility to msvc */ |
|
|
142 | #else |
|
|
143 | typedef __int64 eio_off_t; /* unsigned not supported by msvc */ |
|
|
144 | #endif |
|
|
145 | |
|
|
146 | static eio_ssize_t |
|
|
147 | eio__pread (int fd, void *buf, eio_ssize_t count, eio_off_t offset) |
|
|
148 | { |
|
|
149 | OVERLAPPED o = { 0 }; |
|
|
150 | DWORD got; |
|
|
151 | |
|
|
152 | o.Offset = offset; |
|
|
153 | o.OffsetHigh = offset >> 32; |
|
|
154 | |
|
|
155 | return ReadFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o) |
|
|
156 | ? got : -1; |
|
|
157 | } |
|
|
158 | |
|
|
159 | static eio_ssize_t |
|
|
160 | eio__pwrite (int fd, void *buf, eio_ssize_t count, eio_off_t offset) |
|
|
161 | { |
|
|
162 | OVERLAPPED o = { 0 }; |
|
|
163 | DWORD got; |
|
|
164 | |
|
|
165 | o.Offset = offset; |
|
|
166 | o.OffsetHigh = offset >> 32; |
|
|
167 | |
|
|
168 | return WriteFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o) |
|
|
169 | ? got : -1; |
|
|
170 | } |
|
|
171 | |
137 | /* rename() uses MoveFile, which fails to overwrite */ |
172 | /* rename() uses MoveFile, which fails to overwrite */ |
138 | #define rename(old,neu) eio__rename (old, neu) |
173 | #define rename(old,neu) eio__rename (old, neu) |
139 | |
174 | |
140 | static int |
175 | static int |
141 | eio__rename (const char *old, const char *neu) |
176 | eio__rename (const char *old, const char *neu) |
… | |
… | |
208 | #endif |
243 | #endif |
209 | |
244 | |
210 | #define D_NAME(entp) entp->d_name |
245 | #define D_NAME(entp) entp->d_name |
211 | |
246 | |
212 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
247 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
213 | #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
248 | #if __FreeBSD__ || __NetBSD__ || __OpenBSD__ |
214 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
249 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
215 | #define D_INO(de) (de)->d_fileno |
250 | #define D_INO(de) (de)->d_fileno |
216 | #define D_NAMLEN(de) (de)->d_namlen |
251 | #define D_NAMLEN(de) (de)->d_namlen |
217 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
252 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
218 | #define D_INO(de) (de)->d_ino |
253 | #define D_INO(de) (de)->d_ino |
… | |
… | |
316 | |
351 | |
317 | struct tmpbuf; |
352 | struct tmpbuf; |
318 | |
353 | |
319 | #if _POSIX_VERSION >= 200809L |
354 | #if _POSIX_VERSION >= 200809L |
320 | #define HAVE_AT 1 |
355 | #define HAVE_AT 1 |
321 | # define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
356 | #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
322 | #ifndef O_SEARCH |
357 | #ifndef O_SEARCH |
323 | #define O_SEARCH O_RDONLY |
358 | #define O_SEARCH O_RDONLY |
324 | #endif |
359 | #endif |
325 | #else |
360 | #else |
326 | #define HAVE_AT 0 |
361 | #define HAVE_AT 0 |
… | |
… | |
379 | static xmutex_t wrklock; |
414 | static xmutex_t wrklock; |
380 | static xmutex_t reslock; |
415 | static xmutex_t reslock; |
381 | static xmutex_t reqlock; |
416 | static xmutex_t reqlock; |
382 | static xcond_t reqwait; |
417 | static xcond_t reqwait; |
383 | |
418 | |
384 | #if !HAVE_PREADWRITE |
|
|
385 | /* |
|
|
386 | * make our pread/pwrite emulation safe against themselves, but not against |
|
|
387 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
388 | * but that's your problem, not mine. |
|
|
389 | */ |
|
|
390 | static xmutex_t preadwritelock; |
|
|
391 | #endif |
|
|
392 | |
|
|
393 | typedef struct etp_worker |
419 | typedef struct etp_worker |
394 | { |
420 | { |
395 | struct tmpbuf tmpbuf; |
421 | struct tmpbuf tmpbuf; |
396 | |
422 | |
397 | /* locked by wrklock */ |
423 | /* locked by wrklock */ |
… | |
… | |
409 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
435 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
410 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
436 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
411 | |
437 | |
412 | /* worker threads management */ |
438 | /* worker threads management */ |
413 | |
439 | |
414 | static void ecb_cold |
440 | static void |
415 | etp_worker_clear (etp_worker *wrk) |
441 | etp_worker_clear (etp_worker *wrk) |
416 | { |
442 | { |
417 | } |
443 | } |
418 | |
444 | |
419 | static void ecb_cold |
445 | static void ecb_cold |
… | |
… | |
943 | } |
969 | } |
944 | |
970 | |
945 | /*****************************************************************************/ |
971 | /*****************************************************************************/ |
946 | /* work around various missing functions */ |
972 | /* work around various missing functions */ |
947 | |
973 | |
948 | #if !HAVE_PREADWRITE |
|
|
949 | # undef pread |
|
|
950 | # undef pwrite |
|
|
951 | # define pread eio__pread |
|
|
952 | # define pwrite eio__pwrite |
|
|
953 | |
|
|
954 | static eio_ssize_t |
|
|
955 | eio__pread (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 = read (fd, buf, count); |
|
|
964 | lseek (fd, ooffset, SEEK_SET); |
|
|
965 | X_UNLOCK (preadwritelock); |
|
|
966 | |
|
|
967 | return res; |
|
|
968 | } |
|
|
969 | |
|
|
970 | static eio_ssize_t |
|
|
971 | eio__pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
972 | { |
|
|
973 | eio_ssize_t res; |
|
|
974 | off_t ooffset; |
|
|
975 | |
|
|
976 | X_LOCK (preadwritelock); |
|
|
977 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
978 | lseek (fd, offset, SEEK_SET); |
|
|
979 | res = write (fd, buf, count); |
|
|
980 | lseek (fd, ooffset, SEEK_SET); |
|
|
981 | X_UNLOCK (preadwritelock); |
|
|
982 | |
|
|
983 | return res; |
|
|
984 | } |
|
|
985 | #endif |
|
|
986 | |
|
|
987 | #ifndef HAVE_UTIMES |
974 | #ifndef HAVE_UTIMES |
988 | |
975 | |
989 | # undef utimes |
976 | # undef utimes |
990 | # define utimes(path,times) eio__utimes (path, times) |
977 | # define utimes(path,times) eio__utimes (path, times) |
991 | |
978 | |
… | |
… | |
1032 | int res; |
1019 | int res; |
1033 | |
1020 | |
1034 | #if HAVE_SYS_SYNCFS |
1021 | #if HAVE_SYS_SYNCFS |
1035 | res = (int)syscall (__NR_syncfs, (int)(fd)); |
1022 | res = (int)syscall (__NR_syncfs, (int)(fd)); |
1036 | #else |
1023 | #else |
1037 | res = -1; |
1024 | res = EIO_ENOSYS (); |
1038 | errno = ENOSYS; |
|
|
1039 | #endif |
1025 | #endif |
1040 | |
1026 | |
1041 | if (res < 0 && errno == ENOSYS && fd >= 0) |
1027 | if (res < 0 && errno == ENOSYS && fd >= 0) |
1042 | sync (); |
1028 | sync (); |
1043 | |
1029 | |
… | |
… | |
1073 | } |
1059 | } |
1074 | |
1060 | |
1075 | static int |
1061 | static int |
1076 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
1062 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
1077 | { |
1063 | { |
1078 | #if HAVE_FALLOCATE |
1064 | #if HAVE_LINUX_FALLOCATE |
1079 | return fallocate (fd, mode, offset, len); |
1065 | return fallocate (fd, mode, offset, len); |
1080 | #else |
1066 | #else |
1081 | errno = ENOSYS; |
1067 | return EIO_ENOSYS (); |
1082 | return -1; |
|
|
1083 | #endif |
1068 | #endif |
1084 | } |
1069 | } |
1085 | |
1070 | |
1086 | #if !HAVE_READAHEAD |
1071 | #if !HAVE_READAHEAD |
1087 | # undef readahead |
1072 | # undef readahead |
… | |
… | |
1102 | todo -= len; |
1087 | todo -= len; |
1103 | } |
1088 | } |
1104 | |
1089 | |
1105 | FUBd; |
1090 | FUBd; |
1106 | |
1091 | |
1107 | errno = 0; |
1092 | /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */ |
|
|
1093 | /* but not for e.g. EIO or eof, so we also never fail */ |
1108 | return count; |
1094 | return 0; |
1109 | } |
1095 | } |
1110 | |
1096 | |
1111 | #endif |
1097 | #endif |
1112 | |
1098 | |
1113 | /* sendfile always needs emulation */ |
1099 | /* sendfile always needs emulation */ |
… | |
… | |
1148 | |
1134 | |
1149 | /* according to source inspection, this is correct, and useful behaviour */ |
1135 | /* according to source inspection, this is correct, and useful behaviour */ |
1150 | if (sbytes) |
1136 | if (sbytes) |
1151 | res = sbytes; |
1137 | res = sbytes; |
1152 | |
1138 | |
1153 | # elif defined (__APPLE__) |
1139 | # elif defined __APPLE__ |
1154 | off_t sbytes = count; |
1140 | off_t sbytes = count; |
1155 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1141 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1156 | |
1142 | |
1157 | /* according to the manpage, sbytes is always valid */ |
1143 | /* according to the manpage, sbytes is always valid */ |
1158 | if (sbytes) |
1144 | if (sbytes) |
… | |
… | |
1185 | HANDLE h = TO_SOCKET (ifd); |
1171 | HANDLE h = TO_SOCKET (ifd); |
1186 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
1172 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
1187 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
1173 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
1188 | |
1174 | |
1189 | #else |
1175 | #else |
1190 | res = -1; |
1176 | res = EIO_ENOSYS (); |
1191 | errno = ENOSYS; |
|
|
1192 | #endif |
1177 | #endif |
1193 | |
1178 | |
1194 | /* we assume sendfile can copy at least 128mb in one go */ |
1179 | /* we assume sendfile can copy at least 128mb in one go */ |
1195 | if (res <= 128 * 1024 * 1024) |
1180 | if (res <= 128 * 1024 * 1024) |
1196 | { |
1181 | { |
… | |
… | |
1382 | } |
1367 | } |
1383 | |
1368 | |
1384 | /*****************************************************************************/ |
1369 | /*****************************************************************************/ |
1385 | /* requests implemented outside eio_execute, because they are so large */ |
1370 | /* requests implemented outside eio_execute, because they are so large */ |
1386 | |
1371 | |
|
|
1372 | static void |
|
|
1373 | eio__lseek (eio_req *req) |
|
|
1374 | { |
|
|
1375 | /* this usually gets optimised away completely, or your compiler sucks, */ |
|
|
1376 | /* or the whence constants really are not 0, 1, 2 */ |
|
|
1377 | int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET |
|
|
1378 | : req->int2 == EIO_SEEK_CUR ? SEEK_CUR |
|
|
1379 | : req->int2 == EIO_SEEK_END ? SEEK_END |
|
|
1380 | : req->int2; |
|
|
1381 | |
|
|
1382 | req->offs = lseek (req->int1, req->offs, whence); |
|
|
1383 | req->result = req->offs == (off_t)-1 ? -1 : 0; |
|
|
1384 | } |
|
|
1385 | |
1387 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
1386 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
1388 | static int |
1387 | static int |
1389 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1388 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1390 | { |
1389 | { |
1391 | const char *rel = path; |
1390 | const char *rel = path; |
… | |
… | |
1487 | } |
1486 | } |
1488 | } |
1487 | } |
1489 | |
1488 | |
1490 | errno = ENAMETOOLONG; |
1489 | errno = ENAMETOOLONG; |
1491 | if (res + 1 + len + 1 >= tmp1) |
1490 | if (res + 1 + len + 1 >= tmp1) |
1492 | return; |
1491 | return -1; |
1493 | |
1492 | |
1494 | /* copy one component */ |
1493 | /* copy one component */ |
1495 | *res = '/'; |
1494 | *res = '/'; |
1496 | memcpy (res + 1, beg, len); |
1495 | memcpy (res + 1, beg, len); |
1497 | |
1496 | |
… | |
… | |
1784 | |
1783 | |
1785 | if (fd < 0) |
1784 | if (fd < 0) |
1786 | return; |
1785 | return; |
1787 | |
1786 | |
1788 | dirp = fdopendir (fd); |
1787 | dirp = fdopendir (fd); |
|
|
1788 | |
|
|
1789 | if (!dirp) |
|
|
1790 | close (fd); |
1789 | } |
1791 | } |
1790 | else |
1792 | else |
1791 | dirp = opendir (req->ptr1); |
1793 | dirp = opendir (req->ptr1); |
1792 | #else |
1794 | #else |
1793 | dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); |
1795 | dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); |
… | |
… | |
1916 | #ifdef DT_FIFO |
1918 | #ifdef DT_FIFO |
1917 | case DT_FIFO: ent->type = EIO_DT_FIFO; break; |
1919 | case DT_FIFO: ent->type = EIO_DT_FIFO; break; |
1918 | #endif |
1920 | #endif |
1919 | #ifdef DT_CHR |
1921 | #ifdef DT_CHR |
1920 | case DT_CHR: ent->type = EIO_DT_CHR; break; |
1922 | case DT_CHR: ent->type = EIO_DT_CHR; break; |
1921 | #endif |
1923 | #endif |
1922 | #ifdef DT_MPC |
1924 | #ifdef DT_MPC |
1923 | case DT_MPC: ent->type = EIO_DT_MPC; break; |
1925 | case DT_MPC: ent->type = EIO_DT_MPC; break; |
1924 | #endif |
1926 | #endif |
1925 | #ifdef DT_DIR |
1927 | #ifdef DT_DIR |
1926 | case DT_DIR: ent->type = EIO_DT_DIR; break; |
1928 | case DT_DIR: ent->type = EIO_DT_DIR; break; |
1927 | #endif |
1929 | #endif |
1928 | #ifdef DT_NAM |
1930 | #ifdef DT_NAM |
1929 | case DT_NAM: ent->type = EIO_DT_NAM; break; |
1931 | case DT_NAM: ent->type = EIO_DT_NAM; break; |
1930 | #endif |
1932 | #endif |
1931 | #ifdef DT_BLK |
1933 | #ifdef DT_BLK |
1932 | case DT_BLK: ent->type = EIO_DT_BLK; break; |
1934 | case DT_BLK: ent->type = EIO_DT_BLK; break; |
1933 | #endif |
1935 | #endif |
1934 | #ifdef DT_MPB |
1936 | #ifdef DT_MPB |
1935 | case DT_MPB: ent->type = EIO_DT_MPB; break; |
1937 | case DT_MPB: ent->type = EIO_DT_MPB; break; |
1936 | #endif |
1938 | #endif |
1937 | #ifdef DT_REG |
1939 | #ifdef DT_REG |
1938 | case DT_REG: ent->type = EIO_DT_REG; break; |
1940 | case DT_REG: ent->type = EIO_DT_REG; break; |
1939 | #endif |
1941 | #endif |
1940 | #ifdef DT_NWK |
1942 | #ifdef DT_NWK |
1941 | case DT_NWK: ent->type = EIO_DT_NWK; break; |
1943 | case DT_NWK: ent->type = EIO_DT_NWK; break; |
1942 | #endif |
1944 | #endif |
1943 | #ifdef DT_CMP |
1945 | #ifdef DT_CMP |
1944 | case DT_CMP: ent->type = EIO_DT_CMP; break; |
1946 | case DT_CMP: ent->type = EIO_DT_CMP; break; |
1945 | #endif |
1947 | #endif |
1946 | #ifdef DT_LNK |
1948 | #ifdef DT_LNK |
1947 | case DT_LNK: ent->type = EIO_DT_LNK; break; |
1949 | case DT_LNK: ent->type = EIO_DT_LNK; break; |
1948 | #endif |
1950 | #endif |
1949 | #ifdef DT_SOCK |
1951 | #ifdef DT_SOCK |
1950 | case DT_SOCK: ent->type = EIO_DT_SOCK; break; |
1952 | case DT_SOCK: ent->type = EIO_DT_SOCK; break; |
… | |
… | |
1963 | { |
1965 | { |
1964 | if (ent->type == EIO_DT_UNKNOWN) |
1966 | if (ent->type == EIO_DT_UNKNOWN) |
1965 | { |
1967 | { |
1966 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1968 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1967 | ent->score = 1; |
1969 | ent->score = 1; |
1968 | else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ |
1970 | else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */ |
1969 | ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ |
1971 | ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ |
1970 | } |
1972 | } |
1971 | else if (ent->type == EIO_DT_DIR) |
1973 | else if (ent->type == EIO_DT_DIR) |
1972 | ent->score = 0; |
1974 | ent->score = 0; |
1973 | } |
1975 | } |
… | |
… | |
2130 | req->result = -1; \ |
2132 | req->result = -1; \ |
2131 | break; \ |
2133 | break; \ |
2132 | } \ |
2134 | } \ |
2133 | } |
2135 | } |
2134 | |
2136 | |
|
|
2137 | static void ecb_noinline ecb_cold |
|
|
2138 | etp_proc_init (void) |
|
|
2139 | { |
|
|
2140 | #if HAVE_PRCTL_SET_NAME |
|
|
2141 | /* provide a more sensible "thread name" */ |
|
|
2142 | char name[16 + 1]; |
|
|
2143 | const int namelen = sizeof (name) - 1; |
|
|
2144 | int len; |
|
|
2145 | |
|
|
2146 | prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
2147 | name [namelen] = 0; |
|
|
2148 | len = strlen (name); |
|
|
2149 | strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio"); |
|
|
2150 | prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
2151 | #endif |
|
|
2152 | } |
|
|
2153 | |
2135 | X_THREAD_PROC (etp_proc) |
2154 | X_THREAD_PROC (etp_proc) |
2136 | { |
2155 | { |
2137 | ETP_REQ *req; |
2156 | ETP_REQ *req; |
2138 | struct timespec ts; |
2157 | struct timespec ts; |
2139 | etp_worker *self = (etp_worker *)thr_arg; |
2158 | etp_worker *self = (etp_worker *)thr_arg; |
2140 | |
2159 | |
2141 | #if HAVE_PRCTL_SET_NAME |
2160 | etp_proc_init (); |
2142 | prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0); |
|
|
2143 | #endif |
|
|
2144 | |
2161 | |
2145 | /* try to distribute timeouts somewhat evenly */ |
2162 | /* try to distribute timeouts somewhat evenly */ |
2146 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
2163 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
2147 | |
2164 | |
2148 | for (;;) |
2165 | for (;;) |
… | |
… | |
2219 | /*****************************************************************************/ |
2236 | /*****************************************************************************/ |
2220 | |
2237 | |
2221 | int ecb_cold |
2238 | int ecb_cold |
2222 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
2239 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
2223 | { |
2240 | { |
2224 | #if !HAVE_PREADWRITE |
|
|
2225 | X_MUTEX_CREATE (preadwritelock); |
|
|
2226 | #endif |
|
|
2227 | |
|
|
2228 | return etp_init (want_poll, done_poll); |
2241 | return etp_init (want_poll, done_poll); |
2229 | } |
2242 | } |
2230 | |
2243 | |
2231 | ecb_inline void |
2244 | ecb_inline void |
2232 | eio_api_destroy (eio_req *req) |
2245 | eio_api_destroy (eio_req *req) |
2233 | { |
2246 | { |
2234 | free (req); |
2247 | free (req); |
2235 | } |
2248 | } |
2236 | |
2249 | |
2237 | #define REQ(rtype) \ |
2250 | #define REQ(rtype) \ |
2238 | eio_req *req; \ |
2251 | eio_req *req; \ |
2239 | \ |
2252 | \ |
2240 | req = (eio_req *)calloc (1, sizeof *req); \ |
2253 | req = (eio_req *)calloc (1, sizeof *req); \ |
2241 | if (!req) \ |
2254 | if (!req) \ |
2242 | return 0; \ |
2255 | return 0; \ |
… | |
… | |
2296 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2309 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2297 | break; |
2310 | break; |
2298 | case EIO_WD_CLOSE: req->result = 0; |
2311 | case EIO_WD_CLOSE: req->result = 0; |
2299 | eio_wd_close_sync (req->wd); break; |
2312 | eio_wd_close_sync (req->wd); break; |
2300 | |
2313 | |
|
|
2314 | case EIO_SEEK: eio__lseek (req); break; |
2301 | case EIO_READ: ALLOC (req->size); |
2315 | case EIO_READ: ALLOC (req->size); |
2302 | req->result = req->offs >= 0 |
2316 | req->result = req->offs >= 0 |
2303 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2317 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2304 | : read (req->int1, req->ptr2, req->size); break; |
2318 | : read (req->int1, req->ptr2, req->size); break; |
2305 | case EIO_WRITE: req->result = req->offs >= 0 |
2319 | case EIO_WRITE: req->result = req->offs >= 0 |
… | |
… | |
2461 | case EIO_CUSTOM: |
2475 | case EIO_CUSTOM: |
2462 | req->feed (req); |
2476 | req->feed (req); |
2463 | break; |
2477 | break; |
2464 | |
2478 | |
2465 | default: |
2479 | default: |
2466 | errno = ENOSYS; |
|
|
2467 | req->result = -1; |
2480 | req->result = EIO_ENOSYS (); |
2468 | break; |
2481 | break; |
2469 | } |
2482 | } |
2470 | |
2483 | |
2471 | req->errorno = errno; |
2484 | req->errorno = errno; |
2472 | } |
2485 | } |
2473 | |
2486 | |
2474 | #ifndef EIO_NO_WRAPPERS |
2487 | #ifndef EIO_NO_WRAPPERS |
2475 | |
2488 | |
|
|
2489 | eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data) |
|
|
2490 | { |
|
|
2491 | REQ (EIO_WD_OPEN); PATH; SEND; |
|
|
2492 | } |
|
|
2493 | |
|
|
2494 | eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data) |
|
|
2495 | { |
|
|
2496 | REQ (EIO_WD_CLOSE); req->wd = wd; SEND; |
|
|
2497 | } |
|
|
2498 | |
2476 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
2499 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
2477 | { |
2500 | { |
2478 | REQ (EIO_NOP); SEND; |
2501 | REQ (EIO_NOP); SEND; |
2479 | } |
2502 | } |
2480 | |
2503 | |
… | |
… | |
2539 | } |
2562 | } |
2540 | |
2563 | |
2541 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2564 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2542 | { |
2565 | { |
2543 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
2566 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
|
|
2567 | } |
|
|
2568 | |
|
|
2569 | eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data) |
|
|
2570 | { |
|
|
2571 | REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND; |
2544 | } |
2572 | } |
2545 | |
2573 | |
2546 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2574 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2547 | { |
2575 | { |
2548 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2576 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |