rdwr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /* Storage object read/write
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/mount.h>
  12. #include <linux/slab.h>
  13. #include <linux/file.h>
  14. #include "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. fscache_mark_page_cached(monitor->op,
  147. monitor->netfs_page);
  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. fscache_mark_page_cached(op, netpage);
  275. copy_highpage(netpage, backpage);
  276. fscache_end_io(op, netpage, 0);
  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. goto out;
  293. io_error:
  294. cachefiles_io_error_obj(object, "Page read error on backing file");
  295. ret = -ENOBUFS;
  296. goto out;
  297. nomem_page:
  298. page_cache_release(newpage);
  299. nomem_monitor:
  300. fscache_put_retrieval(monitor->op);
  301. kfree(monitor);
  302. nomem:
  303. _leave(" = -ENOMEM");
  304. return -ENOMEM;
  305. }
  306. /*
  307. * read a page from the cache or allocate a block in which to store it
  308. * - cache withdrawal is prevented by the caller
  309. * - returns -EINTR if interrupted
  310. * - returns -ENOMEM if ran out of memory
  311. * - returns -ENOBUFS if no buffers can be made available
  312. * - returns -ENOBUFS if page is beyond EOF
  313. * - if the page is backed by a block in the cache:
  314. * - a read will be started which will call the callback on completion
  315. * - 0 will be returned
  316. * - else if the page is unbacked:
  317. * - the metadata will be retained
  318. * - -ENODATA will be returned
  319. */
  320. int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
  321. struct page *page,
  322. gfp_t gfp)
  323. {
  324. struct cachefiles_object *object;
  325. struct cachefiles_cache *cache;
  326. struct pagevec pagevec;
  327. struct inode *inode;
  328. sector_t block0, block;
  329. unsigned shift;
  330. int ret;
  331. object = container_of(op->op.object,
  332. struct cachefiles_object, fscache);
  333. cache = container_of(object->fscache.cache,
  334. struct cachefiles_cache, cache);
  335. _enter("{%p},{%lx},,,", object, page->index);
  336. if (!object->backer)
  337. return -ENOBUFS;
  338. inode = object->backer->d_inode;
  339. ASSERT(S_ISREG(inode->i_mode));
  340. ASSERT(inode->i_mapping->a_ops->bmap);
  341. ASSERT(inode->i_mapping->a_ops->readpages);
  342. /* calculate the shift required to use bmap */
  343. if (inode->i_sb->s_blocksize > PAGE_SIZE)
  344. return -ENOBUFS;
  345. shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
  346. op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
  347. op->op.flags |= FSCACHE_OP_ASYNC;
  348. op->op.processor = cachefiles_read_copier;
  349. pagevec_init(&pagevec, 0);
  350. /* we assume the absence or presence of the first block is a good
  351. * enough indication for the page as a whole
  352. * - TODO: don't use bmap() for this as it is _not_ actually good
  353. * enough for this as it doesn't indicate errors, but it's all we've
  354. * got for the moment
  355. */
  356. block0 = page->index;
  357. block0 <<= shift;
  358. block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0);
  359. _debug("%llx -> %llx",
  360. (unsigned long long) block0,
  361. (unsigned long long) block);
  362. if (block) {
  363. /* submit the apparently valid page to the backing fs to be
  364. * read from disk */
  365. ret = cachefiles_read_backing_file_one(object, op, page,
  366. &pagevec);
  367. } else if (cachefiles_has_space(cache, 0, 1) == 0) {
  368. /* there's space in the cache we can use */
  369. fscache_mark_page_cached(op, page);
  370. ret = -ENODATA;
  371. } else {
  372. ret = -ENOBUFS;
  373. }
  374. _leave(" = %d", ret);
  375. return ret;
  376. }
  377. /*
  378. * read the corresponding pages to the given set from the backing file
  379. * - any uncertain pages are simply discarded, to be tried again another time
  380. */
  381. static int cachefiles_read_backing_file(struct cachefiles_object *object,
  382. struct fscache_retrieval *op,
  383. struct list_head *list)
  384. {
  385. struct cachefiles_one_read *monitor = NULL;
  386. struct address_space *bmapping = object->backer->d_inode->i_mapping;
  387. struct pagevec lru_pvec;
  388. struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
  389. int ret = 0;
  390. _enter("");
  391. pagevec_init(&lru_pvec, 0);
  392. list_for_each_entry_safe(netpage, _n, list, lru) {
  393. list_del(&netpage->lru);
  394. _debug("read back %p{%lu,%d}",
  395. netpage, netpage->index, page_count(netpage));
  396. if (!monitor) {
  397. monitor = kzalloc(sizeof(*monitor), GFP_KERNEL);
  398. if (!monitor)
  399. goto nomem;
  400. monitor->op = fscache_get_retrieval(op);
  401. init_waitqueue_func_entry(&monitor->monitor,
  402. cachefiles_read_waiter);
  403. }
  404. for (;;) {
  405. backpage = find_get_page(bmapping, netpage->index);
  406. if (backpage)
  407. goto backing_page_already_present;
  408. if (!newpage) {
  409. newpage = page_cache_alloc_cold(bmapping);
  410. if (!newpage)
  411. goto nomem;
  412. }
  413. ret = add_to_page_cache(newpage, bmapping,
  414. netpage->index, GFP_KERNEL);
  415. if (ret == 0)
  416. goto installed_new_backing_page;
  417. if (ret != -EEXIST)
  418. goto nomem;
  419. }
  420. /* we've installed a new backing page, so now we need to add it
  421. * to the LRU list and start it reading */
  422. installed_new_backing_page:
  423. _debug("- new %p", newpage);
  424. backpage = newpage;
  425. newpage = NULL;
  426. page_cache_get(backpage);
  427. if (!pagevec_add(&lru_pvec, backpage))
  428. __pagevec_lru_add_file(&lru_pvec);
  429. reread_backing_page:
  430. ret = bmapping->a_ops->readpage(NULL, backpage);
  431. if (ret < 0)
  432. goto read_error;
  433. /* add the netfs page to the pagecache and LRU, and set the
  434. * monitor to transfer the data across */
  435. monitor_backing_page:
  436. _debug("- monitor add");
  437. ret = add_to_page_cache(netpage, op->mapping, netpage->index,
  438. GFP_KERNEL);
  439. if (ret < 0) {
  440. if (ret == -EEXIST) {
  441. page_cache_release(netpage);
  442. continue;
  443. }
  444. goto nomem;
  445. }
  446. page_cache_get(netpage);
  447. if (!pagevec_add(&lru_pvec, netpage))
  448. __pagevec_lru_add_file(&lru_pvec);
  449. /* install a monitor */
  450. page_cache_get(netpage);
  451. monitor->netfs_page = netpage;
  452. page_cache_get(backpage);
  453. monitor->back_page = backpage;
  454. monitor->monitor.private = backpage;
  455. add_page_wait_queue(backpage, &monitor->monitor);
  456. monitor = NULL;
  457. /* but the page may have been read before the monitor was
  458. * installed, so the monitor may miss the event - so we have to
  459. * ensure that we do get one in such a case */
  460. if (trylock_page(backpage)) {
  461. _debug("2unlock %p {%lx}", backpage, backpage->flags);
  462. unlock_page(backpage);
  463. }
  464. page_cache_release(backpage);
  465. backpage = NULL;
  466. page_cache_release(netpage);
  467. netpage = NULL;
  468. continue;
  469. /* if the backing page is already present, it can be in one of
  470. * three states: read in progress, read failed or read okay */
  471. backing_page_already_present:
  472. _debug("- present %p", backpage);
  473. if (PageError(backpage))
  474. goto io_error;
  475. if (PageUptodate(backpage))
  476. goto backing_page_already_uptodate;
  477. _debug("- not ready %p{%lx}", backpage, backpage->flags);
  478. if (!trylock_page(backpage))
  479. goto monitor_backing_page;
  480. if (PageError(backpage)) {
  481. _debug("error %lx", backpage->flags);
  482. unlock_page(backpage);
  483. goto io_error;
  484. }
  485. if (PageUptodate(backpage))
  486. goto backing_page_already_uptodate_unlock;
  487. /* we've locked a page that's neither up to date nor erroneous,
  488. * so we need to attempt to read it again */
  489. goto reread_backing_page;
  490. /* the backing page is already up to date, attach the netfs
  491. * page to the pagecache and LRU and copy the data across */
  492. backing_page_already_uptodate_unlock:
  493. _debug("uptodate %lx", backpage->flags);
  494. unlock_page(backpage);
  495. backing_page_already_uptodate:
  496. _debug("- uptodate");
  497. ret = add_to_page_cache(netpage, op->mapping, netpage->index,
  498. GFP_KERNEL);
  499. if (ret < 0) {
  500. if (ret == -EEXIST) {
  501. page_cache_release(netpage);
  502. continue;
  503. }
  504. goto nomem;
  505. }
  506. copy_highpage(netpage, backpage);
  507. page_cache_release(backpage);
  508. backpage = NULL;
  509. fscache_mark_page_cached(op, netpage);
  510. page_cache_get(netpage);
  511. if (!pagevec_add(&lru_pvec, netpage))
  512. __pagevec_lru_add_file(&lru_pvec);
  513. /* the netpage is unlocked and marked up to date here */
  514. fscache_end_io(op, netpage, 0);
  515. page_cache_release(netpage);
  516. netpage = NULL;
  517. continue;
  518. }
  519. netpage = NULL;
  520. _debug("out");
  521. out:
  522. /* tidy up */
  523. pagevec_lru_add_file(&lru_pvec);
  524. if (newpage)
  525. page_cache_release(newpage);
  526. if (netpage)
  527. page_cache_release(netpage);
  528. if (backpage)
  529. page_cache_release(backpage);
  530. if (monitor) {
  531. fscache_put_retrieval(op);
  532. kfree(monitor);
  533. }
  534. list_for_each_entry_safe(netpage, _n, list, lru) {
  535. list_del(&netpage->lru);
  536. page_cache_release(netpage);
  537. }
  538. _leave(" = %d", ret);
  539. return ret;
  540. nomem:
  541. _debug("nomem");
  542. ret = -ENOMEM;
  543. goto out;
  544. read_error:
  545. _debug("read error %d", ret);
  546. if (ret == -ENOMEM)
  547. goto out;
  548. io_error:
  549. cachefiles_io_error_obj(object, "Page read error on backing file");
  550. ret = -ENOBUFS;
  551. goto out;
  552. }
  553. /*
  554. * read a list of pages from the cache or allocate blocks in which to store
  555. * them
  556. */
  557. int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
  558. struct list_head *pages,
  559. unsigned *nr_pages,
  560. gfp_t gfp)
  561. {
  562. struct cachefiles_object *object;
  563. struct cachefiles_cache *cache;
  564. struct list_head backpages;
  565. struct pagevec pagevec;
  566. struct inode *inode;
  567. struct page *page, *_n;
  568. unsigned shift, nrbackpages;
  569. int ret, ret2, space;
  570. object = container_of(op->op.object,
  571. struct cachefiles_object, fscache);
  572. cache = container_of(object->fscache.cache,
  573. struct cachefiles_cache, cache);
  574. _enter("{OBJ%x,%d},,%d,,",
  575. object->fscache.debug_id, atomic_read(&op->op.usage),
  576. *nr_pages);
  577. if (!object->backer)
  578. return -ENOBUFS;
  579. space = 1;
  580. if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
  581. space = 0;
  582. inode = object->backer->d_inode;
  583. ASSERT(S_ISREG(inode->i_mode));
  584. ASSERT(inode->i_mapping->a_ops->bmap);
  585. ASSERT(inode->i_mapping->a_ops->readpages);
  586. /* calculate the shift required to use bmap */
  587. if (inode->i_sb->s_blocksize > PAGE_SIZE)
  588. return -ENOBUFS;
  589. shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
  590. pagevec_init(&pagevec, 0);
  591. op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
  592. op->op.flags |= FSCACHE_OP_ASYNC;
  593. op->op.processor = cachefiles_read_copier;
  594. INIT_LIST_HEAD(&backpages);
  595. nrbackpages = 0;
  596. ret = space ? -ENODATA : -ENOBUFS;
  597. list_for_each_entry_safe(page, _n, pages, lru) {
  598. sector_t block0, block;
  599. /* we assume the absence or presence of the first block is a
  600. * good enough indication for the page as a whole
  601. * - TODO: don't use bmap() for this as it is _not_ actually
  602. * good enough for this as it doesn't indicate errors, but
  603. * it's all we've got for the moment
  604. */
  605. block0 = page->index;
  606. block0 <<= shift;
  607. block = inode->i_mapping->a_ops->bmap(inode->i_mapping,
  608. block0);
  609. _debug("%llx -> %llx",
  610. (unsigned long long) block0,
  611. (unsigned long long) block);
  612. if (block) {
  613. /* we have data - add it to the list to give to the
  614. * backing fs */
  615. list_move(&page->lru, &backpages);
  616. (*nr_pages)--;
  617. nrbackpages++;
  618. } else if (space && pagevec_add(&pagevec, page) == 0) {
  619. fscache_mark_pages_cached(op, &pagevec);
  620. ret = -ENODATA;
  621. }
  622. }
  623. if (pagevec_count(&pagevec) > 0)
  624. fscache_mark_pages_cached(op, &pagevec);
  625. if (list_empty(pages))
  626. ret = 0;
  627. /* submit the apparently valid pages to the backing fs to be read from
  628. * disk */
  629. if (nrbackpages > 0) {
  630. ret2 = cachefiles_read_backing_file(object, op, &backpages);
  631. if (ret2 == -ENOMEM || ret2 == -EINTR)
  632. ret = ret2;
  633. }
  634. _leave(" = %d [nr=%u%s]",
  635. ret, *nr_pages, list_empty(pages) ? " empty" : "");
  636. return ret;
  637. }
  638. /*
  639. * allocate a block in the cache in which to store a page
  640. * - cache withdrawal is prevented by the caller
  641. * - returns -EINTR if interrupted
  642. * - returns -ENOMEM if ran out of memory
  643. * - returns -ENOBUFS if no buffers can be made available
  644. * - returns -ENOBUFS if page is beyond EOF
  645. * - otherwise:
  646. * - the metadata will be retained
  647. * - 0 will be returned
  648. */
  649. int cachefiles_allocate_page(struct fscache_retrieval *op,
  650. struct page *page,
  651. gfp_t gfp)
  652. {
  653. struct cachefiles_object *object;
  654. struct cachefiles_cache *cache;
  655. int ret;
  656. object = container_of(op->op.object,
  657. struct cachefiles_object, fscache);
  658. cache = container_of(object->fscache.cache,
  659. struct cachefiles_cache, cache);
  660. _enter("%p,{%lx},", object, page->index);
  661. ret = cachefiles_has_space(cache, 0, 1);
  662. if (ret == 0)
  663. fscache_mark_page_cached(op, page);
  664. else
  665. ret = -ENOBUFS;
  666. _leave(" = %d", ret);
  667. return ret;
  668. }
  669. /*
  670. * allocate blocks in the cache in which to store a set of pages
  671. * - cache withdrawal is prevented by the caller
  672. * - returns -EINTR if interrupted
  673. * - returns -ENOMEM if ran out of memory
  674. * - returns -ENOBUFS if some buffers couldn't be made available
  675. * - returns -ENOBUFS if some pages are beyond EOF
  676. * - otherwise:
  677. * - -ENODATA will be returned
  678. * - metadata will be retained for any page marked
  679. */
  680. int cachefiles_allocate_pages(struct fscache_retrieval *op,
  681. struct list_head *pages,
  682. unsigned *nr_pages,
  683. gfp_t gfp)
  684. {
  685. struct cachefiles_object *object;
  686. struct cachefiles_cache *cache;
  687. struct pagevec pagevec;
  688. struct page *page;
  689. int ret;
  690. object = container_of(op->op.object,
  691. struct cachefiles_object, fscache);
  692. cache = container_of(object->fscache.cache,
  693. struct cachefiles_cache, cache);
  694. _enter("%p,,,%d,", object, *nr_pages);
  695. ret = cachefiles_has_space(cache, 0, *nr_pages);
  696. if (ret == 0) {
  697. pagevec_init(&pagevec, 0);
  698. list_for_each_entry(page, pages, lru) {
  699. if (pagevec_add(&pagevec, page) == 0)
  700. fscache_mark_pages_cached(op, &pagevec);
  701. }
  702. if (pagevec_count(&pagevec) > 0)
  703. fscache_mark_pages_cached(op, &pagevec);
  704. ret = -ENODATA;
  705. } else {
  706. ret = -ENOBUFS;
  707. }
  708. _leave(" = %d", ret);
  709. return ret;
  710. }
  711. /*
  712. * request a page be stored in the cache
  713. * - cache withdrawal is prevented by the caller
  714. * - this request may be ignored if there's no cache block available, in which
  715. * case -ENOBUFS will be returned
  716. * - if the op is in progress, 0 will be returned
  717. */
  718. int cachefiles_write_page(struct fscache_storage *op, struct page *page)
  719. {
  720. struct cachefiles_object *object;
  721. struct cachefiles_cache *cache;
  722. mm_segment_t old_fs;
  723. struct file *file;
  724. struct path path;
  725. loff_t pos, eof;
  726. size_t len;
  727. void *data;
  728. int ret;
  729. ASSERT(op != NULL);
  730. ASSERT(page != NULL);
  731. object = container_of(op->op.object,
  732. struct cachefiles_object, fscache);
  733. _enter("%p,%p{%lx},,,", object, page, page->index);
  734. if (!object->backer) {
  735. _leave(" = -ENOBUFS");
  736. return -ENOBUFS;
  737. }
  738. ASSERT(S_ISREG(object->backer->d_inode->i_mode));
  739. cache = container_of(object->fscache.cache,
  740. struct cachefiles_cache, cache);
  741. /* write the page to the backing filesystem and let it store it in its
  742. * own time */
  743. path.mnt = cache->mnt;
  744. path.dentry = object->backer;
  745. file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
  746. if (IS_ERR(file)) {
  747. ret = PTR_ERR(file);
  748. } else {
  749. ret = -EIO;
  750. if (file->f_op->write) {
  751. pos = (loff_t) page->index << PAGE_SHIFT;
  752. /* we mustn't write more data than we have, so we have
  753. * to beware of a partial page at EOF */
  754. eof = object->fscache.store_limit_l;
  755. len = PAGE_SIZE;
  756. if (eof & ~PAGE_MASK) {
  757. ASSERTCMP(pos, <, eof);
  758. if (eof - pos < PAGE_SIZE) {
  759. _debug("cut short %llx to %llx",
  760. pos, eof);
  761. len = eof - pos;
  762. ASSERTCMP(pos + len, ==, eof);
  763. }
  764. }
  765. data = kmap(page);
  766. old_fs = get_fs();
  767. set_fs(KERNEL_DS);
  768. ret = file->f_op->write(
  769. file, (const void __user *) data, len, &pos);
  770. set_fs(old_fs);
  771. kunmap(page);
  772. if (ret != len)
  773. ret = -EIO;
  774. }
  775. fput(file);
  776. }
  777. if (ret < 0) {
  778. if (ret == -EIO)
  779. cachefiles_io_error_obj(
  780. object, "Write page to backing file failed");
  781. ret = -ENOBUFS;
  782. }
  783. _leave(" = %d", ret);
  784. return ret;
  785. }
  786. /*
  787. * detach a backing block from a page
  788. * - cache withdrawal is prevented by the caller
  789. */
  790. void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
  791. {
  792. struct cachefiles_object *object;
  793. struct cachefiles_cache *cache;
  794. object = container_of(_object, struct cachefiles_object, fscache);
  795. cache = container_of(object->fscache.cache,
  796. struct cachefiles_cache, cache);
  797. _enter("%p,{%lu}", object, page->index);
  798. spin_unlock(&object->fscache.cookie->lock);
  799. }