rdwr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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_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->task_list);
  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. __GFP_COLD);
  215. if (!newpage)
  216. goto nomem_monitor;
  217. }
  218. ret = add_to_page_cache_lru(newpage, bmapping,
  219. netpage->index, cachefiles_gfp);
  220. if (ret == 0)
  221. goto installed_new_backing_page;
  222. if (ret != -EEXIST)
  223. goto nomem_page;
  224. }
  225. /* we've installed a new backing page, so now we need to start
  226. * it reading */
  227. installed_new_backing_page:
  228. _debug("- new %p", newpage);
  229. backpage = newpage;
  230. newpage = NULL;
  231. read_backing_page:
  232. ret = bmapping->a_ops->readpage(NULL, backpage);
  233. if (ret < 0)
  234. goto read_error;
  235. /* set the monitor to transfer the data across */
  236. monitor_backing_page:
  237. _debug("- monitor add");
  238. /* install the monitor */
  239. get_page(monitor->netfs_page);
  240. get_page(backpage);
  241. monitor->back_page = backpage;
  242. monitor->monitor.private = backpage;
  243. add_page_wait_queue(backpage, &monitor->monitor);
  244. monitor = NULL;
  245. /* but the page may have been read before the monitor was installed, so
  246. * the monitor may miss the event - so we have to ensure that we do get
  247. * one in such a case */
  248. if (trylock_page(backpage)) {
  249. _debug("jumpstart %p {%lx}", backpage, backpage->flags);
  250. unlock_page(backpage);
  251. }
  252. goto success;
  253. /* if the backing page is already present, it can be in one of
  254. * three states: read in progress, read failed or read okay */
  255. backing_page_already_present:
  256. _debug("- present");
  257. if (newpage) {
  258. put_page(newpage);
  259. newpage = NULL;
  260. }
  261. if (PageError(backpage))
  262. goto io_error;
  263. if (PageUptodate(backpage))
  264. goto backing_page_already_uptodate;
  265. if (!trylock_page(backpage))
  266. goto monitor_backing_page;
  267. _debug("read %p {%lx}", backpage, backpage->flags);
  268. goto read_backing_page;
  269. /* the backing page is already up to date, attach the netfs
  270. * page to the pagecache and LRU and copy the data across */
  271. backing_page_already_uptodate:
  272. _debug("- uptodate");
  273. fscache_mark_page_cached(op, netpage);
  274. copy_highpage(netpage, backpage);
  275. fscache_end_io(op, netpage, 0);
  276. fscache_retrieval_complete(op, 1);
  277. success:
  278. _debug("success");
  279. ret = 0;
  280. out:
  281. if (backpage)
  282. put_page(backpage);
  283. if (monitor) {
  284. fscache_put_retrieval(monitor->op);
  285. kfree(monitor);
  286. }
  287. _leave(" = %d", ret);
  288. return ret;
  289. read_error:
  290. _debug("read error %d", ret);
  291. if (ret == -ENOMEM) {
  292. fscache_retrieval_complete(op, 1);
  293. goto out;
  294. }
  295. io_error:
  296. cachefiles_io_error_obj(object, "Page read error on backing file");
  297. fscache_retrieval_complete(op, 1);
  298. ret = -ENOBUFS;
  299. goto out;
  300. nomem_page:
  301. put_page(newpage);
  302. nomem_monitor:
  303. fscache_put_retrieval(monitor->op);
  304. kfree(monitor);
  305. nomem:
  306. fscache_retrieval_complete(op, 1);
  307. _leave(" = -ENOMEM");
  308. return -ENOMEM;
  309. }
  310. /*
  311. * read a page from the cache or allocate a block in which to store it
  312. * - cache withdrawal is prevented by the caller
  313. * - returns -EINTR if interrupted
  314. * - returns -ENOMEM if ran out of memory
  315. * - returns -ENOBUFS if no buffers can be made available
  316. * - returns -ENOBUFS if page is beyond EOF
  317. * - if the page is backed by a block in the cache:
  318. * - a read will be started which will call the callback on completion
  319. * - 0 will be returned
  320. * - else if the page is unbacked:
  321. * - the metadata will be retained
  322. * - -ENODATA will be returned
  323. */
  324. int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
  325. struct page *page,
  326. gfp_t gfp)
  327. {
  328. struct cachefiles_object *object;
  329. struct cachefiles_cache *cache;
  330. struct inode *inode;
  331. sector_t block0, block;
  332. unsigned shift;
  333. int ret;
  334. object = container_of(op->op.object,
  335. struct cachefiles_object, fscache);
  336. cache = container_of(object->fscache.cache,
  337. struct cachefiles_cache, cache);
  338. _enter("{%p},{%lx},,,", object, page->index);
  339. if (!object->backer)
  340. goto enobufs;
  341. inode = d_backing_inode(object->backer);
  342. ASSERT(S_ISREG(inode->i_mode));
  343. ASSERT(inode->i_mapping->a_ops->bmap);
  344. ASSERT(inode->i_mapping->a_ops->readpages);
  345. /* calculate the shift required to use bmap */
  346. shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
  347. op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
  348. op->op.flags |= FSCACHE_OP_ASYNC;
  349. op->op.processor = cachefiles_read_copier;
  350. /* we assume the absence or presence of the first block is a good
  351. * enough indication for the page as a whole
  352. * - TODO: don't use bmap() for this as it is _not_ actually good
  353. * enough for this as it doesn't indicate errors, but it's all we've
  354. * got for the moment
  355. */
  356. block0 = page->index;
  357. block0 <<= shift;
  358. block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
  359. _debug("%llx -> %llx",
  360. (unsigned long long) block0,
  361. (unsigned long long) block);
  362. if (block) {
  363. /* submit the apparently valid page to the backing fs to be
  364. * read from disk */
  365. ret = cachefiles_read_backing_file_one(object, op, page);
  366. } else if (cachefiles_has_space(cache, 0, 1) == 0) {
  367. /* there's space in the cache we can use */
  368. fscache_mark_page_cached(op, page);
  369. fscache_retrieval_complete(op, 1);
  370. ret = -ENODATA;
  371. } else {
  372. goto enobufs;
  373. }
  374. _leave(" = %d", ret);
  375. return ret;
  376. enobufs:
  377. fscache_retrieval_complete(op, 1);
  378. _leave(" = -ENOBUFS");
  379. return -ENOBUFS;
  380. }
  381. /*
  382. * read the corresponding pages to the given set from the backing file
  383. * - any uncertain pages are simply discarded, to be tried again another time
  384. */
  385. static int cachefiles_read_backing_file(struct cachefiles_object *object,
  386. struct fscache_retrieval *op,
  387. struct list_head *list)
  388. {
  389. struct cachefiles_one_read *monitor = NULL;
  390. struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
  391. struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
  392. int ret = 0;
  393. _enter("");
  394. list_for_each_entry_safe(netpage, _n, list, lru) {
  395. list_del(&netpage->lru);
  396. _debug("read back %p{%lu,%d}",
  397. netpage, netpage->index, page_count(netpage));
  398. if (!monitor) {
  399. monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
  400. if (!monitor)
  401. goto nomem;
  402. monitor->op = fscache_get_retrieval(op);
  403. init_waitqueue_func_entry(&monitor->monitor,
  404. cachefiles_read_waiter);
  405. }
  406. for (;;) {
  407. backpage = find_get_page(bmapping, netpage->index);
  408. if (backpage)
  409. goto backing_page_already_present;
  410. if (!newpage) {
  411. newpage = __page_cache_alloc(cachefiles_gfp |
  412. __GFP_COLD);
  413. if (!newpage)
  414. goto nomem;
  415. }
  416. ret = add_to_page_cache_lru(newpage, bmapping,
  417. netpage->index,
  418. cachefiles_gfp);
  419. if (ret == 0)
  420. goto installed_new_backing_page;
  421. if (ret != -EEXIST)
  422. goto nomem;
  423. }
  424. /* we've installed a new backing page, so now we need
  425. * to start it reading */
  426. installed_new_backing_page:
  427. _debug("- new %p", newpage);
  428. backpage = newpage;
  429. newpage = NULL;
  430. reread_backing_page:
  431. ret = bmapping->a_ops->readpage(NULL, backpage);
  432. if (ret < 0)
  433. goto read_error;
  434. /* add the netfs page to the pagecache and LRU, and set the
  435. * monitor to transfer the data across */
  436. monitor_backing_page:
  437. _debug("- monitor add");
  438. ret = add_to_page_cache_lru(netpage, op->mapping,
  439. netpage->index, cachefiles_gfp);
  440. if (ret < 0) {
  441. if (ret == -EEXIST) {
  442. put_page(netpage);
  443. fscache_retrieval_complete(op, 1);
  444. continue;
  445. }
  446. goto nomem;
  447. }
  448. /* install a monitor */
  449. get_page(netpage);
  450. monitor->netfs_page = netpage;
  451. get_page(backpage);
  452. monitor->back_page = backpage;
  453. monitor->monitor.private = backpage;
  454. add_page_wait_queue(backpage, &monitor->monitor);
  455. monitor = NULL;
  456. /* but the page may have been read before the monitor was
  457. * installed, so the monitor may miss the event - so we have to
  458. * ensure that we do get one in such a case */
  459. if (trylock_page(backpage)) {
  460. _debug("2unlock %p {%lx}", backpage, backpage->flags);
  461. unlock_page(backpage);
  462. }
  463. put_page(backpage);
  464. backpage = NULL;
  465. put_page(netpage);
  466. netpage = NULL;
  467. continue;
  468. /* if the backing page is already present, it can be in one of
  469. * three states: read in progress, read failed or read okay */
  470. backing_page_already_present:
  471. _debug("- present %p", backpage);
  472. if (PageError(backpage))
  473. goto io_error;
  474. if (PageUptodate(backpage))
  475. goto backing_page_already_uptodate;
  476. _debug("- not ready %p{%lx}", backpage, backpage->flags);
  477. if (!trylock_page(backpage))
  478. goto monitor_backing_page;
  479. if (PageError(backpage)) {
  480. _debug("error %lx", backpage->flags);
  481. unlock_page(backpage);
  482. goto io_error;
  483. }
  484. if (PageUptodate(backpage))
  485. goto backing_page_already_uptodate_unlock;
  486. /* we've locked a page that's neither up to date nor erroneous,
  487. * so we need to attempt to read it again */
  488. goto reread_backing_page;
  489. /* the backing page is already up to date, attach the netfs
  490. * page to the pagecache and LRU and copy the data across */
  491. backing_page_already_uptodate_unlock:
  492. _debug("uptodate %lx", backpage->flags);
  493. unlock_page(backpage);
  494. backing_page_already_uptodate:
  495. _debug("- uptodate");
  496. ret = add_to_page_cache_lru(netpage, op->mapping,
  497. netpage->index, cachefiles_gfp);
  498. if (ret < 0) {
  499. if (ret == -EEXIST) {
  500. put_page(netpage);
  501. fscache_retrieval_complete(op, 1);
  502. continue;
  503. }
  504. goto nomem;
  505. }
  506. copy_highpage(netpage, backpage);
  507. put_page(backpage);
  508. backpage = NULL;
  509. fscache_mark_page_cached(op, netpage);
  510. /* the netpage is unlocked and marked up to date here */
  511. fscache_end_io(op, netpage, 0);
  512. put_page(netpage);
  513. netpage = NULL;
  514. fscache_retrieval_complete(op, 1);
  515. continue;
  516. }
  517. netpage = NULL;
  518. _debug("out");
  519. out:
  520. /* tidy up */
  521. if (newpage)
  522. put_page(newpage);
  523. if (netpage)
  524. put_page(netpage);
  525. if (backpage)
  526. put_page(backpage);
  527. if (monitor) {
  528. fscache_put_retrieval(op);
  529. kfree(monitor);
  530. }
  531. list_for_each_entry_safe(netpage, _n, list, lru) {
  532. list_del(&netpage->lru);
  533. put_page(netpage);
  534. fscache_retrieval_complete(op, 1);
  535. }
  536. _leave(" = %d", ret);
  537. return ret;
  538. nomem:
  539. _debug("nomem");
  540. ret = -ENOMEM;
  541. goto record_page_complete;
  542. read_error:
  543. _debug("read error %d", ret);
  544. if (ret == -ENOMEM)
  545. goto record_page_complete;
  546. io_error:
  547. cachefiles_io_error_obj(object, "Page read error on backing file");
  548. ret = -ENOBUFS;
  549. record_page_complete:
  550. fscache_retrieval_complete(op, 1);
  551. goto out;
  552. }
  553. /*
  554. * read a list of pages from the cache or allocate blocks in which to store
  555. * them
  556. */
  557. int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
  558. struct list_head *pages,
  559. unsigned *nr_pages,
  560. gfp_t gfp)
  561. {
  562. struct cachefiles_object *object;
  563. struct cachefiles_cache *cache;
  564. struct list_head backpages;
  565. struct pagevec pagevec;
  566. struct inode *inode;
  567. struct page *page, *_n;
  568. unsigned shift, nrbackpages;
  569. int ret, ret2, space;
  570. object = container_of(op->op.object,
  571. struct cachefiles_object, fscache);
  572. cache = container_of(object->fscache.cache,
  573. struct cachefiles_cache, cache);
  574. _enter("{OBJ%x,%d},,%d,,",
  575. object->fscache.debug_id, atomic_read(&op->op.usage),
  576. *nr_pages);
  577. if (!object->backer)
  578. goto all_enobufs;
  579. space = 1;
  580. if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
  581. space = 0;
  582. inode = d_backing_inode(object->backer);
  583. ASSERT(S_ISREG(inode->i_mode));
  584. ASSERT(inode->i_mapping->a_ops->bmap);
  585. ASSERT(inode->i_mapping->a_ops->readpages);
  586. /* calculate the shift required to use bmap */
  587. shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
  588. pagevec_init(&pagevec, 0);
  589. op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
  590. op->op.flags |= FSCACHE_OP_ASYNC;
  591. op->op.processor = cachefiles_read_copier;
  592. INIT_LIST_HEAD(&backpages);
  593. nrbackpages = 0;
  594. ret = space ? -ENODATA : -ENOBUFS;
  595. list_for_each_entry_safe(page, _n, pages, lru) {
  596. sector_t block0, block;
  597. /* we assume the absence or presence of the first block is a
  598. * good enough indication for the page as a whole
  599. * - TODO: don't use bmap() for this as it is _not_ actually
  600. * good enough for this as it doesn't indicate errors, but
  601. * it's all we've got for the moment
  602. */
  603. block0 = page->index;
  604. block0 <<= shift;
  605. block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
  606. block0);
  607. _debug("%llx -> %llx",
  608. (unsigned long long) block0,
  609. (unsigned long long) block);
  610. if (block) {
  611. /* we have data - add it to the list to give to the
  612. * backing fs */
  613. list_move(&page->lru, &backpages);
  614. (*nr_pages)--;
  615. nrbackpages++;
  616. } else if (space && pagevec_add(&pagevec, page) == 0) {
  617. fscache_mark_pages_cached(op, &pagevec);
  618. fscache_retrieval_complete(op, 1);
  619. ret = -ENODATA;
  620. } else {
  621. fscache_retrieval_complete(op, 1);
  622. }
  623. }
  624. if (pagevec_count(&pagevec) > 0)
  625. fscache_mark_pages_cached(op, &pagevec);
  626. if (list_empty(pages))
  627. ret = 0;
  628. /* submit the apparently valid pages to the backing fs to be read from
  629. * disk */
  630. if (nrbackpages > 0) {
  631. ret2 = cachefiles_read_backing_file(object, op, &backpages);
  632. if (ret2 == -ENOMEM || ret2 == -EINTR)
  633. ret = ret2;
  634. }
  635. _leave(" = %d [nr=%u%s]",
  636. ret, *nr_pages, list_empty(pages) ? " empty" : "");
  637. return ret;
  638. all_enobufs:
  639. fscache_retrieval_complete(op, *nr_pages);
  640. return -ENOBUFS;
  641. }
  642. /*
  643. * allocate a block in the cache in which to store a page
  644. * - cache withdrawal is prevented by the caller
  645. * - returns -EINTR if interrupted
  646. * - returns -ENOMEM if ran out of memory
  647. * - returns -ENOBUFS if no buffers can be made available
  648. * - returns -ENOBUFS if page is beyond EOF
  649. * - otherwise:
  650. * - the metadata will be retained
  651. * - 0 will be returned
  652. */
  653. int cachefiles_allocate_page(struct fscache_retrieval *op,
  654. struct page *page,
  655. gfp_t gfp)
  656. {
  657. struct cachefiles_object *object;
  658. struct cachefiles_cache *cache;
  659. int ret;
  660. object = container_of(op->op.object,
  661. struct cachefiles_object, fscache);
  662. cache = container_of(object->fscache.cache,
  663. struct cachefiles_cache, cache);
  664. _enter("%p,{%lx},", object, page->index);
  665. ret = cachefiles_has_space(cache, 0, 1);
  666. if (ret == 0)
  667. fscache_mark_page_cached(op, page);
  668. else
  669. ret = -ENOBUFS;
  670. fscache_retrieval_complete(op, 1);
  671. _leave(" = %d", ret);
  672. return ret;
  673. }
  674. /*
  675. * allocate blocks in the cache in which to store a set of pages
  676. * - cache withdrawal is prevented by the caller
  677. * - returns -EINTR if interrupted
  678. * - returns -ENOMEM if ran out of memory
  679. * - returns -ENOBUFS if some buffers couldn't be made available
  680. * - returns -ENOBUFS if some pages are beyond EOF
  681. * - otherwise:
  682. * - -ENODATA will be returned
  683. * - metadata will be retained for any page marked
  684. */
  685. int cachefiles_allocate_pages(struct fscache_retrieval *op,
  686. struct list_head *pages,
  687. unsigned *nr_pages,
  688. gfp_t gfp)
  689. {
  690. struct cachefiles_object *object;
  691. struct cachefiles_cache *cache;
  692. struct pagevec pagevec;
  693. struct page *page;
  694. int ret;
  695. object = container_of(op->op.object,
  696. struct cachefiles_object, fscache);
  697. cache = container_of(object->fscache.cache,
  698. struct cachefiles_cache, cache);
  699. _enter("%p,,,%d,", object, *nr_pages);
  700. ret = cachefiles_has_space(cache, 0, *nr_pages);
  701. if (ret == 0) {
  702. pagevec_init(&pagevec, 0);
  703. list_for_each_entry(page, pages, lru) {
  704. if (pagevec_add(&pagevec, page) == 0)
  705. fscache_mark_pages_cached(op, &pagevec);
  706. }
  707. if (pagevec_count(&pagevec) > 0)
  708. fscache_mark_pages_cached(op, &pagevec);
  709. ret = -ENODATA;
  710. } else {
  711. ret = -ENOBUFS;
  712. }
  713. fscache_retrieval_complete(op, *nr_pages);
  714. _leave(" = %d", ret);
  715. return ret;
  716. }
  717. /*
  718. * request a page be stored in the cache
  719. * - cache withdrawal is prevented by the caller
  720. * - this request may be ignored if there's no cache block available, in which
  721. * case -ENOBUFS will be returned
  722. * - if the op is in progress, 0 will be returned
  723. */
  724. int cachefiles_write_page(struct fscache_storage *op, struct page *page)
  725. {
  726. struct cachefiles_object *object;
  727. struct cachefiles_cache *cache;
  728. struct file *file;
  729. struct path path;
  730. loff_t pos, eof;
  731. size_t len;
  732. void *data;
  733. int ret = -ENOBUFS;
  734. ASSERT(op != NULL);
  735. ASSERT(page != NULL);
  736. object = container_of(op->op.object,
  737. struct cachefiles_object, fscache);
  738. _enter("%p,%p{%lx},,,", object, page, page->index);
  739. if (!object->backer) {
  740. _leave(" = -ENOBUFS");
  741. return -ENOBUFS;
  742. }
  743. ASSERT(d_is_reg(object->backer));
  744. cache = container_of(object->fscache.cache,
  745. struct cachefiles_cache, cache);
  746. pos = (loff_t)page->index << PAGE_SHIFT;
  747. /* We mustn't write more data than we have, so we have to beware of a
  748. * partial page at EOF.
  749. */
  750. eof = object->fscache.store_limit_l;
  751. if (pos >= eof)
  752. goto error;
  753. /* write the page to the backing filesystem and let it store it in its
  754. * own time */
  755. path.mnt = cache->mnt;
  756. path.dentry = object->backer;
  757. file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
  758. if (IS_ERR(file)) {
  759. ret = PTR_ERR(file);
  760. goto error_2;
  761. }
  762. len = PAGE_SIZE;
  763. if (eof & ~PAGE_MASK) {
  764. if (eof - pos < PAGE_SIZE) {
  765. _debug("cut short %llx to %llx",
  766. pos, eof);
  767. len = eof - pos;
  768. ASSERTCMP(pos + len, ==, eof);
  769. }
  770. }
  771. data = kmap(page);
  772. ret = __kernel_write(file, data, len, &pos);
  773. kunmap(page);
  774. fput(file);
  775. if (ret != len)
  776. goto error_eio;
  777. _leave(" = 0");
  778. return 0;
  779. error_eio:
  780. ret = -EIO;
  781. error_2:
  782. if (ret == -EIO)
  783. cachefiles_io_error_obj(object,
  784. "Write page to backing file failed");
  785. error:
  786. _leave(" = -ENOBUFS [%d]", ret);
  787. return -ENOBUFS;
  788. }
  789. /*
  790. * detach a backing block from a page
  791. * - cache withdrawal is prevented by the caller
  792. */
  793. void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
  794. {
  795. struct cachefiles_object *object;
  796. struct cachefiles_cache *cache;
  797. object = container_of(_object, struct cachefiles_object, fscache);
  798. cache = container_of(object->fscache.cache,
  799. struct cachefiles_cache, cache);
  800. _enter("%p,{%lu}", object, page->index);
  801. spin_unlock(&object->fscache.cookie->lock);
  802. }