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

Comparing libeio/eio.c (file contents):
Revision 1.86 by root, Thu Jul 14 18:30:10 2011 UTC vs.
Revision 1.95 by root, Tue Jul 19 04:56:43 2011 UTC

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#include <sys/statvfs.h>
64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
65/* intptr_t only comes from stdint.h, says idiot openbsd coder */ 64/* intptr_t only comes from stdint.h, says idiot openbsd coder */
66#if HAVE_STDINT_H 65#if HAVE_STDINT_H
67# include <stdint.h> 66# include <stdint.h>
68#endif 67#endif
69 68
70#ifndef ECANCELED 69#ifndef ECANCELED
71# define ECANCELED EDOM 70# define ECANCELED EDOM
72#endif 71#endif
72#ifndef ELOOP
73# define ELOOP EDOM
74#endif
75
76#if !defined(ENOTSOCK) && defined(WSAENOTSOCK)
77# define ENOTSOCK WSAENOTSOCK
78#endif
73 79
74static void eio_destroy (eio_req *req); 80static void eio_destroy (eio_req *req);
75 81
76#ifndef EIO_FINISH 82#ifndef EIO_FINISH
77# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 83# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
96 102
97#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1) 103#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1)
98 104
99#ifdef _WIN32 105#ifdef _WIN32
100 106
107 #undef PAGESIZE
101 #define PAGESIZE 4096 /* GetSystemInfo? */ 108 #define PAGESIZE 4096 /* GetSystemInfo? */
102 109
110 #ifdef EIO_STRUCT_STATI64
103 #define stat(path,buf) _stati64 (path,buf) 111 #define stat(path,buf) _stati64 (path,buf)
112 #define fstat(fd,buf) _fstati64 (fd,buf)
113 #endif
104 #define lstat(path,buf) stat (path,buf) 114 #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)) 115 #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1))
107 #define mkdir(path,mode) _mkdir (path) 116 #define mkdir(path,mode) _mkdir (path)
108 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) 117 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
109 118
119 #define chmod(path,mode) _chmod (path, mode)
120 #define dup(fd) _dup (fd)
121 #define dup2(fd1,fd2) _dup2 (fd1, fd2)
122
123 #define fchmod(fd,mode) EIO_ENOSYS ()
110 #define chown(path,uid,gid) EIO_ENOSYS () 124 #define chown(path,uid,gid) EIO_ENOSYS ()
111 #define fchown(fd,uid,gid) EIO_ENOSYS () 125 #define fchown(fd,uid,gid) EIO_ENOSYS ()
112 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ 126 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
113 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ 127 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
114 #define mknod(path,mode,dev) EIO_ENOSYS () 128 #define mknod(path,mode,dev) EIO_ENOSYS ()
115 #define sync() EIO_ENOSYS () 129 #define sync() EIO_ENOSYS ()
130 #define readlink(path,buf,s) EIO_ENOSYS ()
131 #define statvfs(path,buf) EIO_ENOSYS ()
132 #define fstatvfs(fd,buf) EIO_ENOSYS ()
116 133
117 /* we could even stat and see if it exists */ 134 /* we could even stat and see if it exists */
118 static int 135 static int
119 symlink (const char *old, const char *neu) 136 symlink (const char *old, const char *neu)
120 { 137 {
138 #if WINVER >= 0x0600
121 if (CreateSymbolicLink (neu, old, 1)) 139 if (CreateSymbolicLink (neu, old, 1))
122 return 0; 140 return 0;
123 141
124 if (CreateSymbolicLink (neu, old, 0)) 142 if (CreateSymbolicLink (neu, old, 0))
125 return 0; 143 return 0;
144 #endif
126 145
127 return EIO_ERRNO (ENOENT, -1); 146 return EIO_ERRNO (ENOENT, -1);
128 } 147 }
148
149 /* POSIX API only */
150 #define CreateHardLink(neu,old,flags) 0
151 #define CreateSymbolicLink(neu,old,flags) 0
152
153 struct statvfs
154 {
155 int dummy;
156 };
157
158 #define DT_DIR EIO_DT_DIR
159 #define DT_REG EIO_DT_REG
160 #define D_NAME(entp) entp.cFileName
161 #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG)
129 162
130#else 163#else
131 164
132 #include <sys/time.h> 165 #include <sys/time.h>
133 #include <sys/select.h> 166 #include <sys/select.h>
138 #include <dirent.h> 171 #include <dirent.h>
139 172
140 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 173 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
141 #include <sys/mman.h> 174 #include <sys/mman.h>
142 #endif 175 #endif
176
177 #define D_NAME(entp) entp->d_name
143 178
144 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 179 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
145 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 180 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
146 #define _DIRENT_HAVE_D_TYPE /* sigh */ 181 #define _DIRENT_HAVE_D_TYPE /* sigh */
147 #define D_INO(de) (de)->d_fileno 182 #define D_INO(de) (de)->d_fileno
185#endif 220#endif
186#ifndef D_INO 221#ifndef D_INO
187# define D_INO(de) 0 222# define D_INO(de) 0
188#endif 223#endif
189#ifndef D_NAMLEN 224#ifndef D_NAMLEN
190# define D_NAMLEN(de) strlen ((de)->d_name) 225# define D_NAMLEN(entp) strlen (D_NAME (entp))
191#endif 226#endif
192 227
193/* used for struct dirent, AIX doesn't provide it */ 228/* used for struct dirent, AIX doesn't provide it */
194#ifndef NAME_MAX 229#ifndef NAME_MAX
195# define NAME_MAX 4096 230# define NAME_MAX 4096
202 237
203/* buffer size for various temporary buffers */ 238/* buffer size for various temporary buffers */
204#define EIO_BUFSIZE 65536 239#define EIO_BUFSIZE 65536
205 240
206#define dBUF \ 241#define dBUF \
207 char *eio_buf; \
208 ETP_WORKER_LOCK (self); \
209 self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \ 242 char *eio_buf = malloc (EIO_BUFSIZE); \
210 ETP_WORKER_UNLOCK (self); \
211 errno = ENOMEM; \ 243 errno = ENOMEM; \
212 if (!eio_buf) \ 244 if (!eio_buf) \
213 return -1; 245 return -1
246
247#define FUBd \
248 free (eio_buf)
214 249
215#define EIO_TICKS ((1000000 + 1023) >> 10) 250#define EIO_TICKS ((1000000 + 1023) >> 10)
216 251
217#define ETP_PRI_MIN EIO_PRI_MIN 252#define ETP_PRI_MIN EIO_PRI_MIN
218#define ETP_PRI_MAX EIO_PRI_MAX 253#define ETP_PRI_MAX EIO_PRI_MAX
224static int eio_finish (eio_req *req); 259static int eio_finish (eio_req *req);
225#define ETP_FINISH(req) eio_finish (req) 260#define ETP_FINISH(req) eio_finish (req)
226static void eio_execute (struct etp_worker *self, eio_req *req); 261static void eio_execute (struct etp_worker *self, eio_req *req);
227#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 262#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req)
228 263
229#define ETP_WORKER_CLEAR(req) \
230 if (wrk->dbuf) \
231 { \
232 free (wrk->dbuf); \
233 wrk->dbuf = 0; \
234 } \
235 \
236 if (wrk->dirp) \
237 { \
238 closedir (wrk->dirp); \
239 wrk->dirp = 0; \
240 }
241
242#define ETP_WORKER_COMMON \
243 void *dbuf; \
244 DIR *dirp;
245
246/*****************************************************************************/ 264/*****************************************************************************/
247 265
248#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 266#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
249 267
250/* calculate time difference in ~1/EIO_TICKS of a second */ 268/* calculate time difference in ~1/EIO_TICKS of a second */
278/* 296/*
279 * make our pread/pwrite emulation safe against themselves, but not against 297 * make our pread/pwrite emulation safe against themselves, but not against
280 * normal read/write by using a mutex. slows down execution a lot, 298 * normal read/write by using a mutex. slows down execution a lot,
281 * but that's your problem, not mine. 299 * but that's your problem, not mine.
282 */ 300 */
283static xmutex_t preadwritelock = X_MUTEX_INIT; 301static xmutex_t preadwritelock;
284#endif 302#endif
285 303
286typedef struct etp_worker 304typedef struct etp_worker
287{ 305{
288 /* locked by wrklock */ 306 /* locked by wrklock */
291 xthread_t tid; 309 xthread_t tid;
292 310
293 /* locked by reslock, reqlock or wrklock */ 311 /* locked by reslock, reqlock or wrklock */
294 ETP_REQ *req; /* currently processed request */ 312 ETP_REQ *req; /* currently processed request */
295 313
314#ifdef ETP_WORKER_COMMON
296 ETP_WORKER_COMMON 315 ETP_WORKER_COMMON
316#endif
297} etp_worker; 317} etp_worker;
298 318
299static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */ 319static etp_worker wrk_first; /* NOT etp */
300 320
301#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 321#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
302#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 322#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
303 323
304/* worker threads management */ 324/* worker threads management */
305 325
306static void ecb_cold 326static void ecb_cold
307etp_worker_clear (etp_worker *wrk) 327etp_worker_clear (etp_worker *wrk)
308{ 328{
309 ETP_WORKER_CLEAR (wrk);
310} 329}
311 330
312static void ecb_cold 331static void ecb_cold
313etp_worker_free (etp_worker *wrk) 332etp_worker_free (etp_worker *wrk)
314{ 333{
375} etp_reqq; 394} etp_reqq;
376 395
377static etp_reqq req_queue; 396static etp_reqq req_queue;
378static etp_reqq res_queue; 397static etp_reqq res_queue;
379 398
399static void ecb_noinline ecb_cold
400reqq_init (etp_reqq *q)
401{
402 int pri;
403
404 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
405 q->qs[pri] = q->qe[pri] = 0;
406
407 q->size = 0;
408}
409
380static int ecb_noinline 410static int ecb_noinline
381reqq_push (etp_reqq *q, ETP_REQ *req) 411reqq_push (etp_reqq *q, ETP_REQ *req)
382{ 412{
383 int pri = req->pri; 413 int pri = req->pri;
384 req->next = 0; 414 req->next = 0;
418 } 448 }
419 449
420 abort (); 450 abort ();
421} 451}
422 452
423static void ecb_cold 453static int ecb_cold
424etp_thread_init (void) 454etp_init (void (*want_poll)(void), void (*done_poll)(void))
425{ 455{
426#if !HAVE_PREADWRITE
427 X_MUTEX_CREATE (preadwritelock);
428#endif
429 X_MUTEX_CREATE (wrklock); 456 X_MUTEX_CREATE (wrklock);
430 X_MUTEX_CREATE (reslock); 457 X_MUTEX_CREATE (reslock);
431 X_MUTEX_CREATE (reqlock); 458 X_MUTEX_CREATE (reqlock);
432 X_COND_CREATE (reqwait); 459 X_COND_CREATE (reqwait);
433}
434 460
435static void ecb_cold 461 reqq_init (&req_queue);
436etp_atfork_prepare (void) 462 reqq_init (&res_queue);
437{
438}
439 463
440static void ecb_cold 464 wrk_first.next =
441etp_atfork_parent (void) 465 wrk_first.prev = &wrk_first;
442{
443}
444
445static void ecb_cold
446etp_atfork_child (void)
447{
448 ETP_REQ *prv;
449
450 while ((prv = reqq_shift (&req_queue)))
451 ETP_DESTROY (prv);
452
453 while ((prv = reqq_shift (&res_queue)))
454 ETP_DESTROY (prv);
455
456 while (wrk_first.next != &wrk_first)
457 {
458 etp_worker *wrk = wrk_first.next;
459
460 if (wrk->req)
461 ETP_DESTROY (wrk->req);
462
463 etp_worker_clear (wrk);
464 etp_worker_free (wrk);
465 }
466 466
467 started = 0; 467 started = 0;
468 idle = 0; 468 idle = 0;
469 nreqs = 0; 469 nreqs = 0;
470 nready = 0; 470 nready = 0;
471 npending = 0; 471 npending = 0;
472
473 etp_thread_init ();
474}
475
476static void ecb_cold
477etp_once_init (void)
478{
479 etp_thread_init ();
480 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
481}
482
483static int ecb_cold
484etp_init (void (*want_poll)(void), void (*done_poll)(void))
485{
486 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
487
488 pthread_once (&doinit, etp_once_init);
489 472
490 want_poll_cb = want_poll; 473 want_poll_cb = want_poll;
491 done_poll_cb = done_poll; 474 done_poll_cb = done_poll;
492 475
493 return 0; 476 return 0;
876# undef pread 859# undef pread
877# undef pwrite 860# undef pwrite
878# define pread eio__pread 861# define pread eio__pread
879# define pwrite eio__pwrite 862# define pwrite eio__pwrite
880 863
881static ssize_t 864static eio_ssize_t
882eio__pread (int fd, void *buf, size_t count, off_t offset) 865eio__pread (int fd, void *buf, size_t count, off_t offset)
883{ 866{
884 ssize_t res; 867 eio_ssize_t res;
885 off_t ooffset; 868 off_t ooffset;
886 869
887 X_LOCK (preadwritelock); 870 X_LOCK (preadwritelock);
888 ooffset = lseek (fd, 0, SEEK_CUR); 871 ooffset = lseek (fd, 0, SEEK_CUR);
889 lseek (fd, offset, SEEK_SET); 872 lseek (fd, offset, SEEK_SET);
892 X_UNLOCK (preadwritelock); 875 X_UNLOCK (preadwritelock);
893 876
894 return res; 877 return res;
895} 878}
896 879
897static ssize_t 880static eio_ssize_t
898eio__pwrite (int fd, void *buf, size_t count, off_t offset) 881eio__pwrite (int fd, void *buf, size_t count, off_t offset)
899{ 882{
900 ssize_t res; 883 eio_ssize_t res;
901 off_t ooffset; 884 off_t ooffset;
902 885
903 X_LOCK (preadwritelock); 886 X_LOCK (preadwritelock);
904 ooffset = lseek (fd, 0, SEEK_CUR); 887 ooffset = lseek (fd, 0, SEEK_CUR);
905 lseek (fd, offset, SEEK_SET); 888 lseek (fd, offset, SEEK_SET);
994 977
995#if !HAVE_READAHEAD 978#if !HAVE_READAHEAD
996# undef readahead 979# undef readahead
997# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 980# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
998 981
999static ssize_t 982static eio_ssize_t
1000eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) 983eio__readahead (int fd, off_t offset, size_t count, etp_worker *self)
1001{ 984{
1002 size_t todo = count; 985 size_t todo = count;
1003 dBUF; 986 dBUF;
1004 987
1009 pread (fd, eio_buf, len, offset); 992 pread (fd, eio_buf, len, offset);
1010 offset += len; 993 offset += len;
1011 todo -= len; 994 todo -= len;
1012 } 995 }
1013 996
997 FUBd;
998
1014 errno = 0; 999 errno = 0;
1015 return count; 1000 return count;
1016} 1001}
1017 1002
1018#endif 1003#endif
1019 1004
1020/* sendfile always needs emulation */ 1005/* sendfile always needs emulation */
1021static ssize_t 1006static eio_ssize_t
1022eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 1007eio__sendfile (int ofd, int ifd, off_t offset, size_t count)
1023{ 1008{
1024 ssize_t written = 0; 1009 eio_ssize_t written = 0;
1025 ssize_t res; 1010 eio_ssize_t res;
1026 1011
1027 if (!count) 1012 if (!count)
1028 return 0; 1013 return 0;
1029 1014
1030 for (;;) 1015 for (;;)
1125 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1110 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1126 /* BSDs */ 1111 /* BSDs */
1127#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1112#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1128 || errno == ENOTSUP 1113 || errno == ENOTSUP
1129#endif 1114#endif
1115#ifdef EOPNOTSUPP /* windows */
1130 || errno == EOPNOTSUPP /* BSDs */ 1116 || errno == EOPNOTSUPP /* BSDs */
1117#endif
1131#if __solaris 1118#if __solaris
1132 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 1119 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1133#endif 1120#endif
1134 ) 1121 )
1135 ) 1122 )
1139 1126
1140 res = 0; 1127 res = 0;
1141 1128
1142 while (count) 1129 while (count)
1143 { 1130 {
1144 ssize_t cnt; 1131 eio_ssize_t cnt;
1145 1132
1146 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset); 1133 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset);
1147 1134
1148 if (cnt <= 0) 1135 if (cnt <= 0)
1149 { 1136 {
1161 1148
1162 offset += cnt; 1149 offset += cnt;
1163 res += cnt; 1150 res += cnt;
1164 count -= cnt; 1151 count -= cnt;
1165 } 1152 }
1153
1154 FUBd;
1166 } 1155 }
1167 1156
1168 return res; 1157 return res;
1169} 1158}
1170 1159
1197 /* round up length */ 1186 /* round up length */
1198 *length = (*length + mask) & ~mask; 1187 *length = (*length + mask) & ~mask;
1199} 1188}
1200 1189
1201#if !_POSIX_MEMLOCK 1190#if !_POSIX_MEMLOCK
1202# define eio__mlockall(a) eio_nosyscall() 1191# define eio__mlockall(a) EIO_ENOSYS ()
1203#else 1192#else
1204 1193
1205static int 1194static int
1206eio__mlockall (int flags) 1195eio__mlockall (int flags)
1207{ 1196{
1357 res += strlen (res); 1346 res += strlen (res);
1358 } 1347 }
1359 1348
1360 while (*rel) 1349 while (*rel)
1361 { 1350 {
1362 ssize_t len, linklen; 1351 eio_ssize_t len, linklen;
1363 char *beg = rel; 1352 char *beg = rel;
1364 1353
1365 while (*rel && *rel != '/') 1354 while (*rel && *rel != '/')
1366 ++rel; 1355 ++rel;
1367 1356
1460 1449
1461#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1450#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1462#define EIO_SORT_FAST 60 /* when to only use insertion sort */ 1451#define EIO_SORT_FAST 60 /* when to only use insertion sort */
1463 1452
1464static void 1453static void
1465eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1454eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1466{ 1455{
1467 unsigned char bits [9 + sizeof (ino_t) * 8]; 1456 unsigned char bits [9 + sizeof (eio_ino_t) * 8];
1468 unsigned char *bit = bits; 1457 unsigned char *bit = bits;
1469 1458
1470 assert (CHAR_BIT == 8); 1459 assert (CHAR_BIT == 8);
1471 assert (sizeof (eio_dirent) * 8 < 256); 1460 assert (sizeof (eio_dirent) * 8 < 256);
1472 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ 1461 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1474 1463
1475 if (size <= EIO_SORT_FAST) 1464 if (size <= EIO_SORT_FAST)
1476 return; 1465 return;
1477 1466
1478 /* first prepare an array of bits to test in our radix sort */ 1467 /* first prepare an array of bits to test in our radix sort */
1479 /* try to take endianness into account, as well as differences in ino_t sizes */ 1468 /* try to take endianness into account, as well as differences in eio_ino_t sizes */
1480 /* inode_bits must contain all inodes ORed together */ 1469 /* inode_bits must contain all inodes ORed together */
1481 /* which is used to skip bits that are 0 everywhere, which is very common */ 1470 /* which is used to skip bits that are 0 everywhere, which is very common */
1482 { 1471 {
1483 ino_t endianness; 1472 eio_ino_t endianness;
1484 int i, j; 1473 int i, j;
1485 1474
1486 /* we store the byte offset of byte n into byte n of "endianness" */ 1475 /* we store the byte offset of byte n into byte n of "endianness" */
1487 for (i = 0; i < sizeof (ino_t); ++i) 1476 for (i = 0; i < sizeof (eio_ino_t); ++i)
1488 ((unsigned char *)&endianness)[i] = i; 1477 ((unsigned char *)&endianness)[i] = i;
1489 1478
1490 *bit++ = 0; 1479 *bit++ = 0;
1491 1480
1492 for (i = 0; i < sizeof (ino_t); ++i) 1481 for (i = 0; i < sizeof (eio_ino_t); ++i)
1493 { 1482 {
1494 /* shifting off the byte offsets out of "endianness" */ 1483 /* shifting off the byte offsets out of "endianness" */
1495 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8; 1484 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8;
1496 endianness >>= 8; 1485 endianness >>= 8;
1497 1486
1498 for (j = 0; j < 8; ++j) 1487 for (j = 0; j < 8; ++j)
1499 if (inode_bits & (((ino_t)1) << (i * 8 + j))) 1488 if (inode_bits & (((eio_ino_t)1) << (i * 8 + j)))
1500 *bit++ = offs + j; 1489 *bit++ = offs + j;
1501 } 1490 }
1502 1491
1503 for (j = 0; j < 8; ++j) 1492 for (j = 0; j < 8; ++j)
1504 if (score_bits & (1 << j)) 1493 if (score_bits & (1 << j))
1505 *bit++ = offsetof (eio_dirent, score) * 8 + j; 1494 *bit++ = offsetof (eio_dirent, score) * 8 + j;
1506 } 1495 }
1507 1496
1508 /* now actually do the sorting (a variant of MSD radix sort) */ 1497 /* now actually do the sorting (a variant of MSD radix sort) */
1509 { 1498 {
1510 eio_dirent *base_stk [9 + sizeof (ino_t) * 8], *base; 1499 eio_dirent *base_stk [9 + sizeof (eio_ino_t) * 8], *base;
1511 eio_dirent *end_stk [9 + sizeof (ino_t) * 8], *end; 1500 eio_dirent *end_stk [9 + sizeof (eio_ino_t) * 8], *end;
1512 unsigned char *bit_stk [9 + sizeof (ino_t) * 8]; 1501 unsigned char *bit_stk [9 + sizeof (eio_ino_t) * 8];
1513 int stk_idx = 0; 1502 int stk_idx = 0;
1514 1503
1515 base_stk [stk_idx] = dents; 1504 base_stk [stk_idx] = dents;
1516 end_stk [stk_idx] = dents + size; 1505 end_stk [stk_idx] = dents + size;
1517 bit_stk [stk_idx] = bit - 1; 1506 bit_stk [stk_idx] = bit - 1;
1596 } 1585 }
1597 } 1586 }
1598} 1587}
1599 1588
1600static void 1589static void
1601eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1590eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1602{ 1591{
1603 if (size <= 1) 1592 if (size <= 1)
1604 return; /* our insertion sort relies on size > 0 */ 1593 return; /* our insertion sort relies on size > 0 */
1605 1594
1606 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */ 1595 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */
1614 1603
1615/* read a full directory */ 1604/* read a full directory */
1616static void 1605static void
1617eio__scandir (eio_req *req, etp_worker *self) 1606eio__scandir (eio_req *req, etp_worker *self)
1618{ 1607{
1619 DIR *dirp;
1620 EIO_STRUCT_DIRENT *entp;
1621 char *name, *names; 1608 char *name, *names;
1622 int namesalloc = 4096; 1609 int namesalloc = 4096 - sizeof (void *) * 4;
1623 int namesoffs = 0; 1610 int namesoffs = 0;
1624 int flags = req->int1; 1611 int flags = req->int1;
1625 eio_dirent *dents = 0; 1612 eio_dirent *dents = 0;
1626 int dentalloc = 128; 1613 int dentalloc = 128;
1627 int dentoffs = 0; 1614 int dentoffs = 0;
1628 ino_t inode_bits = 0; 1615 eio_ino_t inode_bits = 0;
1616#ifdef _WIN32
1617 HANDLE dirp;
1618 WIN32_FIND_DATA entp;
1619#else
1620 DIR *dirp;
1621 EIO_STRUCT_DIRENT *entp;
1622#endif
1629 1623
1630 req->result = -1; 1624 req->result = -1;
1631 1625
1632 if (!(flags & EIO_READDIR_DENTS)) 1626 if (!(flags & EIO_READDIR_DENTS))
1633 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1627 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1634 1628
1635 X_LOCK (wrklock); 1629#ifdef _WIN32
1636 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1630 {
1631 int len = strlen ((const char *)req->ptr1);
1632 char *path = malloc (MAX_PATH);
1633 const char *fmt;
1634
1635 if (!len)
1636 fmt = "./*";
1637 else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\')
1638 fmt = "%s*";
1639 else
1640 fmt = "%s/*";
1641
1642 _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1);
1643 dirp = FindFirstFile (path, &entp);
1644 free (path);
1645
1646 if (dirp == INVALID_HANDLE_VALUE)
1647 {
1648 dirp = 0;
1649
1650 switch (GetLastError ())
1651 {
1652 case ERROR_FILE_NOT_FOUND:
1653 req->result = 0;
1654 break;
1655
1656 case ERROR_INVALID_NAME:
1657 case ERROR_PATH_NOT_FOUND:
1658 case ERROR_NO_MORE_FILES:
1659 errno = ENOENT;
1660 break;
1661
1662 case ERROR_NOT_ENOUGH_MEMORY:
1663 errno = ENOMEM;
1664 break;
1665
1666 default:
1667 errno = EINVAL;
1668 break;
1669 }
1670 }
1671 }
1672#else
1637 self->dirp = dirp = opendir (req->ptr1); 1673 dirp = opendir (req->ptr1);
1674#endif
1638 1675
1639 if (req->flags & EIO_FLAG_PTR1_FREE) 1676 if (req->flags & EIO_FLAG_PTR1_FREE)
1640 free (req->ptr1); 1677 free (req->ptr1);
1641 1678
1642 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1679 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1643 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1680 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1644 req->ptr2 = names = malloc (namesalloc); 1681 req->ptr2 = names = malloc (namesalloc);
1645 X_UNLOCK (wrklock);
1646 1682
1647 if (dirp && names && (!flags || dents)) 1683 if (dirp && names && (!flags || dents))
1648 for (;;) 1684 for (;;)
1649 { 1685 {
1686 int done;
1687
1688#ifdef _WIN32
1689 done = !dirp;
1690#else
1650 errno = 0; 1691 errno = 0;
1651 entp = readdir (dirp); 1692 entp = readdir (dirp);
1693 done = !entp;
1694#endif
1652 1695
1653 if (!entp) 1696 if (done)
1654 { 1697 {
1698#ifndef _WIN32
1699 int old_errno = errno;
1700 closedir (dirp);
1701 errno = old_errno;
1702
1655 if (errno) 1703 if (errno)
1656 break; 1704 break;
1705#endif
1657 1706
1658 /* sort etc. */ 1707 /* sort etc. */
1659 req->int1 = flags; 1708 req->int1 = flags;
1660 req->result = dentoffs; 1709 req->result = dentoffs;
1661 1710
1690 1739
1691 break; 1740 break;
1692 } 1741 }
1693 1742
1694 /* now add the entry to our list(s) */ 1743 /* now add the entry to our list(s) */
1695 name = entp->d_name; 1744 name = D_NAME (entp);
1696 1745
1697 /* skip . and .. entries */ 1746 /* skip . and .. entries */
1698 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1747 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1699 { 1748 {
1700 int len = D_NAMLEN (entp) + 1; 1749 int len = D_NAMLEN (entp) + 1;
1701 1750
1702 while (ecb_expect_false (namesoffs + len > namesalloc)) 1751 while (ecb_expect_false (namesoffs + len > namesalloc))
1703 { 1752 {
1704 namesalloc *= 2; 1753 namesalloc *= 2;
1705 X_LOCK (wrklock);
1706 req->ptr2 = names = realloc (names, namesalloc); 1754 req->ptr2 = names = realloc (names, namesalloc);
1707 X_UNLOCK (wrklock);
1708 1755
1709 if (!names) 1756 if (!names)
1710 break; 1757 break;
1711 } 1758 }
1712 1759
1717 struct eio_dirent *ent; 1764 struct eio_dirent *ent;
1718 1765
1719 if (ecb_expect_false (dentoffs == dentalloc)) 1766 if (ecb_expect_false (dentoffs == dentalloc))
1720 { 1767 {
1721 dentalloc *= 2; 1768 dentalloc *= 2;
1722 X_LOCK (wrklock);
1723 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1769 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1724 X_UNLOCK (wrklock);
1725 1770
1726 if (!dents) 1771 if (!dents)
1727 break; 1772 break;
1728 } 1773 }
1729 1774
1809 if (EIO_CANCELLED (req)) 1854 if (EIO_CANCELLED (req))
1810 { 1855 {
1811 errno = ECANCELED; 1856 errno = ECANCELED;
1812 break; 1857 break;
1813 } 1858 }
1859
1860#ifdef _WIN32
1861 if (!FindNextFile (dirp, &entp))
1862 {
1863 FindClose (dirp);
1864 dirp = 0;
1865 }
1866#endif
1814 } 1867 }
1815} 1868}
1816 1869
1817/*****************************************************************************/ 1870/*****************************************************************************/
1818 1871
1834X_THREAD_PROC (etp_proc) 1887X_THREAD_PROC (etp_proc)
1835{ 1888{
1836 ETP_REQ *req; 1889 ETP_REQ *req;
1837 struct timespec ts; 1890 struct timespec ts;
1838 etp_worker *self = (etp_worker *)thr_arg; 1891 etp_worker *self = (etp_worker *)thr_arg;
1839 int timeout;
1840 1892
1841 /* try to distribute timeouts somewhat evenly */ 1893 /* try to distribute timeouts somewhat evenly */
1842 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1894 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1843 1895
1844 for (;;) 1896 for (;;)
1914/*****************************************************************************/ 1966/*****************************************************************************/
1915 1967
1916int ecb_cold 1968int ecb_cold
1917eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1969eio_init (void (*want_poll)(void), void (*done_poll)(void))
1918{ 1970{
1971#if !HAVE_PREADWRITE
1972 X_MUTEX_CREATE (preadwritelock);
1973#endif
1974
1919 return etp_init (want_poll, done_poll); 1975 return etp_init (want_poll, done_poll);
1920} 1976}
1921 1977
1922ecb_inline void 1978ecb_inline void
1923eio_api_destroy (eio_req *req) 1979eio_api_destroy (eio_req *req)
1968 case EIO_WRITE: req->result = req->offs >= 0 2024 case EIO_WRITE: req->result = req->offs >= 0
1969 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 2025 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
1970 : write (req->int1, req->ptr2, req->size); break; 2026 : write (req->int1, req->ptr2, req->size); break;
1971 2027
1972 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 2028 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1973 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break; 2029 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
1974 2030
1975 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2031 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1976 req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; 2032 req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1977 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2033 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1978 req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; 2034 req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
2178eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data) 2234eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
2179{ 2235{
2180 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; 2236 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
2181} 2237}
2182 2238
2183eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2239eio_req *eio_fchown (int fd, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2184{ 2240{
2185 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2241 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2186} 2242}
2187 2243
2188eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) 2244eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
2208eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data) 2264eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
2209{ 2265{
2210 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; 2266 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
2211} 2267}
2212 2268
2213eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2269eio_req *eio_chown (const char *path, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2214{ 2270{
2215 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2271 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2216} 2272}
2217 2273
2218eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) 2274eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
2365} 2421}
2366 2422
2367/*****************************************************************************/ 2423/*****************************************************************************/
2368/* misc garbage */ 2424/* misc garbage */
2369 2425
2370ssize_t 2426eio_ssize_t
2371eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2427eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2372{ 2428{
2373 etp_worker wrk;
2374 ssize_t ret;
2375
2376 wrk.dbuf = 0;
2377
2378 ret = eio__sendfile (ofd, ifd, offset, count, &wrk); 2429 return eio__sendfile (ofd, ifd, offset, count);
2379
2380 if (wrk.dbuf)
2381 free (wrk.dbuf);
2382
2383 return ret;
2384} 2430}
2385 2431

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines