fscache.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. }