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

Comparing libeio/eio.c (file contents):
Revision 1.120 by root, Tue Apr 24 18:47:50 2012 UTC vs.
Revision 1.161 by root, Fri Feb 16 21:20:52 2024 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,2018,2019 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>
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
205 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
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 O_NONBLOCK
335 #define O_NONBLOCK 0
336#endif
337
338#ifndef O_SEARCH
339 #define O_SEARCH O_RDONLY
340#endif
341
342#ifndef O_DIRECTORY
343 #define O_DIRECTORY 0
344#endif
345
346#ifndef EIO_PATH_MIN
347# define EIO_PATH_MIN 8160
348#endif
349
350#if defined(O_PATH)
351# define EIO_O_PATH O_PATH
352#elif defined(O_SEARCH)
353# define EIO_O_PATH O_SEARCH
354#else
355# define EIO_O_PATH 0
356#endif
357
358#define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX)
282 359
283/* buffer size for various temporary buffers */ 360/* buffer size for various temporary buffers */
284#define EIO_BUFSIZE 65536 361#define EIO_BUFSIZE 65536
285 362
286#define dBUF \ 363#define dBUF \
287 char *eio_buf = malloc (EIO_BUFSIZE); \ 364 char *eio_buf = malloc (EIO_BUFSIZE); \
288 errno = ENOMEM; \ 365 errno = ENOMEM; \
289 if (!eio_buf) \ 366 if (!eio_buf) \
290 return -1 367 return -1
291 368
292#define FUBd \ 369#define FUBd \
293 free (eio_buf) 370 free (eio_buf)
294 371
295#define EIO_TICKS ((1000000 + 1023) >> 10)
296
297/*****************************************************************************/ 372/*****************************************************************************/
298 373
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; 374struct etp_tmpbuf;
318 375
319#if _POSIX_VERSION >= 200809L 376#if _POSIX_VERSION >= 200809L
320 #define HAVE_AT 1 377 #define HAVE_AT 1
321 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 378 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
322 #ifndef O_SEARCH
323 #define O_SEARCH O_RDONLY
324 #endif
325#else 379#else
326 #define HAVE_AT 0 380 #define HAVE_AT 0
327 static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); 381 static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path);
328#endif 382#endif
329 383
330struct eio_pwd 384struct eio_pwd
331{ 385{
332#if HAVE_AT 386#if HAVE_AT
339/*****************************************************************************/ 393/*****************************************************************************/
340 394
341#define ETP_PRI_MIN EIO_PRI_MIN 395#define ETP_PRI_MIN EIO_PRI_MIN
342#define ETP_PRI_MAX EIO_PRI_MAX 396#define ETP_PRI_MAX EIO_PRI_MAX
343 397
398#define ETP_TYPE_QUIT -1
399#define ETP_TYPE_GROUP EIO_GROUP
400
401static void eio_nop_callback (void) { }
402static void (*eio_want_poll_cb)(void) = eio_nop_callback;
403static void (*eio_done_poll_cb)(void) = eio_nop_callback;
404
405#define ETP_WANT_POLL(pool) eio_want_poll_cb ()
406#define ETP_DONE_POLL(pool) eio_done_poll_cb ()
407
344struct etp_worker; 408struct etp_worker;
345
346#define ETP_REQ eio_req 409#define ETP_REQ eio_req
347#define ETP_DESTROY(req) eio_destroy (req) 410#define ETP_DESTROY(req) eio_destroy (req)
348static int eio_finish (eio_req *req); 411static int eio_finish (eio_req *req);
349#define ETP_FINISH(req) eio_finish (req) 412#define ETP_FINISH(req) eio_finish (req)
350static void eio_execute (struct etp_worker *self, eio_req *req); 413static void eio_execute (struct etp_worker *self, eio_req *req);
351#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 414#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
352 415
353/*****************************************************************************/ 416#include "etp.c"
354 417
355#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 418static struct etp_pool eio_pool;
356 419#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 420
789/*****************************************************************************/ 421/*****************************************************************************/
790 422
791static void 423static void
792grp_try_feed (eio_req *grp) 424grp_try_feed (eio_req *grp)
793{ 425{
794 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 426 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
795 { 427 {
796 grp->flags &= ~EIO_FLAG_GROUPADD; 428 grp->flags &= ~ETP_FLAG_GROUPADD;
797 429
798 EIO_FEED (grp); 430 EIO_FEED (grp);
799 431
800 /* stop if no progress has been made */ 432 /* stop if no progress has been made */
801 if (!(grp->flags & EIO_FLAG_GROUPADD)) 433 if (!(grp->flags & ETP_FLAG_GROUPADD))
802 { 434 {
803 grp->feed = 0; 435 grp->feed = 0;
804 break; 436 break;
805 } 437 }
806 } 438 }
813 445
814 /* call feeder, if applicable */ 446 /* call feeder, if applicable */
815 grp_try_feed (grp); 447 grp_try_feed (grp);
816 448
817 /* finish, if done */ 449 /* finish, if done */
818 if (!grp->size && grp->int1) 450 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
819 return eio_finish (grp); 451 return eio_finish (grp);
820 else 452 else
821 return 0; 453 return 0;
822} 454}
823 455
859} 491}
860 492
861void 493void
862eio_grp_cancel (eio_req *grp) 494eio_grp_cancel (eio_req *grp)
863{ 495{
864 for (grp = grp->grp_first; grp; grp = grp->grp_next) 496 etp_grp_cancel (EIO_POOL, grp);
865 eio_cancel (grp);
866} 497}
867 498
868void 499void
869eio_cancel (eio_req *req) 500eio_cancel (eio_req *req)
870{ 501{
871 etp_cancel (req); 502 etp_cancel (EIO_POOL, req);
872} 503}
873 504
874void 505void
875eio_submit (eio_req *req) 506eio_submit (eio_req *req)
876{ 507{
877 etp_submit (req); 508 etp_submit (EIO_POOL, req);
878} 509}
879 510
880unsigned int 511unsigned int
881eio_nreqs (void) 512eio_nreqs (void)
882{ 513{
883 return etp_nreqs (); 514 return etp_nreqs (EIO_POOL);
884} 515}
885 516
886unsigned int 517unsigned int
887eio_nready (void) 518eio_nready (void)
888{ 519{
889 return etp_nready (); 520 return etp_nready (EIO_POOL);
890} 521}
891 522
892unsigned int 523unsigned int
893eio_npending (void) 524eio_npending (void)
894{ 525{
895 return etp_npending (); 526 return etp_npending (EIO_POOL);
896} 527}
897 528
898unsigned int ecb_cold 529unsigned int ecb_cold
899eio_nthreads (void) 530eio_nthreads (void)
900{ 531{
901 return etp_nthreads (); 532 return etp_nthreads (EIO_POOL);
902} 533}
903 534
904void ecb_cold 535void ecb_cold
905eio_set_max_poll_time (double nseconds) 536eio_set_max_poll_time (double nseconds)
906{ 537{
907 etp_set_max_poll_time (nseconds); 538 etp_set_max_poll_time (EIO_POOL, nseconds);
908} 539}
909 540
910void ecb_cold 541void ecb_cold
911eio_set_max_poll_reqs (unsigned int maxreqs) 542eio_set_max_poll_reqs (unsigned int maxreqs)
912{ 543{
913 etp_set_max_poll_reqs (maxreqs); 544 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
914} 545}
915 546
916void ecb_cold 547void ecb_cold
917eio_set_max_idle (unsigned int nthreads) 548eio_set_max_idle (unsigned int nthreads)
918{ 549{
919 etp_set_max_idle (nthreads); 550 etp_set_max_idle (EIO_POOL, nthreads);
920} 551}
921 552
922void ecb_cold 553void ecb_cold
923eio_set_idle_timeout (unsigned int seconds) 554eio_set_idle_timeout (unsigned int seconds)
924{ 555{
925 etp_set_idle_timeout (seconds); 556 etp_set_idle_timeout (EIO_POOL, seconds);
926} 557}
927 558
928void ecb_cold 559void ecb_cold
929eio_set_min_parallel (unsigned int nthreads) 560eio_set_min_parallel (unsigned int nthreads)
930{ 561{
931 etp_set_min_parallel (nthreads); 562 etp_set_min_parallel (EIO_POOL, nthreads);
932} 563}
933 564
934void ecb_cold 565void ecb_cold
935eio_set_max_parallel (unsigned int nthreads) 566eio_set_max_parallel (unsigned int nthreads)
936{ 567{
937 etp_set_max_parallel (nthreads); 568 etp_set_max_parallel (EIO_POOL, nthreads);
938} 569}
939 570
940int eio_poll (void) 571int eio_poll (void)
941{ 572{
942 return etp_poll (); 573 return etp_poll (EIO_POOL);
943} 574}
944 575
945/*****************************************************************************/ 576/*****************************************************************************/
946/* work around various missing functions */ 577/* work around various missing functions */
947 578
948#if !HAVE_PREADWRITE 579#if HAVE_POSIX_CLOSE && !__linux
949# undef pread 580# define eio__close(fd) posix_close (fd, 0)
950# undef pwrite 581#else
951# define pread eio__pread 582# 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 583#endif
584
585/* close() without disturbing errno */
586static void
587silent_close (int fd)
588{
589 int saved_errno = errno;
590 eio__close (fd);
591 errno = saved_errno;
592}
986 593
987#ifndef HAVE_UTIMES 594#ifndef HAVE_UTIMES
988 595
989# undef utimes 596# undef utimes
990# define utimes(path,times) eio__utimes (path, times) 597# define utimes(path,times) eio__utimes (path, times)
1303 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 910 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1304 extern int mallopt (int, int); 911 extern int mallopt (int, int);
1305 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ 912 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1306 #endif 913 #endif
1307 914
915 #ifndef MCL_ONFAULT
916 if (flags & EIO_MCL_ONFAULT)
917 return EIO_ERRNO (EINVAL, -1);
918 #define MCL_ONFAULT 4
919 #endif
920
1308 if (EIO_MCL_CURRENT != MCL_CURRENT 921 if (EIO_MCL_CURRENT != MCL_CURRENT
1309 || EIO_MCL_FUTURE != MCL_FUTURE) 922 || EIO_MCL_FUTURE != MCL_FUTURE
923 || EIO_MCL_ONFAULT != MCL_ONFAULT)
1310 { 924 {
1311 flags = 0 925 flags = 0
1312 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) 926 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1313 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); 927 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0)
928 | (flags & EIO_MCL_ONFAULT ? MCL_ONFAULT : 0)
929 ;
1314 } 930 }
1315 931
1316 return mlockall (flags); 932 return mlockall (flags);
1317} 933}
1318#endif 934#endif
1369 intptr_t end = addr + len; 985 intptr_t end = addr + len;
1370 intptr_t page = eio_pagesize (); 986 intptr_t page = eio_pagesize ();
1371 987
1372 if (addr < end) 988 if (addr < end)
1373 if (flags & EIO_MT_MODIFY) /* modify */ 989 if (flags & EIO_MT_MODIFY) /* modify */
1374 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); 990 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < end && !EIO_CANCELLED (req));
1375 else 991 else
1376 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); 992 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < end && !EIO_CANCELLED (req));
1377 } 993 }
1378 994
1379 return 0; 995 return 0;
1380} 996}
1381 997
1396 req->result = req->offs == (off_t)-1 ? -1 : 0; 1012 req->result = req->offs == (off_t)-1 ? -1 : 0;
1397} 1013}
1398 1014
1399/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 1015/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1400static int 1016static int
1401eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1017eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1402{ 1018{
1019 char *res;
1403 const char *rel = path; 1020 const char *rel = path;
1404 char *res;
1405 char *tmp1, *tmp2; 1021 char *tmp1, *tmp2;
1406#if SYMLOOP_MAX > 32 1022#if SYMLOOP_MAX > 32
1407 int symlinks = SYMLOOP_MAX; 1023 int symlinks = SYMLOOP_MAX;
1408#else 1024#else
1409 int symlinks = 32; 1025 int symlinks = 32;
1415 1031
1416 errno = ENOENT; 1032 errno = ENOENT;
1417 if (!*rel) 1033 if (!*rel)
1418 return -1; 1034 return -1;
1419 1035
1420 res = tmpbuf_get (tmpbuf, PATH_MAX * 3); 1036 res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3);
1037#ifdef _WIN32
1038 if (_access (rel, 4) != 0)
1039 return -1;
1040
1041 symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0);
1042
1043 errno = ENAMETOOLONG;
1044 if (symlinks >= EIO_PATH_MAX * 3)
1045 return -1;
1046
1047 errno = EIO;
1048 if (symlinks <= 0)
1049 return -1;
1050
1051 return symlinks;
1052
1053#else
1421 tmp1 = res + PATH_MAX; 1054 tmp1 = res + EIO_PATH_MAX;
1422 tmp2 = tmp1 + PATH_MAX; 1055 tmp2 = tmp1 + EIO_PATH_MAX;
1423 1056
1424#if 0 /* disabled, the musl way to do things is just too racy */ 1057#if 0 /* disabled, the musl way to do things is just too racy */
1425#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1058#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1426 /* on linux we may be able to ask the kernel */ 1059 /* on linux we may be able to ask the kernel */
1427 { 1060 {
1428 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); 1061 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1429 1062
1430 if (fd >= 0) 1063 if (fd >= 0)
1431 { 1064 {
1432 sprintf (tmp1, "/proc/self/fd/%d", fd); 1065 sprintf (tmp1, "/proc/self/fd/%d", fd);
1433 req->result = readlink (tmp1, res, PATH_MAX); 1066 req->result = readlink (tmp1, res, EIO_PATH_MAX);
1434 close (fd);
1435
1436 /* here we should probably stat the open file and the disk file, to make sure they still match */ 1067 /* here we should probably stat the open file and the disk file, to make sure they still match */
1068 eio__close (fd);
1437 1069
1438 if (req->result > 0) 1070 if (req->result > 0)
1439 goto done; 1071 goto done;
1440 } 1072 }
1441 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) 1073 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1442 return; 1074 return -1;
1443 } 1075 }
1444#endif 1076#endif
1445#endif 1077#endif
1446 1078
1447 if (*rel != '/') 1079 if (*rel != '/')
1452 if (wd == EIO_INVALID_WD) 1084 if (wd == EIO_INVALID_WD)
1453 return -1; 1085 return -1;
1454 1086
1455 if (wd == EIO_CWD) 1087 if (wd == EIO_CWD)
1456 { 1088 {
1457 if (!getcwd (res, PATH_MAX)) 1089 if (!getcwd (res, EIO_PATH_MAX))
1458 return -1; 1090 return -1;
1459 1091
1460 len = strlen (res); 1092 len = strlen (res);
1461 } 1093 }
1462 else 1094 else
1509 1141
1510 /* zero-terminate, for readlink */ 1142 /* zero-terminate, for readlink */
1511 res [len + 1] = 0; 1143 res [len + 1] = 0;
1512 1144
1513 /* now check if it's a symlink */ 1145 /* now check if it's a symlink */
1514 linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); 1146 linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX);
1515 1147
1516 if (linklen < 0) 1148 if (linklen < 0)
1517 { 1149 {
1518 if (errno != EINVAL) 1150 if (errno != EINVAL)
1519 return -1; 1151 return -1;
1525 { 1157 {
1526 /* yay, it was a symlink - build new path in tmp2 */ 1158 /* yay, it was a symlink - build new path in tmp2 */
1527 int rellen = strlen (rel); 1159 int rellen = strlen (rel);
1528 1160
1529 errno = ENAMETOOLONG; 1161 errno = ENAMETOOLONG;
1530 if (linklen + 1 + rellen >= PATH_MAX) 1162 if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */
1531 return -1; 1163 return -1;
1532 1164
1533 errno = ELOOP; 1165 errno = ELOOP;
1534 if (!--symlinks) 1166 if (!--symlinks)
1535 return -1; 1167 return -1;
1549 /* special case for the lone root path */ 1181 /* special case for the lone root path */
1550 if (res == tmpbuf->ptr) 1182 if (res == tmpbuf->ptr)
1551 *res++ = '/'; 1183 *res++ = '/';
1552 1184
1553 return res - (char *)tmpbuf->ptr; 1185 return res - (char *)tmpbuf->ptr;
1186#endif
1554} 1187}
1555 1188
1556static signed char 1189static signed char
1557eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1190eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1558{ 1191{
1788 return; 1421 return;
1789 } 1422 }
1790 } 1423 }
1791#else 1424#else
1792 #if HAVE_AT 1425 #if HAVE_AT
1793 if (req->wd)
1794 { 1426 {
1795 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 1427 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1796 1428
1797 if (fd < 0) 1429 if (fd < 0)
1430 return;
1431
1432 dirp = fdopendir (fd);
1433
1434 if (!dirp)
1435 {
1436 silent_close (fd);
1798 return; 1437 return;
1799
1800 dirp = fdopendir (fd);
1801
1802 if (!dirp)
1803 close (fd);
1804 } 1438 }
1805 else 1439 }
1806 dirp = opendir (req->ptr1);
1807 #else 1440 #else
1808 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); 1441 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
1442
1443 if (!dirp)
1444 return;
1809 #endif 1445 #endif
1810
1811 if (!dirp)
1812 return;
1813#endif 1446#endif
1814 1447
1815 if (req->flags & EIO_FLAG_PTR1_FREE) 1448 if (req->flags & EIO_FLAG_PTR1_FREE)
1816 free (req->ptr1); 1449 free (req->ptr1);
1817 1450
1931 #ifdef DT_FIFO 1564 #ifdef DT_FIFO
1932 case DT_FIFO: ent->type = EIO_DT_FIFO; break; 1565 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1933 #endif 1566 #endif
1934 #ifdef DT_CHR 1567 #ifdef DT_CHR
1935 case DT_CHR: ent->type = EIO_DT_CHR; break; 1568 case DT_CHR: ent->type = EIO_DT_CHR; break;
1936 #endif 1569 #endif
1937 #ifdef DT_MPC 1570 #ifdef DT_MPC
1938 case DT_MPC: ent->type = EIO_DT_MPC; break; 1571 case DT_MPC: ent->type = EIO_DT_MPC; break;
1939 #endif 1572 #endif
1940 #ifdef DT_DIR 1573 #ifdef DT_DIR
1941 case DT_DIR: ent->type = EIO_DT_DIR; break; 1574 case DT_DIR: ent->type = EIO_DT_DIR; break;
1942 #endif 1575 #endif
1943 #ifdef DT_NAM 1576 #ifdef DT_NAM
1944 case DT_NAM: ent->type = EIO_DT_NAM; break; 1577 case DT_NAM: ent->type = EIO_DT_NAM; break;
1945 #endif 1578 #endif
1946 #ifdef DT_BLK 1579 #ifdef DT_BLK
1947 case DT_BLK: ent->type = EIO_DT_BLK; break; 1580 case DT_BLK: ent->type = EIO_DT_BLK; break;
1948 #endif 1581 #endif
1949 #ifdef DT_MPB 1582 #ifdef DT_MPB
1950 case DT_MPB: ent->type = EIO_DT_MPB; break; 1583 case DT_MPB: ent->type = EIO_DT_MPB; break;
1951 #endif 1584 #endif
1952 #ifdef DT_REG 1585 #ifdef DT_REG
1953 case DT_REG: ent->type = EIO_DT_REG; break; 1586 case DT_REG: ent->type = EIO_DT_REG; break;
1954 #endif 1587 #endif
1955 #ifdef DT_NWK 1588 #ifdef DT_NWK
1956 case DT_NWK: ent->type = EIO_DT_NWK; break; 1589 case DT_NWK: ent->type = EIO_DT_NWK; break;
1957 #endif 1590 #endif
1958 #ifdef DT_CMP 1591 #ifdef DT_CMP
1959 case DT_CMP: ent->type = EIO_DT_CMP; break; 1592 case DT_CMP: ent->type = EIO_DT_CMP; break;
1960 #endif 1593 #endif
1961 #ifdef DT_LNK 1594 #ifdef DT_LNK
1962 case DT_LNK: ent->type = EIO_DT_LNK; break; 1595 case DT_LNK: ent->type = EIO_DT_LNK; break;
1963 #endif 1596 #endif
1964 #ifdef DT_SOCK 1597 #ifdef DT_SOCK
1965 case DT_SOCK: ent->type = EIO_DT_SOCK; break; 1598 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
2014/* keep the absolute path in string form at all times */ 1647/* keep the absolute path in string form at all times */
2015/* fuck yeah. */ 1648/* fuck yeah. */
2016 1649
2017#if !HAVE_AT 1650#if !HAVE_AT
2018 1651
2019/* a bit like realpath, but usually faster because it doesn'T have to return */ 1652/* a bit like realpath, but usually faster because it doesn't have to return */
2020/* an absolute or canonical path */ 1653/* an absolute or canonical path */
2021static const char * 1654static const char *
2022wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1655wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2023{ 1656{
2024 if (!wd || *path == '/') 1657 if (!wd || *path == '/')
2025 return path; 1658 return path;
2026 1659
2027 if (path [0] == '.' && !path [1]) 1660 if (path [0] == '.' && !path [1])
2029 1662
2030 { 1663 {
2031 int l1 = wd->len; 1664 int l1 = wd->len;
2032 int l2 = strlen (path); 1665 int l2 = strlen (path);
2033 1666
2034 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1667 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
2035 1668
2036 memcpy (res, wd->str, l1); 1669 memcpy (res, wd->str, l1);
2037 res [l1] = '/'; 1670 res [l1] = '/';
2038 memcpy (res + l1 + 1, path, l2 + 1); 1671 memcpy (res + l1 + 1, path, l2 + 1);
2039 1672
2042} 1675}
2043 1676
2044#endif 1677#endif
2045 1678
2046static eio_wd 1679static eio_wd
2047eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1680eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2048{ 1681{
2049 int fd; 1682 int fd;
2050 eio_wd res; 1683 eio_wd res;
2051 int len = eio__realpath (tmpbuf, wd, path); 1684 int len = eio__realpath (tmpbuf, wd, path);
2052 1685
2053 if (len < 0) 1686 if (len < 0)
2054 return EIO_INVALID_WD; 1687 return EIO_INVALID_WD;
2055 1688
2056#if HAVE_AT 1689#if HAVE_AT
2057 fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 1690 fd = openat (WD2FD (wd), path, EIO_O_PATH | O_DIRECTORY | O_NONBLOCK | O_CLOEXEC);
1691
1692 /* 0 is a valid fd, but we use it for EIO_CWD, so in the very unlikely */
1693 /* case of fd 0 being available (almost certainly an a pplication bug) */
1694 /* make sure we use another fd value */
1695 #if EIO_CWD
1696 error EIO_CWD must be 0
1697 #endif
1698 if (ecb_expect_false (fd == 0))
1699 {
1700 int fd2 = fcntl (fd, F_DUPFD_CLOEXEC ? F_DUPFD_CLOEXEC : F_DUPFD);
1701 fcntl (fd2, F_SETFD, FD_CLOEXEC);
1702 eio__close (fd);
1703 fd = fd2;
1704 }
2058 1705
2059 if (fd < 0) 1706 if (fd < 0)
2060 return EIO_INVALID_WD; 1707 return EIO_INVALID_WD;
2061#endif 1708#endif
2062 1709
2074} 1721}
2075 1722
2076eio_wd 1723eio_wd
2077eio_wd_open_sync (eio_wd wd, const char *path) 1724eio_wd_open_sync (eio_wd wd, const char *path)
2078{ 1725{
2079 struct tmpbuf tmpbuf = { 0 }; 1726 struct etp_tmpbuf tmpbuf = { 0 };
2080 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1727 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2081 free (tmpbuf.ptr); 1728 free (tmpbuf.ptr);
2082 1729
2083 return wd; 1730 return wd;
2084} 1731}
2087eio_wd_close_sync (eio_wd wd) 1734eio_wd_close_sync (eio_wd wd)
2088{ 1735{
2089 if (wd != EIO_INVALID_WD && wd != EIO_CWD) 1736 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
2090 { 1737 {
2091 #if HAVE_AT 1738 #if HAVE_AT
2092 close (wd->fd); 1739 eio__close (wd->fd);
2093 #endif 1740 #endif
2094 free (wd); 1741 free (wd);
2095 } 1742 }
2096} 1743}
2097 1744
2098#if HAVE_AT 1745#if HAVE_AT
2099 1746
1747static int
1748eio__renameat2 (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags)
1749{
1750#if HAVE_RENAMEAT2
1751 return syscall (SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags);
1752#else
1753 if (flags)
1754 return EIO_ENOSYS ();
1755
1756 return renameat (olddirfd, oldpath, newdirfd, newpath);
1757#endif
1758}
1759
2100/* they forgot these */ 1760/* they forgot these */
2101 1761
2102static int 1762static int
2103eio__truncateat (int dirfd, const char *path, off_t length) 1763eio__truncateat (int dirfd, const char *path, off_t length)
2104{ 1764{
2105 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); 1765 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC | O_NONBLOCK);
2106 int res; 1766 int res;
2107 1767
2108 if (fd < 0) 1768 if (fd < 0)
2109 return fd; 1769 return fd;
2110 1770
2111 res = ftruncate (fd, length); 1771 res = ftruncate (fd, length);
2112 close (fd); 1772 silent_close (fd);
2113 return res; 1773 return res;
2114} 1774}
2115 1775
2116static int 1776static int
2117eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) 1777eio__statvfsat (int dirfd, const char *path, struct statvfs *buf)
2118{ 1778{
2119 int fd = openat (dirfd, path, O_SEARCH | O_CLOEXEC); 1779 int fd = openat (dirfd, path, EIO_O_PATH | O_CLOEXEC | O_NONBLOCK);
2120 int res; 1780 int res;
2121 1781
2122 if (fd < 0) 1782 if (fd < 0)
2123 return fd; 1783 return fd;
2124 1784
2125 res = fstatvfs (fd, buf); 1785 res = fstatvfs (fd, buf);
2126 close (fd); 1786 silent_close (fd);
2127 return res; 1787 return res;
2128
2129} 1788}
2130 1789
2131#endif 1790#endif
2132 1791
2133/*****************************************************************************/ 1792/*****************************************************************************/
2134 1793
2135#define ALLOC(len) \ 1794#define ALLOC(len) \
2136 if (!req->ptr2) \ 1795 if (!req->ptr2) \
2137 { \ 1796 { \
2138 X_LOCK (wrklock); \ 1797 X_LOCK (EIO_POOL->wrklock); \
2139 req->flags |= EIO_FLAG_PTR2_FREE; \ 1798 req->flags |= EIO_FLAG_PTR2_FREE; \
2140 X_UNLOCK (wrklock); \ 1799 X_UNLOCK (EIO_POOL->wrklock); \
2141 req->ptr2 = malloc (len); \ 1800 req->ptr2 = malloc (len); \
2142 if (!req->ptr2) \ 1801 if (!req->ptr2) \
2143 { \ 1802 { \
2144 errno = ENOMEM; \ 1803 errno = ENOMEM; \
2145 req->result = -1; \ 1804 req->result = -1; \
2146 break; \ 1805 goto alloc_fail; \
2147 } \ 1806 } \
2148 } 1807 }
2149 1808
2150static void ecb_noinline ecb_cold 1809/*****************************************************************************/
2151etp_proc_init (void)
2152{
2153#if HAVE_PRCTL_SET_NAME
2154 /* provide a more sensible "thread name" */
2155 char name[16 + 1];
2156 const int namelen = sizeof (name) - 1;
2157 int len;
2158 1810
2159 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); 1811static void
2160 name [namelen] = 0; 1812eio__slurp (int fd, eio_req *req)
2161 len = strlen (name);
2162 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2163 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2164#endif
2165}
2166
2167X_THREAD_PROC (etp_proc)
2168{ 1813{
2169 ETP_REQ *req; 1814 req->result = fd;
2170 struct timespec ts;
2171 etp_worker *self = (etp_worker *)thr_arg;
2172 1815
2173 etp_proc_init (); 1816 if (fd < 0)
1817 return;
2174 1818
2175 /* try to distribute timeouts somewhat evenly */ 1819 if (req->offs < 0 || !req->size) /* do we need the size? */
2176 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2177
2178 for (;;)
2179 { 1820 {
2180 ts.tv_sec = 0; 1821 off_t size = lseek (fd, 0, SEEK_END);
2181 1822
2182 X_LOCK (reqlock);
2183
2184 for (;;)
2185 {
2186 req = reqq_shift (&req_queue);
2187
2188 if (req)
2189 break;
2190
2191 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2192 {
2193 X_UNLOCK (reqlock);
2194 X_LOCK (wrklock);
2195 --started;
2196 X_UNLOCK (wrklock);
2197 goto quit;
2198 }
2199
2200 ++idle;
2201
2202 if (idle <= max_idle)
2203 /* we are allowed to idle, so do so without any timeout */
2204 X_COND_WAIT (reqwait, reqlock);
2205 else
2206 {
2207 /* initialise timeout once */
2208 if (!ts.tv_sec)
2209 ts.tv_sec = time (0) + idle_timeout;
2210
2211 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2212 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2213 }
2214
2215 --idle;
2216 }
2217
2218 --nready;
2219
2220 X_UNLOCK (reqlock);
2221
2222 if (req->type < 0) 1823 if (req->offs < 0)
2223 goto quit; 1824 req->offs += size;
2224 1825
2225 ETP_EXECUTE (self, req); 1826 if (!req->size)
2226 1827 req->size = size - req->offs;
2227 X_LOCK (reslock);
2228
2229 ++npending;
2230
2231 if (!reqq_push (&res_queue, req) && want_poll_cb)
2232 want_poll_cb ();
2233
2234 etp_worker_clear (self);
2235
2236 X_UNLOCK (reslock);
2237 } 1828 }
2238 1829
2239quit: 1830 ALLOC (req->size);
2240 free (req); 1831 req->result = pread (fd, req->ptr2, req->size, req->offs);
2241 1832
2242 X_LOCK (wrklock); 1833 silent_close (fd);
2243 etp_worker_free (self);
2244 X_UNLOCK (wrklock);
2245}
2246 1834
2247/*****************************************************************************/ 1835alloc_fail:
1836 ;
1837}
2248 1838
2249int ecb_cold 1839int ecb_cold
2250eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1840eio_init (void (*want_poll)(void), void (*done_poll)(void))
2251{ 1841{
2252#if !HAVE_PREADWRITE 1842 eio_want_poll_cb = want_poll;
2253 X_MUTEX_CREATE (preadwritelock); 1843 eio_done_poll_cb = done_poll;
2254#endif
2255 1844
2256 return etp_init (want_poll, done_poll); 1845 return etp_init (EIO_POOL, 0, 0, 0);
2257} 1846}
2258 1847
2259ecb_inline void 1848ecb_inline void
2260eio_api_destroy (eio_req *req) 1849eio_api_destroy (eio_req *req)
2261{ 1850{
2262 free (req); 1851 free (req);
2263} 1852}
2264 1853
2265#define REQ(rtype) \ 1854#define REQ(rtype) \
2266 eio_req *req; \ 1855 eio_req *req; \
2267 \ 1856 \
2268 req = (eio_req *)calloc (1, sizeof *req); \ 1857 req = (eio_req *)calloc (1, sizeof *req); \
2269 if (!req) \ 1858 if (!req) \
2270 return 0; \ 1859 return 0; \
2284 { \ 1873 { \
2285 eio_api_destroy (req); \ 1874 eio_api_destroy (req); \
2286 return 0; \ 1875 return 0; \
2287 } 1876 }
2288 1877
1878#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)])
1879
2289static void 1880static void
2290eio_execute (etp_worker *self, eio_req *req) 1881eio_execute (etp_worker *self, eio_req *req)
2291{ 1882{
2292#if HAVE_AT 1883#if HAVE_AT
2293 int dirfd; 1884 int dirfd;
2333 : read (req->int1, req->ptr2, req->size); break; 1924 : read (req->int1, req->ptr2, req->size); break;
2334 case EIO_WRITE: req->result = req->offs >= 0 1925 case EIO_WRITE: req->result = req->offs >= 0
2335 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1926 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
2336 : write (req->int1, req->ptr2, req->size); break; 1927 : write (req->int1, req->ptr2, req->size); break;
2337 1928
1929 case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break;
1930 case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break;
1931
2338 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 1932 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
2339 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; 1933 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
2340 1934
2341#if HAVE_AT 1935#if HAVE_AT
2342 1936
2346 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; 1940 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break;
2347 case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; 1941 case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break;
2348 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; 1942 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break;
2349 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; 1943 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break;
2350 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; 1944 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
1945 case EIO_SLURP: eio__slurp ( openat (dirfd, req->ptr1, O_RDONLY | O_CLOEXEC), req); break;
2351 1946
2352 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1947 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2353 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 1948 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
1949 req->result = req->wd && SINGLEDOT (req->ptr1)
1950 ? rmdir (req->wd->str)
1951 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2354 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 1952 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
2355 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 1953 case EIO_RENAME: req->result = eio__renameat2 (
1954 dirfd,
1955 /* complications arise because "." cannot be renamed, so we might have to expand */
1956 req->wd && SINGLEDOT (req->ptr1) ? req->wd->str : req->ptr1,
1957 WD2FD ((eio_wd)req->int3),
1958 req->ptr2,
1959 req->int2
1960 );
1961 break;
2356 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; 1962 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break;
2357 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; 1963 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break;
2358 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 1964 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
2359 case EIO_READLINK: ALLOC (PATH_MAX);
2360 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2361 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1965 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2362 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; 1966 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
1967 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
1968 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX);
1969 if (req->result == EIO_PATH_MAX)
1970 {
1971 req->result = -1;
1972 errno = ENAMETOOLONG;
1973 }
1974 break;
2363 case EIO_UTIME: 1975 case EIO_UTIME:
2364 case EIO_FUTIME: 1976 case EIO_FUTIME:
2365 { 1977 {
2366 struct timespec ts[2]; 1978 struct timespec ts[2];
2367 struct timespec *times; 1979 struct timespec *times;
2392 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 2004 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2393 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; 2005 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break;
2394 case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break; 2006 case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break;
2395 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; 2007 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break;
2396 case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; 2008 case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break;
2009 case EIO_SLURP: eio__slurp ( open (path , O_RDONLY | O_CLOEXEC), req); break;
2397 2010
2398 case EIO_UNLINK: req->result = unlink (path ); break; 2011 case EIO_UNLINK: req->result = unlink (path ); break;
2399 case EIO_RMDIR: req->result = rmdir (path ); break; 2012 case EIO_RMDIR: req->result = rmdir (path ); break;
2400 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; 2013 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break;
2401 case EIO_RENAME: req->result = rename (path , req->ptr2); break; 2014 case EIO_RENAME: req->result = req->int2 ? EIO_ENOSYS () : rename (path, req->ptr2); break;
2402 case EIO_LINK: req->result = link (path , req->ptr2); break; 2015 case EIO_LINK: req->result = link (path , req->ptr2); break;
2403 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; 2016 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break;
2404 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; 2017 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break;
2405 case EIO_READLINK: ALLOC (PATH_MAX);
2406 req->result = readlink (path, req->ptr2, PATH_MAX); break;
2407 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 2018 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2408 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; 2019 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break;
2020 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
2021 req->result = readlink (path, req->ptr2, EIO_PATH_MAX);
2022 if (req->result == EIO_PATH_MAX)
2023 {
2024 req->result = -1;
2025 errno = ENAMETOOLONG;
2026 }
2027 break;
2409 2028
2410 case EIO_UTIME: 2029 case EIO_UTIME:
2411 case EIO_FUTIME: 2030 case EIO_FUTIME:
2412 { 2031 {
2413 struct timeval tv[2]; 2032 struct timeval tv[2];
2448 2067
2449 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; 2068 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
2450 case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; 2069 case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break;
2451 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; 2070 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
2452 2071
2453 case EIO_CLOSE: req->result = close (req->int1); break; 2072 case EIO_CLOSE: req->result = eio__close (req->int1); break;
2454 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; 2073 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
2455 case EIO_SYNC: req->result = 0; sync (); break; 2074 case EIO_SYNC: req->result = 0; sync (); break;
2456 case EIO_FSYNC: req->result = fsync (req->int1); break; 2075 case EIO_FSYNC: req->result = fsync (req->int1); break;
2457 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 2076 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
2458 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; 2077 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break;
2478 req->result = select (0, 0, 0, 0, &tv); 2097 req->result = select (0, 0, 0, 0, &tv);
2479 } 2098 }
2480#endif 2099#endif
2481 break; 2100 break;
2482 2101
2102#if 0
2483 case EIO_GROUP: 2103 case EIO_GROUP:
2484 abort (); /* handled in eio_request */ 2104 abort (); /* handled in eio_request */
2105#endif
2485 2106
2486 case EIO_NOP: 2107 case EIO_NOP:
2487 req->result = 0; 2108 req->result = 0;
2488 break; 2109 break;
2489 2110
2494 default: 2115 default:
2495 req->result = EIO_ENOSYS (); 2116 req->result = EIO_ENOSYS ();
2496 break; 2117 break;
2497 } 2118 }
2498 2119
2120alloc_fail:
2499 req->errorno = errno; 2121 req->errorno = errno;
2500} 2122}
2501 2123
2502#ifndef EIO_NO_WRAPPERS 2124#ifndef EIO_NO_WRAPPERS
2503 2125
2592} 2214}
2593 2215
2594eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2216eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2595{ 2217{
2596 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2218 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2219}
2220
2221eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data)
2222{
2223 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND;
2224}
2225
2226eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data)
2227{
2228 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND;
2597} 2229}
2598 2230
2599eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) 2231eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
2600{ 2232{
2601 REQ (EIO_FSTAT); req->int1 = fd; SEND; 2233 REQ (EIO_FSTAT); req->int1 = fd; SEND;
2746eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2378eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2747{ 2379{
2748 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2380 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2749} 2381}
2750 2382
2383eio_req *eio_slurp (const char *path, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2384{
2385 REQ (EIO_SLURP); PATH; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2386}
2387
2751eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) 2388eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2752{ 2389{
2753 REQ (EIO_CUSTOM); req->feed = execute; SEND; 2390 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2754} 2391}
2755 2392
2789void 2426void
2790eio_grp_add (eio_req *grp, eio_req *req) 2427eio_grp_add (eio_req *grp, eio_req *req)
2791{ 2428{
2792 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2429 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2793 2430
2794 grp->flags |= EIO_FLAG_GROUPADD; 2431 grp->flags |= ETP_FLAG_GROUPADD;
2795 2432
2796 ++grp->size; 2433 ++grp->size;
2797 req->grp = grp; 2434 req->grp = grp;
2798 2435
2799 req->grp_prev = 0; 2436 req->grp_prev = 0;
2812eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2449eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2813{ 2450{
2814 return eio__sendfile (ofd, ifd, offset, count); 2451 return eio__sendfile (ofd, ifd, offset, count);
2815} 2452}
2816 2453
2454int eio_mlockall_sync (int flags)
2455{
2456 return eio__mlockall (flags);
2457}
2458

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines