fscache.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * fs/cifs/fscache.c - CIFS filesystem cache interface
  3. *
  4. * Copyright (c) 2010 Novell, Inc.
  5. * Author(s): Suresh Jayaraman (sjayaraman@suse.de>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "fscache.h"
  22. #include "cifsglob.h"
  23. #include "cifs_debug.h"
  24. #include "cifs_fs_sb.h"
  25. void cifs_fscache_get_client_cookie(struct TCP_Server_Info *server)
  26. {
  27. server->fscache =
  28. fscache_acquire_cookie(cifs_fscache_netfs.primary_index,
  29. &cifs_fscache_server_index_def, server);
  30. cFYI(1, "CIFS: get client cookie (0x%p/0x%p)", server,
  31. server->fscache);
  32. }
  33. void cifs_fscache_release_client_cookie(struct TCP_Server_Info *server)
  34. {
  35. cFYI(1, "CIFS: release client cookie (0x%p/0x%p)", server,
  36. server->fscache);
  37. fscache_relinquish_cookie(server->fscache, 0);
  38. server->fscache = NULL;
  39. }
  40. void cifs_fscache_get_super_cookie(struct cifsTconInfo *tcon)
  41. {
  42. struct TCP_Server_Info *server = tcon->ses->server;
  43. tcon->fscache =
  44. fscache_acquire_cookie(server->fscache,
  45. &cifs_fscache_super_index_def, tcon);
  46. cFYI(1, "CIFS: get superblock cookie (0x%p/0x%p)",
  47. server->fscache, tcon->fscache);
  48. }
  49. void cifs_fscache_release_super_cookie(struct cifsTconInfo *tcon)
  50. {
  51. cFYI(1, "CIFS: releasing superblock cookie (0x%p)", tcon->fscache);
  52. fscache_relinquish_cookie(tcon->fscache, 0);
  53. tcon->fscache = NULL;
  54. }
  55. static void cifs_fscache_enable_inode_cookie(struct inode *inode)
  56. {
  57. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  58. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  59. if (cifsi->fscache)
  60. return;
  61. cifsi->fscache = fscache_acquire_cookie(cifs_sb->tcon->fscache,
  62. &cifs_fscache_inode_object_def,
  63. cifsi);
  64. cFYI(1, "CIFS: got FH cookie (0x%p/0x%p)",
  65. cifs_sb->tcon->fscache, cifsi->fscache);
  66. }
  67. void cifs_fscache_release_inode_cookie(struct inode *inode)
  68. {
  69. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  70. if (cifsi->fscache) {
  71. cFYI(1, "CIFS releasing inode cookie (0x%p)",
  72. cifsi->fscache);
  73. fscache_relinquish_cookie(cifsi->fscache, 0);
  74. cifsi->fscache = NULL;
  75. }
  76. }
  77. static void cifs_fscache_disable_inode_cookie(struct inode *inode)
  78. {
  79. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  80. if (cifsi->fscache) {
  81. cFYI(1, "CIFS disabling inode cookie (0x%p)",
  82. cifsi->fscache);
  83. fscache_relinquish_cookie(cifsi->fscache, 1);
  84. cifsi->fscache = NULL;
  85. }
  86. }
  87. void cifs_fscache_set_inode_cookie(struct inode *inode, struct file *filp)
  88. {
  89. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  90. cifs_fscache_disable_inode_cookie(inode);
  91. else {
  92. cifs_fscache_enable_inode_cookie(inode);
  93. cFYI(1, "CIFS: fscache inode cookie set");
  94. }
  95. }
  96. void cifs_fscache_reset_inode_cookie(struct inode *inode)
  97. {
  98. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  99. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  100. struct fscache_cookie *old = cifsi->fscache;
  101. if (cifsi->fscache) {
  102. /* retire the current fscache cache and get a new one */
  103. fscache_relinquish_cookie(cifsi->fscache, 1);
  104. cifsi->fscache = fscache_acquire_cookie(cifs_sb->tcon->fscache,
  105. &cifs_fscache_inode_object_def,
  106. cifsi);
  107. cFYI(1, "CIFS: new cookie 0x%p oldcookie 0x%p",
  108. cifsi->fscache, old);
  109. }
  110. }
  111. int cifs_fscache_release_page(struct page *page, gfp_t gfp)
  112. {
  113. if (PageFsCache(page)) {
  114. struct inode *inode = page->mapping->host;
  115. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  116. cFYI(1, "CIFS: fscache release page (0x%p/0x%p)",
  117. page, cifsi->fscache);
  118. if (!fscache_maybe_release_page(cifsi->fscache, page, gfp))
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. static void cifs_readpage_from_fscache_complete(struct page *page, void *ctx,
  124. int error)
  125. {
  126. cFYI(1, "CFS: readpage_from_fscache_complete (0x%p/%d)",
  127. page, error);
  128. if (!error)
  129. SetPageUptodate(page);
  130. unlock_page(page);
  131. }
  132. /*
  133. * Retrieve a page from FS-Cache
  134. */
  135. int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
  136. {
  137. int ret;
  138. cFYI(1, "CIFS: readpage_from_fscache(fsc:%p, p:%p, i:0x%p",
  139. CIFS_I(inode)->fscache, page, inode);
  140. ret = fscache_read_or_alloc_page(CIFS_I(inode)->fscache, page,
  141. cifs_readpage_from_fscache_complete,
  142. NULL,
  143. GFP_KERNEL);
  144. switch (ret) {
  145. case 0: /* page found in fscache, read submitted */
  146. cFYI(1, "CIFS: readpage_from_fscache: submitted");
  147. return ret;
  148. case -ENOBUFS: /* page won't be cached */
  149. case -ENODATA: /* page not in cache */
  150. cFYI(1, "CIFS: readpage_from_fscache %d", ret);
  151. return 1;
  152. default:
  153. cERROR(1, "unknown error ret = %d", ret);
  154. }
  155. return ret;
  156. }
  157. /*
  158. * Retrieve a set of pages from FS-Cache
  159. */
  160. int __cifs_readpages_from_fscache(struct inode *inode,
  161. struct address_space *mapping,
  162. struct list_head *pages,
  163. unsigned *nr_pages)
  164. {
  165. int ret;
  166. cFYI(1, "CIFS: __cifs_readpages_from_fscache (0x%p/%u/0x%p)",
  167. CIFS_I(inode)->fscache, *nr_pages, inode);
  168. ret = fscache_read_or_alloc_pages(CIFS_I(inode)->fscache, mapping,
  169. pages, nr_pages,
  170. cifs_readpage_from_fscache_complete,
  171. NULL,
  172. mapping_gfp_mask(mapping));
  173. switch (ret) {
  174. case 0: /* read submitted to the cache for all pages */
  175. cFYI(1, "CIFS: readpages_from_fscache: submitted");
  176. return ret;
  177. case -ENOBUFS: /* some pages are not cached and can't be */
  178. case -ENODATA: /* some pages are not cached */
  179. cFYI(1, "CIFS: readpages_from_fscache: no page");
  180. return 1;
  181. default:
  182. cFYI(1, "unknown error ret = %d", ret);
  183. }
  184. return ret;
  185. }
  186. void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
  187. {
  188. int ret;
  189. cFYI(1, "CIFS: readpage_to_fscache(fsc: %p, p: %p, i: %p",
  190. CIFS_I(inode)->fscache, page, inode);
  191. ret = fscache_write_page(CIFS_I(inode)->fscache, page, GFP_KERNEL);
  192. if (ret != 0)
  193. fscache_uncache_page(CIFS_I(inode)->fscache, page);
  194. }
  195. void __cifs_fscache_invalidate_page(struct page *page, struct inode *inode)
  196. {
  197. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  198. struct fscache_cookie *cookie = cifsi->fscache;
  199. cFYI(1, "CIFS: fscache invalidatepage (0x%p/0x%p)", page, cookie);
  200. fscache_wait_on_page_write(cookie, page);
  201. fscache_uncache_page(cookie, page);
  202. }