cookie.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. #define fscache_cookie_hash_shift 15
  21. static struct hlist_bl_head fscache_cookie_hash[1 << fscache_cookie_hash_shift];
  22. static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie,
  23. loff_t object_size);
  24. static int fscache_alloc_object(struct fscache_cache *cache,
  25. struct fscache_cookie *cookie);
  26. static int fscache_attach_object(struct fscache_cookie *cookie,
  27. struct fscache_object *object);
  28. static void fscache_print_cookie(struct fscache_cookie *cookie, char prefix)
  29. {
  30. struct hlist_node *object;
  31. const u8 *k;
  32. unsigned loop;
  33. pr_err("%c-cookie c=%p [p=%p fl=%lx nc=%u na=%u]\n",
  34. prefix, cookie, cookie->parent, cookie->flags,
  35. atomic_read(&cookie->n_children),
  36. atomic_read(&cookie->n_active));
  37. pr_err("%c-cookie d=%p n=%p\n",
  38. prefix, cookie->def, cookie->netfs_data);
  39. object = READ_ONCE(cookie->backing_objects.first);
  40. if (object)
  41. pr_err("%c-cookie o=%p\n",
  42. prefix, hlist_entry(object, struct fscache_object, cookie_link));
  43. pr_err("%c-key=[%u] '", prefix, cookie->key_len);
  44. k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
  45. cookie->inline_key : cookie->key;
  46. for (loop = 0; loop < cookie->key_len; loop++)
  47. pr_cont("%02x", k[loop]);
  48. pr_cont("'\n");
  49. }
  50. void fscache_free_cookie(struct fscache_cookie *cookie)
  51. {
  52. if (cookie) {
  53. BUG_ON(!hlist_empty(&cookie->backing_objects));
  54. if (cookie->aux_len > sizeof(cookie->inline_aux))
  55. kfree(cookie->aux);
  56. if (cookie->key_len > sizeof(cookie->inline_key))
  57. kfree(cookie->key);
  58. kmem_cache_free(fscache_cookie_jar, cookie);
  59. }
  60. }
  61. /*
  62. * Set the index key in a cookie. The cookie struct has space for a 16-byte
  63. * key plus length and hash, but if that's not big enough, it's instead a
  64. * pointer to a buffer containing 3 bytes of hash, 1 byte of length and then
  65. * the key data.
  66. */
  67. static int fscache_set_key(struct fscache_cookie *cookie,
  68. const void *index_key, size_t index_key_len)
  69. {
  70. unsigned long long h;
  71. u32 *buf;
  72. int bufs;
  73. int i;
  74. bufs = DIV_ROUND_UP(index_key_len, sizeof(*buf));
  75. if (index_key_len > sizeof(cookie->inline_key)) {
  76. buf = kcalloc(bufs, sizeof(*buf), GFP_KERNEL);
  77. if (!buf)
  78. return -ENOMEM;
  79. cookie->key = buf;
  80. } else {
  81. buf = (u32 *)cookie->inline_key;
  82. }
  83. memcpy(buf, index_key, index_key_len);
  84. /* Calculate a hash and combine this with the length in the first word
  85. * or first half word
  86. */
  87. h = (unsigned long)cookie->parent;
  88. h += index_key_len + cookie->type;
  89. for (i = 0; i < bufs; i++)
  90. h += buf[i];
  91. cookie->key_hash = h ^ (h >> 32);
  92. return 0;
  93. }
  94. static long fscache_compare_cookie(const struct fscache_cookie *a,
  95. const struct fscache_cookie *b)
  96. {
  97. const void *ka, *kb;
  98. if (a->key_hash != b->key_hash)
  99. return (long)a->key_hash - (long)b->key_hash;
  100. if (a->parent != b->parent)
  101. return (long)a->parent - (long)b->parent;
  102. if (a->key_len != b->key_len)
  103. return (long)a->key_len - (long)b->key_len;
  104. if (a->type != b->type)
  105. return (long)a->type - (long)b->type;
  106. if (a->key_len <= sizeof(a->inline_key)) {
  107. ka = &a->inline_key;
  108. kb = &b->inline_key;
  109. } else {
  110. ka = a->key;
  111. kb = b->key;
  112. }
  113. return memcmp(ka, kb, a->key_len);
  114. }
  115. /*
  116. * Allocate a cookie.
  117. */
  118. struct fscache_cookie *fscache_alloc_cookie(
  119. struct fscache_cookie *parent,
  120. const struct fscache_cookie_def *def,
  121. const void *index_key, size_t index_key_len,
  122. const void *aux_data, size_t aux_data_len,
  123. void *netfs_data,
  124. loff_t object_size)
  125. {
  126. struct fscache_cookie *cookie;
  127. /* allocate and initialise a cookie */
  128. cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
  129. if (!cookie)
  130. return NULL;
  131. cookie->key_len = index_key_len;
  132. cookie->aux_len = aux_data_len;
  133. if (fscache_set_key(cookie, index_key, index_key_len) < 0)
  134. goto nomem;
  135. if (cookie->aux_len <= sizeof(cookie->inline_aux)) {
  136. memcpy(cookie->inline_aux, aux_data, cookie->aux_len);
  137. } else {
  138. cookie->aux = kmemdup(aux_data, cookie->aux_len, GFP_KERNEL);
  139. if (!cookie->aux)
  140. goto nomem;
  141. }
  142. atomic_set(&cookie->usage, 1);
  143. atomic_set(&cookie->n_children, 0);
  144. /* We keep the active count elevated until relinquishment to prevent an
  145. * attempt to wake up every time the object operations queue quiesces.
  146. */
  147. atomic_set(&cookie->n_active, 1);
  148. cookie->def = def;
  149. cookie->parent = parent;
  150. cookie->netfs_data = netfs_data;
  151. cookie->flags = (1 << FSCACHE_COOKIE_NO_DATA_YET);
  152. cookie->type = def->type;
  153. spin_lock_init(&cookie->lock);
  154. spin_lock_init(&cookie->stores_lock);
  155. INIT_HLIST_HEAD(&cookie->backing_objects);
  156. /* radix tree insertion won't use the preallocation pool unless it's
  157. * told it may not wait */
  158. INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
  159. return cookie;
  160. nomem:
  161. fscache_free_cookie(cookie);
  162. return NULL;
  163. }
  164. /*
  165. * Attempt to insert the new cookie into the hash. If there's a collision, we
  166. * return the old cookie if it's not in use and an error otherwise.
  167. */
  168. struct fscache_cookie *fscache_hash_cookie(struct fscache_cookie *candidate)
  169. {
  170. struct fscache_cookie *cursor;
  171. struct hlist_bl_head *h;
  172. struct hlist_bl_node *p;
  173. unsigned int bucket;
  174. bucket = candidate->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
  175. h = &fscache_cookie_hash[bucket];
  176. hlist_bl_lock(h);
  177. hlist_bl_for_each_entry(cursor, p, h, hash_link) {
  178. if (fscache_compare_cookie(candidate, cursor) == 0)
  179. goto collision;
  180. }
  181. __set_bit(FSCACHE_COOKIE_ACQUIRED, &candidate->flags);
  182. fscache_cookie_get(candidate->parent, fscache_cookie_get_acquire_parent);
  183. atomic_inc(&candidate->parent->n_children);
  184. hlist_bl_add_head(&candidate->hash_link, h);
  185. hlist_bl_unlock(h);
  186. return candidate;
  187. collision:
  188. if (test_and_set_bit(FSCACHE_COOKIE_ACQUIRED, &cursor->flags)) {
  189. trace_fscache_cookie(cursor, fscache_cookie_collision,
  190. atomic_read(&cursor->usage));
  191. pr_err("Duplicate cookie detected\n");
  192. fscache_print_cookie(cursor, 'O');
  193. fscache_print_cookie(candidate, 'N');
  194. hlist_bl_unlock(h);
  195. return NULL;
  196. }
  197. fscache_cookie_get(cursor, fscache_cookie_get_reacquire);
  198. hlist_bl_unlock(h);
  199. return cursor;
  200. }
  201. /*
  202. * request a cookie to represent an object (index, datafile, xattr, etc)
  203. * - parent specifies the parent object
  204. * - the top level index cookie for each netfs is stored in the fscache_netfs
  205. * struct upon registration
  206. * - def points to the definition
  207. * - the netfs_data will be passed to the functions pointed to in *def
  208. * - all attached caches will be searched to see if they contain this object
  209. * - index objects aren't stored on disk until there's a dependent file that
  210. * needs storing
  211. * - other objects are stored in a selected cache immediately, and all the
  212. * indices forming the path to it are instantiated if necessary
  213. * - we never let on to the netfs about errors
  214. * - we may set a negative cookie pointer, but that's okay
  215. */
  216. struct fscache_cookie *__fscache_acquire_cookie(
  217. struct fscache_cookie *parent,
  218. const struct fscache_cookie_def *def,
  219. const void *index_key, size_t index_key_len,
  220. const void *aux_data, size_t aux_data_len,
  221. void *netfs_data,
  222. loff_t object_size,
  223. bool enable)
  224. {
  225. struct fscache_cookie *candidate, *cookie;
  226. BUG_ON(!def);
  227. _enter("{%s},{%s},%p,%u",
  228. parent ? (char *) parent->def->name : "<no-parent>",
  229. def->name, netfs_data, enable);
  230. if (!index_key || !index_key_len || index_key_len > 255 || aux_data_len > 255)
  231. return NULL;
  232. if (!aux_data || !aux_data_len) {
  233. aux_data = NULL;
  234. aux_data_len = 0;
  235. }
  236. fscache_stat(&fscache_n_acquires);
  237. /* if there's no parent cookie, then we don't create one here either */
  238. if (!parent) {
  239. fscache_stat(&fscache_n_acquires_null);
  240. _leave(" [no parent]");
  241. return NULL;
  242. }
  243. /* validate the definition */
  244. BUG_ON(!def->name[0]);
  245. BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX &&
  246. parent->type != FSCACHE_COOKIE_TYPE_INDEX);
  247. candidate = fscache_alloc_cookie(parent, def,
  248. index_key, index_key_len,
  249. aux_data, aux_data_len,
  250. netfs_data, object_size);
  251. if (!candidate) {
  252. fscache_stat(&fscache_n_acquires_oom);
  253. _leave(" [ENOMEM]");
  254. return NULL;
  255. }
  256. cookie = fscache_hash_cookie(candidate);
  257. if (!cookie) {
  258. trace_fscache_cookie(candidate, fscache_cookie_discard, 1);
  259. goto out;
  260. }
  261. if (cookie == candidate)
  262. candidate = NULL;
  263. switch (cookie->type) {
  264. case FSCACHE_COOKIE_TYPE_INDEX:
  265. fscache_stat(&fscache_n_cookie_index);
  266. break;
  267. case FSCACHE_COOKIE_TYPE_DATAFILE:
  268. fscache_stat(&fscache_n_cookie_data);
  269. break;
  270. default:
  271. fscache_stat(&fscache_n_cookie_special);
  272. break;
  273. }
  274. trace_fscache_acquire(cookie);
  275. if (enable) {
  276. /* if the object is an index then we need do nothing more here
  277. * - we create indices on disk when we need them as an index
  278. * may exist in multiple caches */
  279. if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
  280. if (fscache_acquire_non_index_cookie(cookie, object_size) == 0) {
  281. set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
  282. } else {
  283. atomic_dec(&parent->n_children);
  284. fscache_cookie_put(cookie,
  285. fscache_cookie_put_acquire_nobufs);
  286. fscache_stat(&fscache_n_acquires_nobufs);
  287. _leave(" = NULL");
  288. return NULL;
  289. }
  290. } else {
  291. set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
  292. }
  293. }
  294. fscache_stat(&fscache_n_acquires_ok);
  295. out:
  296. fscache_free_cookie(candidate);
  297. return cookie;
  298. }
  299. EXPORT_SYMBOL(__fscache_acquire_cookie);
  300. /*
  301. * Enable a cookie to permit it to accept new operations.
  302. */
  303. void __fscache_enable_cookie(struct fscache_cookie *cookie,
  304. const void *aux_data,
  305. loff_t object_size,
  306. bool (*can_enable)(void *data),
  307. void *data)
  308. {
  309. _enter("%p", cookie);
  310. trace_fscache_enable(cookie);
  311. wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
  312. TASK_UNINTERRUPTIBLE);
  313. fscache_update_aux(cookie, aux_data);
  314. if (test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
  315. goto out_unlock;
  316. if (can_enable && !can_enable(data)) {
  317. /* The netfs decided it didn't want to enable after all */
  318. } else if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
  319. /* Wait for outstanding disablement to complete */
  320. __fscache_wait_on_invalidate(cookie);
  321. if (fscache_acquire_non_index_cookie(cookie, object_size) == 0)
  322. set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
  323. } else {
  324. set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
  325. }
  326. out_unlock:
  327. clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
  328. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
  329. }
  330. EXPORT_SYMBOL(__fscache_enable_cookie);
  331. /*
  332. * acquire a non-index cookie
  333. * - this must make sure the index chain is instantiated and instantiate the
  334. * object representation too
  335. */
  336. static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie,
  337. loff_t object_size)
  338. {
  339. struct fscache_object *object;
  340. struct fscache_cache *cache;
  341. int ret;
  342. _enter("");
  343. set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
  344. /* now we need to see whether the backing objects for this cookie yet
  345. * exist, if not there'll be nothing to search */
  346. down_read(&fscache_addremove_sem);
  347. if (list_empty(&fscache_cache_list)) {
  348. up_read(&fscache_addremove_sem);
  349. _leave(" = 0 [no caches]");
  350. return 0;
  351. }
  352. /* select a cache in which to store the object */
  353. cache = fscache_select_cache_for_object(cookie->parent);
  354. if (!cache) {
  355. up_read(&fscache_addremove_sem);
  356. fscache_stat(&fscache_n_acquires_no_cache);
  357. _leave(" = -ENOMEDIUM [no cache]");
  358. return -ENOMEDIUM;
  359. }
  360. _debug("cache %s", cache->tag->name);
  361. set_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
  362. /* ask the cache to allocate objects for this cookie and its parent
  363. * chain */
  364. ret = fscache_alloc_object(cache, cookie);
  365. if (ret < 0) {
  366. up_read(&fscache_addremove_sem);
  367. _leave(" = %d", ret);
  368. return ret;
  369. }
  370. spin_lock(&cookie->lock);
  371. if (hlist_empty(&cookie->backing_objects)) {
  372. spin_unlock(&cookie->lock);
  373. goto unavailable;
  374. }
  375. object = hlist_entry(cookie->backing_objects.first,
  376. struct fscache_object, cookie_link);
  377. fscache_set_store_limit(object, object_size);
  378. /* initiate the process of looking up all the objects in the chain
  379. * (done by fscache_initialise_object()) */
  380. fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD);
  381. spin_unlock(&cookie->lock);
  382. /* we may be required to wait for lookup to complete at this point */
  383. if (!fscache_defer_lookup) {
  384. _debug("non-deferred lookup %p", &cookie->flags);
  385. wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
  386. TASK_UNINTERRUPTIBLE);
  387. _debug("complete");
  388. if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags))
  389. goto unavailable;
  390. }
  391. up_read(&fscache_addremove_sem);
  392. _leave(" = 0 [deferred]");
  393. return 0;
  394. unavailable:
  395. up_read(&fscache_addremove_sem);
  396. _leave(" = -ENOBUFS");
  397. return -ENOBUFS;
  398. }
  399. /*
  400. * recursively allocate cache object records for a cookie/cache combination
  401. * - caller must be holding the addremove sem
  402. */
  403. static int fscache_alloc_object(struct fscache_cache *cache,
  404. struct fscache_cookie *cookie)
  405. {
  406. struct fscache_object *object;
  407. int ret;
  408. _enter("%p,%p{%s}", cache, cookie, cookie->def->name);
  409. spin_lock(&cookie->lock);
  410. hlist_for_each_entry(object, &cookie->backing_objects,
  411. cookie_link) {
  412. if (object->cache == cache)
  413. goto object_already_extant;
  414. }
  415. spin_unlock(&cookie->lock);
  416. /* ask the cache to allocate an object (we may end up with duplicate
  417. * objects at this stage, but we sort that out later) */
  418. fscache_stat(&fscache_n_cop_alloc_object);
  419. object = cache->ops->alloc_object(cache, cookie);
  420. fscache_stat_d(&fscache_n_cop_alloc_object);
  421. if (IS_ERR(object)) {
  422. fscache_stat(&fscache_n_object_no_alloc);
  423. ret = PTR_ERR(object);
  424. goto error;
  425. }
  426. ASSERTCMP(object->cookie, ==, cookie);
  427. fscache_stat(&fscache_n_object_alloc);
  428. object->debug_id = atomic_inc_return(&fscache_object_debug_id);
  429. _debug("ALLOC OBJ%x: %s {%lx}",
  430. object->debug_id, cookie->def->name, object->events);
  431. ret = fscache_alloc_object(cache, cookie->parent);
  432. if (ret < 0)
  433. goto error_put;
  434. /* only attach if we managed to allocate all we needed, otherwise
  435. * discard the object we just allocated and instead use the one
  436. * attached to the cookie */
  437. if (fscache_attach_object(cookie, object) < 0) {
  438. fscache_stat(&fscache_n_cop_put_object);
  439. cache->ops->put_object(object, fscache_obj_put_attach_fail);
  440. fscache_stat_d(&fscache_n_cop_put_object);
  441. }
  442. _leave(" = 0");
  443. return 0;
  444. object_already_extant:
  445. ret = -ENOBUFS;
  446. if (fscache_object_is_dying(object) ||
  447. fscache_cache_is_broken(object)) {
  448. spin_unlock(&cookie->lock);
  449. goto error;
  450. }
  451. spin_unlock(&cookie->lock);
  452. _leave(" = 0 [found]");
  453. return 0;
  454. error_put:
  455. fscache_stat(&fscache_n_cop_put_object);
  456. cache->ops->put_object(object, fscache_obj_put_alloc_fail);
  457. fscache_stat_d(&fscache_n_cop_put_object);
  458. error:
  459. _leave(" = %d", ret);
  460. return ret;
  461. }
  462. /*
  463. * attach a cache object to a cookie
  464. */
  465. static int fscache_attach_object(struct fscache_cookie *cookie,
  466. struct fscache_object *object)
  467. {
  468. struct fscache_object *p;
  469. struct fscache_cache *cache = object->cache;
  470. int ret;
  471. _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id);
  472. ASSERTCMP(object->cookie, ==, cookie);
  473. spin_lock(&cookie->lock);
  474. /* there may be multiple initial creations of this object, but we only
  475. * want one */
  476. ret = -EEXIST;
  477. hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) {
  478. if (p->cache == object->cache) {
  479. if (fscache_object_is_dying(p))
  480. ret = -ENOBUFS;
  481. goto cant_attach_object;
  482. }
  483. }
  484. /* pin the parent object */
  485. spin_lock_nested(&cookie->parent->lock, 1);
  486. hlist_for_each_entry(p, &cookie->parent->backing_objects,
  487. cookie_link) {
  488. if (p->cache == object->cache) {
  489. if (fscache_object_is_dying(p)) {
  490. ret = -ENOBUFS;
  491. spin_unlock(&cookie->parent->lock);
  492. goto cant_attach_object;
  493. }
  494. object->parent = p;
  495. spin_lock(&p->lock);
  496. p->n_children++;
  497. spin_unlock(&p->lock);
  498. break;
  499. }
  500. }
  501. spin_unlock(&cookie->parent->lock);
  502. /* attach to the cache's object list */
  503. if (list_empty(&object->cache_link)) {
  504. spin_lock(&cache->object_list_lock);
  505. list_add(&object->cache_link, &cache->object_list);
  506. spin_unlock(&cache->object_list_lock);
  507. }
  508. /* Attach to the cookie. The object already has a ref on it. */
  509. hlist_add_head(&object->cookie_link, &cookie->backing_objects);
  510. fscache_objlist_add(object);
  511. ret = 0;
  512. cant_attach_object:
  513. spin_unlock(&cookie->lock);
  514. _leave(" = %d", ret);
  515. return ret;
  516. }
  517. /*
  518. * Invalidate an object. Callable with spinlocks held.
  519. */
  520. void __fscache_invalidate(struct fscache_cookie *cookie)
  521. {
  522. struct fscache_object *object;
  523. _enter("{%s}", cookie->def->name);
  524. fscache_stat(&fscache_n_invalidates);
  525. /* Only permit invalidation of data files. Invalidating an index will
  526. * require the caller to release all its attachments to the tree rooted
  527. * there, and if it's doing that, it may as well just retire the
  528. * cookie.
  529. */
  530. ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
  531. /* If there's an object, we tell the object state machine to handle the
  532. * invalidation on our behalf, otherwise there's nothing to do.
  533. */
  534. if (!hlist_empty(&cookie->backing_objects)) {
  535. spin_lock(&cookie->lock);
  536. if (fscache_cookie_enabled(cookie) &&
  537. !hlist_empty(&cookie->backing_objects) &&
  538. !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
  539. &cookie->flags)) {
  540. object = hlist_entry(cookie->backing_objects.first,
  541. struct fscache_object,
  542. cookie_link);
  543. if (fscache_object_is_live(object))
  544. fscache_raise_event(
  545. object, FSCACHE_OBJECT_EV_INVALIDATE);
  546. }
  547. spin_unlock(&cookie->lock);
  548. }
  549. _leave("");
  550. }
  551. EXPORT_SYMBOL(__fscache_invalidate);
  552. /*
  553. * Wait for object invalidation to complete.
  554. */
  555. void __fscache_wait_on_invalidate(struct fscache_cookie *cookie)
  556. {
  557. _enter("%p", cookie);
  558. wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING,
  559. TASK_UNINTERRUPTIBLE);
  560. _leave("");
  561. }
  562. EXPORT_SYMBOL(__fscache_wait_on_invalidate);
  563. /*
  564. * update the index entries backing a cookie
  565. */
  566. void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data)
  567. {
  568. struct fscache_object *object;
  569. fscache_stat(&fscache_n_updates);
  570. if (!cookie) {
  571. fscache_stat(&fscache_n_updates_null);
  572. _leave(" [no cookie]");
  573. return;
  574. }
  575. _enter("{%s}", cookie->def->name);
  576. spin_lock(&cookie->lock);
  577. fscache_update_aux(cookie, aux_data);
  578. if (fscache_cookie_enabled(cookie)) {
  579. /* update the index entry on disk in each cache backing this
  580. * cookie.
  581. */
  582. hlist_for_each_entry(object,
  583. &cookie->backing_objects, cookie_link) {
  584. fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
  585. }
  586. }
  587. spin_unlock(&cookie->lock);
  588. _leave("");
  589. }
  590. EXPORT_SYMBOL(__fscache_update_cookie);
  591. /*
  592. * Disable a cookie to stop it from accepting new requests from the netfs.
  593. */
  594. void __fscache_disable_cookie(struct fscache_cookie *cookie,
  595. const void *aux_data,
  596. bool invalidate)
  597. {
  598. struct fscache_object *object;
  599. bool awaken = false;
  600. _enter("%p,%u", cookie, invalidate);
  601. trace_fscache_disable(cookie);
  602. ASSERTCMP(atomic_read(&cookie->n_active), >, 0);
  603. if (atomic_read(&cookie->n_children) != 0) {
  604. pr_err("Cookie '%s' still has children\n",
  605. cookie->def->name);
  606. BUG();
  607. }
  608. wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
  609. TASK_UNINTERRUPTIBLE);
  610. fscache_update_aux(cookie, aux_data);
  611. if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
  612. goto out_unlock_enable;
  613. /* If the cookie is being invalidated, wait for that to complete first
  614. * so that we can reuse the flag.
  615. */
  616. __fscache_wait_on_invalidate(cookie);
  617. /* Dispose of the backing objects */
  618. set_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags);
  619. spin_lock(&cookie->lock);
  620. if (!hlist_empty(&cookie->backing_objects)) {
  621. hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) {
  622. if (invalidate)
  623. set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
  624. clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
  625. fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
  626. }
  627. } else {
  628. if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
  629. awaken = true;
  630. }
  631. spin_unlock(&cookie->lock);
  632. if (awaken)
  633. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
  634. /* Wait for cessation of activity requiring access to the netfs (when
  635. * n_active reaches 0). This makes sure outstanding reads and writes
  636. * have completed.
  637. */
  638. if (!atomic_dec_and_test(&cookie->n_active)) {
  639. wait_var_event(&cookie->n_active,
  640. !atomic_read(&cookie->n_active));
  641. }
  642. /* Make sure any pending writes are cancelled. */
  643. if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX)
  644. fscache_invalidate_writes(cookie);
  645. /* Reset the cookie state if it wasn't relinquished */
  646. if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) {
  647. atomic_inc(&cookie->n_active);
  648. set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  649. }
  650. out_unlock_enable:
  651. clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
  652. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
  653. _leave("");
  654. }
  655. EXPORT_SYMBOL(__fscache_disable_cookie);
  656. /*
  657. * release a cookie back to the cache
  658. * - the object will be marked as recyclable on disk if retire is true
  659. * - all dependents of this cookie must have already been unregistered
  660. * (indices/files/pages)
  661. */
  662. void __fscache_relinquish_cookie(struct fscache_cookie *cookie,
  663. const void *aux_data,
  664. bool retire)
  665. {
  666. fscache_stat(&fscache_n_relinquishes);
  667. if (retire)
  668. fscache_stat(&fscache_n_relinquishes_retire);
  669. if (!cookie) {
  670. fscache_stat(&fscache_n_relinquishes_null);
  671. _leave(" [no cookie]");
  672. return;
  673. }
  674. _enter("%p{%s,%p,%d},%d",
  675. cookie, cookie->def->name, cookie->netfs_data,
  676. atomic_read(&cookie->n_active), retire);
  677. trace_fscache_relinquish(cookie, retire);
  678. /* No further netfs-accessing operations on this cookie permitted */
  679. if (test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags))
  680. BUG();
  681. __fscache_disable_cookie(cookie, aux_data, retire);
  682. /* Clear pointers back to the netfs */
  683. cookie->netfs_data = NULL;
  684. cookie->def = NULL;
  685. BUG_ON(!radix_tree_empty(&cookie->stores));
  686. if (cookie->parent) {
  687. ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
  688. ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0);
  689. atomic_dec(&cookie->parent->n_children);
  690. }
  691. /* Dispose of the netfs's link to the cookie */
  692. ASSERTCMP(atomic_read(&cookie->usage), >, 0);
  693. fscache_cookie_put(cookie, fscache_cookie_put_relinquish);
  694. _leave("");
  695. }
  696. EXPORT_SYMBOL(__fscache_relinquish_cookie);
  697. /*
  698. * Remove a cookie from the hash table.
  699. */
  700. static void fscache_unhash_cookie(struct fscache_cookie *cookie)
  701. {
  702. struct hlist_bl_head *h;
  703. unsigned int bucket;
  704. bucket = cookie->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
  705. h = &fscache_cookie_hash[bucket];
  706. hlist_bl_lock(h);
  707. hlist_bl_del(&cookie->hash_link);
  708. hlist_bl_unlock(h);
  709. }
  710. /*
  711. * Drop a reference to a cookie.
  712. */
  713. void fscache_cookie_put(struct fscache_cookie *cookie,
  714. enum fscache_cookie_trace where)
  715. {
  716. struct fscache_cookie *parent;
  717. int usage;
  718. _enter("%p", cookie);
  719. do {
  720. usage = atomic_dec_return(&cookie->usage);
  721. trace_fscache_cookie(cookie, where, usage);
  722. if (usage > 0)
  723. return;
  724. BUG_ON(usage < 0);
  725. parent = cookie->parent;
  726. fscache_unhash_cookie(cookie);
  727. fscache_free_cookie(cookie);
  728. cookie = parent;
  729. where = fscache_cookie_put_parent;
  730. } while (cookie);
  731. _leave("");
  732. }
  733. /*
  734. * check the consistency between the netfs inode and the backing cache
  735. *
  736. * NOTE: it only serves no-index type
  737. */
  738. int __fscache_check_consistency(struct fscache_cookie *cookie,
  739. const void *aux_data)
  740. {
  741. struct fscache_operation *op;
  742. struct fscache_object *object;
  743. bool wake_cookie = false;
  744. int ret;
  745. _enter("%p,", cookie);
  746. ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
  747. if (fscache_wait_for_deferred_lookup(cookie) < 0)
  748. return -ERESTARTSYS;
  749. if (hlist_empty(&cookie->backing_objects))
  750. return 0;
  751. op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
  752. if (!op)
  753. return -ENOMEM;
  754. fscache_operation_init(cookie, op, NULL, NULL, NULL);
  755. op->flags = FSCACHE_OP_MYTHREAD |
  756. (1 << FSCACHE_OP_WAITING) |
  757. (1 << FSCACHE_OP_UNUSE_COOKIE);
  758. trace_fscache_page_op(cookie, NULL, op, fscache_page_op_check_consistency);
  759. spin_lock(&cookie->lock);
  760. fscache_update_aux(cookie, aux_data);
  761. if (!fscache_cookie_enabled(cookie) ||
  762. hlist_empty(&cookie->backing_objects))
  763. goto inconsistent;
  764. object = hlist_entry(cookie->backing_objects.first,
  765. struct fscache_object, cookie_link);
  766. if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
  767. goto inconsistent;
  768. op->debug_id = atomic_inc_return(&fscache_op_debug_id);
  769. __fscache_use_cookie(cookie);
  770. if (fscache_submit_op(object, op) < 0)
  771. goto submit_failed;
  772. /* the work queue now carries its own ref on the object */
  773. spin_unlock(&cookie->lock);
  774. ret = fscache_wait_for_operation_activation(object, op, NULL, NULL);
  775. if (ret == 0) {
  776. /* ask the cache to honour the operation */
  777. ret = object->cache->ops->check_consistency(op);
  778. fscache_op_complete(op, false);
  779. } else if (ret == -ENOBUFS) {
  780. ret = 0;
  781. }
  782. fscache_put_operation(op);
  783. _leave(" = %d", ret);
  784. return ret;
  785. submit_failed:
  786. wake_cookie = __fscache_unuse_cookie(cookie);
  787. inconsistent:
  788. spin_unlock(&cookie->lock);
  789. if (wake_cookie)
  790. __fscache_wake_unused_cookie(cookie);
  791. kfree(op);
  792. _leave(" = -ESTALE");
  793. return -ESTALE;
  794. }
  795. EXPORT_SYMBOL(__fscache_check_consistency);