cache.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. return 0;
  273. }
  274. static int cache_dirfill (void *buf, const char *name,
  275. const struct stat *stbuf, off_t off,
  276. enum fuse_fill_dir_flags flags)
  277. {
  278. int err;
  279. struct readdir_handle *ch;
  280. ch = (struct readdir_handle*) buf;
  281. err = ch->filler(ch->buf, name, stbuf, off, flags);
  282. if (!err) {
  283. g_ptr_array_add(ch->dir, g_strdup(name));
  284. if (stbuf->st_mode & S_IFMT) {
  285. char *fullpath;
  286. const char *basepath = !ch->path[1] ? "" : ch->path;
  287. fullpath = g_strdup_printf("%s/%s", basepath, name);
  288. cache_add_attr(fullpath, stbuf, ch->wrctr);
  289. g_free(fullpath);
  290. }
  291. }
  292. return err;
  293. }
  294. static int cache_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  295. off_t offset, struct fuse_file_info *fi,
  296. enum fuse_readdir_flags flags)
  297. {
  298. struct readdir_handle ch;
  299. struct file_handle *cfi;
  300. int err;
  301. char **dir;
  302. struct node *node;
  303. assert(offset == 0);
  304. pthread_mutex_lock(&cache.lock);
  305. node = cache_lookup(path);
  306. if (node != NULL && node->dir != NULL) {
  307. time_t now = time(NULL);
  308. if (node->dir_valid - now >= 0) {
  309. for(dir = node->dir; *dir != NULL; dir++)
  310. // FIXME: What about st_mode?
  311. filler(buf, *dir, NULL, 0, 0);
  312. pthread_mutex_unlock(&cache.lock);
  313. return 0;
  314. }
  315. }
  316. pthread_mutex_unlock(&cache.lock);
  317. cfi = (struct file_handle*) fi->fh;
  318. if(cfi->is_open)
  319. fi->fh = cfi->fs_fh;
  320. else {
  321. if(cache.next_oper->opendir) {
  322. err = cache.next_oper->opendir(path, fi);
  323. if(err)
  324. return err;
  325. }
  326. cfi->is_open = 1;
  327. cfi->fs_fh = fi->fh;
  328. }
  329. ch.path = path;
  330. ch.buf = buf;
  331. ch.filler = filler;
  332. ch.dir = g_ptr_array_new();
  333. ch.wrctr = cache_get_write_ctr();
  334. err = cache.next_oper->readdir(path, &ch, cache_dirfill, offset, fi, flags);
  335. g_ptr_array_add(ch.dir, NULL);
  336. dir = (char **) ch.dir->pdata;
  337. if (!err) {
  338. cache_add_dir(path, dir);
  339. } else {
  340. g_strfreev(dir);
  341. }
  342. g_ptr_array_free(ch.dir, FALSE);
  343. return err;
  344. }
  345. static int cache_mknod(const char *path, mode_t mode, dev_t rdev)
  346. {
  347. int err = cache.next_oper->mknod(path, mode, rdev);
  348. if (!err)
  349. cache_invalidate_dir(path);
  350. return err;
  351. }
  352. static int cache_mkdir(const char *path, mode_t mode)
  353. {
  354. int err = cache.next_oper->mkdir(path, mode);
  355. if (!err)
  356. cache_invalidate_dir(path);
  357. return err;
  358. }
  359. static int cache_unlink(const char *path)
  360. {
  361. int err = cache.next_oper->unlink(path);
  362. if (!err)
  363. cache_invalidate_dir(path);
  364. return err;
  365. }
  366. static int cache_rmdir(const char *path)
  367. {
  368. int err = cache.next_oper->rmdir(path);
  369. if (!err)
  370. cache_invalidate_dir(path);
  371. return err;
  372. }
  373. static int cache_symlink(const char *from, const char *to)
  374. {
  375. int err = cache.next_oper->symlink(from, to);
  376. if (!err)
  377. cache_invalidate_dir(to);
  378. return err;
  379. }
  380. static int cache_rename(const char *from, const char *to, unsigned int flags)
  381. {
  382. int err = cache.next_oper->rename(from, to, flags);
  383. if (!err)
  384. cache_do_rename(from, to);
  385. return err;
  386. }
  387. static int cache_link(const char *from, const char *to)
  388. {
  389. int err = cache.next_oper->link(from, to);
  390. if (!err) {
  391. cache_invalidate(from);
  392. cache_invalidate_dir(to);
  393. }
  394. return err;
  395. }
  396. static int cache_chmod(const char *path, mode_t mode,
  397. struct fuse_file_info *fi)
  398. {
  399. int err = cache.next_oper->chmod(path, mode, fi);
  400. if (!err)
  401. cache_invalidate(path);
  402. return err;
  403. }
  404. static int cache_chown(const char *path, uid_t uid, gid_t gid,
  405. struct fuse_file_info *fi)
  406. {
  407. int err = cache.next_oper->chown(path, uid, gid, fi);
  408. if (!err)
  409. cache_invalidate(path);
  410. return err;
  411. }
  412. static int cache_utimens(const char *path, const struct timespec tv[2],
  413. struct fuse_file_info *fi)
  414. {
  415. int err = cache.next_oper->utimens(path, tv, fi);
  416. if (!err)
  417. cache_invalidate(path);
  418. return err;
  419. }
  420. static int cache_write(const char *path, const char *buf, size_t size,
  421. off_t offset, struct fuse_file_info *fi)
  422. {
  423. int res = cache.next_oper->write(path, buf, size, offset, fi);
  424. if (res >= 0)
  425. cache_invalidate_write(path);
  426. return res;
  427. }
  428. static int cache_create(const char *path, mode_t mode,
  429. struct fuse_file_info *fi)
  430. {
  431. int err = cache.next_oper->create(path, mode, fi);
  432. if (!err)
  433. cache_invalidate_dir(path);
  434. return err;
  435. }
  436. static int cache_truncate(const char *path, off_t size,
  437. struct fuse_file_info *fi)
  438. {
  439. int err = cache.next_oper->truncate(path, size, fi);
  440. if (!err)
  441. cache_invalidate(path);
  442. return err;
  443. }
  444. static void cache_fill(struct fuse_operations *oper,
  445. struct fuse_operations *cache_oper)
  446. {
  447. cache_oper->access = oper->access;
  448. cache_oper->chmod = oper->chmod ? cache_chmod : NULL;
  449. cache_oper->chown = oper->chown ? cache_chown : NULL;
  450. cache_oper->create = oper->create ? cache_create : NULL;
  451. cache_oper->flush = oper->flush;
  452. cache_oper->fsync = oper->fsync;
  453. cache_oper->getattr = oper->getattr ? cache_getattr : NULL;
  454. cache_oper->getxattr = oper->getxattr;
  455. cache_oper->init = cache_init;
  456. cache_oper->link = oper->link ? cache_link : NULL;
  457. cache_oper->listxattr = oper->listxattr;
  458. cache_oper->mkdir = oper->mkdir ? cache_mkdir : NULL;
  459. cache_oper->mknod = oper->mknod ? cache_mknod : NULL;
  460. cache_oper->open = oper->open;
  461. cache_oper->opendir = cache_opendir;
  462. cache_oper->read = oper->read;
  463. cache_oper->readdir = oper->readdir ? cache_readdir : NULL;
  464. cache_oper->readlink = oper->readlink ? cache_readlink : NULL;
  465. cache_oper->release = oper->release;
  466. cache_oper->releasedir = cache_releasedir;
  467. cache_oper->removexattr = oper->removexattr;
  468. cache_oper->rename = oper->rename ? cache_rename : NULL;
  469. cache_oper->rmdir = oper->rmdir ? cache_rmdir : NULL;
  470. cache_oper->setxattr = oper->setxattr;
  471. cache_oper->statfs = oper->statfs;
  472. cache_oper->symlink = oper->symlink ? cache_symlink : NULL;
  473. cache_oper->truncate = oper->truncate ? cache_truncate : NULL;
  474. cache_oper->unlink = oper->unlink ? cache_unlink : NULL;
  475. cache_oper->utimens = oper->utimens ? cache_utimens : NULL;
  476. cache_oper->write = oper->write ? cache_write : NULL;
  477. }
  478. struct fuse_operations *cache_wrap(struct fuse_operations *oper) {
  479. static struct fuse_operations cache_oper;
  480. cache.next_oper = oper;
  481. cache_fill(oper, &cache_oper);
  482. pthread_mutex_init(&cache.lock, NULL);
  483. cache.table = g_hash_table_new_full(g_str_hash, g_str_equal,
  484. g_free, free_node);
  485. if (cache.table == NULL) {
  486. fprintf(stderr, "failed to create cache\n");
  487. return NULL;
  488. }
  489. return &cache_oper;
  490. }
  491. static const struct fuse_opt cache_opts[] = {
  492. { "dcache_timeout=%u", offsetof(struct cache, stat_timeout_secs), 0 },
  493. { "dcache_timeout=%u", offsetof(struct cache, dir_timeout_secs), 0 },
  494. { "dcache_timeout=%u", offsetof(struct cache, link_timeout_secs), 0 },
  495. { "dcache_stat_timeout=%u", offsetof(struct cache, stat_timeout_secs), 0 },
  496. { "dcache_dir_timeout=%u", offsetof(struct cache, dir_timeout_secs), 0 },
  497. { "dcache_link_timeout=%u", offsetof(struct cache, link_timeout_secs), 0 },
  498. { "dcache_max_size=%u", offsetof(struct cache, max_size), 0 },
  499. { "dcache_clean_interval=%u", offsetof(struct cache,
  500. clean_interval_secs), 0 },
  501. { "dcache_min_clean_interval=%u", offsetof(struct cache,
  502. min_clean_interval_secs), 0 },
  503. /* For backwards compatibility */
  504. { "cache_timeout=%u", offsetof(struct cache, stat_timeout_secs), 0 },
  505. { "cache_timeout=%u", offsetof(struct cache, dir_timeout_secs), 0 },
  506. { "cache_timeout=%u", offsetof(struct cache, link_timeout_secs), 0 },
  507. { "cache_stat_timeout=%u", offsetof(struct cache, stat_timeout_secs), 0 },
  508. { "cache_dir_timeout=%u", offsetof(struct cache, dir_timeout_secs), 0 },
  509. { "cache_link_timeout=%u", offsetof(struct cache, link_timeout_secs), 0 },
  510. { "cache_max_size=%u", offsetof(struct cache, max_size), 0 },
  511. { "cache_clean_interval=%u", offsetof(struct cache,
  512. clean_interval_secs), 0 },
  513. { "cache_min_clean_interval=%u", offsetof(struct cache,
  514. min_clean_interval_secs), 0 },
  515. FUSE_OPT_END
  516. };
  517. int cache_parse_options(struct fuse_args *args)
  518. {
  519. cache.stat_timeout_secs = DEFAULT_CACHE_TIMEOUT_SECS;
  520. cache.dir_timeout_secs = DEFAULT_CACHE_TIMEOUT_SECS;
  521. cache.link_timeout_secs = DEFAULT_CACHE_TIMEOUT_SECS;
  522. cache.max_size = DEFAULT_MAX_CACHE_SIZE;
  523. cache.clean_interval_secs = DEFAULT_CACHE_CLEAN_INTERVAL_SECS;
  524. cache.min_clean_interval_secs = DEFAULT_MIN_CACHE_CLEAN_INTERVAL_SECS;
  525. return fuse_opt_parse(args, &cache, cache_opts, NULL);
  526. }