pnfs.c 47 KB

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