page.c 26 KB

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