pnfs.c 29 KB

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