pnfs.c 47 KB

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