pnfs.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324
  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. lo->plh_block_lgets++;
  978. pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
  979. spin_unlock(&ino->i_lock);
  980. pnfs_free_lseg_list(&tmp_list);
  981. pnfs_layoutcommit_inode(ino, true);
  982. return true;
  983. out_noroc:
  984. if (lo) {
  985. stateid = lo->plh_stateid;
  986. if (test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
  987. &lo->plh_flags))
  988. layoutreturn = pnfs_prepare_layoutreturn(lo);
  989. }
  990. spin_unlock(&ino->i_lock);
  991. if (layoutreturn) {
  992. pnfs_layoutcommit_inode(ino, true);
  993. pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true);
  994. }
  995. return false;
  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. lo->plh_block_lgets--;
  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. }
  1019. bool pnfs_roc_drain(struct inode *ino, u32 *barrier, struct rpc_task *task)
  1020. {
  1021. struct nfs_inode *nfsi = NFS_I(ino);
  1022. struct pnfs_layout_hdr *lo;
  1023. struct pnfs_layout_segment *lseg;
  1024. nfs4_stateid stateid;
  1025. u32 current_seqid;
  1026. bool layoutreturn = false;
  1027. spin_lock(&ino->i_lock);
  1028. list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list) {
  1029. if (!test_bit(NFS_LSEG_ROC, &lseg->pls_flags))
  1030. continue;
  1031. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
  1032. continue;
  1033. rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
  1034. spin_unlock(&ino->i_lock);
  1035. return true;
  1036. }
  1037. lo = nfsi->layout;
  1038. current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
  1039. /* Since close does not return a layout stateid for use as
  1040. * a barrier, we choose the worst-case barrier.
  1041. */
  1042. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  1043. stateid = lo->plh_stateid;
  1044. if (test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
  1045. &lo->plh_flags))
  1046. layoutreturn = pnfs_prepare_layoutreturn(lo);
  1047. if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
  1048. rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
  1049. spin_unlock(&ino->i_lock);
  1050. if (layoutreturn) {
  1051. pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, false);
  1052. return true;
  1053. }
  1054. return false;
  1055. }
  1056. /*
  1057. * Compare two layout segments for sorting into layout cache.
  1058. * We want to preferentially return RW over RO layouts, so ensure those
  1059. * are seen first.
  1060. */
  1061. static s64
  1062. pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
  1063. const struct pnfs_layout_range *l2)
  1064. {
  1065. s64 d;
  1066. /* high offset > low offset */
  1067. d = l1->offset - l2->offset;
  1068. if (d)
  1069. return d;
  1070. /* short length > long length */
  1071. d = l2->length - l1->length;
  1072. if (d)
  1073. return d;
  1074. /* read > read/write */
  1075. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  1076. }
  1077. static void
  1078. pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  1079. struct pnfs_layout_segment *lseg)
  1080. {
  1081. struct pnfs_layout_segment *lp;
  1082. dprintk("%s:Begin\n", __func__);
  1083. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  1084. if (pnfs_lseg_range_cmp(&lseg->pls_range, &lp->pls_range) > 0)
  1085. continue;
  1086. list_add_tail(&lseg->pls_list, &lp->pls_list);
  1087. dprintk("%s: inserted lseg %p "
  1088. "iomode %d offset %llu length %llu before "
  1089. "lp %p iomode %d offset %llu length %llu\n",
  1090. __func__, lseg, lseg->pls_range.iomode,
  1091. lseg->pls_range.offset, lseg->pls_range.length,
  1092. lp, lp->pls_range.iomode, lp->pls_range.offset,
  1093. lp->pls_range.length);
  1094. goto out;
  1095. }
  1096. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  1097. dprintk("%s: inserted lseg %p "
  1098. "iomode %d offset %llu length %llu at tail\n",
  1099. __func__, lseg, lseg->pls_range.iomode,
  1100. lseg->pls_range.offset, lseg->pls_range.length);
  1101. out:
  1102. pnfs_get_layout_hdr(lo);
  1103. dprintk("%s:Return\n", __func__);
  1104. }
  1105. static struct pnfs_layout_hdr *
  1106. alloc_init_layout_hdr(struct inode *ino,
  1107. struct nfs_open_context *ctx,
  1108. gfp_t gfp_flags)
  1109. {
  1110. struct pnfs_layout_hdr *lo;
  1111. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  1112. if (!lo)
  1113. return NULL;
  1114. atomic_set(&lo->plh_refcount, 1);
  1115. INIT_LIST_HEAD(&lo->plh_layouts);
  1116. INIT_LIST_HEAD(&lo->plh_segs);
  1117. INIT_LIST_HEAD(&lo->plh_bulk_destroy);
  1118. lo->plh_inode = ino;
  1119. lo->plh_lc_cred = get_rpccred(ctx->cred);
  1120. return lo;
  1121. }
  1122. static struct pnfs_layout_hdr *
  1123. pnfs_find_alloc_layout(struct inode *ino,
  1124. struct nfs_open_context *ctx,
  1125. gfp_t gfp_flags)
  1126. {
  1127. struct nfs_inode *nfsi = NFS_I(ino);
  1128. struct pnfs_layout_hdr *new = NULL;
  1129. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  1130. if (nfsi->layout != NULL)
  1131. goto out_existing;
  1132. spin_unlock(&ino->i_lock);
  1133. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  1134. spin_lock(&ino->i_lock);
  1135. if (likely(nfsi->layout == NULL)) { /* Won the race? */
  1136. nfsi->layout = new;
  1137. return new;
  1138. } else if (new != NULL)
  1139. pnfs_free_layout_hdr(new);
  1140. out_existing:
  1141. pnfs_get_layout_hdr(nfsi->layout);
  1142. return nfsi->layout;
  1143. }
  1144. /*
  1145. * iomode matching rules:
  1146. * iomode lseg match
  1147. * ----- ----- -----
  1148. * ANY READ true
  1149. * ANY RW true
  1150. * RW READ false
  1151. * RW RW true
  1152. * READ READ true
  1153. * READ RW true
  1154. */
  1155. static bool
  1156. pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
  1157. const struct pnfs_layout_range *range)
  1158. {
  1159. struct pnfs_layout_range range1;
  1160. if ((range->iomode == IOMODE_RW &&
  1161. ls_range->iomode != IOMODE_RW) ||
  1162. !pnfs_lseg_range_intersecting(ls_range, range))
  1163. return 0;
  1164. /* range1 covers only the first byte in the range */
  1165. range1 = *range;
  1166. range1.length = 1;
  1167. return pnfs_lseg_range_contained(ls_range, &range1);
  1168. }
  1169. /*
  1170. * lookup range in layout
  1171. */
  1172. static struct pnfs_layout_segment *
  1173. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  1174. struct pnfs_layout_range *range)
  1175. {
  1176. struct pnfs_layout_segment *lseg, *ret = NULL;
  1177. dprintk("%s:Begin\n", __func__);
  1178. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  1179. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  1180. !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
  1181. pnfs_lseg_range_match(&lseg->pls_range, range)) {
  1182. ret = pnfs_get_lseg(lseg);
  1183. break;
  1184. }
  1185. if (lseg->pls_range.offset > range->offset)
  1186. break;
  1187. }
  1188. dprintk("%s:Return lseg %p ref %d\n",
  1189. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  1190. return ret;
  1191. }
  1192. /*
  1193. * Use mdsthreshold hints set at each OPEN to determine if I/O should go
  1194. * to the MDS or over pNFS
  1195. *
  1196. * The nfs_inode read_io and write_io fields are cumulative counters reset
  1197. * when there are no layout segments. Note that in pnfs_update_layout iomode
  1198. * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
  1199. * WRITE request.
  1200. *
  1201. * A return of true means use MDS I/O.
  1202. *
  1203. * From rfc 5661:
  1204. * If a file's size is smaller than the file size threshold, data accesses
  1205. * SHOULD be sent to the metadata server. If an I/O request has a length that
  1206. * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
  1207. * server. If both file size and I/O size are provided, the client SHOULD
  1208. * reach or exceed both thresholds before sending its read or write
  1209. * requests to the data server.
  1210. */
  1211. static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
  1212. struct inode *ino, int iomode)
  1213. {
  1214. struct nfs4_threshold *t = ctx->mdsthreshold;
  1215. struct nfs_inode *nfsi = NFS_I(ino);
  1216. loff_t fsize = i_size_read(ino);
  1217. bool size = false, size_set = false, io = false, io_set = false, ret = false;
  1218. if (t == NULL)
  1219. return ret;
  1220. dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
  1221. __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
  1222. switch (iomode) {
  1223. case IOMODE_READ:
  1224. if (t->bm & THRESHOLD_RD) {
  1225. dprintk("%s fsize %llu\n", __func__, fsize);
  1226. size_set = true;
  1227. if (fsize < t->rd_sz)
  1228. size = true;
  1229. }
  1230. if (t->bm & THRESHOLD_RD_IO) {
  1231. dprintk("%s nfsi->read_io %llu\n", __func__,
  1232. nfsi->read_io);
  1233. io_set = true;
  1234. if (nfsi->read_io < t->rd_io_sz)
  1235. io = true;
  1236. }
  1237. break;
  1238. case IOMODE_RW:
  1239. if (t->bm & THRESHOLD_WR) {
  1240. dprintk("%s fsize %llu\n", __func__, fsize);
  1241. size_set = true;
  1242. if (fsize < t->wr_sz)
  1243. size = true;
  1244. }
  1245. if (t->bm & THRESHOLD_WR_IO) {
  1246. dprintk("%s nfsi->write_io %llu\n", __func__,
  1247. nfsi->write_io);
  1248. io_set = true;
  1249. if (nfsi->write_io < t->wr_io_sz)
  1250. io = true;
  1251. }
  1252. break;
  1253. }
  1254. if (size_set && io_set) {
  1255. if (size && io)
  1256. ret = true;
  1257. } else if (size || io)
  1258. ret = true;
  1259. dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
  1260. return ret;
  1261. }
  1262. /* stop waiting if someone clears NFS_LAYOUT_RETRY_LAYOUTGET bit. */
  1263. static int pnfs_layoutget_retry_bit_wait(struct wait_bit_key *key)
  1264. {
  1265. if (!test_bit(NFS_LAYOUT_RETRY_LAYOUTGET, key->flags))
  1266. return 1;
  1267. return nfs_wait_bit_killable(key);
  1268. }
  1269. static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
  1270. {
  1271. /*
  1272. * send layoutcommit as it can hold up layoutreturn due to lseg
  1273. * reference
  1274. */
  1275. pnfs_layoutcommit_inode(lo->plh_inode, false);
  1276. return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1277. pnfs_layoutget_retry_bit_wait,
  1278. TASK_UNINTERRUPTIBLE);
  1279. }
  1280. static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
  1281. {
  1282. unsigned long *bitlock = &lo->plh_flags;
  1283. clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
  1284. smp_mb__after_atomic();
  1285. wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
  1286. }
  1287. /*
  1288. * Layout segment is retreived from the server if not cached.
  1289. * The appropriate layout segment is referenced and returned to the caller.
  1290. */
  1291. struct pnfs_layout_segment *
  1292. pnfs_update_layout(struct inode *ino,
  1293. struct nfs_open_context *ctx,
  1294. loff_t pos,
  1295. u64 count,
  1296. enum pnfs_iomode iomode,
  1297. gfp_t gfp_flags)
  1298. {
  1299. struct pnfs_layout_range arg = {
  1300. .iomode = iomode,
  1301. .offset = pos,
  1302. .length = count,
  1303. };
  1304. unsigned pg_offset;
  1305. struct nfs_server *server = NFS_SERVER(ino);
  1306. struct nfs_client *clp = server->nfs_client;
  1307. struct pnfs_layout_hdr *lo;
  1308. struct pnfs_layout_segment *lseg = NULL;
  1309. bool first;
  1310. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  1311. goto out;
  1312. if (pnfs_within_mdsthreshold(ctx, ino, iomode))
  1313. goto out;
  1314. lookup_again:
  1315. first = false;
  1316. spin_lock(&ino->i_lock);
  1317. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  1318. if (lo == NULL) {
  1319. spin_unlock(&ino->i_lock);
  1320. goto out;
  1321. }
  1322. /* Do we even need to bother with this? */
  1323. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1324. dprintk("%s matches recall, use MDS\n", __func__);
  1325. goto out_unlock;
  1326. }
  1327. /* if LAYOUTGET already failed once we don't try again */
  1328. if (pnfs_layout_io_test_failed(lo, iomode) &&
  1329. !pnfs_should_retry_layoutget(lo))
  1330. goto out_unlock;
  1331. first = list_empty(&lo->plh_segs);
  1332. if (first) {
  1333. /* The first layoutget for the file. Need to serialize per
  1334. * RFC 5661 Errata 3208.
  1335. */
  1336. if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
  1337. &lo->plh_flags)) {
  1338. spin_unlock(&ino->i_lock);
  1339. wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
  1340. TASK_UNINTERRUPTIBLE);
  1341. pnfs_put_layout_hdr(lo);
  1342. goto lookup_again;
  1343. }
  1344. } else {
  1345. /* Check to see if the layout for the given range
  1346. * already exists
  1347. */
  1348. lseg = pnfs_find_lseg(lo, &arg);
  1349. if (lseg)
  1350. goto out_unlock;
  1351. }
  1352. /*
  1353. * Because we free lsegs before sending LAYOUTRETURN, we need to wait
  1354. * for LAYOUTRETURN even if first is true.
  1355. */
  1356. if (!lseg && pnfs_should_retry_layoutget(lo) &&
  1357. test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
  1358. spin_unlock(&ino->i_lock);
  1359. dprintk("%s wait for layoutreturn\n", __func__);
  1360. if (pnfs_prepare_to_retry_layoutget(lo)) {
  1361. if (first)
  1362. pnfs_clear_first_layoutget(lo);
  1363. pnfs_put_layout_hdr(lo);
  1364. dprintk("%s retrying\n", __func__);
  1365. goto lookup_again;
  1366. }
  1367. goto out_put_layout_hdr;
  1368. }
  1369. if (pnfs_layoutgets_blocked(lo, &arg))
  1370. goto out_unlock;
  1371. atomic_inc(&lo->plh_outstanding);
  1372. spin_unlock(&ino->i_lock);
  1373. if (list_empty(&lo->plh_layouts)) {
  1374. /* The lo must be on the clp list if there is any
  1375. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  1376. */
  1377. spin_lock(&clp->cl_lock);
  1378. if (list_empty(&lo->plh_layouts))
  1379. list_add_tail(&lo->plh_layouts, &server->layouts);
  1380. spin_unlock(&clp->cl_lock);
  1381. }
  1382. pg_offset = arg.offset & ~PAGE_CACHE_MASK;
  1383. if (pg_offset) {
  1384. arg.offset -= pg_offset;
  1385. arg.length += pg_offset;
  1386. }
  1387. if (arg.length != NFS4_MAX_UINT64)
  1388. arg.length = PAGE_CACHE_ALIGN(arg.length);
  1389. lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
  1390. pnfs_clear_retry_layoutget(lo);
  1391. atomic_dec(&lo->plh_outstanding);
  1392. out_put_layout_hdr:
  1393. if (first)
  1394. pnfs_clear_first_layoutget(lo);
  1395. pnfs_put_layout_hdr(lo);
  1396. out:
  1397. dprintk("%s: inode %s/%llu pNFS layout segment %s for "
  1398. "(%s, offset: %llu, length: %llu)\n",
  1399. __func__, ino->i_sb->s_id,
  1400. (unsigned long long)NFS_FILEID(ino),
  1401. lseg == NULL ? "not found" : "found",
  1402. iomode==IOMODE_RW ? "read/write" : "read-only",
  1403. (unsigned long long)pos,
  1404. (unsigned long long)count);
  1405. return lseg;
  1406. out_unlock:
  1407. spin_unlock(&ino->i_lock);
  1408. goto out_put_layout_hdr;
  1409. }
  1410. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  1411. struct pnfs_layout_segment *
  1412. pnfs_layout_process(struct nfs4_layoutget *lgp)
  1413. {
  1414. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  1415. struct nfs4_layoutget_res *res = &lgp->res;
  1416. struct pnfs_layout_segment *lseg;
  1417. struct inode *ino = lo->plh_inode;
  1418. LIST_HEAD(free_me);
  1419. int status = 0;
  1420. /* Inject layout blob into I/O device driver */
  1421. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  1422. if (!lseg || IS_ERR(lseg)) {
  1423. if (!lseg)
  1424. status = -ENOMEM;
  1425. else
  1426. status = PTR_ERR(lseg);
  1427. dprintk("%s: Could not allocate layout: error %d\n",
  1428. __func__, status);
  1429. goto out;
  1430. }
  1431. init_lseg(lo, lseg);
  1432. lseg->pls_range = res->range;
  1433. spin_lock(&ino->i_lock);
  1434. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1435. dprintk("%s forget reply due to recall\n", __func__);
  1436. goto out_forget_reply;
  1437. }
  1438. if (pnfs_layoutgets_blocked(lo, &lgp->args.range)) {
  1439. dprintk("%s forget reply due to state\n", __func__);
  1440. goto out_forget_reply;
  1441. }
  1442. if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
  1443. /* existing state ID, make sure the sequence number matches. */
  1444. if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
  1445. dprintk("%s forget reply due to sequence\n", __func__);
  1446. goto out_forget_reply;
  1447. }
  1448. pnfs_set_layout_stateid(lo, &res->stateid, false);
  1449. } else {
  1450. /*
  1451. * We got an entirely new state ID. Mark all segments for the
  1452. * inode invalid, and don't bother validating the stateid
  1453. * sequence number.
  1454. */
  1455. pnfs_mark_matching_lsegs_invalid(lo, &free_me, NULL);
  1456. nfs4_stateid_copy(&lo->plh_stateid, &res->stateid);
  1457. lo->plh_barrier = be32_to_cpu(res->stateid.seqid);
  1458. }
  1459. clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
  1460. pnfs_get_lseg(lseg);
  1461. pnfs_layout_insert_lseg(lo, lseg);
  1462. if (res->return_on_close) {
  1463. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  1464. set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
  1465. }
  1466. spin_unlock(&ino->i_lock);
  1467. pnfs_free_lseg_list(&free_me);
  1468. return lseg;
  1469. out:
  1470. return ERR_PTR(status);
  1471. out_forget_reply:
  1472. spin_unlock(&ino->i_lock);
  1473. lseg->pls_layout = lo;
  1474. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  1475. goto out;
  1476. }
  1477. static void
  1478. pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
  1479. struct list_head *tmp_list,
  1480. struct pnfs_layout_range *return_range)
  1481. {
  1482. struct pnfs_layout_segment *lseg, *next;
  1483. dprintk("%s:Begin lo %p\n", __func__, lo);
  1484. if (list_empty(&lo->plh_segs))
  1485. return;
  1486. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  1487. if (should_free_lseg(&lseg->pls_range, return_range)) {
  1488. dprintk("%s: marking lseg %p iomode %d "
  1489. "offset %llu length %llu\n", __func__,
  1490. lseg, lseg->pls_range.iomode,
  1491. lseg->pls_range.offset,
  1492. lseg->pls_range.length);
  1493. set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
  1494. mark_lseg_invalid(lseg, tmp_list);
  1495. }
  1496. }
  1497. void pnfs_error_mark_layout_for_return(struct inode *inode,
  1498. struct pnfs_layout_segment *lseg)
  1499. {
  1500. struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
  1501. int iomode = pnfs_iomode_to_fail_bit(lseg->pls_range.iomode);
  1502. struct pnfs_layout_range range = {
  1503. .iomode = lseg->pls_range.iomode,
  1504. .offset = 0,
  1505. .length = NFS4_MAX_UINT64,
  1506. };
  1507. LIST_HEAD(free_me);
  1508. spin_lock(&inode->i_lock);
  1509. /* set failure bit so that pnfs path will be retried later */
  1510. pnfs_layout_set_fail_bit(lo, iomode);
  1511. if (lo->plh_return_iomode == 0)
  1512. lo->plh_return_iomode = range.iomode;
  1513. else if (lo->plh_return_iomode != range.iomode)
  1514. lo->plh_return_iomode = IOMODE_ANY;
  1515. /*
  1516. * mark all matching lsegs so that we are sure to have no live
  1517. * segments at hand when sending layoutreturn. See pnfs_put_lseg()
  1518. * for how it works.
  1519. */
  1520. pnfs_mark_matching_lsegs_return(lo, &free_me, &range);
  1521. spin_unlock(&inode->i_lock);
  1522. pnfs_free_lseg_list(&free_me);
  1523. }
  1524. EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
  1525. void
  1526. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  1527. {
  1528. u64 rd_size = req->wb_bytes;
  1529. if (pgio->pg_lseg == NULL) {
  1530. if (pgio->pg_dreq == NULL)
  1531. rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
  1532. else
  1533. rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
  1534. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  1535. req->wb_context,
  1536. req_offset(req),
  1537. rd_size,
  1538. IOMODE_READ,
  1539. GFP_KERNEL);
  1540. }
  1541. /* If no lseg, fall back to read through mds */
  1542. if (pgio->pg_lseg == NULL)
  1543. nfs_pageio_reset_read_mds(pgio);
  1544. }
  1545. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  1546. void
  1547. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
  1548. struct nfs_page *req, u64 wb_size)
  1549. {
  1550. if (pgio->pg_lseg == NULL)
  1551. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  1552. req->wb_context,
  1553. req_offset(req),
  1554. wb_size,
  1555. IOMODE_RW,
  1556. GFP_NOFS);
  1557. /* If no lseg, fall back to write through mds */
  1558. if (pgio->pg_lseg == NULL)
  1559. nfs_pageio_reset_write_mds(pgio);
  1560. }
  1561. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  1562. void
  1563. pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
  1564. {
  1565. if (desc->pg_lseg) {
  1566. pnfs_put_lseg(desc->pg_lseg);
  1567. desc->pg_lseg = NULL;
  1568. }
  1569. }
  1570. EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
  1571. /*
  1572. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  1573. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  1574. */
  1575. size_t
  1576. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
  1577. struct nfs_page *prev, struct nfs_page *req)
  1578. {
  1579. unsigned int size;
  1580. u64 seg_end, req_start, seg_left;
  1581. size = nfs_generic_pg_test(pgio, prev, req);
  1582. if (!size)
  1583. return 0;
  1584. /*
  1585. * 'size' contains the number of bytes left in the current page (up
  1586. * to the original size asked for in @req->wb_bytes).
  1587. *
  1588. * Calculate how many bytes are left in the layout segment
  1589. * and if there are less bytes than 'size', return that instead.
  1590. *
  1591. * Please also note that 'end_offset' is actually the offset of the
  1592. * first byte that lies outside the pnfs_layout_range. FIXME?
  1593. *
  1594. */
  1595. if (pgio->pg_lseg) {
  1596. seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
  1597. pgio->pg_lseg->pls_range.length);
  1598. req_start = req_offset(req);
  1599. WARN_ON_ONCE(req_start >= seg_end);
  1600. /* start of request is past the last byte of this segment */
  1601. if (req_start >= seg_end) {
  1602. /* reference the new lseg */
  1603. if (pgio->pg_ops->pg_cleanup)
  1604. pgio->pg_ops->pg_cleanup(pgio);
  1605. if (pgio->pg_ops->pg_init)
  1606. pgio->pg_ops->pg_init(pgio, req);
  1607. return 0;
  1608. }
  1609. /* adjust 'size' iff there are fewer bytes left in the
  1610. * segment than what nfs_generic_pg_test returned */
  1611. seg_left = seg_end - req_start;
  1612. if (seg_left < size)
  1613. size = (unsigned int)seg_left;
  1614. }
  1615. return size;
  1616. }
  1617. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  1618. int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
  1619. {
  1620. struct nfs_pageio_descriptor pgio;
  1621. /* Resend all requests through the MDS */
  1622. nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
  1623. hdr->completion_ops);
  1624. set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
  1625. return nfs_pageio_resend(&pgio, hdr);
  1626. }
  1627. EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
  1628. static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
  1629. {
  1630. dprintk("pnfs write error = %d\n", hdr->pnfs_error);
  1631. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1632. PNFS_LAYOUTRET_ON_ERROR) {
  1633. pnfs_return_layout(hdr->inode);
  1634. }
  1635. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1636. hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
  1637. }
  1638. /*
  1639. * Called by non rpc-based layout drivers
  1640. */
  1641. void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
  1642. {
  1643. trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
  1644. if (!hdr->pnfs_error) {
  1645. pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
  1646. hdr->mds_offset + hdr->res.count);
  1647. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  1648. } else
  1649. pnfs_ld_handle_write_error(hdr);
  1650. hdr->mds_ops->rpc_release(hdr);
  1651. }
  1652. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  1653. static void
  1654. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  1655. struct nfs_pgio_header *hdr)
  1656. {
  1657. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  1658. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1659. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  1660. nfs_pageio_reset_write_mds(desc);
  1661. mirror->pg_recoalesce = 1;
  1662. }
  1663. nfs_pgio_data_destroy(hdr);
  1664. hdr->release(hdr);
  1665. }
  1666. static enum pnfs_try_status
  1667. pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
  1668. const struct rpc_call_ops *call_ops,
  1669. struct pnfs_layout_segment *lseg,
  1670. int how)
  1671. {
  1672. struct inode *inode = hdr->inode;
  1673. enum pnfs_try_status trypnfs;
  1674. struct nfs_server *nfss = NFS_SERVER(inode);
  1675. hdr->mds_ops = call_ops;
  1676. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  1677. inode->i_ino, hdr->args.count, hdr->args.offset, how);
  1678. trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
  1679. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1680. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  1681. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1682. return trypnfs;
  1683. }
  1684. static void
  1685. pnfs_do_write(struct nfs_pageio_descriptor *desc,
  1686. struct nfs_pgio_header *hdr, int how)
  1687. {
  1688. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1689. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1690. enum pnfs_try_status trypnfs;
  1691. trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
  1692. if (trypnfs == PNFS_NOT_ATTEMPTED)
  1693. pnfs_write_through_mds(desc, hdr);
  1694. }
  1695. static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
  1696. {
  1697. pnfs_put_lseg(hdr->lseg);
  1698. nfs_pgio_header_free(hdr);
  1699. }
  1700. int
  1701. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  1702. {
  1703. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  1704. struct nfs_pgio_header *hdr;
  1705. int ret;
  1706. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  1707. if (!hdr) {
  1708. desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
  1709. return -ENOMEM;
  1710. }
  1711. nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
  1712. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  1713. ret = nfs_generic_pgio(desc, hdr);
  1714. if (!ret)
  1715. pnfs_do_write(desc, hdr, desc->pg_ioflags);
  1716. return ret;
  1717. }
  1718. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  1719. int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
  1720. {
  1721. struct nfs_pageio_descriptor pgio;
  1722. /* Resend all requests through the MDS */
  1723. nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
  1724. return nfs_pageio_resend(&pgio, hdr);
  1725. }
  1726. EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
  1727. static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
  1728. {
  1729. dprintk("pnfs read error = %d\n", hdr->pnfs_error);
  1730. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  1731. PNFS_LAYOUTRET_ON_ERROR) {
  1732. pnfs_return_layout(hdr->inode);
  1733. }
  1734. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  1735. hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
  1736. }
  1737. /*
  1738. * Called by non rpc-based layout drivers
  1739. */
  1740. void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
  1741. {
  1742. trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
  1743. if (likely(!hdr->pnfs_error)) {
  1744. __nfs4_read_done_cb(hdr);
  1745. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  1746. } else
  1747. pnfs_ld_handle_read_error(hdr);
  1748. hdr->mds_ops->rpc_release(hdr);
  1749. }
  1750. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  1751. static void
  1752. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  1753. struct nfs_pgio_header *hdr)
  1754. {
  1755. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  1756. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  1757. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  1758. nfs_pageio_reset_read_mds(desc);
  1759. mirror->pg_recoalesce = 1;
  1760. }
  1761. nfs_pgio_data_destroy(hdr);
  1762. hdr->release(hdr);
  1763. }
  1764. /*
  1765. * Call the appropriate parallel I/O subsystem read function.
  1766. */
  1767. static enum pnfs_try_status
  1768. pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
  1769. const struct rpc_call_ops *call_ops,
  1770. struct pnfs_layout_segment *lseg)
  1771. {
  1772. struct inode *inode = hdr->inode;
  1773. struct nfs_server *nfss = NFS_SERVER(inode);
  1774. enum pnfs_try_status trypnfs;
  1775. hdr->mds_ops = call_ops;
  1776. dprintk("%s: Reading ino:%lu %u@%llu\n",
  1777. __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
  1778. trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
  1779. if (trypnfs != PNFS_NOT_ATTEMPTED)
  1780. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  1781. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  1782. return trypnfs;
  1783. }
  1784. /* Resend all requests through pnfs. */
  1785. int pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
  1786. {
  1787. struct nfs_pageio_descriptor pgio;
  1788. nfs_pageio_init_read(&pgio, hdr->inode, false, hdr->completion_ops);
  1789. return nfs_pageio_resend(&pgio, hdr);
  1790. }
  1791. EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
  1792. static void
  1793. pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
  1794. {
  1795. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  1796. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  1797. enum pnfs_try_status trypnfs;
  1798. int err = 0;
  1799. trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
  1800. if (trypnfs == PNFS_TRY_AGAIN)
  1801. err = pnfs_read_resend_pnfs(hdr);
  1802. if (trypnfs == PNFS_NOT_ATTEMPTED || err)
  1803. pnfs_read_through_mds(desc, hdr);
  1804. }
  1805. static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
  1806. {
  1807. pnfs_put_lseg(hdr->lseg);
  1808. nfs_pgio_header_free(hdr);
  1809. }
  1810. int
  1811. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  1812. {
  1813. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  1814. struct nfs_pgio_header *hdr;
  1815. int ret;
  1816. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  1817. if (!hdr) {
  1818. desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
  1819. return -ENOMEM;
  1820. }
  1821. nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
  1822. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  1823. ret = nfs_generic_pgio(desc, hdr);
  1824. if (!ret)
  1825. pnfs_do_read(desc, hdr);
  1826. return ret;
  1827. }
  1828. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  1829. static void pnfs_clear_layoutcommitting(struct inode *inode)
  1830. {
  1831. unsigned long *bitlock = &NFS_I(inode)->flags;
  1832. clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
  1833. smp_mb__after_atomic();
  1834. wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
  1835. }
  1836. /*
  1837. * There can be multiple RW segments.
  1838. */
  1839. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  1840. {
  1841. struct pnfs_layout_segment *lseg;
  1842. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  1843. if (lseg->pls_range.iomode == IOMODE_RW &&
  1844. test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  1845. list_add(&lseg->pls_lc_list, listp);
  1846. }
  1847. }
  1848. static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
  1849. {
  1850. struct pnfs_layout_segment *lseg, *tmp;
  1851. /* Matched by references in pnfs_set_layoutcommit */
  1852. list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
  1853. list_del_init(&lseg->pls_lc_list);
  1854. pnfs_put_lseg(lseg);
  1855. }
  1856. pnfs_clear_layoutcommitting(inode);
  1857. }
  1858. void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
  1859. {
  1860. pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
  1861. }
  1862. EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
  1863. void
  1864. pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
  1865. loff_t end_pos)
  1866. {
  1867. struct nfs_inode *nfsi = NFS_I(inode);
  1868. bool mark_as_dirty = false;
  1869. spin_lock(&inode->i_lock);
  1870. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  1871. nfsi->layout->plh_lwb = end_pos;
  1872. mark_as_dirty = true;
  1873. dprintk("%s: Set layoutcommit for inode %lu ",
  1874. __func__, inode->i_ino);
  1875. } else if (end_pos > nfsi->layout->plh_lwb)
  1876. nfsi->layout->plh_lwb = end_pos;
  1877. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
  1878. /* references matched in nfs4_layoutcommit_release */
  1879. pnfs_get_lseg(lseg);
  1880. }
  1881. spin_unlock(&inode->i_lock);
  1882. dprintk("%s: lseg %p end_pos %llu\n",
  1883. __func__, lseg, nfsi->layout->plh_lwb);
  1884. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  1885. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  1886. if (mark_as_dirty)
  1887. mark_inode_dirty_sync(inode);
  1888. }
  1889. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  1890. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  1891. {
  1892. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  1893. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  1894. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  1895. pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
  1896. }
  1897. /*
  1898. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  1899. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  1900. * data to disk to allow the server to recover the data if it crashes.
  1901. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  1902. * is off, and a COMMIT is sent to a data server, or
  1903. * if WRITEs to a data server return NFS_DATA_SYNC.
  1904. */
  1905. int
  1906. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  1907. {
  1908. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  1909. struct nfs4_layoutcommit_data *data;
  1910. struct nfs_inode *nfsi = NFS_I(inode);
  1911. loff_t end_pos;
  1912. int status;
  1913. if (!pnfs_layoutcommit_outstanding(inode))
  1914. return 0;
  1915. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  1916. status = -EAGAIN;
  1917. if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
  1918. if (!sync)
  1919. goto out;
  1920. status = wait_on_bit_lock_action(&nfsi->flags,
  1921. NFS_INO_LAYOUTCOMMITTING,
  1922. nfs_wait_bit_killable,
  1923. TASK_KILLABLE);
  1924. if (status)
  1925. goto out;
  1926. }
  1927. status = -ENOMEM;
  1928. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  1929. data = kzalloc(sizeof(*data), GFP_NOFS);
  1930. if (!data)
  1931. goto clear_layoutcommitting;
  1932. status = 0;
  1933. spin_lock(&inode->i_lock);
  1934. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  1935. goto out_unlock;
  1936. INIT_LIST_HEAD(&data->lseg_list);
  1937. pnfs_list_write_lseg(inode, &data->lseg_list);
  1938. end_pos = nfsi->layout->plh_lwb;
  1939. nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
  1940. spin_unlock(&inode->i_lock);
  1941. data->args.inode = inode;
  1942. data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
  1943. nfs_fattr_init(&data->fattr);
  1944. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  1945. data->res.fattr = &data->fattr;
  1946. data->args.lastbytewritten = end_pos - 1;
  1947. data->res.server = NFS_SERVER(inode);
  1948. if (ld->prepare_layoutcommit) {
  1949. status = ld->prepare_layoutcommit(&data->args);
  1950. if (status) {
  1951. put_rpccred(data->cred);
  1952. spin_lock(&inode->i_lock);
  1953. set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
  1954. if (end_pos > nfsi->layout->plh_lwb)
  1955. nfsi->layout->plh_lwb = end_pos;
  1956. goto out_unlock;
  1957. }
  1958. }
  1959. status = nfs4_proc_layoutcommit(data, sync);
  1960. out:
  1961. if (status)
  1962. mark_inode_dirty_sync(inode);
  1963. dprintk("<-- %s status %d\n", __func__, status);
  1964. return status;
  1965. out_unlock:
  1966. spin_unlock(&inode->i_lock);
  1967. kfree(data);
  1968. clear_layoutcommitting:
  1969. pnfs_clear_layoutcommitting(inode);
  1970. goto out;
  1971. }
  1972. EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
  1973. int
  1974. pnfs_generic_sync(struct inode *inode, bool datasync)
  1975. {
  1976. return pnfs_layoutcommit_inode(inode, true);
  1977. }
  1978. EXPORT_SYMBOL_GPL(pnfs_generic_sync);
  1979. struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
  1980. {
  1981. struct nfs4_threshold *thp;
  1982. thp = kzalloc(sizeof(*thp), GFP_NOFS);
  1983. if (!thp) {
  1984. dprintk("%s mdsthreshold allocation failed\n", __func__);
  1985. return NULL;
  1986. }
  1987. return thp;
  1988. }
  1989. #if IS_ENABLED(CONFIG_NFS_V4_2)
  1990. int
  1991. pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
  1992. {
  1993. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  1994. struct nfs_server *server = NFS_SERVER(inode);
  1995. struct nfs_inode *nfsi = NFS_I(inode);
  1996. struct nfs42_layoutstat_data *data;
  1997. struct pnfs_layout_hdr *hdr;
  1998. int status = 0;
  1999. if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
  2000. goto out;
  2001. if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
  2002. goto out;
  2003. if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
  2004. goto out;
  2005. spin_lock(&inode->i_lock);
  2006. if (!NFS_I(inode)->layout) {
  2007. spin_unlock(&inode->i_lock);
  2008. goto out;
  2009. }
  2010. hdr = NFS_I(inode)->layout;
  2011. pnfs_get_layout_hdr(hdr);
  2012. spin_unlock(&inode->i_lock);
  2013. data = kzalloc(sizeof(*data), gfp_flags);
  2014. if (!data) {
  2015. status = -ENOMEM;
  2016. goto out_put;
  2017. }
  2018. data->args.fh = NFS_FH(inode);
  2019. data->args.inode = inode;
  2020. nfs4_stateid_copy(&data->args.stateid, &hdr->plh_stateid);
  2021. status = ld->prepare_layoutstats(&data->args);
  2022. if (status)
  2023. goto out_free;
  2024. status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
  2025. out:
  2026. dprintk("%s returns %d\n", __func__, status);
  2027. return status;
  2028. out_free:
  2029. kfree(data);
  2030. out_put:
  2031. pnfs_put_layout_hdr(hdr);
  2032. smp_mb__before_atomic();
  2033. clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
  2034. smp_mb__after_atomic();
  2035. goto out;
  2036. }
  2037. EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
  2038. #endif