pnfs.c 50 KB

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