pnfs.c 68 KB

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