pnfs_dev.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <linux/nfs_fs.h>
  32. #include "nfs4session.h"
  33. #include "internal.h"
  34. #include "pnfs.h"
  35. #include "nfs4trace.h"
  36. #define NFSDBG_FACILITY NFSDBG_PNFS
  37. /*
  38. * Device ID RCU cache. A device ID is unique per server and layout type.
  39. */
  40. #define NFS4_DEVICE_ID_HASH_BITS 5
  41. #define NFS4_DEVICE_ID_HASH_SIZE (1 << NFS4_DEVICE_ID_HASH_BITS)
  42. #define NFS4_DEVICE_ID_HASH_MASK (NFS4_DEVICE_ID_HASH_SIZE - 1)
  43. static struct hlist_head nfs4_deviceid_cache[NFS4_DEVICE_ID_HASH_SIZE];
  44. static DEFINE_SPINLOCK(nfs4_deviceid_lock);
  45. #ifdef NFS_DEBUG
  46. void
  47. nfs4_print_deviceid(const struct nfs4_deviceid *id)
  48. {
  49. u32 *p = (u32 *)id;
  50. dprintk("%s: device id= [%x%x%x%x]\n", __func__,
  51. p[0], p[1], p[2], p[3]);
  52. }
  53. EXPORT_SYMBOL_GPL(nfs4_print_deviceid);
  54. #endif
  55. static inline u32
  56. nfs4_deviceid_hash(const struct nfs4_deviceid *id)
  57. {
  58. unsigned char *cptr = (unsigned char *)id->data;
  59. unsigned int nbytes = NFS4_DEVICEID4_SIZE;
  60. u32 x = 0;
  61. while (nbytes--) {
  62. x *= 37;
  63. x += *cptr++;
  64. }
  65. return x & NFS4_DEVICE_ID_HASH_MASK;
  66. }
  67. static struct nfs4_deviceid_node *
  68. _lookup_deviceid(const struct pnfs_layoutdriver_type *ld,
  69. const struct nfs_client *clp, const struct nfs4_deviceid *id,
  70. long hash)
  71. {
  72. struct nfs4_deviceid_node *d;
  73. hlist_for_each_entry_rcu(d, &nfs4_deviceid_cache[hash], node)
  74. if (d->ld == ld && d->nfs_client == clp &&
  75. !memcmp(&d->deviceid, id, sizeof(*id))) {
  76. if (atomic_read(&d->ref))
  77. return d;
  78. else
  79. continue;
  80. }
  81. return NULL;
  82. }
  83. static struct nfs4_deviceid_node *
  84. nfs4_get_device_info(struct nfs_server *server,
  85. const struct nfs4_deviceid *dev_id,
  86. const struct cred *cred, gfp_t gfp_flags)
  87. {
  88. struct nfs4_deviceid_node *d = NULL;
  89. struct pnfs_device *pdev = NULL;
  90. struct page **pages = NULL;
  91. u32 max_resp_sz;
  92. int max_pages;
  93. int rc, i;
  94. /*
  95. * Use the session max response size as the basis for setting
  96. * GETDEVICEINFO's maxcount
  97. */
  98. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  99. max_pages = nfs_page_array_len(0, max_resp_sz);
  100. dprintk("%s: server %p max_resp_sz %u max_pages %d\n",
  101. __func__, server, max_resp_sz, max_pages);
  102. pdev = kzalloc(sizeof(*pdev), gfp_flags);
  103. if (!pdev)
  104. return NULL;
  105. pages = kcalloc(max_pages, sizeof(struct page *), gfp_flags);
  106. if (!pages)
  107. goto out_free_pdev;
  108. for (i = 0; i < max_pages; i++) {
  109. pages[i] = alloc_page(gfp_flags);
  110. if (!pages[i])
  111. goto out_free_pages;
  112. }
  113. memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
  114. pdev->layout_type = server->pnfs_curr_ld->id;
  115. pdev->pages = pages;
  116. pdev->pgbase = 0;
  117. pdev->pglen = max_resp_sz;
  118. pdev->mincount = 0;
  119. pdev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead;
  120. rc = nfs4_proc_getdeviceinfo(server, pdev, cred);
  121. dprintk("%s getdevice info returns %d\n", __func__, rc);
  122. if (rc)
  123. goto out_free_pages;
  124. /*
  125. * Found new device, need to decode it and then add it to the
  126. * list of known devices for this mountpoint.
  127. */
  128. d = server->pnfs_curr_ld->alloc_deviceid_node(server, pdev,
  129. gfp_flags);
  130. if (d && pdev->nocache)
  131. set_bit(NFS_DEVICEID_NOCACHE, &d->flags);
  132. out_free_pages:
  133. while (--i >= 0)
  134. __free_page(pages[i]);
  135. kfree(pages);
  136. out_free_pdev:
  137. kfree(pdev);
  138. dprintk("<-- %s d %p\n", __func__, d);
  139. return d;
  140. }
  141. /*
  142. * Lookup a deviceid in cache and get a reference count on it if found
  143. *
  144. * @clp nfs_client associated with deviceid
  145. * @id deviceid to look up
  146. */
  147. static struct nfs4_deviceid_node *
  148. __nfs4_find_get_deviceid(struct nfs_server *server,
  149. const struct nfs4_deviceid *id, long hash)
  150. {
  151. struct nfs4_deviceid_node *d;
  152. rcu_read_lock();
  153. d = _lookup_deviceid(server->pnfs_curr_ld, server->nfs_client, id,
  154. hash);
  155. if (d != NULL && !atomic_inc_not_zero(&d->ref))
  156. d = NULL;
  157. rcu_read_unlock();
  158. return d;
  159. }
  160. struct nfs4_deviceid_node *
  161. nfs4_find_get_deviceid(struct nfs_server *server,
  162. const struct nfs4_deviceid *id, const struct cred *cred,
  163. gfp_t gfp_mask)
  164. {
  165. long hash = nfs4_deviceid_hash(id);
  166. struct nfs4_deviceid_node *d, *new;
  167. d = __nfs4_find_get_deviceid(server, id, hash);
  168. if (d)
  169. goto found;
  170. new = nfs4_get_device_info(server, id, cred, gfp_mask);
  171. if (!new) {
  172. trace_nfs4_find_deviceid(server, id, -ENOENT);
  173. return new;
  174. }
  175. spin_lock(&nfs4_deviceid_lock);
  176. d = __nfs4_find_get_deviceid(server, id, hash);
  177. if (d) {
  178. spin_unlock(&nfs4_deviceid_lock);
  179. server->pnfs_curr_ld->free_deviceid_node(new);
  180. } else {
  181. atomic_inc(&new->ref);
  182. hlist_add_head_rcu(&new->node, &nfs4_deviceid_cache[hash]);
  183. spin_unlock(&nfs4_deviceid_lock);
  184. d = new;
  185. }
  186. found:
  187. trace_nfs4_find_deviceid(server, id, 0);
  188. return d;
  189. }
  190. EXPORT_SYMBOL_GPL(nfs4_find_get_deviceid);
  191. /*
  192. * Remove a deviceid from cache
  193. *
  194. * @clp nfs_client associated with deviceid
  195. * @id the deviceid to unhash
  196. *
  197. * @ret the unhashed node, if found and dereferenced to zero, NULL otherwise.
  198. */
  199. void
  200. nfs4_delete_deviceid(const struct pnfs_layoutdriver_type *ld,
  201. const struct nfs_client *clp, const struct nfs4_deviceid *id)
  202. {
  203. struct nfs4_deviceid_node *d;
  204. spin_lock(&nfs4_deviceid_lock);
  205. rcu_read_lock();
  206. d = _lookup_deviceid(ld, clp, id, nfs4_deviceid_hash(id));
  207. rcu_read_unlock();
  208. if (!d) {
  209. spin_unlock(&nfs4_deviceid_lock);
  210. return;
  211. }
  212. hlist_del_init_rcu(&d->node);
  213. clear_bit(NFS_DEVICEID_NOCACHE, &d->flags);
  214. spin_unlock(&nfs4_deviceid_lock);
  215. /* balance the initial ref set in pnfs_insert_deviceid */
  216. nfs4_put_deviceid_node(d);
  217. }
  218. EXPORT_SYMBOL_GPL(nfs4_delete_deviceid);
  219. void
  220. nfs4_init_deviceid_node(struct nfs4_deviceid_node *d, struct nfs_server *server,
  221. const struct nfs4_deviceid *id)
  222. {
  223. INIT_HLIST_NODE(&d->node);
  224. INIT_HLIST_NODE(&d->tmpnode);
  225. d->ld = server->pnfs_curr_ld;
  226. d->nfs_client = server->nfs_client;
  227. d->flags = 0;
  228. d->deviceid = *id;
  229. atomic_set(&d->ref, 1);
  230. }
  231. EXPORT_SYMBOL_GPL(nfs4_init_deviceid_node);
  232. /*
  233. * Dereference a deviceid node and delete it when its reference count drops
  234. * to zero.
  235. *
  236. * @d deviceid node to put
  237. *
  238. * return true iff the node was deleted
  239. * Note that since the test for d->ref == 0 is sufficient to establish
  240. * that the node is no longer hashed in the global device id cache.
  241. */
  242. bool
  243. nfs4_put_deviceid_node(struct nfs4_deviceid_node *d)
  244. {
  245. if (test_bit(NFS_DEVICEID_NOCACHE, &d->flags)) {
  246. if (atomic_add_unless(&d->ref, -1, 2))
  247. return false;
  248. nfs4_delete_deviceid(d->ld, d->nfs_client, &d->deviceid);
  249. }
  250. if (!atomic_dec_and_test(&d->ref))
  251. return false;
  252. trace_nfs4_deviceid_free(d->nfs_client, &d->deviceid);
  253. d->ld->free_deviceid_node(d);
  254. return true;
  255. }
  256. EXPORT_SYMBOL_GPL(nfs4_put_deviceid_node);
  257. void
  258. nfs4_mark_deviceid_available(struct nfs4_deviceid_node *node)
  259. {
  260. if (test_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags)) {
  261. clear_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
  262. smp_mb__after_atomic();
  263. }
  264. }
  265. EXPORT_SYMBOL_GPL(nfs4_mark_deviceid_available);
  266. void
  267. nfs4_mark_deviceid_unavailable(struct nfs4_deviceid_node *node)
  268. {
  269. node->timestamp_unavailable = jiffies;
  270. smp_mb__before_atomic();
  271. set_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
  272. smp_mb__after_atomic();
  273. }
  274. EXPORT_SYMBOL_GPL(nfs4_mark_deviceid_unavailable);
  275. bool
  276. nfs4_test_deviceid_unavailable(struct nfs4_deviceid_node *node)
  277. {
  278. if (test_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags)) {
  279. unsigned long start, end;
  280. end = jiffies;
  281. start = end - PNFS_DEVICE_RETRY_TIMEOUT;
  282. if (time_in_range(node->timestamp_unavailable, start, end))
  283. return true;
  284. clear_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
  285. smp_mb__after_atomic();
  286. }
  287. return false;
  288. }
  289. EXPORT_SYMBOL_GPL(nfs4_test_deviceid_unavailable);
  290. static void
  291. _deviceid_purge_client(const struct nfs_client *clp, long hash)
  292. {
  293. struct nfs4_deviceid_node *d;
  294. HLIST_HEAD(tmp);
  295. spin_lock(&nfs4_deviceid_lock);
  296. rcu_read_lock();
  297. hlist_for_each_entry_rcu(d, &nfs4_deviceid_cache[hash], node)
  298. if (d->nfs_client == clp && atomic_read(&d->ref)) {
  299. hlist_del_init_rcu(&d->node);
  300. hlist_add_head(&d->tmpnode, &tmp);
  301. clear_bit(NFS_DEVICEID_NOCACHE, &d->flags);
  302. }
  303. rcu_read_unlock();
  304. spin_unlock(&nfs4_deviceid_lock);
  305. if (hlist_empty(&tmp))
  306. return;
  307. while (!hlist_empty(&tmp)) {
  308. d = hlist_entry(tmp.first, struct nfs4_deviceid_node, tmpnode);
  309. hlist_del(&d->tmpnode);
  310. nfs4_put_deviceid_node(d);
  311. }
  312. }
  313. void
  314. nfs4_deviceid_purge_client(const struct nfs_client *clp)
  315. {
  316. long h;
  317. if (!(clp->cl_exchange_flags & EXCHGID4_FLAG_USE_PNFS_MDS))
  318. return;
  319. for (h = 0; h < NFS4_DEVICE_ID_HASH_SIZE; h++)
  320. _deviceid_purge_client(clp, h);
  321. }
  322. /*
  323. * Stop use of all deviceids associated with an nfs_client
  324. */
  325. void
  326. nfs4_deviceid_mark_client_invalid(struct nfs_client *clp)
  327. {
  328. struct nfs4_deviceid_node *d;
  329. int i;
  330. rcu_read_lock();
  331. for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i ++){
  332. hlist_for_each_entry_rcu(d, &nfs4_deviceid_cache[i], node)
  333. if (d->nfs_client == clp)
  334. set_bit(NFS_DEVICEID_INVALID, &d->flags);
  335. }
  336. rcu_read_unlock();
  337. }