pnfs_dev.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. void
  42. nfs4_print_deviceid(const struct nfs4_deviceid *id)
  43. {
  44. u32 *p = (u32 *)id;
  45. dprintk("%s: device id= [%x%x%x%x]\n", __func__,
  46. p[0], p[1], p[2], p[3]);
  47. }
  48. EXPORT_SYMBOL_GPL(nfs4_print_deviceid);
  49. static inline u32
  50. nfs4_deviceid_hash(const struct nfs4_deviceid *id)
  51. {
  52. unsigned char *cptr = (unsigned char *)id->data;
  53. unsigned int nbytes = NFS4_DEVICEID4_SIZE;
  54. u32 x = 0;
  55. while (nbytes--) {
  56. x *= 37;
  57. x += *cptr++;
  58. }
  59. return x & NFS4_DEVICE_ID_HASH_MASK;
  60. }
  61. static struct nfs4_deviceid_node *
  62. _lookup_deviceid(const struct pnfs_layoutdriver_type *ld,
  63. const struct nfs_client *clp, const struct nfs4_deviceid *id,
  64. long hash)
  65. {
  66. struct nfs4_deviceid_node *d;
  67. struct hlist_node *n;
  68. hlist_for_each_entry_rcu(d, n, &nfs4_deviceid_cache[hash], node)
  69. if (d->ld == ld && d->nfs_client == clp &&
  70. !memcmp(&d->deviceid, id, sizeof(*id))) {
  71. if (atomic_read(&d->ref))
  72. return d;
  73. else
  74. continue;
  75. }
  76. return NULL;
  77. }
  78. /*
  79. * Lookup a deviceid in cache and get a reference count on it if found
  80. *
  81. * @clp nfs_client associated with deviceid
  82. * @id deviceid to look up
  83. */
  84. struct nfs4_deviceid_node *
  85. _find_get_deviceid(const struct pnfs_layoutdriver_type *ld,
  86. const struct nfs_client *clp, const struct nfs4_deviceid *id,
  87. long hash)
  88. {
  89. struct nfs4_deviceid_node *d;
  90. rcu_read_lock();
  91. d = _lookup_deviceid(ld, clp, id, hash);
  92. if (d != NULL)
  93. atomic_inc(&d->ref);
  94. rcu_read_unlock();
  95. return d;
  96. }
  97. struct nfs4_deviceid_node *
  98. nfs4_find_get_deviceid(const struct pnfs_layoutdriver_type *ld,
  99. const struct nfs_client *clp, const struct nfs4_deviceid *id)
  100. {
  101. return _find_get_deviceid(ld, clp, id, nfs4_deviceid_hash(id));
  102. }
  103. EXPORT_SYMBOL_GPL(nfs4_find_get_deviceid);
  104. /*
  105. * Remove a deviceid from cache
  106. *
  107. * @clp nfs_client associated with deviceid
  108. * @id the deviceid to unhash
  109. *
  110. * @ret the unhashed node, if found and dereferenced to zero, NULL otherwise.
  111. */
  112. void
  113. nfs4_delete_deviceid(const struct pnfs_layoutdriver_type *ld,
  114. const struct nfs_client *clp, const struct nfs4_deviceid *id)
  115. {
  116. struct nfs4_deviceid_node *d;
  117. spin_lock(&nfs4_deviceid_lock);
  118. rcu_read_lock();
  119. d = _lookup_deviceid(ld, clp, id, nfs4_deviceid_hash(id));
  120. rcu_read_unlock();
  121. if (!d) {
  122. spin_unlock(&nfs4_deviceid_lock);
  123. return;
  124. }
  125. hlist_del_init_rcu(&d->node);
  126. spin_unlock(&nfs4_deviceid_lock);
  127. synchronize_rcu();
  128. /* balance the initial ref set in pnfs_insert_deviceid */
  129. if (atomic_dec_and_test(&d->ref))
  130. d->ld->free_deviceid_node(d);
  131. }
  132. EXPORT_SYMBOL_GPL(nfs4_delete_deviceid);
  133. void
  134. nfs4_init_deviceid_node(struct nfs4_deviceid_node *d,
  135. const struct pnfs_layoutdriver_type *ld,
  136. const struct nfs_client *nfs_client,
  137. const struct nfs4_deviceid *id)
  138. {
  139. INIT_HLIST_NODE(&d->node);
  140. INIT_HLIST_NODE(&d->tmpnode);
  141. d->ld = ld;
  142. d->nfs_client = nfs_client;
  143. d->flags = 0;
  144. d->deviceid = *id;
  145. atomic_set(&d->ref, 1);
  146. }
  147. EXPORT_SYMBOL_GPL(nfs4_init_deviceid_node);
  148. /*
  149. * Uniquely initialize and insert a deviceid node into cache
  150. *
  151. * @new new deviceid node
  152. * Note that the caller must set up the following members:
  153. * new->ld
  154. * new->nfs_client
  155. * new->deviceid
  156. *
  157. * @ret the inserted node, if none found, otherwise, the found entry.
  158. */
  159. struct nfs4_deviceid_node *
  160. nfs4_insert_deviceid_node(struct nfs4_deviceid_node *new)
  161. {
  162. struct nfs4_deviceid_node *d;
  163. long hash;
  164. spin_lock(&nfs4_deviceid_lock);
  165. hash = nfs4_deviceid_hash(&new->deviceid);
  166. d = _find_get_deviceid(new->ld, new->nfs_client, &new->deviceid, hash);
  167. if (d) {
  168. spin_unlock(&nfs4_deviceid_lock);
  169. return d;
  170. }
  171. hlist_add_head_rcu(&new->node, &nfs4_deviceid_cache[hash]);
  172. spin_unlock(&nfs4_deviceid_lock);
  173. atomic_inc(&new->ref);
  174. return new;
  175. }
  176. EXPORT_SYMBOL_GPL(nfs4_insert_deviceid_node);
  177. /*
  178. * Dereference a deviceid node and delete it when its reference count drops
  179. * to zero.
  180. *
  181. * @d deviceid node to put
  182. *
  183. * return true iff the node was deleted
  184. * Note that since the test for d->ref == 0 is sufficient to establish
  185. * that the node is no longer hashed in the global device id cache.
  186. */
  187. bool
  188. nfs4_put_deviceid_node(struct nfs4_deviceid_node *d)
  189. {
  190. if (!atomic_dec_and_test(&d->ref))
  191. return false;
  192. d->ld->free_deviceid_node(d);
  193. return true;
  194. }
  195. EXPORT_SYMBOL_GPL(nfs4_put_deviceid_node);
  196. static void
  197. _deviceid_purge_client(const struct nfs_client *clp, long hash)
  198. {
  199. struct nfs4_deviceid_node *d;
  200. struct hlist_node *n;
  201. HLIST_HEAD(tmp);
  202. spin_lock(&nfs4_deviceid_lock);
  203. rcu_read_lock();
  204. hlist_for_each_entry_rcu(d, n, &nfs4_deviceid_cache[hash], node)
  205. if (d->nfs_client == clp && atomic_read(&d->ref)) {
  206. hlist_del_init_rcu(&d->node);
  207. hlist_add_head(&d->tmpnode, &tmp);
  208. }
  209. rcu_read_unlock();
  210. spin_unlock(&nfs4_deviceid_lock);
  211. if (hlist_empty(&tmp))
  212. return;
  213. synchronize_rcu();
  214. while (!hlist_empty(&tmp)) {
  215. d = hlist_entry(tmp.first, struct nfs4_deviceid_node, tmpnode);
  216. hlist_del(&d->tmpnode);
  217. if (atomic_dec_and_test(&d->ref))
  218. d->ld->free_deviceid_node(d);
  219. }
  220. }
  221. void
  222. nfs4_deviceid_purge_client(const struct nfs_client *clp)
  223. {
  224. long h;
  225. if (!(clp->cl_exchange_flags & EXCHGID4_FLAG_USE_PNFS_MDS))
  226. return;
  227. for (h = 0; h < NFS4_DEVICE_ID_HASH_SIZE; h++)
  228. _deviceid_purge_client(clp, h);
  229. }
  230. /*
  231. * Stop use of all deviceids associated with an nfs_client
  232. */
  233. void
  234. nfs4_deviceid_mark_client_invalid(struct nfs_client *clp)
  235. {
  236. struct nfs4_deviceid_node *d;
  237. struct hlist_node *n;
  238. int i;
  239. rcu_read_lock();
  240. for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i ++){
  241. hlist_for_each_entry_rcu(d, n, &nfs4_deviceid_cache[i], node)
  242. if (d->nfs_client == clp)
  243. set_bit(NFS_DEVICEID_INVALID, &d->flags);
  244. }
  245. rcu_read_unlock();
  246. }