page.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /* Cache page management and data I/O routines
  2. *
  3. * Copyright (C) 2004-2008 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. #define FSCACHE_DEBUG_LEVEL PAGE
  12. #include <linux/module.h>
  13. #include <linux/fscache-cache.h>
  14. #include <linux/buffer_head.h>
  15. #include <linux/pagevec.h>
  16. #include <linux/slab.h>
  17. #include "internal.h"
  18. /*
  19. * check to see if a page is being written to the cache
  20. */
  21. bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page)
  22. {
  23. void *val;
  24. rcu_read_lock();
  25. val = radix_tree_lookup(&cookie->stores, page->index);
  26. rcu_read_unlock();
  27. return val != NULL;
  28. }
  29. EXPORT_SYMBOL(__fscache_check_page_write);
  30. /*
  31. * wait for a page to finish being written to the cache
  32. */
  33. void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page)
  34. {
  35. wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
  36. wait_event(*wq, !__fscache_check_page_write(cookie, page));
  37. }
  38. EXPORT_SYMBOL(__fscache_wait_on_page_write);
  39. /*
  40. * wait for a page to finish being written to the cache. Put a timeout here
  41. * since we might be called recursively via parent fs.
  42. */
  43. static
  44. bool release_page_wait_timeout(struct fscache_cookie *cookie, struct page *page)
  45. {
  46. wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
  47. return wait_event_timeout(*wq, !__fscache_check_page_write(cookie, page),
  48. HZ);
  49. }
  50. /*
  51. * decide whether a page can be released, possibly by cancelling a store to it
  52. * - we're allowed to sleep if __GFP_WAIT is flagged
  53. */
  54. bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
  55. struct page *page,
  56. gfp_t gfp)
  57. {
  58. struct page *xpage;
  59. void *val;
  60. _enter("%p,%p,%x", cookie, page, gfp);
  61. try_again:
  62. rcu_read_lock();
  63. val = radix_tree_lookup(&cookie->stores, page->index);
  64. if (!val) {
  65. rcu_read_unlock();
  66. fscache_stat(&fscache_n_store_vmscan_not_storing);
  67. __fscache_uncache_page(cookie, page);
  68. return true;
  69. }
  70. /* see if the page is actually undergoing storage - if so we can't get
  71. * rid of it till the cache has finished with it */
  72. if (radix_tree_tag_get(&cookie->stores, page->index,
  73. FSCACHE_COOKIE_STORING_TAG)) {
  74. rcu_read_unlock();
  75. goto page_busy;
  76. }
  77. /* the page is pending storage, so we attempt to cancel the store and
  78. * discard the store request so that the page can be reclaimed */
  79. spin_lock(&cookie->stores_lock);
  80. rcu_read_unlock();
  81. if (radix_tree_tag_get(&cookie->stores, page->index,
  82. FSCACHE_COOKIE_STORING_TAG)) {
  83. /* the page started to undergo storage whilst we were looking,
  84. * so now we can only wait or return */
  85. spin_unlock(&cookie->stores_lock);
  86. goto page_busy;
  87. }
  88. xpage = radix_tree_delete(&cookie->stores, page->index);
  89. spin_unlock(&cookie->stores_lock);
  90. if (xpage) {
  91. fscache_stat(&fscache_n_store_vmscan_cancelled);
  92. fscache_stat(&fscache_n_store_radix_deletes);
  93. ASSERTCMP(xpage, ==, page);
  94. } else {
  95. fscache_stat(&fscache_n_store_vmscan_gone);
  96. }
  97. wake_up_bit(&cookie->flags, 0);
  98. if (xpage)
  99. page_cache_release(xpage);
  100. __fscache_uncache_page(cookie, page);
  101. return true;
  102. page_busy:
  103. /* We will wait here if we're allowed to, but that could deadlock the
  104. * allocator as the work threads writing to the cache may all end up
  105. * sleeping on memory allocation, so we may need to impose a timeout
  106. * too. */
  107. if (!(gfp & __GFP_WAIT) || !(gfp & __GFP_FS)) {
  108. fscache_stat(&fscache_n_store_vmscan_busy);
  109. return false;
  110. }
  111. fscache_stat(&fscache_n_store_vmscan_wait);
  112. if (!release_page_wait_timeout(cookie, page))
  113. _debug("fscache writeout timeout page: %p{%lx}",
  114. page, page->index);
  115. gfp &= ~__GFP_WAIT;
  116. goto try_again;
  117. }
  118. EXPORT_SYMBOL(__fscache_maybe_release_page);
  119. /*
  120. * note that a page has finished being written to the cache
  121. */
  122. static void fscache_end_page_write(struct fscache_object *object,
  123. struct page *page)
  124. {
  125. struct fscache_cookie *cookie;
  126. struct page *xpage = NULL;
  127. spin_lock(&object->lock);
  128. cookie = object->cookie;
  129. if (cookie) {
  130. /* delete the page from the tree if it is now no longer
  131. * pending */
  132. spin_lock(&cookie->stores_lock);
  133. radix_tree_tag_clear(&cookie->stores, page->index,
  134. FSCACHE_COOKIE_STORING_TAG);
  135. if (!radix_tree_tag_get(&cookie->stores, page->index,
  136. FSCACHE_COOKIE_PENDING_TAG)) {
  137. fscache_stat(&fscache_n_store_radix_deletes);
  138. xpage = radix_tree_delete(&cookie->stores, page->index);
  139. }
  140. spin_unlock(&cookie->stores_lock);
  141. wake_up_bit(&cookie->flags, 0);
  142. }
  143. spin_unlock(&object->lock);
  144. if (xpage)
  145. page_cache_release(xpage);
  146. }
  147. /*
  148. * actually apply the changed attributes to a cache object
  149. */
  150. static void fscache_attr_changed_op(struct fscache_operation *op)
  151. {
  152. struct fscache_object *object = op->object;
  153. int ret;
  154. _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
  155. fscache_stat(&fscache_n_attr_changed_calls);
  156. if (fscache_object_is_active(object)) {
  157. fscache_stat(&fscache_n_cop_attr_changed);
  158. ret = object->cache->ops->attr_changed(object);
  159. fscache_stat_d(&fscache_n_cop_attr_changed);
  160. if (ret < 0)
  161. fscache_abort_object(object);
  162. }
  163. fscache_op_complete(op, true);
  164. _leave("");
  165. }
  166. /*
  167. * notification that the attributes on an object have changed
  168. */
  169. int __fscache_attr_changed(struct fscache_cookie *cookie)
  170. {
  171. struct fscache_operation *op;
  172. struct fscache_object *object;
  173. bool wake_cookie = false;
  174. _enter("%p", cookie);
  175. ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  176. fscache_stat(&fscache_n_attr_changed);
  177. op = kzalloc(sizeof(*op), GFP_KERNEL);
  178. if (!op) {
  179. fscache_stat(&fscache_n_attr_changed_nomem);
  180. _leave(" = -ENOMEM");
  181. return -ENOMEM;
  182. }
  183. fscache_operation_init(op, fscache_attr_changed_op, NULL);
  184. op->flags = FSCACHE_OP_ASYNC |
  185. (1 << FSCACHE_OP_EXCLUSIVE) |
  186. (1 << FSCACHE_OP_UNUSE_COOKIE);
  187. spin_lock(&cookie->lock);
  188. if (!fscache_cookie_enabled(cookie) ||
  189. hlist_empty(&cookie->backing_objects))
  190. goto nobufs;
  191. object = hlist_entry(cookie->backing_objects.first,
  192. struct fscache_object, cookie_link);
  193. __fscache_use_cookie(cookie);
  194. if (fscache_submit_exclusive_op(object, op) < 0)
  195. goto nobufs_dec;
  196. spin_unlock(&cookie->lock);
  197. fscache_stat(&fscache_n_attr_changed_ok);
  198. fscache_put_operation(op);
  199. _leave(" = 0");
  200. return 0;
  201. nobufs_dec:
  202. wake_cookie = __fscache_unuse_cookie(cookie);
  203. nobufs:
  204. spin_unlock(&cookie->lock);
  205. kfree(op);
  206. if (wake_cookie)
  207. __fscache_wake_unused_cookie(cookie);
  208. fscache_stat(&fscache_n_attr_changed_nobufs);
  209. _leave(" = %d", -ENOBUFS);
  210. return -ENOBUFS;
  211. }
  212. EXPORT_SYMBOL(__fscache_attr_changed);
  213. /*
  214. * release a retrieval op reference
  215. */
  216. static void fscache_release_retrieval_op(struct fscache_operation *_op)
  217. {
  218. struct fscache_retrieval *op =
  219. container_of(_op, struct fscache_retrieval, op);
  220. _enter("{OP%x}", op->op.debug_id);
  221. ASSERTCMP(atomic_read(&op->n_pages), ==, 0);
  222. fscache_hist(fscache_retrieval_histogram, op->start_time);
  223. if (op->context)
  224. fscache_put_context(op->op.object->cookie, op->context);
  225. _leave("");
  226. }
  227. /*
  228. * allocate a retrieval op
  229. */
  230. static struct fscache_retrieval *fscache_alloc_retrieval(
  231. struct fscache_cookie *cookie,
  232. struct address_space *mapping,
  233. fscache_rw_complete_t end_io_func,
  234. void *context)
  235. {
  236. struct fscache_retrieval *op;
  237. /* allocate a retrieval operation and attempt to submit it */
  238. op = kzalloc(sizeof(*op), GFP_NOIO);
  239. if (!op) {
  240. fscache_stat(&fscache_n_retrievals_nomem);
  241. return NULL;
  242. }
  243. fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
  244. op->op.flags = FSCACHE_OP_MYTHREAD |
  245. (1UL << FSCACHE_OP_WAITING) |
  246. (1UL << FSCACHE_OP_UNUSE_COOKIE);
  247. op->mapping = mapping;
  248. op->end_io_func = end_io_func;
  249. op->context = context;
  250. op->start_time = jiffies;
  251. INIT_LIST_HEAD(&op->to_do);
  252. return op;
  253. }
  254. /*
  255. * wait for a deferred lookup to complete
  256. */
  257. int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
  258. {
  259. unsigned long jif;
  260. _enter("");
  261. if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
  262. _leave(" = 0 [imm]");
  263. return 0;
  264. }
  265. fscache_stat(&fscache_n_retrievals_wait);
  266. jif = jiffies;
  267. if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
  268. TASK_INTERRUPTIBLE) != 0) {
  269. fscache_stat(&fscache_n_retrievals_intr);
  270. _leave(" = -ERESTARTSYS");
  271. return -ERESTARTSYS;
  272. }
  273. ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
  274. smp_rmb();
  275. fscache_hist(fscache_retrieval_delay_histogram, jif);
  276. _leave(" = 0 [dly]");
  277. return 0;
  278. }
  279. /*
  280. * Handle cancellation of a pending retrieval op
  281. */
  282. static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
  283. {
  284. struct fscache_retrieval *op =
  285. container_of(_op, struct fscache_retrieval, op);
  286. atomic_set(&op->n_pages, 0);
  287. }
  288. /*
  289. * wait for an object to become active (or dead)
  290. */
  291. int fscache_wait_for_operation_activation(struct fscache_object *object,
  292. struct fscache_operation *op,
  293. atomic_t *stat_op_waits,
  294. atomic_t *stat_object_dead,
  295. void (*do_cancel)(struct fscache_operation *))
  296. {
  297. int ret;
  298. if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
  299. goto check_if_dead;
  300. _debug(">>> WT");
  301. if (stat_op_waits)
  302. fscache_stat(stat_op_waits);
  303. if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
  304. TASK_INTERRUPTIBLE) != 0) {
  305. ret = fscache_cancel_op(op, do_cancel);
  306. if (ret == 0)
  307. return -ERESTARTSYS;
  308. /* it's been removed from the pending queue by another party,
  309. * so we should get to run shortly */
  310. wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
  311. TASK_UNINTERRUPTIBLE);
  312. }
  313. _debug("<<< GO");
  314. check_if_dead:
  315. if (op->state == FSCACHE_OP_ST_CANCELLED) {
  316. if (stat_object_dead)
  317. fscache_stat(stat_object_dead);
  318. _leave(" = -ENOBUFS [cancelled]");
  319. return -ENOBUFS;
  320. }
  321. if (unlikely(fscache_object_is_dead(object))) {
  322. pr_err("%s() = -ENOBUFS [obj dead %d]\n", __func__, op->state);
  323. fscache_cancel_op(op, do_cancel);
  324. if (stat_object_dead)
  325. fscache_stat(stat_object_dead);
  326. return -ENOBUFS;
  327. }
  328. return 0;
  329. }
  330. /*
  331. * read a page from the cache or allocate a block in which to store it
  332. * - we return:
  333. * -ENOMEM - out of memory, nothing done
  334. * -ERESTARTSYS - interrupted
  335. * -ENOBUFS - no backing object available in which to cache the block
  336. * -ENODATA - no data available in the backing object for this block
  337. * 0 - dispatched a read - it'll call end_io_func() when finished
  338. */
  339. int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
  340. struct page *page,
  341. fscache_rw_complete_t end_io_func,
  342. void *context,
  343. gfp_t gfp)
  344. {
  345. struct fscache_retrieval *op;
  346. struct fscache_object *object;
  347. bool wake_cookie = false;
  348. int ret;
  349. _enter("%p,%p,,,", cookie, page);
  350. fscache_stat(&fscache_n_retrievals);
  351. if (hlist_empty(&cookie->backing_objects))
  352. goto nobufs;
  353. if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
  354. _leave(" = -ENOBUFS [invalidating]");
  355. return -ENOBUFS;
  356. }
  357. ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  358. ASSERTCMP(page, !=, NULL);
  359. if (fscache_wait_for_deferred_lookup(cookie) < 0)
  360. return -ERESTARTSYS;
  361. op = fscache_alloc_retrieval(cookie, page->mapping,
  362. end_io_func, context);
  363. if (!op) {
  364. _leave(" = -ENOMEM");
  365. return -ENOMEM;
  366. }
  367. atomic_set(&op->n_pages, 1);
  368. spin_lock(&cookie->lock);
  369. if (!fscache_cookie_enabled(cookie) ||
  370. hlist_empty(&cookie->backing_objects))
  371. goto nobufs_unlock;
  372. object = hlist_entry(cookie->backing_objects.first,
  373. struct fscache_object, cookie_link);
  374. ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
  375. __fscache_use_cookie(cookie);
  376. atomic_inc(&object->n_reads);
  377. __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
  378. if (fscache_submit_op(object, &op->op) < 0)
  379. goto nobufs_unlock_dec;
  380. spin_unlock(&cookie->lock);
  381. fscache_stat(&fscache_n_retrieval_ops);
  382. /* pin the netfs read context in case we need to do the actual netfs
  383. * read because we've encountered a cache read failure */
  384. fscache_get_context(object->cookie, op->context);
  385. /* we wait for the operation to become active, and then process it
  386. * *here*, in this thread, and not in the thread pool */
  387. ret = fscache_wait_for_operation_activation(
  388. object, &op->op,
  389. __fscache_stat(&fscache_n_retrieval_op_waits),
  390. __fscache_stat(&fscache_n_retrievals_object_dead),
  391. fscache_do_cancel_retrieval);
  392. if (ret < 0)
  393. goto error;
  394. /* ask the cache to honour the operation */
  395. if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
  396. fscache_stat(&fscache_n_cop_allocate_page);
  397. ret = object->cache->ops->allocate_page(op, page, gfp);
  398. fscache_stat_d(&fscache_n_cop_allocate_page);
  399. if (ret == 0)
  400. ret = -ENODATA;
  401. } else {
  402. fscache_stat(&fscache_n_cop_read_or_alloc_page);
  403. ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
  404. fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
  405. }
  406. error:
  407. if (ret == -ENOMEM)
  408. fscache_stat(&fscache_n_retrievals_nomem);
  409. else if (ret == -ERESTARTSYS)
  410. fscache_stat(&fscache_n_retrievals_intr);
  411. else if (ret == -ENODATA)
  412. fscache_stat(&fscache_n_retrievals_nodata);
  413. else if (ret < 0)
  414. fscache_stat(&fscache_n_retrievals_nobufs);
  415. else
  416. fscache_stat(&fscache_n_retrievals_ok);
  417. fscache_put_retrieval(op);
  418. _leave(" = %d", ret);
  419. return ret;
  420. nobufs_unlock_dec:
  421. atomic_dec(&object->n_reads);
  422. wake_cookie = __fscache_unuse_cookie(cookie);
  423. nobufs_unlock:
  424. spin_unlock(&cookie->lock);
  425. if (wake_cookie)
  426. __fscache_wake_unused_cookie(cookie);
  427. kfree(op);
  428. nobufs:
  429. fscache_stat(&fscache_n_retrievals_nobufs);
  430. _leave(" = -ENOBUFS");
  431. return -ENOBUFS;
  432. }
  433. EXPORT_SYMBOL(__fscache_read_or_alloc_page);
  434. /*
  435. * read a list of page from the cache or allocate a block in which to store
  436. * them
  437. * - we return:
  438. * -ENOMEM - out of memory, some pages may be being read
  439. * -ERESTARTSYS - interrupted, some pages may be being read
  440. * -ENOBUFS - no backing object or space available in which to cache any
  441. * pages not being read
  442. * -ENODATA - no data available in the backing object for some or all of
  443. * the pages
  444. * 0 - dispatched a read on all pages
  445. *
  446. * end_io_func() will be called for each page read from the cache as it is
  447. * finishes being read
  448. *
  449. * any pages for which a read is dispatched will be removed from pages and
  450. * nr_pages
  451. */
  452. int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
  453. struct address_space *mapping,
  454. struct list_head *pages,
  455. unsigned *nr_pages,
  456. fscache_rw_complete_t end_io_func,
  457. void *context,
  458. gfp_t gfp)
  459. {
  460. struct fscache_retrieval *op;
  461. struct fscache_object *object;
  462. bool wake_cookie = false;
  463. int ret;
  464. _enter("%p,,%d,,,", cookie, *nr_pages);
  465. fscache_stat(&fscache_n_retrievals);
  466. if (hlist_empty(&cookie->backing_objects))
  467. goto nobufs;
  468. if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
  469. _leave(" = -ENOBUFS [invalidating]");
  470. return -ENOBUFS;
  471. }
  472. ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  473. ASSERTCMP(*nr_pages, >, 0);
  474. ASSERT(!list_empty(pages));
  475. if (fscache_wait_for_deferred_lookup(cookie) < 0)
  476. return -ERESTARTSYS;
  477. op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
  478. if (!op)
  479. return -ENOMEM;
  480. atomic_set(&op->n_pages, *nr_pages);
  481. spin_lock(&cookie->lock);
  482. if (!fscache_cookie_enabled(cookie) ||
  483. hlist_empty(&cookie->backing_objects))
  484. goto nobufs_unlock;
  485. object = hlist_entry(cookie->backing_objects.first,
  486. struct fscache_object, cookie_link);
  487. __fscache_use_cookie(cookie);
  488. atomic_inc(&object->n_reads);
  489. __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
  490. if (fscache_submit_op(object, &op->op) < 0)
  491. goto nobufs_unlock_dec;
  492. spin_unlock(&cookie->lock);
  493. fscache_stat(&fscache_n_retrieval_ops);
  494. /* pin the netfs read context in case we need to do the actual netfs
  495. * read because we've encountered a cache read failure */
  496. fscache_get_context(object->cookie, op->context);
  497. /* we wait for the operation to become active, and then process it
  498. * *here*, in this thread, and not in the thread pool */
  499. ret = fscache_wait_for_operation_activation(
  500. object, &op->op,
  501. __fscache_stat(&fscache_n_retrieval_op_waits),
  502. __fscache_stat(&fscache_n_retrievals_object_dead),
  503. fscache_do_cancel_retrieval);
  504. if (ret < 0)
  505. goto error;
  506. /* ask the cache to honour the operation */
  507. if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
  508. fscache_stat(&fscache_n_cop_allocate_pages);
  509. ret = object->cache->ops->allocate_pages(
  510. op, pages, nr_pages, gfp);
  511. fscache_stat_d(&fscache_n_cop_allocate_pages);
  512. } else {
  513. fscache_stat(&fscache_n_cop_read_or_alloc_pages);
  514. ret = object->cache->ops->read_or_alloc_pages(
  515. op, pages, nr_pages, gfp);
  516. fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
  517. }
  518. error:
  519. if (ret == -ENOMEM)
  520. fscache_stat(&fscache_n_retrievals_nomem);
  521. else if (ret == -ERESTARTSYS)
  522. fscache_stat(&fscache_n_retrievals_intr);
  523. else if (ret == -ENODATA)
  524. fscache_stat(&fscache_n_retrievals_nodata);
  525. else if (ret < 0)
  526. fscache_stat(&fscache_n_retrievals_nobufs);
  527. else
  528. fscache_stat(&fscache_n_retrievals_ok);
  529. fscache_put_retrieval(op);
  530. _leave(" = %d", ret);
  531. return ret;
  532. nobufs_unlock_dec:
  533. atomic_dec(&object->n_reads);
  534. wake_cookie = __fscache_unuse_cookie(cookie);
  535. nobufs_unlock:
  536. spin_unlock(&cookie->lock);
  537. kfree(op);
  538. if (wake_cookie)
  539. __fscache_wake_unused_cookie(cookie);
  540. nobufs:
  541. fscache_stat(&fscache_n_retrievals_nobufs);
  542. _leave(" = -ENOBUFS");
  543. return -ENOBUFS;
  544. }
  545. EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
  546. /*
  547. * allocate a block in the cache on which to store a page
  548. * - we return:
  549. * -ENOMEM - out of memory, nothing done
  550. * -ERESTARTSYS - interrupted
  551. * -ENOBUFS - no backing object available in which to cache the block
  552. * 0 - block allocated
  553. */
  554. int __fscache_alloc_page(struct fscache_cookie *cookie,
  555. struct page *page,
  556. gfp_t gfp)
  557. {
  558. struct fscache_retrieval *op;
  559. struct fscache_object *object;
  560. bool wake_cookie = false;
  561. int ret;
  562. _enter("%p,%p,,,", cookie, page);
  563. fscache_stat(&fscache_n_allocs);
  564. if (hlist_empty(&cookie->backing_objects))
  565. goto nobufs;
  566. ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  567. ASSERTCMP(page, !=, NULL);
  568. if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
  569. _leave(" = -ENOBUFS [invalidating]");
  570. return -ENOBUFS;
  571. }
  572. if (fscache_wait_for_deferred_lookup(cookie) < 0)
  573. return -ERESTARTSYS;
  574. op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
  575. if (!op)
  576. return -ENOMEM;
  577. atomic_set(&op->n_pages, 1);
  578. spin_lock(&cookie->lock);
  579. if (!fscache_cookie_enabled(cookie) ||
  580. hlist_empty(&cookie->backing_objects))
  581. goto nobufs_unlock;
  582. object = hlist_entry(cookie->backing_objects.first,
  583. struct fscache_object, cookie_link);
  584. __fscache_use_cookie(cookie);
  585. if (fscache_submit_op(object, &op->op) < 0)
  586. goto nobufs_unlock_dec;
  587. spin_unlock(&cookie->lock);
  588. fscache_stat(&fscache_n_alloc_ops);
  589. ret = fscache_wait_for_operation_activation(
  590. object, &op->op,
  591. __fscache_stat(&fscache_n_alloc_op_waits),
  592. __fscache_stat(&fscache_n_allocs_object_dead),
  593. fscache_do_cancel_retrieval);
  594. if (ret < 0)
  595. goto error;
  596. /* ask the cache to honour the operation */
  597. fscache_stat(&fscache_n_cop_allocate_page);
  598. ret = object->cache->ops->allocate_page(op, page, gfp);
  599. fscache_stat_d(&fscache_n_cop_allocate_page);
  600. error:
  601. if (ret == -ERESTARTSYS)
  602. fscache_stat(&fscache_n_allocs_intr);
  603. else if (ret < 0)
  604. fscache_stat(&fscache_n_allocs_nobufs);
  605. else
  606. fscache_stat(&fscache_n_allocs_ok);
  607. fscache_put_retrieval(op);
  608. _leave(" = %d", ret);
  609. return ret;
  610. nobufs_unlock_dec:
  611. wake_cookie = __fscache_unuse_cookie(cookie);
  612. nobufs_unlock:
  613. spin_unlock(&cookie->lock);
  614. kfree(op);
  615. if (wake_cookie)
  616. __fscache_wake_unused_cookie(cookie);
  617. nobufs:
  618. fscache_stat(&fscache_n_allocs_nobufs);
  619. _leave(" = -ENOBUFS");
  620. return -ENOBUFS;
  621. }
  622. EXPORT_SYMBOL(__fscache_alloc_page);
  623. /*
  624. * Unmark pages allocate in the readahead code path (via:
  625. * fscache_readpages_or_alloc) after delegating to the base filesystem
  626. */
  627. void __fscache_readpages_cancel(struct fscache_cookie *cookie,
  628. struct list_head *pages)
  629. {
  630. struct page *page;
  631. list_for_each_entry(page, pages, lru) {
  632. if (PageFsCache(page))
  633. __fscache_uncache_page(cookie, page);
  634. }
  635. }
  636. EXPORT_SYMBOL(__fscache_readpages_cancel);
  637. /*
  638. * release a write op reference
  639. */
  640. static void fscache_release_write_op(struct fscache_operation *_op)
  641. {
  642. _enter("{OP%x}", _op->debug_id);
  643. }
  644. /*
  645. * perform the background storage of a page into the cache
  646. */
  647. static void fscache_write_op(struct fscache_operation *_op)
  648. {
  649. struct fscache_storage *op =
  650. container_of(_op, struct fscache_storage, op);
  651. struct fscache_object *object = op->op.object;
  652. struct fscache_cookie *cookie;
  653. struct page *page;
  654. unsigned n;
  655. void *results[1];
  656. int ret;
  657. _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
  658. spin_lock(&object->lock);
  659. cookie = object->cookie;
  660. if (!fscache_object_is_active(object)) {
  661. /* If we get here, then the on-disk cache object likely longer
  662. * exists, so we should just cancel this write operation.
  663. */
  664. spin_unlock(&object->lock);
  665. fscache_op_complete(&op->op, false);
  666. _leave(" [inactive]");
  667. return;
  668. }
  669. if (!cookie) {
  670. /* If we get here, then the cookie belonging to the object was
  671. * detached, probably by the cookie being withdrawn due to
  672. * memory pressure, which means that the pages we might write
  673. * to the cache from no longer exist - therefore, we can just
  674. * cancel this write operation.
  675. */
  676. spin_unlock(&object->lock);
  677. fscache_op_complete(&op->op, false);
  678. _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
  679. _op->flags, _op->state, object->state->short_name,
  680. object->flags);
  681. return;
  682. }
  683. spin_lock(&cookie->stores_lock);
  684. fscache_stat(&fscache_n_store_calls);
  685. /* find a page to store */
  686. page = NULL;
  687. n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
  688. FSCACHE_COOKIE_PENDING_TAG);
  689. if (n != 1)
  690. goto superseded;
  691. page = results[0];
  692. _debug("gang %d [%lx]", n, page->index);
  693. if (page->index > op->store_limit) {
  694. fscache_stat(&fscache_n_store_pages_over_limit);
  695. goto superseded;
  696. }
  697. radix_tree_tag_set(&cookie->stores, page->index,
  698. FSCACHE_COOKIE_STORING_TAG);
  699. radix_tree_tag_clear(&cookie->stores, page->index,
  700. FSCACHE_COOKIE_PENDING_TAG);
  701. spin_unlock(&cookie->stores_lock);
  702. spin_unlock(&object->lock);
  703. fscache_stat(&fscache_n_store_pages);
  704. fscache_stat(&fscache_n_cop_write_page);
  705. ret = object->cache->ops->write_page(op, page);
  706. fscache_stat_d(&fscache_n_cop_write_page);
  707. fscache_end_page_write(object, page);
  708. if (ret < 0) {
  709. fscache_abort_object(object);
  710. fscache_op_complete(&op->op, true);
  711. } else {
  712. fscache_enqueue_operation(&op->op);
  713. }
  714. _leave("");
  715. return;
  716. superseded:
  717. /* this writer is going away and there aren't any more things to
  718. * write */
  719. _debug("cease");
  720. spin_unlock(&cookie->stores_lock);
  721. clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
  722. spin_unlock(&object->lock);
  723. fscache_op_complete(&op->op, true);
  724. _leave("");
  725. }
  726. /*
  727. * Clear the pages pending writing for invalidation
  728. */
  729. void fscache_invalidate_writes(struct fscache_cookie *cookie)
  730. {
  731. struct page *page;
  732. void *results[16];
  733. int n, i;
  734. _enter("");
  735. for (;;) {
  736. spin_lock(&cookie->stores_lock);
  737. n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
  738. ARRAY_SIZE(results),
  739. FSCACHE_COOKIE_PENDING_TAG);
  740. if (n == 0) {
  741. spin_unlock(&cookie->stores_lock);
  742. break;
  743. }
  744. for (i = n - 1; i >= 0; i--) {
  745. page = results[i];
  746. radix_tree_delete(&cookie->stores, page->index);
  747. }
  748. spin_unlock(&cookie->stores_lock);
  749. for (i = n - 1; i >= 0; i--)
  750. page_cache_release(results[i]);
  751. }
  752. _leave("");
  753. }
  754. /*
  755. * request a page be stored in the cache
  756. * - returns:
  757. * -ENOMEM - out of memory, nothing done
  758. * -ENOBUFS - no backing object available in which to cache the page
  759. * 0 - dispatched a write - it'll call end_io_func() when finished
  760. *
  761. * if the cookie still has a backing object at this point, that object can be
  762. * in one of a few states with respect to storage processing:
  763. *
  764. * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
  765. * set)
  766. *
  767. * (a) no writes yet
  768. *
  769. * (b) writes deferred till post-creation (mark page for writing and
  770. * return immediately)
  771. *
  772. * (2) negative lookup, object created, initial fill being made from netfs
  773. *
  774. * (a) fill point not yet reached this page (mark page for writing and
  775. * return)
  776. *
  777. * (b) fill point passed this page (queue op to store this page)
  778. *
  779. * (3) object extant (queue op to store this page)
  780. *
  781. * any other state is invalid
  782. */
  783. int __fscache_write_page(struct fscache_cookie *cookie,
  784. struct page *page,
  785. gfp_t gfp)
  786. {
  787. struct fscache_storage *op;
  788. struct fscache_object *object;
  789. bool wake_cookie = false;
  790. int ret;
  791. _enter("%p,%x,", cookie, (u32) page->flags);
  792. ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  793. ASSERT(PageFsCache(page));
  794. fscache_stat(&fscache_n_stores);
  795. if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
  796. _leave(" = -ENOBUFS [invalidating]");
  797. return -ENOBUFS;
  798. }
  799. op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
  800. if (!op)
  801. goto nomem;
  802. fscache_operation_init(&op->op, fscache_write_op,
  803. fscache_release_write_op);
  804. op->op.flags = FSCACHE_OP_ASYNC |
  805. (1 << FSCACHE_OP_WAITING) |
  806. (1 << FSCACHE_OP_UNUSE_COOKIE);
  807. ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM);
  808. if (ret < 0)
  809. goto nomem_free;
  810. ret = -ENOBUFS;
  811. spin_lock(&cookie->lock);
  812. if (!fscache_cookie_enabled(cookie) ||
  813. hlist_empty(&cookie->backing_objects))
  814. goto nobufs;
  815. object = hlist_entry(cookie->backing_objects.first,
  816. struct fscache_object, cookie_link);
  817. if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
  818. goto nobufs;
  819. /* add the page to the pending-storage radix tree on the backing
  820. * object */
  821. spin_lock(&object->lock);
  822. spin_lock(&cookie->stores_lock);
  823. _debug("store limit %llx", (unsigned long long) object->store_limit);
  824. ret = radix_tree_insert(&cookie->stores, page->index, page);
  825. if (ret < 0) {
  826. if (ret == -EEXIST)
  827. goto already_queued;
  828. _debug("insert failed %d", ret);
  829. goto nobufs_unlock_obj;
  830. }
  831. radix_tree_tag_set(&cookie->stores, page->index,
  832. FSCACHE_COOKIE_PENDING_TAG);
  833. page_cache_get(page);
  834. /* we only want one writer at a time, but we do need to queue new
  835. * writers after exclusive ops */
  836. if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
  837. goto already_pending;
  838. spin_unlock(&cookie->stores_lock);
  839. spin_unlock(&object->lock);
  840. op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
  841. op->store_limit = object->store_limit;
  842. __fscache_use_cookie(cookie);
  843. if (fscache_submit_op(object, &op->op) < 0)
  844. goto submit_failed;
  845. spin_unlock(&cookie->lock);
  846. radix_tree_preload_end();
  847. fscache_stat(&fscache_n_store_ops);
  848. fscache_stat(&fscache_n_stores_ok);
  849. /* the work queue now carries its own ref on the object */
  850. fscache_put_operation(&op->op);
  851. _leave(" = 0");
  852. return 0;
  853. already_queued:
  854. fscache_stat(&fscache_n_stores_again);
  855. already_pending:
  856. spin_unlock(&cookie->stores_lock);
  857. spin_unlock(&object->lock);
  858. spin_unlock(&cookie->lock);
  859. radix_tree_preload_end();
  860. kfree(op);
  861. fscache_stat(&fscache_n_stores_ok);
  862. _leave(" = 0");
  863. return 0;
  864. submit_failed:
  865. spin_lock(&cookie->stores_lock);
  866. radix_tree_delete(&cookie->stores, page->index);
  867. spin_unlock(&cookie->stores_lock);
  868. wake_cookie = __fscache_unuse_cookie(cookie);
  869. page_cache_release(page);
  870. ret = -ENOBUFS;
  871. goto nobufs;
  872. nobufs_unlock_obj:
  873. spin_unlock(&cookie->stores_lock);
  874. spin_unlock(&object->lock);
  875. nobufs:
  876. spin_unlock(&cookie->lock);
  877. radix_tree_preload_end();
  878. kfree(op);
  879. if (wake_cookie)
  880. __fscache_wake_unused_cookie(cookie);
  881. fscache_stat(&fscache_n_stores_nobufs);
  882. _leave(" = -ENOBUFS");
  883. return -ENOBUFS;
  884. nomem_free:
  885. kfree(op);
  886. nomem:
  887. fscache_stat(&fscache_n_stores_oom);
  888. _leave(" = -ENOMEM");
  889. return -ENOMEM;
  890. }
  891. EXPORT_SYMBOL(__fscache_write_page);
  892. /*
  893. * remove a page from the cache
  894. */
  895. void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
  896. {
  897. struct fscache_object *object;
  898. _enter(",%p", page);
  899. ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  900. ASSERTCMP(page, !=, NULL);
  901. fscache_stat(&fscache_n_uncaches);
  902. /* cache withdrawal may beat us to it */
  903. if (!PageFsCache(page))
  904. goto done;
  905. /* get the object */
  906. spin_lock(&cookie->lock);
  907. if (hlist_empty(&cookie->backing_objects)) {
  908. ClearPageFsCache(page);
  909. goto done_unlock;
  910. }
  911. object = hlist_entry(cookie->backing_objects.first,
  912. struct fscache_object, cookie_link);
  913. /* there might now be stuff on disk we could read */
  914. clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  915. /* only invoke the cache backend if we managed to mark the page
  916. * uncached here; this deals with synchronisation vs withdrawal */
  917. if (TestClearPageFsCache(page) &&
  918. object->cache->ops->uncache_page) {
  919. /* the cache backend releases the cookie lock */
  920. fscache_stat(&fscache_n_cop_uncache_page);
  921. object->cache->ops->uncache_page(object, page);
  922. fscache_stat_d(&fscache_n_cop_uncache_page);
  923. goto done;
  924. }
  925. done_unlock:
  926. spin_unlock(&cookie->lock);
  927. done:
  928. _leave("");
  929. }
  930. EXPORT_SYMBOL(__fscache_uncache_page);
  931. /**
  932. * fscache_mark_page_cached - Mark a page as being cached
  933. * @op: The retrieval op pages are being marked for
  934. * @page: The page to be marked
  935. *
  936. * Mark a netfs page as being cached. After this is called, the netfs
  937. * must call fscache_uncache_page() to remove the mark.
  938. */
  939. void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
  940. {
  941. struct fscache_cookie *cookie = op->op.object->cookie;
  942. #ifdef CONFIG_FSCACHE_STATS
  943. atomic_inc(&fscache_n_marks);
  944. #endif
  945. _debug("- mark %p{%lx}", page, page->index);
  946. if (TestSetPageFsCache(page)) {
  947. static bool once_only;
  948. if (!once_only) {
  949. once_only = true;
  950. pr_warn("Cookie type %s marked page %lx multiple times\n",
  951. cookie->def->name, page->index);
  952. }
  953. }
  954. if (cookie->def->mark_page_cached)
  955. cookie->def->mark_page_cached(cookie->netfs_data,
  956. op->mapping, page);
  957. }
  958. EXPORT_SYMBOL(fscache_mark_page_cached);
  959. /**
  960. * fscache_mark_pages_cached - Mark pages as being cached
  961. * @op: The retrieval op pages are being marked for
  962. * @pagevec: The pages to be marked
  963. *
  964. * Mark a bunch of netfs pages as being cached. After this is called,
  965. * the netfs must call fscache_uncache_page() to remove the mark.
  966. */
  967. void fscache_mark_pages_cached(struct fscache_retrieval *op,
  968. struct pagevec *pagevec)
  969. {
  970. unsigned long loop;
  971. for (loop = 0; loop < pagevec->nr; loop++)
  972. fscache_mark_page_cached(op, pagevec->pages[loop]);
  973. pagevec_reinit(pagevec);
  974. }
  975. EXPORT_SYMBOL(fscache_mark_pages_cached);
  976. /*
  977. * Uncache all the pages in an inode that are marked PG_fscache, assuming them
  978. * to be associated with the given cookie.
  979. */
  980. void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
  981. struct inode *inode)
  982. {
  983. struct address_space *mapping = inode->i_mapping;
  984. struct pagevec pvec;
  985. pgoff_t next;
  986. int i;
  987. _enter("%p,%p", cookie, inode);
  988. if (!mapping || mapping->nrpages == 0) {
  989. _leave(" [no pages]");
  990. return;
  991. }
  992. pagevec_init(&pvec, 0);
  993. next = 0;
  994. do {
  995. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
  996. break;
  997. for (i = 0; i < pagevec_count(&pvec); i++) {
  998. struct page *page = pvec.pages[i];
  999. next = page->index;
  1000. if (PageFsCache(page)) {
  1001. __fscache_wait_on_page_write(cookie, page);
  1002. __fscache_uncache_page(cookie, page);
  1003. }
  1004. }
  1005. pagevec_release(&pvec);
  1006. cond_resched();
  1007. } while (++next);
  1008. _leave("");
  1009. }
  1010. EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);