pnfs.c 52 KB

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