rdwr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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 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->task_list);
  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. spin_unlock(&object->work_lock);
  57. fscache_enqueue_retrieval(op);
  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 unlock_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. spin_lock_irq(&object->work_lock);
  122. list_del(&monitor->op_link);
  123. spin_unlock_irq(&object->work_lock);
  124. _leave(" = %d", ret);
  125. return ret;
  126. }
  127. /*
  128. * copy data from backing pages to netfs pages to complete a read operation
  129. * - driven by FS-Cache's thread pool
  130. */
  131. static void cachefiles_read_copier(struct fscache_operation *_op)
  132. {
  133. struct cachefiles_one_read *monitor;
  134. struct cachefiles_object *object;
  135. struct fscache_retrieval *op;
  136. int error, max;
  137. op = container_of(_op, struct fscache_retrieval, op);
  138. object = container_of(op->op.object,
  139. struct cachefiles_object, fscache);
  140. _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
  141. max = 8;
  142. spin_lock_irq(&object->work_lock);
  143. while (!list_empty(&op->to_do)) {
  144. monitor = list_entry(op->to_do.next,
  145. struct cachefiles_one_read, op_link);
  146. list_del(&monitor->op_link);
  147. spin_unlock_irq(&object->work_lock);
  148. _debug("- copy {%lu}", monitor->back_page->index);
  149. recheck:
  150. if (test_bit(FSCACHE_COOKIE_INVALIDATING,
  151. &object->fscache.cookie->flags)) {
  152. error = -ESTALE;
  153. } else if (PageUptodate(monitor->back_page)) {
  154. copy_highpage(monitor->netfs_page, monitor->back_page);
  155. fscache_mark_page_cached(monitor->op,
  156. monitor->netfs_page);
  157. error = 0;
  158. } else if (!PageError(monitor->back_page)) {
  159. /* the page has probably been truncated */
  160. error = cachefiles_read_reissue(object, monitor);
  161. if (error == -EINPROGRESS)
  162. goto next;
  163. goto recheck;
  164. } else {
  165. cachefiles_io_error_obj(
  166. object,
  167. "Readpage failed on backing file %lx",
  168. (unsigned long) monitor->back_page->flags);
  169. error = -EIO;
  170. }
  171. page_cache_release(monitor->back_page);
  172. fscache_end_io(op, monitor->netfs_page, error);
  173. page_cache_release(monitor->netfs_page);
  174. fscache_retrieval_complete(op, 1);
  175. fscache_put_retrieval(op);
  176. kfree(monitor);
  177. next:
  178. /* let the thread pool have some air occasionally */
  179. max--;
  180. if (max < 0 || need_resched()) {
  181. if (!list_empty(&op->to_do))
  182. fscache_enqueue_retrieval(op);
  183. _leave(" [maxed out]");
  184. return;
  185. }
  186. spin_lock_irq(&object->work_lock);
  187. }
  188. spin_unlock_irq(&object->work_lock);
  189. _leave("");
  190. }
  191. /*
  192. * read the corresponding page to the given set from the backing file
  193. * - an uncertain page is simply discarded, to be tried again another time
  194. */
  195. static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
  196. struct fscache_retrieval *op,
  197. struct page *netpage)
  198. {
  199. struct cachefiles_one_read *monitor;
  200. struct address_space *bmapping;
  201. struct page *newpage, *backpage;
  202. int ret;
  203. _enter("");
  204. _debug("read back %p{%lu,%d}",
  205. netpage, netpage->index, page_count(netpage));
  206. monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
  207. if (!monitor)
  208. goto nomem;
  209. monitor->netfs_page = netpage;
  210. monitor->op = fscache_get_retrieval(op);
  211. init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
  212. /* attempt to get hold of the backing page */
  213. bmapping = d_backing_inode(object->backer)->i_mapping;
  214. newpage = NULL;
  215. for (;;) {
  216. backpage = find_get_page(bmapping, netpage->index);
  217. if (backpage)
  218. goto backing_page_already_present;
  219. if (!newpage) {
  220. newpage = __page_cache_alloc(cachefiles_gfp |
  221. __GFP_COLD);
  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. page_cache_get(monitor->netfs_page);
  247. page_cache_get(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. page_cache_release(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. page_cache_release(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. page_cache_release(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. __GFP_COLD);
  420. if (!newpage)
  421. goto nomem;
  422. }
  423. ret = add_to_page_cache_lru(newpage, bmapping,
  424. netpage->index,
  425. cachefiles_gfp);
  426. if (ret == 0)
  427. goto installed_new_backing_page;
  428. if (ret != -EEXIST)
  429. goto nomem;
  430. }
  431. /* we've installed a new backing page, so now we need
  432. * to start it reading */
  433. installed_new_backing_page:
  434. _debug("- new %p", newpage);
  435. backpage = newpage;
  436. newpage = NULL;
  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_lru(netpage, op->mapping,
  446. netpage->index, 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. /* install a monitor */
  456. page_cache_get(netpage);
  457. monitor->netfs_page = netpage;
  458. page_cache_get(backpage);
  459. monitor->back_page = backpage;
  460. monitor->monitor.private = backpage;
  461. add_page_wait_queue(backpage, &monitor->monitor);
  462. monitor = NULL;
  463. /* but the page may have been read before the monitor was
  464. * installed, so the monitor may miss the event - so we have to
  465. * ensure that we do get one in such a case */
  466. if (trylock_page(backpage)) {
  467. _debug("2unlock %p {%lx}", backpage, backpage->flags);
  468. unlock_page(backpage);
  469. }
  470. page_cache_release(backpage);
  471. backpage = NULL;
  472. page_cache_release(netpage);
  473. netpage = NULL;
  474. continue;
  475. /* if the backing page is already present, it can be in one of
  476. * three states: read in progress, read failed or read okay */
  477. backing_page_already_present:
  478. _debug("- present %p", backpage);
  479. if (PageError(backpage))
  480. goto io_error;
  481. if (PageUptodate(backpage))
  482. goto backing_page_already_uptodate;
  483. _debug("- not ready %p{%lx}", backpage, backpage->flags);
  484. if (!trylock_page(backpage))
  485. goto monitor_backing_page;
  486. if (PageError(backpage)) {
  487. _debug("error %lx", backpage->flags);
  488. unlock_page(backpage);
  489. goto io_error;
  490. }
  491. if (PageUptodate(backpage))
  492. goto backing_page_already_uptodate_unlock;
  493. /* we've locked a page that's neither up to date nor erroneous,
  494. * so we need to attempt to read it again */
  495. goto reread_backing_page;
  496. /* the backing page is already up to date, attach the netfs
  497. * page to the pagecache and LRU and copy the data across */
  498. backing_page_already_uptodate_unlock:
  499. _debug("uptodate %lx", backpage->flags);
  500. unlock_page(backpage);
  501. backing_page_already_uptodate:
  502. _debug("- uptodate");
  503. ret = add_to_page_cache_lru(netpage, op->mapping,
  504. netpage->index, cachefiles_gfp);
  505. if (ret < 0) {
  506. if (ret == -EEXIST) {
  507. page_cache_release(netpage);
  508. fscache_retrieval_complete(op, 1);
  509. continue;
  510. }
  511. goto nomem;
  512. }
  513. copy_highpage(netpage, backpage);
  514. page_cache_release(backpage);
  515. backpage = NULL;
  516. fscache_mark_page_cached(op, netpage);
  517. /* the netpage is unlocked and marked up to date here */
  518. fscache_end_io(op, netpage, 0);
  519. page_cache_release(netpage);
  520. netpage = NULL;
  521. fscache_retrieval_complete(op, 1);
  522. continue;
  523. }
  524. netpage = NULL;
  525. _debug("out");
  526. out:
  527. /* tidy up */
  528. if (newpage)
  529. page_cache_release(newpage);
  530. if (netpage)
  531. page_cache_release(netpage);
  532. if (backpage)
  533. page_cache_release(backpage);
  534. if (monitor) {
  535. fscache_put_retrieval(op);
  536. kfree(monitor);
  537. }
  538. list_for_each_entry_safe(netpage, _n, list, lru) {
  539. list_del(&netpage->lru);
  540. page_cache_release(netpage);
  541. fscache_retrieval_complete(op, 1);
  542. }
  543. _leave(" = %d", ret);
  544. return ret;
  545. nomem:
  546. _debug("nomem");
  547. ret = -ENOMEM;
  548. goto record_page_complete;
  549. read_error:
  550. _debug("read error %d", ret);
  551. if (ret == -ENOMEM)
  552. goto record_page_complete;
  553. io_error:
  554. cachefiles_io_error_obj(object, "Page read error on backing file");
  555. ret = -ENOBUFS;
  556. record_page_complete:
  557. fscache_retrieval_complete(op, 1);
  558. goto out;
  559. }
  560. /*
  561. * read a list of pages from the cache or allocate blocks in which to store
  562. * them
  563. */
  564. int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
  565. struct list_head *pages,
  566. unsigned *nr_pages,
  567. gfp_t gfp)
  568. {
  569. struct cachefiles_object *object;
  570. struct cachefiles_cache *cache;
  571. struct list_head backpages;
  572. struct pagevec pagevec;
  573. struct inode *inode;
  574. struct page *page, *_n;
  575. unsigned shift, nrbackpages;
  576. int ret, ret2, space;
  577. object = container_of(op->op.object,
  578. struct cachefiles_object, fscache);
  579. cache = container_of(object->fscache.cache,
  580. struct cachefiles_cache, cache);
  581. _enter("{OBJ%x,%d},,%d,,",
  582. object->fscache.debug_id, atomic_read(&op->op.usage),
  583. *nr_pages);
  584. if (!object->backer)
  585. goto all_enobufs;
  586. space = 1;
  587. if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
  588. space = 0;
  589. inode = d_backing_inode(object->backer);
  590. ASSERT(S_ISREG(inode->i_mode));
  591. ASSERT(inode->i_mapping->a_ops->bmap);
  592. ASSERT(inode->i_mapping->a_ops->readpages);
  593. /* calculate the shift required to use bmap */
  594. shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
  595. pagevec_init(&pagevec, 0);
  596. op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
  597. op->op.flags |= FSCACHE_OP_ASYNC;
  598. op->op.processor = cachefiles_read_copier;
  599. INIT_LIST_HEAD(&backpages);
  600. nrbackpages = 0;
  601. ret = space ? -ENODATA : -ENOBUFS;
  602. list_for_each_entry_safe(page, _n, pages, lru) {
  603. sector_t block0, block;
  604. /* we assume the absence or presence of the first block is a
  605. * good enough indication for the page as a whole
  606. * - TODO: don't use bmap() for this as it is _not_ actually
  607. * good enough for this as it doesn't indicate errors, but
  608. * it's all we've got for the moment
  609. */
  610. block0 = page->index;
  611. block0 <<= shift;
  612. block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
  613. block0);
  614. _debug("%llx -> %llx",
  615. (unsigned long long) block0,
  616. (unsigned long long) block);
  617. if (block) {
  618. /* we have data - add it to the list to give to the
  619. * backing fs */
  620. list_move(&page->lru, &backpages);
  621. (*nr_pages)--;
  622. nrbackpages++;
  623. } else if (space && pagevec_add(&pagevec, page) == 0) {
  624. fscache_mark_pages_cached(op, &pagevec);
  625. fscache_retrieval_complete(op, 1);
  626. ret = -ENODATA;
  627. } else {
  628. fscache_retrieval_complete(op, 1);
  629. }
  630. }
  631. if (pagevec_count(&pagevec) > 0)
  632. fscache_mark_pages_cached(op, &pagevec);
  633. if (list_empty(pages))
  634. ret = 0;
  635. /* submit the apparently valid pages to the backing fs to be read from
  636. * disk */
  637. if (nrbackpages > 0) {
  638. ret2 = cachefiles_read_backing_file(object, op, &backpages);
  639. if (ret2 == -ENOMEM || ret2 == -EINTR)
  640. ret = ret2;
  641. }
  642. _leave(" = %d [nr=%u%s]",
  643. ret, *nr_pages, list_empty(pages) ? " empty" : "");
  644. return ret;
  645. all_enobufs:
  646. fscache_retrieval_complete(op, *nr_pages);
  647. return -ENOBUFS;
  648. }
  649. /*
  650. * allocate a block in the cache in which to store a page
  651. * - cache withdrawal is prevented by the caller
  652. * - returns -EINTR if interrupted
  653. * - returns -ENOMEM if ran out of memory
  654. * - returns -ENOBUFS if no buffers can be made available
  655. * - returns -ENOBUFS if page is beyond EOF
  656. * - otherwise:
  657. * - the metadata will be retained
  658. * - 0 will be returned
  659. */
  660. int cachefiles_allocate_page(struct fscache_retrieval *op,
  661. struct page *page,
  662. gfp_t gfp)
  663. {
  664. struct cachefiles_object *object;
  665. struct cachefiles_cache *cache;
  666. int ret;
  667. object = container_of(op->op.object,
  668. struct cachefiles_object, fscache);
  669. cache = container_of(object->fscache.cache,
  670. struct cachefiles_cache, cache);
  671. _enter("%p,{%lx},", object, page->index);
  672. ret = cachefiles_has_space(cache, 0, 1);
  673. if (ret == 0)
  674. fscache_mark_page_cached(op, page);
  675. else
  676. ret = -ENOBUFS;
  677. fscache_retrieval_complete(op, 1);
  678. _leave(" = %d", ret);
  679. return ret;
  680. }
  681. /*
  682. * allocate blocks in the cache in which to store a set of pages
  683. * - cache withdrawal is prevented by the caller
  684. * - returns -EINTR if interrupted
  685. * - returns -ENOMEM if ran out of memory
  686. * - returns -ENOBUFS if some buffers couldn't be made available
  687. * - returns -ENOBUFS if some pages are beyond EOF
  688. * - otherwise:
  689. * - -ENODATA will be returned
  690. * - metadata will be retained for any page marked
  691. */
  692. int cachefiles_allocate_pages(struct fscache_retrieval *op,
  693. struct list_head *pages,
  694. unsigned *nr_pages,
  695. gfp_t gfp)
  696. {
  697. struct cachefiles_object *object;
  698. struct cachefiles_cache *cache;
  699. struct pagevec pagevec;
  700. struct page *page;
  701. int ret;
  702. object = container_of(op->op.object,
  703. struct cachefiles_object, fscache);
  704. cache = container_of(object->fscache.cache,
  705. struct cachefiles_cache, cache);
  706. _enter("%p,,,%d,", object, *nr_pages);
  707. ret = cachefiles_has_space(cache, 0, *nr_pages);
  708. if (ret == 0) {
  709. pagevec_init(&pagevec, 0);
  710. list_for_each_entry(page, pages, lru) {
  711. if (pagevec_add(&pagevec, page) == 0)
  712. fscache_mark_pages_cached(op, &pagevec);
  713. }
  714. if (pagevec_count(&pagevec) > 0)
  715. fscache_mark_pages_cached(op, &pagevec);
  716. ret = -ENODATA;
  717. } else {
  718. ret = -ENOBUFS;
  719. }
  720. fscache_retrieval_complete(op, *nr_pages);
  721. _leave(" = %d", ret);
  722. return ret;
  723. }
  724. /*
  725. * request a page be stored in the cache
  726. * - cache withdrawal is prevented by the caller
  727. * - this request may be ignored if there's no cache block available, in which
  728. * case -ENOBUFS will be returned
  729. * - if the op is in progress, 0 will be returned
  730. */
  731. int cachefiles_write_page(struct fscache_storage *op, struct page *page)
  732. {
  733. struct cachefiles_object *object;
  734. struct cachefiles_cache *cache;
  735. struct file *file;
  736. struct path path;
  737. loff_t pos, eof;
  738. size_t len;
  739. void *data;
  740. int ret = -ENOBUFS;
  741. ASSERT(op != NULL);
  742. ASSERT(page != NULL);
  743. object = container_of(op->op.object,
  744. struct cachefiles_object, fscache);
  745. _enter("%p,%p{%lx},,,", object, page, page->index);
  746. if (!object->backer) {
  747. _leave(" = -ENOBUFS");
  748. return -ENOBUFS;
  749. }
  750. ASSERT(d_is_reg(object->backer));
  751. cache = container_of(object->fscache.cache,
  752. struct cachefiles_cache, cache);
  753. pos = (loff_t)page->index << PAGE_SHIFT;
  754. /* We mustn't write more data than we have, so we have to beware of a
  755. * partial page at EOF.
  756. */
  757. eof = object->fscache.store_limit_l;
  758. if (pos >= eof)
  759. goto error;
  760. /* write the page to the backing filesystem and let it store it in its
  761. * own time */
  762. path.mnt = cache->mnt;
  763. path.dentry = object->backer;
  764. file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
  765. if (IS_ERR(file)) {
  766. ret = PTR_ERR(file);
  767. goto error_2;
  768. }
  769. len = PAGE_SIZE;
  770. if (eof & ~PAGE_MASK) {
  771. if (eof - pos < PAGE_SIZE) {
  772. _debug("cut short %llx to %llx",
  773. pos, eof);
  774. len = eof - pos;
  775. ASSERTCMP(pos + len, ==, eof);
  776. }
  777. }
  778. data = kmap(page);
  779. ret = __kernel_write(file, data, len, &pos);
  780. kunmap(page);
  781. fput(file);
  782. if (ret != len)
  783. goto error_eio;
  784. _leave(" = 0");
  785. return 0;
  786. error_eio:
  787. ret = -EIO;
  788. error_2:
  789. if (ret == -EIO)
  790. cachefiles_io_error_obj(object,
  791. "Write page to backing file failed");
  792. error:
  793. _leave(" = -ENOBUFS [%d]", ret);
  794. return -ENOBUFS;
  795. }
  796. /*
  797. * detach a backing block from a page
  798. * - cache withdrawal is prevented by the caller
  799. */
  800. void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
  801. {
  802. struct cachefiles_object *object;
  803. object = container_of(_object, struct cachefiles_object, fscache);
  804. _enter("%p,{%lu}", object, page->index);
  805. spin_unlock(&object->fscache.cookie->lock);
  806. }