pnfs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. #include "iostat.h"
  33. #define NFSDBG_FACILITY NFSDBG_PNFS
  34. /* Locking:
  35. *
  36. * pnfs_spinlock:
  37. * protects pnfs_modules_tbl.
  38. */
  39. static DEFINE_SPINLOCK(pnfs_spinlock);
  40. /*
  41. * pnfs_modules_tbl holds all pnfs modules
  42. */
  43. static LIST_HEAD(pnfs_modules_tbl);
  44. /* Return the registered pnfs layout driver module matching given id */
  45. static struct pnfs_layoutdriver_type *
  46. find_pnfs_driver_locked(u32 id)
  47. {
  48. struct pnfs_layoutdriver_type *local;
  49. list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
  50. if (local->id == id)
  51. goto out;
  52. local = NULL;
  53. out:
  54. dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
  55. return local;
  56. }
  57. static struct pnfs_layoutdriver_type *
  58. find_pnfs_driver(u32 id)
  59. {
  60. struct pnfs_layoutdriver_type *local;
  61. spin_lock(&pnfs_spinlock);
  62. local = find_pnfs_driver_locked(id);
  63. spin_unlock(&pnfs_spinlock);
  64. return local;
  65. }
  66. void
  67. unset_pnfs_layoutdriver(struct nfs_server *nfss)
  68. {
  69. if (nfss->pnfs_curr_ld)
  70. module_put(nfss->pnfs_curr_ld->owner);
  71. nfss->pnfs_curr_ld = NULL;
  72. }
  73. /*
  74. * Try to set the server's pnfs module to the pnfs layout type specified by id.
  75. * Currently only one pNFS layout driver per filesystem is supported.
  76. *
  77. * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
  78. */
  79. void
  80. set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
  81. {
  82. struct pnfs_layoutdriver_type *ld_type = NULL;
  83. if (id == 0)
  84. goto out_no_driver;
  85. if (!(server->nfs_client->cl_exchange_flags &
  86. (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
  87. printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
  88. id, server->nfs_client->cl_exchange_flags);
  89. goto out_no_driver;
  90. }
  91. ld_type = find_pnfs_driver(id);
  92. if (!ld_type) {
  93. request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
  94. ld_type = find_pnfs_driver(id);
  95. if (!ld_type) {
  96. dprintk("%s: No pNFS module found for %u.\n",
  97. __func__, id);
  98. goto out_no_driver;
  99. }
  100. }
  101. if (!try_module_get(ld_type->owner)) {
  102. dprintk("%s: Could not grab reference on module\n", __func__);
  103. goto out_no_driver;
  104. }
  105. server->pnfs_curr_ld = ld_type;
  106. dprintk("%s: pNFS module for %u set\n", __func__, id);
  107. return;
  108. out_no_driver:
  109. dprintk("%s: Using NFSv4 I/O\n", __func__);
  110. server->pnfs_curr_ld = NULL;
  111. }
  112. int
  113. pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  114. {
  115. int status = -EINVAL;
  116. struct pnfs_layoutdriver_type *tmp;
  117. if (ld_type->id == 0) {
  118. printk(KERN_ERR "%s id 0 is reserved\n", __func__);
  119. return status;
  120. }
  121. if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
  122. printk(KERN_ERR "%s Layout driver must provide "
  123. "alloc_lseg and free_lseg.\n", __func__);
  124. return status;
  125. }
  126. spin_lock(&pnfs_spinlock);
  127. tmp = find_pnfs_driver_locked(ld_type->id);
  128. if (!tmp) {
  129. list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
  130. status = 0;
  131. dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
  132. ld_type->name);
  133. } else {
  134. printk(KERN_ERR "%s Module with id %d already loaded!\n",
  135. __func__, ld_type->id);
  136. }
  137. spin_unlock(&pnfs_spinlock);
  138. return status;
  139. }
  140. EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
  141. void
  142. pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  143. {
  144. dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
  145. spin_lock(&pnfs_spinlock);
  146. list_del(&ld_type->pnfs_tblid);
  147. spin_unlock(&pnfs_spinlock);
  148. }
  149. EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
  150. /*
  151. * pNFS client layout cache
  152. */
  153. /* Need to hold i_lock if caller does not already hold reference */
  154. void
  155. get_layout_hdr(struct pnfs_layout_hdr *lo)
  156. {
  157. atomic_inc(&lo->plh_refcount);
  158. }
  159. static void
  160. destroy_layout_hdr(struct pnfs_layout_hdr *lo)
  161. {
  162. dprintk("%s: freeing layout cache %p\n", __func__, lo);
  163. BUG_ON(!list_empty(&lo->plh_layouts));
  164. NFS_I(lo->plh_inode)->layout = NULL;
  165. kfree(lo);
  166. }
  167. static void
  168. put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
  169. {
  170. if (atomic_dec_and_test(&lo->plh_refcount))
  171. destroy_layout_hdr(lo);
  172. }
  173. void
  174. put_layout_hdr(struct pnfs_layout_hdr *lo)
  175. {
  176. struct inode *inode = lo->plh_inode;
  177. if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
  178. destroy_layout_hdr(lo);
  179. spin_unlock(&inode->i_lock);
  180. }
  181. }
  182. static void
  183. init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
  184. {
  185. INIT_LIST_HEAD(&lseg->pls_list);
  186. atomic_set(&lseg->pls_refcount, 1);
  187. smp_mb();
  188. set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
  189. lseg->pls_layout = lo;
  190. }
  191. static void free_lseg(struct pnfs_layout_segment *lseg)
  192. {
  193. struct inode *ino = lseg->pls_layout->plh_inode;
  194. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  195. /* Matched by get_layout_hdr in pnfs_insert_layout */
  196. put_layout_hdr(NFS_I(ino)->layout);
  197. }
  198. static void
  199. put_lseg_common(struct pnfs_layout_segment *lseg)
  200. {
  201. struct inode *inode = lseg->pls_layout->plh_inode;
  202. BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  203. list_del_init(&lseg->pls_list);
  204. if (list_empty(&lseg->pls_layout->plh_segs)) {
  205. set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
  206. /* Matched by initial refcount set in alloc_init_layout_hdr */
  207. put_layout_hdr_locked(lseg->pls_layout);
  208. }
  209. rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
  210. }
  211. void
  212. put_lseg(struct pnfs_layout_segment *lseg)
  213. {
  214. struct inode *inode;
  215. if (!lseg)
  216. return;
  217. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  218. atomic_read(&lseg->pls_refcount),
  219. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  220. inode = lseg->pls_layout->plh_inode;
  221. if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
  222. LIST_HEAD(free_me);
  223. put_lseg_common(lseg);
  224. list_add(&lseg->pls_list, &free_me);
  225. spin_unlock(&inode->i_lock);
  226. pnfs_free_lseg_list(&free_me);
  227. }
  228. }
  229. static bool
  230. should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
  231. {
  232. return (recall_iomode == IOMODE_ANY ||
  233. lseg_iomode == recall_iomode);
  234. }
  235. /* Returns 1 if lseg is removed from list, 0 otherwise */
  236. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  237. struct list_head *tmp_list)
  238. {
  239. int rv = 0;
  240. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  241. /* Remove the reference keeping the lseg in the
  242. * list. It will now be removed when all
  243. * outstanding io is finished.
  244. */
  245. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  246. atomic_read(&lseg->pls_refcount));
  247. if (atomic_dec_and_test(&lseg->pls_refcount)) {
  248. put_lseg_common(lseg);
  249. list_add(&lseg->pls_list, tmp_list);
  250. rv = 1;
  251. }
  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. if (list_empty(&lo->plh_segs)) {
  267. if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
  268. put_layout_hdr_locked(lo);
  269. return 0;
  270. }
  271. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  272. if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
  273. dprintk("%s: freeing lseg %p iomode %d "
  274. "offset %llu length %llu\n", __func__,
  275. lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
  276. lseg->pls_range.length);
  277. invalid++;
  278. removed += mark_lseg_invalid(lseg, tmp_list);
  279. }
  280. dprintk("%s:Return %i\n", __func__, invalid - removed);
  281. return invalid - removed;
  282. }
  283. /* note free_me must contain lsegs from a single layout_hdr */
  284. void
  285. pnfs_free_lseg_list(struct list_head *free_me)
  286. {
  287. struct pnfs_layout_segment *lseg, *tmp;
  288. struct pnfs_layout_hdr *lo;
  289. if (list_empty(free_me))
  290. return;
  291. lo = list_first_entry(free_me, struct pnfs_layout_segment,
  292. pls_list)->pls_layout;
  293. if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
  294. struct nfs_client *clp;
  295. clp = NFS_SERVER(lo->plh_inode)->nfs_client;
  296. spin_lock(&clp->cl_lock);
  297. list_del_init(&lo->plh_layouts);
  298. spin_unlock(&clp->cl_lock);
  299. }
  300. list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
  301. list_del(&lseg->pls_list);
  302. free_lseg(lseg);
  303. }
  304. }
  305. void
  306. pnfs_destroy_layout(struct nfs_inode *nfsi)
  307. {
  308. struct pnfs_layout_hdr *lo;
  309. LIST_HEAD(tmp_list);
  310. spin_lock(&nfsi->vfs_inode.i_lock);
  311. lo = nfsi->layout;
  312. if (lo) {
  313. lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
  314. mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
  315. }
  316. spin_unlock(&nfsi->vfs_inode.i_lock);
  317. pnfs_free_lseg_list(&tmp_list);
  318. }
  319. /*
  320. * Called by the state manger to remove all layouts established under an
  321. * expired lease.
  322. */
  323. void
  324. pnfs_destroy_all_layouts(struct nfs_client *clp)
  325. {
  326. struct pnfs_layout_hdr *lo;
  327. LIST_HEAD(tmp_list);
  328. spin_lock(&clp->cl_lock);
  329. list_splice_init(&clp->cl_layouts, &tmp_list);
  330. spin_unlock(&clp->cl_lock);
  331. while (!list_empty(&tmp_list)) {
  332. lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
  333. plh_layouts);
  334. dprintk("%s freeing layout for inode %lu\n", __func__,
  335. lo->plh_inode->i_ino);
  336. pnfs_destroy_layout(NFS_I(lo->plh_inode));
  337. }
  338. }
  339. /* update lo->plh_stateid with new if is more recent */
  340. void
  341. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  342. bool update_barrier)
  343. {
  344. u32 oldseq, newseq;
  345. oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  346. newseq = be32_to_cpu(new->stateid.seqid);
  347. if ((int)(newseq - oldseq) > 0) {
  348. memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
  349. if (update_barrier) {
  350. u32 new_barrier = be32_to_cpu(new->stateid.seqid);
  351. if ((int)(new_barrier - lo->plh_barrier))
  352. lo->plh_barrier = new_barrier;
  353. } else {
  354. /* Because of wraparound, we want to keep the barrier
  355. * "close" to the current seqids. It needs to be
  356. * within 2**31 to count as "behind", so if it
  357. * gets too near that limit, give us a litle leeway
  358. * and bring it to within 2**30.
  359. * NOTE - and yes, this is all unsigned arithmetic.
  360. */
  361. if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
  362. lo->plh_barrier = newseq - (1 << 30);
  363. }
  364. }
  365. }
  366. /* lget is set to 1 if called from inside send_layoutget call chain */
  367. static bool
  368. pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
  369. int lget)
  370. {
  371. if ((stateid) &&
  372. (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
  373. return true;
  374. return lo->plh_block_lgets ||
  375. test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
  376. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  377. (list_empty(&lo->plh_segs) &&
  378. (atomic_read(&lo->plh_outstanding) > lget));
  379. }
  380. int
  381. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  382. struct nfs4_state *open_state)
  383. {
  384. int status = 0;
  385. dprintk("--> %s\n", __func__);
  386. spin_lock(&lo->plh_inode->i_lock);
  387. if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
  388. status = -EAGAIN;
  389. } else if (list_empty(&lo->plh_segs)) {
  390. int seq;
  391. do {
  392. seq = read_seqbegin(&open_state->seqlock);
  393. memcpy(dst->data, open_state->stateid.data,
  394. sizeof(open_state->stateid.data));
  395. } while (read_seqretry(&open_state->seqlock, seq));
  396. } else
  397. memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
  398. spin_unlock(&lo->plh_inode->i_lock);
  399. dprintk("<-- %s\n", __func__);
  400. return status;
  401. }
  402. /*
  403. * Get layout from server.
  404. * for now, assume that whole file layouts are requested.
  405. * arg->offset: 0
  406. * arg->length: all ones
  407. */
  408. static struct pnfs_layout_segment *
  409. send_layoutget(struct pnfs_layout_hdr *lo,
  410. struct nfs_open_context *ctx,
  411. u32 iomode)
  412. {
  413. struct inode *ino = lo->plh_inode;
  414. struct nfs_server *server = NFS_SERVER(ino);
  415. struct nfs4_layoutget *lgp;
  416. struct pnfs_layout_segment *lseg = NULL;
  417. dprintk("--> %s\n", __func__);
  418. BUG_ON(ctx == NULL);
  419. lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
  420. if (lgp == NULL)
  421. return NULL;
  422. lgp->args.minlength = NFS4_MAX_UINT64;
  423. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  424. lgp->args.range.iomode = iomode;
  425. lgp->args.range.offset = 0;
  426. lgp->args.range.length = NFS4_MAX_UINT64;
  427. lgp->args.type = server->pnfs_curr_ld->id;
  428. lgp->args.inode = ino;
  429. lgp->args.ctx = get_nfs_open_context(ctx);
  430. lgp->lsegpp = &lseg;
  431. /* Synchronously retrieve layout information from server and
  432. * store in lseg.
  433. */
  434. nfs4_proc_layoutget(lgp);
  435. if (!lseg) {
  436. /* remember that LAYOUTGET failed and suspend trying */
  437. set_bit(lo_fail_bit(iomode), &lo->plh_flags);
  438. }
  439. return lseg;
  440. }
  441. bool pnfs_roc(struct inode *ino)
  442. {
  443. struct pnfs_layout_hdr *lo;
  444. struct pnfs_layout_segment *lseg, *tmp;
  445. LIST_HEAD(tmp_list);
  446. bool found = false;
  447. spin_lock(&ino->i_lock);
  448. lo = NFS_I(ino)->layout;
  449. if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
  450. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
  451. goto out_nolayout;
  452. list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
  453. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  454. mark_lseg_invalid(lseg, &tmp_list);
  455. found = true;
  456. }
  457. if (!found)
  458. goto out_nolayout;
  459. lo->plh_block_lgets++;
  460. get_layout_hdr(lo); /* matched in pnfs_roc_release */
  461. spin_unlock(&ino->i_lock);
  462. pnfs_free_lseg_list(&tmp_list);
  463. return true;
  464. out_nolayout:
  465. spin_unlock(&ino->i_lock);
  466. return false;
  467. }
  468. void pnfs_roc_release(struct inode *ino)
  469. {
  470. struct pnfs_layout_hdr *lo;
  471. spin_lock(&ino->i_lock);
  472. lo = NFS_I(ino)->layout;
  473. lo->plh_block_lgets--;
  474. put_layout_hdr_locked(lo);
  475. spin_unlock(&ino->i_lock);
  476. }
  477. void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
  478. {
  479. struct pnfs_layout_hdr *lo;
  480. spin_lock(&ino->i_lock);
  481. lo = NFS_I(ino)->layout;
  482. if ((int)(barrier - lo->plh_barrier) > 0)
  483. lo->plh_barrier = barrier;
  484. spin_unlock(&ino->i_lock);
  485. }
  486. bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
  487. {
  488. struct nfs_inode *nfsi = NFS_I(ino);
  489. struct pnfs_layout_segment *lseg;
  490. bool found = false;
  491. spin_lock(&ino->i_lock);
  492. list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
  493. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  494. found = true;
  495. break;
  496. }
  497. if (!found) {
  498. struct pnfs_layout_hdr *lo = nfsi->layout;
  499. u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  500. /* Since close does not return a layout stateid for use as
  501. * a barrier, we choose the worst-case barrier.
  502. */
  503. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  504. }
  505. spin_unlock(&ino->i_lock);
  506. return found;
  507. }
  508. /*
  509. * Compare two layout segments for sorting into layout cache.
  510. * We want to preferentially return RW over RO layouts, so ensure those
  511. * are seen first.
  512. */
  513. static s64
  514. cmp_layout(u32 iomode1, u32 iomode2)
  515. {
  516. /* read > read/write */
  517. return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
  518. }
  519. static void
  520. pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  521. struct pnfs_layout_segment *lseg)
  522. {
  523. struct pnfs_layout_segment *lp;
  524. int found = 0;
  525. dprintk("%s:Begin\n", __func__);
  526. assert_spin_locked(&lo->plh_inode->i_lock);
  527. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  528. if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
  529. continue;
  530. list_add_tail(&lseg->pls_list, &lp->pls_list);
  531. dprintk("%s: inserted lseg %p "
  532. "iomode %d offset %llu length %llu before "
  533. "lp %p iomode %d offset %llu length %llu\n",
  534. __func__, lseg, lseg->pls_range.iomode,
  535. lseg->pls_range.offset, lseg->pls_range.length,
  536. lp, lp->pls_range.iomode, lp->pls_range.offset,
  537. lp->pls_range.length);
  538. found = 1;
  539. break;
  540. }
  541. if (!found) {
  542. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  543. dprintk("%s: inserted lseg %p "
  544. "iomode %d offset %llu length %llu at tail\n",
  545. __func__, lseg, lseg->pls_range.iomode,
  546. lseg->pls_range.offset, lseg->pls_range.length);
  547. }
  548. get_layout_hdr(lo);
  549. dprintk("%s:Return\n", __func__);
  550. }
  551. static struct pnfs_layout_hdr *
  552. alloc_init_layout_hdr(struct inode *ino)
  553. {
  554. struct pnfs_layout_hdr *lo;
  555. lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
  556. if (!lo)
  557. return NULL;
  558. atomic_set(&lo->plh_refcount, 1);
  559. INIT_LIST_HEAD(&lo->plh_layouts);
  560. INIT_LIST_HEAD(&lo->plh_segs);
  561. INIT_LIST_HEAD(&lo->plh_bulk_recall);
  562. lo->plh_inode = ino;
  563. return lo;
  564. }
  565. static struct pnfs_layout_hdr *
  566. pnfs_find_alloc_layout(struct inode *ino)
  567. {
  568. struct nfs_inode *nfsi = NFS_I(ino);
  569. struct pnfs_layout_hdr *new = NULL;
  570. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  571. assert_spin_locked(&ino->i_lock);
  572. if (nfsi->layout) {
  573. if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
  574. return NULL;
  575. else
  576. return nfsi->layout;
  577. }
  578. spin_unlock(&ino->i_lock);
  579. new = alloc_init_layout_hdr(ino);
  580. spin_lock(&ino->i_lock);
  581. if (likely(nfsi->layout == NULL)) /* Won the race? */
  582. nfsi->layout = new;
  583. else
  584. kfree(new);
  585. return nfsi->layout;
  586. }
  587. /*
  588. * iomode matching rules:
  589. * iomode lseg match
  590. * ----- ----- -----
  591. * ANY READ true
  592. * ANY RW true
  593. * RW READ false
  594. * RW RW true
  595. * READ READ true
  596. * READ RW true
  597. */
  598. static int
  599. is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
  600. {
  601. return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
  602. }
  603. /*
  604. * lookup range in layout
  605. */
  606. static struct pnfs_layout_segment *
  607. pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode)
  608. {
  609. struct pnfs_layout_segment *lseg, *ret = NULL;
  610. dprintk("%s:Begin\n", __func__);
  611. assert_spin_locked(&lo->plh_inode->i_lock);
  612. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  613. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  614. is_matching_lseg(lseg, iomode)) {
  615. ret = get_lseg(lseg);
  616. break;
  617. }
  618. if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
  619. break;
  620. }
  621. dprintk("%s:Return lseg %p ref %d\n",
  622. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  623. return ret;
  624. }
  625. /*
  626. * Layout segment is retreived from the server if not cached.
  627. * The appropriate layout segment is referenced and returned to the caller.
  628. */
  629. struct pnfs_layout_segment *
  630. pnfs_update_layout(struct inode *ino,
  631. struct nfs_open_context *ctx,
  632. enum pnfs_iomode iomode)
  633. {
  634. struct nfs_inode *nfsi = NFS_I(ino);
  635. struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
  636. struct pnfs_layout_hdr *lo;
  637. struct pnfs_layout_segment *lseg = NULL;
  638. bool first = false;
  639. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  640. return NULL;
  641. spin_lock(&ino->i_lock);
  642. lo = pnfs_find_alloc_layout(ino);
  643. if (lo == NULL) {
  644. dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
  645. goto out_unlock;
  646. }
  647. /* Do we even need to bother with this? */
  648. if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
  649. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  650. dprintk("%s matches recall, use MDS\n", __func__);
  651. goto out_unlock;
  652. }
  653. /* if LAYOUTGET already failed once we don't try again */
  654. if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
  655. goto out_unlock;
  656. /* Check to see if the layout for the given range already exists */
  657. lseg = pnfs_find_lseg(lo, iomode);
  658. if (lseg)
  659. goto out_unlock;
  660. if (pnfs_layoutgets_blocked(lo, NULL, 0))
  661. goto out_unlock;
  662. atomic_inc(&lo->plh_outstanding);
  663. get_layout_hdr(lo);
  664. if (list_empty(&lo->plh_segs))
  665. first = true;
  666. spin_unlock(&ino->i_lock);
  667. if (first) {
  668. /* The lo must be on the clp list if there is any
  669. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  670. */
  671. spin_lock(&clp->cl_lock);
  672. BUG_ON(!list_empty(&lo->plh_layouts));
  673. list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
  674. spin_unlock(&clp->cl_lock);
  675. }
  676. lseg = send_layoutget(lo, ctx, iomode);
  677. if (!lseg && first) {
  678. spin_lock(&clp->cl_lock);
  679. list_del_init(&lo->plh_layouts);
  680. spin_unlock(&clp->cl_lock);
  681. }
  682. atomic_dec(&lo->plh_outstanding);
  683. put_layout_hdr(lo);
  684. out:
  685. dprintk("%s end, state 0x%lx lseg %p\n", __func__,
  686. nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
  687. return lseg;
  688. out_unlock:
  689. spin_unlock(&ino->i_lock);
  690. goto out;
  691. }
  692. int
  693. pnfs_layout_process(struct nfs4_layoutget *lgp)
  694. {
  695. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  696. struct nfs4_layoutget_res *res = &lgp->res;
  697. struct pnfs_layout_segment *lseg;
  698. struct inode *ino = lo->plh_inode;
  699. struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
  700. int status = 0;
  701. /* Verify we got what we asked for.
  702. * Note that because the xdr parsing only accepts a single
  703. * element array, this can fail even if the server is behaving
  704. * correctly.
  705. */
  706. if (lgp->args.range.iomode > res->range.iomode ||
  707. res->range.offset != 0 ||
  708. res->range.length != NFS4_MAX_UINT64) {
  709. status = -EINVAL;
  710. goto out;
  711. }
  712. /* Inject layout blob into I/O device driver */
  713. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
  714. if (!lseg || IS_ERR(lseg)) {
  715. if (!lseg)
  716. status = -ENOMEM;
  717. else
  718. status = PTR_ERR(lseg);
  719. dprintk("%s: Could not allocate layout: error %d\n",
  720. __func__, status);
  721. goto out;
  722. }
  723. spin_lock(&ino->i_lock);
  724. if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
  725. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  726. dprintk("%s forget reply due to recall\n", __func__);
  727. goto out_forget_reply;
  728. }
  729. if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
  730. dprintk("%s forget reply due to state\n", __func__);
  731. goto out_forget_reply;
  732. }
  733. init_lseg(lo, lseg);
  734. lseg->pls_range = res->range;
  735. *lgp->lsegpp = get_lseg(lseg);
  736. pnfs_insert_layout(lo, lseg);
  737. if (res->return_on_close) {
  738. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  739. set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
  740. }
  741. /* Done processing layoutget. Set the layout stateid */
  742. pnfs_set_layout_stateid(lo, &res->stateid, false);
  743. spin_unlock(&ino->i_lock);
  744. out:
  745. return status;
  746. out_forget_reply:
  747. spin_unlock(&ino->i_lock);
  748. lseg->pls_layout = lo;
  749. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  750. goto out;
  751. }
  752. static int pnfs_read_pg_test(struct nfs_pageio_descriptor *pgio,
  753. struct nfs_page *prev,
  754. struct nfs_page *req)
  755. {
  756. if (pgio->pg_count == prev->wb_bytes) {
  757. /* This is first coelesce call for a series of nfs_pages */
  758. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  759. prev->wb_context,
  760. IOMODE_READ);
  761. }
  762. return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
  763. }
  764. void
  765. pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
  766. {
  767. struct pnfs_layoutdriver_type *ld;
  768. ld = NFS_SERVER(inode)->pnfs_curr_ld;
  769. pgio->pg_test = (ld && ld->pg_test) ? pnfs_read_pg_test : NULL;
  770. }
  771. static int pnfs_write_pg_test(struct nfs_pageio_descriptor *pgio,
  772. struct nfs_page *prev,
  773. struct nfs_page *req)
  774. {
  775. if (pgio->pg_count == prev->wb_bytes) {
  776. /* This is first coelesce call for a series of nfs_pages */
  777. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  778. prev->wb_context,
  779. IOMODE_RW);
  780. }
  781. return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
  782. }
  783. void
  784. pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode)
  785. {
  786. struct pnfs_layoutdriver_type *ld;
  787. ld = NFS_SERVER(inode)->pnfs_curr_ld;
  788. pgio->pg_test = (ld && ld->pg_test) ? pnfs_write_pg_test : NULL;
  789. }
  790. enum pnfs_try_status
  791. pnfs_try_to_write_data(struct nfs_write_data *wdata,
  792. const struct rpc_call_ops *call_ops, int how)
  793. {
  794. struct inode *inode = wdata->inode;
  795. enum pnfs_try_status trypnfs;
  796. struct nfs_server *nfss = NFS_SERVER(inode);
  797. wdata->mds_ops = call_ops;
  798. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  799. inode->i_ino, wdata->args.count, wdata->args.offset, how);
  800. trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
  801. if (trypnfs == PNFS_NOT_ATTEMPTED) {
  802. put_lseg(wdata->lseg);
  803. wdata->lseg = NULL;
  804. } else
  805. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  806. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  807. return trypnfs;
  808. }
  809. /*
  810. * Call the appropriate parallel I/O subsystem read function.
  811. */
  812. enum pnfs_try_status
  813. pnfs_try_to_read_data(struct nfs_read_data *rdata,
  814. const struct rpc_call_ops *call_ops)
  815. {
  816. struct inode *inode = rdata->inode;
  817. struct nfs_server *nfss = NFS_SERVER(inode);
  818. enum pnfs_try_status trypnfs;
  819. rdata->mds_ops = call_ops;
  820. dprintk("%s: Reading ino:%lu %u@%llu\n",
  821. __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
  822. trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
  823. if (trypnfs == PNFS_NOT_ATTEMPTED) {
  824. put_lseg(rdata->lseg);
  825. rdata->lseg = NULL;
  826. } else {
  827. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  828. }
  829. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  830. return trypnfs;
  831. }