pnfs.c 79 KB

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