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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines