page.c 28 KB

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