rdwr.c 25 KB

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