pnfs.c 88 KB

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