pnfs.c 63 KB

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