pnfs.c 60 KB

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