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

Comparing libeio/eio.c (file contents):
Revision 1.120 by root, Tue Apr 24 18:47:50 2012 UTC vs.
Revision 1.130 by root, Thu May 9 03:03:24 2013 UTC

1/* 1/*
2 * libeio implementation 2 * libeio implementation
3 * 3 *
4 * Copyright (c) 2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann <libeio@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann <libeio@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
10 * 1. Redistributions of source code must retain the above copyright notice, 10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer. 11 * this list of conditions and the following disclaimer.
12 * 12 *
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
132 #define sync() EIO_ENOSYS () 132 #define sync() EIO_ENOSYS ()
133 #define readlink(path,buf,s) EIO_ENOSYS () 133 #define readlink(path,buf,s) EIO_ENOSYS ()
134 #define statvfs(path,buf) EIO_ENOSYS () 134 #define statvfs(path,buf) EIO_ENOSYS ()
135 #define fstatvfs(fd,buf) EIO_ENOSYS () 135 #define fstatvfs(fd,buf) EIO_ENOSYS ()
136 136
137 #define pread(fd,buf,count,offset) eio__pread (fd, buf, count, offset)
138 #define pwrite(fd,buf,count,offset) eio__pwrite (fd, buf, count, offset)
139
140 #if __GNUC__
141 typedef long long eio_off_t; /* signed for compatibility to msvc */
142 #else
143 typedef __int64 eio_off_t; /* unsigned not supported by msvc */
144 #endif
145
146 static eio_ssize_t
147 eio__pread (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
148 {
149 OVERLAPPED o = { 0 };
150 DWORD got;
151
152 o.Offset = offset;
153 o.OffsetHigh = offset >> 32;
154
155 return ReadFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
156 ? got : -1;
157 }
158
159 static eio_ssize_t
160 eio__pwrite (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
161 {
162 OVERLAPPED o = { 0 };
163 DWORD got;
164
165 o.Offset = offset;
166 o.OffsetHigh = offset >> 32;
167
168 return WriteFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
169 ? got : -1;
170 }
171
137 /* rename() uses MoveFile, which fails to overwrite */ 172 /* rename() uses MoveFile, which fails to overwrite */
138 #define rename(old,neu) eio__rename (old, neu) 173 #define rename(old,neu) eio__rename (old, neu)
139 174
140 static int 175 static int
141 eio__rename (const char *old, const char *neu) 176 eio__rename (const char *old, const char *neu)
178 #endif 213 #endif
179 214
180 return EIO_ERRNO (ENOENT, -1); 215 return EIO_ERRNO (ENOENT, -1);
181 } 216 }
182 217
183 /* POSIX API only */ 218 /* POSIX API only, causing trouble for win32 apps */
184 #define CreateHardLink(neu,old,flags) 0 219 #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */
185 #define CreateSymbolicLink(neu,old,flags) 0 220 #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */
186 221
187 struct statvfs 222 struct statvfs
188 { 223 {
189 int dummy; 224 int dummy;
190 }; 225 };
196 231
197#else 232#else
198 233
199 #include <sys/time.h> 234 #include <sys/time.h>
200 #include <sys/select.h> 235 #include <sys/select.h>
201 #include <sys/statvfs.h>
202 #include <unistd.h> 236 #include <unistd.h>
203 #include <signal.h> 237 #include <signal.h>
204 #include <dirent.h> 238 #include <dirent.h>
239
240 #ifdef ANDROID
241 #include <sys/vfs.h>
242 #define statvfs statfs
243 #define fstatvfs fstatfs
244 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */
245 #else
246 #include <sys/statvfs.h>
247 #endif
205 248
206 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 249 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
207 #include <sys/mman.h> 250 #include <sys/mman.h>
208 #endif 251 #endif
209 252
281#endif 324#endif
282 325
283/* buffer size for various temporary buffers */ 326/* buffer size for various temporary buffers */
284#define EIO_BUFSIZE 65536 327#define EIO_BUFSIZE 65536
285 328
286#define dBUF \ 329#define dBUF \
287 char *eio_buf = malloc (EIO_BUFSIZE); \ 330 char *eio_buf = malloc (EIO_BUFSIZE); \
288 errno = ENOMEM; \ 331 errno = ENOMEM; \
289 if (!eio_buf) \ 332 if (!eio_buf) \
290 return -1 333 return -1
291 334
292#define FUBd \ 335#define FUBd \
293 free (eio_buf) 336 free (eio_buf)
294
295#define EIO_TICKS ((1000000 + 1023) >> 10)
296 337
297/*****************************************************************************/ 338/*****************************************************************************/
298 339
299struct tmpbuf 340struct tmpbuf
300{ 341{
339/*****************************************************************************/ 380/*****************************************************************************/
340 381
341#define ETP_PRI_MIN EIO_PRI_MIN 382#define ETP_PRI_MIN EIO_PRI_MIN
342#define ETP_PRI_MAX EIO_PRI_MAX 383#define ETP_PRI_MAX EIO_PRI_MAX
343 384
385#define ETP_TYPE_QUIT -1
386#define ETP_TYPE_GROUP EIO_GROUP
387
344struct etp_worker; 388struct etp_worker;
345 389
346#define ETP_REQ eio_req 390#define ETP_REQ eio_req
347#define ETP_DESTROY(req) eio_destroy (req) 391#define ETP_DESTROY(req) eio_destroy (req)
348static int eio_finish (eio_req *req); 392static int eio_finish (eio_req *req);
349#define ETP_FINISH(req) eio_finish (req) 393#define ETP_FINISH(req) eio_finish (req)
350static void eio_execute (struct etp_worker *self, eio_req *req); 394static void eio_execute (struct etp_worker *self, eio_req *req);
351#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 395#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
352 396
353/*****************************************************************************/ 397#include "etp.c"
354
355#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
356
357/* calculate time difference in ~1/EIO_TICKS of a second */
358ecb_inline int
359tvdiff (struct timeval *tv1, struct timeval *tv2)
360{
361 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
362 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
363}
364
365static unsigned int started, idle, wanted = 4;
366
367static void (*want_poll_cb) (void);
368static void (*done_poll_cb) (void);
369
370static unsigned int max_poll_time; /* reslock */
371static unsigned int max_poll_reqs; /* reslock */
372
373static unsigned int nreqs; /* reqlock */
374static unsigned int nready; /* reqlock */
375static unsigned int npending; /* reqlock */
376static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
377static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
378
379static xmutex_t wrklock;
380static xmutex_t reslock;
381static xmutex_t reqlock;
382static xcond_t reqwait;
383
384#if !HAVE_PREADWRITE
385/*
386 * make our pread/pwrite emulation safe against themselves, but not against
387 * normal read/write by using a mutex. slows down execution a lot,
388 * but that's your problem, not mine.
389 */
390static xmutex_t preadwritelock;
391#endif
392
393typedef struct etp_worker
394{
395 struct tmpbuf tmpbuf;
396
397 /* locked by wrklock */
398 struct etp_worker *prev, *next;
399
400 xthread_t tid;
401
402#ifdef ETP_WORKER_COMMON
403 ETP_WORKER_COMMON
404#endif
405} etp_worker;
406
407static etp_worker wrk_first; /* NOT etp */
408
409#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
410#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
411
412/* worker threads management */
413
414static void
415etp_worker_clear (etp_worker *wrk)
416{
417}
418
419static void ecb_cold
420etp_worker_free (etp_worker *wrk)
421{
422 free (wrk->tmpbuf.ptr);
423
424 wrk->next->prev = wrk->prev;
425 wrk->prev->next = wrk->next;
426
427 free (wrk);
428}
429
430static unsigned int
431etp_nreqs (void)
432{
433 int retval;
434 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
435 retval = nreqs;
436 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
437 return retval;
438}
439
440static unsigned int
441etp_nready (void)
442{
443 unsigned int retval;
444
445 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
446 retval = nready;
447 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
448
449 return retval;
450}
451
452static unsigned int
453etp_npending (void)
454{
455 unsigned int retval;
456
457 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
458 retval = npending;
459 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
460
461 return retval;
462}
463
464static unsigned int
465etp_nthreads (void)
466{
467 unsigned int retval;
468
469 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
470 retval = started;
471 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
472
473 return retval;
474}
475
476/*
477 * a somewhat faster data structure might be nice, but
478 * with 8 priorities this actually needs <20 insns
479 * per shift, the most expensive operation.
480 */
481typedef struct {
482 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
483 int size;
484} etp_reqq;
485
486static etp_reqq req_queue;
487static etp_reqq res_queue;
488
489static void ecb_noinline ecb_cold
490reqq_init (etp_reqq *q)
491{
492 int pri;
493
494 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
495 q->qs[pri] = q->qe[pri] = 0;
496
497 q->size = 0;
498}
499
500static int ecb_noinline
501reqq_push (etp_reqq *q, ETP_REQ *req)
502{
503 int pri = req->pri;
504 req->next = 0;
505
506 if (q->qe[pri])
507 {
508 q->qe[pri]->next = req;
509 q->qe[pri] = req;
510 }
511 else
512 q->qe[pri] = q->qs[pri] = req;
513
514 return q->size++;
515}
516
517static ETP_REQ * ecb_noinline
518reqq_shift (etp_reqq *q)
519{
520 int pri;
521
522 if (!q->size)
523 return 0;
524
525 --q->size;
526
527 for (pri = ETP_NUM_PRI; pri--; )
528 {
529 eio_req *req = q->qs[pri];
530
531 if (req)
532 {
533 if (!(q->qs[pri] = (eio_req *)req->next))
534 q->qe[pri] = 0;
535
536 return req;
537 }
538 }
539
540 abort ();
541}
542
543static int ecb_cold
544etp_init (void (*want_poll)(void), void (*done_poll)(void))
545{
546 X_MUTEX_CREATE (wrklock);
547 X_MUTEX_CREATE (reslock);
548 X_MUTEX_CREATE (reqlock);
549 X_COND_CREATE (reqwait);
550
551 reqq_init (&req_queue);
552 reqq_init (&res_queue);
553
554 wrk_first.next =
555 wrk_first.prev = &wrk_first;
556
557 started = 0;
558 idle = 0;
559 nreqs = 0;
560 nready = 0;
561 npending = 0;
562
563 want_poll_cb = want_poll;
564 done_poll_cb = done_poll;
565
566 return 0;
567}
568
569X_THREAD_PROC (etp_proc);
570
571static void ecb_cold
572etp_start_thread (void)
573{
574 etp_worker *wrk = calloc (1, sizeof (etp_worker));
575
576 /*TODO*/
577 assert (("unable to allocate worker thread data", wrk));
578
579 X_LOCK (wrklock);
580
581 if (thread_create (&wrk->tid, etp_proc, (void *)wrk))
582 {
583 wrk->prev = &wrk_first;
584 wrk->next = wrk_first.next;
585 wrk_first.next->prev = wrk;
586 wrk_first.next = wrk;
587 ++started;
588 }
589 else
590 free (wrk);
591
592 X_UNLOCK (wrklock);
593}
594
595static void
596etp_maybe_start_thread (void)
597{
598 if (ecb_expect_true (etp_nthreads () >= wanted))
599 return;
600
601 /* todo: maybe use idle here, but might be less exact */
602 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
603 return;
604
605 etp_start_thread ();
606}
607
608static void ecb_cold
609etp_end_thread (void)
610{
611 eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */
612
613 req->type = -1;
614 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
615
616 X_LOCK (reqlock);
617 reqq_push (&req_queue, req);
618 X_COND_SIGNAL (reqwait);
619 X_UNLOCK (reqlock);
620
621 X_LOCK (wrklock);
622 --started;
623 X_UNLOCK (wrklock);
624}
625
626static int
627etp_poll (void)
628{
629 unsigned int maxreqs;
630 unsigned int maxtime;
631 struct timeval tv_start, tv_now;
632
633 X_LOCK (reslock);
634 maxreqs = max_poll_reqs;
635 maxtime = max_poll_time;
636 X_UNLOCK (reslock);
637
638 if (maxtime)
639 gettimeofday (&tv_start, 0);
640
641 for (;;)
642 {
643 ETP_REQ *req;
644
645 etp_maybe_start_thread ();
646
647 X_LOCK (reslock);
648 req = reqq_shift (&res_queue);
649
650 if (req)
651 {
652 --npending;
653
654 if (!res_queue.size && done_poll_cb)
655 done_poll_cb ();
656 }
657
658 X_UNLOCK (reslock);
659
660 if (!req)
661 return 0;
662
663 X_LOCK (reqlock);
664 --nreqs;
665 X_UNLOCK (reqlock);
666
667 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
668 {
669 req->int1 = 1; /* mark request as delayed */
670 continue;
671 }
672 else
673 {
674 int res = ETP_FINISH (req);
675 if (ecb_expect_false (res))
676 return res;
677 }
678
679 if (ecb_expect_false (maxreqs && !--maxreqs))
680 break;
681
682 if (maxtime)
683 {
684 gettimeofday (&tv_now, 0);
685
686 if (tvdiff (&tv_start, &tv_now) >= maxtime)
687 break;
688 }
689 }
690
691 errno = EAGAIN;
692 return -1;
693}
694
695static void
696etp_cancel (ETP_REQ *req)
697{
698 req->cancelled = 1;
699
700 eio_grp_cancel (req);
701}
702
703static void
704etp_submit (ETP_REQ *req)
705{
706 req->pri -= ETP_PRI_MIN;
707
708 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
709 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
710
711 if (ecb_expect_false (req->type == EIO_GROUP))
712 {
713 /* I hope this is worth it :/ */
714 X_LOCK (reqlock);
715 ++nreqs;
716 X_UNLOCK (reqlock);
717
718 X_LOCK (reslock);
719
720 ++npending;
721
722 if (!reqq_push (&res_queue, req) && want_poll_cb)
723 want_poll_cb ();
724
725 X_UNLOCK (reslock);
726 }
727 else
728 {
729 X_LOCK (reqlock);
730 ++nreqs;
731 ++nready;
732 reqq_push (&req_queue, req);
733 X_COND_SIGNAL (reqwait);
734 X_UNLOCK (reqlock);
735
736 etp_maybe_start_thread ();
737 }
738}
739
740static void ecb_cold
741etp_set_max_poll_time (double nseconds)
742{
743 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
744 max_poll_time = nseconds * EIO_TICKS;
745 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
746}
747
748static void ecb_cold
749etp_set_max_poll_reqs (unsigned int maxreqs)
750{
751 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
752 max_poll_reqs = maxreqs;
753 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
754}
755
756static void ecb_cold
757etp_set_max_idle (unsigned int nthreads)
758{
759 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
760 max_idle = nthreads;
761 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
762}
763
764static void ecb_cold
765etp_set_idle_timeout (unsigned int seconds)
766{
767 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
768 idle_timeout = seconds;
769 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
770}
771
772static void ecb_cold
773etp_set_min_parallel (unsigned int nthreads)
774{
775 if (wanted < nthreads)
776 wanted = nthreads;
777}
778
779static void ecb_cold
780etp_set_max_parallel (unsigned int nthreads)
781{
782 if (wanted > nthreads)
783 wanted = nthreads;
784
785 while (started > wanted)
786 etp_end_thread ();
787}
788 398
789/*****************************************************************************/ 399/*****************************************************************************/
790 400
791static void 401static void
792grp_try_feed (eio_req *grp) 402grp_try_feed (eio_req *grp)
859} 469}
860 470
861void 471void
862eio_grp_cancel (eio_req *grp) 472eio_grp_cancel (eio_req *grp)
863{ 473{
864 for (grp = grp->grp_first; grp; grp = grp->grp_next)
865 eio_cancel (grp); 474 etp_grp_cancel (grp);
866} 475}
867 476
868void 477void
869eio_cancel (eio_req *req) 478eio_cancel (eio_req *req)
870{ 479{
942 return etp_poll (); 551 return etp_poll ();
943} 552}
944 553
945/*****************************************************************************/ 554/*****************************************************************************/
946/* work around various missing functions */ 555/* work around various missing functions */
947
948#if !HAVE_PREADWRITE
949# undef pread
950# undef pwrite
951# define pread eio__pread
952# define pwrite eio__pwrite
953
954static eio_ssize_t
955eio__pread (int fd, void *buf, size_t count, off_t offset)
956{
957 eio_ssize_t res;
958 off_t ooffset;
959
960 X_LOCK (preadwritelock);
961 ooffset = lseek (fd, 0, SEEK_CUR);
962 lseek (fd, offset, SEEK_SET);
963 res = read (fd, buf, count);
964 lseek (fd, ooffset, SEEK_SET);
965 X_UNLOCK (preadwritelock);
966
967 return res;
968}
969
970static eio_ssize_t
971eio__pwrite (int fd, void *buf, size_t count, off_t offset)
972{
973 eio_ssize_t res;
974 off_t ooffset;
975
976 X_LOCK (preadwritelock);
977 ooffset = lseek (fd, 0, SEEK_CUR);
978 lseek (fd, offset, SEEK_SET);
979 res = write (fd, buf, count);
980 lseek (fd, ooffset, SEEK_SET);
981 X_UNLOCK (preadwritelock);
982
983 return res;
984}
985#endif
986 556
987#ifndef HAVE_UTIMES 557#ifndef HAVE_UTIMES
988 558
989# undef utimes 559# undef utimes
990# define utimes(path,times) eio__utimes (path, times) 560# define utimes(path,times) eio__utimes (path, times)
1398 968
1399/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 969/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1400static int 970static int
1401eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 971eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
1402{ 972{
973 char *res;
1403 const char *rel = path; 974 const char *rel = path;
1404 char *res;
1405 char *tmp1, *tmp2; 975 char *tmp1, *tmp2;
1406#if SYMLOOP_MAX > 32 976#if SYMLOOP_MAX > 32
1407 int symlinks = SYMLOOP_MAX; 977 int symlinks = SYMLOOP_MAX;
1408#else 978#else
1409 int symlinks = 32; 979 int symlinks = 32;
1416 errno = ENOENT; 986 errno = ENOENT;
1417 if (!*rel) 987 if (!*rel)
1418 return -1; 988 return -1;
1419 989
1420 res = tmpbuf_get (tmpbuf, PATH_MAX * 3); 990 res = tmpbuf_get (tmpbuf, PATH_MAX * 3);
991#ifdef _WIN32
992 if (_access (rel, 4) != 0)
993 return -1;
994
995 symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0);
996
997 errno = ENAMETOOLONG;
998 if (symlinks >= PATH_MAX * 3)
999 return -1;
1000
1001 errno = EIO;
1002 if (symlinks <= 0)
1003 return -1;
1004
1005 return symlinks;
1006
1007#else
1421 tmp1 = res + PATH_MAX; 1008 tmp1 = res + PATH_MAX;
1422 tmp2 = tmp1 + PATH_MAX; 1009 tmp2 = tmp1 + PATH_MAX;
1423 1010
1424#if 0 /* disabled, the musl way to do things is just too racy */ 1011#if 0 /* disabled, the musl way to do things is just too racy */
1425#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1012#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1429 1016
1430 if (fd >= 0) 1017 if (fd >= 0)
1431 { 1018 {
1432 sprintf (tmp1, "/proc/self/fd/%d", fd); 1019 sprintf (tmp1, "/proc/self/fd/%d", fd);
1433 req->result = readlink (tmp1, res, PATH_MAX); 1020 req->result = readlink (tmp1, res, PATH_MAX);
1021 /* here we should probably stat the open file and the disk file, to make sure they still match */
1434 close (fd); 1022 close (fd);
1435
1436 /* here we should probably stat the open file and the disk file, to make sure they still match */
1437 1023
1438 if (req->result > 0) 1024 if (req->result > 0)
1439 goto done; 1025 goto done;
1440 } 1026 }
1441 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) 1027 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1442 return; 1028 return -1;
1443 } 1029 }
1444#endif 1030#endif
1445#endif 1031#endif
1446 1032
1447 if (*rel != '/') 1033 if (*rel != '/')
1549 /* special case for the lone root path */ 1135 /* special case for the lone root path */
1550 if (res == tmpbuf->ptr) 1136 if (res == tmpbuf->ptr)
1551 *res++ = '/'; 1137 *res++ = '/';
1552 1138
1553 return res - (char *)tmpbuf->ptr; 1139 return res - (char *)tmpbuf->ptr;
1140#endif
1554} 1141}
1555 1142
1556static signed char 1143static signed char
1557eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1144eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1558{ 1145{
1931 #ifdef DT_FIFO 1518 #ifdef DT_FIFO
1932 case DT_FIFO: ent->type = EIO_DT_FIFO; break; 1519 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1933 #endif 1520 #endif
1934 #ifdef DT_CHR 1521 #ifdef DT_CHR
1935 case DT_CHR: ent->type = EIO_DT_CHR; break; 1522 case DT_CHR: ent->type = EIO_DT_CHR; break;
1936 #endif 1523 #endif
1937 #ifdef DT_MPC 1524 #ifdef DT_MPC
1938 case DT_MPC: ent->type = EIO_DT_MPC; break; 1525 case DT_MPC: ent->type = EIO_DT_MPC; break;
1939 #endif 1526 #endif
1940 #ifdef DT_DIR 1527 #ifdef DT_DIR
1941 case DT_DIR: ent->type = EIO_DT_DIR; break; 1528 case DT_DIR: ent->type = EIO_DT_DIR; break;
1942 #endif 1529 #endif
1943 #ifdef DT_NAM 1530 #ifdef DT_NAM
1944 case DT_NAM: ent->type = EIO_DT_NAM; break; 1531 case DT_NAM: ent->type = EIO_DT_NAM; break;
1945 #endif 1532 #endif
1946 #ifdef DT_BLK 1533 #ifdef DT_BLK
1947 case DT_BLK: ent->type = EIO_DT_BLK; break; 1534 case DT_BLK: ent->type = EIO_DT_BLK; break;
1948 #endif 1535 #endif
1949 #ifdef DT_MPB 1536 #ifdef DT_MPB
1950 case DT_MPB: ent->type = EIO_DT_MPB; break; 1537 case DT_MPB: ent->type = EIO_DT_MPB; break;
1951 #endif 1538 #endif
1952 #ifdef DT_REG 1539 #ifdef DT_REG
1953 case DT_REG: ent->type = EIO_DT_REG; break; 1540 case DT_REG: ent->type = EIO_DT_REG; break;
1954 #endif 1541 #endif
1955 #ifdef DT_NWK 1542 #ifdef DT_NWK
1956 case DT_NWK: ent->type = EIO_DT_NWK; break; 1543 case DT_NWK: ent->type = EIO_DT_NWK; break;
1957 #endif 1544 #endif
1958 #ifdef DT_CMP 1545 #ifdef DT_CMP
1959 case DT_CMP: ent->type = EIO_DT_CMP; break; 1546 case DT_CMP: ent->type = EIO_DT_CMP; break;
1960 #endif 1547 #endif
1961 #ifdef DT_LNK 1548 #ifdef DT_LNK
1962 case DT_LNK: ent->type = EIO_DT_LNK; break; 1549 case DT_LNK: ent->type = EIO_DT_LNK; break;
1963 #endif 1550 #endif
1964 #ifdef DT_SOCK 1551 #ifdef DT_SOCK
1965 case DT_SOCK: ent->type = EIO_DT_SOCK; break; 1552 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
2162 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio"); 1749 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2163 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0); 1750 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2164#endif 1751#endif
2165} 1752}
2166 1753
1754/* TODO: move somehow to etp.c */
2167X_THREAD_PROC (etp_proc) 1755X_THREAD_PROC (etp_proc)
2168{ 1756{
2169 ETP_REQ *req; 1757 ETP_REQ *req;
2170 struct timespec ts; 1758 struct timespec ts;
2171 etp_worker *self = (etp_worker *)thr_arg; 1759 etp_worker *self = (etp_worker *)thr_arg;
2217 1805
2218 --nready; 1806 --nready;
2219 1807
2220 X_UNLOCK (reqlock); 1808 X_UNLOCK (reqlock);
2221 1809
2222 if (req->type < 0) 1810 if (req->type == ETP_TYPE_QUIT)
2223 goto quit; 1811 goto quit;
2224 1812
2225 ETP_EXECUTE (self, req); 1813 ETP_EXECUTE (self, req);
2226 1814
2227 X_LOCK (reslock); 1815 X_LOCK (reslock);
2240 free (req); 1828 free (req);
2241 1829
2242 X_LOCK (wrklock); 1830 X_LOCK (wrklock);
2243 etp_worker_free (self); 1831 etp_worker_free (self);
2244 X_UNLOCK (wrklock); 1832 X_UNLOCK (wrklock);
1833
1834 return 0;
2245} 1835}
2246 1836
2247/*****************************************************************************/ 1837/*****************************************************************************/
2248 1838
2249int ecb_cold 1839int ecb_cold
2250eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1840eio_init (void (*want_poll)(void), void (*done_poll)(void))
2251{ 1841{
2252#if !HAVE_PREADWRITE
2253 X_MUTEX_CREATE (preadwritelock);
2254#endif
2255
2256 return etp_init (want_poll, done_poll); 1842 return etp_init (want_poll, done_poll);
2257} 1843}
2258 1844
2259ecb_inline void 1845ecb_inline void
2260eio_api_destroy (eio_req *req) 1846eio_api_destroy (eio_req *req)
2261{ 1847{
2262 free (req); 1848 free (req);
2263} 1849}
2264 1850
2265#define REQ(rtype) \ 1851#define REQ(rtype) \
2266 eio_req *req; \ 1852 eio_req *req; \
2267 \ 1853 \
2268 req = (eio_req *)calloc (1, sizeof *req); \ 1854 req = (eio_req *)calloc (1, sizeof *req); \
2269 if (!req) \ 1855 if (!req) \
2270 return 0; \ 1856 return 0; \
2284 { \ 1870 { \
2285 eio_api_destroy (req); \ 1871 eio_api_destroy (req); \
2286 return 0; \ 1872 return 0; \
2287 } 1873 }
2288 1874
1875#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)])
1876
2289static void 1877static void
2290eio_execute (etp_worker *self, eio_req *req) 1878eio_execute (etp_worker *self, eio_req *req)
2291{ 1879{
2292#if HAVE_AT 1880#if HAVE_AT
2293 int dirfd; 1881 int dirfd;
2348 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; 1936 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break;
2349 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; 1937 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break;
2350 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; 1938 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
2351 1939
2352 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1940 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2353 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 1941 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
1942 req->result = req->wd && SINGLEDOT (req->ptr1)
1943 ? rmdir (req->wd->str)
1944 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2354 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 1945 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
2355 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 1946 case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */
1947 req->result = req->wd && SINGLEDOT (req->ptr1)
1948 ? rename (req->wd->str, req->ptr2)
1949 : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break;
2356 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; 1950 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break;
2357 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; 1951 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break;
2358 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 1952 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
2359 case EIO_READLINK: ALLOC (PATH_MAX); 1953 case EIO_READLINK: ALLOC (PATH_MAX);
2360 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; 1954 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines