pnfs.c 61 KB

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