rdwr.c 24 KB

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