rdwr.c 25 KB

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