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

Comparing libeio/eio.c (file contents):
Revision 1.104 by root, Mon Sep 26 16:54:25 2011 UTC vs.
Revision 1.143 by root, Sat Dec 3 16:33:46 2016 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 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 };
196 233
197#else 234#else
198 235
199 #include <sys/time.h> 236 #include <sys/time.h>
200 #include <sys/select.h> 237 #include <sys/select.h>
201 #include <sys/statvfs.h>
202 #include <unistd.h> 238 #include <unistd.h>
203 #include <signal.h> 239 #include <signal.h>
204 #include <dirent.h> 240 #include <dirent.h>
205 241
242 #ifdef ANDROID
243 #include <sys/vfs.h>
244 #define statvfs statfs
245 #define fstatvfs fstatfs
246 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */
247 #else
248 #include <sys/statvfs.h>
249 #endif
250
206 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 251 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
207 #include <sys/mman.h> 252 #include <sys/mman.h>
208 #endif 253 #endif
209 254
210 #define D_NAME(entp) entp->d_name 255 #define D_NAME(entp) entp->d_name
211 256
212 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 257 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
213 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 258 #if __FreeBSD__ || __NetBSD__ || __OpenBSD__
214 #define _DIRENT_HAVE_D_TYPE /* sigh */ 259 #define _DIRENT_HAVE_D_TYPE /* sigh */
215 #define D_INO(de) (de)->d_fileno 260 #define D_INO(de) (de)->d_fileno
216 #define D_NAMLEN(de) (de)->d_namlen 261 #define D_NAMLEN(de) (de)->d_namlen
217 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 262 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
218 #define D_INO(de) (de)->d_ino 263 #define D_INO(de) (de)->d_ino
237# include <utime.h> 282# include <utime.h>
238#endif 283#endif
239 284
240#if HAVE_SYS_SYSCALL_H 285#if HAVE_SYS_SYSCALL_H
241# include <sys/syscall.h> 286# include <sys/syscall.h>
242#endif
243
244#if HAVE_SYS_PRCTL_H
245# include <sys/prctl.h>
246#endif 287#endif
247 288
248#if HAVE_SENDFILE 289#if HAVE_SENDFILE
249# if __linux 290# if __linux
250# include <sys/sendfile.h> 291# include <sys/sendfile.h>
281#endif 322#endif
282 323
283/* buffer size for various temporary buffers */ 324/* buffer size for various temporary buffers */
284#define EIO_BUFSIZE 65536 325#define EIO_BUFSIZE 65536
285 326
286#define dBUF \ 327#define dBUF \
287 char *eio_buf = malloc (EIO_BUFSIZE); \ 328 char *eio_buf = malloc (EIO_BUFSIZE); \
288 errno = ENOMEM; \ 329 errno = ENOMEM; \
289 if (!eio_buf) \ 330 if (!eio_buf) \
290 return -1 331 return -1
291 332
292#define FUBd \ 333#define FUBd \
293 free (eio_buf) 334 free (eio_buf)
294 335
295#define EIO_TICKS ((1000000 + 1023) >> 10)
296
297/*****************************************************************************/ 336/*****************************************************************************/
298 337
299struct tmpbuf 338struct etp_tmpbuf;
339
340#if _POSIX_VERSION >= 200809L
341 #define HAVE_AT 1
342 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
343 #ifndef O_SEARCH
344 #define O_SEARCH O_RDONLY
345 #endif
346#else
347 #define HAVE_AT 0
348 static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path);
349#endif
350
351struct eio_pwd
300{ 352{
301 void *ptr; 353#if HAVE_AT
354 int fd;
355#endif
302 int len; 356 int len;
357 char str[1]; /* actually, a 0-terminated canonical path */
303}; 358};
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 359
317/*****************************************************************************/ 360/*****************************************************************************/
318 361
319#define ETP_PRI_MIN EIO_PRI_MIN 362#define ETP_PRI_MIN EIO_PRI_MIN
320#define ETP_PRI_MAX EIO_PRI_MAX 363#define ETP_PRI_MAX EIO_PRI_MAX
321 364
365#define ETP_TYPE_QUIT -1
366#define ETP_TYPE_GROUP EIO_GROUP
367
368static void eio_nop_callback (void) { }
369static void (*eio_want_poll_cb)(void) = eio_nop_callback;
370static void (*eio_done_poll_cb)(void) = eio_nop_callback;
371
372#define ETP_WANT_POLL(pool) eio_want_poll_cb ()
373#define ETP_DONE_POLL(pool) eio_done_poll_cb ()
374
322struct etp_worker; 375struct etp_worker;
323
324#define ETP_REQ eio_req 376#define ETP_REQ eio_req
325#define ETP_DESTROY(req) eio_destroy (req) 377#define ETP_DESTROY(req) eio_destroy (req)
326static int eio_finish (eio_req *req); 378static int eio_finish (eio_req *req);
327#define ETP_FINISH(req) eio_finish (req) 379#define ETP_FINISH(req) eio_finish (req)
328static void eio_execute (struct etp_worker *self, eio_req *req); 380static void eio_execute (struct etp_worker *self, eio_req *req);
329#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 381#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
330 382
331/*****************************************************************************/ 383#include "etp.c"
332 384
333#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 385static struct etp_pool eio_pool;
334 386#define EIO_POOL (&eio_pool)
335/* calculate time difference in ~1/EIO_TICKS of a second */
336ecb_inline int
337tvdiff (struct timeval *tv1, struct timeval *tv2)
338{
339 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
340 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
341}
342
343static unsigned int started, idle, wanted = 4;
344
345static void (*want_poll_cb) (void);
346static void (*done_poll_cb) (void);
347
348static unsigned int max_poll_time; /* reslock */
349static unsigned int max_poll_reqs; /* reslock */
350
351static unsigned int nreqs; /* reqlock */
352static unsigned int nready; /* reqlock */
353static unsigned int npending; /* reqlock */
354static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
355static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
356
357static xmutex_t wrklock;
358static xmutex_t reslock;
359static xmutex_t reqlock;
360static xcond_t reqwait;
361
362#if !HAVE_PREADWRITE
363/*
364 * make our pread/pwrite emulation safe against themselves, but not against
365 * normal read/write by using a mutex. slows down execution a lot,
366 * but that's your problem, not mine.
367 */
368static xmutex_t preadwritelock;
369#endif
370
371typedef struct etp_worker
372{
373 struct tmpbuf tmpbuf;
374
375 /* locked by wrklock */
376 struct etp_worker *prev, *next;
377
378 xthread_t tid;
379
380#ifdef ETP_WORKER_COMMON
381 ETP_WORKER_COMMON
382#endif
383} etp_worker;
384
385static etp_worker wrk_first; /* NOT etp */
386
387#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
388#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
389
390/* worker threads management */
391
392static void ecb_cold
393etp_worker_clear (etp_worker *wrk)
394{
395}
396
397static void ecb_cold
398etp_worker_free (etp_worker *wrk)
399{
400 free (wrk->tmpbuf.ptr);
401
402 wrk->next->prev = wrk->prev;
403 wrk->prev->next = wrk->next;
404
405 free (wrk);
406}
407
408static unsigned int
409etp_nreqs (void)
410{
411 int retval;
412 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
413 retval = nreqs;
414 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
415 return retval;
416}
417
418static unsigned int
419etp_nready (void)
420{
421 unsigned int retval;
422
423 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
424 retval = nready;
425 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
426
427 return retval;
428}
429
430static unsigned int
431etp_npending (void)
432{
433 unsigned int retval;
434
435 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
436 retval = npending;
437 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
438
439 return retval;
440}
441
442static unsigned int
443etp_nthreads (void)
444{
445 unsigned int retval;
446
447 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
448 retval = started;
449 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
450
451 return retval;
452}
453
454/*
455 * a somewhat faster data structure might be nice, but
456 * with 8 priorities this actually needs <20 insns
457 * per shift, the most expensive operation.
458 */
459typedef struct {
460 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
461 int size;
462} etp_reqq;
463
464static etp_reqq req_queue;
465static etp_reqq res_queue;
466
467static void ecb_noinline ecb_cold
468reqq_init (etp_reqq *q)
469{
470 int pri;
471
472 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
473 q->qs[pri] = q->qe[pri] = 0;
474
475 q->size = 0;
476}
477
478static int ecb_noinline
479reqq_push (etp_reqq *q, ETP_REQ *req)
480{
481 int pri = req->pri;
482 req->next = 0;
483
484 if (q->qe[pri])
485 {
486 q->qe[pri]->next = req;
487 q->qe[pri] = req;
488 }
489 else
490 q->qe[pri] = q->qs[pri] = req;
491
492 return q->size++;
493}
494
495static ETP_REQ * ecb_noinline
496reqq_shift (etp_reqq *q)
497{
498 int pri;
499
500 if (!q->size)
501 return 0;
502
503 --q->size;
504
505 for (pri = ETP_NUM_PRI; pri--; )
506 {
507 eio_req *req = q->qs[pri];
508
509 if (req)
510 {
511 if (!(q->qs[pri] = (eio_req *)req->next))
512 q->qe[pri] = 0;
513
514 return req;
515 }
516 }
517
518 abort ();
519}
520
521static int ecb_cold
522etp_init (void (*want_poll)(void), void (*done_poll)(void))
523{
524 X_MUTEX_CREATE (wrklock);
525 X_MUTEX_CREATE (reslock);
526 X_MUTEX_CREATE (reqlock);
527 X_COND_CREATE (reqwait);
528
529 reqq_init (&req_queue);
530 reqq_init (&res_queue);
531
532 wrk_first.next =
533 wrk_first.prev = &wrk_first;
534
535 started = 0;
536 idle = 0;
537 nreqs = 0;
538 nready = 0;
539 npending = 0;
540
541 want_poll_cb = want_poll;
542 done_poll_cb = done_poll;
543
544 return 0;
545}
546
547X_THREAD_PROC (etp_proc);
548
549static void ecb_cold
550etp_start_thread (void)
551{
552 etp_worker *wrk = calloc (1, sizeof (etp_worker));
553
554 /*TODO*/
555 assert (("unable to allocate worker thread data", wrk));
556
557 X_LOCK (wrklock);
558
559 if (thread_create (&wrk->tid, etp_proc, (void *)wrk))
560 {
561 wrk->prev = &wrk_first;
562 wrk->next = wrk_first.next;
563 wrk_first.next->prev = wrk;
564 wrk_first.next = wrk;
565 ++started;
566 }
567 else
568 free (wrk);
569
570 X_UNLOCK (wrklock);
571}
572
573static void
574etp_maybe_start_thread (void)
575{
576 if (ecb_expect_true (etp_nthreads () >= wanted))
577 return;
578
579 /* todo: maybe use idle here, but might be less exact */
580 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
581 return;
582
583 etp_start_thread ();
584}
585
586static void ecb_cold
587etp_end_thread (void)
588{
589 eio_req *req = calloc (1, sizeof (eio_req));
590
591 req->type = -1;
592 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
593
594 X_LOCK (reqlock);
595 reqq_push (&req_queue, req);
596 X_COND_SIGNAL (reqwait);
597 X_UNLOCK (reqlock);
598
599 X_LOCK (wrklock);
600 --started;
601 X_UNLOCK (wrklock);
602}
603
604static int
605etp_poll (void)
606{
607 unsigned int maxreqs;
608 unsigned int maxtime;
609 struct timeval tv_start, tv_now;
610
611 X_LOCK (reslock);
612 maxreqs = max_poll_reqs;
613 maxtime = max_poll_time;
614 X_UNLOCK (reslock);
615
616 if (maxtime)
617 gettimeofday (&tv_start, 0);
618
619 for (;;)
620 {
621 ETP_REQ *req;
622
623 etp_maybe_start_thread ();
624
625 X_LOCK (reslock);
626 req = reqq_shift (&res_queue);
627
628 if (req)
629 {
630 --npending;
631
632 if (!res_queue.size && done_poll_cb)
633 done_poll_cb ();
634 }
635
636 X_UNLOCK (reslock);
637
638 if (!req)
639 return 0;
640
641 X_LOCK (reqlock);
642 --nreqs;
643 X_UNLOCK (reqlock);
644
645 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
646 {
647 req->int1 = 1; /* mark request as delayed */
648 continue;
649 }
650 else
651 {
652 int res = ETP_FINISH (req);
653 if (ecb_expect_false (res))
654 return res;
655 }
656
657 if (ecb_expect_false (maxreqs && !--maxreqs))
658 break;
659
660 if (maxtime)
661 {
662 gettimeofday (&tv_now, 0);
663
664 if (tvdiff (&tv_start, &tv_now) >= maxtime)
665 break;
666 }
667 }
668
669 errno = EAGAIN;
670 return -1;
671}
672
673static void
674etp_cancel (ETP_REQ *req)
675{
676 req->cancelled = 1;
677
678 eio_grp_cancel (req);
679}
680
681static void
682etp_submit (ETP_REQ *req)
683{
684 req->pri -= ETP_PRI_MIN;
685
686 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
687 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
688
689 if (ecb_expect_false (req->type == EIO_GROUP))
690 {
691 /* I hope this is worth it :/ */
692 X_LOCK (reqlock);
693 ++nreqs;
694 X_UNLOCK (reqlock);
695
696 X_LOCK (reslock);
697
698 ++npending;
699
700 if (!reqq_push (&res_queue, req) && want_poll_cb)
701 want_poll_cb ();
702
703 X_UNLOCK (reslock);
704 }
705 else
706 {
707 X_LOCK (reqlock);
708 ++nreqs;
709 ++nready;
710 reqq_push (&req_queue, req);
711 X_COND_SIGNAL (reqwait);
712 X_UNLOCK (reqlock);
713
714 etp_maybe_start_thread ();
715 }
716}
717
718static void ecb_cold
719etp_set_max_poll_time (double nseconds)
720{
721 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
722 max_poll_time = nseconds * EIO_TICKS;
723 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
724}
725
726static void ecb_cold
727etp_set_max_poll_reqs (unsigned int maxreqs)
728{
729 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
730 max_poll_reqs = maxreqs;
731 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
732}
733
734static void ecb_cold
735etp_set_max_idle (unsigned int nthreads)
736{
737 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
738 max_idle = nthreads;
739 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
740}
741
742static void ecb_cold
743etp_set_idle_timeout (unsigned int seconds)
744{
745 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
746 idle_timeout = seconds;
747 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
748}
749
750static void ecb_cold
751etp_set_min_parallel (unsigned int nthreads)
752{
753 if (wanted < nthreads)
754 wanted = nthreads;
755}
756
757static void ecb_cold
758etp_set_max_parallel (unsigned int nthreads)
759{
760 if (wanted > nthreads)
761 wanted = nthreads;
762
763 while (started > wanted)
764 etp_end_thread ();
765}
766 387
767/*****************************************************************************/ 388/*****************************************************************************/
768 389
769static void 390static void
770grp_try_feed (eio_req *grp) 391grp_try_feed (eio_req *grp)
771{ 392{
772 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 393 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
773 { 394 {
774 grp->flags &= ~EIO_FLAG_GROUPADD; 395 grp->flags &= ~ETP_FLAG_GROUPADD;
775 396
776 EIO_FEED (grp); 397 EIO_FEED (grp);
777 398
778 /* stop if no progress has been made */ 399 /* stop if no progress has been made */
779 if (!(grp->flags & EIO_FLAG_GROUPADD)) 400 if (!(grp->flags & ETP_FLAG_GROUPADD))
780 { 401 {
781 grp->feed = 0; 402 grp->feed = 0;
782 break; 403 break;
783 } 404 }
784 } 405 }
791 412
792 /* call feeder, if applicable */ 413 /* call feeder, if applicable */
793 grp_try_feed (grp); 414 grp_try_feed (grp);
794 415
795 /* finish, if done */ 416 /* finish, if done */
796 if (!grp->size && grp->int1) 417 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
797 return eio_finish (grp); 418 return eio_finish (grp);
798 else 419 else
799 return 0; 420 return 0;
800} 421}
801 422
837} 458}
838 459
839void 460void
840eio_grp_cancel (eio_req *grp) 461eio_grp_cancel (eio_req *grp)
841{ 462{
842 for (grp = grp->grp_first; grp; grp = grp->grp_next) 463 etp_grp_cancel (EIO_POOL, grp);
843 eio_cancel (grp);
844} 464}
845 465
846void 466void
847eio_cancel (eio_req *req) 467eio_cancel (eio_req *req)
848{ 468{
849 etp_cancel (req); 469 etp_cancel (EIO_POOL, req);
850} 470}
851 471
852void 472void
853eio_submit (eio_req *req) 473eio_submit (eio_req *req)
854{ 474{
855 etp_submit (req); 475 etp_submit (EIO_POOL, req);
856} 476}
857 477
858unsigned int 478unsigned int
859eio_nreqs (void) 479eio_nreqs (void)
860{ 480{
861 return etp_nreqs (); 481 return etp_nreqs (EIO_POOL);
862} 482}
863 483
864unsigned int 484unsigned int
865eio_nready (void) 485eio_nready (void)
866{ 486{
867 return etp_nready (); 487 return etp_nready (EIO_POOL);
868} 488}
869 489
870unsigned int 490unsigned int
871eio_npending (void) 491eio_npending (void)
872{ 492{
873 return etp_npending (); 493 return etp_npending (EIO_POOL);
874} 494}
875 495
876unsigned int ecb_cold 496unsigned int ecb_cold
877eio_nthreads (void) 497eio_nthreads (void)
878{ 498{
879 return etp_nthreads (); 499 return etp_nthreads (EIO_POOL);
880} 500}
881 501
882void ecb_cold 502void ecb_cold
883eio_set_max_poll_time (double nseconds) 503eio_set_max_poll_time (double nseconds)
884{ 504{
885 etp_set_max_poll_time (nseconds); 505 etp_set_max_poll_time (EIO_POOL, nseconds);
886} 506}
887 507
888void ecb_cold 508void ecb_cold
889eio_set_max_poll_reqs (unsigned int maxreqs) 509eio_set_max_poll_reqs (unsigned int maxreqs)
890{ 510{
891 etp_set_max_poll_reqs (maxreqs); 511 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
892} 512}
893 513
894void ecb_cold 514void ecb_cold
895eio_set_max_idle (unsigned int nthreads) 515eio_set_max_idle (unsigned int nthreads)
896{ 516{
897 etp_set_max_idle (nthreads); 517 etp_set_max_idle (EIO_POOL, nthreads);
898} 518}
899 519
900void ecb_cold 520void ecb_cold
901eio_set_idle_timeout (unsigned int seconds) 521eio_set_idle_timeout (unsigned int seconds)
902{ 522{
903 etp_set_idle_timeout (seconds); 523 etp_set_idle_timeout (EIO_POOL, seconds);
904} 524}
905 525
906void ecb_cold 526void ecb_cold
907eio_set_min_parallel (unsigned int nthreads) 527eio_set_min_parallel (unsigned int nthreads)
908{ 528{
909 etp_set_min_parallel (nthreads); 529 etp_set_min_parallel (EIO_POOL, nthreads);
910} 530}
911 531
912void ecb_cold 532void ecb_cold
913eio_set_max_parallel (unsigned int nthreads) 533eio_set_max_parallel (unsigned int nthreads)
914{ 534{
915 etp_set_max_parallel (nthreads); 535 etp_set_max_parallel (EIO_POOL, nthreads);
916} 536}
917 537
918int eio_poll (void) 538int eio_poll (void)
919{ 539{
920 return etp_poll (); 540 return etp_poll (EIO_POOL);
921} 541}
922 542
923/*****************************************************************************/ 543/*****************************************************************************/
924/* work around various missing functions */ 544/* work around various missing functions */
925
926#if !HAVE_PREADWRITE
927# undef pread
928# undef pwrite
929# define pread eio__pread
930# define pwrite eio__pwrite
931
932static eio_ssize_t
933eio__pread (int fd, void *buf, size_t count, off_t offset)
934{
935 eio_ssize_t res;
936 off_t ooffset;
937
938 X_LOCK (preadwritelock);
939 ooffset = lseek (fd, 0, SEEK_CUR);
940 lseek (fd, offset, SEEK_SET);
941 res = read (fd, buf, count);
942 lseek (fd, ooffset, SEEK_SET);
943 X_UNLOCK (preadwritelock);
944
945 return res;
946}
947
948static eio_ssize_t
949eio__pwrite (int fd, void *buf, size_t count, off_t offset)
950{
951 eio_ssize_t res;
952 off_t ooffset;
953
954 X_LOCK (preadwritelock);
955 ooffset = lseek (fd, 0, SEEK_CUR);
956 lseek (fd, offset, SEEK_SET);
957 res = write (fd, buf, count);
958 lseek (fd, ooffset, SEEK_SET);
959 X_UNLOCK (preadwritelock);
960
961 return res;
962}
963#endif
964 545
965#ifndef HAVE_UTIMES 546#ifndef HAVE_UTIMES
966 547
967# undef utimes 548# undef utimes
968# define utimes(path,times) eio__utimes (path, times) 549# define utimes(path,times) eio__utimes (path, times)
1010 int res; 591 int res;
1011 592
1012#if HAVE_SYS_SYNCFS 593#if HAVE_SYS_SYNCFS
1013 res = (int)syscall (__NR_syncfs, (int)(fd)); 594 res = (int)syscall (__NR_syncfs, (int)(fd));
1014#else 595#else
1015 res = -1; 596 res = EIO_ENOSYS ();
1016 errno = ENOSYS;
1017#endif 597#endif
1018 598
1019 if (res < 0 && errno == ENOSYS && fd >= 0) 599 if (res < 0 && errno == ENOSYS && fd >= 0)
1020 sync (); 600 sync ();
1021 601
1051} 631}
1052 632
1053static int 633static int
1054eio__fallocate (int fd, int mode, off_t offset, size_t len) 634eio__fallocate (int fd, int mode, off_t offset, size_t len)
1055{ 635{
1056#if HAVE_FALLOCATE 636#if HAVE_LINUX_FALLOCATE
1057 return fallocate (fd, mode, offset, len); 637 return fallocate (fd, mode, offset, len);
1058#else 638#else
1059 errno = ENOSYS; 639 return EIO_ENOSYS ();
1060 return -1;
1061#endif 640#endif
1062} 641}
1063 642
1064#if !HAVE_READAHEAD 643#if !HAVE_READAHEAD
1065# undef readahead 644# undef readahead
1080 todo -= len; 659 todo -= len;
1081 } 660 }
1082 661
1083 FUBd; 662 FUBd;
1084 663
1085 errno = 0; 664 /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */
665 /* but not for e.g. EIO or eof, so we also never fail */
1086 return count; 666 return 0;
1087} 667}
1088 668
1089#endif 669#endif
1090 670
1091/* sendfile always needs emulation */ 671/* sendfile always needs emulation */
1126 706
1127 /* according to source inspection, this is correct, and useful behaviour */ 707 /* according to source inspection, this is correct, and useful behaviour */
1128 if (sbytes) 708 if (sbytes)
1129 res = sbytes; 709 res = sbytes;
1130 710
1131# elif defined (__APPLE__) 711# elif defined __APPLE__
1132 off_t sbytes = count; 712 off_t sbytes = count;
1133 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 713 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
1134 714
1135 /* according to the manpage, sbytes is always valid */ 715 /* according to the manpage, sbytes is always valid */
1136 if (sbytes) 716 if (sbytes)
1163 HANDLE h = TO_SOCKET (ifd); 743 HANDLE h = TO_SOCKET (ifd);
1164 SetFilePointer (h, offset, 0, FILE_BEGIN); 744 SetFilePointer (h, offset, 0, FILE_BEGIN);
1165 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 745 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1166 746
1167#else 747#else
1168 res = -1; 748 res = EIO_ENOSYS ();
1169 errno = ENOSYS;
1170#endif 749#endif
1171 750
1172 /* we assume sendfile can copy at least 128mb in one go */ 751 /* we assume sendfile can copy at least 128mb in one go */
1173 if (res <= 128 * 1024 * 1024) 752 if (res <= 128 * 1024 * 1024)
1174 { 753 {
1360} 939}
1361 940
1362/*****************************************************************************/ 941/*****************************************************************************/
1363/* requests implemented outside eio_execute, because they are so large */ 942/* requests implemented outside eio_execute, because they are so large */
1364 943
1365/* copies some absolute path to tmpbuf */ 944static void
1366static char * 945eio__lseek (eio_req *req)
1367eio__getwd (struct tmpbuf *tmpbuf, eio_wd wd)
1368{ 946{
1369 if (wd == EIO_CWD) 947 /* this usually gets optimised away completely, or your compiler sucks, */
1370 return getcwd (tmpbuf->ptr, PATH_MAX); 948 /* or the whence constants really are not 0, 1, 2 */
949 int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET
950 : req->int2 == EIO_SEEK_CUR ? SEEK_CUR
951 : req->int2 == EIO_SEEK_END ? SEEK_END
952 : req->int2;
1371 953
1372#if HAVE_AT 954 req->offs = lseek (req->int1, req->offs, whence);
1373 abort (); /*TODO*/ 955 req->result = req->offs == (off_t)-1 ? -1 : 0;
1374#else
1375 strcpy (tmpbuf->ptr, wd);
1376#endif
1377 return tmpbuf->ptr;
1378} 956}
1379 957
1380/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 958/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1381static int 959static int
1382eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 960eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1383{ 961{
962 char *res;
1384 const char *rel = path; 963 const char *rel = path;
1385 char *res;
1386 char *tmp1, *tmp2; 964 char *tmp1, *tmp2;
1387#if SYMLOOP_MAX > 32 965#if SYMLOOP_MAX > 32
1388 int symlinks = SYMLOOP_MAX; 966 int symlinks = SYMLOOP_MAX;
1389#else 967#else
1390 int symlinks = 32; 968 int symlinks = 32;
1391#endif 969#endif
1392 970
1393 /*D*/ /*TODO: wd ignored */
1394
1395 errno = EINVAL; 971 errno = EINVAL;
1396 if (!rel) 972 if (!rel)
1397 return -1; 973 return -1;
1398 974
1399 errno = ENOENT; 975 errno = ENOENT;
1400 if (!*rel) 976 if (!*rel)
1401 return -1; 977 return -1;
1402 978
1403 res = tmpbuf_get (tmpbuf, PATH_MAX * 3); 979 res = etp_tmpbuf_get (tmpbuf, PATH_MAX * 3);
980#ifdef _WIN32
981 if (_access (rel, 4) != 0)
982 return -1;
983
984 symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0);
985
986 errno = ENAMETOOLONG;
987 if (symlinks >= PATH_MAX * 3)
988 return -1;
989
990 errno = EIO;
991 if (symlinks <= 0)
992 return -1;
993
994 return symlinks;
995
996#else
1404 tmp1 = res + PATH_MAX; 997 tmp1 = res + PATH_MAX;
1405 tmp2 = tmp1 + PATH_MAX; 998 tmp2 = tmp1 + PATH_MAX;
1406 999
1407#if 0 /* disabled, the musl way to do things is just too racy */ 1000#if 0 /* disabled, the musl way to do things is just too racy */
1408#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1001#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1412 1005
1413 if (fd >= 0) 1006 if (fd >= 0)
1414 { 1007 {
1415 sprintf (tmp1, "/proc/self/fd/%d", fd); 1008 sprintf (tmp1, "/proc/self/fd/%d", fd);
1416 req->result = readlink (tmp1, res, PATH_MAX); 1009 req->result = readlink (tmp1, res, PATH_MAX);
1010 /* here we should probably stat the open file and the disk file, to make sure they still match */
1417 close (fd); 1011 close (fd);
1418
1419 /* here we should probably stat the open file and the disk file, to make sure they still match */
1420 1012
1421 if (req->result > 0) 1013 if (req->result > 0)
1422 goto done; 1014 goto done;
1423 } 1015 }
1424 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) 1016 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1425 return; 1017 return -1;
1426 } 1018 }
1427#endif 1019#endif
1428#endif 1020#endif
1429 1021
1430 if (*rel != '/') 1022 if (*rel != '/')
1431 { 1023 {
1432 if (!eio__getwd (tmpbuf, wd)) 1024 int len;
1025
1026 errno = ENOENT;
1027 if (wd == EIO_INVALID_WD)
1433 return -1; 1028 return -1;
1029
1030 if (wd == EIO_CWD)
1031 {
1032 if (!getcwd (res, PATH_MAX))
1033 return -1;
1034
1035 len = strlen (res);
1036 }
1037 else
1038 memcpy (res, wd->str, len = wd->len);
1434 1039
1435 if (res [1]) /* only use if not / */ 1040 if (res [1]) /* only use if not / */
1436 res += strlen (res); 1041 res += len;
1437 } 1042 }
1438 1043
1439 while (*rel) 1044 while (*rel)
1440 { 1045 {
1441 eio_ssize_t len, linklen; 1046 eio_ssize_t len, linklen;
1469 } 1074 }
1470 } 1075 }
1471 1076
1472 errno = ENAMETOOLONG; 1077 errno = ENAMETOOLONG;
1473 if (res + 1 + len + 1 >= tmp1) 1078 if (res + 1 + len + 1 >= tmp1)
1474 return; 1079 return -1;
1475 1080
1476 /* copy one component */ 1081 /* copy one component */
1477 *res = '/'; 1082 *res = '/';
1478 memcpy (res + 1, beg, len); 1083 memcpy (res + 1, beg, len);
1479 1084
1519 /* special case for the lone root path */ 1124 /* special case for the lone root path */
1520 if (res == tmpbuf->ptr) 1125 if (res == tmpbuf->ptr)
1521 *res++ = '/'; 1126 *res++ = '/';
1522 1127
1523 return res - (char *)tmpbuf->ptr; 1128 return res - (char *)tmpbuf->ptr;
1129#endif
1524} 1130}
1525 1131
1526static signed char 1132static signed char
1527eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1133eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1528{ 1134{
1688 eio_dent_insertion_sort (dents, size); 1294 eio_dent_insertion_sort (dents, size);
1689} 1295}
1690 1296
1691/* read a full directory */ 1297/* read a full directory */
1692static void 1298static void
1693eio__scandir (eio_req *req) 1299eio__scandir (eio_req *req, etp_worker *self)
1694{ 1300{
1695 char *name, *names; 1301 char *name, *names;
1696 int namesalloc = 4096 - sizeof (void *) * 4; 1302 int namesalloc = 4096 - sizeof (void *) * 4;
1697 int namesoffs = 0; 1303 int namesoffs = 0;
1698 int flags = req->int1; 1304 int flags = req->int1;
1716#ifdef _WIN32 1322#ifdef _WIN32
1717 { 1323 {
1718 int len = strlen ((const char *)req->ptr1); 1324 int len = strlen ((const char *)req->ptr1);
1719 char *path = malloc (MAX_PATH); 1325 char *path = malloc (MAX_PATH);
1720 const char *fmt; 1326 const char *fmt;
1327 const char *reqpath = wd_expand (&self->tmpbuf, req->wd, req->ptr1);
1721 1328
1722 if (!len) 1329 if (!len)
1723 fmt = "./*"; 1330 fmt = "./*";
1724 else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\') 1331 else if (reqpath[len - 1] == '/' || reqpath[len - 1] == '\\')
1725 fmt = "%s*"; 1332 fmt = "%s*";
1726 else 1333 else
1727 fmt = "%s/*"; 1334 fmt = "%s/*";
1728 1335
1729 _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1); 1336 _snprintf (path, MAX_PATH, fmt, reqpath);
1730 dirp = FindFirstFile (path, &entp); 1337 dirp = FindFirstFile (path, &entp);
1731 free (path); 1338 free (path);
1732 1339
1733 if (dirp == INVALID_HANDLE_VALUE) 1340 if (dirp == INVALID_HANDLE_VALUE)
1734 { 1341 {
1756 1363
1757 return; 1364 return;
1758 } 1365 }
1759 } 1366 }
1760#else 1367#else
1368 #if HAVE_AT
1369 if (req->wd)
1370 {
1371 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY);
1372
1373 if (fd < 0)
1374 return;
1375
1376 dirp = fdopendir (fd);
1377
1378 if (!dirp)
1379 close (fd);
1380 }
1381 else
1761 dirp = opendir (req->ptr1); 1382 dirp = opendir (req->ptr1);
1383 #else
1384 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
1385 #endif
1762 1386
1763 if (!dirp) 1387 if (!dirp)
1764 return; 1388 return;
1765#endif 1389#endif
1766 1390
1883 #ifdef DT_FIFO 1507 #ifdef DT_FIFO
1884 case DT_FIFO: ent->type = EIO_DT_FIFO; break; 1508 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1885 #endif 1509 #endif
1886 #ifdef DT_CHR 1510 #ifdef DT_CHR
1887 case DT_CHR: ent->type = EIO_DT_CHR; break; 1511 case DT_CHR: ent->type = EIO_DT_CHR; break;
1888 #endif 1512 #endif
1889 #ifdef DT_MPC 1513 #ifdef DT_MPC
1890 case DT_MPC: ent->type = EIO_DT_MPC; break; 1514 case DT_MPC: ent->type = EIO_DT_MPC; break;
1891 #endif 1515 #endif
1892 #ifdef DT_DIR 1516 #ifdef DT_DIR
1893 case DT_DIR: ent->type = EIO_DT_DIR; break; 1517 case DT_DIR: ent->type = EIO_DT_DIR; break;
1894 #endif 1518 #endif
1895 #ifdef DT_NAM 1519 #ifdef DT_NAM
1896 case DT_NAM: ent->type = EIO_DT_NAM; break; 1520 case DT_NAM: ent->type = EIO_DT_NAM; break;
1897 #endif 1521 #endif
1898 #ifdef DT_BLK 1522 #ifdef DT_BLK
1899 case DT_BLK: ent->type = EIO_DT_BLK; break; 1523 case DT_BLK: ent->type = EIO_DT_BLK; break;
1900 #endif 1524 #endif
1901 #ifdef DT_MPB 1525 #ifdef DT_MPB
1902 case DT_MPB: ent->type = EIO_DT_MPB; break; 1526 case DT_MPB: ent->type = EIO_DT_MPB; break;
1903 #endif 1527 #endif
1904 #ifdef DT_REG 1528 #ifdef DT_REG
1905 case DT_REG: ent->type = EIO_DT_REG; break; 1529 case DT_REG: ent->type = EIO_DT_REG; break;
1906 #endif 1530 #endif
1907 #ifdef DT_NWK 1531 #ifdef DT_NWK
1908 case DT_NWK: ent->type = EIO_DT_NWK; break; 1532 case DT_NWK: ent->type = EIO_DT_NWK; break;
1909 #endif 1533 #endif
1910 #ifdef DT_CMP 1534 #ifdef DT_CMP
1911 case DT_CMP: ent->type = EIO_DT_CMP; break; 1535 case DT_CMP: ent->type = EIO_DT_CMP; break;
1912 #endif 1536 #endif
1913 #ifdef DT_LNK 1537 #ifdef DT_LNK
1914 case DT_LNK: ent->type = EIO_DT_LNK; break; 1538 case DT_LNK: ent->type = EIO_DT_LNK; break;
1915 #endif 1539 #endif
1916 #ifdef DT_SOCK 1540 #ifdef DT_SOCK
1917 case DT_SOCK: ent->type = EIO_DT_SOCK; break; 1541 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
1930 { 1554 {
1931 if (ent->type == EIO_DT_UNKNOWN) 1555 if (ent->type == EIO_DT_UNKNOWN)
1932 { 1556 {
1933 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ 1557 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1934 ent->score = 1; 1558 ent->score = 1;
1935 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ 1559 else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */
1936 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ 1560 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */
1937 } 1561 }
1938 else if (ent->type == EIO_DT_DIR) 1562 else if (ent->type == EIO_DT_DIR)
1939 ent->score = 0; 1563 ent->score = 0;
1940 } 1564 }
1960 } 1584 }
1961} 1585}
1962 1586
1963/*****************************************************************************/ 1587/*****************************************************************************/
1964/* working directory stuff */ 1588/* working directory stuff */
1589/* various deficiencies in the posix 2008 api force us to */
1590/* keep the absolute path in string form at all times */
1591/* fuck yeah. */
1592
1593#if !HAVE_AT
1594
1595/* a bit like realpath, but usually faster because it doesn'T have to return */
1596/* an absolute or canonical path */
1597static const char *
1598wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1599{
1600 if (!wd || *path == '/')
1601 return path;
1602
1603 if (path [0] == '.' && !path [1])
1604 return wd->str;
1605
1606 {
1607 int l1 = wd->len;
1608 int l2 = strlen (path);
1609
1610 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
1611
1612 memcpy (res, wd->str, l1);
1613 res [l1] = '/';
1614 memcpy (res + l1 + 1, path, l2 + 1);
1615
1616 return res;
1617 }
1618}
1619
1620#endif
1621
1622static eio_wd
1623eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1624{
1625 int fd;
1626 eio_wd res;
1627 int len = eio__realpath (tmpbuf, wd, path);
1628
1629 if (len < 0)
1630 return EIO_INVALID_WD;
1965 1631
1966#if HAVE_AT 1632#if HAVE_AT
1633 fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY);
1967 1634
1968#define WD2FD(wd) (wd ? ((int)wd) - 1 : AT_FDCWD) 1635 if (fd < 0)
1636 return EIO_INVALID_WD;
1637#endif
1638
1639 res = malloc (sizeof (*res) + len); /* one extra 0-byte */
1640
1641#if HAVE_AT
1642 res->fd = fd;
1643#endif
1644
1645 res->len = len;
1646 memcpy (res->str, tmpbuf->ptr, len);
1647 res->str [len] = 0;
1648
1649 return res;
1650}
1969 1651
1970eio_wd 1652eio_wd
1971eio_wd_open_sync (eio_wd wd, const char *path) 1653eio_wd_open_sync (eio_wd wd, const char *path)
1972{ 1654{
1973 int fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 1655 struct etp_tmpbuf tmpbuf = { };
1656 wd = eio__wd_open_sync (&tmpbuf, wd, path);
1657 free (tmpbuf.ptr);
1974 1658
1975 return fd >= 0 ? (eio_wd)(fd + 1) : EIO_INVALID_WD; 1659 return wd;
1976} 1660}
1977 1661
1978void 1662void
1979eio_wd_close_sync (eio_wd wd) 1663eio_wd_close_sync (eio_wd wd)
1980{ 1664{
1981 int fd = WD2FD (wd); 1665 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
1982
1983 if (fd >= 0)
1984 close (fd);
1985}
1986
1987#else
1988
1989/* on legacy systems, we represent the working directories simply by their path strings */
1990
1991static const char *
1992wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
1993{
1994 if (!wd || *path == '/')
1995 return path;
1996
1997 {
1998 int l1 = strlen ((const char *)wd);
1999 int l2 = strlen (path);
2000
2001 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2);
2002
2003 memcpy (res, wd, l1);
2004 res [l1] = '/';
2005 memcpy (res + l1 + 1, path, l2 + 1);
2006
2007 return res;
2008 }
2009}
2010
2011eio_wd
2012eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2013{
2014 if (*path == '/') /* absolute paths ignore wd */
2015 path = strdup (path);
2016 else if (path [0] == '.' && !path [1]) /* special case '.', as it is common */
2017 return wd;
2018 else
2019 { 1666 {
2020 int len = eio__realpath (tmpbuf, wd, path); 1667 #if HAVE_AT
2021 1668 close (wd->fd);
2022 path = EIO_INVALID_WD; 1669 #endif
2023 1670 free (wd);
2024 if (len >= 0)
2025 {
2026 ((char *)tmpbuf->ptr)[len] = 0;
2027 path = strdup (tmpbuf->ptr);
2028 }
2029 } 1671 }
2030
2031 if (!path)
2032 path = EIO_INVALID_WD;
2033
2034 return (eio_wd)path;
2035} 1672}
2036 1673
2037eio_wd 1674#if HAVE_AT
2038eio_wd_open_sync (eio_wd wd, const char *path)
2039{
2040 struct tmpbuf tmpbuf = { 0 };
2041 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2042 free (tmpbuf.ptr);
2043 1675
1676/* they forgot these */
1677
1678static int
1679eio__truncateat (int dirfd, const char *path, off_t length)
1680{
1681 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC);
1682 int res;
1683
1684 if (fd < 0)
2044 return wd; 1685 return fd;
2045}
2046 1686
2047void 1687 res = ftruncate (fd, length);
2048eio_wd_close_sync (eio_wd wd) 1688 close (fd);
1689 return res;
1690}
1691
1692static int
1693eio__statvfsat (int dirfd, const char *path, struct statvfs *buf)
2049{ 1694{
2050 if (wd != EIO_INVALID_WD) 1695 int fd = openat (dirfd, path, O_SEARCH | O_CLOEXEC);
2051 free (wd); 1696 int res;
1697
1698 if (fd < 0)
1699 return fd;
1700
1701 res = fstatvfs (fd, buf);
1702 close (fd);
1703 return res;
1704
2052} 1705}
2053 1706
2054#endif 1707#endif
2055 1708
2056/*****************************************************************************/ 1709/*****************************************************************************/
2057 1710
2058#define ALLOC(len) \ 1711#define ALLOC(len) \
2059 if (!req->ptr2) \ 1712 if (!req->ptr2) \
2060 { \ 1713 { \
2061 X_LOCK (wrklock); \ 1714 X_LOCK (EIO_POOL->wrklock); \
2062 req->flags |= EIO_FLAG_PTR2_FREE; \ 1715 req->flags |= EIO_FLAG_PTR2_FREE; \
2063 X_UNLOCK (wrklock); \ 1716 X_UNLOCK (EIO_POOL->wrklock); \
2064 req->ptr2 = malloc (len); \ 1717 req->ptr2 = malloc (len); \
2065 if (!req->ptr2) \ 1718 if (!req->ptr2) \
2066 { \ 1719 { \
2067 errno = ENOMEM; \ 1720 errno = ENOMEM; \
2068 req->result = -1; \ 1721 req->result = -1; \
2069 break; \ 1722 break; \
2070 } \ 1723 } \
2071 } 1724 }
2072 1725
2073X_THREAD_PROC (etp_proc)
2074{
2075 ETP_REQ *req;
2076 struct timespec ts;
2077 etp_worker *self = (etp_worker *)thr_arg;
2078
2079#if HAVE_PRCTL_SET_NAME
2080 prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0);
2081#endif
2082
2083 /* try to distribute timeouts somewhat evenly */
2084 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2085
2086 for (;;)
2087 {
2088 ts.tv_sec = 0;
2089
2090 X_LOCK (reqlock);
2091
2092 for (;;)
2093 {
2094 req = reqq_shift (&req_queue);
2095
2096 if (req)
2097 break;
2098
2099 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2100 {
2101 X_UNLOCK (reqlock);
2102 X_LOCK (wrklock);
2103 --started;
2104 X_UNLOCK (wrklock);
2105 goto quit;
2106 }
2107
2108 ++idle;
2109
2110 if (idle <= max_idle)
2111 /* we are allowed to idle, so do so without any timeout */
2112 X_COND_WAIT (reqwait, reqlock);
2113 else
2114 {
2115 /* initialise timeout once */
2116 if (!ts.tv_sec)
2117 ts.tv_sec = time (0) + idle_timeout;
2118
2119 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2120 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2121 }
2122
2123 --idle;
2124 }
2125
2126 --nready;
2127
2128 X_UNLOCK (reqlock);
2129
2130 if (req->type < 0)
2131 goto quit;
2132
2133 ETP_EXECUTE (self, req);
2134
2135 X_LOCK (reslock);
2136
2137 ++npending;
2138
2139 if (!reqq_push (&res_queue, req) && want_poll_cb)
2140 want_poll_cb ();
2141
2142 etp_worker_clear (self);
2143
2144 X_UNLOCK (reslock);
2145 }
2146
2147quit:
2148 free (req);
2149
2150 X_LOCK (wrklock);
2151 etp_worker_free (self);
2152 X_UNLOCK (wrklock);
2153
2154 return 0;
2155}
2156
2157/*****************************************************************************/ 1726/*****************************************************************************/
2158 1727
2159int ecb_cold 1728int ecb_cold
2160eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1729eio_init (void (*want_poll)(void), void (*done_poll)(void))
2161{ 1730{
2162#if !HAVE_PREADWRITE 1731 eio_want_poll_cb = want_poll;
2163 X_MUTEX_CREATE (preadwritelock); 1732 eio_done_poll_cb = done_poll;
2164#endif
2165 1733
2166 return etp_init (want_poll, done_poll); 1734 return etp_init (EIO_POOL, 0, 0, 0);
2167} 1735}
2168 1736
2169ecb_inline void 1737ecb_inline void
2170eio_api_destroy (eio_req *req) 1738eio_api_destroy (eio_req *req)
2171{ 1739{
2172 free (req); 1740 free (req);
2173} 1741}
2174 1742
2175#define REQ(rtype) \ 1743#define REQ(rtype) \
2176 eio_req *req; \ 1744 eio_req *req; \
2177 \ 1745 \
2178 req = (eio_req *)calloc (1, sizeof *req); \ 1746 req = (eio_req *)calloc (1, sizeof *req); \
2179 if (!req) \ 1747 if (!req) \
2180 return 0; \ 1748 return 0; \
2194 { \ 1762 { \
2195 eio_api_destroy (req); \ 1763 eio_api_destroy (req); \
2196 return 0; \ 1764 return 0; \
2197 } 1765 }
2198 1766
1767#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)])
1768
2199static void 1769static void
2200eio_execute (etp_worker *self, eio_req *req) 1770eio_execute (etp_worker *self, eio_req *req)
2201{ 1771{
2202#if HAVE_AT 1772#if HAVE_AT
2203 int dirfd; 1773 int dirfd;
2231 switch (req->type) 1801 switch (req->type)
2232 { 1802 {
2233 case EIO_WD_OPEN: req->wd = eio__wd_open_sync (&self->tmpbuf, req->wd, req->ptr1); 1803 case EIO_WD_OPEN: req->wd = eio__wd_open_sync (&self->tmpbuf, req->wd, req->ptr1);
2234 req->result = req->wd == EIO_INVALID_WD ? -1 : 0; 1804 req->result = req->wd == EIO_INVALID_WD ? -1 : 0;
2235 break; 1805 break;
1806 case EIO_WD_CLOSE: req->result = 0;
1807 eio_wd_close_sync (req->wd); break;
1808
2236 case EIO_WD_CLOSE: eio_wd_close_sync (req->wd); break; 1809 case EIO_SEEK: eio__lseek (req); break;
2237
2238 case EIO_READ: ALLOC (req->size); 1810 case EIO_READ: ALLOC (req->size);
2239 req->result = req->offs >= 0 1811 req->result = req->offs >= 0
2240 ? pread (req->int1, req->ptr2, req->size, req->offs) 1812 ? pread (req->int1, req->ptr2, req->size, req->offs)
2241 : read (req->int1, req->ptr2, req->size); break; 1813 : read (req->int1, req->ptr2, req->size); break;
2242 case EIO_WRITE: req->result = req->offs >= 0 1814 case EIO_WRITE: req->result = req->offs >= 0
2243 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1815 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
2244 : write (req->int1, req->ptr2, req->size); break; 1816 : write (req->int1, req->ptr2, req->size); break;
2245 1817
1818 case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break;
1819 case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break;
1820
2246 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 1821 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
2247 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; 1822 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
2248 1823
2249#if HAVE_AT 1824#if HAVE_AT
2250 case EIO_GETPATH: abort (); 1825
2251 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 1826 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
2252 req->result = fstatat (dirfd, path, (EIO_STRUCT_STAT *)req->ptr2); break; 1827 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, 0); break;
2253 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 1828 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
2254 req->result = lstat (dirfd, path, (EIO_STRUCT_STAT *)req->ptr2); break; 1829 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break;
2255#if 0/*D*/ 1830 case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break;
1831 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break;
1832 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break;
1833 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
1834
1835 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
1836 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
1837 req->result = req->wd && SINGLEDOT (req->ptr1)
1838 ? rmdir (req->wd->str)
1839 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
1840 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
1841 case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */
1842 req->result = req->wd && SINGLEDOT (req->ptr1)
1843 ? rename (req->wd->str, req->ptr2)
1844 : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break;
1845 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break;
1846 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break;
1847 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1848 case EIO_READLINK: ALLOC (PATH_MAX);
1849 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2256 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1850 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2257 req->result = statvfs (dirfd, path, (EIO_STRUCT_STATVFS *)req->ptr2); break; 1851 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
2258#endif 1852 case EIO_UTIME:
2259 case EIO_CHOWN: req->result = chown (dirfd, path, req->int2, req->int3); break; 1853 case EIO_FUTIME:
2260 case EIO_CHMOD: req->result = chmod (dirfd, path, (mode_t)req->int2); break; 1854 {
2261 case EIO_TRUNCATE: req->result = truncate (dirfd, path, req->offs); break; 1855 struct timespec ts[2];
2262 case EIO_OPEN: req->result = open (dirfd, path, req->int1, (mode_t)req->int2); break; 1856 struct timespec *times;
2263 1857
2264 case EIO_UNLINK: req->result = unlink (dirfd, path); break; 1858 if (req->nv1 != -1. || req->nv2 != -1.)
2265 case EIO_RMDIR: req->result = rmdir (dirfd, path); break; 1859 {
2266 case EIO_MKDIR: req->result = mkdir (dirfd, path, (mode_t)req->int2); break; 1860 ts[0].tv_sec = req->nv1;
2267 case EIO_RENAME: req->result = rename (dirfd, path, req->ptr2); break; 1861 ts[0].tv_nsec = (req->nv1 - ts[0].tv_sec) * 1e9;
2268 case EIO_LINK: req->result = link (dirfd, path, req->ptr2); break; 1862 ts[1].tv_sec = req->nv2;
2269 case EIO_SYMLINK: req->result = symlink (dirfd, path, req->ptr2); break; 1863 ts[1].tv_nsec = (req->nv2 - ts[1].tv_sec) * 1e9;
2270 case EIO_MKNOD: req->result = mknod (dirfd, path, (mode_t)req->int2, (dev_t)req->offs); break; 1864
1865 times = ts;
1866 }
1867 else
1868 times = 0;
1869
1870 req->result = req->type == EIO_FUTIME
1871 ? futimens (req->int1, times)
1872 : utimensat (dirfd, req->ptr1, times, 0);
1873 }
1874 break;
1875
2271#else 1876#else
1877
2272 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 1878 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
2273 req->result = stat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 1879 req->result = stat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2274 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 1880 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
2275 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 1881 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2276 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; 1882 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break;
2283 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; 1889 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break;
2284 case EIO_RENAME: req->result = rename (path , req->ptr2); break; 1890 case EIO_RENAME: req->result = rename (path , req->ptr2); break;
2285 case EIO_LINK: req->result = link (path , req->ptr2); break; 1891 case EIO_LINK: req->result = link (path , req->ptr2); break;
2286 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; 1892 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break;
2287 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; 1893 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break;
2288
2289 case EIO_READLINK: ALLOC (PATH_MAX); 1894 case EIO_READLINK: ALLOC (PATH_MAX);
2290 req->result = readlink (path, req->ptr2, PATH_MAX); break; 1895 req->result = readlink (path, req->ptr2, PATH_MAX); break;
1896 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
1897 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break;
1898
1899 case EIO_UTIME:
1900 case EIO_FUTIME:
1901 {
1902 struct timeval tv[2];
1903 struct timeval *times;
1904
1905 if (req->nv1 != -1. || req->nv2 != -1.)
1906 {
1907 tv[0].tv_sec = req->nv1;
1908 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1e6;
1909 tv[1].tv_sec = req->nv2;
1910 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1e6;
1911
1912 times = tv;
1913 }
1914 else
1915 times = 0;
1916
1917 req->result = req->type == EIO_FUTIME
1918 ? futimes (req->int1, times)
1919 : utimes (req->ptr1, times);
1920 }
1921 break;
1922
2291#endif 1923#endif
2292 1924
2293 case EIO_REALPATH: if (0 <= (req->result = eio__realpath (&self->tmpbuf, req->wd, req->ptr1))) 1925 case EIO_REALPATH: if (0 <= (req->result = eio__realpath (&self->tmpbuf, req->wd, req->ptr1)))
2294 { 1926 {
2295 ALLOC (req->result); 1927 ALLOC (req->result);
2296 memcpy (req->ptr2, self->tmpbuf.ptr, req->result); 1928 memcpy (req->ptr2, self->tmpbuf.ptr, req->result);
2297 } 1929 }
2298 break; 1930 break;
2299
2300 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); /*D*/
2301 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break;
2302 1931
2303 case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 1932 case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
2304 req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; 1933 req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break;
2305 1934
2306 case EIO_FSTATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1935 case EIO_FSTATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2321 case EIO_MTOUCH: req->result = eio__mtouch (req); break; 1950 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
2322 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 1951 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
2323 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 1952 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
2324 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; 1953 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
2325 1954
2326 case EIO_READDIR: eio__scandir (req); break; 1955 case EIO_READDIR: eio__scandir (req, self); break;
2327 1956
2328 case EIO_BUSY: 1957 case EIO_BUSY:
2329#ifdef _WIN32 1958#ifdef _WIN32
2330 Sleep (req->nv1 * 1e3); 1959 Sleep (req->nv1 * 1e3);
2331#else 1960#else
2338 req->result = select (0, 0, 0, 0, &tv); 1967 req->result = select (0, 0, 0, 0, &tv);
2339 } 1968 }
2340#endif 1969#endif
2341 break; 1970 break;
2342 1971
2343 case EIO_UTIME: 1972#if 0
2344 case EIO_FUTIME:
2345 {
2346 struct timeval tv[2];
2347 struct timeval *times;
2348
2349 if (req->nv1 != -1. || req->nv2 != -1.)
2350 {
2351 tv[0].tv_sec = req->nv1;
2352 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
2353 tv[1].tv_sec = req->nv2;
2354 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
2355
2356 times = tv;
2357 }
2358 else
2359 times = 0;
2360
2361 req->result = req->type == EIO_FUTIME
2362 ? futimes (req->int1, times)
2363 : utimes (req->ptr1, times);
2364 }
2365 break;
2366
2367 case EIO_GROUP: 1973 case EIO_GROUP:
2368 abort (); /* handled in eio_request */ 1974 abort (); /* handled in eio_request */
1975#endif
2369 1976
2370 case EIO_NOP: 1977 case EIO_NOP:
2371 req->result = 0; 1978 req->result = 0;
2372 break; 1979 break;
2373 1980
2374 case EIO_CUSTOM: 1981 case EIO_CUSTOM:
2375 req->feed (req); 1982 req->feed (req);
2376 break; 1983 break;
2377 1984
2378 default: 1985 default:
2379 errno = ENOSYS;
2380 req->result = -1; 1986 req->result = EIO_ENOSYS ();
2381 break; 1987 break;
2382 } 1988 }
2383 1989
2384 req->errorno = errno; 1990 req->errorno = errno;
2385} 1991}
2386 1992
2387#ifndef EIO_NO_WRAPPERS 1993#ifndef EIO_NO_WRAPPERS
2388 1994
1995eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data)
1996{
1997 REQ (EIO_WD_OPEN); PATH; SEND;
1998}
1999
2000eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data)
2001{
2002 REQ (EIO_WD_CLOSE); req->wd = wd; SEND;
2003}
2004
2389eio_req *eio_nop (int pri, eio_cb cb, void *data) 2005eio_req *eio_nop (int pri, eio_cb cb, void *data)
2390{ 2006{
2391 REQ (EIO_NOP); SEND; 2007 REQ (EIO_NOP); SEND;
2392} 2008}
2393 2009
2454eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) 2070eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
2455{ 2071{
2456 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; 2072 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
2457} 2073}
2458 2074
2075eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data)
2076{
2077 REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND;
2078}
2079
2459eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2080eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2460{ 2081{
2461 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2082 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2462} 2083}
2463 2084
2464eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2085eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2465{ 2086{
2466 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2087 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2088}
2089
2090eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data)
2091{
2092 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND;
2093}
2094
2095eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data)
2096{
2097 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND;
2467} 2098}
2468 2099
2469eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) 2100eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
2470{ 2101{
2471 REQ (EIO_FSTAT); req->int1 = fd; SEND; 2102 REQ (EIO_FSTAT); req->int1 = fd; SEND;
2659void 2290void
2660eio_grp_add (eio_req *grp, eio_req *req) 2291eio_grp_add (eio_req *grp, eio_req *req)
2661{ 2292{
2662 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2293 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2663 2294
2664 grp->flags |= EIO_FLAG_GROUPADD; 2295 grp->flags |= ETP_FLAG_GROUPADD;
2665 2296
2666 ++grp->size; 2297 ++grp->size;
2667 req->grp = grp; 2298 req->grp = grp;
2668 2299
2669 req->grp_prev = 0; 2300 req->grp_prev = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines