cookie.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. INIT_HLIST_HEAD(&cookie->backing_objects);
  34. }
  35. /*
  36. * request a cookie to represent an object (index, datafile, xattr, etc)
  37. * - parent specifies the parent object
  38. * - the top level index cookie for each netfs is stored in the fscache_netfs
  39. * struct upon registration
  40. * - def points to the definition
  41. * - the netfs_data will be passed to the functions pointed to in *def
  42. * - all attached caches will be searched to see if they contain this object
  43. * - index objects aren't stored on disk until there's a dependent file that
  44. * needs storing
  45. * - other objects are stored in a selected cache immediately, and all the
  46. * indices forming the path to it are instantiated if necessary
  47. * - we never let on to the netfs about errors
  48. * - we may set a negative cookie pointer, but that's okay
  49. */
  50. struct fscache_cookie *__fscache_acquire_cookie(
  51. struct fscache_cookie *parent,
  52. const struct fscache_cookie_def *def,
  53. void *netfs_data)
  54. {
  55. struct fscache_cookie *cookie;
  56. BUG_ON(!def);
  57. _enter("{%s},{%s},%p",
  58. parent ? (char *) parent->def->name : "<no-parent>",
  59. def->name, netfs_data);
  60. fscache_stat(&fscache_n_acquires);
  61. /* if there's no parent cookie, then we don't create one here either */
  62. if (!parent) {
  63. fscache_stat(&fscache_n_acquires_null);
  64. _leave(" [no parent]");
  65. return NULL;
  66. }
  67. /* validate the definition */
  68. BUG_ON(!def->get_key);
  69. BUG_ON(!def->name[0]);
  70. BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX &&
  71. parent->def->type != FSCACHE_COOKIE_TYPE_INDEX);
  72. /* allocate and initialise a cookie */
  73. cookie = kmem_cache_alloc(fscache_cookie_jar, GFP_KERNEL);
  74. if (!cookie) {
  75. fscache_stat(&fscache_n_acquires_oom);
  76. _leave(" [ENOMEM]");
  77. return NULL;
  78. }
  79. atomic_set(&cookie->usage, 1);
  80. atomic_set(&cookie->n_children, 0);
  81. atomic_inc(&parent->usage);
  82. atomic_inc(&parent->n_children);
  83. cookie->def = def;
  84. cookie->parent = parent;
  85. cookie->netfs_data = netfs_data;
  86. cookie->flags = 0;
  87. INIT_RADIX_TREE(&cookie->stores, GFP_NOFS);
  88. switch (cookie->def->type) {
  89. case FSCACHE_COOKIE_TYPE_INDEX:
  90. fscache_stat(&fscache_n_cookie_index);
  91. break;
  92. case FSCACHE_COOKIE_TYPE_DATAFILE:
  93. fscache_stat(&fscache_n_cookie_data);
  94. break;
  95. default:
  96. fscache_stat(&fscache_n_cookie_special);
  97. break;
  98. }
  99. /* if the object is an index then we need do nothing more here - we
  100. * create indices on disk when we need them as an index may exist in
  101. * multiple caches */
  102. if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
  103. if (fscache_acquire_non_index_cookie(cookie) < 0) {
  104. atomic_dec(&parent->n_children);
  105. __fscache_cookie_put(cookie);
  106. fscache_stat(&fscache_n_acquires_nobufs);
  107. _leave(" = NULL");
  108. return NULL;
  109. }
  110. }
  111. fscache_stat(&fscache_n_acquires_ok);
  112. _leave(" = %p", cookie);
  113. return cookie;
  114. }
  115. EXPORT_SYMBOL(__fscache_acquire_cookie);
  116. /*
  117. * acquire a non-index cookie
  118. * - this must make sure the index chain is instantiated and instantiate the
  119. * object representation too
  120. */
  121. static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie)
  122. {
  123. struct fscache_object *object;
  124. struct fscache_cache *cache;
  125. uint64_t i_size;
  126. int ret;
  127. _enter("");
  128. cookie->flags = 1 << FSCACHE_COOKIE_UNAVAILABLE;
  129. /* now we need to see whether the backing objects for this cookie yet
  130. * exist, if not there'll be nothing to search */
  131. down_read(&fscache_addremove_sem);
  132. if (list_empty(&fscache_cache_list)) {
  133. up_read(&fscache_addremove_sem);
  134. _leave(" = 0 [no caches]");
  135. return 0;
  136. }
  137. /* select a cache in which to store the object */
  138. cache = fscache_select_cache_for_object(cookie->parent);
  139. if (!cache) {
  140. up_read(&fscache_addremove_sem);
  141. fscache_stat(&fscache_n_acquires_no_cache);
  142. _leave(" = -ENOMEDIUM [no cache]");
  143. return -ENOMEDIUM;
  144. }
  145. _debug("cache %s", cache->tag->name);
  146. cookie->flags =
  147. (1 << FSCACHE_COOKIE_LOOKING_UP) |
  148. (1 << FSCACHE_COOKIE_CREATING) |
  149. (1 << FSCACHE_COOKIE_NO_DATA_YET);
  150. /* ask the cache to allocate objects for this cookie and its parent
  151. * chain */
  152. ret = fscache_alloc_object(cache, cookie);
  153. if (ret < 0) {
  154. up_read(&fscache_addremove_sem);
  155. _leave(" = %d", ret);
  156. return ret;
  157. }
  158. /* pass on how big the object we're caching is supposed to be */
  159. cookie->def->get_attr(cookie->netfs_data, &i_size);
  160. spin_lock(&cookie->lock);
  161. if (hlist_empty(&cookie->backing_objects)) {
  162. spin_unlock(&cookie->lock);
  163. goto unavailable;
  164. }
  165. object = hlist_entry(cookie->backing_objects.first,
  166. struct fscache_object, cookie_link);
  167. fscache_set_store_limit(object, i_size);
  168. /* initiate the process of looking up all the objects in the chain
  169. * (done by fscache_initialise_object()) */
  170. fscache_enqueue_object(object);
  171. spin_unlock(&cookie->lock);
  172. /* we may be required to wait for lookup to complete at this point */
  173. if (!fscache_defer_lookup) {
  174. _debug("non-deferred lookup %p", &cookie->flags);
  175. wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
  176. fscache_wait_bit, TASK_UNINTERRUPTIBLE);
  177. _debug("complete");
  178. if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags))
  179. goto unavailable;
  180. }
  181. up_read(&fscache_addremove_sem);
  182. _leave(" = 0 [deferred]");
  183. return 0;
  184. unavailable:
  185. up_read(&fscache_addremove_sem);
  186. _leave(" = -ENOBUFS");
  187. return -ENOBUFS;
  188. }
  189. /*
  190. * recursively allocate cache object records for a cookie/cache combination
  191. * - caller must be holding the addremove sem
  192. */
  193. static int fscache_alloc_object(struct fscache_cache *cache,
  194. struct fscache_cookie *cookie)
  195. {
  196. struct fscache_object *object;
  197. struct hlist_node *_n;
  198. int ret;
  199. _enter("%p,%p{%s}", cache, cookie, cookie->def->name);
  200. spin_lock(&cookie->lock);
  201. hlist_for_each_entry(object, _n, &cookie->backing_objects,
  202. cookie_link) {
  203. if (object->cache == cache)
  204. goto object_already_extant;
  205. }
  206. spin_unlock(&cookie->lock);
  207. /* ask the cache to allocate an object (we may end up with duplicate
  208. * objects at this stage, but we sort that out later) */
  209. object = cache->ops->alloc_object(cache, cookie);
  210. if (IS_ERR(object)) {
  211. fscache_stat(&fscache_n_object_no_alloc);
  212. ret = PTR_ERR(object);
  213. goto error;
  214. }
  215. fscache_stat(&fscache_n_object_alloc);
  216. object->debug_id = atomic_inc_return(&fscache_object_debug_id);
  217. _debug("ALLOC OBJ%x: %s {%lx}",
  218. object->debug_id, cookie->def->name, object->events);
  219. ret = fscache_alloc_object(cache, cookie->parent);
  220. if (ret < 0)
  221. goto error_put;
  222. /* only attach if we managed to allocate all we needed, otherwise
  223. * discard the object we just allocated and instead use the one
  224. * attached to the cookie */
  225. if (fscache_attach_object(cookie, object) < 0)
  226. cache->ops->put_object(object);
  227. _leave(" = 0");
  228. return 0;
  229. object_already_extant:
  230. ret = -ENOBUFS;
  231. if (object->state >= FSCACHE_OBJECT_DYING) {
  232. spin_unlock(&cookie->lock);
  233. goto error;
  234. }
  235. spin_unlock(&cookie->lock);
  236. _leave(" = 0 [found]");
  237. return 0;
  238. error_put:
  239. cache->ops->put_object(object);
  240. error:
  241. _leave(" = %d", ret);
  242. return ret;
  243. }
  244. /*
  245. * attach a cache object to a cookie
  246. */
  247. static int fscache_attach_object(struct fscache_cookie *cookie,
  248. struct fscache_object *object)
  249. {
  250. struct fscache_object *p;
  251. struct fscache_cache *cache = object->cache;
  252. struct hlist_node *_n;
  253. int ret;
  254. _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id);
  255. spin_lock(&cookie->lock);
  256. /* there may be multiple initial creations of this object, but we only
  257. * want one */
  258. ret = -EEXIST;
  259. hlist_for_each_entry(p, _n, &cookie->backing_objects, cookie_link) {
  260. if (p->cache == object->cache) {
  261. if (p->state >= FSCACHE_OBJECT_DYING)
  262. ret = -ENOBUFS;
  263. goto cant_attach_object;
  264. }
  265. }
  266. /* pin the parent object */
  267. spin_lock_nested(&cookie->parent->lock, 1);
  268. hlist_for_each_entry(p, _n, &cookie->parent->backing_objects,
  269. cookie_link) {
  270. if (p->cache == object->cache) {
  271. if (p->state >= FSCACHE_OBJECT_DYING) {
  272. ret = -ENOBUFS;
  273. spin_unlock(&cookie->parent->lock);
  274. goto cant_attach_object;
  275. }
  276. object->parent = p;
  277. spin_lock(&p->lock);
  278. p->n_children++;
  279. spin_unlock(&p->lock);
  280. break;
  281. }
  282. }
  283. spin_unlock(&cookie->parent->lock);
  284. /* attach to the cache's object list */
  285. if (list_empty(&object->cache_link)) {
  286. spin_lock(&cache->object_list_lock);
  287. list_add(&object->cache_link, &cache->object_list);
  288. spin_unlock(&cache->object_list_lock);
  289. }
  290. /* attach to the cookie */
  291. object->cookie = cookie;
  292. atomic_inc(&cookie->usage);
  293. hlist_add_head(&object->cookie_link, &cookie->backing_objects);
  294. ret = 0;
  295. cant_attach_object:
  296. spin_unlock(&cookie->lock);
  297. _leave(" = %d", ret);
  298. return ret;
  299. }
  300. /*
  301. * update the index entries backing a cookie
  302. */
  303. void __fscache_update_cookie(struct fscache_cookie *cookie)
  304. {
  305. struct fscache_object *object;
  306. struct hlist_node *_p;
  307. fscache_stat(&fscache_n_updates);
  308. if (!cookie) {
  309. fscache_stat(&fscache_n_updates_null);
  310. _leave(" [no cookie]");
  311. return;
  312. }
  313. _enter("{%s}", cookie->def->name);
  314. BUG_ON(!cookie->def->get_aux);
  315. spin_lock(&cookie->lock);
  316. /* update the index entry on disk in each cache backing this cookie */
  317. hlist_for_each_entry(object, _p,
  318. &cookie->backing_objects, cookie_link) {
  319. fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
  320. }
  321. spin_unlock(&cookie->lock);
  322. _leave("");
  323. }
  324. EXPORT_SYMBOL(__fscache_update_cookie);
  325. /*
  326. * release a cookie back to the cache
  327. * - the object will be marked as recyclable on disk if retire is true
  328. * - all dependents of this cookie must have already been unregistered
  329. * (indices/files/pages)
  330. */
  331. void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
  332. {
  333. struct fscache_cache *cache;
  334. struct fscache_object *object;
  335. unsigned long event;
  336. fscache_stat(&fscache_n_relinquishes);
  337. if (!cookie) {
  338. fscache_stat(&fscache_n_relinquishes_null);
  339. _leave(" [no cookie]");
  340. return;
  341. }
  342. _enter("%p{%s,%p},%d",
  343. cookie, cookie->def->name, cookie->netfs_data, retire);
  344. if (atomic_read(&cookie->n_children) != 0) {
  345. printk(KERN_ERR "FS-Cache: Cookie '%s' still has children\n",
  346. cookie->def->name);
  347. BUG();
  348. }
  349. /* wait for the cookie to finish being instantiated (or to fail) */
  350. if (test_bit(FSCACHE_COOKIE_CREATING, &cookie->flags)) {
  351. fscache_stat(&fscache_n_relinquishes_waitcrt);
  352. wait_on_bit(&cookie->flags, FSCACHE_COOKIE_CREATING,
  353. fscache_wait_bit, TASK_UNINTERRUPTIBLE);
  354. }
  355. event = retire ? FSCACHE_OBJECT_EV_RETIRE : FSCACHE_OBJECT_EV_RELEASE;
  356. /* detach pointers back to the netfs */
  357. spin_lock(&cookie->lock);
  358. cookie->netfs_data = NULL;
  359. cookie->def = NULL;
  360. /* break links with all the active objects */
  361. while (!hlist_empty(&cookie->backing_objects)) {
  362. object = hlist_entry(cookie->backing_objects.first,
  363. struct fscache_object,
  364. cookie_link);
  365. _debug("RELEASE OBJ%x", object->debug_id);
  366. /* detach each cache object from the object cookie */
  367. spin_lock(&object->lock);
  368. hlist_del_init(&object->cookie_link);
  369. cache = object->cache;
  370. object->cookie = NULL;
  371. fscache_raise_event(object, event);
  372. spin_unlock(&object->lock);
  373. if (atomic_dec_and_test(&cookie->usage))
  374. /* the cookie refcount shouldn't be reduced to 0 yet */
  375. BUG();
  376. }
  377. spin_unlock(&cookie->lock);
  378. if (cookie->parent) {
  379. ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
  380. ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0);
  381. atomic_dec(&cookie->parent->n_children);
  382. }
  383. /* finally dispose of the cookie */
  384. ASSERTCMP(atomic_read(&cookie->usage), >, 0);
  385. fscache_cookie_put(cookie);
  386. _leave("");
  387. }
  388. EXPORT_SYMBOL(__fscache_relinquish_cookie);
  389. /*
  390. * destroy a cookie
  391. */
  392. void __fscache_cookie_put(struct fscache_cookie *cookie)
  393. {
  394. struct fscache_cookie *parent;
  395. _enter("%p", cookie);
  396. for (;;) {
  397. _debug("FREE COOKIE %p", cookie);
  398. parent = cookie->parent;
  399. BUG_ON(!hlist_empty(&cookie->backing_objects));
  400. kmem_cache_free(fscache_cookie_jar, cookie);
  401. if (!parent)
  402. break;
  403. cookie = parent;
  404. BUG_ON(atomic_read(&cookie->usage) <= 0);
  405. if (!atomic_dec_and_test(&cookie->usage))
  406. break;
  407. }
  408. _leave("");
  409. }