pnfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. atomic_set(&lseg->pls_refcount, 1);
  192. smp_mb();
  193. set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
  194. lseg->pls_layout = lo;
  195. }
  196. static void free_lseg(struct pnfs_layout_segment *lseg)
  197. {
  198. struct inode *ino = lseg->pls_layout->plh_inode;
  199. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  200. /* Matched by get_layout_hdr in pnfs_insert_layout */
  201. put_layout_hdr(ino);
  202. }
  203. /* The use of tmp_list is necessary because pnfs_curr_ld->free_lseg
  204. * could sleep, so must be called outside of the lock.
  205. * Returns 1 if object was removed, otherwise return 0.
  206. */
  207. static int
  208. put_lseg_locked(struct pnfs_layout_segment *lseg,
  209. struct list_head *tmp_list)
  210. {
  211. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  212. atomic_read(&lseg->pls_refcount),
  213. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  214. if (atomic_dec_and_test(&lseg->pls_refcount)) {
  215. struct inode *ino = lseg->pls_layout->plh_inode;
  216. BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  217. list_del(&lseg->pls_list);
  218. if (list_empty(&lseg->pls_layout->plh_segs)) {
  219. struct nfs_client *clp;
  220. clp = NFS_SERVER(ino)->nfs_client;
  221. spin_lock(&clp->cl_lock);
  222. /* List does not take a reference, so no need for put here */
  223. list_del_init(&lseg->pls_layout->plh_layouts);
  224. spin_unlock(&clp->cl_lock);
  225. }
  226. list_add(&lseg->pls_list, tmp_list);
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. static bool
  232. should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
  233. {
  234. return (recall_iomode == IOMODE_ANY ||
  235. lseg_iomode == recall_iomode);
  236. }
  237. /* Returns 1 if lseg is removed from list, 0 otherwise */
  238. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  239. struct list_head *tmp_list)
  240. {
  241. int rv = 0;
  242. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  243. /* Remove the reference keeping the lseg in the
  244. * list. It will now be removed when all
  245. * outstanding io is finished.
  246. */
  247. rv = put_lseg_locked(lseg, tmp_list);
  248. }
  249. return rv;
  250. }
  251. /* Returns count of number of matching invalid lsegs remaining in list
  252. * after call.
  253. */
  254. static int
  255. mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
  256. struct list_head *tmp_list,
  257. u32 iomode)
  258. {
  259. struct pnfs_layout_segment *lseg, *next;
  260. int invalid = 0, removed = 0;
  261. dprintk("%s:Begin lo %p\n", __func__, lo);
  262. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  263. if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
  264. dprintk("%s: freeing lseg %p iomode %d "
  265. "offset %llu length %llu\n", __func__,
  266. lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
  267. lseg->pls_range.length);
  268. invalid++;
  269. removed += mark_lseg_invalid(lseg, tmp_list);
  270. }
  271. dprintk("%s:Return %i\n", __func__, invalid - removed);
  272. return invalid - removed;
  273. }
  274. static void
  275. pnfs_free_lseg_list(struct list_head *free_me)
  276. {
  277. struct pnfs_layout_segment *lseg, *tmp;
  278. list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
  279. list_del(&lseg->pls_list);
  280. free_lseg(lseg);
  281. }
  282. }
  283. void
  284. pnfs_destroy_layout(struct nfs_inode *nfsi)
  285. {
  286. struct pnfs_layout_hdr *lo;
  287. LIST_HEAD(tmp_list);
  288. spin_lock(&nfsi->vfs_inode.i_lock);
  289. lo = nfsi->layout;
  290. if (lo) {
  291. set_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags);
  292. mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
  293. /* Matched by refcount set to 1 in alloc_init_layout_hdr */
  294. put_layout_hdr_locked(lo);
  295. }
  296. spin_unlock(&nfsi->vfs_inode.i_lock);
  297. pnfs_free_lseg_list(&tmp_list);
  298. }
  299. /*
  300. * Called by the state manger to remove all layouts established under an
  301. * expired lease.
  302. */
  303. void
  304. pnfs_destroy_all_layouts(struct nfs_client *clp)
  305. {
  306. struct pnfs_layout_hdr *lo;
  307. LIST_HEAD(tmp_list);
  308. spin_lock(&clp->cl_lock);
  309. list_splice_init(&clp->cl_layouts, &tmp_list);
  310. spin_unlock(&clp->cl_lock);
  311. while (!list_empty(&tmp_list)) {
  312. lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
  313. plh_layouts);
  314. dprintk("%s freeing layout for inode %lu\n", __func__,
  315. lo->plh_inode->i_ino);
  316. pnfs_destroy_layout(NFS_I(lo->plh_inode));
  317. }
  318. }
  319. /* update lo->plh_stateid with new if is more recent */
  320. static void
  321. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo,
  322. const nfs4_stateid *new)
  323. {
  324. u32 oldseq, newseq;
  325. oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  326. newseq = be32_to_cpu(new->stateid.seqid);
  327. if ((int)(newseq - oldseq) > 0)
  328. memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
  329. }
  330. int
  331. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  332. struct nfs4_state *open_state)
  333. {
  334. int status = 0;
  335. dprintk("--> %s\n", __func__);
  336. spin_lock(&lo->plh_inode->i_lock);
  337. if (list_empty(&lo->plh_segs)) {
  338. int seq;
  339. do {
  340. seq = read_seqbegin(&open_state->seqlock);
  341. memcpy(dst->data, open_state->stateid.data,
  342. sizeof(open_state->stateid.data));
  343. } while (read_seqretry(&open_state->seqlock, seq));
  344. } else
  345. memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
  346. spin_unlock(&lo->plh_inode->i_lock);
  347. dprintk("<-- %s\n", __func__);
  348. return status;
  349. }
  350. /*
  351. * Get layout from server.
  352. * for now, assume that whole file layouts are requested.
  353. * arg->offset: 0
  354. * arg->length: all ones
  355. */
  356. static struct pnfs_layout_segment *
  357. send_layoutget(struct pnfs_layout_hdr *lo,
  358. struct nfs_open_context *ctx,
  359. u32 iomode)
  360. {
  361. struct inode *ino = lo->plh_inode;
  362. struct nfs_server *server = NFS_SERVER(ino);
  363. struct nfs4_layoutget *lgp;
  364. struct pnfs_layout_segment *lseg = NULL;
  365. dprintk("--> %s\n", __func__);
  366. BUG_ON(ctx == NULL);
  367. lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
  368. if (lgp == NULL) {
  369. put_layout_hdr(lo->plh_inode);
  370. return NULL;
  371. }
  372. lgp->args.minlength = NFS4_MAX_UINT64;
  373. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  374. lgp->args.range.iomode = iomode;
  375. lgp->args.range.offset = 0;
  376. lgp->args.range.length = NFS4_MAX_UINT64;
  377. lgp->args.type = server->pnfs_curr_ld->id;
  378. lgp->args.inode = ino;
  379. lgp->args.ctx = get_nfs_open_context(ctx);
  380. lgp->lsegpp = &lseg;
  381. /* Synchronously retrieve layout information from server and
  382. * store in lseg.
  383. */
  384. nfs4_proc_layoutget(lgp);
  385. if (!lseg) {
  386. /* remember that LAYOUTGET failed and suspend trying */
  387. set_bit(lo_fail_bit(iomode), &lo->plh_flags);
  388. }
  389. return lseg;
  390. }
  391. /*
  392. * Compare two layout segments for sorting into layout cache.
  393. * We want to preferentially return RW over RO layouts, so ensure those
  394. * are seen first.
  395. */
  396. static s64
  397. cmp_layout(u32 iomode1, u32 iomode2)
  398. {
  399. /* read > read/write */
  400. return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
  401. }
  402. static void
  403. pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  404. struct pnfs_layout_segment *lseg)
  405. {
  406. struct pnfs_layout_segment *lp;
  407. int found = 0;
  408. dprintk("%s:Begin\n", __func__);
  409. assert_spin_locked(&lo->plh_inode->i_lock);
  410. if (list_empty(&lo->plh_segs)) {
  411. struct nfs_client *clp = NFS_SERVER(lo->plh_inode)->nfs_client;
  412. spin_lock(&clp->cl_lock);
  413. BUG_ON(!list_empty(&lo->plh_layouts));
  414. list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
  415. spin_unlock(&clp->cl_lock);
  416. }
  417. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  418. if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
  419. continue;
  420. list_add_tail(&lseg->pls_list, &lp->pls_list);
  421. dprintk("%s: inserted lseg %p "
  422. "iomode %d offset %llu length %llu before "
  423. "lp %p iomode %d offset %llu length %llu\n",
  424. __func__, lseg, lseg->pls_range.iomode,
  425. lseg->pls_range.offset, lseg->pls_range.length,
  426. lp, lp->pls_range.iomode, lp->pls_range.offset,
  427. lp->pls_range.length);
  428. found = 1;
  429. break;
  430. }
  431. if (!found) {
  432. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  433. dprintk("%s: inserted lseg %p "
  434. "iomode %d offset %llu length %llu at tail\n",
  435. __func__, lseg, lseg->pls_range.iomode,
  436. lseg->pls_range.offset, lseg->pls_range.length);
  437. }
  438. get_layout_hdr_locked(lo);
  439. dprintk("%s:Return\n", __func__);
  440. }
  441. static struct pnfs_layout_hdr *
  442. alloc_init_layout_hdr(struct inode *ino)
  443. {
  444. struct pnfs_layout_hdr *lo;
  445. lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
  446. if (!lo)
  447. return NULL;
  448. lo->plh_refcount = 1;
  449. INIT_LIST_HEAD(&lo->plh_layouts);
  450. INIT_LIST_HEAD(&lo->plh_segs);
  451. lo->plh_inode = ino;
  452. return lo;
  453. }
  454. static struct pnfs_layout_hdr *
  455. pnfs_find_alloc_layout(struct inode *ino)
  456. {
  457. struct nfs_inode *nfsi = NFS_I(ino);
  458. struct pnfs_layout_hdr *new = NULL;
  459. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  460. assert_spin_locked(&ino->i_lock);
  461. if (nfsi->layout) {
  462. if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
  463. return NULL;
  464. else
  465. return nfsi->layout;
  466. }
  467. spin_unlock(&ino->i_lock);
  468. new = alloc_init_layout_hdr(ino);
  469. spin_lock(&ino->i_lock);
  470. if (likely(nfsi->layout == NULL)) /* Won the race? */
  471. nfsi->layout = new;
  472. else
  473. kfree(new);
  474. return nfsi->layout;
  475. }
  476. /*
  477. * iomode matching rules:
  478. * iomode lseg match
  479. * ----- ----- -----
  480. * ANY READ true
  481. * ANY RW true
  482. * RW READ false
  483. * RW RW true
  484. * READ READ true
  485. * READ RW true
  486. */
  487. static int
  488. is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
  489. {
  490. return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
  491. }
  492. /*
  493. * lookup range in layout
  494. */
  495. static struct pnfs_layout_segment *
  496. pnfs_has_layout(struct pnfs_layout_hdr *lo, u32 iomode)
  497. {
  498. struct pnfs_layout_segment *lseg, *ret = NULL;
  499. dprintk("%s:Begin\n", __func__);
  500. assert_spin_locked(&lo->plh_inode->i_lock);
  501. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  502. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  503. is_matching_lseg(lseg, iomode)) {
  504. ret = lseg;
  505. break;
  506. }
  507. if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
  508. break;
  509. }
  510. dprintk("%s:Return lseg %p ref %d\n",
  511. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  512. return ret;
  513. }
  514. /*
  515. * Layout segment is retreived from the server if not cached.
  516. * The appropriate layout segment is referenced and returned to the caller.
  517. */
  518. struct pnfs_layout_segment *
  519. pnfs_update_layout(struct inode *ino,
  520. struct nfs_open_context *ctx,
  521. enum pnfs_iomode iomode)
  522. {
  523. struct nfs_inode *nfsi = NFS_I(ino);
  524. struct pnfs_layout_hdr *lo;
  525. struct pnfs_layout_segment *lseg = NULL;
  526. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  527. return NULL;
  528. spin_lock(&ino->i_lock);
  529. lo = pnfs_find_alloc_layout(ino);
  530. if (lo == NULL) {
  531. dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
  532. goto out_unlock;
  533. }
  534. /* Check to see if the layout for the given range already exists */
  535. lseg = pnfs_has_layout(lo, iomode);
  536. if (lseg) {
  537. dprintk("%s: Using cached lseg %p for iomode %d)\n",
  538. __func__, lseg, iomode);
  539. goto out_unlock;
  540. }
  541. /* if LAYOUTGET already failed once we don't try again */
  542. if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
  543. goto out_unlock;
  544. get_layout_hdr_locked(lo); /* Matched in nfs4_layoutget_release */
  545. spin_unlock(&ino->i_lock);
  546. lseg = send_layoutget(lo, ctx, iomode);
  547. out:
  548. dprintk("%s end, state 0x%lx lseg %p\n", __func__,
  549. nfsi->layout->plh_flags, lseg);
  550. return lseg;
  551. out_unlock:
  552. spin_unlock(&ino->i_lock);
  553. goto out;
  554. }
  555. int
  556. pnfs_layout_process(struct nfs4_layoutget *lgp)
  557. {
  558. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  559. struct nfs4_layoutget_res *res = &lgp->res;
  560. struct pnfs_layout_segment *lseg;
  561. struct inode *ino = lo->plh_inode;
  562. int status = 0;
  563. /* Inject layout blob into I/O device driver */
  564. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
  565. if (!lseg || IS_ERR(lseg)) {
  566. if (!lseg)
  567. status = -ENOMEM;
  568. else
  569. status = PTR_ERR(lseg);
  570. dprintk("%s: Could not allocate layout: error %d\n",
  571. __func__, status);
  572. goto out;
  573. }
  574. spin_lock(&ino->i_lock);
  575. init_lseg(lo, lseg);
  576. lseg->pls_range = res->range;
  577. *lgp->lsegpp = lseg;
  578. pnfs_insert_layout(lo, lseg);
  579. /* Done processing layoutget. Set the layout stateid */
  580. pnfs_set_layout_stateid(lo, &res->stateid);
  581. spin_unlock(&ino->i_lock);
  582. out:
  583. return status;
  584. }
  585. /*
  586. * Device ID cache. Currently supports one layout type per struct nfs_client.
  587. * Add layout type to the lookup key to expand to support multiple types.
  588. */
  589. int
  590. pnfs_alloc_init_deviceid_cache(struct nfs_client *clp,
  591. void (*free_callback)(struct pnfs_deviceid_node *))
  592. {
  593. struct pnfs_deviceid_cache *c;
  594. c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL);
  595. if (!c)
  596. return -ENOMEM;
  597. spin_lock(&clp->cl_lock);
  598. if (clp->cl_devid_cache != NULL) {
  599. atomic_inc(&clp->cl_devid_cache->dc_ref);
  600. dprintk("%s [kref [%d]]\n", __func__,
  601. atomic_read(&clp->cl_devid_cache->dc_ref));
  602. kfree(c);
  603. } else {
  604. /* kzalloc initializes hlists */
  605. spin_lock_init(&c->dc_lock);
  606. atomic_set(&c->dc_ref, 1);
  607. c->dc_free_callback = free_callback;
  608. clp->cl_devid_cache = c;
  609. dprintk("%s [new]\n", __func__);
  610. }
  611. spin_unlock(&clp->cl_lock);
  612. return 0;
  613. }
  614. EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache);
  615. /*
  616. * Called from pnfs_layoutdriver_type->free_lseg
  617. * last layout segment reference frees deviceid
  618. */
  619. void
  620. pnfs_put_deviceid(struct pnfs_deviceid_cache *c,
  621. struct pnfs_deviceid_node *devid)
  622. {
  623. struct nfs4_deviceid *id = &devid->de_id;
  624. struct pnfs_deviceid_node *d;
  625. struct hlist_node *n;
  626. long h = nfs4_deviceid_hash(id);
  627. dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref));
  628. if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock))
  629. return;
  630. hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node)
  631. if (!memcmp(&d->de_id, id, sizeof(*id))) {
  632. hlist_del_rcu(&d->de_node);
  633. spin_unlock(&c->dc_lock);
  634. synchronize_rcu();
  635. c->dc_free_callback(devid);
  636. return;
  637. }
  638. spin_unlock(&c->dc_lock);
  639. /* Why wasn't it found in the list? */
  640. BUG();
  641. }
  642. EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
  643. /* Find and reference a deviceid */
  644. struct pnfs_deviceid_node *
  645. pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id)
  646. {
  647. struct pnfs_deviceid_node *d;
  648. struct hlist_node *n;
  649. long hash = nfs4_deviceid_hash(id);
  650. dprintk("--> %s hash %ld\n", __func__, hash);
  651. rcu_read_lock();
  652. hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) {
  653. if (!memcmp(&d->de_id, id, sizeof(*id))) {
  654. if (!atomic_inc_not_zero(&d->de_ref)) {
  655. goto fail;
  656. } else {
  657. rcu_read_unlock();
  658. return d;
  659. }
  660. }
  661. }
  662. fail:
  663. rcu_read_unlock();
  664. return NULL;
  665. }
  666. EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid);
  667. /*
  668. * Add a deviceid to the cache.
  669. * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new
  670. */
  671. struct pnfs_deviceid_node *
  672. pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new)
  673. {
  674. struct pnfs_deviceid_node *d;
  675. long hash = nfs4_deviceid_hash(&new->de_id);
  676. dprintk("--> %s hash %ld\n", __func__, hash);
  677. spin_lock(&c->dc_lock);
  678. d = pnfs_find_get_deviceid(c, &new->de_id);
  679. if (d) {
  680. spin_unlock(&c->dc_lock);
  681. dprintk("%s [discard]\n", __func__);
  682. c->dc_free_callback(new);
  683. return d;
  684. }
  685. INIT_HLIST_NODE(&new->de_node);
  686. atomic_set(&new->de_ref, 1);
  687. hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]);
  688. spin_unlock(&c->dc_lock);
  689. dprintk("%s [new]\n", __func__);
  690. return new;
  691. }
  692. EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
  693. void
  694. pnfs_put_deviceid_cache(struct nfs_client *clp)
  695. {
  696. struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
  697. dprintk("--> %s cl_devid_cache %p\n", __func__, clp->cl_devid_cache);
  698. if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) {
  699. int i;
  700. /* Verify cache is empty */
  701. for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++)
  702. BUG_ON(!hlist_empty(&local->dc_deviceids[i]));
  703. clp->cl_devid_cache = NULL;
  704. spin_unlock(&clp->cl_lock);
  705. kfree(local);
  706. }
  707. }
  708. EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);