pnfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * pNFS functions to call and manage layout drivers.
  3. *
  4. * Copyright (c) 2002 [year of first publication]
  5. * The Regents of the University of Michigan
  6. * All Rights Reserved
  7. *
  8. * Dean Hildebrand <dhildebz@umich.edu>
  9. *
  10. * Permission is granted to use, copy, create derivative works, and
  11. * redistribute this software and such derivative works for any purpose,
  12. * so long as the name of the University of Michigan is not used in
  13. * any advertising or publicity pertaining to the use or distribution
  14. * of this software without specific, written prior authorization. If
  15. * the above copyright notice or any other identification of the
  16. * University of Michigan is included in any copy of any portion of
  17. * this software, then the disclaimer below must also be included.
  18. *
  19. * This software is provided as is, without representation or warranty
  20. * of any kind either express or implied, including without limitation
  21. * the implied warranties of merchantability, fitness for a particular
  22. * purpose, or noninfringement. The Regents of the University of
  23. * Michigan shall not be liable for any damages, including special,
  24. * indirect, incidental, or consequential damages, with respect to any
  25. * claim arising out of or in connection with the use of the software,
  26. * even if it has been or is hereafter advised of the possibility of
  27. * such damages.
  28. */
  29. #include <linux/nfs_fs.h>
  30. #include "internal.h"
  31. #include "pnfs.h"
  32. #define NFSDBG_FACILITY NFSDBG_PNFS
  33. /* Locking:
  34. *
  35. * pnfs_spinlock:
  36. * protects pnfs_modules_tbl.
  37. */
  38. static DEFINE_SPINLOCK(pnfs_spinlock);
  39. /*
  40. * pnfs_modules_tbl holds all pnfs modules
  41. */
  42. static LIST_HEAD(pnfs_modules_tbl);
  43. /* Return the registered pnfs layout driver module matching given id */
  44. static struct pnfs_layoutdriver_type *
  45. find_pnfs_driver_locked(u32 id)
  46. {
  47. struct pnfs_layoutdriver_type *local;
  48. list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
  49. if (local->id == id)
  50. goto out;
  51. local = NULL;
  52. out:
  53. dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
  54. return local;
  55. }
  56. static struct pnfs_layoutdriver_type *
  57. find_pnfs_driver(u32 id)
  58. {
  59. struct pnfs_layoutdriver_type *local;
  60. spin_lock(&pnfs_spinlock);
  61. local = find_pnfs_driver_locked(id);
  62. spin_unlock(&pnfs_spinlock);
  63. return local;
  64. }
  65. void
  66. unset_pnfs_layoutdriver(struct nfs_server *nfss)
  67. {
  68. if (nfss->pnfs_curr_ld) {
  69. nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
  70. module_put(nfss->pnfs_curr_ld->owner);
  71. }
  72. nfss->pnfs_curr_ld = NULL;
  73. }
  74. /*
  75. * Try to set the server's pnfs module to the pnfs layout type specified by id.
  76. * Currently only one pNFS layout driver per filesystem is supported.
  77. *
  78. * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
  79. */
  80. void
  81. set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
  82. {
  83. struct pnfs_layoutdriver_type *ld_type = NULL;
  84. if (id == 0)
  85. goto out_no_driver;
  86. if (!(server->nfs_client->cl_exchange_flags &
  87. (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
  88. printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
  89. id, server->nfs_client->cl_exchange_flags);
  90. goto out_no_driver;
  91. }
  92. ld_type = find_pnfs_driver(id);
  93. if (!ld_type) {
  94. request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
  95. ld_type = find_pnfs_driver(id);
  96. if (!ld_type) {
  97. dprintk("%s: No pNFS module found for %u.\n",
  98. __func__, id);
  99. goto out_no_driver;
  100. }
  101. }
  102. if (!try_module_get(ld_type->owner)) {
  103. dprintk("%s: Could not grab reference on module\n", __func__);
  104. goto out_no_driver;
  105. }
  106. server->pnfs_curr_ld = ld_type;
  107. if (ld_type->set_layoutdriver(server)) {
  108. printk(KERN_ERR
  109. "%s: Error initializing mount point for layout driver %u.\n",
  110. __func__, id);
  111. module_put(ld_type->owner);
  112. goto out_no_driver;
  113. }
  114. dprintk("%s: pNFS module for %u set\n", __func__, id);
  115. return;
  116. out_no_driver:
  117. dprintk("%s: Using NFSv4 I/O\n", __func__);
  118. server->pnfs_curr_ld = NULL;
  119. }
  120. int
  121. pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  122. {
  123. int status = -EINVAL;
  124. struct pnfs_layoutdriver_type *tmp;
  125. if (ld_type->id == 0) {
  126. printk(KERN_ERR "%s id 0 is reserved\n", __func__);
  127. return status;
  128. }
  129. if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
  130. printk(KERN_ERR "%s Layout driver must provide "
  131. "alloc_lseg and free_lseg.\n", __func__);
  132. return status;
  133. }
  134. spin_lock(&pnfs_spinlock);
  135. tmp = find_pnfs_driver_locked(ld_type->id);
  136. if (!tmp) {
  137. list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
  138. status = 0;
  139. dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
  140. ld_type->name);
  141. } else {
  142. printk(KERN_ERR "%s Module with id %d already loaded!\n",
  143. __func__, ld_type->id);
  144. }
  145. spin_unlock(&pnfs_spinlock);
  146. return status;
  147. }
  148. EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
  149. void
  150. pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  151. {
  152. dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
  153. spin_lock(&pnfs_spinlock);
  154. list_del(&ld_type->pnfs_tblid);
  155. spin_unlock(&pnfs_spinlock);
  156. }
  157. EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
  158. /*
  159. * pNFS client layout cache
  160. */
  161. static void
  162. get_layout_hdr_locked(struct pnfs_layout_hdr *lo)
  163. {
  164. assert_spin_locked(&lo->plh_inode->i_lock);
  165. lo->plh_refcount++;
  166. }
  167. static void
  168. put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
  169. {
  170. assert_spin_locked(&lo->plh_inode->i_lock);
  171. BUG_ON(lo->plh_refcount == 0);
  172. lo->plh_refcount--;
  173. if (!lo->plh_refcount) {
  174. dprintk("%s: freeing layout cache %p\n", __func__, lo);
  175. BUG_ON(!list_empty(&lo->plh_layouts));
  176. NFS_I(lo->plh_inode)->layout = NULL;
  177. kfree(lo);
  178. }
  179. }
  180. void
  181. put_layout_hdr(struct inode *inode)
  182. {
  183. spin_lock(&inode->i_lock);
  184. put_layout_hdr_locked(NFS_I(inode)->layout);
  185. spin_unlock(&inode->i_lock);
  186. }
  187. static void
  188. init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
  189. {
  190. INIT_LIST_HEAD(&lseg->pls_list);
  191. kref_init(&lseg->pls_refcount);
  192. lseg->pls_layout = lo;
  193. }
  194. /* Called without i_lock held, as the free_lseg call may sleep */
  195. static void
  196. destroy_lseg(struct kref *kref)
  197. {
  198. struct pnfs_layout_segment *lseg =
  199. container_of(kref, struct pnfs_layout_segment, pls_refcount);
  200. struct inode *ino = lseg->pls_layout->plh_inode;
  201. dprintk("--> %s\n", __func__);
  202. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  203. /* Matched by get_layout_hdr in pnfs_insert_layout */
  204. put_layout_hdr(ino);
  205. }
  206. static void
  207. put_lseg(struct pnfs_layout_segment *lseg)
  208. {
  209. if (!lseg)
  210. return;
  211. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  212. atomic_read(&lseg->pls_refcount.refcount));
  213. kref_put(&lseg->pls_refcount, destroy_lseg);
  214. }
  215. static void
  216. pnfs_clear_lseg_list(struct pnfs_layout_hdr *lo, struct list_head *tmp_list)
  217. {
  218. struct pnfs_layout_segment *lseg, *next;
  219. struct nfs_client *clp;
  220. dprintk("%s:Begin lo %p\n", __func__, lo);
  221. assert_spin_locked(&lo->plh_inode->i_lock);
  222. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
  223. dprintk("%s: freeing lseg %p\n", __func__, lseg);
  224. list_move(&lseg->pls_list, tmp_list);
  225. }
  226. clp = NFS_SERVER(lo->plh_inode)->nfs_client;
  227. spin_lock(&clp->cl_lock);
  228. /* List does not take a reference, so no need for put here */
  229. list_del_init(&lo->plh_layouts);
  230. spin_unlock(&clp->cl_lock);
  231. dprintk("%s:Return\n", __func__);
  232. }
  233. static void
  234. pnfs_free_lseg_list(struct list_head *tmp_list)
  235. {
  236. struct pnfs_layout_segment *lseg;
  237. while (!list_empty(tmp_list)) {
  238. lseg = list_entry(tmp_list->next, struct pnfs_layout_segment,
  239. pls_list);
  240. dprintk("%s calling put_lseg on %p\n", __func__, lseg);
  241. list_del(&lseg->pls_list);
  242. put_lseg(lseg);
  243. }
  244. }
  245. void
  246. pnfs_destroy_layout(struct nfs_inode *nfsi)
  247. {
  248. struct pnfs_layout_hdr *lo;
  249. LIST_HEAD(tmp_list);
  250. spin_lock(&nfsi->vfs_inode.i_lock);
  251. lo = nfsi->layout;
  252. if (lo) {
  253. pnfs_clear_lseg_list(lo, &tmp_list);
  254. /* Matched by refcount set to 1 in alloc_init_layout_hdr */
  255. put_layout_hdr_locked(lo);
  256. }
  257. spin_unlock(&nfsi->vfs_inode.i_lock);
  258. pnfs_free_lseg_list(&tmp_list);
  259. }
  260. /*
  261. * Called by the state manger to remove all layouts established under an
  262. * expired lease.
  263. */
  264. void
  265. pnfs_destroy_all_layouts(struct nfs_client *clp)
  266. {
  267. struct pnfs_layout_hdr *lo;
  268. LIST_HEAD(tmp_list);
  269. spin_lock(&clp->cl_lock);
  270. list_splice_init(&clp->cl_layouts, &tmp_list);
  271. spin_unlock(&clp->cl_lock);
  272. while (!list_empty(&tmp_list)) {
  273. lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
  274. plh_layouts);
  275. dprintk("%s freeing layout for inode %lu\n", __func__,
  276. lo->plh_inode->i_ino);
  277. pnfs_destroy_layout(NFS_I(lo->plh_inode));
  278. }
  279. }
  280. /* update lo->plh_stateid with new if is more recent */
  281. static void
  282. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo,
  283. const nfs4_stateid *new)
  284. {
  285. u32 oldseq, newseq;
  286. oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  287. newseq = be32_to_cpu(new->stateid.seqid);
  288. if ((int)(newseq - oldseq) > 0)
  289. memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
  290. }
  291. int
  292. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  293. struct nfs4_state *open_state)
  294. {
  295. int status = 0;
  296. dprintk("--> %s\n", __func__);
  297. spin_lock(&lo->plh_inode->i_lock);
  298. if (list_empty(&lo->plh_segs)) {
  299. int seq;
  300. do {
  301. seq = read_seqbegin(&open_state->seqlock);
  302. memcpy(dst->data, open_state->stateid.data,
  303. sizeof(open_state->stateid.data));
  304. } while (read_seqretry(&open_state->seqlock, seq));
  305. } else
  306. memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
  307. spin_unlock(&lo->plh_inode->i_lock);
  308. dprintk("<-- %s\n", __func__);
  309. return status;
  310. }
  311. /*
  312. * Get layout from server.
  313. * for now, assume that whole file layouts are requested.
  314. * arg->offset: 0
  315. * arg->length: all ones
  316. */
  317. static struct pnfs_layout_segment *
  318. send_layoutget(struct pnfs_layout_hdr *lo,
  319. struct nfs_open_context *ctx,
  320. u32 iomode)
  321. {
  322. struct inode *ino = lo->plh_inode;
  323. struct nfs_server *server = NFS_SERVER(ino);
  324. struct nfs4_layoutget *lgp;
  325. struct pnfs_layout_segment *lseg = NULL;
  326. dprintk("--> %s\n", __func__);
  327. BUG_ON(ctx == NULL);
  328. lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
  329. if (lgp == NULL) {
  330. put_layout_hdr(lo->plh_inode);
  331. return NULL;
  332. }
  333. lgp->args.minlength = NFS4_MAX_UINT64;
  334. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  335. lgp->args.range.iomode = iomode;
  336. lgp->args.range.offset = 0;
  337. lgp->args.range.length = NFS4_MAX_UINT64;
  338. lgp->args.type = server->pnfs_curr_ld->id;
  339. lgp->args.inode = ino;
  340. lgp->args.ctx = get_nfs_open_context(ctx);
  341. lgp->lsegpp = &lseg;
  342. /* Synchronously retrieve layout information from server and
  343. * store in lseg.
  344. */
  345. nfs4_proc_layoutget(lgp);
  346. if (!lseg) {
  347. /* remember that LAYOUTGET failed and suspend trying */
  348. set_bit(lo_fail_bit(iomode), &lo->plh_flags);
  349. }
  350. return lseg;
  351. }
  352. /*
  353. * Compare two layout segments for sorting into layout cache.
  354. * We want to preferentially return RW over RO layouts, so ensure those
  355. * are seen first.
  356. */
  357. static s64
  358. cmp_layout(u32 iomode1, u32 iomode2)
  359. {
  360. /* read > read/write */
  361. return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
  362. }
  363. static void
  364. pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  365. struct pnfs_layout_segment *lseg)
  366. {
  367. struct pnfs_layout_segment *lp;
  368. int found = 0;
  369. dprintk("%s:Begin\n", __func__);
  370. assert_spin_locked(&lo->plh_inode->i_lock);
  371. if (list_empty(&lo->plh_segs)) {
  372. struct nfs_client *clp = NFS_SERVER(lo->plh_inode)->nfs_client;
  373. spin_lock(&clp->cl_lock);
  374. BUG_ON(!list_empty(&lo->plh_layouts));
  375. list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
  376. spin_unlock(&clp->cl_lock);
  377. }
  378. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  379. if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
  380. continue;
  381. list_add_tail(&lseg->pls_list, &lp->pls_list);
  382. dprintk("%s: inserted lseg %p "
  383. "iomode %d offset %llu length %llu before "
  384. "lp %p iomode %d offset %llu length %llu\n",
  385. __func__, lseg, lseg->pls_range.iomode,
  386. lseg->pls_range.offset, lseg->pls_range.length,
  387. lp, lp->pls_range.iomode, lp->pls_range.offset,
  388. lp->pls_range.length);
  389. found = 1;
  390. break;
  391. }
  392. if (!found) {
  393. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  394. dprintk("%s: inserted lseg %p "
  395. "iomode %d offset %llu length %llu at tail\n",
  396. __func__, lseg, lseg->pls_range.iomode,
  397. lseg->pls_range.offset, lseg->pls_range.length);
  398. }
  399. get_layout_hdr_locked(lo);
  400. dprintk("%s:Return\n", __func__);
  401. }
  402. static struct pnfs_layout_hdr *
  403. alloc_init_layout_hdr(struct inode *ino)
  404. {
  405. struct pnfs_layout_hdr *lo;
  406. lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
  407. if (!lo)
  408. return NULL;
  409. lo->plh_refcount = 1;
  410. INIT_LIST_HEAD(&lo->plh_layouts);
  411. INIT_LIST_HEAD(&lo->plh_segs);
  412. lo->plh_inode = ino;
  413. return lo;
  414. }
  415. static struct pnfs_layout_hdr *
  416. pnfs_find_alloc_layout(struct inode *ino)
  417. {
  418. struct nfs_inode *nfsi = NFS_I(ino);
  419. struct pnfs_layout_hdr *new = NULL;
  420. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  421. assert_spin_locked(&ino->i_lock);
  422. if (nfsi->layout)
  423. return nfsi->layout;
  424. spin_unlock(&ino->i_lock);
  425. new = alloc_init_layout_hdr(ino);
  426. spin_lock(&ino->i_lock);
  427. if (likely(nfsi->layout == NULL)) /* Won the race? */
  428. nfsi->layout = new;
  429. else
  430. kfree(new);
  431. return nfsi->layout;
  432. }
  433. /*
  434. * iomode matching rules:
  435. * iomode lseg match
  436. * ----- ----- -----
  437. * ANY READ true
  438. * ANY RW true
  439. * RW READ false
  440. * RW RW true
  441. * READ READ true
  442. * READ RW true
  443. */
  444. static int
  445. is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
  446. {
  447. return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
  448. }
  449. /*
  450. * lookup range in layout
  451. */
  452. static struct pnfs_layout_segment *
  453. pnfs_has_layout(struct pnfs_layout_hdr *lo, u32 iomode)
  454. {
  455. struct pnfs_layout_segment *lseg, *ret = NULL;
  456. dprintk("%s:Begin\n", __func__);
  457. assert_spin_locked(&lo->plh_inode->i_lock);
  458. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  459. if (is_matching_lseg(lseg, iomode)) {
  460. ret = lseg;
  461. break;
  462. }
  463. if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
  464. break;
  465. }
  466. dprintk("%s:Return lseg %p ref %d\n",
  467. __func__, ret, ret ? atomic_read(&ret->pls_refcount.refcount) : 0);
  468. return ret;
  469. }
  470. /*
  471. * Layout segment is retreived from the server if not cached.
  472. * The appropriate layout segment is referenced and returned to the caller.
  473. */
  474. struct pnfs_layout_segment *
  475. pnfs_update_layout(struct inode *ino,
  476. struct nfs_open_context *ctx,
  477. enum pnfs_iomode iomode)
  478. {
  479. struct nfs_inode *nfsi = NFS_I(ino);
  480. struct pnfs_layout_hdr *lo;
  481. struct pnfs_layout_segment *lseg = NULL;
  482. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  483. return NULL;
  484. spin_lock(&ino->i_lock);
  485. lo = pnfs_find_alloc_layout(ino);
  486. if (lo == NULL) {
  487. dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
  488. goto out_unlock;
  489. }
  490. /* Check to see if the layout for the given range already exists */
  491. lseg = pnfs_has_layout(lo, iomode);
  492. if (lseg) {
  493. dprintk("%s: Using cached lseg %p for iomode %d)\n",
  494. __func__, lseg, iomode);
  495. goto out_unlock;
  496. }
  497. /* if LAYOUTGET already failed once we don't try again */
  498. if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
  499. goto out_unlock;
  500. get_layout_hdr_locked(lo); /* Matched in nfs4_layoutget_release */
  501. spin_unlock(&ino->i_lock);
  502. lseg = send_layoutget(lo, ctx, iomode);
  503. out:
  504. dprintk("%s end, state 0x%lx lseg %p\n", __func__,
  505. nfsi->layout->plh_flags, lseg);
  506. return lseg;
  507. out_unlock:
  508. spin_unlock(&ino->i_lock);
  509. goto out;
  510. }
  511. int
  512. pnfs_layout_process(struct nfs4_layoutget *lgp)
  513. {
  514. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  515. struct nfs4_layoutget_res *res = &lgp->res;
  516. struct pnfs_layout_segment *lseg;
  517. struct inode *ino = lo->plh_inode;
  518. int status = 0;
  519. /* Inject layout blob into I/O device driver */
  520. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
  521. if (!lseg || IS_ERR(lseg)) {
  522. if (!lseg)
  523. status = -ENOMEM;
  524. else
  525. status = PTR_ERR(lseg);
  526. dprintk("%s: Could not allocate layout: error %d\n",
  527. __func__, status);
  528. goto out;
  529. }
  530. spin_lock(&ino->i_lock);
  531. init_lseg(lo, lseg);
  532. lseg->pls_range = res->range;
  533. *lgp->lsegpp = lseg;
  534. pnfs_insert_layout(lo, lseg);
  535. /* Done processing layoutget. Set the layout stateid */
  536. pnfs_set_layout_stateid(lo, &res->stateid);
  537. spin_unlock(&ino->i_lock);
  538. out:
  539. return status;
  540. }
  541. /*
  542. * Device ID cache. Currently supports one layout type per struct nfs_client.
  543. * Add layout type to the lookup key to expand to support multiple types.
  544. */
  545. int
  546. pnfs_alloc_init_deviceid_cache(struct nfs_client *clp,
  547. void (*free_callback)(struct pnfs_deviceid_node *))
  548. {
  549. struct pnfs_deviceid_cache *c;
  550. c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL);
  551. if (!c)
  552. return -ENOMEM;
  553. spin_lock(&clp->cl_lock);
  554. if (clp->cl_devid_cache != NULL) {
  555. atomic_inc(&clp->cl_devid_cache->dc_ref);
  556. dprintk("%s [kref [%d]]\n", __func__,
  557. atomic_read(&clp->cl_devid_cache->dc_ref));
  558. kfree(c);
  559. } else {
  560. /* kzalloc initializes hlists */
  561. spin_lock_init(&c->dc_lock);
  562. atomic_set(&c->dc_ref, 1);
  563. c->dc_free_callback = free_callback;
  564. clp->cl_devid_cache = c;
  565. dprintk("%s [new]\n", __func__);
  566. }
  567. spin_unlock(&clp->cl_lock);
  568. return 0;
  569. }
  570. EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache);
  571. /*
  572. * Called from pnfs_layoutdriver_type->free_lseg
  573. * last layout segment reference frees deviceid
  574. */
  575. void
  576. pnfs_put_deviceid(struct pnfs_deviceid_cache *c,
  577. struct pnfs_deviceid_node *devid)
  578. {
  579. struct nfs4_deviceid *id = &devid->de_id;
  580. struct pnfs_deviceid_node *d;
  581. struct hlist_node *n;
  582. long h = nfs4_deviceid_hash(id);
  583. dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref));
  584. if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock))
  585. return;
  586. hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node)
  587. if (!memcmp(&d->de_id, id, sizeof(*id))) {
  588. hlist_del_rcu(&d->de_node);
  589. spin_unlock(&c->dc_lock);
  590. synchronize_rcu();
  591. c->dc_free_callback(devid);
  592. return;
  593. }
  594. spin_unlock(&c->dc_lock);
  595. /* Why wasn't it found in the list? */
  596. BUG();
  597. }
  598. EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
  599. /* Find and reference a deviceid */
  600. struct pnfs_deviceid_node *
  601. pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id)
  602. {
  603. struct pnfs_deviceid_node *d;
  604. struct hlist_node *n;
  605. long hash = nfs4_deviceid_hash(id);
  606. dprintk("--> %s hash %ld\n", __func__, hash);
  607. rcu_read_lock();
  608. hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) {
  609. if (!memcmp(&d->de_id, id, sizeof(*id))) {
  610. if (!atomic_inc_not_zero(&d->de_ref)) {
  611. goto fail;
  612. } else {
  613. rcu_read_unlock();
  614. return d;
  615. }
  616. }
  617. }
  618. fail:
  619. rcu_read_unlock();
  620. return NULL;
  621. }
  622. EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid);
  623. /*
  624. * Add a deviceid to the cache.
  625. * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new
  626. */
  627. struct pnfs_deviceid_node *
  628. pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new)
  629. {
  630. struct pnfs_deviceid_node *d;
  631. long hash = nfs4_deviceid_hash(&new->de_id);
  632. dprintk("--> %s hash %ld\n", __func__, hash);
  633. spin_lock(&c->dc_lock);
  634. d = pnfs_find_get_deviceid(c, &new->de_id);
  635. if (d) {
  636. spin_unlock(&c->dc_lock);
  637. dprintk("%s [discard]\n", __func__);
  638. c->dc_free_callback(new);
  639. return d;
  640. }
  641. INIT_HLIST_NODE(&new->de_node);
  642. atomic_set(&new->de_ref, 1);
  643. hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]);
  644. spin_unlock(&c->dc_lock);
  645. dprintk("%s [new]\n", __func__);
  646. return new;
  647. }
  648. EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
  649. void
  650. pnfs_put_deviceid_cache(struct nfs_client *clp)
  651. {
  652. struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
  653. dprintk("--> %s cl_devid_cache %p\n", __func__, clp->cl_devid_cache);
  654. if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) {
  655. int i;
  656. /* Verify cache is empty */
  657. for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++)
  658. BUG_ON(!hlist_empty(&local->dc_deviceids[i]));
  659. clp->cl_devid_cache = NULL;
  660. spin_unlock(&clp->cl_lock);
  661. kfree(local);
  662. }
  663. }
  664. EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);