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

Comparing libeio/eio.c (file contents):
Revision 1.80 by root, Tue Jul 5 18:59:28 2011 UTC vs.
Revision 1.92 by root, Sun Jul 17 18:27:17 2011 UTC

54#include <stdlib.h> 54#include <stdlib.h>
55#include <string.h> 55#include <string.h>
56#include <errno.h> 56#include <errno.h>
57#include <sys/types.h> 57#include <sys/types.h>
58#include <sys/stat.h> 58#include <sys/stat.h>
59#include <sys/statvfs.h>
60#include <limits.h> 59#include <limits.h>
61#include <fcntl.h> 60#include <fcntl.h>
62#include <assert.h> 61#include <assert.h>
63 62
64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
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
83 85
84#ifndef EIO_FEED 86#ifndef EIO_FEED
85# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0) 87# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0)
86#endif 88#endif
87 89
90#ifndef EIO_FD_TO_WIN32_HANDLE
91# define EIO_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
92#endif
93#ifndef EIO_WIN32_HANDLE_TO_FD
94# define EIO_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
95#endif
96
97#define EIO_ERRNO(errval,retval) ((errno = errval), retval)
98
99#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1)
100
88#ifdef _WIN32 101#ifdef _WIN32
89 102
90 /*doh*/ 103 #undef PAGESIZE
104 #define PAGESIZE 4096 /* GetSystemInfo? */
105
106 #ifdef EIO_STRUCT_STATI64
107 #define stat(path,buf) _stati64 (path,buf)
108 #define fstat(fd,buf) _fstati64 (path,buf)
109 #endif
110 #define lstat(path,buf) stat (path,buf)
111 #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1))
112 #define mkdir(path,mode) _mkdir (path)
113 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
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 ()
120 #define chown(path,uid,gid) EIO_ENOSYS ()
121 #define fchown(fd,uid,gid) EIO_ENOSYS ()
122 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
123 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
124 #define mknod(path,mode,dev) 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 ()
129
130 /* we could even stat and see if it exists */
131 static int
132 symlink (const char *old, const char *neu)
133 {
134 #if WINVER >= 0x0600
135 if (CreateSymbolicLink (neu, old, 1))
136 return 0;
137
138 if (CreateSymbolicLink (neu, old, 0))
139 return 0;
140 #endif
141
142 return EIO_ERRNO (ENOENT, -1);
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)
158
91#else 159#else
92 160
93# include <sys/time.h> 161 #include <sys/time.h>
94# include <sys/select.h> 162 #include <sys/select.h>
163 #include <sys/statvfs.h>
95# include <unistd.h> 164 #include <unistd.h>
96# include <utime.h> 165 #include <utime.h>
97# include <signal.h> 166 #include <signal.h>
98# include <dirent.h> 167 #include <dirent.h>
99 168
100#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 169 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
101# include <sys/mman.h> 170 #include <sys/mman.h>
102#endif 171 #endif
103 172
173 #define D_NAME(entp) entp->d_name
174
104/* 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 */
105# if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 176 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
106# define _DIRENT_HAVE_D_TYPE /* sigh */ 177 #define _DIRENT_HAVE_D_TYPE /* sigh */
107# define D_INO(de) (de)->d_fileno 178 #define D_INO(de) (de)->d_fileno
108# define D_NAMLEN(de) (de)->d_namlen 179 #define D_NAMLEN(de) (de)->d_namlen
109# elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 180 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
110# define D_INO(de) (de)->d_ino 181 #define D_INO(de) (de)->d_ino
111# endif 182 #endif
112 183
113#ifdef _D_EXACT_NAMLEN 184 #ifdef _D_EXACT_NAMLEN
114# undef D_NAMLEN 185 #undef D_NAMLEN
115# define D_NAMLEN(de) _D_EXACT_NAMLEN (de) 186 #define D_NAMLEN(de) _D_EXACT_NAMLEN (de)
116#endif 187 #endif
117 188
118# ifdef _DIRENT_HAVE_D_TYPE 189 #ifdef _DIRENT_HAVE_D_TYPE
119# define D_TYPE(de) (de)->d_type 190 #define D_TYPE(de) (de)->d_type
120# endif 191 #endif
121 192
122# ifndef EIO_STRUCT_DIRENT 193 #ifndef EIO_STRUCT_DIRENT
123# define EIO_STRUCT_DIRENT struct dirent 194 #define EIO_STRUCT_DIRENT struct dirent
124# endif 195 #endif
125 196
126#endif 197#endif
127 198
128#if HAVE_SENDFILE 199#if HAVE_SENDFILE
129# if __linux 200# if __linux
145#endif 216#endif
146#ifndef D_INO 217#ifndef D_INO
147# define D_INO(de) 0 218# define D_INO(de) 0
148#endif 219#endif
149#ifndef D_NAMLEN 220#ifndef D_NAMLEN
150# define D_NAMLEN(de) strlen ((de)->d_name) 221# define D_NAMLEN(entp) strlen (D_NAME (entp))
151#endif 222#endif
152 223
153/* used for struct dirent, AIX doesn't provide it */ 224/* used for struct dirent, AIX doesn't provide it */
154#ifndef NAME_MAX 225#ifndef NAME_MAX
155# define NAME_MAX 4096 226# define NAME_MAX 4096
189#define ETP_WORKER_CLEAR(req) \ 260#define ETP_WORKER_CLEAR(req) \
190 if (wrk->dbuf) \ 261 if (wrk->dbuf) \
191 { \ 262 { \
192 free (wrk->dbuf); \ 263 free (wrk->dbuf); \
193 wrk->dbuf = 0; \ 264 wrk->dbuf = 0; \
194 } \
195 \
196 if (wrk->dirp) \
197 { \
198 closedir (wrk->dirp); \
199 wrk->dirp = 0; \
200 } 265 }
201 266
202#define ETP_WORKER_COMMON \ 267#define ETP_WORKER_COMMON \
203 void *dbuf; \ 268 void *dbuf;
204 DIR *dirp;
205 269
206/*****************************************************************************/ 270/*****************************************************************************/
207 271
208#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 272#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
209 273
335} etp_reqq; 399} etp_reqq;
336 400
337static etp_reqq req_queue; 401static etp_reqq req_queue;
338static etp_reqq res_queue; 402static etp_reqq res_queue;
339 403
404static void ecb_noinline ecb_cold
405reqq_init (etp_reqq *q)
406{
407 int pri;
408
409 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
410 q->qs[pri] = q->qe[pri] = 0;
411
412 q->size = 0;
413}
414
340static int ecb_noinline 415static int ecb_noinline
341reqq_push (etp_reqq *q, ETP_REQ *req) 416reqq_push (etp_reqq *q, ETP_REQ *req)
342{ 417{
343 int pri = req->pri; 418 int pri = req->pri;
344 req->next = 0; 419 req->next = 0;
381} 456}
382 457
383static void ecb_cold 458static void ecb_cold
384etp_thread_init (void) 459etp_thread_init (void)
385{ 460{
461#if !HAVE_PREADWRITE
462 X_MUTEX_CREATE (preadwritelock);
463#endif
386 X_MUTEX_CREATE (wrklock); 464 X_MUTEX_CREATE (wrklock);
387 X_MUTEX_CREATE (reslock); 465 X_MUTEX_CREATE (reslock);
388 X_MUTEX_CREATE (reqlock); 466 X_MUTEX_CREATE (reqlock);
389 X_COND_CREATE (reqwait); 467 X_COND_CREATE (reqwait);
390} 468}
391 469
392static void ecb_cold 470static void ecb_cold
393etp_atfork_prepare (void)
394{
395 X_LOCK (wrklock);
396 X_LOCK (reqlock);
397 X_LOCK (reslock);
398#if !HAVE_PREADWRITE
399 X_LOCK (preadwritelock);
400#endif
401}
402
403static void ecb_cold
404etp_atfork_parent (void)
405{
406#if !HAVE_PREADWRITE
407 X_UNLOCK (preadwritelock);
408#endif
409 X_UNLOCK (reslock);
410 X_UNLOCK (reqlock);
411 X_UNLOCK (wrklock);
412}
413
414static void ecb_cold
415etp_atfork_child (void) 471etp_atfork_child (void)
416{ 472{
417 ETP_REQ *prv; 473 reqq_init (&req_queue);
474 reqq_init (&res_queue);
418 475
419 while ((prv = reqq_shift (&req_queue))) 476 wrk_first.next =
420 ETP_DESTROY (prv); 477 wrk_first.prev = &wrk_first;
421
422 while ((prv = reqq_shift (&res_queue)))
423 ETP_DESTROY (prv);
424
425 while (wrk_first.next != &wrk_first)
426 {
427 etp_worker *wrk = wrk_first.next;
428
429 if (wrk->req)
430 ETP_DESTROY (wrk->req);
431
432 etp_worker_clear (wrk);
433 etp_worker_free (wrk);
434 }
435 478
436 started = 0; 479 started = 0;
437 idle = 0; 480 idle = 0;
438 nreqs = 0; 481 nreqs = 0;
439 nready = 0; 482 nready = 0;
444 487
445static void ecb_cold 488static void ecb_cold
446etp_once_init (void) 489etp_once_init (void)
447{ 490{
448 etp_thread_init (); 491 etp_thread_init ();
449 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); 492 X_THREAD_ATFORK (0, 0, etp_atfork_child);
450} 493}
451 494
452static int ecb_cold 495static int ecb_cold
453etp_init (void (*want_poll)(void), void (*done_poll)(void)) 496etp_init (void (*want_poll)(void), void (*done_poll)(void))
454{ 497{
589} 632}
590 633
591static void 634static void
592etp_cancel (ETP_REQ *req) 635etp_cancel (ETP_REQ *req)
593{ 636{
594 X_LOCK (wrklock); 637 req->cancelled = 1;
595 req->flags |= EIO_FLAG_CANCELLED;
596 X_UNLOCK (wrklock);
597 638
598 eio_grp_cancel (req); 639 eio_grp_cancel (req);
599} 640}
600 641
601static void 642static void
847# undef pread 888# undef pread
848# undef pwrite 889# undef pwrite
849# define pread eio__pread 890# define pread eio__pread
850# define pwrite eio__pwrite 891# define pwrite eio__pwrite
851 892
852static ssize_t 893static eio_ssize_t
853eio__pread (int fd, void *buf, size_t count, off_t offset) 894eio__pread (int fd, void *buf, size_t count, off_t offset)
854{ 895{
855 ssize_t res; 896 eio_ssize_t res;
856 off_t ooffset; 897 off_t ooffset;
857 898
858 X_LOCK (preadwritelock); 899 X_LOCK (preadwritelock);
859 ooffset = lseek (fd, 0, SEEK_CUR); 900 ooffset = lseek (fd, 0, SEEK_CUR);
860 lseek (fd, offset, SEEK_SET); 901 lseek (fd, offset, SEEK_SET);
863 X_UNLOCK (preadwritelock); 904 X_UNLOCK (preadwritelock);
864 905
865 return res; 906 return res;
866} 907}
867 908
868static ssize_t 909static eio_ssize_t
869eio__pwrite (int fd, void *buf, size_t count, off_t offset) 910eio__pwrite (int fd, void *buf, size_t count, off_t offset)
870{ 911{
871 ssize_t res; 912 eio_ssize_t res;
872 off_t ooffset; 913 off_t ooffset;
873 914
874 X_LOCK (preadwritelock); 915 X_LOCK (preadwritelock);
875 ooffset = lseek (fd, 0, SEEK_CUR); 916 ooffset = lseek (fd, 0, SEEK_CUR);
876 lseek (fd, offset, SEEK_SET); 917 lseek (fd, offset, SEEK_SET);
950 /* even though we could play tricks with the flags, it's better to always 991 /* even though we could play tricks with the flags, it's better to always
951 * call fdatasync, as that matches the expectation of its users best */ 992 * call fdatasync, as that matches the expectation of its users best */
952 return fdatasync (fd); 993 return fdatasync (fd);
953} 994}
954 995
996static int
997eio__fallocate (int fd, int mode, off_t offset, size_t len)
998{
999#if HAVE_FALLOCATE
1000 return fallocate (fd, mode, offset, len);
1001#else
1002 errno = ENOSYS;
1003 return -1;
1004#endif
1005}
1006
955#if !HAVE_READAHEAD 1007#if !HAVE_READAHEAD
956# undef readahead 1008# undef readahead
957# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 1009# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
958 1010
959static ssize_t 1011static eio_ssize_t
960eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) 1012eio__readahead (int fd, off_t offset, size_t count, etp_worker *self)
961{ 1013{
962 size_t todo = count; 1014 size_t todo = count;
963 dBUF; 1015 dBUF;
964 1016
976} 1028}
977 1029
978#endif 1030#endif
979 1031
980/* sendfile always needs emulation */ 1032/* sendfile always needs emulation */
981static ssize_t 1033static eio_ssize_t
982eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 1034eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
983{ 1035{
984 ssize_t written = 0; 1036 eio_ssize_t written = 0;
985 ssize_t res; 1037 eio_ssize_t res;
986 1038
987 if (!count) 1039 if (!count)
988 return 0; 1040 return 0;
989 1041
990 for (;;) 1042 for (;;)
991 { 1043 {
1044#ifdef __APPLE__
1045# undef HAVE_SENDFILE /* broken, as everything on os x */
1046#endif
992#if HAVE_SENDFILE 1047#if HAVE_SENDFILE
993# if __linux 1048# if __linux
994 off_t soffset = offset; 1049 off_t soffset = offset;
995 res = sendfile (ofd, ifd, &soffset, count); 1050 res = sendfile (ofd, ifd, &soffset, count);
996 1051
1012 1067
1013 /* according to source inspection, this is correct, and useful behaviour */ 1068 /* according to source inspection, this is correct, and useful behaviour */
1014 if (sbytes) 1069 if (sbytes)
1015 res = sbytes; 1070 res = sbytes;
1016 1071
1017# elif defined (__APPLE__) && 0 /* broken, as everything on os x */ 1072# elif defined (__APPLE__)
1018 off_t sbytes = count; 1073 off_t sbytes = count;
1019 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1074 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
1020 1075
1021 /* according to the manpage, sbytes is always valid */ 1076 /* according to the manpage, sbytes is always valid */
1022 if (sbytes) 1077 if (sbytes)
1039 if (res < 0 && sbytes) 1094 if (res < 0 && sbytes)
1040 res = sbytes; 1095 res = sbytes;
1041 1096
1042# endif 1097# endif
1043 1098
1044#elif defined (_WIN32) 1099#elif defined (_WIN32) && 0
1045 /* does not work, just for documentation of what would need to be done */ 1100 /* does not work, just for documentation of what would need to be done */
1046 /* actually, cannot be done like this, as TransmitFile changes the file offset, */ 1101 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1047 /* libeio guarantees that the file offset does not change, and windows */ 1102 /* libeio guarantees that the file offset does not change, and windows */
1048 /* has no way to get an independent handle to the same file description */ 1103 /* has no way to get an independent handle to the same file description */
1049 HANDLE h = TO_SOCKET (ifd); 1104 HANDLE h = TO_SOCKET (ifd);
1082 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1137 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1083 /* BSDs */ 1138 /* BSDs */
1084#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1139#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1085 || errno == ENOTSUP 1140 || errno == ENOTSUP
1086#endif 1141#endif
1142#ifdef EOPNOTSUPP /* windows */
1087 || errno == EOPNOTSUPP /* BSDs */ 1143 || errno == EOPNOTSUPP /* BSDs */
1144#endif
1088#if __solaris 1145#if __solaris
1089 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 1146 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1090#endif 1147#endif
1091 ) 1148 )
1092 ) 1149 )
1096 1153
1097 res = 0; 1154 res = 0;
1098 1155
1099 while (count) 1156 while (count)
1100 { 1157 {
1101 ssize_t cnt; 1158 eio_ssize_t cnt;
1102 1159
1103 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset); 1160 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset);
1104 1161
1105 if (cnt <= 0) 1162 if (cnt <= 0)
1106 { 1163 {
1154 /* round up length */ 1211 /* round up length */
1155 *length = (*length + mask) & ~mask; 1212 *length = (*length + mask) & ~mask;
1156} 1213}
1157 1214
1158#if !_POSIX_MEMLOCK 1215#if !_POSIX_MEMLOCK
1159# define eio__mlockall(a) ((errno = ENOSYS), -1) 1216# define eio__mlockall(a) EIO_ENOSYS ()
1160#else 1217#else
1161 1218
1162static int 1219static int
1163eio__mlockall (int flags) 1220eio__mlockall (int flags)
1164{ 1221{
1178 return mlockall (flags); 1235 return mlockall (flags);
1179} 1236}
1180#endif 1237#endif
1181 1238
1182#if !_POSIX_MEMLOCK_RANGE 1239#if !_POSIX_MEMLOCK_RANGE
1183# define eio__mlock(a,b) ((errno = ENOSYS), -1) 1240# define eio__mlock(a,b) EIO_ENOSYS ()
1184#else 1241#else
1185 1242
1186static int 1243static int
1187eio__mlock (void *addr, size_t length) 1244eio__mlock (void *addr, size_t length)
1188{ 1245{
1192} 1249}
1193 1250
1194#endif 1251#endif
1195 1252
1196#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) 1253#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1197# define eio__msync(a,b,c) ((errno = ENOSYS), -1) 1254# define eio__msync(a,b,c) EIO_ENOSYS ()
1198#else 1255#else
1199 1256
1200static int 1257static int
1201eio__msync (void *mem, size_t len, int flags) 1258eio__msync (void *mem, size_t len, int flags)
1202{ 1259{
1314 res += strlen (res); 1371 res += strlen (res);
1315 } 1372 }
1316 1373
1317 while (*rel) 1374 while (*rel)
1318 { 1375 {
1319 ssize_t len, linklen; 1376 eio_ssize_t len, linklen;
1320 char *beg = rel; 1377 char *beg = rel;
1321 1378
1322 while (*rel && *rel != '/') 1379 while (*rel && *rel != '/')
1323 ++rel; 1380 ++rel;
1324 1381
1417 1474
1418#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1475#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1419#define EIO_SORT_FAST 60 /* when to only use insertion sort */ 1476#define EIO_SORT_FAST 60 /* when to only use insertion sort */
1420 1477
1421static void 1478static void
1422eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1479eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1423{ 1480{
1424 unsigned char bits [9 + sizeof (ino_t) * 8]; 1481 unsigned char bits [9 + sizeof (eio_ino_t) * 8];
1425 unsigned char *bit = bits; 1482 unsigned char *bit = bits;
1426 1483
1427 assert (CHAR_BIT == 8); 1484 assert (CHAR_BIT == 8);
1428 assert (sizeof (eio_dirent) * 8 < 256); 1485 assert (sizeof (eio_dirent) * 8 < 256);
1429 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ 1486 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1431 1488
1432 if (size <= EIO_SORT_FAST) 1489 if (size <= EIO_SORT_FAST)
1433 return; 1490 return;
1434 1491
1435 /* first prepare an array of bits to test in our radix sort */ 1492 /* first prepare an array of bits to test in our radix sort */
1436 /* try to take endianness into account, as well as differences in ino_t sizes */ 1493 /* try to take endianness into account, as well as differences in eio_ino_t sizes */
1437 /* inode_bits must contain all inodes ORed together */ 1494 /* inode_bits must contain all inodes ORed together */
1438 /* which is used to skip bits that are 0 everywhere, which is very common */ 1495 /* which is used to skip bits that are 0 everywhere, which is very common */
1439 { 1496 {
1440 ino_t endianness; 1497 eio_ino_t endianness;
1441 int i, j; 1498 int i, j;
1442 1499
1443 /* we store the byte offset of byte n into byte n of "endianness" */ 1500 /* we store the byte offset of byte n into byte n of "endianness" */
1444 for (i = 0; i < sizeof (ino_t); ++i) 1501 for (i = 0; i < sizeof (eio_ino_t); ++i)
1445 ((unsigned char *)&endianness)[i] = i; 1502 ((unsigned char *)&endianness)[i] = i;
1446 1503
1447 *bit++ = 0; 1504 *bit++ = 0;
1448 1505
1449 for (i = 0; i < sizeof (ino_t); ++i) 1506 for (i = 0; i < sizeof (eio_ino_t); ++i)
1450 { 1507 {
1451 /* shifting off the byte offsets out of "endianness" */ 1508 /* shifting off the byte offsets out of "endianness" */
1452 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8; 1509 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8;
1453 endianness >>= 8; 1510 endianness >>= 8;
1454 1511
1455 for (j = 0; j < 8; ++j) 1512 for (j = 0; j < 8; ++j)
1456 if (inode_bits & (((ino_t)1) << (i * 8 + j))) 1513 if (inode_bits & (((eio_ino_t)1) << (i * 8 + j)))
1457 *bit++ = offs + j; 1514 *bit++ = offs + j;
1458 } 1515 }
1459 1516
1460 for (j = 0; j < 8; ++j) 1517 for (j = 0; j < 8; ++j)
1461 if (score_bits & (1 << j)) 1518 if (score_bits & (1 << j))
1462 *bit++ = offsetof (eio_dirent, score) * 8 + j; 1519 *bit++ = offsetof (eio_dirent, score) * 8 + j;
1463 } 1520 }
1464 1521
1465 /* now actually do the sorting (a variant of MSD radix sort) */ 1522 /* now actually do the sorting (a variant of MSD radix sort) */
1466 { 1523 {
1467 eio_dirent *base_stk [9 + sizeof (ino_t) * 8], *base; 1524 eio_dirent *base_stk [9 + sizeof (eio_ino_t) * 8], *base;
1468 eio_dirent *end_stk [9 + sizeof (ino_t) * 8], *end; 1525 eio_dirent *end_stk [9 + sizeof (eio_ino_t) * 8], *end;
1469 unsigned char *bit_stk [9 + sizeof (ino_t) * 8]; 1526 unsigned char *bit_stk [9 + sizeof (eio_ino_t) * 8];
1470 int stk_idx = 0; 1527 int stk_idx = 0;
1471 1528
1472 base_stk [stk_idx] = dents; 1529 base_stk [stk_idx] = dents;
1473 end_stk [stk_idx] = dents + size; 1530 end_stk [stk_idx] = dents + size;
1474 bit_stk [stk_idx] = bit - 1; 1531 bit_stk [stk_idx] = bit - 1;
1553 } 1610 }
1554 } 1611 }
1555} 1612}
1556 1613
1557static void 1614static void
1558eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1615eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1559{ 1616{
1560 if (size <= 1) 1617 if (size <= 1)
1561 return; /* our insertion sort relies on size > 0 */ 1618 return; /* our insertion sort relies on size > 0 */
1562 1619
1563 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */ 1620 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */
1571 1628
1572/* read a full directory */ 1629/* read a full directory */
1573static void 1630static void
1574eio__scandir (eio_req *req, etp_worker *self) 1631eio__scandir (eio_req *req, etp_worker *self)
1575{ 1632{
1576 DIR *dirp;
1577 EIO_STRUCT_DIRENT *entp;
1578 char *name, *names; 1633 char *name, *names;
1579 int namesalloc = 4096; 1634 int namesalloc = 4096 - sizeof (void *) * 4;
1580 int namesoffs = 0; 1635 int namesoffs = 0;
1581 int flags = req->int1; 1636 int flags = req->int1;
1582 eio_dirent *dents = 0; 1637 eio_dirent *dents = 0;
1583 int dentalloc = 128; 1638 int dentalloc = 128;
1584 int dentoffs = 0; 1639 int dentoffs = 0;
1585 ino_t inode_bits = 0; 1640 eio_ino_t inode_bits = 0;
1641#ifdef _WIN32
1642 HANDLE dirp;
1643 WIN32_FIND_DATA entp;
1644#else
1645 DIR *dirp;
1646 EIO_STRUCT_DIRENT *entp;
1647#endif
1586 1648
1587 req->result = -1; 1649 req->result = -1;
1588 1650
1589 if (!(flags & EIO_READDIR_DENTS)) 1651 if (!(flags & EIO_READDIR_DENTS))
1590 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1652 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1591 1653
1592 X_LOCK (wrklock); 1654#ifdef _WIN32
1593 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1655 {
1656 int len = strlen ((const char *)req->ptr1);
1657 char *path = malloc (MAX_PATH);
1658 const char *fmt;
1659
1660 if (!len)
1661 fmt = "./*";
1662 else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\')
1663 fmt = "%s*";
1664 else
1665 fmt = "%s/*";
1666
1667 _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1);
1668 dirp = FindFirstFile (path, &entp);
1669 free (path);
1670
1671 if (dirp == INVALID_HANDLE_VALUE)
1672 {
1673 dirp = 0;
1674
1675 switch (GetLastError ())
1676 {
1677 case ERROR_FILE_NOT_FOUND:
1678 req->result = 0;
1679 break;
1680
1681 case ERROR_INVALID_NAME:
1682 case ERROR_PATH_NOT_FOUND:
1683 case ERROR_NO_MORE_FILES:
1684 errno = ENOENT;
1685 break;
1686
1687 case ERROR_NOT_ENOUGH_MEMORY:
1688 errno = ENOMEM;
1689 break;
1690
1691 default:
1692 errno = EINVAL;
1693 break;
1694 }
1695 }
1696 }
1697#else
1594 self->dirp = dirp = opendir (req->ptr1); 1698 dirp = opendir (req->ptr1);
1699#endif
1595 1700
1596 if (req->flags & EIO_FLAG_PTR1_FREE) 1701 if (req->flags & EIO_FLAG_PTR1_FREE)
1597 free (req->ptr1); 1702 free (req->ptr1);
1598 1703
1599 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1704 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1600 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1705 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1601 req->ptr2 = names = malloc (namesalloc); 1706 req->ptr2 = names = malloc (namesalloc);
1602 X_UNLOCK (wrklock);
1603 1707
1604 if (dirp && names && (!flags || dents)) 1708 if (dirp && names && (!flags || dents))
1605 for (;;) 1709 for (;;)
1606 { 1710 {
1711 int done;
1712
1713#ifdef _WIN32
1714 done = !dirp;
1715#else
1607 errno = 0; 1716 errno = 0;
1608 entp = readdir (dirp); 1717 entp = readdir (dirp);
1718 done = !entp;
1719#endif
1609 1720
1610 if (!entp) 1721 if (done)
1611 { 1722 {
1723#ifndef _WIN32
1724 int old_errno = errno;
1725 closedir (dirp);
1726 errno = old_errno;
1727
1612 if (errno) 1728 if (errno)
1613 break; 1729 break;
1730#endif
1614 1731
1615 /* sort etc. */ 1732 /* sort etc. */
1616 req->int1 = flags; 1733 req->int1 = flags;
1617 req->result = dentoffs; 1734 req->result = dentoffs;
1618 1735
1647 1764
1648 break; 1765 break;
1649 } 1766 }
1650 1767
1651 /* now add the entry to our list(s) */ 1768 /* now add the entry to our list(s) */
1652 name = entp->d_name; 1769 name = D_NAME (entp);
1653 1770
1654 /* skip . and .. entries */ 1771 /* skip . and .. entries */
1655 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1772 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1656 { 1773 {
1657 int len = D_NAMLEN (entp) + 1; 1774 int len = D_NAMLEN (entp) + 1;
1658 1775
1659 while (ecb_expect_false (namesoffs + len > namesalloc)) 1776 while (ecb_expect_false (namesoffs + len > namesalloc))
1660 { 1777 {
1661 namesalloc *= 2; 1778 namesalloc *= 2;
1662 X_LOCK (wrklock);
1663 req->ptr2 = names = realloc (names, namesalloc); 1779 req->ptr2 = names = realloc (names, namesalloc);
1664 X_UNLOCK (wrklock);
1665 1780
1666 if (!names) 1781 if (!names)
1667 break; 1782 break;
1668 } 1783 }
1669 1784
1674 struct eio_dirent *ent; 1789 struct eio_dirent *ent;
1675 1790
1676 if (ecb_expect_false (dentoffs == dentalloc)) 1791 if (ecb_expect_false (dentoffs == dentalloc))
1677 { 1792 {
1678 dentalloc *= 2; 1793 dentalloc *= 2;
1679 X_LOCK (wrklock);
1680 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1794 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1681 X_UNLOCK (wrklock);
1682 1795
1683 if (!dents) 1796 if (!dents)
1684 break; 1797 break;
1685 } 1798 }
1686 1799
1766 if (EIO_CANCELLED (req)) 1879 if (EIO_CANCELLED (req))
1767 { 1880 {
1768 errno = ECANCELED; 1881 errno = ECANCELED;
1769 break; 1882 break;
1770 } 1883 }
1884
1885#ifdef _WIN32
1886 if (!FindNextFile (dirp, &entp))
1887 {
1888 FindClose (dirp);
1889 dirp = 0;
1890 }
1891#endif
1771 } 1892 }
1772} 1893}
1773 1894
1774/*****************************************************************************/ 1895/*****************************************************************************/
1775 1896
1792{ 1913{
1793 ETP_REQ *req; 1914 ETP_REQ *req;
1794 struct timespec ts; 1915 struct timespec ts;
1795 etp_worker *self = (etp_worker *)thr_arg; 1916 etp_worker *self = (etp_worker *)thr_arg;
1796 1917
1797 /* try to distribute timeouts somewhat randomly */ 1918 /* try to distribute timeouts somewhat evenly */
1798 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1919 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1799 1920
1800 for (;;) 1921 for (;;)
1801 { 1922 {
1923 ts.tv_sec = 0;
1924
1802 X_LOCK (reqlock); 1925 X_LOCK (reqlock);
1803 1926
1804 for (;;) 1927 for (;;)
1805 { 1928 {
1806 self->req = req = reqq_shift (&req_queue); 1929 self->req = req = reqq_shift (&req_queue);
1807 1930
1808 if (req) 1931 if (req)
1809 break; 1932 break;
1810 1933
1934 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
1935 {
1936 X_UNLOCK (reqlock);
1937 X_LOCK (wrklock);
1938 --started;
1939 X_UNLOCK (wrklock);
1940 goto quit;
1941 }
1942
1811 ++idle; 1943 ++idle;
1812 1944
1813 ts.tv_sec = time (0) + idle_timeout; 1945 if (idle <= max_idle)
1814 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) 1946 /* we are allowed to idle, so do so without any timeout */
1947 X_COND_WAIT (reqwait, reqlock);
1948 else
1815 { 1949 {
1816 if (idle > max_idle) 1950 /* initialise timeout once */
1817 { 1951 if (!ts.tv_sec)
1818 --idle; 1952 ts.tv_sec = time (0) + idle_timeout;
1819 X_UNLOCK (reqlock);
1820 X_LOCK (wrklock);
1821 --started;
1822 X_UNLOCK (wrklock);
1823 goto quit;
1824 }
1825 1953
1826 /* we are allowed to idle, so do so without any timeout */
1827 X_COND_WAIT (reqwait, reqlock); 1954 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1955 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
1828 } 1956 }
1829 1957
1830 --idle; 1958 --idle;
1831 } 1959 }
1832 1960
1962 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 2090 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1963 case EIO_MTOUCH: req->result = eio__mtouch (req); break; 2091 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1964 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 2092 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1965 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 2093 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1966 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 2094 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
2095 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
1967 2096
1968 case EIO_READDIR: eio__scandir (req, self); break; 2097 case EIO_READDIR: eio__scandir (req, self); break;
1969 2098
1970 case EIO_BUSY: 2099 case EIO_BUSY:
1971#ifdef _WIN32 2100#ifdef _WIN32
2071eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) 2200eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
2072{ 2201{
2073 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; 2202 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
2074} 2203}
2075 2204
2205eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data)
2206{
2207 REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND;
2208}
2209
2076eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) 2210eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
2077{ 2211{
2078 REQ (EIO_FDATASYNC); req->int1 = fd; SEND; 2212 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
2079} 2213}
2080 2214
2121eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data) 2255eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
2122{ 2256{
2123 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; 2257 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
2124} 2258}
2125 2259
2126eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2260eio_req *eio_fchown (int fd, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2127{ 2261{
2128 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2262 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2129} 2263}
2130 2264
2131eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) 2265eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
2151eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data) 2285eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
2152{ 2286{
2153 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; 2287 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
2154} 2288}
2155 2289
2156eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2290eio_req *eio_chown (const char *path, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2157{ 2291{
2158 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2292 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2159} 2293}
2160 2294
2161eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) 2295eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
2308} 2442}
2309 2443
2310/*****************************************************************************/ 2444/*****************************************************************************/
2311/* misc garbage */ 2445/* misc garbage */
2312 2446
2313ssize_t 2447eio_ssize_t
2314eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2448eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2315{ 2449{
2316 etp_worker wrk; 2450 etp_worker wrk;
2317 ssize_t ret; 2451 eio_ssize_t ret;
2318 2452
2319 wrk.dbuf = 0; 2453 wrk.dbuf = 0;
2320 2454
2321 ret = eio__sendfile (ofd, ifd, offset, count, &wrk); 2455 ret = eio__sendfile (ofd, ifd, offset, count, &wrk);
2322 2456

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines