pnfs.c 25 KB

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