fscache.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. struct cifsTconInfo *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, "CIFS: got FH cookie (0x%p/0x%p)", tcon->fscache,
  66. 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, "CIFS releasing inode cookie (0x%p)",
  74. cifsi->fscache);
  75. fscache_relinquish_cookie(cifsi->fscache, 0);
  76. cifsi->fscache = NULL;
  77. }
  78. }
  79. static void cifs_fscache_disable_inode_cookie(struct inode *inode)
  80. {
  81. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  82. if (cifsi->fscache) {
  83. cFYI(1, "CIFS disabling inode cookie (0x%p)",
  84. cifsi->fscache);
  85. fscache_relinquish_cookie(cifsi->fscache, 1);
  86. cifsi->fscache = NULL;
  87. }
  88. }
  89. void cifs_fscache_set_inode_cookie(struct inode *inode, struct file *filp)
  90. {
  91. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  92. cifs_fscache_disable_inode_cookie(inode);
  93. else
  94. cifs_fscache_enable_inode_cookie(inode);
  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(
  105. cifs_sb_master_tcon(cifs_sb)->fscache,
  106. &cifs_fscache_inode_object_def,
  107. cifsi);
  108. cFYI(1, "CIFS: new cookie 0x%p oldcookie 0x%p",
  109. cifsi->fscache, old);
  110. }
  111. }
  112. int cifs_fscache_release_page(struct page *page, gfp_t gfp)
  113. {
  114. if (PageFsCache(page)) {
  115. struct inode *inode = page->mapping->host;
  116. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  117. cFYI(1, "CIFS: fscache release page (0x%p/0x%p)",
  118. page, cifsi->fscache);
  119. if (!fscache_maybe_release_page(cifsi->fscache, page, gfp))
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. static void cifs_readpage_from_fscache_complete(struct page *page, void *ctx,
  125. int error)
  126. {
  127. cFYI(1, "CFS: readpage_from_fscache_complete (0x%p/%d)",
  128. page, error);
  129. if (!error)
  130. SetPageUptodate(page);
  131. unlock_page(page);
  132. }
  133. /*
  134. * Retrieve a page from FS-Cache
  135. */
  136. int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
  137. {
  138. int ret;
  139. cFYI(1, "CIFS: readpage_from_fscache(fsc:%p, p:%p, i:0x%p",
  140. CIFS_I(inode)->fscache, page, inode);
  141. ret = fscache_read_or_alloc_page(CIFS_I(inode)->fscache, page,
  142. cifs_readpage_from_fscache_complete,
  143. NULL,
  144. GFP_KERNEL);
  145. switch (ret) {
  146. case 0: /* page found in fscache, read submitted */
  147. cFYI(1, "CIFS: readpage_from_fscache: submitted");
  148. return ret;
  149. case -ENOBUFS: /* page won't be cached */
  150. case -ENODATA: /* page not in cache */
  151. cFYI(1, "CIFS: readpage_from_fscache %d", ret);
  152. return 1;
  153. default:
  154. cERROR(1, "unknown error ret = %d", ret);
  155. }
  156. return ret;
  157. }
  158. /*
  159. * Retrieve a set of pages from FS-Cache
  160. */
  161. int __cifs_readpages_from_fscache(struct inode *inode,
  162. struct address_space *mapping,
  163. struct list_head *pages,
  164. unsigned *nr_pages)
  165. {
  166. int ret;
  167. cFYI(1, "CIFS: __cifs_readpages_from_fscache (0x%p/%u/0x%p)",
  168. CIFS_I(inode)->fscache, *nr_pages, inode);
  169. ret = fscache_read_or_alloc_pages(CIFS_I(inode)->fscache, mapping,
  170. pages, nr_pages,
  171. cifs_readpage_from_fscache_complete,
  172. NULL,
  173. mapping_gfp_mask(mapping));
  174. switch (ret) {
  175. case 0: /* read submitted to the cache for all pages */
  176. cFYI(1, "CIFS: readpages_from_fscache: submitted");
  177. return ret;
  178. case -ENOBUFS: /* some pages are not cached and can't be */
  179. case -ENODATA: /* some pages are not cached */
  180. cFYI(1, "CIFS: readpages_from_fscache: no page");
  181. return 1;
  182. default:
  183. cFYI(1, "unknown error ret = %d", ret);
  184. }
  185. return ret;
  186. }
  187. void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
  188. {
  189. int ret;
  190. cFYI(1, "CIFS: readpage_to_fscache(fsc: %p, p: %p, i: %p",
  191. CIFS_I(inode)->fscache, page, inode);
  192. ret = fscache_write_page(CIFS_I(inode)->fscache, page, GFP_KERNEL);
  193. if (ret != 0)
  194. fscache_uncache_page(CIFS_I(inode)->fscache, page);
  195. }
  196. void __cifs_fscache_invalidate_page(struct page *page, struct inode *inode)
  197. {
  198. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  199. struct fscache_cookie *cookie = cifsi->fscache;
  200. cFYI(1, "CIFS: fscache invalidatepage (0x%p/0x%p)", page, cookie);
  201. fscache_wait_on_page_write(cookie, page);
  202. fscache_uncache_page(cookie, page);
  203. }