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.96 by root, Tue Jul 19 05:20:36 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>
134 #include <sys/statvfs.h> 167 #include <sys/statvfs.h>
135 #include <unistd.h> 168 #include <unistd.h>
136 #include <utime.h>
137 #include <signal.h> 169 #include <signal.h>
138 #include <dirent.h> 170 #include <dirent.h>
139 171
140 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 172 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
141 #include <sys/mman.h> 173 #include <sys/mman.h>
142 #endif 174 #endif
175
176 #define D_NAME(entp) entp->d_name
143 177
144 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 178 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
145 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 179 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
146 #define _DIRENT_HAVE_D_TYPE /* sigh */ 180 #define _DIRENT_HAVE_D_TYPE /* sigh */
147 #define D_INO(de) (de)->d_fileno 181 #define D_INO(de) (de)->d_fileno
161 195
162 #ifndef EIO_STRUCT_DIRENT 196 #ifndef EIO_STRUCT_DIRENT
163 #define EIO_STRUCT_DIRENT struct dirent 197 #define EIO_STRUCT_DIRENT struct dirent
164 #endif 198 #endif
165 199
200#endif
201
202#if HAVE_UTIMES
203# include <utime.h>
166#endif 204#endif
167 205
168#if HAVE_SENDFILE 206#if HAVE_SENDFILE
169# if __linux 207# if __linux
170# include <sys/sendfile.h> 208# include <sys/sendfile.h>
185#endif 223#endif
186#ifndef D_INO 224#ifndef D_INO
187# define D_INO(de) 0 225# define D_INO(de) 0
188#endif 226#endif
189#ifndef D_NAMLEN 227#ifndef D_NAMLEN
190# define D_NAMLEN(de) strlen ((de)->d_name) 228# define D_NAMLEN(entp) strlen (D_NAME (entp))
191#endif 229#endif
192 230
193/* used for struct dirent, AIX doesn't provide it */ 231/* used for struct dirent, AIX doesn't provide it */
194#ifndef NAME_MAX 232#ifndef NAME_MAX
195# define NAME_MAX 4096 233# define NAME_MAX 4096
202 240
203/* buffer size for various temporary buffers */ 241/* buffer size for various temporary buffers */
204#define EIO_BUFSIZE 65536 242#define EIO_BUFSIZE 65536
205 243
206#define dBUF \ 244#define dBUF \
207 char *eio_buf; \
208 ETP_WORKER_LOCK (self); \
209 self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \ 245 char *eio_buf = malloc (EIO_BUFSIZE); \
210 ETP_WORKER_UNLOCK (self); \
211 errno = ENOMEM; \ 246 errno = ENOMEM; \
212 if (!eio_buf) \ 247 if (!eio_buf) \
213 return -1; 248 return -1
249
250#define FUBd \
251 free (eio_buf)
214 252
215#define EIO_TICKS ((1000000 + 1023) >> 10) 253#define EIO_TICKS ((1000000 + 1023) >> 10)
216 254
217#define ETP_PRI_MIN EIO_PRI_MIN 255#define ETP_PRI_MIN EIO_PRI_MIN
218#define ETP_PRI_MAX EIO_PRI_MAX 256#define ETP_PRI_MAX EIO_PRI_MAX
224static int eio_finish (eio_req *req); 262static int eio_finish (eio_req *req);
225#define ETP_FINISH(req) eio_finish (req) 263#define ETP_FINISH(req) eio_finish (req)
226static void eio_execute (struct etp_worker *self, eio_req *req); 264static void eio_execute (struct etp_worker *self, eio_req *req);
227#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 265#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req)
228 266
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/*****************************************************************************/ 267/*****************************************************************************/
247 268
248#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 269#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
249 270
250/* calculate time difference in ~1/EIO_TICKS of a second */ 271/* calculate time difference in ~1/EIO_TICKS of a second */
278/* 299/*
279 * make our pread/pwrite emulation safe against themselves, but not against 300 * make our pread/pwrite emulation safe against themselves, but not against
280 * normal read/write by using a mutex. slows down execution a lot, 301 * normal read/write by using a mutex. slows down execution a lot,
281 * but that's your problem, not mine. 302 * but that's your problem, not mine.
282 */ 303 */
283static xmutex_t preadwritelock = X_MUTEX_INIT; 304static xmutex_t preadwritelock;
284#endif 305#endif
285 306
286typedef struct etp_worker 307typedef struct etp_worker
287{ 308{
288 /* locked by wrklock */ 309 /* locked by wrklock */
291 xthread_t tid; 312 xthread_t tid;
292 313
293 /* locked by reslock, reqlock or wrklock */ 314 /* locked by reslock, reqlock or wrklock */
294 ETP_REQ *req; /* currently processed request */ 315 ETP_REQ *req; /* currently processed request */
295 316
317#ifdef ETP_WORKER_COMMON
296 ETP_WORKER_COMMON 318 ETP_WORKER_COMMON
319#endif
297} etp_worker; 320} etp_worker;
298 321
299static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */ 322static etp_worker wrk_first; /* NOT etp */
300 323
301#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 324#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
302#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 325#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
303 326
304/* worker threads management */ 327/* worker threads management */
305 328
306static void ecb_cold 329static void ecb_cold
307etp_worker_clear (etp_worker *wrk) 330etp_worker_clear (etp_worker *wrk)
308{ 331{
309 ETP_WORKER_CLEAR (wrk);
310} 332}
311 333
312static void ecb_cold 334static void ecb_cold
313etp_worker_free (etp_worker *wrk) 335etp_worker_free (etp_worker *wrk)
314{ 336{
375} etp_reqq; 397} etp_reqq;
376 398
377static etp_reqq req_queue; 399static etp_reqq req_queue;
378static etp_reqq res_queue; 400static etp_reqq res_queue;
379 401
402static void ecb_noinline ecb_cold
403reqq_init (etp_reqq *q)
404{
405 int pri;
406
407 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
408 q->qs[pri] = q->qe[pri] = 0;
409
410 q->size = 0;
411}
412
380static int ecb_noinline 413static int ecb_noinline
381reqq_push (etp_reqq *q, ETP_REQ *req) 414reqq_push (etp_reqq *q, ETP_REQ *req)
382{ 415{
383 int pri = req->pri; 416 int pri = req->pri;
384 req->next = 0; 417 req->next = 0;
418 } 451 }
419 452
420 abort (); 453 abort ();
421} 454}
422 455
423static void ecb_cold 456static int ecb_cold
424etp_thread_init (void) 457etp_init (void (*want_poll)(void), void (*done_poll)(void))
425{ 458{
426#if !HAVE_PREADWRITE
427 X_MUTEX_CREATE (preadwritelock);
428#endif
429 X_MUTEX_CREATE (wrklock); 459 X_MUTEX_CREATE (wrklock);
430 X_MUTEX_CREATE (reslock); 460 X_MUTEX_CREATE (reslock);
431 X_MUTEX_CREATE (reqlock); 461 X_MUTEX_CREATE (reqlock);
432 X_COND_CREATE (reqwait); 462 X_COND_CREATE (reqwait);
433}
434 463
435static void ecb_cold 464 reqq_init (&req_queue);
436etp_atfork_prepare (void) 465 reqq_init (&res_queue);
437{
438}
439 466
440static void ecb_cold 467 wrk_first.next =
441etp_atfork_parent (void) 468 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 469
467 started = 0; 470 started = 0;
468 idle = 0; 471 idle = 0;
469 nreqs = 0; 472 nreqs = 0;
470 nready = 0; 473 nready = 0;
471 npending = 0; 474 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 475
490 want_poll_cb = want_poll; 476 want_poll_cb = want_poll;
491 done_poll_cb = done_poll; 477 done_poll_cb = done_poll;
492 478
493 return 0; 479 return 0;
876# undef pread 862# undef pread
877# undef pwrite 863# undef pwrite
878# define pread eio__pread 864# define pread eio__pread
879# define pwrite eio__pwrite 865# define pwrite eio__pwrite
880 866
881static ssize_t 867static eio_ssize_t
882eio__pread (int fd, void *buf, size_t count, off_t offset) 868eio__pread (int fd, void *buf, size_t count, off_t offset)
883{ 869{
884 ssize_t res; 870 eio_ssize_t res;
885 off_t ooffset; 871 off_t ooffset;
886 872
887 X_LOCK (preadwritelock); 873 X_LOCK (preadwritelock);
888 ooffset = lseek (fd, 0, SEEK_CUR); 874 ooffset = lseek (fd, 0, SEEK_CUR);
889 lseek (fd, offset, SEEK_SET); 875 lseek (fd, offset, SEEK_SET);
892 X_UNLOCK (preadwritelock); 878 X_UNLOCK (preadwritelock);
893 879
894 return res; 880 return res;
895} 881}
896 882
897static ssize_t 883static eio_ssize_t
898eio__pwrite (int fd, void *buf, size_t count, off_t offset) 884eio__pwrite (int fd, void *buf, size_t count, off_t offset)
899{ 885{
900 ssize_t res; 886 eio_ssize_t res;
901 off_t ooffset; 887 off_t ooffset;
902 888
903 X_LOCK (preadwritelock); 889 X_LOCK (preadwritelock);
904 ooffset = lseek (fd, 0, SEEK_CUR); 890 ooffset = lseek (fd, 0, SEEK_CUR);
905 lseek (fd, offset, SEEK_SET); 891 lseek (fd, offset, SEEK_SET);
994 980
995#if !HAVE_READAHEAD 981#if !HAVE_READAHEAD
996# undef readahead 982# undef readahead
997# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 983# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
998 984
999static ssize_t 985static eio_ssize_t
1000eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) 986eio__readahead (int fd, off_t offset, size_t count, etp_worker *self)
1001{ 987{
1002 size_t todo = count; 988 size_t todo = count;
1003 dBUF; 989 dBUF;
1004 990
1009 pread (fd, eio_buf, len, offset); 995 pread (fd, eio_buf, len, offset);
1010 offset += len; 996 offset += len;
1011 todo -= len; 997 todo -= len;
1012 } 998 }
1013 999
1000 FUBd;
1001
1014 errno = 0; 1002 errno = 0;
1015 return count; 1003 return count;
1016} 1004}
1017 1005
1018#endif 1006#endif
1019 1007
1020/* sendfile always needs emulation */ 1008/* sendfile always needs emulation */
1021static ssize_t 1009static eio_ssize_t
1022eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 1010eio__sendfile (int ofd, int ifd, off_t offset, size_t count)
1023{ 1011{
1024 ssize_t written = 0; 1012 eio_ssize_t written = 0;
1025 ssize_t res; 1013 eio_ssize_t res;
1026 1014
1027 if (!count) 1015 if (!count)
1028 return 0; 1016 return 0;
1029 1017
1030 for (;;) 1018 for (;;)
1125 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1113 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1126 /* BSDs */ 1114 /* BSDs */
1127#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1115#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1128 || errno == ENOTSUP 1116 || errno == ENOTSUP
1129#endif 1117#endif
1118#ifdef EOPNOTSUPP /* windows */
1130 || errno == EOPNOTSUPP /* BSDs */ 1119 || errno == EOPNOTSUPP /* BSDs */
1120#endif
1131#if __solaris 1121#if __solaris
1132 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 1122 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1133#endif 1123#endif
1134 ) 1124 )
1135 ) 1125 )
1139 1129
1140 res = 0; 1130 res = 0;
1141 1131
1142 while (count) 1132 while (count)
1143 { 1133 {
1144 ssize_t cnt; 1134 eio_ssize_t cnt;
1145 1135
1146 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset); 1136 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset);
1147 1137
1148 if (cnt <= 0) 1138 if (cnt <= 0)
1149 { 1139 {
1161 1151
1162 offset += cnt; 1152 offset += cnt;
1163 res += cnt; 1153 res += cnt;
1164 count -= cnt; 1154 count -= cnt;
1165 } 1155 }
1156
1157 FUBd;
1166 } 1158 }
1167 1159
1168 return res; 1160 return res;
1169} 1161}
1170 1162
1197 /* round up length */ 1189 /* round up length */
1198 *length = (*length + mask) & ~mask; 1190 *length = (*length + mask) & ~mask;
1199} 1191}
1200 1192
1201#if !_POSIX_MEMLOCK 1193#if !_POSIX_MEMLOCK
1202# define eio__mlockall(a) eio_nosyscall() 1194# define eio__mlockall(a) EIO_ENOSYS ()
1203#else 1195#else
1204 1196
1205static int 1197static int
1206eio__mlockall (int flags) 1198eio__mlockall (int flags)
1207{ 1199{
1357 res += strlen (res); 1349 res += strlen (res);
1358 } 1350 }
1359 1351
1360 while (*rel) 1352 while (*rel)
1361 { 1353 {
1362 ssize_t len, linklen; 1354 eio_ssize_t len, linklen;
1363 char *beg = rel; 1355 char *beg = rel;
1364 1356
1365 while (*rel && *rel != '/') 1357 while (*rel && *rel != '/')
1366 ++rel; 1358 ++rel;
1367 1359
1460 1452
1461#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1453#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 */ 1454#define EIO_SORT_FAST 60 /* when to only use insertion sort */
1463 1455
1464static void 1456static void
1465eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1457eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1466{ 1458{
1467 unsigned char bits [9 + sizeof (ino_t) * 8]; 1459 unsigned char bits [9 + sizeof (eio_ino_t) * 8];
1468 unsigned char *bit = bits; 1460 unsigned char *bit = bits;
1469 1461
1470 assert (CHAR_BIT == 8); 1462 assert (CHAR_BIT == 8);
1471 assert (sizeof (eio_dirent) * 8 < 256); 1463 assert (sizeof (eio_dirent) * 8 < 256);
1472 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ 1464 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1474 1466
1475 if (size <= EIO_SORT_FAST) 1467 if (size <= EIO_SORT_FAST)
1476 return; 1468 return;
1477 1469
1478 /* first prepare an array of bits to test in our radix sort */ 1470 /* 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 */ 1471 /* try to take endianness into account, as well as differences in eio_ino_t sizes */
1480 /* inode_bits must contain all inodes ORed together */ 1472 /* inode_bits must contain all inodes ORed together */
1481 /* which is used to skip bits that are 0 everywhere, which is very common */ 1473 /* which is used to skip bits that are 0 everywhere, which is very common */
1482 { 1474 {
1483 ino_t endianness; 1475 eio_ino_t endianness;
1484 int i, j; 1476 int i, j;
1485 1477
1486 /* we store the byte offset of byte n into byte n of "endianness" */ 1478 /* we store the byte offset of byte n into byte n of "endianness" */
1487 for (i = 0; i < sizeof (ino_t); ++i) 1479 for (i = 0; i < sizeof (eio_ino_t); ++i)
1488 ((unsigned char *)&endianness)[i] = i; 1480 ((unsigned char *)&endianness)[i] = i;
1489 1481
1490 *bit++ = 0; 1482 *bit++ = 0;
1491 1483
1492 for (i = 0; i < sizeof (ino_t); ++i) 1484 for (i = 0; i < sizeof (eio_ino_t); ++i)
1493 { 1485 {
1494 /* shifting off the byte offsets out of "endianness" */ 1486 /* shifting off the byte offsets out of "endianness" */
1495 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8; 1487 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8;
1496 endianness >>= 8; 1488 endianness >>= 8;
1497 1489
1498 for (j = 0; j < 8; ++j) 1490 for (j = 0; j < 8; ++j)
1499 if (inode_bits & (((ino_t)1) << (i * 8 + j))) 1491 if (inode_bits & (((eio_ino_t)1) << (i * 8 + j)))
1500 *bit++ = offs + j; 1492 *bit++ = offs + j;
1501 } 1493 }
1502 1494
1503 for (j = 0; j < 8; ++j) 1495 for (j = 0; j < 8; ++j)
1504 if (score_bits & (1 << j)) 1496 if (score_bits & (1 << j))
1505 *bit++ = offsetof (eio_dirent, score) * 8 + j; 1497 *bit++ = offsetof (eio_dirent, score) * 8 + j;
1506 } 1498 }
1507 1499
1508 /* now actually do the sorting (a variant of MSD radix sort) */ 1500 /* now actually do the sorting (a variant of MSD radix sort) */
1509 { 1501 {
1510 eio_dirent *base_stk [9 + sizeof (ino_t) * 8], *base; 1502 eio_dirent *base_stk [9 + sizeof (eio_ino_t) * 8], *base;
1511 eio_dirent *end_stk [9 + sizeof (ino_t) * 8], *end; 1503 eio_dirent *end_stk [9 + sizeof (eio_ino_t) * 8], *end;
1512 unsigned char *bit_stk [9 + sizeof (ino_t) * 8]; 1504 unsigned char *bit_stk [9 + sizeof (eio_ino_t) * 8];
1513 int stk_idx = 0; 1505 int stk_idx = 0;
1514 1506
1515 base_stk [stk_idx] = dents; 1507 base_stk [stk_idx] = dents;
1516 end_stk [stk_idx] = dents + size; 1508 end_stk [stk_idx] = dents + size;
1517 bit_stk [stk_idx] = bit - 1; 1509 bit_stk [stk_idx] = bit - 1;
1596 } 1588 }
1597 } 1589 }
1598} 1590}
1599 1591
1600static void 1592static void
1601eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1593eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1602{ 1594{
1603 if (size <= 1) 1595 if (size <= 1)
1604 return; /* our insertion sort relies on size > 0 */ 1596 return; /* our insertion sort relies on size > 0 */
1605 1597
1606 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */ 1598 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */
1614 1606
1615/* read a full directory */ 1607/* read a full directory */
1616static void 1608static void
1617eio__scandir (eio_req *req, etp_worker *self) 1609eio__scandir (eio_req *req, etp_worker *self)
1618{ 1610{
1619 DIR *dirp;
1620 EIO_STRUCT_DIRENT *entp;
1621 char *name, *names; 1611 char *name, *names;
1622 int namesalloc = 4096; 1612 int namesalloc = 4096 - sizeof (void *) * 4;
1623 int namesoffs = 0; 1613 int namesoffs = 0;
1624 int flags = req->int1; 1614 int flags = req->int1;
1625 eio_dirent *dents = 0; 1615 eio_dirent *dents = 0;
1626 int dentalloc = 128; 1616 int dentalloc = 128;
1627 int dentoffs = 0; 1617 int dentoffs = 0;
1628 ino_t inode_bits = 0; 1618 eio_ino_t inode_bits = 0;
1619#ifdef _WIN32
1620 HANDLE dirp;
1621 WIN32_FIND_DATA entp;
1622#else
1623 DIR *dirp;
1624 EIO_STRUCT_DIRENT *entp;
1625#endif
1629 1626
1630 req->result = -1; 1627 req->result = -1;
1631 1628
1632 if (!(flags & EIO_READDIR_DENTS)) 1629 if (!(flags & EIO_READDIR_DENTS))
1633 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1630 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1634 1631
1635 X_LOCK (wrklock); 1632#ifdef _WIN32
1636 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1633 {
1634 int len = strlen ((const char *)req->ptr1);
1635 char *path = malloc (MAX_PATH);
1636 const char *fmt;
1637
1638 if (!len)
1639 fmt = "./*";
1640 else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\')
1641 fmt = "%s*";
1642 else
1643 fmt = "%s/*";
1644
1645 _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1);
1646 dirp = FindFirstFile (path, &entp);
1647 free (path);
1648
1649 if (dirp == INVALID_HANDLE_VALUE)
1650 {
1651 dirp = 0;
1652
1653 switch (GetLastError ())
1654 {
1655 case ERROR_FILE_NOT_FOUND:
1656 req->result = 0;
1657 break;
1658
1659 case ERROR_INVALID_NAME:
1660 case ERROR_PATH_NOT_FOUND:
1661 case ERROR_NO_MORE_FILES:
1662 errno = ENOENT;
1663 break;
1664
1665 case ERROR_NOT_ENOUGH_MEMORY:
1666 errno = ENOMEM;
1667 break;
1668
1669 default:
1670 errno = EINVAL;
1671 break;
1672 }
1673 }
1674 }
1675#else
1637 self->dirp = dirp = opendir (req->ptr1); 1676 dirp = opendir (req->ptr1);
1677#endif
1638 1678
1639 if (req->flags & EIO_FLAG_PTR1_FREE) 1679 if (req->flags & EIO_FLAG_PTR1_FREE)
1640 free (req->ptr1); 1680 free (req->ptr1);
1641 1681
1642 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1682 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1643 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1683 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1644 req->ptr2 = names = malloc (namesalloc); 1684 req->ptr2 = names = malloc (namesalloc);
1645 X_UNLOCK (wrklock);
1646 1685
1647 if (dirp && names && (!flags || dents)) 1686 if (dirp && names && (!flags || dents))
1648 for (;;) 1687 for (;;)
1649 { 1688 {
1689 int done;
1690
1691#ifdef _WIN32
1692 done = !dirp;
1693#else
1650 errno = 0; 1694 errno = 0;
1651 entp = readdir (dirp); 1695 entp = readdir (dirp);
1696 done = !entp;
1697#endif
1652 1698
1653 if (!entp) 1699 if (done)
1654 { 1700 {
1701#ifndef _WIN32
1702 int old_errno = errno;
1703 closedir (dirp);
1704 errno = old_errno;
1705
1655 if (errno) 1706 if (errno)
1656 break; 1707 break;
1708#endif
1657 1709
1658 /* sort etc. */ 1710 /* sort etc. */
1659 req->int1 = flags; 1711 req->int1 = flags;
1660 req->result = dentoffs; 1712 req->result = dentoffs;
1661 1713
1690 1742
1691 break; 1743 break;
1692 } 1744 }
1693 1745
1694 /* now add the entry to our list(s) */ 1746 /* now add the entry to our list(s) */
1695 name = entp->d_name; 1747 name = D_NAME (entp);
1696 1748
1697 /* skip . and .. entries */ 1749 /* skip . and .. entries */
1698 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1750 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1699 { 1751 {
1700 int len = D_NAMLEN (entp) + 1; 1752 int len = D_NAMLEN (entp) + 1;
1701 1753
1702 while (ecb_expect_false (namesoffs + len > namesalloc)) 1754 while (ecb_expect_false (namesoffs + len > namesalloc))
1703 { 1755 {
1704 namesalloc *= 2; 1756 namesalloc *= 2;
1705 X_LOCK (wrklock);
1706 req->ptr2 = names = realloc (names, namesalloc); 1757 req->ptr2 = names = realloc (names, namesalloc);
1707 X_UNLOCK (wrklock);
1708 1758
1709 if (!names) 1759 if (!names)
1710 break; 1760 break;
1711 } 1761 }
1712 1762
1717 struct eio_dirent *ent; 1767 struct eio_dirent *ent;
1718 1768
1719 if (ecb_expect_false (dentoffs == dentalloc)) 1769 if (ecb_expect_false (dentoffs == dentalloc))
1720 { 1770 {
1721 dentalloc *= 2; 1771 dentalloc *= 2;
1722 X_LOCK (wrklock);
1723 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1772 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1724 X_UNLOCK (wrklock);
1725 1773
1726 if (!dents) 1774 if (!dents)
1727 break; 1775 break;
1728 } 1776 }
1729 1777
1809 if (EIO_CANCELLED (req)) 1857 if (EIO_CANCELLED (req))
1810 { 1858 {
1811 errno = ECANCELED; 1859 errno = ECANCELED;
1812 break; 1860 break;
1813 } 1861 }
1862
1863#ifdef _WIN32
1864 if (!FindNextFile (dirp, &entp))
1865 {
1866 FindClose (dirp);
1867 dirp = 0;
1868 }
1869#endif
1814 } 1870 }
1815} 1871}
1816 1872
1817/*****************************************************************************/ 1873/*****************************************************************************/
1818 1874
1834X_THREAD_PROC (etp_proc) 1890X_THREAD_PROC (etp_proc)
1835{ 1891{
1836 ETP_REQ *req; 1892 ETP_REQ *req;
1837 struct timespec ts; 1893 struct timespec ts;
1838 etp_worker *self = (etp_worker *)thr_arg; 1894 etp_worker *self = (etp_worker *)thr_arg;
1839 int timeout;
1840 1895
1841 /* try to distribute timeouts somewhat evenly */ 1896 /* try to distribute timeouts somewhat evenly */
1842 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1897 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1843 1898
1844 for (;;) 1899 for (;;)
1914/*****************************************************************************/ 1969/*****************************************************************************/
1915 1970
1916int ecb_cold 1971int ecb_cold
1917eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1972eio_init (void (*want_poll)(void), void (*done_poll)(void))
1918{ 1973{
1974#if !HAVE_PREADWRITE
1975 X_MUTEX_CREATE (preadwritelock);
1976#endif
1977
1919 return etp_init (want_poll, done_poll); 1978 return etp_init (want_poll, done_poll);
1920} 1979}
1921 1980
1922ecb_inline void 1981ecb_inline void
1923eio_api_destroy (eio_req *req) 1982eio_api_destroy (eio_req *req)
1968 case EIO_WRITE: req->result = req->offs >= 0 2027 case EIO_WRITE: req->result = req->offs >= 0
1969 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 2028 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
1970 : write (req->int1, req->ptr2, req->size); break; 2029 : write (req->int1, req->ptr2, req->size); break;
1971 2030
1972 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 2031 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; 2032 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
1974 2033
1975 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2034 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1976 req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; 2035 req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1977 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2036 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1978 req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; 2037 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) 2237eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
2179{ 2238{
2180 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; 2239 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
2181} 2240}
2182 2241
2183eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2242eio_req *eio_fchown (int fd, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2184{ 2243{
2185 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2244 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2186} 2245}
2187 2246
2188eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) 2247eio_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) 2267eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
2209{ 2268{
2210 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; 2269 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
2211} 2270}
2212 2271
2213eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2272eio_req *eio_chown (const char *path, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2214{ 2273{
2215 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2274 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2216} 2275}
2217 2276
2218eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) 2277eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
2365} 2424}
2366 2425
2367/*****************************************************************************/ 2426/*****************************************************************************/
2368/* misc garbage */ 2427/* misc garbage */
2369 2428
2370ssize_t 2429eio_ssize_t
2371eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2430eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2372{ 2431{
2373 etp_worker wrk;
2374 ssize_t ret;
2375
2376 wrk.dbuf = 0;
2377
2378 ret = eio__sendfile (ofd, ifd, offset, count, &wrk); 2432 return eio__sendfile (ofd, ifd, offset, count);
2379
2380 if (wrk.dbuf)
2381 free (wrk.dbuf);
2382
2383 return ret;
2384} 2433}
2385 2434

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines