pnfs.c 49 KB

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