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

Comparing libeio/eio.c (file contents):
Revision 1.127 by root, Mon Feb 18 03:15:19 2013 UTC vs.
Revision 1.150 by root, Fri Jun 15 02:57:36 2018 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,2016,2017 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 *
42#endif 42#endif
43 43
44#include "eio.h" 44#include "eio.h"
45#include "ecb.h" 45#include "ecb.h"
46 46
47#ifdef EIO_STACKSIZE
48# define X_STACKSIZE EIO_STACKSIZE
49#endif
50#include "xthread.h"
51
52#include <errno.h> 47#include <errno.h>
53#include <stddef.h> 48#include <stddef.h>
54#include <stdlib.h> 49#include <stdlib.h>
55#include <string.h> 50#include <string.h>
56#include <errno.h> 51#include <errno.h>
120 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) 115 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
121 116
122 #define chmod(path,mode) _chmod (path, mode) 117 #define chmod(path,mode) _chmod (path, mode)
123 #define dup(fd) _dup (fd) 118 #define dup(fd) _dup (fd)
124 #define dup2(fd1,fd2) _dup2 (fd1, fd2) 119 #define dup2(fd1,fd2) _dup2 (fd1, fd2)
120 #define pipe(fds) _pipe (fds, 4096, O_BINARY)
125 121
122 #define fcntl(fd,cmd,arg) EIO_ENOSYS ()
123 #define ioctl(fd,cmd,arg) EIO_ENOSYS ()
126 #define fchmod(fd,mode) EIO_ENOSYS () 124 #define fchmod(fd,mode) EIO_ENOSYS ()
127 #define chown(path,uid,gid) EIO_ENOSYS () 125 #define chown(path,uid,gid) EIO_ENOSYS ()
128 #define fchown(fd,uid,gid) EIO_ENOSYS () 126 #define fchown(fd,uid,gid) EIO_ENOSYS ()
129 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ 127 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
130 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ 128 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
203 /* we could even stat and see if it exists */ 201 /* we could even stat and see if it exists */
204 static int 202 static int
205 symlink (const char *old, const char *neu) 203 symlink (const char *old, const char *neu)
206 { 204 {
207 #if WINVER >= 0x0600 205 #if WINVER >= 0x0600
206 int flags;
207
208 /* This tries out all combinations of SYMBOLIC_LINK_FLAG_DIRECTORY
209 * and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE,
210 * with directory first.
211 */
212 for (flags = 3; flags >= 0; --flags)
208 if (CreateSymbolicLink (neu, old, 1)) 213 if (CreateSymbolicLink (neu, old, flags))
209 return 0; 214 return 0;
210
211 if (CreateSymbolicLink (neu, old, 0))
212 return 0;
213 #endif 215 #endif
214 216
215 return EIO_ERRNO (ENOENT, -1); 217 return EIO_ERRNO (ENOENT, -1);
216 } 218 }
217 219
218 /* POSIX API only */ 220 /* POSIX API only, causing trouble for win32 apps */
219 #define CreateHardLink(neu,old,flags) 0 221 #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */
220 #define CreateSymbolicLink(neu,old,flags) 0 222 #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */
221 223
222 struct statvfs 224 struct statvfs
223 { 225 {
224 int dummy; 226 int dummy;
225 }; 227 };
229 #define D_NAME(entp) entp.cFileName 231 #define D_NAME(entp) entp.cFileName
230 #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG) 232 #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG)
231 233
232#else 234#else
233 235
236 #include <sys/ioctl.h>
234 #include <sys/time.h> 237 #include <sys/time.h>
235 #include <sys/select.h> 238 #include <sys/select.h>
236 #include <unistd.h> 239 #include <unistd.h>
237 #include <signal.h> 240 #include <signal.h>
238 #include <dirent.h> 241 #include <dirent.h>
239 242
240 #if ANDROID 243 #ifdef ANDROID
241 #include <sys/vfs.h> 244 #include <sys/vfs.h>
242 #define statvfs statfs 245 #define statvfs statfs
243 #define fstatvfs fstatfs 246 #define fstatvfs fstatfs
244 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */ 247 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */
245 #else 248 #else
280# include <utime.h> 283# include <utime.h>
281#endif 284#endif
282 285
283#if HAVE_SYS_SYSCALL_H 286#if HAVE_SYS_SYSCALL_H
284# include <sys/syscall.h> 287# include <sys/syscall.h>
285#endif
286
287#if HAVE_SYS_PRCTL_H
288# include <sys/prctl.h>
289#endif 288#endif
290 289
291#if HAVE_SENDFILE 290#if HAVE_SENDFILE
292# if __linux 291# if __linux
293# include <sys/sendfile.h> 292# include <sys/sendfile.h>
301# else 300# else
302# error sendfile support requested but not available 301# error sendfile support requested but not available
303# endif 302# endif
304#endif 303#endif
305 304
305#if HAVE_RENAMEAT2
306# include <sys/syscall.h>
307# include <linux/fs.h>
308#endif
309
306#ifndef D_TYPE 310#ifndef D_TYPE
307# define D_TYPE(de) 0 311# define D_TYPE(de) 0
308#endif 312#endif
309#ifndef D_INO 313#ifndef D_INO
310# define D_INO(de) 0 314# define D_INO(de) 0
318# define NAME_MAX 4096 322# define NAME_MAX 4096
319#endif 323#endif
320 324
321/* used for readlink etc. */ 325/* used for readlink etc. */
322#ifndef PATH_MAX 326#ifndef PATH_MAX
323# define PATH_MAX 4096 327# define PATH_MAX 0
324#endif 328#endif
329
330#ifndef O_CLOEXEC
331 #define O_CLOEXEC 0
332#endif
333
334#ifndef EIO_PATH_MIN
335# define EIO_PATH_MIN 8160
336#endif
337
338#define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX)
325 339
326/* buffer size for various temporary buffers */ 340/* buffer size for various temporary buffers */
327#define EIO_BUFSIZE 65536 341#define EIO_BUFSIZE 65536
328 342
329#define dBUF \ 343#define dBUF \
333 return -1 347 return -1
334 348
335#define FUBd \ 349#define FUBd \
336 free (eio_buf) 350 free (eio_buf)
337 351
338#define EIO_TICKS ((1000000 + 1023) >> 10)
339
340/*****************************************************************************/ 352/*****************************************************************************/
341 353
342struct tmpbuf
343{
344 void *ptr;
345 int len;
346};
347
348static void *
349tmpbuf_get (struct tmpbuf *buf, int len)
350{
351 if (buf->len < len)
352 {
353 free (buf->ptr);
354 buf->ptr = malloc (buf->len = len);
355 }
356
357 return buf->ptr;
358}
359
360struct tmpbuf; 354struct etp_tmpbuf;
361 355
362#if _POSIX_VERSION >= 200809L 356#if _POSIX_VERSION >= 200809L
363 #define HAVE_AT 1 357 #define HAVE_AT 1
364 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 358 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
365 #ifndef O_SEARCH 359 #ifndef O_SEARCH
366 #define O_SEARCH O_RDONLY 360 #define O_SEARCH O_RDONLY
367 #endif 361 #endif
368#else 362#else
369 #define HAVE_AT 0 363 #define HAVE_AT 0
370 static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); 364 static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path);
371#endif 365#endif
372 366
373struct eio_pwd 367struct eio_pwd
374{ 368{
375#if HAVE_AT 369#if HAVE_AT
382/*****************************************************************************/ 376/*****************************************************************************/
383 377
384#define ETP_PRI_MIN EIO_PRI_MIN 378#define ETP_PRI_MIN EIO_PRI_MIN
385#define ETP_PRI_MAX EIO_PRI_MAX 379#define ETP_PRI_MAX EIO_PRI_MAX
386 380
381#define ETP_TYPE_QUIT -1
382#define ETP_TYPE_GROUP EIO_GROUP
383
384static void eio_nop_callback (void) { }
385static void (*eio_want_poll_cb)(void) = eio_nop_callback;
386static void (*eio_done_poll_cb)(void) = eio_nop_callback;
387
388#define ETP_WANT_POLL(pool) eio_want_poll_cb ()
389#define ETP_DONE_POLL(pool) eio_done_poll_cb ()
390
387struct etp_worker; 391struct etp_worker;
388
389#define ETP_REQ eio_req 392#define ETP_REQ eio_req
390#define ETP_DESTROY(req) eio_destroy (req) 393#define ETP_DESTROY(req) eio_destroy (req)
391static int eio_finish (eio_req *req); 394static int eio_finish (eio_req *req);
392#define ETP_FINISH(req) eio_finish (req) 395#define ETP_FINISH(req) eio_finish (req)
393static void eio_execute (struct etp_worker *self, eio_req *req); 396static void eio_execute (struct etp_worker *self, eio_req *req);
394#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 397#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
395 398
396/*****************************************************************************/ 399#include "etp.c"
397 400
398#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 401static struct etp_pool eio_pool;
399 402#define EIO_POOL (&eio_pool)
400/* calculate time difference in ~1/EIO_TICKS of a second */
401ecb_inline int
402tvdiff (struct timeval *tv1, struct timeval *tv2)
403{
404 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
405 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
406}
407
408static unsigned int started, idle, wanted = 4;
409
410static void (*want_poll_cb) (void);
411static void (*done_poll_cb) (void);
412
413static unsigned int max_poll_time; /* reslock */
414static unsigned int max_poll_reqs; /* reslock */
415
416static unsigned int nreqs; /* reqlock */
417static unsigned int nready; /* reqlock */
418static unsigned int npending; /* reqlock */
419static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
420static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
421
422static xmutex_t wrklock;
423static xmutex_t reslock;
424static xmutex_t reqlock;
425static xcond_t reqwait;
426
427typedef struct etp_worker
428{
429 struct tmpbuf tmpbuf;
430
431 /* locked by wrklock */
432 struct etp_worker *prev, *next;
433
434 xthread_t tid;
435
436#ifdef ETP_WORKER_COMMON
437 ETP_WORKER_COMMON
438#endif
439} etp_worker;
440
441static etp_worker wrk_first; /* NOT etp */
442
443#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
444#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
445
446/* worker threads management */
447
448static void
449etp_worker_clear (etp_worker *wrk)
450{
451}
452
453static void ecb_cold
454etp_worker_free (etp_worker *wrk)
455{
456 free (wrk->tmpbuf.ptr);
457
458 wrk->next->prev = wrk->prev;
459 wrk->prev->next = wrk->next;
460
461 free (wrk);
462}
463
464static unsigned int
465etp_nreqs (void)
466{
467 int retval;
468 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
469 retval = nreqs;
470 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
471 return retval;
472}
473
474static unsigned int
475etp_nready (void)
476{
477 unsigned int retval;
478
479 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
480 retval = nready;
481 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
482
483 return retval;
484}
485
486static unsigned int
487etp_npending (void)
488{
489 unsigned int retval;
490
491 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
492 retval = npending;
493 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
494
495 return retval;
496}
497
498static unsigned int
499etp_nthreads (void)
500{
501 unsigned int retval;
502
503 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
504 retval = started;
505 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
506
507 return retval;
508}
509
510/*
511 * a somewhat faster data structure might be nice, but
512 * with 8 priorities this actually needs <20 insns
513 * per shift, the most expensive operation.
514 */
515typedef struct {
516 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
517 int size;
518} etp_reqq;
519
520static etp_reqq req_queue;
521static etp_reqq res_queue;
522
523static void ecb_noinline ecb_cold
524reqq_init (etp_reqq *q)
525{
526 int pri;
527
528 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
529 q->qs[pri] = q->qe[pri] = 0;
530
531 q->size = 0;
532}
533
534static int ecb_noinline
535reqq_push (etp_reqq *q, ETP_REQ *req)
536{
537 int pri = req->pri;
538 req->next = 0;
539
540 if (q->qe[pri])
541 {
542 q->qe[pri]->next = req;
543 q->qe[pri] = req;
544 }
545 else
546 q->qe[pri] = q->qs[pri] = req;
547
548 return q->size++;
549}
550
551static ETP_REQ * ecb_noinline
552reqq_shift (etp_reqq *q)
553{
554 int pri;
555
556 if (!q->size)
557 return 0;
558
559 --q->size;
560
561 for (pri = ETP_NUM_PRI; pri--; )
562 {
563 eio_req *req = q->qs[pri];
564
565 if (req)
566 {
567 if (!(q->qs[pri] = (eio_req *)req->next))
568 q->qe[pri] = 0;
569
570 return req;
571 }
572 }
573
574 abort ();
575}
576
577static int ecb_cold
578etp_init (void (*want_poll)(void), void (*done_poll)(void))
579{
580 X_MUTEX_CREATE (wrklock);
581 X_MUTEX_CREATE (reslock);
582 X_MUTEX_CREATE (reqlock);
583 X_COND_CREATE (reqwait);
584
585 reqq_init (&req_queue);
586 reqq_init (&res_queue);
587
588 wrk_first.next =
589 wrk_first.prev = &wrk_first;
590
591 started = 0;
592 idle = 0;
593 nreqs = 0;
594 nready = 0;
595 npending = 0;
596
597 want_poll_cb = want_poll;
598 done_poll_cb = done_poll;
599
600 return 0;
601}
602
603X_THREAD_PROC (etp_proc);
604
605static void ecb_cold
606etp_start_thread (void)
607{
608 etp_worker *wrk = calloc (1, sizeof (etp_worker));
609
610 /*TODO*/
611 assert (("unable to allocate worker thread data", wrk));
612
613 X_LOCK (wrklock);
614
615 if (xthread_create (&wrk->tid, etp_proc, (void *)wrk))
616 {
617 wrk->prev = &wrk_first;
618 wrk->next = wrk_first.next;
619 wrk_first.next->prev = wrk;
620 wrk_first.next = wrk;
621 ++started;
622 }
623 else
624 free (wrk);
625
626 X_UNLOCK (wrklock);
627}
628
629static void
630etp_maybe_start_thread (void)
631{
632 if (ecb_expect_true (etp_nthreads () >= wanted))
633 return;
634
635 /* todo: maybe use idle here, but might be less exact */
636 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
637 return;
638
639 etp_start_thread ();
640}
641
642static void ecb_cold
643etp_end_thread (void)
644{
645 eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */
646
647 req->type = -1;
648 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
649
650 X_LOCK (reqlock);
651 reqq_push (&req_queue, req);
652 X_COND_SIGNAL (reqwait);
653 X_UNLOCK (reqlock);
654
655 X_LOCK (wrklock);
656 --started;
657 X_UNLOCK (wrklock);
658}
659
660static int
661etp_poll (void)
662{
663 unsigned int maxreqs;
664 unsigned int maxtime;
665 struct timeval tv_start, tv_now;
666
667 X_LOCK (reslock);
668 maxreqs = max_poll_reqs;
669 maxtime = max_poll_time;
670 X_UNLOCK (reslock);
671
672 if (maxtime)
673 gettimeofday (&tv_start, 0);
674
675 for (;;)
676 {
677 ETP_REQ *req;
678
679 etp_maybe_start_thread ();
680
681 X_LOCK (reslock);
682 req = reqq_shift (&res_queue);
683
684 if (req)
685 {
686 --npending;
687
688 if (!res_queue.size && done_poll_cb)
689 done_poll_cb ();
690 }
691
692 X_UNLOCK (reslock);
693
694 if (!req)
695 return 0;
696
697 X_LOCK (reqlock);
698 --nreqs;
699 X_UNLOCK (reqlock);
700
701 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
702 {
703 req->int1 = 1; /* mark request as delayed */
704 continue;
705 }
706 else
707 {
708 int res = ETP_FINISH (req);
709 if (ecb_expect_false (res))
710 return res;
711 }
712
713 if (ecb_expect_false (maxreqs && !--maxreqs))
714 break;
715
716 if (maxtime)
717 {
718 gettimeofday (&tv_now, 0);
719
720 if (tvdiff (&tv_start, &tv_now) >= maxtime)
721 break;
722 }
723 }
724
725 errno = EAGAIN;
726 return -1;
727}
728
729static void
730etp_cancel (ETP_REQ *req)
731{
732 req->cancelled = 1;
733
734 eio_grp_cancel (req);
735}
736
737static void
738etp_submit (ETP_REQ *req)
739{
740 req->pri -= ETP_PRI_MIN;
741
742 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
743 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
744
745 if (ecb_expect_false (req->type == EIO_GROUP))
746 {
747 /* I hope this is worth it :/ */
748 X_LOCK (reqlock);
749 ++nreqs;
750 X_UNLOCK (reqlock);
751
752 X_LOCK (reslock);
753
754 ++npending;
755
756 if (!reqq_push (&res_queue, req) && want_poll_cb)
757 want_poll_cb ();
758
759 X_UNLOCK (reslock);
760 }
761 else
762 {
763 X_LOCK (reqlock);
764 ++nreqs;
765 ++nready;
766 reqq_push (&req_queue, req);
767 X_COND_SIGNAL (reqwait);
768 X_UNLOCK (reqlock);
769
770 etp_maybe_start_thread ();
771 }
772}
773
774static void ecb_cold
775etp_set_max_poll_time (double nseconds)
776{
777 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
778 max_poll_time = nseconds * EIO_TICKS;
779 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
780}
781
782static void ecb_cold
783etp_set_max_poll_reqs (unsigned int maxreqs)
784{
785 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
786 max_poll_reqs = maxreqs;
787 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
788}
789
790static void ecb_cold
791etp_set_max_idle (unsigned int nthreads)
792{
793 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
794 max_idle = nthreads;
795 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
796}
797
798static void ecb_cold
799etp_set_idle_timeout (unsigned int seconds)
800{
801 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
802 idle_timeout = seconds;
803 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
804}
805
806static void ecb_cold
807etp_set_min_parallel (unsigned int nthreads)
808{
809 if (wanted < nthreads)
810 wanted = nthreads;
811}
812
813static void ecb_cold
814etp_set_max_parallel (unsigned int nthreads)
815{
816 if (wanted > nthreads)
817 wanted = nthreads;
818
819 while (started > wanted)
820 etp_end_thread ();
821}
822 403
823/*****************************************************************************/ 404/*****************************************************************************/
824 405
825static void 406static void
826grp_try_feed (eio_req *grp) 407grp_try_feed (eio_req *grp)
827{ 408{
828 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 409 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
829 { 410 {
830 grp->flags &= ~EIO_FLAG_GROUPADD; 411 grp->flags &= ~ETP_FLAG_GROUPADD;
831 412
832 EIO_FEED (grp); 413 EIO_FEED (grp);
833 414
834 /* stop if no progress has been made */ 415 /* stop if no progress has been made */
835 if (!(grp->flags & EIO_FLAG_GROUPADD)) 416 if (!(grp->flags & ETP_FLAG_GROUPADD))
836 { 417 {
837 grp->feed = 0; 418 grp->feed = 0;
838 break; 419 break;
839 } 420 }
840 } 421 }
847 428
848 /* call feeder, if applicable */ 429 /* call feeder, if applicable */
849 grp_try_feed (grp); 430 grp_try_feed (grp);
850 431
851 /* finish, if done */ 432 /* finish, if done */
852 if (!grp->size && grp->int1) 433 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
853 return eio_finish (grp); 434 return eio_finish (grp);
854 else 435 else
855 return 0; 436 return 0;
856} 437}
857 438
893} 474}
894 475
895void 476void
896eio_grp_cancel (eio_req *grp) 477eio_grp_cancel (eio_req *grp)
897{ 478{
898 for (grp = grp->grp_first; grp; grp = grp->grp_next) 479 etp_grp_cancel (EIO_POOL, grp);
899 eio_cancel (grp);
900} 480}
901 481
902void 482void
903eio_cancel (eio_req *req) 483eio_cancel (eio_req *req)
904{ 484{
905 etp_cancel (req); 485 etp_cancel (EIO_POOL, req);
906} 486}
907 487
908void 488void
909eio_submit (eio_req *req) 489eio_submit (eio_req *req)
910{ 490{
911 etp_submit (req); 491 etp_submit (EIO_POOL, req);
912} 492}
913 493
914unsigned int 494unsigned int
915eio_nreqs (void) 495eio_nreqs (void)
916{ 496{
917 return etp_nreqs (); 497 return etp_nreqs (EIO_POOL);
918} 498}
919 499
920unsigned int 500unsigned int
921eio_nready (void) 501eio_nready (void)
922{ 502{
923 return etp_nready (); 503 return etp_nready (EIO_POOL);
924} 504}
925 505
926unsigned int 506unsigned int
927eio_npending (void) 507eio_npending (void)
928{ 508{
929 return etp_npending (); 509 return etp_npending (EIO_POOL);
930} 510}
931 511
932unsigned int ecb_cold 512unsigned int ecb_cold
933eio_nthreads (void) 513eio_nthreads (void)
934{ 514{
935 return etp_nthreads (); 515 return etp_nthreads (EIO_POOL);
936} 516}
937 517
938void ecb_cold 518void ecb_cold
939eio_set_max_poll_time (double nseconds) 519eio_set_max_poll_time (double nseconds)
940{ 520{
941 etp_set_max_poll_time (nseconds); 521 etp_set_max_poll_time (EIO_POOL, nseconds);
942} 522}
943 523
944void ecb_cold 524void ecb_cold
945eio_set_max_poll_reqs (unsigned int maxreqs) 525eio_set_max_poll_reqs (unsigned int maxreqs)
946{ 526{
947 etp_set_max_poll_reqs (maxreqs); 527 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
948} 528}
949 529
950void ecb_cold 530void ecb_cold
951eio_set_max_idle (unsigned int nthreads) 531eio_set_max_idle (unsigned int nthreads)
952{ 532{
953 etp_set_max_idle (nthreads); 533 etp_set_max_idle (EIO_POOL, nthreads);
954} 534}
955 535
956void ecb_cold 536void ecb_cold
957eio_set_idle_timeout (unsigned int seconds) 537eio_set_idle_timeout (unsigned int seconds)
958{ 538{
959 etp_set_idle_timeout (seconds); 539 etp_set_idle_timeout (EIO_POOL, seconds);
960} 540}
961 541
962void ecb_cold 542void ecb_cold
963eio_set_min_parallel (unsigned int nthreads) 543eio_set_min_parallel (unsigned int nthreads)
964{ 544{
965 etp_set_min_parallel (nthreads); 545 etp_set_min_parallel (EIO_POOL, nthreads);
966} 546}
967 547
968void ecb_cold 548void ecb_cold
969eio_set_max_parallel (unsigned int nthreads) 549eio_set_max_parallel (unsigned int nthreads)
970{ 550{
971 etp_set_max_parallel (nthreads); 551 etp_set_max_parallel (EIO_POOL, nthreads);
972} 552}
973 553
974int eio_poll (void) 554int eio_poll (void)
975{ 555{
976 return etp_poll (); 556 return etp_poll (EIO_POOL);
977} 557}
978 558
979/*****************************************************************************/ 559/*****************************************************************************/
980/* work around various missing functions */ 560/* work around various missing functions */
561
562#if HAVE_POSIX_CLOSE && !__linux
563# define eio__close(fd) posix_close (fd, 0)
564#else
565# define eio__close(fd) close (fd)
566#endif
567
568/* close() without disturbing errno */
569static void
570silent_close (int fd)
571{
572 int saved_errno = errno;
573 eio__close (fd);
574 errno = saved_errno;
575}
981 576
982#ifndef HAVE_UTIMES 577#ifndef HAVE_UTIMES
983 578
984# undef utimes 579# undef utimes
985# define utimes(path,times) eio__utimes (path, times) 580# define utimes(path,times) eio__utimes (path, times)
1364 intptr_t end = addr + len; 959 intptr_t end = addr + len;
1365 intptr_t page = eio_pagesize (); 960 intptr_t page = eio_pagesize ();
1366 961
1367 if (addr < end) 962 if (addr < end)
1368 if (flags & EIO_MT_MODIFY) /* modify */ 963 if (flags & EIO_MT_MODIFY) /* modify */
1369 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); 964 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < end && !EIO_CANCELLED (req));
1370 else 965 else
1371 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); 966 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < end && !EIO_CANCELLED (req));
1372 } 967 }
1373 968
1374 return 0; 969 return 0;
1375} 970}
1376 971
1391 req->result = req->offs == (off_t)-1 ? -1 : 0; 986 req->result = req->offs == (off_t)-1 ? -1 : 0;
1392} 987}
1393 988
1394/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 989/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1395static int 990static int
1396eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 991eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1397{ 992{
993 char *res;
1398 const char *rel = path; 994 const char *rel = path;
1399 char *res;
1400 char *tmp1, *tmp2; 995 char *tmp1, *tmp2;
1401#if SYMLOOP_MAX > 32 996#if SYMLOOP_MAX > 32
1402 int symlinks = SYMLOOP_MAX; 997 int symlinks = SYMLOOP_MAX;
1403#else 998#else
1404 int symlinks = 32; 999 int symlinks = 32;
1410 1005
1411 errno = ENOENT; 1006 errno = ENOENT;
1412 if (!*rel) 1007 if (!*rel)
1413 return -1; 1008 return -1;
1414 1009
1415 res = tmpbuf_get (tmpbuf, PATH_MAX * 3); 1010 res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3);
1011#ifdef _WIN32
1012 if (_access (rel, 4) != 0)
1013 return -1;
1014
1015 symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0);
1016
1017 errno = ENAMETOOLONG;
1018 if (symlinks >= EIO_PATH_MAX * 3)
1019 return -1;
1020
1021 errno = EIO;
1022 if (symlinks <= 0)
1023 return -1;
1024
1025 return symlinks;
1026
1027#else
1416 tmp1 = res + PATH_MAX; 1028 tmp1 = res + EIO_PATH_MAX;
1417 tmp2 = tmp1 + PATH_MAX; 1029 tmp2 = tmp1 + EIO_PATH_MAX;
1418 1030
1419#if 0 /* disabled, the musl way to do things is just too racy */ 1031#if 0 /* disabled, the musl way to do things is just too racy */
1420#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1032#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1421 /* on linux we may be able to ask the kernel */ 1033 /* on linux we may be able to ask the kernel */
1422 { 1034 {
1423 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); 1035 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1424 1036
1425 if (fd >= 0) 1037 if (fd >= 0)
1426 { 1038 {
1427 sprintf (tmp1, "/proc/self/fd/%d", fd); 1039 sprintf (tmp1, "/proc/self/fd/%d", fd);
1428 req->result = readlink (tmp1, res, PATH_MAX); 1040 req->result = readlink (tmp1, res, EIO_PATH_MAX);
1429 /* here we should probably stat the open file and the disk file, to make sure they still match */ 1041 /* here we should probably stat the open file and the disk file, to make sure they still match */
1430 close (fd); 1042 eio__close (fd);
1431 1043
1432 if (req->result > 0) 1044 if (req->result > 0)
1433 goto done; 1045 goto done;
1434 } 1046 }
1435 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) 1047 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1446 if (wd == EIO_INVALID_WD) 1058 if (wd == EIO_INVALID_WD)
1447 return -1; 1059 return -1;
1448 1060
1449 if (wd == EIO_CWD) 1061 if (wd == EIO_CWD)
1450 { 1062 {
1451 if (!getcwd (res, PATH_MAX)) 1063 if (!getcwd (res, EIO_PATH_MAX))
1452 return -1; 1064 return -1;
1453 1065
1454 len = strlen (res); 1066 len = strlen (res);
1455 } 1067 }
1456 else 1068 else
1503 1115
1504 /* zero-terminate, for readlink */ 1116 /* zero-terminate, for readlink */
1505 res [len + 1] = 0; 1117 res [len + 1] = 0;
1506 1118
1507 /* now check if it's a symlink */ 1119 /* now check if it's a symlink */
1508 linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); 1120 linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX);
1509 1121
1510 if (linklen < 0) 1122 if (linklen < 0)
1511 { 1123 {
1512 if (errno != EINVAL) 1124 if (errno != EINVAL)
1513 return -1; 1125 return -1;
1519 { 1131 {
1520 /* yay, it was a symlink - build new path in tmp2 */ 1132 /* yay, it was a symlink - build new path in tmp2 */
1521 int rellen = strlen (rel); 1133 int rellen = strlen (rel);
1522 1134
1523 errno = ENAMETOOLONG; 1135 errno = ENAMETOOLONG;
1524 if (linklen + 1 + rellen >= PATH_MAX) 1136 if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */
1525 return -1; 1137 return -1;
1526 1138
1527 errno = ELOOP; 1139 errno = ELOOP;
1528 if (!--symlinks) 1140 if (!--symlinks)
1529 return -1; 1141 return -1;
1543 /* special case for the lone root path */ 1155 /* special case for the lone root path */
1544 if (res == tmpbuf->ptr) 1156 if (res == tmpbuf->ptr)
1545 *res++ = '/'; 1157 *res++ = '/';
1546 1158
1547 return res - (char *)tmpbuf->ptr; 1159 return res - (char *)tmpbuf->ptr;
1160#endif
1548} 1161}
1549 1162
1550static signed char 1163static signed char
1551eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1164eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1552{ 1165{
1792 return; 1405 return;
1793 1406
1794 dirp = fdopendir (fd); 1407 dirp = fdopendir (fd);
1795 1408
1796 if (!dirp) 1409 if (!dirp)
1797 close (fd); 1410 silent_close (fd);
1798 } 1411 }
1799 else 1412 else
1800 dirp = opendir (req->ptr1); 1413 dirp = opendir (req->ptr1);
1801 #else 1414 #else
1802 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); 1415 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
2011#if !HAVE_AT 1624#if !HAVE_AT
2012 1625
2013/* a bit like realpath, but usually faster because it doesn'T have to return */ 1626/* a bit like realpath, but usually faster because it doesn'T have to return */
2014/* an absolute or canonical path */ 1627/* an absolute or canonical path */
2015static const char * 1628static const char *
2016wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1629wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2017{ 1630{
2018 if (!wd || *path == '/') 1631 if (!wd || *path == '/')
2019 return path; 1632 return path;
2020 1633
2021 if (path [0] == '.' && !path [1]) 1634 if (path [0] == '.' && !path [1])
2023 1636
2024 { 1637 {
2025 int l1 = wd->len; 1638 int l1 = wd->len;
2026 int l2 = strlen (path); 1639 int l2 = strlen (path);
2027 1640
2028 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1641 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
2029 1642
2030 memcpy (res, wd->str, l1); 1643 memcpy (res, wd->str, l1);
2031 res [l1] = '/'; 1644 res [l1] = '/';
2032 memcpy (res + l1 + 1, path, l2 + 1); 1645 memcpy (res + l1 + 1, path, l2 + 1);
2033 1646
2036} 1649}
2037 1650
2038#endif 1651#endif
2039 1652
2040static eio_wd 1653static eio_wd
2041eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1654eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2042{ 1655{
2043 int fd; 1656 int fd;
2044 eio_wd res; 1657 eio_wd res;
2045 int len = eio__realpath (tmpbuf, wd, path); 1658 int len = eio__realpath (tmpbuf, wd, path);
2046 1659
2068} 1681}
2069 1682
2070eio_wd 1683eio_wd
2071eio_wd_open_sync (eio_wd wd, const char *path) 1684eio_wd_open_sync (eio_wd wd, const char *path)
2072{ 1685{
2073 struct tmpbuf tmpbuf = { 0 }; 1686 struct etp_tmpbuf tmpbuf = { };
2074 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1687 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2075 free (tmpbuf.ptr); 1688 free (tmpbuf.ptr);
2076 1689
2077 return wd; 1690 return wd;
2078} 1691}
2081eio_wd_close_sync (eio_wd wd) 1694eio_wd_close_sync (eio_wd wd)
2082{ 1695{
2083 if (wd != EIO_INVALID_WD && wd != EIO_CWD) 1696 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
2084 { 1697 {
2085 #if HAVE_AT 1698 #if HAVE_AT
2086 close (wd->fd); 1699 eio__close (wd->fd);
2087 #endif 1700 #endif
2088 free (wd); 1701 free (wd);
2089 } 1702 }
2090} 1703}
2091 1704
2092#if HAVE_AT 1705#if HAVE_AT
2093 1706
1707static int
1708eio__renameat2 (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags)
1709{
1710#if HAVE_RENAMEAT2
1711 return syscall (SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags);
1712#else
1713 if (flags)
1714 return EIO_ENOSYS ();
1715
1716 return renameat (olddirfd, oldpath, newdirfd, newpath);
1717#endif
1718}
1719
2094/* they forgot these */ 1720/* they forgot these */
2095 1721
2096static int 1722static int
2097eio__truncateat (int dirfd, const char *path, off_t length) 1723eio__truncateat (int dirfd, const char *path, off_t length)
2098{ 1724{
2101 1727
2102 if (fd < 0) 1728 if (fd < 0)
2103 return fd; 1729 return fd;
2104 1730
2105 res = ftruncate (fd, length); 1731 res = ftruncate (fd, length);
2106 close (fd); 1732 silent_close (fd);
2107 return res; 1733 return res;
2108} 1734}
2109 1735
2110static int 1736static int
2111eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) 1737eio__statvfsat (int dirfd, const char *path, struct statvfs *buf)
2115 1741
2116 if (fd < 0) 1742 if (fd < 0)
2117 return fd; 1743 return fd;
2118 1744
2119 res = fstatvfs (fd, buf); 1745 res = fstatvfs (fd, buf);
2120 close (fd); 1746 silent_close (fd);
2121 return res; 1747 return res;
2122
2123} 1748}
2124 1749
2125#endif 1750#endif
2126 1751
2127/*****************************************************************************/ 1752/*****************************************************************************/
2128 1753
2129#define ALLOC(len) \ 1754#define ALLOC(len) \
2130 if (!req->ptr2) \ 1755 if (!req->ptr2) \
2131 { \ 1756 { \
2132 X_LOCK (wrklock); \ 1757 X_LOCK (EIO_POOL->wrklock); \
2133 req->flags |= EIO_FLAG_PTR2_FREE; \ 1758 req->flags |= EIO_FLAG_PTR2_FREE; \
2134 X_UNLOCK (wrklock); \ 1759 X_UNLOCK (EIO_POOL->wrklock); \
2135 req->ptr2 = malloc (len); \ 1760 req->ptr2 = malloc (len); \
2136 if (!req->ptr2) \ 1761 if (!req->ptr2) \
2137 { \ 1762 { \
2138 errno = ENOMEM; \ 1763 errno = ENOMEM; \
2139 req->result = -1; \ 1764 req->result = -1; \
2140 break; \ 1765 goto alloc_fail; \
2141 } \ 1766 } \
2142 } 1767 }
2143 1768
2144static void ecb_noinline ecb_cold 1769/*****************************************************************************/
2145etp_proc_init (void)
2146{
2147#if HAVE_PRCTL_SET_NAME
2148 /* provide a more sensible "thread name" */
2149 char name[16 + 1];
2150 const int namelen = sizeof (name) - 1;
2151 int len;
2152 1770
2153 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); 1771static void
2154 name [namelen] = 0; 1772eio__slurp (int fd, eio_req *req)
2155 len = strlen (name);
2156 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2157 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2158#endif
2159}
2160
2161X_THREAD_PROC (etp_proc)
2162{ 1773{
2163 ETP_REQ *req; 1774 req->result = fd;
2164 struct timespec ts;
2165 etp_worker *self = (etp_worker *)thr_arg;
2166 1775
2167 etp_proc_init (); 1776 if (fd < 0)
1777 return;
2168 1778
2169 /* try to distribute timeouts somewhat evenly */ 1779 if (req->offs < 0 || !req->size) /* do we need the size? */
2170 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2171
2172 for (;;)
2173 { 1780 {
2174 ts.tv_sec = 0; 1781 off_t size = lseek (fd, 0, SEEK_END);
2175 1782
2176 X_LOCK (reqlock);
2177
2178 for (;;)
2179 {
2180 req = reqq_shift (&req_queue);
2181
2182 if (req)
2183 break;
2184
2185 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2186 {
2187 X_UNLOCK (reqlock);
2188 X_LOCK (wrklock);
2189 --started;
2190 X_UNLOCK (wrklock);
2191 goto quit;
2192 }
2193
2194 ++idle;
2195
2196 if (idle <= max_idle)
2197 /* we are allowed to idle, so do so without any timeout */
2198 X_COND_WAIT (reqwait, reqlock);
2199 else
2200 {
2201 /* initialise timeout once */
2202 if (!ts.tv_sec)
2203 ts.tv_sec = time (0) + idle_timeout;
2204
2205 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2206 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2207 }
2208
2209 --idle;
2210 }
2211
2212 --nready;
2213
2214 X_UNLOCK (reqlock);
2215
2216 if (req->type < 0) 1783 if (req->offs < 0)
2217 goto quit; 1784 req->offs += size;
2218 1785
2219 ETP_EXECUTE (self, req); 1786 if (!req->size)
2220 1787 req->size = size - req->offs;
2221 X_LOCK (reslock);
2222
2223 ++npending;
2224
2225 if (!reqq_push (&res_queue, req) && want_poll_cb)
2226 want_poll_cb ();
2227
2228 etp_worker_clear (self);
2229
2230 X_UNLOCK (reslock);
2231 } 1788 }
2232 1789
2233quit: 1790 ALLOC (req->size);
2234 free (req); 1791 req->result = pread (fd, req->ptr2, req->size, req->offs);
2235 1792
2236 X_LOCK (wrklock); 1793 silent_close (fd);
2237 etp_worker_free (self);
2238 X_UNLOCK (wrklock);
2239 1794
2240 return 0; 1795alloc_fail:
1796 ;
2241} 1797}
2242
2243/*****************************************************************************/
2244 1798
2245int ecb_cold 1799int ecb_cold
2246eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1800eio_init (void (*want_poll)(void), void (*done_poll)(void))
2247{ 1801{
2248 return etp_init (want_poll, done_poll); 1802 eio_want_poll_cb = want_poll;
1803 eio_done_poll_cb = done_poll;
1804
1805 return etp_init (EIO_POOL, 0, 0, 0);
2249} 1806}
2250 1807
2251ecb_inline void 1808ecb_inline void
2252eio_api_destroy (eio_req *req) 1809eio_api_destroy (eio_req *req)
2253{ 1810{
2327 : read (req->int1, req->ptr2, req->size); break; 1884 : read (req->int1, req->ptr2, req->size); break;
2328 case EIO_WRITE: req->result = req->offs >= 0 1885 case EIO_WRITE: req->result = req->offs >= 0
2329 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1886 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
2330 : write (req->int1, req->ptr2, req->size); break; 1887 : write (req->int1, req->ptr2, req->size); break;
2331 1888
1889 case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break;
1890 case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break;
1891
2332 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 1892 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
2333 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; 1893 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
2334 1894
2335#if HAVE_AT 1895#if HAVE_AT
2336 1896
2340 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; 1900 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break;
2341 case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; 1901 case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break;
2342 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; 1902 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break;
2343 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; 1903 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break;
2344 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; 1904 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
1905 case EIO_SLURP: eio__slurp ( openat (dirfd, req->ptr1, O_RDONLY | O_CLOEXEC), req); break;
2345 1906
2346 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1907 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2347 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */ 1908 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
2348 req->result = req->wd && SINGLEDOT (req->ptr1) 1909 req->result = req->wd && SINGLEDOT (req->ptr1)
2349 ? rmdir (req->wd->str) 1910 ? rmdir (req->wd->str)
2350 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 1911 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2351 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 1912 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
1913 case EIO_RENAME: req->result = eio__renameat2 (
1914 dirfd,
2352 case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */ 1915 /* complications arise because "." cannot be renamed, so we might have to expand */
2353 req->result = req->wd && SINGLEDOT (req->ptr1) 1916 req->wd && SINGLEDOT (req->ptr1) ? req->wd->str : req->ptr1,
2354 ? rename (req->wd->str, req->ptr2) 1917 WD2FD ((eio_wd)req->int3),
2355 : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 1918 req->ptr2,
1919 req->int2
1920 );
1921 break;
2356 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; 1922 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; 1923 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; 1924 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);
2360 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2361 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1925 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2362 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; 1926 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
1927 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
1928 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX);
1929 if (req->result == EIO_PATH_MAX)
1930 {
1931 req->result = -1;
1932 errno = ENAMETOOLONG;
1933 }
1934 break;
2363 case EIO_UTIME: 1935 case EIO_UTIME:
2364 case EIO_FUTIME: 1936 case EIO_FUTIME:
2365 { 1937 {
2366 struct timespec ts[2]; 1938 struct timespec ts[2];
2367 struct timespec *times; 1939 struct timespec *times;
2392 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 1964 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2393 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; 1965 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break;
2394 case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break; 1966 case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break;
2395 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; 1967 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break;
2396 case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; 1968 case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break;
1969 case EIO_SLURP: eio__slurp ( open (path , O_RDONLY | O_CLOEXEC), req); break;
2397 1970
2398 case EIO_UNLINK: req->result = unlink (path ); break; 1971 case EIO_UNLINK: req->result = unlink (path ); break;
2399 case EIO_RMDIR: req->result = rmdir (path ); break; 1972 case EIO_RMDIR: req->result = rmdir (path ); break;
2400 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; 1973 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break;
2401 case EIO_RENAME: req->result = rename (path , req->ptr2); break; 1974 case EIO_RENAME: req->result = req->int2 ? EIO_ENOSYS () : rename (path, req->ptr2); break;
2402 case EIO_LINK: req->result = link (path , req->ptr2); break; 1975 case EIO_LINK: req->result = link (path , req->ptr2); break;
2403 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; 1976 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break;
2404 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; 1977 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break;
2405 case EIO_READLINK: ALLOC (PATH_MAX);
2406 req->result = readlink (path, req->ptr2, PATH_MAX); break;
2407 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1978 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2408 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; 1979 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break;
1980 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
1981 req->result = readlink (path, req->ptr2, EIO_PATH_MAX);
1982 if (req->result == EIO_PATH_MAX)
1983 {
1984 req->result = -1;
1985 errno = ENAMETOOLONG;
1986 }
1987 break;
2409 1988
2410 case EIO_UTIME: 1989 case EIO_UTIME:
2411 case EIO_FUTIME: 1990 case EIO_FUTIME:
2412 { 1991 {
2413 struct timeval tv[2]; 1992 struct timeval tv[2];
2448 2027
2449 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; 2028 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
2450 case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; 2029 case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break;
2451 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; 2030 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
2452 2031
2453 case EIO_CLOSE: req->result = close (req->int1); break; 2032 case EIO_CLOSE: req->result = eio__close (req->int1); break;
2454 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; 2033 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
2455 case EIO_SYNC: req->result = 0; sync (); break; 2034 case EIO_SYNC: req->result = 0; sync (); break;
2456 case EIO_FSYNC: req->result = fsync (req->int1); break; 2035 case EIO_FSYNC: req->result = fsync (req->int1); break;
2457 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 2036 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
2458 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; 2037 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break;
2478 req->result = select (0, 0, 0, 0, &tv); 2057 req->result = select (0, 0, 0, 0, &tv);
2479 } 2058 }
2480#endif 2059#endif
2481 break; 2060 break;
2482 2061
2062#if 0
2483 case EIO_GROUP: 2063 case EIO_GROUP:
2484 abort (); /* handled in eio_request */ 2064 abort (); /* handled in eio_request */
2065#endif
2485 2066
2486 case EIO_NOP: 2067 case EIO_NOP:
2487 req->result = 0; 2068 req->result = 0;
2488 break; 2069 break;
2489 2070
2494 default: 2075 default:
2495 req->result = EIO_ENOSYS (); 2076 req->result = EIO_ENOSYS ();
2496 break; 2077 break;
2497 } 2078 }
2498 2079
2080alloc_fail:
2499 req->errorno = errno; 2081 req->errorno = errno;
2500} 2082}
2501 2083
2502#ifndef EIO_NO_WRAPPERS 2084#ifndef EIO_NO_WRAPPERS
2503 2085
2592} 2174}
2593 2175
2594eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2176eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2595{ 2177{
2596 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2178 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2179}
2180
2181eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data)
2182{
2183 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND;
2184}
2185
2186eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data)
2187{
2188 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND;
2597} 2189}
2598 2190
2599eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) 2191eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
2600{ 2192{
2601 REQ (EIO_FSTAT); req->int1 = fd; SEND; 2193 REQ (EIO_FSTAT); req->int1 = fd; SEND;
2746eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2338eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2747{ 2339{
2748 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2340 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2749} 2341}
2750 2342
2343eio_req *eio_slurp (const char *path, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2344{
2345 REQ (EIO_SLURP); PATH; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2346}
2347
2751eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) 2348eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2752{ 2349{
2753 REQ (EIO_CUSTOM); req->feed = execute; SEND; 2350 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2754} 2351}
2755 2352
2789void 2386void
2790eio_grp_add (eio_req *grp, eio_req *req) 2387eio_grp_add (eio_req *grp, eio_req *req)
2791{ 2388{
2792 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2389 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2793 2390
2794 grp->flags |= EIO_FLAG_GROUPADD; 2391 grp->flags |= ETP_FLAG_GROUPADD;
2795 2392
2796 ++grp->size; 2393 ++grp->size;
2797 req->grp = grp; 2394 req->grp = grp;
2798 2395
2799 req->grp_prev = 0; 2396 req->grp_prev = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines