pnfs.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. /*
  487. * Compare 2 layout stateid sequence ids, to see which is newer,
  488. * taking into account wraparound issues.
  489. */
  490. static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
  491. {
  492. return (s32)s1 - (s32)s2 > 0;
  493. }
  494. /* update lo->plh_stateid with new if is more recent */
  495. void
  496. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  497. bool update_barrier)
  498. {
  499. u32 oldseq, newseq, new_barrier;
  500. int empty = list_empty(&lo->plh_segs);
  501. oldseq = be32_to_cpu(lo->plh_stateid.seqid);
  502. newseq = be32_to_cpu(new->seqid);
  503. if (empty || pnfs_seqid_is_newer(newseq, oldseq)) {
  504. nfs4_stateid_copy(&lo->plh_stateid, new);
  505. if (update_barrier) {
  506. new_barrier = be32_to_cpu(new->seqid);
  507. } else {
  508. /* Because of wraparound, we want to keep the barrier
  509. * "close" to the current seqids.
  510. */
  511. new_barrier = newseq - atomic_read(&lo->plh_outstanding);
  512. }
  513. if (empty || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
  514. lo->plh_barrier = new_barrier;
  515. }
  516. }
  517. static bool
  518. pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
  519. const nfs4_stateid *stateid)
  520. {
  521. u32 seqid = be32_to_cpu(stateid->seqid);
  522. return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
  523. }
  524. /* lget is set to 1 if called from inside send_layoutget call chain */
  525. static bool
  526. pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo, int lget)
  527. {
  528. return lo->plh_block_lgets ||
  529. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  530. (list_empty(&lo->plh_segs) &&
  531. (atomic_read(&lo->plh_outstanding) > lget));
  532. }
  533. int
  534. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  535. struct nfs4_state *open_state)
  536. {
  537. int status = 0;
  538. dprintk("--> %s\n", __func__);
  539. spin_lock(&lo->plh_inode->i_lock);
  540. if (pnfs_layoutgets_blocked(lo, 1)) {
  541. status = -EAGAIN;
  542. } else if (list_empty(&lo->plh_segs)) {
  543. int seq;
  544. do {
  545. seq = read_seqbegin(&open_state->seqlock);
  546. nfs4_stateid_copy(dst, &open_state->stateid);
  547. } while (read_seqretry(&open_state->seqlock, seq));
  548. } else
  549. nfs4_stateid_copy(dst, &lo->plh_stateid);
  550. spin_unlock(&lo->plh_inode->i_lock);
  551. dprintk("<-- %s\n", __func__);
  552. return status;
  553. }
  554. /*
  555. * Get layout from server.
  556. * for now, assume that whole file layouts are requested.
  557. * arg->offset: 0
  558. * arg->length: all ones
  559. */
  560. static struct pnfs_layout_segment *
  561. send_layoutget(struct pnfs_layout_hdr *lo,
  562. struct nfs_open_context *ctx,
  563. struct pnfs_layout_range *range,
  564. gfp_t gfp_flags)
  565. {
  566. struct inode *ino = lo->plh_inode;
  567. struct nfs_server *server = NFS_SERVER(ino);
  568. struct nfs4_layoutget *lgp;
  569. struct pnfs_layout_segment *lseg;
  570. dprintk("--> %s\n", __func__);
  571. BUG_ON(ctx == NULL);
  572. lgp = kzalloc(sizeof(*lgp), gfp_flags);
  573. if (lgp == NULL)
  574. return NULL;
  575. lgp->args.minlength = PAGE_CACHE_SIZE;
  576. if (lgp->args.minlength > range->length)
  577. lgp->args.minlength = range->length;
  578. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  579. lgp->args.range = *range;
  580. lgp->args.type = server->pnfs_curr_ld->id;
  581. lgp->args.inode = ino;
  582. lgp->args.ctx = get_nfs_open_context(ctx);
  583. lgp->gfp_flags = gfp_flags;
  584. /* Synchronously retrieve layout information from server and
  585. * store in lseg.
  586. */
  587. lseg = nfs4_proc_layoutget(lgp, gfp_flags);
  588. if (IS_ERR(lseg)) {
  589. switch (PTR_ERR(lseg)) {
  590. case -ENOMEM:
  591. case -ERESTARTSYS:
  592. break;
  593. default:
  594. /* remember that LAYOUTGET failed and suspend trying */
  595. pnfs_layout_io_set_failed(lo, range->iomode);
  596. }
  597. return NULL;
  598. }
  599. return lseg;
  600. }
  601. /*
  602. * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
  603. * when the layout segment list is empty.
  604. *
  605. * Note that a pnfs_layout_hdr can exist with an empty layout segment
  606. * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
  607. * deviceid is marked invalid.
  608. */
  609. int
  610. _pnfs_return_layout(struct inode *ino)
  611. {
  612. struct pnfs_layout_hdr *lo = NULL;
  613. struct nfs_inode *nfsi = NFS_I(ino);
  614. LIST_HEAD(tmp_list);
  615. struct nfs4_layoutreturn *lrp;
  616. nfs4_stateid stateid;
  617. int status = 0, empty;
  618. dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
  619. spin_lock(&ino->i_lock);
  620. lo = nfsi->layout;
  621. if (!lo) {
  622. spin_unlock(&ino->i_lock);
  623. dprintk("NFS: %s no layout to return\n", __func__);
  624. goto out;
  625. }
  626. stateid = nfsi->layout->plh_stateid;
  627. /* Reference matched in nfs4_layoutreturn_release */
  628. pnfs_get_layout_hdr(lo);
  629. empty = list_empty(&lo->plh_segs);
  630. pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
  631. /* Don't send a LAYOUTRETURN if list was initially empty */
  632. if (empty) {
  633. spin_unlock(&ino->i_lock);
  634. pnfs_put_layout_hdr(lo);
  635. dprintk("NFS: %s no layout segments to return\n", __func__);
  636. goto out;
  637. }
  638. lo->plh_block_lgets++;
  639. spin_unlock(&ino->i_lock);
  640. pnfs_free_lseg_list(&tmp_list);
  641. WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
  642. lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
  643. if (unlikely(lrp == NULL)) {
  644. status = -ENOMEM;
  645. spin_lock(&ino->i_lock);
  646. lo->plh_block_lgets--;
  647. spin_unlock(&ino->i_lock);
  648. pnfs_put_layout_hdr(lo);
  649. goto out;
  650. }
  651. lrp->args.stateid = stateid;
  652. lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
  653. lrp->args.inode = ino;
  654. lrp->args.layout = lo;
  655. lrp->clp = NFS_SERVER(ino)->nfs_client;
  656. status = nfs4_proc_layoutreturn(lrp);
  657. out:
  658. dprintk("<-- %s status: %d\n", __func__, status);
  659. return status;
  660. }
  661. EXPORT_SYMBOL_GPL(_pnfs_return_layout);
  662. bool pnfs_roc(struct inode *ino)
  663. {
  664. struct pnfs_layout_hdr *lo;
  665. struct pnfs_layout_segment *lseg, *tmp;
  666. LIST_HEAD(tmp_list);
  667. bool found = false;
  668. spin_lock(&ino->i_lock);
  669. lo = NFS_I(ino)->layout;
  670. if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
  671. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
  672. goto out_nolayout;
  673. list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
  674. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  675. mark_lseg_invalid(lseg, &tmp_list);
  676. found = true;
  677. }
  678. if (!found)
  679. goto out_nolayout;
  680. lo->plh_block_lgets++;
  681. pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
  682. spin_unlock(&ino->i_lock);
  683. pnfs_free_lseg_list(&tmp_list);
  684. return true;
  685. out_nolayout:
  686. spin_unlock(&ino->i_lock);
  687. return false;
  688. }
  689. void pnfs_roc_release(struct inode *ino)
  690. {
  691. struct pnfs_layout_hdr *lo;
  692. spin_lock(&ino->i_lock);
  693. lo = NFS_I(ino)->layout;
  694. lo->plh_block_lgets--;
  695. if (atomic_dec_and_test(&lo->plh_refcount)) {
  696. pnfs_detach_layout_hdr(lo);
  697. spin_unlock(&ino->i_lock);
  698. pnfs_free_layout_hdr(lo);
  699. } else
  700. spin_unlock(&ino->i_lock);
  701. }
  702. void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
  703. {
  704. struct pnfs_layout_hdr *lo;
  705. spin_lock(&ino->i_lock);
  706. lo = NFS_I(ino)->layout;
  707. if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
  708. lo->plh_barrier = barrier;
  709. spin_unlock(&ino->i_lock);
  710. }
  711. bool pnfs_roc_drain(struct inode *ino, u32 *barrier, struct rpc_task *task)
  712. {
  713. struct nfs_inode *nfsi = NFS_I(ino);
  714. struct pnfs_layout_hdr *lo;
  715. struct pnfs_layout_segment *lseg;
  716. u32 current_seqid;
  717. bool found = false;
  718. spin_lock(&ino->i_lock);
  719. list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
  720. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  721. rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
  722. found = true;
  723. goto out;
  724. }
  725. lo = nfsi->layout;
  726. current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
  727. /* Since close does not return a layout stateid for use as
  728. * a barrier, we choose the worst-case barrier.
  729. */
  730. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  731. out:
  732. spin_unlock(&ino->i_lock);
  733. return found;
  734. }
  735. /*
  736. * Compare two layout segments for sorting into layout cache.
  737. * We want to preferentially return RW over RO layouts, so ensure those
  738. * are seen first.
  739. */
  740. static s64
  741. cmp_layout(struct pnfs_layout_range *l1,
  742. struct pnfs_layout_range *l2)
  743. {
  744. s64 d;
  745. /* high offset > low offset */
  746. d = l1->offset - l2->offset;
  747. if (d)
  748. return d;
  749. /* short length > long length */
  750. d = l2->length - l1->length;
  751. if (d)
  752. return d;
  753. /* read > read/write */
  754. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  755. }
  756. static void
  757. pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  758. struct pnfs_layout_segment *lseg)
  759. {
  760. struct pnfs_layout_segment *lp;
  761. dprintk("%s:Begin\n", __func__);
  762. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  763. if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
  764. continue;
  765. list_add_tail(&lseg->pls_list, &lp->pls_list);
  766. dprintk("%s: inserted lseg %p "
  767. "iomode %d offset %llu length %llu before "
  768. "lp %p iomode %d offset %llu length %llu\n",
  769. __func__, lseg, lseg->pls_range.iomode,
  770. lseg->pls_range.offset, lseg->pls_range.length,
  771. lp, lp->pls_range.iomode, lp->pls_range.offset,
  772. lp->pls_range.length);
  773. goto out;
  774. }
  775. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  776. dprintk("%s: inserted lseg %p "
  777. "iomode %d offset %llu length %llu at tail\n",
  778. __func__, lseg, lseg->pls_range.iomode,
  779. lseg->pls_range.offset, lseg->pls_range.length);
  780. out:
  781. pnfs_get_layout_hdr(lo);
  782. dprintk("%s:Return\n", __func__);
  783. }
  784. static struct pnfs_layout_hdr *
  785. alloc_init_layout_hdr(struct inode *ino,
  786. struct nfs_open_context *ctx,
  787. gfp_t gfp_flags)
  788. {
  789. struct pnfs_layout_hdr *lo;
  790. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  791. if (!lo)
  792. return NULL;
  793. atomic_set(&lo->plh_refcount, 1);
  794. INIT_LIST_HEAD(&lo->plh_layouts);
  795. INIT_LIST_HEAD(&lo->plh_segs);
  796. INIT_LIST_HEAD(&lo->plh_bulk_recall);
  797. lo->plh_inode = ino;
  798. lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
  799. return lo;
  800. }
  801. static struct pnfs_layout_hdr *
  802. pnfs_find_alloc_layout(struct inode *ino,
  803. struct nfs_open_context *ctx,
  804. gfp_t gfp_flags)
  805. {
  806. struct nfs_inode *nfsi = NFS_I(ino);
  807. struct pnfs_layout_hdr *new = NULL;
  808. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  809. if (nfsi->layout != NULL)
  810. goto out_existing;
  811. spin_unlock(&ino->i_lock);
  812. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  813. spin_lock(&ino->i_lock);
  814. if (likely(nfsi->layout == NULL)) { /* Won the race? */
  815. nfsi->layout = new;
  816. return new;
  817. }
  818. pnfs_free_layout_hdr(new);
  819. out_existing:
  820. pnfs_get_layout_hdr(nfsi->layout);
  821. return nfsi->layout;
  822. }
  823. /*
  824. * iomode matching rules:
  825. * iomode lseg match
  826. * ----- ----- -----
  827. * ANY READ true
  828. * ANY RW true
  829. * RW READ false
  830. * RW RW true
  831. * READ READ true
  832. * READ RW true
  833. */
  834. static int
  835. is_matching_lseg(struct pnfs_layout_range *ls_range,
  836. struct pnfs_layout_range *range)
  837. {
  838. struct pnfs_layout_range range1;
  839. if ((range->iomode == IOMODE_RW &&
  840. ls_range->iomode != IOMODE_RW) ||
  841. !lo_seg_intersecting(ls_range, range))
  842. return 0;
  843. /* range1 covers only the first byte in the range */
  844. range1 = *range;
  845. range1.length = 1;
  846. return lo_seg_contained(ls_range, &range1);
  847. }
  848. /*
  849. * lookup range in layout
  850. */
  851. static struct pnfs_layout_segment *
  852. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  853. struct pnfs_layout_range *range)
  854. {
  855. struct pnfs_layout_segment *lseg, *ret = NULL;
  856. dprintk("%s:Begin\n", __func__);
  857. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  858. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  859. is_matching_lseg(&lseg->pls_range, range)) {
  860. ret = pnfs_get_lseg(lseg);
  861. break;
  862. }
  863. if (lseg->pls_range.offset > range->offset)
  864. break;
  865. }
  866. dprintk("%s:Return lseg %p ref %d\n",
  867. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  868. return ret;
  869. }
  870. /*
  871. * Use mdsthreshold hints set at each OPEN to determine if I/O should go
  872. * to the MDS or over pNFS
  873. *
  874. * The nfs_inode read_io and write_io fields are cumulative counters reset
  875. * when there are no layout segments. Note that in pnfs_update_layout iomode
  876. * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
  877. * WRITE request.
  878. *
  879. * A return of true means use MDS I/O.
  880. *
  881. * From rfc 5661:
  882. * If a file's size is smaller than the file size threshold, data accesses
  883. * SHOULD be sent to the metadata server. If an I/O request has a length that
  884. * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
  885. * server. If both file size and I/O size are provided, the client SHOULD
  886. * reach or exceed both thresholds before sending its read or write
  887. * requests to the data server.
  888. */
  889. static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
  890. struct inode *ino, int iomode)
  891. {
  892. struct nfs4_threshold *t = ctx->mdsthreshold;
  893. struct nfs_inode *nfsi = NFS_I(ino);
  894. loff_t fsize = i_size_read(ino);
  895. bool size = false, size_set = false, io = false, io_set = false, ret = false;
  896. if (t == NULL)
  897. return ret;
  898. dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
  899. __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
  900. switch (iomode) {
  901. case IOMODE_READ:
  902. if (t->bm & THRESHOLD_RD) {
  903. dprintk("%s fsize %llu\n", __func__, fsize);
  904. size_set = true;
  905. if (fsize < t->rd_sz)
  906. size = true;
  907. }
  908. if (t->bm & THRESHOLD_RD_IO) {
  909. dprintk("%s nfsi->read_io %llu\n", __func__,
  910. nfsi->read_io);
  911. io_set = true;
  912. if (nfsi->read_io < t->rd_io_sz)
  913. io = true;
  914. }
  915. break;
  916. case IOMODE_RW:
  917. if (t->bm & THRESHOLD_WR) {
  918. dprintk("%s fsize %llu\n", __func__, fsize);
  919. size_set = true;
  920. if (fsize < t->wr_sz)
  921. size = true;
  922. }
  923. if (t->bm & THRESHOLD_WR_IO) {
  924. dprintk("%s nfsi->write_io %llu\n", __func__,
  925. nfsi->write_io);
  926. io_set = true;
  927. if (nfsi->write_io < t->wr_io_sz)
  928. io = true;
  929. }
  930. break;
  931. }
  932. if (size_set && io_set) {
  933. if (size && io)
  934. ret = true;
  935. } else if (size || io)
  936. ret = true;
  937. dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
  938. return ret;
  939. }
  940. /*
  941. * Layout segment is retreived from the server if not cached.
  942. * The appropriate layout segment is referenced and returned to the caller.
  943. */
  944. struct pnfs_layout_segment *
  945. pnfs_update_layout(struct inode *ino,
  946. struct nfs_open_context *ctx,
  947. loff_t pos,
  948. u64 count,
  949. enum pnfs_iomode iomode,
  950. gfp_t gfp_flags)
  951. {
  952. struct pnfs_layout_range arg = {
  953. .iomode = iomode,
  954. .offset = pos,
  955. .length = count,
  956. };
  957. unsigned pg_offset;
  958. struct nfs_server *server = NFS_SERVER(ino);
  959. struct nfs_client *clp = server->nfs_client;
  960. struct pnfs_layout_hdr *lo;
  961. struct pnfs_layout_segment *lseg = NULL;
  962. bool first = false;
  963. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  964. goto out;
  965. if (pnfs_within_mdsthreshold(ctx, ino, iomode))
  966. goto out;
  967. spin_lock(&ino->i_lock);
  968. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  969. if (lo == NULL) {
  970. spin_unlock(&ino->i_lock);
  971. goto out;
  972. }
  973. /* Do we even need to bother with this? */
  974. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  975. dprintk("%s matches recall, use MDS\n", __func__);
  976. goto out_unlock;
  977. }
  978. /* if LAYOUTGET already failed once we don't try again */
  979. if (pnfs_layout_io_test_failed(lo, iomode))
  980. goto out_unlock;
  981. /* Check to see if the layout for the given range already exists */
  982. lseg = pnfs_find_lseg(lo, &arg);
  983. if (lseg)
  984. goto out_unlock;
  985. if (pnfs_layoutgets_blocked(lo, 0))
  986. goto out_unlock;
  987. atomic_inc(&lo->plh_outstanding);
  988. if (list_empty(&lo->plh_segs))
  989. first = true;
  990. spin_unlock(&ino->i_lock);
  991. if (first) {
  992. /* The lo must be on the clp list if there is any
  993. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  994. */
  995. spin_lock(&clp->cl_lock);
  996. BUG_ON(!list_empty(&lo->plh_layouts));
  997. list_add_tail(&lo->plh_layouts, &server->layouts);
  998. spin_unlock(&clp->cl_lock);
  999. }
  1000. pg_offset = arg.offset & ~PAGE_CACHE_MASK;
  1001. if (pg_offset) {
  1002. arg.offset -= pg_offset;
  1003. arg.length += pg_offset;
  1004. }
  1005. if (arg.length != NFS4_MAX_UINT64)
  1006. arg.length = PAGE_CACHE_ALIGN(arg.length);
  1007. lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
  1008. atomic_dec(&lo->plh_outstanding);
  1009. out_put_layout_hdr:
  1010. pnfs_put_layout_hdr(lo);
  1011. out:
  1012. dprintk("%s: inode %s/%llu pNFS layout segment %s for "
  1013. "(%s, offset: %llu, length: %llu)\n",
  1014. __func__, ino->i_sb->s_id,
  1015. (unsigned long long)NFS_FILEID(ino),
  1016. lseg == NULL ? "not found" : "found",
  1017. iomode==IOMODE_RW ? "read/write" : "read-only",
  1018. (unsigned long long)pos,
  1019. (unsigned long long)count);
  1020. return lseg;
  1021. out_unlock:
  1022. spin_unlock(&ino->i_lock);
  1023. goto out_put_layout_hdr;
  1024. }
  1025. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  1026. struct pnfs_layout_segment *
  1027. pnfs_layout_process(struct nfs4_layoutget *lgp)
  1028. {
  1029. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  1030. struct nfs4_layoutget_res *res = &lgp->res;
  1031. struct pnfs_layout_segment *lseg;
  1032. struct inode *ino = lo->plh_inode;
  1033. int status = 0;
  1034. /* Inject layout blob into I/O device driver */
  1035. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  1036. if (!lseg || IS_ERR(lseg)) {
  1037. if (!lseg)
  1038. status = -ENOMEM;
  1039. else
  1040. status = PTR_ERR(lseg);
  1041. dprintk("%s: Could not allocate layout: error %d\n",
  1042. __func__, status);
  1043. goto out;
  1044. }
  1045. spin_lock(&ino->i_lock);
  1046. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1047. dprintk("%s forget reply due to recall\n", __func__);
  1048. goto out_forget_reply;
  1049. }
  1050. if (pnfs_layoutgets_blocked(lo, 1) ||
  1051. pnfs_layout_stateid_blocked(lo, &res->stateid)) {
  1052. dprintk("%s forget reply due to state\n", __func__);
  1053. goto out_forget_reply;
  1054. }
  1055. /* Done processing layoutget. Set the layout stateid */
  1056. pnfs_set_layout_stateid(lo, &res->stateid, false);
  1057. init_lseg(lo, lseg);
  1058. lseg->pls_range = res->range;
  1059. pnfs_get_lseg(lseg);
  1060. pnfs_layout_insert_lseg(lo, lseg);
  1061. if (res->return_on_close) {
  1062. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  1063. set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
  1064. }
  1065. spin_unlock(&ino->i_lock);
  1066. return lseg;
  1067. out:
  1068. return ERR_PTR(status);
  1069. out_forget_reply:
  1070. spin_unlock(&ino->i_lock);
  1071. lseg->pls_layout = lo;
  1072. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  1073. goto out;
  1074. }
  1075. void
  1076. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  1077. {
  1078. u64 rd_size = req->wb_bytes;
  1079. BUG_ON(pgio->pg_lseg != NULL);
  1080. if (req->wb_offset != req->wb_pgbase) {
  1081. nfs_pageio_reset_read_mds(pgio);
  1082. return;
  1083. }
  1084. if (pgio->pg_dreq == NULL)
  1085. rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
  1086. else
  1087. rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
  1088. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  1089. req->wb_context,
  1090. req_offset(req),
  1091. rd_size,
  1092. IOMODE_READ,
  1093. GFP_KERNEL);
  1094. /* If no lseg, fall back to read through mds */
  1095. if (pgio->pg_lseg == NULL)
  1096. nfs_pageio_reset_read_mds(pgio);
  1097. }
  1098. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  1099. void
  1100. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
  1101. struct nfs_page *req, u64 wb_size)
  1102. {
  1103. BUG_ON(pgio->pg_lseg != NULL);
  1104. if (req->wb_offset != req->wb_pgbase) {
  1105. nfs_pageio_reset_write_mds(pgio);
  1106. return;
  1107. }
  1108. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  1109. req->wb_context,
  1110. req_offset(req),
  1111. wb_size,
  1112. IOMODE_RW,
  1113. GFP_NOFS);
  1114. /* If no lseg, fall back to write through mds */
  1115. if (pgio->pg_lseg == NULL)
  1116. nfs_pageio_reset_write_mds(pgio);
  1117. }
  1118. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  1119. void
  1120. pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode,
  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_read(pgio, inode, compl_ops);
  1127. else
  1128. nfs_pageio_init(pgio, inode, ld->pg_read_ops, compl_ops, server->rsize, 0);
  1129. }
  1130. void
  1131. pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode,
  1132. int ioflags,
  1133. const struct nfs_pgio_completion_ops *compl_ops)
  1134. {
  1135. struct nfs_server *server = NFS_SERVER(inode);
  1136. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  1137. if (ld == NULL)
  1138. nfs_pageio_init_write(pgio, inode, ioflags, compl_ops);
  1139. else
  1140. nfs_pageio_init(pgio, inode, ld->pg_write_ops, compl_ops, server->wsize, ioflags);
  1141. }
  1142. bool
  1143. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  1144. struct nfs_page *req)
  1145. {
  1146. if (pgio->pg_lseg == NULL)
  1147. return nfs_generic_pg_test(pgio, prev, req);
  1148. /*
  1149. * Test if a nfs_page is fully contained in the pnfs_layout_range.
  1150. * Note that this test makes several assumptions:
  1151. * - that the previous nfs_page in the struct nfs_pageio_descriptor
  1152. * is known to lie within the range.
  1153. * - that the nfs_page being tested is known to be contiguous with the
  1154. * previous nfs_page.
  1155. * - Layout ranges are page aligned, so we only have to test the
  1156. * start offset of the request.
  1157. *
  1158. * Please also note that 'end_offset' is actually the offset of the
  1159. * first byte that lies outside the pnfs_layout_range. FIXME?
  1160. *
  1161. */
  1162. return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
  1163. pgio->pg_lseg->pls_range.length);
  1164. }
  1165. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  1166. int pnfs_write_done_resend_to_mds(struct inode *inode,
  1167. struct list_head *head,
  1168. const struct nfs_pgio_completion_ops *compl_ops)
  1169. {
  1170. struct nfs_pageio_descriptor pgio;
  1171. LIST_HEAD(failed);
  1172. /* Resend all requests through the MDS */
  1173. nfs_pageio_init_write(&pgio, inode, FLUSH_STABLE, compl_ops);
  1174. while (!list_empty(head)) {
  1175. struct nfs_page *req = nfs_list_entry(head->next);
  1176. nfs_list_remove_request(req);
  1177. if (!nfs_pageio_add_request(&pgio, req))
  1178. nfs_list_add_request(req, &failed);
  1179. }
  1180. nfs_pageio_complete(&pgio);
  1181. if (!list_empty(&failed)) {
  1182. /* For some reason our attempt to resend pages. Mark the
  1183. * overall send request as having failed, and let
  1184. * nfs_writeback_release_full deal with the error.
  1185. */
  1186. list_move(&failed, head);
  1187. return -EIO;
  1188. }
  1189. return 0;
  1190. }
  1191. EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
  1192. static void pnfs_ld_handle_write_error(struct nfs_write_data *data)
  1193. {
  1194. struct nfs_pgio_header *hdr = data->header;
  1195. dprintk("pnfs write error = %d\n", hdr->pnfs_error);
  1196. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1197. PNFS_LAYOUTRET_ON_ERROR) {
  1198. clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
  1199. pnfs_return_layout(hdr->inode);
  1200. }
  1201. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1202. data->task.tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
  1203. &hdr->pages,
  1204. hdr->completion_ops);
  1205. }
  1206. /*
  1207. * Called by non rpc-based layout drivers
  1208. */
  1209. void pnfs_ld_write_done(struct nfs_write_data *data)
  1210. {
  1211. struct nfs_pgio_header *hdr = data->header;
  1212. if (!hdr->pnfs_error) {
  1213. pnfs_set_layoutcommit(data);
  1214. hdr->mds_ops->rpc_call_done(&data->task, data);
  1215. } else
  1216. pnfs_ld_handle_write_error(data);
  1217. hdr->mds_ops->rpc_release(data);
  1218. }
  1219. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  1220. static void
  1221. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  1222. struct nfs_write_data *data)
  1223. {
  1224. struct nfs_pgio_header *hdr = data->header;
  1225. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1226. list_splice_tail_init(&hdr->pages, &desc->pg_list);
  1227. nfs_pageio_reset_write_mds(desc);
  1228. desc->pg_recoalesce = 1;
  1229. }
  1230. nfs_writedata_release(data);
  1231. }
  1232. static enum pnfs_try_status
  1233. pnfs_try_to_write_data(struct nfs_write_data *wdata,
  1234. const struct rpc_call_ops *call_ops,
  1235. struct pnfs_layout_segment *lseg,
  1236. int how)
  1237. {
  1238. struct nfs_pgio_header *hdr = wdata->header;
  1239. struct inode *inode = hdr->inode;
  1240. enum pnfs_try_status trypnfs;
  1241. struct nfs_server *nfss = NFS_SERVER(inode);
  1242. hdr->mds_ops = call_ops;
  1243. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  1244. inode->i_ino, wdata->args.count, wdata->args.offset, how);
  1245. trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
  1246. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1247. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  1248. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1249. return trypnfs;
  1250. }
  1251. static void
  1252. pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
  1253. {
  1254. struct nfs_write_data *data;
  1255. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1256. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1257. desc->pg_lseg = NULL;
  1258. while (!list_empty(head)) {
  1259. enum pnfs_try_status trypnfs;
  1260. data = list_first_entry(head, struct nfs_write_data, list);
  1261. list_del_init(&data->list);
  1262. trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
  1263. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1264. pnfs_write_through_mds(desc, data);
  1265. }
  1266. pnfs_put_lseg(lseg);
  1267. }
  1268. static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
  1269. {
  1270. pnfs_put_lseg(hdr->lseg);
  1271. nfs_writehdr_free(hdr);
  1272. }
  1273. EXPORT_SYMBOL_GPL(pnfs_writehdr_free);
  1274. int
  1275. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  1276. {
  1277. struct nfs_write_header *whdr;
  1278. struct nfs_pgio_header *hdr;
  1279. int ret;
  1280. whdr = nfs_writehdr_alloc();
  1281. if (!whdr) {
  1282. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  1283. pnfs_put_lseg(desc->pg_lseg);
  1284. desc->pg_lseg = NULL;
  1285. return -ENOMEM;
  1286. }
  1287. hdr = &whdr->header;
  1288. nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
  1289. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  1290. atomic_inc(&hdr->refcnt);
  1291. ret = nfs_generic_flush(desc, hdr);
  1292. if (ret != 0) {
  1293. pnfs_put_lseg(desc->pg_lseg);
  1294. desc->pg_lseg = NULL;
  1295. } else
  1296. pnfs_do_multiple_writes(desc, &hdr->rpc_list, desc->pg_ioflags);
  1297. if (atomic_dec_and_test(&hdr->refcnt))
  1298. hdr->completion_ops->completion(hdr);
  1299. return ret;
  1300. }
  1301. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  1302. int pnfs_read_done_resend_to_mds(struct inode *inode,
  1303. struct list_head *head,
  1304. const struct nfs_pgio_completion_ops *compl_ops)
  1305. {
  1306. struct nfs_pageio_descriptor pgio;
  1307. LIST_HEAD(failed);
  1308. /* Resend all requests through the MDS */
  1309. nfs_pageio_init_read(&pgio, inode, compl_ops);
  1310. while (!list_empty(head)) {
  1311. struct nfs_page *req = nfs_list_entry(head->next);
  1312. nfs_list_remove_request(req);
  1313. if (!nfs_pageio_add_request(&pgio, req))
  1314. nfs_list_add_request(req, &failed);
  1315. }
  1316. nfs_pageio_complete(&pgio);
  1317. if (!list_empty(&failed)) {
  1318. list_move(&failed, head);
  1319. return -EIO;
  1320. }
  1321. return 0;
  1322. }
  1323. EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
  1324. static void pnfs_ld_handle_read_error(struct nfs_read_data *data)
  1325. {
  1326. struct nfs_pgio_header *hdr = data->header;
  1327. dprintk("pnfs read error = %d\n", hdr->pnfs_error);
  1328. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1329. PNFS_LAYOUTRET_ON_ERROR) {
  1330. clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
  1331. pnfs_return_layout(hdr->inode);
  1332. }
  1333. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1334. data->task.tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
  1335. &hdr->pages,
  1336. hdr->completion_ops);
  1337. }
  1338. /*
  1339. * Called by non rpc-based layout drivers
  1340. */
  1341. void pnfs_ld_read_done(struct nfs_read_data *data)
  1342. {
  1343. struct nfs_pgio_header *hdr = data->header;
  1344. if (likely(!hdr->pnfs_error)) {
  1345. __nfs4_read_done_cb(data);
  1346. hdr->mds_ops->rpc_call_done(&data->task, data);
  1347. } else
  1348. pnfs_ld_handle_read_error(data);
  1349. hdr->mds_ops->rpc_release(data);
  1350. }
  1351. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  1352. static void
  1353. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  1354. struct nfs_read_data *data)
  1355. {
  1356. struct nfs_pgio_header *hdr = data->header;
  1357. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1358. list_splice_tail_init(&hdr->pages, &desc->pg_list);
  1359. nfs_pageio_reset_read_mds(desc);
  1360. desc->pg_recoalesce = 1;
  1361. }
  1362. nfs_readdata_release(data);
  1363. }
  1364. /*
  1365. * Call the appropriate parallel I/O subsystem read function.
  1366. */
  1367. static enum pnfs_try_status
  1368. pnfs_try_to_read_data(struct nfs_read_data *rdata,
  1369. const struct rpc_call_ops *call_ops,
  1370. struct pnfs_layout_segment *lseg)
  1371. {
  1372. struct nfs_pgio_header *hdr = rdata->header;
  1373. struct inode *inode = hdr->inode;
  1374. struct nfs_server *nfss = NFS_SERVER(inode);
  1375. enum pnfs_try_status trypnfs;
  1376. hdr->mds_ops = call_ops;
  1377. dprintk("%s: Reading ino:%lu %u@%llu\n",
  1378. __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
  1379. trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
  1380. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1381. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  1382. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1383. return trypnfs;
  1384. }
  1385. static void
  1386. pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
  1387. {
  1388. struct nfs_read_data *data;
  1389. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1390. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1391. desc->pg_lseg = NULL;
  1392. while (!list_empty(head)) {
  1393. enum pnfs_try_status trypnfs;
  1394. data = list_first_entry(head, struct nfs_read_data, list);
  1395. list_del_init(&data->list);
  1396. trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
  1397. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1398. pnfs_read_through_mds(desc, data);
  1399. }
  1400. pnfs_put_lseg(lseg);
  1401. }
  1402. static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
  1403. {
  1404. pnfs_put_lseg(hdr->lseg);
  1405. nfs_readhdr_free(hdr);
  1406. }
  1407. EXPORT_SYMBOL_GPL(pnfs_readhdr_free);
  1408. int
  1409. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  1410. {
  1411. struct nfs_read_header *rhdr;
  1412. struct nfs_pgio_header *hdr;
  1413. int ret;
  1414. rhdr = nfs_readhdr_alloc();
  1415. if (!rhdr) {
  1416. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  1417. ret = -ENOMEM;
  1418. pnfs_put_lseg(desc->pg_lseg);
  1419. desc->pg_lseg = NULL;
  1420. return ret;
  1421. }
  1422. hdr = &rhdr->header;
  1423. nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
  1424. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  1425. atomic_inc(&hdr->refcnt);
  1426. ret = nfs_generic_pagein(desc, hdr);
  1427. if (ret != 0) {
  1428. pnfs_put_lseg(desc->pg_lseg);
  1429. desc->pg_lseg = NULL;
  1430. } else
  1431. pnfs_do_multiple_reads(desc, &hdr->rpc_list);
  1432. if (atomic_dec_and_test(&hdr->refcnt))
  1433. hdr->completion_ops->completion(hdr);
  1434. return ret;
  1435. }
  1436. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  1437. /*
  1438. * There can be multiple RW segments.
  1439. */
  1440. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  1441. {
  1442. struct pnfs_layout_segment *lseg;
  1443. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  1444. if (lseg->pls_range.iomode == IOMODE_RW &&
  1445. test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  1446. list_add(&lseg->pls_lc_list, listp);
  1447. }
  1448. }
  1449. void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
  1450. {
  1451. pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
  1452. }
  1453. EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
  1454. void
  1455. pnfs_set_layoutcommit(struct nfs_write_data *wdata)
  1456. {
  1457. struct nfs_pgio_header *hdr = wdata->header;
  1458. struct inode *inode = hdr->inode;
  1459. struct nfs_inode *nfsi = NFS_I(inode);
  1460. loff_t end_pos = wdata->mds_offset + wdata->res.count;
  1461. bool mark_as_dirty = false;
  1462. spin_lock(&inode->i_lock);
  1463. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1464. mark_as_dirty = true;
  1465. dprintk("%s: Set layoutcommit for inode %lu ",
  1466. __func__, inode->i_ino);
  1467. }
  1468. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
  1469. /* references matched in nfs4_layoutcommit_release */
  1470. pnfs_get_lseg(hdr->lseg);
  1471. }
  1472. if (end_pos > nfsi->layout->plh_lwb)
  1473. nfsi->layout->plh_lwb = end_pos;
  1474. spin_unlock(&inode->i_lock);
  1475. dprintk("%s: lseg %p end_pos %llu\n",
  1476. __func__, hdr->lseg, nfsi->layout->plh_lwb);
  1477. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  1478. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  1479. if (mark_as_dirty)
  1480. mark_inode_dirty_sync(inode);
  1481. }
  1482. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  1483. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  1484. {
  1485. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  1486. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  1487. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  1488. }
  1489. /*
  1490. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  1491. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  1492. * data to disk to allow the server to recover the data if it crashes.
  1493. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  1494. * is off, and a COMMIT is sent to a data server, or
  1495. * if WRITEs to a data server return NFS_DATA_SYNC.
  1496. */
  1497. int
  1498. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  1499. {
  1500. struct nfs4_layoutcommit_data *data;
  1501. struct nfs_inode *nfsi = NFS_I(inode);
  1502. loff_t end_pos;
  1503. int status = 0;
  1504. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  1505. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1506. return 0;
  1507. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  1508. data = kzalloc(sizeof(*data), GFP_NOFS);
  1509. if (!data) {
  1510. status = -ENOMEM;
  1511. goto out;
  1512. }
  1513. if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1514. goto out_free;
  1515. if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
  1516. if (!sync) {
  1517. status = -EAGAIN;
  1518. goto out_free;
  1519. }
  1520. status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING,
  1521. nfs_wait_bit_killable, TASK_KILLABLE);
  1522. if (status)
  1523. goto out_free;
  1524. }
  1525. INIT_LIST_HEAD(&data->lseg_list);
  1526. spin_lock(&inode->i_lock);
  1527. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1528. clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags);
  1529. spin_unlock(&inode->i_lock);
  1530. wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING);
  1531. goto out_free;
  1532. }
  1533. pnfs_list_write_lseg(inode, &data->lseg_list);
  1534. end_pos = nfsi->layout->plh_lwb;
  1535. nfsi->layout->plh_lwb = 0;
  1536. nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
  1537. spin_unlock(&inode->i_lock);
  1538. data->args.inode = inode;
  1539. data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
  1540. nfs_fattr_init(&data->fattr);
  1541. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  1542. data->res.fattr = &data->fattr;
  1543. data->args.lastbytewritten = end_pos - 1;
  1544. data->res.server = NFS_SERVER(inode);
  1545. status = nfs4_proc_layoutcommit(data, sync);
  1546. out:
  1547. if (status)
  1548. mark_inode_dirty_sync(inode);
  1549. dprintk("<-- %s status %d\n", __func__, status);
  1550. return status;
  1551. out_free:
  1552. kfree(data);
  1553. goto out;
  1554. }
  1555. struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
  1556. {
  1557. struct nfs4_threshold *thp;
  1558. thp = kzalloc(sizeof(*thp), GFP_NOFS);
  1559. if (!thp) {
  1560. dprintk("%s mdsthreshold allocation failed\n", __func__);
  1561. return NULL;
  1562. }
  1563. return thp;
  1564. }