fscache.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_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. }
  94. void cifs_fscache_reset_inode_cookie(struct inode *inode)
  95. {
  96. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  97. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  98. struct fscache_cookie *old = cifsi->fscache;
  99. if (cifsi->fscache) {
  100. /* retire the current fscache cache and get a new one */
  101. fscache_relinquish_cookie(cifsi->fscache, 1);
  102. cifsi->fscache = fscache_acquire_cookie(
  103. cifs_sb_master_tcon(cifs_sb)->fscache,
  104. &cifs_fscache_inode_object_def,
  105. cifsi);
  106. cFYI(1, "%s: new cookie 0x%p oldcookie 0x%p",
  107. __func__, cifsi->fscache, old);
  108. }
  109. }
  110. int cifs_fscache_release_page(struct page *page, gfp_t gfp)
  111. {
  112. if (PageFsCache(page)) {
  113. struct inode *inode = page->mapping->host;
  114. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  115. cFYI(1, "%s: (0x%p/0x%p)", __func__, page,
  116. cifsi->fscache);
  117. if (!fscache_maybe_release_page(cifsi->fscache, page, gfp))
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. static void cifs_readpage_from_fscache_complete(struct page *page, void *ctx,
  123. int error)
  124. {
  125. cFYI(1, "%s: (0x%p/%d)", __func__, page, error);
  126. if (!error)
  127. SetPageUptodate(page);
  128. unlock_page(page);
  129. }
  130. /*
  131. * Retrieve a page from FS-Cache
  132. */
  133. int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
  134. {
  135. int ret;
  136. cFYI(1, "%s: (fsc:%p, p:%p, i:0x%p", __func__,
  137. CIFS_I(inode)->fscache, page, inode);
  138. ret = fscache_read_or_alloc_page(CIFS_I(inode)->fscache, page,
  139. cifs_readpage_from_fscache_complete,
  140. NULL,
  141. GFP_KERNEL);
  142. switch (ret) {
  143. case 0: /* page found in fscache, read submitted */
  144. cFYI(1, "%s: submitted", __func__);
  145. return ret;
  146. case -ENOBUFS: /* page won't be cached */
  147. case -ENODATA: /* page not in cache */
  148. cFYI(1, "%s: %d", __func__, ret);
  149. return 1;
  150. default:
  151. cERROR(1, "unknown error ret = %d", ret);
  152. }
  153. return ret;
  154. }
  155. /*
  156. * Retrieve a set of pages from FS-Cache
  157. */
  158. int __cifs_readpages_from_fscache(struct inode *inode,
  159. struct address_space *mapping,
  160. struct list_head *pages,
  161. unsigned *nr_pages)
  162. {
  163. int ret;
  164. cFYI(1, "%s: (0x%p/%u/0x%p)", __func__,
  165. CIFS_I(inode)->fscache, *nr_pages, inode);
  166. ret = fscache_read_or_alloc_pages(CIFS_I(inode)->fscache, mapping,
  167. pages, nr_pages,
  168. cifs_readpage_from_fscache_complete,
  169. NULL,
  170. mapping_gfp_mask(mapping));
  171. switch (ret) {
  172. case 0: /* read submitted to the cache for all pages */
  173. cFYI(1, "%s: submitted", __func__);
  174. return ret;
  175. case -ENOBUFS: /* some pages are not cached and can't be */
  176. case -ENODATA: /* some pages are not cached */
  177. cFYI(1, "%s: no page", __func__);
  178. return 1;
  179. default:
  180. cFYI(1, "unknown error ret = %d", ret);
  181. }
  182. return ret;
  183. }
  184. void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
  185. {
  186. int ret;
  187. cFYI(1, "%s: (fsc: %p, p: %p, i: %p)", __func__,
  188. CIFS_I(inode)->fscache, page, inode);
  189. ret = fscache_write_page(CIFS_I(inode)->fscache, page, GFP_KERNEL);
  190. if (ret != 0)
  191. fscache_uncache_page(CIFS_I(inode)->fscache, page);
  192. }
  193. void __cifs_fscache_invalidate_page(struct page *page, struct inode *inode)
  194. {
  195. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  196. struct fscache_cookie *cookie = cifsi->fscache;
  197. cFYI(1, "%s: (0x%p/0x%p)", __func__, page, cookie);
  198. fscache_wait_on_page_write(cookie, page);
  199. fscache_uncache_page(cookie, page);
  200. }