pnfs.c 66 KB

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