pnfs.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523
  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 <linux/sort.h>
  33. #include "internal.h"
  34. #include "pnfs.h"
  35. #include "iostat.h"
  36. #include "nfs4trace.h"
  37. #include "delegation.h"
  38. #include "nfs42.h"
  39. #include "nfs4_fs.h"
  40. #define NFSDBG_FACILITY NFSDBG_PNFS
  41. #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
  42. /* Locking:
  43. *
  44. * pnfs_spinlock:
  45. * protects pnfs_modules_tbl.
  46. */
  47. static DEFINE_SPINLOCK(pnfs_spinlock);
  48. /*
  49. * pnfs_modules_tbl holds all pnfs modules
  50. */
  51. static LIST_HEAD(pnfs_modules_tbl);
  52. static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
  53. static void pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
  54. struct list_head *free_me,
  55. const struct pnfs_layout_range *range,
  56. u32 seq);
  57. static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
  58. struct list_head *tmp_list);
  59. static int pnfs_layout_return_on_reboot(struct pnfs_layout_hdr *lo);
  60. /* Return the registered pnfs layout driver module matching given id */
  61. static struct pnfs_layoutdriver_type *
  62. find_pnfs_driver_locked(u32 id)
  63. {
  64. struct pnfs_layoutdriver_type *local;
  65. list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
  66. if (local->id == id)
  67. goto out;
  68. local = NULL;
  69. out:
  70. dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
  71. return local;
  72. }
  73. static struct pnfs_layoutdriver_type *
  74. find_pnfs_driver(u32 id)
  75. {
  76. struct pnfs_layoutdriver_type *local;
  77. spin_lock(&pnfs_spinlock);
  78. local = find_pnfs_driver_locked(id);
  79. if (local != NULL && !try_module_get(local->owner)) {
  80. dprintk("%s: Could not grab reference on module\n", __func__);
  81. local = NULL;
  82. }
  83. spin_unlock(&pnfs_spinlock);
  84. return local;
  85. }
  86. const struct pnfs_layoutdriver_type *pnfs_find_layoutdriver(u32 id)
  87. {
  88. return find_pnfs_driver(id);
  89. }
  90. void pnfs_put_layoutdriver(const struct pnfs_layoutdriver_type *ld)
  91. {
  92. if (ld)
  93. module_put(ld->owner);
  94. }
  95. void
  96. unset_pnfs_layoutdriver(struct nfs_server *nfss)
  97. {
  98. if (nfss->pnfs_curr_ld) {
  99. if (nfss->pnfs_curr_ld->clear_layoutdriver)
  100. nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
  101. /* Decrement the MDS count. Purge the deviceid cache if zero */
  102. if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
  103. nfs4_deviceid_purge_client(nfss->nfs_client);
  104. module_put(nfss->pnfs_curr_ld->owner);
  105. }
  106. nfss->pnfs_curr_ld = NULL;
  107. }
  108. /*
  109. * When the server sends a list of layout types, we choose one in the order
  110. * given in the list below.
  111. *
  112. * FIXME: should this list be configurable in some fashion? module param?
  113. * mount option? something else?
  114. */
  115. static const u32 ld_prefs[] = {
  116. LAYOUT_SCSI,
  117. LAYOUT_BLOCK_VOLUME,
  118. LAYOUT_OSD2_OBJECTS,
  119. LAYOUT_FLEX_FILES,
  120. LAYOUT_NFSV4_1_FILES,
  121. 0
  122. };
  123. static int
  124. ld_cmp(const void *e1, const void *e2)
  125. {
  126. u32 ld1 = *((u32 *)e1);
  127. u32 ld2 = *((u32 *)e2);
  128. int i;
  129. for (i = 0; ld_prefs[i] != 0; i++) {
  130. if (ld1 == ld_prefs[i])
  131. return -1;
  132. if (ld2 == ld_prefs[i])
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. /*
  138. * Try to set the server's pnfs module to the pnfs layout type specified by id.
  139. * Currently only one pNFS layout driver per filesystem is supported.
  140. *
  141. * @ids array of layout types supported by MDS.
  142. */
  143. void
  144. set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
  145. struct nfs_fsinfo *fsinfo)
  146. {
  147. struct pnfs_layoutdriver_type *ld_type = NULL;
  148. u32 id;
  149. int i;
  150. if (fsinfo->nlayouttypes == 0)
  151. goto out_no_driver;
  152. if (!(server->nfs_client->cl_exchange_flags &
  153. (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
  154. printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
  155. __func__, server->nfs_client->cl_exchange_flags);
  156. goto out_no_driver;
  157. }
  158. sort(fsinfo->layouttype, fsinfo->nlayouttypes,
  159. sizeof(*fsinfo->layouttype), ld_cmp, NULL);
  160. for (i = 0; i < fsinfo->nlayouttypes; i++) {
  161. id = fsinfo->layouttype[i];
  162. ld_type = find_pnfs_driver(id);
  163. if (!ld_type) {
  164. request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
  165. id);
  166. ld_type = find_pnfs_driver(id);
  167. }
  168. if (ld_type)
  169. break;
  170. }
  171. if (!ld_type) {
  172. dprintk("%s: No pNFS module found!\n", __func__);
  173. goto out_no_driver;
  174. }
  175. server->pnfs_curr_ld = ld_type;
  176. if (ld_type->set_layoutdriver
  177. && ld_type->set_layoutdriver(server, mntfh)) {
  178. printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
  179. "driver %u.\n", __func__, id);
  180. module_put(ld_type->owner);
  181. goto out_no_driver;
  182. }
  183. /* Bump the MDS count */
  184. atomic_inc(&server->nfs_client->cl_mds_count);
  185. dprintk("%s: pNFS module for %u set\n", __func__, id);
  186. return;
  187. out_no_driver:
  188. dprintk("%s: Using NFSv4 I/O\n", __func__);
  189. server->pnfs_curr_ld = NULL;
  190. }
  191. int
  192. pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  193. {
  194. int status = -EINVAL;
  195. struct pnfs_layoutdriver_type *tmp;
  196. if (ld_type->id == 0) {
  197. printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
  198. return status;
  199. }
  200. if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
  201. printk(KERN_ERR "NFS: %s Layout driver must provide "
  202. "alloc_lseg and free_lseg.\n", __func__);
  203. return status;
  204. }
  205. spin_lock(&pnfs_spinlock);
  206. tmp = find_pnfs_driver_locked(ld_type->id);
  207. if (!tmp) {
  208. list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
  209. status = 0;
  210. dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
  211. ld_type->name);
  212. } else {
  213. printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
  214. __func__, ld_type->id);
  215. }
  216. spin_unlock(&pnfs_spinlock);
  217. return status;
  218. }
  219. EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
  220. void
  221. pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  222. {
  223. dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
  224. spin_lock(&pnfs_spinlock);
  225. list_del(&ld_type->pnfs_tblid);
  226. spin_unlock(&pnfs_spinlock);
  227. }
  228. EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
  229. /*
  230. * pNFS client layout cache
  231. */
  232. /* Need to hold i_lock if caller does not already hold reference */
  233. void
  234. pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
  235. {
  236. refcount_inc(&lo->plh_refcount);
  237. }
  238. static struct pnfs_layout_hdr *
  239. pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
  240. {
  241. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  242. return ld->alloc_layout_hdr(ino, gfp_flags);
  243. }
  244. static void
  245. pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
  246. {
  247. struct nfs_server *server = NFS_SERVER(lo->plh_inode);
  248. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  249. if (test_and_clear_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) {
  250. struct nfs_client *clp = server->nfs_client;
  251. spin_lock(&clp->cl_lock);
  252. list_del_rcu(&lo->plh_layouts);
  253. spin_unlock(&clp->cl_lock);
  254. }
  255. put_cred(lo->plh_lc_cred);
  256. return ld->free_layout_hdr(lo);
  257. }
  258. static void
  259. pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
  260. {
  261. struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
  262. dprintk("%s: freeing layout cache %p\n", __func__, lo);
  263. nfsi->layout = NULL;
  264. /* Reset MDS Threshold I/O counters */
  265. nfsi->write_io = 0;
  266. nfsi->read_io = 0;
  267. }
  268. void
  269. pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
  270. {
  271. struct inode *inode;
  272. if (!lo)
  273. return;
  274. inode = lo->plh_inode;
  275. pnfs_layoutreturn_before_put_layout_hdr(lo);
  276. if (refcount_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
  277. if (!list_empty(&lo->plh_segs))
  278. WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
  279. pnfs_detach_layout_hdr(lo);
  280. /* Notify pnfs_destroy_layout_final() that we're done */
  281. if (inode_state_read(inode) & (I_FREEING | I_CLEAR))
  282. wake_up_var_locked(lo, &inode->i_lock);
  283. spin_unlock(&inode->i_lock);
  284. pnfs_free_layout_hdr(lo);
  285. }
  286. }
  287. static struct inode *
  288. pnfs_grab_inode_layout_hdr(struct pnfs_layout_hdr *lo)
  289. {
  290. struct inode *inode = igrab(lo->plh_inode);
  291. if (inode)
  292. return inode;
  293. set_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags);
  294. return NULL;
  295. }
  296. /*
  297. * Compare 2 layout stateid sequence ids, to see which is newer,
  298. * taking into account wraparound issues.
  299. */
  300. static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
  301. {
  302. return (s32)(s1 - s2) > 0;
  303. }
  304. static void pnfs_barrier_update(struct pnfs_layout_hdr *lo, u32 newseq)
  305. {
  306. if (pnfs_seqid_is_newer(newseq, lo->plh_barrier) || !lo->plh_barrier)
  307. lo->plh_barrier = newseq;
  308. }
  309. static void
  310. pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
  311. u32 seq)
  312. {
  313. if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
  314. iomode = IOMODE_ANY;
  315. lo->plh_return_iomode = iomode;
  316. set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
  317. /*
  318. * We must set lo->plh_return_seq to avoid livelocks with
  319. * pnfs_layout_need_return()
  320. */
  321. if (seq == 0)
  322. seq = be32_to_cpu(lo->plh_stateid.seqid);
  323. if (!lo->plh_return_seq || pnfs_seqid_is_newer(seq, lo->plh_return_seq))
  324. lo->plh_return_seq = seq;
  325. pnfs_barrier_update(lo, seq);
  326. }
  327. static void
  328. pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
  329. {
  330. struct pnfs_layout_segment *lseg;
  331. lo->plh_return_iomode = 0;
  332. lo->plh_return_seq = 0;
  333. clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
  334. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  335. if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
  336. continue;
  337. pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
  338. }
  339. }
  340. static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
  341. {
  342. clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
  343. clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags);
  344. smp_mb__after_atomic();
  345. wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
  346. rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
  347. }
  348. static void
  349. pnfs_clear_lseg_state(struct pnfs_layout_segment *lseg,
  350. struct list_head *free_me)
  351. {
  352. clear_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  353. clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
  354. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags))
  355. pnfs_lseg_dec_and_remove_zero(lseg, free_me);
  356. if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  357. pnfs_lseg_dec_and_remove_zero(lseg, free_me);
  358. }
  359. /*
  360. * Update the seqid of a layout stateid after receiving
  361. * NFS4ERR_OLD_STATEID
  362. */
  363. bool nfs4_layout_refresh_old_stateid(nfs4_stateid *dst,
  364. struct pnfs_layout_range *dst_range,
  365. struct inode *inode)
  366. {
  367. struct pnfs_layout_hdr *lo;
  368. struct pnfs_layout_range range = {
  369. .iomode = IOMODE_ANY,
  370. .offset = 0,
  371. .length = NFS4_MAX_UINT64,
  372. };
  373. bool ret = false;
  374. LIST_HEAD(head);
  375. int err;
  376. spin_lock(&inode->i_lock);
  377. lo = NFS_I(inode)->layout;
  378. if (lo && pnfs_layout_is_valid(lo) &&
  379. nfs4_stateid_match_other(dst, &lo->plh_stateid)) {
  380. /* Is our call using the most recent seqid? If so, bump it */
  381. if (!nfs4_stateid_is_newer(&lo->plh_stateid, dst)) {
  382. nfs4_stateid_seqid_inc(dst);
  383. ret = true;
  384. goto out;
  385. }
  386. /* Try to update the seqid to the most recent */
  387. err = pnfs_mark_matching_lsegs_return(lo, &head, &range, 0);
  388. if (err != -EBUSY) {
  389. dst->seqid = lo->plh_stateid.seqid;
  390. *dst_range = range;
  391. ret = true;
  392. }
  393. }
  394. out:
  395. spin_unlock(&inode->i_lock);
  396. pnfs_free_lseg_list(&head);
  397. return ret;
  398. }
  399. /*
  400. * Mark a pnfs_layout_hdr and all associated layout segments as invalid
  401. *
  402. * In order to continue using the pnfs_layout_hdr, a full recovery
  403. * is required.
  404. * Note that caller must hold inode->i_lock.
  405. */
  406. int
  407. pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
  408. struct list_head *lseg_list)
  409. {
  410. struct pnfs_layout_range range = {
  411. .iomode = IOMODE_ANY,
  412. .offset = 0,
  413. .length = NFS4_MAX_UINT64,
  414. };
  415. struct pnfs_layout_segment *lseg, *next;
  416. if (test_and_set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags))
  417. return !list_empty(&lo->plh_segs);
  418. clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(lo->plh_inode)->flags);
  419. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  420. pnfs_clear_lseg_state(lseg, lseg_list);
  421. pnfs_clear_layoutreturn_info(lo);
  422. pnfs_free_returned_lsegs(lo, lseg_list, &range, 0);
  423. set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags);
  424. if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
  425. !test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
  426. pnfs_clear_layoutreturn_waitbit(lo);
  427. return !list_empty(&lo->plh_segs);
  428. }
  429. static int pnfs_mark_layout_stateid_return(struct pnfs_layout_hdr *lo,
  430. struct list_head *lseg_list,
  431. enum pnfs_iomode iomode, u32 seq)
  432. {
  433. struct pnfs_layout_range range = {
  434. .iomode = iomode,
  435. .length = NFS4_MAX_UINT64,
  436. };
  437. return pnfs_mark_matching_lsegs_return(lo, lseg_list, &range, seq);
  438. }
  439. static int
  440. pnfs_iomode_to_fail_bit(u32 iomode)
  441. {
  442. return iomode == IOMODE_RW ?
  443. NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
  444. }
  445. static void
  446. pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
  447. {
  448. lo->plh_retry_timestamp = jiffies;
  449. if (!test_and_set_bit(fail_bit, &lo->plh_flags))
  450. refcount_inc(&lo->plh_refcount);
  451. }
  452. static void
  453. pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
  454. {
  455. if (test_and_clear_bit(fail_bit, &lo->plh_flags))
  456. refcount_dec(&lo->plh_refcount);
  457. }
  458. static void
  459. pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
  460. {
  461. struct inode *inode = lo->plh_inode;
  462. struct pnfs_layout_range range = {
  463. .iomode = iomode,
  464. .offset = 0,
  465. .length = NFS4_MAX_UINT64,
  466. };
  467. LIST_HEAD(head);
  468. spin_lock(&inode->i_lock);
  469. pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  470. pnfs_mark_matching_lsegs_return(lo, &head, &range, 0);
  471. spin_unlock(&inode->i_lock);
  472. pnfs_free_lseg_list(&head);
  473. dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
  474. iomode == IOMODE_RW ? "RW" : "READ");
  475. }
  476. static bool
  477. pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
  478. {
  479. unsigned long start, end;
  480. int fail_bit = pnfs_iomode_to_fail_bit(iomode);
  481. if (test_bit(fail_bit, &lo->plh_flags) == 0)
  482. return false;
  483. end = jiffies;
  484. start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
  485. if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
  486. /* It is time to retry the failed layoutgets */
  487. pnfs_layout_clear_fail_bit(lo, fail_bit);
  488. return false;
  489. }
  490. return true;
  491. }
  492. static void
  493. pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
  494. const struct pnfs_layout_range *range,
  495. const nfs4_stateid *stateid)
  496. {
  497. INIT_LIST_HEAD(&lseg->pls_list);
  498. INIT_LIST_HEAD(&lseg->pls_lc_list);
  499. INIT_LIST_HEAD(&lseg->pls_commits);
  500. refcount_set(&lseg->pls_refcount, 1);
  501. set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
  502. lseg->pls_layout = lo;
  503. lseg->pls_range = *range;
  504. lseg->pls_seq = be32_to_cpu(stateid->seqid);
  505. }
  506. static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
  507. {
  508. if (lseg != NULL) {
  509. struct inode *inode = lseg->pls_layout->plh_inode;
  510. NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
  511. }
  512. }
  513. static void
  514. pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
  515. struct pnfs_layout_segment *lseg)
  516. {
  517. WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  518. list_del_init(&lseg->pls_list);
  519. /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
  520. refcount_dec(&lo->plh_refcount);
  521. if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
  522. return;
  523. if (list_empty(&lo->plh_segs) &&
  524. !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) &&
  525. !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
  526. if (atomic_read(&lo->plh_outstanding) == 0)
  527. set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
  528. clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  529. }
  530. }
  531. static bool
  532. pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo,
  533. struct pnfs_layout_segment *lseg)
  534. {
  535. if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
  536. pnfs_layout_is_valid(lo)) {
  537. pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
  538. list_move_tail(&lseg->pls_list, &lo->plh_return_segs);
  539. return true;
  540. }
  541. return false;
  542. }
  543. void
  544. pnfs_put_lseg(struct pnfs_layout_segment *lseg)
  545. {
  546. struct pnfs_layout_hdr *lo;
  547. struct inode *inode;
  548. if (!lseg)
  549. return;
  550. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  551. refcount_read(&lseg->pls_refcount),
  552. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  553. lo = lseg->pls_layout;
  554. inode = lo->plh_inode;
  555. if (refcount_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
  556. pnfs_get_layout_hdr(lo);
  557. pnfs_layout_remove_lseg(lo, lseg);
  558. if (pnfs_cache_lseg_for_layoutreturn(lo, lseg))
  559. lseg = NULL;
  560. spin_unlock(&inode->i_lock);
  561. pnfs_free_lseg(lseg);
  562. pnfs_put_layout_hdr(lo);
  563. }
  564. }
  565. EXPORT_SYMBOL_GPL(pnfs_put_lseg);
  566. /*
  567. * is l2 fully contained in l1?
  568. * start1 end1
  569. * [----------------------------------)
  570. * start2 end2
  571. * [----------------)
  572. */
  573. static bool
  574. pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
  575. const struct pnfs_layout_range *l2)
  576. {
  577. u64 start1 = l1->offset;
  578. u64 end1 = pnfs_end_offset(start1, l1->length);
  579. u64 start2 = l2->offset;
  580. u64 end2 = pnfs_end_offset(start2, l2->length);
  581. return (start1 <= start2) && (end1 >= end2);
  582. }
  583. static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
  584. struct list_head *tmp_list)
  585. {
  586. if (!refcount_dec_and_test(&lseg->pls_refcount))
  587. return false;
  588. pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
  589. list_add(&lseg->pls_list, tmp_list);
  590. return true;
  591. }
  592. /* Returns 1 if lseg is removed from list, 0 otherwise */
  593. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  594. struct list_head *tmp_list)
  595. {
  596. int rv = 0;
  597. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  598. /* Remove the reference keeping the lseg in the
  599. * list. It will now be removed when all
  600. * outstanding io is finished.
  601. */
  602. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  603. refcount_read(&lseg->pls_refcount));
  604. if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
  605. rv = 1;
  606. }
  607. return rv;
  608. }
  609. static bool
  610. pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
  611. const struct pnfs_layout_range *recall_range)
  612. {
  613. return (recall_range->iomode == IOMODE_ANY ||
  614. lseg_range->iomode == recall_range->iomode) &&
  615. pnfs_lseg_range_intersecting(lseg_range, recall_range);
  616. }
  617. static bool
  618. pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
  619. const struct pnfs_layout_range *recall_range,
  620. u32 seq)
  621. {
  622. if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
  623. return false;
  624. if (recall_range == NULL)
  625. return true;
  626. return pnfs_should_free_range(&lseg->pls_range, recall_range);
  627. }
  628. /**
  629. * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
  630. * @lo: layout header containing the lsegs
  631. * @tmp_list: list head where doomed lsegs should go
  632. * @recall_range: optional recall range argument to match (may be NULL)
  633. * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
  634. *
  635. * Walk the list of lsegs in the layout header, and tear down any that should
  636. * be destroyed. If "recall_range" is specified then the segment must match
  637. * that range. If "seq" is non-zero, then only match segments that were handed
  638. * out at or before that sequence.
  639. *
  640. * Returns number of matching invalid lsegs remaining in list after scanning
  641. * it and purging them.
  642. */
  643. int
  644. pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
  645. struct list_head *tmp_list,
  646. const struct pnfs_layout_range *recall_range,
  647. u32 seq)
  648. {
  649. struct pnfs_layout_segment *lseg, *next;
  650. struct nfs_server *server = NFS_SERVER(lo->plh_inode);
  651. int remaining = 0;
  652. dprintk("%s:Begin lo %p\n", __func__, lo);
  653. if (list_empty(&lo->plh_segs))
  654. return 0;
  655. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  656. if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
  657. dprintk("%s: freeing lseg %p iomode %d seq %u "
  658. "offset %llu length %llu\n", __func__,
  659. lseg, lseg->pls_range.iomode, lseg->pls_seq,
  660. lseg->pls_range.offset, lseg->pls_range.length);
  661. if (mark_lseg_invalid(lseg, tmp_list))
  662. continue;
  663. remaining++;
  664. pnfs_lseg_cancel_io(server, lseg);
  665. }
  666. dprintk("%s:Return %i\n", __func__, remaining);
  667. return remaining;
  668. }
  669. static void pnfs_reset_return_info(struct pnfs_layout_hdr *lo)
  670. {
  671. struct pnfs_layout_segment *lseg;
  672. list_for_each_entry(lseg, &lo->plh_return_segs, pls_list)
  673. pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
  674. }
  675. static void
  676. pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
  677. struct list_head *free_me,
  678. const struct pnfs_layout_range *range,
  679. u32 seq)
  680. {
  681. struct pnfs_layout_segment *lseg, *next;
  682. list_for_each_entry_safe(lseg, next, &lo->plh_return_segs, pls_list) {
  683. if (pnfs_match_lseg_recall(lseg, range, seq))
  684. list_move_tail(&lseg->pls_list, free_me);
  685. }
  686. }
  687. /* note free_me must contain lsegs from a single layout_hdr */
  688. void
  689. pnfs_free_lseg_list(struct list_head *free_me)
  690. {
  691. struct pnfs_layout_segment *lseg, *tmp;
  692. if (list_empty(free_me))
  693. return;
  694. list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
  695. list_del(&lseg->pls_list);
  696. pnfs_free_lseg(lseg);
  697. }
  698. }
  699. static struct pnfs_layout_hdr *__pnfs_destroy_layout(struct nfs_inode *nfsi)
  700. {
  701. struct pnfs_layout_hdr *lo;
  702. LIST_HEAD(tmp_list);
  703. spin_lock(&nfsi->vfs_inode.i_lock);
  704. lo = nfsi->layout;
  705. if (lo) {
  706. pnfs_get_layout_hdr(lo);
  707. pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
  708. pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
  709. pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
  710. spin_unlock(&nfsi->vfs_inode.i_lock);
  711. pnfs_free_lseg_list(&tmp_list);
  712. nfs_commit_inode(&nfsi->vfs_inode, 0);
  713. pnfs_put_layout_hdr(lo);
  714. } else
  715. spin_unlock(&nfsi->vfs_inode.i_lock);
  716. return lo;
  717. }
  718. void pnfs_destroy_layout(struct nfs_inode *nfsi)
  719. {
  720. __pnfs_destroy_layout(nfsi);
  721. }
  722. EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
  723. void pnfs_destroy_layout_final(struct nfs_inode *nfsi)
  724. {
  725. struct pnfs_layout_hdr *lo = __pnfs_destroy_layout(nfsi);
  726. struct inode *inode = &nfsi->vfs_inode;
  727. if (lo) {
  728. spin_lock(&inode->i_lock);
  729. wait_var_event_spinlock(lo, nfsi->layout != lo,
  730. &inode->i_lock);
  731. spin_unlock(&inode->i_lock);
  732. }
  733. }
  734. static bool
  735. pnfs_layout_add_bulk_destroy_list(struct inode *inode,
  736. struct list_head *layout_list)
  737. {
  738. struct pnfs_layout_hdr *lo;
  739. bool ret = false;
  740. spin_lock(&inode->i_lock);
  741. lo = NFS_I(inode)->layout;
  742. if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
  743. pnfs_get_layout_hdr(lo);
  744. list_add(&lo->plh_bulk_destroy, layout_list);
  745. ret = true;
  746. }
  747. spin_unlock(&inode->i_lock);
  748. return ret;
  749. }
  750. /* Caller must hold rcu_read_lock and clp->cl_lock */
  751. static int
  752. pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
  753. struct nfs_server *server,
  754. struct list_head *layout_list)
  755. __must_hold(&clp->cl_lock)
  756. __must_hold(RCU)
  757. {
  758. struct pnfs_layout_hdr *lo, *next;
  759. struct inode *inode;
  760. list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
  761. if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
  762. test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) ||
  763. !list_empty(&lo->plh_bulk_destroy))
  764. continue;
  765. /* If the sb is being destroyed, just bail */
  766. if (!nfs_sb_active(server->super))
  767. break;
  768. inode = pnfs_grab_inode_layout_hdr(lo);
  769. if (inode != NULL) {
  770. if (pnfs_layout_add_bulk_destroy_list(inode,
  771. layout_list))
  772. continue;
  773. rcu_read_unlock();
  774. spin_unlock(&clp->cl_lock);
  775. iput(inode);
  776. } else {
  777. rcu_read_unlock();
  778. spin_unlock(&clp->cl_lock);
  779. }
  780. nfs_sb_deactive(server->super);
  781. spin_lock(&clp->cl_lock);
  782. rcu_read_lock();
  783. return -EAGAIN;
  784. }
  785. return 0;
  786. }
  787. static int
  788. pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
  789. enum pnfs_layout_destroy_mode mode)
  790. {
  791. struct pnfs_layout_hdr *lo;
  792. struct inode *inode;
  793. LIST_HEAD(lseg_list);
  794. int ret = 0;
  795. while (!list_empty(layout_list)) {
  796. lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
  797. plh_bulk_destroy);
  798. dprintk("%s freeing layout for inode %lu\n", __func__,
  799. lo->plh_inode->i_ino);
  800. inode = lo->plh_inode;
  801. pnfs_layoutcommit_inode(inode, false);
  802. spin_lock(&inode->i_lock);
  803. list_del_init(&lo->plh_bulk_destroy);
  804. if (mode == PNFS_LAYOUT_FILE_BULK_RETURN) {
  805. pnfs_mark_layout_stateid_return(lo, &lseg_list,
  806. IOMODE_ANY, 0);
  807. } else if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
  808. if (mode == PNFS_LAYOUT_BULK_RETURN)
  809. set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  810. ret = -EAGAIN;
  811. }
  812. spin_unlock(&inode->i_lock);
  813. pnfs_free_lseg_list(&lseg_list);
  814. /* Free all lsegs that are attached to commit buckets */
  815. nfs_commit_inode(inode, 0);
  816. pnfs_put_layout_hdr(lo);
  817. nfs_iput_and_deactive(inode);
  818. }
  819. return ret;
  820. }
  821. int pnfs_layout_destroy_byfsid(struct nfs_client *clp, struct nfs_fsid *fsid,
  822. enum pnfs_layout_destroy_mode mode)
  823. {
  824. struct nfs_server *server;
  825. LIST_HEAD(layout_list);
  826. spin_lock(&clp->cl_lock);
  827. rcu_read_lock();
  828. restart:
  829. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  830. if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
  831. continue;
  832. if (pnfs_layout_bulk_destroy_byserver_locked(clp,
  833. server,
  834. &layout_list) != 0)
  835. goto restart;
  836. }
  837. rcu_read_unlock();
  838. spin_unlock(&clp->cl_lock);
  839. return pnfs_layout_free_bulk_destroy_list(&layout_list, mode);
  840. }
  841. static void pnfs_layout_build_destroy_list_byclient(struct nfs_client *clp,
  842. struct list_head *list)
  843. {
  844. struct nfs_server *server;
  845. spin_lock(&clp->cl_lock);
  846. rcu_read_lock();
  847. restart:
  848. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  849. if (pnfs_layout_bulk_destroy_byserver_locked(clp, server,
  850. list) != 0)
  851. goto restart;
  852. }
  853. rcu_read_unlock();
  854. spin_unlock(&clp->cl_lock);
  855. }
  856. static int pnfs_layout_do_destroy_byclid(struct nfs_client *clp,
  857. struct list_head *list,
  858. enum pnfs_layout_destroy_mode mode)
  859. {
  860. pnfs_layout_build_destroy_list_byclient(clp, list);
  861. return pnfs_layout_free_bulk_destroy_list(list, mode);
  862. }
  863. int pnfs_layout_destroy_byclid(struct nfs_client *clp,
  864. enum pnfs_layout_destroy_mode mode)
  865. {
  866. LIST_HEAD(layout_list);
  867. return pnfs_layout_do_destroy_byclid(clp, &layout_list, mode);
  868. }
  869. /*
  870. * Called by the state manager to remove all layouts established under an
  871. * expired lease.
  872. */
  873. void
  874. pnfs_destroy_all_layouts(struct nfs_client *clp)
  875. {
  876. nfs4_deviceid_mark_client_invalid(clp);
  877. nfs4_deviceid_purge_client(clp);
  878. pnfs_layout_destroy_byclid(clp, PNFS_LAYOUT_INVALIDATE);
  879. }
  880. static void pnfs_layout_build_recover_list_byclient(struct nfs_client *clp,
  881. struct list_head *list)
  882. {
  883. struct nfs_server *server;
  884. spin_lock(&clp->cl_lock);
  885. rcu_read_lock();
  886. restart:
  887. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  888. if (!(server->caps & NFS_CAP_REBOOT_LAYOUTRETURN))
  889. continue;
  890. if (pnfs_layout_bulk_destroy_byserver_locked(clp, server,
  891. list) != 0)
  892. goto restart;
  893. }
  894. rcu_read_unlock();
  895. spin_unlock(&clp->cl_lock);
  896. }
  897. static int pnfs_layout_bulk_list_reboot(struct list_head *list)
  898. {
  899. struct pnfs_layout_hdr *lo;
  900. struct nfs_server *server;
  901. int ret;
  902. list_for_each_entry(lo, list, plh_bulk_destroy) {
  903. server = NFS_SERVER(lo->plh_inode);
  904. ret = pnfs_layout_return_on_reboot(lo);
  905. switch (ret) {
  906. case 0:
  907. continue;
  908. case -NFS4ERR_BAD_STATEID:
  909. server->caps &= ~NFS_CAP_REBOOT_LAYOUTRETURN;
  910. break;
  911. case -NFS4ERR_NO_GRACE:
  912. break;
  913. default:
  914. goto err;
  915. }
  916. break;
  917. }
  918. return 0;
  919. err:
  920. return ret;
  921. }
  922. int pnfs_layout_handle_reboot(struct nfs_client *clp)
  923. {
  924. LIST_HEAD(list);
  925. int ret = 0, ret2;
  926. pnfs_layout_build_recover_list_byclient(clp, &list);
  927. if (!list_empty(&list))
  928. ret = pnfs_layout_bulk_list_reboot(&list);
  929. ret2 = pnfs_layout_do_destroy_byclid(clp, &list,
  930. PNFS_LAYOUT_INVALIDATE);
  931. if (!ret)
  932. ret = ret2;
  933. return (ret == 0) ? 0 : -EAGAIN;
  934. }
  935. static void
  936. pnfs_set_layout_cred(struct pnfs_layout_hdr *lo, const struct cred *cred)
  937. {
  938. const struct cred *old;
  939. if (cred && cred_fscmp(lo->plh_lc_cred, cred) != 0) {
  940. old = xchg(&lo->plh_lc_cred, get_cred(cred));
  941. put_cred(old);
  942. }
  943. }
  944. /* update lo->plh_stateid with new if is more recent */
  945. void
  946. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  947. const struct cred *cred, bool update_barrier)
  948. {
  949. u32 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
  950. u32 newseq = be32_to_cpu(new->seqid);
  951. if (!pnfs_layout_is_valid(lo)) {
  952. pnfs_set_layout_cred(lo, cred);
  953. nfs4_stateid_copy(&lo->plh_stateid, new);
  954. lo->plh_barrier = newseq;
  955. pnfs_clear_layoutreturn_info(lo);
  956. clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
  957. return;
  958. }
  959. if (pnfs_seqid_is_newer(newseq, oldseq))
  960. nfs4_stateid_copy(&lo->plh_stateid, new);
  961. if (update_barrier) {
  962. pnfs_barrier_update(lo, newseq);
  963. return;
  964. }
  965. /*
  966. * Because of wraparound, we want to keep the barrier
  967. * "close" to the current seqids. We really only want to
  968. * get here from a layoutget call.
  969. */
  970. if (atomic_read(&lo->plh_outstanding) == 1)
  971. pnfs_barrier_update(lo, be32_to_cpu(lo->plh_stateid.seqid));
  972. }
  973. static bool
  974. pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
  975. const nfs4_stateid *stateid)
  976. {
  977. u32 seqid = be32_to_cpu(stateid->seqid);
  978. return lo->plh_barrier && pnfs_seqid_is_newer(lo->plh_barrier, seqid);
  979. }
  980. /* lget is set to 1 if called from inside send_layoutget call chain */
  981. static bool
  982. pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
  983. {
  984. return lo->plh_block_lgets ||
  985. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  986. }
  987. static struct nfs_server *
  988. pnfs_find_server(struct inode *inode, struct nfs_open_context *ctx)
  989. {
  990. struct nfs_server *server;
  991. if (inode) {
  992. server = NFS_SERVER(inode);
  993. } else {
  994. struct dentry *parent_dir = dget_parent(ctx->dentry);
  995. server = NFS_SERVER(parent_dir->d_inode);
  996. dput(parent_dir);
  997. }
  998. return server;
  999. }
  1000. static void nfs4_free_pages(struct page **pages, size_t size)
  1001. {
  1002. int i;
  1003. if (!pages)
  1004. return;
  1005. for (i = 0; i < size; i++) {
  1006. if (!pages[i])
  1007. break;
  1008. __free_page(pages[i]);
  1009. }
  1010. kfree(pages);
  1011. }
  1012. static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
  1013. {
  1014. struct page **pages;
  1015. int i;
  1016. pages = kmalloc_objs(struct page *, size, gfp_flags);
  1017. if (!pages) {
  1018. dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
  1019. return NULL;
  1020. }
  1021. for (i = 0; i < size; i++) {
  1022. pages[i] = alloc_page(gfp_flags);
  1023. if (!pages[i]) {
  1024. dprintk("%s: failed to allocate page\n", __func__);
  1025. nfs4_free_pages(pages, i);
  1026. return NULL;
  1027. }
  1028. }
  1029. return pages;
  1030. }
  1031. static struct nfs4_layoutget *
  1032. pnfs_alloc_init_layoutget_args(struct inode *ino,
  1033. struct nfs_open_context *ctx,
  1034. const nfs4_stateid *stateid,
  1035. const struct pnfs_layout_range *range,
  1036. gfp_t gfp_flags)
  1037. {
  1038. struct nfs_server *server = pnfs_find_server(ino, ctx);
  1039. size_t max_reply_sz = server->pnfs_curr_ld->max_layoutget_response;
  1040. size_t max_pages = max_response_pages(server);
  1041. struct nfs4_layoutget *lgp;
  1042. dprintk("--> %s\n", __func__);
  1043. lgp = kzalloc_obj(*lgp, gfp_flags);
  1044. if (lgp == NULL)
  1045. return NULL;
  1046. if (max_reply_sz) {
  1047. size_t npages = (max_reply_sz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1048. if (npages < max_pages)
  1049. max_pages = npages;
  1050. }
  1051. lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
  1052. if (!lgp->args.layout.pages) {
  1053. kfree(lgp);
  1054. return NULL;
  1055. }
  1056. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  1057. lgp->res.layoutp = &lgp->args.layout;
  1058. /* Don't confuse uninitialised result and success */
  1059. lgp->res.status = -NFS4ERR_DELAY;
  1060. lgp->args.minlength = PAGE_SIZE;
  1061. if (lgp->args.minlength > range->length)
  1062. lgp->args.minlength = range->length;
  1063. if (ino) {
  1064. loff_t i_size = i_size_read(ino);
  1065. if (range->iomode == IOMODE_READ) {
  1066. if (range->offset >= i_size)
  1067. lgp->args.minlength = 0;
  1068. else if (i_size - range->offset < lgp->args.minlength)
  1069. lgp->args.minlength = i_size - range->offset;
  1070. }
  1071. }
  1072. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  1073. pnfs_copy_range(&lgp->args.range, range);
  1074. lgp->args.type = server->pnfs_curr_ld->id;
  1075. lgp->args.inode = ino;
  1076. lgp->args.ctx = get_nfs_open_context(ctx);
  1077. nfs4_stateid_copy(&lgp->args.stateid, stateid);
  1078. lgp->gfp_flags = gfp_flags;
  1079. lgp->cred = ctx->cred;
  1080. return lgp;
  1081. }
  1082. void pnfs_layoutget_free(struct nfs4_layoutget *lgp)
  1083. {
  1084. size_t max_pages = lgp->args.layout.pglen / PAGE_SIZE;
  1085. nfs4_free_pages(lgp->args.layout.pages, max_pages);
  1086. pnfs_put_layout_hdr(lgp->lo);
  1087. put_nfs_open_context(lgp->args.ctx);
  1088. kfree(lgp);
  1089. }
  1090. static void pnfs_clear_layoutcommit(struct inode *inode,
  1091. struct list_head *head)
  1092. {
  1093. struct nfs_inode *nfsi = NFS_I(inode);
  1094. struct pnfs_layout_segment *lseg, *tmp;
  1095. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1096. return;
  1097. list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
  1098. if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  1099. continue;
  1100. pnfs_lseg_dec_and_remove_zero(lseg, head);
  1101. }
  1102. }
  1103. static void
  1104. pnfs_layoutreturn_retry_later_locked(struct pnfs_layout_hdr *lo,
  1105. const nfs4_stateid *arg_stateid,
  1106. const struct pnfs_layout_range *range,
  1107. struct list_head *freeme)
  1108. {
  1109. if (pnfs_layout_is_valid(lo) &&
  1110. nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
  1111. pnfs_reset_return_info(lo);
  1112. else
  1113. pnfs_mark_layout_stateid_invalid(lo, freeme);
  1114. pnfs_clear_layoutreturn_waitbit(lo);
  1115. }
  1116. void pnfs_layoutreturn_retry_later(struct pnfs_layout_hdr *lo,
  1117. const nfs4_stateid *arg_stateid,
  1118. const struct pnfs_layout_range *range)
  1119. {
  1120. struct inode *inode = lo->plh_inode;
  1121. LIST_HEAD(freeme);
  1122. spin_lock(&inode->i_lock);
  1123. pnfs_layoutreturn_retry_later_locked(lo, arg_stateid, range, &freeme);
  1124. spin_unlock(&inode->i_lock);
  1125. pnfs_free_lseg_list(&freeme);
  1126. }
  1127. void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
  1128. const nfs4_stateid *arg_stateid,
  1129. const struct pnfs_layout_range *range,
  1130. const nfs4_stateid *stateid)
  1131. {
  1132. struct inode *inode = lo->plh_inode;
  1133. LIST_HEAD(freeme);
  1134. spin_lock(&inode->i_lock);
  1135. if (!nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
  1136. goto out_unlock;
  1137. if (stateid && pnfs_layout_is_valid(lo)) {
  1138. u32 seq = be32_to_cpu(arg_stateid->seqid);
  1139. pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq);
  1140. pnfs_free_returned_lsegs(lo, &freeme, range, seq);
  1141. pnfs_set_layout_stateid(lo, stateid, NULL, true);
  1142. pnfs_reset_return_info(lo);
  1143. } else
  1144. pnfs_mark_layout_stateid_invalid(lo, &freeme);
  1145. out_unlock:
  1146. pnfs_clear_layoutreturn_waitbit(lo);
  1147. spin_unlock(&inode->i_lock);
  1148. pnfs_free_lseg_list(&freeme);
  1149. }
  1150. static bool
  1151. pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
  1152. nfs4_stateid *stateid,
  1153. const struct cred **cred,
  1154. enum pnfs_iomode *iomode)
  1155. {
  1156. /* Serialise LAYOUTGET/LAYOUTRETURN */
  1157. if (atomic_read(&lo->plh_outstanding) != 0 && lo->plh_return_seq == 0)
  1158. return false;
  1159. if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
  1160. return false;
  1161. set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
  1162. pnfs_get_layout_hdr(lo);
  1163. nfs4_stateid_copy(stateid, &lo->plh_stateid);
  1164. *cred = get_cred(lo->plh_lc_cred);
  1165. if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
  1166. if (lo->plh_return_seq != 0)
  1167. stateid->seqid = cpu_to_be32(lo->plh_return_seq);
  1168. if (iomode != NULL)
  1169. *iomode = lo->plh_return_iomode;
  1170. pnfs_clear_layoutreturn_info(lo);
  1171. } else if (iomode != NULL)
  1172. *iomode = IOMODE_ANY;
  1173. pnfs_barrier_update(lo, be32_to_cpu(stateid->seqid));
  1174. return true;
  1175. }
  1176. static void
  1177. pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
  1178. struct pnfs_layout_hdr *lo,
  1179. const nfs4_stateid *stateid,
  1180. enum pnfs_iomode iomode)
  1181. {
  1182. struct inode *inode = lo->plh_inode;
  1183. args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
  1184. args->inode = inode;
  1185. args->range.iomode = iomode;
  1186. args->range.offset = 0;
  1187. args->range.length = NFS4_MAX_UINT64;
  1188. args->layout = lo;
  1189. nfs4_stateid_copy(&args->stateid, stateid);
  1190. }
  1191. static int
  1192. pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo,
  1193. const nfs4_stateid *stateid,
  1194. const struct cred **pcred,
  1195. enum pnfs_iomode iomode,
  1196. unsigned int flags)
  1197. {
  1198. struct inode *ino = lo->plh_inode;
  1199. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  1200. struct nfs4_layoutreturn *lrp;
  1201. const struct cred *cred = *pcred;
  1202. int status = 0;
  1203. *pcred = NULL;
  1204. lrp = kzalloc_obj(*lrp, nfs_io_gfp_mask());
  1205. if (unlikely(lrp == NULL)) {
  1206. status = -ENOMEM;
  1207. spin_lock(&ino->i_lock);
  1208. pnfs_clear_layoutreturn_waitbit(lo);
  1209. spin_unlock(&ino->i_lock);
  1210. put_cred(cred);
  1211. pnfs_put_layout_hdr(lo);
  1212. goto out;
  1213. }
  1214. pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
  1215. lrp->args.ld_private = &lrp->ld_private;
  1216. lrp->clp = NFS_SERVER(ino)->nfs_client;
  1217. lrp->cred = cred;
  1218. if (ld->prepare_layoutreturn)
  1219. ld->prepare_layoutreturn(&lrp->args);
  1220. status = nfs4_proc_layoutreturn(lrp, flags);
  1221. out:
  1222. dprintk("<-- %s status: %d\n", __func__, status);
  1223. return status;
  1224. }
  1225. /* Return true if layoutreturn is needed */
  1226. static bool
  1227. pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
  1228. {
  1229. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  1230. return false;
  1231. return pnfs_mark_layout_stateid_return(lo, &lo->plh_return_segs,
  1232. lo->plh_return_iomode,
  1233. lo->plh_return_seq) != EBUSY;
  1234. }
  1235. static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
  1236. {
  1237. struct inode *inode= lo->plh_inode;
  1238. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  1239. return;
  1240. spin_lock(&inode->i_lock);
  1241. if (pnfs_layout_need_return(lo)) {
  1242. const struct cred *cred;
  1243. nfs4_stateid stateid;
  1244. enum pnfs_iomode iomode;
  1245. bool send;
  1246. send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode);
  1247. spin_unlock(&inode->i_lock);
  1248. if (send) {
  1249. /* Send an async layoutreturn so we dont deadlock */
  1250. pnfs_send_layoutreturn(lo, &stateid, &cred, iomode,
  1251. PNFS_FL_LAYOUTRETURN_ASYNC);
  1252. }
  1253. } else
  1254. spin_unlock(&inode->i_lock);
  1255. }
  1256. /*
  1257. * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
  1258. * when the layout segment list is empty.
  1259. *
  1260. * Note that a pnfs_layout_hdr can exist with an empty layout segment
  1261. * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
  1262. * deviceid is marked invalid.
  1263. */
  1264. int
  1265. _pnfs_return_layout(struct inode *ino)
  1266. {
  1267. struct pnfs_layout_hdr *lo = NULL;
  1268. struct nfs_inode *nfsi = NFS_I(ino);
  1269. struct pnfs_layout_range range = {
  1270. .iomode = IOMODE_ANY,
  1271. .offset = 0,
  1272. .length = NFS4_MAX_UINT64,
  1273. };
  1274. LIST_HEAD(tmp_list);
  1275. const struct cred *cred;
  1276. nfs4_stateid stateid;
  1277. int status = 0;
  1278. bool send, valid_layout;
  1279. dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
  1280. spin_lock(&ino->i_lock);
  1281. lo = nfsi->layout;
  1282. if (!lo) {
  1283. spin_unlock(&ino->i_lock);
  1284. dprintk("NFS: %s no layout to return\n", __func__);
  1285. goto out;
  1286. }
  1287. /* Reference matched in nfs4_layoutreturn_release */
  1288. pnfs_get_layout_hdr(lo);
  1289. /* Is there an outstanding layoutreturn ? */
  1290. if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
  1291. spin_unlock(&ino->i_lock);
  1292. if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1293. TASK_UNINTERRUPTIBLE))
  1294. goto out_put_layout_hdr;
  1295. spin_lock(&ino->i_lock);
  1296. }
  1297. valid_layout = pnfs_layout_is_valid(lo);
  1298. pnfs_clear_layoutcommit(ino, &tmp_list);
  1299. pnfs_mark_matching_lsegs_return(lo, &tmp_list, &range, 0);
  1300. if (NFS_SERVER(ino)->pnfs_curr_ld->return_range)
  1301. NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
  1302. /* Don't send a LAYOUTRETURN if list was initially empty */
  1303. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) ||
  1304. !valid_layout) {
  1305. spin_unlock(&ino->i_lock);
  1306. dprintk("NFS: %s no layout segments to return\n", __func__);
  1307. goto out_wait_layoutreturn;
  1308. }
  1309. send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, NULL);
  1310. spin_unlock(&ino->i_lock);
  1311. if (send)
  1312. status = pnfs_send_layoutreturn(lo, &stateid, &cred, IOMODE_ANY,
  1313. 0);
  1314. out_wait_layoutreturn:
  1315. wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, TASK_UNINTERRUPTIBLE);
  1316. out_put_layout_hdr:
  1317. pnfs_free_lseg_list(&tmp_list);
  1318. pnfs_put_layout_hdr(lo);
  1319. out:
  1320. dprintk("<-- %s status: %d\n", __func__, status);
  1321. return status;
  1322. }
  1323. int
  1324. pnfs_commit_and_return_layout(struct inode *inode)
  1325. {
  1326. struct pnfs_layout_hdr *lo;
  1327. int ret;
  1328. spin_lock(&inode->i_lock);
  1329. lo = NFS_I(inode)->layout;
  1330. if (lo == NULL) {
  1331. spin_unlock(&inode->i_lock);
  1332. return 0;
  1333. }
  1334. pnfs_get_layout_hdr(lo);
  1335. /* Block new layoutgets and read/write to ds */
  1336. lo->plh_block_lgets++;
  1337. spin_unlock(&inode->i_lock);
  1338. filemap_fdatawait(inode->i_mapping);
  1339. ret = pnfs_layoutcommit_inode(inode, true);
  1340. if (ret == 0)
  1341. ret = _pnfs_return_layout(inode);
  1342. spin_lock(&inode->i_lock);
  1343. lo->plh_block_lgets--;
  1344. spin_unlock(&inode->i_lock);
  1345. pnfs_put_layout_hdr(lo);
  1346. return ret;
  1347. }
  1348. static int pnfs_layout_return_on_reboot(struct pnfs_layout_hdr *lo)
  1349. {
  1350. struct inode *inode = lo->plh_inode;
  1351. const struct cred *cred;
  1352. spin_lock(&inode->i_lock);
  1353. if (!pnfs_layout_is_valid(lo)) {
  1354. spin_unlock(&inode->i_lock);
  1355. return 0;
  1356. }
  1357. cred = get_cred(lo->plh_lc_cred);
  1358. pnfs_get_layout_hdr(lo);
  1359. spin_unlock(&inode->i_lock);
  1360. return pnfs_send_layoutreturn(lo, &zero_stateid, &cred, IOMODE_ANY,
  1361. PNFS_FL_LAYOUTRETURN_PRIVILEGED);
  1362. }
  1363. bool pnfs_roc(struct inode *ino, struct nfs4_layoutreturn_args *args,
  1364. struct nfs4_layoutreturn_res *res, const struct cred *cred,
  1365. bool sync)
  1366. {
  1367. struct nfs_inode *nfsi = NFS_I(ino);
  1368. struct nfs_open_context *ctx;
  1369. struct nfs4_state *state;
  1370. struct pnfs_layout_hdr *lo;
  1371. struct pnfs_layout_segment *lseg, *next;
  1372. const struct cred *lc_cred;
  1373. nfs4_stateid stateid;
  1374. enum pnfs_iomode iomode = 0;
  1375. bool layoutreturn = false, roc = false;
  1376. bool skip_read;
  1377. if (!nfs_have_layout(ino))
  1378. return false;
  1379. retry:
  1380. rcu_read_lock();
  1381. spin_lock(&ino->i_lock);
  1382. lo = nfsi->layout;
  1383. if (!lo || !pnfs_layout_is_valid(lo) ||
  1384. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1385. lo = NULL;
  1386. goto out_noroc;
  1387. }
  1388. /* no roc if we hold a delegation */
  1389. skip_read = false;
  1390. if (nfs4_check_delegation(ino, FMODE_READ)) {
  1391. if (nfs4_check_delegation(ino, FMODE_WRITE)) {
  1392. lo = NULL;
  1393. goto out_noroc;
  1394. }
  1395. skip_read = true;
  1396. }
  1397. list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
  1398. state = ctx->state;
  1399. if (state == NULL)
  1400. continue;
  1401. /* Don't return layout if there is open file state */
  1402. if (state->state & FMODE_WRITE) {
  1403. lo = NULL;
  1404. goto out_noroc;
  1405. }
  1406. if (state->state & FMODE_READ)
  1407. skip_read = true;
  1408. }
  1409. if (skip_read) {
  1410. bool writes = false;
  1411. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  1412. if (lseg->pls_range.iomode != IOMODE_READ) {
  1413. writes = true;
  1414. break;
  1415. }
  1416. }
  1417. if (!writes) {
  1418. lo = NULL;
  1419. goto out_noroc;
  1420. }
  1421. }
  1422. pnfs_get_layout_hdr(lo);
  1423. if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
  1424. if (!sync) {
  1425. pnfs_set_plh_return_info(
  1426. lo, skip_read ? IOMODE_RW : IOMODE_ANY, 0);
  1427. goto out_noroc;
  1428. }
  1429. spin_unlock(&ino->i_lock);
  1430. rcu_read_unlock();
  1431. wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1432. TASK_UNINTERRUPTIBLE);
  1433. pnfs_put_layout_hdr(lo);
  1434. goto retry;
  1435. }
  1436. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
  1437. if (skip_read && lseg->pls_range.iomode == IOMODE_READ)
  1438. continue;
  1439. /* If we are sending layoutreturn, invalidate all valid lsegs */
  1440. if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags))
  1441. continue;
  1442. /*
  1443. * Note: mark lseg for return so pnfs_layout_remove_lseg
  1444. * doesn't invalidate the layout for us.
  1445. */
  1446. set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
  1447. if (!mark_lseg_invalid(lseg, &lo->plh_return_segs))
  1448. continue;
  1449. pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
  1450. }
  1451. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  1452. goto out_noroc;
  1453. /* ROC in two conditions:
  1454. * 1. there are ROC lsegs
  1455. * 2. we don't send layoutreturn
  1456. */
  1457. /* lo ref dropped in pnfs_roc_release() */
  1458. layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &lc_cred, &iomode);
  1459. /* If the creds don't match, we can't compound the layoutreturn */
  1460. if (!layoutreturn || cred_fscmp(cred, lc_cred) != 0)
  1461. goto out_noroc;
  1462. roc = layoutreturn;
  1463. pnfs_init_layoutreturn_args(args, lo, &stateid, iomode);
  1464. res->lrs_present = 0;
  1465. layoutreturn = false;
  1466. put_cred(lc_cred);
  1467. out_noroc:
  1468. spin_unlock(&ino->i_lock);
  1469. rcu_read_unlock();
  1470. pnfs_layoutcommit_inode(ino, sync);
  1471. if (roc) {
  1472. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  1473. if (ld->prepare_layoutreturn)
  1474. ld->prepare_layoutreturn(args);
  1475. pnfs_put_layout_hdr(lo);
  1476. return true;
  1477. }
  1478. if (layoutreturn)
  1479. pnfs_send_layoutreturn(lo, &stateid, &lc_cred, iomode, 0);
  1480. pnfs_put_layout_hdr(lo);
  1481. return false;
  1482. }
  1483. int pnfs_roc_done(struct rpc_task *task, struct nfs4_layoutreturn_args **argpp,
  1484. struct nfs4_layoutreturn_res **respp, int *ret)
  1485. {
  1486. struct nfs4_layoutreturn_args *arg = *argpp;
  1487. int retval = -EAGAIN;
  1488. if (!arg)
  1489. return 0;
  1490. /* Handle Layoutreturn errors */
  1491. switch (*ret) {
  1492. case 0:
  1493. retval = 0;
  1494. break;
  1495. case -NFS4ERR_NOMATCHING_LAYOUT:
  1496. /* Was there an RPC level error? If not, retry */
  1497. if (task->tk_rpc_status == 0)
  1498. break;
  1499. /*
  1500. * Is there a fatal network level error?
  1501. * If so release the layout, but flag the error.
  1502. */
  1503. if ((task->tk_rpc_status == -ENETDOWN ||
  1504. task->tk_rpc_status == -ENETUNREACH) &&
  1505. task->tk_flags & RPC_TASK_NETUNREACH_FATAL) {
  1506. *ret = 0;
  1507. (*respp)->lrs_present = 0;
  1508. retval = -EIO;
  1509. break;
  1510. }
  1511. /* If the call was not sent, let caller handle it */
  1512. if (!RPC_WAS_SENT(task))
  1513. return 0;
  1514. /*
  1515. * Otherwise, assume the call succeeded and
  1516. * that we need to release the layout
  1517. */
  1518. *ret = 0;
  1519. (*respp)->lrs_present = 0;
  1520. retval = 0;
  1521. break;
  1522. case -NFS4ERR_DELAY:
  1523. /* Let the caller handle the retry */
  1524. *ret = -NFS4ERR_NOMATCHING_LAYOUT;
  1525. return 0;
  1526. case -NFS4ERR_OLD_STATEID:
  1527. if (!nfs4_layout_refresh_old_stateid(&arg->stateid,
  1528. &arg->range, arg->inode))
  1529. break;
  1530. *ret = -NFS4ERR_NOMATCHING_LAYOUT;
  1531. return -EAGAIN;
  1532. }
  1533. *argpp = NULL;
  1534. *respp = NULL;
  1535. return retval;
  1536. }
  1537. void pnfs_roc_release(struct nfs4_layoutreturn_args *args,
  1538. struct nfs4_layoutreturn_res *res, int ret)
  1539. {
  1540. struct pnfs_layout_hdr *lo = args->layout;
  1541. struct inode *inode = args->inode;
  1542. const nfs4_stateid *res_stateid = NULL;
  1543. struct nfs4_xdr_opaque_data *ld_private = args->ld_private;
  1544. LIST_HEAD(freeme);
  1545. switch (ret) {
  1546. case -NFS4ERR_BADSESSION:
  1547. case -NFS4ERR_DEADSESSION:
  1548. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1549. case -NFS4ERR_NOMATCHING_LAYOUT:
  1550. spin_lock(&inode->i_lock);
  1551. pnfs_layoutreturn_retry_later_locked(lo, &args->stateid,
  1552. &args->range, &freeme);
  1553. spin_unlock(&inode->i_lock);
  1554. pnfs_free_lseg_list(&freeme);
  1555. break;
  1556. case 0:
  1557. if (res->lrs_present)
  1558. res_stateid = &res->stateid;
  1559. fallthrough;
  1560. default:
  1561. pnfs_layoutreturn_free_lsegs(lo, &args->stateid, &args->range,
  1562. res_stateid);
  1563. }
  1564. trace_nfs4_layoutreturn_on_close(args->inode, &args->stateid, ret);
  1565. if (ld_private && ld_private->ops && ld_private->ops->free)
  1566. ld_private->ops->free(ld_private);
  1567. pnfs_put_layout_hdr(lo);
  1568. }
  1569. bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
  1570. {
  1571. struct nfs_inode *nfsi = NFS_I(ino);
  1572. struct pnfs_layout_hdr *lo;
  1573. bool sleep = false;
  1574. /* we might not have grabbed lo reference. so need to check under
  1575. * i_lock */
  1576. spin_lock(&ino->i_lock);
  1577. lo = nfsi->layout;
  1578. if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
  1579. rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
  1580. sleep = true;
  1581. }
  1582. spin_unlock(&ino->i_lock);
  1583. return sleep;
  1584. }
  1585. /*
  1586. * Compare two layout segments for sorting into layout cache.
  1587. * We want to preferentially return RW over RO layouts, so ensure those
  1588. * are seen first.
  1589. */
  1590. static s64
  1591. pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
  1592. const struct pnfs_layout_range *l2)
  1593. {
  1594. s64 d;
  1595. /* high offset > low offset */
  1596. d = l1->offset - l2->offset;
  1597. if (d)
  1598. return d;
  1599. /* short length > long length */
  1600. d = l2->length - l1->length;
  1601. if (d)
  1602. return d;
  1603. /* read > read/write */
  1604. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  1605. }
  1606. static bool
  1607. pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
  1608. const struct pnfs_layout_range *l2)
  1609. {
  1610. return pnfs_lseg_range_cmp(l1, l2) > 0;
  1611. }
  1612. static bool
  1613. pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
  1614. struct pnfs_layout_segment *old)
  1615. {
  1616. return false;
  1617. }
  1618. void
  1619. pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  1620. struct pnfs_layout_segment *lseg,
  1621. bool (*is_after)(const struct pnfs_layout_range *,
  1622. const struct pnfs_layout_range *),
  1623. bool (*do_merge)(struct pnfs_layout_segment *,
  1624. struct pnfs_layout_segment *),
  1625. struct list_head *free_me)
  1626. {
  1627. struct pnfs_layout_segment *lp, *tmp;
  1628. dprintk("%s:Begin\n", __func__);
  1629. list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
  1630. if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
  1631. continue;
  1632. if (do_merge(lseg, lp)) {
  1633. mark_lseg_invalid(lp, free_me);
  1634. continue;
  1635. }
  1636. if (is_after(&lseg->pls_range, &lp->pls_range))
  1637. continue;
  1638. list_add_tail(&lseg->pls_list, &lp->pls_list);
  1639. dprintk("%s: inserted lseg %p "
  1640. "iomode %d offset %llu length %llu before "
  1641. "lp %p iomode %d offset %llu length %llu\n",
  1642. __func__, lseg, lseg->pls_range.iomode,
  1643. lseg->pls_range.offset, lseg->pls_range.length,
  1644. lp, lp->pls_range.iomode, lp->pls_range.offset,
  1645. lp->pls_range.length);
  1646. goto out;
  1647. }
  1648. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  1649. dprintk("%s: inserted lseg %p "
  1650. "iomode %d offset %llu length %llu at tail\n",
  1651. __func__, lseg, lseg->pls_range.iomode,
  1652. lseg->pls_range.offset, lseg->pls_range.length);
  1653. out:
  1654. pnfs_get_layout_hdr(lo);
  1655. dprintk("%s:Return\n", __func__);
  1656. }
  1657. EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
  1658. static void
  1659. pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  1660. struct pnfs_layout_segment *lseg,
  1661. struct list_head *free_me)
  1662. {
  1663. struct inode *inode = lo->plh_inode;
  1664. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  1665. if (ld->add_lseg != NULL)
  1666. ld->add_lseg(lo, lseg, free_me);
  1667. else
  1668. pnfs_generic_layout_insert_lseg(lo, lseg,
  1669. pnfs_lseg_range_is_after,
  1670. pnfs_lseg_no_merge,
  1671. free_me);
  1672. }
  1673. static struct pnfs_layout_hdr *
  1674. alloc_init_layout_hdr(struct inode *ino,
  1675. struct nfs_open_context *ctx,
  1676. gfp_t gfp_flags)
  1677. {
  1678. struct pnfs_layout_hdr *lo;
  1679. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  1680. if (!lo)
  1681. return NULL;
  1682. refcount_set(&lo->plh_refcount, 1);
  1683. INIT_LIST_HEAD(&lo->plh_layouts);
  1684. INIT_LIST_HEAD(&lo->plh_segs);
  1685. INIT_LIST_HEAD(&lo->plh_return_segs);
  1686. INIT_LIST_HEAD(&lo->plh_bulk_destroy);
  1687. lo->plh_inode = ino;
  1688. lo->plh_lc_cred = get_cred(ctx->cred);
  1689. lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
  1690. return lo;
  1691. }
  1692. static struct pnfs_layout_hdr *
  1693. pnfs_find_alloc_layout(struct inode *ino,
  1694. struct nfs_open_context *ctx,
  1695. gfp_t gfp_flags)
  1696. __releases(&ino->i_lock)
  1697. __acquires(&ino->i_lock)
  1698. {
  1699. struct nfs_inode *nfsi = NFS_I(ino);
  1700. struct pnfs_layout_hdr *new = NULL;
  1701. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  1702. if (nfsi->layout != NULL)
  1703. goto out_existing;
  1704. spin_unlock(&ino->i_lock);
  1705. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  1706. spin_lock(&ino->i_lock);
  1707. if (likely(nfsi->layout == NULL)) { /* Won the race? */
  1708. nfsi->layout = new;
  1709. return new;
  1710. } else if (new != NULL)
  1711. pnfs_free_layout_hdr(new);
  1712. out_existing:
  1713. pnfs_get_layout_hdr(nfsi->layout);
  1714. return nfsi->layout;
  1715. }
  1716. /*
  1717. * iomode matching rules:
  1718. * iomode lseg strict match
  1719. * iomode
  1720. * ----- ----- ------ -----
  1721. * ANY READ N/A true
  1722. * ANY RW N/A true
  1723. * RW READ N/A false
  1724. * RW RW N/A true
  1725. * READ READ N/A true
  1726. * READ RW true false
  1727. * READ RW false true
  1728. */
  1729. static bool
  1730. pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
  1731. const struct pnfs_layout_range *range,
  1732. bool strict_iomode)
  1733. {
  1734. struct pnfs_layout_range range1;
  1735. if ((range->iomode == IOMODE_RW &&
  1736. ls_range->iomode != IOMODE_RW) ||
  1737. (range->iomode != ls_range->iomode &&
  1738. strict_iomode) ||
  1739. !pnfs_lseg_range_intersecting(ls_range, range))
  1740. return false;
  1741. /* range1 covers only the first byte in the range */
  1742. range1 = *range;
  1743. range1.length = 1;
  1744. return pnfs_lseg_range_contained(ls_range, &range1);
  1745. }
  1746. /*
  1747. * lookup range in layout
  1748. */
  1749. static struct pnfs_layout_segment *
  1750. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  1751. struct pnfs_layout_range *range,
  1752. bool strict_iomode)
  1753. {
  1754. struct pnfs_layout_segment *lseg, *ret = NULL;
  1755. dprintk("%s:Begin\n", __func__);
  1756. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  1757. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  1758. pnfs_lseg_range_match(&lseg->pls_range, range,
  1759. strict_iomode)) {
  1760. ret = pnfs_get_lseg(lseg);
  1761. break;
  1762. }
  1763. }
  1764. dprintk("%s:Return lseg %p ref %d\n",
  1765. __func__, ret, ret ? refcount_read(&ret->pls_refcount) : 0);
  1766. return ret;
  1767. }
  1768. /*
  1769. * Use mdsthreshold hints set at each OPEN to determine if I/O should go
  1770. * to the MDS or over pNFS
  1771. *
  1772. * The nfs_inode read_io and write_io fields are cumulative counters reset
  1773. * when there are no layout segments. Note that in pnfs_update_layout iomode
  1774. * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
  1775. * WRITE request.
  1776. *
  1777. * A return of true means use MDS I/O.
  1778. *
  1779. * From rfc 5661:
  1780. * If a file's size is smaller than the file size threshold, data accesses
  1781. * SHOULD be sent to the metadata server. If an I/O request has a length that
  1782. * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
  1783. * server. If both file size and I/O size are provided, the client SHOULD
  1784. * reach or exceed both thresholds before sending its read or write
  1785. * requests to the data server.
  1786. */
  1787. static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
  1788. struct inode *ino, int iomode)
  1789. {
  1790. struct nfs4_threshold *t = ctx->mdsthreshold;
  1791. struct nfs_inode *nfsi = NFS_I(ino);
  1792. loff_t fsize = i_size_read(ino);
  1793. bool size = false, size_set = false, io = false, io_set = false, ret = false;
  1794. if (t == NULL)
  1795. return ret;
  1796. dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
  1797. __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
  1798. switch (iomode) {
  1799. case IOMODE_READ:
  1800. if (t->bm & THRESHOLD_RD) {
  1801. dprintk("%s fsize %llu\n", __func__, fsize);
  1802. size_set = true;
  1803. if (fsize < t->rd_sz)
  1804. size = true;
  1805. }
  1806. if (t->bm & THRESHOLD_RD_IO) {
  1807. dprintk("%s nfsi->read_io %llu\n", __func__,
  1808. nfsi->read_io);
  1809. io_set = true;
  1810. if (nfsi->read_io < t->rd_io_sz)
  1811. io = true;
  1812. }
  1813. break;
  1814. case IOMODE_RW:
  1815. if (t->bm & THRESHOLD_WR) {
  1816. dprintk("%s fsize %llu\n", __func__, fsize);
  1817. size_set = true;
  1818. if (fsize < t->wr_sz)
  1819. size = true;
  1820. }
  1821. if (t->bm & THRESHOLD_WR_IO) {
  1822. dprintk("%s nfsi->write_io %llu\n", __func__,
  1823. nfsi->write_io);
  1824. io_set = true;
  1825. if (nfsi->write_io < t->wr_io_sz)
  1826. io = true;
  1827. }
  1828. break;
  1829. }
  1830. if (size_set && io_set) {
  1831. if (size && io)
  1832. ret = true;
  1833. } else if (size || io)
  1834. ret = true;
  1835. dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
  1836. return ret;
  1837. }
  1838. static int pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
  1839. {
  1840. /*
  1841. * send layoutcommit as it can hold up layoutreturn due to lseg
  1842. * reference
  1843. */
  1844. pnfs_layoutcommit_inode(lo->plh_inode, false);
  1845. return wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1846. nfs_wait_bit_killable,
  1847. TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
  1848. }
  1849. static void nfs_layoutget_begin(struct pnfs_layout_hdr *lo)
  1850. {
  1851. atomic_inc(&lo->plh_outstanding);
  1852. }
  1853. static void nfs_layoutget_end(struct pnfs_layout_hdr *lo)
  1854. {
  1855. if (atomic_dec_and_test(&lo->plh_outstanding) &&
  1856. test_and_clear_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags)) {
  1857. smp_mb__after_atomic();
  1858. wake_up_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN);
  1859. }
  1860. }
  1861. static bool pnfs_is_first_layoutget(struct pnfs_layout_hdr *lo)
  1862. {
  1863. return test_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags);
  1864. }
  1865. static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
  1866. {
  1867. unsigned long *bitlock = &lo->plh_flags;
  1868. clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
  1869. smp_mb__after_atomic();
  1870. wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
  1871. }
  1872. static void _add_to_server_list(struct pnfs_layout_hdr *lo,
  1873. struct nfs_server *server)
  1874. {
  1875. if (!test_and_set_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) {
  1876. struct nfs_client *clp = server->nfs_client;
  1877. /* The lo must be on the clp list if there is any
  1878. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  1879. */
  1880. spin_lock(&clp->cl_lock);
  1881. list_add_tail_rcu(&lo->plh_layouts, &server->layouts);
  1882. spin_unlock(&clp->cl_lock);
  1883. }
  1884. }
  1885. /*
  1886. * Layout segment is retreived from the server if not cached.
  1887. * The appropriate layout segment is referenced and returned to the caller.
  1888. */
  1889. struct pnfs_layout_segment *
  1890. pnfs_update_layout(struct inode *ino,
  1891. struct nfs_open_context *ctx,
  1892. loff_t pos,
  1893. u64 count,
  1894. enum pnfs_iomode iomode,
  1895. bool strict_iomode,
  1896. gfp_t gfp_flags)
  1897. {
  1898. struct pnfs_layout_range arg = {
  1899. .iomode = iomode,
  1900. .offset = pos,
  1901. .length = count,
  1902. };
  1903. unsigned pg_offset;
  1904. struct nfs_server *server = NFS_SERVER(ino);
  1905. struct nfs_client *clp = server->nfs_client;
  1906. struct pnfs_layout_hdr *lo = NULL;
  1907. struct pnfs_layout_segment *lseg = NULL;
  1908. struct nfs4_layoutget *lgp;
  1909. nfs4_stateid stateid;
  1910. struct nfs4_exception exception = {
  1911. .inode = ino,
  1912. };
  1913. unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
  1914. bool first;
  1915. if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
  1916. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1917. PNFS_UPDATE_LAYOUT_NO_PNFS);
  1918. goto out;
  1919. }
  1920. if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
  1921. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1922. PNFS_UPDATE_LAYOUT_MDSTHRESH);
  1923. goto out;
  1924. }
  1925. lookup_again:
  1926. if (!nfs4_valid_open_stateid(ctx->state)) {
  1927. trace_pnfs_update_layout(ino, pos, count,
  1928. iomode, lo, lseg,
  1929. PNFS_UPDATE_LAYOUT_INVALID_OPEN);
  1930. lseg = ERR_PTR(-EIO);
  1931. goto out;
  1932. }
  1933. lseg = ERR_PTR(nfs4_client_recover_expired_lease(clp));
  1934. if (IS_ERR(lseg))
  1935. goto out;
  1936. first = false;
  1937. spin_lock(&ino->i_lock);
  1938. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  1939. if (lo == NULL) {
  1940. spin_unlock(&ino->i_lock);
  1941. lseg = ERR_PTR(-ENOMEM);
  1942. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1943. PNFS_UPDATE_LAYOUT_NOMEM);
  1944. goto out;
  1945. }
  1946. /* Do we even need to bother with this? */
  1947. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1948. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1949. PNFS_UPDATE_LAYOUT_BULK_RECALL);
  1950. dprintk("%s matches recall, use MDS\n", __func__);
  1951. goto out_unlock;
  1952. }
  1953. /* if LAYOUTGET already failed once we don't try again */
  1954. if (pnfs_layout_io_test_failed(lo, iomode)) {
  1955. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1956. PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
  1957. goto out_unlock;
  1958. }
  1959. /*
  1960. * If the layout segment list is empty, but there are outstanding
  1961. * layoutget calls, then they might be subject to a layoutrecall.
  1962. */
  1963. if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
  1964. atomic_read(&lo->plh_outstanding) != 0) {
  1965. spin_unlock(&ino->i_lock);
  1966. lseg = ERR_PTR(wait_on_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN,
  1967. TASK_KILLABLE));
  1968. if (IS_ERR(lseg))
  1969. goto out_put_layout_hdr;
  1970. pnfs_put_layout_hdr(lo);
  1971. goto lookup_again;
  1972. }
  1973. /*
  1974. * Because we free lsegs when sending LAYOUTRETURN, we need to wait
  1975. * for LAYOUTRETURN.
  1976. */
  1977. if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
  1978. spin_unlock(&ino->i_lock);
  1979. dprintk("%s wait for layoutreturn\n", __func__);
  1980. lseg = ERR_PTR(pnfs_prepare_to_retry_layoutget(lo));
  1981. if (!IS_ERR(lseg)) {
  1982. pnfs_put_layout_hdr(lo);
  1983. dprintk("%s retrying\n", __func__);
  1984. trace_pnfs_update_layout(ino, pos, count, iomode, lo,
  1985. lseg,
  1986. PNFS_UPDATE_LAYOUT_RETRY);
  1987. goto lookup_again;
  1988. }
  1989. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1990. PNFS_UPDATE_LAYOUT_RETURN);
  1991. goto out_put_layout_hdr;
  1992. }
  1993. lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
  1994. if (lseg) {
  1995. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1996. PNFS_UPDATE_LAYOUT_FOUND_CACHED);
  1997. goto out_unlock;
  1998. }
  1999. /*
  2000. * Choose a stateid for the LAYOUTGET. If we don't have a layout
  2001. * stateid, or it has been invalidated, then we must use the open
  2002. * stateid.
  2003. */
  2004. if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
  2005. int status;
  2006. /*
  2007. * The first layoutget for the file. Need to serialize per
  2008. * RFC 5661 Errata 3208.
  2009. */
  2010. if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
  2011. &lo->plh_flags)) {
  2012. spin_unlock(&ino->i_lock);
  2013. lseg = ERR_PTR(wait_on_bit(&lo->plh_flags,
  2014. NFS_LAYOUT_FIRST_LAYOUTGET,
  2015. TASK_KILLABLE));
  2016. if (IS_ERR(lseg))
  2017. goto out_put_layout_hdr;
  2018. pnfs_put_layout_hdr(lo);
  2019. dprintk("%s retrying\n", __func__);
  2020. goto lookup_again;
  2021. }
  2022. spin_unlock(&ino->i_lock);
  2023. first = true;
  2024. status = nfs4_select_rw_stateid(ctx->state,
  2025. iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ,
  2026. NULL, &stateid, NULL);
  2027. if (status != 0) {
  2028. lseg = ERR_PTR(status);
  2029. trace_pnfs_update_layout(ino, pos, count,
  2030. iomode, lo, lseg,
  2031. PNFS_UPDATE_LAYOUT_INVALID_OPEN);
  2032. nfs4_schedule_stateid_recovery(server, ctx->state);
  2033. pnfs_clear_first_layoutget(lo);
  2034. pnfs_put_layout_hdr(lo);
  2035. goto lookup_again;
  2036. }
  2037. spin_lock(&ino->i_lock);
  2038. } else {
  2039. nfs4_stateid_copy(&stateid, &lo->plh_stateid);
  2040. }
  2041. if (pnfs_layoutgets_blocked(lo)) {
  2042. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  2043. PNFS_UPDATE_LAYOUT_BLOCKED);
  2044. goto out_unlock;
  2045. }
  2046. nfs_layoutget_begin(lo);
  2047. spin_unlock(&ino->i_lock);
  2048. _add_to_server_list(lo, server);
  2049. pg_offset = arg.offset & ~PAGE_MASK;
  2050. if (pg_offset) {
  2051. arg.offset -= pg_offset;
  2052. arg.length += pg_offset;
  2053. }
  2054. if (arg.length != NFS4_MAX_UINT64)
  2055. arg.length = PAGE_ALIGN(arg.length);
  2056. lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &stateid, &arg, gfp_flags);
  2057. if (!lgp) {
  2058. lseg = ERR_PTR(-ENOMEM);
  2059. trace_pnfs_update_layout(ino, pos, count, iomode, lo, NULL,
  2060. PNFS_UPDATE_LAYOUT_NOMEM);
  2061. nfs_layoutget_end(lo);
  2062. goto out_put_layout_hdr;
  2063. }
  2064. lgp->lo = lo;
  2065. pnfs_get_layout_hdr(lo);
  2066. lseg = nfs4_proc_layoutget(lgp, &exception);
  2067. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  2068. PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
  2069. nfs_layoutget_end(lo);
  2070. if (IS_ERR(lseg)) {
  2071. switch(PTR_ERR(lseg)) {
  2072. case -EBUSY:
  2073. if (time_after(jiffies, giveup))
  2074. lseg = NULL;
  2075. break;
  2076. case -ERECALLCONFLICT:
  2077. case -EAGAIN:
  2078. break;
  2079. case -ENODATA:
  2080. /* The server returned NFS4ERR_LAYOUTUNAVAILABLE */
  2081. pnfs_layout_set_fail_bit(
  2082. lo, pnfs_iomode_to_fail_bit(iomode));
  2083. lseg = NULL;
  2084. goto out_put_layout_hdr;
  2085. default:
  2086. if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
  2087. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  2088. lseg = NULL;
  2089. }
  2090. goto out_put_layout_hdr;
  2091. }
  2092. if (lseg) {
  2093. if (!exception.retry)
  2094. goto out_put_layout_hdr;
  2095. if (first)
  2096. pnfs_clear_first_layoutget(lo);
  2097. trace_pnfs_update_layout(ino, pos, count,
  2098. iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
  2099. pnfs_put_layout_hdr(lo);
  2100. goto lookup_again;
  2101. }
  2102. } else {
  2103. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  2104. }
  2105. out_put_layout_hdr:
  2106. if (first)
  2107. pnfs_clear_first_layoutget(lo);
  2108. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  2109. PNFS_UPDATE_LAYOUT_EXIT);
  2110. pnfs_put_layout_hdr(lo);
  2111. out:
  2112. dprintk("%s: inode %s/%llu pNFS layout segment %s for "
  2113. "(%s, offset: %llu, length: %llu)\n",
  2114. __func__, ino->i_sb->s_id,
  2115. (unsigned long long)NFS_FILEID(ino),
  2116. IS_ERR_OR_NULL(lseg) ? "not found" : "found",
  2117. iomode==IOMODE_RW ? "read/write" : "read-only",
  2118. (unsigned long long)pos,
  2119. (unsigned long long)count);
  2120. return lseg;
  2121. out_unlock:
  2122. spin_unlock(&ino->i_lock);
  2123. goto out_put_layout_hdr;
  2124. }
  2125. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  2126. static bool
  2127. pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
  2128. {
  2129. switch (range->iomode) {
  2130. case IOMODE_READ:
  2131. case IOMODE_RW:
  2132. break;
  2133. default:
  2134. return false;
  2135. }
  2136. if (range->offset == NFS4_MAX_UINT64)
  2137. return false;
  2138. if (range->length == 0)
  2139. return false;
  2140. if (range->length != NFS4_MAX_UINT64 &&
  2141. range->length > NFS4_MAX_UINT64 - range->offset)
  2142. return false;
  2143. return true;
  2144. }
  2145. static struct pnfs_layout_hdr *
  2146. _pnfs_grab_empty_layout(struct inode *ino, struct nfs_open_context *ctx)
  2147. {
  2148. struct pnfs_layout_hdr *lo;
  2149. spin_lock(&ino->i_lock);
  2150. lo = pnfs_find_alloc_layout(ino, ctx, nfs_io_gfp_mask());
  2151. if (!lo)
  2152. goto out_unlock;
  2153. if (!test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags))
  2154. goto out_unlock;
  2155. if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
  2156. goto out_unlock;
  2157. if (pnfs_layoutgets_blocked(lo))
  2158. goto out_unlock;
  2159. if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags))
  2160. goto out_unlock;
  2161. nfs_layoutget_begin(lo);
  2162. spin_unlock(&ino->i_lock);
  2163. _add_to_server_list(lo, NFS_SERVER(ino));
  2164. return lo;
  2165. out_unlock:
  2166. spin_unlock(&ino->i_lock);
  2167. pnfs_put_layout_hdr(lo);
  2168. return NULL;
  2169. }
  2170. static void _lgopen_prepare_attached(struct nfs4_opendata *data,
  2171. struct nfs_open_context *ctx)
  2172. {
  2173. struct inode *ino = data->dentry->d_inode;
  2174. struct pnfs_layout_range rng = {
  2175. .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
  2176. IOMODE_RW: IOMODE_READ,
  2177. .offset = 0,
  2178. .length = NFS4_MAX_UINT64,
  2179. };
  2180. struct nfs4_layoutget *lgp;
  2181. struct pnfs_layout_hdr *lo;
  2182. /* Heuristic: don't send layoutget if we have cached data */
  2183. if (rng.iomode == IOMODE_READ &&
  2184. (i_size_read(ino) == 0 || ino->i_mapping->nrpages != 0))
  2185. return;
  2186. lo = _pnfs_grab_empty_layout(ino, ctx);
  2187. if (!lo)
  2188. return;
  2189. lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &current_stateid, &rng,
  2190. nfs_io_gfp_mask());
  2191. if (!lgp) {
  2192. pnfs_clear_first_layoutget(lo);
  2193. nfs_layoutget_end(lo);
  2194. pnfs_put_layout_hdr(lo);
  2195. return;
  2196. }
  2197. lgp->lo = lo;
  2198. data->lgp = lgp;
  2199. data->o_arg.lg_args = &lgp->args;
  2200. data->o_res.lg_res = &lgp->res;
  2201. }
  2202. static void _lgopen_prepare_floating(struct nfs4_opendata *data,
  2203. struct nfs_open_context *ctx)
  2204. {
  2205. struct inode *ino = data->dentry->d_inode;
  2206. struct pnfs_layout_range rng = {
  2207. .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
  2208. IOMODE_RW: IOMODE_READ,
  2209. .offset = 0,
  2210. .length = NFS4_MAX_UINT64,
  2211. };
  2212. struct nfs4_layoutget *lgp;
  2213. lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &current_stateid, &rng,
  2214. nfs_io_gfp_mask());
  2215. if (!lgp)
  2216. return;
  2217. data->lgp = lgp;
  2218. data->o_arg.lg_args = &lgp->args;
  2219. data->o_res.lg_res = &lgp->res;
  2220. }
  2221. void pnfs_lgopen_prepare(struct nfs4_opendata *data,
  2222. struct nfs_open_context *ctx)
  2223. {
  2224. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  2225. if (!(pnfs_enabled_sb(server) &&
  2226. server->pnfs_curr_ld->flags & PNFS_LAYOUTGET_ON_OPEN))
  2227. return;
  2228. /* Could check on max_ops, but currently hardcoded high enough */
  2229. if (!nfs_server_capable(data->dir->d_inode, NFS_CAP_LGOPEN))
  2230. return;
  2231. if (data->lgp)
  2232. return;
  2233. if (data->state)
  2234. _lgopen_prepare_attached(data, ctx);
  2235. else
  2236. _lgopen_prepare_floating(data, ctx);
  2237. }
  2238. void pnfs_parse_lgopen(struct inode *ino, struct nfs4_layoutget *lgp,
  2239. struct nfs_open_context *ctx)
  2240. {
  2241. struct pnfs_layout_hdr *lo;
  2242. struct pnfs_layout_segment *lseg;
  2243. struct nfs_server *srv = NFS_SERVER(ino);
  2244. u32 iomode;
  2245. if (!lgp)
  2246. return;
  2247. dprintk("%s: entered with status %i\n", __func__, lgp->res.status);
  2248. if (lgp->res.status) {
  2249. switch (lgp->res.status) {
  2250. default:
  2251. break;
  2252. /*
  2253. * Halt lgopen attempts if the server doesn't recognise
  2254. * the "current stateid" value, the layout type, or the
  2255. * layoutget operation as being valid.
  2256. * Also if it complains about too many ops in the compound
  2257. * or of the request/reply being too big.
  2258. */
  2259. case -NFS4ERR_BAD_STATEID:
  2260. case -NFS4ERR_NOTSUPP:
  2261. case -NFS4ERR_REP_TOO_BIG:
  2262. case -NFS4ERR_REP_TOO_BIG_TO_CACHE:
  2263. case -NFS4ERR_REQ_TOO_BIG:
  2264. case -NFS4ERR_TOO_MANY_OPS:
  2265. case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
  2266. srv->caps &= ~NFS_CAP_LGOPEN;
  2267. }
  2268. return;
  2269. }
  2270. if (!lgp->lo) {
  2271. lo = _pnfs_grab_empty_layout(ino, ctx);
  2272. if (!lo)
  2273. return;
  2274. lgp->lo = lo;
  2275. } else
  2276. lo = lgp->lo;
  2277. lseg = pnfs_layout_process(lgp);
  2278. if (!IS_ERR(lseg)) {
  2279. iomode = lgp->args.range.iomode;
  2280. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  2281. pnfs_put_lseg(lseg);
  2282. }
  2283. }
  2284. void nfs4_lgopen_release(struct nfs4_layoutget *lgp)
  2285. {
  2286. if (lgp != NULL) {
  2287. if (lgp->lo) {
  2288. pnfs_clear_first_layoutget(lgp->lo);
  2289. nfs_layoutget_end(lgp->lo);
  2290. }
  2291. pnfs_layoutget_free(lgp);
  2292. }
  2293. }
  2294. struct pnfs_layout_segment *
  2295. pnfs_layout_process(struct nfs4_layoutget *lgp)
  2296. {
  2297. struct pnfs_layout_hdr *lo = lgp->lo;
  2298. struct nfs4_layoutget_res *res = &lgp->res;
  2299. struct pnfs_layout_segment *lseg;
  2300. struct inode *ino = lo->plh_inode;
  2301. LIST_HEAD(free_me);
  2302. if (!pnfs_sanity_check_layout_range(&res->range))
  2303. return ERR_PTR(-EINVAL);
  2304. /* Inject layout blob into I/O device driver */
  2305. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  2306. if (IS_ERR_OR_NULL(lseg)) {
  2307. if (!lseg)
  2308. lseg = ERR_PTR(-ENOMEM);
  2309. dprintk("%s: Could not allocate layout: error %ld\n",
  2310. __func__, PTR_ERR(lseg));
  2311. return lseg;
  2312. }
  2313. pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
  2314. spin_lock(&ino->i_lock);
  2315. if (pnfs_layoutgets_blocked(lo)) {
  2316. dprintk("%s forget reply due to state\n", __func__);
  2317. goto out_forget;
  2318. }
  2319. if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
  2320. !pnfs_is_first_layoutget(lo))
  2321. goto out_forget;
  2322. if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
  2323. /* existing state ID, make sure the sequence number matches. */
  2324. if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
  2325. if (!pnfs_layout_is_valid(lo))
  2326. lo->plh_barrier = 0;
  2327. dprintk("%s forget reply due to sequence\n", __func__);
  2328. goto out_forget;
  2329. }
  2330. pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, false);
  2331. } else if (pnfs_layout_is_valid(lo)) {
  2332. /*
  2333. * We got an entirely new state ID. Mark all segments for the
  2334. * inode invalid, and retry the layoutget
  2335. */
  2336. struct pnfs_layout_range range = {
  2337. .iomode = IOMODE_ANY,
  2338. .length = NFS4_MAX_UINT64,
  2339. };
  2340. pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0);
  2341. goto out_forget;
  2342. } else {
  2343. /* We have a completely new layout */
  2344. pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, true);
  2345. }
  2346. pnfs_get_lseg(lseg);
  2347. pnfs_layout_insert_lseg(lo, lseg, &free_me);
  2348. if (res->return_on_close)
  2349. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  2350. spin_unlock(&ino->i_lock);
  2351. pnfs_free_lseg_list(&free_me);
  2352. return lseg;
  2353. out_forget:
  2354. spin_unlock(&ino->i_lock);
  2355. lseg->pls_layout = lo;
  2356. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  2357. return ERR_PTR(-EAGAIN);
  2358. }
  2359. /**
  2360. * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
  2361. * @lo: pointer to layout header
  2362. * @tmp_list: list header to be used with pnfs_free_lseg_list()
  2363. * @return_range: describe layout segment ranges to be returned
  2364. * @seq: stateid seqid to match
  2365. *
  2366. * This function is mainly intended for use by layoutrecall. It attempts
  2367. * to free the layout segment immediately, or else to mark it for return
  2368. * as soon as its reference count drops to zero.
  2369. *
  2370. * Returns
  2371. * - 0: a layoutreturn needs to be scheduled.
  2372. * - EBUSY: there are layout segment that are still in use.
  2373. * - ENOENT: there are no layout segments that need to be returned.
  2374. */
  2375. int
  2376. pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
  2377. struct list_head *tmp_list,
  2378. const struct pnfs_layout_range *return_range,
  2379. u32 seq)
  2380. {
  2381. struct pnfs_layout_segment *lseg, *next;
  2382. struct nfs_server *server = NFS_SERVER(lo->plh_inode);
  2383. int remaining = 0;
  2384. dprintk("%s:Begin lo %p\n", __func__, lo);
  2385. assert_spin_locked(&lo->plh_inode->i_lock);
  2386. if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  2387. tmp_list = &lo->plh_return_segs;
  2388. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  2389. if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
  2390. dprintk("%s: marking lseg %p iomode %d "
  2391. "offset %llu length %llu\n", __func__,
  2392. lseg, lseg->pls_range.iomode,
  2393. lseg->pls_range.offset,
  2394. lseg->pls_range.length);
  2395. if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
  2396. tmp_list = &lo->plh_return_segs;
  2397. if (mark_lseg_invalid(lseg, tmp_list))
  2398. continue;
  2399. remaining++;
  2400. set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
  2401. pnfs_lseg_cancel_io(server, lseg);
  2402. }
  2403. if (remaining) {
  2404. pnfs_set_plh_return_info(lo, return_range->iomode, seq);
  2405. return -EBUSY;
  2406. }
  2407. if (!list_empty(&lo->plh_return_segs)) {
  2408. pnfs_set_plh_return_info(lo, return_range->iomode, seq);
  2409. return 0;
  2410. }
  2411. return -ENOENT;
  2412. }
  2413. static void
  2414. pnfs_mark_layout_for_return(struct inode *inode,
  2415. const struct pnfs_layout_range *range)
  2416. {
  2417. struct pnfs_layout_hdr *lo;
  2418. bool return_now = false;
  2419. spin_lock(&inode->i_lock);
  2420. lo = NFS_I(inode)->layout;
  2421. if (!pnfs_layout_is_valid(lo)) {
  2422. spin_unlock(&inode->i_lock);
  2423. return;
  2424. }
  2425. pnfs_set_plh_return_info(lo, range->iomode, 0);
  2426. /*
  2427. * mark all matching lsegs so that we are sure to have no live
  2428. * segments at hand when sending layoutreturn. See pnfs_put_lseg()
  2429. * for how it works.
  2430. */
  2431. if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, range, 0) != -EBUSY) {
  2432. const struct cred *cred;
  2433. nfs4_stateid stateid;
  2434. enum pnfs_iomode iomode;
  2435. return_now = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode);
  2436. spin_unlock(&inode->i_lock);
  2437. if (return_now)
  2438. pnfs_send_layoutreturn(lo, &stateid, &cred, iomode,
  2439. PNFS_FL_LAYOUTRETURN_ASYNC);
  2440. } else {
  2441. spin_unlock(&inode->i_lock);
  2442. nfs_commit_inode(inode, 0);
  2443. }
  2444. }
  2445. void pnfs_error_mark_layout_for_return(struct inode *inode,
  2446. struct pnfs_layout_segment *lseg)
  2447. {
  2448. struct pnfs_layout_range range = {
  2449. .iomode = lseg->pls_range.iomode,
  2450. .offset = 0,
  2451. .length = NFS4_MAX_UINT64,
  2452. };
  2453. pnfs_mark_layout_for_return(inode, &range);
  2454. }
  2455. EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
  2456. static bool
  2457. pnfs_layout_can_be_returned(struct pnfs_layout_hdr *lo)
  2458. {
  2459. return pnfs_layout_is_valid(lo) &&
  2460. !test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) &&
  2461. !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
  2462. }
  2463. static struct pnfs_layout_segment *
  2464. pnfs_find_first_lseg(struct pnfs_layout_hdr *lo,
  2465. const struct pnfs_layout_range *range,
  2466. enum pnfs_iomode iomode)
  2467. {
  2468. struct pnfs_layout_segment *lseg;
  2469. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  2470. if (!test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
  2471. continue;
  2472. if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
  2473. continue;
  2474. if (lseg->pls_range.iomode != iomode && iomode != IOMODE_ANY)
  2475. continue;
  2476. if (pnfs_lseg_range_intersecting(&lseg->pls_range, range))
  2477. return lseg;
  2478. }
  2479. return NULL;
  2480. }
  2481. /* Find open file states whose mode matches that of the range */
  2482. static bool
  2483. pnfs_should_return_unused_layout(struct pnfs_layout_hdr *lo,
  2484. const struct pnfs_layout_range *range)
  2485. {
  2486. struct list_head *head;
  2487. struct nfs_open_context *ctx;
  2488. fmode_t mode = 0;
  2489. if (!pnfs_layout_can_be_returned(lo) ||
  2490. !pnfs_find_first_lseg(lo, range, range->iomode))
  2491. return false;
  2492. head = &NFS_I(lo->plh_inode)->open_files;
  2493. list_for_each_entry_rcu(ctx, head, list) {
  2494. if (ctx->state)
  2495. mode |= ctx->state->state & (FMODE_READ|FMODE_WRITE);
  2496. }
  2497. switch (range->iomode) {
  2498. default:
  2499. break;
  2500. case IOMODE_READ:
  2501. mode &= ~FMODE_WRITE;
  2502. break;
  2503. case IOMODE_RW:
  2504. if (pnfs_find_first_lseg(lo, range, IOMODE_READ))
  2505. mode &= ~FMODE_READ;
  2506. }
  2507. return mode == 0;
  2508. }
  2509. static int pnfs_layout_return_unused_byserver(struct nfs_server *server,
  2510. void *data)
  2511. {
  2512. const struct pnfs_layout_range *range = data;
  2513. const struct cred *cred;
  2514. struct pnfs_layout_hdr *lo;
  2515. struct inode *inode;
  2516. nfs4_stateid stateid;
  2517. enum pnfs_iomode iomode;
  2518. restart:
  2519. rcu_read_lock();
  2520. list_for_each_entry_rcu(lo, &server->layouts, plh_layouts) {
  2521. inode = lo->plh_inode;
  2522. if (!inode || !pnfs_layout_can_be_returned(lo) ||
  2523. test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  2524. continue;
  2525. spin_lock(&inode->i_lock);
  2526. if (!lo->plh_inode ||
  2527. !pnfs_should_return_unused_layout(lo, range)) {
  2528. spin_unlock(&inode->i_lock);
  2529. continue;
  2530. }
  2531. pnfs_get_layout_hdr(lo);
  2532. pnfs_set_plh_return_info(lo, range->iomode, 0);
  2533. if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
  2534. range, 0) != 0 ||
  2535. !pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode)) {
  2536. spin_unlock(&inode->i_lock);
  2537. rcu_read_unlock();
  2538. pnfs_put_layout_hdr(lo);
  2539. cond_resched();
  2540. goto restart;
  2541. }
  2542. spin_unlock(&inode->i_lock);
  2543. rcu_read_unlock();
  2544. pnfs_send_layoutreturn(lo, &stateid, &cred, iomode,
  2545. PNFS_FL_LAYOUTRETURN_ASYNC);
  2546. pnfs_put_layout_hdr(lo);
  2547. cond_resched();
  2548. goto restart;
  2549. }
  2550. rcu_read_unlock();
  2551. return 0;
  2552. }
  2553. void
  2554. pnfs_layout_return_unused_byclid(struct nfs_client *clp,
  2555. enum pnfs_iomode iomode)
  2556. {
  2557. struct pnfs_layout_range range = {
  2558. .iomode = iomode,
  2559. .offset = 0,
  2560. .length = NFS4_MAX_UINT64,
  2561. };
  2562. nfs_client_for_each_server(clp, pnfs_layout_return_unused_byserver,
  2563. &range);
  2564. }
  2565. /* Check if we have we have a valid layout but if there isn't an intersection
  2566. * between the request and the pgio->pg_lseg, put this pgio->pg_lseg away.
  2567. */
  2568. void
  2569. pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio,
  2570. struct nfs_page *req)
  2571. {
  2572. if (pgio->pg_lseg == NULL ||
  2573. (test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags) &&
  2574. pnfs_lseg_request_intersecting(pgio->pg_lseg, req)))
  2575. return;
  2576. pnfs_put_lseg(pgio->pg_lseg);
  2577. pgio->pg_lseg = NULL;
  2578. }
  2579. EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
  2580. void
  2581. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  2582. {
  2583. u64 rd_size;
  2584. pnfs_generic_pg_check_layout(pgio, req);
  2585. if (pgio->pg_lseg == NULL) {
  2586. if (pgio->pg_dreq == NULL)
  2587. rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
  2588. else
  2589. rd_size = nfs_dreq_bytes_left(pgio->pg_dreq,
  2590. req_offset(req));
  2591. pgio->pg_lseg =
  2592. pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
  2593. req_offset(req), rd_size,
  2594. IOMODE_READ, false,
  2595. nfs_io_gfp_mask());
  2596. if (IS_ERR(pgio->pg_lseg)) {
  2597. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  2598. pgio->pg_lseg = NULL;
  2599. return;
  2600. }
  2601. }
  2602. /* If no lseg, fall back to read through mds */
  2603. if (pgio->pg_lseg == NULL)
  2604. nfs_pageio_reset_read_mds(pgio);
  2605. }
  2606. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  2607. void
  2608. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
  2609. struct nfs_page *req, u64 wb_size)
  2610. {
  2611. pnfs_generic_pg_check_layout(pgio, req);
  2612. if (pgio->pg_lseg == NULL) {
  2613. pgio->pg_lseg =
  2614. pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
  2615. req_offset(req), wb_size, IOMODE_RW,
  2616. false, nfs_io_gfp_mask());
  2617. if (IS_ERR(pgio->pg_lseg)) {
  2618. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  2619. pgio->pg_lseg = NULL;
  2620. return;
  2621. }
  2622. }
  2623. /* If no lseg, fall back to write through mds */
  2624. if (pgio->pg_lseg == NULL)
  2625. nfs_pageio_reset_write_mds(pgio);
  2626. }
  2627. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  2628. void
  2629. pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
  2630. {
  2631. if (desc->pg_lseg) {
  2632. pnfs_put_lseg(desc->pg_lseg);
  2633. desc->pg_lseg = NULL;
  2634. }
  2635. }
  2636. EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
  2637. /*
  2638. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  2639. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  2640. */
  2641. size_t
  2642. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
  2643. struct nfs_page *prev, struct nfs_page *req)
  2644. {
  2645. unsigned int size;
  2646. u64 seg_end, req_start, seg_left;
  2647. size = nfs_generic_pg_test(pgio, prev, req);
  2648. if (!size)
  2649. return 0;
  2650. /*
  2651. * 'size' contains the number of bytes left in the current page (up
  2652. * to the original size asked for in @req->wb_bytes).
  2653. *
  2654. * Calculate how many bytes are left in the layout segment
  2655. * and if there are less bytes than 'size', return that instead.
  2656. *
  2657. * Please also note that 'end_offset' is actually the offset of the
  2658. * first byte that lies outside the pnfs_layout_range. FIXME?
  2659. *
  2660. */
  2661. if (pgio->pg_lseg) {
  2662. seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
  2663. pgio->pg_lseg->pls_range.length);
  2664. req_start = req_offset(req);
  2665. /* start of request is past the last byte of this segment */
  2666. if (req_start >= seg_end)
  2667. return 0;
  2668. /* adjust 'size' iff there are fewer bytes left in the
  2669. * segment than what nfs_generic_pg_test returned */
  2670. seg_left = seg_end - req_start;
  2671. if (seg_left < size)
  2672. size = (unsigned int)seg_left;
  2673. }
  2674. return size;
  2675. }
  2676. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  2677. int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
  2678. {
  2679. struct nfs_pageio_descriptor pgio;
  2680. /* Resend all requests through the MDS */
  2681. nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
  2682. hdr->completion_ops);
  2683. return nfs_pageio_resend(&pgio, hdr);
  2684. }
  2685. EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
  2686. static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
  2687. {
  2688. dprintk("pnfs write error = %d\n", hdr->pnfs_error);
  2689. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  2690. PNFS_LAYOUTRET_ON_ERROR) {
  2691. pnfs_return_layout(hdr->inode);
  2692. }
  2693. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  2694. hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
  2695. }
  2696. /*
  2697. * Called by non rpc-based layout drivers
  2698. */
  2699. void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
  2700. {
  2701. if (likely(!hdr->pnfs_error)) {
  2702. pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
  2703. hdr->mds_offset + hdr->res.count);
  2704. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  2705. }
  2706. trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
  2707. if (unlikely(hdr->pnfs_error))
  2708. pnfs_ld_handle_write_error(hdr);
  2709. hdr->mds_ops->rpc_release(hdr);
  2710. }
  2711. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  2712. static void
  2713. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  2714. struct nfs_pgio_header *hdr)
  2715. {
  2716. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2717. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2718. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  2719. nfs_pageio_reset_write_mds(desc);
  2720. mirror->pg_recoalesce = 1;
  2721. }
  2722. hdr->completion_ops->completion(hdr);
  2723. }
  2724. static enum pnfs_try_status
  2725. pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
  2726. const struct rpc_call_ops *call_ops,
  2727. struct pnfs_layout_segment *lseg,
  2728. int how)
  2729. {
  2730. struct inode *inode = hdr->inode;
  2731. enum pnfs_try_status trypnfs;
  2732. struct nfs_server *nfss = NFS_SERVER(inode);
  2733. hdr->mds_ops = call_ops;
  2734. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  2735. inode->i_ino, hdr->args.count, hdr->args.offset, how);
  2736. trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
  2737. if (trypnfs != PNFS_NOT_ATTEMPTED)
  2738. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  2739. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  2740. return trypnfs;
  2741. }
  2742. static void
  2743. pnfs_do_write(struct nfs_pageio_descriptor *desc,
  2744. struct nfs_pgio_header *hdr, int how)
  2745. {
  2746. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  2747. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  2748. enum pnfs_try_status trypnfs;
  2749. trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
  2750. switch (trypnfs) {
  2751. case PNFS_NOT_ATTEMPTED:
  2752. pnfs_write_through_mds(desc, hdr);
  2753. break;
  2754. case PNFS_ATTEMPTED:
  2755. break;
  2756. case PNFS_TRY_AGAIN:
  2757. /* cleanup hdr and prepare to redo pnfs */
  2758. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2759. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2760. list_splice_init(&hdr->pages, &mirror->pg_list);
  2761. mirror->pg_recoalesce = 1;
  2762. }
  2763. hdr->mds_ops->rpc_release(hdr);
  2764. }
  2765. }
  2766. static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
  2767. {
  2768. pnfs_put_lseg(hdr->lseg);
  2769. nfs_pgio_header_free(hdr);
  2770. }
  2771. int
  2772. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  2773. {
  2774. struct nfs_pgio_header *hdr;
  2775. int ret;
  2776. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  2777. if (!hdr) {
  2778. desc->pg_error = -ENOMEM;
  2779. return desc->pg_error;
  2780. }
  2781. nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
  2782. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  2783. ret = nfs_generic_pgio(desc, hdr);
  2784. if (!ret)
  2785. pnfs_do_write(desc, hdr, desc->pg_ioflags);
  2786. return ret;
  2787. }
  2788. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  2789. int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
  2790. {
  2791. struct nfs_pageio_descriptor pgio;
  2792. /* Resend all requests through the MDS */
  2793. nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
  2794. return nfs_pageio_resend(&pgio, hdr);
  2795. }
  2796. EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
  2797. static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
  2798. {
  2799. dprintk("pnfs read error = %d\n", hdr->pnfs_error);
  2800. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  2801. PNFS_LAYOUTRET_ON_ERROR) {
  2802. pnfs_return_layout(hdr->inode);
  2803. }
  2804. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  2805. hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
  2806. }
  2807. /*
  2808. * Called by non rpc-based layout drivers
  2809. */
  2810. void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
  2811. {
  2812. if (likely(!hdr->pnfs_error))
  2813. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  2814. trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
  2815. if (unlikely(hdr->pnfs_error))
  2816. pnfs_ld_handle_read_error(hdr);
  2817. hdr->mds_ops->rpc_release(hdr);
  2818. }
  2819. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  2820. static void
  2821. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  2822. struct nfs_pgio_header *hdr)
  2823. {
  2824. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2825. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2826. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  2827. nfs_pageio_reset_read_mds(desc);
  2828. mirror->pg_recoalesce = 1;
  2829. }
  2830. hdr->completion_ops->completion(hdr);
  2831. }
  2832. /*
  2833. * Call the appropriate parallel I/O subsystem read function.
  2834. */
  2835. static enum pnfs_try_status
  2836. pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
  2837. const struct rpc_call_ops *call_ops,
  2838. struct pnfs_layout_segment *lseg)
  2839. {
  2840. struct inode *inode = hdr->inode;
  2841. struct nfs_server *nfss = NFS_SERVER(inode);
  2842. enum pnfs_try_status trypnfs;
  2843. hdr->mds_ops = call_ops;
  2844. dprintk("%s: Reading ino:%lu %u@%llu\n",
  2845. __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
  2846. trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
  2847. if (trypnfs != PNFS_NOT_ATTEMPTED)
  2848. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  2849. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  2850. return trypnfs;
  2851. }
  2852. /* Resend all requests through pnfs. */
  2853. void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr,
  2854. unsigned int mirror_idx)
  2855. {
  2856. struct nfs_pageio_descriptor pgio;
  2857. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2858. /* Prevent deadlocks with layoutreturn! */
  2859. pnfs_put_lseg(hdr->lseg);
  2860. hdr->lseg = NULL;
  2861. nfs_pageio_init_read(&pgio, hdr->inode, false,
  2862. hdr->completion_ops);
  2863. pgio.pg_mirror_idx = mirror_idx;
  2864. hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
  2865. }
  2866. }
  2867. EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
  2868. static void
  2869. pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
  2870. {
  2871. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  2872. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  2873. enum pnfs_try_status trypnfs;
  2874. trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
  2875. switch (trypnfs) {
  2876. case PNFS_NOT_ATTEMPTED:
  2877. pnfs_read_through_mds(desc, hdr);
  2878. break;
  2879. case PNFS_ATTEMPTED:
  2880. break;
  2881. case PNFS_TRY_AGAIN:
  2882. /* cleanup hdr and prepare to redo pnfs */
  2883. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2884. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2885. list_splice_init(&hdr->pages, &mirror->pg_list);
  2886. mirror->pg_recoalesce = 1;
  2887. }
  2888. hdr->mds_ops->rpc_release(hdr);
  2889. }
  2890. }
  2891. static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
  2892. {
  2893. pnfs_put_lseg(hdr->lseg);
  2894. nfs_pgio_header_free(hdr);
  2895. }
  2896. int
  2897. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  2898. {
  2899. struct nfs_pgio_header *hdr;
  2900. int ret;
  2901. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  2902. if (!hdr) {
  2903. desc->pg_error = -ENOMEM;
  2904. return desc->pg_error;
  2905. }
  2906. nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
  2907. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  2908. ret = nfs_generic_pgio(desc, hdr);
  2909. if (!ret)
  2910. pnfs_do_read(desc, hdr);
  2911. return ret;
  2912. }
  2913. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  2914. static void pnfs_clear_layoutcommitting(struct inode *inode)
  2915. {
  2916. unsigned long *bitlock = &NFS_I(inode)->flags;
  2917. clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
  2918. smp_mb__after_atomic();
  2919. wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
  2920. }
  2921. /*
  2922. * There can be multiple RW segments.
  2923. */
  2924. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  2925. {
  2926. struct pnfs_layout_segment *lseg;
  2927. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  2928. if (lseg->pls_range.iomode == IOMODE_RW &&
  2929. test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  2930. list_add(&lseg->pls_lc_list, listp);
  2931. }
  2932. }
  2933. static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
  2934. {
  2935. struct pnfs_layout_segment *lseg, *tmp;
  2936. /* Matched by references in pnfs_set_layoutcommit */
  2937. list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
  2938. list_del_init(&lseg->pls_lc_list);
  2939. pnfs_put_lseg(lseg);
  2940. }
  2941. pnfs_clear_layoutcommitting(inode);
  2942. }
  2943. void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
  2944. {
  2945. pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
  2946. }
  2947. EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
  2948. void
  2949. pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
  2950. loff_t end_pos)
  2951. {
  2952. struct nfs_inode *nfsi = NFS_I(inode);
  2953. bool mark_as_dirty = false;
  2954. spin_lock(&inode->i_lock);
  2955. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  2956. nfsi->layout->plh_lwb = end_pos;
  2957. mark_as_dirty = true;
  2958. dprintk("%s: Set layoutcommit for inode %lu ",
  2959. __func__, inode->i_ino);
  2960. } else if (end_pos > nfsi->layout->plh_lwb)
  2961. nfsi->layout->plh_lwb = end_pos;
  2962. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
  2963. /* references matched in nfs4_layoutcommit_release */
  2964. pnfs_get_lseg(lseg);
  2965. }
  2966. spin_unlock(&inode->i_lock);
  2967. dprintk("%s: lseg %p end_pos %llu\n",
  2968. __func__, lseg, nfsi->layout->plh_lwb);
  2969. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  2970. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  2971. if (mark_as_dirty)
  2972. mark_inode_dirty_sync(inode);
  2973. }
  2974. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  2975. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  2976. {
  2977. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  2978. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  2979. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  2980. pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
  2981. }
  2982. /*
  2983. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  2984. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  2985. * data to disk to allow the server to recover the data if it crashes.
  2986. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  2987. * is off, and a COMMIT is sent to a data server, or
  2988. * if WRITEs to a data server return NFS_DATA_SYNC.
  2989. */
  2990. int
  2991. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  2992. {
  2993. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  2994. struct nfs4_layoutcommit_data *data;
  2995. struct nfs_inode *nfsi = NFS_I(inode);
  2996. loff_t end_pos;
  2997. int status;
  2998. bool mark_as_dirty = false;
  2999. if (!pnfs_layoutcommit_outstanding(inode))
  3000. return 0;
  3001. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  3002. status = -EAGAIN;
  3003. if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
  3004. if (!sync)
  3005. goto out;
  3006. status = wait_on_bit_lock_action(&nfsi->flags,
  3007. NFS_INO_LAYOUTCOMMITTING,
  3008. nfs_wait_bit_killable,
  3009. TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
  3010. if (status)
  3011. goto out;
  3012. }
  3013. status = -ENOMEM;
  3014. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  3015. data = kzalloc_obj(*data, nfs_io_gfp_mask());
  3016. if (!data)
  3017. goto clear_layoutcommitting;
  3018. status = 0;
  3019. spin_lock(&inode->i_lock);
  3020. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  3021. goto out_unlock;
  3022. INIT_LIST_HEAD(&data->lseg_list);
  3023. pnfs_list_write_lseg(inode, &data->lseg_list);
  3024. end_pos = nfsi->layout->plh_lwb;
  3025. nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
  3026. data->cred = get_cred(nfsi->layout->plh_lc_cred);
  3027. spin_unlock(&inode->i_lock);
  3028. data->args.inode = inode;
  3029. nfs_fattr_init(&data->fattr);
  3030. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  3031. data->res.fattr = &data->fattr;
  3032. if (end_pos != 0)
  3033. data->args.lastbytewritten = end_pos - 1;
  3034. else
  3035. data->args.lastbytewritten = U64_MAX;
  3036. data->res.server = NFS_SERVER(inode);
  3037. if (ld->prepare_layoutcommit) {
  3038. status = ld->prepare_layoutcommit(&data->args);
  3039. if (status) {
  3040. if (status != -ENOSPC)
  3041. put_cred(data->cred);
  3042. spin_lock(&inode->i_lock);
  3043. set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
  3044. if (end_pos > nfsi->layout->plh_lwb)
  3045. nfsi->layout->plh_lwb = end_pos;
  3046. if (status != -ENOSPC)
  3047. goto out_unlock;
  3048. spin_unlock(&inode->i_lock);
  3049. mark_as_dirty = true;
  3050. }
  3051. }
  3052. status = nfs4_proc_layoutcommit(data, sync);
  3053. out:
  3054. if (status || mark_as_dirty)
  3055. mark_inode_dirty_sync(inode);
  3056. dprintk("<-- %s status %d\n", __func__, status);
  3057. return status;
  3058. out_unlock:
  3059. spin_unlock(&inode->i_lock);
  3060. kfree(data);
  3061. clear_layoutcommitting:
  3062. pnfs_clear_layoutcommitting(inode);
  3063. goto out;
  3064. }
  3065. EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
  3066. int
  3067. pnfs_generic_sync(struct inode *inode, bool datasync)
  3068. {
  3069. return pnfs_layoutcommit_inode(inode, true);
  3070. }
  3071. EXPORT_SYMBOL_GPL(pnfs_generic_sync);
  3072. struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
  3073. {
  3074. struct nfs4_threshold *thp;
  3075. thp = kzalloc_obj(*thp, nfs_io_gfp_mask());
  3076. if (!thp) {
  3077. dprintk("%s mdsthreshold allocation failed\n", __func__);
  3078. return NULL;
  3079. }
  3080. return thp;
  3081. }
  3082. #if IS_ENABLED(CONFIG_NFS_V4_2)
  3083. int
  3084. pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
  3085. {
  3086. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  3087. struct nfs_server *server = NFS_SERVER(inode);
  3088. struct nfs_inode *nfsi = NFS_I(inode);
  3089. struct nfs42_layoutstat_data *data;
  3090. struct pnfs_layout_hdr *hdr;
  3091. int status = 0;
  3092. if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
  3093. goto out;
  3094. if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
  3095. goto out;
  3096. if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
  3097. goto out;
  3098. spin_lock(&inode->i_lock);
  3099. if (!NFS_I(inode)->layout) {
  3100. spin_unlock(&inode->i_lock);
  3101. goto out_clear_layoutstats;
  3102. }
  3103. hdr = NFS_I(inode)->layout;
  3104. pnfs_get_layout_hdr(hdr);
  3105. spin_unlock(&inode->i_lock);
  3106. data = kzalloc_obj(*data, gfp_flags);
  3107. if (!data) {
  3108. status = -ENOMEM;
  3109. goto out_put;
  3110. }
  3111. data->args.fh = NFS_FH(inode);
  3112. data->args.inode = inode;
  3113. status = ld->prepare_layoutstats(&data->args);
  3114. if (status)
  3115. goto out_free;
  3116. status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
  3117. out:
  3118. dprintk("%s returns %d\n", __func__, status);
  3119. return status;
  3120. out_free:
  3121. kfree(data);
  3122. out_put:
  3123. pnfs_put_layout_hdr(hdr);
  3124. out_clear_layoutstats:
  3125. smp_mb__before_atomic();
  3126. clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
  3127. smp_mb__after_atomic();
  3128. goto out;
  3129. }
  3130. EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
  3131. #endif
  3132. unsigned int layoutstats_timer;
  3133. module_param(layoutstats_timer, uint, 0644);
  3134. EXPORT_SYMBOL_GPL(layoutstats_timer);