pnfs.c 47 KB

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