pnfs.c 23 KB

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