pnfs_dev.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Device operations for the pnfs client.
  3. *
  4. * Copyright (c) 2002
  5. * The Regents of the University of Michigan
  6. * All Rights Reserved
  7. *
  8. * Dean Hildebrand <dhildebz@umich.edu>
  9. * Garth Goodson <Garth.Goodson@netapp.com>
  10. *
  11. * Permission is granted to use, copy, create derivative works, and
  12. * redistribute this software and such derivative works for any purpose,
  13. * so long as the name of the University of Michigan is not used in
  14. * any advertising or publicity pertaining to the use or distribution
  15. * of this software without specific, written prior authorization. If
  16. * the above copyright notice or any other identification of the
  17. * University of Michigan is included in any copy of any portion of
  18. * this software, then the disclaimer below must also be included.
  19. *
  20. * This software is provided as is, without representation or warranty
  21. * of any kind either express or implied, including without limitation
  22. * the implied warranties of merchantability, fitness for a particular
  23. * purpose, or noninfringement. The Regents of the University of
  24. * Michigan shall not be liable for any damages, including special,
  25. * indirect, incidental, or consequential damages, with respect to any
  26. * claim arising out of or in connection with the use of the software,
  27. * even if it has been or is hereafter advised of the possibility of
  28. * such damages.
  29. */
  30. #include <linux/export.h>
  31. #include "pnfs.h"
  32. #define NFSDBG_FACILITY NFSDBG_PNFS
  33. /*
  34. * Device ID RCU cache. A device ID is unique per server and layout type.
  35. */
  36. #define NFS4_DEVICE_ID_HASH_BITS 5
  37. #define NFS4_DEVICE_ID_HASH_SIZE (1 << NFS4_DEVICE_ID_HASH_BITS)
  38. #define NFS4_DEVICE_ID_HASH_MASK (NFS4_DEVICE_ID_HASH_SIZE - 1)
  39. static struct hlist_head nfs4_deviceid_cache[NFS4_DEVICE_ID_HASH_SIZE];
  40. static DEFINE_SPINLOCK(nfs4_deviceid_lock);
  41. #ifdef NFS_DEBUG
  42. void
  43. nfs4_print_deviceid(const struct nfs4_deviceid *id)
  44. {
  45. u32 *p = (u32 *)id;
  46. dprintk("%s: device id= [%x%x%x%x]\n", __func__,
  47. p[0], p[1], p[2], p[3]);
  48. }
  49. EXPORT_SYMBOL_GPL(nfs4_print_deviceid);
  50. #endif
  51. static inline u32
  52. nfs4_deviceid_hash(const struct nfs4_deviceid *id)
  53. {
  54. unsigned char *cptr = (unsigned char *)id->data;
  55. unsigned int nbytes = NFS4_DEVICEID4_SIZE;
  56. u32 x = 0;
  57. while (nbytes--) {
  58. x *= 37;
  59. x += *cptr++;
  60. }
  61. return x & NFS4_DEVICE_ID_HASH_MASK;
  62. }
  63. static struct nfs4_deviceid_node *
  64. _lookup_deviceid(const struct pnfs_layoutdriver_type *ld,
  65. const struct nfs_client *clp, const struct nfs4_deviceid *id,
  66. long hash)
  67. {
  68. struct nfs4_deviceid_node *d;
  69. struct hlist_node *n;
  70. hlist_for_each_entry_rcu(d, n, &nfs4_deviceid_cache[hash], node)
  71. if (d->ld == ld && d->nfs_client == clp &&
  72. !memcmp(&d->deviceid, id, sizeof(*id))) {
  73. if (atomic_read(&d->ref))
  74. return d;
  75. else
  76. continue;
  77. }
  78. return NULL;
  79. }
  80. /*
  81. * Lookup a deviceid in cache and get a reference count on it if found
  82. *
  83. * @clp nfs_client associated with deviceid
  84. * @id deviceid to look up
  85. */
  86. static struct nfs4_deviceid_node *
  87. _find_get_deviceid(const struct pnfs_layoutdriver_type *ld,
  88. const struct nfs_client *clp, const struct nfs4_deviceid *id,
  89. long hash)
  90. {
  91. struct nfs4_deviceid_node *d;
  92. rcu_read_lock();
  93. d = _lookup_deviceid(ld, clp, id, hash);
  94. if (d != NULL)
  95. atomic_inc(&d->ref);
  96. rcu_read_unlock();
  97. return d;
  98. }
  99. struct nfs4_deviceid_node *
  100. nfs4_find_get_deviceid(const struct pnfs_layoutdriver_type *ld,
  101. const struct nfs_client *clp, const struct nfs4_deviceid *id)
  102. {
  103. return _find_get_deviceid(ld, clp, id, nfs4_deviceid_hash(id));
  104. }
  105. EXPORT_SYMBOL_GPL(nfs4_find_get_deviceid);
  106. /*
  107. * Remove a deviceid from cache
  108. *
  109. * @clp nfs_client associated with deviceid
  110. * @id the deviceid to unhash
  111. *
  112. * @ret the unhashed node, if found and dereferenced to zero, NULL otherwise.
  113. */
  114. void
  115. nfs4_delete_deviceid(const struct pnfs_layoutdriver_type *ld,
  116. const struct nfs_client *clp, const struct nfs4_deviceid *id)
  117. {
  118. struct nfs4_deviceid_node *d;
  119. spin_lock(&nfs4_deviceid_lock);
  120. rcu_read_lock();
  121. d = _lookup_deviceid(ld, clp, id, nfs4_deviceid_hash(id));
  122. rcu_read_unlock();
  123. if (!d) {
  124. spin_unlock(&nfs4_deviceid_lock);
  125. return;
  126. }
  127. hlist_del_init_rcu(&d->node);
  128. spin_unlock(&nfs4_deviceid_lock);
  129. synchronize_rcu();
  130. /* balance the initial ref set in pnfs_insert_deviceid */
  131. if (atomic_dec_and_test(&d->ref))
  132. d->ld->free_deviceid_node(d);
  133. }
  134. EXPORT_SYMBOL_GPL(nfs4_delete_deviceid);
  135. void
  136. nfs4_init_deviceid_node(struct nfs4_deviceid_node *d,
  137. const struct pnfs_layoutdriver_type *ld,
  138. const struct nfs_client *nfs_client,
  139. const struct nfs4_deviceid *id)
  140. {
  141. INIT_HLIST_NODE(&d->node);
  142. INIT_HLIST_NODE(&d->tmpnode);
  143. d->ld = ld;
  144. d->nfs_client = nfs_client;
  145. d->flags = 0;
  146. d->deviceid = *id;
  147. atomic_set(&d->ref, 1);
  148. }
  149. EXPORT_SYMBOL_GPL(nfs4_init_deviceid_node);
  150. /*
  151. * Uniquely initialize and insert a deviceid node into cache
  152. *
  153. * @new new deviceid node
  154. * Note that the caller must set up the following members:
  155. * new->ld
  156. * new->nfs_client
  157. * new->deviceid
  158. *
  159. * @ret the inserted node, if none found, otherwise, the found entry.
  160. */
  161. struct nfs4_deviceid_node *
  162. nfs4_insert_deviceid_node(struct nfs4_deviceid_node *new)
  163. {
  164. struct nfs4_deviceid_node *d;
  165. long hash;
  166. spin_lock(&nfs4_deviceid_lock);
  167. hash = nfs4_deviceid_hash(&new->deviceid);
  168. d = _find_get_deviceid(new->ld, new->nfs_client, &new->deviceid, hash);
  169. if (d) {
  170. spin_unlock(&nfs4_deviceid_lock);
  171. return d;
  172. }
  173. hlist_add_head_rcu(&new->node, &nfs4_deviceid_cache[hash]);
  174. spin_unlock(&nfs4_deviceid_lock);
  175. atomic_inc(&new->ref);
  176. return new;
  177. }
  178. EXPORT_SYMBOL_GPL(nfs4_insert_deviceid_node);
  179. /*
  180. * Dereference a deviceid node and delete it when its reference count drops
  181. * to zero.
  182. *
  183. * @d deviceid node to put
  184. *
  185. * return true iff the node was deleted
  186. * Note that since the test for d->ref == 0 is sufficient to establish
  187. * that the node is no longer hashed in the global device id cache.
  188. */
  189. bool
  190. nfs4_put_deviceid_node(struct nfs4_deviceid_node *d)
  191. {
  192. if (!atomic_dec_and_test(&d->ref))
  193. return false;
  194. d->ld->free_deviceid_node(d);
  195. return true;
  196. }
  197. EXPORT_SYMBOL_GPL(nfs4_put_deviceid_node);
  198. static void
  199. _deviceid_purge_client(const struct nfs_client *clp, long hash)
  200. {
  201. struct nfs4_deviceid_node *d;
  202. struct hlist_node *n;
  203. HLIST_HEAD(tmp);
  204. spin_lock(&nfs4_deviceid_lock);
  205. rcu_read_lock();
  206. hlist_for_each_entry_rcu(d, n, &nfs4_deviceid_cache[hash], node)
  207. if (d->nfs_client == clp && atomic_read(&d->ref)) {
  208. hlist_del_init_rcu(&d->node);
  209. hlist_add_head(&d->tmpnode, &tmp);
  210. }
  211. rcu_read_unlock();
  212. spin_unlock(&nfs4_deviceid_lock);
  213. if (hlist_empty(&tmp))
  214. return;
  215. synchronize_rcu();
  216. while (!hlist_empty(&tmp)) {
  217. d = hlist_entry(tmp.first, struct nfs4_deviceid_node, tmpnode);
  218. hlist_del(&d->tmpnode);
  219. if (atomic_dec_and_test(&d->ref))
  220. d->ld->free_deviceid_node(d);
  221. }
  222. }
  223. void
  224. nfs4_deviceid_purge_client(const struct nfs_client *clp)
  225. {
  226. long h;
  227. if (!(clp->cl_exchange_flags & EXCHGID4_FLAG_USE_PNFS_MDS))
  228. return;
  229. for (h = 0; h < NFS4_DEVICE_ID_HASH_SIZE; h++)
  230. _deviceid_purge_client(clp, h);
  231. }
  232. /*
  233. * Stop use of all deviceids associated with an nfs_client
  234. */
  235. void
  236. nfs4_deviceid_mark_client_invalid(struct nfs_client *clp)
  237. {
  238. struct nfs4_deviceid_node *d;
  239. struct hlist_node *n;
  240. int i;
  241. rcu_read_lock();
  242. for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i ++){
  243. hlist_for_each_entry_rcu(d, n, &nfs4_deviceid_cache[i], node)
  244. if (d->nfs_client == clp)
  245. set_bit(NFS_DEVICEID_INVALID, &d->flags);
  246. }
  247. rcu_read_unlock();
  248. }