pnfs.c 81 KB

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