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

Comparing libeio/eio.c (file contents):
Revision 1.112 by root, Fri Sep 30 20:49:57 2011 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 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 */
131 #define mknod(path,mode,dev) EIO_ENOSYS () 129 #define mknod(path,mode,dev) EIO_ENOSYS ()
132 #define sync() EIO_ENOSYS () 130 #define sync() EIO_ENOSYS ()
133 #define readlink(path,buf,s) EIO_ENOSYS () 131 #define readlink(path,buf,s) EIO_ENOSYS ()
134 #define statvfs(path,buf) EIO_ENOSYS () 132 #define statvfs(path,buf) EIO_ENOSYS ()
135 #define fstatvfs(fd,buf) EIO_ENOSYS () 133 #define fstatvfs(fd,buf) EIO_ENOSYS ()
134
135 #define pread(fd,buf,count,offset) eio__pread (fd, buf, count, offset)
136 #define pwrite(fd,buf,count,offset) eio__pwrite (fd, buf, count, offset)
137
138 #if __GNUC__
139 typedef long long eio_off_t; /* signed for compatibility to msvc */
140 #else
141 typedef __int64 eio_off_t; /* unsigned not supported by msvc */
142 #endif
143
144 static eio_ssize_t
145 eio__pread (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
146 {
147 OVERLAPPED o = { 0 };
148 DWORD got;
149
150 o.Offset = offset;
151 o.OffsetHigh = offset >> 32;
152
153 return ReadFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
154 ? got : -1;
155 }
156
157 static eio_ssize_t
158 eio__pwrite (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
159 {
160 OVERLAPPED o = { 0 };
161 DWORD got;
162
163 o.Offset = offset;
164 o.OffsetHigh = offset >> 32;
165
166 return WriteFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
167 ? got : -1;
168 }
136 169
137 /* rename() uses MoveFile, which fails to overwrite */ 170 /* rename() uses MoveFile, which fails to overwrite */
138 #define rename(old,neu) eio__rename (old, neu) 171 #define rename(old,neu) eio__rename (old, neu)
139 172
140 static int 173 static int
168 /* we could even stat and see if it exists */ 201 /* we could even stat and see if it exists */
169 static int 202 static int
170 symlink (const char *old, const char *neu) 203 symlink (const char *old, const char *neu)
171 { 204 {
172 #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)
173 if (CreateSymbolicLink (neu, old, 1)) 213 if (CreateSymbolicLink (neu, old, flags))
174 return 0; 214 return 0;
175
176 if (CreateSymbolicLink (neu, old, 0))
177 return 0;
178 #endif 215 #endif
179 216
180 return EIO_ERRNO (ENOENT, -1); 217 return EIO_ERRNO (ENOENT, -1);
181 } 218 }
182 219
183 /* POSIX API only */ 220 /* POSIX API only, causing trouble for win32 apps */
184 #define CreateHardLink(neu,old,flags) 0 221 #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */
185 #define CreateSymbolicLink(neu,old,flags) 0 222 #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */
186 223
187 struct statvfs 224 struct statvfs
188 { 225 {
189 int dummy; 226 int dummy;
190 }; 227 };
194 #define D_NAME(entp) entp.cFileName 231 #define D_NAME(entp) entp.cFileName
195 #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)
196 233
197#else 234#else
198 235
236 #include <sys/ioctl.h>
199 #include <sys/time.h> 237 #include <sys/time.h>
200 #include <sys/select.h> 238 #include <sys/select.h>
201 #include <sys/statvfs.h>
202 #include <unistd.h> 239 #include <unistd.h>
203 #include <signal.h> 240 #include <signal.h>
204 #include <dirent.h> 241 #include <dirent.h>
205 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
251
206 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 252 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
207 #include <sys/mman.h> 253 #include <sys/mman.h>
208 #endif 254 #endif
209 255
210 #define D_NAME(entp) entp->d_name 256 #define D_NAME(entp) entp->d_name
211 257
212 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 258 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
213 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 259 #if __FreeBSD__ || __NetBSD__ || __OpenBSD__
214 #define _DIRENT_HAVE_D_TYPE /* sigh */ 260 #define _DIRENT_HAVE_D_TYPE /* sigh */
215 #define D_INO(de) (de)->d_fileno 261 #define D_INO(de) (de)->d_fileno
216 #define D_NAMLEN(de) (de)->d_namlen 262 #define D_NAMLEN(de) (de)->d_namlen
217 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 263 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
218 #define D_INO(de) (de)->d_ino 264 #define D_INO(de) (de)->d_ino
237# include <utime.h> 283# include <utime.h>
238#endif 284#endif
239 285
240#if HAVE_SYS_SYSCALL_H 286#if HAVE_SYS_SYSCALL_H
241# include <sys/syscall.h> 287# include <sys/syscall.h>
242#endif
243
244#if HAVE_SYS_PRCTL_H
245# include <sys/prctl.h>
246#endif 288#endif
247 289
248#if HAVE_SENDFILE 290#if HAVE_SENDFILE
249# if __linux 291# if __linux
250# include <sys/sendfile.h> 292# include <sys/sendfile.h>
258# else 300# else
259# error sendfile support requested but not available 301# error sendfile support requested but not available
260# endif 302# endif
261#endif 303#endif
262 304
305#if HAVE_RENAMEAT2
306# include <sys/syscall.h>
307# include <linux/fs.h>
308#endif
309
263#ifndef D_TYPE 310#ifndef D_TYPE
264# define D_TYPE(de) 0 311# define D_TYPE(de) 0
265#endif 312#endif
266#ifndef D_INO 313#ifndef D_INO
267# define D_INO(de) 0 314# define D_INO(de) 0
275# define NAME_MAX 4096 322# define NAME_MAX 4096
276#endif 323#endif
277 324
278/* used for readlink etc. */ 325/* used for readlink etc. */
279#ifndef PATH_MAX 326#ifndef PATH_MAX
280# define PATH_MAX 4096 327# define PATH_MAX 0
281#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)
282 339
283/* buffer size for various temporary buffers */ 340/* buffer size for various temporary buffers */
284#define EIO_BUFSIZE 65536 341#define EIO_BUFSIZE 65536
285 342
286#define dBUF \ 343#define dBUF \
287 char *eio_buf = malloc (EIO_BUFSIZE); \ 344 char *eio_buf = malloc (EIO_BUFSIZE); \
288 errno = ENOMEM; \ 345 errno = ENOMEM; \
289 if (!eio_buf) \ 346 if (!eio_buf) \
290 return -1 347 return -1
291 348
292#define FUBd \ 349#define FUBd \
293 free (eio_buf) 350 free (eio_buf)
294 351
295#define EIO_TICKS ((1000000 + 1023) >> 10)
296
297/*****************************************************************************/ 352/*****************************************************************************/
298 353
299struct tmpbuf
300{
301 void *ptr;
302 int len;
303};
304
305static void *
306tmpbuf_get (struct tmpbuf *buf, int len)
307{
308 if (buf->len < len)
309 {
310 free (buf->ptr);
311 buf->ptr = malloc (buf->len = len);
312 }
313
314 return buf->ptr;
315}
316
317struct tmpbuf; 354struct etp_tmpbuf;
318 355
319#if _POSIX_VERSION >= 200809L 356#if _POSIX_VERSION >= 200809L
320 #define HAVE_AT 1 357 #define HAVE_AT 1
321 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 358 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
322 #ifndef O_SEARCH 359 #ifndef O_SEARCH
323 #define O_SEARCH O_RDONLY 360 #define O_SEARCH O_RDONLY
324 #endif 361 #endif
325#else 362#else
326 #define HAVE_AT 0 363 #define HAVE_AT 0
327 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);
328#endif 365#endif
329 366
330struct eio_pwd 367struct eio_pwd
331{ 368{
332#if HAVE_AT 369#if HAVE_AT
339/*****************************************************************************/ 376/*****************************************************************************/
340 377
341#define ETP_PRI_MIN EIO_PRI_MIN 378#define ETP_PRI_MIN EIO_PRI_MIN
342#define ETP_PRI_MAX EIO_PRI_MAX 379#define ETP_PRI_MAX EIO_PRI_MAX
343 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
344struct etp_worker; 391struct etp_worker;
345
346#define ETP_REQ eio_req 392#define ETP_REQ eio_req
347#define ETP_DESTROY(req) eio_destroy (req) 393#define ETP_DESTROY(req) eio_destroy (req)
348static int eio_finish (eio_req *req); 394static int eio_finish (eio_req *req);
349#define ETP_FINISH(req) eio_finish (req) 395#define ETP_FINISH(req) eio_finish (req)
350static void eio_execute (struct etp_worker *self, eio_req *req); 396static void eio_execute (struct etp_worker *self, eio_req *req);
351#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 397#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
352 398
353/*****************************************************************************/ 399#include "etp.c"
354 400
355#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 401static struct etp_pool eio_pool;
356 402#define EIO_POOL (&eio_pool)
357/* calculate time difference in ~1/EIO_TICKS of a second */
358ecb_inline int
359tvdiff (struct timeval *tv1, struct timeval *tv2)
360{
361 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
362 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
363}
364
365static unsigned int started, idle, wanted = 4;
366
367static void (*want_poll_cb) (void);
368static void (*done_poll_cb) (void);
369
370static unsigned int max_poll_time; /* reslock */
371static unsigned int max_poll_reqs; /* reslock */
372
373static unsigned int nreqs; /* reqlock */
374static unsigned int nready; /* reqlock */
375static unsigned int npending; /* reqlock */
376static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
377static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
378
379static xmutex_t wrklock;
380static xmutex_t reslock;
381static xmutex_t reqlock;
382static xcond_t reqwait;
383
384#if !HAVE_PREADWRITE
385/*
386 * make our pread/pwrite emulation safe against themselves, but not against
387 * normal read/write by using a mutex. slows down execution a lot,
388 * but that's your problem, not mine.
389 */
390static xmutex_t preadwritelock;
391#endif
392
393typedef struct etp_worker
394{
395 struct tmpbuf tmpbuf;
396
397 /* locked by wrklock */
398 struct etp_worker *prev, *next;
399
400 xthread_t tid;
401
402#ifdef ETP_WORKER_COMMON
403 ETP_WORKER_COMMON
404#endif
405} etp_worker;
406
407static etp_worker wrk_first; /* NOT etp */
408
409#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
410#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
411
412/* worker threads management */
413
414static void
415etp_worker_clear (etp_worker *wrk)
416{
417}
418
419static void ecb_cold
420etp_worker_free (etp_worker *wrk)
421{
422 free (wrk->tmpbuf.ptr);
423
424 wrk->next->prev = wrk->prev;
425 wrk->prev->next = wrk->next;
426
427 free (wrk);
428}
429
430static unsigned int
431etp_nreqs (void)
432{
433 int retval;
434 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
435 retval = nreqs;
436 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
437 return retval;
438}
439
440static unsigned int
441etp_nready (void)
442{
443 unsigned int retval;
444
445 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
446 retval = nready;
447 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
448
449 return retval;
450}
451
452static unsigned int
453etp_npending (void)
454{
455 unsigned int retval;
456
457 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
458 retval = npending;
459 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
460
461 return retval;
462}
463
464static unsigned int
465etp_nthreads (void)
466{
467 unsigned int retval;
468
469 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
470 retval = started;
471 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
472
473 return retval;
474}
475
476/*
477 * a somewhat faster data structure might be nice, but
478 * with 8 priorities this actually needs <20 insns
479 * per shift, the most expensive operation.
480 */
481typedef struct {
482 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
483 int size;
484} etp_reqq;
485
486static etp_reqq req_queue;
487static etp_reqq res_queue;
488
489static void ecb_noinline ecb_cold
490reqq_init (etp_reqq *q)
491{
492 int pri;
493
494 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
495 q->qs[pri] = q->qe[pri] = 0;
496
497 q->size = 0;
498}
499
500static int ecb_noinline
501reqq_push (etp_reqq *q, ETP_REQ *req)
502{
503 int pri = req->pri;
504 req->next = 0;
505
506 if (q->qe[pri])
507 {
508 q->qe[pri]->next = req;
509 q->qe[pri] = req;
510 }
511 else
512 q->qe[pri] = q->qs[pri] = req;
513
514 return q->size++;
515}
516
517static ETP_REQ * ecb_noinline
518reqq_shift (etp_reqq *q)
519{
520 int pri;
521
522 if (!q->size)
523 return 0;
524
525 --q->size;
526
527 for (pri = ETP_NUM_PRI; pri--; )
528 {
529 eio_req *req = q->qs[pri];
530
531 if (req)
532 {
533 if (!(q->qs[pri] = (eio_req *)req->next))
534 q->qe[pri] = 0;
535
536 return req;
537 }
538 }
539
540 abort ();
541}
542
543static int ecb_cold
544etp_init (void (*want_poll)(void), void (*done_poll)(void))
545{
546 X_MUTEX_CREATE (wrklock);
547 X_MUTEX_CREATE (reslock);
548 X_MUTEX_CREATE (reqlock);
549 X_COND_CREATE (reqwait);
550
551 reqq_init (&req_queue);
552 reqq_init (&res_queue);
553
554 wrk_first.next =
555 wrk_first.prev = &wrk_first;
556
557 started = 0;
558 idle = 0;
559 nreqs = 0;
560 nready = 0;
561 npending = 0;
562
563 want_poll_cb = want_poll;
564 done_poll_cb = done_poll;
565
566 return 0;
567}
568
569X_THREAD_PROC (etp_proc);
570
571static void ecb_cold
572etp_start_thread (void)
573{
574 etp_worker *wrk = calloc (1, sizeof (etp_worker));
575
576 /*TODO*/
577 assert (("unable to allocate worker thread data", wrk));
578
579 X_LOCK (wrklock);
580
581 if (thread_create (&wrk->tid, etp_proc, (void *)wrk))
582 {
583 wrk->prev = &wrk_first;
584 wrk->next = wrk_first.next;
585 wrk_first.next->prev = wrk;
586 wrk_first.next = wrk;
587 ++started;
588 }
589 else
590 free (wrk);
591
592 X_UNLOCK (wrklock);
593}
594
595static void
596etp_maybe_start_thread (void)
597{
598 if (ecb_expect_true (etp_nthreads () >= wanted))
599 return;
600
601 /* todo: maybe use idle here, but might be less exact */
602 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
603 return;
604
605 etp_start_thread ();
606}
607
608static void ecb_cold
609etp_end_thread (void)
610{
611 eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */
612
613 req->type = -1;
614 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
615
616 X_LOCK (reqlock);
617 reqq_push (&req_queue, req);
618 X_COND_SIGNAL (reqwait);
619 X_UNLOCK (reqlock);
620
621 X_LOCK (wrklock);
622 --started;
623 X_UNLOCK (wrklock);
624}
625
626static int
627etp_poll (void)
628{
629 unsigned int maxreqs;
630 unsigned int maxtime;
631 struct timeval tv_start, tv_now;
632
633 X_LOCK (reslock);
634 maxreqs = max_poll_reqs;
635 maxtime = max_poll_time;
636 X_UNLOCK (reslock);
637
638 if (maxtime)
639 gettimeofday (&tv_start, 0);
640
641 for (;;)
642 {
643 ETP_REQ *req;
644
645 etp_maybe_start_thread ();
646
647 X_LOCK (reslock);
648 req = reqq_shift (&res_queue);
649
650 if (req)
651 {
652 --npending;
653
654 if (!res_queue.size && done_poll_cb)
655 done_poll_cb ();
656 }
657
658 X_UNLOCK (reslock);
659
660 if (!req)
661 return 0;
662
663 X_LOCK (reqlock);
664 --nreqs;
665 X_UNLOCK (reqlock);
666
667 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
668 {
669 req->int1 = 1; /* mark request as delayed */
670 continue;
671 }
672 else
673 {
674 int res = ETP_FINISH (req);
675 if (ecb_expect_false (res))
676 return res;
677 }
678
679 if (ecb_expect_false (maxreqs && !--maxreqs))
680 break;
681
682 if (maxtime)
683 {
684 gettimeofday (&tv_now, 0);
685
686 if (tvdiff (&tv_start, &tv_now) >= maxtime)
687 break;
688 }
689 }
690
691 errno = EAGAIN;
692 return -1;
693}
694
695static void
696etp_cancel (ETP_REQ *req)
697{
698 req->cancelled = 1;
699
700 eio_grp_cancel (req);
701}
702
703static void
704etp_submit (ETP_REQ *req)
705{
706 req->pri -= ETP_PRI_MIN;
707
708 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
709 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
710
711 if (ecb_expect_false (req->type == EIO_GROUP))
712 {
713 /* I hope this is worth it :/ */
714 X_LOCK (reqlock);
715 ++nreqs;
716 X_UNLOCK (reqlock);
717
718 X_LOCK (reslock);
719
720 ++npending;
721
722 if (!reqq_push (&res_queue, req) && want_poll_cb)
723 want_poll_cb ();
724
725 X_UNLOCK (reslock);
726 }
727 else
728 {
729 X_LOCK (reqlock);
730 ++nreqs;
731 ++nready;
732 reqq_push (&req_queue, req);
733 X_COND_SIGNAL (reqwait);
734 X_UNLOCK (reqlock);
735
736 etp_maybe_start_thread ();
737 }
738}
739
740static void ecb_cold
741etp_set_max_poll_time (double nseconds)
742{
743 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
744 max_poll_time = nseconds * EIO_TICKS;
745 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
746}
747
748static void ecb_cold
749etp_set_max_poll_reqs (unsigned int maxreqs)
750{
751 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
752 max_poll_reqs = maxreqs;
753 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
754}
755
756static void ecb_cold
757etp_set_max_idle (unsigned int nthreads)
758{
759 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
760 max_idle = nthreads;
761 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
762}
763
764static void ecb_cold
765etp_set_idle_timeout (unsigned int seconds)
766{
767 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
768 idle_timeout = seconds;
769 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
770}
771
772static void ecb_cold
773etp_set_min_parallel (unsigned int nthreads)
774{
775 if (wanted < nthreads)
776 wanted = nthreads;
777}
778
779static void ecb_cold
780etp_set_max_parallel (unsigned int nthreads)
781{
782 if (wanted > nthreads)
783 wanted = nthreads;
784
785 while (started > wanted)
786 etp_end_thread ();
787}
788 403
789/*****************************************************************************/ 404/*****************************************************************************/
790 405
791static void 406static void
792grp_try_feed (eio_req *grp) 407grp_try_feed (eio_req *grp)
793{ 408{
794 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 409 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
795 { 410 {
796 grp->flags &= ~EIO_FLAG_GROUPADD; 411 grp->flags &= ~ETP_FLAG_GROUPADD;
797 412
798 EIO_FEED (grp); 413 EIO_FEED (grp);
799 414
800 /* stop if no progress has been made */ 415 /* stop if no progress has been made */
801 if (!(grp->flags & EIO_FLAG_GROUPADD)) 416 if (!(grp->flags & ETP_FLAG_GROUPADD))
802 { 417 {
803 grp->feed = 0; 418 grp->feed = 0;
804 break; 419 break;
805 } 420 }
806 } 421 }
813 428
814 /* call feeder, if applicable */ 429 /* call feeder, if applicable */
815 grp_try_feed (grp); 430 grp_try_feed (grp);
816 431
817 /* finish, if done */ 432 /* finish, if done */
818 if (!grp->size && grp->int1) 433 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
819 return eio_finish (grp); 434 return eio_finish (grp);
820 else 435 else
821 return 0; 436 return 0;
822} 437}
823 438
859} 474}
860 475
861void 476void
862eio_grp_cancel (eio_req *grp) 477eio_grp_cancel (eio_req *grp)
863{ 478{
864 for (grp = grp->grp_first; grp; grp = grp->grp_next) 479 etp_grp_cancel (EIO_POOL, grp);
865 eio_cancel (grp);
866} 480}
867 481
868void 482void
869eio_cancel (eio_req *req) 483eio_cancel (eio_req *req)
870{ 484{
871 etp_cancel (req); 485 etp_cancel (EIO_POOL, req);
872} 486}
873 487
874void 488void
875eio_submit (eio_req *req) 489eio_submit (eio_req *req)
876{ 490{
877 etp_submit (req); 491 etp_submit (EIO_POOL, req);
878} 492}
879 493
880unsigned int 494unsigned int
881eio_nreqs (void) 495eio_nreqs (void)
882{ 496{
883 return etp_nreqs (); 497 return etp_nreqs (EIO_POOL);
884} 498}
885 499
886unsigned int 500unsigned int
887eio_nready (void) 501eio_nready (void)
888{ 502{
889 return etp_nready (); 503 return etp_nready (EIO_POOL);
890} 504}
891 505
892unsigned int 506unsigned int
893eio_npending (void) 507eio_npending (void)
894{ 508{
895 return etp_npending (); 509 return etp_npending (EIO_POOL);
896} 510}
897 511
898unsigned int ecb_cold 512unsigned int ecb_cold
899eio_nthreads (void) 513eio_nthreads (void)
900{ 514{
901 return etp_nthreads (); 515 return etp_nthreads (EIO_POOL);
902} 516}
903 517
904void ecb_cold 518void ecb_cold
905eio_set_max_poll_time (double nseconds) 519eio_set_max_poll_time (double nseconds)
906{ 520{
907 etp_set_max_poll_time (nseconds); 521 etp_set_max_poll_time (EIO_POOL, nseconds);
908} 522}
909 523
910void ecb_cold 524void ecb_cold
911eio_set_max_poll_reqs (unsigned int maxreqs) 525eio_set_max_poll_reqs (unsigned int maxreqs)
912{ 526{
913 etp_set_max_poll_reqs (maxreqs); 527 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
914} 528}
915 529
916void ecb_cold 530void ecb_cold
917eio_set_max_idle (unsigned int nthreads) 531eio_set_max_idle (unsigned int nthreads)
918{ 532{
919 etp_set_max_idle (nthreads); 533 etp_set_max_idle (EIO_POOL, nthreads);
920} 534}
921 535
922void ecb_cold 536void ecb_cold
923eio_set_idle_timeout (unsigned int seconds) 537eio_set_idle_timeout (unsigned int seconds)
924{ 538{
925 etp_set_idle_timeout (seconds); 539 etp_set_idle_timeout (EIO_POOL, seconds);
926} 540}
927 541
928void ecb_cold 542void ecb_cold
929eio_set_min_parallel (unsigned int nthreads) 543eio_set_min_parallel (unsigned int nthreads)
930{ 544{
931 etp_set_min_parallel (nthreads); 545 etp_set_min_parallel (EIO_POOL, nthreads);
932} 546}
933 547
934void ecb_cold 548void ecb_cold
935eio_set_max_parallel (unsigned int nthreads) 549eio_set_max_parallel (unsigned int nthreads)
936{ 550{
937 etp_set_max_parallel (nthreads); 551 etp_set_max_parallel (EIO_POOL, nthreads);
938} 552}
939 553
940int eio_poll (void) 554int eio_poll (void)
941{ 555{
942 return etp_poll (); 556 return etp_poll (EIO_POOL);
943} 557}
944 558
945/*****************************************************************************/ 559/*****************************************************************************/
946/* work around various missing functions */ 560/* work around various missing functions */
947 561
948#if !HAVE_PREADWRITE 562#if HAVE_POSIX_CLOSE && !__linux
949# undef pread 563# define eio__close(fd) posix_close (fd, 0)
950# undef pwrite 564#else
951# define pread eio__pread 565# define eio__close(fd) close (fd)
952# define pwrite eio__pwrite
953
954static eio_ssize_t
955eio__pread (int fd, void *buf, size_t count, off_t offset)
956{
957 eio_ssize_t res;
958 off_t ooffset;
959
960 X_LOCK (preadwritelock);
961 ooffset = lseek (fd, 0, SEEK_CUR);
962 lseek (fd, offset, SEEK_SET);
963 res = read (fd, buf, count);
964 lseek (fd, ooffset, SEEK_SET);
965 X_UNLOCK (preadwritelock);
966
967 return res;
968}
969
970static eio_ssize_t
971eio__pwrite (int fd, void *buf, size_t count, off_t offset)
972{
973 eio_ssize_t res;
974 off_t ooffset;
975
976 X_LOCK (preadwritelock);
977 ooffset = lseek (fd, 0, SEEK_CUR);
978 lseek (fd, offset, SEEK_SET);
979 res = write (fd, buf, count);
980 lseek (fd, ooffset, SEEK_SET);
981 X_UNLOCK (preadwritelock);
982
983 return res;
984}
985#endif 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}
986 576
987#ifndef HAVE_UTIMES 577#ifndef HAVE_UTIMES
988 578
989# undef utimes 579# undef utimes
990# define utimes(path,times) eio__utimes (path, times) 580# define utimes(path,times) eio__utimes (path, times)
1032 int res; 622 int res;
1033 623
1034#if HAVE_SYS_SYNCFS 624#if HAVE_SYS_SYNCFS
1035 res = (int)syscall (__NR_syncfs, (int)(fd)); 625 res = (int)syscall (__NR_syncfs, (int)(fd));
1036#else 626#else
1037 res = -1; 627 res = EIO_ENOSYS ();
1038 errno = ENOSYS;
1039#endif 628#endif
1040 629
1041 if (res < 0 && errno == ENOSYS && fd >= 0) 630 if (res < 0 && errno == ENOSYS && fd >= 0)
1042 sync (); 631 sync ();
1043 632
1073} 662}
1074 663
1075static int 664static int
1076eio__fallocate (int fd, int mode, off_t offset, size_t len) 665eio__fallocate (int fd, int mode, off_t offset, size_t len)
1077{ 666{
1078#if HAVE_FALLOCATE 667#if HAVE_LINUX_FALLOCATE
1079 return fallocate (fd, mode, offset, len); 668 return fallocate (fd, mode, offset, len);
1080#else 669#else
1081 errno = ENOSYS; 670 return EIO_ENOSYS ();
1082 return -1;
1083#endif 671#endif
1084} 672}
1085 673
1086#if !HAVE_READAHEAD 674#if !HAVE_READAHEAD
1087# undef readahead 675# undef readahead
1102 todo -= len; 690 todo -= len;
1103 } 691 }
1104 692
1105 FUBd; 693 FUBd;
1106 694
1107 errno = 0; 695 /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */
696 /* but not for e.g. EIO or eof, so we also never fail */
1108 return count; 697 return 0;
1109} 698}
1110 699
1111#endif 700#endif
1112 701
1113/* sendfile always needs emulation */ 702/* sendfile always needs emulation */
1148 737
1149 /* according to source inspection, this is correct, and useful behaviour */ 738 /* according to source inspection, this is correct, and useful behaviour */
1150 if (sbytes) 739 if (sbytes)
1151 res = sbytes; 740 res = sbytes;
1152 741
1153# elif defined (__APPLE__) 742# elif defined __APPLE__
1154 off_t sbytes = count; 743 off_t sbytes = count;
1155 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 744 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
1156 745
1157 /* according to the manpage, sbytes is always valid */ 746 /* according to the manpage, sbytes is always valid */
1158 if (sbytes) 747 if (sbytes)
1185 HANDLE h = TO_SOCKET (ifd); 774 HANDLE h = TO_SOCKET (ifd);
1186 SetFilePointer (h, offset, 0, FILE_BEGIN); 775 SetFilePointer (h, offset, 0, FILE_BEGIN);
1187 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 776 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1188 777
1189#else 778#else
1190 res = -1; 779 res = EIO_ENOSYS ();
1191 errno = ENOSYS;
1192#endif 780#endif
1193 781
1194 /* we assume sendfile can copy at least 128mb in one go */ 782 /* we assume sendfile can copy at least 128mb in one go */
1195 if (res <= 128 * 1024 * 1024) 783 if (res <= 128 * 1024 * 1024)
1196 { 784 {
1371 intptr_t end = addr + len; 959 intptr_t end = addr + len;
1372 intptr_t page = eio_pagesize (); 960 intptr_t page = eio_pagesize ();
1373 961
1374 if (addr < end) 962 if (addr < end)
1375 if (flags & EIO_MT_MODIFY) /* modify */ 963 if (flags & EIO_MT_MODIFY) /* modify */
1376 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));
1377 else 965 else
1378 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));
1379 } 967 }
1380 968
1381 return 0; 969 return 0;
1382} 970}
1383 971
1384/*****************************************************************************/ 972/*****************************************************************************/
1385/* requests implemented outside eio_execute, because they are so large */ 973/* requests implemented outside eio_execute, because they are so large */
1386 974
975static void
976eio__lseek (eio_req *req)
977{
978 /* this usually gets optimised away completely, or your compiler sucks, */
979 /* or the whence constants really are not 0, 1, 2 */
980 int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET
981 : req->int2 == EIO_SEEK_CUR ? SEEK_CUR
982 : req->int2 == EIO_SEEK_END ? SEEK_END
983 : req->int2;
984
985 req->offs = lseek (req->int1, req->offs, whence);
986 req->result = req->offs == (off_t)-1 ? -1 : 0;
987}
988
1387/* 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 */
1388static int 990static int
1389eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 991eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1390{ 992{
993 char *res;
1391 const char *rel = path; 994 const char *rel = path;
1392 char *res;
1393 char *tmp1, *tmp2; 995 char *tmp1, *tmp2;
1394#if SYMLOOP_MAX > 32 996#if SYMLOOP_MAX > 32
1395 int symlinks = SYMLOOP_MAX; 997 int symlinks = SYMLOOP_MAX;
1396#else 998#else
1397 int symlinks = 32; 999 int symlinks = 32;
1403 1005
1404 errno = ENOENT; 1006 errno = ENOENT;
1405 if (!*rel) 1007 if (!*rel)
1406 return -1; 1008 return -1;
1407 1009
1408 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
1409 tmp1 = res + PATH_MAX; 1028 tmp1 = res + EIO_PATH_MAX;
1410 tmp2 = tmp1 + PATH_MAX; 1029 tmp2 = tmp1 + EIO_PATH_MAX;
1411 1030
1412#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 */
1413#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1032#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1414 /* on linux we may be able to ask the kernel */ 1033 /* on linux we may be able to ask the kernel */
1415 { 1034 {
1416 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);
1417 1036
1418 if (fd >= 0) 1037 if (fd >= 0)
1419 { 1038 {
1420 sprintf (tmp1, "/proc/self/fd/%d", fd); 1039 sprintf (tmp1, "/proc/self/fd/%d", fd);
1421 req->result = readlink (tmp1, res, PATH_MAX); 1040 req->result = readlink (tmp1, res, EIO_PATH_MAX);
1422 close (fd);
1423
1424 /* 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);
1425 1043
1426 if (req->result > 0) 1044 if (req->result > 0)
1427 goto done; 1045 goto done;
1428 } 1046 }
1429 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)
1430 return; 1048 return -1;
1431 } 1049 }
1432#endif 1050#endif
1433#endif 1051#endif
1434 1052
1435 if (*rel != '/') 1053 if (*rel != '/')
1440 if (wd == EIO_INVALID_WD) 1058 if (wd == EIO_INVALID_WD)
1441 return -1; 1059 return -1;
1442 1060
1443 if (wd == EIO_CWD) 1061 if (wd == EIO_CWD)
1444 { 1062 {
1445 if (!getcwd (res, PATH_MAX)) 1063 if (!getcwd (res, EIO_PATH_MAX))
1446 return -1; 1064 return -1;
1447 1065
1448 len = strlen (res); 1066 len = strlen (res);
1449 } 1067 }
1450 else 1068 else
1487 } 1105 }
1488 } 1106 }
1489 1107
1490 errno = ENAMETOOLONG; 1108 errno = ENAMETOOLONG;
1491 if (res + 1 + len + 1 >= tmp1) 1109 if (res + 1 + len + 1 >= tmp1)
1492 return; 1110 return -1;
1493 1111
1494 /* copy one component */ 1112 /* copy one component */
1495 *res = '/'; 1113 *res = '/';
1496 memcpy (res + 1, beg, len); 1114 memcpy (res + 1, beg, len);
1497 1115
1498 /* zero-terminate, for readlink */ 1116 /* zero-terminate, for readlink */
1499 res [len + 1] = 0; 1117 res [len + 1] = 0;
1500 1118
1501 /* now check if it's a symlink */ 1119 /* now check if it's a symlink */
1502 linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); 1120 linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX);
1503 1121
1504 if (linklen < 0) 1122 if (linklen < 0)
1505 { 1123 {
1506 if (errno != EINVAL) 1124 if (errno != EINVAL)
1507 return -1; 1125 return -1;
1513 { 1131 {
1514 /* yay, it was a symlink - build new path in tmp2 */ 1132 /* yay, it was a symlink - build new path in tmp2 */
1515 int rellen = strlen (rel); 1133 int rellen = strlen (rel);
1516 1134
1517 errno = ENAMETOOLONG; 1135 errno = ENAMETOOLONG;
1518 if (linklen + 1 + rellen >= PATH_MAX) 1136 if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */
1519 return -1; 1137 return -1;
1520 1138
1521 errno = ELOOP; 1139 errno = ELOOP;
1522 if (!--symlinks) 1140 if (!--symlinks)
1523 return -1; 1141 return -1;
1537 /* special case for the lone root path */ 1155 /* special case for the lone root path */
1538 if (res == tmpbuf->ptr) 1156 if (res == tmpbuf->ptr)
1539 *res++ = '/'; 1157 *res++ = '/';
1540 1158
1541 return res - (char *)tmpbuf->ptr; 1159 return res - (char *)tmpbuf->ptr;
1160#endif
1542} 1161}
1543 1162
1544static signed char 1163static signed char
1545eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1164eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1546{ 1165{
1786 return; 1405 return;
1787 1406
1788 dirp = fdopendir (fd); 1407 dirp = fdopendir (fd);
1789 1408
1790 if (!dirp) 1409 if (!dirp)
1791 close (fd); 1410 silent_close (fd);
1792 } 1411 }
1793 else 1412 else
1794 dirp = opendir (req->ptr1); 1413 dirp = opendir (req->ptr1);
1795 #else 1414 #else
1796 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); 1415 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
1919 #ifdef DT_FIFO 1538 #ifdef DT_FIFO
1920 case DT_FIFO: ent->type = EIO_DT_FIFO; break; 1539 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1921 #endif 1540 #endif
1922 #ifdef DT_CHR 1541 #ifdef DT_CHR
1923 case DT_CHR: ent->type = EIO_DT_CHR; break; 1542 case DT_CHR: ent->type = EIO_DT_CHR; break;
1924 #endif 1543 #endif
1925 #ifdef DT_MPC 1544 #ifdef DT_MPC
1926 case DT_MPC: ent->type = EIO_DT_MPC; break; 1545 case DT_MPC: ent->type = EIO_DT_MPC; break;
1927 #endif 1546 #endif
1928 #ifdef DT_DIR 1547 #ifdef DT_DIR
1929 case DT_DIR: ent->type = EIO_DT_DIR; break; 1548 case DT_DIR: ent->type = EIO_DT_DIR; break;
1930 #endif 1549 #endif
1931 #ifdef DT_NAM 1550 #ifdef DT_NAM
1932 case DT_NAM: ent->type = EIO_DT_NAM; break; 1551 case DT_NAM: ent->type = EIO_DT_NAM; break;
1933 #endif 1552 #endif
1934 #ifdef DT_BLK 1553 #ifdef DT_BLK
1935 case DT_BLK: ent->type = EIO_DT_BLK; break; 1554 case DT_BLK: ent->type = EIO_DT_BLK; break;
1936 #endif 1555 #endif
1937 #ifdef DT_MPB 1556 #ifdef DT_MPB
1938 case DT_MPB: ent->type = EIO_DT_MPB; break; 1557 case DT_MPB: ent->type = EIO_DT_MPB; break;
1939 #endif 1558 #endif
1940 #ifdef DT_REG 1559 #ifdef DT_REG
1941 case DT_REG: ent->type = EIO_DT_REG; break; 1560 case DT_REG: ent->type = EIO_DT_REG; break;
1942 #endif 1561 #endif
1943 #ifdef DT_NWK 1562 #ifdef DT_NWK
1944 case DT_NWK: ent->type = EIO_DT_NWK; break; 1563 case DT_NWK: ent->type = EIO_DT_NWK; break;
1945 #endif 1564 #endif
1946 #ifdef DT_CMP 1565 #ifdef DT_CMP
1947 case DT_CMP: ent->type = EIO_DT_CMP; break; 1566 case DT_CMP: ent->type = EIO_DT_CMP; break;
1948 #endif 1567 #endif
1949 #ifdef DT_LNK 1568 #ifdef DT_LNK
1950 case DT_LNK: ent->type = EIO_DT_LNK; break; 1569 case DT_LNK: ent->type = EIO_DT_LNK; break;
1951 #endif 1570 #endif
1952 #ifdef DT_SOCK 1571 #ifdef DT_SOCK
1953 case DT_SOCK: ent->type = EIO_DT_SOCK; break; 1572 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
1966 { 1585 {
1967 if (ent->type == EIO_DT_UNKNOWN) 1586 if (ent->type == EIO_DT_UNKNOWN)
1968 { 1587 {
1969 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ 1588 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1970 ent->score = 1; 1589 ent->score = 1;
1971 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ 1590 else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */
1972 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ 1591 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */
1973 } 1592 }
1974 else if (ent->type == EIO_DT_DIR) 1593 else if (ent->type == EIO_DT_DIR)
1975 ent->score = 0; 1594 ent->score = 0;
1976 } 1595 }
2005#if !HAVE_AT 1624#if !HAVE_AT
2006 1625
2007/* 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 */
2008/* an absolute or canonical path */ 1627/* an absolute or canonical path */
2009static const char * 1628static const char *
2010wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1629wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2011{ 1630{
2012 if (!wd || *path == '/') 1631 if (!wd || *path == '/')
2013 return path; 1632 return path;
2014 1633
2015 if (path [0] == '.' && !path [1]) 1634 if (path [0] == '.' && !path [1])
2017 1636
2018 { 1637 {
2019 int l1 = wd->len; 1638 int l1 = wd->len;
2020 int l2 = strlen (path); 1639 int l2 = strlen (path);
2021 1640
2022 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1641 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
2023 1642
2024 memcpy (res, wd->str, l1); 1643 memcpy (res, wd->str, l1);
2025 res [l1] = '/'; 1644 res [l1] = '/';
2026 memcpy (res + l1 + 1, path, l2 + 1); 1645 memcpy (res + l1 + 1, path, l2 + 1);
2027 1646
2030} 1649}
2031 1650
2032#endif 1651#endif
2033 1652
2034static eio_wd 1653static eio_wd
2035eio__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)
2036{ 1655{
2037 int fd; 1656 int fd;
2038 eio_wd res; 1657 eio_wd res;
2039 int len = eio__realpath (tmpbuf, wd, path); 1658 int len = eio__realpath (tmpbuf, wd, path);
2040 1659
2062} 1681}
2063 1682
2064eio_wd 1683eio_wd
2065eio_wd_open_sync (eio_wd wd, const char *path) 1684eio_wd_open_sync (eio_wd wd, const char *path)
2066{ 1685{
2067 struct tmpbuf tmpbuf = { 0 }; 1686 struct etp_tmpbuf tmpbuf = { };
2068 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1687 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2069 free (tmpbuf.ptr); 1688 free (tmpbuf.ptr);
2070 1689
2071 return wd; 1690 return wd;
2072} 1691}
2075eio_wd_close_sync (eio_wd wd) 1694eio_wd_close_sync (eio_wd wd)
2076{ 1695{
2077 if (wd != EIO_INVALID_WD && wd != EIO_CWD) 1696 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
2078 { 1697 {
2079 #if HAVE_AT 1698 #if HAVE_AT
2080 close (wd->fd); 1699 eio__close (wd->fd);
2081 #endif 1700 #endif
2082 free (wd); 1701 free (wd);
2083 } 1702 }
2084} 1703}
2085 1704
2086#if HAVE_AT 1705#if HAVE_AT
2087 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
2088/* they forgot these */ 1720/* they forgot these */
2089 1721
2090static int 1722static int
2091eio__truncateat (int dirfd, const char *path, off_t length) 1723eio__truncateat (int dirfd, const char *path, off_t length)
2092{ 1724{
2095 1727
2096 if (fd < 0) 1728 if (fd < 0)
2097 return fd; 1729 return fd;
2098 1730
2099 res = ftruncate (fd, length); 1731 res = ftruncate (fd, length);
2100 close (fd); 1732 silent_close (fd);
2101 return res; 1733 return res;
2102} 1734}
2103 1735
2104static int 1736static int
2105eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) 1737eio__statvfsat (int dirfd, const char *path, struct statvfs *buf)
2109 1741
2110 if (fd < 0) 1742 if (fd < 0)
2111 return fd; 1743 return fd;
2112 1744
2113 res = fstatvfs (fd, buf); 1745 res = fstatvfs (fd, buf);
2114 close (fd); 1746 silent_close (fd);
2115 return res; 1747 return res;
2116
2117} 1748}
2118 1749
2119#endif 1750#endif
2120 1751
2121/*****************************************************************************/ 1752/*****************************************************************************/
2122 1753
2123#define ALLOC(len) \ 1754#define ALLOC(len) \
2124 if (!req->ptr2) \ 1755 if (!req->ptr2) \
2125 { \ 1756 { \
2126 X_LOCK (wrklock); \ 1757 X_LOCK (EIO_POOL->wrklock); \
2127 req->flags |= EIO_FLAG_PTR2_FREE; \ 1758 req->flags |= EIO_FLAG_PTR2_FREE; \
2128 X_UNLOCK (wrklock); \ 1759 X_UNLOCK (EIO_POOL->wrklock); \
2129 req->ptr2 = malloc (len); \ 1760 req->ptr2 = malloc (len); \
2130 if (!req->ptr2) \ 1761 if (!req->ptr2) \
2131 { \ 1762 { \
2132 errno = ENOMEM; \ 1763 errno = ENOMEM; \
2133 req->result = -1; \ 1764 req->result = -1; \
2134 break; \ 1765 goto alloc_fail; \
2135 } \ 1766 } \
2136 } 1767 }
2137 1768
2138static void ecb_noinline ecb_cold 1769/*****************************************************************************/
2139etp_proc_init (void)
2140{
2141#if HAVE_PRCTL_SET_NAME
2142 /* provide a more sensible "thread name" */
2143 char name[16 + 1];
2144 const int namelen = sizeof (name) - 1;
2145 int len;
2146 1770
2147 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); 1771static void
2148 name [namelen] = 0; 1772eio__slurp (int fd, eio_req *req)
2149 len = strlen (name);
2150 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2151 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2152}
2153#endif
2154
2155X_THREAD_PROC (etp_proc)
2156{ 1773{
2157 ETP_REQ *req; 1774 req->result = fd;
2158 struct timespec ts;
2159 etp_worker *self = (etp_worker *)thr_arg;
2160 1775
2161 etp_proc_init (); 1776 if (fd < 0)
1777 return;
2162 1778
2163 /* try to distribute timeouts somewhat evenly */ 1779 if (req->offs < 0 || !req->size) /* do we need the size? */
2164 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2165
2166 for (;;)
2167 { 1780 {
2168 ts.tv_sec = 0; 1781 off_t size = lseek (fd, 0, SEEK_END);
2169 1782
2170 X_LOCK (reqlock);
2171
2172 for (;;)
2173 {
2174 req = reqq_shift (&req_queue);
2175
2176 if (req)
2177 break;
2178
2179 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2180 {
2181 X_UNLOCK (reqlock);
2182 X_LOCK (wrklock);
2183 --started;
2184 X_UNLOCK (wrklock);
2185 goto quit;
2186 }
2187
2188 ++idle;
2189
2190 if (idle <= max_idle)
2191 /* we are allowed to idle, so do so without any timeout */
2192 X_COND_WAIT (reqwait, reqlock);
2193 else
2194 {
2195 /* initialise timeout once */
2196 if (!ts.tv_sec)
2197 ts.tv_sec = time (0) + idle_timeout;
2198
2199 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2200 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2201 }
2202
2203 --idle;
2204 }
2205
2206 --nready;
2207
2208 X_UNLOCK (reqlock);
2209
2210 if (req->type < 0) 1783 if (req->offs < 0)
2211 goto quit; 1784 req->offs += size;
2212 1785
2213 ETP_EXECUTE (self, req); 1786 if (!req->size)
2214 1787 req->size = size - req->offs;
2215 X_LOCK (reslock);
2216
2217 ++npending;
2218
2219 if (!reqq_push (&res_queue, req) && want_poll_cb)
2220 want_poll_cb ();
2221
2222 etp_worker_clear (self);
2223
2224 X_UNLOCK (reslock);
2225 } 1788 }
2226 1789
2227quit: 1790 ALLOC (req->size);
2228 free (req); 1791 req->result = pread (fd, req->ptr2, req->size, req->offs);
2229 1792
2230 X_LOCK (wrklock); 1793 silent_close (fd);
2231 etp_worker_free (self);
2232 X_UNLOCK (wrklock);
2233 1794
2234 return 0; 1795alloc_fail:
1796 ;
2235} 1797}
2236
2237/*****************************************************************************/
2238 1798
2239int ecb_cold 1799int ecb_cold
2240eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1800eio_init (void (*want_poll)(void), void (*done_poll)(void))
2241{ 1801{
2242#if !HAVE_PREADWRITE 1802 eio_want_poll_cb = want_poll;
2243 X_MUTEX_CREATE (preadwritelock); 1803 eio_done_poll_cb = done_poll;
2244#endif
2245 1804
2246 return etp_init (want_poll, done_poll); 1805 return etp_init (EIO_POOL, 0, 0, 0);
2247} 1806}
2248 1807
2249ecb_inline void 1808ecb_inline void
2250eio_api_destroy (eio_req *req) 1809eio_api_destroy (eio_req *req)
2251{ 1810{
2252 free (req); 1811 free (req);
2253} 1812}
2254 1813
2255#define REQ(rtype) \ 1814#define REQ(rtype) \
2256 eio_req *req; \ 1815 eio_req *req; \
2257 \ 1816 \
2258 req = (eio_req *)calloc (1, sizeof *req); \ 1817 req = (eio_req *)calloc (1, sizeof *req); \
2259 if (!req) \ 1818 if (!req) \
2260 return 0; \ 1819 return 0; \
2274 { \ 1833 { \
2275 eio_api_destroy (req); \ 1834 eio_api_destroy (req); \
2276 return 0; \ 1835 return 0; \
2277 } 1836 }
2278 1837
1838#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)])
1839
2279static void 1840static void
2280eio_execute (etp_worker *self, eio_req *req) 1841eio_execute (etp_worker *self, eio_req *req)
2281{ 1842{
2282#if HAVE_AT 1843#if HAVE_AT
2283 int dirfd; 1844 int dirfd;
2314 req->result = req->wd == EIO_INVALID_WD ? -1 : 0; 1875 req->result = req->wd == EIO_INVALID_WD ? -1 : 0;
2315 break; 1876 break;
2316 case EIO_WD_CLOSE: req->result = 0; 1877 case EIO_WD_CLOSE: req->result = 0;
2317 eio_wd_close_sync (req->wd); break; 1878 eio_wd_close_sync (req->wd); break;
2318 1879
1880 case EIO_SEEK: eio__lseek (req); break;
2319 case EIO_READ: ALLOC (req->size); 1881 case EIO_READ: ALLOC (req->size);
2320 req->result = req->offs >= 0 1882 req->result = req->offs >= 0
2321 ? pread (req->int1, req->ptr2, req->size, req->offs) 1883 ? pread (req->int1, req->ptr2, req->size, req->offs)
2322 : read (req->int1, req->ptr2, req->size); break; 1884 : read (req->int1, req->ptr2, req->size); break;
2323 case EIO_WRITE: req->result = req->offs >= 0 1885 case EIO_WRITE: req->result = req->offs >= 0
2324 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1886 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
2325 : write (req->int1, req->ptr2, req->size); break; 1887 : write (req->int1, req->ptr2, req->size); break;
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;
2326 1891
2327 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;
2328 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;
2329 1894
2330#if HAVE_AT 1895#if HAVE_AT
2335 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;
2336 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;
2337 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;
2338 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;
2339 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;
2340 1906
2341 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1907 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2342 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;
2343 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;
2344 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;
2345 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;
2346 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;
2347 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;
2348 case EIO_READLINK: ALLOC (PATH_MAX);
2349 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2350 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1925 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2351 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;
2352 case EIO_UTIME: 1935 case EIO_UTIME:
2353 case EIO_FUTIME: 1936 case EIO_FUTIME:
2354 { 1937 {
2355 struct timespec ts[2]; 1938 struct timespec ts[2];
2356 struct timespec *times; 1939 struct timespec *times;
2381 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 1964 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2382 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;
2383 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;
2384 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; 1967 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break;
2385 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;
2386 1970
2387 case EIO_UNLINK: req->result = unlink (path ); break; 1971 case EIO_UNLINK: req->result = unlink (path ); break;
2388 case EIO_RMDIR: req->result = rmdir (path ); break; 1972 case EIO_RMDIR: req->result = rmdir (path ); break;
2389 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;
2390 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;
2391 case EIO_LINK: req->result = link (path , req->ptr2); break; 1975 case EIO_LINK: req->result = link (path , req->ptr2); break;
2392 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; 1976 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break;
2393 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;
2394 case EIO_READLINK: ALLOC (PATH_MAX);
2395 req->result = readlink (path, req->ptr2, PATH_MAX); break;
2396 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1978 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2397 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;
2398 1988
2399 case EIO_UTIME: 1989 case EIO_UTIME:
2400 case EIO_FUTIME: 1990 case EIO_FUTIME:
2401 { 1991 {
2402 struct timeval tv[2]; 1992 struct timeval tv[2];
2437 2027
2438 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;
2439 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;
2440 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; 2030 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
2441 2031
2442 case EIO_CLOSE: req->result = close (req->int1); break; 2032 case EIO_CLOSE: req->result = eio__close (req->int1); break;
2443 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; 2033 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
2444 case EIO_SYNC: req->result = 0; sync (); break; 2034 case EIO_SYNC: req->result = 0; sync (); break;
2445 case EIO_FSYNC: req->result = fsync (req->int1); break; 2035 case EIO_FSYNC: req->result = fsync (req->int1); break;
2446 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 2036 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
2447 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; 2037 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break;
2467 req->result = select (0, 0, 0, 0, &tv); 2057 req->result = select (0, 0, 0, 0, &tv);
2468 } 2058 }
2469#endif 2059#endif
2470 break; 2060 break;
2471 2061
2062#if 0
2472 case EIO_GROUP: 2063 case EIO_GROUP:
2473 abort (); /* handled in eio_request */ 2064 abort (); /* handled in eio_request */
2065#endif
2474 2066
2475 case EIO_NOP: 2067 case EIO_NOP:
2476 req->result = 0; 2068 req->result = 0;
2477 break; 2069 break;
2478 2070
2479 case EIO_CUSTOM: 2071 case EIO_CUSTOM:
2480 req->feed (req); 2072 req->feed (req);
2481 break; 2073 break;
2482 2074
2483 default: 2075 default:
2484 errno = ENOSYS;
2485 req->result = -1; 2076 req->result = EIO_ENOSYS ();
2486 break; 2077 break;
2487 } 2078 }
2488 2079
2080alloc_fail:
2489 req->errorno = errno; 2081 req->errorno = errno;
2490} 2082}
2491 2083
2492#ifndef EIO_NO_WRAPPERS 2084#ifndef EIO_NO_WRAPPERS
2493 2085
2569eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) 2161eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
2570{ 2162{
2571 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; 2163 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
2572} 2164}
2573 2165
2166eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data)
2167{
2168 REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND;
2169}
2170
2574eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2171eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2575{ 2172{
2576 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2173 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
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