pnfs.c 62 KB

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