pnfs.c 72 KB

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