pnfs.c 61 KB

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