rdwr.c 24 KB

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