rdwr.c 22 KB

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