pnfs.c 82 KB

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