pnfs.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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 <linux/nfs_page.h>
  31. #include "internal.h"
  32. #include "pnfs.h"
  33. #include "iostat.h"
  34. #define NFSDBG_FACILITY NFSDBG_PNFS
  35. /* Locking:
  36. *
  37. * pnfs_spinlock:
  38. * protects pnfs_modules_tbl.
  39. */
  40. static DEFINE_SPINLOCK(pnfs_spinlock);
  41. /*
  42. * pnfs_modules_tbl holds all pnfs modules
  43. */
  44. static LIST_HEAD(pnfs_modules_tbl);
  45. /* Return the registered pnfs layout driver module matching given id */
  46. static struct pnfs_layoutdriver_type *
  47. find_pnfs_driver_locked(u32 id)
  48. {
  49. struct pnfs_layoutdriver_type *local;
  50. list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
  51. if (local->id == id)
  52. goto out;
  53. local = NULL;
  54. out:
  55. dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
  56. return local;
  57. }
  58. static struct pnfs_layoutdriver_type *
  59. find_pnfs_driver(u32 id)
  60. {
  61. struct pnfs_layoutdriver_type *local;
  62. spin_lock(&pnfs_spinlock);
  63. local = find_pnfs_driver_locked(id);
  64. spin_unlock(&pnfs_spinlock);
  65. return local;
  66. }
  67. void
  68. unset_pnfs_layoutdriver(struct nfs_server *nfss)
  69. {
  70. if (nfss->pnfs_curr_ld) {
  71. if (nfss->pnfs_curr_ld->clear_layoutdriver)
  72. nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
  73. module_put(nfss->pnfs_curr_ld->owner);
  74. }
  75. nfss->pnfs_curr_ld = NULL;
  76. }
  77. /*
  78. * Try to set the server's pnfs module to the pnfs layout type specified by id.
  79. * Currently only one pNFS layout driver per filesystem is supported.
  80. *
  81. * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
  82. */
  83. void
  84. set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
  85. u32 id)
  86. {
  87. struct pnfs_layoutdriver_type *ld_type = NULL;
  88. if (id == 0)
  89. goto out_no_driver;
  90. if (!(server->nfs_client->cl_exchange_flags &
  91. (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
  92. printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
  93. id, server->nfs_client->cl_exchange_flags);
  94. goto out_no_driver;
  95. }
  96. ld_type = find_pnfs_driver(id);
  97. if (!ld_type) {
  98. request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
  99. ld_type = find_pnfs_driver(id);
  100. if (!ld_type) {
  101. dprintk("%s: No pNFS module found for %u.\n",
  102. __func__, id);
  103. goto out_no_driver;
  104. }
  105. }
  106. if (!try_module_get(ld_type->owner)) {
  107. dprintk("%s: Could not grab reference on module\n", __func__);
  108. goto out_no_driver;
  109. }
  110. server->pnfs_curr_ld = ld_type;
  111. if (ld_type->set_layoutdriver
  112. && ld_type->set_layoutdriver(server, mntfh)) {
  113. printk(KERN_ERR "%s: Error initializing pNFS layout driver %u.\n",
  114. __func__, id);
  115. module_put(ld_type->owner);
  116. goto out_no_driver;
  117. }
  118. dprintk("%s: pNFS module for %u set\n", __func__, id);
  119. return;
  120. out_no_driver:
  121. dprintk("%s: Using NFSv4 I/O\n", __func__);
  122. server->pnfs_curr_ld = NULL;
  123. }
  124. int
  125. pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  126. {
  127. int status = -EINVAL;
  128. struct pnfs_layoutdriver_type *tmp;
  129. if (ld_type->id == 0) {
  130. printk(KERN_ERR "%s id 0 is reserved\n", __func__);
  131. return status;
  132. }
  133. if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
  134. printk(KERN_ERR "%s Layout driver must provide "
  135. "alloc_lseg and free_lseg.\n", __func__);
  136. return status;
  137. }
  138. spin_lock(&pnfs_spinlock);
  139. tmp = find_pnfs_driver_locked(ld_type->id);
  140. if (!tmp) {
  141. list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
  142. status = 0;
  143. dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
  144. ld_type->name);
  145. } else {
  146. printk(KERN_ERR "%s Module with id %d already loaded!\n",
  147. __func__, ld_type->id);
  148. }
  149. spin_unlock(&pnfs_spinlock);
  150. return status;
  151. }
  152. EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
  153. void
  154. pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  155. {
  156. dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
  157. spin_lock(&pnfs_spinlock);
  158. list_del(&ld_type->pnfs_tblid);
  159. spin_unlock(&pnfs_spinlock);
  160. }
  161. EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
  162. /*
  163. * pNFS client layout cache
  164. */
  165. /* Need to hold i_lock if caller does not already hold reference */
  166. void
  167. get_layout_hdr(struct pnfs_layout_hdr *lo)
  168. {
  169. atomic_inc(&lo->plh_refcount);
  170. }
  171. static struct pnfs_layout_hdr *
  172. pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
  173. {
  174. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  175. return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
  176. kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
  177. }
  178. static void
  179. pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
  180. {
  181. struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
  182. put_rpccred(lo->plh_lc_cred);
  183. return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
  184. }
  185. static void
  186. destroy_layout_hdr(struct pnfs_layout_hdr *lo)
  187. {
  188. dprintk("%s: freeing layout cache %p\n", __func__, lo);
  189. BUG_ON(!list_empty(&lo->plh_layouts));
  190. NFS_I(lo->plh_inode)->layout = NULL;
  191. pnfs_free_layout_hdr(lo);
  192. }
  193. static void
  194. put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
  195. {
  196. if (atomic_dec_and_test(&lo->plh_refcount))
  197. destroy_layout_hdr(lo);
  198. }
  199. void
  200. put_layout_hdr(struct pnfs_layout_hdr *lo)
  201. {
  202. struct inode *inode = lo->plh_inode;
  203. if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
  204. destroy_layout_hdr(lo);
  205. spin_unlock(&inode->i_lock);
  206. }
  207. }
  208. static void
  209. init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
  210. {
  211. INIT_LIST_HEAD(&lseg->pls_list);
  212. INIT_LIST_HEAD(&lseg->pls_lc_list);
  213. atomic_set(&lseg->pls_refcount, 1);
  214. smp_mb();
  215. set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
  216. lseg->pls_layout = lo;
  217. }
  218. static void free_lseg(struct pnfs_layout_segment *lseg)
  219. {
  220. struct inode *ino = lseg->pls_layout->plh_inode;
  221. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  222. /* Matched by get_layout_hdr in pnfs_insert_layout */
  223. put_layout_hdr(NFS_I(ino)->layout);
  224. }
  225. static void
  226. put_lseg_common(struct pnfs_layout_segment *lseg)
  227. {
  228. struct inode *inode = lseg->pls_layout->plh_inode;
  229. WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  230. list_del_init(&lseg->pls_list);
  231. if (list_empty(&lseg->pls_layout->plh_segs)) {
  232. set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
  233. /* Matched by initial refcount set in alloc_init_layout_hdr */
  234. put_layout_hdr_locked(lseg->pls_layout);
  235. }
  236. rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
  237. }
  238. void
  239. put_lseg(struct pnfs_layout_segment *lseg)
  240. {
  241. struct inode *inode;
  242. if (!lseg)
  243. return;
  244. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  245. atomic_read(&lseg->pls_refcount),
  246. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  247. inode = lseg->pls_layout->plh_inode;
  248. if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
  249. LIST_HEAD(free_me);
  250. put_lseg_common(lseg);
  251. list_add(&lseg->pls_list, &free_me);
  252. spin_unlock(&inode->i_lock);
  253. pnfs_free_lseg_list(&free_me);
  254. }
  255. }
  256. EXPORT_SYMBOL_GPL(put_lseg);
  257. static inline u64
  258. end_offset(u64 start, u64 len)
  259. {
  260. u64 end;
  261. end = start + len;
  262. return end >= start ? end : NFS4_MAX_UINT64;
  263. }
  264. /* last octet in a range */
  265. static inline u64
  266. last_byte_offset(u64 start, u64 len)
  267. {
  268. u64 end;
  269. BUG_ON(!len);
  270. end = start + len;
  271. return end > start ? end - 1 : NFS4_MAX_UINT64;
  272. }
  273. /*
  274. * is l2 fully contained in l1?
  275. * start1 end1
  276. * [----------------------------------)
  277. * start2 end2
  278. * [----------------)
  279. */
  280. static inline int
  281. lo_seg_contained(struct pnfs_layout_range *l1,
  282. struct pnfs_layout_range *l2)
  283. {
  284. u64 start1 = l1->offset;
  285. u64 end1 = end_offset(start1, l1->length);
  286. u64 start2 = l2->offset;
  287. u64 end2 = end_offset(start2, l2->length);
  288. return (start1 <= start2) && (end1 >= end2);
  289. }
  290. /*
  291. * is l1 and l2 intersecting?
  292. * start1 end1
  293. * [----------------------------------)
  294. * start2 end2
  295. * [----------------)
  296. */
  297. static inline int
  298. lo_seg_intersecting(struct pnfs_layout_range *l1,
  299. struct pnfs_layout_range *l2)
  300. {
  301. u64 start1 = l1->offset;
  302. u64 end1 = end_offset(start1, l1->length);
  303. u64 start2 = l2->offset;
  304. u64 end2 = end_offset(start2, l2->length);
  305. return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
  306. (end2 == NFS4_MAX_UINT64 || end2 > start1);
  307. }
  308. static bool
  309. should_free_lseg(struct pnfs_layout_range *lseg_range,
  310. struct pnfs_layout_range *recall_range)
  311. {
  312. return (recall_range->iomode == IOMODE_ANY ||
  313. lseg_range->iomode == recall_range->iomode) &&
  314. lo_seg_intersecting(lseg_range, recall_range);
  315. }
  316. /* Returns 1 if lseg is removed from list, 0 otherwise */
  317. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  318. struct list_head *tmp_list)
  319. {
  320. int rv = 0;
  321. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  322. /* Remove the reference keeping the lseg in the
  323. * list. It will now be removed when all
  324. * outstanding io is finished.
  325. */
  326. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  327. atomic_read(&lseg->pls_refcount));
  328. if (atomic_dec_and_test(&lseg->pls_refcount)) {
  329. put_lseg_common(lseg);
  330. list_add(&lseg->pls_list, tmp_list);
  331. rv = 1;
  332. }
  333. }
  334. return rv;
  335. }
  336. /* Returns count of number of matching invalid lsegs remaining in list
  337. * after call.
  338. */
  339. int
  340. mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
  341. struct list_head *tmp_list,
  342. struct pnfs_layout_range *recall_range)
  343. {
  344. struct pnfs_layout_segment *lseg, *next;
  345. int invalid = 0, removed = 0;
  346. dprintk("%s:Begin lo %p\n", __func__, lo);
  347. if (list_empty(&lo->plh_segs)) {
  348. if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
  349. put_layout_hdr_locked(lo);
  350. return 0;
  351. }
  352. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  353. if (!recall_range ||
  354. should_free_lseg(&lseg->pls_range, recall_range)) {
  355. dprintk("%s: freeing lseg %p iomode %d "
  356. "offset %llu length %llu\n", __func__,
  357. lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
  358. lseg->pls_range.length);
  359. invalid++;
  360. removed += mark_lseg_invalid(lseg, tmp_list);
  361. }
  362. dprintk("%s:Return %i\n", __func__, invalid - removed);
  363. return invalid - removed;
  364. }
  365. /* note free_me must contain lsegs from a single layout_hdr */
  366. void
  367. pnfs_free_lseg_list(struct list_head *free_me)
  368. {
  369. struct pnfs_layout_segment *lseg, *tmp;
  370. struct pnfs_layout_hdr *lo;
  371. if (list_empty(free_me))
  372. return;
  373. lo = list_first_entry(free_me, struct pnfs_layout_segment,
  374. pls_list)->pls_layout;
  375. if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
  376. struct nfs_client *clp;
  377. clp = NFS_SERVER(lo->plh_inode)->nfs_client;
  378. spin_lock(&clp->cl_lock);
  379. list_del_init(&lo->plh_layouts);
  380. spin_unlock(&clp->cl_lock);
  381. }
  382. list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
  383. list_del(&lseg->pls_list);
  384. free_lseg(lseg);
  385. }
  386. }
  387. void
  388. pnfs_destroy_layout(struct nfs_inode *nfsi)
  389. {
  390. struct pnfs_layout_hdr *lo;
  391. LIST_HEAD(tmp_list);
  392. spin_lock(&nfsi->vfs_inode.i_lock);
  393. lo = nfsi->layout;
  394. if (lo) {
  395. lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
  396. mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
  397. }
  398. spin_unlock(&nfsi->vfs_inode.i_lock);
  399. pnfs_free_lseg_list(&tmp_list);
  400. }
  401. /*
  402. * Called by the state manger to remove all layouts established under an
  403. * expired lease.
  404. */
  405. void
  406. pnfs_destroy_all_layouts(struct nfs_client *clp)
  407. {
  408. struct nfs_server *server;
  409. struct pnfs_layout_hdr *lo;
  410. LIST_HEAD(tmp_list);
  411. nfs4_deviceid_mark_client_invalid(clp);
  412. nfs4_deviceid_purge_client(clp);
  413. spin_lock(&clp->cl_lock);
  414. rcu_read_lock();
  415. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  416. if (!list_empty(&server->layouts))
  417. list_splice_init(&server->layouts, &tmp_list);
  418. }
  419. rcu_read_unlock();
  420. spin_unlock(&clp->cl_lock);
  421. while (!list_empty(&tmp_list)) {
  422. lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
  423. plh_layouts);
  424. dprintk("%s freeing layout for inode %lu\n", __func__,
  425. lo->plh_inode->i_ino);
  426. list_del_init(&lo->plh_layouts);
  427. pnfs_destroy_layout(NFS_I(lo->plh_inode));
  428. }
  429. }
  430. /* update lo->plh_stateid with new if is more recent */
  431. void
  432. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  433. bool update_barrier)
  434. {
  435. u32 oldseq, newseq;
  436. oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  437. newseq = be32_to_cpu(new->stateid.seqid);
  438. if ((int)(newseq - oldseq) > 0) {
  439. memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
  440. if (update_barrier) {
  441. u32 new_barrier = be32_to_cpu(new->stateid.seqid);
  442. if ((int)(new_barrier - lo->plh_barrier))
  443. lo->plh_barrier = new_barrier;
  444. } else {
  445. /* Because of wraparound, we want to keep the barrier
  446. * "close" to the current seqids. It needs to be
  447. * within 2**31 to count as "behind", so if it
  448. * gets too near that limit, give us a litle leeway
  449. * and bring it to within 2**30.
  450. * NOTE - and yes, this is all unsigned arithmetic.
  451. */
  452. if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
  453. lo->plh_barrier = newseq - (1 << 30);
  454. }
  455. }
  456. }
  457. /* lget is set to 1 if called from inside send_layoutget call chain */
  458. static bool
  459. pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
  460. int lget)
  461. {
  462. if ((stateid) &&
  463. (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
  464. return true;
  465. return lo->plh_block_lgets ||
  466. test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
  467. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  468. (list_empty(&lo->plh_segs) &&
  469. (atomic_read(&lo->plh_outstanding) > lget));
  470. }
  471. int
  472. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  473. struct nfs4_state *open_state)
  474. {
  475. int status = 0;
  476. dprintk("--> %s\n", __func__);
  477. spin_lock(&lo->plh_inode->i_lock);
  478. if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
  479. status = -EAGAIN;
  480. } else if (list_empty(&lo->plh_segs)) {
  481. int seq;
  482. do {
  483. seq = read_seqbegin(&open_state->seqlock);
  484. memcpy(dst->data, open_state->stateid.data,
  485. sizeof(open_state->stateid.data));
  486. } while (read_seqretry(&open_state->seqlock, seq));
  487. } else
  488. memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
  489. spin_unlock(&lo->plh_inode->i_lock);
  490. dprintk("<-- %s\n", __func__);
  491. return status;
  492. }
  493. /*
  494. * Get layout from server.
  495. * for now, assume that whole file layouts are requested.
  496. * arg->offset: 0
  497. * arg->length: all ones
  498. */
  499. static struct pnfs_layout_segment *
  500. send_layoutget(struct pnfs_layout_hdr *lo,
  501. struct nfs_open_context *ctx,
  502. struct pnfs_layout_range *range,
  503. gfp_t gfp_flags)
  504. {
  505. struct inode *ino = lo->plh_inode;
  506. struct nfs_server *server = NFS_SERVER(ino);
  507. struct nfs4_layoutget *lgp;
  508. struct pnfs_layout_segment *lseg = NULL;
  509. struct page **pages = NULL;
  510. int i;
  511. u32 max_resp_sz, max_pages;
  512. dprintk("--> %s\n", __func__);
  513. BUG_ON(ctx == NULL);
  514. lgp = kzalloc(sizeof(*lgp), gfp_flags);
  515. if (lgp == NULL)
  516. return NULL;
  517. /* allocate pages for xdr post processing */
  518. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  519. max_pages = max_resp_sz >> PAGE_SHIFT;
  520. pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
  521. if (!pages)
  522. goto out_err_free;
  523. for (i = 0; i < max_pages; i++) {
  524. pages[i] = alloc_page(gfp_flags);
  525. if (!pages[i])
  526. goto out_err_free;
  527. }
  528. lgp->args.minlength = PAGE_CACHE_SIZE;
  529. if (lgp->args.minlength > range->length)
  530. lgp->args.minlength = range->length;
  531. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  532. lgp->args.range = *range;
  533. lgp->args.type = server->pnfs_curr_ld->id;
  534. lgp->args.inode = ino;
  535. lgp->args.ctx = get_nfs_open_context(ctx);
  536. lgp->args.layout.pages = pages;
  537. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  538. lgp->lsegpp = &lseg;
  539. lgp->gfp_flags = gfp_flags;
  540. /* Synchronously retrieve layout information from server and
  541. * store in lseg.
  542. */
  543. nfs4_proc_layoutget(lgp);
  544. if (!lseg) {
  545. /* remember that LAYOUTGET failed and suspend trying */
  546. set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
  547. }
  548. /* free xdr pages */
  549. for (i = 0; i < max_pages; i++)
  550. __free_page(pages[i]);
  551. kfree(pages);
  552. return lseg;
  553. out_err_free:
  554. /* free any allocated xdr pages, lgp as it's not used */
  555. if (pages) {
  556. for (i = 0; i < max_pages; i++) {
  557. if (!pages[i])
  558. break;
  559. __free_page(pages[i]);
  560. }
  561. kfree(pages);
  562. }
  563. kfree(lgp);
  564. return NULL;
  565. }
  566. /* Initiates a LAYOUTRETURN(FILE) */
  567. int
  568. _pnfs_return_layout(struct inode *ino)
  569. {
  570. struct pnfs_layout_hdr *lo = NULL;
  571. struct nfs_inode *nfsi = NFS_I(ino);
  572. LIST_HEAD(tmp_list);
  573. struct nfs4_layoutreturn *lrp;
  574. nfs4_stateid stateid;
  575. int status = 0;
  576. dprintk("--> %s\n", __func__);
  577. spin_lock(&ino->i_lock);
  578. lo = nfsi->layout;
  579. if (!lo) {
  580. spin_unlock(&ino->i_lock);
  581. dprintk("%s: no layout to return\n", __func__);
  582. return status;
  583. }
  584. stateid = nfsi->layout->plh_stateid;
  585. /* Reference matched in nfs4_layoutreturn_release */
  586. get_layout_hdr(lo);
  587. mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
  588. lo->plh_block_lgets++;
  589. spin_unlock(&ino->i_lock);
  590. pnfs_free_lseg_list(&tmp_list);
  591. WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
  592. lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
  593. if (unlikely(lrp == NULL)) {
  594. status = -ENOMEM;
  595. set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
  596. set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
  597. put_layout_hdr(lo);
  598. goto out;
  599. }
  600. lrp->args.stateid = stateid;
  601. lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
  602. lrp->args.inode = ino;
  603. lrp->args.layout = lo;
  604. lrp->clp = NFS_SERVER(ino)->nfs_client;
  605. status = nfs4_proc_layoutreturn(lrp);
  606. out:
  607. dprintk("<-- %s status: %d\n", __func__, status);
  608. return status;
  609. }
  610. bool pnfs_roc(struct inode *ino)
  611. {
  612. struct pnfs_layout_hdr *lo;
  613. struct pnfs_layout_segment *lseg, *tmp;
  614. LIST_HEAD(tmp_list);
  615. bool found = false;
  616. spin_lock(&ino->i_lock);
  617. lo = NFS_I(ino)->layout;
  618. if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
  619. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
  620. goto out_nolayout;
  621. list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
  622. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  623. mark_lseg_invalid(lseg, &tmp_list);
  624. found = true;
  625. }
  626. if (!found)
  627. goto out_nolayout;
  628. lo->plh_block_lgets++;
  629. get_layout_hdr(lo); /* matched in pnfs_roc_release */
  630. spin_unlock(&ino->i_lock);
  631. pnfs_free_lseg_list(&tmp_list);
  632. return true;
  633. out_nolayout:
  634. spin_unlock(&ino->i_lock);
  635. return false;
  636. }
  637. void pnfs_roc_release(struct inode *ino)
  638. {
  639. struct pnfs_layout_hdr *lo;
  640. spin_lock(&ino->i_lock);
  641. lo = NFS_I(ino)->layout;
  642. lo->plh_block_lgets--;
  643. put_layout_hdr_locked(lo);
  644. spin_unlock(&ino->i_lock);
  645. }
  646. void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
  647. {
  648. struct pnfs_layout_hdr *lo;
  649. spin_lock(&ino->i_lock);
  650. lo = NFS_I(ino)->layout;
  651. if ((int)(barrier - lo->plh_barrier) > 0)
  652. lo->plh_barrier = barrier;
  653. spin_unlock(&ino->i_lock);
  654. }
  655. bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
  656. {
  657. struct nfs_inode *nfsi = NFS_I(ino);
  658. struct pnfs_layout_segment *lseg;
  659. bool found = false;
  660. spin_lock(&ino->i_lock);
  661. list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
  662. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  663. found = true;
  664. break;
  665. }
  666. if (!found) {
  667. struct pnfs_layout_hdr *lo = nfsi->layout;
  668. u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  669. /* Since close does not return a layout stateid for use as
  670. * a barrier, we choose the worst-case barrier.
  671. */
  672. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  673. }
  674. spin_unlock(&ino->i_lock);
  675. return found;
  676. }
  677. /*
  678. * Compare two layout segments for sorting into layout cache.
  679. * We want to preferentially return RW over RO layouts, so ensure those
  680. * are seen first.
  681. */
  682. static s64
  683. cmp_layout(struct pnfs_layout_range *l1,
  684. struct pnfs_layout_range *l2)
  685. {
  686. s64 d;
  687. /* high offset > low offset */
  688. d = l1->offset - l2->offset;
  689. if (d)
  690. return d;
  691. /* short length > long length */
  692. d = l2->length - l1->length;
  693. if (d)
  694. return d;
  695. /* read > read/write */
  696. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  697. }
  698. static void
  699. pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  700. struct pnfs_layout_segment *lseg)
  701. {
  702. struct pnfs_layout_segment *lp;
  703. dprintk("%s:Begin\n", __func__);
  704. assert_spin_locked(&lo->plh_inode->i_lock);
  705. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  706. if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
  707. continue;
  708. list_add_tail(&lseg->pls_list, &lp->pls_list);
  709. dprintk("%s: inserted lseg %p "
  710. "iomode %d offset %llu length %llu before "
  711. "lp %p iomode %d offset %llu length %llu\n",
  712. __func__, lseg, lseg->pls_range.iomode,
  713. lseg->pls_range.offset, lseg->pls_range.length,
  714. lp, lp->pls_range.iomode, lp->pls_range.offset,
  715. lp->pls_range.length);
  716. goto out;
  717. }
  718. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  719. dprintk("%s: inserted lseg %p "
  720. "iomode %d offset %llu length %llu at tail\n",
  721. __func__, lseg, lseg->pls_range.iomode,
  722. lseg->pls_range.offset, lseg->pls_range.length);
  723. out:
  724. get_layout_hdr(lo);
  725. dprintk("%s:Return\n", __func__);
  726. }
  727. static struct pnfs_layout_hdr *
  728. alloc_init_layout_hdr(struct inode *ino,
  729. struct nfs_open_context *ctx,
  730. gfp_t gfp_flags)
  731. {
  732. struct pnfs_layout_hdr *lo;
  733. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  734. if (!lo)
  735. return NULL;
  736. atomic_set(&lo->plh_refcount, 1);
  737. INIT_LIST_HEAD(&lo->plh_layouts);
  738. INIT_LIST_HEAD(&lo->plh_segs);
  739. INIT_LIST_HEAD(&lo->plh_bulk_recall);
  740. lo->plh_inode = ino;
  741. lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
  742. return lo;
  743. }
  744. static struct pnfs_layout_hdr *
  745. pnfs_find_alloc_layout(struct inode *ino,
  746. struct nfs_open_context *ctx,
  747. gfp_t gfp_flags)
  748. {
  749. struct nfs_inode *nfsi = NFS_I(ino);
  750. struct pnfs_layout_hdr *new = NULL;
  751. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  752. assert_spin_locked(&ino->i_lock);
  753. if (nfsi->layout) {
  754. if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
  755. return NULL;
  756. else
  757. return nfsi->layout;
  758. }
  759. spin_unlock(&ino->i_lock);
  760. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  761. spin_lock(&ino->i_lock);
  762. if (likely(nfsi->layout == NULL)) /* Won the race? */
  763. nfsi->layout = new;
  764. else
  765. pnfs_free_layout_hdr(new);
  766. return nfsi->layout;
  767. }
  768. /*
  769. * iomode matching rules:
  770. * iomode lseg match
  771. * ----- ----- -----
  772. * ANY READ true
  773. * ANY RW true
  774. * RW READ false
  775. * RW RW true
  776. * READ READ true
  777. * READ RW true
  778. */
  779. static int
  780. is_matching_lseg(struct pnfs_layout_range *ls_range,
  781. struct pnfs_layout_range *range)
  782. {
  783. struct pnfs_layout_range range1;
  784. if ((range->iomode == IOMODE_RW &&
  785. ls_range->iomode != IOMODE_RW) ||
  786. !lo_seg_intersecting(ls_range, range))
  787. return 0;
  788. /* range1 covers only the first byte in the range */
  789. range1 = *range;
  790. range1.length = 1;
  791. return lo_seg_contained(ls_range, &range1);
  792. }
  793. /*
  794. * lookup range in layout
  795. */
  796. static struct pnfs_layout_segment *
  797. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  798. struct pnfs_layout_range *range)
  799. {
  800. struct pnfs_layout_segment *lseg, *ret = NULL;
  801. dprintk("%s:Begin\n", __func__);
  802. assert_spin_locked(&lo->plh_inode->i_lock);
  803. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  804. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  805. is_matching_lseg(&lseg->pls_range, range)) {
  806. ret = get_lseg(lseg);
  807. break;
  808. }
  809. if (lseg->pls_range.offset > range->offset)
  810. break;
  811. }
  812. dprintk("%s:Return lseg %p ref %d\n",
  813. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  814. return ret;
  815. }
  816. /*
  817. * Layout segment is retreived from the server if not cached.
  818. * The appropriate layout segment is referenced and returned to the caller.
  819. */
  820. struct pnfs_layout_segment *
  821. pnfs_update_layout(struct inode *ino,
  822. struct nfs_open_context *ctx,
  823. loff_t pos,
  824. u64 count,
  825. enum pnfs_iomode iomode,
  826. gfp_t gfp_flags)
  827. {
  828. struct pnfs_layout_range arg = {
  829. .iomode = iomode,
  830. .offset = pos,
  831. .length = count,
  832. };
  833. unsigned pg_offset;
  834. struct nfs_inode *nfsi = NFS_I(ino);
  835. struct nfs_server *server = NFS_SERVER(ino);
  836. struct nfs_client *clp = server->nfs_client;
  837. struct pnfs_layout_hdr *lo;
  838. struct pnfs_layout_segment *lseg = NULL;
  839. bool first = false;
  840. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  841. return NULL;
  842. spin_lock(&ino->i_lock);
  843. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  844. if (lo == NULL) {
  845. dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
  846. goto out_unlock;
  847. }
  848. /* Do we even need to bother with this? */
  849. if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
  850. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  851. dprintk("%s matches recall, use MDS\n", __func__);
  852. goto out_unlock;
  853. }
  854. /* if LAYOUTGET already failed once we don't try again */
  855. if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
  856. goto out_unlock;
  857. /* Check to see if the layout for the given range already exists */
  858. lseg = pnfs_find_lseg(lo, &arg);
  859. if (lseg)
  860. goto out_unlock;
  861. if (pnfs_layoutgets_blocked(lo, NULL, 0))
  862. goto out_unlock;
  863. atomic_inc(&lo->plh_outstanding);
  864. get_layout_hdr(lo);
  865. if (list_empty(&lo->plh_segs))
  866. first = true;
  867. spin_unlock(&ino->i_lock);
  868. if (first) {
  869. /* The lo must be on the clp list if there is any
  870. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  871. */
  872. spin_lock(&clp->cl_lock);
  873. BUG_ON(!list_empty(&lo->plh_layouts));
  874. list_add_tail(&lo->plh_layouts, &server->layouts);
  875. spin_unlock(&clp->cl_lock);
  876. }
  877. pg_offset = arg.offset & ~PAGE_CACHE_MASK;
  878. if (pg_offset) {
  879. arg.offset -= pg_offset;
  880. arg.length += pg_offset;
  881. }
  882. if (arg.length != NFS4_MAX_UINT64)
  883. arg.length = PAGE_CACHE_ALIGN(arg.length);
  884. lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
  885. if (!lseg && first) {
  886. spin_lock(&clp->cl_lock);
  887. list_del_init(&lo->plh_layouts);
  888. spin_unlock(&clp->cl_lock);
  889. }
  890. atomic_dec(&lo->plh_outstanding);
  891. put_layout_hdr(lo);
  892. out:
  893. dprintk("%s end, state 0x%lx lseg %p\n", __func__,
  894. nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
  895. return lseg;
  896. out_unlock:
  897. spin_unlock(&ino->i_lock);
  898. goto out;
  899. }
  900. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  901. int
  902. pnfs_layout_process(struct nfs4_layoutget *lgp)
  903. {
  904. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  905. struct nfs4_layoutget_res *res = &lgp->res;
  906. struct pnfs_layout_segment *lseg;
  907. struct inode *ino = lo->plh_inode;
  908. struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
  909. int status = 0;
  910. /* Inject layout blob into I/O device driver */
  911. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  912. if (!lseg || IS_ERR(lseg)) {
  913. if (!lseg)
  914. status = -ENOMEM;
  915. else
  916. status = PTR_ERR(lseg);
  917. dprintk("%s: Could not allocate layout: error %d\n",
  918. __func__, status);
  919. goto out;
  920. }
  921. spin_lock(&ino->i_lock);
  922. if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
  923. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  924. dprintk("%s forget reply due to recall\n", __func__);
  925. goto out_forget_reply;
  926. }
  927. if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
  928. dprintk("%s forget reply due to state\n", __func__);
  929. goto out_forget_reply;
  930. }
  931. init_lseg(lo, lseg);
  932. lseg->pls_range = res->range;
  933. *lgp->lsegpp = get_lseg(lseg);
  934. pnfs_insert_layout(lo, lseg);
  935. if (res->return_on_close) {
  936. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  937. set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
  938. }
  939. /* Done processing layoutget. Set the layout stateid */
  940. pnfs_set_layout_stateid(lo, &res->stateid, false);
  941. spin_unlock(&ino->i_lock);
  942. out:
  943. return status;
  944. out_forget_reply:
  945. spin_unlock(&ino->i_lock);
  946. lseg->pls_layout = lo;
  947. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  948. goto out;
  949. }
  950. void
  951. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  952. {
  953. BUG_ON(pgio->pg_lseg != NULL);
  954. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  955. req->wb_context,
  956. req_offset(req),
  957. req->wb_bytes,
  958. IOMODE_READ,
  959. GFP_KERNEL);
  960. /* If no lseg, fall back to read through mds */
  961. if (pgio->pg_lseg == NULL)
  962. nfs_pageio_reset_read_mds(pgio);
  963. }
  964. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  965. void
  966. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  967. {
  968. BUG_ON(pgio->pg_lseg != NULL);
  969. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  970. req->wb_context,
  971. req_offset(req),
  972. req->wb_bytes,
  973. IOMODE_RW,
  974. GFP_NOFS);
  975. /* If no lseg, fall back to write through mds */
  976. if (pgio->pg_lseg == NULL)
  977. nfs_pageio_reset_write_mds(pgio);
  978. }
  979. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  980. bool
  981. pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
  982. {
  983. struct nfs_server *server = NFS_SERVER(inode);
  984. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  985. if (ld == NULL)
  986. return false;
  987. nfs_pageio_init(pgio, inode, ld->pg_read_ops, server->rsize, 0);
  988. return true;
  989. }
  990. bool
  991. pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode, int ioflags)
  992. {
  993. struct nfs_server *server = NFS_SERVER(inode);
  994. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  995. if (ld == NULL)
  996. return false;
  997. nfs_pageio_init(pgio, inode, ld->pg_write_ops, server->wsize, ioflags);
  998. return true;
  999. }
  1000. bool
  1001. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  1002. struct nfs_page *req)
  1003. {
  1004. if (pgio->pg_lseg == NULL)
  1005. return nfs_generic_pg_test(pgio, prev, req);
  1006. /*
  1007. * Test if a nfs_page is fully contained in the pnfs_layout_range.
  1008. * Note that this test makes several assumptions:
  1009. * - that the previous nfs_page in the struct nfs_pageio_descriptor
  1010. * is known to lie within the range.
  1011. * - that the nfs_page being tested is known to be contiguous with the
  1012. * previous nfs_page.
  1013. * - Layout ranges are page aligned, so we only have to test the
  1014. * start offset of the request.
  1015. *
  1016. * Please also note that 'end_offset' is actually the offset of the
  1017. * first byte that lies outside the pnfs_layout_range. FIXME?
  1018. *
  1019. */
  1020. return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
  1021. pgio->pg_lseg->pls_range.length);
  1022. }
  1023. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  1024. /*
  1025. * Called by non rpc-based layout drivers
  1026. */
  1027. int
  1028. pnfs_ld_write_done(struct nfs_write_data *data)
  1029. {
  1030. int status;
  1031. if (!data->pnfs_error) {
  1032. pnfs_set_layoutcommit(data);
  1033. data->mds_ops->rpc_call_done(&data->task, data);
  1034. data->mds_ops->rpc_release(data);
  1035. return 0;
  1036. }
  1037. dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
  1038. data->pnfs_error);
  1039. status = nfs_initiate_write(data, NFS_CLIENT(data->inode),
  1040. data->mds_ops, NFS_FILE_SYNC);
  1041. return status ? : -EAGAIN;
  1042. }
  1043. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  1044. static void
  1045. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  1046. struct nfs_write_data *data)
  1047. {
  1048. list_splice_tail_init(&data->pages, &desc->pg_list);
  1049. if (data->req && list_empty(&data->req->wb_list))
  1050. nfs_list_add_request(data->req, &desc->pg_list);
  1051. nfs_pageio_reset_write_mds(desc);
  1052. desc->pg_recoalesce = 1;
  1053. nfs_writedata_release(data);
  1054. }
  1055. static enum pnfs_try_status
  1056. pnfs_try_to_write_data(struct nfs_write_data *wdata,
  1057. const struct rpc_call_ops *call_ops,
  1058. struct pnfs_layout_segment *lseg,
  1059. int how)
  1060. {
  1061. struct inode *inode = wdata->inode;
  1062. enum pnfs_try_status trypnfs;
  1063. struct nfs_server *nfss = NFS_SERVER(inode);
  1064. wdata->mds_ops = call_ops;
  1065. wdata->lseg = get_lseg(lseg);
  1066. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  1067. inode->i_ino, wdata->args.count, wdata->args.offset, how);
  1068. trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
  1069. if (trypnfs == PNFS_NOT_ATTEMPTED) {
  1070. put_lseg(wdata->lseg);
  1071. wdata->lseg = NULL;
  1072. } else
  1073. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  1074. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1075. return trypnfs;
  1076. }
  1077. static void
  1078. pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
  1079. {
  1080. struct nfs_write_data *data;
  1081. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1082. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1083. desc->pg_lseg = NULL;
  1084. while (!list_empty(head)) {
  1085. enum pnfs_try_status trypnfs;
  1086. data = list_entry(head->next, struct nfs_write_data, list);
  1087. list_del_init(&data->list);
  1088. trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
  1089. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1090. pnfs_write_through_mds(desc, data);
  1091. }
  1092. put_lseg(lseg);
  1093. }
  1094. int
  1095. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  1096. {
  1097. LIST_HEAD(head);
  1098. int ret;
  1099. ret = nfs_generic_flush(desc, &head);
  1100. if (ret != 0) {
  1101. put_lseg(desc->pg_lseg);
  1102. desc->pg_lseg = NULL;
  1103. return ret;
  1104. }
  1105. pnfs_do_multiple_writes(desc, &head, desc->pg_ioflags);
  1106. return 0;
  1107. }
  1108. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  1109. /*
  1110. * Called by non rpc-based layout drivers
  1111. */
  1112. int
  1113. pnfs_ld_read_done(struct nfs_read_data *data)
  1114. {
  1115. int status;
  1116. if (!data->pnfs_error) {
  1117. __nfs4_read_done_cb(data);
  1118. data->mds_ops->rpc_call_done(&data->task, data);
  1119. data->mds_ops->rpc_release(data);
  1120. return 0;
  1121. }
  1122. dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
  1123. data->pnfs_error);
  1124. status = nfs_initiate_read(data, NFS_CLIENT(data->inode),
  1125. data->mds_ops);
  1126. return status ? : -EAGAIN;
  1127. }
  1128. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  1129. static void
  1130. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  1131. struct nfs_read_data *data)
  1132. {
  1133. list_splice_tail_init(&data->pages, &desc->pg_list);
  1134. if (data->req && list_empty(&data->req->wb_list))
  1135. nfs_list_add_request(data->req, &desc->pg_list);
  1136. nfs_pageio_reset_read_mds(desc);
  1137. desc->pg_recoalesce = 1;
  1138. nfs_readdata_release(data);
  1139. }
  1140. /*
  1141. * Call the appropriate parallel I/O subsystem read function.
  1142. */
  1143. static enum pnfs_try_status
  1144. pnfs_try_to_read_data(struct nfs_read_data *rdata,
  1145. const struct rpc_call_ops *call_ops,
  1146. struct pnfs_layout_segment *lseg)
  1147. {
  1148. struct inode *inode = rdata->inode;
  1149. struct nfs_server *nfss = NFS_SERVER(inode);
  1150. enum pnfs_try_status trypnfs;
  1151. rdata->mds_ops = call_ops;
  1152. rdata->lseg = get_lseg(lseg);
  1153. dprintk("%s: Reading ino:%lu %u@%llu\n",
  1154. __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
  1155. trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
  1156. if (trypnfs == PNFS_NOT_ATTEMPTED) {
  1157. put_lseg(rdata->lseg);
  1158. rdata->lseg = NULL;
  1159. } else {
  1160. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  1161. }
  1162. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1163. return trypnfs;
  1164. }
  1165. static void
  1166. pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
  1167. {
  1168. struct nfs_read_data *data;
  1169. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1170. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1171. desc->pg_lseg = NULL;
  1172. while (!list_empty(head)) {
  1173. enum pnfs_try_status trypnfs;
  1174. data = list_entry(head->next, struct nfs_read_data, list);
  1175. list_del_init(&data->list);
  1176. trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
  1177. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1178. pnfs_read_through_mds(desc, data);
  1179. }
  1180. put_lseg(lseg);
  1181. }
  1182. int
  1183. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  1184. {
  1185. LIST_HEAD(head);
  1186. int ret;
  1187. ret = nfs_generic_pagein(desc, &head);
  1188. if (ret != 0) {
  1189. put_lseg(desc->pg_lseg);
  1190. desc->pg_lseg = NULL;
  1191. return ret;
  1192. }
  1193. pnfs_do_multiple_reads(desc, &head);
  1194. return 0;
  1195. }
  1196. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  1197. /*
  1198. * There can be multiple RW segments.
  1199. */
  1200. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  1201. {
  1202. struct pnfs_layout_segment *lseg;
  1203. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  1204. if (lseg->pls_range.iomode == IOMODE_RW &&
  1205. test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  1206. list_add(&lseg->pls_lc_list, listp);
  1207. }
  1208. }
  1209. void
  1210. pnfs_set_layoutcommit(struct nfs_write_data *wdata)
  1211. {
  1212. struct nfs_inode *nfsi = NFS_I(wdata->inode);
  1213. loff_t end_pos = wdata->mds_offset + wdata->res.count;
  1214. bool mark_as_dirty = false;
  1215. spin_lock(&nfsi->vfs_inode.i_lock);
  1216. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1217. mark_as_dirty = true;
  1218. dprintk("%s: Set layoutcommit for inode %lu ",
  1219. __func__, wdata->inode->i_ino);
  1220. }
  1221. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &wdata->lseg->pls_flags)) {
  1222. /* references matched in nfs4_layoutcommit_release */
  1223. get_lseg(wdata->lseg);
  1224. }
  1225. if (end_pos > nfsi->layout->plh_lwb)
  1226. nfsi->layout->plh_lwb = end_pos;
  1227. spin_unlock(&nfsi->vfs_inode.i_lock);
  1228. dprintk("%s: lseg %p end_pos %llu\n",
  1229. __func__, wdata->lseg, nfsi->layout->plh_lwb);
  1230. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  1231. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  1232. if (mark_as_dirty)
  1233. mark_inode_dirty_sync(wdata->inode);
  1234. }
  1235. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  1236. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  1237. {
  1238. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  1239. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  1240. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  1241. }
  1242. /*
  1243. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  1244. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  1245. * data to disk to allow the server to recover the data if it crashes.
  1246. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  1247. * is off, and a COMMIT is sent to a data server, or
  1248. * if WRITEs to a data server return NFS_DATA_SYNC.
  1249. */
  1250. int
  1251. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  1252. {
  1253. struct nfs4_layoutcommit_data *data;
  1254. struct nfs_inode *nfsi = NFS_I(inode);
  1255. loff_t end_pos;
  1256. int status = 0;
  1257. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  1258. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1259. return 0;
  1260. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  1261. data = kzalloc(sizeof(*data), GFP_NOFS);
  1262. if (!data) {
  1263. mark_inode_dirty_sync(inode);
  1264. status = -ENOMEM;
  1265. goto out;
  1266. }
  1267. INIT_LIST_HEAD(&data->lseg_list);
  1268. spin_lock(&inode->i_lock);
  1269. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1270. spin_unlock(&inode->i_lock);
  1271. kfree(data);
  1272. goto out;
  1273. }
  1274. pnfs_list_write_lseg(inode, &data->lseg_list);
  1275. end_pos = nfsi->layout->plh_lwb;
  1276. nfsi->layout->plh_lwb = 0;
  1277. memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
  1278. sizeof(nfsi->layout->plh_stateid.data));
  1279. spin_unlock(&inode->i_lock);
  1280. data->args.inode = inode;
  1281. data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
  1282. nfs_fattr_init(&data->fattr);
  1283. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  1284. data->res.fattr = &data->fattr;
  1285. data->args.lastbytewritten = end_pos - 1;
  1286. data->res.server = NFS_SERVER(inode);
  1287. status = nfs4_proc_layoutcommit(data, sync);
  1288. out:
  1289. dprintk("<-- %s status %d\n", __func__, status);
  1290. return status;
  1291. }