pnfs.c 69 KB

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