cache.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. Caching file system proxy
  3. Copyright (C) 2004 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include "cache.h"
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <glib.h>
  14. #include <pthread.h>
  15. struct cache {
  16. int on;
  17. unsigned int stat_timeout_secs;
  18. unsigned int dir_timeout_secs;
  19. unsigned int link_timeout_secs;
  20. unsigned int max_size;
  21. unsigned int clean_interval_secs;
  22. unsigned int min_clean_interval_secs;
  23. struct fuse_operations *next_oper;
  24. GHashTable *table;
  25. pthread_mutex_t lock;
  26. time_t last_cleaned;
  27. uint64_t write_ctr;
  28. };
  29. static struct cache cache;
  30. struct node {
  31. struct stat stat;
  32. time_t stat_valid;
  33. char **dir;
  34. time_t dir_valid;
  35. char *link;
  36. time_t link_valid;
  37. time_t valid;
  38. };
  39. struct readdir_handle {
  40. const char *path;
  41. void *buf;
  42. fuse_fill_dir_t filler;
  43. GPtrArray *dir;
  44. uint64_t wrctr;
  45. };
  46. struct file_handle {
  47. /* Did we send an open request to the underlying fs? */
  48. int is_open;
  49. /* If so, this will hold its handle */
  50. unsigned long fs_fh;
  51. };
  52. static void free_node(gpointer node_)
  53. {
  54. struct node *node = (struct node *) node_;
  55. g_strfreev(node->dir);
  56. g_free(node);
  57. }
  58. static int cache_clean_entry(void *key_, struct node *node, time_t *now)
  59. {
  60. (void) key_;
  61. if (*now > node->valid)
  62. return TRUE;
  63. else
  64. return FALSE;
  65. }
  66. static void cache_clean(void)
  67. {
  68. time_t now = time(NULL);
  69. if (now > cache.last_cleaned + cache.min_clean_interval_secs &&
  70. (g_hash_table_size(cache.table) > cache.max_size ||
  71. now > cache.last_cleaned + cache.clean_interval_secs)) {
  72. g_hash_table_foreach_remove(cache.table,
  73. (GHRFunc) cache_clean_entry, &now);
  74. cache.last_cleaned = now;
  75. }
  76. }
  77. static struct node *cache_lookup(const char *path)
  78. {
  79. return (struct node *) g_hash_table_lookup(cache.table, path);
  80. }
  81. static void cache_purge(const char *path)
  82. {
  83. g_hash_table_remove(cache.table, path);
  84. }
  85. static void cache_purge_parent(const char *path)
  86. {
  87. const char *s = strrchr(path, '/');
  88. if (s) {
  89. if (s == path)
  90. g_hash_table_remove(cache.table, "/");
  91. else {
  92. char *parent = g_strndup(path, s - path);
  93. cache_purge(parent);
  94. g_free(parent);
  95. }
  96. }
  97. }
  98. void cache_invalidate(const char *path)
  99. {
  100. pthread_mutex_lock(&cache.lock);
  101. cache_purge(path);
  102. pthread_mutex_unlock(&cache.lock);
  103. }
  104. static void cache_invalidate_write(const char *path)
  105. {
  106. pthread_mutex_lock(&cache.lock);
  107. cache_purge(path);
  108. cache.write_ctr++;
  109. pthread_mutex_unlock(&cache.lock);
  110. }
  111. static void cache_invalidate_dir(const char *path)
  112. {
  113. pthread_mutex_lock(&cache.lock);
  114. cache_purge(path);
  115. cache_purge_parent(path);
  116. pthread_mutex_unlock(&cache.lock);
  117. }
  118. static int cache_del_children(const char *key, void *val_, const char *path)
  119. {
  120. (void) val_;
  121. if (strncmp(key, path, strlen(path)) == 0)
  122. return TRUE;
  123. else
  124. return FALSE;
  125. }
  126. static void cache_do_rename(const char *from, const char *to)
  127. {
  128. pthread_mutex_lock(&cache.lock);
  129. g_hash_table_foreach_remove(cache.table, (GHRFunc) cache_del_children,
  130. (char *) from);
  131. cache_purge(from);
  132. cache_purge(to);
  133. cache_purge_parent(from);
  134. cache_purge_parent(to);
  135. pthread_mutex_unlock(&cache.lock);
  136. }
  137. static struct node *cache_get(const char *path)
  138. {
  139. struct node *node = cache_lookup(path);
  140. if (node == NULL) {
  141. char *pathcopy = g_strdup(path);
  142. node = g_new0(struct node, 1);
  143. g_hash_table_insert(cache.table, pathcopy, node);
  144. }
  145. return node;
  146. }
  147. void cache_add_attr(const char *path, const struct stat *stbuf, uint64_t wrctr)
  148. {
  149. struct node *node;
  150. pthread_mutex_lock(&cache.lock);
  151. if (wrctr == cache.write_ctr) {
  152. node = cache_get(path);
  153. node->stat = *stbuf;
  154. node->stat_valid = time(NULL) + cache.stat_timeout_secs;
  155. if (node->stat_valid > node->valid)
  156. node->valid = node->stat_valid;
  157. cache_clean();
  158. }
  159. pthread_mutex_unlock(&cache.lock);
  160. }
  161. static void cache_add_dir(const char *path, char **dir)
  162. {
  163. struct node *node;
  164. pthread_mutex_lock(&cache.lock);
  165. node = cache_get(path);
  166. g_strfreev(node->dir);
  167. node->dir = dir;
  168. node->dir_valid = time(NULL) + cache.dir_timeout_secs;
  169. if (node->dir_valid > node->valid)
  170. node->valid = node->dir_valid;
  171. cache_clean();
  172. pthread_mutex_unlock(&cache.lock);
  173. }
  174. static size_t my_strnlen(const char *s, size_t maxsize)
  175. {
  176. const char *p;
  177. for (p = s; maxsize && *p; maxsize--, p++);
  178. return p - s;
  179. }
  180. void cache_add_link(const char *path, const char *link, size_t size)
  181. {
  182. struct node *node;
  183. pthread_mutex_lock(&cache.lock);
  184. node = cache_get(path);
  185. g_free(node->link);
  186. node->link = g_strndup(link, my_strnlen(link, size-1));
  187. node->link_valid = time(NULL) + cache.link_timeout_secs;
  188. if (node->link_valid > node->valid)
  189. node->valid = node->link_valid;
  190. cache_clean();
  191. pthread_mutex_unlock(&cache.lock);
  192. }
  193. static int cache_get_attr(const char *path, struct stat *stbuf)
  194. {
  195. struct node *node;
  196. int err = -EAGAIN;
  197. pthread_mutex_lock(&cache.lock);
  198. node = cache_lookup(path);
  199. if (node != NULL) {
  200. time_t now = time(NULL);
  201. if (node->stat_valid - now >= 0) {
  202. *stbuf = node->stat;
  203. err = 0;
  204. }
  205. }
  206. pthread_mutex_unlock(&cache.lock);
  207. return err;
  208. }
  209. uint64_t cache_get_write_ctr(void)
  210. {
  211. uint64_t res;
  212. pthread_mutex_lock(&cache.lock);
  213. res = cache.write_ctr;
  214. pthread_mutex_unlock(&cache.lock);
  215. return res;
  216. }
  217. static void *cache_init(struct fuse_conn_info *conn,
  218. struct fuse_config *cfg)
  219. {
  220. void *res;
  221. res = cache.next_oper->init(conn, cfg);
  222. // Cache requires a path for each request
  223. cfg->nullpath_ok = 0;
  224. return res;
  225. }
  226. static int cache_getattr(const char *path, struct stat *stbuf,
  227. struct fuse_file_info *fi)
  228. {
  229. int err = cache_get_attr(path, stbuf);
  230. if (err) {
  231. uint64_t wrctr = cache_get_write_ctr();
  232. err = cache.next_oper->getattr(path, stbuf, fi);
  233. if (!err)
  234. cache_add_attr(path, stbuf, wrctr);
  235. }
  236. return err;
  237. }
  238. static int cache_readlink(const char *path, char *buf, size_t size)
  239. {
  240. struct node *node;
  241. int err;
  242. pthread_mutex_lock(&cache.lock);
  243. node = cache_lookup(path);
  244. if (node != NULL) {
  245. time_t now = time(NULL);
  246. if (node->link_valid - now >= 0) {
  247. strncpy(buf, node->link, size-1);
  248. buf[size-1] = '\0';
  249. pthread_mutex_unlock(&cache.lock);
  250. return 0;
  251. }
  252. }
  253. pthread_mutex_unlock(&cache.lock);
  254. err = cache.next_oper->readlink(path, buf, size);
  255. if (!err)
  256. cache_add_link(path, buf, size);
  257. return err;
  258. }
  259. static int cache_opendir(const char *path, struct fuse_file_info *fi)
  260. {
  261. (void) path;
  262. struct file_handle *cfi;
  263. cfi = malloc(sizeof(struct file_handle));
  264. if(cfi == NULL)
  265. return -ENOMEM;
  266. cfi->is_open = 0;
  267. fi->fh = (unsigned long) cfi;
  268. return 0;
  269. }
  270. /* There's no dirs to release */
  271. static int cache_releasedir(const char *path, struct fuse_file_info *fi) {
  272. (void) path;
  273. (void) fi;
  274. return 0;
  275. }
  276. static int cache_dirfill (void *buf, const char *name,
  277. const struct stat *stbuf, off_t off,
  278. enum fuse_fill_dir_flags flags)
  279. {
  280. int err;
  281. struct readdir_handle *ch;
  282. ch = (struct readdir_handle*) buf;
  283. err = ch->filler(ch->buf, name, stbuf, off, flags);
  284. if (!err) {
  285. g_ptr_array_add(ch->dir, g_strdup(name));
  286. if (stbuf->st_mode & S_IFMT) {
  287. char *fullpath;
  288. const char *basepath = !ch->path[1] ? "" : ch->path;
  289. fullpath = g_strdup_printf("%s/%s", basepath, name);
  290. cache_add_attr(fullpath, stbuf, ch->wrctr);
  291. g_free(fullpath);
  292. }
  293. }
  294. return err;
  295. }
  296. static int cache_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  297. off_t offset, struct fuse_file_info *fi,
  298. enum fuse_readdir_flags flags)
  299. {
  300. struct readdir_handle ch;
  301. struct file_handle *cfi;
  302. int err;
  303. char **dir;
  304. struct node *node;
  305. assert(offset == 0);
  306. pthread_mutex_lock(&cache.lock);
  307. node = cache_lookup(path);
  308. if (node != NULL && node->dir != NULL) {
  309. time_t now = time(NULL);
  310. if (node->dir_valid - now >= 0) {
  311. for(dir = node->dir; *dir != NULL; dir++)
  312. // FIXME: What about st_mode?
  313. filler(buf, *dir, NULL, 0, 0);
  314. pthread_mutex_unlock(&cache.lock);
  315. return 0;
  316. }
  317. }
  318. pthread_mutex_unlock(&cache.lock);
  319. cfi = (struct file_handle *)(intptr_t) fi->fh;
  320. if(cfi->is_open)
  321. fi->fh = cfi->fs_fh;
  322. else {
  323. if(cache.next_oper->opendir) {
  324. err = cache.next_oper->opendir(path, fi);
  325. if(err)
  326. return err;
  327. }
  328. cfi->is_open = 1;
  329. cfi->fs_fh = fi->fh;
  330. }
  331. ch.path = path;
  332. ch.buf = buf;
  333. ch.filler = filler;
  334. ch.dir = g_ptr_array_new();
  335. ch.wrctr = cache_get_write_ctr();
  336. err = cache.next_oper->readdir(path, &ch, cache_dirfill, offset, fi, flags);
  337. g_ptr_array_add(ch.dir, NULL);
  338. dir = (char **) ch.dir->pdata;
  339. if (!err) {
  340. cache_add_dir(path, dir);
  341. } else {
  342. g_strfreev(dir);
  343. }
  344. g_ptr_array_free(ch.dir, FALSE);
  345. return err;
  346. }
  347. static int cache_mknod(const char *path, mode_t mode, dev_t rdev)
  348. {
  349. int err = cache.next_oper->mknod(path, mode, rdev);
  350. if (!err)
  351. cache_invalidate_dir(path);
  352. return err;
  353. }
  354. static int cache_mkdir(const char *path, mode_t mode)
  355. {
  356. int err = cache.next_oper->mkdir(path, mode);
  357. if (!err)
  358. cache_invalidate_dir(path);
  359. return err;
  360. }
  361. static int cache_unlink(const char *path)
  362. {
  363. int err = cache.next_oper->unlink(path);
  364. if (!err)
  365. cache_invalidate_dir(path);
  366. return err;
  367. }
  368. static int cache_rmdir(const char *path)
  369. {
  370. int err = cache.next_oper->rmdir(path);
  371. if (!err)
  372. cache_invalidate_dir(path);
  373. return err;
  374. }
  375. static int cache_symlink(const char *from, const char *to)
  376. {
  377. int err = cache.next_oper->symlink(from, to);
  378. if (!err)
  379. cache_invalidate_dir(to);
  380. return err;
  381. }
  382. static int cache_rename(const char *from, const char *to, unsigned int flags)
  383. {
  384. int err = cache.next_oper->rename(from, to, flags);
  385. if (!err)
  386. cache_do_rename(from, to);
  387. return err;
  388. }
  389. static int cache_link(const char *from, const char *to)
  390. {
  391. int err = cache.next_oper->link(from, to);
  392. if (!err) {
  393. cache_invalidate(from);
  394. cache_invalidate_dir(to);
  395. }
  396. return err;
  397. }
  398. static int cache_chmod(const char *path, mode_t mode,
  399. struct fuse_file_info *fi)
  400. {
  401. int err = cache.next_oper->chmod(path, mode, fi);
  402. if (!err)
  403. cache_invalidate(path);
  404. return err;
  405. }
  406. static int cache_chown(const char *path, uid_t uid, gid_t gid,
  407. struct fuse_file_info *fi)
  408. {
  409. int err = cache.next_oper->chown(path, uid, gid, fi);
  410. if (!err)
  411. cache_invalidate(path);
  412. return err;
  413. }
  414. static int cache_utimens(const char *path, const struct timespec tv[2],
  415. struct fuse_file_info *fi)
  416. {
  417. int err = cache.next_oper->utimens(path, tv, fi);
  418. if (!err)
  419. cache_invalidate(path);
  420. return err;
  421. }
  422. static int cache_write(const char *path, const char *buf, size_t size,
  423. off_t offset, struct fuse_file_info *fi)
  424. {
  425. int res = cache.next_oper->write(path, buf, size, offset, fi);
  426. if (res >= 0)
  427. cache_invalidate_write(path);
  428. return res;
  429. }
  430. static int cache_create(const char *path, mode_t mode,
  431. struct fuse_file_info *fi)
  432. {
  433. int err = cache.next_oper->create(path, mode, fi);
  434. if (!err)
  435. cache_invalidate_dir(path);
  436. return err;
  437. }
  438. static int cache_truncate(const char *path, off_t size,
  439. struct fuse_file_info *fi)
  440. {
  441. int err = cache.next_oper->truncate(path, size, fi);
  442. if (!err)
  443. cache_invalidate(path);
  444. return err;
  445. }
  446. static void cache_fill(struct fuse_operations *oper,
  447. struct fuse_operations *cache_oper)
  448. {
  449. cache_oper->access = oper->access;
  450. cache_oper->chmod = oper->chmod ? cache_chmod : NULL;
  451. cache_oper->chown = oper->chown ? cache_chown : NULL;
  452. cache_oper->create = oper->create ? cache_create : NULL;
  453. cache_oper->flush = oper->flush;
  454. cache_oper->fsync = oper->fsync;
  455. cache_oper->getattr = oper->getattr ? cache_getattr : NULL;
  456. cache_oper->getxattr = oper->getxattr;
  457. cache_oper->init = cache_init;
  458. cache_oper->link = oper->link ? cache_link : NULL;
  459. cache_oper->listxattr = oper->listxattr;
  460. cache_oper->mkdir = oper->mkdir ? cache_mkdir : NULL;
  461. cache_oper->mknod = oper->mknod ? cache_mknod : NULL;
  462. cache_oper->open = oper->open;
  463. cache_oper->opendir = cache_opendir;
  464. cache_oper->read = oper->read;
  465. cache_oper->readdir = oper->readdir ? cache_readdir : NULL;
  466. cache_oper->readlink = oper->readlink ? cache_readlink : NULL;
  467. cache_oper->release = oper->release;
  468. cache_oper->releasedir = cache_releasedir;
  469. cache_oper->removexattr = oper->removexattr;
  470. cache_oper->rename = oper->rename ? cache_rename : NULL;
  471. cache_oper->rmdir = oper->rmdir ? cache_rmdir : NULL;
  472. cache_oper->setxattr = oper->setxattr;
  473. cache_oper->statfs = oper->statfs;
  474. cache_oper->symlink = oper->symlink ? cache_symlink : NULL;
  475. cache_oper->truncate = oper->truncate ? cache_truncate : NULL;
  476. cache_oper->unlink = oper->unlink ? cache_unlink : NULL;
  477. cache_oper->utimens = oper->utimens ? cache_utimens : NULL;
  478. cache_oper->write = oper->write ? cache_write : NULL;
  479. }
  480. struct fuse_operations *cache_wrap(struct fuse_operations *oper) {
  481. static struct fuse_operations cache_oper;
  482. cache.next_oper = oper;
  483. cache_fill(oper, &cache_oper);
  484. pthread_mutex_init(&cache.lock, NULL);
  485. cache.table = g_hash_table_new_full(g_str_hash, g_str_equal,
  486. g_free, free_node);
  487. if (cache.table == NULL) {
  488. fprintf(stderr, "failed to create cache\n");
  489. return NULL;
  490. }
  491. return &cache_oper;
  492. }
  493. static const struct fuse_opt cache_opts[] = {
  494. { "dcache_timeout=%u", offsetof(struct cache, stat_timeout_secs), 0 },
  495. { "dcache_timeout=%u", offsetof(struct cache, dir_timeout_secs), 0 },
  496. { "dcache_timeout=%u", offsetof(struct cache, link_timeout_secs), 0 },
  497. { "dcache_stat_timeout=%u", offsetof(struct cache, stat_timeout_secs), 0 },
  498. { "dcache_dir_timeout=%u", offsetof(struct cache, dir_timeout_secs), 0 },
  499. { "dcache_link_timeout=%u", offsetof(struct cache, link_timeout_secs), 0 },
  500. { "dcache_max_size=%u", offsetof(struct cache, max_size), 0 },
  501. { "dcache_clean_interval=%u", offsetof(struct cache,
  502. clean_interval_secs), 0 },
  503. { "dcache_min_clean_interval=%u", offsetof(struct cache,
  504. min_clean_interval_secs), 0 },
  505. /* For backwards compatibility */
  506. { "cache_timeout=%u", offsetof(struct cache, stat_timeout_secs), 0 },
  507. { "cache_timeout=%u", offsetof(struct cache, dir_timeout_secs), 0 },
  508. { "cache_timeout=%u", offsetof(struct cache, link_timeout_secs), 0 },
  509. { "cache_stat_timeout=%u", offsetof(struct cache, stat_timeout_secs), 0 },
  510. { "cache_dir_timeout=%u", offsetof(struct cache, dir_timeout_secs), 0 },
  511. { "cache_link_timeout=%u", offsetof(struct cache, link_timeout_secs), 0 },
  512. { "cache_max_size=%u", offsetof(struct cache, max_size), 0 },
  513. { "cache_clean_interval=%u", offsetof(struct cache,
  514. clean_interval_secs), 0 },
  515. { "cache_min_clean_interval=%u", offsetof(struct cache,
  516. min_clean_interval_secs), 0 },
  517. FUSE_OPT_END
  518. };
  519. int cache_parse_options(struct fuse_args *args)
  520. {
  521. cache.stat_timeout_secs = DEFAULT_CACHE_TIMEOUT_SECS;
  522. cache.dir_timeout_secs = DEFAULT_CACHE_TIMEOUT_SECS;
  523. cache.link_timeout_secs = DEFAULT_CACHE_TIMEOUT_SECS;
  524. cache.max_size = DEFAULT_MAX_CACHE_SIZE;
  525. cache.clean_interval_secs = DEFAULT_CACHE_CLEAN_INTERVAL_SECS;
  526. cache.min_clean_interval_secs = DEFAULT_MIN_CACHE_CLEAN_INTERVAL_SECS;
  527. return fuse_opt_parse(args, &cache, cache_opts, NULL);
  528. }