1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libeio@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013,2016,2017 Marc Alexander Lehmann <libeio@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
11 | * this list of conditions and the following disclaimer. |
12 | * |
12 | * |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
… | |
… | |
42 | #endif |
42 | #endif |
43 | |
43 | |
44 | #include "eio.h" |
44 | #include "eio.h" |
45 | #include "ecb.h" |
45 | #include "ecb.h" |
46 | |
46 | |
47 | #ifdef EIO_STACKSIZE |
|
|
48 | # define X_STACKSIZE EIO_STACKSIZE |
|
|
49 | #endif |
|
|
50 | #include "xthread.h" |
|
|
51 | |
|
|
52 | #include <errno.h> |
47 | #include <errno.h> |
53 | #include <stddef.h> |
48 | #include <stddef.h> |
54 | #include <stdlib.h> |
49 | #include <stdlib.h> |
55 | #include <string.h> |
50 | #include <string.h> |
56 | #include <errno.h> |
51 | #include <errno.h> |
… | |
… | |
120 | #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) |
115 | #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) |
121 | |
116 | |
122 | #define chmod(path,mode) _chmod (path, mode) |
117 | #define chmod(path,mode) _chmod (path, mode) |
123 | #define dup(fd) _dup (fd) |
118 | #define dup(fd) _dup (fd) |
124 | #define dup2(fd1,fd2) _dup2 (fd1, fd2) |
119 | #define dup2(fd1,fd2) _dup2 (fd1, fd2) |
|
|
120 | #define pipe(fds) _pipe (fds, 4096, O_BINARY) |
125 | |
121 | |
|
|
122 | #define fcntl(fd,cmd,arg) EIO_ENOSYS () |
|
|
123 | #define ioctl(fd,cmd,arg) EIO_ENOSYS () |
126 | #define fchmod(fd,mode) EIO_ENOSYS () |
124 | #define fchmod(fd,mode) EIO_ENOSYS () |
127 | #define chown(path,uid,gid) EIO_ENOSYS () |
125 | #define chown(path,uid,gid) EIO_ENOSYS () |
128 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
126 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
129 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
127 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
130 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
128 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
131 | #define mknod(path,mode,dev) EIO_ENOSYS () |
129 | #define mknod(path,mode,dev) EIO_ENOSYS () |
132 | #define sync() EIO_ENOSYS () |
130 | #define sync() EIO_ENOSYS () |
133 | #define readlink(path,buf,s) EIO_ENOSYS () |
131 | #define readlink(path,buf,s) EIO_ENOSYS () |
134 | #define statvfs(path,buf) EIO_ENOSYS () |
132 | #define statvfs(path,buf) EIO_ENOSYS () |
135 | #define fstatvfs(fd,buf) EIO_ENOSYS () |
133 | #define fstatvfs(fd,buf) EIO_ENOSYS () |
|
|
134 | |
|
|
135 | #define pread(fd,buf,count,offset) eio__pread (fd, buf, count, offset) |
|
|
136 | #define pwrite(fd,buf,count,offset) eio__pwrite (fd, buf, count, offset) |
|
|
137 | |
|
|
138 | #if __GNUC__ |
|
|
139 | typedef long long eio_off_t; /* signed for compatibility to msvc */ |
|
|
140 | #else |
|
|
141 | typedef __int64 eio_off_t; /* unsigned not supported by msvc */ |
|
|
142 | #endif |
|
|
143 | |
|
|
144 | static eio_ssize_t |
|
|
145 | eio__pread (int fd, void *buf, eio_ssize_t count, eio_off_t offset) |
|
|
146 | { |
|
|
147 | OVERLAPPED o = { 0 }; |
|
|
148 | DWORD got; |
|
|
149 | |
|
|
150 | o.Offset = offset; |
|
|
151 | o.OffsetHigh = offset >> 32; |
|
|
152 | |
|
|
153 | return ReadFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o) |
|
|
154 | ? got : -1; |
|
|
155 | } |
|
|
156 | |
|
|
157 | static eio_ssize_t |
|
|
158 | eio__pwrite (int fd, void *buf, eio_ssize_t count, eio_off_t offset) |
|
|
159 | { |
|
|
160 | OVERLAPPED o = { 0 }; |
|
|
161 | DWORD got; |
|
|
162 | |
|
|
163 | o.Offset = offset; |
|
|
164 | o.OffsetHigh = offset >> 32; |
|
|
165 | |
|
|
166 | return WriteFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o) |
|
|
167 | ? got : -1; |
|
|
168 | } |
136 | |
169 | |
137 | /* rename() uses MoveFile, which fails to overwrite */ |
170 | /* rename() uses MoveFile, which fails to overwrite */ |
138 | #define rename(old,neu) eio__rename (old, neu) |
171 | #define rename(old,neu) eio__rename (old, neu) |
139 | |
172 | |
140 | static int |
173 | static int |
… | |
… | |
168 | /* we could even stat and see if it exists */ |
201 | /* we could even stat and see if it exists */ |
169 | static int |
202 | static int |
170 | symlink (const char *old, const char *neu) |
203 | symlink (const char *old, const char *neu) |
171 | { |
204 | { |
172 | #if WINVER >= 0x0600 |
205 | #if WINVER >= 0x0600 |
|
|
206 | int flags; |
|
|
207 | |
|
|
208 | /* This tries out all combinations of SYMBOLIC_LINK_FLAG_DIRECTORY |
|
|
209 | * and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE, |
|
|
210 | * with directory first. |
|
|
211 | */ |
|
|
212 | for (flags = 3; flags >= 0; --flags) |
173 | if (CreateSymbolicLink (neu, old, 1)) |
213 | if (CreateSymbolicLink (neu, old, flags)) |
174 | return 0; |
214 | return 0; |
175 | |
|
|
176 | if (CreateSymbolicLink (neu, old, 0)) |
|
|
177 | return 0; |
|
|
178 | #endif |
215 | #endif |
179 | |
216 | |
180 | return EIO_ERRNO (ENOENT, -1); |
217 | return EIO_ERRNO (ENOENT, -1); |
181 | } |
218 | } |
182 | |
219 | |
183 | /* POSIX API only */ |
220 | /* POSIX API only, causing trouble for win32 apps */ |
184 | #define CreateHardLink(neu,old,flags) 0 |
221 | #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */ |
185 | #define CreateSymbolicLink(neu,old,flags) 0 |
222 | #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */ |
186 | |
223 | |
187 | struct statvfs |
224 | struct statvfs |
188 | { |
225 | { |
189 | int dummy; |
226 | int dummy; |
190 | }; |
227 | }; |
… | |
… | |
194 | #define D_NAME(entp) entp.cFileName |
231 | #define D_NAME(entp) entp.cFileName |
195 | #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG) |
232 | #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG) |
196 | |
233 | |
197 | #else |
234 | #else |
198 | |
235 | |
|
|
236 | #include <sys/ioctl.h> |
199 | #include <sys/time.h> |
237 | #include <sys/time.h> |
200 | #include <sys/select.h> |
238 | #include <sys/select.h> |
201 | #include <sys/statvfs.h> |
|
|
202 | #include <unistd.h> |
239 | #include <unistd.h> |
203 | #include <signal.h> |
240 | #include <signal.h> |
204 | #include <dirent.h> |
241 | #include <dirent.h> |
205 | |
242 | |
|
|
243 | #ifdef ANDROID |
|
|
244 | #include <sys/vfs.h> |
|
|
245 | #define statvfs statfs |
|
|
246 | #define fstatvfs fstatfs |
|
|
247 | #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */ |
|
|
248 | #else |
|
|
249 | #include <sys/statvfs.h> |
|
|
250 | #endif |
|
|
251 | |
206 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
252 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
207 | #include <sys/mman.h> |
253 | #include <sys/mman.h> |
208 | #endif |
254 | #endif |
209 | |
255 | |
210 | #define D_NAME(entp) entp->d_name |
256 | #define D_NAME(entp) entp->d_name |
211 | |
257 | |
212 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
258 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
213 | #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
259 | #if __FreeBSD__ || __NetBSD__ || __OpenBSD__ |
214 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
260 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
215 | #define D_INO(de) (de)->d_fileno |
261 | #define D_INO(de) (de)->d_fileno |
216 | #define D_NAMLEN(de) (de)->d_namlen |
262 | #define D_NAMLEN(de) (de)->d_namlen |
217 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
263 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
218 | #define D_INO(de) (de)->d_ino |
264 | #define D_INO(de) (de)->d_ino |
… | |
… | |
237 | # include <utime.h> |
283 | # include <utime.h> |
238 | #endif |
284 | #endif |
239 | |
285 | |
240 | #if HAVE_SYS_SYSCALL_H |
286 | #if HAVE_SYS_SYSCALL_H |
241 | # include <sys/syscall.h> |
287 | # include <sys/syscall.h> |
242 | #endif |
|
|
243 | |
|
|
244 | #if HAVE_SYS_PRCTL_H |
|
|
245 | # include <sys/prctl.h> |
|
|
246 | #endif |
288 | #endif |
247 | |
289 | |
248 | #if HAVE_SENDFILE |
290 | #if HAVE_SENDFILE |
249 | # if __linux |
291 | # if __linux |
250 | # include <sys/sendfile.h> |
292 | # include <sys/sendfile.h> |
… | |
… | |
258 | # else |
300 | # else |
259 | # error sendfile support requested but not available |
301 | # error sendfile support requested but not available |
260 | # endif |
302 | # endif |
261 | #endif |
303 | #endif |
262 | |
304 | |
|
|
305 | #if HAVE_RENAMEAT2 |
|
|
306 | # include <sys/syscall.h> |
|
|
307 | # include <linux/fs.h> |
|
|
308 | #endif |
|
|
309 | |
263 | #ifndef D_TYPE |
310 | #ifndef D_TYPE |
264 | # define D_TYPE(de) 0 |
311 | # define D_TYPE(de) 0 |
265 | #endif |
312 | #endif |
266 | #ifndef D_INO |
313 | #ifndef D_INO |
267 | # define D_INO(de) 0 |
314 | # define D_INO(de) 0 |
… | |
… | |
275 | # define NAME_MAX 4096 |
322 | # define NAME_MAX 4096 |
276 | #endif |
323 | #endif |
277 | |
324 | |
278 | /* used for readlink etc. */ |
325 | /* used for readlink etc. */ |
279 | #ifndef PATH_MAX |
326 | #ifndef PATH_MAX |
280 | # define PATH_MAX 4096 |
327 | # define PATH_MAX 0 |
281 | #endif |
328 | #endif |
|
|
329 | |
|
|
330 | #ifndef O_CLOEXEC |
|
|
331 | #define O_CLOEXEC 0 |
|
|
332 | #endif |
|
|
333 | |
|
|
334 | #ifndef EIO_PATH_MIN |
|
|
335 | # define EIO_PATH_MIN 8160 |
|
|
336 | #endif |
|
|
337 | |
|
|
338 | #define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX) |
282 | |
339 | |
283 | /* buffer size for various temporary buffers */ |
340 | /* buffer size for various temporary buffers */ |
284 | #define EIO_BUFSIZE 65536 |
341 | #define EIO_BUFSIZE 65536 |
285 | |
342 | |
286 | #define dBUF \ |
343 | #define dBUF \ |
287 | char *eio_buf = malloc (EIO_BUFSIZE); \ |
344 | char *eio_buf = malloc (EIO_BUFSIZE); \ |
288 | errno = ENOMEM; \ |
345 | errno = ENOMEM; \ |
289 | if (!eio_buf) \ |
346 | if (!eio_buf) \ |
290 | return -1 |
347 | return -1 |
291 | |
348 | |
292 | #define FUBd \ |
349 | #define FUBd \ |
293 | free (eio_buf) |
350 | free (eio_buf) |
294 | |
351 | |
295 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
|
|
296 | |
|
|
297 | /*****************************************************************************/ |
352 | /*****************************************************************************/ |
298 | |
353 | |
299 | struct tmpbuf |
354 | struct etp_tmpbuf; |
|
|
355 | |
|
|
356 | #if _POSIX_VERSION >= 200809L |
|
|
357 | #define HAVE_AT 1 |
|
|
358 | #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
|
|
359 | #ifndef O_SEARCH |
|
|
360 | #define O_SEARCH O_RDONLY |
|
|
361 | #endif |
|
|
362 | #else |
|
|
363 | #define HAVE_AT 0 |
|
|
364 | static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path); |
|
|
365 | #endif |
|
|
366 | |
|
|
367 | struct eio_pwd |
300 | { |
368 | { |
301 | void *ptr; |
369 | #if HAVE_AT |
|
|
370 | int fd; |
|
|
371 | #endif |
302 | int len; |
372 | int len; |
|
|
373 | char str[1]; /* actually, a 0-terminated canonical path */ |
303 | }; |
374 | }; |
304 | |
|
|
305 | static void * |
|
|
306 | tmpbuf_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 | |
375 | |
317 | /*****************************************************************************/ |
376 | /*****************************************************************************/ |
318 | |
377 | |
319 | #define ETP_PRI_MIN EIO_PRI_MIN |
378 | #define ETP_PRI_MIN EIO_PRI_MIN |
320 | #define ETP_PRI_MAX EIO_PRI_MAX |
379 | #define ETP_PRI_MAX EIO_PRI_MAX |
321 | |
380 | |
|
|
381 | #define ETP_TYPE_QUIT -1 |
|
|
382 | #define ETP_TYPE_GROUP EIO_GROUP |
|
|
383 | |
|
|
384 | static void eio_nop_callback (void) { } |
|
|
385 | static void (*eio_want_poll_cb)(void) = eio_nop_callback; |
|
|
386 | static void (*eio_done_poll_cb)(void) = eio_nop_callback; |
|
|
387 | |
|
|
388 | #define ETP_WANT_POLL(pool) eio_want_poll_cb () |
|
|
389 | #define ETP_DONE_POLL(pool) eio_done_poll_cb () |
|
|
390 | |
322 | struct etp_worker; |
391 | struct etp_worker; |
323 | |
|
|
324 | #define ETP_REQ eio_req |
392 | #define ETP_REQ eio_req |
325 | #define ETP_DESTROY(req) eio_destroy (req) |
393 | #define ETP_DESTROY(req) eio_destroy (req) |
326 | static int eio_finish (eio_req *req); |
394 | static int eio_finish (eio_req *req); |
327 | #define ETP_FINISH(req) eio_finish (req) |
395 | #define ETP_FINISH(req) eio_finish (req) |
328 | static void eio_execute (struct etp_worker *self, eio_req *req); |
396 | static void eio_execute (struct etp_worker *self, eio_req *req); |
329 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) |
397 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) |
330 | |
398 | |
331 | /*****************************************************************************/ |
399 | #include "etp.c" |
332 | |
400 | |
333 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
401 | static struct etp_pool eio_pool; |
334 | |
402 | #define EIO_POOL (&eio_pool) |
335 | /* calculate time difference in ~1/EIO_TICKS of a second */ |
|
|
336 | ecb_inline int |
|
|
337 | tvdiff (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 | |
|
|
343 | static unsigned int started, idle, wanted = 4; |
|
|
344 | |
|
|
345 | static void (*want_poll_cb) (void); |
|
|
346 | static void (*done_poll_cb) (void); |
|
|
347 | |
|
|
348 | static unsigned int max_poll_time; /* reslock */ |
|
|
349 | static unsigned int max_poll_reqs; /* reslock */ |
|
|
350 | |
|
|
351 | static unsigned int nreqs; /* reqlock */ |
|
|
352 | static unsigned int nready; /* reqlock */ |
|
|
353 | static unsigned int npending; /* reqlock */ |
|
|
354 | static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ |
|
|
355 | static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ |
|
|
356 | |
|
|
357 | static xmutex_t wrklock; |
|
|
358 | static xmutex_t reslock; |
|
|
359 | static xmutex_t reqlock; |
|
|
360 | static 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 | */ |
|
|
368 | static xmutex_t preadwritelock; |
|
|
369 | #endif |
|
|
370 | |
|
|
371 | typedef 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 | |
|
|
385 | static 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 | |
|
|
392 | static void ecb_cold |
|
|
393 | etp_worker_clear (etp_worker *wrk) |
|
|
394 | { |
|
|
395 | } |
|
|
396 | |
|
|
397 | static void ecb_cold |
|
|
398 | etp_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 | |
|
|
408 | static unsigned int |
|
|
409 | etp_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 | |
|
|
418 | static unsigned int |
|
|
419 | etp_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 | |
|
|
430 | static unsigned int |
|
|
431 | etp_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 | |
|
|
442 | static unsigned int |
|
|
443 | etp_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 | */ |
|
|
459 | typedef struct { |
|
|
460 | ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */ |
|
|
461 | int size; |
|
|
462 | } etp_reqq; |
|
|
463 | |
|
|
464 | static etp_reqq req_queue; |
|
|
465 | static etp_reqq res_queue; |
|
|
466 | |
|
|
467 | static void ecb_noinline ecb_cold |
|
|
468 | reqq_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 | |
|
|
478 | static int ecb_noinline |
|
|
479 | reqq_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 | |
|
|
495 | static ETP_REQ * ecb_noinline |
|
|
496 | reqq_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 | |
|
|
521 | static int ecb_cold |
|
|
522 | etp_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 | |
|
|
547 | X_THREAD_PROC (etp_proc); |
|
|
548 | |
|
|
549 | static void ecb_cold |
|
|
550 | etp_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 | |
|
|
573 | static void |
|
|
574 | etp_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 | |
|
|
586 | static void ecb_cold |
|
|
587 | etp_end_thread (void) |
|
|
588 | { |
|
|
589 | eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */ |
|
|
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 | |
|
|
604 | static int |
|
|
605 | etp_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 | |
|
|
673 | static void |
|
|
674 | etp_cancel (ETP_REQ *req) |
|
|
675 | { |
|
|
676 | req->cancelled = 1; |
|
|
677 | |
|
|
678 | eio_grp_cancel (req); |
|
|
679 | } |
|
|
680 | |
|
|
681 | static void |
|
|
682 | etp_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 | |
|
|
718 | static void ecb_cold |
|
|
719 | etp_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 | |
|
|
726 | static void ecb_cold |
|
|
727 | etp_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 | |
|
|
734 | static void ecb_cold |
|
|
735 | etp_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 | |
|
|
742 | static void ecb_cold |
|
|
743 | etp_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 | |
|
|
750 | static void ecb_cold |
|
|
751 | etp_set_min_parallel (unsigned int nthreads) |
|
|
752 | { |
|
|
753 | if (wanted < nthreads) |
|
|
754 | wanted = nthreads; |
|
|
755 | } |
|
|
756 | |
|
|
757 | static void ecb_cold |
|
|
758 | etp_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 | |
403 | |
767 | /*****************************************************************************/ |
404 | /*****************************************************************************/ |
768 | |
405 | |
769 | static void |
406 | static void |
770 | grp_try_feed (eio_req *grp) |
407 | grp_try_feed (eio_req *grp) |
771 | { |
408 | { |
772 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
409 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
773 | { |
410 | { |
774 | grp->flags &= ~EIO_FLAG_GROUPADD; |
411 | grp->flags &= ~ETP_FLAG_GROUPADD; |
775 | |
412 | |
776 | EIO_FEED (grp); |
413 | EIO_FEED (grp); |
777 | |
414 | |
778 | /* stop if no progress has been made */ |
415 | /* stop if no progress has been made */ |
779 | if (!(grp->flags & EIO_FLAG_GROUPADD)) |
416 | if (!(grp->flags & ETP_FLAG_GROUPADD)) |
780 | { |
417 | { |
781 | grp->feed = 0; |
418 | grp->feed = 0; |
782 | break; |
419 | break; |
783 | } |
420 | } |
784 | } |
421 | } |
… | |
… | |
791 | |
428 | |
792 | /* call feeder, if applicable */ |
429 | /* call feeder, if applicable */ |
793 | grp_try_feed (grp); |
430 | grp_try_feed (grp); |
794 | |
431 | |
795 | /* finish, if done */ |
432 | /* finish, if done */ |
796 | if (!grp->size && grp->int1) |
433 | if (!grp->size && grp->flags & ETP_FLAG_DELAYED) |
797 | return eio_finish (grp); |
434 | return eio_finish (grp); |
798 | else |
435 | else |
799 | return 0; |
436 | return 0; |
800 | } |
437 | } |
801 | |
438 | |
… | |
… | |
837 | } |
474 | } |
838 | |
475 | |
839 | void |
476 | void |
840 | eio_grp_cancel (eio_req *grp) |
477 | eio_grp_cancel (eio_req *grp) |
841 | { |
478 | { |
842 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
479 | etp_grp_cancel (EIO_POOL, grp); |
843 | eio_cancel (grp); |
|
|
844 | } |
480 | } |
845 | |
481 | |
846 | void |
482 | void |
847 | eio_cancel (eio_req *req) |
483 | eio_cancel (eio_req *req) |
848 | { |
484 | { |
849 | etp_cancel (req); |
485 | etp_cancel (EIO_POOL, req); |
850 | } |
486 | } |
851 | |
487 | |
852 | void |
488 | void |
853 | eio_submit (eio_req *req) |
489 | eio_submit (eio_req *req) |
854 | { |
490 | { |
855 | etp_submit (req); |
491 | etp_submit (EIO_POOL, req); |
856 | } |
492 | } |
857 | |
493 | |
858 | unsigned int |
494 | unsigned int |
859 | eio_nreqs (void) |
495 | eio_nreqs (void) |
860 | { |
496 | { |
861 | return etp_nreqs (); |
497 | return etp_nreqs (EIO_POOL); |
862 | } |
498 | } |
863 | |
499 | |
864 | unsigned int |
500 | unsigned int |
865 | eio_nready (void) |
501 | eio_nready (void) |
866 | { |
502 | { |
867 | return etp_nready (); |
503 | return etp_nready (EIO_POOL); |
868 | } |
504 | } |
869 | |
505 | |
870 | unsigned int |
506 | unsigned int |
871 | eio_npending (void) |
507 | eio_npending (void) |
872 | { |
508 | { |
873 | return etp_npending (); |
509 | return etp_npending (EIO_POOL); |
874 | } |
510 | } |
875 | |
511 | |
876 | unsigned int ecb_cold |
512 | unsigned int ecb_cold |
877 | eio_nthreads (void) |
513 | eio_nthreads (void) |
878 | { |
514 | { |
879 | return etp_nthreads (); |
515 | return etp_nthreads (EIO_POOL); |
880 | } |
516 | } |
881 | |
517 | |
882 | void ecb_cold |
518 | void ecb_cold |
883 | eio_set_max_poll_time (double nseconds) |
519 | eio_set_max_poll_time (double nseconds) |
884 | { |
520 | { |
885 | etp_set_max_poll_time (nseconds); |
521 | etp_set_max_poll_time (EIO_POOL, nseconds); |
886 | } |
522 | } |
887 | |
523 | |
888 | void ecb_cold |
524 | void ecb_cold |
889 | eio_set_max_poll_reqs (unsigned int maxreqs) |
525 | eio_set_max_poll_reqs (unsigned int maxreqs) |
890 | { |
526 | { |
891 | etp_set_max_poll_reqs (maxreqs); |
527 | etp_set_max_poll_reqs (EIO_POOL, maxreqs); |
892 | } |
528 | } |
893 | |
529 | |
894 | void ecb_cold |
530 | void ecb_cold |
895 | eio_set_max_idle (unsigned int nthreads) |
531 | eio_set_max_idle (unsigned int nthreads) |
896 | { |
532 | { |
897 | etp_set_max_idle (nthreads); |
533 | etp_set_max_idle (EIO_POOL, nthreads); |
898 | } |
534 | } |
899 | |
535 | |
900 | void ecb_cold |
536 | void ecb_cold |
901 | eio_set_idle_timeout (unsigned int seconds) |
537 | eio_set_idle_timeout (unsigned int seconds) |
902 | { |
538 | { |
903 | etp_set_idle_timeout (seconds); |
539 | etp_set_idle_timeout (EIO_POOL, seconds); |
904 | } |
540 | } |
905 | |
541 | |
906 | void ecb_cold |
542 | void ecb_cold |
907 | eio_set_min_parallel (unsigned int nthreads) |
543 | eio_set_min_parallel (unsigned int nthreads) |
908 | { |
544 | { |
909 | etp_set_min_parallel (nthreads); |
545 | etp_set_min_parallel (EIO_POOL, nthreads); |
910 | } |
546 | } |
911 | |
547 | |
912 | void ecb_cold |
548 | void ecb_cold |
913 | eio_set_max_parallel (unsigned int nthreads) |
549 | eio_set_max_parallel (unsigned int nthreads) |
914 | { |
550 | { |
915 | etp_set_max_parallel (nthreads); |
551 | etp_set_max_parallel (EIO_POOL, nthreads); |
916 | } |
552 | } |
917 | |
553 | |
918 | int eio_poll (void) |
554 | int eio_poll (void) |
919 | { |
555 | { |
920 | return etp_poll (); |
556 | return etp_poll (EIO_POOL); |
921 | } |
557 | } |
922 | |
558 | |
923 | /*****************************************************************************/ |
559 | /*****************************************************************************/ |
924 | /* work around various missing functions */ |
560 | /* work around various missing functions */ |
925 | |
561 | |
926 | #if !HAVE_PREADWRITE |
562 | #if HAVE_POSIX_CLOSE && !__linux |
927 | # undef pread |
563 | # define eio__close(fd) posix_close (fd, 0) |
928 | # undef pwrite |
564 | #else |
929 | # define pread eio__pread |
565 | # define eio__close(fd) close (fd) |
930 | # define pwrite eio__pwrite |
|
|
931 | |
|
|
932 | static eio_ssize_t |
|
|
933 | eio__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 | |
|
|
948 | static eio_ssize_t |
|
|
949 | eio__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 |
566 | #endif |
|
|
567 | |
|
|
568 | /* close() without disturbing errno */ |
|
|
569 | static void |
|
|
570 | silent_close (int fd) |
|
|
571 | { |
|
|
572 | int saved_errno = errno; |
|
|
573 | eio__close (fd); |
|
|
574 | errno = saved_errno; |
|
|
575 | } |
964 | |
576 | |
965 | #ifndef HAVE_UTIMES |
577 | #ifndef HAVE_UTIMES |
966 | |
578 | |
967 | # undef utimes |
579 | # undef utimes |
968 | # define utimes(path,times) eio__utimes (path, times) |
580 | # define utimes(path,times) eio__utimes (path, times) |
… | |
… | |
1010 | int res; |
622 | int res; |
1011 | |
623 | |
1012 | #if HAVE_SYS_SYNCFS |
624 | #if HAVE_SYS_SYNCFS |
1013 | res = (int)syscall (__NR_syncfs, (int)(fd)); |
625 | res = (int)syscall (__NR_syncfs, (int)(fd)); |
1014 | #else |
626 | #else |
1015 | res = -1; |
627 | res = EIO_ENOSYS (); |
1016 | errno = ENOSYS; |
|
|
1017 | #endif |
628 | #endif |
1018 | |
629 | |
1019 | if (res < 0 && errno == ENOSYS && fd >= 0) |
630 | if (res < 0 && errno == ENOSYS && fd >= 0) |
1020 | sync (); |
631 | sync (); |
1021 | |
632 | |
… | |
… | |
1051 | } |
662 | } |
1052 | |
663 | |
1053 | static int |
664 | static int |
1054 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
665 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
1055 | { |
666 | { |
1056 | #if HAVE_FALLOCATE |
667 | #if HAVE_LINUX_FALLOCATE |
1057 | return fallocate (fd, mode, offset, len); |
668 | return fallocate (fd, mode, offset, len); |
1058 | #else |
669 | #else |
1059 | errno = ENOSYS; |
670 | return EIO_ENOSYS (); |
1060 | return -1; |
|
|
1061 | #endif |
671 | #endif |
1062 | } |
672 | } |
1063 | |
673 | |
1064 | #if !HAVE_READAHEAD |
674 | #if !HAVE_READAHEAD |
1065 | # undef readahead |
675 | # undef readahead |
… | |
… | |
1080 | todo -= len; |
690 | todo -= len; |
1081 | } |
691 | } |
1082 | |
692 | |
1083 | FUBd; |
693 | FUBd; |
1084 | |
694 | |
1085 | errno = 0; |
695 | /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */ |
|
|
696 | /* but not for e.g. EIO or eof, so we also never fail */ |
1086 | return count; |
697 | return 0; |
1087 | } |
698 | } |
1088 | |
699 | |
1089 | #endif |
700 | #endif |
1090 | |
701 | |
1091 | /* sendfile always needs emulation */ |
702 | /* sendfile always needs emulation */ |
… | |
… | |
1126 | |
737 | |
1127 | /* according to source inspection, this is correct, and useful behaviour */ |
738 | /* according to source inspection, this is correct, and useful behaviour */ |
1128 | if (sbytes) |
739 | if (sbytes) |
1129 | res = sbytes; |
740 | res = sbytes; |
1130 | |
741 | |
1131 | # elif defined (__APPLE__) |
742 | # elif defined __APPLE__ |
1132 | off_t sbytes = count; |
743 | off_t sbytes = count; |
1133 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
744 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1134 | |
745 | |
1135 | /* according to the manpage, sbytes is always valid */ |
746 | /* according to the manpage, sbytes is always valid */ |
1136 | if (sbytes) |
747 | if (sbytes) |
… | |
… | |
1163 | HANDLE h = TO_SOCKET (ifd); |
774 | HANDLE h = TO_SOCKET (ifd); |
1164 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
775 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
1165 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
776 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
1166 | |
777 | |
1167 | #else |
778 | #else |
1168 | res = -1; |
779 | res = EIO_ENOSYS (); |
1169 | errno = ENOSYS; |
|
|
1170 | #endif |
780 | #endif |
1171 | |
781 | |
1172 | /* we assume sendfile can copy at least 128mb in one go */ |
782 | /* we assume sendfile can copy at least 128mb in one go */ |
1173 | if (res <= 128 * 1024 * 1024) |
783 | if (res <= 128 * 1024 * 1024) |
1174 | { |
784 | { |
… | |
… | |
1349 | intptr_t end = addr + len; |
959 | intptr_t end = addr + len; |
1350 | intptr_t page = eio_pagesize (); |
960 | intptr_t page = eio_pagesize (); |
1351 | |
961 | |
1352 | if (addr < end) |
962 | if (addr < end) |
1353 | if (flags & EIO_MT_MODIFY) /* modify */ |
963 | if (flags & EIO_MT_MODIFY) /* modify */ |
1354 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
964 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < end && !EIO_CANCELLED (req)); |
1355 | else |
965 | else |
1356 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
966 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < end && !EIO_CANCELLED (req)); |
1357 | } |
967 | } |
1358 | |
968 | |
1359 | return 0; |
969 | return 0; |
1360 | } |
970 | } |
1361 | |
971 | |
1362 | /*****************************************************************************/ |
972 | /*****************************************************************************/ |
1363 | /* requests implemented outside eio_execute, because they are so large */ |
973 | /* requests implemented outside eio_execute, because they are so large */ |
1364 | |
974 | |
1365 | /* copies some absolute path to tmpbuf */ |
975 | static void |
1366 | static char * |
976 | eio__lseek (eio_req *req) |
1367 | eio__getwd (struct tmpbuf *tmpbuf, eio_wd wd) |
|
|
1368 | { |
977 | { |
1369 | if (wd == EIO_CWD) |
978 | /* this usually gets optimised away completely, or your compiler sucks, */ |
1370 | return getcwd (tmpbuf->ptr, PATH_MAX); |
979 | /* or the whence constants really are not 0, 1, 2 */ |
|
|
980 | int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET |
|
|
981 | : req->int2 == EIO_SEEK_CUR ? SEEK_CUR |
|
|
982 | : req->int2 == EIO_SEEK_END ? SEEK_END |
|
|
983 | : req->int2; |
1371 | |
984 | |
1372 | #if HAVE_AT |
985 | req->offs = lseek (req->int1, req->offs, whence); |
1373 | abort (); /*TODO*/ |
986 | req->result = req->offs == (off_t)-1 ? -1 : 0; |
1374 | #else |
|
|
1375 | strcpy (tmpbuf->ptr, wd); |
|
|
1376 | #endif |
|
|
1377 | return tmpbuf->ptr; |
|
|
1378 | } |
987 | } |
1379 | |
988 | |
1380 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
989 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
1381 | static int |
990 | static int |
1382 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
991 | eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1383 | { |
992 | { |
|
|
993 | char *res; |
1384 | const char *rel = path; |
994 | const char *rel = path; |
1385 | char *res; |
|
|
1386 | char *tmp1, *tmp2; |
995 | char *tmp1, *tmp2; |
1387 | #if SYMLOOP_MAX > 32 |
996 | #if SYMLOOP_MAX > 32 |
1388 | int symlinks = SYMLOOP_MAX; |
997 | int symlinks = SYMLOOP_MAX; |
1389 | #else |
998 | #else |
1390 | int symlinks = 32; |
999 | int symlinks = 32; |
1391 | #endif |
1000 | #endif |
1392 | |
1001 | |
1393 | /*D*/ /*TODO: wd ignored */ |
|
|
1394 | |
|
|
1395 | errno = EINVAL; |
1002 | errno = EINVAL; |
1396 | if (!rel) |
1003 | if (!rel) |
1397 | return -1; |
1004 | return -1; |
1398 | |
1005 | |
1399 | errno = ENOENT; |
1006 | errno = ENOENT; |
1400 | if (!*rel) |
1007 | if (!*rel) |
1401 | return -1; |
1008 | return -1; |
1402 | |
1009 | |
1403 | res = tmpbuf_get (tmpbuf, PATH_MAX * 3); |
1010 | res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3); |
|
|
1011 | #ifdef _WIN32 |
|
|
1012 | if (_access (rel, 4) != 0) |
|
|
1013 | return -1; |
|
|
1014 | |
|
|
1015 | symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0); |
|
|
1016 | |
|
|
1017 | errno = ENAMETOOLONG; |
|
|
1018 | if (symlinks >= EIO_PATH_MAX * 3) |
|
|
1019 | return -1; |
|
|
1020 | |
|
|
1021 | errno = EIO; |
|
|
1022 | if (symlinks <= 0) |
|
|
1023 | return -1; |
|
|
1024 | |
|
|
1025 | return symlinks; |
|
|
1026 | |
|
|
1027 | #else |
1404 | tmp1 = res + PATH_MAX; |
1028 | tmp1 = res + EIO_PATH_MAX; |
1405 | tmp2 = tmp1 + PATH_MAX; |
1029 | tmp2 = tmp1 + EIO_PATH_MAX; |
1406 | |
1030 | |
1407 | #if 0 /* disabled, the musl way to do things is just too racy */ |
1031 | #if 0 /* disabled, the musl way to do things is just too racy */ |
1408 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
1032 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
1409 | /* on linux we may be able to ask the kernel */ |
1033 | /* on linux we may be able to ask the kernel */ |
1410 | { |
1034 | { |
1411 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
1035 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
1412 | |
1036 | |
1413 | if (fd >= 0) |
1037 | if (fd >= 0) |
1414 | { |
1038 | { |
1415 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1039 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1416 | req->result = readlink (tmp1, res, PATH_MAX); |
1040 | req->result = readlink (tmp1, res, EIO_PATH_MAX); |
1417 | close (fd); |
|
|
1418 | |
|
|
1419 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
1041 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
|
|
1042 | eio__close (fd); |
1420 | |
1043 | |
1421 | if (req->result > 0) |
1044 | if (req->result > 0) |
1422 | goto done; |
1045 | goto done; |
1423 | } |
1046 | } |
1424 | else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) |
1047 | else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) |
1425 | return; |
1048 | return -1; |
1426 | } |
1049 | } |
1427 | #endif |
1050 | #endif |
1428 | #endif |
1051 | #endif |
1429 | |
1052 | |
1430 | if (*rel != '/') |
1053 | if (*rel != '/') |
1431 | { |
1054 | { |
1432 | if (!eio__getwd (tmpbuf, wd)) |
1055 | int len; |
|
|
1056 | |
|
|
1057 | errno = ENOENT; |
|
|
1058 | if (wd == EIO_INVALID_WD) |
1433 | return -1; |
1059 | return -1; |
|
|
1060 | |
|
|
1061 | if (wd == EIO_CWD) |
|
|
1062 | { |
|
|
1063 | if (!getcwd (res, EIO_PATH_MAX)) |
|
|
1064 | return -1; |
|
|
1065 | |
|
|
1066 | len = strlen (res); |
|
|
1067 | } |
|
|
1068 | else |
|
|
1069 | memcpy (res, wd->str, len = wd->len); |
1434 | |
1070 | |
1435 | if (res [1]) /* only use if not / */ |
1071 | if (res [1]) /* only use if not / */ |
1436 | res += strlen (res); |
1072 | res += len; |
1437 | } |
1073 | } |
1438 | |
1074 | |
1439 | while (*rel) |
1075 | while (*rel) |
1440 | { |
1076 | { |
1441 | eio_ssize_t len, linklen; |
1077 | eio_ssize_t len, linklen; |
… | |
… | |
1469 | } |
1105 | } |
1470 | } |
1106 | } |
1471 | |
1107 | |
1472 | errno = ENAMETOOLONG; |
1108 | errno = ENAMETOOLONG; |
1473 | if (res + 1 + len + 1 >= tmp1) |
1109 | if (res + 1 + len + 1 >= tmp1) |
1474 | return; |
1110 | return -1; |
1475 | |
1111 | |
1476 | /* copy one component */ |
1112 | /* copy one component */ |
1477 | *res = '/'; |
1113 | *res = '/'; |
1478 | memcpy (res + 1, beg, len); |
1114 | memcpy (res + 1, beg, len); |
1479 | |
1115 | |
1480 | /* zero-terminate, for readlink */ |
1116 | /* zero-terminate, for readlink */ |
1481 | res [len + 1] = 0; |
1117 | res [len + 1] = 0; |
1482 | |
1118 | |
1483 | /* now check if it's a symlink */ |
1119 | /* now check if it's a symlink */ |
1484 | linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); |
1120 | linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX); |
1485 | |
1121 | |
1486 | if (linklen < 0) |
1122 | if (linklen < 0) |
1487 | { |
1123 | { |
1488 | if (errno != EINVAL) |
1124 | if (errno != EINVAL) |
1489 | return -1; |
1125 | return -1; |
… | |
… | |
1495 | { |
1131 | { |
1496 | /* yay, it was a symlink - build new path in tmp2 */ |
1132 | /* yay, it was a symlink - build new path in tmp2 */ |
1497 | int rellen = strlen (rel); |
1133 | int rellen = strlen (rel); |
1498 | |
1134 | |
1499 | errno = ENAMETOOLONG; |
1135 | errno = ENAMETOOLONG; |
1500 | if (linklen + 1 + rellen >= PATH_MAX) |
1136 | if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */ |
1501 | return -1; |
1137 | return -1; |
1502 | |
1138 | |
1503 | errno = ELOOP; |
1139 | errno = ELOOP; |
1504 | if (!--symlinks) |
1140 | if (!--symlinks) |
1505 | return -1; |
1141 | return -1; |
… | |
… | |
1519 | /* special case for the lone root path */ |
1155 | /* special case for the lone root path */ |
1520 | if (res == tmpbuf->ptr) |
1156 | if (res == tmpbuf->ptr) |
1521 | *res++ = '/'; |
1157 | *res++ = '/'; |
1522 | |
1158 | |
1523 | return res - (char *)tmpbuf->ptr; |
1159 | return res - (char *)tmpbuf->ptr; |
|
|
1160 | #endif |
1524 | } |
1161 | } |
1525 | |
1162 | |
1526 | static signed char |
1163 | static signed char |
1527 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1164 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1528 | { |
1165 | { |
… | |
… | |
1688 | eio_dent_insertion_sort (dents, size); |
1325 | eio_dent_insertion_sort (dents, size); |
1689 | } |
1326 | } |
1690 | |
1327 | |
1691 | /* read a full directory */ |
1328 | /* read a full directory */ |
1692 | static void |
1329 | static void |
1693 | eio__scandir (eio_req *req) |
1330 | eio__scandir (eio_req *req, etp_worker *self) |
1694 | { |
1331 | { |
1695 | char *name, *names; |
1332 | char *name, *names; |
1696 | int namesalloc = 4096 - sizeof (void *) * 4; |
1333 | int namesalloc = 4096 - sizeof (void *) * 4; |
1697 | int namesoffs = 0; |
1334 | int namesoffs = 0; |
1698 | int flags = req->int1; |
1335 | int flags = req->int1; |
… | |
… | |
1716 | #ifdef _WIN32 |
1353 | #ifdef _WIN32 |
1717 | { |
1354 | { |
1718 | int len = strlen ((const char *)req->ptr1); |
1355 | int len = strlen ((const char *)req->ptr1); |
1719 | char *path = malloc (MAX_PATH); |
1356 | char *path = malloc (MAX_PATH); |
1720 | const char *fmt; |
1357 | const char *fmt; |
|
|
1358 | const char *reqpath = wd_expand (&self->tmpbuf, req->wd, req->ptr1); |
1721 | |
1359 | |
1722 | if (!len) |
1360 | if (!len) |
1723 | fmt = "./*"; |
1361 | fmt = "./*"; |
1724 | else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\') |
1362 | else if (reqpath[len - 1] == '/' || reqpath[len - 1] == '\\') |
1725 | fmt = "%s*"; |
1363 | fmt = "%s*"; |
1726 | else |
1364 | else |
1727 | fmt = "%s/*"; |
1365 | fmt = "%s/*"; |
1728 | |
1366 | |
1729 | _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1); |
1367 | _snprintf (path, MAX_PATH, fmt, reqpath); |
1730 | dirp = FindFirstFile (path, &entp); |
1368 | dirp = FindFirstFile (path, &entp); |
1731 | free (path); |
1369 | free (path); |
1732 | |
1370 | |
1733 | if (dirp == INVALID_HANDLE_VALUE) |
1371 | if (dirp == INVALID_HANDLE_VALUE) |
1734 | { |
1372 | { |
… | |
… | |
1756 | |
1394 | |
1757 | return; |
1395 | return; |
1758 | } |
1396 | } |
1759 | } |
1397 | } |
1760 | #else |
1398 | #else |
|
|
1399 | #if HAVE_AT |
|
|
1400 | if (req->wd) |
|
|
1401 | { |
|
|
1402 | int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY); |
|
|
1403 | |
|
|
1404 | if (fd < 0) |
|
|
1405 | return; |
|
|
1406 | |
|
|
1407 | dirp = fdopendir (fd); |
|
|
1408 | |
|
|
1409 | if (!dirp) |
|
|
1410 | silent_close (fd); |
|
|
1411 | } |
|
|
1412 | else |
1761 | dirp = opendir (req->ptr1); |
1413 | dirp = opendir (req->ptr1); |
|
|
1414 | #else |
|
|
1415 | dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); |
|
|
1416 | #endif |
1762 | |
1417 | |
1763 | if (!dirp) |
1418 | if (!dirp) |
1764 | return; |
1419 | return; |
1765 | #endif |
1420 | #endif |
1766 | |
1421 | |
… | |
… | |
1883 | #ifdef DT_FIFO |
1538 | #ifdef DT_FIFO |
1884 | case DT_FIFO: ent->type = EIO_DT_FIFO; break; |
1539 | case DT_FIFO: ent->type = EIO_DT_FIFO; break; |
1885 | #endif |
1540 | #endif |
1886 | #ifdef DT_CHR |
1541 | #ifdef DT_CHR |
1887 | case DT_CHR: ent->type = EIO_DT_CHR; break; |
1542 | case DT_CHR: ent->type = EIO_DT_CHR; break; |
1888 | #endif |
1543 | #endif |
1889 | #ifdef DT_MPC |
1544 | #ifdef DT_MPC |
1890 | case DT_MPC: ent->type = EIO_DT_MPC; break; |
1545 | case DT_MPC: ent->type = EIO_DT_MPC; break; |
1891 | #endif |
1546 | #endif |
1892 | #ifdef DT_DIR |
1547 | #ifdef DT_DIR |
1893 | case DT_DIR: ent->type = EIO_DT_DIR; break; |
1548 | case DT_DIR: ent->type = EIO_DT_DIR; break; |
1894 | #endif |
1549 | #endif |
1895 | #ifdef DT_NAM |
1550 | #ifdef DT_NAM |
1896 | case DT_NAM: ent->type = EIO_DT_NAM; break; |
1551 | case DT_NAM: ent->type = EIO_DT_NAM; break; |
1897 | #endif |
1552 | #endif |
1898 | #ifdef DT_BLK |
1553 | #ifdef DT_BLK |
1899 | case DT_BLK: ent->type = EIO_DT_BLK; break; |
1554 | case DT_BLK: ent->type = EIO_DT_BLK; break; |
1900 | #endif |
1555 | #endif |
1901 | #ifdef DT_MPB |
1556 | #ifdef DT_MPB |
1902 | case DT_MPB: ent->type = EIO_DT_MPB; break; |
1557 | case DT_MPB: ent->type = EIO_DT_MPB; break; |
1903 | #endif |
1558 | #endif |
1904 | #ifdef DT_REG |
1559 | #ifdef DT_REG |
1905 | case DT_REG: ent->type = EIO_DT_REG; break; |
1560 | case DT_REG: ent->type = EIO_DT_REG; break; |
1906 | #endif |
1561 | #endif |
1907 | #ifdef DT_NWK |
1562 | #ifdef DT_NWK |
1908 | case DT_NWK: ent->type = EIO_DT_NWK; break; |
1563 | case DT_NWK: ent->type = EIO_DT_NWK; break; |
1909 | #endif |
1564 | #endif |
1910 | #ifdef DT_CMP |
1565 | #ifdef DT_CMP |
1911 | case DT_CMP: ent->type = EIO_DT_CMP; break; |
1566 | case DT_CMP: ent->type = EIO_DT_CMP; break; |
1912 | #endif |
1567 | #endif |
1913 | #ifdef DT_LNK |
1568 | #ifdef DT_LNK |
1914 | case DT_LNK: ent->type = EIO_DT_LNK; break; |
1569 | case DT_LNK: ent->type = EIO_DT_LNK; break; |
1915 | #endif |
1570 | #endif |
1916 | #ifdef DT_SOCK |
1571 | #ifdef DT_SOCK |
1917 | case DT_SOCK: ent->type = EIO_DT_SOCK; break; |
1572 | case DT_SOCK: ent->type = EIO_DT_SOCK; break; |
… | |
… | |
1930 | { |
1585 | { |
1931 | if (ent->type == EIO_DT_UNKNOWN) |
1586 | if (ent->type == EIO_DT_UNKNOWN) |
1932 | { |
1587 | { |
1933 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1588 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1934 | ent->score = 1; |
1589 | ent->score = 1; |
1935 | else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ |
1590 | 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 */ |
1591 | ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ |
1937 | } |
1592 | } |
1938 | else if (ent->type == EIO_DT_DIR) |
1593 | else if (ent->type == EIO_DT_DIR) |
1939 | ent->score = 0; |
1594 | ent->score = 0; |
1940 | } |
1595 | } |
… | |
… | |
1960 | } |
1615 | } |
1961 | } |
1616 | } |
1962 | |
1617 | |
1963 | /*****************************************************************************/ |
1618 | /*****************************************************************************/ |
1964 | /* working directory stuff */ |
1619 | /* working directory stuff */ |
|
|
1620 | /* various deficiencies in the posix 2008 api force us to */ |
|
|
1621 | /* keep the absolute path in string form at all times */ |
|
|
1622 | /* fuck yeah. */ |
|
|
1623 | |
|
|
1624 | #if !HAVE_AT |
|
|
1625 | |
|
|
1626 | /* a bit like realpath, but usually faster because it doesn'T have to return */ |
|
|
1627 | /* an absolute or canonical path */ |
|
|
1628 | static const char * |
|
|
1629 | wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
1630 | { |
|
|
1631 | if (!wd || *path == '/') |
|
|
1632 | return path; |
|
|
1633 | |
|
|
1634 | if (path [0] == '.' && !path [1]) |
|
|
1635 | return wd->str; |
|
|
1636 | |
|
|
1637 | { |
|
|
1638 | int l1 = wd->len; |
|
|
1639 | int l2 = strlen (path); |
|
|
1640 | |
|
|
1641 | char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2); |
|
|
1642 | |
|
|
1643 | memcpy (res, wd->str, l1); |
|
|
1644 | res [l1] = '/'; |
|
|
1645 | memcpy (res + l1 + 1, path, l2 + 1); |
|
|
1646 | |
|
|
1647 | return res; |
|
|
1648 | } |
|
|
1649 | } |
|
|
1650 | |
|
|
1651 | #endif |
|
|
1652 | |
|
|
1653 | static eio_wd |
|
|
1654 | eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
1655 | { |
|
|
1656 | int fd; |
|
|
1657 | eio_wd res; |
|
|
1658 | int len = eio__realpath (tmpbuf, wd, path); |
|
|
1659 | |
|
|
1660 | if (len < 0) |
|
|
1661 | return EIO_INVALID_WD; |
1965 | |
1662 | |
1966 | #if HAVE_AT |
1663 | #if HAVE_AT |
|
|
1664 | fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); |
1967 | |
1665 | |
1968 | #define WD2FD(wd) ((wd) ? ((int)(long)(wd)) - 1 : AT_FDCWD) |
1666 | if (fd < 0) |
1969 | |
1667 | return EIO_INVALID_WD; |
1970 | #ifndef O_SEARCH |
|
|
1971 | # define O_SEARCH O_RDONLY |
|
|
1972 | #endif |
1668 | #endif |
|
|
1669 | |
|
|
1670 | res = malloc (sizeof (*res) + len); /* one extra 0-byte */ |
|
|
1671 | |
|
|
1672 | #if HAVE_AT |
|
|
1673 | res->fd = fd; |
|
|
1674 | #endif |
|
|
1675 | |
|
|
1676 | res->len = len; |
|
|
1677 | memcpy (res->str, tmpbuf->ptr, len); |
|
|
1678 | res->str [len] = 0; |
|
|
1679 | |
|
|
1680 | return res; |
|
|
1681 | } |
1973 | |
1682 | |
1974 | eio_wd |
1683 | eio_wd |
1975 | eio_wd_open_sync (eio_wd wd, const char *path) |
1684 | eio_wd_open_sync (eio_wd wd, const char *path) |
1976 | { |
1685 | { |
1977 | int fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); |
1686 | struct etp_tmpbuf tmpbuf = { }; |
1978 | |
|
|
1979 | return fd >= 0 ? (eio_wd)(long)(fd + 1) : EIO_INVALID_WD; |
|
|
1980 | } |
|
|
1981 | |
|
|
1982 | static eio_wd |
|
|
1983 | eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
1984 | { |
|
|
1985 | return eio_wd_open_sync (wd, path); |
1687 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
|
|
1688 | free (tmpbuf.ptr); |
|
|
1689 | |
|
|
1690 | return wd; |
1986 | } |
1691 | } |
1987 | |
1692 | |
1988 | void |
1693 | void |
1989 | eio_wd_close_sync (eio_wd wd) |
1694 | eio_wd_close_sync (eio_wd wd) |
1990 | { |
1695 | { |
1991 | int fd = WD2FD (wd); |
1696 | if (wd != EIO_INVALID_WD && wd != EIO_CWD) |
1992 | |
1697 | { |
1993 | if (fd >= 0) |
1698 | #if HAVE_AT |
1994 | close (fd); |
1699 | eio__close (wd->fd); |
|
|
1700 | #endif |
|
|
1701 | free (wd); |
|
|
1702 | } |
1995 | } |
1703 | } |
|
|
1704 | |
|
|
1705 | #if HAVE_AT |
|
|
1706 | |
|
|
1707 | static int |
|
|
1708 | eio__renameat2 (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags) |
|
|
1709 | { |
|
|
1710 | #if HAVE_RENAMEAT2 |
|
|
1711 | return syscall (SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags); |
|
|
1712 | #else |
|
|
1713 | if (flags) |
|
|
1714 | return EIO_ENOSYS (); |
|
|
1715 | |
|
|
1716 | return renameat (olddirfd, oldpath, newdirfd, newpath); |
|
|
1717 | #endif |
|
|
1718 | } |
|
|
1719 | |
|
|
1720 | /* they forgot these */ |
1996 | |
1721 | |
1997 | static int |
1722 | static int |
1998 | eio__truncateat (int dirfd, const char *path, off_t length) |
1723 | eio__truncateat (int dirfd, const char *path, off_t length) |
1999 | { |
1724 | { |
2000 | int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); |
1725 | int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); |
… | |
… | |
2002 | |
1727 | |
2003 | if (fd < 0) |
1728 | if (fd < 0) |
2004 | return fd; |
1729 | return fd; |
2005 | |
1730 | |
2006 | res = ftruncate (fd, length); |
1731 | res = ftruncate (fd, length); |
2007 | close (fd); |
1732 | silent_close (fd); |
2008 | return res; |
1733 | return res; |
2009 | } |
1734 | } |
2010 | |
1735 | |
2011 | static int |
1736 | static int |
2012 | eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) |
1737 | eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) |
… | |
… | |
2016 | |
1741 | |
2017 | if (fd < 0) |
1742 | if (fd < 0) |
2018 | return fd; |
1743 | return fd; |
2019 | |
1744 | |
2020 | res = fstatvfs (fd, buf); |
1745 | res = fstatvfs (fd, buf); |
2021 | close (fd); |
1746 | silent_close (fd); |
2022 | return res; |
1747 | return res; |
2023 | |
|
|
2024 | } |
|
|
2025 | |
|
|
2026 | #else |
|
|
2027 | |
|
|
2028 | /* on legacy systems, we represent the working directories simply by their path strings */ |
|
|
2029 | |
|
|
2030 | static const char * |
|
|
2031 | wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
2032 | { |
|
|
2033 | if (!wd || *path == '/') |
|
|
2034 | return path; |
|
|
2035 | |
|
|
2036 | { |
|
|
2037 | int l1 = strlen ((const char *)wd); |
|
|
2038 | int l2 = strlen (path); |
|
|
2039 | |
|
|
2040 | char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); |
|
|
2041 | |
|
|
2042 | memcpy (res, wd, l1); |
|
|
2043 | res [l1] = '/'; |
|
|
2044 | memcpy (res + l1 + 1, path, l2 + 1); |
|
|
2045 | |
|
|
2046 | return res; |
|
|
2047 | } |
|
|
2048 | } |
|
|
2049 | |
|
|
2050 | static eio_wd |
|
|
2051 | eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
2052 | { |
|
|
2053 | if (*path == '/') /* absolute paths ignore wd */ |
|
|
2054 | path = strdup (path); |
|
|
2055 | else if (path [0] == '.' && !path [1]) /* special case '.', as it is common */ |
|
|
2056 | return wd; |
|
|
2057 | else |
|
|
2058 | { |
|
|
2059 | int len = eio__realpath (tmpbuf, wd, path); |
|
|
2060 | |
|
|
2061 | path = EIO_INVALID_WD; |
|
|
2062 | |
|
|
2063 | if (len >= 0) |
|
|
2064 | { |
|
|
2065 | ((char *)tmpbuf->ptr)[len] = 0; |
|
|
2066 | path = strdup (tmpbuf->ptr); |
|
|
2067 | } |
|
|
2068 | } |
|
|
2069 | |
|
|
2070 | if (!path) |
|
|
2071 | path = EIO_INVALID_WD; |
|
|
2072 | |
|
|
2073 | return (eio_wd)path; |
|
|
2074 | } |
|
|
2075 | |
|
|
2076 | eio_wd |
|
|
2077 | eio_wd_open_sync (eio_wd wd, const char *path) |
|
|
2078 | { |
|
|
2079 | struct tmpbuf tmpbuf = { 0 }; |
|
|
2080 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
|
|
2081 | free (tmpbuf.ptr); |
|
|
2082 | |
|
|
2083 | return wd; |
|
|
2084 | } |
|
|
2085 | |
|
|
2086 | void |
|
|
2087 | eio_wd_close_sync (eio_wd wd) |
|
|
2088 | { |
|
|
2089 | if (wd != EIO_INVALID_WD) |
|
|
2090 | free (wd); |
|
|
2091 | } |
1748 | } |
2092 | |
1749 | |
2093 | #endif |
1750 | #endif |
2094 | |
1751 | |
2095 | /*****************************************************************************/ |
1752 | /*****************************************************************************/ |
2096 | |
1753 | |
2097 | #define ALLOC(len) \ |
1754 | #define ALLOC(len) \ |
2098 | if (!req->ptr2) \ |
1755 | if (!req->ptr2) \ |
2099 | { \ |
1756 | { \ |
2100 | X_LOCK (wrklock); \ |
1757 | X_LOCK (EIO_POOL->wrklock); \ |
2101 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1758 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
2102 | X_UNLOCK (wrklock); \ |
1759 | X_UNLOCK (EIO_POOL->wrklock); \ |
2103 | req->ptr2 = malloc (len); \ |
1760 | req->ptr2 = malloc (len); \ |
2104 | if (!req->ptr2) \ |
1761 | if (!req->ptr2) \ |
2105 | { \ |
1762 | { \ |
2106 | errno = ENOMEM; \ |
1763 | errno = ENOMEM; \ |
2107 | req->result = -1; \ |
1764 | req->result = -1; \ |
2108 | break; \ |
1765 | goto alloc_fail; \ |
2109 | } \ |
1766 | } \ |
2110 | } |
1767 | } |
2111 | |
1768 | |
2112 | X_THREAD_PROC (etp_proc) |
1769 | /*****************************************************************************/ |
2113 | { |
|
|
2114 | ETP_REQ *req; |
|
|
2115 | struct timespec ts; |
|
|
2116 | etp_worker *self = (etp_worker *)thr_arg; |
|
|
2117 | |
1770 | |
2118 | #if HAVE_PRCTL_SET_NAME |
1771 | static void |
2119 | prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0); |
1772 | eio__slurp (int fd, eio_req *req) |
2120 | #endif |
1773 | { |
|
|
1774 | req->result = fd; |
2121 | |
1775 | |
2122 | /* try to distribute timeouts somewhat evenly */ |
1776 | if (fd < 0) |
2123 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1777 | return; |
2124 | |
1778 | |
2125 | for (;;) |
1779 | if (req->offs < 0 || !req->size) /* do we need the size? */ |
2126 | { |
1780 | { |
2127 | ts.tv_sec = 0; |
1781 | off_t size = lseek (fd, 0, SEEK_END); |
2128 | |
1782 | |
2129 | X_LOCK (reqlock); |
|
|
2130 | |
|
|
2131 | for (;;) |
|
|
2132 | { |
|
|
2133 | req = reqq_shift (&req_queue); |
|
|
2134 | |
|
|
2135 | if (req) |
|
|
2136 | break; |
|
|
2137 | |
|
|
2138 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
|
|
2139 | { |
|
|
2140 | X_UNLOCK (reqlock); |
|
|
2141 | X_LOCK (wrklock); |
|
|
2142 | --started; |
|
|
2143 | X_UNLOCK (wrklock); |
|
|
2144 | goto quit; |
|
|
2145 | } |
|
|
2146 | |
|
|
2147 | ++idle; |
|
|
2148 | |
|
|
2149 | if (idle <= max_idle) |
|
|
2150 | /* we are allowed to idle, so do so without any timeout */ |
|
|
2151 | X_COND_WAIT (reqwait, reqlock); |
|
|
2152 | else |
|
|
2153 | { |
|
|
2154 | /* initialise timeout once */ |
|
|
2155 | if (!ts.tv_sec) |
|
|
2156 | ts.tv_sec = time (0) + idle_timeout; |
|
|
2157 | |
|
|
2158 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
|
|
2159 | ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ |
|
|
2160 | } |
|
|
2161 | |
|
|
2162 | --idle; |
|
|
2163 | } |
|
|
2164 | |
|
|
2165 | --nready; |
|
|
2166 | |
|
|
2167 | X_UNLOCK (reqlock); |
|
|
2168 | |
|
|
2169 | if (req->type < 0) |
1783 | if (req->offs < 0) |
2170 | goto quit; |
1784 | req->offs += size; |
2171 | |
1785 | |
2172 | ETP_EXECUTE (self, req); |
1786 | if (!req->size) |
2173 | |
1787 | req->size = size - req->offs; |
2174 | X_LOCK (reslock); |
|
|
2175 | |
|
|
2176 | ++npending; |
|
|
2177 | |
|
|
2178 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
|
|
2179 | want_poll_cb (); |
|
|
2180 | |
|
|
2181 | etp_worker_clear (self); |
|
|
2182 | |
|
|
2183 | X_UNLOCK (reslock); |
|
|
2184 | } |
1788 | } |
2185 | |
1789 | |
2186 | quit: |
1790 | ALLOC (req->size); |
2187 | free (req); |
1791 | req->result = pread (fd, req->ptr2, req->size, req->offs); |
2188 | |
1792 | |
2189 | X_LOCK (wrklock); |
1793 | silent_close (fd); |
2190 | etp_worker_free (self); |
|
|
2191 | X_UNLOCK (wrklock); |
|
|
2192 | |
1794 | |
2193 | return 0; |
1795 | alloc_fail: |
|
|
1796 | ; |
2194 | } |
1797 | } |
2195 | |
|
|
2196 | /*****************************************************************************/ |
|
|
2197 | |
1798 | |
2198 | int ecb_cold |
1799 | int ecb_cold |
2199 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1800 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
2200 | { |
1801 | { |
2201 | #if !HAVE_PREADWRITE |
1802 | eio_want_poll_cb = want_poll; |
2202 | X_MUTEX_CREATE (preadwritelock); |
1803 | eio_done_poll_cb = done_poll; |
2203 | #endif |
|
|
2204 | |
1804 | |
2205 | return etp_init (want_poll, done_poll); |
1805 | return etp_init (EIO_POOL, 0, 0, 0); |
2206 | } |
1806 | } |
2207 | |
1807 | |
2208 | ecb_inline void |
1808 | ecb_inline void |
2209 | eio_api_destroy (eio_req *req) |
1809 | eio_api_destroy (eio_req *req) |
2210 | { |
1810 | { |
2211 | free (req); |
1811 | free (req); |
2212 | } |
1812 | } |
2213 | |
1813 | |
2214 | #define REQ(rtype) \ |
1814 | #define REQ(rtype) \ |
2215 | eio_req *req; \ |
1815 | eio_req *req; \ |
2216 | \ |
1816 | \ |
2217 | req = (eio_req *)calloc (1, sizeof *req); \ |
1817 | req = (eio_req *)calloc (1, sizeof *req); \ |
2218 | if (!req) \ |
1818 | if (!req) \ |
2219 | return 0; \ |
1819 | return 0; \ |
… | |
… | |
2233 | { \ |
1833 | { \ |
2234 | eio_api_destroy (req); \ |
1834 | eio_api_destroy (req); \ |
2235 | return 0; \ |
1835 | return 0; \ |
2236 | } |
1836 | } |
2237 | |
1837 | |
|
|
1838 | #define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)]) |
|
|
1839 | |
2238 | static void |
1840 | static void |
2239 | eio_execute (etp_worker *self, eio_req *req) |
1841 | eio_execute (etp_worker *self, eio_req *req) |
2240 | { |
1842 | { |
2241 | #if HAVE_AT |
1843 | #if HAVE_AT |
2242 | int dirfd; |
1844 | int dirfd; |
… | |
… | |
2270 | switch (req->type) |
1872 | switch (req->type) |
2271 | { |
1873 | { |
2272 | case EIO_WD_OPEN: req->wd = eio__wd_open_sync (&self->tmpbuf, req->wd, req->ptr1); |
1874 | case EIO_WD_OPEN: req->wd = eio__wd_open_sync (&self->tmpbuf, req->wd, req->ptr1); |
2273 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
1875 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2274 | break; |
1876 | break; |
|
|
1877 | case EIO_WD_CLOSE: req->result = 0; |
|
|
1878 | eio_wd_close_sync (req->wd); break; |
|
|
1879 | |
2275 | case EIO_WD_CLOSE: eio_wd_close_sync (req->wd); break; |
1880 | case EIO_SEEK: eio__lseek (req); break; |
2276 | |
|
|
2277 | case EIO_READ: ALLOC (req->size); |
1881 | case EIO_READ: ALLOC (req->size); |
2278 | req->result = req->offs >= 0 |
1882 | req->result = req->offs >= 0 |
2279 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1883 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2280 | : read (req->int1, req->ptr2, req->size); break; |
1884 | : read (req->int1, req->ptr2, req->size); break; |
2281 | case EIO_WRITE: req->result = req->offs >= 0 |
1885 | case EIO_WRITE: req->result = req->offs >= 0 |
2282 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1886 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
2283 | : write (req->int1, req->ptr2, req->size); break; |
1887 | : write (req->int1, req->ptr2, req->size); break; |
2284 | |
1888 | |
|
|
1889 | case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break; |
|
|
1890 | case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break; |
|
|
1891 | |
2285 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1892 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
2286 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1893 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
2287 | |
1894 | |
2288 | #if HAVE_AT |
1895 | #if HAVE_AT |
|
|
1896 | |
2289 | case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
1897 | case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
2290 | req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, 0); break; |
1898 | req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, 0); break; |
2291 | case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
1899 | case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
2292 | req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; |
1900 | req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; |
2293 | case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; |
1901 | case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; |
2294 | case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; |
1902 | case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; |
2295 | case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; |
1903 | case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; |
2296 | case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; |
1904 | case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; |
|
|
1905 | case EIO_SLURP: eio__slurp ( openat (dirfd, req->ptr1, O_RDONLY | O_CLOEXEC), req); break; |
2297 | |
1906 | |
2298 | case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; |
1907 | case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; |
2299 | case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; |
1908 | case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */ |
|
|
1909 | req->result = req->wd && SINGLEDOT (req->ptr1) |
|
|
1910 | ? rmdir (req->wd->str) |
|
|
1911 | : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; |
2300 | case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; |
1912 | case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; |
2301 | case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2); break; |
1913 | case EIO_RENAME: req->result = eio__renameat2 ( |
|
|
1914 | dirfd, |
|
|
1915 | /* complications arise because "." cannot be renamed, so we might have to expand */ |
|
|
1916 | req->wd && SINGLEDOT (req->ptr1) ? req->wd->str : req->ptr1, |
|
|
1917 | WD2FD ((eio_wd)req->int3), |
|
|
1918 | req->ptr2, |
|
|
1919 | req->int2 |
|
|
1920 | ); |
|
|
1921 | break; |
2302 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2, 0); break; |
1922 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
2303 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
1923 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
2304 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1924 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
2305 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
2306 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; |
|
|
2307 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1925 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
2308 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
1926 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
1927 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
1928 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX); |
|
|
1929 | if (req->result == EIO_PATH_MAX) |
|
|
1930 | { |
|
|
1931 | req->result = -1; |
|
|
1932 | errno = ENAMETOOLONG; |
|
|
1933 | } |
|
|
1934 | break; |
|
|
1935 | case EIO_UTIME: |
|
|
1936 | case EIO_FUTIME: |
|
|
1937 | { |
|
|
1938 | struct timespec ts[2]; |
|
|
1939 | struct timespec *times; |
|
|
1940 | |
|
|
1941 | if (req->nv1 != -1. || req->nv2 != -1.) |
|
|
1942 | { |
|
|
1943 | ts[0].tv_sec = req->nv1; |
|
|
1944 | ts[0].tv_nsec = (req->nv1 - ts[0].tv_sec) * 1e9; |
|
|
1945 | ts[1].tv_sec = req->nv2; |
|
|
1946 | ts[1].tv_nsec = (req->nv2 - ts[1].tv_sec) * 1e9; |
|
|
1947 | |
|
|
1948 | times = ts; |
|
|
1949 | } |
|
|
1950 | else |
|
|
1951 | times = 0; |
|
|
1952 | |
|
|
1953 | req->result = req->type == EIO_FUTIME |
|
|
1954 | ? futimens (req->int1, times) |
|
|
1955 | : utimensat (dirfd, req->ptr1, times, 0); |
|
|
1956 | } |
|
|
1957 | break; |
|
|
1958 | |
2309 | #else |
1959 | #else |
|
|
1960 | |
2310 | case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
1961 | case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
2311 | req->result = stat (path , (EIO_STRUCT_STAT *)req->ptr2); break; |
1962 | req->result = stat (path , (EIO_STRUCT_STAT *)req->ptr2); break; |
2312 | case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
1963 | case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
2313 | req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; |
1964 | req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; |
2314 | case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; |
1965 | case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; |
2315 | case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break; |
1966 | case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break; |
2316 | case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; |
1967 | case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; |
2317 | case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; |
1968 | case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; |
|
|
1969 | case EIO_SLURP: eio__slurp ( open (path , O_RDONLY | O_CLOEXEC), req); break; |
2318 | |
1970 | |
2319 | case EIO_UNLINK: req->result = unlink (path ); break; |
1971 | case EIO_UNLINK: req->result = unlink (path ); break; |
2320 | case EIO_RMDIR: req->result = rmdir (path ); break; |
1972 | case EIO_RMDIR: req->result = rmdir (path ); break; |
2321 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
1973 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
2322 | case EIO_RENAME: req->result = rename (path , req->ptr2); break; |
1974 | case EIO_RENAME: req->result = req->int2 ? EIO_ENOSYS () : rename (path, req->ptr2); break; |
2323 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
1975 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
2324 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
1976 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
2325 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
1977 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
2326 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
2327 | req->result = readlink (path, req->ptr2, PATH_MAX); break; |
|
|
2328 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1978 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
2329 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
1979 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
1980 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
1981 | req->result = readlink (path, req->ptr2, EIO_PATH_MAX); |
|
|
1982 | if (req->result == EIO_PATH_MAX) |
|
|
1983 | { |
|
|
1984 | req->result = -1; |
|
|
1985 | errno = ENAMETOOLONG; |
|
|
1986 | } |
|
|
1987 | break; |
|
|
1988 | |
|
|
1989 | case EIO_UTIME: |
|
|
1990 | case EIO_FUTIME: |
|
|
1991 | { |
|
|
1992 | struct timeval tv[2]; |
|
|
1993 | struct timeval *times; |
|
|
1994 | |
|
|
1995 | if (req->nv1 != -1. || req->nv2 != -1.) |
|
|
1996 | { |
|
|
1997 | tv[0].tv_sec = req->nv1; |
|
|
1998 | tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1e6; |
|
|
1999 | tv[1].tv_sec = req->nv2; |
|
|
2000 | tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1e6; |
|
|
2001 | |
|
|
2002 | times = tv; |
|
|
2003 | } |
|
|
2004 | else |
|
|
2005 | times = 0; |
|
|
2006 | |
|
|
2007 | req->result = req->type == EIO_FUTIME |
|
|
2008 | ? futimes (req->int1, times) |
|
|
2009 | : utimes (req->ptr1, times); |
|
|
2010 | } |
|
|
2011 | break; |
|
|
2012 | |
2330 | #endif |
2013 | #endif |
2331 | |
2014 | |
2332 | case EIO_REALPATH: if (0 <= (req->result = eio__realpath (&self->tmpbuf, req->wd, req->ptr1))) |
2015 | case EIO_REALPATH: if (0 <= (req->result = eio__realpath (&self->tmpbuf, req->wd, req->ptr1))) |
2333 | { |
2016 | { |
2334 | ALLOC (req->result); |
2017 | ALLOC (req->result); |
… | |
… | |
2344 | |
2027 | |
2345 | case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
2028 | case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
2346 | case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; |
2029 | case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; |
2347 | case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
2030 | case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
2348 | |
2031 | |
2349 | case EIO_CLOSE: req->result = close (req->int1); break; |
2032 | case EIO_CLOSE: req->result = eio__close (req->int1); break; |
2350 | case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; |
2033 | case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; |
2351 | case EIO_SYNC: req->result = 0; sync (); break; |
2034 | case EIO_SYNC: req->result = 0; sync (); break; |
2352 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
2035 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
2353 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
2036 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
2354 | case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; |
2037 | case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; |
… | |
… | |
2357 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
2040 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
2358 | case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; |
2041 | case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; |
2359 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
2042 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
2360 | case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; |
2043 | case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; |
2361 | |
2044 | |
2362 | case EIO_READDIR: eio__scandir (req); break; |
2045 | case EIO_READDIR: eio__scandir (req, self); break; |
2363 | |
2046 | |
2364 | case EIO_BUSY: |
2047 | case EIO_BUSY: |
2365 | #ifdef _WIN32 |
2048 | #ifdef _WIN32 |
2366 | Sleep (req->nv1 * 1e3); |
2049 | Sleep (req->nv1 * 1e3); |
2367 | #else |
2050 | #else |
… | |
… | |
2374 | req->result = select (0, 0, 0, 0, &tv); |
2057 | req->result = select (0, 0, 0, 0, &tv); |
2375 | } |
2058 | } |
2376 | #endif |
2059 | #endif |
2377 | break; |
2060 | break; |
2378 | |
2061 | |
2379 | case EIO_UTIME: |
2062 | #if 0 |
2380 | case EIO_FUTIME: |
|
|
2381 | { |
|
|
2382 | struct timeval tv[2]; |
|
|
2383 | struct timeval *times; |
|
|
2384 | |
|
|
2385 | if (req->nv1 != -1. || req->nv2 != -1.) |
|
|
2386 | { |
|
|
2387 | tv[0].tv_sec = req->nv1; |
|
|
2388 | tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; |
|
|
2389 | tv[1].tv_sec = req->nv2; |
|
|
2390 | tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; |
|
|
2391 | |
|
|
2392 | times = tv; |
|
|
2393 | } |
|
|
2394 | else |
|
|
2395 | times = 0; |
|
|
2396 | |
|
|
2397 | req->result = req->type == EIO_FUTIME |
|
|
2398 | ? futimes (req->int1, times) |
|
|
2399 | : utimes (req->ptr1, times); |
|
|
2400 | } |
|
|
2401 | break; |
|
|
2402 | |
|
|
2403 | case EIO_GROUP: |
2063 | case EIO_GROUP: |
2404 | abort (); /* handled in eio_request */ |
2064 | abort (); /* handled in eio_request */ |
|
|
2065 | #endif |
2405 | |
2066 | |
2406 | case EIO_NOP: |
2067 | case EIO_NOP: |
2407 | req->result = 0; |
2068 | req->result = 0; |
2408 | break; |
2069 | break; |
2409 | |
2070 | |
2410 | case EIO_CUSTOM: |
2071 | case EIO_CUSTOM: |
2411 | req->feed (req); |
2072 | req->feed (req); |
2412 | break; |
2073 | break; |
2413 | |
2074 | |
2414 | default: |
2075 | default: |
2415 | errno = ENOSYS; |
|
|
2416 | req->result = -1; |
2076 | req->result = EIO_ENOSYS (); |
2417 | break; |
2077 | break; |
2418 | } |
2078 | } |
2419 | |
2079 | |
|
|
2080 | alloc_fail: |
2420 | req->errorno = errno; |
2081 | req->errorno = errno; |
2421 | } |
2082 | } |
2422 | |
2083 | |
2423 | #ifndef EIO_NO_WRAPPERS |
2084 | #ifndef EIO_NO_WRAPPERS |
2424 | |
2085 | |
|
|
2086 | eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data) |
|
|
2087 | { |
|
|
2088 | REQ (EIO_WD_OPEN); PATH; SEND; |
|
|
2089 | } |
|
|
2090 | |
|
|
2091 | eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data) |
|
|
2092 | { |
|
|
2093 | REQ (EIO_WD_CLOSE); req->wd = wd; SEND; |
|
|
2094 | } |
|
|
2095 | |
2425 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
2096 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
2426 | { |
2097 | { |
2427 | REQ (EIO_NOP); SEND; |
2098 | REQ (EIO_NOP); SEND; |
2428 | } |
2099 | } |
2429 | |
2100 | |
… | |
… | |
2490 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2161 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2491 | { |
2162 | { |
2492 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
2163 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
2493 | } |
2164 | } |
2494 | |
2165 | |
|
|
2166 | eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data) |
|
|
2167 | { |
|
|
2168 | REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND; |
|
|
2169 | } |
|
|
2170 | |
2495 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2171 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2496 | { |
2172 | { |
2497 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2173 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2498 | } |
2174 | } |
2499 | |
2175 | |
2500 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2176 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2501 | { |
2177 | { |
2502 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2178 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
|
|
2179 | } |
|
|
2180 | |
|
|
2181 | eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data) |
|
|
2182 | { |
|
|
2183 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND; |
|
|
2184 | } |
|
|
2185 | |
|
|
2186 | eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data) |
|
|
2187 | { |
|
|
2188 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND; |
2503 | } |
2189 | } |
2504 | |
2190 | |
2505 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2191 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2506 | { |
2192 | { |
2507 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
2193 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
… | |
… | |
2652 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
2338 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
2653 | { |
2339 | { |
2654 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
2340 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
2655 | } |
2341 | } |
2656 | |
2342 | |
|
|
2343 | eio_req *eio_slurp (const char *path, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
|
|
2344 | { |
|
|
2345 | REQ (EIO_SLURP); PATH; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
|
|
2346 | } |
|
|
2347 | |
2657 | eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) |
2348 | eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) |
2658 | { |
2349 | { |
2659 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
2350 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
2660 | } |
2351 | } |
2661 | |
2352 | |
… | |
… | |
2695 | void |
2386 | void |
2696 | eio_grp_add (eio_req *grp, eio_req *req) |
2387 | eio_grp_add (eio_req *grp, eio_req *req) |
2697 | { |
2388 | { |
2698 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2389 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2699 | |
2390 | |
2700 | grp->flags |= EIO_FLAG_GROUPADD; |
2391 | grp->flags |= ETP_FLAG_GROUPADD; |
2701 | |
2392 | |
2702 | ++grp->size; |
2393 | ++grp->size; |
2703 | req->grp = grp; |
2394 | req->grp = grp; |
2704 | |
2395 | |
2705 | req->grp_prev = 0; |
2396 | req->grp_prev = 0; |