rdwr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /* Storage object read/write
  2. *
  3. * Copyright (C) 2007 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/mount.h>
  12. #include <linux/slab.h>
  13. #include <linux/file.h>
  14. #include <linux/swap.h>
  15. #include "internal.h"
  16. /*
  17. * detect wake up events generated by the unlocking of pages in which we're
  18. * interested
  19. * - we use this to detect read completion of backing pages
  20. * - the caller holds the waitqueue lock
  21. */
  22. static int cachefiles_read_waiter(wait_queue_entry_t *wait, unsigned mode,
  23. int sync, void *_key)
  24. {
  25. struct cachefiles_one_read *monitor =
  26. container_of(wait, struct cachefiles_one_read, monitor);
  27. struct cachefiles_object *object;
  28. struct wait_bit_key *key = _key;
  29. struct page *page = wait->private;
  30. ASSERT(key);
  31. _enter("{%lu},%u,%d,{%p,%u}",
  32. monitor->netfs_page->index, mode, sync,
  33. key->flags, key->bit_nr);
  34. if (key->flags != &page->flags ||
  35. key->bit_nr != PG_locked)
  36. return 0;
  37. _debug("--- monitor %p %lx ---", page, page->flags);
  38. if (!PageUptodate(page) && !PageError(page)) {
  39. /* unlocked, not uptodate and not erronous? */
  40. _debug("page probably truncated");
  41. }
  42. /* remove from the waitqueue */
  43. list_del(&wait->entry);
  44. /* move onto the action list and queue for FS-Cache thread pool */
  45. ASSERT(monitor->op);
  46. object = container_of(monitor->op->op.object,
  47. struct cachefiles_object, fscache);
  48. spin_lock(&object->work_lock);
  49. list_add_tail(&monitor->op_link, &monitor->op->to_do);
  50. spin_unlock(&object->work_lock);
  51. fscache_enqueue_retrieval(monitor->op);
  52. return 0;
  53. }
  54. /*
  55. * handle a probably truncated page
  56. * - check to see if the page is still relevant and reissue the read if
  57. * possible
  58. * - return -EIO on error, -ENODATA if the page is gone, -EINPROGRESS if we
  59. * must wait again and 0 if successful
  60. */
  61. static int cachefiles_read_reissue(struct cachefiles_object *object,
  62. struct cachefiles_one_read *monitor)
  63. {
  64. struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
  65. struct page *backpage = monitor->back_page, *backpage2;
  66. int ret;
  67. _enter("{ino=%lx},{%lx,%lx}",
  68. d_backing_inode(object->backer)->i_ino,
  69. backpage->index, backpage->flags);
  70. /* skip if the page was truncated away completely */
  71. if (backpage->mapping != bmapping) {
  72. _leave(" = -ENODATA [mapping]");
  73. return -ENODATA;
  74. }
  75. backpage2 = find_get_page(bmapping, backpage->index);
  76. if (!backpage2) {
  77. _leave(" = -ENODATA [gone]");
  78. return -ENODATA;
  79. }
  80. if (backpage != backpage2) {
  81. put_page(backpage2);
  82. _leave(" = -ENODATA [different]");
  83. return -ENODATA;
  84. }
  85. /* the page is still there and we already have a ref on it, so we don't
  86. * need a second */
  87. put_page(backpage2);
  88. INIT_LIST_HEAD(&monitor->op_link);
  89. add_page_wait_queue(backpage, &monitor->monitor);
  90. if (trylock_page(backpage)) {
  91. ret = -EIO;
  92. if (PageError(backpage))
  93. goto unlock_discard;
  94. ret = 0;
  95. if (PageUptodate(backpage))
  96. goto unlock_discard;
  97. _debug("reissue read");
  98. ret = bmapping->a_ops->readpage(NULL, backpage);
  99. if (ret < 0)
  100. goto unlock_discard;
  101. }
  102. /* but the page may have been read before the monitor was installed, so
  103. * the monitor may miss the event - so we have to ensure that we do get
  104. * one in such a case */
  105. if (trylock_page(backpage)) {
  106. _debug("jumpstart %p {%lx}", backpage, backpage->flags);
  107. unlock_page(backpage);
  108. }
  109. /* it'll reappear on the todo list */
  110. _leave(" = -EINPROGRESS");
  111. return -EINPROGRESS;
  112. unlock_discard:
  113. unlock_page(backpage);
  114. spin_lock_irq(&object->work_lock);
  115. list_del(&monitor->op_link);
  116. spin_unlock_irq(&object->work_lock);
  117. _leave(" = %d", ret);
  118. return ret;
  119. }
  120. /*
  121. * copy data from backing pages to netfs pages to complete a read operation
  122. * - driven by FS-Cache's thread pool
  123. */
  124. static void cachefiles_read_copier(struct fscache_operation *_op)
  125. {
  126. struct cachefiles_one_read *monitor;
  127. struct cachefiles_object *object;
  128. struct fscache_retrieval *op;
  129. int error, max;
  130. op = container_of(_op, struct fscache_retrieval, op);
  131. object = container_of(op->op.object,
  132. struct cachefiles_object, fscache);
  133. _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
  134. max = 8;
  135. spin_lock_irq(&object->work_lock);
  136. while (!list_empty(&op->to_do)) {
  137. monitor = list_entry(op->to_do.next,
  138. struct cachefiles_one_read, op_link);
  139. list_del(&monitor->op_link);
  140. spin_unlock_irq(&object->work_lock);
  141. _debug("- copy {%lu}", monitor->back_page->index);
  142. recheck:
  143. if (test_bit(FSCACHE_COOKIE_INVALIDATING,
  144. &object->fscache.cookie->flags)) {
  145. error = -ESTALE;
  146. } else if (PageUptodate(monitor->back_page)) {
  147. copy_highpage(monitor->netfs_page, monitor->back_page);
  148. fscache_mark_page_cached(monitor->op,
  149. monitor->netfs_page);
  150. error = 0;
  151. } else if (!PageError(monitor->back_page)) {
  152. /* the page has probably been truncated */
  153. error = cachefiles_read_reissue(object, monitor);
  154. if (error == -EINPROGRESS)
  155. goto next;
  156. goto recheck;
  157. } else {
  158. cachefiles_io_error_obj(
  159. object,
  160. "Readpage failed on backing file %lx",
  161. (unsigned long) monitor->back_page->flags);
  162. error = -EIO;
  163. }
  164. put_page(monitor->back_page);
  165. fscache_end_io(op, monitor->netfs_page, error);
  166. put_page(monitor->netfs_page);
  167. fscache_retrieval_complete(op, 1);
  168. fscache_put_retrieval(op);
  169. kfree(monitor);
  170. next:
  171. /* let the thread pool have some air occasionally */
  172. max--;
  173. if (max < 0 || need_resched()) {
  174. if (!list_empty(&op->to_do))
  175. fscache_enqueue_retrieval(op);
  176. _leave(" [maxed out]");
  177. return;
  178. }
  179. spin_lock_irq(&object->work_lock);
  180. }
  181. spin_unlock_irq(&object->work_lock);
  182. _leave("");
  183. }
  184. /*
  185. * read the corresponding page to the given set from the backing file
  186. * - an uncertain page is simply discarded, to be tried again another time
  187. */
  188. static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
  189. struct fscache_retrieval *op,
  190. struct page *netpage)
  191. {
  192. struct cachefiles_one_read *monitor;
  193. struct address_space *bmapping;
  194. struct page *newpage, *backpage;
  195. int ret;
  196. _enter("");
  197. _debug("read back %p{%lu,%d}",
  198. netpage, netpage->index, page_count(netpage));
  199. monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
  200. if (!monitor)
  201. goto nomem;
  202. monitor->netfs_page = netpage;
  203. monitor->op = fscache_get_retrieval(op);
  204. init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
  205. /* attempt to get hold of the backing page */
  206. bmapping = d_backing_inode(object->backer)->i_mapping;
  207. newpage = NULL;
  208. for (;;) {
  209. backpage = find_get_page(bmapping, netpage->index);
  210. if (backpage)
  211. goto backing_page_already_present;
  212. if (!newpage) {
  213. newpage = __page_cache_alloc(cachefiles_gfp);
  214. if (!newpage)
  215. goto nomem_monitor;
  216. }
  217. ret = add_to_page_cache_lru(newpage, bmapping,
  218. netpage->index, cachefiles_gfp);
  219. if (ret == 0)
  220. goto installed_new_backing_page;
  221. if (ret != -EEXIST)
  222. goto nomem_page;
  223. }
  224. /* we've installed a new backing page, so now we need to start
  225. * it reading */
  226. installed_new_backing_page:
  227. _debug("- new %p", newpage);
  228. backpage = newpage;
  229. newpage = NULL;
  230. read_backing_page:
  231. ret = bmapping->a_ops->readpage(NULL, backpage);
  232. if (ret < 0)
  233. goto read_error;
  234. /* set the monitor to transfer the data across */
  235. monitor_backing_page:
  236. _debug("- monitor add");
  237. /* install the monitor */
  238. get_page(monitor->netfs_page);
  239. get_page(backpage);
  240. monitor->back_page = backpage;
  241. monitor->monitor.private = backpage;
  242. add_page_wait_queue(backpage, &monitor->monitor);
  243. monitor = NULL;
  244. /* but the page may have been read before the monitor was installed, so
  245. * the monitor may miss the event - so we have to ensure that we do get
  246. * one in such a case */
  247. if (trylock_page(backpage)) {
  248. _debug("jumpstart %p {%lx}", backpage, backpage->flags);
  249. unlock_page(backpage);
  250. }
  251. goto success;
  252. /* if the backing page is already present, it can be in one of
  253. * three states: read in progress, read failed or read okay */
  254. backing_page_already_present:
  255. _debug("- present");
  256. if (newpage) {
  257. put_page(newpage);
  258. newpage = NULL;
  259. }
  260. if (PageError(backpage))
  261. goto io_error;
  262. if (PageUptodate(backpage))
  263. goto backing_page_already_uptodate;
  264. if (!trylock_page(backpage))
  265. goto monitor_backing_page;
  266. _debug("read %p {%lx}", backpage, backpage->flags);
  267. goto read_backing_page;
  268. /* the backing page is already up to date, attach the netfs
  269. * page to the pagecache and LRU and copy the data across */
  270. backing_page_already_uptodate:
  271. _debug("- uptodate");
  272. fscache_mark_page_cached(op, netpage);
  273. copy_highpage(netpage, backpage);
  274. fscache_end_io(op, netpage, 0);
  275. fscache_retrieval_complete(op, 1);
  276. success:
  277. _debug("success");
  278. ret = 0;
  279. out:
  280. if (backpage)
  281. put_page(backpage);
  282. if (monitor) {
  283. fscache_put_retrieval(monitor->op);
  284. kfree(monitor);
  285. }
  286. _leave(" = %d", ret);
  287. return ret;
  288. read_error:
  289. _debug("read error %d", ret);
  290. if (ret == -ENOMEM) {
  291. fscache_retrieval_complete(op, 1);
  292. goto out;
  293. }
  294. io_error:
  295. cachefiles_io_error_obj(object, "Page read error on backing file");
  296. fscache_retrieval_complete(op, 1);
  297. ret = -ENOBUFS;
  298. goto out;
  299. nomem_page:
  300. put_page(newpage);
  301. nomem_monitor:
  302. fscache_put_retrieval(monitor->op);
  303. kfree(monitor);
  304. nomem:
  305. fscache_retrieval_complete(op, 1);
  306. _leave(" = -ENOMEM");
  307. return -ENOMEM;
  308. }
  309. /*
  310. * read a page from the cache or allocate a block in which to store it
  311. * - cache withdrawal is prevented by the caller
  312. * - returns -EINTR if interrupted
  313. * - returns -ENOMEM if ran out of memory
  314. * - returns -ENOBUFS if no buffers can be made available
  315. * - returns -ENOBUFS if page is beyond EOF
  316. * - if the page is backed by a block in the cache:
  317. * - a read will be started which will call the callback on completion
  318. * - 0 will be returned
  319. * - else if the page is unbacked:
  320. * - the metadata will be retained
  321. * - -ENODATA will be returned
  322. */
  323. int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
  324. struct page *page,
  325. gfp_t gfp)
  326. {
  327. struct cachefiles_object *object;
  328. struct cachefiles_cache *cache;
  329. struct inode *inode;
  330. sector_t block0, block;
  331. unsigned shift;
  332. int ret;
  333. object = container_of(op->op.object,
  334. struct cachefiles_object, fscache);
  335. cache = container_of(object->fscache.cache,
  336. struct cachefiles_cache, cache);
  337. _enter("{%p},{%lx},,,", object, page->index);
  338. if (!object->backer)
  339. goto enobufs;
  340. inode = d_backing_inode(object->backer);
  341. ASSERT(S_ISREG(inode->i_mode));
  342. ASSERT(inode->i_mapping->a_ops->bmap);
  343. ASSERT(inode->i_mapping->a_ops->readpages);
  344. /* calculate the shift required to use bmap */
  345. shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
  346. op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
  347. op->op.flags |= FSCACHE_OP_ASYNC;
  348. op->op.processor = cachefiles_read_copier;
  349. /* we assume the absence or presence of the first block is a good
  350. * enough indication for the page as a whole
  351. * - TODO: don't use bmap() for this as it is _not_ actually good
  352. * enough for this as it doesn't indicate errors, but it's all we've
  353. * got for the moment
  354. */
  355. block0 = page->index;
  356. block0 <<= shift;
  357. block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
  358. _debug("%llx -> %llx",
  359. (unsigned long long) block0,
  360. (unsigned long long) block);
  361. if (block) {
  362. /* submit the apparently valid page to the backing fs to be
  363. * read from disk */
  364. ret = cachefiles_read_backing_file_one(object, op, page);
  365. } else if (cachefiles_has_space(cache, 0, 1) == 0) {
  366. /* there's space in the cache we can use */
  367. fscache_mark_page_cached(op, page);
  368. fscache_retrieval_complete(op, 1);
  369. ret = -ENODATA;
  370. } else {
  371. goto enobufs;
  372. }
  373. _leave(" = %d", ret);
  374. return ret;
  375. enobufs:
  376. fscache_retrieval_complete(op, 1);
  377. _leave(" = -ENOBUFS");
  378. return -ENOBUFS;
  379. }
  380. /*
  381. * read the corresponding pages to the given set from the backing file
  382. * - any uncertain pages are simply discarded, to be tried again another time
  383. */
  384. static int cachefiles_read_backing_file(struct cachefiles_object *object,
  385. struct fscache_retrieval *op,
  386. struct list_head *list)
  387. {
  388. struct cachefiles_one_read *monitor = NULL;
  389. struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
  390. struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
  391. int ret = 0;
  392. _enter("");
  393. list_for_each_entry_safe(netpage, _n, list, lru) {
  394. list_del(&netpage->lru);
  395. _debug("read back %p{%lu,%d}",
  396. netpage, netpage->index, page_count(netpage));
  397. if (!monitor) {
  398. monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
  399. if (!monitor)
  400. goto nomem;
  401. monitor->op = fscache_get_retrieval(op);
  402. init_waitqueue_func_entry(&monitor->monitor,
  403. cachefiles_read_waiter);
  404. }
  405. for (;;) {
  406. backpage = find_get_page(bmapping, netpage->index);
  407. if (backpage)
  408. goto backing_page_already_present;
  409. if (!newpage) {
  410. newpage = __page_cache_alloc(cachefiles_gfp);
  411. if (!newpage)
  412. goto nomem;
  413. }
  414. ret = add_to_page_cache_lru(newpage, bmapping,
  415. netpage->index,
  416. cachefiles_gfp);
  417. if (ret == 0)
  418. goto installed_new_backing_page;
  419. if (ret != -EEXIST)
  420. goto nomem;
  421. }
  422. /* we've installed a new backing page, so now we need
  423. * to start it reading */
  424. installed_new_backing_page:
  425. _debug("- new %p", newpage);
  426. backpage = newpage;
  427. newpage = NULL;
  428. reread_backing_page:
  429. ret = bmapping->a_ops->readpage(NULL, backpage);
  430. if (ret < 0)
  431. goto read_error;
  432. /* add the netfs page to the pagecache and LRU, and set the
  433. * monitor to transfer the data across */
  434. monitor_backing_page:
  435. _debug("- monitor add");
  436. ret = add_to_page_cache_lru(netpage, op->mapping,
  437. netpage->index, cachefiles_gfp);
  438. if (ret < 0) {
  439. if (ret == -EEXIST) {
  440. put_page(netpage);
  441. fscache_retrieval_complete(op, 1);
  442. continue;
  443. }
  444. goto nomem;
  445. }
  446. /* install a monitor */
  447. get_page(netpage);
  448. monitor->netfs_page = netpage;
  449. get_page(backpage);
  450. monitor->back_page = backpage;
  451. monitor->monitor.private = backpage;
  452. add_page_wait_queue(backpage, &monitor->monitor);
  453. monitor = NULL;
  454. /* but the page may have been read before the monitor was
  455. * installed, so the monitor may miss the event - so we have to
  456. * ensure that we do get one in such a case */
  457. if (trylock_page(backpage)) {
  458. _debug("2unlock %p {%lx}", backpage, backpage->flags);
  459. unlock_page(backpage);
  460. }
  461. put_page(backpage);
  462. backpage = NULL;
  463. put_page(netpage);
  464. netpage = NULL;
  465. continue;
  466. /* if the backing page is already present, it can be in one of
  467. * three states: read in progress, read failed or read okay */
  468. backing_page_already_present:
  469. _debug("- present %p", backpage);
  470. if (PageError(backpage))
  471. goto io_error;
  472. if (PageUptodate(backpage))
  473. goto backing_page_already_uptodate;
  474. _debug("- not ready %p{%lx}", backpage, backpage->flags);
  475. if (!trylock_page(backpage))
  476. goto monitor_backing_page;
  477. if (PageError(backpage)) {
  478. _debug("error %lx", backpage->flags);
  479. unlock_page(backpage);
  480. goto io_error;
  481. }
  482. if (PageUptodate(backpage))
  483. goto backing_page_already_uptodate_unlock;
  484. /* we've locked a page that's neither up to date nor erroneous,
  485. * so we need to attempt to read it again */
  486. goto reread_backing_page;
  487. /* the backing page is already up to date, attach the netfs
  488. * page to the pagecache and LRU and copy the data across */
  489. backing_page_already_uptodate_unlock:
  490. _debug("uptodate %lx", backpage->flags);
  491. unlock_page(backpage);
  492. backing_page_already_uptodate:
  493. _debug("- uptodate");
  494. ret = add_to_page_cache_lru(netpage, op->mapping,
  495. netpage->index, cachefiles_gfp);
  496. if (ret < 0) {
  497. if (ret == -EEXIST) {
  498. put_page(netpage);
  499. fscache_retrieval_complete(op, 1);
  500. continue;
  501. }
  502. goto nomem;
  503. }
  504. copy_highpage(netpage, backpage);
  505. put_page(backpage);
  506. backpage = NULL;
  507. fscache_mark_page_cached(op, netpage);
  508. /* the netpage is unlocked and marked up to date here */
  509. fscache_end_io(op, netpage, 0);
  510. put_page(netpage);
  511. netpage = NULL;
  512. fscache_retrieval_complete(op, 1);
  513. continue;
  514. }
  515. netpage = NULL;
  516. _debug("out");
  517. out:
  518. /* tidy up */
  519. if (newpage)
  520. put_page(newpage);
  521. if (netpage)
  522. put_page(netpage);
  523. if (backpage)
  524. put_page(backpage);
  525. if (monitor) {
  526. fscache_put_retrieval(op);
  527. kfree(monitor);
  528. }
  529. list_for_each_entry_safe(netpage, _n, list, lru) {
  530. list_del(&netpage->lru);
  531. put_page(netpage);
  532. fscache_retrieval_complete(op, 1);
  533. }
  534. _leave(" = %d", ret);
  535. return ret;
  536. nomem:
  537. _debug("nomem");
  538. ret = -ENOMEM;
  539. goto record_page_complete;
  540. read_error:
  541. _debug("read error %d", ret);
  542. if (ret == -ENOMEM)
  543. goto record_page_complete;
  544. io_error:
  545. cachefiles_io_error_obj(object, "Page read error on backing file");
  546. ret = -ENOBUFS;
  547. record_page_complete:
  548. fscache_retrieval_complete(op, 1);
  549. goto out;
  550. }
  551. /*
  552. * read a list of pages from the cache or allocate blocks in which to store
  553. * them
  554. */
  555. int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
  556. struct list_head *pages,
  557. unsigned *nr_pages,
  558. gfp_t gfp)
  559. {
  560. struct cachefiles_object *object;
  561. struct cachefiles_cache *cache;
  562. struct list_head backpages;
  563. struct pagevec pagevec;
  564. struct inode *inode;
  565. struct page *page, *_n;
  566. unsigned shift, nrbackpages;
  567. int ret, ret2, space;
  568. object = container_of(op->op.object,
  569. struct cachefiles_object, fscache);
  570. cache = container_of(object->fscache.cache,
  571. struct cachefiles_cache, cache);
  572. _enter("{OBJ%x,%d},,%d,,",
  573. object->fscache.debug_id, atomic_read(&op->op.usage),
  574. *nr_pages);
  575. if (!object->backer)
  576. goto all_enobufs;
  577. space = 1;
  578. if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
  579. space = 0;
  580. inode = d_backing_inode(object->backer);
  581. ASSERT(S_ISREG(inode->i_mode));
  582. ASSERT(inode->i_mapping->a_ops->bmap);
  583. ASSERT(inode->i_mapping->a_ops->readpages);
  584. /* calculate the shift required to use bmap */
  585. shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
  586. pagevec_init(&pagevec);
  587. op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
  588. op->op.flags |= FSCACHE_OP_ASYNC;
  589. op->op.processor = cachefiles_read_copier;
  590. INIT_LIST_HEAD(&backpages);
  591. nrbackpages = 0;
  592. ret = space ? -ENODATA : -ENOBUFS;
  593. list_for_each_entry_safe(page, _n, pages, lru) {
  594. sector_t block0, block;
  595. /* we assume the absence or presence of the first block is a
  596. * good enough indication for the page as a whole
  597. * - TODO: don't use bmap() for this as it is _not_ actually
  598. * good enough for this as it doesn't indicate errors, but
  599. * it's all we've got for the moment
  600. */
  601. block0 = page->index;
  602. block0 <<= shift;
  603. block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
  604. block0);
  605. _debug("%llx -> %llx",
  606. (unsigned long long) block0,
  607. (unsigned long long) block);
  608. if (block) {
  609. /* we have data - add it to the list to give to the
  610. * backing fs */
  611. list_move(&page->lru, &backpages);
  612. (*nr_pages)--;
  613. nrbackpages++;
  614. } else if (space && pagevec_add(&pagevec, page) == 0) {
  615. fscache_mark_pages_cached(op, &pagevec);
  616. fscache_retrieval_complete(op, 1);
  617. ret = -ENODATA;
  618. } else {
  619. fscache_retrieval_complete(op, 1);
  620. }
  621. }
  622. if (pagevec_count(&pagevec) > 0)
  623. fscache_mark_pages_cached(op, &pagevec);
  624. if (list_empty(pages))
  625. ret = 0;
  626. /* submit the apparently valid pages to the backing fs to be read from
  627. * disk */
  628. if (nrbackpages > 0) {
  629. ret2 = cachefiles_read_backing_file(object, op, &backpages);
  630. if (ret2 == -ENOMEM || ret2 == -EINTR)
  631. ret = ret2;
  632. }
  633. _leave(" = %d [nr=%u%s]",
  634. ret, *nr_pages, list_empty(pages) ? " empty" : "");
  635. return ret;
  636. all_enobufs:
  637. fscache_retrieval_complete(op, *nr_pages);
  638. return -ENOBUFS;
  639. }
  640. /*
  641. * allocate a block in the cache in which to store a page
  642. * - cache withdrawal is prevented by the caller
  643. * - returns -EINTR if interrupted
  644. * - returns -ENOMEM if ran out of memory
  645. * - returns -ENOBUFS if no buffers can be made available
  646. * - returns -ENOBUFS if page is beyond EOF
  647. * - otherwise:
  648. * - the metadata will be retained
  649. * - 0 will be returned
  650. */
  651. int cachefiles_allocate_page(struct fscache_retrieval *op,
  652. struct page *page,
  653. gfp_t gfp)
  654. {
  655. struct cachefiles_object *object;
  656. struct cachefiles_cache *cache;
  657. int ret;
  658. object = container_of(op->op.object,
  659. struct cachefiles_object, fscache);
  660. cache = container_of(object->fscache.cache,
  661. struct cachefiles_cache, cache);
  662. _enter("%p,{%lx},", object, page->index);
  663. ret = cachefiles_has_space(cache, 0, 1);
  664. if (ret == 0)
  665. fscache_mark_page_cached(op, page);
  666. else
  667. ret = -ENOBUFS;
  668. fscache_retrieval_complete(op, 1);
  669. _leave(" = %d", ret);
  670. return ret;
  671. }
  672. /*
  673. * allocate blocks in the cache in which to store a set of pages
  674. * - cache withdrawal is prevented by the caller
  675. * - returns -EINTR if interrupted
  676. * - returns -ENOMEM if ran out of memory
  677. * - returns -ENOBUFS if some buffers couldn't be made available
  678. * - returns -ENOBUFS if some pages are beyond EOF
  679. * - otherwise:
  680. * - -ENODATA will be returned
  681. * - metadata will be retained for any page marked
  682. */
  683. int cachefiles_allocate_pages(struct fscache_retrieval *op,
  684. struct list_head *pages,
  685. unsigned *nr_pages,
  686. gfp_t gfp)
  687. {
  688. struct cachefiles_object *object;
  689. struct cachefiles_cache *cache;
  690. struct pagevec pagevec;
  691. struct page *page;
  692. int ret;
  693. object = container_of(op->op.object,
  694. struct cachefiles_object, fscache);
  695. cache = container_of(object->fscache.cache,
  696. struct cachefiles_cache, cache);
  697. _enter("%p,,,%d,", object, *nr_pages);
  698. ret = cachefiles_has_space(cache, 0, *nr_pages);
  699. if (ret == 0) {
  700. pagevec_init(&pagevec);
  701. list_for_each_entry(page, pages, lru) {
  702. if (pagevec_add(&pagevec, page) == 0)
  703. fscache_mark_pages_cached(op, &pagevec);
  704. }
  705. if (pagevec_count(&pagevec) > 0)
  706. fscache_mark_pages_cached(op, &pagevec);
  707. ret = -ENODATA;
  708. } else {
  709. ret = -ENOBUFS;
  710. }
  711. fscache_retrieval_complete(op, *nr_pages);
  712. _leave(" = %d", ret);
  713. return ret;
  714. }
  715. /*
  716. * request a page be stored in the cache
  717. * - cache withdrawal is prevented by the caller
  718. * - this request may be ignored if there's no cache block available, in which
  719. * case -ENOBUFS will be returned
  720. * - if the op is in progress, 0 will be returned
  721. */
  722. int cachefiles_write_page(struct fscache_storage *op, struct page *page)
  723. {
  724. struct cachefiles_object *object;
  725. struct cachefiles_cache *cache;
  726. struct file *file;
  727. struct path path;
  728. loff_t pos, eof;
  729. size_t len;
  730. void *data;
  731. int ret = -ENOBUFS;
  732. ASSERT(op != NULL);
  733. ASSERT(page != NULL);
  734. object = container_of(op->op.object,
  735. struct cachefiles_object, fscache);
  736. _enter("%p,%p{%lx},,,", object, page, page->index);
  737. if (!object->backer) {
  738. _leave(" = -ENOBUFS");
  739. return -ENOBUFS;
  740. }
  741. ASSERT(d_is_reg(object->backer));
  742. cache = container_of(object->fscache.cache,
  743. struct cachefiles_cache, cache);
  744. pos = (loff_t)page->index << PAGE_SHIFT;
  745. /* We mustn't write more data than we have, so we have to beware of a
  746. * partial page at EOF.
  747. */
  748. eof = object->fscache.store_limit_l;
  749. if (pos >= eof)
  750. goto error;
  751. /* write the page to the backing filesystem and let it store it in its
  752. * own time */
  753. path.mnt = cache->mnt;
  754. path.dentry = object->backer;
  755. file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
  756. if (IS_ERR(file)) {
  757. ret = PTR_ERR(file);
  758. goto error_2;
  759. }
  760. len = PAGE_SIZE;
  761. if (eof & ~PAGE_MASK) {
  762. if (eof - pos < PAGE_SIZE) {
  763. _debug("cut short %llx to %llx",
  764. pos, eof);
  765. len = eof - pos;
  766. ASSERTCMP(pos + len, ==, eof);
  767. }
  768. }
  769. data = kmap(page);
  770. ret = __kernel_write(file, data, len, &pos);
  771. kunmap(page);
  772. fput(file);
  773. if (ret != len)
  774. goto error_eio;
  775. _leave(" = 0");
  776. return 0;
  777. error_eio:
  778. ret = -EIO;
  779. error_2:
  780. if (ret == -EIO)
  781. cachefiles_io_error_obj(object,
  782. "Write page to backing file failed");
  783. error:
  784. _leave(" = -ENOBUFS [%d]", ret);
  785. return -ENOBUFS;
  786. }
  787. /*
  788. * detach a backing block from a page
  789. * - cache withdrawal is prevented by the caller
  790. */
  791. void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
  792. __releases(&object->fscache.cookie->lock)
  793. {
  794. struct cachefiles_object *object;
  795. struct cachefiles_cache *cache;
  796. object = container_of(_object, struct cachefiles_object, fscache);
  797. cache = container_of(object->fscache.cache,
  798. struct cachefiles_cache, cache);
  799. _enter("%p,{%lu}", object, page->index);
  800. spin_unlock(&object->fscache.cookie->lock);
  801. }