cookie.c 13 KB

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