page.c 26 KB

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