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

Comparing libeio/eio.c (file contents):
Revision 1.124 by root, Thu Oct 11 05:01:56 2012 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 *
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,
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 <sys/statvfs.h>
237 #include <unistd.h> 239 #include <unistd.h>
238 #include <signal.h> 240 #include <signal.h>
239 #include <dirent.h> 241 #include <dirent.h>
242
243 #ifdef ANDROID
244 #include <sys/vfs.h>
245 #define statvfs statfs
246 #define fstatvfs fstatfs
247 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */
248 #else
249 #include <sys/statvfs.h>
250 #endif
240 251
241 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 252 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
242 #include <sys/mman.h> 253 #include <sys/mman.h>
243 #endif 254 #endif
244 255
272# include <utime.h> 283# include <utime.h>
273#endif 284#endif
274 285
275#if HAVE_SYS_SYSCALL_H 286#if HAVE_SYS_SYSCALL_H
276# include <sys/syscall.h> 287# include <sys/syscall.h>
277#endif
278
279#if HAVE_SYS_PRCTL_H
280# include <sys/prctl.h>
281#endif 288#endif
282 289
283#if HAVE_SENDFILE 290#if HAVE_SENDFILE
284# if __linux 291# if __linux
285# include <sys/sendfile.h> 292# include <sys/sendfile.h>
293# else 300# else
294# error sendfile support requested but not available 301# error sendfile support requested but not available
295# endif 302# endif
296#endif 303#endif
297 304
305#if HAVE_RENAMEAT2
306# include <sys/syscall.h>
307# include <linux/fs.h>
308#endif
309
298#ifndef D_TYPE 310#ifndef D_TYPE
299# define D_TYPE(de) 0 311# define D_TYPE(de) 0
300#endif 312#endif
301#ifndef D_INO 313#ifndef D_INO
302# define D_INO(de) 0 314# define D_INO(de) 0
310# define NAME_MAX 4096 322# define NAME_MAX 4096
311#endif 323#endif
312 324
313/* used for readlink etc. */ 325/* used for readlink etc. */
314#ifndef PATH_MAX 326#ifndef PATH_MAX
315# define PATH_MAX 4096 327# define PATH_MAX 0
316#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)
317 339
318/* buffer size for various temporary buffers */ 340/* buffer size for various temporary buffers */
319#define EIO_BUFSIZE 65536 341#define EIO_BUFSIZE 65536
320 342
321#define dBUF \ 343#define dBUF \
322 char *eio_buf = malloc (EIO_BUFSIZE); \ 344 char *eio_buf = malloc (EIO_BUFSIZE); \
323 errno = ENOMEM; \ 345 errno = ENOMEM; \
324 if (!eio_buf) \ 346 if (!eio_buf) \
325 return -1 347 return -1
326 348
327#define FUBd \ 349#define FUBd \
328 free (eio_buf) 350 free (eio_buf)
329 351
330#define EIO_TICKS ((1000000 + 1023) >> 10)
331
332/*****************************************************************************/ 352/*****************************************************************************/
333 353
334struct tmpbuf
335{
336 void *ptr;
337 int len;
338};
339
340static void *
341tmpbuf_get (struct tmpbuf *buf, int len)
342{
343 if (buf->len < len)
344 {
345 free (buf->ptr);
346 buf->ptr = malloc (buf->len = len);
347 }
348
349 return buf->ptr;
350}
351
352struct tmpbuf; 354struct etp_tmpbuf;
353 355
354#if _POSIX_VERSION >= 200809L 356#if _POSIX_VERSION >= 200809L
355 #define HAVE_AT 1 357 #define HAVE_AT 1
356 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 358 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
357 #ifndef O_SEARCH 359 #ifndef O_SEARCH
358 #define O_SEARCH O_RDONLY 360 #define O_SEARCH O_RDONLY
359 #endif 361 #endif
360#else 362#else
361 #define HAVE_AT 0 363 #define HAVE_AT 0
362 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);
363#endif 365#endif
364 366
365struct eio_pwd 367struct eio_pwd
366{ 368{
367#if HAVE_AT 369#if HAVE_AT
374/*****************************************************************************/ 376/*****************************************************************************/
375 377
376#define ETP_PRI_MIN EIO_PRI_MIN 378#define ETP_PRI_MIN EIO_PRI_MIN
377#define ETP_PRI_MAX EIO_PRI_MAX 379#define ETP_PRI_MAX EIO_PRI_MAX
378 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
379struct etp_worker; 391struct etp_worker;
380
381#define ETP_REQ eio_req 392#define ETP_REQ eio_req
382#define ETP_DESTROY(req) eio_destroy (req) 393#define ETP_DESTROY(req) eio_destroy (req)
383static int eio_finish (eio_req *req); 394static int eio_finish (eio_req *req);
384#define ETP_FINISH(req) eio_finish (req) 395#define ETP_FINISH(req) eio_finish (req)
385static void eio_execute (struct etp_worker *self, eio_req *req); 396static void eio_execute (struct etp_worker *self, eio_req *req);
386#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 397#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
387 398
388/*****************************************************************************/ 399#include "etp.c"
389 400
390#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 401static struct etp_pool eio_pool;
391 402#define EIO_POOL (&eio_pool)
392/* calculate time difference in ~1/EIO_TICKS of a second */
393ecb_inline int
394tvdiff (struct timeval *tv1, struct timeval *tv2)
395{
396 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
397 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
398}
399
400static unsigned int started, idle, wanted = 4;
401
402static void (*want_poll_cb) (void);
403static void (*done_poll_cb) (void);
404
405static unsigned int max_poll_time; /* reslock */
406static unsigned int max_poll_reqs; /* reslock */
407
408static unsigned int nreqs; /* reqlock */
409static unsigned int nready; /* reqlock */
410static unsigned int npending; /* reqlock */
411static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
412static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
413
414static xmutex_t wrklock;
415static xmutex_t reslock;
416static xmutex_t reqlock;
417static xcond_t reqwait;
418
419typedef struct etp_worker
420{
421 struct tmpbuf tmpbuf;
422
423 /* locked by wrklock */
424 struct etp_worker *prev, *next;
425
426 xthread_t tid;
427
428#ifdef ETP_WORKER_COMMON
429 ETP_WORKER_COMMON
430#endif
431} etp_worker;
432
433static etp_worker wrk_first; /* NOT etp */
434
435#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
436#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
437
438/* worker threads management */
439
440static void
441etp_worker_clear (etp_worker *wrk)
442{
443}
444
445static void ecb_cold
446etp_worker_free (etp_worker *wrk)
447{
448 free (wrk->tmpbuf.ptr);
449
450 wrk->next->prev = wrk->prev;
451 wrk->prev->next = wrk->next;
452
453 free (wrk);
454}
455
456static unsigned int
457etp_nreqs (void)
458{
459 int retval;
460 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
461 retval = nreqs;
462 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
463 return retval;
464}
465
466static unsigned int
467etp_nready (void)
468{
469 unsigned int retval;
470
471 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
472 retval = nready;
473 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
474
475 return retval;
476}
477
478static unsigned int
479etp_npending (void)
480{
481 unsigned int retval;
482
483 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
484 retval = npending;
485 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
486
487 return retval;
488}
489
490static unsigned int
491etp_nthreads (void)
492{
493 unsigned int retval;
494
495 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
496 retval = started;
497 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
498
499 return retval;
500}
501
502/*
503 * a somewhat faster data structure might be nice, but
504 * with 8 priorities this actually needs <20 insns
505 * per shift, the most expensive operation.
506 */
507typedef struct {
508 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
509 int size;
510} etp_reqq;
511
512static etp_reqq req_queue;
513static etp_reqq res_queue;
514
515static void ecb_noinline ecb_cold
516reqq_init (etp_reqq *q)
517{
518 int pri;
519
520 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
521 q->qs[pri] = q->qe[pri] = 0;
522
523 q->size = 0;
524}
525
526static int ecb_noinline
527reqq_push (etp_reqq *q, ETP_REQ *req)
528{
529 int pri = req->pri;
530 req->next = 0;
531
532 if (q->qe[pri])
533 {
534 q->qe[pri]->next = req;
535 q->qe[pri] = req;
536 }
537 else
538 q->qe[pri] = q->qs[pri] = req;
539
540 return q->size++;
541}
542
543static ETP_REQ * ecb_noinline
544reqq_shift (etp_reqq *q)
545{
546 int pri;
547
548 if (!q->size)
549 return 0;
550
551 --q->size;
552
553 for (pri = ETP_NUM_PRI; pri--; )
554 {
555 eio_req *req = q->qs[pri];
556
557 if (req)
558 {
559 if (!(q->qs[pri] = (eio_req *)req->next))
560 q->qe[pri] = 0;
561
562 return req;
563 }
564 }
565
566 abort ();
567}
568
569static int ecb_cold
570etp_init (void (*want_poll)(void), void (*done_poll)(void))
571{
572 X_MUTEX_CREATE (wrklock);
573 X_MUTEX_CREATE (reslock);
574 X_MUTEX_CREATE (reqlock);
575 X_COND_CREATE (reqwait);
576
577 reqq_init (&req_queue);
578 reqq_init (&res_queue);
579
580 wrk_first.next =
581 wrk_first.prev = &wrk_first;
582
583 started = 0;
584 idle = 0;
585 nreqs = 0;
586 nready = 0;
587 npending = 0;
588
589 want_poll_cb = want_poll;
590 done_poll_cb = done_poll;
591
592 return 0;
593}
594
595X_THREAD_PROC (etp_proc);
596
597static void ecb_cold
598etp_start_thread (void)
599{
600 etp_worker *wrk = calloc (1, sizeof (etp_worker));
601
602 /*TODO*/
603 assert (("unable to allocate worker thread data", wrk));
604
605 X_LOCK (wrklock);
606
607 if (xthread_create (&wrk->tid, etp_proc, (void *)wrk))
608 {
609 wrk->prev = &wrk_first;
610 wrk->next = wrk_first.next;
611 wrk_first.next->prev = wrk;
612 wrk_first.next = wrk;
613 ++started;
614 }
615 else
616 free (wrk);
617
618 X_UNLOCK (wrklock);
619}
620
621static void
622etp_maybe_start_thread (void)
623{
624 if (ecb_expect_true (etp_nthreads () >= wanted))
625 return;
626
627 /* todo: maybe use idle here, but might be less exact */
628 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
629 return;
630
631 etp_start_thread ();
632}
633
634static void ecb_cold
635etp_end_thread (void)
636{
637 eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */
638
639 req->type = -1;
640 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
641
642 X_LOCK (reqlock);
643 reqq_push (&req_queue, req);
644 X_COND_SIGNAL (reqwait);
645 X_UNLOCK (reqlock);
646
647 X_LOCK (wrklock);
648 --started;
649 X_UNLOCK (wrklock);
650}
651
652static int
653etp_poll (void)
654{
655 unsigned int maxreqs;
656 unsigned int maxtime;
657 struct timeval tv_start, tv_now;
658
659 X_LOCK (reslock);
660 maxreqs = max_poll_reqs;
661 maxtime = max_poll_time;
662 X_UNLOCK (reslock);
663
664 if (maxtime)
665 gettimeofday (&tv_start, 0);
666
667 for (;;)
668 {
669 ETP_REQ *req;
670
671 etp_maybe_start_thread ();
672
673 X_LOCK (reslock);
674 req = reqq_shift (&res_queue);
675
676 if (req)
677 {
678 --npending;
679
680 if (!res_queue.size && done_poll_cb)
681 done_poll_cb ();
682 }
683
684 X_UNLOCK (reslock);
685
686 if (!req)
687 return 0;
688
689 X_LOCK (reqlock);
690 --nreqs;
691 X_UNLOCK (reqlock);
692
693 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
694 {
695 req->int1 = 1; /* mark request as delayed */
696 continue;
697 }
698 else
699 {
700 int res = ETP_FINISH (req);
701 if (ecb_expect_false (res))
702 return res;
703 }
704
705 if (ecb_expect_false (maxreqs && !--maxreqs))
706 break;
707
708 if (maxtime)
709 {
710 gettimeofday (&tv_now, 0);
711
712 if (tvdiff (&tv_start, &tv_now) >= maxtime)
713 break;
714 }
715 }
716
717 errno = EAGAIN;
718 return -1;
719}
720
721static void
722etp_cancel (ETP_REQ *req)
723{
724 req->cancelled = 1;
725
726 eio_grp_cancel (req);
727}
728
729static void
730etp_submit (ETP_REQ *req)
731{
732 req->pri -= ETP_PRI_MIN;
733
734 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
735 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
736
737 if (ecb_expect_false (req->type == EIO_GROUP))
738 {
739 /* I hope this is worth it :/ */
740 X_LOCK (reqlock);
741 ++nreqs;
742 X_UNLOCK (reqlock);
743
744 X_LOCK (reslock);
745
746 ++npending;
747
748 if (!reqq_push (&res_queue, req) && want_poll_cb)
749 want_poll_cb ();
750
751 X_UNLOCK (reslock);
752 }
753 else
754 {
755 X_LOCK (reqlock);
756 ++nreqs;
757 ++nready;
758 reqq_push (&req_queue, req);
759 X_COND_SIGNAL (reqwait);
760 X_UNLOCK (reqlock);
761
762 etp_maybe_start_thread ();
763 }
764}
765
766static void ecb_cold
767etp_set_max_poll_time (double nseconds)
768{
769 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
770 max_poll_time = nseconds * EIO_TICKS;
771 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
772}
773
774static void ecb_cold
775etp_set_max_poll_reqs (unsigned int maxreqs)
776{
777 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
778 max_poll_reqs = maxreqs;
779 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
780}
781
782static void ecb_cold
783etp_set_max_idle (unsigned int nthreads)
784{
785 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
786 max_idle = nthreads;
787 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
788}
789
790static void ecb_cold
791etp_set_idle_timeout (unsigned int seconds)
792{
793 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
794 idle_timeout = seconds;
795 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
796}
797
798static void ecb_cold
799etp_set_min_parallel (unsigned int nthreads)
800{
801 if (wanted < nthreads)
802 wanted = nthreads;
803}
804
805static void ecb_cold
806etp_set_max_parallel (unsigned int nthreads)
807{
808 if (wanted > nthreads)
809 wanted = nthreads;
810
811 while (started > wanted)
812 etp_end_thread ();
813}
814 403
815/*****************************************************************************/ 404/*****************************************************************************/
816 405
817static void 406static void
818grp_try_feed (eio_req *grp) 407grp_try_feed (eio_req *grp)
819{ 408{
820 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 409 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
821 { 410 {
822 grp->flags &= ~EIO_FLAG_GROUPADD; 411 grp->flags &= ~ETP_FLAG_GROUPADD;
823 412
824 EIO_FEED (grp); 413 EIO_FEED (grp);
825 414
826 /* stop if no progress has been made */ 415 /* stop if no progress has been made */
827 if (!(grp->flags & EIO_FLAG_GROUPADD)) 416 if (!(grp->flags & ETP_FLAG_GROUPADD))
828 { 417 {
829 grp->feed = 0; 418 grp->feed = 0;
830 break; 419 break;
831 } 420 }
832 } 421 }
839 428
840 /* call feeder, if applicable */ 429 /* call feeder, if applicable */
841 grp_try_feed (grp); 430 grp_try_feed (grp);
842 431
843 /* finish, if done */ 432 /* finish, if done */
844 if (!grp->size && grp->int1) 433 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
845 return eio_finish (grp); 434 return eio_finish (grp);
846 else 435 else
847 return 0; 436 return 0;
848} 437}
849 438
885} 474}
886 475
887void 476void
888eio_grp_cancel (eio_req *grp) 477eio_grp_cancel (eio_req *grp)
889{ 478{
890 for (grp = grp->grp_first; grp; grp = grp->grp_next) 479 etp_grp_cancel (EIO_POOL, grp);
891 eio_cancel (grp);
892} 480}
893 481
894void 482void
895eio_cancel (eio_req *req) 483eio_cancel (eio_req *req)
896{ 484{
897 etp_cancel (req); 485 etp_cancel (EIO_POOL, req);
898} 486}
899 487
900void 488void
901eio_submit (eio_req *req) 489eio_submit (eio_req *req)
902{ 490{
903 etp_submit (req); 491 etp_submit (EIO_POOL, req);
904} 492}
905 493
906unsigned int 494unsigned int
907eio_nreqs (void) 495eio_nreqs (void)
908{ 496{
909 return etp_nreqs (); 497 return etp_nreqs (EIO_POOL);
910} 498}
911 499
912unsigned int 500unsigned int
913eio_nready (void) 501eio_nready (void)
914{ 502{
915 return etp_nready (); 503 return etp_nready (EIO_POOL);
916} 504}
917 505
918unsigned int 506unsigned int
919eio_npending (void) 507eio_npending (void)
920{ 508{
921 return etp_npending (); 509 return etp_npending (EIO_POOL);
922} 510}
923 511
924unsigned int ecb_cold 512unsigned int ecb_cold
925eio_nthreads (void) 513eio_nthreads (void)
926{ 514{
927 return etp_nthreads (); 515 return etp_nthreads (EIO_POOL);
928} 516}
929 517
930void ecb_cold 518void ecb_cold
931eio_set_max_poll_time (double nseconds) 519eio_set_max_poll_time (double nseconds)
932{ 520{
933 etp_set_max_poll_time (nseconds); 521 etp_set_max_poll_time (EIO_POOL, nseconds);
934} 522}
935 523
936void ecb_cold 524void ecb_cold
937eio_set_max_poll_reqs (unsigned int maxreqs) 525eio_set_max_poll_reqs (unsigned int maxreqs)
938{ 526{
939 etp_set_max_poll_reqs (maxreqs); 527 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
940} 528}
941 529
942void ecb_cold 530void ecb_cold
943eio_set_max_idle (unsigned int nthreads) 531eio_set_max_idle (unsigned int nthreads)
944{ 532{
945 etp_set_max_idle (nthreads); 533 etp_set_max_idle (EIO_POOL, nthreads);
946} 534}
947 535
948void ecb_cold 536void ecb_cold
949eio_set_idle_timeout (unsigned int seconds) 537eio_set_idle_timeout (unsigned int seconds)
950{ 538{
951 etp_set_idle_timeout (seconds); 539 etp_set_idle_timeout (EIO_POOL, seconds);
952} 540}
953 541
954void ecb_cold 542void ecb_cold
955eio_set_min_parallel (unsigned int nthreads) 543eio_set_min_parallel (unsigned int nthreads)
956{ 544{
957 etp_set_min_parallel (nthreads); 545 etp_set_min_parallel (EIO_POOL, nthreads);
958} 546}
959 547
960void ecb_cold 548void ecb_cold
961eio_set_max_parallel (unsigned int nthreads) 549eio_set_max_parallel (unsigned int nthreads)
962{ 550{
963 etp_set_max_parallel (nthreads); 551 etp_set_max_parallel (EIO_POOL, nthreads);
964} 552}
965 553
966int eio_poll (void) 554int eio_poll (void)
967{ 555{
968 return etp_poll (); 556 return etp_poll (EIO_POOL);
969} 557}
970 558
971/*****************************************************************************/ 559/*****************************************************************************/
972/* 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}
973 576
974#ifndef HAVE_UTIMES 577#ifndef HAVE_UTIMES
975 578
976# undef utimes 579# undef utimes
977# define utimes(path,times) eio__utimes (path, times) 580# define utimes(path,times) eio__utimes (path, times)
1356 intptr_t end = addr + len; 959 intptr_t end = addr + len;
1357 intptr_t page = eio_pagesize (); 960 intptr_t page = eio_pagesize ();
1358 961
1359 if (addr < end) 962 if (addr < end)
1360 if (flags & EIO_MT_MODIFY) /* modify */ 963 if (flags & EIO_MT_MODIFY) /* modify */
1361 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));
1362 else 965 else
1363 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));
1364 } 967 }
1365 968
1366 return 0; 969 return 0;
1367} 970}
1368 971
1383 req->result = req->offs == (off_t)-1 ? -1 : 0; 986 req->result = req->offs == (off_t)-1 ? -1 : 0;
1384} 987}
1385 988
1386/* 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 */
1387static int 990static int
1388eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 991eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1389{ 992{
993 char *res;
1390 const char *rel = path; 994 const char *rel = path;
1391 char *res;
1392 char *tmp1, *tmp2; 995 char *tmp1, *tmp2;
1393#if SYMLOOP_MAX > 32 996#if SYMLOOP_MAX > 32
1394 int symlinks = SYMLOOP_MAX; 997 int symlinks = SYMLOOP_MAX;
1395#else 998#else
1396 int symlinks = 32; 999 int symlinks = 32;
1402 1005
1403 errno = ENOENT; 1006 errno = ENOENT;
1404 if (!*rel) 1007 if (!*rel)
1405 return -1; 1008 return -1;
1406 1009
1407 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
1408 tmp1 = res + PATH_MAX; 1028 tmp1 = res + EIO_PATH_MAX;
1409 tmp2 = tmp1 + PATH_MAX; 1029 tmp2 = tmp1 + EIO_PATH_MAX;
1410 1030
1411#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 */
1412#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1032#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1413 /* on linux we may be able to ask the kernel */ 1033 /* on linux we may be able to ask the kernel */
1414 { 1034 {
1415 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);
1416 1036
1417 if (fd >= 0) 1037 if (fd >= 0)
1418 { 1038 {
1419 sprintf (tmp1, "/proc/self/fd/%d", fd); 1039 sprintf (tmp1, "/proc/self/fd/%d", fd);
1420 req->result = readlink (tmp1, res, PATH_MAX); 1040 req->result = readlink (tmp1, res, EIO_PATH_MAX);
1421 close (fd);
1422
1423 /* 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 */
1042 eio__close (fd);
1424 1043
1425 if (req->result > 0) 1044 if (req->result > 0)
1426 goto done; 1045 goto done;
1427 } 1046 }
1428 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)
1429 return; 1048 return -1;
1430 } 1049 }
1431#endif 1050#endif
1432#endif 1051#endif
1433 1052
1434 if (*rel != '/') 1053 if (*rel != '/')
1439 if (wd == EIO_INVALID_WD) 1058 if (wd == EIO_INVALID_WD)
1440 return -1; 1059 return -1;
1441 1060
1442 if (wd == EIO_CWD) 1061 if (wd == EIO_CWD)
1443 { 1062 {
1444 if (!getcwd (res, PATH_MAX)) 1063 if (!getcwd (res, EIO_PATH_MAX))
1445 return -1; 1064 return -1;
1446 1065
1447 len = strlen (res); 1066 len = strlen (res);
1448 } 1067 }
1449 else 1068 else
1496 1115
1497 /* zero-terminate, for readlink */ 1116 /* zero-terminate, for readlink */
1498 res [len + 1] = 0; 1117 res [len + 1] = 0;
1499 1118
1500 /* now check if it's a symlink */ 1119 /* now check if it's a symlink */
1501 linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); 1120 linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX);
1502 1121
1503 if (linklen < 0) 1122 if (linklen < 0)
1504 { 1123 {
1505 if (errno != EINVAL) 1124 if (errno != EINVAL)
1506 return -1; 1125 return -1;
1512 { 1131 {
1513 /* yay, it was a symlink - build new path in tmp2 */ 1132 /* yay, it was a symlink - build new path in tmp2 */
1514 int rellen = strlen (rel); 1133 int rellen = strlen (rel);
1515 1134
1516 errno = ENAMETOOLONG; 1135 errno = ENAMETOOLONG;
1517 if (linklen + 1 + rellen >= PATH_MAX) 1136 if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */
1518 return -1; 1137 return -1;
1519 1138
1520 errno = ELOOP; 1139 errno = ELOOP;
1521 if (!--symlinks) 1140 if (!--symlinks)
1522 return -1; 1141 return -1;
1536 /* special case for the lone root path */ 1155 /* special case for the lone root path */
1537 if (res == tmpbuf->ptr) 1156 if (res == tmpbuf->ptr)
1538 *res++ = '/'; 1157 *res++ = '/';
1539 1158
1540 return res - (char *)tmpbuf->ptr; 1159 return res - (char *)tmpbuf->ptr;
1160#endif
1541} 1161}
1542 1162
1543static signed char 1163static signed char
1544eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1164eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1545{ 1165{
1785 return; 1405 return;
1786 1406
1787 dirp = fdopendir (fd); 1407 dirp = fdopendir (fd);
1788 1408
1789 if (!dirp) 1409 if (!dirp)
1790 close (fd); 1410 silent_close (fd);
1791 } 1411 }
1792 else 1412 else
1793 dirp = opendir (req->ptr1); 1413 dirp = opendir (req->ptr1);
1794 #else 1414 #else
1795 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); 1415 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
2004#if !HAVE_AT 1624#if !HAVE_AT
2005 1625
2006/* 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 */
2007/* an absolute or canonical path */ 1627/* an absolute or canonical path */
2008static const char * 1628static const char *
2009wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1629wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2010{ 1630{
2011 if (!wd || *path == '/') 1631 if (!wd || *path == '/')
2012 return path; 1632 return path;
2013 1633
2014 if (path [0] == '.' && !path [1]) 1634 if (path [0] == '.' && !path [1])
2016 1636
2017 { 1637 {
2018 int l1 = wd->len; 1638 int l1 = wd->len;
2019 int l2 = strlen (path); 1639 int l2 = strlen (path);
2020 1640
2021 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1641 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
2022 1642
2023 memcpy (res, wd->str, l1); 1643 memcpy (res, wd->str, l1);
2024 res [l1] = '/'; 1644 res [l1] = '/';
2025 memcpy (res + l1 + 1, path, l2 + 1); 1645 memcpy (res + l1 + 1, path, l2 + 1);
2026 1646
2029} 1649}
2030 1650
2031#endif 1651#endif
2032 1652
2033static eio_wd 1653static eio_wd
2034eio__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)
2035{ 1655{
2036 int fd; 1656 int fd;
2037 eio_wd res; 1657 eio_wd res;
2038 int len = eio__realpath (tmpbuf, wd, path); 1658 int len = eio__realpath (tmpbuf, wd, path);
2039 1659
2061} 1681}
2062 1682
2063eio_wd 1683eio_wd
2064eio_wd_open_sync (eio_wd wd, const char *path) 1684eio_wd_open_sync (eio_wd wd, const char *path)
2065{ 1685{
2066 struct tmpbuf tmpbuf = { 0 }; 1686 struct etp_tmpbuf tmpbuf = { };
2067 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1687 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2068 free (tmpbuf.ptr); 1688 free (tmpbuf.ptr);
2069 1689
2070 return wd; 1690 return wd;
2071} 1691}
2074eio_wd_close_sync (eio_wd wd) 1694eio_wd_close_sync (eio_wd wd)
2075{ 1695{
2076 if (wd != EIO_INVALID_WD && wd != EIO_CWD) 1696 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
2077 { 1697 {
2078 #if HAVE_AT 1698 #if HAVE_AT
2079 close (wd->fd); 1699 eio__close (wd->fd);
2080 #endif 1700 #endif
2081 free (wd); 1701 free (wd);
2082 } 1702 }
2083} 1703}
2084 1704
2085#if HAVE_AT 1705#if HAVE_AT
2086 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
2087/* they forgot these */ 1720/* they forgot these */
2088 1721
2089static int 1722static int
2090eio__truncateat (int dirfd, const char *path, off_t length) 1723eio__truncateat (int dirfd, const char *path, off_t length)
2091{ 1724{
2094 1727
2095 if (fd < 0) 1728 if (fd < 0)
2096 return fd; 1729 return fd;
2097 1730
2098 res = ftruncate (fd, length); 1731 res = ftruncate (fd, length);
2099 close (fd); 1732 silent_close (fd);
2100 return res; 1733 return res;
2101} 1734}
2102 1735
2103static int 1736static int
2104eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) 1737eio__statvfsat (int dirfd, const char *path, struct statvfs *buf)
2108 1741
2109 if (fd < 0) 1742 if (fd < 0)
2110 return fd; 1743 return fd;
2111 1744
2112 res = fstatvfs (fd, buf); 1745 res = fstatvfs (fd, buf);
2113 close (fd); 1746 silent_close (fd);
2114 return res; 1747 return res;
2115
2116} 1748}
2117 1749
2118#endif 1750#endif
2119 1751
2120/*****************************************************************************/ 1752/*****************************************************************************/
2121 1753
2122#define ALLOC(len) \ 1754#define ALLOC(len) \
2123 if (!req->ptr2) \ 1755 if (!req->ptr2) \
2124 { \ 1756 { \
2125 X_LOCK (wrklock); \ 1757 X_LOCK (EIO_POOL->wrklock); \
2126 req->flags |= EIO_FLAG_PTR2_FREE; \ 1758 req->flags |= EIO_FLAG_PTR2_FREE; \
2127 X_UNLOCK (wrklock); \ 1759 X_UNLOCK (EIO_POOL->wrklock); \
2128 req->ptr2 = malloc (len); \ 1760 req->ptr2 = malloc (len); \
2129 if (!req->ptr2) \ 1761 if (!req->ptr2) \
2130 { \ 1762 { \
2131 errno = ENOMEM; \ 1763 errno = ENOMEM; \
2132 req->result = -1; \ 1764 req->result = -1; \
2133 break; \ 1765 goto alloc_fail; \
2134 } \ 1766 } \
2135 } 1767 }
2136 1768
2137static void ecb_noinline ecb_cold 1769/*****************************************************************************/
2138etp_proc_init (void)
2139{
2140#if HAVE_PRCTL_SET_NAME
2141 /* provide a more sensible "thread name" */
2142 char name[16 + 1];
2143 const int namelen = sizeof (name) - 1;
2144 int len;
2145 1770
2146 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); 1771static void
2147 name [namelen] = 0; 1772eio__slurp (int fd, eio_req *req)
2148 len = strlen (name);
2149 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2150 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2151#endif
2152}
2153
2154X_THREAD_PROC (etp_proc)
2155{ 1773{
2156 ETP_REQ *req; 1774 req->result = fd;
2157 struct timespec ts;
2158 etp_worker *self = (etp_worker *)thr_arg;
2159 1775
2160 etp_proc_init (); 1776 if (fd < 0)
1777 return;
2161 1778
2162 /* try to distribute timeouts somewhat evenly */ 1779 if (req->offs < 0 || !req->size) /* do we need the size? */
2163 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2164
2165 for (;;)
2166 { 1780 {
2167 ts.tv_sec = 0; 1781 off_t size = lseek (fd, 0, SEEK_END);
2168 1782
2169 X_LOCK (reqlock);
2170
2171 for (;;)
2172 {
2173 req = reqq_shift (&req_queue);
2174
2175 if (req)
2176 break;
2177
2178 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2179 {
2180 X_UNLOCK (reqlock);
2181 X_LOCK (wrklock);
2182 --started;
2183 X_UNLOCK (wrklock);
2184 goto quit;
2185 }
2186
2187 ++idle;
2188
2189 if (idle <= max_idle)
2190 /* we are allowed to idle, so do so without any timeout */
2191 X_COND_WAIT (reqwait, reqlock);
2192 else
2193 {
2194 /* initialise timeout once */
2195 if (!ts.tv_sec)
2196 ts.tv_sec = time (0) + idle_timeout;
2197
2198 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2199 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2200 }
2201
2202 --idle;
2203 }
2204
2205 --nready;
2206
2207 X_UNLOCK (reqlock);
2208
2209 if (req->type < 0) 1783 if (req->offs < 0)
2210 goto quit; 1784 req->offs += size;
2211 1785
2212 ETP_EXECUTE (self, req); 1786 if (!req->size)
2213 1787 req->size = size - req->offs;
2214 X_LOCK (reslock);
2215
2216 ++npending;
2217
2218 if (!reqq_push (&res_queue, req) && want_poll_cb)
2219 want_poll_cb ();
2220
2221 etp_worker_clear (self);
2222
2223 X_UNLOCK (reslock);
2224 } 1788 }
2225 1789
2226quit: 1790 ALLOC (req->size);
2227 free (req); 1791 req->result = pread (fd, req->ptr2, req->size, req->offs);
2228 1792
2229 X_LOCK (wrklock); 1793 silent_close (fd);
2230 etp_worker_free (self);
2231 X_UNLOCK (wrklock);
2232 1794
2233 return 0; 1795alloc_fail:
1796 ;
2234} 1797}
2235
2236/*****************************************************************************/
2237 1798
2238int ecb_cold 1799int ecb_cold
2239eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1800eio_init (void (*want_poll)(void), void (*done_poll)(void))
2240{ 1801{
2241 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);
2242} 1806}
2243 1807
2244ecb_inline void 1808ecb_inline void
2245eio_api_destroy (eio_req *req) 1809eio_api_destroy (eio_req *req)
2246{ 1810{
2269 { \ 1833 { \
2270 eio_api_destroy (req); \ 1834 eio_api_destroy (req); \
2271 return 0; \ 1835 return 0; \
2272 } 1836 }
2273 1837
1838#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)])
1839
2274static void 1840static void
2275eio_execute (etp_worker *self, eio_req *req) 1841eio_execute (etp_worker *self, eio_req *req)
2276{ 1842{
2277#if HAVE_AT 1843#if HAVE_AT
2278 int dirfd; 1844 int dirfd;
2318 : read (req->int1, req->ptr2, req->size); break; 1884 : read (req->int1, req->ptr2, req->size); break;
2319 case EIO_WRITE: req->result = req->offs >= 0 1885 case EIO_WRITE: req->result = req->offs >= 0
2320 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1886 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
2321 : write (req->int1, req->ptr2, req->size); break; 1887 : write (req->int1, req->ptr2, req->size); break;
2322 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
2323 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;
2324 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;
2325 1894
2326#if HAVE_AT 1895#if HAVE_AT
2327 1896
2331 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;
2332 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;
2333 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;
2334 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;
2335 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;
2336 1906
2337 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1907 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2338 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 1908 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
1909 req->result = req->wd && SINGLEDOT (req->ptr1)
1910 ? rmdir (req->wd->str)
1911 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2339 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;
2340 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 1913 case EIO_RENAME: req->result = eio__renameat2 (
1914 dirfd,
1915 /* complications arise because "." cannot be renamed, so we might have to expand */
1916 req->wd && SINGLEDOT (req->ptr1) ? req->wd->str : req->ptr1,
1917 WD2FD ((eio_wd)req->int3),
1918 req->ptr2,
1919 req->int2
1920 );
1921 break;
2341 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;
2342 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;
2343 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;
2344 case EIO_READLINK: ALLOC (PATH_MAX);
2345 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2346 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1925 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2347 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;
2348 case EIO_UTIME: 1935 case EIO_UTIME:
2349 case EIO_FUTIME: 1936 case EIO_FUTIME:
2350 { 1937 {
2351 struct timespec ts[2]; 1938 struct timespec ts[2];
2352 struct timespec *times; 1939 struct timespec *times;
2377 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 1964 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2378 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;
2379 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;
2380 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; 1967 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break;
2381 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;
2382 1970
2383 case EIO_UNLINK: req->result = unlink (path ); break; 1971 case EIO_UNLINK: req->result = unlink (path ); break;
2384 case EIO_RMDIR: req->result = rmdir (path ); break; 1972 case EIO_RMDIR: req->result = rmdir (path ); break;
2385 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;
2386 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;
2387 case EIO_LINK: req->result = link (path , req->ptr2); break; 1975 case EIO_LINK: req->result = link (path , req->ptr2); break;
2388 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; 1976 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break;
2389 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;
2390 case EIO_READLINK: ALLOC (PATH_MAX);
2391 req->result = readlink (path, req->ptr2, PATH_MAX); break;
2392 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1978 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2393 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;
2394 1988
2395 case EIO_UTIME: 1989 case EIO_UTIME:
2396 case EIO_FUTIME: 1990 case EIO_FUTIME:
2397 { 1991 {
2398 struct timeval tv[2]; 1992 struct timeval tv[2];
2433 2027
2434 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;
2435 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;
2436 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; 2030 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
2437 2031
2438 case EIO_CLOSE: req->result = close (req->int1); break; 2032 case EIO_CLOSE: req->result = eio__close (req->int1); break;
2439 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; 2033 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
2440 case EIO_SYNC: req->result = 0; sync (); break; 2034 case EIO_SYNC: req->result = 0; sync (); break;
2441 case EIO_FSYNC: req->result = fsync (req->int1); break; 2035 case EIO_FSYNC: req->result = fsync (req->int1); break;
2442 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 2036 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
2443 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; 2037 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break;
2463 req->result = select (0, 0, 0, 0, &tv); 2057 req->result = select (0, 0, 0, 0, &tv);
2464 } 2058 }
2465#endif 2059#endif
2466 break; 2060 break;
2467 2061
2062#if 0
2468 case EIO_GROUP: 2063 case EIO_GROUP:
2469 abort (); /* handled in eio_request */ 2064 abort (); /* handled in eio_request */
2065#endif
2470 2066
2471 case EIO_NOP: 2067 case EIO_NOP:
2472 req->result = 0; 2068 req->result = 0;
2473 break; 2069 break;
2474 2070
2479 default: 2075 default:
2480 req->result = EIO_ENOSYS (); 2076 req->result = EIO_ENOSYS ();
2481 break; 2077 break;
2482 } 2078 }
2483 2079
2080alloc_fail:
2484 req->errorno = errno; 2081 req->errorno = errno;
2485} 2082}
2486 2083
2487#ifndef EIO_NO_WRAPPERS 2084#ifndef EIO_NO_WRAPPERS
2488 2085
2577} 2174}
2578 2175
2579eio_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)
2580{ 2177{
2581 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;
2582} 2189}
2583 2190
2584eio_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)
2585{ 2192{
2586 REQ (EIO_FSTAT); req->int1 = fd; SEND; 2193 REQ (EIO_FSTAT); req->int1 = fd; SEND;
2731eio_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)
2732{ 2339{
2733 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2340 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2734} 2341}
2735 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
2736eio_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)
2737{ 2349{
2738 REQ (EIO_CUSTOM); req->feed = execute; SEND; 2350 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2739} 2351}
2740 2352
2774void 2386void
2775eio_grp_add (eio_req *grp, eio_req *req) 2387eio_grp_add (eio_req *grp, eio_req *req)
2776{ 2388{
2777 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));
2778 2390
2779 grp->flags |= EIO_FLAG_GROUPADD; 2391 grp->flags |= ETP_FLAG_GROUPADD;
2780 2392
2781 ++grp->size; 2393 ++grp->size;
2782 req->grp = grp; 2394 req->grp = grp;
2783 2395
2784 req->grp_prev = 0; 2396 req->grp_prev = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines