rdwr.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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 = object->backer->d_inode->i_mapping;
  65. struct page *backpage = monitor->back_page, *backpage2;
  66. int ret;
  67. _enter("{ino=%lx},{%lx,%lx}",
  68. object->backer->d_inode->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. struct pagevec pagevec;
  130. int error, max;
  131. op = container_of(_op, struct fscache_retrieval, op);
  132. object = container_of(op->op.object,
  133. struct cachefiles_object, fscache);
  134. _enter("{ino=%lu}", object->backer->d_inode->i_ino);
  135. pagevec_init(&pagevec, 0);
  136. max = 8;
  137. spin_lock_irq(&object->work_lock);
  138. while (!list_empty(&op->to_do)) {
  139. monitor = list_entry(op->to_do.next,
  140. struct cachefiles_one_read, op_link);
  141. list_del(&monitor->op_link);
  142. spin_unlock_irq(&object->work_lock);
  143. _debug("- copy {%lu}", monitor->back_page->index);
  144. recheck:
  145. if (test_bit(FSCACHE_COOKIE_INVALIDATING,
  146. &object->fscache.cookie->flags)) {
  147. error = -ESTALE;
  148. } else if (PageUptodate(monitor->back_page)) {
  149. copy_highpage(monitor->netfs_page, monitor->back_page);
  150. fscache_mark_page_cached(monitor->op,
  151. monitor->netfs_page);
  152. error = 0;
  153. } else if (!PageError(monitor->back_page)) {
  154. /* the page has probably been truncated */
  155. error = cachefiles_read_reissue(object, monitor);
  156. if (error == -EINPROGRESS)
  157. goto next;
  158. goto recheck;
  159. } else {
  160. cachefiles_io_error_obj(
  161. object,
  162. "Readpage failed on backing file %lx",
  163. (unsigned long) monitor->back_page->flags);
  164. error = -EIO;
  165. }
  166. page_cache_release(monitor->back_page);
  167. fscache_end_io(op, monitor->netfs_page, error);
  168. page_cache_release(monitor->netfs_page);
  169. fscache_retrieval_complete(op, 1);
  170. fscache_put_retrieval(op);
  171. kfree(monitor);
  172. next:
  173. /* let the thread pool have some air occasionally */
  174. max--;
  175. if (max < 0 || need_resched()) {
  176. if (!list_empty(&op->to_do))
  177. fscache_enqueue_retrieval(op);
  178. _leave(" [maxed out]");
  179. return;
  180. }
  181. spin_lock_irq(&object->work_lock);
  182. }
  183. spin_unlock_irq(&object->work_lock);
  184. _leave("");
  185. }
  186. /*
  187. * read the corresponding page to the given set from the backing file
  188. * - an uncertain page is simply discarded, to be tried again another time
  189. */
  190. static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
  191. struct fscache_retrieval *op,
  192. struct page *netpage)
  193. {
  194. struct cachefiles_one_read *monitor;
  195. struct address_space *bmapping;
  196. struct page *newpage, *backpage;
  197. int ret;
  198. _enter("");
  199. _debug("read back %p{%lu,%d}",
  200. netpage, netpage->index, page_count(netpage));
  201. monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
  202. if (!monitor)
  203. goto nomem;
  204. monitor->netfs_page = netpage;
  205. monitor->op = fscache_get_retrieval(op);
  206. init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
  207. /* attempt to get hold of the backing page */
  208. bmapping = object->backer->d_inode->i_mapping;
  209. newpage = NULL;
  210. for (;;) {
  211. backpage = find_get_page(bmapping, netpage->index);
  212. if (backpage)
  213. goto backing_page_already_present;
  214. if (!newpage) {
  215. newpage = __page_cache_alloc(cachefiles_gfp |
  216. __GFP_COLD);
  217. if (!newpage)
  218. goto nomem_monitor;
  219. }
  220. ret = add_to_page_cache(newpage, bmapping,
  221. netpage->index, cachefiles_gfp);
  222. if (ret == 0)
  223. goto installed_new_backing_page;
  224. if (ret != -EEXIST)
  225. goto nomem_page;
  226. }
  227. /* we've installed a new backing page, so now we need to add it
  228. * to the LRU list and start it reading */
  229. installed_new_backing_page:
  230. _debug("- new %p", newpage);
  231. backpage = newpage;
  232. newpage = NULL;
  233. lru_cache_add_file(backpage);
  234. read_backing_page:
  235. ret = bmapping->a_ops->readpage(NULL, backpage);
  236. if (ret < 0)
  237. goto read_error;
  238. /* set the monitor to transfer the data across */
  239. monitor_backing_page:
  240. _debug("- monitor add");
  241. /* install the monitor */
  242. page_cache_get(monitor->netfs_page);
  243. page_cache_get(backpage);
  244. monitor->back_page = backpage;
  245. monitor->monitor.private = backpage;
  246. add_page_wait_queue(backpage, &monitor->monitor);
  247. monitor = NULL;
  248. /* but the page may have been read before the monitor was installed, so
  249. * the monitor may miss the event - so we have to ensure that we do get
  250. * one in such a case */
  251. if (trylock_page(backpage)) {
  252. _debug("jumpstart %p {%lx}", backpage, backpage->flags);
  253. unlock_page(backpage);
  254. }
  255. goto success;
  256. /* if the backing page is already present, it can be in one of
  257. * three states: read in progress, read failed or read okay */
  258. backing_page_already_present:
  259. _debug("- present");
  260. if (newpage) {
  261. page_cache_release(newpage);
  262. newpage = NULL;
  263. }
  264. if (PageError(backpage))
  265. goto io_error;
  266. if (PageUptodate(backpage))
  267. goto backing_page_already_uptodate;
  268. if (!trylock_page(backpage))
  269. goto monitor_backing_page;
  270. _debug("read %p {%lx}", backpage, backpage->flags);
  271. goto read_backing_page;
  272. /* the backing page is already up to date, attach the netfs
  273. * page to the pagecache and LRU and copy the data across */
  274. backing_page_already_uptodate:
  275. _debug("- uptodate");
  276. fscache_mark_page_cached(op, netpage);
  277. copy_highpage(netpage, backpage);
  278. fscache_end_io(op, netpage, 0);
  279. fscache_retrieval_complete(op, 1);
  280. success:
  281. _debug("success");
  282. ret = 0;
  283. out:
  284. if (backpage)
  285. page_cache_release(backpage);
  286. if (monitor) {
  287. fscache_put_retrieval(monitor->op);
  288. kfree(monitor);
  289. }
  290. _leave(" = %d", ret);
  291. return ret;
  292. read_error:
  293. _debug("read error %d", ret);
  294. if (ret == -ENOMEM) {
  295. fscache_retrieval_complete(op, 1);
  296. goto out;
  297. }
  298. io_error:
  299. cachefiles_io_error_obj(object, "Page read error on backing file");
  300. fscache_retrieval_complete(op, 1);
  301. ret = -ENOBUFS;
  302. goto out;
  303. nomem_page:
  304. page_cache_release(newpage);
  305. nomem_monitor:
  306. fscache_put_retrieval(monitor->op);
  307. kfree(monitor);
  308. nomem:
  309. fscache_retrieval_complete(op, 1);
  310. _leave(" = -ENOMEM");
  311. return -ENOMEM;
  312. }
  313. /*
  314. * read a page from the cache or allocate a block in which to store it
  315. * - cache withdrawal is prevented by the caller
  316. * - returns -EINTR if interrupted
  317. * - returns -ENOMEM if ran out of memory
  318. * - returns -ENOBUFS if no buffers can be made available
  319. * - returns -ENOBUFS if page is beyond EOF
  320. * - if the page is backed by a block in the cache:
  321. * - a read will be started which will call the callback on completion
  322. * - 0 will be returned
  323. * - else if the page is unbacked:
  324. * - the metadata will be retained
  325. * - -ENODATA will be returned
  326. */
  327. int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
  328. struct page *page,
  329. gfp_t gfp)
  330. {
  331. struct cachefiles_object *object;
  332. struct cachefiles_cache *cache;
  333. struct pagevec pagevec;
  334. struct inode *inode;
  335. sector_t block0, block;
  336. unsigned shift;
  337. int ret;
  338. object = container_of(op->op.object,
  339. struct cachefiles_object, fscache);
  340. cache = container_of(object->fscache.cache,
  341. struct cachefiles_cache, cache);
  342. _enter("{%p},{%lx},,,", object, page->index);
  343. if (!object->backer)
  344. goto enobufs;
  345. inode = object->backer->d_inode;
  346. ASSERT(S_ISREG(inode->i_mode));
  347. ASSERT(inode->i_mapping->a_ops->bmap);
  348. ASSERT(inode->i_mapping->a_ops->readpages);
  349. /* calculate the shift required to use bmap */
  350. if (inode->i_sb->s_blocksize > PAGE_SIZE)
  351. goto enobufs;
  352. shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
  353. op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
  354. op->op.flags |= FSCACHE_OP_ASYNC;
  355. op->op.processor = cachefiles_read_copier;
  356. pagevec_init(&pagevec, 0);
  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 = object->backer->d_inode->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. __GFP_COLD);
  420. if (!newpage)
  421. goto nomem;
  422. }
  423. ret = add_to_page_cache(newpage, bmapping,
  424. netpage->index, 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 to add it
  431. * to the LRU list and start it reading */
  432. installed_new_backing_page:
  433. _debug("- new %p", newpage);
  434. backpage = newpage;
  435. newpage = NULL;
  436. lru_cache_add_file(backpage);
  437. reread_backing_page:
  438. ret = bmapping->a_ops->readpage(NULL, backpage);
  439. if (ret < 0)
  440. goto read_error;
  441. /* add the netfs page to the pagecache and LRU, and set the
  442. * monitor to transfer the data across */
  443. monitor_backing_page:
  444. _debug("- monitor add");
  445. ret = add_to_page_cache(netpage, op->mapping, netpage->index,
  446. cachefiles_gfp);
  447. if (ret < 0) {
  448. if (ret == -EEXIST) {
  449. page_cache_release(netpage);
  450. fscache_retrieval_complete(op, 1);
  451. continue;
  452. }
  453. goto nomem;
  454. }
  455. lru_cache_add_file(netpage);
  456. /* install a monitor */
  457. page_cache_get(netpage);
  458. monitor->netfs_page = netpage;
  459. page_cache_get(backpage);
  460. monitor->back_page = backpage;
  461. monitor->monitor.private = backpage;
  462. add_page_wait_queue(backpage, &monitor->monitor);
  463. monitor = NULL;
  464. /* but the page may have been read before the monitor was
  465. * installed, so the monitor may miss the event - so we have to
  466. * ensure that we do get one in such a case */
  467. if (trylock_page(backpage)) {
  468. _debug("2unlock %p {%lx}", backpage, backpage->flags);
  469. unlock_page(backpage);
  470. }
  471. page_cache_release(backpage);
  472. backpage = NULL;
  473. page_cache_release(netpage);
  474. netpage = NULL;
  475. continue;
  476. /* if the backing page is already present, it can be in one of
  477. * three states: read in progress, read failed or read okay */
  478. backing_page_already_present:
  479. _debug("- present %p", backpage);
  480. if (PageError(backpage))
  481. goto io_error;
  482. if (PageUptodate(backpage))
  483. goto backing_page_already_uptodate;
  484. _debug("- not ready %p{%lx}", backpage, backpage->flags);
  485. if (!trylock_page(backpage))
  486. goto monitor_backing_page;
  487. if (PageError(backpage)) {
  488. _debug("error %lx", backpage->flags);
  489. unlock_page(backpage);
  490. goto io_error;
  491. }
  492. if (PageUptodate(backpage))
  493. goto backing_page_already_uptodate_unlock;
  494. /* we've locked a page that's neither up to date nor erroneous,
  495. * so we need to attempt to read it again */
  496. goto reread_backing_page;
  497. /* the backing page is already up to date, attach the netfs
  498. * page to the pagecache and LRU and copy the data across */
  499. backing_page_already_uptodate_unlock:
  500. _debug("uptodate %lx", backpage->flags);
  501. unlock_page(backpage);
  502. backing_page_already_uptodate:
  503. _debug("- uptodate");
  504. ret = add_to_page_cache(netpage, op->mapping, netpage->index,
  505. cachefiles_gfp);
  506. if (ret < 0) {
  507. if (ret == -EEXIST) {
  508. page_cache_release(netpage);
  509. fscache_retrieval_complete(op, 1);
  510. continue;
  511. }
  512. goto nomem;
  513. }
  514. copy_highpage(netpage, backpage);
  515. page_cache_release(backpage);
  516. backpage = NULL;
  517. fscache_mark_page_cached(op, netpage);
  518. lru_cache_add_file(netpage);
  519. /* the netpage is unlocked and marked up to date here */
  520. fscache_end_io(op, netpage, 0);
  521. page_cache_release(netpage);
  522. netpage = NULL;
  523. fscache_retrieval_complete(op, 1);
  524. continue;
  525. }
  526. netpage = NULL;
  527. _debug("out");
  528. out:
  529. /* tidy up */
  530. if (newpage)
  531. page_cache_release(newpage);
  532. if (netpage)
  533. page_cache_release(netpage);
  534. if (backpage)
  535. page_cache_release(backpage);
  536. if (monitor) {
  537. fscache_put_retrieval(op);
  538. kfree(monitor);
  539. }
  540. list_for_each_entry_safe(netpage, _n, list, lru) {
  541. list_del(&netpage->lru);
  542. page_cache_release(netpage);
  543. fscache_retrieval_complete(op, 1);
  544. }
  545. _leave(" = %d", ret);
  546. return ret;
  547. nomem:
  548. _debug("nomem");
  549. ret = -ENOMEM;
  550. goto record_page_complete;
  551. read_error:
  552. _debug("read error %d", ret);
  553. if (ret == -ENOMEM)
  554. goto record_page_complete;
  555. io_error:
  556. cachefiles_io_error_obj(object, "Page read error on backing file");
  557. ret = -ENOBUFS;
  558. record_page_complete:
  559. fscache_retrieval_complete(op, 1);
  560. goto out;
  561. }
  562. /*
  563. * read a list of pages from the cache or allocate blocks in which to store
  564. * them
  565. */
  566. int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
  567. struct list_head *pages,
  568. unsigned *nr_pages,
  569. gfp_t gfp)
  570. {
  571. struct cachefiles_object *object;
  572. struct cachefiles_cache *cache;
  573. struct list_head backpages;
  574. struct pagevec pagevec;
  575. struct inode *inode;
  576. struct page *page, *_n;
  577. unsigned shift, nrbackpages;
  578. int ret, ret2, space;
  579. object = container_of(op->op.object,
  580. struct cachefiles_object, fscache);
  581. cache = container_of(object->fscache.cache,
  582. struct cachefiles_cache, cache);
  583. _enter("{OBJ%x,%d},,%d,,",
  584. object->fscache.debug_id, atomic_read(&op->op.usage),
  585. *nr_pages);
  586. if (!object->backer)
  587. goto all_enobufs;
  588. space = 1;
  589. if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
  590. space = 0;
  591. inode = object->backer->d_inode;
  592. ASSERT(S_ISREG(inode->i_mode));
  593. ASSERT(inode->i_mapping->a_ops->bmap);
  594. ASSERT(inode->i_mapping->a_ops->readpages);
  595. /* calculate the shift required to use bmap */
  596. if (inode->i_sb->s_blocksize > PAGE_SIZE)
  597. goto all_enobufs;
  598. shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
  599. pagevec_init(&pagevec, 0);
  600. op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
  601. op->op.flags |= FSCACHE_OP_ASYNC;
  602. op->op.processor = cachefiles_read_copier;
  603. INIT_LIST_HEAD(&backpages);
  604. nrbackpages = 0;
  605. ret = space ? -ENODATA : -ENOBUFS;
  606. list_for_each_entry_safe(page, _n, pages, lru) {
  607. sector_t block0, block;
  608. /* we assume the absence or presence of the first block is a
  609. * good enough indication for the page as a whole
  610. * - TODO: don't use bmap() for this as it is _not_ actually
  611. * good enough for this as it doesn't indicate errors, but
  612. * it's all we've got for the moment
  613. */
  614. block0 = page->index;
  615. block0 <<= shift;
  616. block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
  617. block0);
  618. _debug("%llx -> %llx",
  619. (unsigned long long) block0,
  620. (unsigned long long) block);
  621. if (block) {
  622. /* we have data - add it to the list to give to the
  623. * backing fs */
  624. list_move(&page->lru, &backpages);
  625. (*nr_pages)--;
  626. nrbackpages++;
  627. } else if (space && pagevec_add(&pagevec, page) == 0) {
  628. fscache_mark_pages_cached(op, &pagevec);
  629. fscache_retrieval_complete(op, 1);
  630. ret = -ENODATA;
  631. } else {
  632. fscache_retrieval_complete(op, 1);
  633. }
  634. }
  635. if (pagevec_count(&pagevec) > 0)
  636. fscache_mark_pages_cached(op, &pagevec);
  637. if (list_empty(pages))
  638. ret = 0;
  639. /* submit the apparently valid pages to the backing fs to be read from
  640. * disk */
  641. if (nrbackpages > 0) {
  642. ret2 = cachefiles_read_backing_file(object, op, &backpages);
  643. if (ret2 == -ENOMEM || ret2 == -EINTR)
  644. ret = ret2;
  645. }
  646. _leave(" = %d [nr=%u%s]",
  647. ret, *nr_pages, list_empty(pages) ? " empty" : "");
  648. return ret;
  649. all_enobufs:
  650. fscache_retrieval_complete(op, *nr_pages);
  651. return -ENOBUFS;
  652. }
  653. /*
  654. * allocate a block in the cache in which to store a page
  655. * - cache withdrawal is prevented by the caller
  656. * - returns -EINTR if interrupted
  657. * - returns -ENOMEM if ran out of memory
  658. * - returns -ENOBUFS if no buffers can be made available
  659. * - returns -ENOBUFS if page is beyond EOF
  660. * - otherwise:
  661. * - the metadata will be retained
  662. * - 0 will be returned
  663. */
  664. int cachefiles_allocate_page(struct fscache_retrieval *op,
  665. struct page *page,
  666. gfp_t gfp)
  667. {
  668. struct cachefiles_object *object;
  669. struct cachefiles_cache *cache;
  670. int ret;
  671. object = container_of(op->op.object,
  672. struct cachefiles_object, fscache);
  673. cache = container_of(object->fscache.cache,
  674. struct cachefiles_cache, cache);
  675. _enter("%p,{%lx},", object, page->index);
  676. ret = cachefiles_has_space(cache, 0, 1);
  677. if (ret == 0)
  678. fscache_mark_page_cached(op, page);
  679. else
  680. ret = -ENOBUFS;
  681. fscache_retrieval_complete(op, 1);
  682. _leave(" = %d", ret);
  683. return ret;
  684. }
  685. /*
  686. * allocate blocks in the cache in which to store a set of pages
  687. * - cache withdrawal is prevented by the caller
  688. * - returns -EINTR if interrupted
  689. * - returns -ENOMEM if ran out of memory
  690. * - returns -ENOBUFS if some buffers couldn't be made available
  691. * - returns -ENOBUFS if some pages are beyond EOF
  692. * - otherwise:
  693. * - -ENODATA will be returned
  694. * - metadata will be retained for any page marked
  695. */
  696. int cachefiles_allocate_pages(struct fscache_retrieval *op,
  697. struct list_head *pages,
  698. unsigned *nr_pages,
  699. gfp_t gfp)
  700. {
  701. struct cachefiles_object *object;
  702. struct cachefiles_cache *cache;
  703. struct pagevec pagevec;
  704. struct page *page;
  705. int ret;
  706. object = container_of(op->op.object,
  707. struct cachefiles_object, fscache);
  708. cache = container_of(object->fscache.cache,
  709. struct cachefiles_cache, cache);
  710. _enter("%p,,,%d,", object, *nr_pages);
  711. ret = cachefiles_has_space(cache, 0, *nr_pages);
  712. if (ret == 0) {
  713. pagevec_init(&pagevec, 0);
  714. list_for_each_entry(page, pages, lru) {
  715. if (pagevec_add(&pagevec, page) == 0)
  716. fscache_mark_pages_cached(op, &pagevec);
  717. }
  718. if (pagevec_count(&pagevec) > 0)
  719. fscache_mark_pages_cached(op, &pagevec);
  720. ret = -ENODATA;
  721. } else {
  722. ret = -ENOBUFS;
  723. }
  724. fscache_retrieval_complete(op, *nr_pages);
  725. _leave(" = %d", ret);
  726. return ret;
  727. }
  728. /*
  729. * request a page be stored in the cache
  730. * - cache withdrawal is prevented by the caller
  731. * - this request may be ignored if there's no cache block available, in which
  732. * case -ENOBUFS will be returned
  733. * - if the op is in progress, 0 will be returned
  734. */
  735. int cachefiles_write_page(struct fscache_storage *op, struct page *page)
  736. {
  737. struct cachefiles_object *object;
  738. struct cachefiles_cache *cache;
  739. mm_segment_t old_fs;
  740. struct file *file;
  741. struct path path;
  742. loff_t pos, eof;
  743. size_t len;
  744. void *data;
  745. int ret;
  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(S_ISREG(object->backer->d_inode->i_mode));
  756. cache = container_of(object->fscache.cache,
  757. struct cachefiles_cache, cache);
  758. /* write the page to the backing filesystem and let it store it in its
  759. * own time */
  760. path.mnt = cache->mnt;
  761. path.dentry = object->backer;
  762. file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
  763. if (IS_ERR(file)) {
  764. ret = PTR_ERR(file);
  765. } else {
  766. ret = -EIO;
  767. if (file->f_op->write) {
  768. pos = (loff_t) page->index << PAGE_SHIFT;
  769. /* we mustn't write more data than we have, so we have
  770. * to beware of a partial page at EOF */
  771. eof = object->fscache.store_limit_l;
  772. len = PAGE_SIZE;
  773. if (eof & ~PAGE_MASK) {
  774. ASSERTCMP(pos, <, eof);
  775. if (eof - pos < PAGE_SIZE) {
  776. _debug("cut short %llx to %llx",
  777. pos, eof);
  778. len = eof - pos;
  779. ASSERTCMP(pos + len, ==, eof);
  780. }
  781. }
  782. data = kmap(page);
  783. file_start_write(file);
  784. old_fs = get_fs();
  785. set_fs(KERNEL_DS);
  786. ret = file->f_op->write(
  787. file, (const void __user *) data, len, &pos);
  788. set_fs(old_fs);
  789. kunmap(page);
  790. file_end_write(file);
  791. if (ret != len)
  792. ret = -EIO;
  793. }
  794. fput(file);
  795. }
  796. if (ret < 0) {
  797. if (ret == -EIO)
  798. cachefiles_io_error_obj(
  799. object, "Write page to backing file failed");
  800. ret = -ENOBUFS;
  801. }
  802. _leave(" = %d", ret);
  803. return ret;
  804. }
  805. /*
  806. * detach a backing block from a page
  807. * - cache withdrawal is prevented by the caller
  808. */
  809. void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
  810. {
  811. struct cachefiles_object *object;
  812. struct cachefiles_cache *cache;
  813. object = container_of(_object, struct cachefiles_object, fscache);
  814. cache = container_of(object->fscache.cache,
  815. struct cachefiles_cache, cache);
  816. _enter("%p,{%lu}", object, page->index);
  817. spin_unlock(&object->fscache.cookie->lock);
  818. }