fscache.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* NFS filesystem cache interface
  2. *
  3. * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/nfs_fs.h>
  16. #include <linux/nfs_fs_sb.h>
  17. #include <linux/in6.h>
  18. #include <linux/seq_file.h>
  19. #include "internal.h"
  20. #include "fscache.h"
  21. #define NFSDBG_FACILITY NFSDBG_FSCACHE
  22. static struct rb_root nfs_fscache_keys = RB_ROOT;
  23. static DEFINE_SPINLOCK(nfs_fscache_keys_lock);
  24. /*
  25. * Get the per-client index cookie for an NFS client if the appropriate mount
  26. * flag was set
  27. * - We always try and get an index cookie for the client, but get filehandle
  28. * cookies on a per-superblock basis, depending on the mount flags
  29. */
  30. void nfs_fscache_get_client_cookie(struct nfs_client *clp)
  31. {
  32. /* create a cache index for looking up filehandles */
  33. clp->fscache = fscache_acquire_cookie(nfs_fscache_netfs.primary_index,
  34. &nfs_fscache_server_index_def,
  35. clp);
  36. dfprintk(FSCACHE, "NFS: get client cookie (0x%p/0x%p)\n",
  37. clp, clp->fscache);
  38. }
  39. /*
  40. * Dispose of a per-client cookie
  41. */
  42. void nfs_fscache_release_client_cookie(struct nfs_client *clp)
  43. {
  44. dfprintk(FSCACHE, "NFS: releasing client cookie (0x%p/0x%p)\n",
  45. clp, clp->fscache);
  46. fscache_relinquish_cookie(clp->fscache, 0);
  47. clp->fscache = NULL;
  48. }
  49. /*
  50. * Get the cache cookie for an NFS superblock. We have to handle
  51. * uniquification here because the cache doesn't do it for us.
  52. */
  53. void nfs_fscache_get_super_cookie(struct super_block *sb,
  54. struct nfs_parsed_mount_data *data)
  55. {
  56. struct nfs_fscache_key *key, *xkey;
  57. struct nfs_server *nfss = NFS_SB(sb);
  58. struct rb_node **p, *parent;
  59. const char *uniq = data->fscache_uniq ?: "";
  60. int diff, ulen;
  61. ulen = strlen(uniq);
  62. key = kzalloc(sizeof(*key) + ulen, GFP_KERNEL);
  63. if (!key)
  64. return;
  65. key->nfs_client = nfss->nfs_client;
  66. key->key.super.s_flags = sb->s_flags & NFS_MS_MASK;
  67. key->key.nfs_server.flags = nfss->flags;
  68. key->key.nfs_server.rsize = nfss->rsize;
  69. key->key.nfs_server.wsize = nfss->wsize;
  70. key->key.nfs_server.acregmin = nfss->acregmin;
  71. key->key.nfs_server.acregmax = nfss->acregmax;
  72. key->key.nfs_server.acdirmin = nfss->acdirmin;
  73. key->key.nfs_server.acdirmax = nfss->acdirmax;
  74. key->key.nfs_server.fsid = nfss->fsid;
  75. key->key.rpc_auth.au_flavor = nfss->client->cl_auth->au_flavor;
  76. key->key.uniq_len = ulen;
  77. memcpy(key->key.uniquifier, uniq, ulen);
  78. spin_lock(&nfs_fscache_keys_lock);
  79. p = &nfs_fscache_keys.rb_node;
  80. parent = NULL;
  81. while (*p) {
  82. parent = *p;
  83. xkey = rb_entry(parent, struct nfs_fscache_key, node);
  84. if (key->nfs_client < xkey->nfs_client)
  85. goto go_left;
  86. if (key->nfs_client > xkey->nfs_client)
  87. goto go_right;
  88. diff = memcmp(&key->key, &xkey->key, sizeof(key->key));
  89. if (diff < 0)
  90. goto go_left;
  91. if (diff > 0)
  92. goto go_right;
  93. if (key->key.uniq_len == 0)
  94. goto non_unique;
  95. diff = memcmp(key->key.uniquifier,
  96. xkey->key.uniquifier,
  97. key->key.uniq_len);
  98. if (diff < 0)
  99. goto go_left;
  100. if (diff > 0)
  101. goto go_right;
  102. goto non_unique;
  103. go_left:
  104. p = &(*p)->rb_left;
  105. continue;
  106. go_right:
  107. p = &(*p)->rb_right;
  108. }
  109. rb_link_node(&key->node, parent, p);
  110. rb_insert_color(&key->node, &nfs_fscache_keys);
  111. spin_unlock(&nfs_fscache_keys_lock);
  112. nfss->fscache_key = key;
  113. /* create a cache index for looking up filehandles */
  114. nfss->fscache = fscache_acquire_cookie(nfss->nfs_client->fscache,
  115. &nfs_fscache_super_index_def,
  116. nfss);
  117. dfprintk(FSCACHE, "NFS: get superblock cookie (0x%p/0x%p)\n",
  118. nfss, nfss->fscache);
  119. return;
  120. non_unique:
  121. spin_unlock(&nfs_fscache_keys_lock);
  122. kfree(key);
  123. nfss->fscache_key = NULL;
  124. nfss->fscache = NULL;
  125. printk(KERN_WARNING "NFS:"
  126. " Cache request denied due to non-unique superblock keys\n");
  127. }
  128. /*
  129. * release a per-superblock cookie
  130. */
  131. void nfs_fscache_release_super_cookie(struct super_block *sb)
  132. {
  133. struct nfs_server *nfss = NFS_SB(sb);
  134. dfprintk(FSCACHE, "NFS: releasing superblock cookie (0x%p/0x%p)\n",
  135. nfss, nfss->fscache);
  136. fscache_relinquish_cookie(nfss->fscache, 0);
  137. nfss->fscache = NULL;
  138. if (nfss->fscache_key) {
  139. spin_lock(&nfs_fscache_keys_lock);
  140. rb_erase(&nfss->fscache_key->node, &nfs_fscache_keys);
  141. spin_unlock(&nfs_fscache_keys_lock);
  142. kfree(nfss->fscache_key);
  143. nfss->fscache_key = NULL;
  144. }
  145. }
  146. /*
  147. * Initialise the per-inode cache cookie pointer for an NFS inode.
  148. */
  149. void nfs_fscache_init_inode_cookie(struct inode *inode)
  150. {
  151. NFS_I(inode)->fscache = NULL;
  152. if (S_ISREG(inode->i_mode))
  153. set_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags);
  154. }
  155. /*
  156. * Get the per-inode cache cookie for an NFS inode.
  157. */
  158. static void nfs_fscache_enable_inode_cookie(struct inode *inode)
  159. {
  160. struct super_block *sb = inode->i_sb;
  161. struct nfs_inode *nfsi = NFS_I(inode);
  162. if (nfsi->fscache || !NFS_FSCACHE(inode))
  163. return;
  164. if ((NFS_SB(sb)->options & NFS_OPTION_FSCACHE)) {
  165. nfsi->fscache = fscache_acquire_cookie(
  166. NFS_SB(sb)->fscache,
  167. &nfs_fscache_inode_object_def,
  168. nfsi);
  169. dfprintk(FSCACHE, "NFS: get FH cookie (0x%p/0x%p/0x%p)\n",
  170. sb, nfsi, nfsi->fscache);
  171. }
  172. }
  173. /*
  174. * Release a per-inode cookie.
  175. */
  176. void nfs_fscache_release_inode_cookie(struct inode *inode)
  177. {
  178. struct nfs_inode *nfsi = NFS_I(inode);
  179. dfprintk(FSCACHE, "NFS: clear cookie (0x%p/0x%p)\n",
  180. nfsi, nfsi->fscache);
  181. fscache_relinquish_cookie(nfsi->fscache, 0);
  182. nfsi->fscache = NULL;
  183. }
  184. /*
  185. * Retire a per-inode cookie, destroying the data attached to it.
  186. */
  187. void nfs_fscache_zap_inode_cookie(struct inode *inode)
  188. {
  189. struct nfs_inode *nfsi = NFS_I(inode);
  190. dfprintk(FSCACHE, "NFS: zapping cookie (0x%p/0x%p)\n",
  191. nfsi, nfsi->fscache);
  192. fscache_relinquish_cookie(nfsi->fscache, 1);
  193. nfsi->fscache = NULL;
  194. }
  195. /*
  196. * Turn off the cache with regard to a per-inode cookie if opened for writing,
  197. * invalidating all the pages in the page cache relating to the associated
  198. * inode to clear the per-page caching.
  199. */
  200. static void nfs_fscache_disable_inode_cookie(struct inode *inode)
  201. {
  202. clear_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags);
  203. if (NFS_I(inode)->fscache) {
  204. dfprintk(FSCACHE,
  205. "NFS: nfsi 0x%p turning cache off\n", NFS_I(inode));
  206. /* Need to invalidate any mapped pages that were read in before
  207. * turning off the cache.
  208. */
  209. if (inode->i_mapping && inode->i_mapping->nrpages)
  210. invalidate_inode_pages2(inode->i_mapping);
  211. nfs_fscache_zap_inode_cookie(inode);
  212. }
  213. }
  214. /*
  215. * wait_on_bit() sleep function for uninterruptible waiting
  216. */
  217. static int nfs_fscache_wait_bit(void *flags)
  218. {
  219. schedule();
  220. return 0;
  221. }
  222. /*
  223. * Lock against someone else trying to also acquire or relinquish a cookie
  224. */
  225. static inline void nfs_fscache_inode_lock(struct inode *inode)
  226. {
  227. struct nfs_inode *nfsi = NFS_I(inode);
  228. while (test_and_set_bit(NFS_INO_FSCACHE_LOCK, &nfsi->flags))
  229. wait_on_bit(&nfsi->flags, NFS_INO_FSCACHE_LOCK,
  230. nfs_fscache_wait_bit, TASK_UNINTERRUPTIBLE);
  231. }
  232. /*
  233. * Unlock cookie management lock
  234. */
  235. static inline void nfs_fscache_inode_unlock(struct inode *inode)
  236. {
  237. struct nfs_inode *nfsi = NFS_I(inode);
  238. smp_mb__before_clear_bit();
  239. clear_bit(NFS_INO_FSCACHE_LOCK, &nfsi->flags);
  240. smp_mb__after_clear_bit();
  241. wake_up_bit(&nfsi->flags, NFS_INO_FSCACHE_LOCK);
  242. }
  243. /*
  244. * Decide if we should enable or disable local caching for this inode.
  245. * - For now, with NFS, only regular files that are open read-only will be able
  246. * to use the cache.
  247. * - May be invoked multiple times in parallel by parallel nfs_open() functions.
  248. */
  249. void nfs_fscache_set_inode_cookie(struct inode *inode, struct file *filp)
  250. {
  251. if (NFS_FSCACHE(inode)) {
  252. nfs_fscache_inode_lock(inode);
  253. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  254. nfs_fscache_disable_inode_cookie(inode);
  255. else
  256. nfs_fscache_enable_inode_cookie(inode);
  257. nfs_fscache_inode_unlock(inode);
  258. }
  259. }
  260. /*
  261. * Replace a per-inode cookie due to revalidation detecting a file having
  262. * changed on the server.
  263. */
  264. void nfs_fscache_reset_inode_cookie(struct inode *inode)
  265. {
  266. struct nfs_inode *nfsi = NFS_I(inode);
  267. struct nfs_server *nfss = NFS_SERVER(inode);
  268. struct fscache_cookie *old = nfsi->fscache;
  269. nfs_fscache_inode_lock(inode);
  270. if (nfsi->fscache) {
  271. /* retire the current fscache cache and get a new one */
  272. fscache_relinquish_cookie(nfsi->fscache, 1);
  273. nfsi->fscache = fscache_acquire_cookie(
  274. nfss->nfs_client->fscache,
  275. &nfs_fscache_inode_object_def,
  276. nfsi);
  277. dfprintk(FSCACHE,
  278. "NFS: revalidation new cookie (0x%p/0x%p/0x%p/0x%p)\n",
  279. nfss, nfsi, old, nfsi->fscache);
  280. }
  281. nfs_fscache_inode_unlock(inode);
  282. }