pnfs.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050
  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. {
  680. struct pnfs_layout_hdr *lo, *next;
  681. struct inode *inode;
  682. list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
  683. if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags))
  684. continue;
  685. inode = igrab(lo->plh_inode);
  686. if (inode == NULL)
  687. continue;
  688. list_del_init(&lo->plh_layouts);
  689. if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
  690. continue;
  691. rcu_read_unlock();
  692. spin_unlock(&clp->cl_lock);
  693. iput(inode);
  694. spin_lock(&clp->cl_lock);
  695. rcu_read_lock();
  696. return -EAGAIN;
  697. }
  698. return 0;
  699. }
  700. static int
  701. pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
  702. bool is_bulk_recall)
  703. {
  704. struct pnfs_layout_hdr *lo;
  705. struct inode *inode;
  706. LIST_HEAD(lseg_list);
  707. int ret = 0;
  708. while (!list_empty(layout_list)) {
  709. lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
  710. plh_bulk_destroy);
  711. dprintk("%s freeing layout for inode %lu\n", __func__,
  712. lo->plh_inode->i_ino);
  713. inode = lo->plh_inode;
  714. pnfs_layoutcommit_inode(inode, false);
  715. spin_lock(&inode->i_lock);
  716. list_del_init(&lo->plh_bulk_destroy);
  717. if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
  718. if (is_bulk_recall)
  719. set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  720. ret = -EAGAIN;
  721. }
  722. spin_unlock(&inode->i_lock);
  723. pnfs_free_lseg_list(&lseg_list);
  724. /* Free all lsegs that are attached to commit buckets */
  725. nfs_commit_inode(inode, 0);
  726. pnfs_put_layout_hdr(lo);
  727. iput(inode);
  728. }
  729. return ret;
  730. }
  731. int
  732. pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
  733. struct nfs_fsid *fsid,
  734. bool is_recall)
  735. {
  736. struct nfs_server *server;
  737. LIST_HEAD(layout_list);
  738. spin_lock(&clp->cl_lock);
  739. rcu_read_lock();
  740. restart:
  741. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  742. if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
  743. continue;
  744. if (pnfs_layout_bulk_destroy_byserver_locked(clp,
  745. server,
  746. &layout_list) != 0)
  747. goto restart;
  748. }
  749. rcu_read_unlock();
  750. spin_unlock(&clp->cl_lock);
  751. if (list_empty(&layout_list))
  752. return 0;
  753. return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
  754. }
  755. int
  756. pnfs_destroy_layouts_byclid(struct nfs_client *clp,
  757. bool is_recall)
  758. {
  759. struct nfs_server *server;
  760. LIST_HEAD(layout_list);
  761. spin_lock(&clp->cl_lock);
  762. rcu_read_lock();
  763. restart:
  764. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  765. if (pnfs_layout_bulk_destroy_byserver_locked(clp,
  766. server,
  767. &layout_list) != 0)
  768. goto restart;
  769. }
  770. rcu_read_unlock();
  771. spin_unlock(&clp->cl_lock);
  772. if (list_empty(&layout_list))
  773. return 0;
  774. return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
  775. }
  776. /*
  777. * Called by the state manger to remove all layouts established under an
  778. * expired lease.
  779. */
  780. void
  781. pnfs_destroy_all_layouts(struct nfs_client *clp)
  782. {
  783. nfs4_deviceid_mark_client_invalid(clp);
  784. nfs4_deviceid_purge_client(clp);
  785. pnfs_destroy_layouts_byclid(clp, false);
  786. }
  787. /* update lo->plh_stateid with new if is more recent */
  788. void
  789. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  790. bool update_barrier)
  791. {
  792. u32 oldseq, newseq, new_barrier = 0;
  793. oldseq = be32_to_cpu(lo->plh_stateid.seqid);
  794. newseq = be32_to_cpu(new->seqid);
  795. if (!pnfs_layout_is_valid(lo)) {
  796. nfs4_stateid_copy(&lo->plh_stateid, new);
  797. lo->plh_barrier = newseq;
  798. pnfs_clear_layoutreturn_info(lo);
  799. clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
  800. return;
  801. }
  802. if (pnfs_seqid_is_newer(newseq, oldseq)) {
  803. nfs4_stateid_copy(&lo->plh_stateid, new);
  804. /*
  805. * Because of wraparound, we want to keep the barrier
  806. * "close" to the current seqids.
  807. */
  808. new_barrier = newseq - atomic_read(&lo->plh_outstanding);
  809. }
  810. if (update_barrier)
  811. new_barrier = be32_to_cpu(new->seqid);
  812. else if (new_barrier == 0)
  813. return;
  814. if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
  815. lo->plh_barrier = new_barrier;
  816. }
  817. static bool
  818. pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
  819. const nfs4_stateid *stateid)
  820. {
  821. u32 seqid = be32_to_cpu(stateid->seqid);
  822. return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
  823. }
  824. /* lget is set to 1 if called from inside send_layoutget call chain */
  825. static bool
  826. pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
  827. {
  828. return lo->plh_block_lgets ||
  829. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  830. }
  831. static struct nfs_server *
  832. pnfs_find_server(struct inode *inode, struct nfs_open_context *ctx)
  833. {
  834. struct nfs_server *server;
  835. if (inode) {
  836. server = NFS_SERVER(inode);
  837. } else {
  838. struct dentry *parent_dir = dget_parent(ctx->dentry);
  839. server = NFS_SERVER(parent_dir->d_inode);
  840. dput(parent_dir);
  841. }
  842. return server;
  843. }
  844. static void nfs4_free_pages(struct page **pages, size_t size)
  845. {
  846. int i;
  847. if (!pages)
  848. return;
  849. for (i = 0; i < size; i++) {
  850. if (!pages[i])
  851. break;
  852. __free_page(pages[i]);
  853. }
  854. kfree(pages);
  855. }
  856. static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
  857. {
  858. struct page **pages;
  859. int i;
  860. pages = kmalloc_array(size, sizeof(struct page *), gfp_flags);
  861. if (!pages) {
  862. dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
  863. return NULL;
  864. }
  865. for (i = 0; i < size; i++) {
  866. pages[i] = alloc_page(gfp_flags);
  867. if (!pages[i]) {
  868. dprintk("%s: failed to allocate page\n", __func__);
  869. nfs4_free_pages(pages, i);
  870. return NULL;
  871. }
  872. }
  873. return pages;
  874. }
  875. static struct nfs4_layoutget *
  876. pnfs_alloc_init_layoutget_args(struct inode *ino,
  877. struct nfs_open_context *ctx,
  878. const nfs4_stateid *stateid,
  879. const struct pnfs_layout_range *range,
  880. gfp_t gfp_flags)
  881. {
  882. struct nfs_server *server = pnfs_find_server(ino, ctx);
  883. size_t max_reply_sz = server->pnfs_curr_ld->max_layoutget_response;
  884. size_t max_pages = max_response_pages(server);
  885. struct nfs4_layoutget *lgp;
  886. dprintk("--> %s\n", __func__);
  887. lgp = kzalloc(sizeof(*lgp), gfp_flags);
  888. if (lgp == NULL)
  889. return NULL;
  890. if (max_reply_sz) {
  891. size_t npages = (max_reply_sz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  892. if (npages < max_pages)
  893. max_pages = npages;
  894. }
  895. lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
  896. if (!lgp->args.layout.pages) {
  897. kfree(lgp);
  898. return NULL;
  899. }
  900. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  901. lgp->res.layoutp = &lgp->args.layout;
  902. /* Don't confuse uninitialised result and success */
  903. lgp->res.status = -NFS4ERR_DELAY;
  904. lgp->args.minlength = PAGE_SIZE;
  905. if (lgp->args.minlength > range->length)
  906. lgp->args.minlength = range->length;
  907. if (ino) {
  908. loff_t i_size = i_size_read(ino);
  909. if (range->iomode == IOMODE_READ) {
  910. if (range->offset >= i_size)
  911. lgp->args.minlength = 0;
  912. else if (i_size - range->offset < lgp->args.minlength)
  913. lgp->args.minlength = i_size - range->offset;
  914. }
  915. }
  916. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  917. pnfs_copy_range(&lgp->args.range, range);
  918. lgp->args.type = server->pnfs_curr_ld->id;
  919. lgp->args.inode = ino;
  920. lgp->args.ctx = get_nfs_open_context(ctx);
  921. nfs4_stateid_copy(&lgp->args.stateid, stateid);
  922. lgp->gfp_flags = gfp_flags;
  923. lgp->cred = get_rpccred(ctx->cred);
  924. return lgp;
  925. }
  926. void pnfs_layoutget_free(struct nfs4_layoutget *lgp)
  927. {
  928. size_t max_pages = lgp->args.layout.pglen / PAGE_SIZE;
  929. nfs4_free_pages(lgp->args.layout.pages, max_pages);
  930. if (lgp->args.inode)
  931. pnfs_put_layout_hdr(NFS_I(lgp->args.inode)->layout);
  932. put_rpccred(lgp->cred);
  933. put_nfs_open_context(lgp->args.ctx);
  934. kfree(lgp);
  935. }
  936. static void pnfs_clear_layoutcommit(struct inode *inode,
  937. struct list_head *head)
  938. {
  939. struct nfs_inode *nfsi = NFS_I(inode);
  940. struct pnfs_layout_segment *lseg, *tmp;
  941. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  942. return;
  943. list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
  944. if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  945. continue;
  946. pnfs_lseg_dec_and_remove_zero(lseg, head);
  947. }
  948. }
  949. void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
  950. const nfs4_stateid *arg_stateid,
  951. const struct pnfs_layout_range *range,
  952. const nfs4_stateid *stateid)
  953. {
  954. struct inode *inode = lo->plh_inode;
  955. LIST_HEAD(freeme);
  956. spin_lock(&inode->i_lock);
  957. if (!pnfs_layout_is_valid(lo) || !arg_stateid ||
  958. !nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
  959. goto out_unlock;
  960. if (stateid) {
  961. u32 seq = be32_to_cpu(arg_stateid->seqid);
  962. pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq);
  963. pnfs_free_returned_lsegs(lo, &freeme, range, seq);
  964. pnfs_set_layout_stateid(lo, stateid, true);
  965. } else
  966. pnfs_mark_layout_stateid_invalid(lo, &freeme);
  967. out_unlock:
  968. pnfs_clear_layoutreturn_waitbit(lo);
  969. spin_unlock(&inode->i_lock);
  970. pnfs_free_lseg_list(&freeme);
  971. }
  972. static bool
  973. pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
  974. nfs4_stateid *stateid,
  975. enum pnfs_iomode *iomode)
  976. {
  977. /* Serialise LAYOUTGET/LAYOUTRETURN */
  978. if (atomic_read(&lo->plh_outstanding) != 0)
  979. return false;
  980. if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
  981. return false;
  982. set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
  983. pnfs_get_layout_hdr(lo);
  984. if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
  985. if (stateid != NULL) {
  986. nfs4_stateid_copy(stateid, &lo->plh_stateid);
  987. if (lo->plh_return_seq != 0)
  988. stateid->seqid = cpu_to_be32(lo->plh_return_seq);
  989. }
  990. if (iomode != NULL)
  991. *iomode = lo->plh_return_iomode;
  992. pnfs_clear_layoutreturn_info(lo);
  993. return true;
  994. }
  995. if (stateid != NULL)
  996. nfs4_stateid_copy(stateid, &lo->plh_stateid);
  997. if (iomode != NULL)
  998. *iomode = IOMODE_ANY;
  999. return true;
  1000. }
  1001. static void
  1002. pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
  1003. struct pnfs_layout_hdr *lo,
  1004. const nfs4_stateid *stateid,
  1005. enum pnfs_iomode iomode)
  1006. {
  1007. struct inode *inode = lo->plh_inode;
  1008. args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
  1009. args->inode = inode;
  1010. args->range.iomode = iomode;
  1011. args->range.offset = 0;
  1012. args->range.length = NFS4_MAX_UINT64;
  1013. args->layout = lo;
  1014. nfs4_stateid_copy(&args->stateid, stateid);
  1015. }
  1016. static int
  1017. pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
  1018. enum pnfs_iomode iomode, bool sync)
  1019. {
  1020. struct inode *ino = lo->plh_inode;
  1021. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  1022. struct nfs4_layoutreturn *lrp;
  1023. int status = 0;
  1024. lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
  1025. if (unlikely(lrp == NULL)) {
  1026. status = -ENOMEM;
  1027. spin_lock(&ino->i_lock);
  1028. pnfs_clear_layoutreturn_waitbit(lo);
  1029. spin_unlock(&ino->i_lock);
  1030. pnfs_put_layout_hdr(lo);
  1031. goto out;
  1032. }
  1033. pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
  1034. lrp->args.ld_private = &lrp->ld_private;
  1035. lrp->clp = NFS_SERVER(ino)->nfs_client;
  1036. lrp->cred = lo->plh_lc_cred;
  1037. if (ld->prepare_layoutreturn)
  1038. ld->prepare_layoutreturn(&lrp->args);
  1039. status = nfs4_proc_layoutreturn(lrp, sync);
  1040. out:
  1041. dprintk("<-- %s status: %d\n", __func__, status);
  1042. return status;
  1043. }
  1044. /* Return true if layoutreturn is needed */
  1045. static bool
  1046. pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
  1047. {
  1048. struct pnfs_layout_segment *s;
  1049. enum pnfs_iomode iomode;
  1050. u32 seq;
  1051. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  1052. return false;
  1053. seq = lo->plh_return_seq;
  1054. iomode = lo->plh_return_iomode;
  1055. /* Defer layoutreturn until all recalled lsegs are done */
  1056. list_for_each_entry(s, &lo->plh_segs, pls_list) {
  1057. if (seq && pnfs_seqid_is_newer(s->pls_seq, seq))
  1058. continue;
  1059. if (iomode != IOMODE_ANY && s->pls_range.iomode != iomode)
  1060. continue;
  1061. if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
  1062. return false;
  1063. }
  1064. return true;
  1065. }
  1066. static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
  1067. {
  1068. struct inode *inode= lo->plh_inode;
  1069. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  1070. return;
  1071. spin_lock(&inode->i_lock);
  1072. if (pnfs_layout_need_return(lo)) {
  1073. nfs4_stateid stateid;
  1074. enum pnfs_iomode iomode;
  1075. bool send;
  1076. send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
  1077. spin_unlock(&inode->i_lock);
  1078. if (send) {
  1079. /* Send an async layoutreturn so we dont deadlock */
  1080. pnfs_send_layoutreturn(lo, &stateid, iomode, false);
  1081. }
  1082. } else
  1083. spin_unlock(&inode->i_lock);
  1084. }
  1085. /*
  1086. * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
  1087. * when the layout segment list is empty.
  1088. *
  1089. * Note that a pnfs_layout_hdr can exist with an empty layout segment
  1090. * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
  1091. * deviceid is marked invalid.
  1092. */
  1093. int
  1094. _pnfs_return_layout(struct inode *ino)
  1095. {
  1096. struct pnfs_layout_hdr *lo = NULL;
  1097. struct nfs_inode *nfsi = NFS_I(ino);
  1098. LIST_HEAD(tmp_list);
  1099. nfs4_stateid stateid;
  1100. int status = 0;
  1101. bool send, valid_layout;
  1102. dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
  1103. spin_lock(&ino->i_lock);
  1104. lo = nfsi->layout;
  1105. if (!lo) {
  1106. spin_unlock(&ino->i_lock);
  1107. dprintk("NFS: %s no layout to return\n", __func__);
  1108. goto out;
  1109. }
  1110. /* Reference matched in nfs4_layoutreturn_release */
  1111. pnfs_get_layout_hdr(lo);
  1112. /* Is there an outstanding layoutreturn ? */
  1113. if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
  1114. spin_unlock(&ino->i_lock);
  1115. if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1116. TASK_UNINTERRUPTIBLE))
  1117. goto out_put_layout_hdr;
  1118. spin_lock(&ino->i_lock);
  1119. }
  1120. valid_layout = pnfs_layout_is_valid(lo);
  1121. pnfs_clear_layoutcommit(ino, &tmp_list);
  1122. pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0);
  1123. if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
  1124. struct pnfs_layout_range range = {
  1125. .iomode = IOMODE_ANY,
  1126. .offset = 0,
  1127. .length = NFS4_MAX_UINT64,
  1128. };
  1129. NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
  1130. }
  1131. /* Don't send a LAYOUTRETURN if list was initially empty */
  1132. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) ||
  1133. !valid_layout) {
  1134. spin_unlock(&ino->i_lock);
  1135. dprintk("NFS: %s no layout segments to return\n", __func__);
  1136. goto out_put_layout_hdr;
  1137. }
  1138. send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
  1139. spin_unlock(&ino->i_lock);
  1140. if (send)
  1141. status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
  1142. out_put_layout_hdr:
  1143. pnfs_free_lseg_list(&tmp_list);
  1144. pnfs_put_layout_hdr(lo);
  1145. out:
  1146. dprintk("<-- %s status: %d\n", __func__, status);
  1147. return status;
  1148. }
  1149. int
  1150. pnfs_commit_and_return_layout(struct inode *inode)
  1151. {
  1152. struct pnfs_layout_hdr *lo;
  1153. int ret;
  1154. spin_lock(&inode->i_lock);
  1155. lo = NFS_I(inode)->layout;
  1156. if (lo == NULL) {
  1157. spin_unlock(&inode->i_lock);
  1158. return 0;
  1159. }
  1160. pnfs_get_layout_hdr(lo);
  1161. /* Block new layoutgets and read/write to ds */
  1162. lo->plh_block_lgets++;
  1163. spin_unlock(&inode->i_lock);
  1164. filemap_fdatawait(inode->i_mapping);
  1165. ret = pnfs_layoutcommit_inode(inode, true);
  1166. if (ret == 0)
  1167. ret = _pnfs_return_layout(inode);
  1168. spin_lock(&inode->i_lock);
  1169. lo->plh_block_lgets--;
  1170. spin_unlock(&inode->i_lock);
  1171. pnfs_put_layout_hdr(lo);
  1172. return ret;
  1173. }
  1174. bool pnfs_roc(struct inode *ino,
  1175. struct nfs4_layoutreturn_args *args,
  1176. struct nfs4_layoutreturn_res *res,
  1177. const struct rpc_cred *cred)
  1178. {
  1179. struct nfs_inode *nfsi = NFS_I(ino);
  1180. struct nfs_open_context *ctx;
  1181. struct nfs4_state *state;
  1182. struct pnfs_layout_hdr *lo;
  1183. struct pnfs_layout_segment *lseg, *next;
  1184. nfs4_stateid stateid;
  1185. enum pnfs_iomode iomode = 0;
  1186. bool layoutreturn = false, roc = false;
  1187. bool skip_read = false;
  1188. if (!nfs_have_layout(ino))
  1189. return false;
  1190. retry:
  1191. rcu_read_lock();
  1192. spin_lock(&ino->i_lock);
  1193. lo = nfsi->layout;
  1194. if (!lo || !pnfs_layout_is_valid(lo) ||
  1195. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1196. lo = NULL;
  1197. goto out_noroc;
  1198. }
  1199. pnfs_get_layout_hdr(lo);
  1200. if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
  1201. spin_unlock(&ino->i_lock);
  1202. rcu_read_unlock();
  1203. wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1204. TASK_UNINTERRUPTIBLE);
  1205. pnfs_put_layout_hdr(lo);
  1206. goto retry;
  1207. }
  1208. /* no roc if we hold a delegation */
  1209. if (nfs4_check_delegation(ino, FMODE_READ)) {
  1210. if (nfs4_check_delegation(ino, FMODE_WRITE))
  1211. goto out_noroc;
  1212. skip_read = true;
  1213. }
  1214. list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
  1215. state = ctx->state;
  1216. if (state == NULL)
  1217. continue;
  1218. /* Don't return layout if there is open file state */
  1219. if (state->state & FMODE_WRITE)
  1220. goto out_noroc;
  1221. if (state->state & FMODE_READ)
  1222. skip_read = true;
  1223. }
  1224. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
  1225. if (skip_read && lseg->pls_range.iomode == IOMODE_READ)
  1226. continue;
  1227. /* If we are sending layoutreturn, invalidate all valid lsegs */
  1228. if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags))
  1229. continue;
  1230. /*
  1231. * Note: mark lseg for return so pnfs_layout_remove_lseg
  1232. * doesn't invalidate the layout for us.
  1233. */
  1234. set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
  1235. if (!mark_lseg_invalid(lseg, &lo->plh_return_segs))
  1236. continue;
  1237. pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
  1238. }
  1239. if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
  1240. goto out_noroc;
  1241. /* ROC in two conditions:
  1242. * 1. there are ROC lsegs
  1243. * 2. we don't send layoutreturn
  1244. */
  1245. /* lo ref dropped in pnfs_roc_release() */
  1246. layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
  1247. /* If the creds don't match, we can't compound the layoutreturn */
  1248. if (!layoutreturn || cred != lo->plh_lc_cred)
  1249. goto out_noroc;
  1250. roc = layoutreturn;
  1251. pnfs_init_layoutreturn_args(args, lo, &stateid, iomode);
  1252. res->lrs_present = 0;
  1253. layoutreturn = false;
  1254. out_noroc:
  1255. spin_unlock(&ino->i_lock);
  1256. rcu_read_unlock();
  1257. pnfs_layoutcommit_inode(ino, true);
  1258. if (roc) {
  1259. struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
  1260. if (ld->prepare_layoutreturn)
  1261. ld->prepare_layoutreturn(args);
  1262. pnfs_put_layout_hdr(lo);
  1263. return true;
  1264. }
  1265. if (layoutreturn)
  1266. pnfs_send_layoutreturn(lo, &stateid, iomode, true);
  1267. pnfs_put_layout_hdr(lo);
  1268. return false;
  1269. }
  1270. void pnfs_roc_release(struct nfs4_layoutreturn_args *args,
  1271. struct nfs4_layoutreturn_res *res,
  1272. int ret)
  1273. {
  1274. struct pnfs_layout_hdr *lo = args->layout;
  1275. const nfs4_stateid *arg_stateid = NULL;
  1276. const nfs4_stateid *res_stateid = NULL;
  1277. struct nfs4_xdr_opaque_data *ld_private = args->ld_private;
  1278. if (ret == 0) {
  1279. arg_stateid = &args->stateid;
  1280. if (res->lrs_present)
  1281. res_stateid = &res->stateid;
  1282. }
  1283. pnfs_layoutreturn_free_lsegs(lo, arg_stateid, &args->range,
  1284. res_stateid);
  1285. if (ld_private && ld_private->ops && ld_private->ops->free)
  1286. ld_private->ops->free(ld_private);
  1287. pnfs_put_layout_hdr(lo);
  1288. trace_nfs4_layoutreturn_on_close(args->inode, 0);
  1289. }
  1290. bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
  1291. {
  1292. struct nfs_inode *nfsi = NFS_I(ino);
  1293. struct pnfs_layout_hdr *lo;
  1294. bool sleep = false;
  1295. /* we might not have grabbed lo reference. so need to check under
  1296. * i_lock */
  1297. spin_lock(&ino->i_lock);
  1298. lo = nfsi->layout;
  1299. if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
  1300. rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
  1301. sleep = true;
  1302. }
  1303. spin_unlock(&ino->i_lock);
  1304. return sleep;
  1305. }
  1306. /*
  1307. * Compare two layout segments for sorting into layout cache.
  1308. * We want to preferentially return RW over RO layouts, so ensure those
  1309. * are seen first.
  1310. */
  1311. static s64
  1312. pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
  1313. const struct pnfs_layout_range *l2)
  1314. {
  1315. s64 d;
  1316. /* high offset > low offset */
  1317. d = l1->offset - l2->offset;
  1318. if (d)
  1319. return d;
  1320. /* short length > long length */
  1321. d = l2->length - l1->length;
  1322. if (d)
  1323. return d;
  1324. /* read > read/write */
  1325. return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
  1326. }
  1327. static bool
  1328. pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
  1329. const struct pnfs_layout_range *l2)
  1330. {
  1331. return pnfs_lseg_range_cmp(l1, l2) > 0;
  1332. }
  1333. static bool
  1334. pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
  1335. struct pnfs_layout_segment *old)
  1336. {
  1337. return false;
  1338. }
  1339. void
  1340. pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  1341. struct pnfs_layout_segment *lseg,
  1342. bool (*is_after)(const struct pnfs_layout_range *,
  1343. const struct pnfs_layout_range *),
  1344. bool (*do_merge)(struct pnfs_layout_segment *,
  1345. struct pnfs_layout_segment *),
  1346. struct list_head *free_me)
  1347. {
  1348. struct pnfs_layout_segment *lp, *tmp;
  1349. dprintk("%s:Begin\n", __func__);
  1350. list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
  1351. if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
  1352. continue;
  1353. if (do_merge(lseg, lp)) {
  1354. mark_lseg_invalid(lp, free_me);
  1355. continue;
  1356. }
  1357. if (is_after(&lseg->pls_range, &lp->pls_range))
  1358. continue;
  1359. list_add_tail(&lseg->pls_list, &lp->pls_list);
  1360. dprintk("%s: inserted lseg %p "
  1361. "iomode %d offset %llu length %llu before "
  1362. "lp %p iomode %d offset %llu length %llu\n",
  1363. __func__, lseg, lseg->pls_range.iomode,
  1364. lseg->pls_range.offset, lseg->pls_range.length,
  1365. lp, lp->pls_range.iomode, lp->pls_range.offset,
  1366. lp->pls_range.length);
  1367. goto out;
  1368. }
  1369. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  1370. dprintk("%s: inserted lseg %p "
  1371. "iomode %d offset %llu length %llu at tail\n",
  1372. __func__, lseg, lseg->pls_range.iomode,
  1373. lseg->pls_range.offset, lseg->pls_range.length);
  1374. out:
  1375. pnfs_get_layout_hdr(lo);
  1376. dprintk("%s:Return\n", __func__);
  1377. }
  1378. EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
  1379. static void
  1380. pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
  1381. struct pnfs_layout_segment *lseg,
  1382. struct list_head *free_me)
  1383. {
  1384. struct inode *inode = lo->plh_inode;
  1385. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  1386. if (ld->add_lseg != NULL)
  1387. ld->add_lseg(lo, lseg, free_me);
  1388. else
  1389. pnfs_generic_layout_insert_lseg(lo, lseg,
  1390. pnfs_lseg_range_is_after,
  1391. pnfs_lseg_no_merge,
  1392. free_me);
  1393. }
  1394. static struct pnfs_layout_hdr *
  1395. alloc_init_layout_hdr(struct inode *ino,
  1396. struct nfs_open_context *ctx,
  1397. gfp_t gfp_flags)
  1398. {
  1399. struct pnfs_layout_hdr *lo;
  1400. lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
  1401. if (!lo)
  1402. return NULL;
  1403. refcount_set(&lo->plh_refcount, 1);
  1404. INIT_LIST_HEAD(&lo->plh_layouts);
  1405. INIT_LIST_HEAD(&lo->plh_segs);
  1406. INIT_LIST_HEAD(&lo->plh_return_segs);
  1407. INIT_LIST_HEAD(&lo->plh_bulk_destroy);
  1408. lo->plh_inode = ino;
  1409. lo->plh_lc_cred = get_rpccred(ctx->cred);
  1410. lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
  1411. return lo;
  1412. }
  1413. static struct pnfs_layout_hdr *
  1414. pnfs_find_alloc_layout(struct inode *ino,
  1415. struct nfs_open_context *ctx,
  1416. gfp_t gfp_flags)
  1417. __releases(&ino->i_lock)
  1418. __acquires(&ino->i_lock)
  1419. {
  1420. struct nfs_inode *nfsi = NFS_I(ino);
  1421. struct pnfs_layout_hdr *new = NULL;
  1422. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  1423. if (nfsi->layout != NULL)
  1424. goto out_existing;
  1425. spin_unlock(&ino->i_lock);
  1426. new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
  1427. spin_lock(&ino->i_lock);
  1428. if (likely(nfsi->layout == NULL)) { /* Won the race? */
  1429. nfsi->layout = new;
  1430. return new;
  1431. } else if (new != NULL)
  1432. pnfs_free_layout_hdr(new);
  1433. out_existing:
  1434. pnfs_get_layout_hdr(nfsi->layout);
  1435. return nfsi->layout;
  1436. }
  1437. /*
  1438. * iomode matching rules:
  1439. * iomode lseg strict match
  1440. * iomode
  1441. * ----- ----- ------ -----
  1442. * ANY READ N/A true
  1443. * ANY RW N/A true
  1444. * RW READ N/A false
  1445. * RW RW N/A true
  1446. * READ READ N/A true
  1447. * READ RW true false
  1448. * READ RW false true
  1449. */
  1450. static bool
  1451. pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
  1452. const struct pnfs_layout_range *range,
  1453. bool strict_iomode)
  1454. {
  1455. struct pnfs_layout_range range1;
  1456. if ((range->iomode == IOMODE_RW &&
  1457. ls_range->iomode != IOMODE_RW) ||
  1458. (range->iomode != ls_range->iomode &&
  1459. strict_iomode) ||
  1460. !pnfs_lseg_range_intersecting(ls_range, range))
  1461. return false;
  1462. /* range1 covers only the first byte in the range */
  1463. range1 = *range;
  1464. range1.length = 1;
  1465. return pnfs_lseg_range_contained(ls_range, &range1);
  1466. }
  1467. /*
  1468. * lookup range in layout
  1469. */
  1470. static struct pnfs_layout_segment *
  1471. pnfs_find_lseg(struct pnfs_layout_hdr *lo,
  1472. struct pnfs_layout_range *range,
  1473. bool strict_iomode)
  1474. {
  1475. struct pnfs_layout_segment *lseg, *ret = NULL;
  1476. dprintk("%s:Begin\n", __func__);
  1477. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  1478. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  1479. pnfs_lseg_range_match(&lseg->pls_range, range,
  1480. strict_iomode)) {
  1481. ret = pnfs_get_lseg(lseg);
  1482. break;
  1483. }
  1484. }
  1485. dprintk("%s:Return lseg %p ref %d\n",
  1486. __func__, ret, ret ? refcount_read(&ret->pls_refcount) : 0);
  1487. return ret;
  1488. }
  1489. /*
  1490. * Use mdsthreshold hints set at each OPEN to determine if I/O should go
  1491. * to the MDS or over pNFS
  1492. *
  1493. * The nfs_inode read_io and write_io fields are cumulative counters reset
  1494. * when there are no layout segments. Note that in pnfs_update_layout iomode
  1495. * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
  1496. * WRITE request.
  1497. *
  1498. * A return of true means use MDS I/O.
  1499. *
  1500. * From rfc 5661:
  1501. * If a file's size is smaller than the file size threshold, data accesses
  1502. * SHOULD be sent to the metadata server. If an I/O request has a length that
  1503. * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
  1504. * server. If both file size and I/O size are provided, the client SHOULD
  1505. * reach or exceed both thresholds before sending its read or write
  1506. * requests to the data server.
  1507. */
  1508. static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
  1509. struct inode *ino, int iomode)
  1510. {
  1511. struct nfs4_threshold *t = ctx->mdsthreshold;
  1512. struct nfs_inode *nfsi = NFS_I(ino);
  1513. loff_t fsize = i_size_read(ino);
  1514. bool size = false, size_set = false, io = false, io_set = false, ret = false;
  1515. if (t == NULL)
  1516. return ret;
  1517. dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
  1518. __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
  1519. switch (iomode) {
  1520. case IOMODE_READ:
  1521. if (t->bm & THRESHOLD_RD) {
  1522. dprintk("%s fsize %llu\n", __func__, fsize);
  1523. size_set = true;
  1524. if (fsize < t->rd_sz)
  1525. size = true;
  1526. }
  1527. if (t->bm & THRESHOLD_RD_IO) {
  1528. dprintk("%s nfsi->read_io %llu\n", __func__,
  1529. nfsi->read_io);
  1530. io_set = true;
  1531. if (nfsi->read_io < t->rd_io_sz)
  1532. io = true;
  1533. }
  1534. break;
  1535. case IOMODE_RW:
  1536. if (t->bm & THRESHOLD_WR) {
  1537. dprintk("%s fsize %llu\n", __func__, fsize);
  1538. size_set = true;
  1539. if (fsize < t->wr_sz)
  1540. size = true;
  1541. }
  1542. if (t->bm & THRESHOLD_WR_IO) {
  1543. dprintk("%s nfsi->write_io %llu\n", __func__,
  1544. nfsi->write_io);
  1545. io_set = true;
  1546. if (nfsi->write_io < t->wr_io_sz)
  1547. io = true;
  1548. }
  1549. break;
  1550. }
  1551. if (size_set && io_set) {
  1552. if (size && io)
  1553. ret = true;
  1554. } else if (size || io)
  1555. ret = true;
  1556. dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
  1557. return ret;
  1558. }
  1559. static int pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
  1560. {
  1561. /*
  1562. * send layoutcommit as it can hold up layoutreturn due to lseg
  1563. * reference
  1564. */
  1565. pnfs_layoutcommit_inode(lo->plh_inode, false);
  1566. return wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
  1567. nfs_wait_bit_killable,
  1568. TASK_KILLABLE);
  1569. }
  1570. static void nfs_layoutget_begin(struct pnfs_layout_hdr *lo)
  1571. {
  1572. atomic_inc(&lo->plh_outstanding);
  1573. }
  1574. static void nfs_layoutget_end(struct pnfs_layout_hdr *lo)
  1575. {
  1576. if (atomic_dec_and_test(&lo->plh_outstanding))
  1577. wake_up_var(&lo->plh_outstanding);
  1578. }
  1579. static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
  1580. {
  1581. unsigned long *bitlock = &lo->plh_flags;
  1582. clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
  1583. smp_mb__after_atomic();
  1584. wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
  1585. }
  1586. static void _add_to_server_list(struct pnfs_layout_hdr *lo,
  1587. struct nfs_server *server)
  1588. {
  1589. if (list_empty(&lo->plh_layouts)) {
  1590. struct nfs_client *clp = server->nfs_client;
  1591. /* The lo must be on the clp list if there is any
  1592. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  1593. */
  1594. spin_lock(&clp->cl_lock);
  1595. if (list_empty(&lo->plh_layouts))
  1596. list_add_tail(&lo->plh_layouts, &server->layouts);
  1597. spin_unlock(&clp->cl_lock);
  1598. }
  1599. }
  1600. /*
  1601. * Layout segment is retreived from the server if not cached.
  1602. * The appropriate layout segment is referenced and returned to the caller.
  1603. */
  1604. struct pnfs_layout_segment *
  1605. pnfs_update_layout(struct inode *ino,
  1606. struct nfs_open_context *ctx,
  1607. loff_t pos,
  1608. u64 count,
  1609. enum pnfs_iomode iomode,
  1610. bool strict_iomode,
  1611. gfp_t gfp_flags)
  1612. {
  1613. struct pnfs_layout_range arg = {
  1614. .iomode = iomode,
  1615. .offset = pos,
  1616. .length = count,
  1617. };
  1618. unsigned pg_offset;
  1619. struct nfs_server *server = NFS_SERVER(ino);
  1620. struct nfs_client *clp = server->nfs_client;
  1621. struct pnfs_layout_hdr *lo = NULL;
  1622. struct pnfs_layout_segment *lseg = NULL;
  1623. struct nfs4_layoutget *lgp;
  1624. nfs4_stateid stateid;
  1625. long timeout = 0;
  1626. unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
  1627. bool first;
  1628. if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
  1629. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1630. PNFS_UPDATE_LAYOUT_NO_PNFS);
  1631. goto out;
  1632. }
  1633. if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
  1634. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1635. PNFS_UPDATE_LAYOUT_MDSTHRESH);
  1636. goto out;
  1637. }
  1638. lookup_again:
  1639. lseg = ERR_PTR(nfs4_client_recover_expired_lease(clp));
  1640. if (IS_ERR(lseg))
  1641. goto out;
  1642. first = false;
  1643. spin_lock(&ino->i_lock);
  1644. lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
  1645. if (lo == NULL) {
  1646. spin_unlock(&ino->i_lock);
  1647. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1648. PNFS_UPDATE_LAYOUT_NOMEM);
  1649. goto out;
  1650. }
  1651. /* Do we even need to bother with this? */
  1652. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  1653. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1654. PNFS_UPDATE_LAYOUT_BULK_RECALL);
  1655. dprintk("%s matches recall, use MDS\n", __func__);
  1656. goto out_unlock;
  1657. }
  1658. /* if LAYOUTGET already failed once we don't try again */
  1659. if (pnfs_layout_io_test_failed(lo, iomode)) {
  1660. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1661. PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
  1662. goto out_unlock;
  1663. }
  1664. /*
  1665. * If the layout segment list is empty, but there are outstanding
  1666. * layoutget calls, then they might be subject to a layoutrecall.
  1667. */
  1668. if (list_empty(&lo->plh_segs) &&
  1669. atomic_read(&lo->plh_outstanding) != 0) {
  1670. spin_unlock(&ino->i_lock);
  1671. lseg = ERR_PTR(wait_var_event_killable(&lo->plh_outstanding,
  1672. atomic_read(&lo->plh_outstanding)));
  1673. if (IS_ERR(lseg) || !list_empty(&lo->plh_segs))
  1674. goto out_put_layout_hdr;
  1675. pnfs_put_layout_hdr(lo);
  1676. goto lookup_again;
  1677. }
  1678. lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
  1679. if (lseg) {
  1680. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1681. PNFS_UPDATE_LAYOUT_FOUND_CACHED);
  1682. goto out_unlock;
  1683. }
  1684. if (!nfs4_valid_open_stateid(ctx->state)) {
  1685. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1686. PNFS_UPDATE_LAYOUT_INVALID_OPEN);
  1687. goto out_unlock;
  1688. }
  1689. /*
  1690. * Choose a stateid for the LAYOUTGET. If we don't have a layout
  1691. * stateid, or it has been invalidated, then we must use the open
  1692. * stateid.
  1693. */
  1694. if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
  1695. /*
  1696. * The first layoutget for the file. Need to serialize per
  1697. * RFC 5661 Errata 3208.
  1698. */
  1699. if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
  1700. &lo->plh_flags)) {
  1701. spin_unlock(&ino->i_lock);
  1702. lseg = ERR_PTR(wait_on_bit(&lo->plh_flags,
  1703. NFS_LAYOUT_FIRST_LAYOUTGET,
  1704. TASK_KILLABLE));
  1705. if (IS_ERR(lseg))
  1706. goto out_put_layout_hdr;
  1707. pnfs_put_layout_hdr(lo);
  1708. dprintk("%s retrying\n", __func__);
  1709. goto lookup_again;
  1710. }
  1711. first = true;
  1712. if (nfs4_select_rw_stateid(ctx->state,
  1713. iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ,
  1714. NULL, &stateid, NULL) != 0) {
  1715. trace_pnfs_update_layout(ino, pos, count,
  1716. iomode, lo, lseg,
  1717. PNFS_UPDATE_LAYOUT_INVALID_OPEN);
  1718. goto out_unlock;
  1719. }
  1720. } else {
  1721. nfs4_stateid_copy(&stateid, &lo->plh_stateid);
  1722. }
  1723. /*
  1724. * Because we free lsegs before sending LAYOUTRETURN, we need to wait
  1725. * for LAYOUTRETURN even if first is true.
  1726. */
  1727. if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
  1728. spin_unlock(&ino->i_lock);
  1729. dprintk("%s wait for layoutreturn\n", __func__);
  1730. lseg = ERR_PTR(pnfs_prepare_to_retry_layoutget(lo));
  1731. if (!IS_ERR(lseg)) {
  1732. if (first)
  1733. pnfs_clear_first_layoutget(lo);
  1734. pnfs_put_layout_hdr(lo);
  1735. dprintk("%s retrying\n", __func__);
  1736. trace_pnfs_update_layout(ino, pos, count, iomode, lo,
  1737. lseg, PNFS_UPDATE_LAYOUT_RETRY);
  1738. goto lookup_again;
  1739. }
  1740. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1741. PNFS_UPDATE_LAYOUT_RETURN);
  1742. goto out_put_layout_hdr;
  1743. }
  1744. if (pnfs_layoutgets_blocked(lo)) {
  1745. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1746. PNFS_UPDATE_LAYOUT_BLOCKED);
  1747. goto out_unlock;
  1748. }
  1749. nfs_layoutget_begin(lo);
  1750. spin_unlock(&ino->i_lock);
  1751. _add_to_server_list(lo, server);
  1752. pg_offset = arg.offset & ~PAGE_MASK;
  1753. if (pg_offset) {
  1754. arg.offset -= pg_offset;
  1755. arg.length += pg_offset;
  1756. }
  1757. if (arg.length != NFS4_MAX_UINT64)
  1758. arg.length = PAGE_ALIGN(arg.length);
  1759. lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &stateid, &arg, gfp_flags);
  1760. if (!lgp) {
  1761. trace_pnfs_update_layout(ino, pos, count, iomode, lo, NULL,
  1762. PNFS_UPDATE_LAYOUT_NOMEM);
  1763. nfs_layoutget_end(lo);
  1764. goto out_put_layout_hdr;
  1765. }
  1766. lseg = nfs4_proc_layoutget(lgp, &timeout);
  1767. trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
  1768. PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
  1769. nfs_layoutget_end(lo);
  1770. if (IS_ERR(lseg)) {
  1771. switch(PTR_ERR(lseg)) {
  1772. case -EBUSY:
  1773. if (time_after(jiffies, giveup))
  1774. lseg = NULL;
  1775. break;
  1776. case -ERECALLCONFLICT:
  1777. case -EAGAIN:
  1778. break;
  1779. default:
  1780. if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
  1781. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  1782. lseg = NULL;
  1783. }
  1784. goto out_put_layout_hdr;
  1785. }
  1786. if (lseg) {
  1787. if (first)
  1788. pnfs_clear_first_layoutget(lo);
  1789. trace_pnfs_update_layout(ino, pos, count,
  1790. iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
  1791. pnfs_put_layout_hdr(lo);
  1792. goto lookup_again;
  1793. }
  1794. } else {
  1795. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  1796. }
  1797. out_put_layout_hdr:
  1798. if (first)
  1799. pnfs_clear_first_layoutget(lo);
  1800. pnfs_put_layout_hdr(lo);
  1801. out:
  1802. dprintk("%s: inode %s/%llu pNFS layout segment %s for "
  1803. "(%s, offset: %llu, length: %llu)\n",
  1804. __func__, ino->i_sb->s_id,
  1805. (unsigned long long)NFS_FILEID(ino),
  1806. IS_ERR_OR_NULL(lseg) ? "not found" : "found",
  1807. iomode==IOMODE_RW ? "read/write" : "read-only",
  1808. (unsigned long long)pos,
  1809. (unsigned long long)count);
  1810. return lseg;
  1811. out_unlock:
  1812. spin_unlock(&ino->i_lock);
  1813. goto out_put_layout_hdr;
  1814. }
  1815. EXPORT_SYMBOL_GPL(pnfs_update_layout);
  1816. static bool
  1817. pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
  1818. {
  1819. switch (range->iomode) {
  1820. case IOMODE_READ:
  1821. case IOMODE_RW:
  1822. break;
  1823. default:
  1824. return false;
  1825. }
  1826. if (range->offset == NFS4_MAX_UINT64)
  1827. return false;
  1828. if (range->length == 0)
  1829. return false;
  1830. if (range->length != NFS4_MAX_UINT64 &&
  1831. range->length > NFS4_MAX_UINT64 - range->offset)
  1832. return false;
  1833. return true;
  1834. }
  1835. static struct pnfs_layout_hdr *
  1836. _pnfs_grab_empty_layout(struct inode *ino, struct nfs_open_context *ctx)
  1837. {
  1838. struct pnfs_layout_hdr *lo;
  1839. spin_lock(&ino->i_lock);
  1840. lo = pnfs_find_alloc_layout(ino, ctx, GFP_KERNEL);
  1841. if (!lo)
  1842. goto out_unlock;
  1843. if (!test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags))
  1844. goto out_unlock;
  1845. if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
  1846. goto out_unlock;
  1847. if (pnfs_layoutgets_blocked(lo))
  1848. goto out_unlock;
  1849. if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags))
  1850. goto out_unlock;
  1851. nfs_layoutget_begin(lo);
  1852. spin_unlock(&ino->i_lock);
  1853. _add_to_server_list(lo, NFS_SERVER(ino));
  1854. return lo;
  1855. out_unlock:
  1856. spin_unlock(&ino->i_lock);
  1857. pnfs_put_layout_hdr(lo);
  1858. return NULL;
  1859. }
  1860. extern const nfs4_stateid current_stateid;
  1861. static void _lgopen_prepare_attached(struct nfs4_opendata *data,
  1862. struct nfs_open_context *ctx)
  1863. {
  1864. struct inode *ino = data->dentry->d_inode;
  1865. struct pnfs_layout_range rng = {
  1866. .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
  1867. IOMODE_RW: IOMODE_READ,
  1868. .offset = 0,
  1869. .length = NFS4_MAX_UINT64,
  1870. };
  1871. struct nfs4_layoutget *lgp;
  1872. struct pnfs_layout_hdr *lo;
  1873. /* Heuristic: don't send layoutget if we have cached data */
  1874. if (rng.iomode == IOMODE_READ &&
  1875. (i_size_read(ino) == 0 || ino->i_mapping->nrpages != 0))
  1876. return;
  1877. lo = _pnfs_grab_empty_layout(ino, ctx);
  1878. if (!lo)
  1879. return;
  1880. lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &current_stateid,
  1881. &rng, GFP_KERNEL);
  1882. if (!lgp) {
  1883. pnfs_clear_first_layoutget(lo);
  1884. pnfs_put_layout_hdr(lo);
  1885. return;
  1886. }
  1887. data->lgp = lgp;
  1888. data->o_arg.lg_args = &lgp->args;
  1889. data->o_res.lg_res = &lgp->res;
  1890. }
  1891. static void _lgopen_prepare_floating(struct nfs4_opendata *data,
  1892. struct nfs_open_context *ctx)
  1893. {
  1894. struct pnfs_layout_range rng = {
  1895. .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
  1896. IOMODE_RW: IOMODE_READ,
  1897. .offset = 0,
  1898. .length = NFS4_MAX_UINT64,
  1899. };
  1900. struct nfs4_layoutget *lgp;
  1901. lgp = pnfs_alloc_init_layoutget_args(NULL, ctx, &current_stateid,
  1902. &rng, GFP_KERNEL);
  1903. if (!lgp)
  1904. return;
  1905. data->lgp = lgp;
  1906. data->o_arg.lg_args = &lgp->args;
  1907. data->o_res.lg_res = &lgp->res;
  1908. }
  1909. void pnfs_lgopen_prepare(struct nfs4_opendata *data,
  1910. struct nfs_open_context *ctx)
  1911. {
  1912. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  1913. if (!(pnfs_enabled_sb(server) &&
  1914. server->pnfs_curr_ld->flags & PNFS_LAYOUTGET_ON_OPEN))
  1915. return;
  1916. /* Could check on max_ops, but currently hardcoded high enough */
  1917. if (!nfs_server_capable(data->dir->d_inode, NFS_CAP_LGOPEN))
  1918. return;
  1919. if (data->state)
  1920. _lgopen_prepare_attached(data, ctx);
  1921. else
  1922. _lgopen_prepare_floating(data, ctx);
  1923. }
  1924. void pnfs_parse_lgopen(struct inode *ino, struct nfs4_layoutget *lgp,
  1925. struct nfs_open_context *ctx)
  1926. {
  1927. struct pnfs_layout_hdr *lo;
  1928. struct pnfs_layout_segment *lseg;
  1929. struct nfs_server *srv = NFS_SERVER(ino);
  1930. u32 iomode;
  1931. if (!lgp)
  1932. return;
  1933. dprintk("%s: entered with status %i\n", __func__, lgp->res.status);
  1934. if (lgp->res.status) {
  1935. switch (lgp->res.status) {
  1936. default:
  1937. break;
  1938. /*
  1939. * Halt lgopen attempts if the server doesn't recognise
  1940. * the "current stateid" value, the layout type, or the
  1941. * layoutget operation as being valid.
  1942. * Also if it complains about too many ops in the compound
  1943. * or of the request/reply being too big.
  1944. */
  1945. case -NFS4ERR_BAD_STATEID:
  1946. case -NFS4ERR_NOTSUPP:
  1947. case -NFS4ERR_REP_TOO_BIG:
  1948. case -NFS4ERR_REP_TOO_BIG_TO_CACHE:
  1949. case -NFS4ERR_REQ_TOO_BIG:
  1950. case -NFS4ERR_TOO_MANY_OPS:
  1951. case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
  1952. srv->caps &= ~NFS_CAP_LGOPEN;
  1953. }
  1954. return;
  1955. }
  1956. if (!lgp->args.inode) {
  1957. lo = _pnfs_grab_empty_layout(ino, ctx);
  1958. if (!lo)
  1959. return;
  1960. lgp->args.inode = ino;
  1961. } else
  1962. lo = NFS_I(lgp->args.inode)->layout;
  1963. lseg = pnfs_layout_process(lgp);
  1964. if (!IS_ERR(lseg)) {
  1965. iomode = lgp->args.range.iomode;
  1966. pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
  1967. pnfs_put_lseg(lseg);
  1968. }
  1969. }
  1970. void nfs4_lgopen_release(struct nfs4_layoutget *lgp)
  1971. {
  1972. if (lgp != NULL) {
  1973. struct inode *inode = lgp->args.inode;
  1974. if (inode) {
  1975. struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
  1976. pnfs_clear_first_layoutget(lo);
  1977. nfs_layoutget_end(lo);
  1978. }
  1979. pnfs_layoutget_free(lgp);
  1980. }
  1981. }
  1982. struct pnfs_layout_segment *
  1983. pnfs_layout_process(struct nfs4_layoutget *lgp)
  1984. {
  1985. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  1986. struct nfs4_layoutget_res *res = &lgp->res;
  1987. struct pnfs_layout_segment *lseg;
  1988. struct inode *ino = lo->plh_inode;
  1989. LIST_HEAD(free_me);
  1990. if (!pnfs_sanity_check_layout_range(&res->range))
  1991. return ERR_PTR(-EINVAL);
  1992. /* Inject layout blob into I/O device driver */
  1993. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
  1994. if (IS_ERR_OR_NULL(lseg)) {
  1995. if (!lseg)
  1996. lseg = ERR_PTR(-ENOMEM);
  1997. dprintk("%s: Could not allocate layout: error %ld\n",
  1998. __func__, PTR_ERR(lseg));
  1999. return lseg;
  2000. }
  2001. pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
  2002. spin_lock(&ino->i_lock);
  2003. if (pnfs_layoutgets_blocked(lo)) {
  2004. dprintk("%s forget reply due to state\n", __func__);
  2005. goto out_forget;
  2006. }
  2007. if (!pnfs_layout_is_valid(lo)) {
  2008. /* We have a completely new layout */
  2009. pnfs_set_layout_stateid(lo, &res->stateid, true);
  2010. } else if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
  2011. /* existing state ID, make sure the sequence number matches. */
  2012. if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
  2013. dprintk("%s forget reply due to sequence\n", __func__);
  2014. goto out_forget;
  2015. }
  2016. pnfs_set_layout_stateid(lo, &res->stateid, false);
  2017. } else {
  2018. /*
  2019. * We got an entirely new state ID. Mark all segments for the
  2020. * inode invalid, and retry the layoutget
  2021. */
  2022. pnfs_mark_layout_stateid_invalid(lo, &free_me);
  2023. goto out_forget;
  2024. }
  2025. pnfs_get_lseg(lseg);
  2026. pnfs_layout_insert_lseg(lo, lseg, &free_me);
  2027. if (res->return_on_close)
  2028. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  2029. spin_unlock(&ino->i_lock);
  2030. pnfs_free_lseg_list(&free_me);
  2031. return lseg;
  2032. out_forget:
  2033. spin_unlock(&ino->i_lock);
  2034. lseg->pls_layout = lo;
  2035. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  2036. return ERR_PTR(-EAGAIN);
  2037. }
  2038. static int
  2039. mark_lseg_invalid_or_return(struct pnfs_layout_segment *lseg,
  2040. struct list_head *tmp_list)
  2041. {
  2042. if (!mark_lseg_invalid(lseg, tmp_list))
  2043. return 0;
  2044. pnfs_cache_lseg_for_layoutreturn(lseg->pls_layout, lseg);
  2045. return 1;
  2046. }
  2047. /**
  2048. * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
  2049. * @lo: pointer to layout header
  2050. * @tmp_list: list header to be used with pnfs_free_lseg_list()
  2051. * @return_range: describe layout segment ranges to be returned
  2052. * @seq: stateid seqid to match
  2053. *
  2054. * This function is mainly intended for use by layoutrecall. It attempts
  2055. * to free the layout segment immediately, or else to mark it for return
  2056. * as soon as its reference count drops to zero.
  2057. *
  2058. * Returns
  2059. * - 0: a layoutreturn needs to be scheduled.
  2060. * - EBUSY: there are layout segment that are still in use.
  2061. * - ENOENT: there are no layout segments that need to be returned.
  2062. */
  2063. int
  2064. pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
  2065. struct list_head *tmp_list,
  2066. const struct pnfs_layout_range *return_range,
  2067. u32 seq)
  2068. {
  2069. struct pnfs_layout_segment *lseg, *next;
  2070. int remaining = 0;
  2071. dprintk("%s:Begin lo %p\n", __func__, lo);
  2072. assert_spin_locked(&lo->plh_inode->i_lock);
  2073. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  2074. if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
  2075. dprintk("%s: marking lseg %p iomode %d "
  2076. "offset %llu length %llu\n", __func__,
  2077. lseg, lseg->pls_range.iomode,
  2078. lseg->pls_range.offset,
  2079. lseg->pls_range.length);
  2080. if (mark_lseg_invalid_or_return(lseg, tmp_list))
  2081. continue;
  2082. remaining++;
  2083. set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
  2084. }
  2085. if (remaining) {
  2086. pnfs_set_plh_return_info(lo, return_range->iomode, seq);
  2087. return -EBUSY;
  2088. }
  2089. if (!list_empty(&lo->plh_return_segs)) {
  2090. pnfs_set_plh_return_info(lo, return_range->iomode, seq);
  2091. return 0;
  2092. }
  2093. return -ENOENT;
  2094. }
  2095. void pnfs_error_mark_layout_for_return(struct inode *inode,
  2096. struct pnfs_layout_segment *lseg)
  2097. {
  2098. struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
  2099. struct pnfs_layout_range range = {
  2100. .iomode = lseg->pls_range.iomode,
  2101. .offset = 0,
  2102. .length = NFS4_MAX_UINT64,
  2103. };
  2104. bool return_now = false;
  2105. spin_lock(&inode->i_lock);
  2106. if (!pnfs_layout_is_valid(lo)) {
  2107. spin_unlock(&inode->i_lock);
  2108. return;
  2109. }
  2110. pnfs_set_plh_return_info(lo, range.iomode, 0);
  2111. /*
  2112. * mark all matching lsegs so that we are sure to have no live
  2113. * segments at hand when sending layoutreturn. See pnfs_put_lseg()
  2114. * for how it works.
  2115. */
  2116. if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, &range, 0) != -EBUSY) {
  2117. nfs4_stateid stateid;
  2118. enum pnfs_iomode iomode;
  2119. return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
  2120. spin_unlock(&inode->i_lock);
  2121. if (return_now)
  2122. pnfs_send_layoutreturn(lo, &stateid, iomode, false);
  2123. } else {
  2124. spin_unlock(&inode->i_lock);
  2125. nfs_commit_inode(inode, 0);
  2126. }
  2127. }
  2128. EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
  2129. void
  2130. pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio)
  2131. {
  2132. if (pgio->pg_lseg == NULL ||
  2133. test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags))
  2134. return;
  2135. pnfs_put_lseg(pgio->pg_lseg);
  2136. pgio->pg_lseg = NULL;
  2137. }
  2138. EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
  2139. /*
  2140. * Check for any intersection between the request and the pgio->pg_lseg,
  2141. * and if none, put this pgio->pg_lseg away.
  2142. */
  2143. static void
  2144. pnfs_generic_pg_check_range(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  2145. {
  2146. if (pgio->pg_lseg && !pnfs_lseg_request_intersecting(pgio->pg_lseg, req)) {
  2147. pnfs_put_lseg(pgio->pg_lseg);
  2148. pgio->pg_lseg = NULL;
  2149. }
  2150. }
  2151. void
  2152. pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  2153. {
  2154. u64 rd_size = req->wb_bytes;
  2155. pnfs_generic_pg_check_layout(pgio);
  2156. pnfs_generic_pg_check_range(pgio, req);
  2157. if (pgio->pg_lseg == NULL) {
  2158. if (pgio->pg_dreq == NULL)
  2159. rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
  2160. else
  2161. rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
  2162. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  2163. req->wb_context,
  2164. req_offset(req),
  2165. rd_size,
  2166. IOMODE_READ,
  2167. false,
  2168. GFP_KERNEL);
  2169. if (IS_ERR(pgio->pg_lseg)) {
  2170. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  2171. pgio->pg_lseg = NULL;
  2172. return;
  2173. }
  2174. }
  2175. /* If no lseg, fall back to read through mds */
  2176. if (pgio->pg_lseg == NULL)
  2177. nfs_pageio_reset_read_mds(pgio);
  2178. }
  2179. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
  2180. void
  2181. pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
  2182. struct nfs_page *req, u64 wb_size)
  2183. {
  2184. pnfs_generic_pg_check_layout(pgio);
  2185. pnfs_generic_pg_check_range(pgio, req);
  2186. if (pgio->pg_lseg == NULL) {
  2187. pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
  2188. req->wb_context,
  2189. req_offset(req),
  2190. wb_size,
  2191. IOMODE_RW,
  2192. false,
  2193. GFP_NOFS);
  2194. if (IS_ERR(pgio->pg_lseg)) {
  2195. pgio->pg_error = PTR_ERR(pgio->pg_lseg);
  2196. pgio->pg_lseg = NULL;
  2197. return;
  2198. }
  2199. }
  2200. /* If no lseg, fall back to write through mds */
  2201. if (pgio->pg_lseg == NULL)
  2202. nfs_pageio_reset_write_mds(pgio);
  2203. }
  2204. EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
  2205. void
  2206. pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
  2207. {
  2208. if (desc->pg_lseg) {
  2209. pnfs_put_lseg(desc->pg_lseg);
  2210. desc->pg_lseg = NULL;
  2211. }
  2212. }
  2213. EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
  2214. /*
  2215. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  2216. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  2217. */
  2218. size_t
  2219. pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
  2220. struct nfs_page *prev, struct nfs_page *req)
  2221. {
  2222. unsigned int size;
  2223. u64 seg_end, req_start, seg_left;
  2224. size = nfs_generic_pg_test(pgio, prev, req);
  2225. if (!size)
  2226. return 0;
  2227. /*
  2228. * 'size' contains the number of bytes left in the current page (up
  2229. * to the original size asked for in @req->wb_bytes).
  2230. *
  2231. * Calculate how many bytes are left in the layout segment
  2232. * and if there are less bytes than 'size', return that instead.
  2233. *
  2234. * Please also note that 'end_offset' is actually the offset of the
  2235. * first byte that lies outside the pnfs_layout_range. FIXME?
  2236. *
  2237. */
  2238. if (pgio->pg_lseg) {
  2239. seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
  2240. pgio->pg_lseg->pls_range.length);
  2241. req_start = req_offset(req);
  2242. /* start of request is past the last byte of this segment */
  2243. if (req_start >= seg_end)
  2244. return 0;
  2245. /* adjust 'size' iff there are fewer bytes left in the
  2246. * segment than what nfs_generic_pg_test returned */
  2247. seg_left = seg_end - req_start;
  2248. if (seg_left < size)
  2249. size = (unsigned int)seg_left;
  2250. }
  2251. return size;
  2252. }
  2253. EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
  2254. int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
  2255. {
  2256. struct nfs_pageio_descriptor pgio;
  2257. /* Resend all requests through the MDS */
  2258. nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
  2259. hdr->completion_ops);
  2260. set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
  2261. return nfs_pageio_resend(&pgio, hdr);
  2262. }
  2263. EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
  2264. static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
  2265. {
  2266. dprintk("pnfs write error = %d\n", hdr->pnfs_error);
  2267. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  2268. PNFS_LAYOUTRET_ON_ERROR) {
  2269. pnfs_return_layout(hdr->inode);
  2270. }
  2271. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  2272. hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
  2273. }
  2274. /*
  2275. * Called by non rpc-based layout drivers
  2276. */
  2277. void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
  2278. {
  2279. if (likely(!hdr->pnfs_error)) {
  2280. pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
  2281. hdr->mds_offset + hdr->res.count);
  2282. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  2283. }
  2284. trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
  2285. if (unlikely(hdr->pnfs_error))
  2286. pnfs_ld_handle_write_error(hdr);
  2287. hdr->mds_ops->rpc_release(hdr);
  2288. }
  2289. EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
  2290. static void
  2291. pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
  2292. struct nfs_pgio_header *hdr)
  2293. {
  2294. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2295. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2296. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  2297. nfs_pageio_reset_write_mds(desc);
  2298. mirror->pg_recoalesce = 1;
  2299. }
  2300. hdr->completion_ops->completion(hdr);
  2301. }
  2302. static enum pnfs_try_status
  2303. pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
  2304. const struct rpc_call_ops *call_ops,
  2305. struct pnfs_layout_segment *lseg,
  2306. int how)
  2307. {
  2308. struct inode *inode = hdr->inode;
  2309. enum pnfs_try_status trypnfs;
  2310. struct nfs_server *nfss = NFS_SERVER(inode);
  2311. hdr->mds_ops = call_ops;
  2312. dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
  2313. inode->i_ino, hdr->args.count, hdr->args.offset, how);
  2314. trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
  2315. if (trypnfs != PNFS_NOT_ATTEMPTED)
  2316. nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
  2317. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  2318. return trypnfs;
  2319. }
  2320. static void
  2321. pnfs_do_write(struct nfs_pageio_descriptor *desc,
  2322. struct nfs_pgio_header *hdr, int how)
  2323. {
  2324. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  2325. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  2326. enum pnfs_try_status trypnfs;
  2327. trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
  2328. switch (trypnfs) {
  2329. case PNFS_NOT_ATTEMPTED:
  2330. pnfs_write_through_mds(desc, hdr);
  2331. case PNFS_ATTEMPTED:
  2332. break;
  2333. case PNFS_TRY_AGAIN:
  2334. /* cleanup hdr and prepare to redo pnfs */
  2335. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2336. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2337. list_splice_init(&hdr->pages, &mirror->pg_list);
  2338. mirror->pg_recoalesce = 1;
  2339. }
  2340. hdr->mds_ops->rpc_release(hdr);
  2341. }
  2342. }
  2343. static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
  2344. {
  2345. pnfs_put_lseg(hdr->lseg);
  2346. nfs_pgio_header_free(hdr);
  2347. }
  2348. int
  2349. pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  2350. {
  2351. struct nfs_pgio_header *hdr;
  2352. int ret;
  2353. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  2354. if (!hdr) {
  2355. desc->pg_error = -ENOMEM;
  2356. return desc->pg_error;
  2357. }
  2358. nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
  2359. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  2360. ret = nfs_generic_pgio(desc, hdr);
  2361. if (!ret)
  2362. pnfs_do_write(desc, hdr, desc->pg_ioflags);
  2363. return ret;
  2364. }
  2365. EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
  2366. int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
  2367. {
  2368. struct nfs_pageio_descriptor pgio;
  2369. /* Resend all requests through the MDS */
  2370. nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
  2371. return nfs_pageio_resend(&pgio, hdr);
  2372. }
  2373. EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
  2374. static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
  2375. {
  2376. dprintk("pnfs read error = %d\n", hdr->pnfs_error);
  2377. if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
  2378. PNFS_LAYOUTRET_ON_ERROR) {
  2379. pnfs_return_layout(hdr->inode);
  2380. }
  2381. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
  2382. hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
  2383. }
  2384. /*
  2385. * Called by non rpc-based layout drivers
  2386. */
  2387. void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
  2388. {
  2389. if (likely(!hdr->pnfs_error))
  2390. hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
  2391. trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
  2392. if (unlikely(hdr->pnfs_error))
  2393. pnfs_ld_handle_read_error(hdr);
  2394. hdr->mds_ops->rpc_release(hdr);
  2395. }
  2396. EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
  2397. static void
  2398. pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
  2399. struct nfs_pgio_header *hdr)
  2400. {
  2401. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2402. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2403. list_splice_tail_init(&hdr->pages, &mirror->pg_list);
  2404. nfs_pageio_reset_read_mds(desc);
  2405. mirror->pg_recoalesce = 1;
  2406. }
  2407. hdr->completion_ops->completion(hdr);
  2408. }
  2409. /*
  2410. * Call the appropriate parallel I/O subsystem read function.
  2411. */
  2412. static enum pnfs_try_status
  2413. pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
  2414. const struct rpc_call_ops *call_ops,
  2415. struct pnfs_layout_segment *lseg)
  2416. {
  2417. struct inode *inode = hdr->inode;
  2418. struct nfs_server *nfss = NFS_SERVER(inode);
  2419. enum pnfs_try_status trypnfs;
  2420. hdr->mds_ops = call_ops;
  2421. dprintk("%s: Reading ino:%lu %u@%llu\n",
  2422. __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
  2423. trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
  2424. if (trypnfs != PNFS_NOT_ATTEMPTED)
  2425. nfs_inc_stats(inode, NFSIOS_PNFS_READ);
  2426. dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
  2427. return trypnfs;
  2428. }
  2429. /* Resend all requests through pnfs. */
  2430. void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
  2431. {
  2432. struct nfs_pageio_descriptor pgio;
  2433. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2434. /* Prevent deadlocks with layoutreturn! */
  2435. pnfs_put_lseg(hdr->lseg);
  2436. hdr->lseg = NULL;
  2437. nfs_pageio_init_read(&pgio, hdr->inode, false,
  2438. hdr->completion_ops);
  2439. hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
  2440. }
  2441. }
  2442. EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
  2443. static void
  2444. pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
  2445. {
  2446. const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
  2447. struct pnfs_layout_segment *lseg = desc->pg_lseg;
  2448. enum pnfs_try_status trypnfs;
  2449. trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
  2450. switch (trypnfs) {
  2451. case PNFS_NOT_ATTEMPTED:
  2452. pnfs_read_through_mds(desc, hdr);
  2453. case PNFS_ATTEMPTED:
  2454. break;
  2455. case PNFS_TRY_AGAIN:
  2456. /* cleanup hdr and prepare to redo pnfs */
  2457. if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  2458. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  2459. list_splice_init(&hdr->pages, &mirror->pg_list);
  2460. mirror->pg_recoalesce = 1;
  2461. }
  2462. hdr->mds_ops->rpc_release(hdr);
  2463. }
  2464. }
  2465. static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
  2466. {
  2467. pnfs_put_lseg(hdr->lseg);
  2468. nfs_pgio_header_free(hdr);
  2469. }
  2470. int
  2471. pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  2472. {
  2473. struct nfs_pgio_header *hdr;
  2474. int ret;
  2475. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  2476. if (!hdr) {
  2477. desc->pg_error = -ENOMEM;
  2478. return desc->pg_error;
  2479. }
  2480. nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
  2481. hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
  2482. ret = nfs_generic_pgio(desc, hdr);
  2483. if (!ret)
  2484. pnfs_do_read(desc, hdr);
  2485. return ret;
  2486. }
  2487. EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
  2488. static void pnfs_clear_layoutcommitting(struct inode *inode)
  2489. {
  2490. unsigned long *bitlock = &NFS_I(inode)->flags;
  2491. clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
  2492. smp_mb__after_atomic();
  2493. wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
  2494. }
  2495. /*
  2496. * There can be multiple RW segments.
  2497. */
  2498. static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
  2499. {
  2500. struct pnfs_layout_segment *lseg;
  2501. list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
  2502. if (lseg->pls_range.iomode == IOMODE_RW &&
  2503. test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
  2504. list_add(&lseg->pls_lc_list, listp);
  2505. }
  2506. }
  2507. static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
  2508. {
  2509. struct pnfs_layout_segment *lseg, *tmp;
  2510. /* Matched by references in pnfs_set_layoutcommit */
  2511. list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
  2512. list_del_init(&lseg->pls_lc_list);
  2513. pnfs_put_lseg(lseg);
  2514. }
  2515. pnfs_clear_layoutcommitting(inode);
  2516. }
  2517. void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
  2518. {
  2519. pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
  2520. }
  2521. EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
  2522. void
  2523. pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
  2524. loff_t end_pos)
  2525. {
  2526. struct nfs_inode *nfsi = NFS_I(inode);
  2527. bool mark_as_dirty = false;
  2528. spin_lock(&inode->i_lock);
  2529. if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
  2530. nfsi->layout->plh_lwb = end_pos;
  2531. mark_as_dirty = true;
  2532. dprintk("%s: Set layoutcommit for inode %lu ",
  2533. __func__, inode->i_ino);
  2534. } else if (end_pos > nfsi->layout->plh_lwb)
  2535. nfsi->layout->plh_lwb = end_pos;
  2536. if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
  2537. /* references matched in nfs4_layoutcommit_release */
  2538. pnfs_get_lseg(lseg);
  2539. }
  2540. spin_unlock(&inode->i_lock);
  2541. dprintk("%s: lseg %p end_pos %llu\n",
  2542. __func__, lseg, nfsi->layout->plh_lwb);
  2543. /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
  2544. * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
  2545. if (mark_as_dirty)
  2546. mark_inode_dirty_sync(inode);
  2547. }
  2548. EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
  2549. void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
  2550. {
  2551. struct nfs_server *nfss = NFS_SERVER(data->args.inode);
  2552. if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
  2553. nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
  2554. pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
  2555. }
  2556. /*
  2557. * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
  2558. * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
  2559. * data to disk to allow the server to recover the data if it crashes.
  2560. * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
  2561. * is off, and a COMMIT is sent to a data server, or
  2562. * if WRITEs to a data server return NFS_DATA_SYNC.
  2563. */
  2564. int
  2565. pnfs_layoutcommit_inode(struct inode *inode, bool sync)
  2566. {
  2567. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  2568. struct nfs4_layoutcommit_data *data;
  2569. struct nfs_inode *nfsi = NFS_I(inode);
  2570. loff_t end_pos;
  2571. int status;
  2572. if (!pnfs_layoutcommit_outstanding(inode))
  2573. return 0;
  2574. dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
  2575. status = -EAGAIN;
  2576. if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
  2577. if (!sync)
  2578. goto out;
  2579. status = wait_on_bit_lock_action(&nfsi->flags,
  2580. NFS_INO_LAYOUTCOMMITTING,
  2581. nfs_wait_bit_killable,
  2582. TASK_KILLABLE);
  2583. if (status)
  2584. goto out;
  2585. }
  2586. status = -ENOMEM;
  2587. /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
  2588. data = kzalloc(sizeof(*data), GFP_NOFS);
  2589. if (!data)
  2590. goto clear_layoutcommitting;
  2591. status = 0;
  2592. spin_lock(&inode->i_lock);
  2593. if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
  2594. goto out_unlock;
  2595. INIT_LIST_HEAD(&data->lseg_list);
  2596. pnfs_list_write_lseg(inode, &data->lseg_list);
  2597. end_pos = nfsi->layout->plh_lwb;
  2598. nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
  2599. spin_unlock(&inode->i_lock);
  2600. data->args.inode = inode;
  2601. data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
  2602. nfs_fattr_init(&data->fattr);
  2603. data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  2604. data->res.fattr = &data->fattr;
  2605. if (end_pos != 0)
  2606. data->args.lastbytewritten = end_pos - 1;
  2607. else
  2608. data->args.lastbytewritten = U64_MAX;
  2609. data->res.server = NFS_SERVER(inode);
  2610. if (ld->prepare_layoutcommit) {
  2611. status = ld->prepare_layoutcommit(&data->args);
  2612. if (status) {
  2613. put_rpccred(data->cred);
  2614. spin_lock(&inode->i_lock);
  2615. set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
  2616. if (end_pos > nfsi->layout->plh_lwb)
  2617. nfsi->layout->plh_lwb = end_pos;
  2618. goto out_unlock;
  2619. }
  2620. }
  2621. status = nfs4_proc_layoutcommit(data, sync);
  2622. out:
  2623. if (status)
  2624. mark_inode_dirty_sync(inode);
  2625. dprintk("<-- %s status %d\n", __func__, status);
  2626. return status;
  2627. out_unlock:
  2628. spin_unlock(&inode->i_lock);
  2629. kfree(data);
  2630. clear_layoutcommitting:
  2631. pnfs_clear_layoutcommitting(inode);
  2632. goto out;
  2633. }
  2634. EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
  2635. int
  2636. pnfs_generic_sync(struct inode *inode, bool datasync)
  2637. {
  2638. return pnfs_layoutcommit_inode(inode, true);
  2639. }
  2640. EXPORT_SYMBOL_GPL(pnfs_generic_sync);
  2641. struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
  2642. {
  2643. struct nfs4_threshold *thp;
  2644. thp = kzalloc(sizeof(*thp), GFP_NOFS);
  2645. if (!thp) {
  2646. dprintk("%s mdsthreshold allocation failed\n", __func__);
  2647. return NULL;
  2648. }
  2649. return thp;
  2650. }
  2651. #if IS_ENABLED(CONFIG_NFS_V4_2)
  2652. int
  2653. pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
  2654. {
  2655. struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
  2656. struct nfs_server *server = NFS_SERVER(inode);
  2657. struct nfs_inode *nfsi = NFS_I(inode);
  2658. struct nfs42_layoutstat_data *data;
  2659. struct pnfs_layout_hdr *hdr;
  2660. int status = 0;
  2661. if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
  2662. goto out;
  2663. if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
  2664. goto out;
  2665. if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
  2666. goto out;
  2667. spin_lock(&inode->i_lock);
  2668. if (!NFS_I(inode)->layout) {
  2669. spin_unlock(&inode->i_lock);
  2670. goto out_clear_layoutstats;
  2671. }
  2672. hdr = NFS_I(inode)->layout;
  2673. pnfs_get_layout_hdr(hdr);
  2674. spin_unlock(&inode->i_lock);
  2675. data = kzalloc(sizeof(*data), gfp_flags);
  2676. if (!data) {
  2677. status = -ENOMEM;
  2678. goto out_put;
  2679. }
  2680. data->args.fh = NFS_FH(inode);
  2681. data->args.inode = inode;
  2682. status = ld->prepare_layoutstats(&data->args);
  2683. if (status)
  2684. goto out_free;
  2685. status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
  2686. out:
  2687. dprintk("%s returns %d\n", __func__, status);
  2688. return status;
  2689. out_free:
  2690. kfree(data);
  2691. out_put:
  2692. pnfs_put_layout_hdr(hdr);
  2693. out_clear_layoutstats:
  2694. smp_mb__before_atomic();
  2695. clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
  2696. smp_mb__after_atomic();
  2697. goto out;
  2698. }
  2699. EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
  2700. #endif
  2701. unsigned int layoutstats_timer;
  2702. module_param(layoutstats_timer, uint, 0644);
  2703. EXPORT_SYMBOL_GPL(layoutstats_timer);