pnfs_dev.c 8.0 KB

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