page.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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. do {
  819. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
  820. break;
  821. for (i = 0; i < pagevec_count(&pvec); i++) {
  822. struct page *page = pvec.pages[i];
  823. next = page->index;
  824. if (PageFsCache(page)) {
  825. __fscache_wait_on_page_write(cookie, page);
  826. __fscache_uncache_page(cookie, page);
  827. }
  828. }
  829. pagevec_release(&pvec);
  830. cond_resched();
  831. } while (++next);
  832. _leave("");
  833. }
  834. EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);