pnfs.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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. #include "nfs4trace.h"
  36. #include "delegation.h"
  37. #include "nfs42.h"
  38. #define NFSDBG_FACILITY NFSDBG_PNFS
  39. #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
  40. /* Locking:
  41. *
  42. * pnfs_spinlock:
  43. * protects pnfs_modules_tbl.
  44. */
  45. static DEFINE_SPINLOCK(pnfs_spinlock);
  46. /*
  47. * pnfs_modules_tbl holds all pnfs modules
  48. */
  49. static LIST_HEAD(pnfs_modules_tbl);
  50. static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
  51. /* Return the registered pnfs layout driver module matching given id */
  52. static struct pnfs_layoutdriver_type *
  53. find_pnfs_driver_locked(u32 id)
  54. {
  55. struct pnfs_layoutdriver_type *local;
  56. list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
  57. if (local->id == id)
  58. goto out;
  59. local = NULL;
  60. out:
  61. dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
  62. return local;
  63. }
  64. static struct pnfs_layoutdriver_type *
  65. find_pnfs_driver(u32 id)
  66. {
  67. struct pnfs_layoutdriver_type *local;
  68. spin_lock(&pnfs_spinlock);
  69. local = find_pnfs_driver_locked(id);
  70. if (local != NULL && !try_module_get(local->owner)) {
  71. dprintk("%s: Could not grab reference on module\n", __func__);
  72. local = NULL;
  73. }
  74. spin_unlock(&pnfs_spinlock);
  75. return local;
  76. }
  77. void
  78. unset_pnfs_layoutdriver(struct nfs_server *nfss)
  79. {
  80. if (nfss->pnfs_curr_ld) {
  81. if (nfss->pnfs_curr_ld->clear_layoutdriver)
  82. nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
  83. /* Decrement the MDS count. Purge the deviceid cache if zero */
  84. if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
  85. nfs4_deviceid_purge_client(nfss->nfs_client);
  86. module_put(nfss->pnfs_curr_ld->owner);
  87. }
  88. nfss->pnfs_curr_ld = NULL;
  89. }
  90. /*
  91. * Try to set the server's pnfs module to the pnfs layout type specified by id.
  92. * Currently only one pNFS layout driver per filesystem is supported.
  93. *
  94. * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
  95. */
  96. void
  97. set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
  98. u32 id)
  99. {
  100. struct pnfs_layoutdriver_type *ld_type = NULL;
  101. if (id == 0)
  102. goto out_no_driver;
  103. if (!(server->nfs_client->cl_exchange_flags &
  104. (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
  105. printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
  106. __func__, id, server->nfs_client->cl_exchange_flags);
  107. goto out_no_driver;
  108. }
  109. ld_type = find_pnfs_driver(id);
  110. if (!ld_type) {
  111. request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
  112. ld_type = find_pnfs_driver(id);
  113. if (!ld_type) {
  114. dprintk("%s: No pNFS module found for %u.\n",
  115. __func__, id);
  116. goto out_no_driver;
  117. }
  118. }
  119. server->pnfs_curr_ld = ld_type;
  120. if (ld_type->set_layoutdriver
  121. && ld_type->set_layoutdriver(server, mntfh)) {
  122. printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
  123. "driver %u.\n", __func__, id);
  124. module_put(ld_type->owner);
  125. goto out_no_driver;
  126. }
  127. /* Bump the MDS count */
  128. atomic_inc(&server->nfs_client->cl_mds_count);
  129. dprintk("%s: pNFS module for %u set\n", __func__, id);
  130. return;
  131. out_no_driver:
  132. dprintk("%s: Using NFSv4 I/O\n", __func__);
  133. server->pnfs_curr_ld = NULL;
  134. }
  135. int
  136. pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  137. {
  138. int status = -EINVAL;
  139. struct pnfs_layoutdriver_type *tmp;
  140. if (ld_type->id == 0) {
  141. printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
  142. return status;
  143. }
  144. if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
  145. printk(KERN_ERR "NFS: %s Layout driver must provide "
  146. "alloc_lseg and free_lseg.\n", __func__);
  147. return status;
  148. }
  149. spin_lock(&pnfs_spinlock);
  150. tmp = find_pnfs_driver_locked(ld_type->id);
  151. if (!tmp) {
  152. list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
  153. status = 0;
  154. dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
  155. ld_type->name);
  156. } else {
  157. printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
  158. __func__, ld_type->id);
  159. }
  160. spin_unlock(&pnfs_spinlock);
  161. return status;
  162. }
  163. EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
  164. void
  165. pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  166. {
  167. dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
  168. spin_lock(&pnfs_spinlock);
  169. list_del(&ld_type->pnfs_tblid);
  170. spin_unlock(&pnfs_spinlock);
  171. }
  172. EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
  173. /*
  174. * pNFS client layout cache
  175. */
  176. /* Need to hold i_lock if caller does not already hold reference */
  177. void
  178. pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
  179. {
  180. atomic_inc(&lo->plh_refcount);
  181. }
  182. static struct pnfs_layout_hdr *
  183. pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
  184. {
  185. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  186. return ld->alloc_layout_hdr(ino, gfp_flags);
  187. }
  188. static void
  189. pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
  190. {
  191. struct nfs_server *server = NFS_SERVER(lo->plh_inode);
  192. struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
  193. if (!list_empty(&lo->plh_layouts)) {
  194. struct nfs_client *clp = server->nfs_client;
  195. spin_lock(&clp->cl_lock);
  196. list_del_init(&lo->plh_layouts);
  197. spin_unlock(&clp->cl_lock);
  198. }
  199. put_rpccred(lo->plh_lc_cred);
  200. return ld->free_layout_hdr(lo);
  201. }
  202. static void
  203. pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
  204. {
  205. struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
  206. dprintk("%s: freeing layout cache %p\n", __func__, lo);
  207. nfsi->layout = NULL;
  208. /* Reset MDS Threshold I/O counters */
  209. nfsi->write_io = 0;
  210. nfsi->read_io = 0;
  211. }
  212. void
  213. pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
  214. {
  215. struct inode *inode = lo->plh_inode;
  216. pnfs_layoutreturn_before_put_layout_hdr(lo);
  217. if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
  218. if (!list_empty(&lo->plh_segs))
  219. WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
  220. pnfs_detach_layout_hdr(lo);
  221. spin_unlock(&inode->i_lock);
  222. pnfs_free_layout_hdr(lo);
  223. }
  224. }
  225. /*
  226. * Mark a pnfs_layout_hdr and all associated layout segments as invalid
  227. *
  228. * In order to continue using the pnfs_layout_hdr, a full recovery
  229. * is required.
  230. * Note that caller must hold inode->i_lock.
  231. */
  232. int
  233. pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
  234. struct list_head *lseg_list)
  235. {
  236. struct pnfs_layout_range range = {
  237. .iomode = IOMODE_ANY,
  238. .offset = 0,
  239. .length = NFS4_MAX_UINT64,
  240. };
  241. set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
  242. return pnfs_mark_matching_lsegs_invalid(lo, lseg_list, &range, 0);
  243. }
  244. static int
  245. pnfs_iomode_to_fail_bit(u32 iomode)
  246. {
  247. return iomode == IOMODE_RW ?
  248. NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
  249. }
  250. static void
  251. pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
  252. {
  253. lo->plh_retry_timestamp = jiffies;
  254. if (!test_and_set_bit(fail_bit, &lo->plh_flags))
  255. atomic_inc(&lo->plh_refcount);
  256. }
  257. static void
  258. pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
  259. {
  260. if (test_and_clear_bit(fail_bit, &lo->plh_flags))
  261. atomic_dec(&lo->plh_refcount);
  262. }
  263. static void
  264. pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
  265. {
  266. struct inode *inode = lo->plh_inode;
  267. struct pnfs_layout_range range = {
  268. .iomode = iomode,
  269. .offset = 0,
  270. .length = NFS4_MAX_UINT64,
  271. };
  272. LIST_HEAD(head);
  273. spin_lock(&inode->i_lock);
  274. pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  275. pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
  276. spin_unlock(&inode->i_lock);
  277. pnfs_free_lseg_list(&head);
  278. dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
  279. iomode == IOMODE_RW ? "RW" : "READ");
  280. }
  281. static bool
  282. pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
  283. {
  284. unsigned long start, end;
  285. int fail_bit = pnfs_iomode_to_fail_bit(iomode);
  286. if (test_bit(fail_bit, &lo->plh_flags) == 0)
  287. return false;
  288. end = jiffies;
  289. start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
  290. if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
  291. /* It is time to retry the failed layoutgets */
  292. pnfs_layout_clear_fail_bit(lo, fail_bit);
  293. return false;
  294. }
  295. return true;
  296. }
  297. static void
  298. pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
  299. const struct pnfs_layout_range *range,
  300. const nfs4_stateid *stateid)
  301. {
  302. INIT_LIST_HEAD(&lseg->pls_list);
  303. INIT_LIST_HEAD(&lseg->pls_lc_list);
  304. atomic_set(&lseg->pls_refcount, 1);
  305. set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
  306. lseg->pls_layout = lo;
  307. lseg->pls_range = *range;
  308. lseg->pls_seq = be32_to_cpu(stateid->seqid);
  309. }
  310. static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
  311. {
  312. struct inode *ino = lseg->pls_layout->plh_inode;
  313. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  314. }
  315. static void
  316. pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
  317. struct pnfs_layout_segment *lseg)
  318. {
  319. struct inode *inode = lo->plh_inode;
  320. WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  321. list_del_init(&lseg->pls_list);
  322. /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
  323. atomic_dec(&lo->plh_refcount);
  324. if (list_empty(&lo->plh_segs)) {
  325. if (atomic_read(&lo->plh_outstanding) == 0)
  326. set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
  327. clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  328. }
  329. rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
  330. }
  331. void
  332. pnfs_put_lseg(struct pnfs_layout_segment *lseg)
  333. {
  334. struct pnfs_layout_hdr *lo;
  335. struct inode *inode;
  336. if (!lseg)
  337. return;
  338. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  339. atomic_read(&lseg->pls_refcount),
  340. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  341. lo = lseg->pls_layout;
  342. inode = lo->plh_inode;
  343. if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
  344. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  345. spin_unlock(&inode->i_lock);
  346. return;
  347. }
  348. pnfs_get_layout_hdr(lo);
  349. pnfs_layout_remove_lseg(lo, lseg);
  350. spin_unlock(&inode->i_lock);
  351. pnfs_free_lseg(lseg);
  352. pnfs_put_layout_hdr(lo);
  353. }
  354. }
  355. EXPORT_SYMBOL_GPL(pnfs_put_lseg);
  356. static void pnfs_free_lseg_async_work(struct work_struct *work)
  357. {
  358. struct pnfs_layout_segment *lseg;
  359. struct pnfs_layout_hdr *lo;
  360. lseg = container_of(work, struct pnfs_layout_segment, pls_work);
  361. lo = lseg->pls_layout;
  362. pnfs_free_lseg(lseg);
  363. pnfs_put_layout_hdr(lo);
  364. }
  365. static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
  366. {
  367. INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
  368. schedule_work(&lseg->pls_work);
  369. }
  370. void
  371. pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
  372. {
  373. if (!lseg)
  374. return;
  375. assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
  376. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  377. atomic_read(&lseg->pls_refcount),
  378. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  379. if (atomic_dec_and_test(&lseg->pls_refcount)) {
  380. struct pnfs_layout_hdr *lo = lseg->pls_layout;
  381. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
  382. return;
  383. pnfs_get_layout_hdr(lo);
  384. pnfs_layout_remove_lseg(lo, lseg);
  385. pnfs_free_lseg_async(lseg);
  386. }
  387. }
  388. EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
  389. static u64
  390. end_offset(u64 start, u64 len)
  391. {
  392. u64 end;
  393. end = start + len;
  394. return end >= start ? end : NFS4_MAX_UINT64;
  395. }
  396. /*
  397. * is l2 fully contained in l1?
  398. * start1 end1
  399. * [----------------------------------)
  400. * start2 end2
  401. * [----------------)
  402. */
  403. static bool
  404. pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
  405. const struct pnfs_layout_range *l2)
  406. {
  407. u64 start1 = l1->offset;
  408. u64 end1 = end_offset(start1, l1->length);
  409. u64 start2 = l2->offset;
  410. u64 end2 = end_offset(start2, l2->length);
  411. return (start1 <= start2) && (end1 >= end2);
  412. }
  413. /*
  414. * is l1 and l2 intersecting?
  415. * start1 end1
  416. * [----------------------------------)
  417. * start2 end2
  418. * [----------------)
  419. */
  420. static bool
  421. pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
  422. const struct pnfs_layout_range *l2)
  423. {
  424. u64 start1 = l1->offset;
  425. u64 end1 = end_offset(start1, l1->length);
  426. u64 start2 = l2->offset;
  427. u64 end2 = end_offset(start2, l2->length);
  428. return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
  429. (end2 == NFS4_MAX_UINT64 || end2 > start1);
  430. }
  431. static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
  432. struct list_head *tmp_list)
  433. {
  434. if (!atomic_dec_and_test(&lseg->pls_refcount))
  435. return false;
  436. pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
  437. list_add(&lseg->pls_list, tmp_list);
  438. return true;
  439. }
  440. /* Returns 1 if lseg is removed from list, 0 otherwise */
  441. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  442. struct list_head *tmp_list)
  443. {
  444. int rv = 0;
  445. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  446. /* Remove the reference keeping the lseg in the
  447. * list. It will now be removed when all
  448. * outstanding io is finished.
  449. */
  450. dprintk("%s: lseg %p ref %d\n", __func__, lseg,
  451. atomic_read(&lseg->pls_refcount));
  452. if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
  453. rv = 1;
  454. }
  455. return rv;
  456. }
  457. /*
  458. * Compare 2 layout stateid sequence ids, to see which is newer,
  459. * taking into account wraparound issues.
  460. */
  461. static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
  462. {
  463. return (s32)(s1 - s2) > 0;
  464. }
  465. static bool
  466. pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
  467. const struct pnfs_layout_range *recall_range)
  468. {
  469. return (recall_range->iomode == IOMODE_ANY ||
  470. lseg_range->iomode == recall_range->iomode) &&
  471. pnfs_lseg_range_intersecting(lseg_range, recall_range);
  472. }
  473. static bool
  474. pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
  475. const struct pnfs_layout_range *recall_range,
  476. u32 seq)
  477. {
  478. if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
  479. return false;
  480. if (recall_range == NULL)
  481. return true;
  482. return pnfs_should_free_range(&lseg->pls_range, recall_range);
  483. }
  484. /**
  485. * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
  486. * @lo: layout header containing the lsegs
  487. * @tmp_list: list head where doomed lsegs should go
  488. * @recall_range: optional recall range argument to match (may be NULL)
  489. * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
  490. *
  491. * Walk the list of lsegs in the layout header, and tear down any that should
  492. * be destroyed. If "recall_range" is specified then the segment must match
  493. * that range. If "seq" is non-zero, then only match segments that were handed
  494. * out at or before that sequence.
  495. *
  496. * Returns number of matching invalid lsegs remaining in list after scanning
  497. * it and purging them.
  498. */
  499. int
  500. pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
  501. struct list_head *tmp_list,
  502. const struct pnfs_layout_range *recall_range,
  503. u32 seq)
  504. {
  505. struct pnfs_layout_segment *lseg, *next;
  506. int remaining = 0;
  507. dprintk("%s:Begin lo %p\n", __func__, lo);
  508. if (list_empty(&lo->plh_segs))
  509. return 0;
  510. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  511. if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
  512. dprintk("%s: freeing lseg %p iomode %d seq %u"
  513. "offset %llu length %llu\n", __func__,
  514. lseg, lseg->pls_range.iomode, lseg->pls_seq,
  515. lseg->pls_range.offset, lseg->pls_range.length);
  516. if (!mark_lseg_invalid(lseg, tmp_list))
  517. remaining++;
  518. }
  519. dprintk("%s:Return %i\n", __func__, remaining);
  520. return remaining;
  521. }
  522. /* note free_me must contain lsegs from a single layout_hdr */
  523. void
  524. pnfs_free_lseg_list(struct list_head *free_me)
  525. {
  526. struct pnfs_layout_segment *lseg, *tmp;
  527. if (list_empty(free_me))
  528. return;
  529. list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
  530. list_del(&lseg->pls_list);
  531. pnfs_free_lseg(lseg);
  532. }
  533. }
  534. void
  535. pnfs_destroy_layout(struct nfs_inode *nfsi)
  536. {
  537. struct pnfs_layout_hdr *lo;
  538. LIST_HEAD(tmp_list);
  539. spin_lock(&nfsi->vfs_inode.i_lock);
  540. lo = nfsi->layout;
  541. if (lo) {
  542. pnfs_get_layout_hdr(lo);
  543. pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
  544. pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
  545. pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
  546. spin_unlock(&nfsi->vfs_inode.i_lock);
  547. pnfs_free_lseg_list(&tmp_list);
  548. pnfs_put_layout_hdr(lo);
  549. } else
  550. spin_unlock(&nfsi->vfs_inode.i_lock);
  551. }
  552. EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
  553. static bool
  554. pnfs_layout_add_bulk_destroy_list(struct inode *inode,
  555. struct list_head *layout_list)
  556. {
  557. struct pnfs_layout_hdr *lo;
  558. bool ret = false;
  559. spin_lock(&inode->i_lock);
  560. lo = NFS_I(inode)->layout;
  561. if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
  562. pnfs_get_layout_hdr(lo);
  563. list_add(&lo->plh_bulk_destroy, layout_list);
  564. ret = true;
  565. }
  566. spin_unlock(&inode->i_lock);
  567. return ret;
  568. }
  569. /* Caller must hold rcu_read_lock and clp->cl_lock */
  570. static int
  571. pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
  572. struct nfs_server *server,
  573. struct list_head *layout_list)
  574. {
  575. struct pnfs_layout_hdr *lo, *next;
  576. struct inode *inode;
  577. list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
  578. inode = igrab(lo->plh_inode);
  579. if (inode == NULL)
  580. continue;
  581. list_del_init(&lo->plh_layouts);
  582. if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
  583. continue;
  584. rcu_read_unlock();
  585. spin_unlock(&clp->cl_lock);
  586. iput(inode);
  587. spin_lock(&clp->cl_lock);
  588. rcu_read_lock();
  589. return -EAGAIN;
  590. }
  591. return 0;
  592. }
  593. static int
  594. pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
  595. bool is_bulk_recall)
  596. {
  597. struct pnfs_layout_hdr *lo;
  598. struct inode *inode;
  599. LIST_HEAD(lseg_list);
  600. int ret = 0;
  601. while (!list_empty(layout_list)) {
  602. lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
  603. plh_bulk_destroy);
  604. dprintk("%s freeing layout for inode %lu\n", __func__,
  605. lo->plh_inode->i_ino);
  606. inode = lo->plh_inode;
  607. pnfs_layoutcommit_inode(inode, false);
  608. spin_lock(&inode->i_lock);
  609. list_del_init(&lo->plh_bulk_destroy);
  610. if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
  611. if (is_bulk_recall)
  612. set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  613. ret = -EAGAIN;
  614. }
  615. spin_unlock(&inode->i_lock);
  616. pnfs_free_lseg_list(&lseg_list);
  617. /* Free all lsegs that are attached to commit buckets */
  618. nfs_commit_inode(inode, 0);
  619. pnfs_put_layout_hdr(lo);
  620. iput(inode);
  621. }
  622. return ret;
  623. }
  624. int
  625. pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
  626. struct nfs_fsid *fsid,
  627. bool is_recall)
  628. {
  629. struct nfs_server *server;
  630. LIST_HEAD(layout_list);
  631. spin_lock(&clp->cl_lock);
  632. rcu_read_lock();
  633. restart:
  634. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  635. if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
  636. continue;
  637. if (pnfs_layout_bulk_destroy_byserver_locked(clp,
  638. server,
  639. &layout_list) != 0)
  640. goto restart;
  641. }
  642. rcu_read_unlock();
  643. spin_unlock(&clp->cl_lock);
  644. if (list_empty(&layout_list))
  645. return 0;
  646. return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
  647. }
  648. int
  649. pnfs_destroy_layouts_byclid(struct nfs_client *clp,
  650. bool is_recall)
  651. {
  652. struct nfs_server *server;
  653. LIST_HEAD(layout_list);
  654. spin_lock(&clp->cl_lock);
  655. rcu_read_lock();
  656. restart:
  657. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  658. if (pnfs_layout_bulk_destroy_byserver_locked(clp,
  659. server,
  660. &layout_list) != 0)
  661. goto restart;
  662. }
  663. rcu_read_unlock();
  664. spin_unlock(&clp->cl_lock);
  665. if (list_empty(&layout_list))
  666. return 0;
  667. return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
  668. }
  669. /*
  670. * Called by the state manger to remove all layouts established under an
  671. * expired lease.
  672. */
  673. void
  674. pnfs_destroy_all_layouts(struct nfs_client *clp)
  675. {
  676. nfs4_deviceid_mark_client_invalid(clp);
  677. nfs4_deviceid_purge_client(clp);
  678. pnfs_destroy_layouts_byclid(clp, false);
  679. }
  680. static void
  681. pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
  682. {
  683. lo->plh_return_iomode = 0;
  684. lo->plh_return_seq = 0;
  685. clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
  686. }
  687. /* update lo->plh_stateid with new if is more recent */
  688. void
  689. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  690. bool update_barrier)
  691. {
  692. u32 oldseq, newseq, new_barrier = 0;
  693. oldseq = be32_to_cpu(lo->plh_stateid.seqid);
  694. newseq = be32_to_cpu(new->seqid);
  695. if (!pnfs_layout_is_valid(lo)) {
  696. nfs4_stateid_copy(&lo->plh_stateid, new);
  697. lo->plh_barrier = newseq;
  698. pnfs_clear_layoutreturn_info(lo);
  699. clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
  700. return;
  701. }
  702. if (pnfs_seqid_is_newer(newseq, oldseq)) {
  703. nfs4_stateid_copy(&lo->plh_stateid, new);
  704. /*
  705. * Because of wraparound, we want to keep the barrier
  706. * "close" to the current seqids.
  707. */
  708. new_barrier = newseq - atomic_read(&lo->plh_outstanding);
  709. }
  710. if (update_barrier)
  711. new_barrier = be32_to_cpu(new->seqid);
  712. else if (new_barrier == 0)
  713. return;
  714. if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
  715. lo->plh_barrier = new_barrier;
  716. }
  717. static bool
  718. pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
  719. const nfs4_stateid *stateid)
  720. {
  721. u32 seqid = be32_to_cpu(stateid->seqid);
  722. return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
  723. }
  724. /* lget is set to 1 if called from inside send_layoutget call chain */
  725. static bool
  726. pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
  727. {
  728. return lo->plh_block_lgets ||
  729. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  730. }
  731. /*
  732. * Get layout from server.
  733. * for now, assume that whole file layouts are requested.
  734. * arg->offset: 0
  735. * arg->length: all ones
  736. */
  737. static struct pnfs_layout_segment *
  738. send_layoutget(struct pnfs_layout_hdr *lo,
  739. struct nfs_open_context *ctx,
  740. nfs4_stateid *stateid,
  741. const struct pnfs_layout_range *range,
  742. long *timeout, gfp_t gfp_flags)
  743. {
  744. struct inode *ino = lo->plh_inode;
  745. struct nfs_server *server = NFS_SERVER(ino);
  746. struct nfs4_layoutget *lgp;
  747. loff_t i_size;
  748. dprintk("--> %s\n", __func__);
  749. /*
  750. * Synchronously retrieve layout information from server and
  751. * store in lseg. If we race with a concurrent seqid morphing
  752. * op, then re-send the LAYOUTGET.
  753. */
  754. lgp = kzalloc(sizeof(*lgp), gfp_flags);
  755. if (lgp == NULL)
  756. return ERR_PTR(-ENOMEM);
  757. i_size = i_size_read(ino);
  758. lgp->args.minlength = PAGE_SIZE;
  759. if (lgp->args.minlength > range->length)
  760. lgp->args.minlength = range->length;
  761. if (range->iomode == IOMODE_READ) {
  762. if (range->offset >= i_size)
  763. lgp->args.minlength = 0;
  764. else if (i_size - range->offset < lgp->args.minlength)
  765. lgp->args.minlength = i_size - range->offset;
  766. }
  767. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  768. pnfs_copy_range(&lgp->args.range, range);
  769. lgp->args.type = server->pnfs_curr_ld->id;
  770. lgp->args.inode = ino;
  771. lgp->args.ctx = get_nfs_open_context(ctx);
  772. nfs4_stateid_copy(&lgp->args.stateid, stateid);
  773. lgp->gfp_flags = gfp_flags;
  774. lgp->cred = lo->plh_lc_cred;
  775. return nfs4_proc_layoutget(lgp, timeout, gfp_flags);
  776. }
  777. static void pnfs_clear_layoutcommit(struct inode *inode,
  778. struct list_head *head)
  779. {
  780. struct nfs_inode *nfsi = NFS_I(inode);
  781. struct pnfs_layout_segment *lseg, *tmp;
  782. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  783. return;
  784. list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
  785. if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  786. continue;
  787. pnfs_lseg_dec_and_remove_zero(lseg, head);
  788. }
  789. }
  790. void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
  791. {
  792. clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
  793. smp_mb__after_atomic();
  794. wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
  795. rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
  796. }
  797. static bool
  798. pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
  799. nfs4_stateid *stateid,
  800. enum pnfs_iomode *iomode)
  801. {
  802. /* Serialise LAYOUTGET/LAYOUTRETURN */
  803. if (atomic_read(&lo->plh_outstanding) != 0)
  804. return false;
  805. if (test_and_set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
  806. return false;
  807. pnfs_get_layout_hdr(lo);
  808. if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
  809. if (stateid != NULL) {
  810. nfs4_stateid_copy(stateid, &lo->plh_stateid);
  811. if (lo->plh_return_seq != 0)
  812. stateid->seqid = cpu_to_be32(lo->plh_return_seq);
  813. }
  814. if (iomode != NULL)
  815. *iomode = lo->plh_return_iomode;
  816. pnfs_clear_layoutreturn_info(lo);
  817. return true;
  818. }
  819. if (stateid != NULL)
  820. nfs4_stateid_copy(stateid, &lo->plh_stateid);
  821. if (iomode != NULL)
  822. *iomode = IOMODE_ANY;
  823. return true;
  824. }
  825. static int
  826. pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
  827. enum pnfs_iomode iomode, bool sync)
  828. {
  829. struct inode *ino = lo->plh_inode;
  830. struct nfs4_layoutreturn *lrp;
  831. int status = 0;
  832. lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
  833. if (unlikely(lrp == NULL)) {
  834. status = -ENOMEM;
  835. spin_lock(&ino->i_lock);
  836. pnfs_clear_layoutreturn_waitbit(lo);
  837. spin_unlock(&ino->i_lock);
  838. pnfs_put_layout_hdr(lo);
  839. goto out;
  840. }
  841. nfs4_stateid_copy(&lrp->args.stateid, stateid);
  842. lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
  843. lrp->args.inode = ino;
  844. lrp->args.range.iomode = iomode;
  845. lrp->args.range.offset = 0;
  846. lrp->args.range.length = NFS4_MAX_UINT64;
  847. lrp->args.layout = lo;
  848. lrp->clp = NFS_SERVER(ino)->nfs_client;
  849. lrp->cred = lo->plh_lc_cred;
  850. status = nfs4_proc_layoutreturn(lrp, sync);
  851. out:
  852. dprintk("<-- %s status: %d\n", __func__, status);
  853. return status;
  854. }
  855. /* Return true if layoutreturn is needed */
  856. static bool
  857. pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
  858. {
  859. struct pnfs_layout_segment *s;
  860. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  861. return false;
  862. /* Defer layoutreturn until all lsegs are done */
  863. list_for_each_entry(s, &lo->plh_segs, pls_list) {
  864. if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
  865. return false;
  866. }
  867. return true;
  868. }
  869. static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
  870. {
  871. struct inode *inode= lo->plh_inode;
  872. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  873. return;
  874. spin_lock(&inode->i_lock);
  875. if (pnfs_layout_need_return(lo)) {
  876. nfs4_stateid stateid;
  877. enum pnfs_iomode iomode;
  878. bool send;
  879. send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
  880. spin_unlock(&inode->i_lock);
  881. if (send) {
  882. /* Send an async layoutreturn so we dont deadlock */
  883. pnfs_send_layoutreturn(lo, &stateid, iomode, false);
  884. }
  885. } else
  886. spin_unlock(&inode->i_lock);
  887. }
  888. /*
  889. * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
  890. * when the layout segment list is empty.
  891. *
  892. * Note that a pnfs_layout_hdr can exist with an empty layout segment
  893. * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
  894. * deviceid is marked invalid.
  895. */
  896. int
  897. _pnfs_return_layout(struct inode *ino)
  898. {
  899. struct pnfs_layout_hdr *lo = NULL;
  900. struct nfs_inode *nfsi = NFS_I(ino);
  901. LIST_HEAD(tmp_list);
  902. nfs4_stateid stateid;
  903. int status = 0, empty;
  904. bool send;
  905. dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
  906. spin_lock(&ino->i_lock);
  907. lo = nfsi->layout;
  908. if (!lo) {
  909. spin_unlock(&ino->i_lock);
  910. dprintk("NFS: %s no layout to return\n", __func__);
  911. goto out;
  912. }
  913. /* Reference matched in nfs4_layoutreturn_release */
  914. pnfs_get_layout_hdr(lo);
  915. empty = list_empty(&lo->plh_segs);
  916. pnfs_clear_layoutcommit(ino, &tmp_list);
  917. pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0);
  918. if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
  919. struct pnfs_layout_range range = {
  920. .iomode = IOMODE_ANY,
  921. .offset = 0,
  922. .length = NFS4_MAX_UINT64,
  923. };
  924. NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
  925. }
  926. /* Don't send a LAYOUTRETURN if list was initially empty */
  927. if (empty) {
  928. spin_unlock(&ino->i_lock);
  929. dprintk("NFS: %s no layout segments to return\n", __func__);
  930. goto out_put_layout_hdr;
  931. }
  932. send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
  933. spin_unlock(&ino->i_lock);
  934. pnfs_free_lseg_list(&tmp_list);
  935. if (send)
  936. status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
  937. out_put_layout_hdr:
  938. pnfs_put_layout_hdr(lo);
  939. out:
  940. dprintk("<-- %s status: %d\n", __func__, status);
  941. return status;
  942. }
  943. EXPORT_SYMBOL_GPL(_pnfs_return_layout);
  944. int
  945. pnfs_commit_and_return_layout(struct inode *inode)
  946. {
  947. struct pnfs_layout_hdr *lo;
  948. int ret;
  949. spin_lock(&inode->i_lock);
  950. lo = NFS_I(inode)->layout;
  951. if (lo == NULL) {
  952. spin_unlock(&inode->i_lock);
  953. return 0;
  954. }
  955. pnfs_get_layout_hdr(lo);
  956. /* Block new layoutgets and read/write to ds */
  957. lo->plh_block_lgets++;
  958. spin_unlock(&inode->i_lock);
  959. filemap_fdatawait(inode->i_mapping);
  960. ret = pnfs_layoutcommit_inode(inode, true);
  961. if (ret == 0)
  962. ret = _pnfs_return_layout(inode);
  963. spin_lock(&inode->i_lock);
  964. lo->plh_block_lgets--;
  965. spin_unlock(&inode->i_lock);
  966. pnfs_put_layout_hdr(lo);
  967. return ret;
  968. }
  969. bool pnfs_roc(struct inode *ino)
  970. {
  971. struct nfs_inode *nfsi = NFS_I(ino);
  972. struct nfs_open_context *ctx;
  973. struct nfs4_state *state;
  974. struct pnfs_layout_hdr *lo;
  975. struct pnfs_layout_segment *lseg, *tmp;
  976. nfs4_stateid stateid;
  977. LIST_HEAD(tmp_list);
  978. bool found = false, layoutreturn = false, roc = false;
  979. spin_lock(&ino->i_lock);
  980. lo = nfsi->layout;
  981. if (!lo || test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
  982. goto out_noroc;
  983. /* no roc if we hold a delegation */
  984. if (nfs4_check_delegation(ino, FMODE_READ))
  985. goto out_noroc;
  986. list_for_each_entry(ctx, &nfsi->open_files, list) {
  987. state = ctx->state;
  988. /* Don't return layout if there is open file state */
  989. if (state != NULL && state->state != 0)
  990. goto out_noroc;
  991. }
  992. /* always send layoutreturn if being marked so */
  993. if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  994. layoutreturn = pnfs_prepare_layoutreturn(lo,
  995. &stateid, NULL);
  996. list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
  997. /* If we are sending layoutreturn, invalidate all valid lsegs */
  998. if (layoutreturn || test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  999. mark_lseg_invalid(lseg, &tmp_list);
  1000. found = true;
  1001. }
  1002. /* ROC in two conditions:
  1003. * 1. there are ROC lsegs
  1004. * 2. we don't send layoutreturn
  1005. */
  1006. if (found && !layoutreturn) {
  1007. /* lo ref dropped in pnfs_roc_release() */
  1008. pnfs_get_layout_hdr(lo);
  1009. roc = true;
  1010. }
  1011. out_noroc:
  1012. spin_unlock(&ino->i_lock);
  1013. pnfs_free_lseg_list(&tmp_list);
  1014. pnfs_layoutcommit_inode(ino, true);
  1015. if (layoutreturn)
  1016. pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
  1017. return roc;
  1018. }
  1019. void pnfs_roc_release(struct inode *ino)
  1020. {
  1021. struct pnfs_layout_hdr *lo;
  1022. spin_lock(&ino->i_lock);
  1023. lo = NFS_I(ino)->layout;
  1024. pnfs_clear_layoutreturn_waitbit(lo);
  1025. if (atomic_dec_and_test(&lo->plh_refcount)) {
  1026. pnfs_detach_layout_hdr(lo);
  1027. spin_unlock(&ino->i_lock);
  1028. pnfs_free_layout_hdr(lo);
  1029. } else
  1030. spin_unlock(&ino->i_lock);
  1031. }
  1032. void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
  1033. {
  1034. struct pnfs_layout_hdr *lo;
  1035. spin_lock(&ino->i_lock);
  1036. lo = NFS_I(ino)->layout;
  1037. if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
  1038. lo->plh_barrier = barrier;
  1039. spin_unlock(&ino->i_lock);
  1040. trace_nfs4_layoutreturn_on_close(ino, 0);
  1041. }
  1042. void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier)
  1043. {
  1044. struct nfs_inode *nfsi = NFS_I(ino);
  1045. struct pnfs_layout_hdr *lo;
  1046. u32 current_seqid;
  1047. spin_lock(&ino->i_lock);
  1048. lo = nfsi->layout;
  1049. current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
  1050. /* Since close does not return a layout stateid for use as
  1051. * a barrier, we choose the worst-case barrier.
  1052. */
  1053. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  1054. spin_unlock(&ino->i_lock);
  1055. }
  1056. bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
  1057. {
  1058. struct nfs_inode *nfsi = NFS_I(ino);
  1059. struct pnfs_layout_hdr *lo;
  1060. bool sleep = false;
  1061. /* we might not have grabbed lo reference. so need to check under
  1062. * i_lock */
  1063. spin_lock(&ino->i_lock);
  1064. lo = nfsi->layout;
  1065. if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
  1066. sleep = true;
  1067. spin_unlock(&ino->i_lock);
  1068. if (sleep)
  1069. rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
  1070. return sleep;
  1071. }
  1072. /*
  1073. * Compare two layout segments for sorting into layout cache.
  1074. * We want to preferentially return RW over RO layouts, so ensure those
  1075. * are seen first.
  1076. */
  1077. static s64
  1078. pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
  1079. const struct pnfs_layout_range *l2)
  1080. {
  1081. s64 d;
  1082. /* high offset > low offset */
  1083. d = l1->offset - l2->offset;
  1084. if (d)
  1085. return d;
  1086. /* short length > long length */
  1087. d = l2->length - l1->length;
  1088. if (d)
  1089. return d;
  1090. /* read > read/write */
  1091. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  1092. }
  1093. static bool
  1094. pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
  1095. const struct pnfs_layout_range *l2)
  1096. {
  1097. return pnfs_lseg_range_cmp(l1, l2) > 0;
  1098. }
  1099. static bool
  1100. pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
  1101. struct pnfs_layout_segment *old)
  1102. {
  1103. return false;
  1104. }
  1105. void
  1106. pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  1107. struct pnfs_layout_segment *lseg,
  1108. bool (*is_after)(const struct pnfs_layout_range *,
  1109. const struct pnfs_layout_range *),
  1110. bool (*do_merge)(struct pnfs_layout_segment *,
  1111. struct pnfs_layout_segment *),
  1112. struct list_head *free_me)
  1113. {
  1114. struct pnfs_layout_segment *lp, *tmp;
  1115. dprintk("%s:Begin\n", __func__);
  1116. list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
  1117. if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
  1118. continue;
  1119. if (do_merge(lseg, lp)) {
  1120. mark_lseg_invalid(lp, free_me);
  1121. continue;
  1122. }
  1123. if (is_after(&lseg->pls_range, &lp->pls_range))
  1124. continue;
  1125. list_add_tail(&lseg->pls_list, &lp->pls_list);
  1126. dprintk("%s: inserted lseg %p "
  1127. "iomode %d offset %llu length %llu before "
  1128. "lp %p iomode %d offset %llu length %llu\n",
  1129. __func__, lseg, lseg->pls_range.iomode,
  1130. lseg->pls_range.offset, lseg->pls_range.length,
  1131. lp, lp->pls_range.iomode, lp->pls_range.offset,
  1132. lp->pls_range.length);
  1133. goto out;
  1134. }
  1135. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  1136. dprintk("%s: inserted lseg %p "
  1137. "iomode %d offset %llu length %llu at tail\n",
  1138. __func__, lseg, lseg->pls_range.iomode,
  1139. lseg->pls_range.offset, lseg->pls_range.length);
  1140. out:
  1141. pnfs_get_layout_hdr(lo);
  1142. dprintk("%s:Return\n", __func__);
  1143. }
  1144. EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
  1145. static void
  1146. pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  1147. struct pnfs_layout_segment *lseg,
  1148. struct list_head *free_me)
  1149. {
  1150. struct inode *inode = lo->plh_inode;
  1151. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  1152. if (ld->add_lseg != NULL)
  1153. ld->add_lseg(lo, lseg, free_me);
  1154. else
  1155. pnfs_generic_layout_insert_lseg(lo, lseg,
  1156. pnfs_lseg_range_is_after,
  1157. pnfs_lseg_no_merge,
  1158. free_me);
  1159. }
  1160. static struct pnfs_layout_hdr *
  1161. alloc_init_layout_hdr(struct inode *ino,
  1162. struct nfs_open_context *ctx,
  1163. gfp_t gfp_flags)
  1164. {
  1165. struct pnfs_layout_hdr *lo;
  1166. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  1167. if (!lo)
  1168. return NULL;
  1169. atomic_set(&lo->plh_refcount, 1);
  1170. INIT_LIST_HEAD(&lo->plh_layouts);
  1171. INIT_LIST_HEAD(&lo->plh_segs);
  1172. INIT_LIST_HEAD(&lo->plh_bulk_destroy);
  1173. lo->plh_inode = ino;
  1174. lo->plh_lc_cred = get_rpccred(ctx->cred);
  1175. lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
  1176. return lo;
  1177. }
  1178. static struct pnfs_layout_hdr *
  1179. pnfs_find_alloc_layout(struct inode *ino,
  1180. struct nfs_open_context *ctx,
  1181. gfp_t gfp_flags)
  1182. __releases(&ino->i_lock)
  1183. __acquires(&ino->i_lock)
  1184. {
  1185. struct nfs_inode *nfsi = NFS_I(ino);
  1186. struct pnfs_layout_hdr *new = NULL;
  1187. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  1188. if (nfsi->layout != NULL)
  1189. goto out_existing;
  1190. spin_unlock(&ino->i_lock);
  1191. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  1192. spin_lock(&ino->i_lock);
  1193. if (likely(nfsi->layout == NULL)) { /* Won the race? */
  1194. nfsi->layout = new;
  1195. return new;
  1196. } else if (new != NULL)
  1197. pnfs_free_layout_hdr(new);
  1198. out_existing:
  1199. pnfs_get_layout_hdr(nfsi->layout);
  1200. return nfsi->layout;
  1201. }
  1202. /*
  1203. * iomode matching rules:
  1204. * iomode lseg strict match
  1205. * iomode
  1206. * ----- ----- ------ -----
  1207. * ANY READ N/A true
  1208. * ANY RW N/A true
  1209. * RW READ N/A false
  1210. * RW RW N/A true
  1211. * READ READ N/A true
  1212. * READ RW true false
  1213. * READ RW false true
  1214. */
  1215. static bool
  1216. pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
  1217. const struct pnfs_layout_range *range,
  1218. bool strict_iomode)
  1219. {
  1220. struct pnfs_layout_range range1;
  1221. if ((range->iomode == IOMODE_RW &&
  1222. ls_range->iomode != IOMODE_RW) ||
  1223. (range->iomode != ls_range->iomode &&
  1224. strict_iomode == true) ||
  1225. !pnfs_lseg_range_intersecting(ls_range, range))
  1226. return 0;
  1227. /* range1 covers only the first byte in the range */
  1228. range1 = *range;
  1229. range1.length = 1;
  1230. return pnfs_lseg_range_contained(ls_range, &range1);
  1231. }
  1232. /*
  1233. * lookup range in layout
  1234. */
  1235. static struct pnfs_layout_segment *
  1236. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  1237. struct pnfs_layout_range *range,
  1238. bool strict_iomode)
  1239. {
  1240. struct pnfs_layout_segment *lseg, *ret = NULL;
  1241. dprintk("%s:Begin\n", __func__);
  1242. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  1243. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  1244. !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
  1245. pnfs_lseg_range_match(&lseg->pls_range, range,
  1246. strict_iomode)) {
  1247. ret = pnfs_get_lseg(lseg);
  1248. break;
  1249. }
  1250. }
  1251. dprintk("%s:Return lseg %p ref %d\n",
  1252. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  1253. return ret;
  1254. }
  1255. /*
  1256. * Use mdsthreshold hints set at each OPEN to determine if I/O should go
  1257. * to the MDS or over pNFS
  1258. *
  1259. * The nfs_inode read_io and write_io fields are cumulative counters reset
  1260. * when there are no layout segments. Note that in pnfs_update_layout iomode
  1261. * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
  1262. * WRITE request.
  1263. *
  1264. * A return of true means use MDS I/O.
  1265. *
  1266. * From rfc 5661:
  1267. * If a file's size is smaller than the file size threshold, data accesses
  1268. * SHOULD be sent to the metadata server. If an I/O request has a length that
  1269. * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
  1270. * server. If both file size and I/O size are provided, the client SHOULD
  1271. * reach or exceed both thresholds before sending its read or write
  1272. * requests to the data server.
  1273. */
  1274. static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
  1275. struct inode *ino, int iomode)
  1276. {
  1277. struct nfs4_threshold *t = ctx->mdsthreshold;
  1278. struct nfs_inode *nfsi = NFS_I(ino);
  1279. loff_t fsize = i_size_read(ino);
  1280. bool size = false, size_set = false, io = false, io_set = false, ret = false;
  1281. if (t == NULL)
  1282. return ret;
  1283. dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
  1284. __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
  1285. switch (iomode) {
  1286. case IOMODE_READ:
  1287. if (t->bm & THRESHOLD_RD) {
  1288. dprintk("%s fsize %llu\n", __func__, fsize);
  1289. size_set = true;
  1290. if (fsize < t->rd_sz)
  1291. size = true;
  1292. }
  1293. if (t->bm & THRESHOLD_RD_IO) {
  1294. dprintk("%s nfsi->read_io %llu\n", __func__,
  1295. nfsi->read_io);
  1296. io_set = true;
  1297. if (nfsi->read_io < t->rd_io_sz)
  1298. io = true;
  1299. }
  1300. break;
  1301. case IOMODE_RW:
  1302. if (t->bm & THRESHOLD_WR) {
  1303. dprintk("%s fsize %llu\n", __func__, fsize);
  1304. size_set = true;
  1305. if (fsize < t->wr_sz)
  1306. size = true;
  1307. }
  1308. if (t->bm & THRESHOLD_WR_IO) {
  1309. dprintk("%s nfsi->write_io %llu\n", __func__,
  1310. nfsi->write_io);
  1311. io_set = true;
  1312. if (nfsi->write_io < t->wr_io_sz)
  1313. io = true;
  1314. }
  1315. break;
  1316. }
  1317. if (size_set && io_set) {
  1318. if (size && io)
  1319. ret = true;
  1320. } else if (size || io)
  1321. ret = true;
  1322. dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
  1323. return ret;
  1324. }
  1325. static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
  1326. {
  1327. /*
  1328. * send layoutcommit as it can hold up layoutreturn due to lseg
  1329. * reference
  1330. */
  1331. pnfs_layoutcommit_inode(lo->plh_inode, false);
  1332. return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1333. nfs_wait_bit_killable,
  1334. TASK_UNINTERRUPTIBLE);
  1335. }
  1336. static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
  1337. {
  1338. unsigned long *bitlock = &lo->plh_flags;
  1339. clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
  1340. smp_mb__after_atomic();
  1341. wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
  1342. }
  1343. /*
  1344. * Layout segment is retreived from the server if not cached.
  1345. * The appropriate layout segment is referenced and returned to the caller.
  1346. */
  1347. struct pnfs_layout_segment *
  1348. pnfs_update_layout(struct inode *ino,
  1349. struct nfs_open_context *ctx,
  1350. loff_t pos,
  1351. u64 count,
  1352. enum pnfs_iomode iomode,
  1353. bool strict_iomode,
  1354. gfp_t gfp_flags)
  1355. {
  1356. struct pnfs_layout_range arg = {
  1357. .iomode = iomode,
  1358. .offset = pos,
  1359. .length = count,
  1360. };
  1361. unsigned pg_offset, seq;
  1362. struct nfs_server *server = NFS_SERVER(ino);
  1363. struct nfs_client *clp = server->nfs_client;
  1364. struct pnfs_layout_hdr *lo = NULL;
  1365. struct pnfs_layout_segment *lseg = NULL;
  1366. nfs4_stateid stateid;
  1367. long timeout = 0;
  1368. unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
  1369. bool first;
  1370. if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
  1371. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1372. PNFS_UPDATE_LAYOUT_NO_PNFS);
  1373. goto out;
  1374. }
  1375. if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
  1376. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1377. PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
  1378. goto out;
  1379. }
  1380. if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
  1381. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1382. PNFS_UPDATE_LAYOUT_MDSTHRESH);
  1383. goto out;
  1384. }
  1385. lookup_again:
  1386. nfs4_client_recover_expired_lease(clp);
  1387. first = false;
  1388. spin_lock(&ino->i_lock);
  1389. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  1390. if (lo == NULL) {
  1391. spin_unlock(&ino->i_lock);
  1392. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1393. PNFS_UPDATE_LAYOUT_NOMEM);
  1394. goto out;
  1395. }
  1396. /* Do we even need to bother with this? */
  1397. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1398. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1399. PNFS_UPDATE_LAYOUT_BULK_RECALL);
  1400. dprintk("%s matches recall, use MDS\n", __func__);
  1401. goto out_unlock;
  1402. }
  1403. /* if LAYOUTGET already failed once we don't try again */
  1404. if (pnfs_layout_io_test_failed(lo, iomode)) {
  1405. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1406. PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
  1407. goto out_unlock;
  1408. }
  1409. lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
  1410. if (lseg) {
  1411. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1412. PNFS_UPDATE_LAYOUT_FOUND_CACHED);
  1413. goto out_unlock;
  1414. }
  1415. if (!nfs4_valid_open_stateid(ctx->state)) {
  1416. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1417. PNFS_UPDATE_LAYOUT_INVALID_OPEN);
  1418. goto out_unlock;
  1419. }
  1420. /*
  1421. * Choose a stateid for the LAYOUTGET. If we don't have a layout
  1422. * stateid, or it has been invalidated, then we must use the open
  1423. * stateid.
  1424. */
  1425. if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
  1426. /*
  1427. * The first layoutget for the file. Need to serialize per
  1428. * RFC 5661 Errata 3208.
  1429. */
  1430. if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
  1431. &lo->plh_flags)) {
  1432. spin_unlock(&ino->i_lock);
  1433. wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
  1434. TASK_UNINTERRUPTIBLE);
  1435. pnfs_put_layout_hdr(lo);
  1436. dprintk("%s retrying\n", __func__);
  1437. goto lookup_again;
  1438. }
  1439. first = true;
  1440. do {
  1441. seq = read_seqbegin(&ctx->state->seqlock);
  1442. nfs4_stateid_copy(&stateid, &ctx->state->stateid);
  1443. } while (read_seqretry(&ctx->state->seqlock, seq));
  1444. } else {
  1445. nfs4_stateid_copy(&stateid, &lo->plh_stateid);
  1446. }
  1447. /*
  1448. * Because we free lsegs before sending LAYOUTRETURN, we need to wait
  1449. * for LAYOUTRETURN even if first is true.
  1450. */
  1451. if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
  1452. spin_unlock(&ino->i_lock);
  1453. dprintk("%s wait for layoutreturn\n", __func__);
  1454. if (pnfs_prepare_to_retry_layoutget(lo)) {
  1455. if (first)
  1456. pnfs_clear_first_layoutget(lo);
  1457. pnfs_put_layout_hdr(lo);
  1458. dprintk("%s retrying\n", __func__);
  1459. trace_pnfs_update_layout(ino, pos, count, iomode, lo,
  1460. lseg, PNFS_UPDATE_LAYOUT_RETRY);
  1461. goto lookup_again;
  1462. }
  1463. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1464. PNFS_UPDATE_LAYOUT_RETURN);
  1465. goto out_put_layout_hdr;
  1466. }
  1467. if (pnfs_layoutgets_blocked(lo)) {
  1468. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1469. PNFS_UPDATE_LAYOUT_BLOCKED);
  1470. goto out_unlock;
  1471. }
  1472. atomic_inc(&lo->plh_outstanding);
  1473. spin_unlock(&ino->i_lock);
  1474. if (list_empty(&lo->plh_layouts)) {
  1475. /* The lo must be on the clp list if there is any
  1476. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  1477. */
  1478. spin_lock(&clp->cl_lock);
  1479. if (list_empty(&lo->plh_layouts))
  1480. list_add_tail(&lo->plh_layouts, &server->layouts);
  1481. spin_unlock(&clp->cl_lock);
  1482. }
  1483. pg_offset = arg.offset & ~PAGE_MASK;
  1484. if (pg_offset) {
  1485. arg.offset -= pg_offset;
  1486. arg.length += pg_offset;
  1487. }
  1488. if (arg.length != NFS4_MAX_UINT64)
  1489. arg.length = PAGE_ALIGN(arg.length);
  1490. lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
  1491. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1492. PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
  1493. atomic_dec(&lo->plh_outstanding);
  1494. if (IS_ERR(lseg)) {
  1495. switch(PTR_ERR(lseg)) {
  1496. case -EBUSY:
  1497. if (time_after(jiffies, giveup))
  1498. lseg = NULL;
  1499. break;
  1500. case -ERECALLCONFLICT:
  1501. /* Huh? We hold no layouts, how is there a recall? */
  1502. if (first) {
  1503. lseg = NULL;
  1504. break;
  1505. }
  1506. /* Destroy the existing layout and start over */
  1507. if (time_after(jiffies, giveup))
  1508. pnfs_destroy_layout(NFS_I(ino));
  1509. /* Fallthrough */
  1510. case -EAGAIN:
  1511. break;
  1512. default:
  1513. if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
  1514. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  1515. lseg = NULL;
  1516. }
  1517. goto out_put_layout_hdr;
  1518. }
  1519. if (lseg) {
  1520. if (first)
  1521. pnfs_clear_first_layoutget(lo);
  1522. trace_pnfs_update_layout(ino, pos, count,
  1523. iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
  1524. pnfs_put_layout_hdr(lo);
  1525. goto lookup_again;
  1526. }
  1527. } else {
  1528. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  1529. }
  1530. out_put_layout_hdr:
  1531. if (first)
  1532. pnfs_clear_first_layoutget(lo);
  1533. pnfs_put_layout_hdr(lo);
  1534. out:
  1535. dprintk("%s: inode %s/%llu pNFS layout segment %s for "
  1536. "(%s, offset: %llu, length: %llu)\n",
  1537. __func__, ino->i_sb->s_id,
  1538. (unsigned long long)NFS_FILEID(ino),
  1539. IS_ERR_OR_NULL(lseg) ? "not found" : "found",
  1540. iomode==IOMODE_RW ? "read/write" : "read-only",
  1541. (unsigned long long)pos,
  1542. (unsigned long long)count);
  1543. return lseg;
  1544. out_unlock:
  1545. spin_unlock(&ino->i_lock);
  1546. goto out_put_layout_hdr;
  1547. }
  1548. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  1549. static bool
  1550. pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
  1551. {
  1552. switch (range->iomode) {
  1553. case IOMODE_READ:
  1554. case IOMODE_RW:
  1555. break;
  1556. default:
  1557. return false;
  1558. }
  1559. if (range->offset == NFS4_MAX_UINT64)
  1560. return false;
  1561. if (range->length == 0)
  1562. return false;
  1563. if (range->length != NFS4_MAX_UINT64 &&
  1564. range->length > NFS4_MAX_UINT64 - range->offset)
  1565. return false;
  1566. return true;
  1567. }
  1568. struct pnfs_layout_segment *
  1569. pnfs_layout_process(struct nfs4_layoutget *lgp)
  1570. {
  1571. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  1572. struct nfs4_layoutget_res *res = &lgp->res;
  1573. struct pnfs_layout_segment *lseg;
  1574. struct inode *ino = lo->plh_inode;
  1575. LIST_HEAD(free_me);
  1576. if (!pnfs_sanity_check_layout_range(&res->range))
  1577. return ERR_PTR(-EINVAL);
  1578. /* Inject layout blob into I/O device driver */
  1579. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  1580. if (IS_ERR_OR_NULL(lseg)) {
  1581. if (!lseg)
  1582. lseg = ERR_PTR(-ENOMEM);
  1583. dprintk("%s: Could not allocate layout: error %ld\n",
  1584. __func__, PTR_ERR(lseg));
  1585. return lseg;
  1586. }
  1587. pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
  1588. spin_lock(&ino->i_lock);
  1589. if (pnfs_layoutgets_blocked(lo)) {
  1590. dprintk("%s forget reply due to state\n", __func__);
  1591. goto out_forget;
  1592. }
  1593. if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
  1594. /* existing state ID, make sure the sequence number matches. */
  1595. if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
  1596. dprintk("%s forget reply due to sequence\n", __func__);
  1597. goto out_forget;
  1598. }
  1599. pnfs_set_layout_stateid(lo, &res->stateid, false);
  1600. } else {
  1601. /*
  1602. * We got an entirely new state ID. Mark all segments for the
  1603. * inode invalid, and don't bother validating the stateid
  1604. * sequence number.
  1605. */
  1606. pnfs_mark_layout_stateid_invalid(lo, &free_me);
  1607. pnfs_set_layout_stateid(lo, &res->stateid, true);
  1608. }
  1609. pnfs_get_lseg(lseg);
  1610. pnfs_layout_insert_lseg(lo, lseg, &free_me);
  1611. if (res->return_on_close)
  1612. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  1613. spin_unlock(&ino->i_lock);
  1614. pnfs_free_lseg_list(&free_me);
  1615. return lseg;
  1616. out_forget:
  1617. spin_unlock(&ino->i_lock);
  1618. lseg->pls_layout = lo;
  1619. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  1620. return ERR_PTR(-EAGAIN);
  1621. }
  1622. static void
  1623. pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
  1624. u32 seq)
  1625. {
  1626. if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
  1627. iomode = IOMODE_ANY;
  1628. lo->plh_return_iomode = iomode;
  1629. set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
  1630. if (seq != 0) {
  1631. WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
  1632. lo->plh_return_seq = seq;
  1633. }
  1634. }
  1635. /**
  1636. * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
  1637. * @lo: pointer to layout header
  1638. * @tmp_list: list header to be used with pnfs_free_lseg_list()
  1639. * @return_range: describe layout segment ranges to be returned
  1640. *
  1641. * This function is mainly intended for use by layoutrecall. It attempts
  1642. * to free the layout segment immediately, or else to mark it for return
  1643. * as soon as its reference count drops to zero.
  1644. */
  1645. int
  1646. pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
  1647. struct list_head *tmp_list,
  1648. const struct pnfs_layout_range *return_range,
  1649. u32 seq)
  1650. {
  1651. struct pnfs_layout_segment *lseg, *next;
  1652. int remaining = 0;
  1653. dprintk("%s:Begin lo %p\n", __func__, lo);
  1654. if (list_empty(&lo->plh_segs))
  1655. return 0;
  1656. assert_spin_locked(&lo->plh_inode->i_lock);
  1657. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  1658. if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
  1659. dprintk("%s: marking lseg %p iomode %d "
  1660. "offset %llu length %llu\n", __func__,
  1661. lseg, lseg->pls_range.iomode,
  1662. lseg->pls_range.offset,
  1663. lseg->pls_range.length);
  1664. if (mark_lseg_invalid(lseg, tmp_list))
  1665. continue;
  1666. remaining++;
  1667. set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
  1668. }
  1669. if (remaining)
  1670. pnfs_set_plh_return_info(lo, return_range->iomode, seq);
  1671. return remaining;
  1672. }
  1673. void pnfs_error_mark_layout_for_return(struct inode *inode,
  1674. struct pnfs_layout_segment *lseg)
  1675. {
  1676. struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
  1677. struct pnfs_layout_range range = {
  1678. .iomode = lseg->pls_range.iomode,
  1679. .offset = 0,
  1680. .length = NFS4_MAX_UINT64,
  1681. };
  1682. LIST_HEAD(free_me);
  1683. bool return_now = false;
  1684. spin_lock(&inode->i_lock);
  1685. pnfs_set_plh_return_info(lo, range.iomode, 0);
  1686. /*
  1687. * mark all matching lsegs so that we are sure to have no live
  1688. * segments at hand when sending layoutreturn. See pnfs_put_lseg()
  1689. * for how it works.
  1690. */
  1691. if (!pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0)) {
  1692. nfs4_stateid stateid;
  1693. enum pnfs_iomode iomode;
  1694. return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
  1695. spin_unlock(&inode->i_lock);
  1696. if (return_now)
  1697. pnfs_send_layoutreturn(lo, &stateid, iomode, false);
  1698. } else {
  1699. spin_unlock(&inode->i_lock);
  1700. nfs_commit_inode(inode, 0);
  1701. }
  1702. pnfs_free_lseg_list(&free_me);
  1703. }
  1704. EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
  1705. void
  1706. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  1707. {
  1708. u64 rd_size = req->wb_bytes;
  1709. if (pgio->pg_lseg == NULL) {
  1710. if (pgio->pg_dreq == NULL)
  1711. rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
  1712. else
  1713. rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
  1714. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  1715. req->wb_context,
  1716. req_offset(req),
  1717. rd_size,
  1718. IOMODE_READ,
  1719. false,
  1720. GFP_KERNEL);
  1721. if (IS_ERR(pgio->pg_lseg)) {
  1722. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  1723. pgio->pg_lseg = NULL;
  1724. return;
  1725. }
  1726. }
  1727. /* If no lseg, fall back to read through mds */
  1728. if (pgio->pg_lseg == NULL)
  1729. nfs_pageio_reset_read_mds(pgio);
  1730. }
  1731. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  1732. void
  1733. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
  1734. struct nfs_page *req, u64 wb_size)
  1735. {
  1736. if (pgio->pg_lseg == NULL) {
  1737. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  1738. req->wb_context,
  1739. req_offset(req),
  1740. wb_size,
  1741. IOMODE_RW,
  1742. false,
  1743. GFP_NOFS);
  1744. if (IS_ERR(pgio->pg_lseg)) {
  1745. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  1746. pgio->pg_lseg = NULL;
  1747. return;
  1748. }
  1749. }
  1750. /* If no lseg, fall back to write through mds */
  1751. if (pgio->pg_lseg == NULL)
  1752. nfs_pageio_reset_write_mds(pgio);
  1753. }
  1754. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  1755. void
  1756. pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
  1757. {
  1758. if (desc->pg_lseg) {
  1759. pnfs_put_lseg(desc->pg_lseg);
  1760. desc->pg_lseg = NULL;
  1761. }
  1762. }
  1763. EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
  1764. /*
  1765. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  1766. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  1767. */
  1768. size_t
  1769. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
  1770. struct nfs_page *prev, struct nfs_page *req)
  1771. {
  1772. unsigned int size;
  1773. u64 seg_end, req_start, seg_left;
  1774. size = nfs_generic_pg_test(pgio, prev, req);
  1775. if (!size)
  1776. return 0;
  1777. /*
  1778. * 'size' contains the number of bytes left in the current page (up
  1779. * to the original size asked for in @req->wb_bytes).
  1780. *
  1781. * Calculate how many bytes are left in the layout segment
  1782. * and if there are less bytes than 'size', return that instead.
  1783. *
  1784. * Please also note that 'end_offset' is actually the offset of the
  1785. * first byte that lies outside the pnfs_layout_range. FIXME?
  1786. *
  1787. */
  1788. if (pgio->pg_lseg) {
  1789. seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
  1790. pgio->pg_lseg->pls_range.length);
  1791. req_start = req_offset(req);
  1792. WARN_ON_ONCE(req_start >= seg_end);
  1793. /* start of request is past the last byte of this segment */
  1794. if (req_start >= seg_end) {
  1795. /* reference the new lseg */
  1796. if (pgio->pg_ops->pg_cleanup)
  1797. pgio->pg_ops->pg_cleanup(pgio);
  1798. if (pgio->pg_ops->pg_init)
  1799. pgio->pg_ops->pg_init(pgio, req);
  1800. return 0;
  1801. }
  1802. /* adjust 'size' iff there are fewer bytes left in the
  1803. * segment than what nfs_generic_pg_test returned */
  1804. seg_left = seg_end - req_start;
  1805. if (seg_left < size)
  1806. size = (unsigned int)seg_left;
  1807. }
  1808. return size;
  1809. }
  1810. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  1811. int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
  1812. {
  1813. struct nfs_pageio_descriptor pgio;
  1814. /* Resend all requests through the MDS */
  1815. nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
  1816. hdr->completion_ops);
  1817. set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
  1818. return nfs_pageio_resend(&pgio, hdr);
  1819. }
  1820. EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
  1821. static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
  1822. {
  1823. dprintk("pnfs write error = %d\n", hdr->pnfs_error);
  1824. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1825. PNFS_LAYOUTRET_ON_ERROR) {
  1826. pnfs_return_layout(hdr->inode);
  1827. }
  1828. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1829. hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
  1830. }
  1831. /*
  1832. * Called by non rpc-based layout drivers
  1833. */
  1834. void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
  1835. {
  1836. if (likely(!hdr->pnfs_error)) {
  1837. pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
  1838. hdr->mds_offset + hdr->res.count);
  1839. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  1840. }
  1841. trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
  1842. if (unlikely(hdr->pnfs_error))
  1843. pnfs_ld_handle_write_error(hdr);
  1844. hdr->mds_ops->rpc_release(hdr);
  1845. }
  1846. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  1847. static void
  1848. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  1849. struct nfs_pgio_header *hdr)
  1850. {
  1851. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  1852. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1853. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  1854. nfs_pageio_reset_write_mds(desc);
  1855. mirror->pg_recoalesce = 1;
  1856. }
  1857. nfs_pgio_data_destroy(hdr);
  1858. hdr->release(hdr);
  1859. }
  1860. static enum pnfs_try_status
  1861. pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
  1862. const struct rpc_call_ops *call_ops,
  1863. struct pnfs_layout_segment *lseg,
  1864. int how)
  1865. {
  1866. struct inode *inode = hdr->inode;
  1867. enum pnfs_try_status trypnfs;
  1868. struct nfs_server *nfss = NFS_SERVER(inode);
  1869. hdr->mds_ops = call_ops;
  1870. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  1871. inode->i_ino, hdr->args.count, hdr->args.offset, how);
  1872. trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
  1873. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1874. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  1875. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1876. return trypnfs;
  1877. }
  1878. static void
  1879. pnfs_do_write(struct nfs_pageio_descriptor *desc,
  1880. struct nfs_pgio_header *hdr, int how)
  1881. {
  1882. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1883. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1884. enum pnfs_try_status trypnfs;
  1885. trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
  1886. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1887. pnfs_write_through_mds(desc, hdr);
  1888. }
  1889. static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
  1890. {
  1891. pnfs_put_lseg(hdr->lseg);
  1892. nfs_pgio_header_free(hdr);
  1893. }
  1894. int
  1895. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  1896. {
  1897. struct nfs_pgio_header *hdr;
  1898. int ret;
  1899. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  1900. if (!hdr) {
  1901. desc->pg_error = -ENOMEM;
  1902. return desc->pg_error;
  1903. }
  1904. nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
  1905. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  1906. ret = nfs_generic_pgio(desc, hdr);
  1907. if (!ret)
  1908. pnfs_do_write(desc, hdr, desc->pg_ioflags);
  1909. return ret;
  1910. }
  1911. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  1912. int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
  1913. {
  1914. struct nfs_pageio_descriptor pgio;
  1915. /* Resend all requests through the MDS */
  1916. nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
  1917. return nfs_pageio_resend(&pgio, hdr);
  1918. }
  1919. EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
  1920. static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
  1921. {
  1922. dprintk("pnfs read error = %d\n", hdr->pnfs_error);
  1923. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1924. PNFS_LAYOUTRET_ON_ERROR) {
  1925. pnfs_return_layout(hdr->inode);
  1926. }
  1927. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1928. hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
  1929. }
  1930. /*
  1931. * Called by non rpc-based layout drivers
  1932. */
  1933. void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
  1934. {
  1935. if (likely(!hdr->pnfs_error)) {
  1936. __nfs4_read_done_cb(hdr);
  1937. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  1938. }
  1939. trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
  1940. if (unlikely(hdr->pnfs_error))
  1941. pnfs_ld_handle_read_error(hdr);
  1942. hdr->mds_ops->rpc_release(hdr);
  1943. }
  1944. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  1945. static void
  1946. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  1947. struct nfs_pgio_header *hdr)
  1948. {
  1949. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  1950. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1951. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  1952. nfs_pageio_reset_read_mds(desc);
  1953. mirror->pg_recoalesce = 1;
  1954. }
  1955. nfs_pgio_data_destroy(hdr);
  1956. hdr->release(hdr);
  1957. }
  1958. /*
  1959. * Call the appropriate parallel I/O subsystem read function.
  1960. */
  1961. static enum pnfs_try_status
  1962. pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
  1963. const struct rpc_call_ops *call_ops,
  1964. struct pnfs_layout_segment *lseg)
  1965. {
  1966. struct inode *inode = hdr->inode;
  1967. struct nfs_server *nfss = NFS_SERVER(inode);
  1968. enum pnfs_try_status trypnfs;
  1969. hdr->mds_ops = call_ops;
  1970. dprintk("%s: Reading ino:%lu %u@%llu\n",
  1971. __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
  1972. trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
  1973. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1974. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  1975. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1976. return trypnfs;
  1977. }
  1978. /* Resend all requests through pnfs. */
  1979. void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
  1980. {
  1981. struct nfs_pageio_descriptor pgio;
  1982. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1983. nfs_pageio_init_read(&pgio, hdr->inode, false,
  1984. hdr->completion_ops);
  1985. hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
  1986. }
  1987. }
  1988. EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
  1989. static void
  1990. pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
  1991. {
  1992. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1993. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1994. enum pnfs_try_status trypnfs;
  1995. trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
  1996. if (trypnfs == PNFS_TRY_AGAIN)
  1997. pnfs_read_resend_pnfs(hdr);
  1998. if (trypnfs == PNFS_NOT_ATTEMPTED || hdr->task.tk_status)
  1999. pnfs_read_through_mds(desc, hdr);
  2000. }
  2001. static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
  2002. {
  2003. pnfs_put_lseg(hdr->lseg);
  2004. nfs_pgio_header_free(hdr);
  2005. }
  2006. int
  2007. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  2008. {
  2009. struct nfs_pgio_header *hdr;
  2010. int ret;
  2011. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  2012. if (!hdr) {
  2013. desc->pg_error = -ENOMEM;
  2014. return desc->pg_error;
  2015. }
  2016. nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
  2017. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  2018. ret = nfs_generic_pgio(desc, hdr);
  2019. if (!ret)
  2020. pnfs_do_read(desc, hdr);
  2021. return ret;
  2022. }
  2023. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  2024. static void pnfs_clear_layoutcommitting(struct inode *inode)
  2025. {
  2026. unsigned long *bitlock = &NFS_I(inode)->flags;
  2027. clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
  2028. smp_mb__after_atomic();
  2029. wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
  2030. }
  2031. /*
  2032. * There can be multiple RW segments.
  2033. */
  2034. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  2035. {
  2036. struct pnfs_layout_segment *lseg;
  2037. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  2038. if (lseg->pls_range.iomode == IOMODE_RW &&
  2039. test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  2040. list_add(&lseg->pls_lc_list, listp);
  2041. }
  2042. }
  2043. static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
  2044. {
  2045. struct pnfs_layout_segment *lseg, *tmp;
  2046. /* Matched by references in pnfs_set_layoutcommit */
  2047. list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
  2048. list_del_init(&lseg->pls_lc_list);
  2049. pnfs_put_lseg(lseg);
  2050. }
  2051. pnfs_clear_layoutcommitting(inode);
  2052. }
  2053. void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
  2054. {
  2055. pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
  2056. }
  2057. EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
  2058. void
  2059. pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
  2060. loff_t end_pos)
  2061. {
  2062. struct nfs_inode *nfsi = NFS_I(inode);
  2063. bool mark_as_dirty = false;
  2064. spin_lock(&inode->i_lock);
  2065. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  2066. nfsi->layout->plh_lwb = end_pos;
  2067. mark_as_dirty = true;
  2068. dprintk("%s: Set layoutcommit for inode %lu ",
  2069. __func__, inode->i_ino);
  2070. } else if (end_pos > nfsi->layout->plh_lwb)
  2071. nfsi->layout->plh_lwb = end_pos;
  2072. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
  2073. /* references matched in nfs4_layoutcommit_release */
  2074. pnfs_get_lseg(lseg);
  2075. }
  2076. spin_unlock(&inode->i_lock);
  2077. dprintk("%s: lseg %p end_pos %llu\n",
  2078. __func__, lseg, nfsi->layout->plh_lwb);
  2079. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  2080. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  2081. if (mark_as_dirty)
  2082. mark_inode_dirty_sync(inode);
  2083. }
  2084. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  2085. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  2086. {
  2087. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  2088. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  2089. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  2090. pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
  2091. }
  2092. /*
  2093. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  2094. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  2095. * data to disk to allow the server to recover the data if it crashes.
  2096. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  2097. * is off, and a COMMIT is sent to a data server, or
  2098. * if WRITEs to a data server return NFS_DATA_SYNC.
  2099. */
  2100. int
  2101. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  2102. {
  2103. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  2104. struct nfs4_layoutcommit_data *data;
  2105. struct nfs_inode *nfsi = NFS_I(inode);
  2106. loff_t end_pos;
  2107. int status;
  2108. if (!pnfs_layoutcommit_outstanding(inode))
  2109. return 0;
  2110. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  2111. status = -EAGAIN;
  2112. if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
  2113. if (!sync)
  2114. goto out;
  2115. status = wait_on_bit_lock_action(&nfsi->flags,
  2116. NFS_INO_LAYOUTCOMMITTING,
  2117. nfs_wait_bit_killable,
  2118. TASK_KILLABLE);
  2119. if (status)
  2120. goto out;
  2121. }
  2122. status = -ENOMEM;
  2123. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  2124. data = kzalloc(sizeof(*data), GFP_NOFS);
  2125. if (!data)
  2126. goto clear_layoutcommitting;
  2127. status = 0;
  2128. spin_lock(&inode->i_lock);
  2129. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  2130. goto out_unlock;
  2131. INIT_LIST_HEAD(&data->lseg_list);
  2132. pnfs_list_write_lseg(inode, &data->lseg_list);
  2133. end_pos = nfsi->layout->plh_lwb;
  2134. nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
  2135. spin_unlock(&inode->i_lock);
  2136. data->args.inode = inode;
  2137. data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
  2138. nfs_fattr_init(&data->fattr);
  2139. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  2140. data->res.fattr = &data->fattr;
  2141. if (end_pos != 0)
  2142. data->args.lastbytewritten = end_pos - 1;
  2143. else
  2144. data->args.lastbytewritten = U64_MAX;
  2145. data->res.server = NFS_SERVER(inode);
  2146. if (ld->prepare_layoutcommit) {
  2147. status = ld->prepare_layoutcommit(&data->args);
  2148. if (status) {
  2149. put_rpccred(data->cred);
  2150. spin_lock(&inode->i_lock);
  2151. set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
  2152. if (end_pos > nfsi->layout->plh_lwb)
  2153. nfsi->layout->plh_lwb = end_pos;
  2154. goto out_unlock;
  2155. }
  2156. }
  2157. status = nfs4_proc_layoutcommit(data, sync);
  2158. out:
  2159. if (status)
  2160. mark_inode_dirty_sync(inode);
  2161. dprintk("<-- %s status %d\n", __func__, status);
  2162. return status;
  2163. out_unlock:
  2164. spin_unlock(&inode->i_lock);
  2165. kfree(data);
  2166. clear_layoutcommitting:
  2167. pnfs_clear_layoutcommitting(inode);
  2168. goto out;
  2169. }
  2170. EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
  2171. int
  2172. pnfs_generic_sync(struct inode *inode, bool datasync)
  2173. {
  2174. return pnfs_layoutcommit_inode(inode, true);
  2175. }
  2176. EXPORT_SYMBOL_GPL(pnfs_generic_sync);
  2177. struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
  2178. {
  2179. struct nfs4_threshold *thp;
  2180. thp = kzalloc(sizeof(*thp), GFP_NOFS);
  2181. if (!thp) {
  2182. dprintk("%s mdsthreshold allocation failed\n", __func__);
  2183. return NULL;
  2184. }
  2185. return thp;
  2186. }
  2187. #if IS_ENABLED(CONFIG_NFS_V4_2)
  2188. int
  2189. pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
  2190. {
  2191. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  2192. struct nfs_server *server = NFS_SERVER(inode);
  2193. struct nfs_inode *nfsi = NFS_I(inode);
  2194. struct nfs42_layoutstat_data *data;
  2195. struct pnfs_layout_hdr *hdr;
  2196. int status = 0;
  2197. if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
  2198. goto out;
  2199. if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
  2200. goto out;
  2201. if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
  2202. goto out;
  2203. spin_lock(&inode->i_lock);
  2204. if (!NFS_I(inode)->layout) {
  2205. spin_unlock(&inode->i_lock);
  2206. goto out_clear_layoutstats;
  2207. }
  2208. hdr = NFS_I(inode)->layout;
  2209. pnfs_get_layout_hdr(hdr);
  2210. spin_unlock(&inode->i_lock);
  2211. data = kzalloc(sizeof(*data), gfp_flags);
  2212. if (!data) {
  2213. status = -ENOMEM;
  2214. goto out_put;
  2215. }
  2216. data->args.fh = NFS_FH(inode);
  2217. data->args.inode = inode;
  2218. status = ld->prepare_layoutstats(&data->args);
  2219. if (status)
  2220. goto out_free;
  2221. status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
  2222. out:
  2223. dprintk("%s returns %d\n", __func__, status);
  2224. return status;
  2225. out_free:
  2226. kfree(data);
  2227. out_put:
  2228. pnfs_put_layout_hdr(hdr);
  2229. out_clear_layoutstats:
  2230. smp_mb__before_atomic();
  2231. clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
  2232. smp_mb__after_atomic();
  2233. goto out;
  2234. }
  2235. EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
  2236. #endif
  2237. unsigned int layoutstats_timer;
  2238. module_param(layoutstats_timer, uint, 0644);
  2239. EXPORT_SYMBOL_GPL(layoutstats_timer);