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

Comparing libeio/eio.c (file contents):
Revision 1.107 by root, Tue Sep 27 00:41:52 2011 UTC vs.
Revision 1.120 by root, Tue Apr 24 18:47:50 2012 UTC

1/* 1/*
2 * libeio implementation 2 * libeio implementation
3 * 3 *
4 * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libeio@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010,2011,2012 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 *
208 #endif 208 #endif
209 209
210 #define D_NAME(entp) entp->d_name 210 #define D_NAME(entp) entp->d_name
211 211
212 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 212 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
213 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 213 #if __FreeBSD__ || __NetBSD__ || __OpenBSD__
214 #define _DIRENT_HAVE_D_TYPE /* sigh */ 214 #define _DIRENT_HAVE_D_TYPE /* sigh */
215 #define D_INO(de) (de)->d_fileno 215 #define D_INO(de) (de)->d_fileno
216 #define D_NAMLEN(de) (de)->d_namlen 216 #define D_NAMLEN(de) (de)->d_namlen
217 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 217 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
218 #define D_INO(de) (de)->d_ino 218 #define D_INO(de) (de)->d_ino
316 316
317struct tmpbuf; 317struct tmpbuf;
318 318
319#if _POSIX_VERSION >= 200809L 319#if _POSIX_VERSION >= 200809L
320 #define HAVE_AT 1 320 #define HAVE_AT 1
321 # define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 321 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
322 #ifndef O_SEARCH 322 #ifndef O_SEARCH
323 #define O_SEARCH O_RDONLY 323 #define O_SEARCH O_RDONLY
324 #endif 324 #endif
325#else 325#else
326 #define HAVE_AT 0 326 #define HAVE_AT 0
409#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 409#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
410#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 410#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
411 411
412/* worker threads management */ 412/* worker threads management */
413 413
414static void ecb_cold 414static void
415etp_worker_clear (etp_worker *wrk) 415etp_worker_clear (etp_worker *wrk)
416{ 416{
417} 417}
418 418
419static void ecb_cold 419static void ecb_cold
1032 int res; 1032 int res;
1033 1033
1034#if HAVE_SYS_SYNCFS 1034#if HAVE_SYS_SYNCFS
1035 res = (int)syscall (__NR_syncfs, (int)(fd)); 1035 res = (int)syscall (__NR_syncfs, (int)(fd));
1036#else 1036#else
1037 res = -1; 1037 res = EIO_ENOSYS ();
1038 errno = ENOSYS;
1039#endif 1038#endif
1040 1039
1041 if (res < 0 && errno == ENOSYS && fd >= 0) 1040 if (res < 0 && errno == ENOSYS && fd >= 0)
1042 sync (); 1041 sync ();
1043 1042
1073} 1072}
1074 1073
1075static int 1074static int
1076eio__fallocate (int fd, int mode, off_t offset, size_t len) 1075eio__fallocate (int fd, int mode, off_t offset, size_t len)
1077{ 1076{
1078#if HAVE_FALLOCATE 1077#if HAVE_LINUX_FALLOCATE
1079 return fallocate (fd, mode, offset, len); 1078 return fallocate (fd, mode, offset, len);
1080#else 1079#else
1081 errno = ENOSYS; 1080 return EIO_ENOSYS ();
1082 return -1;
1083#endif 1081#endif
1084} 1082}
1085 1083
1086#if !HAVE_READAHEAD 1084#if !HAVE_READAHEAD
1087# undef readahead 1085# undef readahead
1102 todo -= len; 1100 todo -= len;
1103 } 1101 }
1104 1102
1105 FUBd; 1103 FUBd;
1106 1104
1107 errno = 0; 1105 /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */
1106 /* but not for e.g. EIO or eof, so we also never fail */
1108 return count; 1107 return 0;
1109} 1108}
1110 1109
1111#endif 1110#endif
1112 1111
1113/* sendfile always needs emulation */ 1112/* sendfile always needs emulation */
1148 1147
1149 /* according to source inspection, this is correct, and useful behaviour */ 1148 /* according to source inspection, this is correct, and useful behaviour */
1150 if (sbytes) 1149 if (sbytes)
1151 res = sbytes; 1150 res = sbytes;
1152 1151
1153# elif defined (__APPLE__) 1152# elif defined __APPLE__
1154 off_t sbytes = count; 1153 off_t sbytes = count;
1155 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1154 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
1156 1155
1157 /* according to the manpage, sbytes is always valid */ 1156 /* according to the manpage, sbytes is always valid */
1158 if (sbytes) 1157 if (sbytes)
1185 HANDLE h = TO_SOCKET (ifd); 1184 HANDLE h = TO_SOCKET (ifd);
1186 SetFilePointer (h, offset, 0, FILE_BEGIN); 1185 SetFilePointer (h, offset, 0, FILE_BEGIN);
1187 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1186 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1188 1187
1189#else 1188#else
1190 res = -1; 1189 res = EIO_ENOSYS ();
1191 errno = ENOSYS;
1192#endif 1190#endif
1193 1191
1194 /* we assume sendfile can copy at least 128mb in one go */ 1192 /* we assume sendfile can copy at least 128mb in one go */
1195 if (res <= 128 * 1024 * 1024) 1193 if (res <= 128 * 1024 * 1024)
1196 { 1194 {
1382} 1380}
1383 1381
1384/*****************************************************************************/ 1382/*****************************************************************************/
1385/* requests implemented outside eio_execute, because they are so large */ 1383/* requests implemented outside eio_execute, because they are so large */
1386 1384
1385static void
1386eio__lseek (eio_req *req)
1387{
1388 /* this usually gets optimised away completely, or your compiler sucks, */
1389 /* or the whence constants really are not 0, 1, 2 */
1390 int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET
1391 : req->int2 == EIO_SEEK_CUR ? SEEK_CUR
1392 : req->int2 == EIO_SEEK_END ? SEEK_END
1393 : req->int2;
1394
1395 req->offs = lseek (req->int1, req->offs, whence);
1396 req->result = req->offs == (off_t)-1 ? -1 : 0;
1397}
1398
1387/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 1399/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1388static int 1400static int
1389eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1401eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
1390{ 1402{
1391 const char *rel = path; 1403 const char *rel = path;
1487 } 1499 }
1488 } 1500 }
1489 1501
1490 errno = ENAMETOOLONG; 1502 errno = ENAMETOOLONG;
1491 if (res + 1 + len + 1 >= tmp1) 1503 if (res + 1 + len + 1 >= tmp1)
1492 return; 1504 return -1;
1493 1505
1494 /* copy one component */ 1506 /* copy one component */
1495 *res = '/'; 1507 *res = '/';
1496 memcpy (res + 1, beg, len); 1508 memcpy (res + 1, beg, len);
1497 1509
1784 1796
1785 if (fd < 0) 1797 if (fd < 0)
1786 return; 1798 return;
1787 1799
1788 dirp = fdopendir (fd); 1800 dirp = fdopendir (fd);
1801
1802 if (!dirp)
1803 close (fd);
1789 } 1804 }
1790 else 1805 else
1791 dirp = opendir (req->ptr1); 1806 dirp = opendir (req->ptr1);
1792 #else 1807 #else
1793 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); 1808 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
1963 { 1978 {
1964 if (ent->type == EIO_DT_UNKNOWN) 1979 if (ent->type == EIO_DT_UNKNOWN)
1965 { 1980 {
1966 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ 1981 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1967 ent->score = 1; 1982 ent->score = 1;
1968 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ 1983 else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */
1969 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ 1984 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */
1970 } 1985 }
1971 else if (ent->type == EIO_DT_DIR) 1986 else if (ent->type == EIO_DT_DIR)
1972 ent->score = 0; 1987 ent->score = 0;
1973 } 1988 }
2130 req->result = -1; \ 2145 req->result = -1; \
2131 break; \ 2146 break; \
2132 } \ 2147 } \
2133 } 2148 }
2134 2149
2150static void ecb_noinline ecb_cold
2151etp_proc_init (void)
2152{
2153#if HAVE_PRCTL_SET_NAME
2154 /* provide a more sensible "thread name" */
2155 char name[16 + 1];
2156 const int namelen = sizeof (name) - 1;
2157 int len;
2158
2159 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0);
2160 name [namelen] = 0;
2161 len = strlen (name);
2162 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2163 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2164#endif
2165}
2166
2135X_THREAD_PROC (etp_proc) 2167X_THREAD_PROC (etp_proc)
2136{ 2168{
2137 ETP_REQ *req; 2169 ETP_REQ *req;
2138 struct timespec ts; 2170 struct timespec ts;
2139 etp_worker *self = (etp_worker *)thr_arg; 2171 etp_worker *self = (etp_worker *)thr_arg;
2140 2172
2141#if HAVE_PRCTL_SET_NAME 2173 etp_proc_init ();
2142 prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0);
2143#endif
2144 2174
2145 /* try to distribute timeouts somewhat evenly */ 2175 /* try to distribute timeouts somewhat evenly */
2146 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 2176 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2147 2177
2148 for (;;) 2178 for (;;)
2210 free (req); 2240 free (req);
2211 2241
2212 X_LOCK (wrklock); 2242 X_LOCK (wrklock);
2213 etp_worker_free (self); 2243 etp_worker_free (self);
2214 X_UNLOCK (wrklock); 2244 X_UNLOCK (wrklock);
2215
2216 return 0;
2217} 2245}
2218 2246
2219/*****************************************************************************/ 2247/*****************************************************************************/
2220 2248
2221int ecb_cold 2249int ecb_cold
2296 req->result = req->wd == EIO_INVALID_WD ? -1 : 0; 2324 req->result = req->wd == EIO_INVALID_WD ? -1 : 0;
2297 break; 2325 break;
2298 case EIO_WD_CLOSE: req->result = 0; 2326 case EIO_WD_CLOSE: req->result = 0;
2299 eio_wd_close_sync (req->wd); break; 2327 eio_wd_close_sync (req->wd); break;
2300 2328
2329 case EIO_SEEK: eio__lseek (req); break;
2301 case EIO_READ: ALLOC (req->size); 2330 case EIO_READ: ALLOC (req->size);
2302 req->result = req->offs >= 0 2331 req->result = req->offs >= 0
2303 ? pread (req->int1, req->ptr2, req->size, req->offs) 2332 ? pread (req->int1, req->ptr2, req->size, req->offs)
2304 : read (req->int1, req->ptr2, req->size); break; 2333 : read (req->int1, req->ptr2, req->size); break;
2305 case EIO_WRITE: req->result = req->offs >= 0 2334 case EIO_WRITE: req->result = req->offs >= 0
2461 case EIO_CUSTOM: 2490 case EIO_CUSTOM:
2462 req->feed (req); 2491 req->feed (req);
2463 break; 2492 break;
2464 2493
2465 default: 2494 default:
2466 errno = ENOSYS;
2467 req->result = -1; 2495 req->result = EIO_ENOSYS ();
2468 break; 2496 break;
2469 } 2497 }
2470 2498
2471 req->errorno = errno; 2499 req->errorno = errno;
2472} 2500}
2473 2501
2474#ifndef EIO_NO_WRAPPERS 2502#ifndef EIO_NO_WRAPPERS
2475 2503
2504eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data)
2505{
2506 REQ (EIO_WD_OPEN); PATH; SEND;
2507}
2508
2509eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data)
2510{
2511 REQ (EIO_WD_CLOSE); req->wd = wd; SEND;
2512}
2513
2476eio_req *eio_nop (int pri, eio_cb cb, void *data) 2514eio_req *eio_nop (int pri, eio_cb cb, void *data)
2477{ 2515{
2478 REQ (EIO_NOP); SEND; 2516 REQ (EIO_NOP); SEND;
2479} 2517}
2480 2518
2539} 2577}
2540 2578
2541eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) 2579eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
2542{ 2580{
2543 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; 2581 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
2582}
2583
2584eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data)
2585{
2586 REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND;
2544} 2587}
2545 2588
2546eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2589eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2547{ 2590{
2548 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2591 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines