pnfs.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357
  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 <[email protected]>
  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. set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags);
  423. if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
  424. !test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
  425. pnfs_clear_layoutreturn_waitbit(lo);
  426. return !list_empty(&lo->plh_segs);
  427. }
  428. static int
  429. pnfs_iomode_to_fail_bit(u32 iomode)
  430. {
  431. return iomode == IOMODE_RW ?
  432. NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
  433. }
  434. static void
  435. pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
  436. {
  437. lo->plh_retry_timestamp = jiffies;
  438. if (!test_and_set_bit(fail_bit, &lo->plh_flags))
  439. refcount_inc(&lo->plh_refcount);
  440. }
  441. static void
  442. pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
  443. {
  444. if (test_and_clear_bit(fail_bit, &lo->plh_flags))
  445. refcount_dec(&lo->plh_refcount);
  446. }
  447. static void
  448. pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
  449. {
  450. struct inode *inode = lo->plh_inode;
  451. struct pnfs_layout_range range = {
  452. .iomode = iomode,
  453. .offset = 0,
  454. .length = NFS4_MAX_UINT64,
  455. };
  456. LIST_HEAD(head);
  457. spin_lock(&inode->i_lock);
  458. pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  459. pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
  460. spin_unlock(&inode->i_lock);
  461. pnfs_free_lseg_list(&head);
  462. dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
  463. iomode == IOMODE_RW ? "RW" : "READ");
  464. }
  465. static bool
  466. pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
  467. {
  468. unsigned long start, end;
  469. int fail_bit = pnfs_iomode_to_fail_bit(iomode);
  470. if (test_bit(fail_bit, &lo->plh_flags) == 0)
  471. return false;
  472. end = jiffies;
  473. start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
  474. if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
  475. /* It is time to retry the failed layoutgets */
  476. pnfs_layout_clear_fail_bit(lo, fail_bit);
  477. return false;
  478. }
  479. return true;
  480. }
  481. static void
  482. pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
  483. const struct pnfs_layout_range *range,
  484. const nfs4_stateid *stateid)
  485. {
  486. INIT_LIST_HEAD(&lseg->pls_list);
  487. INIT_LIST_HEAD(&lseg->pls_lc_list);
  488. INIT_LIST_HEAD(&lseg->pls_commits);
  489. refcount_set(&lseg->pls_refcount, 1);
  490. set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
  491. lseg->pls_layout = lo;
  492. lseg->pls_range = *range;
  493. lseg->pls_seq = be32_to_cpu(stateid->seqid);
  494. }
  495. static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
  496. {
  497. if (lseg != NULL) {
  498. struct inode *inode = lseg->pls_layout->plh_inode;
  499. NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
  500. }
  501. }
  502. static void
  503. pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
  504. struct pnfs_layout_segment *lseg)
  505. {
  506. WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  507. list_del_init(&lseg->pls_list);
  508. /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
  509. refcount_dec(&lo->plh_refcount);
  510. if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
  511. return;
  512. if (list_empty(&lo->plh_segs) &&
  513. !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) &&
  514. !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
  515. if (atomic_read(&lo->plh_outstanding) == 0)
  516. set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
  517. clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  518. }
  519. }
  520. static bool
  521. pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo,
  522. struct pnfs_layout_segment *lseg)
  523. {
  524. if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
  525. pnfs_layout_is_valid(lo)) {
  526. pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
  527. list_move_tail(&lseg->pls_list, &lo->plh_return_segs);
  528. return true;
  529. }
  530. return false;
  531. }
  532. void
  533. pnfs_put_lseg(struct pnfs_layout_segment *lseg)
  534. {
  535. struct pnfs_layout_hdr *lo;
  536. struct inode *inode;
  537. if (!lseg)
  538. return;
  539. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  540. refcount_read(&lseg->pls_refcount),
  541. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  542. lo = lseg->pls_layout;
  543. inode = lo->plh_inode;
  544. if (refcount_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
  545. pnfs_get_layout_hdr(lo);
  546. pnfs_layout_remove_lseg(lo, lseg);
  547. if (pnfs_cache_lseg_for_layoutreturn(lo, lseg))
  548. lseg = NULL;
  549. spin_unlock(&inode->i_lock);
  550. pnfs_free_lseg(lseg);
  551. pnfs_put_layout_hdr(lo);
  552. }
  553. }
  554. EXPORT_SYMBOL_GPL(pnfs_put_lseg);
  555. /*
  556. * is l2 fully contained in l1?
  557. * start1 end1
  558. * [----------------------------------)
  559. * start2 end2
  560. * [----------------)
  561. */
  562. static bool
  563. pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
  564. const struct pnfs_layout_range *l2)
  565. {
  566. u64 start1 = l1->offset;
  567. u64 end1 = pnfs_end_offset(start1, l1->length);
  568. u64 start2 = l2->offset;
  569. u64 end2 = pnfs_end_offset(start2, l2->length);
  570. return (start1 <= start2) && (end1 >= end2);
  571. }
  572. static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
  573. struct list_head *tmp_list)
  574. {
  575. if (!refcount_dec_and_test(&lseg->pls_refcount))
  576. return false;
  577. pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
  578. list_add(&lseg->pls_list, tmp_list);
  579. return true;
  580. }
  581. /* Returns 1 if lseg is removed from list, 0 otherwise */
  582. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  583. struct list_head *tmp_list)
  584. {
  585. int rv = 0;
  586. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  587. /* Remove the reference keeping the lseg in the
  588. * list. It will now be removed when all
  589. * outstanding io is finished.
  590. */
  591. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  592. refcount_read(&lseg->pls_refcount));
  593. if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
  594. rv = 1;
  595. }
  596. return rv;
  597. }
  598. static bool
  599. pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
  600. const struct pnfs_layout_range *recall_range)
  601. {
  602. return (recall_range->iomode == IOMODE_ANY ||
  603. lseg_range->iomode == recall_range->iomode) &&
  604. pnfs_lseg_range_intersecting(lseg_range, recall_range);
  605. }
  606. static bool
  607. pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
  608. const struct pnfs_layout_range *recall_range,
  609. u32 seq)
  610. {
  611. if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
  612. return false;
  613. if (recall_range == NULL)
  614. return true;
  615. return pnfs_should_free_range(&lseg->pls_range, recall_range);
  616. }
  617. /**
  618. * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
  619. * @lo: layout header containing the lsegs
  620. * @tmp_list: list head where doomed lsegs should go
  621. * @recall_range: optional recall range argument to match (may be NULL)
  622. * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
  623. *
  624. * Walk the list of lsegs in the layout header, and tear down any that should
  625. * be destroyed. If "recall_range" is specified then the segment must match
  626. * that range. If "seq" is non-zero, then only match segments that were handed
  627. * out at or before that sequence.
  628. *
  629. * Returns number of matching invalid lsegs remaining in list after scanning
  630. * it and purging them.
  631. */
  632. int
  633. pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
  634. struct list_head *tmp_list,
  635. const struct pnfs_layout_range *recall_range,
  636. u32 seq)
  637. {
  638. struct pnfs_layout_segment *lseg, *next;
  639. struct nfs_server *server = NFS_SERVER(lo->plh_inode);
  640. int remaining = 0;
  641. dprintk("%s:Begin lo %p\n", __func__, lo);
  642. if (list_empty(&lo->plh_segs))
  643. return 0;
  644. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  645. if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
  646. dprintk("%s: freeing lseg %p iomode %d seq %u "
  647. "offset %llu length %llu\n", __func__,
  648. lseg, lseg->pls_range.iomode, lseg->pls_seq,
  649. lseg->pls_range.offset, lseg->pls_range.length);
  650. if (mark_lseg_invalid(lseg, tmp_list))
  651. continue;
  652. remaining++;
  653. pnfs_lseg_cancel_io(server, lseg);
  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. pnfs_put_layout_hdr(lgp->lo);
  1015. put_nfs_open_context(lgp->args.ctx);
  1016. kfree(lgp);
  1017. }
  1018. static void pnfs_clear_layoutcommit(struct inode *inode,
  1019. struct list_head *head)
  1020. {
  1021. struct nfs_inode *nfsi = NFS_I(inode);
  1022. struct pnfs_layout_segment *lseg, *tmp;
  1023. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1024. return;
  1025. list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
  1026. if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  1027. continue;
  1028. pnfs_lseg_dec_and_remove_zero(lseg, head);
  1029. }
  1030. }
  1031. void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
  1032. const nfs4_stateid *arg_stateid,
  1033. const struct pnfs_layout_range *range,
  1034. const nfs4_stateid *stateid)
  1035. {
  1036. struct inode *inode = lo->plh_inode;
  1037. LIST_HEAD(freeme);
  1038. spin_lock(&inode->i_lock);
  1039. if (!pnfs_layout_is_valid(lo) ||
  1040. !nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
  1041. goto out_unlock;
  1042. if (stateid) {
  1043. u32 seq = be32_to_cpu(arg_stateid->seqid);
  1044. pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq);
  1045. pnfs_free_returned_lsegs(lo, &freeme, range, seq);
  1046. pnfs_set_layout_stateid(lo, stateid, NULL, true);
  1047. } else
  1048. pnfs_mark_layout_stateid_invalid(lo, &freeme);
  1049. out_unlock:
  1050. pnfs_clear_layoutreturn_waitbit(lo);
  1051. spin_unlock(&inode->i_lock);
  1052. pnfs_free_lseg_list(&freeme);
  1053. }
  1054. static bool
  1055. pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
  1056. nfs4_stateid *stateid,
  1057. const struct cred **cred,
  1058. enum pnfs_iomode *iomode)
  1059. {
  1060. /* Serialise LAYOUTGET/LAYOUTRETURN */
  1061. if (atomic_read(&lo->plh_outstanding) != 0)
  1062. return false;
  1063. if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
  1064. return false;
  1065. set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
  1066. pnfs_get_layout_hdr(lo);
  1067. nfs4_stateid_copy(stateid, &lo->plh_stateid);
  1068. *cred = get_cred(lo->plh_lc_cred);
  1069. if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
  1070. if (lo->plh_return_seq != 0)
  1071. stateid->seqid = cpu_to_be32(lo->plh_return_seq);
  1072. if (iomode != NULL)
  1073. *iomode = lo->plh_return_iomode;
  1074. pnfs_clear_layoutreturn_info(lo);
  1075. } else if (iomode != NULL)
  1076. *iomode = IOMODE_ANY;
  1077. pnfs_barrier_update(lo, be32_to_cpu(stateid->seqid));
  1078. return true;
  1079. }
  1080. static void
  1081. pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
  1082. struct pnfs_layout_hdr *lo,
  1083. const nfs4_stateid *stateid,
  1084. enum pnfs_iomode iomode)
  1085. {
  1086. struct inode *inode = lo->plh_inode;
  1087. args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
  1088. args->inode = inode;
  1089. args->range.iomode = iomode;
  1090. args->range.offset = 0;
  1091. args->range.length = NFS4_MAX_UINT64;
  1092. args->layout = lo;
  1093. nfs4_stateid_copy(&args->stateid, stateid);
  1094. }
  1095. static int
  1096. pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo,
  1097. const nfs4_stateid *stateid,
  1098. const struct cred **pcred,
  1099. enum pnfs_iomode iomode,
  1100. bool sync)
  1101. {
  1102. struct inode *ino = lo->plh_inode;
  1103. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  1104. struct nfs4_layoutreturn *lrp;
  1105. const struct cred *cred = *pcred;
  1106. int status = 0;
  1107. *pcred = NULL;
  1108. lrp = kzalloc(sizeof(*lrp), nfs_io_gfp_mask());
  1109. if (unlikely(lrp == NULL)) {
  1110. status = -ENOMEM;
  1111. spin_lock(&ino->i_lock);
  1112. pnfs_clear_layoutreturn_waitbit(lo);
  1113. spin_unlock(&ino->i_lock);
  1114. put_cred(cred);
  1115. pnfs_put_layout_hdr(lo);
  1116. goto out;
  1117. }
  1118. pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
  1119. lrp->args.ld_private = &lrp->ld_private;
  1120. lrp->clp = NFS_SERVER(ino)->nfs_client;
  1121. lrp->cred = cred;
  1122. if (ld->prepare_layoutreturn)
  1123. ld->prepare_layoutreturn(&lrp->args);
  1124. status = nfs4_proc_layoutreturn(lrp, sync);
  1125. out:
  1126. dprintk("<-- %s status: %d\n", __func__, status);
  1127. return status;
  1128. }
  1129. static bool
  1130. pnfs_layout_segments_returnable(struct pnfs_layout_hdr *lo,
  1131. enum pnfs_iomode iomode,
  1132. u32 seq)
  1133. {
  1134. struct pnfs_layout_range recall_range = {
  1135. .length = NFS4_MAX_UINT64,
  1136. .iomode = iomode,
  1137. };
  1138. return pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
  1139. &recall_range, seq) != -EBUSY;
  1140. }
  1141. /* Return true if layoutreturn is needed */
  1142. static bool
  1143. pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
  1144. {
  1145. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  1146. return false;
  1147. return pnfs_layout_segments_returnable(lo, lo->plh_return_iomode,
  1148. lo->plh_return_seq);
  1149. }
  1150. static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
  1151. {
  1152. struct inode *inode= lo->plh_inode;
  1153. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  1154. return;
  1155. spin_lock(&inode->i_lock);
  1156. if (pnfs_layout_need_return(lo)) {
  1157. const struct cred *cred;
  1158. nfs4_stateid stateid;
  1159. enum pnfs_iomode iomode;
  1160. bool send;
  1161. send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode);
  1162. spin_unlock(&inode->i_lock);
  1163. if (send) {
  1164. /* Send an async layoutreturn so we dont deadlock */
  1165. pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false);
  1166. }
  1167. } else
  1168. spin_unlock(&inode->i_lock);
  1169. }
  1170. /*
  1171. * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
  1172. * when the layout segment list is empty.
  1173. *
  1174. * Note that a pnfs_layout_hdr can exist with an empty layout segment
  1175. * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
  1176. * deviceid is marked invalid.
  1177. */
  1178. int
  1179. _pnfs_return_layout(struct inode *ino)
  1180. {
  1181. struct pnfs_layout_hdr *lo = NULL;
  1182. struct nfs_inode *nfsi = NFS_I(ino);
  1183. struct pnfs_layout_range range = {
  1184. .iomode = IOMODE_ANY,
  1185. .offset = 0,
  1186. .length = NFS4_MAX_UINT64,
  1187. };
  1188. LIST_HEAD(tmp_list);
  1189. const struct cred *cred;
  1190. nfs4_stateid stateid;
  1191. int status = 0;
  1192. bool send, valid_layout;
  1193. dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
  1194. spin_lock(&ino->i_lock);
  1195. lo = nfsi->layout;
  1196. if (!lo) {
  1197. spin_unlock(&ino->i_lock);
  1198. dprintk("NFS: %s no layout to return\n", __func__);
  1199. goto out;
  1200. }
  1201. /* Reference matched in nfs4_layoutreturn_release */
  1202. pnfs_get_layout_hdr(lo);
  1203. /* Is there an outstanding layoutreturn ? */
  1204. if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
  1205. spin_unlock(&ino->i_lock);
  1206. if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1207. TASK_UNINTERRUPTIBLE))
  1208. goto out_put_layout_hdr;
  1209. spin_lock(&ino->i_lock);
  1210. }
  1211. valid_layout = pnfs_layout_is_valid(lo);
  1212. pnfs_clear_layoutcommit(ino, &tmp_list);
  1213. pnfs_mark_matching_lsegs_return(lo, &tmp_list, &range, 0);
  1214. if (NFS_SERVER(ino)->pnfs_curr_ld->return_range)
  1215. NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
  1216. /* Don't send a LAYOUTRETURN if list was initially empty */
  1217. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) ||
  1218. !valid_layout) {
  1219. spin_unlock(&ino->i_lock);
  1220. dprintk("NFS: %s no layout segments to return\n", __func__);
  1221. goto out_wait_layoutreturn;
  1222. }
  1223. send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, NULL);
  1224. spin_unlock(&ino->i_lock);
  1225. if (send)
  1226. status = pnfs_send_layoutreturn(lo, &stateid, &cred, IOMODE_ANY, true);
  1227. out_wait_layoutreturn:
  1228. wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, TASK_UNINTERRUPTIBLE);
  1229. out_put_layout_hdr:
  1230. pnfs_free_lseg_list(&tmp_list);
  1231. pnfs_put_layout_hdr(lo);
  1232. out:
  1233. dprintk("<-- %s status: %d\n", __func__, status);
  1234. return status;
  1235. }
  1236. int
  1237. pnfs_commit_and_return_layout(struct inode *inode)
  1238. {
  1239. struct pnfs_layout_hdr *lo;
  1240. int ret;
  1241. spin_lock(&inode->i_lock);
  1242. lo = NFS_I(inode)->layout;
  1243. if (lo == NULL) {
  1244. spin_unlock(&inode->i_lock);
  1245. return 0;
  1246. }
  1247. pnfs_get_layout_hdr(lo);
  1248. /* Block new layoutgets and read/write to ds */
  1249. lo->plh_block_lgets++;
  1250. spin_unlock(&inode->i_lock);
  1251. filemap_fdatawait(inode->i_mapping);
  1252. ret = pnfs_layoutcommit_inode(inode, true);
  1253. if (ret == 0)
  1254. ret = _pnfs_return_layout(inode);
  1255. spin_lock(&inode->i_lock);
  1256. lo->plh_block_lgets--;
  1257. spin_unlock(&inode->i_lock);
  1258. pnfs_put_layout_hdr(lo);
  1259. return ret;
  1260. }
  1261. bool pnfs_roc(struct inode *ino,
  1262. struct nfs4_layoutreturn_args *args,
  1263. struct nfs4_layoutreturn_res *res,
  1264. const struct cred *cred)
  1265. {
  1266. struct nfs_inode *nfsi = NFS_I(ino);
  1267. struct nfs_open_context *ctx;
  1268. struct nfs4_state *state;
  1269. struct pnfs_layout_hdr *lo;
  1270. struct pnfs_layout_segment *lseg, *next;
  1271. const struct cred *lc_cred;
  1272. nfs4_stateid stateid;
  1273. enum pnfs_iomode iomode = 0;
  1274. bool layoutreturn = false, roc = false;
  1275. bool skip_read = false;
  1276. if (!nfs_have_layout(ino))
  1277. return false;
  1278. retry:
  1279. rcu_read_lock();
  1280. spin_lock(&ino->i_lock);
  1281. lo = nfsi->layout;
  1282. if (!lo || !pnfs_layout_is_valid(lo) ||
  1283. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1284. lo = NULL;
  1285. goto out_noroc;
  1286. }
  1287. pnfs_get_layout_hdr(lo);
  1288. if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
  1289. spin_unlock(&ino->i_lock);
  1290. rcu_read_unlock();
  1291. wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1292. TASK_UNINTERRUPTIBLE);
  1293. pnfs_put_layout_hdr(lo);
  1294. goto retry;
  1295. }
  1296. /* no roc if we hold a delegation */
  1297. if (nfs4_check_delegation(ino, FMODE_READ)) {
  1298. if (nfs4_check_delegation(ino, FMODE_WRITE))
  1299. goto out_noroc;
  1300. skip_read = true;
  1301. }
  1302. list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
  1303. state = ctx->state;
  1304. if (state == NULL)
  1305. continue;
  1306. /* Don't return layout if there is open file state */
  1307. if (state->state & FMODE_WRITE)
  1308. goto out_noroc;
  1309. if (state->state & FMODE_READ)
  1310. skip_read = true;
  1311. }
  1312. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
  1313. if (skip_read && lseg->pls_range.iomode == IOMODE_READ)
  1314. continue;
  1315. /* If we are sending layoutreturn, invalidate all valid lsegs */
  1316. if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags))
  1317. continue;
  1318. /*
  1319. * Note: mark lseg for return so pnfs_layout_remove_lseg
  1320. * doesn't invalidate the layout for us.
  1321. */
  1322. set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
  1323. if (!mark_lseg_invalid(lseg, &lo->plh_return_segs))
  1324. continue;
  1325. pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
  1326. }
  1327. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  1328. goto out_noroc;
  1329. /* ROC in two conditions:
  1330. * 1. there are ROC lsegs
  1331. * 2. we don't send layoutreturn
  1332. */
  1333. /* lo ref dropped in pnfs_roc_release() */
  1334. layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &lc_cred, &iomode);
  1335. /* If the creds don't match, we can't compound the layoutreturn */
  1336. if (!layoutreturn || cred_fscmp(cred, lc_cred) != 0)
  1337. goto out_noroc;
  1338. roc = layoutreturn;
  1339. pnfs_init_layoutreturn_args(args, lo, &stateid, iomode);
  1340. res->lrs_present = 0;
  1341. layoutreturn = false;
  1342. put_cred(lc_cred);
  1343. out_noroc:
  1344. spin_unlock(&ino->i_lock);
  1345. rcu_read_unlock();
  1346. pnfs_layoutcommit_inode(ino, true);
  1347. if (roc) {
  1348. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  1349. if (ld->prepare_layoutreturn)
  1350. ld->prepare_layoutreturn(args);
  1351. pnfs_put_layout_hdr(lo);
  1352. return true;
  1353. }
  1354. if (layoutreturn)
  1355. pnfs_send_layoutreturn(lo, &stateid, &lc_cred, iomode, true);
  1356. pnfs_put_layout_hdr(lo);
  1357. return false;
  1358. }
  1359. int pnfs_roc_done(struct rpc_task *task, struct nfs4_layoutreturn_args **argpp,
  1360. struct nfs4_layoutreturn_res **respp, int *ret)
  1361. {
  1362. struct nfs4_layoutreturn_args *arg = *argpp;
  1363. int retval = -EAGAIN;
  1364. if (!arg)
  1365. return 0;
  1366. /* Handle Layoutreturn errors */
  1367. switch (*ret) {
  1368. case 0:
  1369. retval = 0;
  1370. break;
  1371. case -NFS4ERR_NOMATCHING_LAYOUT:
  1372. /* Was there an RPC level error? If not, retry */
  1373. if (task->tk_rpc_status == 0)
  1374. break;
  1375. /* If the call was not sent, let caller handle it */
  1376. if (!RPC_WAS_SENT(task))
  1377. return 0;
  1378. /*
  1379. * Otherwise, assume the call succeeded and
  1380. * that we need to release the layout
  1381. */
  1382. *ret = 0;
  1383. (*respp)->lrs_present = 0;
  1384. retval = 0;
  1385. break;
  1386. case -NFS4ERR_DELAY:
  1387. /* Let the caller handle the retry */
  1388. *ret = -NFS4ERR_NOMATCHING_LAYOUT;
  1389. return 0;
  1390. case -NFS4ERR_OLD_STATEID:
  1391. if (!nfs4_layout_refresh_old_stateid(&arg->stateid,
  1392. &arg->range, arg->inode))
  1393. break;
  1394. *ret = -NFS4ERR_NOMATCHING_LAYOUT;
  1395. return -EAGAIN;
  1396. }
  1397. *argpp = NULL;
  1398. *respp = NULL;
  1399. return retval;
  1400. }
  1401. void pnfs_roc_release(struct nfs4_layoutreturn_args *args,
  1402. struct nfs4_layoutreturn_res *res,
  1403. int ret)
  1404. {
  1405. struct pnfs_layout_hdr *lo = args->layout;
  1406. struct inode *inode = args->inode;
  1407. const nfs4_stateid *res_stateid = NULL;
  1408. struct nfs4_xdr_opaque_data *ld_private = args->ld_private;
  1409. switch (ret) {
  1410. case -NFS4ERR_NOMATCHING_LAYOUT:
  1411. spin_lock(&inode->i_lock);
  1412. if (pnfs_layout_is_valid(lo) &&
  1413. nfs4_stateid_match_other(&args->stateid, &lo->plh_stateid))
  1414. pnfs_set_plh_return_info(lo, args->range.iomode, 0);
  1415. pnfs_clear_layoutreturn_waitbit(lo);
  1416. spin_unlock(&inode->i_lock);
  1417. break;
  1418. case 0:
  1419. if (res->lrs_present)
  1420. res_stateid = &res->stateid;
  1421. fallthrough;
  1422. default:
  1423. pnfs_layoutreturn_free_lsegs(lo, &args->stateid, &args->range,
  1424. res_stateid);
  1425. }
  1426. trace_nfs4_layoutreturn_on_close(args->inode, &args->stateid, ret);
  1427. if (ld_private && ld_private->ops && ld_private->ops->free)
  1428. ld_private->ops->free(ld_private);
  1429. pnfs_put_layout_hdr(lo);
  1430. }
  1431. bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
  1432. {
  1433. struct nfs_inode *nfsi = NFS_I(ino);
  1434. struct pnfs_layout_hdr *lo;
  1435. bool sleep = false;
  1436. /* we might not have grabbed lo reference. so need to check under
  1437. * i_lock */
  1438. spin_lock(&ino->i_lock);
  1439. lo = nfsi->layout;
  1440. if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
  1441. rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
  1442. sleep = true;
  1443. }
  1444. spin_unlock(&ino->i_lock);
  1445. return sleep;
  1446. }
  1447. /*
  1448. * Compare two layout segments for sorting into layout cache.
  1449. * We want to preferentially return RW over RO layouts, so ensure those
  1450. * are seen first.
  1451. */
  1452. static s64
  1453. pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
  1454. const struct pnfs_layout_range *l2)
  1455. {
  1456. s64 d;
  1457. /* high offset > low offset */
  1458. d = l1->offset - l2->offset;
  1459. if (d)
  1460. return d;
  1461. /* short length > long length */
  1462. d = l2->length - l1->length;
  1463. if (d)
  1464. return d;
  1465. /* read > read/write */
  1466. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  1467. }
  1468. static bool
  1469. pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
  1470. const struct pnfs_layout_range *l2)
  1471. {
  1472. return pnfs_lseg_range_cmp(l1, l2) > 0;
  1473. }
  1474. static bool
  1475. pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
  1476. struct pnfs_layout_segment *old)
  1477. {
  1478. return false;
  1479. }
  1480. void
  1481. pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  1482. struct pnfs_layout_segment *lseg,
  1483. bool (*is_after)(const struct pnfs_layout_range *,
  1484. const struct pnfs_layout_range *),
  1485. bool (*do_merge)(struct pnfs_layout_segment *,
  1486. struct pnfs_layout_segment *),
  1487. struct list_head *free_me)
  1488. {
  1489. struct pnfs_layout_segment *lp, *tmp;
  1490. dprintk("%s:Begin\n", __func__);
  1491. list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
  1492. if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
  1493. continue;
  1494. if (do_merge(lseg, lp)) {
  1495. mark_lseg_invalid(lp, free_me);
  1496. continue;
  1497. }
  1498. if (is_after(&lseg->pls_range, &lp->pls_range))
  1499. continue;
  1500. list_add_tail(&lseg->pls_list, &lp->pls_list);
  1501. dprintk("%s: inserted lseg %p "
  1502. "iomode %d offset %llu length %llu before "
  1503. "lp %p iomode %d offset %llu length %llu\n",
  1504. __func__, lseg, lseg->pls_range.iomode,
  1505. lseg->pls_range.offset, lseg->pls_range.length,
  1506. lp, lp->pls_range.iomode, lp->pls_range.offset,
  1507. lp->pls_range.length);
  1508. goto out;
  1509. }
  1510. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  1511. dprintk("%s: inserted lseg %p "
  1512. "iomode %d offset %llu length %llu at tail\n",
  1513. __func__, lseg, lseg->pls_range.iomode,
  1514. lseg->pls_range.offset, lseg->pls_range.length);
  1515. out:
  1516. pnfs_get_layout_hdr(lo);
  1517. dprintk("%s:Return\n", __func__);
  1518. }
  1519. EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
  1520. static void
  1521. pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  1522. struct pnfs_layout_segment *lseg,
  1523. struct list_head *free_me)
  1524. {
  1525. struct inode *inode = lo->plh_inode;
  1526. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  1527. if (ld->add_lseg != NULL)
  1528. ld->add_lseg(lo, lseg, free_me);
  1529. else
  1530. pnfs_generic_layout_insert_lseg(lo, lseg,
  1531. pnfs_lseg_range_is_after,
  1532. pnfs_lseg_no_merge,
  1533. free_me);
  1534. }
  1535. static struct pnfs_layout_hdr *
  1536. alloc_init_layout_hdr(struct inode *ino,
  1537. struct nfs_open_context *ctx,
  1538. gfp_t gfp_flags)
  1539. {
  1540. struct pnfs_layout_hdr *lo;
  1541. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  1542. if (!lo)
  1543. return NULL;
  1544. refcount_set(&lo->plh_refcount, 1);
  1545. INIT_LIST_HEAD(&lo->plh_layouts);
  1546. INIT_LIST_HEAD(&lo->plh_segs);
  1547. INIT_LIST_HEAD(&lo->plh_return_segs);
  1548. INIT_LIST_HEAD(&lo->plh_bulk_destroy);
  1549. lo->plh_inode = ino;
  1550. lo->plh_lc_cred = get_cred(ctx->cred);
  1551. lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
  1552. return lo;
  1553. }
  1554. static struct pnfs_layout_hdr *
  1555. pnfs_find_alloc_layout(struct inode *ino,
  1556. struct nfs_open_context *ctx,
  1557. gfp_t gfp_flags)
  1558. __releases(&ino->i_lock)
  1559. __acquires(&ino->i_lock)
  1560. {
  1561. struct nfs_inode *nfsi = NFS_I(ino);
  1562. struct pnfs_layout_hdr *new = NULL;
  1563. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  1564. if (nfsi->layout != NULL)
  1565. goto out_existing;
  1566. spin_unlock(&ino->i_lock);
  1567. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  1568. spin_lock(&ino->i_lock);
  1569. if (likely(nfsi->layout == NULL)) { /* Won the race? */
  1570. nfsi->layout = new;
  1571. return new;
  1572. } else if (new != NULL)
  1573. pnfs_free_layout_hdr(new);
  1574. out_existing:
  1575. pnfs_get_layout_hdr(nfsi->layout);
  1576. return nfsi->layout;
  1577. }
  1578. /*
  1579. * iomode matching rules:
  1580. * iomode lseg strict match
  1581. * iomode
  1582. * ----- ----- ------ -----
  1583. * ANY READ N/A true
  1584. * ANY RW N/A true
  1585. * RW READ N/A false
  1586. * RW RW N/A true
  1587. * READ READ N/A true
  1588. * READ RW true false
  1589. * READ RW false true
  1590. */
  1591. static bool
  1592. pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
  1593. const struct pnfs_layout_range *range,
  1594. bool strict_iomode)
  1595. {
  1596. struct pnfs_layout_range range1;
  1597. if ((range->iomode == IOMODE_RW &&
  1598. ls_range->iomode != IOMODE_RW) ||
  1599. (range->iomode != ls_range->iomode &&
  1600. strict_iomode) ||
  1601. !pnfs_lseg_range_intersecting(ls_range, range))
  1602. return false;
  1603. /* range1 covers only the first byte in the range */
  1604. range1 = *range;
  1605. range1.length = 1;
  1606. return pnfs_lseg_range_contained(ls_range, &range1);
  1607. }
  1608. /*
  1609. * lookup range in layout
  1610. */
  1611. static struct pnfs_layout_segment *
  1612. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  1613. struct pnfs_layout_range *range,
  1614. bool strict_iomode)
  1615. {
  1616. struct pnfs_layout_segment *lseg, *ret = NULL;
  1617. dprintk("%s:Begin\n", __func__);
  1618. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  1619. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  1620. pnfs_lseg_range_match(&lseg->pls_range, range,
  1621. strict_iomode)) {
  1622. ret = pnfs_get_lseg(lseg);
  1623. break;
  1624. }
  1625. }
  1626. dprintk("%s:Return lseg %p ref %d\n",
  1627. __func__, ret, ret ? refcount_read(&ret->pls_refcount) : 0);
  1628. return ret;
  1629. }
  1630. /*
  1631. * Use mdsthreshold hints set at each OPEN to determine if I/O should go
  1632. * to the MDS or over pNFS
  1633. *
  1634. * The nfs_inode read_io and write_io fields are cumulative counters reset
  1635. * when there are no layout segments. Note that in pnfs_update_layout iomode
  1636. * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
  1637. * WRITE request.
  1638. *
  1639. * A return of true means use MDS I/O.
  1640. *
  1641. * From rfc 5661:
  1642. * If a file's size is smaller than the file size threshold, data accesses
  1643. * SHOULD be sent to the metadata server. If an I/O request has a length that
  1644. * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
  1645. * server. If both file size and I/O size are provided, the client SHOULD
  1646. * reach or exceed both thresholds before sending its read or write
  1647. * requests to the data server.
  1648. */
  1649. static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
  1650. struct inode *ino, int iomode)
  1651. {
  1652. struct nfs4_threshold *t = ctx->mdsthreshold;
  1653. struct nfs_inode *nfsi = NFS_I(ino);
  1654. loff_t fsize = i_size_read(ino);
  1655. bool size = false, size_set = false, io = false, io_set = false, ret = false;
  1656. if (t == NULL)
  1657. return ret;
  1658. dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
  1659. __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
  1660. switch (iomode) {
  1661. case IOMODE_READ:
  1662. if (t->bm & THRESHOLD_RD) {
  1663. dprintk("%s fsize %llu\n", __func__, fsize);
  1664. size_set = true;
  1665. if (fsize < t->rd_sz)
  1666. size = true;
  1667. }
  1668. if (t->bm & THRESHOLD_RD_IO) {
  1669. dprintk("%s nfsi->read_io %llu\n", __func__,
  1670. nfsi->read_io);
  1671. io_set = true;
  1672. if (nfsi->read_io < t->rd_io_sz)
  1673. io = true;
  1674. }
  1675. break;
  1676. case IOMODE_RW:
  1677. if (t->bm & THRESHOLD_WR) {
  1678. dprintk("%s fsize %llu\n", __func__, fsize);
  1679. size_set = true;
  1680. if (fsize < t->wr_sz)
  1681. size = true;
  1682. }
  1683. if (t->bm & THRESHOLD_WR_IO) {
  1684. dprintk("%s nfsi->write_io %llu\n", __func__,
  1685. nfsi->write_io);
  1686. io_set = true;
  1687. if (nfsi->write_io < t->wr_io_sz)
  1688. io = true;
  1689. }
  1690. break;
  1691. }
  1692. if (size_set && io_set) {
  1693. if (size && io)
  1694. ret = true;
  1695. } else if (size || io)
  1696. ret = true;
  1697. dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
  1698. return ret;
  1699. }
  1700. static int pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
  1701. {
  1702. /*
  1703. * send layoutcommit as it can hold up layoutreturn due to lseg
  1704. * reference
  1705. */
  1706. pnfs_layoutcommit_inode(lo->plh_inode, false);
  1707. return wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1708. nfs_wait_bit_killable,
  1709. TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
  1710. }
  1711. static void nfs_layoutget_begin(struct pnfs_layout_hdr *lo)
  1712. {
  1713. atomic_inc(&lo->plh_outstanding);
  1714. }
  1715. static void nfs_layoutget_end(struct pnfs_layout_hdr *lo)
  1716. {
  1717. if (atomic_dec_and_test(&lo->plh_outstanding) &&
  1718. test_and_clear_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags))
  1719. wake_up_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN);
  1720. }
  1721. static bool pnfs_is_first_layoutget(struct pnfs_layout_hdr *lo)
  1722. {
  1723. return test_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags);
  1724. }
  1725. static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
  1726. {
  1727. unsigned long *bitlock = &lo->plh_flags;
  1728. clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
  1729. smp_mb__after_atomic();
  1730. wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
  1731. }
  1732. static void _add_to_server_list(struct pnfs_layout_hdr *lo,
  1733. struct nfs_server *server)
  1734. {
  1735. if (!test_and_set_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) {
  1736. struct nfs_client *clp = server->nfs_client;
  1737. /* The lo must be on the clp list if there is any
  1738. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  1739. */
  1740. spin_lock(&clp->cl_lock);
  1741. list_add_tail_rcu(&lo->plh_layouts, &server->layouts);
  1742. spin_unlock(&clp->cl_lock);
  1743. }
  1744. }
  1745. /*
  1746. * Layout segment is retreived from the server if not cached.
  1747. * The appropriate layout segment is referenced and returned to the caller.
  1748. */
  1749. struct pnfs_layout_segment *
  1750. pnfs_update_layout(struct inode *ino,
  1751. struct nfs_open_context *ctx,
  1752. loff_t pos,
  1753. u64 count,
  1754. enum pnfs_iomode iomode,
  1755. bool strict_iomode,
  1756. gfp_t gfp_flags)
  1757. {
  1758. struct pnfs_layout_range arg = {
  1759. .iomode = iomode,
  1760. .offset = pos,
  1761. .length = count,
  1762. };
  1763. unsigned pg_offset;
  1764. struct nfs_server *server = NFS_SERVER(ino);
  1765. struct nfs_client *clp = server->nfs_client;
  1766. struct pnfs_layout_hdr *lo = NULL;
  1767. struct pnfs_layout_segment *lseg = NULL;
  1768. struct nfs4_layoutget *lgp;
  1769. nfs4_stateid stateid;
  1770. long timeout = 0;
  1771. unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
  1772. bool first;
  1773. if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
  1774. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1775. PNFS_UPDATE_LAYOUT_NO_PNFS);
  1776. goto out;
  1777. }
  1778. if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
  1779. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1780. PNFS_UPDATE_LAYOUT_MDSTHRESH);
  1781. goto out;
  1782. }
  1783. lookup_again:
  1784. lseg = ERR_PTR(nfs4_client_recover_expired_lease(clp));
  1785. if (IS_ERR(lseg))
  1786. goto out;
  1787. first = false;
  1788. spin_lock(&ino->i_lock);
  1789. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  1790. if (lo == NULL) {
  1791. spin_unlock(&ino->i_lock);
  1792. lseg = ERR_PTR(-ENOMEM);
  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 (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
  1815. atomic_read(&lo->plh_outstanding) != 0) {
  1816. spin_unlock(&ino->i_lock);
  1817. lseg = ERR_PTR(wait_on_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN,
  1818. TASK_KILLABLE));
  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. lseg = ERR_PTR(-ENOMEM);
  1910. trace_pnfs_update_layout(ino, pos, count, iomode, lo, NULL,
  1911. PNFS_UPDATE_LAYOUT_NOMEM);
  1912. nfs_layoutget_end(lo);
  1913. goto out_put_layout_hdr;
  1914. }
  1915. lgp->lo = lo;
  1916. pnfs_get_layout_hdr(lo);
  1917. lseg = nfs4_proc_layoutget(lgp, &timeout);
  1918. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1919. PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
  1920. nfs_layoutget_end(lo);
  1921. if (IS_ERR(lseg)) {
  1922. switch(PTR_ERR(lseg)) {
  1923. case -EBUSY:
  1924. if (time_after(jiffies, giveup))
  1925. lseg = NULL;
  1926. break;
  1927. case -ERECALLCONFLICT:
  1928. case -EAGAIN:
  1929. break;
  1930. case -ENODATA:
  1931. /* The server returned NFS4ERR_LAYOUTUNAVAILABLE */
  1932. pnfs_layout_set_fail_bit(
  1933. lo, pnfs_iomode_to_fail_bit(iomode));
  1934. lseg = NULL;
  1935. goto out_put_layout_hdr;
  1936. default:
  1937. if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
  1938. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  1939. lseg = NULL;
  1940. }
  1941. goto out_put_layout_hdr;
  1942. }
  1943. if (lseg) {
  1944. if (first)
  1945. pnfs_clear_first_layoutget(lo);
  1946. trace_pnfs_update_layout(ino, pos, count,
  1947. iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
  1948. pnfs_put_layout_hdr(lo);
  1949. goto lookup_again;
  1950. }
  1951. } else {
  1952. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  1953. }
  1954. out_put_layout_hdr:
  1955. if (first)
  1956. pnfs_clear_first_layoutget(lo);
  1957. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1958. PNFS_UPDATE_LAYOUT_EXIT);
  1959. pnfs_put_layout_hdr(lo);
  1960. out:
  1961. dprintk("%s: inode %s/%llu pNFS layout segment %s for "
  1962. "(%s, offset: %llu, length: %llu)\n",
  1963. __func__, ino->i_sb->s_id,
  1964. (unsigned long long)NFS_FILEID(ino),
  1965. IS_ERR_OR_NULL(lseg) ? "not found" : "found",
  1966. iomode==IOMODE_RW ? "read/write" : "read-only",
  1967. (unsigned long long)pos,
  1968. (unsigned long long)count);
  1969. return lseg;
  1970. out_unlock:
  1971. spin_unlock(&ino->i_lock);
  1972. goto out_put_layout_hdr;
  1973. }
  1974. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  1975. static bool
  1976. pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
  1977. {
  1978. switch (range->iomode) {
  1979. case IOMODE_READ:
  1980. case IOMODE_RW:
  1981. break;
  1982. default:
  1983. return false;
  1984. }
  1985. if (range->offset == NFS4_MAX_UINT64)
  1986. return false;
  1987. if (range->length == 0)
  1988. return false;
  1989. if (range->length != NFS4_MAX_UINT64 &&
  1990. range->length > NFS4_MAX_UINT64 - range->offset)
  1991. return false;
  1992. return true;
  1993. }
  1994. static struct pnfs_layout_hdr *
  1995. _pnfs_grab_empty_layout(struct inode *ino, struct nfs_open_context *ctx)
  1996. {
  1997. struct pnfs_layout_hdr *lo;
  1998. spin_lock(&ino->i_lock);
  1999. lo = pnfs_find_alloc_layout(ino, ctx, nfs_io_gfp_mask());
  2000. if (!lo)
  2001. goto out_unlock;
  2002. if (!test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags))
  2003. goto out_unlock;
  2004. if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
  2005. goto out_unlock;
  2006. if (pnfs_layoutgets_blocked(lo))
  2007. goto out_unlock;
  2008. if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags))
  2009. goto out_unlock;
  2010. nfs_layoutget_begin(lo);
  2011. spin_unlock(&ino->i_lock);
  2012. _add_to_server_list(lo, NFS_SERVER(ino));
  2013. return lo;
  2014. out_unlock:
  2015. spin_unlock(&ino->i_lock);
  2016. pnfs_put_layout_hdr(lo);
  2017. return NULL;
  2018. }
  2019. static void _lgopen_prepare_attached(struct nfs4_opendata *data,
  2020. struct nfs_open_context *ctx)
  2021. {
  2022. struct inode *ino = data->dentry->d_inode;
  2023. struct pnfs_layout_range rng = {
  2024. .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
  2025. IOMODE_RW: IOMODE_READ,
  2026. .offset = 0,
  2027. .length = NFS4_MAX_UINT64,
  2028. };
  2029. struct nfs4_layoutget *lgp;
  2030. struct pnfs_layout_hdr *lo;
  2031. /* Heuristic: don't send layoutget if we have cached data */
  2032. if (rng.iomode == IOMODE_READ &&
  2033. (i_size_read(ino) == 0 || ino->i_mapping->nrpages != 0))
  2034. return;
  2035. lo = _pnfs_grab_empty_layout(ino, ctx);
  2036. if (!lo)
  2037. return;
  2038. lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &current_stateid, &rng,
  2039. nfs_io_gfp_mask());
  2040. if (!lgp) {
  2041. pnfs_clear_first_layoutget(lo);
  2042. nfs_layoutget_end(lo);
  2043. pnfs_put_layout_hdr(lo);
  2044. return;
  2045. }
  2046. lgp->lo = lo;
  2047. data->lgp = lgp;
  2048. data->o_arg.lg_args = &lgp->args;
  2049. data->o_res.lg_res = &lgp->res;
  2050. }
  2051. static void _lgopen_prepare_floating(struct nfs4_opendata *data,
  2052. struct nfs_open_context *ctx)
  2053. {
  2054. struct inode *ino = data->dentry->d_inode;
  2055. struct pnfs_layout_range rng = {
  2056. .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
  2057. IOMODE_RW: IOMODE_READ,
  2058. .offset = 0,
  2059. .length = NFS4_MAX_UINT64,
  2060. };
  2061. struct nfs4_layoutget *lgp;
  2062. lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &current_stateid, &rng,
  2063. nfs_io_gfp_mask());
  2064. if (!lgp)
  2065. return;
  2066. data->lgp = lgp;
  2067. data->o_arg.lg_args = &lgp->args;
  2068. data->o_res.lg_res = &lgp->res;
  2069. }
  2070. void pnfs_lgopen_prepare(struct nfs4_opendata *data,
  2071. struct nfs_open_context *ctx)
  2072. {
  2073. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  2074. if (!(pnfs_enabled_sb(server) &&
  2075. server->pnfs_curr_ld->flags & PNFS_LAYOUTGET_ON_OPEN))
  2076. return;
  2077. /* Could check on max_ops, but currently hardcoded high enough */
  2078. if (!nfs_server_capable(data->dir->d_inode, NFS_CAP_LGOPEN))
  2079. return;
  2080. if (data->lgp)
  2081. return;
  2082. if (data->state)
  2083. _lgopen_prepare_attached(data, ctx);
  2084. else
  2085. _lgopen_prepare_floating(data, ctx);
  2086. }
  2087. void pnfs_parse_lgopen(struct inode *ino, struct nfs4_layoutget *lgp,
  2088. struct nfs_open_context *ctx)
  2089. {
  2090. struct pnfs_layout_hdr *lo;
  2091. struct pnfs_layout_segment *lseg;
  2092. struct nfs_server *srv = NFS_SERVER(ino);
  2093. u32 iomode;
  2094. if (!lgp)
  2095. return;
  2096. dprintk("%s: entered with status %i\n", __func__, lgp->res.status);
  2097. if (lgp->res.status) {
  2098. switch (lgp->res.status) {
  2099. default:
  2100. break;
  2101. /*
  2102. * Halt lgopen attempts if the server doesn't recognise
  2103. * the "current stateid" value, the layout type, or the
  2104. * layoutget operation as being valid.
  2105. * Also if it complains about too many ops in the compound
  2106. * or of the request/reply being too big.
  2107. */
  2108. case -NFS4ERR_BAD_STATEID:
  2109. case -NFS4ERR_NOTSUPP:
  2110. case -NFS4ERR_REP_TOO_BIG:
  2111. case -NFS4ERR_REP_TOO_BIG_TO_CACHE:
  2112. case -NFS4ERR_REQ_TOO_BIG:
  2113. case -NFS4ERR_TOO_MANY_OPS:
  2114. case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
  2115. srv->caps &= ~NFS_CAP_LGOPEN;
  2116. }
  2117. return;
  2118. }
  2119. if (!lgp->lo) {
  2120. lo = _pnfs_grab_empty_layout(ino, ctx);
  2121. if (!lo)
  2122. return;
  2123. lgp->lo = lo;
  2124. } else
  2125. lo = lgp->lo;
  2126. lseg = pnfs_layout_process(lgp);
  2127. if (!IS_ERR(lseg)) {
  2128. iomode = lgp->args.range.iomode;
  2129. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  2130. pnfs_put_lseg(lseg);
  2131. }
  2132. }
  2133. void nfs4_lgopen_release(struct nfs4_layoutget *lgp)
  2134. {
  2135. if (lgp != NULL) {
  2136. if (lgp->lo) {
  2137. pnfs_clear_first_layoutget(lgp->lo);
  2138. nfs_layoutget_end(lgp->lo);
  2139. }
  2140. pnfs_layoutget_free(lgp);
  2141. }
  2142. }
  2143. struct pnfs_layout_segment *
  2144. pnfs_layout_process(struct nfs4_layoutget *lgp)
  2145. {
  2146. struct pnfs_layout_hdr *lo = lgp->lo;
  2147. struct nfs4_layoutget_res *res = &lgp->res;
  2148. struct pnfs_layout_segment *lseg;
  2149. struct inode *ino = lo->plh_inode;
  2150. LIST_HEAD(free_me);
  2151. if (!pnfs_sanity_check_layout_range(&res->range))
  2152. return ERR_PTR(-EINVAL);
  2153. /* Inject layout blob into I/O device driver */
  2154. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  2155. if (IS_ERR_OR_NULL(lseg)) {
  2156. if (!lseg)
  2157. lseg = ERR_PTR(-ENOMEM);
  2158. dprintk("%s: Could not allocate layout: error %ld\n",
  2159. __func__, PTR_ERR(lseg));
  2160. return lseg;
  2161. }
  2162. pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
  2163. spin_lock(&ino->i_lock);
  2164. if (pnfs_layoutgets_blocked(lo)) {
  2165. dprintk("%s forget reply due to state\n", __func__);
  2166. goto out_forget;
  2167. }
  2168. if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
  2169. !pnfs_is_first_layoutget(lo))
  2170. goto out_forget;
  2171. if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
  2172. /* existing state ID, make sure the sequence number matches. */
  2173. if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
  2174. if (!pnfs_layout_is_valid(lo))
  2175. lo->plh_barrier = 0;
  2176. dprintk("%s forget reply due to sequence\n", __func__);
  2177. goto out_forget;
  2178. }
  2179. pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, false);
  2180. } else if (pnfs_layout_is_valid(lo)) {
  2181. /*
  2182. * We got an entirely new state ID. Mark all segments for the
  2183. * inode invalid, and retry the layoutget
  2184. */
  2185. struct pnfs_layout_range range = {
  2186. .iomode = IOMODE_ANY,
  2187. .length = NFS4_MAX_UINT64,
  2188. };
  2189. pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0);
  2190. goto out_forget;
  2191. } else {
  2192. /* We have a completely new layout */
  2193. pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, true);
  2194. }
  2195. pnfs_get_lseg(lseg);
  2196. pnfs_layout_insert_lseg(lo, lseg, &free_me);
  2197. if (res->return_on_close)
  2198. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  2199. spin_unlock(&ino->i_lock);
  2200. pnfs_free_lseg_list(&free_me);
  2201. return lseg;
  2202. out_forget:
  2203. spin_unlock(&ino->i_lock);
  2204. lseg->pls_layout = lo;
  2205. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  2206. return ERR_PTR(-EAGAIN);
  2207. }
  2208. /**
  2209. * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
  2210. * @lo: pointer to layout header
  2211. * @tmp_list: list header to be used with pnfs_free_lseg_list()
  2212. * @return_range: describe layout segment ranges to be returned
  2213. * @seq: stateid seqid to match
  2214. *
  2215. * This function is mainly intended for use by layoutrecall. It attempts
  2216. * to free the layout segment immediately, or else to mark it for return
  2217. * as soon as its reference count drops to zero.
  2218. *
  2219. * Returns
  2220. * - 0: a layoutreturn needs to be scheduled.
  2221. * - EBUSY: there are layout segment that are still in use.
  2222. * - ENOENT: there are no layout segments that need to be returned.
  2223. */
  2224. int
  2225. pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
  2226. struct list_head *tmp_list,
  2227. const struct pnfs_layout_range *return_range,
  2228. u32 seq)
  2229. {
  2230. struct pnfs_layout_segment *lseg, *next;
  2231. struct nfs_server *server = NFS_SERVER(lo->plh_inode);
  2232. int remaining = 0;
  2233. dprintk("%s:Begin lo %p\n", __func__, lo);
  2234. assert_spin_locked(&lo->plh_inode->i_lock);
  2235. if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  2236. tmp_list = &lo->plh_return_segs;
  2237. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  2238. if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
  2239. dprintk("%s: marking lseg %p iomode %d "
  2240. "offset %llu length %llu\n", __func__,
  2241. lseg, lseg->pls_range.iomode,
  2242. lseg->pls_range.offset,
  2243. lseg->pls_range.length);
  2244. if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
  2245. tmp_list = &lo->plh_return_segs;
  2246. if (mark_lseg_invalid(lseg, tmp_list))
  2247. continue;
  2248. remaining++;
  2249. set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
  2250. pnfs_lseg_cancel_io(server, lseg);
  2251. }
  2252. if (remaining) {
  2253. pnfs_set_plh_return_info(lo, return_range->iomode, seq);
  2254. return -EBUSY;
  2255. }
  2256. if (!list_empty(&lo->plh_return_segs)) {
  2257. pnfs_set_plh_return_info(lo, return_range->iomode, seq);
  2258. return 0;
  2259. }
  2260. return -ENOENT;
  2261. }
  2262. static void
  2263. pnfs_mark_layout_for_return(struct inode *inode,
  2264. const struct pnfs_layout_range *range)
  2265. {
  2266. struct pnfs_layout_hdr *lo;
  2267. bool return_now = false;
  2268. spin_lock(&inode->i_lock);
  2269. lo = NFS_I(inode)->layout;
  2270. if (!pnfs_layout_is_valid(lo)) {
  2271. spin_unlock(&inode->i_lock);
  2272. return;
  2273. }
  2274. pnfs_set_plh_return_info(lo, range->iomode, 0);
  2275. /*
  2276. * mark all matching lsegs so that we are sure to have no live
  2277. * segments at hand when sending layoutreturn. See pnfs_put_lseg()
  2278. * for how it works.
  2279. */
  2280. if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, range, 0) != -EBUSY) {
  2281. const struct cred *cred;
  2282. nfs4_stateid stateid;
  2283. enum pnfs_iomode iomode;
  2284. return_now = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode);
  2285. spin_unlock(&inode->i_lock);
  2286. if (return_now)
  2287. pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false);
  2288. } else {
  2289. spin_unlock(&inode->i_lock);
  2290. nfs_commit_inode(inode, 0);
  2291. }
  2292. }
  2293. void pnfs_error_mark_layout_for_return(struct inode *inode,
  2294. struct pnfs_layout_segment *lseg)
  2295. {
  2296. struct pnfs_layout_range range = {
  2297. .iomode = lseg->pls_range.iomode,
  2298. .offset = 0,
  2299. .length = NFS4_MAX_UINT64,
  2300. };
  2301. pnfs_mark_layout_for_return(inode, &range);
  2302. }
  2303. EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
  2304. static bool
  2305. pnfs_layout_can_be_returned(struct pnfs_layout_hdr *lo)
  2306. {
  2307. return pnfs_layout_is_valid(lo) &&
  2308. !test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) &&
  2309. !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
  2310. }
  2311. static struct pnfs_layout_segment *
  2312. pnfs_find_first_lseg(struct pnfs_layout_hdr *lo,
  2313. const struct pnfs_layout_range *range,
  2314. enum pnfs_iomode iomode)
  2315. {
  2316. struct pnfs_layout_segment *lseg;
  2317. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  2318. if (!test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
  2319. continue;
  2320. if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
  2321. continue;
  2322. if (lseg->pls_range.iomode != iomode && iomode != IOMODE_ANY)
  2323. continue;
  2324. if (pnfs_lseg_range_intersecting(&lseg->pls_range, range))
  2325. return lseg;
  2326. }
  2327. return NULL;
  2328. }
  2329. /* Find open file states whose mode matches that of the range */
  2330. static bool
  2331. pnfs_should_return_unused_layout(struct pnfs_layout_hdr *lo,
  2332. const struct pnfs_layout_range *range)
  2333. {
  2334. struct list_head *head;
  2335. struct nfs_open_context *ctx;
  2336. fmode_t mode = 0;
  2337. if (!pnfs_layout_can_be_returned(lo) ||
  2338. !pnfs_find_first_lseg(lo, range, range->iomode))
  2339. return false;
  2340. head = &NFS_I(lo->plh_inode)->open_files;
  2341. list_for_each_entry_rcu(ctx, head, list) {
  2342. if (ctx->state)
  2343. mode |= ctx->state->state & (FMODE_READ|FMODE_WRITE);
  2344. }
  2345. switch (range->iomode) {
  2346. default:
  2347. break;
  2348. case IOMODE_READ:
  2349. mode &= ~FMODE_WRITE;
  2350. break;
  2351. case IOMODE_RW:
  2352. if (pnfs_find_first_lseg(lo, range, IOMODE_READ))
  2353. mode &= ~FMODE_READ;
  2354. }
  2355. return mode == 0;
  2356. }
  2357. static int pnfs_layout_return_unused_byserver(struct nfs_server *server,
  2358. void *data)
  2359. {
  2360. const struct pnfs_layout_range *range = data;
  2361. const struct cred *cred;
  2362. struct pnfs_layout_hdr *lo;
  2363. struct inode *inode;
  2364. nfs4_stateid stateid;
  2365. enum pnfs_iomode iomode;
  2366. restart:
  2367. rcu_read_lock();
  2368. list_for_each_entry_rcu(lo, &server->layouts, plh_layouts) {
  2369. inode = lo->plh_inode;
  2370. if (!inode || !pnfs_layout_can_be_returned(lo) ||
  2371. test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  2372. continue;
  2373. spin_lock(&inode->i_lock);
  2374. if (!lo->plh_inode ||
  2375. !pnfs_should_return_unused_layout(lo, range)) {
  2376. spin_unlock(&inode->i_lock);
  2377. continue;
  2378. }
  2379. pnfs_get_layout_hdr(lo);
  2380. pnfs_set_plh_return_info(lo, range->iomode, 0);
  2381. if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
  2382. range, 0) != 0 ||
  2383. !pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode)) {
  2384. spin_unlock(&inode->i_lock);
  2385. rcu_read_unlock();
  2386. pnfs_put_layout_hdr(lo);
  2387. cond_resched();
  2388. goto restart;
  2389. }
  2390. spin_unlock(&inode->i_lock);
  2391. rcu_read_unlock();
  2392. pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false);
  2393. pnfs_put_layout_hdr(lo);
  2394. cond_resched();
  2395. goto restart;
  2396. }
  2397. rcu_read_unlock();
  2398. return 0;
  2399. }
  2400. void
  2401. pnfs_layout_return_unused_byclid(struct nfs_client *clp,
  2402. enum pnfs_iomode iomode)
  2403. {
  2404. struct pnfs_layout_range range = {
  2405. .iomode = iomode,
  2406. .offset = 0,
  2407. .length = NFS4_MAX_UINT64,
  2408. };
  2409. nfs_client_for_each_server(clp, pnfs_layout_return_unused_byserver,
  2410. &range);
  2411. }
  2412. void
  2413. pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio)
  2414. {
  2415. if (pgio->pg_lseg == NULL ||
  2416. test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags))
  2417. return;
  2418. pnfs_put_lseg(pgio->pg_lseg);
  2419. pgio->pg_lseg = NULL;
  2420. }
  2421. EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
  2422. /*
  2423. * Check for any intersection between the request and the pgio->pg_lseg,
  2424. * and if none, put this pgio->pg_lseg away.
  2425. */
  2426. void
  2427. pnfs_generic_pg_check_range(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  2428. {
  2429. if (pgio->pg_lseg && !pnfs_lseg_request_intersecting(pgio->pg_lseg, req)) {
  2430. pnfs_put_lseg(pgio->pg_lseg);
  2431. pgio->pg_lseg = NULL;
  2432. }
  2433. }
  2434. EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_range);
  2435. void
  2436. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  2437. {
  2438. u64 rd_size;
  2439. pnfs_generic_pg_check_layout(pgio);
  2440. pnfs_generic_pg_check_range(pgio, req);
  2441. if (pgio->pg_lseg == NULL) {
  2442. if (pgio->pg_dreq == NULL)
  2443. rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
  2444. else
  2445. rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
  2446. pgio->pg_lseg =
  2447. pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
  2448. req_offset(req), rd_size,
  2449. IOMODE_READ, false,
  2450. nfs_io_gfp_mask());
  2451. if (IS_ERR(pgio->pg_lseg)) {
  2452. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  2453. pgio->pg_lseg = NULL;
  2454. return;
  2455. }
  2456. }
  2457. /* If no lseg, fall back to read through mds */
  2458. if (pgio->pg_lseg == NULL)
  2459. nfs_pageio_reset_read_mds(pgio);
  2460. }
  2461. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  2462. void
  2463. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
  2464. struct nfs_page *req, u64 wb_size)
  2465. {
  2466. pnfs_generic_pg_check_layout(pgio);
  2467. pnfs_generic_pg_check_range(pgio, req);
  2468. if (pgio->pg_lseg == NULL) {
  2469. pgio->pg_lseg =
  2470. pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
  2471. req_offset(req), wb_size, IOMODE_RW,
  2472. false, nfs_io_gfp_mask());
  2473. if (IS_ERR(pgio->pg_lseg)) {
  2474. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  2475. pgio->pg_lseg = NULL;
  2476. return;
  2477. }
  2478. }
  2479. /* If no lseg, fall back to write through mds */
  2480. if (pgio->pg_lseg == NULL)
  2481. nfs_pageio_reset_write_mds(pgio);
  2482. }
  2483. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  2484. void
  2485. pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
  2486. {
  2487. if (desc->pg_lseg) {
  2488. pnfs_put_lseg(desc->pg_lseg);
  2489. desc->pg_lseg = NULL;
  2490. }
  2491. }
  2492. EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
  2493. /*
  2494. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  2495. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  2496. */
  2497. size_t
  2498. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
  2499. struct nfs_page *prev, struct nfs_page *req)
  2500. {
  2501. unsigned int size;
  2502. u64 seg_end, req_start, seg_left;
  2503. size = nfs_generic_pg_test(pgio, prev, req);
  2504. if (!size)
  2505. return 0;
  2506. /*
  2507. * 'size' contains the number of bytes left in the current page (up
  2508. * to the original size asked for in @req->wb_bytes).
  2509. *
  2510. * Calculate how many bytes are left in the layout segment
  2511. * and if there are less bytes than 'size', return that instead.
  2512. *
  2513. * Please also note that 'end_offset' is actually the offset of the
  2514. * first byte that lies outside the pnfs_layout_range. FIXME?
  2515. *
  2516. */
  2517. if (pgio->pg_lseg) {
  2518. seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
  2519. pgio->pg_lseg->pls_range.length);
  2520. req_start = req_offset(req);
  2521. /* start of request is past the last byte of this segment */
  2522. if (req_start >= seg_end)
  2523. return 0;
  2524. /* adjust 'size' iff there are fewer bytes left in the
  2525. * segment than what nfs_generic_pg_test returned */
  2526. seg_left = seg_end - req_start;
  2527. if (seg_left < size)
  2528. size = (unsigned int)seg_left;
  2529. }
  2530. return size;
  2531. }
  2532. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  2533. int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
  2534. {
  2535. struct nfs_pageio_descriptor pgio;
  2536. /* Resend all requests through the MDS */
  2537. nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
  2538. hdr->completion_ops);
  2539. return nfs_pageio_resend(&pgio, hdr);
  2540. }
  2541. EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
  2542. static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
  2543. {
  2544. dprintk("pnfs write error = %d\n", hdr->pnfs_error);
  2545. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  2546. PNFS_LAYOUTRET_ON_ERROR) {
  2547. pnfs_return_layout(hdr->inode);
  2548. }
  2549. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  2550. hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
  2551. }
  2552. /*
  2553. * Called by non rpc-based layout drivers
  2554. */
  2555. void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
  2556. {
  2557. if (likely(!hdr->pnfs_error)) {
  2558. pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
  2559. hdr->mds_offset + hdr->res.count);
  2560. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  2561. }
  2562. trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
  2563. if (unlikely(hdr->pnfs_error))
  2564. pnfs_ld_handle_write_error(hdr);
  2565. hdr->mds_ops->rpc_release(hdr);
  2566. }
  2567. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  2568. static void
  2569. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  2570. struct nfs_pgio_header *hdr)
  2571. {
  2572. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2573. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2574. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  2575. nfs_pageio_reset_write_mds(desc);
  2576. mirror->pg_recoalesce = 1;
  2577. }
  2578. hdr->completion_ops->completion(hdr);
  2579. }
  2580. static enum pnfs_try_status
  2581. pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
  2582. const struct rpc_call_ops *call_ops,
  2583. struct pnfs_layout_segment *lseg,
  2584. int how)
  2585. {
  2586. struct inode *inode = hdr->inode;
  2587. enum pnfs_try_status trypnfs;
  2588. struct nfs_server *nfss = NFS_SERVER(inode);
  2589. hdr->mds_ops = call_ops;
  2590. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  2591. inode->i_ino, hdr->args.count, hdr->args.offset, how);
  2592. trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
  2593. if (trypnfs != PNFS_NOT_ATTEMPTED)
  2594. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  2595. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  2596. return trypnfs;
  2597. }
  2598. static void
  2599. pnfs_do_write(struct nfs_pageio_descriptor *desc,
  2600. struct nfs_pgio_header *hdr, int how)
  2601. {
  2602. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  2603. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  2604. enum pnfs_try_status trypnfs;
  2605. trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
  2606. switch (trypnfs) {
  2607. case PNFS_NOT_ATTEMPTED:
  2608. pnfs_write_through_mds(desc, hdr);
  2609. break;
  2610. case PNFS_ATTEMPTED:
  2611. break;
  2612. case PNFS_TRY_AGAIN:
  2613. /* cleanup hdr and prepare to redo pnfs */
  2614. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2615. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2616. list_splice_init(&hdr->pages, &mirror->pg_list);
  2617. mirror->pg_recoalesce = 1;
  2618. }
  2619. hdr->mds_ops->rpc_release(hdr);
  2620. }
  2621. }
  2622. static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
  2623. {
  2624. pnfs_put_lseg(hdr->lseg);
  2625. nfs_pgio_header_free(hdr);
  2626. }
  2627. int
  2628. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  2629. {
  2630. struct nfs_pgio_header *hdr;
  2631. int ret;
  2632. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  2633. if (!hdr) {
  2634. desc->pg_error = -ENOMEM;
  2635. return desc->pg_error;
  2636. }
  2637. nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
  2638. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  2639. ret = nfs_generic_pgio(desc, hdr);
  2640. if (!ret)
  2641. pnfs_do_write(desc, hdr, desc->pg_ioflags);
  2642. return ret;
  2643. }
  2644. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  2645. int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
  2646. {
  2647. struct nfs_pageio_descriptor pgio;
  2648. /* Resend all requests through the MDS */
  2649. nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
  2650. return nfs_pageio_resend(&pgio, hdr);
  2651. }
  2652. EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
  2653. static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
  2654. {
  2655. dprintk("pnfs read error = %d\n", hdr->pnfs_error);
  2656. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  2657. PNFS_LAYOUTRET_ON_ERROR) {
  2658. pnfs_return_layout(hdr->inode);
  2659. }
  2660. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  2661. hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
  2662. }
  2663. /*
  2664. * Called by non rpc-based layout drivers
  2665. */
  2666. void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
  2667. {
  2668. if (likely(!hdr->pnfs_error))
  2669. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  2670. trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
  2671. if (unlikely(hdr->pnfs_error))
  2672. pnfs_ld_handle_read_error(hdr);
  2673. hdr->mds_ops->rpc_release(hdr);
  2674. }
  2675. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  2676. static void
  2677. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  2678. struct nfs_pgio_header *hdr)
  2679. {
  2680. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2681. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2682. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  2683. nfs_pageio_reset_read_mds(desc);
  2684. mirror->pg_recoalesce = 1;
  2685. }
  2686. hdr->completion_ops->completion(hdr);
  2687. }
  2688. /*
  2689. * Call the appropriate parallel I/O subsystem read function.
  2690. */
  2691. static enum pnfs_try_status
  2692. pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
  2693. const struct rpc_call_ops *call_ops,
  2694. struct pnfs_layout_segment *lseg)
  2695. {
  2696. struct inode *inode = hdr->inode;
  2697. struct nfs_server *nfss = NFS_SERVER(inode);
  2698. enum pnfs_try_status trypnfs;
  2699. hdr->mds_ops = call_ops;
  2700. dprintk("%s: Reading ino:%lu %u@%llu\n",
  2701. __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
  2702. trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
  2703. if (trypnfs != PNFS_NOT_ATTEMPTED)
  2704. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  2705. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  2706. return trypnfs;
  2707. }
  2708. /* Resend all requests through pnfs. */
  2709. void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr,
  2710. unsigned int mirror_idx)
  2711. {
  2712. struct nfs_pageio_descriptor pgio;
  2713. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2714. /* Prevent deadlocks with layoutreturn! */
  2715. pnfs_put_lseg(hdr->lseg);
  2716. hdr->lseg = NULL;
  2717. nfs_pageio_init_read(&pgio, hdr->inode, false,
  2718. hdr->completion_ops);
  2719. pgio.pg_mirror_idx = mirror_idx;
  2720. hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
  2721. }
  2722. }
  2723. EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
  2724. static void
  2725. pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
  2726. {
  2727. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  2728. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  2729. enum pnfs_try_status trypnfs;
  2730. trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
  2731. switch (trypnfs) {
  2732. case PNFS_NOT_ATTEMPTED:
  2733. pnfs_read_through_mds(desc, hdr);
  2734. break;
  2735. case PNFS_ATTEMPTED:
  2736. break;
  2737. case PNFS_TRY_AGAIN:
  2738. /* cleanup hdr and prepare to redo pnfs */
  2739. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2740. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2741. list_splice_init(&hdr->pages, &mirror->pg_list);
  2742. mirror->pg_recoalesce = 1;
  2743. }
  2744. hdr->mds_ops->rpc_release(hdr);
  2745. }
  2746. }
  2747. static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
  2748. {
  2749. pnfs_put_lseg(hdr->lseg);
  2750. nfs_pgio_header_free(hdr);
  2751. }
  2752. int
  2753. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  2754. {
  2755. struct nfs_pgio_header *hdr;
  2756. int ret;
  2757. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  2758. if (!hdr) {
  2759. desc->pg_error = -ENOMEM;
  2760. return desc->pg_error;
  2761. }
  2762. nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
  2763. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  2764. ret = nfs_generic_pgio(desc, hdr);
  2765. if (!ret)
  2766. pnfs_do_read(desc, hdr);
  2767. return ret;
  2768. }
  2769. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  2770. static void pnfs_clear_layoutcommitting(struct inode *inode)
  2771. {
  2772. unsigned long *bitlock = &NFS_I(inode)->flags;
  2773. clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
  2774. smp_mb__after_atomic();
  2775. wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
  2776. }
  2777. /*
  2778. * There can be multiple RW segments.
  2779. */
  2780. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  2781. {
  2782. struct pnfs_layout_segment *lseg;
  2783. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  2784. if (lseg->pls_range.iomode == IOMODE_RW &&
  2785. test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  2786. list_add(&lseg->pls_lc_list, listp);
  2787. }
  2788. }
  2789. static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
  2790. {
  2791. struct pnfs_layout_segment *lseg, *tmp;
  2792. /* Matched by references in pnfs_set_layoutcommit */
  2793. list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
  2794. list_del_init(&lseg->pls_lc_list);
  2795. pnfs_put_lseg(lseg);
  2796. }
  2797. pnfs_clear_layoutcommitting(inode);
  2798. }
  2799. void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
  2800. {
  2801. pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
  2802. }
  2803. EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
  2804. void
  2805. pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
  2806. loff_t end_pos)
  2807. {
  2808. struct nfs_inode *nfsi = NFS_I(inode);
  2809. bool mark_as_dirty = false;
  2810. spin_lock(&inode->i_lock);
  2811. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  2812. nfsi->layout->plh_lwb = end_pos;
  2813. mark_as_dirty = true;
  2814. dprintk("%s: Set layoutcommit for inode %lu ",
  2815. __func__, inode->i_ino);
  2816. } else if (end_pos > nfsi->layout->plh_lwb)
  2817. nfsi->layout->plh_lwb = end_pos;
  2818. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
  2819. /* references matched in nfs4_layoutcommit_release */
  2820. pnfs_get_lseg(lseg);
  2821. }
  2822. spin_unlock(&inode->i_lock);
  2823. dprintk("%s: lseg %p end_pos %llu\n",
  2824. __func__, lseg, nfsi->layout->plh_lwb);
  2825. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  2826. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  2827. if (mark_as_dirty)
  2828. mark_inode_dirty_sync(inode);
  2829. }
  2830. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  2831. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  2832. {
  2833. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  2834. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  2835. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  2836. pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
  2837. }
  2838. /*
  2839. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  2840. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  2841. * data to disk to allow the server to recover the data if it crashes.
  2842. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  2843. * is off, and a COMMIT is sent to a data server, or
  2844. * if WRITEs to a data server return NFS_DATA_SYNC.
  2845. */
  2846. int
  2847. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  2848. {
  2849. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  2850. struct nfs4_layoutcommit_data *data;
  2851. struct nfs_inode *nfsi = NFS_I(inode);
  2852. loff_t end_pos;
  2853. int status;
  2854. if (!pnfs_layoutcommit_outstanding(inode))
  2855. return 0;
  2856. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  2857. status = -EAGAIN;
  2858. if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
  2859. if (!sync)
  2860. goto out;
  2861. status = wait_on_bit_lock_action(&nfsi->flags,
  2862. NFS_INO_LAYOUTCOMMITTING,
  2863. nfs_wait_bit_killable,
  2864. TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
  2865. if (status)
  2866. goto out;
  2867. }
  2868. status = -ENOMEM;
  2869. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  2870. data = kzalloc(sizeof(*data), nfs_io_gfp_mask());
  2871. if (!data)
  2872. goto clear_layoutcommitting;
  2873. status = 0;
  2874. spin_lock(&inode->i_lock);
  2875. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  2876. goto out_unlock;
  2877. INIT_LIST_HEAD(&data->lseg_list);
  2878. pnfs_list_write_lseg(inode, &data->lseg_list);
  2879. end_pos = nfsi->layout->plh_lwb;
  2880. nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
  2881. data->cred = get_cred(nfsi->layout->plh_lc_cred);
  2882. spin_unlock(&inode->i_lock);
  2883. data->args.inode = inode;
  2884. nfs_fattr_init(&data->fattr);
  2885. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  2886. data->res.fattr = &data->fattr;
  2887. if (end_pos != 0)
  2888. data->args.lastbytewritten = end_pos - 1;
  2889. else
  2890. data->args.lastbytewritten = U64_MAX;
  2891. data->res.server = NFS_SERVER(inode);
  2892. if (ld->prepare_layoutcommit) {
  2893. status = ld->prepare_layoutcommit(&data->args);
  2894. if (status) {
  2895. put_cred(data->cred);
  2896. spin_lock(&inode->i_lock);
  2897. set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
  2898. if (end_pos > nfsi->layout->plh_lwb)
  2899. nfsi->layout->plh_lwb = end_pos;
  2900. goto out_unlock;
  2901. }
  2902. }
  2903. status = nfs4_proc_layoutcommit(data, sync);
  2904. out:
  2905. if (status)
  2906. mark_inode_dirty_sync(inode);
  2907. dprintk("<-- %s status %d\n", __func__, status);
  2908. return status;
  2909. out_unlock:
  2910. spin_unlock(&inode->i_lock);
  2911. kfree(data);
  2912. clear_layoutcommitting:
  2913. pnfs_clear_layoutcommitting(inode);
  2914. goto out;
  2915. }
  2916. EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
  2917. int
  2918. pnfs_generic_sync(struct inode *inode, bool datasync)
  2919. {
  2920. return pnfs_layoutcommit_inode(inode, true);
  2921. }
  2922. EXPORT_SYMBOL_GPL(pnfs_generic_sync);
  2923. struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
  2924. {
  2925. struct nfs4_threshold *thp;
  2926. thp = kzalloc(sizeof(*thp), nfs_io_gfp_mask());
  2927. if (!thp) {
  2928. dprintk("%s mdsthreshold allocation failed\n", __func__);
  2929. return NULL;
  2930. }
  2931. return thp;
  2932. }
  2933. #if IS_ENABLED(CONFIG_NFS_V4_2)
  2934. int
  2935. pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
  2936. {
  2937. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  2938. struct nfs_server *server = NFS_SERVER(inode);
  2939. struct nfs_inode *nfsi = NFS_I(inode);
  2940. struct nfs42_layoutstat_data *data;
  2941. struct pnfs_layout_hdr *hdr;
  2942. int status = 0;
  2943. if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
  2944. goto out;
  2945. if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
  2946. goto out;
  2947. if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
  2948. goto out;
  2949. spin_lock(&inode->i_lock);
  2950. if (!NFS_I(inode)->layout) {
  2951. spin_unlock(&inode->i_lock);
  2952. goto out_clear_layoutstats;
  2953. }
  2954. hdr = NFS_I(inode)->layout;
  2955. pnfs_get_layout_hdr(hdr);
  2956. spin_unlock(&inode->i_lock);
  2957. data = kzalloc(sizeof(*data), gfp_flags);
  2958. if (!data) {
  2959. status = -ENOMEM;
  2960. goto out_put;
  2961. }
  2962. data->args.fh = NFS_FH(inode);
  2963. data->args.inode = inode;
  2964. status = ld->prepare_layoutstats(&data->args);
  2965. if (status)
  2966. goto out_free;
  2967. status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
  2968. out:
  2969. dprintk("%s returns %d\n", __func__, status);
  2970. return status;
  2971. out_free:
  2972. kfree(data);
  2973. out_put:
  2974. pnfs_put_layout_hdr(hdr);
  2975. out_clear_layoutstats:
  2976. smp_mb__before_atomic();
  2977. clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
  2978. smp_mb__after_atomic();
  2979. goto out;
  2980. }
  2981. EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
  2982. #endif
  2983. unsigned int layoutstats_timer;
  2984. module_param(layoutstats_timer, uint, 0644);
  2985. EXPORT_SYMBOL_GPL(layoutstats_timer);