pnfs.c 46 KB

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