fscache.c 6.4 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, "%s: (0x%p/0x%p)", __func__, server,
  31. server->fscache);
  32. }
  33. void cifs_fscache_release_client_cookie(struct TCP_Server_Info *server)
  34. {
  35. cFYI(1, "%s: (0x%p/0x%p)", __func__, server,
  36. server->fscache);
  37. fscache_relinquish_cookie(server->fscache, 0);
  38. server->fscache = NULL;
  39. }
  40. void cifs_fscache_get_super_cookie(struct cifs_tcon *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, "%s: (0x%p/0x%p)", __func__, server->fscache,
  47. tcon->fscache);
  48. }
  49. void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
  50. {
  51. cFYI(1, "%s: (0x%p)", __func__, 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. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  60. if (cifsi->fscache)
  61. return;
  62. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE) {
  63. cifsi->fscache = fscache_acquire_cookie(tcon->fscache,
  64. &cifs_fscache_inode_object_def, cifsi);
  65. cFYI(1, "%s: got FH cookie (0x%p/0x%p)", __func__,
  66. tcon->fscache, cifsi->fscache);
  67. }
  68. }
  69. void cifs_fscache_release_inode_cookie(struct inode *inode)
  70. {
  71. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  72. if (cifsi->fscache) {
  73. cFYI(1, "%s: (0x%p)", __func__, cifsi->fscache);
  74. fscache_relinquish_cookie(cifsi->fscache, 0);
  75. cifsi->fscache = NULL;
  76. }
  77. }
  78. static void cifs_fscache_disable_inode_cookie(struct inode *inode)
  79. {
  80. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  81. if (cifsi->fscache) {
  82. cFYI(1, "%s: (0x%p)", __func__, cifsi->fscache);
  83. fscache_uncache_all_inode_pages(cifsi->fscache, inode);
  84. fscache_relinquish_cookie(cifsi->fscache, 1);
  85. cifsi->fscache = NULL;
  86. }
  87. }
  88. void cifs_fscache_set_inode_cookie(struct inode *inode, struct file *filp)
  89. {
  90. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  91. cifs_fscache_disable_inode_cookie(inode);
  92. else
  93. cifs_fscache_enable_inode_cookie(inode);
  94. }
  95. void cifs_fscache_reset_inode_cookie(struct inode *inode)
  96. {
  97. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  98. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  99. struct fscache_cookie *old = cifsi->fscache;
  100. if (cifsi->fscache) {
  101. /* retire the current fscache cache and get a new one */
  102. fscache_relinquish_cookie(cifsi->fscache, 1);
  103. cifsi->fscache = fscache_acquire_cookie(
  104. cifs_sb_master_tcon(cifs_sb)->fscache,
  105. &cifs_fscache_inode_object_def,
  106. cifsi);
  107. cFYI(1, "%s: new cookie 0x%p oldcookie 0x%p",
  108. __func__, 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, "%s: (0x%p/0x%p)", __func__, page,
  117. 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, "%s: (0x%p/%d)", __func__, page, error);
  127. if (!error)
  128. SetPageUptodate(page);
  129. unlock_page(page);
  130. }
  131. /*
  132. * Retrieve a page from FS-Cache
  133. */
  134. int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
  135. {
  136. int ret;
  137. cFYI(1, "%s: (fsc:%p, p:%p, i:0x%p", __func__,
  138. CIFS_I(inode)->fscache, page, inode);
  139. ret = fscache_read_or_alloc_page(CIFS_I(inode)->fscache, page,
  140. cifs_readpage_from_fscache_complete,
  141. NULL,
  142. GFP_KERNEL);
  143. switch (ret) {
  144. case 0: /* page found in fscache, read submitted */
  145. cFYI(1, "%s: submitted", __func__);
  146. return ret;
  147. case -ENOBUFS: /* page won't be cached */
  148. case -ENODATA: /* page not in cache */
  149. cFYI(1, "%s: %d", __func__, ret);
  150. return 1;
  151. default:
  152. cERROR(1, "unknown error ret = %d", ret);
  153. }
  154. return ret;
  155. }
  156. /*
  157. * Retrieve a set of pages from FS-Cache
  158. */
  159. int __cifs_readpages_from_fscache(struct inode *inode,
  160. struct address_space *mapping,
  161. struct list_head *pages,
  162. unsigned *nr_pages)
  163. {
  164. int ret;
  165. cFYI(1, "%s: (0x%p/%u/0x%p)", __func__,
  166. CIFS_I(inode)->fscache, *nr_pages, inode);
  167. ret = fscache_read_or_alloc_pages(CIFS_I(inode)->fscache, mapping,
  168. pages, nr_pages,
  169. cifs_readpage_from_fscache_complete,
  170. NULL,
  171. mapping_gfp_mask(mapping));
  172. switch (ret) {
  173. case 0: /* read submitted to the cache for all pages */
  174. cFYI(1, "%s: submitted", __func__);
  175. return ret;
  176. case -ENOBUFS: /* some pages are not cached and can't be */
  177. case -ENODATA: /* some pages are not cached */
  178. cFYI(1, "%s: no page", __func__);
  179. return 1;
  180. default:
  181. cFYI(1, "unknown error ret = %d", ret);
  182. }
  183. return ret;
  184. }
  185. void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
  186. {
  187. int ret;
  188. cFYI(1, "%s: (fsc: %p, p: %p, i: %p)", __func__,
  189. CIFS_I(inode)->fscache, page, inode);
  190. ret = fscache_write_page(CIFS_I(inode)->fscache, page, GFP_KERNEL);
  191. if (ret != 0)
  192. fscache_uncache_page(CIFS_I(inode)->fscache, page);
  193. }
  194. void __cifs_fscache_invalidate_page(struct page *page, struct inode *inode)
  195. {
  196. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  197. struct fscache_cookie *cookie = cifsi->fscache;
  198. cFYI(1, "%s: (0x%p/0x%p)", __func__, page, cookie);
  199. fscache_wait_on_page_write(cookie, page);
  200. fscache_uncache_page(cookie, page);
  201. }