fscache.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. /*
  26. * Key layout of CIFS server cache index object
  27. */
  28. struct cifs_server_key {
  29. struct {
  30. uint16_t family; /* address family */
  31. __be16 port; /* IP port */
  32. } hdr;
  33. union {
  34. struct in_addr ipv4_addr;
  35. struct in6_addr ipv6_addr;
  36. };
  37. } __packed;
  38. /*
  39. * Get a cookie for a server object keyed by {IPaddress,port,family} tuple
  40. */
  41. void cifs_fscache_get_client_cookie(struct TCP_Server_Info *server)
  42. {
  43. const struct sockaddr *sa = (struct sockaddr *) &server->dstaddr;
  44. const struct sockaddr_in *addr = (struct sockaddr_in *) sa;
  45. const struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) sa;
  46. struct cifs_server_key key;
  47. uint16_t key_len = sizeof(key.hdr);
  48. memset(&key, 0, sizeof(key));
  49. /*
  50. * Should not be a problem as sin_family/sin6_family overlays
  51. * sa_family field
  52. */
  53. key.hdr.family = sa->sa_family;
  54. switch (sa->sa_family) {
  55. case AF_INET:
  56. key.hdr.port = addr->sin_port;
  57. key.ipv4_addr = addr->sin_addr;
  58. key_len += sizeof(key.ipv4_addr);
  59. break;
  60. case AF_INET6:
  61. key.hdr.port = addr6->sin6_port;
  62. key.ipv6_addr = addr6->sin6_addr;
  63. key_len += sizeof(key.ipv6_addr);
  64. break;
  65. default:
  66. cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
  67. server->fscache = NULL;
  68. return;
  69. }
  70. server->fscache =
  71. fscache_acquire_cookie(cifs_fscache_netfs.primary_index,
  72. &cifs_fscache_server_index_def,
  73. &key, key_len,
  74. NULL, 0,
  75. server, 0, true);
  76. cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
  77. __func__, server, server->fscache);
  78. }
  79. void cifs_fscache_release_client_cookie(struct TCP_Server_Info *server)
  80. {
  81. cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
  82. __func__, server, server->fscache);
  83. fscache_relinquish_cookie(server->fscache, NULL, false);
  84. server->fscache = NULL;
  85. }
  86. void cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
  87. {
  88. struct TCP_Server_Info *server = tcon->ses->server;
  89. char *sharename;
  90. sharename = extract_sharename(tcon->treeName);
  91. if (IS_ERR(sharename)) {
  92. cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
  93. tcon->fscache = NULL;
  94. return;
  95. }
  96. tcon->fscache =
  97. fscache_acquire_cookie(server->fscache,
  98. &cifs_fscache_super_index_def,
  99. sharename, strlen(sharename),
  100. &tcon->resource_id, sizeof(tcon->resource_id),
  101. tcon, 0, true);
  102. kfree(sharename);
  103. cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
  104. __func__, server->fscache, tcon->fscache);
  105. }
  106. void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
  107. {
  108. cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
  109. fscache_relinquish_cookie(tcon->fscache, &tcon->resource_id, false);
  110. tcon->fscache = NULL;
  111. }
  112. static void cifs_fscache_acquire_inode_cookie(struct cifsInodeInfo *cifsi,
  113. struct cifs_tcon *tcon)
  114. {
  115. struct cifs_fscache_inode_auxdata auxdata;
  116. memset(&auxdata, 0, sizeof(auxdata));
  117. auxdata.eof = cifsi->server_eof;
  118. auxdata.last_write_time_sec = cifsi->vfs_inode.i_mtime.tv_sec;
  119. auxdata.last_change_time_sec = cifsi->vfs_inode.i_ctime.tv_sec;
  120. auxdata.last_write_time_nsec = cifsi->vfs_inode.i_mtime.tv_nsec;
  121. auxdata.last_change_time_nsec = cifsi->vfs_inode.i_ctime.tv_nsec;
  122. cifsi->fscache =
  123. fscache_acquire_cookie(tcon->fscache,
  124. &cifs_fscache_inode_object_def,
  125. &cifsi->uniqueid, sizeof(cifsi->uniqueid),
  126. &auxdata, sizeof(auxdata),
  127. cifsi, cifsi->vfs_inode.i_size, true);
  128. }
  129. static void cifs_fscache_enable_inode_cookie(struct inode *inode)
  130. {
  131. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  132. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  133. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  134. if (cifsi->fscache)
  135. return;
  136. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE))
  137. return;
  138. cifs_fscache_acquire_inode_cookie(cifsi, tcon);
  139. cifs_dbg(FYI, "%s: got FH cookie (0x%p/0x%p)\n",
  140. __func__, tcon->fscache, cifsi->fscache);
  141. }
  142. void cifs_fscache_release_inode_cookie(struct inode *inode)
  143. {
  144. struct cifs_fscache_inode_auxdata auxdata;
  145. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  146. if (cifsi->fscache) {
  147. memset(&auxdata, 0, sizeof(auxdata));
  148. auxdata.eof = cifsi->server_eof;
  149. auxdata.last_write_time_sec = cifsi->vfs_inode.i_mtime.tv_sec;
  150. auxdata.last_change_time_sec = cifsi->vfs_inode.i_ctime.tv_sec;
  151. auxdata.last_write_time_nsec = cifsi->vfs_inode.i_mtime.tv_nsec;
  152. auxdata.last_change_time_nsec = cifsi->vfs_inode.i_ctime.tv_nsec;
  153. cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cifsi->fscache);
  154. fscache_relinquish_cookie(cifsi->fscache, &auxdata, false);
  155. cifsi->fscache = NULL;
  156. }
  157. }
  158. static void cifs_fscache_disable_inode_cookie(struct inode *inode)
  159. {
  160. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  161. if (cifsi->fscache) {
  162. cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cifsi->fscache);
  163. fscache_uncache_all_inode_pages(cifsi->fscache, inode);
  164. fscache_relinquish_cookie(cifsi->fscache, NULL, true);
  165. cifsi->fscache = NULL;
  166. }
  167. }
  168. void cifs_fscache_set_inode_cookie(struct inode *inode, struct file *filp)
  169. {
  170. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  171. cifs_fscache_disable_inode_cookie(inode);
  172. else
  173. cifs_fscache_enable_inode_cookie(inode);
  174. }
  175. void cifs_fscache_reset_inode_cookie(struct inode *inode)
  176. {
  177. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  178. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  179. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  180. struct fscache_cookie *old = cifsi->fscache;
  181. if (cifsi->fscache) {
  182. /* retire the current fscache cache and get a new one */
  183. fscache_relinquish_cookie(cifsi->fscache, NULL, true);
  184. cifs_fscache_acquire_inode_cookie(cifsi, tcon);
  185. cifs_dbg(FYI, "%s: new cookie 0x%p oldcookie 0x%p\n",
  186. __func__, cifsi->fscache, old);
  187. }
  188. }
  189. int cifs_fscache_release_page(struct page *page, gfp_t gfp)
  190. {
  191. if (PageFsCache(page)) {
  192. struct inode *inode = page->mapping->host;
  193. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  194. cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
  195. __func__, page, cifsi->fscache);
  196. if (!fscache_maybe_release_page(cifsi->fscache, page, gfp))
  197. return 0;
  198. }
  199. return 1;
  200. }
  201. static void cifs_readpage_from_fscache_complete(struct page *page, void *ctx,
  202. int error)
  203. {
  204. cifs_dbg(FYI, "%s: (0x%p/%d)\n", __func__, page, error);
  205. if (!error)
  206. SetPageUptodate(page);
  207. unlock_page(page);
  208. }
  209. /*
  210. * Retrieve a page from FS-Cache
  211. */
  212. int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
  213. {
  214. int ret;
  215. cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
  216. __func__, CIFS_I(inode)->fscache, page, inode);
  217. ret = fscache_read_or_alloc_page(CIFS_I(inode)->fscache, page,
  218. cifs_readpage_from_fscache_complete,
  219. NULL,
  220. GFP_KERNEL);
  221. switch (ret) {
  222. case 0: /* page found in fscache, read submitted */
  223. cifs_dbg(FYI, "%s: submitted\n", __func__);
  224. return ret;
  225. case -ENOBUFS: /* page won't be cached */
  226. case -ENODATA: /* page not in cache */
  227. cifs_dbg(FYI, "%s: %d\n", __func__, ret);
  228. return 1;
  229. default:
  230. cifs_dbg(VFS, "unknown error ret = %d\n", ret);
  231. }
  232. return ret;
  233. }
  234. /*
  235. * Retrieve a set of pages from FS-Cache
  236. */
  237. int __cifs_readpages_from_fscache(struct inode *inode,
  238. struct address_space *mapping,
  239. struct list_head *pages,
  240. unsigned *nr_pages)
  241. {
  242. int ret;
  243. cifs_dbg(FYI, "%s: (0x%p/%u/0x%p)\n",
  244. __func__, CIFS_I(inode)->fscache, *nr_pages, inode);
  245. ret = fscache_read_or_alloc_pages(CIFS_I(inode)->fscache, mapping,
  246. pages, nr_pages,
  247. cifs_readpage_from_fscache_complete,
  248. NULL,
  249. mapping_gfp_mask(mapping));
  250. switch (ret) {
  251. case 0: /* read submitted to the cache for all pages */
  252. cifs_dbg(FYI, "%s: submitted\n", __func__);
  253. return ret;
  254. case -ENOBUFS: /* some pages are not cached and can't be */
  255. case -ENODATA: /* some pages are not cached */
  256. cifs_dbg(FYI, "%s: no page\n", __func__);
  257. return 1;
  258. default:
  259. cifs_dbg(FYI, "unknown error ret = %d\n", ret);
  260. }
  261. return ret;
  262. }
  263. void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
  264. {
  265. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  266. int ret;
  267. cifs_dbg(FYI, "%s: (fsc: %p, p: %p, i: %p)\n",
  268. __func__, cifsi->fscache, page, inode);
  269. ret = fscache_write_page(cifsi->fscache, page,
  270. cifsi->vfs_inode.i_size, GFP_KERNEL);
  271. if (ret != 0)
  272. fscache_uncache_page(cifsi->fscache, page);
  273. }
  274. void __cifs_fscache_readpages_cancel(struct inode *inode, struct list_head *pages)
  275. {
  276. cifs_dbg(FYI, "%s: (fsc: %p, i: %p)\n",
  277. __func__, CIFS_I(inode)->fscache, inode);
  278. fscache_readpages_cancel(CIFS_I(inode)->fscache, pages);
  279. }
  280. void __cifs_fscache_invalidate_page(struct page *page, struct inode *inode)
  281. {
  282. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  283. struct fscache_cookie *cookie = cifsi->fscache;
  284. cifs_dbg(FYI, "%s: (0x%p/0x%p)\n", __func__, page, cookie);
  285. fscache_wait_on_page_write(cookie, page);
  286. fscache_uncache_page(cookie, page);
  287. }