1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 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 | * |
… | |
… | |
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> |
… | |
… | |
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) |
125 | #define pipe(fds) _pipe (fds, 4096, O_BINARY) |
120 | #define pipe(fds) _pipe (fds, 4096, O_BINARY) |
126 | |
121 | |
|
|
122 | #define fcntl(fd,cmd,arg) EIO_ENOSYS () |
|
|
123 | #define ioctl(fd,cmd,arg) EIO_ENOSYS () |
127 | #define fchmod(fd,mode) EIO_ENOSYS () |
124 | #define fchmod(fd,mode) EIO_ENOSYS () |
128 | #define chown(path,uid,gid) EIO_ENOSYS () |
125 | #define chown(path,uid,gid) EIO_ENOSYS () |
129 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
126 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
130 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
127 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
131 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
128 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
… | |
… | |
204 | /* we could even stat and see if it exists */ |
201 | /* we could even stat and see if it exists */ |
205 | static int |
202 | static int |
206 | symlink (const char *old, const char *neu) |
203 | symlink (const char *old, const char *neu) |
207 | { |
204 | { |
208 | #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) |
209 | if (CreateSymbolicLink (neu, old, 1)) |
213 | if (CreateSymbolicLink (neu, old, flags)) |
210 | return 0; |
214 | return 0; |
211 | |
|
|
212 | if (CreateSymbolicLink (neu, old, 0)) |
|
|
213 | return 0; |
|
|
214 | #endif |
215 | #endif |
215 | |
216 | |
216 | return EIO_ERRNO (ENOENT, -1); |
217 | return EIO_ERRNO (ENOENT, -1); |
217 | } |
218 | } |
218 | |
219 | |
… | |
… | |
281 | # include <utime.h> |
282 | # include <utime.h> |
282 | #endif |
283 | #endif |
283 | |
284 | |
284 | #if HAVE_SYS_SYSCALL_H |
285 | #if HAVE_SYS_SYSCALL_H |
285 | # include <sys/syscall.h> |
286 | # include <sys/syscall.h> |
286 | #endif |
|
|
287 | |
|
|
288 | #if HAVE_SYS_PRCTL_H |
|
|
289 | # include <sys/prctl.h> |
|
|
290 | #endif |
287 | #endif |
291 | |
288 | |
292 | #if HAVE_SENDFILE |
289 | #if HAVE_SENDFILE |
293 | # if __linux |
290 | # if __linux |
294 | # include <sys/sendfile.h> |
291 | # include <sys/sendfile.h> |
… | |
… | |
319 | # define NAME_MAX 4096 |
316 | # define NAME_MAX 4096 |
320 | #endif |
317 | #endif |
321 | |
318 | |
322 | /* used for readlink etc. */ |
319 | /* used for readlink etc. */ |
323 | #ifndef PATH_MAX |
320 | #ifndef PATH_MAX |
324 | # define PATH_MAX 4096 |
321 | # define PATH_MAX 0 |
325 | #endif |
322 | #endif |
|
|
323 | |
|
|
324 | #ifndef EIO_PATH_MIN |
|
|
325 | # define EIO_PATH_MIN 8160 |
|
|
326 | #endif |
|
|
327 | |
|
|
328 | #define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX) |
326 | |
329 | |
327 | /* buffer size for various temporary buffers */ |
330 | /* buffer size for various temporary buffers */ |
328 | #define EIO_BUFSIZE 65536 |
331 | #define EIO_BUFSIZE 65536 |
329 | |
332 | |
330 | #define dBUF \ |
333 | #define dBUF \ |
… | |
… | |
366 | #define ETP_PRI_MAX EIO_PRI_MAX |
369 | #define ETP_PRI_MAX EIO_PRI_MAX |
367 | |
370 | |
368 | #define ETP_TYPE_QUIT -1 |
371 | #define ETP_TYPE_QUIT -1 |
369 | #define ETP_TYPE_GROUP EIO_GROUP |
372 | #define ETP_TYPE_GROUP EIO_GROUP |
370 | |
373 | |
|
|
374 | static void eio_nop_callback (void) { } |
|
|
375 | static void (*eio_want_poll_cb)(void) = eio_nop_callback; |
|
|
376 | static void (*eio_done_poll_cb)(void) = eio_nop_callback; |
|
|
377 | |
|
|
378 | #define ETP_WANT_POLL(pool) eio_want_poll_cb () |
|
|
379 | #define ETP_DONE_POLL(pool) eio_done_poll_cb () |
|
|
380 | |
371 | struct etp_worker; |
381 | struct etp_worker; |
372 | |
|
|
373 | #define ETP_REQ eio_req |
382 | #define ETP_REQ eio_req |
374 | #define ETP_DESTROY(req) eio_destroy (req) |
383 | #define ETP_DESTROY(req) eio_destroy (req) |
375 | static int eio_finish (eio_req *req); |
384 | static int eio_finish (eio_req *req); |
376 | #define ETP_FINISH(req) eio_finish (req) |
385 | #define ETP_FINISH(req) eio_finish (req) |
377 | static void eio_execute (struct etp_worker *self, eio_req *req); |
386 | static void eio_execute (struct etp_worker *self, eio_req *req); |
378 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) |
387 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) |
379 | |
388 | |
380 | #include "etp.c" |
389 | #include "etp.c" |
381 | |
390 | |
|
|
391 | static struct etp_pool eio_pool; |
|
|
392 | #define EIO_POOL (&eio_pool) |
|
|
393 | |
382 | /*****************************************************************************/ |
394 | /*****************************************************************************/ |
383 | |
395 | |
384 | static void |
396 | static void |
385 | grp_try_feed (eio_req *grp) |
397 | grp_try_feed (eio_req *grp) |
386 | { |
398 | { |
387 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
399 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
388 | { |
400 | { |
389 | grp->flags &= ~EIO_FLAG_GROUPADD; |
401 | grp->flags &= ~ETP_FLAG_GROUPADD; |
390 | |
402 | |
391 | EIO_FEED (grp); |
403 | EIO_FEED (grp); |
392 | |
404 | |
393 | /* stop if no progress has been made */ |
405 | /* stop if no progress has been made */ |
394 | if (!(grp->flags & EIO_FLAG_GROUPADD)) |
406 | if (!(grp->flags & ETP_FLAG_GROUPADD)) |
395 | { |
407 | { |
396 | grp->feed = 0; |
408 | grp->feed = 0; |
397 | break; |
409 | break; |
398 | } |
410 | } |
399 | } |
411 | } |
… | |
… | |
406 | |
418 | |
407 | /* call feeder, if applicable */ |
419 | /* call feeder, if applicable */ |
408 | grp_try_feed (grp); |
420 | grp_try_feed (grp); |
409 | |
421 | |
410 | /* finish, if done */ |
422 | /* finish, if done */ |
411 | if (!grp->size && grp->int1) |
423 | if (!grp->size && grp->flags & ETP_FLAG_DELAYED) |
412 | return eio_finish (grp); |
424 | return eio_finish (grp); |
413 | else |
425 | else |
414 | return 0; |
426 | return 0; |
415 | } |
427 | } |
416 | |
428 | |
… | |
… | |
452 | } |
464 | } |
453 | |
465 | |
454 | void |
466 | void |
455 | eio_grp_cancel (eio_req *grp) |
467 | eio_grp_cancel (eio_req *grp) |
456 | { |
468 | { |
457 | etp_grp_cancel (grp); |
469 | etp_grp_cancel (EIO_POOL, grp); |
458 | } |
470 | } |
459 | |
471 | |
460 | void |
472 | void |
461 | eio_cancel (eio_req *req) |
473 | eio_cancel (eio_req *req) |
462 | { |
474 | { |
463 | etp_cancel (req); |
475 | etp_cancel (EIO_POOL, req); |
464 | } |
476 | } |
465 | |
477 | |
466 | void |
478 | void |
467 | eio_submit (eio_req *req) |
479 | eio_submit (eio_req *req) |
468 | { |
480 | { |
469 | etp_submit (req); |
481 | etp_submit (EIO_POOL, req); |
470 | } |
482 | } |
471 | |
483 | |
472 | unsigned int |
484 | unsigned int |
473 | eio_nreqs (void) |
485 | eio_nreqs (void) |
474 | { |
486 | { |
475 | return etp_nreqs (); |
487 | return etp_nreqs (EIO_POOL); |
476 | } |
488 | } |
477 | |
489 | |
478 | unsigned int |
490 | unsigned int |
479 | eio_nready (void) |
491 | eio_nready (void) |
480 | { |
492 | { |
481 | return etp_nready (); |
493 | return etp_nready (EIO_POOL); |
482 | } |
494 | } |
483 | |
495 | |
484 | unsigned int |
496 | unsigned int |
485 | eio_npending (void) |
497 | eio_npending (void) |
486 | { |
498 | { |
487 | return etp_npending (); |
499 | return etp_npending (EIO_POOL); |
488 | } |
500 | } |
489 | |
501 | |
490 | unsigned int ecb_cold |
502 | unsigned int ecb_cold |
491 | eio_nthreads (void) |
503 | eio_nthreads (void) |
492 | { |
504 | { |
493 | return etp_nthreads (); |
505 | return etp_nthreads (EIO_POOL); |
494 | } |
506 | } |
495 | |
507 | |
496 | void ecb_cold |
508 | void ecb_cold |
497 | eio_set_max_poll_time (double nseconds) |
509 | eio_set_max_poll_time (double nseconds) |
498 | { |
510 | { |
499 | etp_set_max_poll_time (nseconds); |
511 | etp_set_max_poll_time (EIO_POOL, nseconds); |
500 | } |
512 | } |
501 | |
513 | |
502 | void ecb_cold |
514 | void ecb_cold |
503 | eio_set_max_poll_reqs (unsigned int maxreqs) |
515 | eio_set_max_poll_reqs (unsigned int maxreqs) |
504 | { |
516 | { |
505 | etp_set_max_poll_reqs (maxreqs); |
517 | etp_set_max_poll_reqs (EIO_POOL, maxreqs); |
506 | } |
518 | } |
507 | |
519 | |
508 | void ecb_cold |
520 | void ecb_cold |
509 | eio_set_max_idle (unsigned int nthreads) |
521 | eio_set_max_idle (unsigned int nthreads) |
510 | { |
522 | { |
511 | etp_set_max_idle (nthreads); |
523 | etp_set_max_idle (EIO_POOL, nthreads); |
512 | } |
524 | } |
513 | |
525 | |
514 | void ecb_cold |
526 | void ecb_cold |
515 | eio_set_idle_timeout (unsigned int seconds) |
527 | eio_set_idle_timeout (unsigned int seconds) |
516 | { |
528 | { |
517 | etp_set_idle_timeout (seconds); |
529 | etp_set_idle_timeout (EIO_POOL, seconds); |
518 | } |
530 | } |
519 | |
531 | |
520 | void ecb_cold |
532 | void ecb_cold |
521 | eio_set_min_parallel (unsigned int nthreads) |
533 | eio_set_min_parallel (unsigned int nthreads) |
522 | { |
534 | { |
523 | etp_set_min_parallel (nthreads); |
535 | etp_set_min_parallel (EIO_POOL, nthreads); |
524 | } |
536 | } |
525 | |
537 | |
526 | void ecb_cold |
538 | void ecb_cold |
527 | eio_set_max_parallel (unsigned int nthreads) |
539 | eio_set_max_parallel (unsigned int nthreads) |
528 | { |
540 | { |
529 | etp_set_max_parallel (nthreads); |
541 | etp_set_max_parallel (EIO_POOL, nthreads); |
530 | } |
542 | } |
531 | |
543 | |
532 | int eio_poll (void) |
544 | int eio_poll (void) |
533 | { |
545 | { |
534 | return etp_poll (); |
546 | return etp_poll (EIO_POOL); |
535 | } |
547 | } |
536 | |
548 | |
537 | /*****************************************************************************/ |
549 | /*****************************************************************************/ |
538 | /* work around various missing functions */ |
550 | /* work around various missing functions */ |
539 | |
551 | |
… | |
… | |
968 | |
980 | |
969 | errno = ENOENT; |
981 | errno = ENOENT; |
970 | if (!*rel) |
982 | if (!*rel) |
971 | return -1; |
983 | return -1; |
972 | |
984 | |
973 | res = etp_tmpbuf_get (tmpbuf, PATH_MAX * 3); |
985 | res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3); |
974 | #ifdef _WIN32 |
986 | #ifdef _WIN32 |
975 | if (_access (rel, 4) != 0) |
987 | if (_access (rel, 4) != 0) |
976 | return -1; |
988 | return -1; |
977 | |
989 | |
978 | symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0); |
990 | symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0); |
979 | |
991 | |
980 | errno = ENAMETOOLONG; |
992 | errno = ENAMETOOLONG; |
981 | if (symlinks >= PATH_MAX * 3) |
993 | if (symlinks >= EIO_PATH_MAX * 3) |
982 | return -1; |
994 | return -1; |
983 | |
995 | |
984 | errno = EIO; |
996 | errno = EIO; |
985 | if (symlinks <= 0) |
997 | if (symlinks <= 0) |
986 | return -1; |
998 | return -1; |
987 | |
999 | |
988 | return symlinks; |
1000 | return symlinks; |
989 | |
1001 | |
990 | #else |
1002 | #else |
991 | tmp1 = res + PATH_MAX; |
1003 | tmp1 = res + EIO_PATH_MAX; |
992 | tmp2 = tmp1 + PATH_MAX; |
1004 | tmp2 = tmp1 + EIO_PATH_MAX; |
993 | |
1005 | |
994 | #if 0 /* disabled, the musl way to do things is just too racy */ |
1006 | #if 0 /* disabled, the musl way to do things is just too racy */ |
995 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
1007 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
996 | /* on linux we may be able to ask the kernel */ |
1008 | /* on linux we may be able to ask the kernel */ |
997 | { |
1009 | { |
998 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
1010 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
999 | |
1011 | |
1000 | if (fd >= 0) |
1012 | if (fd >= 0) |
1001 | { |
1013 | { |
1002 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1014 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1003 | req->result = readlink (tmp1, res, PATH_MAX); |
1015 | req->result = readlink (tmp1, res, EIO_PATH_MAX); |
1004 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
1016 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
1005 | close (fd); |
1017 | close (fd); |
1006 | |
1018 | |
1007 | if (req->result > 0) |
1019 | if (req->result > 0) |
1008 | goto done; |
1020 | goto done; |
… | |
… | |
1021 | if (wd == EIO_INVALID_WD) |
1033 | if (wd == EIO_INVALID_WD) |
1022 | return -1; |
1034 | return -1; |
1023 | |
1035 | |
1024 | if (wd == EIO_CWD) |
1036 | if (wd == EIO_CWD) |
1025 | { |
1037 | { |
1026 | if (!getcwd (res, PATH_MAX)) |
1038 | if (!getcwd (res, EIO_PATH_MAX)) |
1027 | return -1; |
1039 | return -1; |
1028 | |
1040 | |
1029 | len = strlen (res); |
1041 | len = strlen (res); |
1030 | } |
1042 | } |
1031 | else |
1043 | else |
… | |
… | |
1078 | |
1090 | |
1079 | /* zero-terminate, for readlink */ |
1091 | /* zero-terminate, for readlink */ |
1080 | res [len + 1] = 0; |
1092 | res [len + 1] = 0; |
1081 | |
1093 | |
1082 | /* now check if it's a symlink */ |
1094 | /* now check if it's a symlink */ |
1083 | linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); |
1095 | linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX); |
1084 | |
1096 | |
1085 | if (linklen < 0) |
1097 | if (linklen < 0) |
1086 | { |
1098 | { |
1087 | if (errno != EINVAL) |
1099 | if (errno != EINVAL) |
1088 | return -1; |
1100 | return -1; |
… | |
… | |
1094 | { |
1106 | { |
1095 | /* yay, it was a symlink - build new path in tmp2 */ |
1107 | /* yay, it was a symlink - build new path in tmp2 */ |
1096 | int rellen = strlen (rel); |
1108 | int rellen = strlen (rel); |
1097 | |
1109 | |
1098 | errno = ENAMETOOLONG; |
1110 | errno = ENAMETOOLONG; |
1099 | if (linklen + 1 + rellen >= PATH_MAX) |
1111 | if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */ |
1100 | return -1; |
1112 | return -1; |
1101 | |
1113 | |
1102 | errno = ELOOP; |
1114 | errno = ELOOP; |
1103 | if (!--symlinks) |
1115 | if (!--symlinks) |
1104 | return -1; |
1116 | return -1; |
… | |
… | |
1703 | /*****************************************************************************/ |
1715 | /*****************************************************************************/ |
1704 | |
1716 | |
1705 | #define ALLOC(len) \ |
1717 | #define ALLOC(len) \ |
1706 | if (!req->ptr2) \ |
1718 | if (!req->ptr2) \ |
1707 | { \ |
1719 | { \ |
1708 | X_LOCK (wrklock); \ |
1720 | X_LOCK (EIO_POOL->wrklock); \ |
1709 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1721 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1710 | X_UNLOCK (wrklock); \ |
1722 | X_UNLOCK (EIO_POOL->wrklock); \ |
1711 | req->ptr2 = malloc (len); \ |
1723 | req->ptr2 = malloc (len); \ |
1712 | if (!req->ptr2) \ |
1724 | if (!req->ptr2) \ |
1713 | { \ |
1725 | { \ |
1714 | errno = ENOMEM; \ |
1726 | errno = ENOMEM; \ |
1715 | req->result = -1; \ |
1727 | req->result = -1; \ |
1716 | break; \ |
1728 | break; \ |
1717 | } \ |
1729 | } \ |
1718 | } |
1730 | } |
1719 | |
1731 | |
1720 | static void ecb_noinline ecb_cold |
|
|
1721 | etp_proc_init (void) |
|
|
1722 | { |
|
|
1723 | #if HAVE_PRCTL_SET_NAME |
|
|
1724 | /* provide a more sensible "thread name" */ |
|
|
1725 | char name[16 + 1]; |
|
|
1726 | const int namelen = sizeof (name) - 1; |
|
|
1727 | int len; |
|
|
1728 | |
|
|
1729 | prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
1730 | name [namelen] = 0; |
|
|
1731 | len = strlen (name); |
|
|
1732 | strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio"); |
|
|
1733 | prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
1734 | #endif |
|
|
1735 | } |
|
|
1736 | |
|
|
1737 | /* TODO: move somehow to etp.c */ |
|
|
1738 | X_THREAD_PROC (etp_proc) |
|
|
1739 | { |
|
|
1740 | ETP_REQ *req; |
|
|
1741 | struct timespec ts; |
|
|
1742 | etp_worker *self = (etp_worker *)thr_arg; |
|
|
1743 | |
|
|
1744 | etp_proc_init (); |
|
|
1745 | |
|
|
1746 | /* try to distribute timeouts somewhat evenly */ |
|
|
1747 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
|
|
1748 | |
|
|
1749 | for (;;) |
|
|
1750 | { |
|
|
1751 | ts.tv_sec = 0; |
|
|
1752 | |
|
|
1753 | X_LOCK (reqlock); |
|
|
1754 | |
|
|
1755 | for (;;) |
|
|
1756 | { |
|
|
1757 | req = reqq_shift (&req_queue); |
|
|
1758 | |
|
|
1759 | if (req) |
|
|
1760 | break; |
|
|
1761 | |
|
|
1762 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
|
|
1763 | { |
|
|
1764 | X_UNLOCK (reqlock); |
|
|
1765 | X_LOCK (wrklock); |
|
|
1766 | --started; |
|
|
1767 | X_UNLOCK (wrklock); |
|
|
1768 | goto quit; |
|
|
1769 | } |
|
|
1770 | |
|
|
1771 | ++idle; |
|
|
1772 | |
|
|
1773 | if (idle <= max_idle) |
|
|
1774 | /* we are allowed to idle, so do so without any timeout */ |
|
|
1775 | X_COND_WAIT (reqwait, reqlock); |
|
|
1776 | else |
|
|
1777 | { |
|
|
1778 | /* initialise timeout once */ |
|
|
1779 | if (!ts.tv_sec) |
|
|
1780 | ts.tv_sec = time (0) + idle_timeout; |
|
|
1781 | |
|
|
1782 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
|
|
1783 | ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ |
|
|
1784 | } |
|
|
1785 | |
|
|
1786 | --idle; |
|
|
1787 | } |
|
|
1788 | |
|
|
1789 | --nready; |
|
|
1790 | |
|
|
1791 | X_UNLOCK (reqlock); |
|
|
1792 | |
|
|
1793 | if (req->type == ETP_TYPE_QUIT) |
|
|
1794 | goto quit; |
|
|
1795 | |
|
|
1796 | ETP_EXECUTE (self, req); |
|
|
1797 | |
|
|
1798 | X_LOCK (reslock); |
|
|
1799 | |
|
|
1800 | ++npending; |
|
|
1801 | |
|
|
1802 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
|
|
1803 | want_poll_cb (); |
|
|
1804 | |
|
|
1805 | etp_worker_clear (self); |
|
|
1806 | |
|
|
1807 | X_UNLOCK (reslock); |
|
|
1808 | } |
|
|
1809 | |
|
|
1810 | quit: |
|
|
1811 | free (req); |
|
|
1812 | |
|
|
1813 | X_LOCK (wrklock); |
|
|
1814 | etp_worker_free (self); |
|
|
1815 | X_UNLOCK (wrklock); |
|
|
1816 | |
|
|
1817 | return 0; |
|
|
1818 | } |
|
|
1819 | |
|
|
1820 | /*****************************************************************************/ |
1732 | /*****************************************************************************/ |
1821 | |
1733 | |
1822 | int ecb_cold |
1734 | int ecb_cold |
1823 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1735 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1824 | { |
1736 | { |
1825 | return etp_init (want_poll, done_poll); |
1737 | eio_want_poll_cb = want_poll; |
|
|
1738 | eio_done_poll_cb = done_poll; |
|
|
1739 | |
|
|
1740 | return etp_init (EIO_POOL, 0, 0, 0); |
1826 | } |
1741 | } |
1827 | |
1742 | |
1828 | ecb_inline void |
1743 | ecb_inline void |
1829 | eio_api_destroy (eio_req *req) |
1744 | eio_api_destroy (eio_req *req) |
1830 | { |
1745 | { |
… | |
… | |
1903 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1818 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1904 | : read (req->int1, req->ptr2, req->size); break; |
1819 | : read (req->int1, req->ptr2, req->size); break; |
1905 | case EIO_WRITE: req->result = req->offs >= 0 |
1820 | case EIO_WRITE: req->result = req->offs >= 0 |
1906 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1821 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1907 | : write (req->int1, req->ptr2, req->size); break; |
1822 | : write (req->int1, req->ptr2, req->size); break; |
|
|
1823 | |
|
|
1824 | case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break; |
|
|
1825 | case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break; |
1908 | |
1826 | |
1909 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1827 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1910 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1828 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1911 | |
1829 | |
1912 | #if HAVE_AT |
1830 | #if HAVE_AT |
… | |
… | |
1931 | ? rename (req->wd->str, req->ptr2) |
1849 | ? rename (req->wd->str, req->ptr2) |
1932 | : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; |
1850 | : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; |
1933 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
1851 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
1934 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
1852 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
1935 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1853 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1936 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
1937 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; |
|
|
1938 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1854 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1939 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
1855 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
1856 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
1857 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX); |
|
|
1858 | if (req->result == EIO_PATH_MAX) |
|
|
1859 | { |
|
|
1860 | req->result = -1; |
|
|
1861 | errno = ENAMETOOLONG; |
|
|
1862 | } |
|
|
1863 | break; |
1940 | case EIO_UTIME: |
1864 | case EIO_UTIME: |
1941 | case EIO_FUTIME: |
1865 | case EIO_FUTIME: |
1942 | { |
1866 | { |
1943 | struct timespec ts[2]; |
1867 | struct timespec ts[2]; |
1944 | struct timespec *times; |
1868 | struct timespec *times; |
… | |
… | |
1977 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
1901 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
1978 | case EIO_RENAME: req->result = rename (path , req->ptr2); break; |
1902 | case EIO_RENAME: req->result = rename (path , req->ptr2); break; |
1979 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
1903 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
1980 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
1904 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
1981 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
1905 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
1982 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
1983 | req->result = readlink (path, req->ptr2, PATH_MAX); break; |
|
|
1984 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1906 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1985 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
1907 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
1908 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
1909 | req->result = readlink (path, req->ptr2, EIO_PATH_MAX); |
|
|
1910 | if (req->result == EIO_PATH_MAX) |
|
|
1911 | { |
|
|
1912 | req->result = -1; |
|
|
1913 | errno = ENAMETOOLONG; |
|
|
1914 | } |
|
|
1915 | break; |
1986 | |
1916 | |
1987 | case EIO_UTIME: |
1917 | case EIO_UTIME: |
1988 | case EIO_FUTIME: |
1918 | case EIO_FUTIME: |
1989 | { |
1919 | { |
1990 | struct timeval tv[2]; |
1920 | struct timeval tv[2]; |
… | |
… | |
2055 | req->result = select (0, 0, 0, 0, &tv); |
1985 | req->result = select (0, 0, 0, 0, &tv); |
2056 | } |
1986 | } |
2057 | #endif |
1987 | #endif |
2058 | break; |
1988 | break; |
2059 | |
1989 | |
|
|
1990 | #if 0 |
2060 | case EIO_GROUP: |
1991 | case EIO_GROUP: |
2061 | abort (); /* handled in eio_request */ |
1992 | abort (); /* handled in eio_request */ |
|
|
1993 | #endif |
2062 | |
1994 | |
2063 | case EIO_NOP: |
1995 | case EIO_NOP: |
2064 | req->result = 0; |
1996 | req->result = 0; |
2065 | break; |
1997 | break; |
2066 | |
1998 | |
… | |
… | |
2169 | } |
2101 | } |
2170 | |
2102 | |
2171 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2103 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2172 | { |
2104 | { |
2173 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2105 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
|
|
2106 | } |
|
|
2107 | |
|
|
2108 | eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data) |
|
|
2109 | { |
|
|
2110 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND; |
|
|
2111 | } |
|
|
2112 | |
|
|
2113 | eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data) |
|
|
2114 | { |
|
|
2115 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND; |
2174 | } |
2116 | } |
2175 | |
2117 | |
2176 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2118 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2177 | { |
2119 | { |
2178 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
2120 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
… | |
… | |
2366 | void |
2308 | void |
2367 | eio_grp_add (eio_req *grp, eio_req *req) |
2309 | eio_grp_add (eio_req *grp, eio_req *req) |
2368 | { |
2310 | { |
2369 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2311 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2370 | |
2312 | |
2371 | grp->flags |= EIO_FLAG_GROUPADD; |
2313 | grp->flags |= ETP_FLAG_GROUPADD; |
2372 | |
2314 | |
2373 | ++grp->size; |
2315 | ++grp->size; |
2374 | req->grp = grp; |
2316 | req->grp = grp; |
2375 | |
2317 | |
2376 | req->grp_prev = 0; |
2318 | req->grp_prev = 0; |