cookie.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /* netfs cookie management
  2. *
  3. * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * See Documentation/filesystems/caching/netfs-api.txt for more information on
  12. * the netfs API.
  13. */
  14. #define FSCACHE_DEBUG_LEVEL COOKIE
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include "internal.h"
  18. struct kmem_cache *fscache_cookie_jar;
  19. static atomic_t fscache_object_debug_id = ATOMIC_INIT(0);
  20. static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie);
  21. static int fscache_alloc_object(struct fscache_cache *cache,
  22. struct fscache_cookie *cookie);
  23. static int fscache_attach_object(struct fscache_cookie *cookie,
  24. struct fscache_object *object);
  25. /*
  26. * initialise an cookie jar slab element prior to any use
  27. */
  28. void fscache_cookie_init_once(void *_cookie)
  29. {
  30. struct fscache_cookie *cookie = _cookie;
  31. memset(cookie, 0, sizeof(*cookie));
  32. spin_lock_init(&cookie->lock);
  33. spin_lock_init(&cookie->stores_lock);
  34. INIT_HLIST_HEAD(&cookie->backing_objects);
  35. }
  36. /*
  37. * request a cookie to represent an object (index, datafile, xattr, etc)
  38. * - parent specifies the parent object
  39. * - the top level index cookie for each netfs is stored in the fscache_netfs
  40. * struct upon registration
  41. * - def points to the definition
  42. * - the netfs_data will be passed to the functions pointed to in *def
  43. * - all attached caches will be searched to see if they contain this object
  44. * - index objects aren't stored on disk until there's a dependent file that
  45. * needs storing
  46. * - other objects are stored in a selected cache immediately, and all the
  47. * indices forming the path to it are instantiated if necessary
  48. * - we never let on to the netfs about errors
  49. * - we may set a negative cookie pointer, but that's okay
  50. */
  51. struct fscache_cookie *__fscache_acquire_cookie(
  52. struct fscache_cookie *parent,
  53. const struct fscache_cookie_def *def,
  54. void *netfs_data)
  55. {
  56. struct fscache_cookie *cookie;
  57. BUG_ON(!def);
  58. _enter("{%s},{%s},%p",
  59. parent ? (char *) parent->def->name : "<no-parent>",
  60. def->name, netfs_data);
  61. fscache_stat(&fscache_n_acquires);
  62. /* if there's no parent cookie, then we don't create one here either */
  63. if (!parent) {
  64. fscache_stat(&fscache_n_acquires_null);
  65. _leave(" [no parent]");
  66. return NULL;
  67. }
  68. /* validate the definition */
  69. BUG_ON(!def->get_key);
  70. BUG_ON(!def->name[0]);
  71. BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX &&
  72. parent->def->type != FSCACHE_COOKIE_TYPE_INDEX);
  73. /* allocate and initialise a cookie */
  74. cookie = kmem_cache_alloc(fscache_cookie_jar, GFP_KERNEL);
  75. if (!cookie) {
  76. fscache_stat(&fscache_n_acquires_oom);
  77. _leave(" [ENOMEM]");
  78. return NULL;
  79. }
  80. atomic_set(&cookie->usage, 1);
  81. atomic_set(&cookie->n_children, 0);
  82. /* We keep the active count elevated until relinquishment to prevent an
  83. * attempt to wake up every time the object operations queue quiesces.
  84. */
  85. atomic_set(&cookie->n_active, 1);
  86. atomic_inc(&parent->usage);
  87. atomic_inc(&parent->n_children);
  88. cookie->def = def;
  89. cookie->parent = parent;
  90. cookie->netfs_data = netfs_data;
  91. cookie->flags = 0;
  92. /* radix tree insertion won't use the preallocation pool unless it's
  93. * told it may not wait */
  94. INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_WAIT);
  95. switch (cookie->def->type) {
  96. case FSCACHE_COOKIE_TYPE_INDEX:
  97. fscache_stat(&fscache_n_cookie_index);
  98. break;
  99. case FSCACHE_COOKIE_TYPE_DATAFILE:
  100. fscache_stat(&fscache_n_cookie_data);
  101. break;
  102. default:
  103. fscache_stat(&fscache_n_cookie_special);
  104. break;
  105. }
  106. /* if the object is an index then we need do nothing more here - we
  107. * create indices on disk when we need them as an index may exist in
  108. * multiple caches */
  109. if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
  110. if (fscache_acquire_non_index_cookie(cookie) < 0) {
  111. atomic_dec(&parent->n_children);
  112. __fscache_cookie_put(cookie);
  113. fscache_stat(&fscache_n_acquires_nobufs);
  114. _leave(" = NULL");
  115. return NULL;
  116. }
  117. }
  118. fscache_stat(&fscache_n_acquires_ok);
  119. _leave(" = %p", cookie);
  120. return cookie;
  121. }
  122. EXPORT_SYMBOL(__fscache_acquire_cookie);
  123. /*
  124. * acquire a non-index cookie
  125. * - this must make sure the index chain is instantiated and instantiate the
  126. * object representation too
  127. */
  128. static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie)
  129. {
  130. struct fscache_object *object;
  131. struct fscache_cache *cache;
  132. uint64_t i_size;
  133. int ret;
  134. _enter("");
  135. cookie->flags = 1 << FSCACHE_COOKIE_UNAVAILABLE;
  136. /* now we need to see whether the backing objects for this cookie yet
  137. * exist, if not there'll be nothing to search */
  138. down_read(&fscache_addremove_sem);
  139. if (list_empty(&fscache_cache_list)) {
  140. up_read(&fscache_addremove_sem);
  141. _leave(" = 0 [no caches]");
  142. return 0;
  143. }
  144. /* select a cache in which to store the object */
  145. cache = fscache_select_cache_for_object(cookie->parent);
  146. if (!cache) {
  147. up_read(&fscache_addremove_sem);
  148. fscache_stat(&fscache_n_acquires_no_cache);
  149. _leave(" = -ENOMEDIUM [no cache]");
  150. return -ENOMEDIUM;
  151. }
  152. _debug("cache %s", cache->tag->name);
  153. cookie->flags =
  154. (1 << FSCACHE_COOKIE_LOOKING_UP) |
  155. (1 << FSCACHE_COOKIE_NO_DATA_YET);
  156. /* ask the cache to allocate objects for this cookie and its parent
  157. * chain */
  158. ret = fscache_alloc_object(cache, cookie);
  159. if (ret < 0) {
  160. up_read(&fscache_addremove_sem);
  161. _leave(" = %d", ret);
  162. return ret;
  163. }
  164. /* pass on how big the object we're caching is supposed to be */
  165. cookie->def->get_attr(cookie->netfs_data, &i_size);
  166. spin_lock(&cookie->lock);
  167. if (hlist_empty(&cookie->backing_objects)) {
  168. spin_unlock(&cookie->lock);
  169. goto unavailable;
  170. }
  171. object = hlist_entry(cookie->backing_objects.first,
  172. struct fscache_object, cookie_link);
  173. fscache_set_store_limit(object, i_size);
  174. /* initiate the process of looking up all the objects in the chain
  175. * (done by fscache_initialise_object()) */
  176. fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD);
  177. spin_unlock(&cookie->lock);
  178. /* we may be required to wait for lookup to complete at this point */
  179. if (!fscache_defer_lookup) {
  180. _debug("non-deferred lookup %p", &cookie->flags);
  181. wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
  182. fscache_wait_bit, TASK_UNINTERRUPTIBLE);
  183. _debug("complete");
  184. if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags))
  185. goto unavailable;
  186. }
  187. up_read(&fscache_addremove_sem);
  188. _leave(" = 0 [deferred]");
  189. return 0;
  190. unavailable:
  191. up_read(&fscache_addremove_sem);
  192. _leave(" = -ENOBUFS");
  193. return -ENOBUFS;
  194. }
  195. /*
  196. * recursively allocate cache object records for a cookie/cache combination
  197. * - caller must be holding the addremove sem
  198. */
  199. static int fscache_alloc_object(struct fscache_cache *cache,
  200. struct fscache_cookie *cookie)
  201. {
  202. struct fscache_object *object;
  203. int ret;
  204. _enter("%p,%p{%s}", cache, cookie, cookie->def->name);
  205. spin_lock(&cookie->lock);
  206. hlist_for_each_entry(object, &cookie->backing_objects,
  207. cookie_link) {
  208. if (object->cache == cache)
  209. goto object_already_extant;
  210. }
  211. spin_unlock(&cookie->lock);
  212. /* ask the cache to allocate an object (we may end up with duplicate
  213. * objects at this stage, but we sort that out later) */
  214. fscache_stat(&fscache_n_cop_alloc_object);
  215. object = cache->ops->alloc_object(cache, cookie);
  216. fscache_stat_d(&fscache_n_cop_alloc_object);
  217. if (IS_ERR(object)) {
  218. fscache_stat(&fscache_n_object_no_alloc);
  219. ret = PTR_ERR(object);
  220. goto error;
  221. }
  222. fscache_stat(&fscache_n_object_alloc);
  223. object->debug_id = atomic_inc_return(&fscache_object_debug_id);
  224. _debug("ALLOC OBJ%x: %s {%lx}",
  225. object->debug_id, cookie->def->name, object->events);
  226. ret = fscache_alloc_object(cache, cookie->parent);
  227. if (ret < 0)
  228. goto error_put;
  229. /* only attach if we managed to allocate all we needed, otherwise
  230. * discard the object we just allocated and instead use the one
  231. * attached to the cookie */
  232. if (fscache_attach_object(cookie, object) < 0) {
  233. fscache_stat(&fscache_n_cop_put_object);
  234. cache->ops->put_object(object);
  235. fscache_stat_d(&fscache_n_cop_put_object);
  236. }
  237. _leave(" = 0");
  238. return 0;
  239. object_already_extant:
  240. ret = -ENOBUFS;
  241. if (fscache_object_is_dead(object)) {
  242. spin_unlock(&cookie->lock);
  243. goto error;
  244. }
  245. spin_unlock(&cookie->lock);
  246. _leave(" = 0 [found]");
  247. return 0;
  248. error_put:
  249. fscache_stat(&fscache_n_cop_put_object);
  250. cache->ops->put_object(object);
  251. fscache_stat_d(&fscache_n_cop_put_object);
  252. error:
  253. _leave(" = %d", ret);
  254. return ret;
  255. }
  256. /*
  257. * attach a cache object to a cookie
  258. */
  259. static int fscache_attach_object(struct fscache_cookie *cookie,
  260. struct fscache_object *object)
  261. {
  262. struct fscache_object *p;
  263. struct fscache_cache *cache = object->cache;
  264. int ret;
  265. _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id);
  266. spin_lock(&cookie->lock);
  267. /* there may be multiple initial creations of this object, but we only
  268. * want one */
  269. ret = -EEXIST;
  270. hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) {
  271. if (p->cache == object->cache) {
  272. if (fscache_object_is_dying(p))
  273. ret = -ENOBUFS;
  274. goto cant_attach_object;
  275. }
  276. }
  277. /* pin the parent object */
  278. spin_lock_nested(&cookie->parent->lock, 1);
  279. hlist_for_each_entry(p, &cookie->parent->backing_objects,
  280. cookie_link) {
  281. if (p->cache == object->cache) {
  282. if (fscache_object_is_dying(p)) {
  283. ret = -ENOBUFS;
  284. spin_unlock(&cookie->parent->lock);
  285. goto cant_attach_object;
  286. }
  287. object->parent = p;
  288. spin_lock(&p->lock);
  289. p->n_children++;
  290. spin_unlock(&p->lock);
  291. break;
  292. }
  293. }
  294. spin_unlock(&cookie->parent->lock);
  295. /* attach to the cache's object list */
  296. if (list_empty(&object->cache_link)) {
  297. spin_lock(&cache->object_list_lock);
  298. list_add(&object->cache_link, &cache->object_list);
  299. spin_unlock(&cache->object_list_lock);
  300. }
  301. /* attach to the cookie */
  302. object->cookie = cookie;
  303. atomic_inc(&cookie->usage);
  304. hlist_add_head(&object->cookie_link, &cookie->backing_objects);
  305. fscache_objlist_add(object);
  306. ret = 0;
  307. cant_attach_object:
  308. spin_unlock(&cookie->lock);
  309. _leave(" = %d", ret);
  310. return ret;
  311. }
  312. /*
  313. * Invalidate an object. Callable with spinlocks held.
  314. */
  315. void __fscache_invalidate(struct fscache_cookie *cookie)
  316. {
  317. struct fscache_object *object;
  318. _enter("{%s}", cookie->def->name);
  319. fscache_stat(&fscache_n_invalidates);
  320. /* Only permit invalidation of data files. Invalidating an index will
  321. * require the caller to release all its attachments to the tree rooted
  322. * there, and if it's doing that, it may as well just retire the
  323. * cookie.
  324. */
  325. ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
  326. /* We will be updating the cookie too. */
  327. BUG_ON(!cookie->def->get_aux);
  328. /* If there's an object, we tell the object state machine to handle the
  329. * invalidation on our behalf, otherwise there's nothing to do.
  330. */
  331. if (!hlist_empty(&cookie->backing_objects)) {
  332. spin_lock(&cookie->lock);
  333. if (!hlist_empty(&cookie->backing_objects) &&
  334. !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
  335. &cookie->flags)) {
  336. object = hlist_entry(cookie->backing_objects.first,
  337. struct fscache_object,
  338. cookie_link);
  339. if (fscache_object_is_live(object))
  340. fscache_raise_event(
  341. object, FSCACHE_OBJECT_EV_INVALIDATE);
  342. }
  343. spin_unlock(&cookie->lock);
  344. }
  345. _leave("");
  346. }
  347. EXPORT_SYMBOL(__fscache_invalidate);
  348. /*
  349. * Wait for object invalidation to complete.
  350. */
  351. void __fscache_wait_on_invalidate(struct fscache_cookie *cookie)
  352. {
  353. _enter("%p", cookie);
  354. wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING,
  355. fscache_wait_bit_interruptible,
  356. TASK_UNINTERRUPTIBLE);
  357. _leave("");
  358. }
  359. EXPORT_SYMBOL(__fscache_wait_on_invalidate);
  360. /*
  361. * update the index entries backing a cookie
  362. */
  363. void __fscache_update_cookie(struct fscache_cookie *cookie)
  364. {
  365. struct fscache_object *object;
  366. fscache_stat(&fscache_n_updates);
  367. if (!cookie) {
  368. fscache_stat(&fscache_n_updates_null);
  369. _leave(" [no cookie]");
  370. return;
  371. }
  372. _enter("{%s}", cookie->def->name);
  373. BUG_ON(!cookie->def->get_aux);
  374. spin_lock(&cookie->lock);
  375. /* update the index entry on disk in each cache backing this cookie */
  376. hlist_for_each_entry(object,
  377. &cookie->backing_objects, cookie_link) {
  378. fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
  379. }
  380. spin_unlock(&cookie->lock);
  381. _leave("");
  382. }
  383. EXPORT_SYMBOL(__fscache_update_cookie);
  384. /*
  385. * release a cookie back to the cache
  386. * - the object will be marked as recyclable on disk if retire is true
  387. * - all dependents of this cookie must have already been unregistered
  388. * (indices/files/pages)
  389. */
  390. void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
  391. {
  392. struct fscache_object *object;
  393. fscache_stat(&fscache_n_relinquishes);
  394. if (retire)
  395. fscache_stat(&fscache_n_relinquishes_retire);
  396. if (!cookie) {
  397. fscache_stat(&fscache_n_relinquishes_null);
  398. _leave(" [no cookie]");
  399. return;
  400. }
  401. _enter("%p{%s,%p,%d},%d",
  402. cookie, cookie->def->name, cookie->netfs_data,
  403. atomic_read(&cookie->n_active), retire);
  404. ASSERTCMP(atomic_read(&cookie->n_active), >, 0);
  405. if (atomic_read(&cookie->n_children) != 0) {
  406. printk(KERN_ERR "FS-Cache: Cookie '%s' still has children\n",
  407. cookie->def->name);
  408. BUG();
  409. }
  410. /* No further netfs-accessing operations on this cookie permitted */
  411. set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags);
  412. if (retire)
  413. set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags);
  414. spin_lock(&cookie->lock);
  415. hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) {
  416. fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
  417. }
  418. spin_unlock(&cookie->lock);
  419. /* Wait for cessation of activity requiring access to the netfs (when
  420. * n_active reaches 0).
  421. */
  422. if (!atomic_dec_and_test(&cookie->n_active))
  423. wait_on_atomic_t(&cookie->n_active, fscache_wait_atomic_t,
  424. TASK_UNINTERRUPTIBLE);
  425. /* Clear pointers back to the netfs */
  426. cookie->netfs_data = NULL;
  427. cookie->def = NULL;
  428. BUG_ON(cookie->stores.rnode);
  429. if (cookie->parent) {
  430. ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
  431. ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0);
  432. atomic_dec(&cookie->parent->n_children);
  433. }
  434. /* Dispose of the netfs's link to the cookie */
  435. ASSERTCMP(atomic_read(&cookie->usage), >, 0);
  436. fscache_cookie_put(cookie);
  437. _leave("");
  438. }
  439. EXPORT_SYMBOL(__fscache_relinquish_cookie);
  440. /*
  441. * destroy a cookie
  442. */
  443. void __fscache_cookie_put(struct fscache_cookie *cookie)
  444. {
  445. struct fscache_cookie *parent;
  446. _enter("%p", cookie);
  447. for (;;) {
  448. _debug("FREE COOKIE %p", cookie);
  449. parent = cookie->parent;
  450. BUG_ON(!hlist_empty(&cookie->backing_objects));
  451. kmem_cache_free(fscache_cookie_jar, cookie);
  452. if (!parent)
  453. break;
  454. cookie = parent;
  455. BUG_ON(atomic_read(&cookie->usage) <= 0);
  456. if (!atomic_dec_and_test(&cookie->usage))
  457. break;
  458. }
  459. _leave("");
  460. }
  461. /*
  462. * check the consistency between the netfs inode and the backing cache
  463. *
  464. * NOTE: it only serves no-index type
  465. */
  466. int __fscache_check_consistency(struct fscache_cookie *cookie)
  467. {
  468. struct fscache_operation *op;
  469. struct fscache_object *object;
  470. int ret;
  471. _enter("%p,", cookie);
  472. ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
  473. if (fscache_wait_for_deferred_lookup(cookie) < 0)
  474. return -ERESTARTSYS;
  475. if (hlist_empty(&cookie->backing_objects))
  476. return 0;
  477. op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
  478. if (!op)
  479. return -ENOMEM;
  480. fscache_operation_init(op, NULL, NULL);
  481. op->flags = FSCACHE_OP_MYTHREAD |
  482. (1 << FSCACHE_OP_WAITING);
  483. spin_lock(&cookie->lock);
  484. if (hlist_empty(&cookie->backing_objects))
  485. goto inconsistent;
  486. object = hlist_entry(cookie->backing_objects.first,
  487. struct fscache_object, cookie_link);
  488. if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
  489. goto inconsistent;
  490. op->debug_id = atomic_inc_return(&fscache_op_debug_id);
  491. atomic_inc(&cookie->n_active);
  492. if (fscache_submit_op(object, op) < 0)
  493. goto submit_failed;
  494. /* the work queue now carries its own ref on the object */
  495. spin_unlock(&cookie->lock);
  496. ret = fscache_wait_for_operation_activation(object, op,
  497. NULL, NULL, NULL);
  498. if (ret == 0) {
  499. /* ask the cache to honour the operation */
  500. ret = object->cache->ops->check_consistency(op);
  501. fscache_op_complete(op, false);
  502. } else if (ret == -ENOBUFS) {
  503. ret = 0;
  504. }
  505. fscache_put_operation(op);
  506. _leave(" = %d", ret);
  507. return ret;
  508. submit_failed:
  509. atomic_dec(&cookie->n_active);
  510. inconsistent:
  511. spin_unlock(&cookie->lock);
  512. kfree(op);
  513. _leave(" = -ESTALE");
  514. return -ESTALE;
  515. }
  516. EXPORT_SYMBOL(__fscache_check_consistency);