pnfs.c 61 KB

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