cache.c 7.9 KB

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