page.c 26 KB

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