pnfs.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  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 <linux/module.h>
  32. #include "internal.h"
  33. #include "pnfs.h"
  34. #include "iostat.h"
  35. #define NFSDBG_FACILITY NFSDBG_PNFS
  36. #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
  37. /* Locking:
  38. *
  39. * pnfs_spinlock:
  40. * protects pnfs_modules_tbl.
  41. */
  42. static DEFINE_SPINLOCK(pnfs_spinlock);
  43. /*
  44. * pnfs_modules_tbl holds all pnfs modules
  45. */
  46. static LIST_HEAD(pnfs_modules_tbl);
  47. /* Return the registered pnfs layout driver module matching given id */
  48. static struct pnfs_layoutdriver_type *
  49. find_pnfs_driver_locked(u32 id)
  50. {
  51. struct pnfs_layoutdriver_type *local;
  52. list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
  53. if (local->id == id)
  54. goto out;
  55. local = NULL;
  56. out:
  57. dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
  58. return local;
  59. }
  60. static struct pnfs_layoutdriver_type *
  61. find_pnfs_driver(u32 id)
  62. {
  63. struct pnfs_layoutdriver_type *local;
  64. spin_lock(&pnfs_spinlock);
  65. local = find_pnfs_driver_locked(id);
  66. if (local != NULL && !try_module_get(local->owner)) {
  67. dprintk("%s: Could not grab reference on module\n", __func__);
  68. local = NULL;
  69. }
  70. spin_unlock(&pnfs_spinlock);
  71. return local;
  72. }
  73. void
  74. unset_pnfs_layoutdriver(struct nfs_server *nfss)
  75. {
  76. if (nfss->pnfs_curr_ld) {
  77. if (nfss->pnfs_curr_ld->clear_layoutdriver)
  78. nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
  79. /* Decrement the MDS count. Purge the deviceid cache if zero */
  80. if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
  81. nfs4_deviceid_purge_client(nfss->nfs_client);
  82. module_put(nfss->pnfs_curr_ld->owner);
  83. }
  84. nfss->pnfs_curr_ld = NULL;
  85. }
  86. /*
  87. * Try to set the server's pnfs module to the pnfs layout type specified by id.
  88. * Currently only one pNFS layout driver per filesystem is supported.
  89. *
  90. * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
  91. */
  92. void
  93. set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
  94. u32 id)
  95. {
  96. struct pnfs_layoutdriver_type *ld_type = NULL;
  97. if (id == 0)
  98. goto out_no_driver;
  99. if (!(server->nfs_client->cl_exchange_flags &
  100. (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
  101. printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
  102. __func__, id, server->nfs_client->cl_exchange_flags);
  103. goto out_no_driver;
  104. }
  105. ld_type = find_pnfs_driver(id);
  106. if (!ld_type) {
  107. request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
  108. ld_type = find_pnfs_driver(id);
  109. if (!ld_type) {
  110. dprintk("%s: No pNFS module found for %u.\n",
  111. __func__, id);
  112. goto out_no_driver;
  113. }
  114. }
  115. server->pnfs_curr_ld = ld_type;
  116. if (ld_type->set_layoutdriver
  117. && ld_type->set_layoutdriver(server, mntfh)) {
  118. printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
  119. "driver %u.\n", __func__, id);
  120. module_put(ld_type->owner);
  121. goto out_no_driver;
  122. }
  123. /* Bump the MDS count */
  124. atomic_inc(&server->nfs_client->cl_mds_count);
  125. dprintk("%s: pNFS module for %u set\n", __func__, id);
  126. return;
  127. out_no_driver:
  128. dprintk("%s: Using NFSv4 I/O\n", __func__);
  129. server->pnfs_curr_ld = NULL;
  130. }
  131. int
  132. pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  133. {
  134. int status = -EINVAL;
  135. struct pnfs_layoutdriver_type *tmp;
  136. if (ld_type->id == 0) {
  137. printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
  138. return status;
  139. }
  140. if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
  141. printk(KERN_ERR "NFS: %s Layout driver must provide "
  142. "alloc_lseg and free_lseg.\n", __func__);
  143. return status;
  144. }
  145. spin_lock(&pnfs_spinlock);
  146. tmp = find_pnfs_driver_locked(ld_type->id);
  147. if (!tmp) {
  148. list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
  149. status = 0;
  150. dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
  151. ld_type->name);
  152. } else {
  153. printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
  154. __func__, ld_type->id);
  155. }
  156. spin_unlock(&pnfs_spinlock);
  157. return status;
  158. }
  159. EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
  160. void
  161. pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  162. {
  163. dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
  164. spin_lock(&pnfs_spinlock);
  165. list_del(&ld_type->pnfs_tblid);
  166. spin_unlock(&pnfs_spinlock);
  167. }
  168. EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
  169. /*
  170. * pNFS client layout cache
  171. */
  172. /* Need to hold i_lock if caller does not already hold reference */
  173. void
  174. pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
  175. {
  176. atomic_inc(&lo->plh_refcount);
  177. }
  178. static struct pnfs_layout_hdr *
  179. pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
  180. {
  181. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  182. return ld->alloc_layout_hdr(ino, gfp_flags);
  183. }
  184. static void
  185. pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
  186. {
  187. struct nfs_server *server = NFS_SERVER(lo->plh_inode);
  188. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  189. if (!list_empty(&lo->plh_layouts)) {
  190. struct nfs_client *clp = server->nfs_client;
  191. spin_lock(&clp->cl_lock);
  192. list_del_init(&lo->plh_layouts);
  193. spin_unlock(&clp->cl_lock);
  194. }
  195. put_rpccred(lo->plh_lc_cred);
  196. return ld->free_layout_hdr(lo);
  197. }
  198. static void
  199. pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
  200. {
  201. struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
  202. dprintk("%s: freeing layout cache %p\n", __func__, lo);
  203. nfsi->layout = NULL;
  204. /* Reset MDS Threshold I/O counters */
  205. nfsi->write_io = 0;
  206. nfsi->read_io = 0;
  207. }
  208. void
  209. pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
  210. {
  211. struct inode *inode = lo->plh_inode;
  212. if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
  213. pnfs_detach_layout_hdr(lo);
  214. spin_unlock(&inode->i_lock);
  215. pnfs_free_layout_hdr(lo);
  216. }
  217. }
  218. static int
  219. pnfs_iomode_to_fail_bit(u32 iomode)
  220. {
  221. return iomode == IOMODE_RW ?
  222. NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
  223. }
  224. static void
  225. pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
  226. {
  227. lo->plh_retry_timestamp = jiffies;
  228. if (!test_and_set_bit(fail_bit, &lo->plh_flags))
  229. atomic_inc(&lo->plh_refcount);
  230. }
  231. static void
  232. pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
  233. {
  234. if (test_and_clear_bit(fail_bit, &lo->plh_flags))
  235. atomic_dec(&lo->plh_refcount);
  236. }
  237. static void
  238. pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
  239. {
  240. struct inode *inode = lo->plh_inode;
  241. struct pnfs_layout_range range = {
  242. .iomode = iomode,
  243. .offset = 0,
  244. .length = NFS4_MAX_UINT64,
  245. };
  246. LIST_HEAD(head);
  247. spin_lock(&inode->i_lock);
  248. pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  249. pnfs_mark_matching_lsegs_invalid(lo, &head, &range);
  250. spin_unlock(&inode->i_lock);
  251. pnfs_free_lseg_list(&head);
  252. dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
  253. iomode == IOMODE_RW ? "RW" : "READ");
  254. }
  255. static bool
  256. pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
  257. {
  258. unsigned long start, end;
  259. int fail_bit = pnfs_iomode_to_fail_bit(iomode);
  260. if (test_bit(fail_bit, &lo->plh_flags) == 0)
  261. return false;
  262. end = jiffies;
  263. start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
  264. if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
  265. /* It is time to retry the failed layoutgets */
  266. pnfs_layout_clear_fail_bit(lo, fail_bit);
  267. return false;
  268. }
  269. return true;
  270. }
  271. static void
  272. init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
  273. {
  274. INIT_LIST_HEAD(&lseg->pls_list);
  275. INIT_LIST_HEAD(&lseg->pls_lc_list);
  276. atomic_set(&lseg->pls_refcount, 1);
  277. smp_mb();
  278. set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
  279. lseg->pls_layout = lo;
  280. }
  281. static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
  282. {
  283. struct inode *ino = lseg->pls_layout->plh_inode;
  284. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  285. }
  286. static void
  287. pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
  288. struct pnfs_layout_segment *lseg)
  289. {
  290. struct inode *inode = lo->plh_inode;
  291. WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  292. list_del_init(&lseg->pls_list);
  293. /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
  294. atomic_dec(&lo->plh_refcount);
  295. if (list_empty(&lo->plh_segs))
  296. clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  297. rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
  298. }
  299. void
  300. pnfs_put_lseg(struct pnfs_layout_segment *lseg)
  301. {
  302. struct pnfs_layout_hdr *lo;
  303. struct inode *inode;
  304. if (!lseg)
  305. return;
  306. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  307. atomic_read(&lseg->pls_refcount),
  308. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  309. lo = lseg->pls_layout;
  310. inode = lo->plh_inode;
  311. if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
  312. pnfs_get_layout_hdr(lo);
  313. pnfs_layout_remove_lseg(lo, lseg);
  314. spin_unlock(&inode->i_lock);
  315. pnfs_free_lseg(lseg);
  316. pnfs_put_layout_hdr(lo);
  317. }
  318. }
  319. EXPORT_SYMBOL_GPL(pnfs_put_lseg);
  320. static inline u64
  321. end_offset(u64 start, u64 len)
  322. {
  323. u64 end;
  324. end = start + len;
  325. return end >= start ? end : NFS4_MAX_UINT64;
  326. }
  327. /*
  328. * is l2 fully contained in l1?
  329. * start1 end1
  330. * [----------------------------------)
  331. * start2 end2
  332. * [----------------)
  333. */
  334. static inline int
  335. lo_seg_contained(struct pnfs_layout_range *l1,
  336. struct pnfs_layout_range *l2)
  337. {
  338. u64 start1 = l1->offset;
  339. u64 end1 = end_offset(start1, l1->length);
  340. u64 start2 = l2->offset;
  341. u64 end2 = end_offset(start2, l2->length);
  342. return (start1 <= start2) && (end1 >= end2);
  343. }
  344. /*
  345. * is l1 and l2 intersecting?
  346. * start1 end1
  347. * [----------------------------------)
  348. * start2 end2
  349. * [----------------)
  350. */
  351. static inline int
  352. lo_seg_intersecting(struct pnfs_layout_range *l1,
  353. struct pnfs_layout_range *l2)
  354. {
  355. u64 start1 = l1->offset;
  356. u64 end1 = end_offset(start1, l1->length);
  357. u64 start2 = l2->offset;
  358. u64 end2 = end_offset(start2, l2->length);
  359. return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
  360. (end2 == NFS4_MAX_UINT64 || end2 > start1);
  361. }
  362. static bool
  363. should_free_lseg(struct pnfs_layout_range *lseg_range,
  364. struct pnfs_layout_range *recall_range)
  365. {
  366. return (recall_range->iomode == IOMODE_ANY ||
  367. lseg_range->iomode == recall_range->iomode) &&
  368. lo_seg_intersecting(lseg_range, recall_range);
  369. }
  370. /* Returns 1 if lseg is removed from list, 0 otherwise */
  371. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  372. struct list_head *tmp_list)
  373. {
  374. int rv = 0;
  375. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  376. /* Remove the reference keeping the lseg in the
  377. * list. It will now be removed when all
  378. * outstanding io is finished.
  379. */
  380. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  381. atomic_read(&lseg->pls_refcount));
  382. if (atomic_dec_and_test(&lseg->pls_refcount)) {
  383. pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
  384. list_add(&lseg->pls_list, tmp_list);
  385. rv = 1;
  386. }
  387. }
  388. return rv;
  389. }
  390. /* Returns count of number of matching invalid lsegs remaining in list
  391. * after call.
  392. */
  393. int
  394. pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
  395. struct list_head *tmp_list,
  396. struct pnfs_layout_range *recall_range)
  397. {
  398. struct pnfs_layout_segment *lseg, *next;
  399. int invalid = 0, removed = 0;
  400. dprintk("%s:Begin lo %p\n", __func__, lo);
  401. if (list_empty(&lo->plh_segs))
  402. return 0;
  403. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  404. if (!recall_range ||
  405. should_free_lseg(&lseg->pls_range, recall_range)) {
  406. dprintk("%s: freeing lseg %p iomode %d "
  407. "offset %llu length %llu\n", __func__,
  408. lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
  409. lseg->pls_range.length);
  410. invalid++;
  411. removed += mark_lseg_invalid(lseg, tmp_list);
  412. }
  413. dprintk("%s:Return %i\n", __func__, invalid - removed);
  414. return invalid - removed;
  415. }
  416. /* note free_me must contain lsegs from a single layout_hdr */
  417. void
  418. pnfs_free_lseg_list(struct list_head *free_me)
  419. {
  420. struct pnfs_layout_segment *lseg, *tmp;
  421. if (list_empty(free_me))
  422. return;
  423. list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
  424. list_del(&lseg->pls_list);
  425. pnfs_free_lseg(lseg);
  426. }
  427. }
  428. void
  429. pnfs_destroy_layout(struct nfs_inode *nfsi)
  430. {
  431. struct pnfs_layout_hdr *lo;
  432. LIST_HEAD(tmp_list);
  433. spin_lock(&nfsi->vfs_inode.i_lock);
  434. lo = nfsi->layout;
  435. if (lo) {
  436. lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
  437. pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
  438. pnfs_get_layout_hdr(lo);
  439. pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
  440. pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
  441. spin_unlock(&nfsi->vfs_inode.i_lock);
  442. pnfs_free_lseg_list(&tmp_list);
  443. pnfs_put_layout_hdr(lo);
  444. } else
  445. spin_unlock(&nfsi->vfs_inode.i_lock);
  446. }
  447. EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
  448. static bool
  449. pnfs_layout_add_bulk_destroy_list(struct inode *inode,
  450. struct list_head *layout_list)
  451. {
  452. struct pnfs_layout_hdr *lo;
  453. bool ret = false;
  454. spin_lock(&inode->i_lock);
  455. lo = NFS_I(inode)->layout;
  456. if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
  457. pnfs_get_layout_hdr(lo);
  458. list_add(&lo->plh_bulk_destroy, layout_list);
  459. ret = true;
  460. }
  461. spin_unlock(&inode->i_lock);
  462. return ret;
  463. }
  464. /* Caller must hold rcu_read_lock and clp->cl_lock */
  465. static int
  466. pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
  467. struct nfs_server *server,
  468. struct list_head *layout_list)
  469. {
  470. struct pnfs_layout_hdr *lo, *next;
  471. struct inode *inode;
  472. list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
  473. inode = igrab(lo->plh_inode);
  474. if (inode == NULL)
  475. continue;
  476. list_del_init(&lo->plh_layouts);
  477. if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
  478. continue;
  479. rcu_read_unlock();
  480. spin_unlock(&clp->cl_lock);
  481. iput(inode);
  482. spin_lock(&clp->cl_lock);
  483. rcu_read_lock();
  484. return -EAGAIN;
  485. }
  486. return 0;
  487. }
  488. static int
  489. pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
  490. bool is_bulk_recall)
  491. {
  492. struct pnfs_layout_hdr *lo;
  493. struct inode *inode;
  494. struct pnfs_layout_range range = {
  495. .iomode = IOMODE_ANY,
  496. .offset = 0,
  497. .length = NFS4_MAX_UINT64,
  498. };
  499. LIST_HEAD(lseg_list);
  500. int ret = 0;
  501. while (!list_empty(layout_list)) {
  502. lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
  503. plh_bulk_destroy);
  504. dprintk("%s freeing layout for inode %lu\n", __func__,
  505. lo->plh_inode->i_ino);
  506. inode = lo->plh_inode;
  507. spin_lock(&inode->i_lock);
  508. list_del_init(&lo->plh_bulk_destroy);
  509. lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
  510. if (is_bulk_recall)
  511. set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  512. if (pnfs_mark_matching_lsegs_invalid(lo, &lseg_list, &range))
  513. ret = -EAGAIN;
  514. spin_unlock(&inode->i_lock);
  515. pnfs_free_lseg_list(&lseg_list);
  516. pnfs_put_layout_hdr(lo);
  517. iput(inode);
  518. }
  519. return ret;
  520. }
  521. int
  522. pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
  523. struct nfs_fsid *fsid,
  524. bool is_recall)
  525. {
  526. struct nfs_server *server;
  527. LIST_HEAD(layout_list);
  528. spin_lock(&clp->cl_lock);
  529. rcu_read_lock();
  530. restart:
  531. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  532. if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
  533. continue;
  534. if (pnfs_layout_bulk_destroy_byserver_locked(clp,
  535. server,
  536. &layout_list) != 0)
  537. goto restart;
  538. }
  539. rcu_read_unlock();
  540. spin_unlock(&clp->cl_lock);
  541. if (list_empty(&layout_list))
  542. return 0;
  543. return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
  544. }
  545. int
  546. pnfs_destroy_layouts_byclid(struct nfs_client *clp,
  547. bool is_recall)
  548. {
  549. struct nfs_server *server;
  550. LIST_HEAD(layout_list);
  551. spin_lock(&clp->cl_lock);
  552. rcu_read_lock();
  553. restart:
  554. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  555. if (pnfs_layout_bulk_destroy_byserver_locked(clp,
  556. server,
  557. &layout_list) != 0)
  558. goto restart;
  559. }
  560. rcu_read_unlock();
  561. spin_unlock(&clp->cl_lock);
  562. if (list_empty(&layout_list))
  563. return 0;
  564. return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
  565. }
  566. /*
  567. * Called by the state manger to remove all layouts established under an
  568. * expired lease.
  569. */
  570. void
  571. pnfs_destroy_all_layouts(struct nfs_client *clp)
  572. {
  573. nfs4_deviceid_mark_client_invalid(clp);
  574. nfs4_deviceid_purge_client(clp);
  575. pnfs_destroy_layouts_byclid(clp, false);
  576. }
  577. /*
  578. * Compare 2 layout stateid sequence ids, to see which is newer,
  579. * taking into account wraparound issues.
  580. */
  581. static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
  582. {
  583. return (s32)s1 - (s32)s2 > 0;
  584. }
  585. /* update lo->plh_stateid with new if is more recent */
  586. void
  587. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  588. bool update_barrier)
  589. {
  590. u32 oldseq, newseq, new_barrier;
  591. int empty = list_empty(&lo->plh_segs);
  592. oldseq = be32_to_cpu(lo->plh_stateid.seqid);
  593. newseq = be32_to_cpu(new->seqid);
  594. if (empty || pnfs_seqid_is_newer(newseq, oldseq)) {
  595. nfs4_stateid_copy(&lo->plh_stateid, new);
  596. if (update_barrier) {
  597. new_barrier = be32_to_cpu(new->seqid);
  598. } else {
  599. /* Because of wraparound, we want to keep the barrier
  600. * "close" to the current seqids.
  601. */
  602. new_barrier = newseq - atomic_read(&lo->plh_outstanding);
  603. }
  604. if (empty || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
  605. lo->plh_barrier = new_barrier;
  606. }
  607. }
  608. static bool
  609. pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
  610. const nfs4_stateid *stateid)
  611. {
  612. u32 seqid = be32_to_cpu(stateid->seqid);
  613. return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
  614. }
  615. /* lget is set to 1 if called from inside send_layoutget call chain */
  616. static bool
  617. pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo, int lget)
  618. {
  619. return lo->plh_block_lgets ||
  620. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  621. (list_empty(&lo->plh_segs) &&
  622. (atomic_read(&lo->plh_outstanding) > lget));
  623. }
  624. int
  625. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  626. struct nfs4_state *open_state)
  627. {
  628. int status = 0;
  629. dprintk("--> %s\n", __func__);
  630. spin_lock(&lo->plh_inode->i_lock);
  631. if (pnfs_layoutgets_blocked(lo, 1)) {
  632. status = -EAGAIN;
  633. } else if (list_empty(&lo->plh_segs)) {
  634. int seq;
  635. do {
  636. seq = read_seqbegin(&open_state->seqlock);
  637. nfs4_stateid_copy(dst, &open_state->stateid);
  638. } while (read_seqretry(&open_state->seqlock, seq));
  639. } else
  640. nfs4_stateid_copy(dst, &lo->plh_stateid);
  641. spin_unlock(&lo->plh_inode->i_lock);
  642. dprintk("<-- %s\n", __func__);
  643. return status;
  644. }
  645. /*
  646. * Get layout from server.
  647. * for now, assume that whole file layouts are requested.
  648. * arg->offset: 0
  649. * arg->length: all ones
  650. */
  651. static struct pnfs_layout_segment *
  652. send_layoutget(struct pnfs_layout_hdr *lo,
  653. struct nfs_open_context *ctx,
  654. struct pnfs_layout_range *range,
  655. gfp_t gfp_flags)
  656. {
  657. struct inode *ino = lo->plh_inode;
  658. struct nfs_server *server = NFS_SERVER(ino);
  659. struct nfs4_layoutget *lgp;
  660. struct pnfs_layout_segment *lseg;
  661. dprintk("--> %s\n", __func__);
  662. lgp = kzalloc(sizeof(*lgp), gfp_flags);
  663. if (lgp == NULL)
  664. return NULL;
  665. lgp->args.minlength = PAGE_CACHE_SIZE;
  666. if (lgp->args.minlength > range->length)
  667. lgp->args.minlength = range->length;
  668. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  669. lgp->args.range = *range;
  670. lgp->args.type = server->pnfs_curr_ld->id;
  671. lgp->args.inode = ino;
  672. lgp->args.ctx = get_nfs_open_context(ctx);
  673. lgp->gfp_flags = gfp_flags;
  674. /* Synchronously retrieve layout information from server and
  675. * store in lseg.
  676. */
  677. lseg = nfs4_proc_layoutget(lgp, gfp_flags);
  678. if (IS_ERR(lseg)) {
  679. switch (PTR_ERR(lseg)) {
  680. case -ENOMEM:
  681. case -ERESTARTSYS:
  682. break;
  683. default:
  684. /* remember that LAYOUTGET failed and suspend trying */
  685. pnfs_layout_io_set_failed(lo, range->iomode);
  686. }
  687. return NULL;
  688. }
  689. return lseg;
  690. }
  691. /*
  692. * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
  693. * when the layout segment list is empty.
  694. *
  695. * Note that a pnfs_layout_hdr can exist with an empty layout segment
  696. * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
  697. * deviceid is marked invalid.
  698. */
  699. int
  700. _pnfs_return_layout(struct inode *ino)
  701. {
  702. struct pnfs_layout_hdr *lo = NULL;
  703. struct nfs_inode *nfsi = NFS_I(ino);
  704. LIST_HEAD(tmp_list);
  705. struct nfs4_layoutreturn *lrp;
  706. nfs4_stateid stateid;
  707. int status = 0, empty;
  708. dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
  709. spin_lock(&ino->i_lock);
  710. lo = nfsi->layout;
  711. if (!lo) {
  712. spin_unlock(&ino->i_lock);
  713. dprintk("NFS: %s no layout to return\n", __func__);
  714. goto out;
  715. }
  716. stateid = nfsi->layout->plh_stateid;
  717. /* Reference matched in nfs4_layoutreturn_release */
  718. pnfs_get_layout_hdr(lo);
  719. empty = list_empty(&lo->plh_segs);
  720. pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
  721. /* Don't send a LAYOUTRETURN if list was initially empty */
  722. if (empty) {
  723. spin_unlock(&ino->i_lock);
  724. pnfs_put_layout_hdr(lo);
  725. dprintk("NFS: %s no layout segments to return\n", __func__);
  726. goto out;
  727. }
  728. lo->plh_block_lgets++;
  729. spin_unlock(&ino->i_lock);
  730. pnfs_free_lseg_list(&tmp_list);
  731. WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
  732. lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
  733. if (unlikely(lrp == NULL)) {
  734. status = -ENOMEM;
  735. spin_lock(&ino->i_lock);
  736. lo->plh_block_lgets--;
  737. spin_unlock(&ino->i_lock);
  738. pnfs_put_layout_hdr(lo);
  739. goto out;
  740. }
  741. lrp->args.stateid = stateid;
  742. lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
  743. lrp->args.inode = ino;
  744. lrp->args.layout = lo;
  745. lrp->clp = NFS_SERVER(ino)->nfs_client;
  746. status = nfs4_proc_layoutreturn(lrp);
  747. out:
  748. dprintk("<-- %s status: %d\n", __func__, status);
  749. return status;
  750. }
  751. EXPORT_SYMBOL_GPL(_pnfs_return_layout);
  752. bool pnfs_roc(struct inode *ino)
  753. {
  754. struct pnfs_layout_hdr *lo;
  755. struct pnfs_layout_segment *lseg, *tmp;
  756. LIST_HEAD(tmp_list);
  757. bool found = false;
  758. spin_lock(&ino->i_lock);
  759. lo = NFS_I(ino)->layout;
  760. if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
  761. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
  762. goto out_nolayout;
  763. list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
  764. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  765. mark_lseg_invalid(lseg, &tmp_list);
  766. found = true;
  767. }
  768. if (!found)
  769. goto out_nolayout;
  770. lo->plh_block_lgets++;
  771. pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
  772. spin_unlock(&ino->i_lock);
  773. pnfs_free_lseg_list(&tmp_list);
  774. return true;
  775. out_nolayout:
  776. spin_unlock(&ino->i_lock);
  777. return false;
  778. }
  779. void pnfs_roc_release(struct inode *ino)
  780. {
  781. struct pnfs_layout_hdr *lo;
  782. spin_lock(&ino->i_lock);
  783. lo = NFS_I(ino)->layout;
  784. lo->plh_block_lgets--;
  785. if (atomic_dec_and_test(&lo->plh_refcount)) {
  786. pnfs_detach_layout_hdr(lo);
  787. spin_unlock(&ino->i_lock);
  788. pnfs_free_layout_hdr(lo);
  789. } else
  790. spin_unlock(&ino->i_lock);
  791. }
  792. void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
  793. {
  794. struct pnfs_layout_hdr *lo;
  795. spin_lock(&ino->i_lock);
  796. lo = NFS_I(ino)->layout;
  797. if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
  798. lo->plh_barrier = barrier;
  799. spin_unlock(&ino->i_lock);
  800. }
  801. bool pnfs_roc_drain(struct inode *ino, u32 *barrier, struct rpc_task *task)
  802. {
  803. struct nfs_inode *nfsi = NFS_I(ino);
  804. struct pnfs_layout_hdr *lo;
  805. struct pnfs_layout_segment *lseg;
  806. u32 current_seqid;
  807. bool found = false;
  808. spin_lock(&ino->i_lock);
  809. list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
  810. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  811. rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
  812. found = true;
  813. goto out;
  814. }
  815. lo = nfsi->layout;
  816. current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
  817. /* Since close does not return a layout stateid for use as
  818. * a barrier, we choose the worst-case barrier.
  819. */
  820. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  821. out:
  822. spin_unlock(&ino->i_lock);
  823. return found;
  824. }
  825. /*
  826. * Compare two layout segments for sorting into layout cache.
  827. * We want to preferentially return RW over RO layouts, so ensure those
  828. * are seen first.
  829. */
  830. static s64
  831. cmp_layout(struct pnfs_layout_range *l1,
  832. struct pnfs_layout_range *l2)
  833. {
  834. s64 d;
  835. /* high offset > low offset */
  836. d = l1->offset - l2->offset;
  837. if (d)
  838. return d;
  839. /* short length > long length */
  840. d = l2->length - l1->length;
  841. if (d)
  842. return d;
  843. /* read > read/write */
  844. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  845. }
  846. static void
  847. pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  848. struct pnfs_layout_segment *lseg)
  849. {
  850. struct pnfs_layout_segment *lp;
  851. dprintk("%s:Begin\n", __func__);
  852. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  853. if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
  854. continue;
  855. list_add_tail(&lseg->pls_list, &lp->pls_list);
  856. dprintk("%s: inserted lseg %p "
  857. "iomode %d offset %llu length %llu before "
  858. "lp %p iomode %d offset %llu length %llu\n",
  859. __func__, lseg, lseg->pls_range.iomode,
  860. lseg->pls_range.offset, lseg->pls_range.length,
  861. lp, lp->pls_range.iomode, lp->pls_range.offset,
  862. lp->pls_range.length);
  863. goto out;
  864. }
  865. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  866. dprintk("%s: inserted lseg %p "
  867. "iomode %d offset %llu length %llu at tail\n",
  868. __func__, lseg, lseg->pls_range.iomode,
  869. lseg->pls_range.offset, lseg->pls_range.length);
  870. out:
  871. pnfs_get_layout_hdr(lo);
  872. dprintk("%s:Return\n", __func__);
  873. }
  874. static struct pnfs_layout_hdr *
  875. alloc_init_layout_hdr(struct inode *ino,
  876. struct nfs_open_context *ctx,
  877. gfp_t gfp_flags)
  878. {
  879. struct pnfs_layout_hdr *lo;
  880. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  881. if (!lo)
  882. return NULL;
  883. atomic_set(&lo->plh_refcount, 1);
  884. INIT_LIST_HEAD(&lo->plh_layouts);
  885. INIT_LIST_HEAD(&lo->plh_segs);
  886. INIT_LIST_HEAD(&lo->plh_bulk_destroy);
  887. lo->plh_inode = ino;
  888. lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
  889. return lo;
  890. }
  891. static struct pnfs_layout_hdr *
  892. pnfs_find_alloc_layout(struct inode *ino,
  893. struct nfs_open_context *ctx,
  894. gfp_t gfp_flags)
  895. {
  896. struct nfs_inode *nfsi = NFS_I(ino);
  897. struct pnfs_layout_hdr *new = NULL;
  898. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  899. if (nfsi->layout != NULL)
  900. goto out_existing;
  901. spin_unlock(&ino->i_lock);
  902. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  903. spin_lock(&ino->i_lock);
  904. if (likely(nfsi->layout == NULL)) { /* Won the race? */
  905. nfsi->layout = new;
  906. return new;
  907. } else if (new != NULL)
  908. pnfs_free_layout_hdr(new);
  909. out_existing:
  910. pnfs_get_layout_hdr(nfsi->layout);
  911. return nfsi->layout;
  912. }
  913. /*
  914. * iomode matching rules:
  915. * iomode lseg match
  916. * ----- ----- -----
  917. * ANY READ true
  918. * ANY RW true
  919. * RW READ false
  920. * RW RW true
  921. * READ READ true
  922. * READ RW true
  923. */
  924. static int
  925. is_matching_lseg(struct pnfs_layout_range *ls_range,
  926. struct pnfs_layout_range *range)
  927. {
  928. struct pnfs_layout_range range1;
  929. if ((range->iomode == IOMODE_RW &&
  930. ls_range->iomode != IOMODE_RW) ||
  931. !lo_seg_intersecting(ls_range, range))
  932. return 0;
  933. /* range1 covers only the first byte in the range */
  934. range1 = *range;
  935. range1.length = 1;
  936. return lo_seg_contained(ls_range, &range1);
  937. }
  938. /*
  939. * lookup range in layout
  940. */
  941. static struct pnfs_layout_segment *
  942. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  943. struct pnfs_layout_range *range)
  944. {
  945. struct pnfs_layout_segment *lseg, *ret = NULL;
  946. dprintk("%s:Begin\n", __func__);
  947. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  948. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  949. is_matching_lseg(&lseg->pls_range, range)) {
  950. ret = pnfs_get_lseg(lseg);
  951. break;
  952. }
  953. if (lseg->pls_range.offset > range->offset)
  954. break;
  955. }
  956. dprintk("%s:Return lseg %p ref %d\n",
  957. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  958. return ret;
  959. }
  960. /*
  961. * Use mdsthreshold hints set at each OPEN to determine if I/O should go
  962. * to the MDS or over pNFS
  963. *
  964. * The nfs_inode read_io and write_io fields are cumulative counters reset
  965. * when there are no layout segments. Note that in pnfs_update_layout iomode
  966. * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
  967. * WRITE request.
  968. *
  969. * A return of true means use MDS I/O.
  970. *
  971. * From rfc 5661:
  972. * If a file's size is smaller than the file size threshold, data accesses
  973. * SHOULD be sent to the metadata server. If an I/O request has a length that
  974. * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
  975. * server. If both file size and I/O size are provided, the client SHOULD
  976. * reach or exceed both thresholds before sending its read or write
  977. * requests to the data server.
  978. */
  979. static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
  980. struct inode *ino, int iomode)
  981. {
  982. struct nfs4_threshold *t = ctx->mdsthreshold;
  983. struct nfs_inode *nfsi = NFS_I(ino);
  984. loff_t fsize = i_size_read(ino);
  985. bool size = false, size_set = false, io = false, io_set = false, ret = false;
  986. if (t == NULL)
  987. return ret;
  988. dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
  989. __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
  990. switch (iomode) {
  991. case IOMODE_READ:
  992. if (t->bm & THRESHOLD_RD) {
  993. dprintk("%s fsize %llu\n", __func__, fsize);
  994. size_set = true;
  995. if (fsize < t->rd_sz)
  996. size = true;
  997. }
  998. if (t->bm & THRESHOLD_RD_IO) {
  999. dprintk("%s nfsi->read_io %llu\n", __func__,
  1000. nfsi->read_io);
  1001. io_set = true;
  1002. if (nfsi->read_io < t->rd_io_sz)
  1003. io = true;
  1004. }
  1005. break;
  1006. case IOMODE_RW:
  1007. if (t->bm & THRESHOLD_WR) {
  1008. dprintk("%s fsize %llu\n", __func__, fsize);
  1009. size_set = true;
  1010. if (fsize < t->wr_sz)
  1011. size = true;
  1012. }
  1013. if (t->bm & THRESHOLD_WR_IO) {
  1014. dprintk("%s nfsi->write_io %llu\n", __func__,
  1015. nfsi->write_io);
  1016. io_set = true;
  1017. if (nfsi->write_io < t->wr_io_sz)
  1018. io = true;
  1019. }
  1020. break;
  1021. }
  1022. if (size_set && io_set) {
  1023. if (size && io)
  1024. ret = true;
  1025. } else if (size || io)
  1026. ret = true;
  1027. dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
  1028. return ret;
  1029. }
  1030. /*
  1031. * Layout segment is retreived from the server if not cached.
  1032. * The appropriate layout segment is referenced and returned to the caller.
  1033. */
  1034. struct pnfs_layout_segment *
  1035. pnfs_update_layout(struct inode *ino,
  1036. struct nfs_open_context *ctx,
  1037. loff_t pos,
  1038. u64 count,
  1039. enum pnfs_iomode iomode,
  1040. gfp_t gfp_flags)
  1041. {
  1042. struct pnfs_layout_range arg = {
  1043. .iomode = iomode,
  1044. .offset = pos,
  1045. .length = count,
  1046. };
  1047. unsigned pg_offset;
  1048. struct nfs_server *server = NFS_SERVER(ino);
  1049. struct nfs_client *clp = server->nfs_client;
  1050. struct pnfs_layout_hdr *lo;
  1051. struct pnfs_layout_segment *lseg = NULL;
  1052. bool first;
  1053. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  1054. goto out;
  1055. if (pnfs_within_mdsthreshold(ctx, ino, iomode))
  1056. goto out;
  1057. spin_lock(&ino->i_lock);
  1058. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  1059. if (lo == NULL) {
  1060. spin_unlock(&ino->i_lock);
  1061. goto out;
  1062. }
  1063. /* Do we even need to bother with this? */
  1064. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1065. dprintk("%s matches recall, use MDS\n", __func__);
  1066. goto out_unlock;
  1067. }
  1068. /* if LAYOUTGET already failed once we don't try again */
  1069. if (pnfs_layout_io_test_failed(lo, iomode))
  1070. goto out_unlock;
  1071. /* Check to see if the layout for the given range already exists */
  1072. lseg = pnfs_find_lseg(lo, &arg);
  1073. if (lseg)
  1074. goto out_unlock;
  1075. if (pnfs_layoutgets_blocked(lo, 0))
  1076. goto out_unlock;
  1077. atomic_inc(&lo->plh_outstanding);
  1078. first = list_empty(&lo->plh_layouts) ? true : false;
  1079. spin_unlock(&ino->i_lock);
  1080. if (first) {
  1081. /* The lo must be on the clp list if there is any
  1082. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  1083. */
  1084. spin_lock(&clp->cl_lock);
  1085. list_add_tail(&lo->plh_layouts, &server->layouts);
  1086. spin_unlock(&clp->cl_lock);
  1087. }
  1088. pg_offset = arg.offset & ~PAGE_CACHE_MASK;
  1089. if (pg_offset) {
  1090. arg.offset -= pg_offset;
  1091. arg.length += pg_offset;
  1092. }
  1093. if (arg.length != NFS4_MAX_UINT64)
  1094. arg.length = PAGE_CACHE_ALIGN(arg.length);
  1095. lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
  1096. atomic_dec(&lo->plh_outstanding);
  1097. out_put_layout_hdr:
  1098. pnfs_put_layout_hdr(lo);
  1099. out:
  1100. dprintk("%s: inode %s/%llu pNFS layout segment %s for "
  1101. "(%s, offset: %llu, length: %llu)\n",
  1102. __func__, ino->i_sb->s_id,
  1103. (unsigned long long)NFS_FILEID(ino),
  1104. lseg == NULL ? "not found" : "found",
  1105. iomode==IOMODE_RW ? "read/write" : "read-only",
  1106. (unsigned long long)pos,
  1107. (unsigned long long)count);
  1108. return lseg;
  1109. out_unlock:
  1110. spin_unlock(&ino->i_lock);
  1111. goto out_put_layout_hdr;
  1112. }
  1113. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  1114. struct pnfs_layout_segment *
  1115. pnfs_layout_process(struct nfs4_layoutget *lgp)
  1116. {
  1117. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  1118. struct nfs4_layoutget_res *res = &lgp->res;
  1119. struct pnfs_layout_segment *lseg;
  1120. struct inode *ino = lo->plh_inode;
  1121. int status = 0;
  1122. /* Inject layout blob into I/O device driver */
  1123. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  1124. if (!lseg || IS_ERR(lseg)) {
  1125. if (!lseg)
  1126. status = -ENOMEM;
  1127. else
  1128. status = PTR_ERR(lseg);
  1129. dprintk("%s: Could not allocate layout: error %d\n",
  1130. __func__, status);
  1131. goto out;
  1132. }
  1133. spin_lock(&ino->i_lock);
  1134. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1135. dprintk("%s forget reply due to recall\n", __func__);
  1136. goto out_forget_reply;
  1137. }
  1138. if (pnfs_layoutgets_blocked(lo, 1) ||
  1139. pnfs_layout_stateid_blocked(lo, &res->stateid)) {
  1140. dprintk("%s forget reply due to state\n", __func__);
  1141. goto out_forget_reply;
  1142. }
  1143. /* Done processing layoutget. Set the layout stateid */
  1144. pnfs_set_layout_stateid(lo, &res->stateid, false);
  1145. init_lseg(lo, lseg);
  1146. lseg->pls_range = res->range;
  1147. pnfs_get_lseg(lseg);
  1148. pnfs_layout_insert_lseg(lo, lseg);
  1149. if (res->return_on_close) {
  1150. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  1151. set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
  1152. }
  1153. spin_unlock(&ino->i_lock);
  1154. return lseg;
  1155. out:
  1156. return ERR_PTR(status);
  1157. out_forget_reply:
  1158. spin_unlock(&ino->i_lock);
  1159. lseg->pls_layout = lo;
  1160. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  1161. goto out;
  1162. }
  1163. void
  1164. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  1165. {
  1166. u64 rd_size = req->wb_bytes;
  1167. WARN_ON_ONCE(pgio->pg_lseg != NULL);
  1168. if (req->wb_offset != req->wb_pgbase) {
  1169. nfs_pageio_reset_read_mds(pgio);
  1170. return;
  1171. }
  1172. if (pgio->pg_dreq == NULL)
  1173. rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
  1174. else
  1175. rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
  1176. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  1177. req->wb_context,
  1178. req_offset(req),
  1179. rd_size,
  1180. IOMODE_READ,
  1181. GFP_KERNEL);
  1182. /* If no lseg, fall back to read through mds */
  1183. if (pgio->pg_lseg == NULL)
  1184. nfs_pageio_reset_read_mds(pgio);
  1185. }
  1186. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  1187. void
  1188. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
  1189. struct nfs_page *req, u64 wb_size)
  1190. {
  1191. WARN_ON_ONCE(pgio->pg_lseg != NULL);
  1192. if (req->wb_offset != req->wb_pgbase) {
  1193. nfs_pageio_reset_write_mds(pgio);
  1194. return;
  1195. }
  1196. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  1197. req->wb_context,
  1198. req_offset(req),
  1199. wb_size,
  1200. IOMODE_RW,
  1201. GFP_NOFS);
  1202. /* If no lseg, fall back to write through mds */
  1203. if (pgio->pg_lseg == NULL)
  1204. nfs_pageio_reset_write_mds(pgio);
  1205. }
  1206. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  1207. void
  1208. pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode,
  1209. const struct nfs_pgio_completion_ops *compl_ops)
  1210. {
  1211. struct nfs_server *server = NFS_SERVER(inode);
  1212. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  1213. if (ld == NULL)
  1214. nfs_pageio_init_read(pgio, inode, compl_ops);
  1215. else
  1216. nfs_pageio_init(pgio, inode, ld->pg_read_ops, compl_ops, server->rsize, 0);
  1217. }
  1218. void
  1219. pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode,
  1220. int ioflags,
  1221. const struct nfs_pgio_completion_ops *compl_ops)
  1222. {
  1223. struct nfs_server *server = NFS_SERVER(inode);
  1224. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  1225. if (ld == NULL)
  1226. nfs_pageio_init_write(pgio, inode, ioflags, compl_ops);
  1227. else
  1228. nfs_pageio_init(pgio, inode, ld->pg_write_ops, compl_ops, server->wsize, ioflags);
  1229. }
  1230. bool
  1231. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  1232. struct nfs_page *req)
  1233. {
  1234. if (pgio->pg_lseg == NULL)
  1235. return nfs_generic_pg_test(pgio, prev, req);
  1236. /*
  1237. * Test if a nfs_page is fully contained in the pnfs_layout_range.
  1238. * Note that this test makes several assumptions:
  1239. * - that the previous nfs_page in the struct nfs_pageio_descriptor
  1240. * is known to lie within the range.
  1241. * - that the nfs_page being tested is known to be contiguous with the
  1242. * previous nfs_page.
  1243. * - Layout ranges are page aligned, so we only have to test the
  1244. * start offset of the request.
  1245. *
  1246. * Please also note that 'end_offset' is actually the offset of the
  1247. * first byte that lies outside the pnfs_layout_range. FIXME?
  1248. *
  1249. */
  1250. return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
  1251. pgio->pg_lseg->pls_range.length);
  1252. }
  1253. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  1254. int pnfs_write_done_resend_to_mds(struct inode *inode,
  1255. struct list_head *head,
  1256. const struct nfs_pgio_completion_ops *compl_ops,
  1257. struct nfs_direct_req *dreq)
  1258. {
  1259. struct nfs_pageio_descriptor pgio;
  1260. LIST_HEAD(failed);
  1261. /* Resend all requests through the MDS */
  1262. nfs_pageio_init_write(&pgio, inode, FLUSH_STABLE, compl_ops);
  1263. pgio.pg_dreq = dreq;
  1264. while (!list_empty(head)) {
  1265. struct nfs_page *req = nfs_list_entry(head->next);
  1266. nfs_list_remove_request(req);
  1267. if (!nfs_pageio_add_request(&pgio, req))
  1268. nfs_list_add_request(req, &failed);
  1269. }
  1270. nfs_pageio_complete(&pgio);
  1271. if (!list_empty(&failed)) {
  1272. /* For some reason our attempt to resend pages. Mark the
  1273. * overall send request as having failed, and let
  1274. * nfs_writeback_release_full deal with the error.
  1275. */
  1276. list_move(&failed, head);
  1277. return -EIO;
  1278. }
  1279. return 0;
  1280. }
  1281. EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
  1282. static void pnfs_ld_handle_write_error(struct nfs_write_data *data)
  1283. {
  1284. struct nfs_pgio_header *hdr = data->header;
  1285. dprintk("pnfs write error = %d\n", hdr->pnfs_error);
  1286. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1287. PNFS_LAYOUTRET_ON_ERROR) {
  1288. clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
  1289. pnfs_return_layout(hdr->inode);
  1290. }
  1291. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1292. data->task.tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
  1293. &hdr->pages,
  1294. hdr->completion_ops,
  1295. hdr->dreq);
  1296. }
  1297. /*
  1298. * Called by non rpc-based layout drivers
  1299. */
  1300. void pnfs_ld_write_done(struct nfs_write_data *data)
  1301. {
  1302. struct nfs_pgio_header *hdr = data->header;
  1303. if (!hdr->pnfs_error) {
  1304. pnfs_set_layoutcommit(data);
  1305. hdr->mds_ops->rpc_call_done(&data->task, data);
  1306. } else
  1307. pnfs_ld_handle_write_error(data);
  1308. hdr->mds_ops->rpc_release(data);
  1309. }
  1310. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  1311. static void
  1312. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  1313. struct nfs_write_data *data)
  1314. {
  1315. struct nfs_pgio_header *hdr = data->header;
  1316. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1317. list_splice_tail_init(&hdr->pages, &desc->pg_list);
  1318. nfs_pageio_reset_write_mds(desc);
  1319. desc->pg_recoalesce = 1;
  1320. }
  1321. nfs_writedata_release(data);
  1322. }
  1323. static enum pnfs_try_status
  1324. pnfs_try_to_write_data(struct nfs_write_data *wdata,
  1325. const struct rpc_call_ops *call_ops,
  1326. struct pnfs_layout_segment *lseg,
  1327. int how)
  1328. {
  1329. struct nfs_pgio_header *hdr = wdata->header;
  1330. struct inode *inode = hdr->inode;
  1331. enum pnfs_try_status trypnfs;
  1332. struct nfs_server *nfss = NFS_SERVER(inode);
  1333. hdr->mds_ops = call_ops;
  1334. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  1335. inode->i_ino, wdata->args.count, wdata->args.offset, how);
  1336. trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
  1337. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1338. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  1339. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1340. return trypnfs;
  1341. }
  1342. static void
  1343. pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
  1344. {
  1345. struct nfs_write_data *data;
  1346. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1347. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1348. desc->pg_lseg = NULL;
  1349. while (!list_empty(head)) {
  1350. enum pnfs_try_status trypnfs;
  1351. data = list_first_entry(head, struct nfs_write_data, list);
  1352. list_del_init(&data->list);
  1353. trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
  1354. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1355. pnfs_write_through_mds(desc, data);
  1356. }
  1357. pnfs_put_lseg(lseg);
  1358. }
  1359. static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
  1360. {
  1361. pnfs_put_lseg(hdr->lseg);
  1362. nfs_writehdr_free(hdr);
  1363. }
  1364. EXPORT_SYMBOL_GPL(pnfs_writehdr_free);
  1365. int
  1366. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  1367. {
  1368. struct nfs_write_header *whdr;
  1369. struct nfs_pgio_header *hdr;
  1370. int ret;
  1371. whdr = nfs_writehdr_alloc();
  1372. if (!whdr) {
  1373. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  1374. pnfs_put_lseg(desc->pg_lseg);
  1375. desc->pg_lseg = NULL;
  1376. return -ENOMEM;
  1377. }
  1378. hdr = &whdr->header;
  1379. nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
  1380. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  1381. atomic_inc(&hdr->refcnt);
  1382. ret = nfs_generic_flush(desc, hdr);
  1383. if (ret != 0) {
  1384. pnfs_put_lseg(desc->pg_lseg);
  1385. desc->pg_lseg = NULL;
  1386. } else
  1387. pnfs_do_multiple_writes(desc, &hdr->rpc_list, desc->pg_ioflags);
  1388. if (atomic_dec_and_test(&hdr->refcnt))
  1389. hdr->completion_ops->completion(hdr);
  1390. return ret;
  1391. }
  1392. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  1393. int pnfs_read_done_resend_to_mds(struct inode *inode,
  1394. struct list_head *head,
  1395. const struct nfs_pgio_completion_ops *compl_ops,
  1396. struct nfs_direct_req *dreq)
  1397. {
  1398. struct nfs_pageio_descriptor pgio;
  1399. LIST_HEAD(failed);
  1400. /* Resend all requests through the MDS */
  1401. nfs_pageio_init_read(&pgio, inode, compl_ops);
  1402. pgio.pg_dreq = dreq;
  1403. while (!list_empty(head)) {
  1404. struct nfs_page *req = nfs_list_entry(head->next);
  1405. nfs_list_remove_request(req);
  1406. if (!nfs_pageio_add_request(&pgio, req))
  1407. nfs_list_add_request(req, &failed);
  1408. }
  1409. nfs_pageio_complete(&pgio);
  1410. if (!list_empty(&failed)) {
  1411. list_move(&failed, head);
  1412. return -EIO;
  1413. }
  1414. return 0;
  1415. }
  1416. EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
  1417. static void pnfs_ld_handle_read_error(struct nfs_read_data *data)
  1418. {
  1419. struct nfs_pgio_header *hdr = data->header;
  1420. dprintk("pnfs read error = %d\n", hdr->pnfs_error);
  1421. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1422. PNFS_LAYOUTRET_ON_ERROR) {
  1423. clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
  1424. pnfs_return_layout(hdr->inode);
  1425. }
  1426. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1427. data->task.tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
  1428. &hdr->pages,
  1429. hdr->completion_ops,
  1430. hdr->dreq);
  1431. }
  1432. /*
  1433. * Called by non rpc-based layout drivers
  1434. */
  1435. void pnfs_ld_read_done(struct nfs_read_data *data)
  1436. {
  1437. struct nfs_pgio_header *hdr = data->header;
  1438. if (likely(!hdr->pnfs_error)) {
  1439. __nfs4_read_done_cb(data);
  1440. hdr->mds_ops->rpc_call_done(&data->task, data);
  1441. } else
  1442. pnfs_ld_handle_read_error(data);
  1443. hdr->mds_ops->rpc_release(data);
  1444. }
  1445. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  1446. static void
  1447. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  1448. struct nfs_read_data *data)
  1449. {
  1450. struct nfs_pgio_header *hdr = data->header;
  1451. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1452. list_splice_tail_init(&hdr->pages, &desc->pg_list);
  1453. nfs_pageio_reset_read_mds(desc);
  1454. desc->pg_recoalesce = 1;
  1455. }
  1456. nfs_readdata_release(data);
  1457. }
  1458. /*
  1459. * Call the appropriate parallel I/O subsystem read function.
  1460. */
  1461. static enum pnfs_try_status
  1462. pnfs_try_to_read_data(struct nfs_read_data *rdata,
  1463. const struct rpc_call_ops *call_ops,
  1464. struct pnfs_layout_segment *lseg)
  1465. {
  1466. struct nfs_pgio_header *hdr = rdata->header;
  1467. struct inode *inode = hdr->inode;
  1468. struct nfs_server *nfss = NFS_SERVER(inode);
  1469. enum pnfs_try_status trypnfs;
  1470. hdr->mds_ops = call_ops;
  1471. dprintk("%s: Reading ino:%lu %u@%llu\n",
  1472. __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
  1473. trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
  1474. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1475. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  1476. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1477. return trypnfs;
  1478. }
  1479. static void
  1480. pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
  1481. {
  1482. struct nfs_read_data *data;
  1483. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1484. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1485. desc->pg_lseg = NULL;
  1486. while (!list_empty(head)) {
  1487. enum pnfs_try_status trypnfs;
  1488. data = list_first_entry(head, struct nfs_read_data, list);
  1489. list_del_init(&data->list);
  1490. trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
  1491. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1492. pnfs_read_through_mds(desc, data);
  1493. }
  1494. pnfs_put_lseg(lseg);
  1495. }
  1496. static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
  1497. {
  1498. pnfs_put_lseg(hdr->lseg);
  1499. nfs_readhdr_free(hdr);
  1500. }
  1501. EXPORT_SYMBOL_GPL(pnfs_readhdr_free);
  1502. int
  1503. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  1504. {
  1505. struct nfs_read_header *rhdr;
  1506. struct nfs_pgio_header *hdr;
  1507. int ret;
  1508. rhdr = nfs_readhdr_alloc();
  1509. if (!rhdr) {
  1510. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  1511. ret = -ENOMEM;
  1512. pnfs_put_lseg(desc->pg_lseg);
  1513. desc->pg_lseg = NULL;
  1514. return ret;
  1515. }
  1516. hdr = &rhdr->header;
  1517. nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
  1518. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  1519. atomic_inc(&hdr->refcnt);
  1520. ret = nfs_generic_pagein(desc, hdr);
  1521. if (ret != 0) {
  1522. pnfs_put_lseg(desc->pg_lseg);
  1523. desc->pg_lseg = NULL;
  1524. } else
  1525. pnfs_do_multiple_reads(desc, &hdr->rpc_list);
  1526. if (atomic_dec_and_test(&hdr->refcnt))
  1527. hdr->completion_ops->completion(hdr);
  1528. return ret;
  1529. }
  1530. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  1531. /*
  1532. * There can be multiple RW segments.
  1533. */
  1534. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  1535. {
  1536. struct pnfs_layout_segment *lseg;
  1537. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  1538. if (lseg->pls_range.iomode == IOMODE_RW &&
  1539. test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  1540. list_add(&lseg->pls_lc_list, listp);
  1541. }
  1542. }
  1543. void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
  1544. {
  1545. pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
  1546. }
  1547. EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
  1548. void
  1549. pnfs_set_layoutcommit(struct nfs_write_data *wdata)
  1550. {
  1551. struct nfs_pgio_header *hdr = wdata->header;
  1552. struct inode *inode = hdr->inode;
  1553. struct nfs_inode *nfsi = NFS_I(inode);
  1554. loff_t end_pos = wdata->mds_offset + wdata->res.count;
  1555. bool mark_as_dirty = false;
  1556. spin_lock(&inode->i_lock);
  1557. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1558. mark_as_dirty = true;
  1559. dprintk("%s: Set layoutcommit for inode %lu ",
  1560. __func__, inode->i_ino);
  1561. }
  1562. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
  1563. /* references matched in nfs4_layoutcommit_release */
  1564. pnfs_get_lseg(hdr->lseg);
  1565. }
  1566. if (end_pos > nfsi->layout->plh_lwb)
  1567. nfsi->layout->plh_lwb = end_pos;
  1568. spin_unlock(&inode->i_lock);
  1569. dprintk("%s: lseg %p end_pos %llu\n",
  1570. __func__, hdr->lseg, nfsi->layout->plh_lwb);
  1571. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  1572. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  1573. if (mark_as_dirty)
  1574. mark_inode_dirty_sync(inode);
  1575. }
  1576. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  1577. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  1578. {
  1579. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  1580. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  1581. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  1582. }
  1583. /*
  1584. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  1585. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  1586. * data to disk to allow the server to recover the data if it crashes.
  1587. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  1588. * is off, and a COMMIT is sent to a data server, or
  1589. * if WRITEs to a data server return NFS_DATA_SYNC.
  1590. */
  1591. int
  1592. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  1593. {
  1594. struct nfs4_layoutcommit_data *data;
  1595. struct nfs_inode *nfsi = NFS_I(inode);
  1596. loff_t end_pos;
  1597. int status = 0;
  1598. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  1599. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1600. return 0;
  1601. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  1602. data = kzalloc(sizeof(*data), GFP_NOFS);
  1603. if (!data) {
  1604. status = -ENOMEM;
  1605. goto out;
  1606. }
  1607. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1608. goto out_free;
  1609. if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
  1610. if (!sync) {
  1611. status = -EAGAIN;
  1612. goto out_free;
  1613. }
  1614. status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING,
  1615. nfs_wait_bit_killable, TASK_KILLABLE);
  1616. if (status)
  1617. goto out_free;
  1618. }
  1619. INIT_LIST_HEAD(&data->lseg_list);
  1620. spin_lock(&inode->i_lock);
  1621. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1622. clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags);
  1623. spin_unlock(&inode->i_lock);
  1624. wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING);
  1625. goto out_free;
  1626. }
  1627. pnfs_list_write_lseg(inode, &data->lseg_list);
  1628. end_pos = nfsi->layout->plh_lwb;
  1629. nfsi->layout->plh_lwb = 0;
  1630. nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
  1631. spin_unlock(&inode->i_lock);
  1632. data->args.inode = inode;
  1633. data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
  1634. nfs_fattr_init(&data->fattr);
  1635. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  1636. data->res.fattr = &data->fattr;
  1637. data->args.lastbytewritten = end_pos - 1;
  1638. data->res.server = NFS_SERVER(inode);
  1639. status = nfs4_proc_layoutcommit(data, sync);
  1640. out:
  1641. if (status)
  1642. mark_inode_dirty_sync(inode);
  1643. dprintk("<-- %s status %d\n", __func__, status);
  1644. return status;
  1645. out_free:
  1646. kfree(data);
  1647. goto out;
  1648. }
  1649. struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
  1650. {
  1651. struct nfs4_threshold *thp;
  1652. thp = kzalloc(sizeof(*thp), GFP_NOFS);
  1653. if (!thp) {
  1654. dprintk("%s mdsthreshold allocation failed\n", __func__);
  1655. return NULL;
  1656. }
  1657. return thp;
  1658. }