pnfs.c 73 KB

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