pnfs.c 59 KB

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