pnfs.c 72 KB

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