cache.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * fs/cifs/cache.c - CIFS filesystem cache index structure definitions
  3. *
  4. * Copyright (c) 2010 Novell, Inc.
  5. * Authors(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 "cifs_debug.h"
  23. /*
  24. * CIFS filesystem definition for FS-Cache
  25. */
  26. struct fscache_netfs cifs_fscache_netfs = {
  27. .name = "cifs",
  28. .version = 0,
  29. };
  30. /*
  31. * Register CIFS for caching with FS-Cache
  32. */
  33. int cifs_fscache_register(void)
  34. {
  35. return fscache_register_netfs(&cifs_fscache_netfs);
  36. }
  37. /*
  38. * Unregister CIFS for caching
  39. */
  40. void cifs_fscache_unregister(void)
  41. {
  42. fscache_unregister_netfs(&cifs_fscache_netfs);
  43. }
  44. /*
  45. * Key layout of CIFS server cache index object
  46. */
  47. struct cifs_server_key {
  48. uint16_t family; /* address family */
  49. __be16 port; /* IP port */
  50. union {
  51. struct in_addr ipv4_addr;
  52. struct in6_addr ipv6_addr;
  53. } addr[0];
  54. };
  55. /*
  56. * Server object keyed by {IPaddress,port,family} tuple
  57. */
  58. static uint16_t cifs_server_get_key(const void *cookie_netfs_data,
  59. void *buffer, uint16_t maxbuf)
  60. {
  61. const struct TCP_Server_Info *server = cookie_netfs_data;
  62. const struct sockaddr *sa = (struct sockaddr *) &server->dstaddr;
  63. const struct sockaddr_in *addr = (struct sockaddr_in *) sa;
  64. const struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) sa;
  65. struct cifs_server_key *key = buffer;
  66. uint16_t key_len = sizeof(struct cifs_server_key);
  67. memset(key, 0, key_len);
  68. /*
  69. * Should not be a problem as sin_family/sin6_family overlays
  70. * sa_family field
  71. */
  72. switch (sa->sa_family) {
  73. case AF_INET:
  74. key->family = sa->sa_family;
  75. key->port = addr->sin_port;
  76. key->addr[0].ipv4_addr = addr->sin_addr;
  77. key_len += sizeof(key->addr[0].ipv4_addr);
  78. break;
  79. case AF_INET6:
  80. key->family = sa->sa_family;
  81. key->port = addr6->sin6_port;
  82. key->addr[0].ipv6_addr = addr6->sin6_addr;
  83. key_len += sizeof(key->addr[0].ipv6_addr);
  84. break;
  85. default:
  86. cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
  87. key_len = 0;
  88. break;
  89. }
  90. return key_len;
  91. }
  92. /*
  93. * Server object for FS-Cache
  94. */
  95. const struct fscache_cookie_def cifs_fscache_server_index_def = {
  96. .name = "CIFS.server",
  97. .type = FSCACHE_COOKIE_TYPE_INDEX,
  98. .get_key = cifs_server_get_key,
  99. };
  100. /*
  101. * Auxiliary data attached to CIFS superblock within the cache
  102. */
  103. struct cifs_fscache_super_auxdata {
  104. u64 resource_id; /* unique server resource id */
  105. };
  106. static char *extract_sharename(const char *treename)
  107. {
  108. const char *src;
  109. char *delim, *dst;
  110. int len;
  111. /* skip double chars at the beginning */
  112. src = treename + 2;
  113. /* share name is always preceded by '\\' now */
  114. delim = strchr(src, '\\');
  115. if (!delim)
  116. return ERR_PTR(-EINVAL);
  117. delim++;
  118. len = strlen(delim);
  119. /* caller has to free the memory */
  120. dst = kstrndup(delim, len, GFP_KERNEL);
  121. if (!dst)
  122. return ERR_PTR(-ENOMEM);
  123. return dst;
  124. }
  125. /*
  126. * Superblock object currently keyed by share name
  127. */
  128. static uint16_t cifs_super_get_key(const void *cookie_netfs_data, void *buffer,
  129. uint16_t maxbuf)
  130. {
  131. const struct cifs_tcon *tcon = cookie_netfs_data;
  132. char *sharename;
  133. uint16_t len;
  134. sharename = extract_sharename(tcon->treeName);
  135. if (IS_ERR(sharename)) {
  136. cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
  137. sharename = NULL;
  138. return 0;
  139. }
  140. len = strlen(sharename);
  141. if (len > maxbuf)
  142. return 0;
  143. memcpy(buffer, sharename, len);
  144. kfree(sharename);
  145. return len;
  146. }
  147. static uint16_t
  148. cifs_fscache_super_get_aux(const void *cookie_netfs_data, void *buffer,
  149. uint16_t maxbuf)
  150. {
  151. struct cifs_fscache_super_auxdata auxdata;
  152. const struct cifs_tcon *tcon = cookie_netfs_data;
  153. memset(&auxdata, 0, sizeof(auxdata));
  154. auxdata.resource_id = tcon->resource_id;
  155. if (maxbuf > sizeof(auxdata))
  156. maxbuf = sizeof(auxdata);
  157. memcpy(buffer, &auxdata, maxbuf);
  158. return maxbuf;
  159. }
  160. static enum
  161. fscache_checkaux cifs_fscache_super_check_aux(void *cookie_netfs_data,
  162. const void *data,
  163. uint16_t datalen)
  164. {
  165. struct cifs_fscache_super_auxdata auxdata;
  166. const struct cifs_tcon *tcon = cookie_netfs_data;
  167. if (datalen != sizeof(auxdata))
  168. return FSCACHE_CHECKAUX_OBSOLETE;
  169. memset(&auxdata, 0, sizeof(auxdata));
  170. auxdata.resource_id = tcon->resource_id;
  171. if (memcmp(data, &auxdata, datalen) != 0)
  172. return FSCACHE_CHECKAUX_OBSOLETE;
  173. return FSCACHE_CHECKAUX_OKAY;
  174. }
  175. /*
  176. * Superblock object for FS-Cache
  177. */
  178. const struct fscache_cookie_def cifs_fscache_super_index_def = {
  179. .name = "CIFS.super",
  180. .type = FSCACHE_COOKIE_TYPE_INDEX,
  181. .get_key = cifs_super_get_key,
  182. .get_aux = cifs_fscache_super_get_aux,
  183. .check_aux = cifs_fscache_super_check_aux,
  184. };
  185. /*
  186. * Auxiliary data attached to CIFS inode within the cache
  187. */
  188. struct cifs_fscache_inode_auxdata {
  189. struct timespec last_write_time;
  190. struct timespec last_change_time;
  191. u64 eof;
  192. };
  193. static uint16_t cifs_fscache_inode_get_key(const void *cookie_netfs_data,
  194. void *buffer, uint16_t maxbuf)
  195. {
  196. const struct cifsInodeInfo *cifsi = cookie_netfs_data;
  197. uint16_t keylen;
  198. /* use the UniqueId as the key */
  199. keylen = sizeof(cifsi->uniqueid);
  200. if (keylen > maxbuf)
  201. keylen = 0;
  202. else
  203. memcpy(buffer, &cifsi->uniqueid, keylen);
  204. return keylen;
  205. }
  206. static void
  207. cifs_fscache_inode_get_attr(const void *cookie_netfs_data, uint64_t *size)
  208. {
  209. const struct cifsInodeInfo *cifsi = cookie_netfs_data;
  210. *size = cifsi->vfs_inode.i_size;
  211. }
  212. static uint16_t
  213. cifs_fscache_inode_get_aux(const void *cookie_netfs_data, void *buffer,
  214. uint16_t maxbuf)
  215. {
  216. struct cifs_fscache_inode_auxdata auxdata;
  217. const struct cifsInodeInfo *cifsi = cookie_netfs_data;
  218. memset(&auxdata, 0, sizeof(auxdata));
  219. auxdata.eof = cifsi->server_eof;
  220. auxdata.last_write_time = cifsi->vfs_inode.i_mtime;
  221. auxdata.last_change_time = cifsi->vfs_inode.i_ctime;
  222. if (maxbuf > sizeof(auxdata))
  223. maxbuf = sizeof(auxdata);
  224. memcpy(buffer, &auxdata, maxbuf);
  225. return maxbuf;
  226. }
  227. static enum
  228. fscache_checkaux cifs_fscache_inode_check_aux(void *cookie_netfs_data,
  229. const void *data,
  230. uint16_t datalen)
  231. {
  232. struct cifs_fscache_inode_auxdata auxdata;
  233. struct cifsInodeInfo *cifsi = cookie_netfs_data;
  234. if (datalen != sizeof(auxdata))
  235. return FSCACHE_CHECKAUX_OBSOLETE;
  236. memset(&auxdata, 0, sizeof(auxdata));
  237. auxdata.eof = cifsi->server_eof;
  238. auxdata.last_write_time = cifsi->vfs_inode.i_mtime;
  239. auxdata.last_change_time = cifsi->vfs_inode.i_ctime;
  240. if (memcmp(data, &auxdata, datalen) != 0)
  241. return FSCACHE_CHECKAUX_OBSOLETE;
  242. return FSCACHE_CHECKAUX_OKAY;
  243. }
  244. static void cifs_fscache_inode_now_uncached(void *cookie_netfs_data)
  245. {
  246. struct cifsInodeInfo *cifsi = cookie_netfs_data;
  247. struct pagevec pvec;
  248. pgoff_t first;
  249. int loop, nr_pages;
  250. pagevec_init(&pvec, 0);
  251. first = 0;
  252. cifs_dbg(FYI, "%s: cifs inode 0x%p now uncached\n", __func__, cifsi);
  253. for (;;) {
  254. nr_pages = pagevec_lookup(&pvec,
  255. cifsi->vfs_inode.i_mapping, first,
  256. PAGEVEC_SIZE - pagevec_count(&pvec));
  257. if (!nr_pages)
  258. break;
  259. for (loop = 0; loop < nr_pages; loop++)
  260. ClearPageFsCache(pvec.pages[loop]);
  261. first = pvec.pages[nr_pages - 1]->index + 1;
  262. pvec.nr = nr_pages;
  263. pagevec_release(&pvec);
  264. cond_resched();
  265. }
  266. }
  267. const struct fscache_cookie_def cifs_fscache_inode_object_def = {
  268. .name = "CIFS.uniqueid",
  269. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  270. .get_key = cifs_fscache_inode_get_key,
  271. .get_attr = cifs_fscache_inode_get_attr,
  272. .get_aux = cifs_fscache_inode_get_aux,
  273. .check_aux = cifs_fscache_inode_check_aux,
  274. .now_uncached = cifs_fscache_inode_now_uncached,
  275. };