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

Comparing libeio/eio.c (file contents):
Revision 1.72 by root, Fri Jun 10 12:45:20 2011 UTC vs.
Revision 1.98 by root, Sun Jul 24 05:53:34 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 */
65/* intptr_t only comes form 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
69#ifndef ECANCELED
70# define ECANCELED EDOM
71#endif
72#ifndef ELOOP
73# define ELOOP EDOM
74#endif
75
76#if !defined(ENOTSOCK) && defined(WSAENOTSOCK)
77# define ENOTSOCK WSAENOTSOCK
78#endif
79
80static void eio_destroy (eio_req *req);
81
70#ifndef EIO_FINISH 82#ifndef EIO_FINISH
71# 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
72#endif 84#endif
73 85
74#ifndef EIO_DESTROY 86#ifndef EIO_DESTROY
77 89
78#ifndef EIO_FEED 90#ifndef EIO_FEED
79# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0) 91# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0)
80#endif 92#endif
81 93
94#ifndef EIO_FD_TO_WIN32_HANDLE
95# define EIO_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
96#endif
97#ifndef EIO_WIN32_HANDLE_TO_FD
98# define EIO_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
99#endif
100
101#define EIO_ERRNO(errval,retval) ((errno = errval), retval)
102
103#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1)
104
82#ifdef _WIN32 105#ifdef _WIN32
83 106
84 /*doh*/ 107 #undef PAGESIZE
108 #define PAGESIZE 4096 /* GetSystemInfo? */
109
110 #ifdef EIO_STRUCT_STATI64
111 #define stat(path,buf) _stati64 (path,buf)
112 #define fstat(fd,buf) _fstati64 (fd,buf)
113 #endif
114 #define lstat(path,buf) stat (path,buf)
115 #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1))
116 #define mkdir(path,mode) _mkdir (path)
117 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
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 ()
124 #define chown(path,uid,gid) EIO_ENOSYS ()
125 #define fchown(fd,uid,gid) EIO_ENOSYS ()
126 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
127 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
128 #define mknod(path,mode,dev) 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 ()
133
134 /* we could even stat and see if it exists */
135 static int
136 symlink (const char *old, const char *neu)
137 {
138 #if WINVER >= 0x0600
139 if (CreateSymbolicLink (neu, old, 1))
140 return 0;
141
142 if (CreateSymbolicLink (neu, old, 0))
143 return 0;
144 #endif
145
146 return EIO_ERRNO (ENOENT, -1);
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)
162
85#else 163#else
86 164
87# include <sys/time.h> 165 #include <sys/time.h>
88# include <sys/select.h> 166 #include <sys/select.h>
167 #include <sys/statvfs.h>
89# include <unistd.h> 168 #include <unistd.h>
169 #include <signal.h>
170 #include <dirent.h>
171
172 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
173 #include <sys/mman.h>
174 #endif
175
176 #define D_NAME(entp) entp->d_name
177
178 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
179 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
180 #define _DIRENT_HAVE_D_TYPE /* sigh */
181 #define D_INO(de) (de)->d_fileno
182 #define D_NAMLEN(de) (de)->d_namlen
183 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
184 #define D_INO(de) (de)->d_ino
185 #endif
186
187 #ifdef _D_EXACT_NAMLEN
188 #undef D_NAMLEN
189 #define D_NAMLEN(de) _D_EXACT_NAMLEN (de)
190 #endif
191
192 #ifdef _DIRENT_HAVE_D_TYPE
193 #define D_TYPE(de) (de)->d_type
194 #endif
195
196 #ifndef EIO_STRUCT_DIRENT
197 #define EIO_STRUCT_DIRENT struct dirent
198 #endif
199
200#endif
201
202#if HAVE_UTIMES
90# include <utime.h> 203# include <utime.h>
91# include <signal.h>
92# include <dirent.h>
93
94#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
95# include <sys/mman.h>
96#endif
97
98/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
99# if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
100# define _DIRENT_HAVE_D_TYPE /* sigh */
101# define D_INO(de) (de)->d_fileno
102# define D_NAMLEN(de) (de)->d_namlen
103# elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
104# define D_INO(de) (de)->d_ino
105# endif 204#endif
106 205
107#ifdef _D_EXACT_NAMLEN 206#if HAVE_SYS_SYSCALL_H
108# undef D_NAMLEN 207# include <sys/syscall.h>
109# define D_NAMLEN(de) _D_EXACT_NAMLEN (de)
110#endif
111
112# ifdef _DIRENT_HAVE_D_TYPE
113# define D_TYPE(de) (de)->d_type
114# endif 208#endif
115 209
116# ifndef EIO_STRUCT_DIRENT 210#if HAVE_SYS_PRCTL_H
117# define EIO_STRUCT_DIRENT struct dirent 211# include <sys/prctl.h>
118# endif
119
120#endif 212#endif
121 213
122#if HAVE_SENDFILE 214#if HAVE_SENDFILE
123# if __linux 215# if __linux
124# include <sys/sendfile.h> 216# include <sys/sendfile.h>
139#endif 231#endif
140#ifndef D_INO 232#ifndef D_INO
141# define D_INO(de) 0 233# define D_INO(de) 0
142#endif 234#endif
143#ifndef D_NAMLEN 235#ifndef D_NAMLEN
144# define D_NAMLEN(de) strlen ((de)->d_name) 236# define D_NAMLEN(entp) strlen (D_NAME (entp))
145#endif 237#endif
146 238
147/* used for struct dirent, AIX doesn't provide it */ 239/* used for struct dirent, AIX doesn't provide it */
148#ifndef NAME_MAX 240#ifndef NAME_MAX
149# define NAME_MAX 4096 241# define NAME_MAX 4096
156 248
157/* buffer size for various temporary buffers */ 249/* buffer size for various temporary buffers */
158#define EIO_BUFSIZE 65536 250#define EIO_BUFSIZE 65536
159 251
160#define dBUF \ 252#define dBUF \
161 char *eio_buf; \
162 ETP_WORKER_LOCK (self); \
163 self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \ 253 char *eio_buf = malloc (EIO_BUFSIZE); \
164 ETP_WORKER_UNLOCK (self); \
165 errno = ENOMEM; \ 254 errno = ENOMEM; \
166 if (!eio_buf) \ 255 if (!eio_buf) \
167 return -1; 256 return -1
257
258#define FUBd \
259 free (eio_buf)
168 260
169#define EIO_TICKS ((1000000 + 1023) >> 10) 261#define EIO_TICKS ((1000000 + 1023) >> 10)
170 262
171#define ETP_PRI_MIN EIO_PRI_MIN 263#define ETP_PRI_MIN EIO_PRI_MIN
172#define ETP_PRI_MAX EIO_PRI_MAX 264#define ETP_PRI_MAX EIO_PRI_MAX
178static int eio_finish (eio_req *req); 270static int eio_finish (eio_req *req);
179#define ETP_FINISH(req) eio_finish (req) 271#define ETP_FINISH(req) eio_finish (req)
180static void eio_execute (struct etp_worker *self, eio_req *req); 272static void eio_execute (struct etp_worker *self, eio_req *req);
181#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 273#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req)
182 274
183#define ETP_WORKER_CLEAR(req) \
184 if (wrk->dbuf) \
185 { \
186 free (wrk->dbuf); \
187 wrk->dbuf = 0; \
188 } \
189 \
190 if (wrk->dirp) \
191 { \
192 closedir (wrk->dirp); \
193 wrk->dirp = 0; \
194 }
195
196#define ETP_WORKER_COMMON \
197 void *dbuf; \
198 DIR *dirp;
199
200/*****************************************************************************/ 275/*****************************************************************************/
201 276
202#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 277#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
203 278
204/* calculate time difference in ~1/EIO_TICKS of a second */ 279/* calculate time difference in ~1/EIO_TICKS of a second */
205ECB_INLINE int 280ecb_inline int
206tvdiff (struct timeval *tv1, struct timeval *tv2) 281tvdiff (struct timeval *tv1, struct timeval *tv2)
207{ 282{
208 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS 283 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
209 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 284 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
210} 285}
232/* 307/*
233 * make our pread/pwrite emulation safe against themselves, but not against 308 * make our pread/pwrite emulation safe against themselves, but not against
234 * normal read/write by using a mutex. slows down execution a lot, 309 * normal read/write by using a mutex. slows down execution a lot,
235 * but that's your problem, not mine. 310 * but that's your problem, not mine.
236 */ 311 */
237static xmutex_t preadwritelock = X_MUTEX_INIT; 312static xmutex_t preadwritelock;
238#endif 313#endif
239 314
240typedef struct etp_worker 315typedef struct etp_worker
241{ 316{
242 /* locked by wrklock */ 317 /* locked by wrklock */
245 xthread_t tid; 320 xthread_t tid;
246 321
247 /* locked by reslock, reqlock or wrklock */ 322 /* locked by reslock, reqlock or wrklock */
248 ETP_REQ *req; /* currently processed request */ 323 ETP_REQ *req; /* currently processed request */
249 324
325#ifdef ETP_WORKER_COMMON
250 ETP_WORKER_COMMON 326 ETP_WORKER_COMMON
327#endif
251} etp_worker; 328} etp_worker;
252 329
253static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */ 330static etp_worker wrk_first; /* NOT etp */
254 331
255#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 332#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
256#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 333#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
257 334
258/* worker threads management */ 335/* worker threads management */
259 336
260static void ecb_cold 337static void ecb_cold
261etp_worker_clear (etp_worker *wrk) 338etp_worker_clear (etp_worker *wrk)
262{ 339{
263 ETP_WORKER_CLEAR (wrk);
264} 340}
265 341
266static void ecb_cold 342static void ecb_cold
267etp_worker_free (etp_worker *wrk) 343etp_worker_free (etp_worker *wrk)
268{ 344{
329} etp_reqq; 405} etp_reqq;
330 406
331static etp_reqq req_queue; 407static etp_reqq req_queue;
332static etp_reqq res_queue; 408static etp_reqq res_queue;
333 409
410static void ecb_noinline ecb_cold
411reqq_init (etp_reqq *q)
412{
413 int pri;
414
415 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
416 q->qs[pri] = q->qe[pri] = 0;
417
418 q->size = 0;
419}
420
334static int ecb_noinline 421static int ecb_noinline
335reqq_push (etp_reqq *q, ETP_REQ *req) 422reqq_push (etp_reqq *q, ETP_REQ *req)
336{ 423{
337 int pri = req->pri; 424 int pri = req->pri;
338 req->next = 0; 425 req->next = 0;
372 } 459 }
373 460
374 abort (); 461 abort ();
375} 462}
376 463
377static void ecb_cold 464static int ecb_cold
378etp_thread_init (void) 465etp_init (void (*want_poll)(void), void (*done_poll)(void))
379{ 466{
380 X_MUTEX_CREATE (wrklock); 467 X_MUTEX_CREATE (wrklock);
381 X_MUTEX_CREATE (reslock); 468 X_MUTEX_CREATE (reslock);
382 X_MUTEX_CREATE (reqlock); 469 X_MUTEX_CREATE (reqlock);
383 X_COND_CREATE (reqwait); 470 X_COND_CREATE (reqwait);
384}
385 471
386static void ecb_cold 472 reqq_init (&req_queue);
387etp_atfork_prepare (void) 473 reqq_init (&res_queue);
388{
389 X_LOCK (wrklock);
390 X_LOCK (reqlock);
391 X_LOCK (reslock);
392#if !HAVE_PREADWRITE
393 X_LOCK (preadwritelock);
394#endif
395}
396 474
397static void ecb_cold 475 wrk_first.next =
398etp_atfork_parent (void) 476 wrk_first.prev = &wrk_first;
399{
400#if !HAVE_PREADWRITE
401 X_UNLOCK (preadwritelock);
402#endif
403 X_UNLOCK (reslock);
404 X_UNLOCK (reqlock);
405 X_UNLOCK (wrklock);
406}
407
408static void ecb_cold
409etp_atfork_child (void)
410{
411 ETP_REQ *prv;
412
413 while ((prv = reqq_shift (&req_queue)))
414 ETP_DESTROY (prv);
415
416 while ((prv = reqq_shift (&res_queue)))
417 ETP_DESTROY (prv);
418
419 while (wrk_first.next != &wrk_first)
420 {
421 etp_worker *wrk = wrk_first.next;
422
423 if (wrk->req)
424 ETP_DESTROY (wrk->req);
425
426 etp_worker_clear (wrk);
427 etp_worker_free (wrk);
428 }
429 477
430 started = 0; 478 started = 0;
431 idle = 0; 479 idle = 0;
432 nreqs = 0; 480 nreqs = 0;
433 nready = 0; 481 nready = 0;
434 npending = 0; 482 npending = 0;
435
436 etp_thread_init ();
437}
438
439static void ecb_cold
440etp_once_init (void)
441{
442 etp_thread_init ();
443 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
444}
445
446static int ecb_cold
447etp_init (void (*want_poll)(void), void (*done_poll)(void))
448{
449 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
450
451 pthread_once (&doinit, etp_once_init);
452 483
453 want_poll_cb = want_poll; 484 want_poll_cb = want_poll;
454 done_poll_cb = done_poll; 485 done_poll_cb = done_poll;
455 486
456 return 0; 487 return 0;
583} 614}
584 615
585static void 616static void
586etp_cancel (ETP_REQ *req) 617etp_cancel (ETP_REQ *req)
587{ 618{
588 X_LOCK (wrklock); 619 req->cancelled = 1;
589 req->flags |= EIO_FLAG_CANCELLED;
590 X_UNLOCK (wrklock);
591 620
592 eio_grp_cancel (req); 621 eio_grp_cancel (req);
593} 622}
594 623
595static void 624static void
711 return eio_finish (grp); 740 return eio_finish (grp);
712 else 741 else
713 return 0; 742 return 0;
714} 743}
715 744
716void 745static void
717eio_destroy (eio_req *req) 746eio_destroy (eio_req *req)
718{ 747{
719 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); 748 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
720 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); 749 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
721 750
739 if (grp->grp_first == req) 768 if (grp->grp_first == req)
740 grp->grp_first = req->grp_next; 769 grp->grp_first = req->grp_next;
741 770
742 res2 = grp_dec (grp); 771 res2 = grp_dec (grp);
743 772
744 if (!res && res2) 773 if (!res)
745 res = res2; 774 res = res2;
746 } 775 }
747 776
748 eio_destroy (req); 777 eio_destroy (req);
749 778
841# undef pread 870# undef pread
842# undef pwrite 871# undef pwrite
843# define pread eio__pread 872# define pread eio__pread
844# define pwrite eio__pwrite 873# define pwrite eio__pwrite
845 874
846static ssize_t 875static eio_ssize_t
847eio__pread (int fd, void *buf, size_t count, off_t offset) 876eio__pread (int fd, void *buf, size_t count, off_t offset)
848{ 877{
849 ssize_t res; 878 eio_ssize_t res;
850 off_t ooffset; 879 off_t ooffset;
851 880
852 X_LOCK (preadwritelock); 881 X_LOCK (preadwritelock);
853 ooffset = lseek (fd, 0, SEEK_CUR); 882 ooffset = lseek (fd, 0, SEEK_CUR);
854 lseek (fd, offset, SEEK_SET); 883 lseek (fd, offset, SEEK_SET);
857 X_UNLOCK (preadwritelock); 886 X_UNLOCK (preadwritelock);
858 887
859 return res; 888 return res;
860} 889}
861 890
862static ssize_t 891static eio_ssize_t
863eio__pwrite (int fd, void *buf, size_t count, off_t offset) 892eio__pwrite (int fd, void *buf, size_t count, off_t offset)
864{ 893{
865 ssize_t res; 894 eio_ssize_t res;
866 off_t ooffset; 895 off_t ooffset;
867 896
868 X_LOCK (preadwritelock); 897 X_LOCK (preadwritelock);
869 ooffset = lseek (fd, 0, SEEK_CUR); 898 ooffset = lseek (fd, 0, SEEK_CUR);
870 lseek (fd, offset, SEEK_SET); 899 lseek (fd, offset, SEEK_SET);
915 944
916#if !HAVE_FDATASYNC 945#if !HAVE_FDATASYNC
917# undef fdatasync 946# undef fdatasync
918# define fdatasync(fd) fsync (fd) 947# define fdatasync(fd) fsync (fd)
919#endif 948#endif
949
950static int
951eio__syncfs (int fd)
952{
953 int res;
954
955#if HAVE_SYS_SYNCFS
956 res = (int)syscall (__NR_syncfs, (int)(fd));
957#else
958 res = -1;
959 errno = ENOSYS;
960#endif
961
962 if (res < 0 && errno == ENOSYS && fd >= 0)
963 sync ();
964
965 return res;
966}
920 967
921/* sync_file_range always needs emulation */ 968/* sync_file_range always needs emulation */
922static int 969static int
923eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) 970eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
924{ 971{
944 /* 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
945 * call fdatasync, as that matches the expectation of its users best */ 992 * call fdatasync, as that matches the expectation of its users best */
946 return fdatasync (fd); 993 return fdatasync (fd);
947} 994}
948 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
949#if !HAVE_READAHEAD 1007#if !HAVE_READAHEAD
950# undef readahead 1008# undef readahead
951# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 1009# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
952 1010
953static ssize_t 1011static eio_ssize_t
954eio__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)
955{ 1013{
956 size_t todo = count; 1014 size_t todo = count;
957 dBUF; 1015 dBUF;
958 1016
963 pread (fd, eio_buf, len, offset); 1021 pread (fd, eio_buf, len, offset);
964 offset += len; 1022 offset += len;
965 todo -= len; 1023 todo -= len;
966 } 1024 }
967 1025
1026 FUBd;
1027
968 errno = 0; 1028 errno = 0;
969 return count; 1029 return count;
970} 1030}
971 1031
972#endif 1032#endif
973 1033
974/* sendfile always needs emulation */ 1034/* sendfile always needs emulation */
975static ssize_t 1035static eio_ssize_t
976eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 1036eio__sendfile (int ofd, int ifd, off_t offset, size_t count)
977{ 1037{
978 ssize_t written = 0; 1038 eio_ssize_t written = 0;
979 ssize_t res; 1039 eio_ssize_t res;
980 1040
981 if (!count) 1041 if (!count)
982 return 0; 1042 return 0;
983 1043
984 for (;;) 1044 for (;;)
985 { 1045 {
1046#ifdef __APPLE__
1047# undef HAVE_SENDFILE /* broken, as everything on os x */
1048#endif
986#if HAVE_SENDFILE 1049#if HAVE_SENDFILE
987# if __linux 1050# if __linux
988 off_t soffset = offset; 1051 off_t soffset = offset;
989 res = sendfile (ofd, ifd, &soffset, count); 1052 res = sendfile (ofd, ifd, &soffset, count);
990 1053
1033 if (res < 0 && sbytes) 1096 if (res < 0 && sbytes)
1034 res = sbytes; 1097 res = sbytes;
1035 1098
1036# endif 1099# endif
1037 1100
1038#elif defined (_WIN32) 1101#elif defined (_WIN32) && 0
1039 /* does not work, just for documentation of what would need to be done */ 1102 /* does not work, just for documentation of what would need to be done */
1040 /* actually, cannot be done like this, as TransmitFile changes the file offset, */ 1103 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1041 /* libeio guarantees that the file offset does not change, and windows */ 1104 /* libeio guarantees that the file offset does not change, and windows */
1042 /* has no way to get an independent handle to the same file description */ 1105 /* has no way to get an independent handle to the same file description */
1043 HANDLE h = TO_SOCKET (ifd); 1106 HANDLE h = TO_SOCKET (ifd);
1076 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1139 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1077 /* BSDs */ 1140 /* BSDs */
1078#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1141#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1079 || errno == ENOTSUP 1142 || errno == ENOTSUP
1080#endif 1143#endif
1144#ifdef EOPNOTSUPP /* windows */
1081 || errno == EOPNOTSUPP /* BSDs */ 1145 || errno == EOPNOTSUPP /* BSDs */
1146#endif
1082#if __solaris 1147#if __solaris
1083 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 1148 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1084#endif 1149#endif
1085 ) 1150 )
1086 ) 1151 )
1090 1155
1091 res = 0; 1156 res = 0;
1092 1157
1093 while (count) 1158 while (count)
1094 { 1159 {
1095 ssize_t cnt; 1160 eio_ssize_t cnt;
1096 1161
1097 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset); 1162 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset);
1098 1163
1099 if (cnt <= 0) 1164 if (cnt <= 0)
1100 { 1165 {
1112 1177
1113 offset += cnt; 1178 offset += cnt;
1114 res += cnt; 1179 res += cnt;
1115 count -= cnt; 1180 count -= cnt;
1116 } 1181 }
1182
1183 FUBd;
1117 } 1184 }
1118 1185
1119 return res; 1186 return res;
1187}
1188
1189#ifdef PAGESIZE
1190# define eio_pagesize() PAGESIZE
1191#else
1192static intptr_t
1193eio_pagesize (void)
1194{
1195 static intptr_t page;
1196
1197 if (!page)
1198 page = sysconf (_SC_PAGESIZE);
1199
1200 return page;
1201}
1202#endif
1203
1204static void
1205eio_page_align (void **addr, size_t *length)
1206{
1207 intptr_t mask = eio_pagesize () - 1;
1208
1209 /* round down addr */
1210 intptr_t adj = mask & (intptr_t)*addr;
1211
1212 *addr = (void *)((intptr_t)*addr - adj);
1213 *length += adj;
1214
1215 /* round up length */
1216 *length = (*length + mask) & ~mask;
1217}
1218
1219#if !_POSIX_MEMLOCK
1220# define eio__mlockall(a) EIO_ENOSYS ()
1221#else
1222
1223static int
1224eio__mlockall (int flags)
1225{
1226 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1227 extern int mallopt (int, int);
1228 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1229 #endif
1230
1231 if (EIO_MCL_CURRENT != MCL_CURRENT
1232 || EIO_MCL_FUTURE != MCL_FUTURE)
1233 {
1234 flags = 0
1235 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1236 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1237 }
1238
1239 return mlockall (flags);
1240}
1241#endif
1242
1243#if !_POSIX_MEMLOCK_RANGE
1244# define eio__mlock(a,b) EIO_ENOSYS ()
1245#else
1246
1247static int
1248eio__mlock (void *addr, size_t length)
1249{
1250 eio_page_align (&addr, &length);
1251
1252 return mlock (addr, length);
1253}
1254
1255#endif
1256
1257#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1258# define eio__msync(a,b,c) EIO_ENOSYS ()
1259#else
1260
1261static int
1262eio__msync (void *mem, size_t len, int flags)
1263{
1264 eio_page_align (&mem, &len);
1265
1266 if (EIO_MS_ASYNC != MS_SYNC
1267 || EIO_MS_INVALIDATE != MS_INVALIDATE
1268 || EIO_MS_SYNC != MS_SYNC)
1269 {
1270 flags = 0
1271 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1272 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1273 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1274 }
1275
1276 return msync (mem, len, flags);
1277}
1278
1279#endif
1280
1281static int
1282eio__mtouch (eio_req *req)
1283{
1284 void *mem = req->ptr2;
1285 size_t len = req->size;
1286 int flags = req->int1;
1287
1288 eio_page_align (&mem, &len);
1289
1290 {
1291 intptr_t addr = (intptr_t)mem;
1292 intptr_t end = addr + len;
1293 intptr_t page = eio_pagesize ();
1294
1295 if (addr < end)
1296 if (flags & EIO_MT_MODIFY) /* modify */
1297 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1298 else
1299 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1300 }
1301
1302 return 0;
1303}
1304
1305/*****************************************************************************/
1306/* requests implemented outside eio_execute, because they are so large */
1307
1308static void
1309eio__realpath (eio_req *req, etp_worker *self)
1310{
1311 char *rel = req->ptr1;
1312 char *res;
1313 char *tmp1, *tmp2;
1314#if SYMLOOP_MAX > 32
1315 int symlinks = SYMLOOP_MAX;
1316#else
1317 int symlinks = 32;
1318#endif
1319
1320 req->result = -1;
1321
1322 errno = EINVAL;
1323 if (!rel)
1324 return;
1325
1326 errno = ENOENT;
1327 if (!*rel)
1328 return;
1329
1330 if (!req->ptr2)
1331 {
1332 X_LOCK (wrklock);
1333 req->flags |= EIO_FLAG_PTR2_FREE;
1334 X_UNLOCK (wrklock);
1335 req->ptr2 = malloc (PATH_MAX * 3);
1336
1337 errno = ENOMEM;
1338 if (!req->ptr2)
1339 return;
1340 }
1341
1342 res = req->ptr2;
1343 tmp1 = res + PATH_MAX;
1344 tmp2 = tmp1 + PATH_MAX;
1345
1346#if 0 /* disabled, the musl way to do things is just too racy */
1347#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1348 /* on linux we may be able to ask the kernel */
1349 {
1350 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1351
1352 if (fd >= 0)
1353 {
1354 sprintf (tmp1, "/proc/self/fd/%d", fd);
1355 req->result = readlink (tmp1, res, PATH_MAX);
1356 close (fd);
1357
1358 /* here we should probably stat the open file and the disk file, to make sure they still match */
1359
1360 if (req->result > 0)
1361 goto done;
1362 }
1363 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1364 return;
1365 }
1366#endif
1367#endif
1368
1369 if (*rel != '/')
1370 {
1371 if (!getcwd (res, PATH_MAX))
1372 return;
1373
1374 if (res [1]) /* only use if not / */
1375 res += strlen (res);
1376 }
1377
1378 while (*rel)
1379 {
1380 eio_ssize_t len, linklen;
1381 char *beg = rel;
1382
1383 while (*rel && *rel != '/')
1384 ++rel;
1385
1386 len = rel - beg;
1387
1388 if (!len) /* skip slashes */
1389 {
1390 ++rel;
1391 continue;
1392 }
1393
1394 if (beg [0] == '.')
1395 {
1396 if (len == 1)
1397 continue; /* . - nop */
1398
1399 if (beg [1] == '.' && len == 2)
1400 {
1401 /* .. - back up one component, if possible */
1402
1403 while (res != req->ptr2)
1404 if (*--res == '/')
1405 break;
1406
1407 continue;
1408 }
1409 }
1410
1411 errno = ENAMETOOLONG;
1412 if (res + 1 + len + 1 >= tmp1)
1413 return;
1414
1415 /* copy one component */
1416 *res = '/';
1417 memcpy (res + 1, beg, len);
1418
1419 /* zero-terminate, for readlink */
1420 res [len + 1] = 0;
1421
1422 /* now check if it's a symlink */
1423 linklen = readlink (req->ptr2, tmp1, PATH_MAX);
1424
1425 if (linklen < 0)
1426 {
1427 if (errno != EINVAL)
1428 return;
1429
1430 /* it's a normal directory. hopefully */
1431 res += len + 1;
1432 }
1433 else
1434 {
1435 /* yay, it was a symlink - build new path in tmp2 */
1436 int rellen = strlen (rel);
1437
1438 errno = ENAMETOOLONG;
1439 if (linklen + 1 + rellen >= PATH_MAX)
1440 return;
1441
1442 errno = ELOOP;
1443 if (!--symlinks)
1444 return;
1445
1446 if (*tmp1 == '/')
1447 res = req->ptr2; /* symlink resolves to an absolute path */
1448
1449 /* we need to be careful, as rel might point into tmp2 already */
1450 memmove (tmp2 + linklen + 1, rel, rellen + 1);
1451 tmp2 [linklen] = '/';
1452 memcpy (tmp2, tmp1, linklen);
1453
1454 rel = tmp2;
1455 }
1456 }
1457
1458 /* special case for the lone root path */
1459 if (res == req->ptr2)
1460 *res++ = '/';
1461
1462 req->result = res - (char *)req->ptr2;
1463
1464done:
1465 req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */
1120} 1466}
1121 1467
1122static signed char 1468static signed char
1123eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1469eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1124{ 1470{
1132 1478
1133#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1479#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1134#define EIO_SORT_FAST 60 /* when to only use insertion sort */ 1480#define EIO_SORT_FAST 60 /* when to only use insertion sort */
1135 1481
1136static void 1482static void
1137eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1483eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1138{ 1484{
1139 unsigned char bits [9 + sizeof (ino_t) * 8]; 1485 unsigned char bits [9 + sizeof (eio_ino_t) * 8];
1140 unsigned char *bit = bits; 1486 unsigned char *bit = bits;
1141 1487
1142 assert (CHAR_BIT == 8); 1488 assert (CHAR_BIT == 8);
1143 assert (sizeof (eio_dirent) * 8 < 256); 1489 assert (sizeof (eio_dirent) * 8 < 256);
1144 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ 1490 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1146 1492
1147 if (size <= EIO_SORT_FAST) 1493 if (size <= EIO_SORT_FAST)
1148 return; 1494 return;
1149 1495
1150 /* first prepare an array of bits to test in our radix sort */ 1496 /* first prepare an array of bits to test in our radix sort */
1151 /* try to take endianness into account, as well as differences in ino_t sizes */ 1497 /* try to take endianness into account, as well as differences in eio_ino_t sizes */
1152 /* inode_bits must contain all inodes ORed together */ 1498 /* inode_bits must contain all inodes ORed together */
1153 /* which is used to skip bits that are 0 everywhere, which is very common */ 1499 /* which is used to skip bits that are 0 everywhere, which is very common */
1154 { 1500 {
1155 ino_t endianness; 1501 eio_ino_t endianness;
1156 int i, j; 1502 int i, j;
1157 1503
1158 /* we store the byte offset of byte n into byte n of "endianness" */ 1504 /* we store the byte offset of byte n into byte n of "endianness" */
1159 for (i = 0; i < sizeof (ino_t); ++i) 1505 for (i = 0; i < sizeof (eio_ino_t); ++i)
1160 ((unsigned char *)&endianness)[i] = i; 1506 ((unsigned char *)&endianness)[i] = i;
1161 1507
1162 *bit++ = 0; 1508 *bit++ = 0;
1163 1509
1164 for (i = 0; i < sizeof (ino_t); ++i) 1510 for (i = 0; i < sizeof (eio_ino_t); ++i)
1165 { 1511 {
1166 /* shifting off the byte offsets out of "endianness" */ 1512 /* shifting off the byte offsets out of "endianness" */
1167 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8; 1513 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8;
1168 endianness >>= 8; 1514 endianness >>= 8;
1169 1515
1170 for (j = 0; j < 8; ++j) 1516 for (j = 0; j < 8; ++j)
1171 if (inode_bits & (((ino_t)1) << (i * 8 + j))) 1517 if (inode_bits & (((eio_ino_t)1) << (i * 8 + j)))
1172 *bit++ = offs + j; 1518 *bit++ = offs + j;
1173 } 1519 }
1174 1520
1175 for (j = 0; j < 8; ++j) 1521 for (j = 0; j < 8; ++j)
1176 if (score_bits & (1 << j)) 1522 if (score_bits & (1 << j))
1177 *bit++ = offsetof (eio_dirent, score) * 8 + j; 1523 *bit++ = offsetof (eio_dirent, score) * 8 + j;
1178 } 1524 }
1179 1525
1180 /* now actually do the sorting (a variant of MSD radix sort) */ 1526 /* now actually do the sorting (a variant of MSD radix sort) */
1181 { 1527 {
1182 eio_dirent *base_stk [9 + sizeof (ino_t) * 8], *base; 1528 eio_dirent *base_stk [9 + sizeof (eio_ino_t) * 8], *base;
1183 eio_dirent *end_stk [9 + sizeof (ino_t) * 8], *end; 1529 eio_dirent *end_stk [9 + sizeof (eio_ino_t) * 8], *end;
1184 unsigned char *bit_stk [9 + sizeof (ino_t) * 8]; 1530 unsigned char *bit_stk [9 + sizeof (eio_ino_t) * 8];
1185 int stk_idx = 0; 1531 int stk_idx = 0;
1186 1532
1187 base_stk [stk_idx] = dents; 1533 base_stk [stk_idx] = dents;
1188 end_stk [stk_idx] = dents + size; 1534 end_stk [stk_idx] = dents + size;
1189 bit_stk [stk_idx] = bit - 1; 1535 bit_stk [stk_idx] = bit - 1;
1268 } 1614 }
1269 } 1615 }
1270} 1616}
1271 1617
1272static void 1618static void
1273eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1619eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1274{ 1620{
1275 if (size <= 1) 1621 if (size <= 1)
1276 return; /* our insertion sort relies on size > 0 */ 1622 return; /* our insertion sort relies on size > 0 */
1277 1623
1278 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */ 1624 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */
1286 1632
1287/* read a full directory */ 1633/* read a full directory */
1288static void 1634static void
1289eio__scandir (eio_req *req, etp_worker *self) 1635eio__scandir (eio_req *req, etp_worker *self)
1290{ 1636{
1291 DIR *dirp;
1292 EIO_STRUCT_DIRENT *entp;
1293 char *name, *names; 1637 char *name, *names;
1294 int namesalloc = 4096; 1638 int namesalloc = 4096 - sizeof (void *) * 4;
1295 int namesoffs = 0; 1639 int namesoffs = 0;
1296 int flags = req->int1; 1640 int flags = req->int1;
1297 eio_dirent *dents = 0; 1641 eio_dirent *dents = 0;
1298 int dentalloc = 128; 1642 int dentalloc = 128;
1299 int dentoffs = 0; 1643 int dentoffs = 0;
1300 ino_t inode_bits = 0; 1644 eio_ino_t inode_bits = 0;
1645#ifdef _WIN32
1646 HANDLE dirp;
1647 WIN32_FIND_DATA entp;
1648#else
1649 DIR *dirp;
1650 EIO_STRUCT_DIRENT *entp;
1651#endif
1301 1652
1302 req->result = -1; 1653 req->result = -1;
1303 1654
1304 if (!(flags & EIO_READDIR_DENTS)) 1655 if (!(flags & EIO_READDIR_DENTS))
1305 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1656 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1306 1657
1307 X_LOCK (wrklock); 1658#ifdef _WIN32
1308 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1659 {
1660 int len = strlen ((const char *)req->ptr1);
1661 char *path = malloc (MAX_PATH);
1662 const char *fmt;
1663
1664 if (!len)
1665 fmt = "./*";
1666 else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\')
1667 fmt = "%s*";
1668 else
1669 fmt = "%s/*";
1670
1671 _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1);
1672 dirp = FindFirstFile (path, &entp);
1673 free (path);
1674
1675 if (dirp == INVALID_HANDLE_VALUE)
1676 {
1677 dirp = 0;
1678
1679 switch (GetLastError ())
1680 {
1681 case ERROR_FILE_NOT_FOUND:
1682 req->result = 0;
1683 break;
1684
1685 case ERROR_INVALID_NAME:
1686 case ERROR_PATH_NOT_FOUND:
1687 case ERROR_NO_MORE_FILES:
1688 errno = ENOENT;
1689 break;
1690
1691 case ERROR_NOT_ENOUGH_MEMORY:
1692 errno = ENOMEM;
1693 break;
1694
1695 default:
1696 errno = EINVAL;
1697 break;
1698 }
1699 }
1700 }
1701#else
1309 self->dirp = dirp = opendir (req->ptr1); 1702 dirp = opendir (req->ptr1);
1703#endif
1704
1705 if (req->flags & EIO_FLAG_PTR1_FREE)
1706 free (req->ptr1);
1310 1707
1311 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1708 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1312 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1709 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1313 req->ptr2 = names = malloc (namesalloc); 1710 req->ptr2 = names = malloc (namesalloc);
1314 X_UNLOCK (wrklock);
1315 1711
1316 if (dirp && names && (!flags || dents)) 1712 if (dirp && names && (!flags || dents))
1317 for (;;) 1713 for (;;)
1318 { 1714 {
1715 int done;
1716
1717#ifdef _WIN32
1718 done = !dirp;
1719#else
1319 errno = 0; 1720 errno = 0;
1320 entp = readdir (dirp); 1721 entp = readdir (dirp);
1722 done = !entp;
1723#endif
1321 1724
1322 if (!entp) 1725 if (done)
1323 { 1726 {
1727#ifndef _WIN32
1728 int old_errno = errno;
1729 closedir (dirp);
1730 errno = old_errno;
1731
1324 if (errno) 1732 if (errno)
1325 break; 1733 break;
1734#endif
1326 1735
1327 /* sort etc. */ 1736 /* sort etc. */
1328 req->int1 = flags; 1737 req->int1 = flags;
1329 req->result = dentoffs; 1738 req->result = dentoffs;
1330 1739
1359 1768
1360 break; 1769 break;
1361 } 1770 }
1362 1771
1363 /* now add the entry to our list(s) */ 1772 /* now add the entry to our list(s) */
1364 name = entp->d_name; 1773 name = D_NAME (entp);
1365 1774
1366 /* skip . and .. entries */ 1775 /* skip . and .. entries */
1367 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1776 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1368 { 1777 {
1369 int len = D_NAMLEN (entp) + 1; 1778 int len = D_NAMLEN (entp) + 1;
1370 1779
1371 while (ecb_expect_false (namesoffs + len > namesalloc)) 1780 while (ecb_expect_false (namesoffs + len > namesalloc))
1372 { 1781 {
1373 namesalloc *= 2; 1782 namesalloc *= 2;
1374 X_LOCK (wrklock);
1375 req->ptr2 = names = realloc (names, namesalloc); 1783 req->ptr2 = names = realloc (names, namesalloc);
1376 X_UNLOCK (wrklock);
1377 1784
1378 if (!names) 1785 if (!names)
1379 break; 1786 break;
1380 } 1787 }
1381 1788
1386 struct eio_dirent *ent; 1793 struct eio_dirent *ent;
1387 1794
1388 if (ecb_expect_false (dentoffs == dentalloc)) 1795 if (ecb_expect_false (dentoffs == dentalloc))
1389 { 1796 {
1390 dentalloc *= 2; 1797 dentalloc *= 2;
1391 X_LOCK (wrklock);
1392 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1798 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1393 X_UNLOCK (wrklock);
1394 1799
1395 if (!dents) 1800 if (!dents)
1396 break; 1801 break;
1397 } 1802 }
1398 1803
1478 if (EIO_CANCELLED (req)) 1883 if (EIO_CANCELLED (req))
1479 { 1884 {
1480 errno = ECANCELED; 1885 errno = ECANCELED;
1481 break; 1886 break;
1482 } 1887 }
1888
1889#ifdef _WIN32
1890 if (!FindNextFile (dirp, &entp))
1891 {
1892 FindClose (dirp);
1893 dirp = 0;
1894 }
1895#endif
1483 } 1896 }
1484}
1485
1486#ifdef PAGESIZE
1487# define eio_pagesize() PAGESIZE
1488#else
1489static intptr_t
1490eio_pagesize (void)
1491{
1492 static intptr_t page;
1493
1494 if (!page)
1495 page = sysconf (_SC_PAGESIZE);
1496
1497 return page;
1498}
1499#endif
1500
1501static void
1502eio_page_align (void **addr, size_t *length)
1503{
1504 intptr_t mask = eio_pagesize () - 1;
1505
1506 /* round down addr */
1507 intptr_t adj = mask & (intptr_t)*addr;
1508
1509 *addr = (void *)((intptr_t)*addr - adj);
1510 *length += adj;
1511
1512 /* round up length */
1513 *length = (*length + mask) & ~mask;
1514}
1515
1516#if !_POSIX_MEMLOCK
1517# define eio__mlockall(a) ((errno = ENOSYS), -1)
1518#else
1519
1520static int
1521eio__mlockall (int flags)
1522{
1523 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1524 extern int mallopt (int, int);
1525 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1526 #endif
1527
1528 if (EIO_MCL_CURRENT != MCL_CURRENT
1529 || EIO_MCL_FUTURE != MCL_FUTURE)
1530 {
1531 flags = 0
1532 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1533 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1534 }
1535
1536 return mlockall (flags);
1537}
1538#endif
1539
1540#if !_POSIX_MEMLOCK_RANGE
1541# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1542#else
1543
1544static int
1545eio__mlock (void *addr, size_t length)
1546{
1547 eio_page_align (&addr, &length);
1548
1549 return mlock (addr, length);
1550}
1551
1552#endif
1553
1554#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1555# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1556#else
1557
1558static int
1559eio__msync (void *mem, size_t len, int flags)
1560{
1561 eio_page_align (&mem, &len);
1562
1563 if (EIO_MS_ASYNC != MS_SYNC
1564 || EIO_MS_INVALIDATE != MS_INVALIDATE
1565 || EIO_MS_SYNC != MS_SYNC)
1566 {
1567 flags = 0
1568 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1569 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1570 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1571 }
1572
1573 return msync (mem, len, flags);
1574}
1575
1576#endif
1577
1578static int
1579eio__mtouch (eio_req *req)
1580{
1581 void *mem = req->ptr2;
1582 size_t len = req->size;
1583 int flags = req->int1;
1584
1585 eio_page_align (&mem, &len);
1586
1587 {
1588 intptr_t addr = (intptr_t)mem;
1589 intptr_t end = addr + len;
1590 intptr_t page = eio_pagesize ();
1591
1592 if (addr < end)
1593 if (flags & EIO_MT_MODIFY) /* modify */
1594 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1595 else
1596 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1597 }
1598
1599 return 0;
1600} 1897}
1601 1898
1602/*****************************************************************************/ 1899/*****************************************************************************/
1603 1900
1604#define ALLOC(len) \ 1901#define ALLOC(len) \
1620{ 1917{
1621 ETP_REQ *req; 1918 ETP_REQ *req;
1622 struct timespec ts; 1919 struct timespec ts;
1623 etp_worker *self = (etp_worker *)thr_arg; 1920 etp_worker *self = (etp_worker *)thr_arg;
1624 1921
1922#if HAVE_PRCTL_SET_NAME
1923 prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0);
1924#endif
1925
1625 /* try to distribute timeouts somewhat randomly */ 1926 /* try to distribute timeouts somewhat evenly */
1626 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1927 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1627 1928
1628 for (;;) 1929 for (;;)
1629 { 1930 {
1931 ts.tv_sec = 0;
1932
1630 X_LOCK (reqlock); 1933 X_LOCK (reqlock);
1631 1934
1632 for (;;) 1935 for (;;)
1633 { 1936 {
1634 self->req = req = reqq_shift (&req_queue); 1937 self->req = req = reqq_shift (&req_queue);
1635 1938
1636 if (req) 1939 if (req)
1637 break; 1940 break;
1638 1941
1942 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
1943 {
1944 X_UNLOCK (reqlock);
1945 X_LOCK (wrklock);
1946 --started;
1947 X_UNLOCK (wrklock);
1948 goto quit;
1949 }
1950
1639 ++idle; 1951 ++idle;
1640 1952
1641 ts.tv_sec = time (0) + idle_timeout; 1953 if (idle <= max_idle)
1642 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) 1954 /* we are allowed to idle, so do so without any timeout */
1955 X_COND_WAIT (reqwait, reqlock);
1956 else
1643 { 1957 {
1644 if (idle > max_idle) 1958 /* initialise timeout once */
1645 { 1959 if (!ts.tv_sec)
1646 --idle; 1960 ts.tv_sec = time (0) + idle_timeout;
1647 X_UNLOCK (reqlock);
1648 X_LOCK (wrklock);
1649 --started;
1650 X_UNLOCK (wrklock);
1651 goto quit;
1652 }
1653 1961
1654 /* we are allowed to idle, so do so without any timeout */
1655 X_COND_WAIT (reqwait, reqlock); 1962 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1963 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
1656 } 1964 }
1657 1965
1658 --idle; 1966 --idle;
1659 } 1967 }
1660 1968
1663 X_UNLOCK (reqlock); 1971 X_UNLOCK (reqlock);
1664 1972
1665 if (req->type < 0) 1973 if (req->type < 0)
1666 goto quit; 1974 goto quit;
1667 1975
1668 if (!EIO_CANCELLED (req))
1669 ETP_EXECUTE (self, req); 1976 ETP_EXECUTE (self, req);
1670 1977
1671 X_LOCK (reslock); 1978 X_LOCK (reslock);
1672 1979
1673 ++npending; 1980 ++npending;
1674 1981
1692/*****************************************************************************/ 1999/*****************************************************************************/
1693 2000
1694int ecb_cold 2001int ecb_cold
1695eio_init (void (*want_poll)(void), void (*done_poll)(void)) 2002eio_init (void (*want_poll)(void), void (*done_poll)(void))
1696{ 2003{
2004#if !HAVE_PREADWRITE
2005 X_MUTEX_CREATE (preadwritelock);
2006#endif
2007
1697 return etp_init (want_poll, done_poll); 2008 return etp_init (want_poll, done_poll);
1698} 2009}
1699 2010
1700ECB_INLINE void 2011ecb_inline void
1701eio_api_destroy (eio_req *req) 2012eio_api_destroy (eio_req *req)
1702{ 2013{
1703 free (req); 2014 free (req);
1704} 2015}
1705 2016
1728 } 2039 }
1729 2040
1730static void 2041static void
1731eio_execute (etp_worker *self, eio_req *req) 2042eio_execute (etp_worker *self, eio_req *req)
1732{ 2043{
2044 if (ecb_expect_false (EIO_CANCELLED (req)))
2045 {
2046 req->result = -1;
2047 req->errorno = ECANCELED;
2048 return;
2049 }
2050
1733 switch (req->type) 2051 switch (req->type)
1734 { 2052 {
1735 case EIO_READ: ALLOC (req->size); 2053 case EIO_READ: ALLOC (req->size);
1736 req->result = req->offs >= 0 2054 req->result = req->offs >= 0
1737 ? pread (req->int1, req->ptr2, req->size, req->offs) 2055 ? pread (req->int1, req->ptr2, req->size, req->offs)
1739 case EIO_WRITE: req->result = req->offs >= 0 2057 case EIO_WRITE: req->result = req->offs >= 0
1740 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 2058 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
1741 : write (req->int1, req->ptr2, req->size); break; 2059 : write (req->int1, req->ptr2, req->size); break;
1742 2060
1743 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 2061 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1744 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break; 2062 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
1745 2063
1746 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2064 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1747 req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; 2065 req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1748 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2066 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1749 req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; 2067 req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1771 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; 2089 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1772 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 2090 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1773 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 2091 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1774 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 2092 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1775 2093
2094 case EIO_REALPATH: eio__realpath (req, self); break;
2095
1776 case EIO_READLINK: ALLOC (PATH_MAX); 2096 case EIO_READLINK: ALLOC (PATH_MAX);
1777 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; 2097 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1778 2098
1779 case EIO_SYNC: req->result = 0; sync (); break; 2099 case EIO_SYNC: req->result = 0; sync (); break;
1780 case EIO_FSYNC: req->result = fsync (req->int1); break; 2100 case EIO_FSYNC: req->result = fsync (req->int1); break;
1781 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 2101 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
2102 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break;
2103 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1782 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 2104 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1783 case EIO_MTOUCH: req->result = eio__mtouch (req); break; 2105 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1784 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 2106 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1785 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 2107 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1786 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 2108 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
1787 2109
1788 case EIO_READDIR: eio__scandir (req, self); break; 2110 case EIO_READDIR: eio__scandir (req, self); break;
1789 2111
1790 case EIO_BUSY: 2112 case EIO_BUSY:
1791#ifdef _WIN32 2113#ifdef _WIN32
1871eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) 2193eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1872{ 2194{
1873 REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; 2195 REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1874} 2196}
1875 2197
2198eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
2199{
2200 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
2201}
2202
2203eio_req *eio_syncfs (int fd, int pri, eio_cb cb, void *data)
2204{
2205 REQ (EIO_SYNCFS); req->int1 = fd; SEND;
2206}
2207
2208eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
2209{
2210 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
2211}
2212
1876eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) 2213eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1877{ 2214{
1878 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; 2215 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1879} 2216}
1880 2217
1886eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data) 2223eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data)
1887{ 2224{
1888 REQ (EIO_MLOCKALL); req->int1 = flags; SEND; 2225 REQ (EIO_MLOCKALL); req->int1 = flags; SEND;
1889} 2226}
1890 2227
1891eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) 2228eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data)
1892{ 2229{
1893 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; 2230 REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND;
1894}
1895
1896eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1897{
1898 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1899} 2231}
1900 2232
1901eio_req *eio_close (int fd, int pri, eio_cb cb, void *data) 2233eio_req *eio_close (int fd, int pri, eio_cb cb, void *data)
1902{ 2234{
1903 REQ (EIO_CLOSE); req->int1 = fd; SEND; 2235 REQ (EIO_CLOSE); req->int1 = fd; SEND;
1941eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data) 2273eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
1942{ 2274{
1943 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; 2275 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
1944} 2276}
1945 2277
1946eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2278eio_req *eio_fchown (int fd, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
1947{ 2279{
1948 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2280 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1949} 2281}
1950 2282
1951eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) 2283eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
1971eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data) 2303eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
1972{ 2304{
1973 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; 2305 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
1974} 2306}
1975 2307
1976eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2308eio_req *eio_chown (const char *path, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
1977{ 2309{
1978 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2310 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1979} 2311}
1980 2312
1981eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) 2313eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1995} 2327}
1996 2328
1997eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) 2329eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1998{ 2330{
1999 return eio__1path (EIO_READLINK, path, pri, cb, data); 2331 return eio__1path (EIO_READLINK, path, pri, cb, data);
2332}
2333
2334eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data)
2335{
2336 return eio__1path (EIO_REALPATH, path, pri, cb, data);
2000} 2337}
2001 2338
2002eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) 2339eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
2003{ 2340{
2004 return eio__1path (EIO_STAT, path, pri, cb, data); 2341 return eio__1path (EIO_STAT, path, pri, cb, data);
2123} 2460}
2124 2461
2125/*****************************************************************************/ 2462/*****************************************************************************/
2126/* misc garbage */ 2463/* misc garbage */
2127 2464
2128ssize_t 2465eio_ssize_t
2129eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2466eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2130{ 2467{
2131 etp_worker wrk;
2132 ssize_t ret;
2133
2134 wrk.dbuf = 0;
2135
2136 ret = eio__sendfile (ofd, ifd, offset, count, &wrk); 2468 return eio__sendfile (ofd, ifd, offset, count);
2137
2138 if (wrk.dbuf)
2139 free (wrk.dbuf);
2140
2141 return ret;
2142} 2469}
2143 2470

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines