pnfs.c 67 KB

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