pnfs.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. EXPORT_SYMBOL_GPL(put_lseg);
  230. static bool
  231. should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
  232. {
  233. return (recall_iomode == IOMODE_ANY ||
  234. lseg_iomode == recall_iomode);
  235. }
  236. /* Returns 1 if lseg is removed from list, 0 otherwise */
  237. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  238. struct list_head *tmp_list)
  239. {
  240. int rv = 0;
  241. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  242. /* Remove the reference keeping the lseg in the
  243. * list. It will now be removed when all
  244. * outstanding io is finished.
  245. */
  246. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  247. atomic_read(&lseg->pls_refcount));
  248. if (atomic_dec_and_test(&lseg->pls_refcount)) {
  249. put_lseg_common(lseg);
  250. list_add(&lseg->pls_list, tmp_list);
  251. rv = 1;
  252. }
  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. if (list_empty(&lo->plh_segs)) {
  268. if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
  269. put_layout_hdr_locked(lo);
  270. return 0;
  271. }
  272. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  273. if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
  274. dprintk("%s: freeing lseg %p iomode %d "
  275. "offset %llu length %llu\n", __func__,
  276. lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
  277. lseg->pls_range.length);
  278. invalid++;
  279. removed += mark_lseg_invalid(lseg, tmp_list);
  280. }
  281. dprintk("%s:Return %i\n", __func__, invalid - removed);
  282. return invalid - removed;
  283. }
  284. /* note free_me must contain lsegs from a single layout_hdr */
  285. void
  286. pnfs_free_lseg_list(struct list_head *free_me)
  287. {
  288. struct pnfs_layout_segment *lseg, *tmp;
  289. struct pnfs_layout_hdr *lo;
  290. if (list_empty(free_me))
  291. return;
  292. lo = list_first_entry(free_me, struct pnfs_layout_segment,
  293. pls_list)->pls_layout;
  294. if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
  295. struct nfs_client *clp;
  296. clp = NFS_SERVER(lo->plh_inode)->nfs_client;
  297. spin_lock(&clp->cl_lock);
  298. list_del_init(&lo->plh_layouts);
  299. spin_unlock(&clp->cl_lock);
  300. }
  301. list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
  302. list_del(&lseg->pls_list);
  303. free_lseg(lseg);
  304. }
  305. }
  306. void
  307. pnfs_destroy_layout(struct nfs_inode *nfsi)
  308. {
  309. struct pnfs_layout_hdr *lo;
  310. LIST_HEAD(tmp_list);
  311. spin_lock(&nfsi->vfs_inode.i_lock);
  312. lo = nfsi->layout;
  313. if (lo) {
  314. lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
  315. mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
  316. }
  317. spin_unlock(&nfsi->vfs_inode.i_lock);
  318. pnfs_free_lseg_list(&tmp_list);
  319. }
  320. /*
  321. * Called by the state manger to remove all layouts established under an
  322. * expired lease.
  323. */
  324. void
  325. pnfs_destroy_all_layouts(struct nfs_client *clp)
  326. {
  327. struct pnfs_layout_hdr *lo;
  328. LIST_HEAD(tmp_list);
  329. spin_lock(&clp->cl_lock);
  330. list_splice_init(&clp->cl_layouts, &tmp_list);
  331. spin_unlock(&clp->cl_lock);
  332. while (!list_empty(&tmp_list)) {
  333. lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
  334. plh_layouts);
  335. dprintk("%s freeing layout for inode %lu\n", __func__,
  336. lo->plh_inode->i_ino);
  337. pnfs_destroy_layout(NFS_I(lo->plh_inode));
  338. }
  339. }
  340. /* update lo->plh_stateid with new if is more recent */
  341. void
  342. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  343. bool update_barrier)
  344. {
  345. u32 oldseq, newseq;
  346. oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  347. newseq = be32_to_cpu(new->stateid.seqid);
  348. if ((int)(newseq - oldseq) > 0) {
  349. memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
  350. if (update_barrier) {
  351. u32 new_barrier = be32_to_cpu(new->stateid.seqid);
  352. if ((int)(new_barrier - lo->plh_barrier))
  353. lo->plh_barrier = new_barrier;
  354. } else {
  355. /* Because of wraparound, we want to keep the barrier
  356. * "close" to the current seqids. It needs to be
  357. * within 2**31 to count as "behind", so if it
  358. * gets too near that limit, give us a litle leeway
  359. * and bring it to within 2**30.
  360. * NOTE - and yes, this is all unsigned arithmetic.
  361. */
  362. if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
  363. lo->plh_barrier = newseq - (1 << 30);
  364. }
  365. }
  366. }
  367. /* lget is set to 1 if called from inside send_layoutget call chain */
  368. static bool
  369. pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
  370. int lget)
  371. {
  372. if ((stateid) &&
  373. (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
  374. return true;
  375. return lo->plh_block_lgets ||
  376. test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
  377. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  378. (list_empty(&lo->plh_segs) &&
  379. (atomic_read(&lo->plh_outstanding) > lget));
  380. }
  381. int
  382. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  383. struct nfs4_state *open_state)
  384. {
  385. int status = 0;
  386. dprintk("--> %s\n", __func__);
  387. spin_lock(&lo->plh_inode->i_lock);
  388. if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
  389. status = -EAGAIN;
  390. } else if (list_empty(&lo->plh_segs)) {
  391. int seq;
  392. do {
  393. seq = read_seqbegin(&open_state->seqlock);
  394. memcpy(dst->data, open_state->stateid.data,
  395. sizeof(open_state->stateid.data));
  396. } while (read_seqretry(&open_state->seqlock, seq));
  397. } else
  398. memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
  399. spin_unlock(&lo->plh_inode->i_lock);
  400. dprintk("<-- %s\n", __func__);
  401. return status;
  402. }
  403. /*
  404. * Get layout from server.
  405. * for now, assume that whole file layouts are requested.
  406. * arg->offset: 0
  407. * arg->length: all ones
  408. */
  409. static struct pnfs_layout_segment *
  410. send_layoutget(struct pnfs_layout_hdr *lo,
  411. struct nfs_open_context *ctx,
  412. u32 iomode)
  413. {
  414. struct inode *ino = lo->plh_inode;
  415. struct nfs_server *server = NFS_SERVER(ino);
  416. struct nfs4_layoutget *lgp;
  417. struct pnfs_layout_segment *lseg = NULL;
  418. struct page **pages = NULL;
  419. int i;
  420. u32 max_resp_sz, max_pages;
  421. dprintk("--> %s\n", __func__);
  422. BUG_ON(ctx == NULL);
  423. lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
  424. if (lgp == NULL)
  425. return NULL;
  426. /* allocate pages for xdr post processing */
  427. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  428. max_pages = max_resp_sz >> PAGE_SHIFT;
  429. pages = kzalloc(max_pages * sizeof(struct page *), GFP_KERNEL);
  430. if (!pages)
  431. goto out_err_free;
  432. for (i = 0; i < max_pages; i++) {
  433. pages[i] = alloc_page(GFP_KERNEL);
  434. if (!pages[i])
  435. goto out_err_free;
  436. }
  437. lgp->args.minlength = NFS4_MAX_UINT64;
  438. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  439. lgp->args.range.iomode = iomode;
  440. lgp->args.range.offset = 0;
  441. lgp->args.range.length = NFS4_MAX_UINT64;
  442. lgp->args.type = server->pnfs_curr_ld->id;
  443. lgp->args.inode = ino;
  444. lgp->args.ctx = get_nfs_open_context(ctx);
  445. lgp->args.layout.pages = pages;
  446. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  447. lgp->lsegpp = &lseg;
  448. /* Synchronously retrieve layout information from server and
  449. * store in lseg.
  450. */
  451. nfs4_proc_layoutget(lgp);
  452. if (!lseg) {
  453. /* remember that LAYOUTGET failed and suspend trying */
  454. set_bit(lo_fail_bit(iomode), &lo->plh_flags);
  455. }
  456. /* free xdr pages */
  457. for (i = 0; i < max_pages; i++)
  458. __free_page(pages[i]);
  459. kfree(pages);
  460. return lseg;
  461. out_err_free:
  462. /* free any allocated xdr pages, lgp as it's not used */
  463. if (pages) {
  464. for (i = 0; i < max_pages; i++) {
  465. if (!pages[i])
  466. break;
  467. __free_page(pages[i]);
  468. }
  469. kfree(pages);
  470. }
  471. kfree(lgp);
  472. return NULL;
  473. }
  474. bool pnfs_roc(struct inode *ino)
  475. {
  476. struct pnfs_layout_hdr *lo;
  477. struct pnfs_layout_segment *lseg, *tmp;
  478. LIST_HEAD(tmp_list);
  479. bool found = false;
  480. spin_lock(&ino->i_lock);
  481. lo = NFS_I(ino)->layout;
  482. if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
  483. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
  484. goto out_nolayout;
  485. list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
  486. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  487. mark_lseg_invalid(lseg, &tmp_list);
  488. found = true;
  489. }
  490. if (!found)
  491. goto out_nolayout;
  492. lo->plh_block_lgets++;
  493. get_layout_hdr(lo); /* matched in pnfs_roc_release */
  494. spin_unlock(&ino->i_lock);
  495. pnfs_free_lseg_list(&tmp_list);
  496. return true;
  497. out_nolayout:
  498. spin_unlock(&ino->i_lock);
  499. return false;
  500. }
  501. void pnfs_roc_release(struct inode *ino)
  502. {
  503. struct pnfs_layout_hdr *lo;
  504. spin_lock(&ino->i_lock);
  505. lo = NFS_I(ino)->layout;
  506. lo->plh_block_lgets--;
  507. put_layout_hdr_locked(lo);
  508. spin_unlock(&ino->i_lock);
  509. }
  510. void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
  511. {
  512. struct pnfs_layout_hdr *lo;
  513. spin_lock(&ino->i_lock);
  514. lo = NFS_I(ino)->layout;
  515. if ((int)(barrier - lo->plh_barrier) > 0)
  516. lo->plh_barrier = barrier;
  517. spin_unlock(&ino->i_lock);
  518. }
  519. bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
  520. {
  521. struct nfs_inode *nfsi = NFS_I(ino);
  522. struct pnfs_layout_segment *lseg;
  523. bool found = false;
  524. spin_lock(&ino->i_lock);
  525. list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
  526. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  527. found = true;
  528. break;
  529. }
  530. if (!found) {
  531. struct pnfs_layout_hdr *lo = nfsi->layout;
  532. u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  533. /* Since close does not return a layout stateid for use as
  534. * a barrier, we choose the worst-case barrier.
  535. */
  536. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  537. }
  538. spin_unlock(&ino->i_lock);
  539. return found;
  540. }
  541. /*
  542. * Compare two layout segments for sorting into layout cache.
  543. * We want to preferentially return RW over RO layouts, so ensure those
  544. * are seen first.
  545. */
  546. static s64
  547. cmp_layout(u32 iomode1, u32 iomode2)
  548. {
  549. /* read > read/write */
  550. return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
  551. }
  552. static void
  553. pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  554. struct pnfs_layout_segment *lseg)
  555. {
  556. struct pnfs_layout_segment *lp;
  557. int found = 0;
  558. dprintk("%s:Begin\n", __func__);
  559. assert_spin_locked(&lo->plh_inode->i_lock);
  560. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  561. if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
  562. continue;
  563. list_add_tail(&lseg->pls_list, &lp->pls_list);
  564. dprintk("%s: inserted lseg %p "
  565. "iomode %d offset %llu length %llu before "
  566. "lp %p iomode %d offset %llu length %llu\n",
  567. __func__, lseg, lseg->pls_range.iomode,
  568. lseg->pls_range.offset, lseg->pls_range.length,
  569. lp, lp->pls_range.iomode, lp->pls_range.offset,
  570. lp->pls_range.length);
  571. found = 1;
  572. break;
  573. }
  574. if (!found) {
  575. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  576. dprintk("%s: inserted lseg %p "
  577. "iomode %d offset %llu length %llu at tail\n",
  578. __func__, lseg, lseg->pls_range.iomode,
  579. lseg->pls_range.offset, lseg->pls_range.length);
  580. }
  581. get_layout_hdr(lo);
  582. dprintk("%s:Return\n", __func__);
  583. }
  584. static struct pnfs_layout_hdr *
  585. alloc_init_layout_hdr(struct inode *ino)
  586. {
  587. struct pnfs_layout_hdr *lo;
  588. lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
  589. if (!lo)
  590. return NULL;
  591. atomic_set(&lo->plh_refcount, 1);
  592. INIT_LIST_HEAD(&lo->plh_layouts);
  593. INIT_LIST_HEAD(&lo->plh_segs);
  594. INIT_LIST_HEAD(&lo->plh_bulk_recall);
  595. lo->plh_inode = ino;
  596. return lo;
  597. }
  598. static struct pnfs_layout_hdr *
  599. pnfs_find_alloc_layout(struct inode *ino)
  600. {
  601. struct nfs_inode *nfsi = NFS_I(ino);
  602. struct pnfs_layout_hdr *new = NULL;
  603. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  604. assert_spin_locked(&ino->i_lock);
  605. if (nfsi->layout) {
  606. if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
  607. return NULL;
  608. else
  609. return nfsi->layout;
  610. }
  611. spin_unlock(&ino->i_lock);
  612. new = alloc_init_layout_hdr(ino);
  613. spin_lock(&ino->i_lock);
  614. if (likely(nfsi->layout == NULL)) /* Won the race? */
  615. nfsi->layout = new;
  616. else
  617. kfree(new);
  618. return nfsi->layout;
  619. }
  620. /*
  621. * iomode matching rules:
  622. * iomode lseg match
  623. * ----- ----- -----
  624. * ANY READ true
  625. * ANY RW true
  626. * RW READ false
  627. * RW RW true
  628. * READ READ true
  629. * READ RW true
  630. */
  631. static int
  632. is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
  633. {
  634. return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
  635. }
  636. /*
  637. * lookup range in layout
  638. */
  639. static struct pnfs_layout_segment *
  640. pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode)
  641. {
  642. struct pnfs_layout_segment *lseg, *ret = NULL;
  643. dprintk("%s:Begin\n", __func__);
  644. assert_spin_locked(&lo->plh_inode->i_lock);
  645. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  646. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  647. is_matching_lseg(lseg, iomode)) {
  648. ret = get_lseg(lseg);
  649. break;
  650. }
  651. if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
  652. break;
  653. }
  654. dprintk("%s:Return lseg %p ref %d\n",
  655. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  656. return ret;
  657. }
  658. /*
  659. * Layout segment is retreived from the server if not cached.
  660. * The appropriate layout segment is referenced and returned to the caller.
  661. */
  662. struct pnfs_layout_segment *
  663. pnfs_update_layout(struct inode *ino,
  664. struct nfs_open_context *ctx,
  665. enum pnfs_iomode iomode)
  666. {
  667. struct nfs_inode *nfsi = NFS_I(ino);
  668. struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
  669. struct pnfs_layout_hdr *lo;
  670. struct pnfs_layout_segment *lseg = NULL;
  671. bool first = false;
  672. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  673. return NULL;
  674. spin_lock(&ino->i_lock);
  675. lo = pnfs_find_alloc_layout(ino);
  676. if (lo == NULL) {
  677. dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
  678. goto out_unlock;
  679. }
  680. /* Do we even need to bother with this? */
  681. if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
  682. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  683. dprintk("%s matches recall, use MDS\n", __func__);
  684. goto out_unlock;
  685. }
  686. /* if LAYOUTGET already failed once we don't try again */
  687. if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
  688. goto out_unlock;
  689. /* Check to see if the layout for the given range already exists */
  690. lseg = pnfs_find_lseg(lo, iomode);
  691. if (lseg)
  692. goto out_unlock;
  693. if (pnfs_layoutgets_blocked(lo, NULL, 0))
  694. goto out_unlock;
  695. atomic_inc(&lo->plh_outstanding);
  696. get_layout_hdr(lo);
  697. if (list_empty(&lo->plh_segs))
  698. first = true;
  699. spin_unlock(&ino->i_lock);
  700. if (first) {
  701. /* The lo must be on the clp list if there is any
  702. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  703. */
  704. spin_lock(&clp->cl_lock);
  705. BUG_ON(!list_empty(&lo->plh_layouts));
  706. list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
  707. spin_unlock(&clp->cl_lock);
  708. }
  709. lseg = send_layoutget(lo, ctx, iomode);
  710. if (!lseg && first) {
  711. spin_lock(&clp->cl_lock);
  712. list_del_init(&lo->plh_layouts);
  713. spin_unlock(&clp->cl_lock);
  714. }
  715. atomic_dec(&lo->plh_outstanding);
  716. put_layout_hdr(lo);
  717. out:
  718. dprintk("%s end, state 0x%lx lseg %p\n", __func__,
  719. nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
  720. return lseg;
  721. out_unlock:
  722. spin_unlock(&ino->i_lock);
  723. goto out;
  724. }
  725. int
  726. pnfs_layout_process(struct nfs4_layoutget *lgp)
  727. {
  728. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  729. struct nfs4_layoutget_res *res = &lgp->res;
  730. struct pnfs_layout_segment *lseg;
  731. struct inode *ino = lo->plh_inode;
  732. struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
  733. int status = 0;
  734. /* Verify we got what we asked for.
  735. * Note that because the xdr parsing only accepts a single
  736. * element array, this can fail even if the server is behaving
  737. * correctly.
  738. */
  739. if (lgp->args.range.iomode > res->range.iomode ||
  740. res->range.offset != 0 ||
  741. res->range.length != NFS4_MAX_UINT64) {
  742. status = -EINVAL;
  743. goto out;
  744. }
  745. /* Inject layout blob into I/O device driver */
  746. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
  747. if (!lseg || IS_ERR(lseg)) {
  748. if (!lseg)
  749. status = -ENOMEM;
  750. else
  751. status = PTR_ERR(lseg);
  752. dprintk("%s: Could not allocate layout: error %d\n",
  753. __func__, status);
  754. goto out;
  755. }
  756. spin_lock(&ino->i_lock);
  757. if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
  758. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  759. dprintk("%s forget reply due to recall\n", __func__);
  760. goto out_forget_reply;
  761. }
  762. if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
  763. dprintk("%s forget reply due to state\n", __func__);
  764. goto out_forget_reply;
  765. }
  766. init_lseg(lo, lseg);
  767. lseg->pls_range = res->range;
  768. *lgp->lsegpp = get_lseg(lseg);
  769. pnfs_insert_layout(lo, lseg);
  770. if (res->return_on_close) {
  771. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  772. set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
  773. }
  774. /* Done processing layoutget. Set the layout stateid */
  775. pnfs_set_layout_stateid(lo, &res->stateid, false);
  776. spin_unlock(&ino->i_lock);
  777. out:
  778. return status;
  779. out_forget_reply:
  780. spin_unlock(&ino->i_lock);
  781. lseg->pls_layout = lo;
  782. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  783. goto out;
  784. }
  785. static int pnfs_read_pg_test(struct nfs_pageio_descriptor *pgio,
  786. struct nfs_page *prev,
  787. struct nfs_page *req)
  788. {
  789. if (pgio->pg_count == prev->wb_bytes) {
  790. /* This is first coelesce call for a series of nfs_pages */
  791. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  792. prev->wb_context,
  793. IOMODE_READ);
  794. }
  795. return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
  796. }
  797. void
  798. pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
  799. {
  800. struct pnfs_layoutdriver_type *ld;
  801. ld = NFS_SERVER(inode)->pnfs_curr_ld;
  802. pgio->pg_test = (ld && ld->pg_test) ? pnfs_read_pg_test : NULL;
  803. }
  804. static int pnfs_write_pg_test(struct nfs_pageio_descriptor *pgio,
  805. struct nfs_page *prev,
  806. struct nfs_page *req)
  807. {
  808. if (pgio->pg_count == prev->wb_bytes) {
  809. /* This is first coelesce call for a series of nfs_pages */
  810. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  811. prev->wb_context,
  812. IOMODE_RW);
  813. }
  814. return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
  815. }
  816. void
  817. pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode)
  818. {
  819. struct pnfs_layoutdriver_type *ld;
  820. ld = NFS_SERVER(inode)->pnfs_curr_ld;
  821. pgio->pg_test = (ld && ld->pg_test) ? pnfs_write_pg_test : NULL;
  822. }
  823. enum pnfs_try_status
  824. pnfs_try_to_write_data(struct nfs_write_data *wdata,
  825. const struct rpc_call_ops *call_ops, int how)
  826. {
  827. struct inode *inode = wdata->inode;
  828. enum pnfs_try_status trypnfs;
  829. struct nfs_server *nfss = NFS_SERVER(inode);
  830. wdata->mds_ops = call_ops;
  831. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  832. inode->i_ino, wdata->args.count, wdata->args.offset, how);
  833. trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
  834. if (trypnfs == PNFS_NOT_ATTEMPTED) {
  835. put_lseg(wdata->lseg);
  836. wdata->lseg = NULL;
  837. } else
  838. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  839. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  840. return trypnfs;
  841. }
  842. /*
  843. * Call the appropriate parallel I/O subsystem read function.
  844. */
  845. enum pnfs_try_status
  846. pnfs_try_to_read_data(struct nfs_read_data *rdata,
  847. const struct rpc_call_ops *call_ops)
  848. {
  849. struct inode *inode = rdata->inode;
  850. struct nfs_server *nfss = NFS_SERVER(inode);
  851. enum pnfs_try_status trypnfs;
  852. rdata->mds_ops = call_ops;
  853. dprintk("%s: Reading ino:%lu %u@%llu\n",
  854. __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
  855. trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
  856. if (trypnfs == PNFS_NOT_ATTEMPTED) {
  857. put_lseg(rdata->lseg);
  858. rdata->lseg = NULL;
  859. } else {
  860. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  861. }
  862. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  863. return trypnfs;
  864. }
  865. /*
  866. * Currently there is only one (whole file) write lseg.
  867. */
  868. static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
  869. {
  870. struct pnfs_layout_segment *lseg, *rv = NULL;
  871. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
  872. if (lseg->pls_range.iomode == IOMODE_RW)
  873. rv = lseg;
  874. return rv;
  875. }
  876. void
  877. pnfs_set_layoutcommit(struct nfs_write_data *wdata)
  878. {
  879. struct nfs_inode *nfsi = NFS_I(wdata->inode);
  880. loff_t end_pos = wdata->args.offset + wdata->res.count;
  881. spin_lock(&nfsi->vfs_inode.i_lock);
  882. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  883. /* references matched in nfs4_layoutcommit_release */
  884. get_lseg(wdata->lseg);
  885. wdata->lseg->pls_lc_cred =
  886. get_rpccred(wdata->args.context->state->owner->so_cred);
  887. mark_inode_dirty_sync(wdata->inode);
  888. dprintk("%s: Set layoutcommit for inode %lu ",
  889. __func__, wdata->inode->i_ino);
  890. }
  891. if (end_pos > wdata->lseg->pls_end_pos)
  892. wdata->lseg->pls_end_pos = end_pos;
  893. spin_unlock(&nfsi->vfs_inode.i_lock);
  894. }
  895. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  896. /*
  897. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  898. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  899. * data to disk to allow the server to recover the data if it crashes.
  900. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  901. * is off, and a COMMIT is sent to a data server, or
  902. * if WRITEs to a data server return NFS_DATA_SYNC.
  903. */
  904. int
  905. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  906. {
  907. struct nfs4_layoutcommit_data *data;
  908. struct nfs_inode *nfsi = NFS_I(inode);
  909. struct pnfs_layout_segment *lseg;
  910. struct rpc_cred *cred;
  911. loff_t end_pos;
  912. int status = 0;
  913. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  914. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  915. return 0;
  916. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  917. data = kzalloc(sizeof(*data), GFP_NOFS);
  918. if (!data) {
  919. mark_inode_dirty_sync(inode);
  920. status = -ENOMEM;
  921. goto out;
  922. }
  923. spin_lock(&inode->i_lock);
  924. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  925. spin_unlock(&inode->i_lock);
  926. kfree(data);
  927. goto out;
  928. }
  929. /*
  930. * Currently only one (whole file) write lseg which is referenced
  931. * in pnfs_set_layoutcommit and will be found.
  932. */
  933. lseg = pnfs_list_write_lseg(inode);
  934. end_pos = lseg->pls_end_pos;
  935. cred = lseg->pls_lc_cred;
  936. lseg->pls_end_pos = 0;
  937. lseg->pls_lc_cred = NULL;
  938. memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
  939. sizeof(nfsi->layout->plh_stateid.data));
  940. spin_unlock(&inode->i_lock);
  941. data->args.inode = inode;
  942. data->lseg = lseg;
  943. data->cred = cred;
  944. nfs_fattr_init(&data->fattr);
  945. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  946. data->res.fattr = &data->fattr;
  947. data->args.lastbytewritten = end_pos - 1;
  948. data->res.server = NFS_SERVER(inode);
  949. status = nfs4_proc_layoutcommit(data, sync);
  950. out:
  951. dprintk("<-- %s status %d\n", __func__, status);
  952. return status;
  953. }