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