cookie.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. int ret;
  201. _enter("%p,%p{%s}", cache, cookie, cookie->def->name);
  202. spin_lock(&cookie->lock);
  203. hlist_for_each_entry(object, &cookie->backing_objects,
  204. cookie_link) {
  205. if (object->cache == cache)
  206. goto object_already_extant;
  207. }
  208. spin_unlock(&cookie->lock);
  209. /* ask the cache to allocate an object (we may end up with duplicate
  210. * objects at this stage, but we sort that out later) */
  211. fscache_stat(&fscache_n_cop_alloc_object);
  212. object = cache->ops->alloc_object(cache, cookie);
  213. fscache_stat_d(&fscache_n_cop_alloc_object);
  214. if (IS_ERR(object)) {
  215. fscache_stat(&fscache_n_object_no_alloc);
  216. ret = PTR_ERR(object);
  217. goto error;
  218. }
  219. fscache_stat(&fscache_n_object_alloc);
  220. object->debug_id = atomic_inc_return(&fscache_object_debug_id);
  221. _debug("ALLOC OBJ%x: %s {%lx}",
  222. object->debug_id, cookie->def->name, object->events);
  223. ret = fscache_alloc_object(cache, cookie->parent);
  224. if (ret < 0)
  225. goto error_put;
  226. /* only attach if we managed to allocate all we needed, otherwise
  227. * discard the object we just allocated and instead use the one
  228. * attached to the cookie */
  229. if (fscache_attach_object(cookie, object) < 0) {
  230. fscache_stat(&fscache_n_cop_put_object);
  231. cache->ops->put_object(object);
  232. fscache_stat_d(&fscache_n_cop_put_object);
  233. }
  234. _leave(" = 0");
  235. return 0;
  236. object_already_extant:
  237. ret = -ENOBUFS;
  238. if (object->state >= FSCACHE_OBJECT_DYING) {
  239. spin_unlock(&cookie->lock);
  240. goto error;
  241. }
  242. spin_unlock(&cookie->lock);
  243. _leave(" = 0 [found]");
  244. return 0;
  245. error_put:
  246. fscache_stat(&fscache_n_cop_put_object);
  247. cache->ops->put_object(object);
  248. fscache_stat_d(&fscache_n_cop_put_object);
  249. error:
  250. _leave(" = %d", ret);
  251. return ret;
  252. }
  253. /*
  254. * attach a cache object to a cookie
  255. */
  256. static int fscache_attach_object(struct fscache_cookie *cookie,
  257. struct fscache_object *object)
  258. {
  259. struct fscache_object *p;
  260. struct fscache_cache *cache = object->cache;
  261. int ret;
  262. _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id);
  263. spin_lock(&cookie->lock);
  264. /* there may be multiple initial creations of this object, but we only
  265. * want one */
  266. ret = -EEXIST;
  267. hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) {
  268. if (p->cache == object->cache) {
  269. if (p->state >= FSCACHE_OBJECT_DYING)
  270. ret = -ENOBUFS;
  271. goto cant_attach_object;
  272. }
  273. }
  274. /* pin the parent object */
  275. spin_lock_nested(&cookie->parent->lock, 1);
  276. hlist_for_each_entry(p, &cookie->parent->backing_objects,
  277. cookie_link) {
  278. if (p->cache == object->cache) {
  279. if (p->state >= FSCACHE_OBJECT_DYING) {
  280. ret = -ENOBUFS;
  281. spin_unlock(&cookie->parent->lock);
  282. goto cant_attach_object;
  283. }
  284. object->parent = p;
  285. spin_lock(&p->lock);
  286. p->n_children++;
  287. spin_unlock(&p->lock);
  288. break;
  289. }
  290. }
  291. spin_unlock(&cookie->parent->lock);
  292. /* attach to the cache's object list */
  293. if (list_empty(&object->cache_link)) {
  294. spin_lock(&cache->object_list_lock);
  295. list_add(&object->cache_link, &cache->object_list);
  296. spin_unlock(&cache->object_list_lock);
  297. }
  298. /* attach to the cookie */
  299. object->cookie = cookie;
  300. atomic_inc(&cookie->usage);
  301. hlist_add_head(&object->cookie_link, &cookie->backing_objects);
  302. fscache_objlist_add(object);
  303. ret = 0;
  304. cant_attach_object:
  305. spin_unlock(&cookie->lock);
  306. _leave(" = %d", ret);
  307. return ret;
  308. }
  309. /*
  310. * Invalidate an object. Callable with spinlocks held.
  311. */
  312. void __fscache_invalidate(struct fscache_cookie *cookie)
  313. {
  314. struct fscache_object *object;
  315. _enter("{%s}", cookie->def->name);
  316. fscache_stat(&fscache_n_invalidates);
  317. /* Only permit invalidation of data files. Invalidating an index will
  318. * require the caller to release all its attachments to the tree rooted
  319. * there, and if it's doing that, it may as well just retire the
  320. * cookie.
  321. */
  322. ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
  323. /* We will be updating the cookie too. */
  324. BUG_ON(!cookie->def->get_aux);
  325. /* If there's an object, we tell the object state machine to handle the
  326. * invalidation on our behalf, otherwise there's nothing to do.
  327. */
  328. if (!hlist_empty(&cookie->backing_objects)) {
  329. spin_lock(&cookie->lock);
  330. if (!hlist_empty(&cookie->backing_objects) &&
  331. !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
  332. &cookie->flags)) {
  333. object = hlist_entry(cookie->backing_objects.first,
  334. struct fscache_object,
  335. cookie_link);
  336. if (object->state < FSCACHE_OBJECT_DYING)
  337. fscache_raise_event(
  338. object, FSCACHE_OBJECT_EV_INVALIDATE);
  339. }
  340. spin_unlock(&cookie->lock);
  341. }
  342. _leave("");
  343. }
  344. EXPORT_SYMBOL(__fscache_invalidate);
  345. /*
  346. * Wait for object invalidation to complete.
  347. */
  348. void __fscache_wait_on_invalidate(struct fscache_cookie *cookie)
  349. {
  350. _enter("%p", cookie);
  351. wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING,
  352. fscache_wait_bit_interruptible,
  353. TASK_UNINTERRUPTIBLE);
  354. _leave("");
  355. }
  356. EXPORT_SYMBOL(__fscache_wait_on_invalidate);
  357. /*
  358. * update the index entries backing a cookie
  359. */
  360. void __fscache_update_cookie(struct fscache_cookie *cookie)
  361. {
  362. struct fscache_object *object;
  363. fscache_stat(&fscache_n_updates);
  364. if (!cookie) {
  365. fscache_stat(&fscache_n_updates_null);
  366. _leave(" [no cookie]");
  367. return;
  368. }
  369. _enter("{%s}", cookie->def->name);
  370. BUG_ON(!cookie->def->get_aux);
  371. spin_lock(&cookie->lock);
  372. /* update the index entry on disk in each cache backing this cookie */
  373. hlist_for_each_entry(object,
  374. &cookie->backing_objects, cookie_link) {
  375. fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
  376. }
  377. spin_unlock(&cookie->lock);
  378. _leave("");
  379. }
  380. EXPORT_SYMBOL(__fscache_update_cookie);
  381. /*
  382. * release a cookie back to the cache
  383. * - the object will be marked as recyclable on disk if retire is true
  384. * - all dependents of this cookie must have already been unregistered
  385. * (indices/files/pages)
  386. */
  387. void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
  388. {
  389. struct fscache_cache *cache;
  390. struct fscache_object *object;
  391. unsigned long event;
  392. fscache_stat(&fscache_n_relinquishes);
  393. if (retire)
  394. fscache_stat(&fscache_n_relinquishes_retire);
  395. if (!cookie) {
  396. fscache_stat(&fscache_n_relinquishes_null);
  397. _leave(" [no cookie]");
  398. return;
  399. }
  400. _enter("%p{%s,%p},%d",
  401. cookie, cookie->def->name, cookie->netfs_data, retire);
  402. if (atomic_read(&cookie->n_children) != 0) {
  403. printk(KERN_ERR "FS-Cache: Cookie '%s' still has children\n",
  404. cookie->def->name);
  405. BUG();
  406. }
  407. /* wait for the cookie to finish being instantiated (or to fail) */
  408. if (test_bit(FSCACHE_COOKIE_CREATING, &cookie->flags)) {
  409. fscache_stat(&fscache_n_relinquishes_waitcrt);
  410. wait_on_bit(&cookie->flags, FSCACHE_COOKIE_CREATING,
  411. fscache_wait_bit, TASK_UNINTERRUPTIBLE);
  412. }
  413. event = retire ? FSCACHE_OBJECT_EV_RETIRE : FSCACHE_OBJECT_EV_RELEASE;
  414. try_again:
  415. spin_lock(&cookie->lock);
  416. /* break links with all the active objects */
  417. while (!hlist_empty(&cookie->backing_objects)) {
  418. int n_reads;
  419. object = hlist_entry(cookie->backing_objects.first,
  420. struct fscache_object,
  421. cookie_link);
  422. _debug("RELEASE OBJ%x", object->debug_id);
  423. set_bit(FSCACHE_COOKIE_WAITING_ON_READS, &cookie->flags);
  424. n_reads = atomic_read(&object->n_reads);
  425. if (n_reads) {
  426. int n_ops = object->n_ops;
  427. int n_in_progress = object->n_in_progress;
  428. spin_unlock(&cookie->lock);
  429. printk(KERN_ERR "FS-Cache:"
  430. " Cookie '%s' still has %d outstanding reads (%d,%d)\n",
  431. cookie->def->name,
  432. n_reads, n_ops, n_in_progress);
  433. wait_on_bit(&cookie->flags, FSCACHE_COOKIE_WAITING_ON_READS,
  434. fscache_wait_bit, TASK_UNINTERRUPTIBLE);
  435. printk("Wait finished\n");
  436. goto try_again;
  437. }
  438. /* detach each cache object from the object cookie */
  439. spin_lock(&object->lock);
  440. hlist_del_init(&object->cookie_link);
  441. cache = object->cache;
  442. object->cookie = NULL;
  443. fscache_raise_event(object, event);
  444. spin_unlock(&object->lock);
  445. if (atomic_dec_and_test(&cookie->usage))
  446. /* the cookie refcount shouldn't be reduced to 0 yet */
  447. BUG();
  448. }
  449. /* detach pointers back to the netfs */
  450. cookie->netfs_data = NULL;
  451. cookie->def = NULL;
  452. spin_unlock(&cookie->lock);
  453. if (cookie->parent) {
  454. ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
  455. ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0);
  456. atomic_dec(&cookie->parent->n_children);
  457. }
  458. /* finally dispose of the cookie */
  459. ASSERTCMP(atomic_read(&cookie->usage), >, 0);
  460. fscache_cookie_put(cookie);
  461. _leave("");
  462. }
  463. EXPORT_SYMBOL(__fscache_relinquish_cookie);
  464. /*
  465. * destroy a cookie
  466. */
  467. void __fscache_cookie_put(struct fscache_cookie *cookie)
  468. {
  469. struct fscache_cookie *parent;
  470. _enter("%p", cookie);
  471. for (;;) {
  472. _debug("FREE COOKIE %p", cookie);
  473. parent = cookie->parent;
  474. BUG_ON(!hlist_empty(&cookie->backing_objects));
  475. kmem_cache_free(fscache_cookie_jar, cookie);
  476. if (!parent)
  477. break;
  478. cookie = parent;
  479. BUG_ON(atomic_read(&cookie->usage) <= 0);
  480. if (!atomic_dec_and_test(&cookie->usage))
  481. break;
  482. }
  483. _leave("");
  484. }