page.c 30 KB

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