relocation.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2009 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/writeback.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/rbtree.h>
  10. #include <linux/slab.h>
  11. #include <linux/error-injection.h>
  12. #include "ctree.h"
  13. #include "disk-io.h"
  14. #include "transaction.h"
  15. #include "volumes.h"
  16. #include "locking.h"
  17. #include "btrfs_inode.h"
  18. #include "async-thread.h"
  19. #include "free-space-cache.h"
  20. #include "qgroup.h"
  21. #include "print-tree.h"
  22. #include "delalloc-space.h"
  23. #include "block-group.h"
  24. #include "backref.h"
  25. #include "misc.h"
  26. #include "subpage.h"
  27. #include "zoned.h"
  28. #include "inode-item.h"
  29. /*
  30. * Relocation overview
  31. *
  32. * [What does relocation do]
  33. *
  34. * The objective of relocation is to relocate all extents of the target block
  35. * group to other block groups.
  36. * This is utilized by resize (shrink only), profile converting, compacting
  37. * space, or balance routine to spread chunks over devices.
  38. *
  39. * Before | After
  40. * ------------------------------------------------------------------
  41. * BG A: 10 data extents | BG A: deleted
  42. * BG B: 2 data extents | BG B: 10 data extents (2 old + 8 relocated)
  43. * BG C: 1 extents | BG C: 3 data extents (1 old + 2 relocated)
  44. *
  45. * [How does relocation work]
  46. *
  47. * 1. Mark the target block group read-only
  48. * New extents won't be allocated from the target block group.
  49. *
  50. * 2.1 Record each extent in the target block group
  51. * To build a proper map of extents to be relocated.
  52. *
  53. * 2.2 Build data reloc tree and reloc trees
  54. * Data reloc tree will contain an inode, recording all newly relocated
  55. * data extents.
  56. * There will be only one data reloc tree for one data block group.
  57. *
  58. * Reloc tree will be a special snapshot of its source tree, containing
  59. * relocated tree blocks.
  60. * Each tree referring to a tree block in target block group will get its
  61. * reloc tree built.
  62. *
  63. * 2.3 Swap source tree with its corresponding reloc tree
  64. * Each involved tree only refers to new extents after swap.
  65. *
  66. * 3. Cleanup reloc trees and data reloc tree.
  67. * As old extents in the target block group are still referenced by reloc
  68. * trees, we need to clean them up before really freeing the target block
  69. * group.
  70. *
  71. * The main complexity is in steps 2.2 and 2.3.
  72. *
  73. * The entry point of relocation is relocate_block_group() function.
  74. */
  75. #define RELOCATION_RESERVED_NODES 256
  76. /*
  77. * map address of tree root to tree
  78. */
  79. struct mapping_node {
  80. struct {
  81. struct rb_node rb_node;
  82. u64 bytenr;
  83. }; /* Use rb_simle_node for search/insert */
  84. void *data;
  85. };
  86. struct mapping_tree {
  87. struct rb_root rb_root;
  88. spinlock_t lock;
  89. };
  90. /*
  91. * present a tree block to process
  92. */
  93. struct tree_block {
  94. struct {
  95. struct rb_node rb_node;
  96. u64 bytenr;
  97. }; /* Use rb_simple_node for search/insert */
  98. u64 owner;
  99. struct btrfs_key key;
  100. unsigned int level:8;
  101. unsigned int key_ready:1;
  102. };
  103. #define MAX_EXTENTS 128
  104. struct file_extent_cluster {
  105. u64 start;
  106. u64 end;
  107. u64 boundary[MAX_EXTENTS];
  108. unsigned int nr;
  109. };
  110. struct reloc_control {
  111. /* block group to relocate */
  112. struct btrfs_block_group *block_group;
  113. /* extent tree */
  114. struct btrfs_root *extent_root;
  115. /* inode for moving data */
  116. struct inode *data_inode;
  117. struct btrfs_block_rsv *block_rsv;
  118. struct btrfs_backref_cache backref_cache;
  119. struct file_extent_cluster cluster;
  120. /* tree blocks have been processed */
  121. struct extent_io_tree processed_blocks;
  122. /* map start of tree root to corresponding reloc tree */
  123. struct mapping_tree reloc_root_tree;
  124. /* list of reloc trees */
  125. struct list_head reloc_roots;
  126. /* list of subvolume trees that get relocated */
  127. struct list_head dirty_subvol_roots;
  128. /* size of metadata reservation for merging reloc trees */
  129. u64 merging_rsv_size;
  130. /* size of relocated tree nodes */
  131. u64 nodes_relocated;
  132. /* reserved size for block group relocation*/
  133. u64 reserved_bytes;
  134. u64 search_start;
  135. u64 extents_found;
  136. unsigned int stage:8;
  137. unsigned int create_reloc_tree:1;
  138. unsigned int merge_reloc_tree:1;
  139. unsigned int found_file_extent:1;
  140. };
  141. /* stages of data relocation */
  142. #define MOVE_DATA_EXTENTS 0
  143. #define UPDATE_DATA_PTRS 1
  144. static void mark_block_processed(struct reloc_control *rc,
  145. struct btrfs_backref_node *node)
  146. {
  147. u32 blocksize;
  148. if (node->level == 0 ||
  149. in_range(node->bytenr, rc->block_group->start,
  150. rc->block_group->length)) {
  151. blocksize = rc->extent_root->fs_info->nodesize;
  152. set_extent_bits(&rc->processed_blocks, node->bytenr,
  153. node->bytenr + blocksize - 1, EXTENT_DIRTY);
  154. }
  155. node->processed = 1;
  156. }
  157. static void mapping_tree_init(struct mapping_tree *tree)
  158. {
  159. tree->rb_root = RB_ROOT;
  160. spin_lock_init(&tree->lock);
  161. }
  162. /*
  163. * walk up backref nodes until reach node presents tree root
  164. */
  165. static struct btrfs_backref_node *walk_up_backref(
  166. struct btrfs_backref_node *node,
  167. struct btrfs_backref_edge *edges[], int *index)
  168. {
  169. struct btrfs_backref_edge *edge;
  170. int idx = *index;
  171. while (!list_empty(&node->upper)) {
  172. edge = list_entry(node->upper.next,
  173. struct btrfs_backref_edge, list[LOWER]);
  174. edges[idx++] = edge;
  175. node = edge->node[UPPER];
  176. }
  177. BUG_ON(node->detached);
  178. *index = idx;
  179. return node;
  180. }
  181. /*
  182. * walk down backref nodes to find start of next reference path
  183. */
  184. static struct btrfs_backref_node *walk_down_backref(
  185. struct btrfs_backref_edge *edges[], int *index)
  186. {
  187. struct btrfs_backref_edge *edge;
  188. struct btrfs_backref_node *lower;
  189. int idx = *index;
  190. while (idx > 0) {
  191. edge = edges[idx - 1];
  192. lower = edge->node[LOWER];
  193. if (list_is_last(&edge->list[LOWER], &lower->upper)) {
  194. idx--;
  195. continue;
  196. }
  197. edge = list_entry(edge->list[LOWER].next,
  198. struct btrfs_backref_edge, list[LOWER]);
  199. edges[idx - 1] = edge;
  200. *index = idx;
  201. return edge->node[UPPER];
  202. }
  203. *index = 0;
  204. return NULL;
  205. }
  206. static void update_backref_node(struct btrfs_backref_cache *cache,
  207. struct btrfs_backref_node *node, u64 bytenr)
  208. {
  209. struct rb_node *rb_node;
  210. rb_erase(&node->rb_node, &cache->rb_root);
  211. node->bytenr = bytenr;
  212. rb_node = rb_simple_insert(&cache->rb_root, node->bytenr, &node->rb_node);
  213. if (rb_node)
  214. btrfs_backref_panic(cache->fs_info, bytenr, -EEXIST);
  215. }
  216. /*
  217. * update backref cache after a transaction commit
  218. */
  219. static int update_backref_cache(struct btrfs_trans_handle *trans,
  220. struct btrfs_backref_cache *cache)
  221. {
  222. struct btrfs_backref_node *node;
  223. int level = 0;
  224. if (cache->last_trans == 0) {
  225. cache->last_trans = trans->transid;
  226. return 0;
  227. }
  228. if (cache->last_trans == trans->transid)
  229. return 0;
  230. /*
  231. * detached nodes are used to avoid unnecessary backref
  232. * lookup. transaction commit changes the extent tree.
  233. * so the detached nodes are no longer useful.
  234. */
  235. while (!list_empty(&cache->detached)) {
  236. node = list_entry(cache->detached.next,
  237. struct btrfs_backref_node, list);
  238. btrfs_backref_cleanup_node(cache, node);
  239. }
  240. while (!list_empty(&cache->changed)) {
  241. node = list_entry(cache->changed.next,
  242. struct btrfs_backref_node, list);
  243. list_del_init(&node->list);
  244. BUG_ON(node->pending);
  245. update_backref_node(cache, node, node->new_bytenr);
  246. }
  247. /*
  248. * some nodes can be left in the pending list if there were
  249. * errors during processing the pending nodes.
  250. */
  251. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  252. list_for_each_entry(node, &cache->pending[level], list) {
  253. BUG_ON(!node->pending);
  254. if (node->bytenr == node->new_bytenr)
  255. continue;
  256. update_backref_node(cache, node, node->new_bytenr);
  257. }
  258. }
  259. cache->last_trans = 0;
  260. return 1;
  261. }
  262. static bool reloc_root_is_dead(struct btrfs_root *root)
  263. {
  264. /*
  265. * Pair with set_bit/clear_bit in clean_dirty_subvols and
  266. * btrfs_update_reloc_root. We need to see the updated bit before
  267. * trying to access reloc_root
  268. */
  269. smp_rmb();
  270. if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
  271. return true;
  272. return false;
  273. }
  274. /*
  275. * Check if this subvolume tree has valid reloc tree.
  276. *
  277. * Reloc tree after swap is considered dead, thus not considered as valid.
  278. * This is enough for most callers, as they don't distinguish dead reloc root
  279. * from no reloc root. But btrfs_should_ignore_reloc_root() below is a
  280. * special case.
  281. */
  282. static bool have_reloc_root(struct btrfs_root *root)
  283. {
  284. if (reloc_root_is_dead(root))
  285. return false;
  286. if (!root->reloc_root)
  287. return false;
  288. return true;
  289. }
  290. int btrfs_should_ignore_reloc_root(struct btrfs_root *root)
  291. {
  292. struct btrfs_root *reloc_root;
  293. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  294. return 0;
  295. /* This root has been merged with its reloc tree, we can ignore it */
  296. if (reloc_root_is_dead(root))
  297. return 1;
  298. reloc_root = root->reloc_root;
  299. if (!reloc_root)
  300. return 0;
  301. if (btrfs_header_generation(reloc_root->commit_root) ==
  302. root->fs_info->running_transaction->transid)
  303. return 0;
  304. /*
  305. * if there is reloc tree and it was created in previous
  306. * transaction backref lookup can find the reloc tree,
  307. * so backref node for the fs tree root is useless for
  308. * relocation.
  309. */
  310. return 1;
  311. }
  312. /*
  313. * find reloc tree by address of tree root
  314. */
  315. struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr)
  316. {
  317. struct reloc_control *rc = fs_info->reloc_ctl;
  318. struct rb_node *rb_node;
  319. struct mapping_node *node;
  320. struct btrfs_root *root = NULL;
  321. ASSERT(rc);
  322. spin_lock(&rc->reloc_root_tree.lock);
  323. rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, bytenr);
  324. if (rb_node) {
  325. node = rb_entry(rb_node, struct mapping_node, rb_node);
  326. root = node->data;
  327. }
  328. spin_unlock(&rc->reloc_root_tree.lock);
  329. return btrfs_grab_root(root);
  330. }
  331. /*
  332. * For useless nodes, do two major clean ups:
  333. *
  334. * - Cleanup the children edges and nodes
  335. * If child node is also orphan (no parent) during cleanup, then the child
  336. * node will also be cleaned up.
  337. *
  338. * - Freeing up leaves (level 0), keeps nodes detached
  339. * For nodes, the node is still cached as "detached"
  340. *
  341. * Return false if @node is not in the @useless_nodes list.
  342. * Return true if @node is in the @useless_nodes list.
  343. */
  344. static bool handle_useless_nodes(struct reloc_control *rc,
  345. struct btrfs_backref_node *node)
  346. {
  347. struct btrfs_backref_cache *cache = &rc->backref_cache;
  348. struct list_head *useless_node = &cache->useless_node;
  349. bool ret = false;
  350. while (!list_empty(useless_node)) {
  351. struct btrfs_backref_node *cur;
  352. cur = list_first_entry(useless_node, struct btrfs_backref_node,
  353. list);
  354. list_del_init(&cur->list);
  355. /* Only tree root nodes can be added to @useless_nodes */
  356. ASSERT(list_empty(&cur->upper));
  357. if (cur == node)
  358. ret = true;
  359. /* The node is the lowest node */
  360. if (cur->lowest) {
  361. list_del_init(&cur->lower);
  362. cur->lowest = 0;
  363. }
  364. /* Cleanup the lower edges */
  365. while (!list_empty(&cur->lower)) {
  366. struct btrfs_backref_edge *edge;
  367. struct btrfs_backref_node *lower;
  368. edge = list_entry(cur->lower.next,
  369. struct btrfs_backref_edge, list[UPPER]);
  370. list_del(&edge->list[UPPER]);
  371. list_del(&edge->list[LOWER]);
  372. lower = edge->node[LOWER];
  373. btrfs_backref_free_edge(cache, edge);
  374. /* Child node is also orphan, queue for cleanup */
  375. if (list_empty(&lower->upper))
  376. list_add(&lower->list, useless_node);
  377. }
  378. /* Mark this block processed for relocation */
  379. mark_block_processed(rc, cur);
  380. /*
  381. * Backref nodes for tree leaves are deleted from the cache.
  382. * Backref nodes for upper level tree blocks are left in the
  383. * cache to avoid unnecessary backref lookup.
  384. */
  385. if (cur->level > 0) {
  386. list_add(&cur->list, &cache->detached);
  387. cur->detached = 1;
  388. } else {
  389. rb_erase(&cur->rb_node, &cache->rb_root);
  390. btrfs_backref_free_node(cache, cur);
  391. }
  392. }
  393. return ret;
  394. }
  395. /*
  396. * Build backref tree for a given tree block. Root of the backref tree
  397. * corresponds the tree block, leaves of the backref tree correspond roots of
  398. * b-trees that reference the tree block.
  399. *
  400. * The basic idea of this function is check backrefs of a given block to find
  401. * upper level blocks that reference the block, and then check backrefs of
  402. * these upper level blocks recursively. The recursion stops when tree root is
  403. * reached or backrefs for the block is cached.
  404. *
  405. * NOTE: if we find that backrefs for a block are cached, we know backrefs for
  406. * all upper level blocks that directly/indirectly reference the block are also
  407. * cached.
  408. */
  409. static noinline_for_stack struct btrfs_backref_node *build_backref_tree(
  410. struct reloc_control *rc, struct btrfs_key *node_key,
  411. int level, u64 bytenr)
  412. {
  413. struct btrfs_backref_iter *iter;
  414. struct btrfs_backref_cache *cache = &rc->backref_cache;
  415. /* For searching parent of TREE_BLOCK_REF */
  416. struct btrfs_path *path;
  417. struct btrfs_backref_node *cur;
  418. struct btrfs_backref_node *node = NULL;
  419. struct btrfs_backref_edge *edge;
  420. int ret;
  421. int err = 0;
  422. iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info, GFP_NOFS);
  423. if (!iter)
  424. return ERR_PTR(-ENOMEM);
  425. path = btrfs_alloc_path();
  426. if (!path) {
  427. err = -ENOMEM;
  428. goto out;
  429. }
  430. node = btrfs_backref_alloc_node(cache, bytenr, level);
  431. if (!node) {
  432. err = -ENOMEM;
  433. goto out;
  434. }
  435. node->lowest = 1;
  436. cur = node;
  437. /* Breadth-first search to build backref cache */
  438. do {
  439. ret = btrfs_backref_add_tree_node(cache, path, iter, node_key,
  440. cur);
  441. if (ret < 0) {
  442. err = ret;
  443. goto out;
  444. }
  445. edge = list_first_entry_or_null(&cache->pending_edge,
  446. struct btrfs_backref_edge, list[UPPER]);
  447. /*
  448. * The pending list isn't empty, take the first block to
  449. * process
  450. */
  451. if (edge) {
  452. list_del_init(&edge->list[UPPER]);
  453. cur = edge->node[UPPER];
  454. }
  455. } while (edge);
  456. /* Finish the upper linkage of newly added edges/nodes */
  457. ret = btrfs_backref_finish_upper_links(cache, node);
  458. if (ret < 0) {
  459. err = ret;
  460. goto out;
  461. }
  462. if (handle_useless_nodes(rc, node))
  463. node = NULL;
  464. out:
  465. btrfs_backref_iter_free(iter);
  466. btrfs_free_path(path);
  467. if (err) {
  468. btrfs_backref_error_cleanup(cache, node);
  469. return ERR_PTR(err);
  470. }
  471. ASSERT(!node || !node->detached);
  472. ASSERT(list_empty(&cache->useless_node) &&
  473. list_empty(&cache->pending_edge));
  474. return node;
  475. }
  476. /*
  477. * helper to add backref node for the newly created snapshot.
  478. * the backref node is created by cloning backref node that
  479. * corresponds to root of source tree
  480. */
  481. static int clone_backref_node(struct btrfs_trans_handle *trans,
  482. struct reloc_control *rc,
  483. struct btrfs_root *src,
  484. struct btrfs_root *dest)
  485. {
  486. struct btrfs_root *reloc_root = src->reloc_root;
  487. struct btrfs_backref_cache *cache = &rc->backref_cache;
  488. struct btrfs_backref_node *node = NULL;
  489. struct btrfs_backref_node *new_node;
  490. struct btrfs_backref_edge *edge;
  491. struct btrfs_backref_edge *new_edge;
  492. struct rb_node *rb_node;
  493. if (cache->last_trans > 0)
  494. update_backref_cache(trans, cache);
  495. rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start);
  496. if (rb_node) {
  497. node = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
  498. if (node->detached)
  499. node = NULL;
  500. else
  501. BUG_ON(node->new_bytenr != reloc_root->node->start);
  502. }
  503. if (!node) {
  504. rb_node = rb_simple_search(&cache->rb_root,
  505. reloc_root->commit_root->start);
  506. if (rb_node) {
  507. node = rb_entry(rb_node, struct btrfs_backref_node,
  508. rb_node);
  509. BUG_ON(node->detached);
  510. }
  511. }
  512. if (!node)
  513. return 0;
  514. new_node = btrfs_backref_alloc_node(cache, dest->node->start,
  515. node->level);
  516. if (!new_node)
  517. return -ENOMEM;
  518. new_node->lowest = node->lowest;
  519. new_node->checked = 1;
  520. new_node->root = btrfs_grab_root(dest);
  521. ASSERT(new_node->root);
  522. if (!node->lowest) {
  523. list_for_each_entry(edge, &node->lower, list[UPPER]) {
  524. new_edge = btrfs_backref_alloc_edge(cache);
  525. if (!new_edge)
  526. goto fail;
  527. btrfs_backref_link_edge(new_edge, edge->node[LOWER],
  528. new_node, LINK_UPPER);
  529. }
  530. } else {
  531. list_add_tail(&new_node->lower, &cache->leaves);
  532. }
  533. rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr,
  534. &new_node->rb_node);
  535. if (rb_node)
  536. btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST);
  537. if (!new_node->lowest) {
  538. list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
  539. list_add_tail(&new_edge->list[LOWER],
  540. &new_edge->node[LOWER]->upper);
  541. }
  542. }
  543. return 0;
  544. fail:
  545. while (!list_empty(&new_node->lower)) {
  546. new_edge = list_entry(new_node->lower.next,
  547. struct btrfs_backref_edge, list[UPPER]);
  548. list_del(&new_edge->list[UPPER]);
  549. btrfs_backref_free_edge(cache, new_edge);
  550. }
  551. btrfs_backref_free_node(cache, new_node);
  552. return -ENOMEM;
  553. }
  554. /*
  555. * helper to add 'address of tree root -> reloc tree' mapping
  556. */
  557. static int __must_check __add_reloc_root(struct btrfs_root *root)
  558. {
  559. struct btrfs_fs_info *fs_info = root->fs_info;
  560. struct rb_node *rb_node;
  561. struct mapping_node *node;
  562. struct reloc_control *rc = fs_info->reloc_ctl;
  563. node = kmalloc(sizeof(*node), GFP_NOFS);
  564. if (!node)
  565. return -ENOMEM;
  566. node->bytenr = root->commit_root->start;
  567. node->data = root;
  568. spin_lock(&rc->reloc_root_tree.lock);
  569. rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
  570. node->bytenr, &node->rb_node);
  571. spin_unlock(&rc->reloc_root_tree.lock);
  572. if (rb_node) {
  573. btrfs_err(fs_info,
  574. "Duplicate root found for start=%llu while inserting into relocation tree",
  575. node->bytenr);
  576. return -EEXIST;
  577. }
  578. list_add_tail(&root->root_list, &rc->reloc_roots);
  579. return 0;
  580. }
  581. /*
  582. * helper to delete the 'address of tree root -> reloc tree'
  583. * mapping
  584. */
  585. static void __del_reloc_root(struct btrfs_root *root)
  586. {
  587. struct btrfs_fs_info *fs_info = root->fs_info;
  588. struct rb_node *rb_node;
  589. struct mapping_node *node = NULL;
  590. struct reloc_control *rc = fs_info->reloc_ctl;
  591. bool put_ref = false;
  592. if (rc && root->node) {
  593. spin_lock(&rc->reloc_root_tree.lock);
  594. rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
  595. root->commit_root->start);
  596. if (rb_node) {
  597. node = rb_entry(rb_node, struct mapping_node, rb_node);
  598. rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
  599. RB_CLEAR_NODE(&node->rb_node);
  600. }
  601. spin_unlock(&rc->reloc_root_tree.lock);
  602. ASSERT(!node || (struct btrfs_root *)node->data == root);
  603. }
  604. /*
  605. * We only put the reloc root here if it's on the list. There's a lot
  606. * of places where the pattern is to splice the rc->reloc_roots, process
  607. * the reloc roots, and then add the reloc root back onto
  608. * rc->reloc_roots. If we call __del_reloc_root while it's off of the
  609. * list we don't want the reference being dropped, because the guy
  610. * messing with the list is in charge of the reference.
  611. */
  612. spin_lock(&fs_info->trans_lock);
  613. if (!list_empty(&root->root_list)) {
  614. put_ref = true;
  615. list_del_init(&root->root_list);
  616. }
  617. spin_unlock(&fs_info->trans_lock);
  618. if (put_ref)
  619. btrfs_put_root(root);
  620. kfree(node);
  621. }
  622. /*
  623. * helper to update the 'address of tree root -> reloc tree'
  624. * mapping
  625. */
  626. static int __update_reloc_root(struct btrfs_root *root)
  627. {
  628. struct btrfs_fs_info *fs_info = root->fs_info;
  629. struct rb_node *rb_node;
  630. struct mapping_node *node = NULL;
  631. struct reloc_control *rc = fs_info->reloc_ctl;
  632. spin_lock(&rc->reloc_root_tree.lock);
  633. rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
  634. root->commit_root->start);
  635. if (rb_node) {
  636. node = rb_entry(rb_node, struct mapping_node, rb_node);
  637. rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
  638. }
  639. spin_unlock(&rc->reloc_root_tree.lock);
  640. if (!node)
  641. return 0;
  642. BUG_ON((struct btrfs_root *)node->data != root);
  643. spin_lock(&rc->reloc_root_tree.lock);
  644. node->bytenr = root->node->start;
  645. rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
  646. node->bytenr, &node->rb_node);
  647. spin_unlock(&rc->reloc_root_tree.lock);
  648. if (rb_node)
  649. btrfs_backref_panic(fs_info, node->bytenr, -EEXIST);
  650. return 0;
  651. }
  652. static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
  653. struct btrfs_root *root, u64 objectid)
  654. {
  655. struct btrfs_fs_info *fs_info = root->fs_info;
  656. struct btrfs_root *reloc_root;
  657. struct extent_buffer *eb;
  658. struct btrfs_root_item *root_item;
  659. struct btrfs_key root_key;
  660. int ret = 0;
  661. bool must_abort = false;
  662. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  663. if (!root_item)
  664. return ERR_PTR(-ENOMEM);
  665. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  666. root_key.type = BTRFS_ROOT_ITEM_KEY;
  667. root_key.offset = objectid;
  668. if (root->root_key.objectid == objectid) {
  669. u64 commit_root_gen;
  670. /* called by btrfs_init_reloc_root */
  671. ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
  672. BTRFS_TREE_RELOC_OBJECTID);
  673. if (ret)
  674. goto fail;
  675. /*
  676. * Set the last_snapshot field to the generation of the commit
  677. * root - like this ctree.c:btrfs_block_can_be_shared() behaves
  678. * correctly (returns true) when the relocation root is created
  679. * either inside the critical section of a transaction commit
  680. * (through transaction.c:qgroup_account_snapshot()) and when
  681. * it's created before the transaction commit is started.
  682. */
  683. commit_root_gen = btrfs_header_generation(root->commit_root);
  684. btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
  685. } else {
  686. /*
  687. * called by btrfs_reloc_post_snapshot_hook.
  688. * the source tree is a reloc tree, all tree blocks
  689. * modified after it was created have RELOC flag
  690. * set in their headers. so it's OK to not update
  691. * the 'last_snapshot'.
  692. */
  693. ret = btrfs_copy_root(trans, root, root->node, &eb,
  694. BTRFS_TREE_RELOC_OBJECTID);
  695. if (ret)
  696. goto fail;
  697. }
  698. /*
  699. * We have changed references at this point, we must abort the
  700. * transaction if anything fails.
  701. */
  702. must_abort = true;
  703. memcpy(root_item, &root->root_item, sizeof(*root_item));
  704. btrfs_set_root_bytenr(root_item, eb->start);
  705. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  706. btrfs_set_root_generation(root_item, trans->transid);
  707. if (root->root_key.objectid == objectid) {
  708. btrfs_set_root_refs(root_item, 0);
  709. memset(&root_item->drop_progress, 0,
  710. sizeof(struct btrfs_disk_key));
  711. btrfs_set_root_drop_level(root_item, 0);
  712. }
  713. btrfs_tree_unlock(eb);
  714. free_extent_buffer(eb);
  715. ret = btrfs_insert_root(trans, fs_info->tree_root,
  716. &root_key, root_item);
  717. if (ret)
  718. goto fail;
  719. kfree(root_item);
  720. reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);
  721. if (IS_ERR(reloc_root)) {
  722. ret = PTR_ERR(reloc_root);
  723. goto abort;
  724. }
  725. set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
  726. reloc_root->last_trans = trans->transid;
  727. return reloc_root;
  728. fail:
  729. kfree(root_item);
  730. abort:
  731. if (must_abort)
  732. btrfs_abort_transaction(trans, ret);
  733. return ERR_PTR(ret);
  734. }
  735. /*
  736. * create reloc tree for a given fs tree. reloc tree is just a
  737. * snapshot of the fs tree with special root objectid.
  738. *
  739. * The reloc_root comes out of here with two references, one for
  740. * root->reloc_root, and another for being on the rc->reloc_roots list.
  741. */
  742. int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
  743. struct btrfs_root *root)
  744. {
  745. struct btrfs_fs_info *fs_info = root->fs_info;
  746. struct btrfs_root *reloc_root;
  747. struct reloc_control *rc = fs_info->reloc_ctl;
  748. struct btrfs_block_rsv *rsv;
  749. int clear_rsv = 0;
  750. int ret;
  751. if (!rc)
  752. return 0;
  753. /*
  754. * The subvolume has reloc tree but the swap is finished, no need to
  755. * create/update the dead reloc tree
  756. */
  757. if (reloc_root_is_dead(root))
  758. return 0;
  759. /*
  760. * This is subtle but important. We do not do
  761. * record_root_in_transaction for reloc roots, instead we record their
  762. * corresponding fs root, and then here we update the last trans for the
  763. * reloc root. This means that we have to do this for the entire life
  764. * of the reloc root, regardless of which stage of the relocation we are
  765. * in.
  766. */
  767. if (root->reloc_root) {
  768. reloc_root = root->reloc_root;
  769. reloc_root->last_trans = trans->transid;
  770. return 0;
  771. }
  772. /*
  773. * We are merging reloc roots, we do not need new reloc trees. Also
  774. * reloc trees never need their own reloc tree.
  775. */
  776. if (!rc->create_reloc_tree ||
  777. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  778. return 0;
  779. if (!trans->reloc_reserved) {
  780. rsv = trans->block_rsv;
  781. trans->block_rsv = rc->block_rsv;
  782. clear_rsv = 1;
  783. }
  784. reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
  785. if (clear_rsv)
  786. trans->block_rsv = rsv;
  787. if (IS_ERR(reloc_root))
  788. return PTR_ERR(reloc_root);
  789. ret = __add_reloc_root(reloc_root);
  790. ASSERT(ret != -EEXIST);
  791. if (ret) {
  792. /* Pairs with create_reloc_root */
  793. btrfs_put_root(reloc_root);
  794. return ret;
  795. }
  796. root->reloc_root = btrfs_grab_root(reloc_root);
  797. return 0;
  798. }
  799. /*
  800. * update root item of reloc tree
  801. */
  802. int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
  803. struct btrfs_root *root)
  804. {
  805. struct btrfs_fs_info *fs_info = root->fs_info;
  806. struct btrfs_root *reloc_root;
  807. struct btrfs_root_item *root_item;
  808. int ret;
  809. if (!have_reloc_root(root))
  810. return 0;
  811. reloc_root = root->reloc_root;
  812. root_item = &reloc_root->root_item;
  813. /*
  814. * We are probably ok here, but __del_reloc_root() will drop its ref of
  815. * the root. We have the ref for root->reloc_root, but just in case
  816. * hold it while we update the reloc root.
  817. */
  818. btrfs_grab_root(reloc_root);
  819. /* root->reloc_root will stay until current relocation finished */
  820. if (fs_info->reloc_ctl->merge_reloc_tree &&
  821. btrfs_root_refs(root_item) == 0) {
  822. set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
  823. /*
  824. * Mark the tree as dead before we change reloc_root so
  825. * have_reloc_root will not touch it from now on.
  826. */
  827. smp_wmb();
  828. __del_reloc_root(reloc_root);
  829. }
  830. if (reloc_root->commit_root != reloc_root->node) {
  831. __update_reloc_root(reloc_root);
  832. btrfs_set_root_node(root_item, reloc_root->node);
  833. free_extent_buffer(reloc_root->commit_root);
  834. reloc_root->commit_root = btrfs_root_node(reloc_root);
  835. }
  836. ret = btrfs_update_root(trans, fs_info->tree_root,
  837. &reloc_root->root_key, root_item);
  838. btrfs_put_root(reloc_root);
  839. return ret;
  840. }
  841. /*
  842. * helper to find first cached inode with inode number >= objectid
  843. * in a subvolume
  844. */
  845. static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
  846. {
  847. struct rb_node *node;
  848. struct rb_node *prev;
  849. struct btrfs_inode *entry;
  850. struct inode *inode;
  851. spin_lock(&root->inode_lock);
  852. again:
  853. node = root->inode_tree.rb_node;
  854. prev = NULL;
  855. while (node) {
  856. prev = node;
  857. entry = rb_entry(node, struct btrfs_inode, rb_node);
  858. if (objectid < btrfs_ino(entry))
  859. node = node->rb_left;
  860. else if (objectid > btrfs_ino(entry))
  861. node = node->rb_right;
  862. else
  863. break;
  864. }
  865. if (!node) {
  866. while (prev) {
  867. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  868. if (objectid <= btrfs_ino(entry)) {
  869. node = prev;
  870. break;
  871. }
  872. prev = rb_next(prev);
  873. }
  874. }
  875. while (node) {
  876. entry = rb_entry(node, struct btrfs_inode, rb_node);
  877. inode = igrab(&entry->vfs_inode);
  878. if (inode) {
  879. spin_unlock(&root->inode_lock);
  880. return inode;
  881. }
  882. objectid = btrfs_ino(entry) + 1;
  883. if (cond_resched_lock(&root->inode_lock))
  884. goto again;
  885. node = rb_next(node);
  886. }
  887. spin_unlock(&root->inode_lock);
  888. return NULL;
  889. }
  890. /*
  891. * get new location of data
  892. */
  893. static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
  894. u64 bytenr, u64 num_bytes)
  895. {
  896. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  897. struct btrfs_path *path;
  898. struct btrfs_file_extent_item *fi;
  899. struct extent_buffer *leaf;
  900. int ret;
  901. path = btrfs_alloc_path();
  902. if (!path)
  903. return -ENOMEM;
  904. bytenr -= BTRFS_I(reloc_inode)->index_cnt;
  905. ret = btrfs_lookup_file_extent(NULL, root, path,
  906. btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
  907. if (ret < 0)
  908. goto out;
  909. if (ret > 0) {
  910. ret = -ENOENT;
  911. goto out;
  912. }
  913. leaf = path->nodes[0];
  914. fi = btrfs_item_ptr(leaf, path->slots[0],
  915. struct btrfs_file_extent_item);
  916. BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
  917. btrfs_file_extent_compression(leaf, fi) ||
  918. btrfs_file_extent_encryption(leaf, fi) ||
  919. btrfs_file_extent_other_encoding(leaf, fi));
  920. if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
  921. ret = -EINVAL;
  922. goto out;
  923. }
  924. *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  925. ret = 0;
  926. out:
  927. btrfs_free_path(path);
  928. return ret;
  929. }
  930. /*
  931. * update file extent items in the tree leaf to point to
  932. * the new locations.
  933. */
  934. static noinline_for_stack
  935. int replace_file_extents(struct btrfs_trans_handle *trans,
  936. struct reloc_control *rc,
  937. struct btrfs_root *root,
  938. struct extent_buffer *leaf)
  939. {
  940. struct btrfs_fs_info *fs_info = root->fs_info;
  941. struct btrfs_key key;
  942. struct btrfs_file_extent_item *fi;
  943. struct inode *inode = NULL;
  944. u64 parent;
  945. u64 bytenr;
  946. u64 new_bytenr = 0;
  947. u64 num_bytes;
  948. u64 end;
  949. u32 nritems;
  950. u32 i;
  951. int ret = 0;
  952. int first = 1;
  953. int dirty = 0;
  954. if (rc->stage != UPDATE_DATA_PTRS)
  955. return 0;
  956. /* reloc trees always use full backref */
  957. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  958. parent = leaf->start;
  959. else
  960. parent = 0;
  961. nritems = btrfs_header_nritems(leaf);
  962. for (i = 0; i < nritems; i++) {
  963. struct btrfs_ref ref = { 0 };
  964. cond_resched();
  965. btrfs_item_key_to_cpu(leaf, &key, i);
  966. if (key.type != BTRFS_EXTENT_DATA_KEY)
  967. continue;
  968. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  969. if (btrfs_file_extent_type(leaf, fi) ==
  970. BTRFS_FILE_EXTENT_INLINE)
  971. continue;
  972. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  973. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  974. if (bytenr == 0)
  975. continue;
  976. if (!in_range(bytenr, rc->block_group->start,
  977. rc->block_group->length))
  978. continue;
  979. /*
  980. * if we are modifying block in fs tree, wait for read_folio
  981. * to complete and drop the extent cache
  982. */
  983. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  984. if (first) {
  985. inode = find_next_inode(root, key.objectid);
  986. first = 0;
  987. } else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) {
  988. btrfs_add_delayed_iput(inode);
  989. inode = find_next_inode(root, key.objectid);
  990. }
  991. if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
  992. end = key.offset +
  993. btrfs_file_extent_num_bytes(leaf, fi);
  994. WARN_ON(!IS_ALIGNED(key.offset,
  995. fs_info->sectorsize));
  996. WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
  997. end--;
  998. ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
  999. key.offset, end);
  1000. if (!ret)
  1001. continue;
  1002. btrfs_drop_extent_map_range(BTRFS_I(inode),
  1003. key.offset, end, true);
  1004. unlock_extent(&BTRFS_I(inode)->io_tree,
  1005. key.offset, end, NULL);
  1006. }
  1007. }
  1008. ret = get_new_location(rc->data_inode, &new_bytenr,
  1009. bytenr, num_bytes);
  1010. if (ret) {
  1011. /*
  1012. * Don't have to abort since we've not changed anything
  1013. * in the file extent yet.
  1014. */
  1015. break;
  1016. }
  1017. btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
  1018. dirty = 1;
  1019. key.offset -= btrfs_file_extent_offset(leaf, fi);
  1020. btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
  1021. num_bytes, parent);
  1022. btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
  1023. key.objectid, key.offset,
  1024. root->root_key.objectid, false);
  1025. ret = btrfs_inc_extent_ref(trans, &ref);
  1026. if (ret) {
  1027. btrfs_abort_transaction(trans, ret);
  1028. break;
  1029. }
  1030. btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
  1031. num_bytes, parent);
  1032. btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
  1033. key.objectid, key.offset,
  1034. root->root_key.objectid, false);
  1035. ret = btrfs_free_extent(trans, &ref);
  1036. if (ret) {
  1037. btrfs_abort_transaction(trans, ret);
  1038. break;
  1039. }
  1040. }
  1041. if (dirty)
  1042. btrfs_mark_buffer_dirty(leaf);
  1043. if (inode)
  1044. btrfs_add_delayed_iput(inode);
  1045. return ret;
  1046. }
  1047. static noinline_for_stack
  1048. int memcmp_node_keys(struct extent_buffer *eb, int slot,
  1049. struct btrfs_path *path, int level)
  1050. {
  1051. struct btrfs_disk_key key1;
  1052. struct btrfs_disk_key key2;
  1053. btrfs_node_key(eb, &key1, slot);
  1054. btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
  1055. return memcmp(&key1, &key2, sizeof(key1));
  1056. }
  1057. /*
  1058. * try to replace tree blocks in fs tree with the new blocks
  1059. * in reloc tree. tree blocks haven't been modified since the
  1060. * reloc tree was create can be replaced.
  1061. *
  1062. * if a block was replaced, level of the block + 1 is returned.
  1063. * if no block got replaced, 0 is returned. if there are other
  1064. * errors, a negative error number is returned.
  1065. */
  1066. static noinline_for_stack
  1067. int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
  1068. struct btrfs_root *dest, struct btrfs_root *src,
  1069. struct btrfs_path *path, struct btrfs_key *next_key,
  1070. int lowest_level, int max_level)
  1071. {
  1072. struct btrfs_fs_info *fs_info = dest->fs_info;
  1073. struct extent_buffer *eb;
  1074. struct extent_buffer *parent;
  1075. struct btrfs_ref ref = { 0 };
  1076. struct btrfs_key key;
  1077. u64 old_bytenr;
  1078. u64 new_bytenr;
  1079. u64 old_ptr_gen;
  1080. u64 new_ptr_gen;
  1081. u64 last_snapshot;
  1082. u32 blocksize;
  1083. int cow = 0;
  1084. int level;
  1085. int ret;
  1086. int slot;
  1087. ASSERT(src->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
  1088. ASSERT(dest->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  1089. last_snapshot = btrfs_root_last_snapshot(&src->root_item);
  1090. again:
  1091. slot = path->slots[lowest_level];
  1092. btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
  1093. eb = btrfs_lock_root_node(dest);
  1094. level = btrfs_header_level(eb);
  1095. if (level < lowest_level) {
  1096. btrfs_tree_unlock(eb);
  1097. free_extent_buffer(eb);
  1098. return 0;
  1099. }
  1100. if (cow) {
  1101. ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb,
  1102. BTRFS_NESTING_COW);
  1103. if (ret) {
  1104. btrfs_tree_unlock(eb);
  1105. free_extent_buffer(eb);
  1106. return ret;
  1107. }
  1108. }
  1109. if (next_key) {
  1110. next_key->objectid = (u64)-1;
  1111. next_key->type = (u8)-1;
  1112. next_key->offset = (u64)-1;
  1113. }
  1114. parent = eb;
  1115. while (1) {
  1116. level = btrfs_header_level(parent);
  1117. ASSERT(level >= lowest_level);
  1118. ret = btrfs_bin_search(parent, &key, &slot);
  1119. if (ret < 0)
  1120. break;
  1121. if (ret && slot > 0)
  1122. slot--;
  1123. if (next_key && slot + 1 < btrfs_header_nritems(parent))
  1124. btrfs_node_key_to_cpu(parent, next_key, slot + 1);
  1125. old_bytenr = btrfs_node_blockptr(parent, slot);
  1126. blocksize = fs_info->nodesize;
  1127. old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
  1128. if (level <= max_level) {
  1129. eb = path->nodes[level];
  1130. new_bytenr = btrfs_node_blockptr(eb,
  1131. path->slots[level]);
  1132. new_ptr_gen = btrfs_node_ptr_generation(eb,
  1133. path->slots[level]);
  1134. } else {
  1135. new_bytenr = 0;
  1136. new_ptr_gen = 0;
  1137. }
  1138. if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
  1139. ret = level;
  1140. break;
  1141. }
  1142. if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
  1143. memcmp_node_keys(parent, slot, path, level)) {
  1144. if (level <= lowest_level) {
  1145. ret = 0;
  1146. break;
  1147. }
  1148. eb = btrfs_read_node_slot(parent, slot);
  1149. if (IS_ERR(eb)) {
  1150. ret = PTR_ERR(eb);
  1151. break;
  1152. }
  1153. btrfs_tree_lock(eb);
  1154. if (cow) {
  1155. ret = btrfs_cow_block(trans, dest, eb, parent,
  1156. slot, &eb,
  1157. BTRFS_NESTING_COW);
  1158. if (ret) {
  1159. btrfs_tree_unlock(eb);
  1160. free_extent_buffer(eb);
  1161. break;
  1162. }
  1163. }
  1164. btrfs_tree_unlock(parent);
  1165. free_extent_buffer(parent);
  1166. parent = eb;
  1167. continue;
  1168. }
  1169. if (!cow) {
  1170. btrfs_tree_unlock(parent);
  1171. free_extent_buffer(parent);
  1172. cow = 1;
  1173. goto again;
  1174. }
  1175. btrfs_node_key_to_cpu(path->nodes[level], &key,
  1176. path->slots[level]);
  1177. btrfs_release_path(path);
  1178. path->lowest_level = level;
  1179. set_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &src->state);
  1180. ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
  1181. clear_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &src->state);
  1182. path->lowest_level = 0;
  1183. if (ret) {
  1184. if (ret > 0)
  1185. ret = -ENOENT;
  1186. break;
  1187. }
  1188. /*
  1189. * Info qgroup to trace both subtrees.
  1190. *
  1191. * We must trace both trees.
  1192. * 1) Tree reloc subtree
  1193. * If not traced, we will leak data numbers
  1194. * 2) Fs subtree
  1195. * If not traced, we will double count old data
  1196. *
  1197. * We don't scan the subtree right now, but only record
  1198. * the swapped tree blocks.
  1199. * The real subtree rescan is delayed until we have new
  1200. * CoW on the subtree root node before transaction commit.
  1201. */
  1202. ret = btrfs_qgroup_add_swapped_blocks(trans, dest,
  1203. rc->block_group, parent, slot,
  1204. path->nodes[level], path->slots[level],
  1205. last_snapshot);
  1206. if (ret < 0)
  1207. break;
  1208. /*
  1209. * swap blocks in fs tree and reloc tree.
  1210. */
  1211. btrfs_set_node_blockptr(parent, slot, new_bytenr);
  1212. btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
  1213. btrfs_mark_buffer_dirty(parent);
  1214. btrfs_set_node_blockptr(path->nodes[level],
  1215. path->slots[level], old_bytenr);
  1216. btrfs_set_node_ptr_generation(path->nodes[level],
  1217. path->slots[level], old_ptr_gen);
  1218. btrfs_mark_buffer_dirty(path->nodes[level]);
  1219. btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
  1220. blocksize, path->nodes[level]->start);
  1221. btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
  1222. 0, true);
  1223. ret = btrfs_inc_extent_ref(trans, &ref);
  1224. if (ret) {
  1225. btrfs_abort_transaction(trans, ret);
  1226. break;
  1227. }
  1228. btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
  1229. blocksize, 0);
  1230. btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid, 0,
  1231. true);
  1232. ret = btrfs_inc_extent_ref(trans, &ref);
  1233. if (ret) {
  1234. btrfs_abort_transaction(trans, ret);
  1235. break;
  1236. }
  1237. btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
  1238. blocksize, path->nodes[level]->start);
  1239. btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
  1240. 0, true);
  1241. ret = btrfs_free_extent(trans, &ref);
  1242. if (ret) {
  1243. btrfs_abort_transaction(trans, ret);
  1244. break;
  1245. }
  1246. btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
  1247. blocksize, 0);
  1248. btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid,
  1249. 0, true);
  1250. ret = btrfs_free_extent(trans, &ref);
  1251. if (ret) {
  1252. btrfs_abort_transaction(trans, ret);
  1253. break;
  1254. }
  1255. btrfs_unlock_up_safe(path, 0);
  1256. ret = level;
  1257. break;
  1258. }
  1259. btrfs_tree_unlock(parent);
  1260. free_extent_buffer(parent);
  1261. return ret;
  1262. }
  1263. /*
  1264. * helper to find next relocated block in reloc tree
  1265. */
  1266. static noinline_for_stack
  1267. int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
  1268. int *level)
  1269. {
  1270. struct extent_buffer *eb;
  1271. int i;
  1272. u64 last_snapshot;
  1273. u32 nritems;
  1274. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1275. for (i = 0; i < *level; i++) {
  1276. free_extent_buffer(path->nodes[i]);
  1277. path->nodes[i] = NULL;
  1278. }
  1279. for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
  1280. eb = path->nodes[i];
  1281. nritems = btrfs_header_nritems(eb);
  1282. while (path->slots[i] + 1 < nritems) {
  1283. path->slots[i]++;
  1284. if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
  1285. last_snapshot)
  1286. continue;
  1287. *level = i;
  1288. return 0;
  1289. }
  1290. free_extent_buffer(path->nodes[i]);
  1291. path->nodes[i] = NULL;
  1292. }
  1293. return 1;
  1294. }
  1295. /*
  1296. * walk down reloc tree to find relocated block of lowest level
  1297. */
  1298. static noinline_for_stack
  1299. int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
  1300. int *level)
  1301. {
  1302. struct extent_buffer *eb = NULL;
  1303. int i;
  1304. u64 ptr_gen = 0;
  1305. u64 last_snapshot;
  1306. u32 nritems;
  1307. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1308. for (i = *level; i > 0; i--) {
  1309. eb = path->nodes[i];
  1310. nritems = btrfs_header_nritems(eb);
  1311. while (path->slots[i] < nritems) {
  1312. ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
  1313. if (ptr_gen > last_snapshot)
  1314. break;
  1315. path->slots[i]++;
  1316. }
  1317. if (path->slots[i] >= nritems) {
  1318. if (i == *level)
  1319. break;
  1320. *level = i + 1;
  1321. return 0;
  1322. }
  1323. if (i == 1) {
  1324. *level = i;
  1325. return 0;
  1326. }
  1327. eb = btrfs_read_node_slot(eb, path->slots[i]);
  1328. if (IS_ERR(eb))
  1329. return PTR_ERR(eb);
  1330. BUG_ON(btrfs_header_level(eb) != i - 1);
  1331. path->nodes[i - 1] = eb;
  1332. path->slots[i - 1] = 0;
  1333. }
  1334. return 1;
  1335. }
  1336. /*
  1337. * invalidate extent cache for file extents whose key in range of
  1338. * [min_key, max_key)
  1339. */
  1340. static int invalidate_extent_cache(struct btrfs_root *root,
  1341. struct btrfs_key *min_key,
  1342. struct btrfs_key *max_key)
  1343. {
  1344. struct btrfs_fs_info *fs_info = root->fs_info;
  1345. struct inode *inode = NULL;
  1346. u64 objectid;
  1347. u64 start, end;
  1348. u64 ino;
  1349. objectid = min_key->objectid;
  1350. while (1) {
  1351. cond_resched();
  1352. iput(inode);
  1353. if (objectid > max_key->objectid)
  1354. break;
  1355. inode = find_next_inode(root, objectid);
  1356. if (!inode)
  1357. break;
  1358. ino = btrfs_ino(BTRFS_I(inode));
  1359. if (ino > max_key->objectid) {
  1360. iput(inode);
  1361. break;
  1362. }
  1363. objectid = ino + 1;
  1364. if (!S_ISREG(inode->i_mode))
  1365. continue;
  1366. if (unlikely(min_key->objectid == ino)) {
  1367. if (min_key->type > BTRFS_EXTENT_DATA_KEY)
  1368. continue;
  1369. if (min_key->type < BTRFS_EXTENT_DATA_KEY)
  1370. start = 0;
  1371. else {
  1372. start = min_key->offset;
  1373. WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
  1374. }
  1375. } else {
  1376. start = 0;
  1377. }
  1378. if (unlikely(max_key->objectid == ino)) {
  1379. if (max_key->type < BTRFS_EXTENT_DATA_KEY)
  1380. continue;
  1381. if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
  1382. end = (u64)-1;
  1383. } else {
  1384. if (max_key->offset == 0)
  1385. continue;
  1386. end = max_key->offset;
  1387. WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
  1388. end--;
  1389. }
  1390. } else {
  1391. end = (u64)-1;
  1392. }
  1393. /* the lock_extent waits for read_folio to complete */
  1394. lock_extent(&BTRFS_I(inode)->io_tree, start, end, NULL);
  1395. btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, true);
  1396. unlock_extent(&BTRFS_I(inode)->io_tree, start, end, NULL);
  1397. }
  1398. return 0;
  1399. }
  1400. static int find_next_key(struct btrfs_path *path, int level,
  1401. struct btrfs_key *key)
  1402. {
  1403. while (level < BTRFS_MAX_LEVEL) {
  1404. if (!path->nodes[level])
  1405. break;
  1406. if (path->slots[level] + 1 <
  1407. btrfs_header_nritems(path->nodes[level])) {
  1408. btrfs_node_key_to_cpu(path->nodes[level], key,
  1409. path->slots[level] + 1);
  1410. return 0;
  1411. }
  1412. level++;
  1413. }
  1414. return 1;
  1415. }
  1416. /*
  1417. * Insert current subvolume into reloc_control::dirty_subvol_roots
  1418. */
  1419. static int insert_dirty_subvol(struct btrfs_trans_handle *trans,
  1420. struct reloc_control *rc,
  1421. struct btrfs_root *root)
  1422. {
  1423. struct btrfs_root *reloc_root = root->reloc_root;
  1424. struct btrfs_root_item *reloc_root_item;
  1425. int ret;
  1426. /* @root must be a subvolume tree root with a valid reloc tree */
  1427. ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  1428. ASSERT(reloc_root);
  1429. reloc_root_item = &reloc_root->root_item;
  1430. memset(&reloc_root_item->drop_progress, 0,
  1431. sizeof(reloc_root_item->drop_progress));
  1432. btrfs_set_root_drop_level(reloc_root_item, 0);
  1433. btrfs_set_root_refs(reloc_root_item, 0);
  1434. ret = btrfs_update_reloc_root(trans, root);
  1435. if (ret)
  1436. return ret;
  1437. if (list_empty(&root->reloc_dirty_list)) {
  1438. btrfs_grab_root(root);
  1439. list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
  1440. }
  1441. return 0;
  1442. }
  1443. static int clean_dirty_subvols(struct reloc_control *rc)
  1444. {
  1445. struct btrfs_root *root;
  1446. struct btrfs_root *next;
  1447. int ret = 0;
  1448. int ret2;
  1449. list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
  1450. reloc_dirty_list) {
  1451. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  1452. /* Merged subvolume, cleanup its reloc root */
  1453. struct btrfs_root *reloc_root = root->reloc_root;
  1454. list_del_init(&root->reloc_dirty_list);
  1455. root->reloc_root = NULL;
  1456. /*
  1457. * Need barrier to ensure clear_bit() only happens after
  1458. * root->reloc_root = NULL. Pairs with have_reloc_root.
  1459. */
  1460. smp_wmb();
  1461. clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
  1462. if (reloc_root) {
  1463. /*
  1464. * btrfs_drop_snapshot drops our ref we hold for
  1465. * ->reloc_root. If it fails however we must
  1466. * drop the ref ourselves.
  1467. */
  1468. ret2 = btrfs_drop_snapshot(reloc_root, 0, 1);
  1469. if (ret2 < 0) {
  1470. btrfs_put_root(reloc_root);
  1471. if (!ret)
  1472. ret = ret2;
  1473. }
  1474. }
  1475. btrfs_put_root(root);
  1476. } else {
  1477. /* Orphan reloc tree, just clean it up */
  1478. ret2 = btrfs_drop_snapshot(root, 0, 1);
  1479. if (ret2 < 0) {
  1480. btrfs_put_root(root);
  1481. if (!ret)
  1482. ret = ret2;
  1483. }
  1484. }
  1485. }
  1486. return ret;
  1487. }
  1488. /*
  1489. * merge the relocated tree blocks in reloc tree with corresponding
  1490. * fs tree.
  1491. */
  1492. static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
  1493. struct btrfs_root *root)
  1494. {
  1495. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  1496. struct btrfs_key key;
  1497. struct btrfs_key next_key;
  1498. struct btrfs_trans_handle *trans = NULL;
  1499. struct btrfs_root *reloc_root;
  1500. struct btrfs_root_item *root_item;
  1501. struct btrfs_path *path;
  1502. struct extent_buffer *leaf;
  1503. int reserve_level;
  1504. int level;
  1505. int max_level;
  1506. int replaced = 0;
  1507. int ret = 0;
  1508. u32 min_reserved;
  1509. path = btrfs_alloc_path();
  1510. if (!path)
  1511. return -ENOMEM;
  1512. path->reada = READA_FORWARD;
  1513. reloc_root = root->reloc_root;
  1514. root_item = &reloc_root->root_item;
  1515. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  1516. level = btrfs_root_level(root_item);
  1517. atomic_inc(&reloc_root->node->refs);
  1518. path->nodes[level] = reloc_root->node;
  1519. path->slots[level] = 0;
  1520. } else {
  1521. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  1522. level = btrfs_root_drop_level(root_item);
  1523. BUG_ON(level == 0);
  1524. path->lowest_level = level;
  1525. ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
  1526. path->lowest_level = 0;
  1527. if (ret < 0) {
  1528. btrfs_free_path(path);
  1529. return ret;
  1530. }
  1531. btrfs_node_key_to_cpu(path->nodes[level], &next_key,
  1532. path->slots[level]);
  1533. WARN_ON(memcmp(&key, &next_key, sizeof(key)));
  1534. btrfs_unlock_up_safe(path, 0);
  1535. }
  1536. /*
  1537. * In merge_reloc_root(), we modify the upper level pointer to swap the
  1538. * tree blocks between reloc tree and subvolume tree. Thus for tree
  1539. * block COW, we COW at most from level 1 to root level for each tree.
  1540. *
  1541. * Thus the needed metadata size is at most root_level * nodesize,
  1542. * and * 2 since we have two trees to COW.
  1543. */
  1544. reserve_level = max_t(int, 1, btrfs_root_level(root_item));
  1545. min_reserved = fs_info->nodesize * reserve_level * 2;
  1546. memset(&next_key, 0, sizeof(next_key));
  1547. while (1) {
  1548. ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv,
  1549. min_reserved,
  1550. BTRFS_RESERVE_FLUSH_LIMIT);
  1551. if (ret)
  1552. goto out;
  1553. trans = btrfs_start_transaction(root, 0);
  1554. if (IS_ERR(trans)) {
  1555. ret = PTR_ERR(trans);
  1556. trans = NULL;
  1557. goto out;
  1558. }
  1559. /*
  1560. * At this point we no longer have a reloc_control, so we can't
  1561. * depend on btrfs_init_reloc_root to update our last_trans.
  1562. *
  1563. * But that's ok, we started the trans handle on our
  1564. * corresponding fs_root, which means it's been added to the
  1565. * dirty list. At commit time we'll still call
  1566. * btrfs_update_reloc_root() and update our root item
  1567. * appropriately.
  1568. */
  1569. reloc_root->last_trans = trans->transid;
  1570. trans->block_rsv = rc->block_rsv;
  1571. replaced = 0;
  1572. max_level = level;
  1573. ret = walk_down_reloc_tree(reloc_root, path, &level);
  1574. if (ret < 0)
  1575. goto out;
  1576. if (ret > 0)
  1577. break;
  1578. if (!find_next_key(path, level, &key) &&
  1579. btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
  1580. ret = 0;
  1581. } else {
  1582. ret = replace_path(trans, rc, root, reloc_root, path,
  1583. &next_key, level, max_level);
  1584. }
  1585. if (ret < 0)
  1586. goto out;
  1587. if (ret > 0) {
  1588. level = ret;
  1589. btrfs_node_key_to_cpu(path->nodes[level], &key,
  1590. path->slots[level]);
  1591. replaced = 1;
  1592. }
  1593. ret = walk_up_reloc_tree(reloc_root, path, &level);
  1594. if (ret > 0)
  1595. break;
  1596. BUG_ON(level == 0);
  1597. /*
  1598. * save the merging progress in the drop_progress.
  1599. * this is OK since root refs == 1 in this case.
  1600. */
  1601. btrfs_node_key(path->nodes[level], &root_item->drop_progress,
  1602. path->slots[level]);
  1603. btrfs_set_root_drop_level(root_item, level);
  1604. btrfs_end_transaction_throttle(trans);
  1605. trans = NULL;
  1606. btrfs_btree_balance_dirty(fs_info);
  1607. if (replaced && rc->stage == UPDATE_DATA_PTRS)
  1608. invalidate_extent_cache(root, &key, &next_key);
  1609. }
  1610. /*
  1611. * handle the case only one block in the fs tree need to be
  1612. * relocated and the block is tree root.
  1613. */
  1614. leaf = btrfs_lock_root_node(root);
  1615. ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf,
  1616. BTRFS_NESTING_COW);
  1617. btrfs_tree_unlock(leaf);
  1618. free_extent_buffer(leaf);
  1619. out:
  1620. btrfs_free_path(path);
  1621. if (ret == 0) {
  1622. ret = insert_dirty_subvol(trans, rc, root);
  1623. if (ret)
  1624. btrfs_abort_transaction(trans, ret);
  1625. }
  1626. if (trans)
  1627. btrfs_end_transaction_throttle(trans);
  1628. btrfs_btree_balance_dirty(fs_info);
  1629. if (replaced && rc->stage == UPDATE_DATA_PTRS)
  1630. invalidate_extent_cache(root, &key, &next_key);
  1631. return ret;
  1632. }
  1633. static noinline_for_stack
  1634. int prepare_to_merge(struct reloc_control *rc, int err)
  1635. {
  1636. struct btrfs_root *root = rc->extent_root;
  1637. struct btrfs_fs_info *fs_info = root->fs_info;
  1638. struct btrfs_root *reloc_root;
  1639. struct btrfs_trans_handle *trans;
  1640. LIST_HEAD(reloc_roots);
  1641. u64 num_bytes = 0;
  1642. int ret;
  1643. mutex_lock(&fs_info->reloc_mutex);
  1644. rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
  1645. rc->merging_rsv_size += rc->nodes_relocated * 2;
  1646. mutex_unlock(&fs_info->reloc_mutex);
  1647. again:
  1648. if (!err) {
  1649. num_bytes = rc->merging_rsv_size;
  1650. ret = btrfs_block_rsv_add(fs_info, rc->block_rsv, num_bytes,
  1651. BTRFS_RESERVE_FLUSH_ALL);
  1652. if (ret)
  1653. err = ret;
  1654. }
  1655. trans = btrfs_join_transaction(rc->extent_root);
  1656. if (IS_ERR(trans)) {
  1657. if (!err)
  1658. btrfs_block_rsv_release(fs_info, rc->block_rsv,
  1659. num_bytes, NULL);
  1660. return PTR_ERR(trans);
  1661. }
  1662. if (!err) {
  1663. if (num_bytes != rc->merging_rsv_size) {
  1664. btrfs_end_transaction(trans);
  1665. btrfs_block_rsv_release(fs_info, rc->block_rsv,
  1666. num_bytes, NULL);
  1667. goto again;
  1668. }
  1669. }
  1670. rc->merge_reloc_tree = 1;
  1671. while (!list_empty(&rc->reloc_roots)) {
  1672. reloc_root = list_entry(rc->reloc_roots.next,
  1673. struct btrfs_root, root_list);
  1674. list_del_init(&reloc_root->root_list);
  1675. root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
  1676. false);
  1677. if (IS_ERR(root)) {
  1678. /*
  1679. * Even if we have an error we need this reloc root
  1680. * back on our list so we can clean up properly.
  1681. */
  1682. list_add(&reloc_root->root_list, &reloc_roots);
  1683. btrfs_abort_transaction(trans, (int)PTR_ERR(root));
  1684. if (!err)
  1685. err = PTR_ERR(root);
  1686. break;
  1687. }
  1688. if (unlikely(root->reloc_root != reloc_root)) {
  1689. if (root->reloc_root) {
  1690. btrfs_err(fs_info,
  1691. "reloc tree mismatch, root %lld has reloc root key (%lld %u %llu) gen %llu, expect reloc root key (%lld %u %llu) gen %llu",
  1692. root->root_key.objectid,
  1693. root->reloc_root->root_key.objectid,
  1694. root->reloc_root->root_key.type,
  1695. root->reloc_root->root_key.offset,
  1696. btrfs_root_generation(
  1697. &root->reloc_root->root_item),
  1698. reloc_root->root_key.objectid,
  1699. reloc_root->root_key.type,
  1700. reloc_root->root_key.offset,
  1701. btrfs_root_generation(
  1702. &reloc_root->root_item));
  1703. } else {
  1704. btrfs_err(fs_info,
  1705. "reloc tree mismatch, root %lld has no reloc root, expect reloc root key (%lld %u %llu) gen %llu",
  1706. root->root_key.objectid,
  1707. reloc_root->root_key.objectid,
  1708. reloc_root->root_key.type,
  1709. reloc_root->root_key.offset,
  1710. btrfs_root_generation(
  1711. &reloc_root->root_item));
  1712. }
  1713. list_add(&reloc_root->root_list, &reloc_roots);
  1714. btrfs_put_root(root);
  1715. btrfs_abort_transaction(trans, -EUCLEAN);
  1716. if (!err)
  1717. err = -EUCLEAN;
  1718. break;
  1719. }
  1720. /*
  1721. * set reference count to 1, so btrfs_recover_relocation
  1722. * knows it should resumes merging
  1723. */
  1724. if (!err)
  1725. btrfs_set_root_refs(&reloc_root->root_item, 1);
  1726. ret = btrfs_update_reloc_root(trans, root);
  1727. /*
  1728. * Even if we have an error we need this reloc root back on our
  1729. * list so we can clean up properly.
  1730. */
  1731. list_add(&reloc_root->root_list, &reloc_roots);
  1732. btrfs_put_root(root);
  1733. if (ret) {
  1734. btrfs_abort_transaction(trans, ret);
  1735. if (!err)
  1736. err = ret;
  1737. break;
  1738. }
  1739. }
  1740. list_splice(&reloc_roots, &rc->reloc_roots);
  1741. if (!err)
  1742. err = btrfs_commit_transaction(trans);
  1743. else
  1744. btrfs_end_transaction(trans);
  1745. return err;
  1746. }
  1747. static noinline_for_stack
  1748. void free_reloc_roots(struct list_head *list)
  1749. {
  1750. struct btrfs_root *reloc_root, *tmp;
  1751. list_for_each_entry_safe(reloc_root, tmp, list, root_list)
  1752. __del_reloc_root(reloc_root);
  1753. }
  1754. static noinline_for_stack
  1755. void merge_reloc_roots(struct reloc_control *rc)
  1756. {
  1757. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  1758. struct btrfs_root *root;
  1759. struct btrfs_root *reloc_root;
  1760. LIST_HEAD(reloc_roots);
  1761. int found = 0;
  1762. int ret = 0;
  1763. again:
  1764. root = rc->extent_root;
  1765. /*
  1766. * this serializes us with btrfs_record_root_in_transaction,
  1767. * we have to make sure nobody is in the middle of
  1768. * adding their roots to the list while we are
  1769. * doing this splice
  1770. */
  1771. mutex_lock(&fs_info->reloc_mutex);
  1772. list_splice_init(&rc->reloc_roots, &reloc_roots);
  1773. mutex_unlock(&fs_info->reloc_mutex);
  1774. while (!list_empty(&reloc_roots)) {
  1775. found = 1;
  1776. reloc_root = list_entry(reloc_roots.next,
  1777. struct btrfs_root, root_list);
  1778. root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
  1779. false);
  1780. if (btrfs_root_refs(&reloc_root->root_item) > 0) {
  1781. if (WARN_ON(IS_ERR(root))) {
  1782. /*
  1783. * For recovery we read the fs roots on mount,
  1784. * and if we didn't find the root then we marked
  1785. * the reloc root as a garbage root. For normal
  1786. * relocation obviously the root should exist in
  1787. * memory. However there's no reason we can't
  1788. * handle the error properly here just in case.
  1789. */
  1790. ret = PTR_ERR(root);
  1791. goto out;
  1792. }
  1793. if (WARN_ON(root->reloc_root != reloc_root)) {
  1794. /*
  1795. * This can happen if on-disk metadata has some
  1796. * corruption, e.g. bad reloc tree key offset.
  1797. */
  1798. ret = -EINVAL;
  1799. goto out;
  1800. }
  1801. ret = merge_reloc_root(rc, root);
  1802. btrfs_put_root(root);
  1803. if (ret) {
  1804. if (list_empty(&reloc_root->root_list))
  1805. list_add_tail(&reloc_root->root_list,
  1806. &reloc_roots);
  1807. goto out;
  1808. }
  1809. } else {
  1810. if (!IS_ERR(root)) {
  1811. if (root->reloc_root == reloc_root) {
  1812. root->reloc_root = NULL;
  1813. btrfs_put_root(reloc_root);
  1814. }
  1815. clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE,
  1816. &root->state);
  1817. btrfs_put_root(root);
  1818. }
  1819. list_del_init(&reloc_root->root_list);
  1820. /* Don't forget to queue this reloc root for cleanup */
  1821. list_add_tail(&reloc_root->reloc_dirty_list,
  1822. &rc->dirty_subvol_roots);
  1823. }
  1824. }
  1825. if (found) {
  1826. found = 0;
  1827. goto again;
  1828. }
  1829. out:
  1830. if (ret) {
  1831. btrfs_handle_fs_error(fs_info, ret, NULL);
  1832. free_reloc_roots(&reloc_roots);
  1833. /* new reloc root may be added */
  1834. mutex_lock(&fs_info->reloc_mutex);
  1835. list_splice_init(&rc->reloc_roots, &reloc_roots);
  1836. mutex_unlock(&fs_info->reloc_mutex);
  1837. free_reloc_roots(&reloc_roots);
  1838. }
  1839. /*
  1840. * We used to have
  1841. *
  1842. * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
  1843. *
  1844. * here, but it's wrong. If we fail to start the transaction in
  1845. * prepare_to_merge() we will have only 0 ref reloc roots, none of which
  1846. * have actually been removed from the reloc_root_tree rb tree. This is
  1847. * fine because we're bailing here, and we hold a reference on the root
  1848. * for the list that holds it, so these roots will be cleaned up when we
  1849. * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root
  1850. * will be cleaned up on unmount.
  1851. *
  1852. * The remaining nodes will be cleaned up by free_reloc_control.
  1853. */
  1854. }
  1855. static void free_block_list(struct rb_root *blocks)
  1856. {
  1857. struct tree_block *block;
  1858. struct rb_node *rb_node;
  1859. while ((rb_node = rb_first(blocks))) {
  1860. block = rb_entry(rb_node, struct tree_block, rb_node);
  1861. rb_erase(rb_node, blocks);
  1862. kfree(block);
  1863. }
  1864. }
  1865. static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
  1866. struct btrfs_root *reloc_root)
  1867. {
  1868. struct btrfs_fs_info *fs_info = reloc_root->fs_info;
  1869. struct btrfs_root *root;
  1870. int ret;
  1871. if (reloc_root->last_trans == trans->transid)
  1872. return 0;
  1873. root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false);
  1874. /*
  1875. * This should succeed, since we can't have a reloc root without having
  1876. * already looked up the actual root and created the reloc root for this
  1877. * root.
  1878. *
  1879. * However if there's some sort of corruption where we have a ref to a
  1880. * reloc root without a corresponding root this could return ENOENT.
  1881. */
  1882. if (IS_ERR(root)) {
  1883. ASSERT(0);
  1884. return PTR_ERR(root);
  1885. }
  1886. if (root->reloc_root != reloc_root) {
  1887. ASSERT(0);
  1888. btrfs_err(fs_info,
  1889. "root %llu has two reloc roots associated with it",
  1890. reloc_root->root_key.offset);
  1891. btrfs_put_root(root);
  1892. return -EUCLEAN;
  1893. }
  1894. ret = btrfs_record_root_in_trans(trans, root);
  1895. btrfs_put_root(root);
  1896. return ret;
  1897. }
  1898. static noinline_for_stack
  1899. struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
  1900. struct reloc_control *rc,
  1901. struct btrfs_backref_node *node,
  1902. struct btrfs_backref_edge *edges[])
  1903. {
  1904. struct btrfs_backref_node *next;
  1905. struct btrfs_root *root;
  1906. int index = 0;
  1907. int ret;
  1908. next = node;
  1909. while (1) {
  1910. cond_resched();
  1911. next = walk_up_backref(next, edges, &index);
  1912. root = next->root;
  1913. /*
  1914. * If there is no root, then our references for this block are
  1915. * incomplete, as we should be able to walk all the way up to a
  1916. * block that is owned by a root.
  1917. *
  1918. * This path is only for SHAREABLE roots, so if we come upon a
  1919. * non-SHAREABLE root then we have backrefs that resolve
  1920. * improperly.
  1921. *
  1922. * Both of these cases indicate file system corruption, or a bug
  1923. * in the backref walking code.
  1924. */
  1925. if (!root) {
  1926. ASSERT(0);
  1927. btrfs_err(trans->fs_info,
  1928. "bytenr %llu doesn't have a backref path ending in a root",
  1929. node->bytenr);
  1930. return ERR_PTR(-EUCLEAN);
  1931. }
  1932. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
  1933. ASSERT(0);
  1934. btrfs_err(trans->fs_info,
  1935. "bytenr %llu has multiple refs with one ending in a non-shareable root",
  1936. node->bytenr);
  1937. return ERR_PTR(-EUCLEAN);
  1938. }
  1939. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
  1940. ret = record_reloc_root_in_trans(trans, root);
  1941. if (ret)
  1942. return ERR_PTR(ret);
  1943. break;
  1944. }
  1945. ret = btrfs_record_root_in_trans(trans, root);
  1946. if (ret)
  1947. return ERR_PTR(ret);
  1948. root = root->reloc_root;
  1949. /*
  1950. * We could have raced with another thread which failed, so
  1951. * root->reloc_root may not be set, return ENOENT in this case.
  1952. */
  1953. if (!root)
  1954. return ERR_PTR(-ENOENT);
  1955. if (next->new_bytenr != root->node->start) {
  1956. /*
  1957. * We just created the reloc root, so we shouldn't have
  1958. * ->new_bytenr set and this shouldn't be in the changed
  1959. * list. If it is then we have multiple roots pointing
  1960. * at the same bytenr which indicates corruption, or
  1961. * we've made a mistake in the backref walking code.
  1962. */
  1963. ASSERT(next->new_bytenr == 0);
  1964. ASSERT(list_empty(&next->list));
  1965. if (next->new_bytenr || !list_empty(&next->list)) {
  1966. btrfs_err(trans->fs_info,
  1967. "bytenr %llu possibly has multiple roots pointing at the same bytenr %llu",
  1968. node->bytenr, next->bytenr);
  1969. return ERR_PTR(-EUCLEAN);
  1970. }
  1971. next->new_bytenr = root->node->start;
  1972. btrfs_put_root(next->root);
  1973. next->root = btrfs_grab_root(root);
  1974. ASSERT(next->root);
  1975. list_add_tail(&next->list,
  1976. &rc->backref_cache.changed);
  1977. mark_block_processed(rc, next);
  1978. break;
  1979. }
  1980. WARN_ON(1);
  1981. root = NULL;
  1982. next = walk_down_backref(edges, &index);
  1983. if (!next || next->level <= node->level)
  1984. break;
  1985. }
  1986. if (!root) {
  1987. /*
  1988. * This can happen if there's fs corruption or if there's a bug
  1989. * in the backref lookup code.
  1990. */
  1991. ASSERT(0);
  1992. return ERR_PTR(-ENOENT);
  1993. }
  1994. next = node;
  1995. /* setup backref node path for btrfs_reloc_cow_block */
  1996. while (1) {
  1997. rc->backref_cache.path[next->level] = next;
  1998. if (--index < 0)
  1999. break;
  2000. next = edges[index]->node[UPPER];
  2001. }
  2002. return root;
  2003. }
  2004. /*
  2005. * Select a tree root for relocation.
  2006. *
  2007. * Return NULL if the block is not shareable. We should use do_relocation() in
  2008. * this case.
  2009. *
  2010. * Return a tree root pointer if the block is shareable.
  2011. * Return -ENOENT if the block is root of reloc tree.
  2012. */
  2013. static noinline_for_stack
  2014. struct btrfs_root *select_one_root(struct btrfs_backref_node *node)
  2015. {
  2016. struct btrfs_backref_node *next;
  2017. struct btrfs_root *root;
  2018. struct btrfs_root *fs_root = NULL;
  2019. struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  2020. int index = 0;
  2021. next = node;
  2022. while (1) {
  2023. cond_resched();
  2024. next = walk_up_backref(next, edges, &index);
  2025. root = next->root;
  2026. /*
  2027. * This can occur if we have incomplete extent refs leading all
  2028. * the way up a particular path, in this case return -EUCLEAN.
  2029. */
  2030. if (!root)
  2031. return ERR_PTR(-EUCLEAN);
  2032. /* No other choice for non-shareable tree */
  2033. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  2034. return root;
  2035. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
  2036. fs_root = root;
  2037. if (next != node)
  2038. return NULL;
  2039. next = walk_down_backref(edges, &index);
  2040. if (!next || next->level <= node->level)
  2041. break;
  2042. }
  2043. if (!fs_root)
  2044. return ERR_PTR(-ENOENT);
  2045. return fs_root;
  2046. }
  2047. static noinline_for_stack
  2048. u64 calcu_metadata_size(struct reloc_control *rc,
  2049. struct btrfs_backref_node *node, int reserve)
  2050. {
  2051. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2052. struct btrfs_backref_node *next = node;
  2053. struct btrfs_backref_edge *edge;
  2054. struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  2055. u64 num_bytes = 0;
  2056. int index = 0;
  2057. BUG_ON(reserve && node->processed);
  2058. while (next) {
  2059. cond_resched();
  2060. while (1) {
  2061. if (next->processed && (reserve || next != node))
  2062. break;
  2063. num_bytes += fs_info->nodesize;
  2064. if (list_empty(&next->upper))
  2065. break;
  2066. edge = list_entry(next->upper.next,
  2067. struct btrfs_backref_edge, list[LOWER]);
  2068. edges[index++] = edge;
  2069. next = edge->node[UPPER];
  2070. }
  2071. next = walk_down_backref(edges, &index);
  2072. }
  2073. return num_bytes;
  2074. }
  2075. static int reserve_metadata_space(struct btrfs_trans_handle *trans,
  2076. struct reloc_control *rc,
  2077. struct btrfs_backref_node *node)
  2078. {
  2079. struct btrfs_root *root = rc->extent_root;
  2080. struct btrfs_fs_info *fs_info = root->fs_info;
  2081. u64 num_bytes;
  2082. int ret;
  2083. u64 tmp;
  2084. num_bytes = calcu_metadata_size(rc, node, 1) * 2;
  2085. trans->block_rsv = rc->block_rsv;
  2086. rc->reserved_bytes += num_bytes;
  2087. /*
  2088. * We are under a transaction here so we can only do limited flushing.
  2089. * If we get an enospc just kick back -EAGAIN so we know to drop the
  2090. * transaction and try to refill when we can flush all the things.
  2091. */
  2092. ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv, num_bytes,
  2093. BTRFS_RESERVE_FLUSH_LIMIT);
  2094. if (ret) {
  2095. tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES;
  2096. while (tmp <= rc->reserved_bytes)
  2097. tmp <<= 1;
  2098. /*
  2099. * only one thread can access block_rsv at this point,
  2100. * so we don't need hold lock to protect block_rsv.
  2101. * we expand more reservation size here to allow enough
  2102. * space for relocation and we will return earlier in
  2103. * enospc case.
  2104. */
  2105. rc->block_rsv->size = tmp + fs_info->nodesize *
  2106. RELOCATION_RESERVED_NODES;
  2107. return -EAGAIN;
  2108. }
  2109. return 0;
  2110. }
  2111. /*
  2112. * relocate a block tree, and then update pointers in upper level
  2113. * blocks that reference the block to point to the new location.
  2114. *
  2115. * if called by link_to_upper, the block has already been relocated.
  2116. * in that case this function just updates pointers.
  2117. */
  2118. static int do_relocation(struct btrfs_trans_handle *trans,
  2119. struct reloc_control *rc,
  2120. struct btrfs_backref_node *node,
  2121. struct btrfs_key *key,
  2122. struct btrfs_path *path, int lowest)
  2123. {
  2124. struct btrfs_backref_node *upper;
  2125. struct btrfs_backref_edge *edge;
  2126. struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  2127. struct btrfs_root *root;
  2128. struct extent_buffer *eb;
  2129. u32 blocksize;
  2130. u64 bytenr;
  2131. int slot;
  2132. int ret = 0;
  2133. /*
  2134. * If we are lowest then this is the first time we're processing this
  2135. * block, and thus shouldn't have an eb associated with it yet.
  2136. */
  2137. ASSERT(!lowest || !node->eb);
  2138. path->lowest_level = node->level + 1;
  2139. rc->backref_cache.path[node->level] = node;
  2140. list_for_each_entry(edge, &node->upper, list[LOWER]) {
  2141. struct btrfs_ref ref = { 0 };
  2142. cond_resched();
  2143. upper = edge->node[UPPER];
  2144. root = select_reloc_root(trans, rc, upper, edges);
  2145. if (IS_ERR(root)) {
  2146. ret = PTR_ERR(root);
  2147. goto next;
  2148. }
  2149. if (upper->eb && !upper->locked) {
  2150. if (!lowest) {
  2151. ret = btrfs_bin_search(upper->eb, key, &slot);
  2152. if (ret < 0)
  2153. goto next;
  2154. BUG_ON(ret);
  2155. bytenr = btrfs_node_blockptr(upper->eb, slot);
  2156. if (node->eb->start == bytenr)
  2157. goto next;
  2158. }
  2159. btrfs_backref_drop_node_buffer(upper);
  2160. }
  2161. if (!upper->eb) {
  2162. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  2163. if (ret) {
  2164. if (ret > 0)
  2165. ret = -ENOENT;
  2166. btrfs_release_path(path);
  2167. break;
  2168. }
  2169. if (!upper->eb) {
  2170. upper->eb = path->nodes[upper->level];
  2171. path->nodes[upper->level] = NULL;
  2172. } else {
  2173. BUG_ON(upper->eb != path->nodes[upper->level]);
  2174. }
  2175. upper->locked = 1;
  2176. path->locks[upper->level] = 0;
  2177. slot = path->slots[upper->level];
  2178. btrfs_release_path(path);
  2179. } else {
  2180. ret = btrfs_bin_search(upper->eb, key, &slot);
  2181. if (ret < 0)
  2182. goto next;
  2183. BUG_ON(ret);
  2184. }
  2185. bytenr = btrfs_node_blockptr(upper->eb, slot);
  2186. if (lowest) {
  2187. if (bytenr != node->bytenr) {
  2188. btrfs_err(root->fs_info,
  2189. "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
  2190. bytenr, node->bytenr, slot,
  2191. upper->eb->start);
  2192. ret = -EIO;
  2193. goto next;
  2194. }
  2195. } else {
  2196. if (node->eb->start == bytenr)
  2197. goto next;
  2198. }
  2199. blocksize = root->fs_info->nodesize;
  2200. eb = btrfs_read_node_slot(upper->eb, slot);
  2201. if (IS_ERR(eb)) {
  2202. ret = PTR_ERR(eb);
  2203. goto next;
  2204. }
  2205. btrfs_tree_lock(eb);
  2206. if (!node->eb) {
  2207. ret = btrfs_cow_block(trans, root, eb, upper->eb,
  2208. slot, &eb, BTRFS_NESTING_COW);
  2209. btrfs_tree_unlock(eb);
  2210. free_extent_buffer(eb);
  2211. if (ret < 0)
  2212. goto next;
  2213. /*
  2214. * We've just COWed this block, it should have updated
  2215. * the correct backref node entry.
  2216. */
  2217. ASSERT(node->eb == eb);
  2218. } else {
  2219. btrfs_set_node_blockptr(upper->eb, slot,
  2220. node->eb->start);
  2221. btrfs_set_node_ptr_generation(upper->eb, slot,
  2222. trans->transid);
  2223. btrfs_mark_buffer_dirty(upper->eb);
  2224. btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
  2225. node->eb->start, blocksize,
  2226. upper->eb->start);
  2227. btrfs_init_tree_ref(&ref, node->level,
  2228. btrfs_header_owner(upper->eb),
  2229. root->root_key.objectid, false);
  2230. ret = btrfs_inc_extent_ref(trans, &ref);
  2231. if (!ret)
  2232. ret = btrfs_drop_subtree(trans, root, eb,
  2233. upper->eb);
  2234. if (ret)
  2235. btrfs_abort_transaction(trans, ret);
  2236. }
  2237. next:
  2238. if (!upper->pending)
  2239. btrfs_backref_drop_node_buffer(upper);
  2240. else
  2241. btrfs_backref_unlock_node_buffer(upper);
  2242. if (ret)
  2243. break;
  2244. }
  2245. if (!ret && node->pending) {
  2246. btrfs_backref_drop_node_buffer(node);
  2247. list_move_tail(&node->list, &rc->backref_cache.changed);
  2248. node->pending = 0;
  2249. }
  2250. path->lowest_level = 0;
  2251. /*
  2252. * We should have allocated all of our space in the block rsv and thus
  2253. * shouldn't ENOSPC.
  2254. */
  2255. ASSERT(ret != -ENOSPC);
  2256. return ret;
  2257. }
  2258. static int link_to_upper(struct btrfs_trans_handle *trans,
  2259. struct reloc_control *rc,
  2260. struct btrfs_backref_node *node,
  2261. struct btrfs_path *path)
  2262. {
  2263. struct btrfs_key key;
  2264. btrfs_node_key_to_cpu(node->eb, &key, 0);
  2265. return do_relocation(trans, rc, node, &key, path, 0);
  2266. }
  2267. static int finish_pending_nodes(struct btrfs_trans_handle *trans,
  2268. struct reloc_control *rc,
  2269. struct btrfs_path *path, int err)
  2270. {
  2271. LIST_HEAD(list);
  2272. struct btrfs_backref_cache *cache = &rc->backref_cache;
  2273. struct btrfs_backref_node *node;
  2274. int level;
  2275. int ret;
  2276. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  2277. while (!list_empty(&cache->pending[level])) {
  2278. node = list_entry(cache->pending[level].next,
  2279. struct btrfs_backref_node, list);
  2280. list_move_tail(&node->list, &list);
  2281. BUG_ON(!node->pending);
  2282. if (!err) {
  2283. ret = link_to_upper(trans, rc, node, path);
  2284. if (ret < 0)
  2285. err = ret;
  2286. }
  2287. }
  2288. list_splice_init(&list, &cache->pending[level]);
  2289. }
  2290. return err;
  2291. }
  2292. /*
  2293. * mark a block and all blocks directly/indirectly reference the block
  2294. * as processed.
  2295. */
  2296. static void update_processed_blocks(struct reloc_control *rc,
  2297. struct btrfs_backref_node *node)
  2298. {
  2299. struct btrfs_backref_node *next = node;
  2300. struct btrfs_backref_edge *edge;
  2301. struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  2302. int index = 0;
  2303. while (next) {
  2304. cond_resched();
  2305. while (1) {
  2306. if (next->processed)
  2307. break;
  2308. mark_block_processed(rc, next);
  2309. if (list_empty(&next->upper))
  2310. break;
  2311. edge = list_entry(next->upper.next,
  2312. struct btrfs_backref_edge, list[LOWER]);
  2313. edges[index++] = edge;
  2314. next = edge->node[UPPER];
  2315. }
  2316. next = walk_down_backref(edges, &index);
  2317. }
  2318. }
  2319. static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
  2320. {
  2321. u32 blocksize = rc->extent_root->fs_info->nodesize;
  2322. if (test_range_bit(&rc->processed_blocks, bytenr,
  2323. bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
  2324. return 1;
  2325. return 0;
  2326. }
  2327. static int get_tree_block_key(struct btrfs_fs_info *fs_info,
  2328. struct tree_block *block)
  2329. {
  2330. struct extent_buffer *eb;
  2331. eb = read_tree_block(fs_info, block->bytenr, block->owner,
  2332. block->key.offset, block->level, NULL);
  2333. if (IS_ERR(eb))
  2334. return PTR_ERR(eb);
  2335. if (!extent_buffer_uptodate(eb)) {
  2336. free_extent_buffer(eb);
  2337. return -EIO;
  2338. }
  2339. if (block->level == 0)
  2340. btrfs_item_key_to_cpu(eb, &block->key, 0);
  2341. else
  2342. btrfs_node_key_to_cpu(eb, &block->key, 0);
  2343. free_extent_buffer(eb);
  2344. block->key_ready = 1;
  2345. return 0;
  2346. }
  2347. /*
  2348. * helper function to relocate a tree block
  2349. */
  2350. static int relocate_tree_block(struct btrfs_trans_handle *trans,
  2351. struct reloc_control *rc,
  2352. struct btrfs_backref_node *node,
  2353. struct btrfs_key *key,
  2354. struct btrfs_path *path)
  2355. {
  2356. struct btrfs_root *root;
  2357. int ret = 0;
  2358. if (!node)
  2359. return 0;
  2360. /*
  2361. * If we fail here we want to drop our backref_node because we are going
  2362. * to start over and regenerate the tree for it.
  2363. */
  2364. ret = reserve_metadata_space(trans, rc, node);
  2365. if (ret)
  2366. goto out;
  2367. BUG_ON(node->processed);
  2368. root = select_one_root(node);
  2369. if (IS_ERR(root)) {
  2370. ret = PTR_ERR(root);
  2371. /* See explanation in select_one_root for the -EUCLEAN case. */
  2372. ASSERT(ret == -ENOENT);
  2373. if (ret == -ENOENT) {
  2374. ret = 0;
  2375. update_processed_blocks(rc, node);
  2376. }
  2377. goto out;
  2378. }
  2379. if (root) {
  2380. if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
  2381. /*
  2382. * This block was the root block of a root, and this is
  2383. * the first time we're processing the block and thus it
  2384. * should not have had the ->new_bytenr modified and
  2385. * should have not been included on the changed list.
  2386. *
  2387. * However in the case of corruption we could have
  2388. * multiple refs pointing to the same block improperly,
  2389. * and thus we would trip over these checks. ASSERT()
  2390. * for the developer case, because it could indicate a
  2391. * bug in the backref code, however error out for a
  2392. * normal user in the case of corruption.
  2393. */
  2394. ASSERT(node->new_bytenr == 0);
  2395. ASSERT(list_empty(&node->list));
  2396. if (node->new_bytenr || !list_empty(&node->list)) {
  2397. btrfs_err(root->fs_info,
  2398. "bytenr %llu has improper references to it",
  2399. node->bytenr);
  2400. ret = -EUCLEAN;
  2401. goto out;
  2402. }
  2403. ret = btrfs_record_root_in_trans(trans, root);
  2404. if (ret)
  2405. goto out;
  2406. /*
  2407. * Another thread could have failed, need to check if we
  2408. * have reloc_root actually set.
  2409. */
  2410. if (!root->reloc_root) {
  2411. ret = -ENOENT;
  2412. goto out;
  2413. }
  2414. root = root->reloc_root;
  2415. node->new_bytenr = root->node->start;
  2416. btrfs_put_root(node->root);
  2417. node->root = btrfs_grab_root(root);
  2418. ASSERT(node->root);
  2419. list_add_tail(&node->list, &rc->backref_cache.changed);
  2420. } else {
  2421. path->lowest_level = node->level;
  2422. if (root == root->fs_info->chunk_root)
  2423. btrfs_reserve_chunk_metadata(trans, false);
  2424. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  2425. btrfs_release_path(path);
  2426. if (root == root->fs_info->chunk_root)
  2427. btrfs_trans_release_chunk_metadata(trans);
  2428. if (ret > 0)
  2429. ret = 0;
  2430. }
  2431. if (!ret)
  2432. update_processed_blocks(rc, node);
  2433. } else {
  2434. ret = do_relocation(trans, rc, node, key, path, 1);
  2435. }
  2436. out:
  2437. if (ret || node->level == 0 || node->cowonly)
  2438. btrfs_backref_cleanup_node(&rc->backref_cache, node);
  2439. return ret;
  2440. }
  2441. /*
  2442. * relocate a list of blocks
  2443. */
  2444. static noinline_for_stack
  2445. int relocate_tree_blocks(struct btrfs_trans_handle *trans,
  2446. struct reloc_control *rc, struct rb_root *blocks)
  2447. {
  2448. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2449. struct btrfs_backref_node *node;
  2450. struct btrfs_path *path;
  2451. struct tree_block *block;
  2452. struct tree_block *next;
  2453. int ret;
  2454. int err = 0;
  2455. path = btrfs_alloc_path();
  2456. if (!path) {
  2457. err = -ENOMEM;
  2458. goto out_free_blocks;
  2459. }
  2460. /* Kick in readahead for tree blocks with missing keys */
  2461. rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
  2462. if (!block->key_ready)
  2463. btrfs_readahead_tree_block(fs_info, block->bytenr,
  2464. block->owner, 0,
  2465. block->level);
  2466. }
  2467. /* Get first keys */
  2468. rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
  2469. if (!block->key_ready) {
  2470. err = get_tree_block_key(fs_info, block);
  2471. if (err)
  2472. goto out_free_path;
  2473. }
  2474. }
  2475. /* Do tree relocation */
  2476. rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
  2477. node = build_backref_tree(rc, &block->key,
  2478. block->level, block->bytenr);
  2479. if (IS_ERR(node)) {
  2480. err = PTR_ERR(node);
  2481. goto out;
  2482. }
  2483. ret = relocate_tree_block(trans, rc, node, &block->key,
  2484. path);
  2485. if (ret < 0) {
  2486. err = ret;
  2487. break;
  2488. }
  2489. }
  2490. out:
  2491. err = finish_pending_nodes(trans, rc, path, err);
  2492. out_free_path:
  2493. btrfs_free_path(path);
  2494. out_free_blocks:
  2495. free_block_list(blocks);
  2496. return err;
  2497. }
  2498. static noinline_for_stack int prealloc_file_extent_cluster(
  2499. struct btrfs_inode *inode,
  2500. struct file_extent_cluster *cluster)
  2501. {
  2502. u64 alloc_hint = 0;
  2503. u64 start;
  2504. u64 end;
  2505. u64 offset = inode->index_cnt;
  2506. u64 num_bytes;
  2507. int nr;
  2508. int ret = 0;
  2509. u64 i_size = i_size_read(&inode->vfs_inode);
  2510. u64 prealloc_start = cluster->start - offset;
  2511. u64 prealloc_end = cluster->end - offset;
  2512. u64 cur_offset = prealloc_start;
  2513. /*
  2514. * For subpage case, previous i_size may not be aligned to PAGE_SIZE.
  2515. * This means the range [i_size, PAGE_END + 1) is filled with zeros by
  2516. * btrfs_do_readpage() call of previously relocated file cluster.
  2517. *
  2518. * If the current cluster starts in the above range, btrfs_do_readpage()
  2519. * will skip the read, and relocate_one_page() will later writeback
  2520. * the padding zeros as new data, causing data corruption.
  2521. *
  2522. * Here we have to manually invalidate the range (i_size, PAGE_END + 1).
  2523. */
  2524. if (!IS_ALIGNED(i_size, PAGE_SIZE)) {
  2525. struct address_space *mapping = inode->vfs_inode.i_mapping;
  2526. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  2527. const u32 sectorsize = fs_info->sectorsize;
  2528. struct page *page;
  2529. ASSERT(sectorsize < PAGE_SIZE);
  2530. ASSERT(IS_ALIGNED(i_size, sectorsize));
  2531. /*
  2532. * Subpage can't handle page with DIRTY but without UPTODATE
  2533. * bit as it can lead to the following deadlock:
  2534. *
  2535. * btrfs_read_folio()
  2536. * | Page already *locked*
  2537. * |- btrfs_lock_and_flush_ordered_range()
  2538. * |- btrfs_start_ordered_extent()
  2539. * |- extent_write_cache_pages()
  2540. * |- lock_page()
  2541. * We try to lock the page we already hold.
  2542. *
  2543. * Here we just writeback the whole data reloc inode, so that
  2544. * we will be ensured to have no dirty range in the page, and
  2545. * are safe to clear the uptodate bits.
  2546. *
  2547. * This shouldn't cause too much overhead, as we need to write
  2548. * the data back anyway.
  2549. */
  2550. ret = filemap_write_and_wait(mapping);
  2551. if (ret < 0)
  2552. return ret;
  2553. clear_extent_bits(&inode->io_tree, i_size,
  2554. round_up(i_size, PAGE_SIZE) - 1,
  2555. EXTENT_UPTODATE);
  2556. page = find_lock_page(mapping, i_size >> PAGE_SHIFT);
  2557. /*
  2558. * If page is freed we don't need to do anything then, as we
  2559. * will re-read the whole page anyway.
  2560. */
  2561. if (page) {
  2562. btrfs_subpage_clear_uptodate(fs_info, page, i_size,
  2563. round_up(i_size, PAGE_SIZE) - i_size);
  2564. unlock_page(page);
  2565. put_page(page);
  2566. }
  2567. }
  2568. BUG_ON(cluster->start != cluster->boundary[0]);
  2569. ret = btrfs_alloc_data_chunk_ondemand(inode,
  2570. prealloc_end + 1 - prealloc_start);
  2571. if (ret)
  2572. return ret;
  2573. btrfs_inode_lock(&inode->vfs_inode, 0);
  2574. for (nr = 0; nr < cluster->nr; nr++) {
  2575. start = cluster->boundary[nr] - offset;
  2576. if (nr + 1 < cluster->nr)
  2577. end = cluster->boundary[nr + 1] - 1 - offset;
  2578. else
  2579. end = cluster->end - offset;
  2580. lock_extent(&inode->io_tree, start, end, NULL);
  2581. num_bytes = end + 1 - start;
  2582. ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start,
  2583. num_bytes, num_bytes,
  2584. end + 1, &alloc_hint);
  2585. cur_offset = end + 1;
  2586. unlock_extent(&inode->io_tree, start, end, NULL);
  2587. if (ret)
  2588. break;
  2589. }
  2590. btrfs_inode_unlock(&inode->vfs_inode, 0);
  2591. if (cur_offset < prealloc_end)
  2592. btrfs_free_reserved_data_space_noquota(inode->root->fs_info,
  2593. prealloc_end + 1 - cur_offset);
  2594. return ret;
  2595. }
  2596. static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inode,
  2597. u64 start, u64 end, u64 block_start)
  2598. {
  2599. struct extent_map *em;
  2600. int ret = 0;
  2601. em = alloc_extent_map();
  2602. if (!em)
  2603. return -ENOMEM;
  2604. em->start = start;
  2605. em->len = end + 1 - start;
  2606. em->block_len = em->len;
  2607. em->block_start = block_start;
  2608. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  2609. lock_extent(&BTRFS_I(inode)->io_tree, start, end, NULL);
  2610. ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, false);
  2611. unlock_extent(&BTRFS_I(inode)->io_tree, start, end, NULL);
  2612. free_extent_map(em);
  2613. return ret;
  2614. }
  2615. /*
  2616. * Allow error injection to test balance/relocation cancellation
  2617. */
  2618. noinline int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)
  2619. {
  2620. return atomic_read(&fs_info->balance_cancel_req) ||
  2621. atomic_read(&fs_info->reloc_cancel_req) ||
  2622. fatal_signal_pending(current);
  2623. }
  2624. ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
  2625. static u64 get_cluster_boundary_end(struct file_extent_cluster *cluster,
  2626. int cluster_nr)
  2627. {
  2628. /* Last extent, use cluster end directly */
  2629. if (cluster_nr >= cluster->nr - 1)
  2630. return cluster->end;
  2631. /* Use next boundary start*/
  2632. return cluster->boundary[cluster_nr + 1] - 1;
  2633. }
  2634. static int relocate_one_page(struct inode *inode, struct file_ra_state *ra,
  2635. struct file_extent_cluster *cluster,
  2636. int *cluster_nr, unsigned long page_index)
  2637. {
  2638. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2639. u64 offset = BTRFS_I(inode)->index_cnt;
  2640. const unsigned long last_index = (cluster->end - offset) >> PAGE_SHIFT;
  2641. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  2642. struct page *page;
  2643. u64 page_start;
  2644. u64 page_end;
  2645. u64 cur;
  2646. int ret;
  2647. ASSERT(page_index <= last_index);
  2648. page = find_lock_page(inode->i_mapping, page_index);
  2649. if (!page) {
  2650. page_cache_sync_readahead(inode->i_mapping, ra, NULL,
  2651. page_index, last_index + 1 - page_index);
  2652. page = find_or_create_page(inode->i_mapping, page_index, mask);
  2653. if (!page)
  2654. return -ENOMEM;
  2655. }
  2656. if (PageReadahead(page))
  2657. page_cache_async_readahead(inode->i_mapping, ra, NULL,
  2658. page_folio(page), page_index,
  2659. last_index + 1 - page_index);
  2660. if (!PageUptodate(page)) {
  2661. btrfs_read_folio(NULL, page_folio(page));
  2662. lock_page(page);
  2663. if (!PageUptodate(page)) {
  2664. ret = -EIO;
  2665. goto release_page;
  2666. }
  2667. }
  2668. /*
  2669. * We could have lost page private when we dropped the lock to read the
  2670. * page above, make sure we set_page_extent_mapped here so we have any
  2671. * of the subpage blocksize stuff we need in place.
  2672. */
  2673. ret = set_page_extent_mapped(page);
  2674. if (ret < 0)
  2675. goto release_page;
  2676. page_start = page_offset(page);
  2677. page_end = page_start + PAGE_SIZE - 1;
  2678. /*
  2679. * Start from the cluster, as for subpage case, the cluster can start
  2680. * inside the page.
  2681. */
  2682. cur = max(page_start, cluster->boundary[*cluster_nr] - offset);
  2683. while (cur <= page_end) {
  2684. u64 extent_start = cluster->boundary[*cluster_nr] - offset;
  2685. u64 extent_end = get_cluster_boundary_end(cluster,
  2686. *cluster_nr) - offset;
  2687. u64 clamped_start = max(page_start, extent_start);
  2688. u64 clamped_end = min(page_end, extent_end);
  2689. u32 clamped_len = clamped_end + 1 - clamped_start;
  2690. /* Reserve metadata for this range */
  2691. ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
  2692. clamped_len, clamped_len,
  2693. false);
  2694. if (ret)
  2695. goto release_page;
  2696. /* Mark the range delalloc and dirty for later writeback */
  2697. lock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end, NULL);
  2698. ret = btrfs_set_extent_delalloc(BTRFS_I(inode), clamped_start,
  2699. clamped_end, 0, NULL);
  2700. if (ret) {
  2701. clear_extent_bits(&BTRFS_I(inode)->io_tree,
  2702. clamped_start, clamped_end,
  2703. EXTENT_LOCKED | EXTENT_BOUNDARY);
  2704. btrfs_delalloc_release_metadata(BTRFS_I(inode),
  2705. clamped_len, true);
  2706. btrfs_delalloc_release_extents(BTRFS_I(inode),
  2707. clamped_len);
  2708. goto release_page;
  2709. }
  2710. btrfs_page_set_dirty(fs_info, page, clamped_start, clamped_len);
  2711. /*
  2712. * Set the boundary if it's inside the page.
  2713. * Data relocation requires the destination extents to have the
  2714. * same size as the source.
  2715. * EXTENT_BOUNDARY bit prevents current extent from being merged
  2716. * with previous extent.
  2717. */
  2718. if (in_range(cluster->boundary[*cluster_nr] - offset,
  2719. page_start, PAGE_SIZE)) {
  2720. u64 boundary_start = cluster->boundary[*cluster_nr] -
  2721. offset;
  2722. u64 boundary_end = boundary_start +
  2723. fs_info->sectorsize - 1;
  2724. set_extent_bits(&BTRFS_I(inode)->io_tree,
  2725. boundary_start, boundary_end,
  2726. EXTENT_BOUNDARY);
  2727. }
  2728. unlock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end, NULL);
  2729. btrfs_delalloc_release_extents(BTRFS_I(inode), clamped_len);
  2730. cur += clamped_len;
  2731. /* Crossed extent end, go to next extent */
  2732. if (cur >= extent_end) {
  2733. (*cluster_nr)++;
  2734. /* Just finished the last extent of the cluster, exit. */
  2735. if (*cluster_nr >= cluster->nr)
  2736. break;
  2737. }
  2738. }
  2739. unlock_page(page);
  2740. put_page(page);
  2741. balance_dirty_pages_ratelimited(inode->i_mapping);
  2742. btrfs_throttle(fs_info);
  2743. if (btrfs_should_cancel_balance(fs_info))
  2744. ret = -ECANCELED;
  2745. return ret;
  2746. release_page:
  2747. unlock_page(page);
  2748. put_page(page);
  2749. return ret;
  2750. }
  2751. static int relocate_file_extent_cluster(struct inode *inode,
  2752. struct file_extent_cluster *cluster)
  2753. {
  2754. u64 offset = BTRFS_I(inode)->index_cnt;
  2755. unsigned long index;
  2756. unsigned long last_index;
  2757. struct file_ra_state *ra;
  2758. int cluster_nr = 0;
  2759. int ret = 0;
  2760. if (!cluster->nr)
  2761. return 0;
  2762. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  2763. if (!ra)
  2764. return -ENOMEM;
  2765. ret = prealloc_file_extent_cluster(BTRFS_I(inode), cluster);
  2766. if (ret)
  2767. goto out;
  2768. file_ra_state_init(ra, inode->i_mapping);
  2769. ret = setup_relocation_extent_mapping(inode, cluster->start - offset,
  2770. cluster->end - offset, cluster->start);
  2771. if (ret)
  2772. goto out;
  2773. last_index = (cluster->end - offset) >> PAGE_SHIFT;
  2774. for (index = (cluster->start - offset) >> PAGE_SHIFT;
  2775. index <= last_index && !ret; index++)
  2776. ret = relocate_one_page(inode, ra, cluster, &cluster_nr, index);
  2777. if (ret == 0)
  2778. WARN_ON(cluster_nr != cluster->nr);
  2779. out:
  2780. kfree(ra);
  2781. return ret;
  2782. }
  2783. static noinline_for_stack
  2784. int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
  2785. struct file_extent_cluster *cluster)
  2786. {
  2787. int ret;
  2788. if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
  2789. ret = relocate_file_extent_cluster(inode, cluster);
  2790. if (ret)
  2791. return ret;
  2792. cluster->nr = 0;
  2793. }
  2794. if (!cluster->nr)
  2795. cluster->start = extent_key->objectid;
  2796. else
  2797. BUG_ON(cluster->nr >= MAX_EXTENTS);
  2798. cluster->end = extent_key->objectid + extent_key->offset - 1;
  2799. cluster->boundary[cluster->nr] = extent_key->objectid;
  2800. cluster->nr++;
  2801. if (cluster->nr >= MAX_EXTENTS) {
  2802. ret = relocate_file_extent_cluster(inode, cluster);
  2803. if (ret)
  2804. return ret;
  2805. cluster->nr = 0;
  2806. }
  2807. return 0;
  2808. }
  2809. /*
  2810. * helper to add a tree block to the list.
  2811. * the major work is getting the generation and level of the block
  2812. */
  2813. static int add_tree_block(struct reloc_control *rc,
  2814. struct btrfs_key *extent_key,
  2815. struct btrfs_path *path,
  2816. struct rb_root *blocks)
  2817. {
  2818. struct extent_buffer *eb;
  2819. struct btrfs_extent_item *ei;
  2820. struct btrfs_tree_block_info *bi;
  2821. struct tree_block *block;
  2822. struct rb_node *rb_node;
  2823. u32 item_size;
  2824. int level = -1;
  2825. u64 generation;
  2826. u64 owner = 0;
  2827. eb = path->nodes[0];
  2828. item_size = btrfs_item_size(eb, path->slots[0]);
  2829. if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
  2830. item_size >= sizeof(*ei) + sizeof(*bi)) {
  2831. unsigned long ptr = 0, end;
  2832. ei = btrfs_item_ptr(eb, path->slots[0],
  2833. struct btrfs_extent_item);
  2834. end = (unsigned long)ei + item_size;
  2835. if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
  2836. bi = (struct btrfs_tree_block_info *)(ei + 1);
  2837. level = btrfs_tree_block_level(eb, bi);
  2838. ptr = (unsigned long)(bi + 1);
  2839. } else {
  2840. level = (int)extent_key->offset;
  2841. ptr = (unsigned long)(ei + 1);
  2842. }
  2843. generation = btrfs_extent_generation(eb, ei);
  2844. /*
  2845. * We're reading random blocks without knowing their owner ahead
  2846. * of time. This is ok most of the time, as all reloc roots and
  2847. * fs roots have the same lock type. However normal trees do
  2848. * not, and the only way to know ahead of time is to read the
  2849. * inline ref offset. We know it's an fs root if
  2850. *
  2851. * 1. There's more than one ref.
  2852. * 2. There's a SHARED_DATA_REF_KEY set.
  2853. * 3. FULL_BACKREF is set on the flags.
  2854. *
  2855. * Otherwise it's safe to assume that the ref offset == the
  2856. * owner of this block, so we can use that when calling
  2857. * read_tree_block.
  2858. */
  2859. if (btrfs_extent_refs(eb, ei) == 1 &&
  2860. !(btrfs_extent_flags(eb, ei) &
  2861. BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
  2862. ptr < end) {
  2863. struct btrfs_extent_inline_ref *iref;
  2864. int type;
  2865. iref = (struct btrfs_extent_inline_ref *)ptr;
  2866. type = btrfs_get_extent_inline_ref_type(eb, iref,
  2867. BTRFS_REF_TYPE_BLOCK);
  2868. if (type == BTRFS_REF_TYPE_INVALID)
  2869. return -EINVAL;
  2870. if (type == BTRFS_TREE_BLOCK_REF_KEY)
  2871. owner = btrfs_extent_inline_ref_offset(eb, iref);
  2872. }
  2873. } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
  2874. btrfs_print_v0_err(eb->fs_info);
  2875. btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
  2876. return -EINVAL;
  2877. } else {
  2878. BUG();
  2879. }
  2880. btrfs_release_path(path);
  2881. BUG_ON(level == -1);
  2882. block = kmalloc(sizeof(*block), GFP_NOFS);
  2883. if (!block)
  2884. return -ENOMEM;
  2885. block->bytenr = extent_key->objectid;
  2886. block->key.objectid = rc->extent_root->fs_info->nodesize;
  2887. block->key.offset = generation;
  2888. block->level = level;
  2889. block->key_ready = 0;
  2890. block->owner = owner;
  2891. rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);
  2892. if (rb_node)
  2893. btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr,
  2894. -EEXIST);
  2895. return 0;
  2896. }
  2897. /*
  2898. * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
  2899. */
  2900. static int __add_tree_block(struct reloc_control *rc,
  2901. u64 bytenr, u32 blocksize,
  2902. struct rb_root *blocks)
  2903. {
  2904. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2905. struct btrfs_path *path;
  2906. struct btrfs_key key;
  2907. int ret;
  2908. bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
  2909. if (tree_block_processed(bytenr, rc))
  2910. return 0;
  2911. if (rb_simple_search(blocks, bytenr))
  2912. return 0;
  2913. path = btrfs_alloc_path();
  2914. if (!path)
  2915. return -ENOMEM;
  2916. again:
  2917. key.objectid = bytenr;
  2918. if (skinny) {
  2919. key.type = BTRFS_METADATA_ITEM_KEY;
  2920. key.offset = (u64)-1;
  2921. } else {
  2922. key.type = BTRFS_EXTENT_ITEM_KEY;
  2923. key.offset = blocksize;
  2924. }
  2925. path->search_commit_root = 1;
  2926. path->skip_locking = 1;
  2927. ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
  2928. if (ret < 0)
  2929. goto out;
  2930. if (ret > 0 && skinny) {
  2931. if (path->slots[0]) {
  2932. path->slots[0]--;
  2933. btrfs_item_key_to_cpu(path->nodes[0], &key,
  2934. path->slots[0]);
  2935. if (key.objectid == bytenr &&
  2936. (key.type == BTRFS_METADATA_ITEM_KEY ||
  2937. (key.type == BTRFS_EXTENT_ITEM_KEY &&
  2938. key.offset == blocksize)))
  2939. ret = 0;
  2940. }
  2941. if (ret) {
  2942. skinny = false;
  2943. btrfs_release_path(path);
  2944. goto again;
  2945. }
  2946. }
  2947. if (ret) {
  2948. ASSERT(ret == 1);
  2949. btrfs_print_leaf(path->nodes[0]);
  2950. btrfs_err(fs_info,
  2951. "tree block extent item (%llu) is not found in extent tree",
  2952. bytenr);
  2953. WARN_ON(1);
  2954. ret = -EINVAL;
  2955. goto out;
  2956. }
  2957. ret = add_tree_block(rc, &key, path, blocks);
  2958. out:
  2959. btrfs_free_path(path);
  2960. return ret;
  2961. }
  2962. static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
  2963. struct btrfs_block_group *block_group,
  2964. struct inode *inode,
  2965. u64 ino)
  2966. {
  2967. struct btrfs_root *root = fs_info->tree_root;
  2968. struct btrfs_trans_handle *trans;
  2969. int ret = 0;
  2970. if (inode)
  2971. goto truncate;
  2972. inode = btrfs_iget(fs_info->sb, ino, root);
  2973. if (IS_ERR(inode))
  2974. return -ENOENT;
  2975. truncate:
  2976. ret = btrfs_check_trunc_cache_free_space(fs_info,
  2977. &fs_info->global_block_rsv);
  2978. if (ret)
  2979. goto out;
  2980. trans = btrfs_join_transaction(root);
  2981. if (IS_ERR(trans)) {
  2982. ret = PTR_ERR(trans);
  2983. goto out;
  2984. }
  2985. ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
  2986. btrfs_end_transaction(trans);
  2987. btrfs_btree_balance_dirty(fs_info);
  2988. out:
  2989. iput(inode);
  2990. return ret;
  2991. }
  2992. /*
  2993. * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
  2994. * cache inode, to avoid free space cache data extent blocking data relocation.
  2995. */
  2996. static int delete_v1_space_cache(struct extent_buffer *leaf,
  2997. struct btrfs_block_group *block_group,
  2998. u64 data_bytenr)
  2999. {
  3000. u64 space_cache_ino;
  3001. struct btrfs_file_extent_item *ei;
  3002. struct btrfs_key key;
  3003. bool found = false;
  3004. int i;
  3005. int ret;
  3006. if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID)
  3007. return 0;
  3008. for (i = 0; i < btrfs_header_nritems(leaf); i++) {
  3009. u8 type;
  3010. btrfs_item_key_to_cpu(leaf, &key, i);
  3011. if (key.type != BTRFS_EXTENT_DATA_KEY)
  3012. continue;
  3013. ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  3014. type = btrfs_file_extent_type(leaf, ei);
  3015. if ((type == BTRFS_FILE_EXTENT_REG ||
  3016. type == BTRFS_FILE_EXTENT_PREALLOC) &&
  3017. btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) {
  3018. found = true;
  3019. space_cache_ino = key.objectid;
  3020. break;
  3021. }
  3022. }
  3023. if (!found)
  3024. return -ENOENT;
  3025. ret = delete_block_group_cache(leaf->fs_info, block_group, NULL,
  3026. space_cache_ino);
  3027. return ret;
  3028. }
  3029. /*
  3030. * helper to find all tree blocks that reference a given data extent
  3031. */
  3032. static noinline_for_stack
  3033. int add_data_references(struct reloc_control *rc,
  3034. struct btrfs_key *extent_key,
  3035. struct btrfs_path *path,
  3036. struct rb_root *blocks)
  3037. {
  3038. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  3039. struct ulist *leaves = NULL;
  3040. struct ulist_iterator leaf_uiter;
  3041. struct ulist_node *ref_node = NULL;
  3042. const u32 blocksize = fs_info->nodesize;
  3043. int ret = 0;
  3044. btrfs_release_path(path);
  3045. ret = btrfs_find_all_leafs(NULL, fs_info, extent_key->objectid,
  3046. 0, &leaves, NULL, true);
  3047. if (ret < 0)
  3048. return ret;
  3049. ULIST_ITER_INIT(&leaf_uiter);
  3050. while ((ref_node = ulist_next(leaves, &leaf_uiter))) {
  3051. struct extent_buffer *eb;
  3052. eb = read_tree_block(fs_info, ref_node->val, 0, 0, 0, NULL);
  3053. if (IS_ERR(eb)) {
  3054. ret = PTR_ERR(eb);
  3055. break;
  3056. }
  3057. ret = delete_v1_space_cache(eb, rc->block_group,
  3058. extent_key->objectid);
  3059. free_extent_buffer(eb);
  3060. if (ret < 0)
  3061. break;
  3062. ret = __add_tree_block(rc, ref_node->val, blocksize, blocks);
  3063. if (ret < 0)
  3064. break;
  3065. }
  3066. if (ret < 0)
  3067. free_block_list(blocks);
  3068. ulist_free(leaves);
  3069. return ret;
  3070. }
  3071. /*
  3072. * helper to find next unprocessed extent
  3073. */
  3074. static noinline_for_stack
  3075. int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
  3076. struct btrfs_key *extent_key)
  3077. {
  3078. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  3079. struct btrfs_key key;
  3080. struct extent_buffer *leaf;
  3081. u64 start, end, last;
  3082. int ret;
  3083. last = rc->block_group->start + rc->block_group->length;
  3084. while (1) {
  3085. cond_resched();
  3086. if (rc->search_start >= last) {
  3087. ret = 1;
  3088. break;
  3089. }
  3090. key.objectid = rc->search_start;
  3091. key.type = BTRFS_EXTENT_ITEM_KEY;
  3092. key.offset = 0;
  3093. path->search_commit_root = 1;
  3094. path->skip_locking = 1;
  3095. ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
  3096. 0, 0);
  3097. if (ret < 0)
  3098. break;
  3099. next:
  3100. leaf = path->nodes[0];
  3101. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  3102. ret = btrfs_next_leaf(rc->extent_root, path);
  3103. if (ret != 0)
  3104. break;
  3105. leaf = path->nodes[0];
  3106. }
  3107. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3108. if (key.objectid >= last) {
  3109. ret = 1;
  3110. break;
  3111. }
  3112. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  3113. key.type != BTRFS_METADATA_ITEM_KEY) {
  3114. path->slots[0]++;
  3115. goto next;
  3116. }
  3117. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  3118. key.objectid + key.offset <= rc->search_start) {
  3119. path->slots[0]++;
  3120. goto next;
  3121. }
  3122. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  3123. key.objectid + fs_info->nodesize <=
  3124. rc->search_start) {
  3125. path->slots[0]++;
  3126. goto next;
  3127. }
  3128. ret = find_first_extent_bit(&rc->processed_blocks,
  3129. key.objectid, &start, &end,
  3130. EXTENT_DIRTY, NULL);
  3131. if (ret == 0 && start <= key.objectid) {
  3132. btrfs_release_path(path);
  3133. rc->search_start = end + 1;
  3134. } else {
  3135. if (key.type == BTRFS_EXTENT_ITEM_KEY)
  3136. rc->search_start = key.objectid + key.offset;
  3137. else
  3138. rc->search_start = key.objectid +
  3139. fs_info->nodesize;
  3140. memcpy(extent_key, &key, sizeof(key));
  3141. return 0;
  3142. }
  3143. }
  3144. btrfs_release_path(path);
  3145. return ret;
  3146. }
  3147. static void set_reloc_control(struct reloc_control *rc)
  3148. {
  3149. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  3150. mutex_lock(&fs_info->reloc_mutex);
  3151. fs_info->reloc_ctl = rc;
  3152. mutex_unlock(&fs_info->reloc_mutex);
  3153. }
  3154. static void unset_reloc_control(struct reloc_control *rc)
  3155. {
  3156. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  3157. mutex_lock(&fs_info->reloc_mutex);
  3158. fs_info->reloc_ctl = NULL;
  3159. mutex_unlock(&fs_info->reloc_mutex);
  3160. }
  3161. static noinline_for_stack
  3162. int prepare_to_relocate(struct reloc_control *rc)
  3163. {
  3164. struct btrfs_trans_handle *trans;
  3165. int ret;
  3166. rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
  3167. BTRFS_BLOCK_RSV_TEMP);
  3168. if (!rc->block_rsv)
  3169. return -ENOMEM;
  3170. memset(&rc->cluster, 0, sizeof(rc->cluster));
  3171. rc->search_start = rc->block_group->start;
  3172. rc->extents_found = 0;
  3173. rc->nodes_relocated = 0;
  3174. rc->merging_rsv_size = 0;
  3175. rc->reserved_bytes = 0;
  3176. rc->block_rsv->size = rc->extent_root->fs_info->nodesize *
  3177. RELOCATION_RESERVED_NODES;
  3178. ret = btrfs_block_rsv_refill(rc->extent_root->fs_info,
  3179. rc->block_rsv, rc->block_rsv->size,
  3180. BTRFS_RESERVE_FLUSH_ALL);
  3181. if (ret)
  3182. return ret;
  3183. rc->create_reloc_tree = 1;
  3184. set_reloc_control(rc);
  3185. trans = btrfs_join_transaction(rc->extent_root);
  3186. if (IS_ERR(trans)) {
  3187. unset_reloc_control(rc);
  3188. /*
  3189. * extent tree is not a ref_cow tree and has no reloc_root to
  3190. * cleanup. And callers are responsible to free the above
  3191. * block rsv.
  3192. */
  3193. return PTR_ERR(trans);
  3194. }
  3195. ret = btrfs_commit_transaction(trans);
  3196. if (ret)
  3197. unset_reloc_control(rc);
  3198. return ret;
  3199. }
  3200. static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
  3201. {
  3202. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  3203. struct rb_root blocks = RB_ROOT;
  3204. struct btrfs_key key;
  3205. struct btrfs_trans_handle *trans = NULL;
  3206. struct btrfs_path *path;
  3207. struct btrfs_extent_item *ei;
  3208. u64 flags;
  3209. int ret;
  3210. int err = 0;
  3211. int progress = 0;
  3212. path = btrfs_alloc_path();
  3213. if (!path)
  3214. return -ENOMEM;
  3215. path->reada = READA_FORWARD;
  3216. ret = prepare_to_relocate(rc);
  3217. if (ret) {
  3218. err = ret;
  3219. goto out_free;
  3220. }
  3221. while (1) {
  3222. rc->reserved_bytes = 0;
  3223. ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv,
  3224. rc->block_rsv->size,
  3225. BTRFS_RESERVE_FLUSH_ALL);
  3226. if (ret) {
  3227. err = ret;
  3228. break;
  3229. }
  3230. progress++;
  3231. trans = btrfs_start_transaction(rc->extent_root, 0);
  3232. if (IS_ERR(trans)) {
  3233. err = PTR_ERR(trans);
  3234. trans = NULL;
  3235. break;
  3236. }
  3237. restart:
  3238. if (update_backref_cache(trans, &rc->backref_cache)) {
  3239. btrfs_end_transaction(trans);
  3240. trans = NULL;
  3241. continue;
  3242. }
  3243. ret = find_next_extent(rc, path, &key);
  3244. if (ret < 0)
  3245. err = ret;
  3246. if (ret != 0)
  3247. break;
  3248. rc->extents_found++;
  3249. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3250. struct btrfs_extent_item);
  3251. flags = btrfs_extent_flags(path->nodes[0], ei);
  3252. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  3253. ret = add_tree_block(rc, &key, path, &blocks);
  3254. } else if (rc->stage == UPDATE_DATA_PTRS &&
  3255. (flags & BTRFS_EXTENT_FLAG_DATA)) {
  3256. ret = add_data_references(rc, &key, path, &blocks);
  3257. } else {
  3258. btrfs_release_path(path);
  3259. ret = 0;
  3260. }
  3261. if (ret < 0) {
  3262. err = ret;
  3263. break;
  3264. }
  3265. if (!RB_EMPTY_ROOT(&blocks)) {
  3266. ret = relocate_tree_blocks(trans, rc, &blocks);
  3267. if (ret < 0) {
  3268. if (ret != -EAGAIN) {
  3269. err = ret;
  3270. break;
  3271. }
  3272. rc->extents_found--;
  3273. rc->search_start = key.objectid;
  3274. }
  3275. }
  3276. btrfs_end_transaction_throttle(trans);
  3277. btrfs_btree_balance_dirty(fs_info);
  3278. trans = NULL;
  3279. if (rc->stage == MOVE_DATA_EXTENTS &&
  3280. (flags & BTRFS_EXTENT_FLAG_DATA)) {
  3281. rc->found_file_extent = 1;
  3282. ret = relocate_data_extent(rc->data_inode,
  3283. &key, &rc->cluster);
  3284. if (ret < 0) {
  3285. err = ret;
  3286. break;
  3287. }
  3288. }
  3289. if (btrfs_should_cancel_balance(fs_info)) {
  3290. err = -ECANCELED;
  3291. break;
  3292. }
  3293. }
  3294. if (trans && progress && err == -ENOSPC) {
  3295. ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags);
  3296. if (ret == 1) {
  3297. err = 0;
  3298. progress = 0;
  3299. goto restart;
  3300. }
  3301. }
  3302. btrfs_release_path(path);
  3303. clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
  3304. if (trans) {
  3305. btrfs_end_transaction_throttle(trans);
  3306. btrfs_btree_balance_dirty(fs_info);
  3307. }
  3308. if (!err) {
  3309. ret = relocate_file_extent_cluster(rc->data_inode,
  3310. &rc->cluster);
  3311. if (ret < 0)
  3312. err = ret;
  3313. }
  3314. rc->create_reloc_tree = 0;
  3315. set_reloc_control(rc);
  3316. btrfs_backref_release_cache(&rc->backref_cache);
  3317. btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
  3318. /*
  3319. * Even in the case when the relocation is cancelled, we should all go
  3320. * through prepare_to_merge() and merge_reloc_roots().
  3321. *
  3322. * For error (including cancelled balance), prepare_to_merge() will
  3323. * mark all reloc trees orphan, then queue them for cleanup in
  3324. * merge_reloc_roots()
  3325. */
  3326. err = prepare_to_merge(rc, err);
  3327. merge_reloc_roots(rc);
  3328. rc->merge_reloc_tree = 0;
  3329. unset_reloc_control(rc);
  3330. btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
  3331. /* get rid of pinned extents */
  3332. trans = btrfs_join_transaction(rc->extent_root);
  3333. if (IS_ERR(trans)) {
  3334. err = PTR_ERR(trans);
  3335. goto out_free;
  3336. }
  3337. ret = btrfs_commit_transaction(trans);
  3338. if (ret && !err)
  3339. err = ret;
  3340. out_free:
  3341. ret = clean_dirty_subvols(rc);
  3342. if (ret < 0 && !err)
  3343. err = ret;
  3344. btrfs_free_block_rsv(fs_info, rc->block_rsv);
  3345. btrfs_free_path(path);
  3346. return err;
  3347. }
  3348. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  3349. struct btrfs_root *root, u64 objectid)
  3350. {
  3351. struct btrfs_path *path;
  3352. struct btrfs_inode_item *item;
  3353. struct extent_buffer *leaf;
  3354. int ret;
  3355. path = btrfs_alloc_path();
  3356. if (!path)
  3357. return -ENOMEM;
  3358. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  3359. if (ret)
  3360. goto out;
  3361. leaf = path->nodes[0];
  3362. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  3363. memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
  3364. btrfs_set_inode_generation(leaf, item, 1);
  3365. btrfs_set_inode_size(leaf, item, 0);
  3366. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  3367. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
  3368. BTRFS_INODE_PREALLOC);
  3369. btrfs_mark_buffer_dirty(leaf);
  3370. out:
  3371. btrfs_free_path(path);
  3372. return ret;
  3373. }
  3374. static void delete_orphan_inode(struct btrfs_trans_handle *trans,
  3375. struct btrfs_root *root, u64 objectid)
  3376. {
  3377. struct btrfs_path *path;
  3378. struct btrfs_key key;
  3379. int ret = 0;
  3380. path = btrfs_alloc_path();
  3381. if (!path) {
  3382. ret = -ENOMEM;
  3383. goto out;
  3384. }
  3385. key.objectid = objectid;
  3386. key.type = BTRFS_INODE_ITEM_KEY;
  3387. key.offset = 0;
  3388. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  3389. if (ret) {
  3390. if (ret > 0)
  3391. ret = -ENOENT;
  3392. goto out;
  3393. }
  3394. ret = btrfs_del_item(trans, root, path);
  3395. out:
  3396. if (ret)
  3397. btrfs_abort_transaction(trans, ret);
  3398. btrfs_free_path(path);
  3399. }
  3400. /*
  3401. * helper to create inode for data relocation.
  3402. * the inode is in data relocation tree and its link count is 0
  3403. */
  3404. static noinline_for_stack
  3405. struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
  3406. struct btrfs_block_group *group)
  3407. {
  3408. struct inode *inode = NULL;
  3409. struct btrfs_trans_handle *trans;
  3410. struct btrfs_root *root;
  3411. u64 objectid;
  3412. int err = 0;
  3413. root = btrfs_grab_root(fs_info->data_reloc_root);
  3414. trans = btrfs_start_transaction(root, 6);
  3415. if (IS_ERR(trans)) {
  3416. btrfs_put_root(root);
  3417. return ERR_CAST(trans);
  3418. }
  3419. err = btrfs_get_free_objectid(root, &objectid);
  3420. if (err)
  3421. goto out;
  3422. err = __insert_orphan_inode(trans, root, objectid);
  3423. if (err)
  3424. goto out;
  3425. inode = btrfs_iget(fs_info->sb, objectid, root);
  3426. if (IS_ERR(inode)) {
  3427. delete_orphan_inode(trans, root, objectid);
  3428. err = PTR_ERR(inode);
  3429. inode = NULL;
  3430. goto out;
  3431. }
  3432. BTRFS_I(inode)->index_cnt = group->start;
  3433. err = btrfs_orphan_add(trans, BTRFS_I(inode));
  3434. out:
  3435. btrfs_put_root(root);
  3436. btrfs_end_transaction(trans);
  3437. btrfs_btree_balance_dirty(fs_info);
  3438. if (err) {
  3439. iput(inode);
  3440. inode = ERR_PTR(err);
  3441. }
  3442. return inode;
  3443. }
  3444. /*
  3445. * Mark start of chunk relocation that is cancellable. Check if the cancellation
  3446. * has been requested meanwhile and don't start in that case.
  3447. *
  3448. * Return:
  3449. * 0 success
  3450. * -EINPROGRESS operation is already in progress, that's probably a bug
  3451. * -ECANCELED cancellation request was set before the operation started
  3452. */
  3453. static int reloc_chunk_start(struct btrfs_fs_info *fs_info)
  3454. {
  3455. if (test_and_set_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags)) {
  3456. /* This should not happen */
  3457. btrfs_err(fs_info, "reloc already running, cannot start");
  3458. return -EINPROGRESS;
  3459. }
  3460. if (atomic_read(&fs_info->reloc_cancel_req) > 0) {
  3461. btrfs_info(fs_info, "chunk relocation canceled on start");
  3462. /*
  3463. * On cancel, clear all requests but let the caller mark
  3464. * the end after cleanup operations.
  3465. */
  3466. atomic_set(&fs_info->reloc_cancel_req, 0);
  3467. return -ECANCELED;
  3468. }
  3469. return 0;
  3470. }
  3471. /*
  3472. * Mark end of chunk relocation that is cancellable and wake any waiters.
  3473. */
  3474. static void reloc_chunk_end(struct btrfs_fs_info *fs_info)
  3475. {
  3476. /* Requested after start, clear bit first so any waiters can continue */
  3477. if (atomic_read(&fs_info->reloc_cancel_req) > 0)
  3478. btrfs_info(fs_info, "chunk relocation canceled during operation");
  3479. clear_and_wake_up_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags);
  3480. atomic_set(&fs_info->reloc_cancel_req, 0);
  3481. }
  3482. static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
  3483. {
  3484. struct reloc_control *rc;
  3485. rc = kzalloc(sizeof(*rc), GFP_NOFS);
  3486. if (!rc)
  3487. return NULL;
  3488. INIT_LIST_HEAD(&rc->reloc_roots);
  3489. INIT_LIST_HEAD(&rc->dirty_subvol_roots);
  3490. btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);
  3491. mapping_tree_init(&rc->reloc_root_tree);
  3492. extent_io_tree_init(fs_info, &rc->processed_blocks,
  3493. IO_TREE_RELOC_BLOCKS, NULL);
  3494. return rc;
  3495. }
  3496. static void free_reloc_control(struct reloc_control *rc)
  3497. {
  3498. struct mapping_node *node, *tmp;
  3499. free_reloc_roots(&rc->reloc_roots);
  3500. rbtree_postorder_for_each_entry_safe(node, tmp,
  3501. &rc->reloc_root_tree.rb_root, rb_node)
  3502. kfree(node);
  3503. kfree(rc);
  3504. }
  3505. /*
  3506. * Print the block group being relocated
  3507. */
  3508. static void describe_relocation(struct btrfs_fs_info *fs_info,
  3509. struct btrfs_block_group *block_group)
  3510. {
  3511. char buf[128] = {'\0'};
  3512. btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
  3513. btrfs_info(fs_info,
  3514. "relocating block group %llu flags %s",
  3515. block_group->start, buf);
  3516. }
  3517. static const char *stage_to_string(int stage)
  3518. {
  3519. if (stage == MOVE_DATA_EXTENTS)
  3520. return "move data extents";
  3521. if (stage == UPDATE_DATA_PTRS)
  3522. return "update data pointers";
  3523. return "unknown";
  3524. }
  3525. /*
  3526. * function to relocate all extents in a block group.
  3527. */
  3528. int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
  3529. {
  3530. struct btrfs_block_group *bg;
  3531. struct btrfs_root *extent_root = btrfs_extent_root(fs_info, group_start);
  3532. struct reloc_control *rc;
  3533. struct inode *inode;
  3534. struct btrfs_path *path;
  3535. int ret;
  3536. int rw = 0;
  3537. int err = 0;
  3538. /*
  3539. * This only gets set if we had a half-deleted snapshot on mount. We
  3540. * cannot allow relocation to start while we're still trying to clean up
  3541. * these pending deletions.
  3542. */
  3543. ret = wait_on_bit(&fs_info->flags, BTRFS_FS_UNFINISHED_DROPS, TASK_INTERRUPTIBLE);
  3544. if (ret)
  3545. return ret;
  3546. /* We may have been woken up by close_ctree, so bail if we're closing. */
  3547. if (btrfs_fs_closing(fs_info))
  3548. return -EINTR;
  3549. bg = btrfs_lookup_block_group(fs_info, group_start);
  3550. if (!bg)
  3551. return -ENOENT;
  3552. /*
  3553. * Relocation of a data block group creates ordered extents. Without
  3554. * sb_start_write(), we can freeze the filesystem while unfinished
  3555. * ordered extents are left. Such ordered extents can cause a deadlock
  3556. * e.g. when syncfs() is waiting for their completion but they can't
  3557. * finish because they block when joining a transaction, due to the
  3558. * fact that the freeze locks are being held in write mode.
  3559. */
  3560. if (bg->flags & BTRFS_BLOCK_GROUP_DATA)
  3561. ASSERT(sb_write_started(fs_info->sb));
  3562. if (btrfs_pinned_by_swapfile(fs_info, bg)) {
  3563. btrfs_put_block_group(bg);
  3564. return -ETXTBSY;
  3565. }
  3566. rc = alloc_reloc_control(fs_info);
  3567. if (!rc) {
  3568. btrfs_put_block_group(bg);
  3569. return -ENOMEM;
  3570. }
  3571. ret = reloc_chunk_start(fs_info);
  3572. if (ret < 0) {
  3573. err = ret;
  3574. goto out_put_bg;
  3575. }
  3576. rc->extent_root = extent_root;
  3577. rc->block_group = bg;
  3578. ret = btrfs_inc_block_group_ro(rc->block_group, true);
  3579. if (ret) {
  3580. err = ret;
  3581. goto out;
  3582. }
  3583. rw = 1;
  3584. path = btrfs_alloc_path();
  3585. if (!path) {
  3586. err = -ENOMEM;
  3587. goto out;
  3588. }
  3589. inode = lookup_free_space_inode(rc->block_group, path);
  3590. btrfs_free_path(path);
  3591. if (!IS_ERR(inode))
  3592. ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
  3593. else
  3594. ret = PTR_ERR(inode);
  3595. if (ret && ret != -ENOENT) {
  3596. err = ret;
  3597. goto out;
  3598. }
  3599. rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
  3600. if (IS_ERR(rc->data_inode)) {
  3601. err = PTR_ERR(rc->data_inode);
  3602. rc->data_inode = NULL;
  3603. goto out;
  3604. }
  3605. describe_relocation(fs_info, rc->block_group);
  3606. btrfs_wait_block_group_reservations(rc->block_group);
  3607. btrfs_wait_nocow_writers(rc->block_group);
  3608. btrfs_wait_ordered_roots(fs_info, U64_MAX,
  3609. rc->block_group->start,
  3610. rc->block_group->length);
  3611. ret = btrfs_zone_finish(rc->block_group);
  3612. WARN_ON(ret && ret != -EAGAIN);
  3613. while (1) {
  3614. int finishes_stage;
  3615. mutex_lock(&fs_info->cleaner_mutex);
  3616. ret = relocate_block_group(rc);
  3617. mutex_unlock(&fs_info->cleaner_mutex);
  3618. if (ret < 0)
  3619. err = ret;
  3620. finishes_stage = rc->stage;
  3621. /*
  3622. * We may have gotten ENOSPC after we already dirtied some
  3623. * extents. If writeout happens while we're relocating a
  3624. * different block group we could end up hitting the
  3625. * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
  3626. * btrfs_reloc_cow_block. Make sure we write everything out
  3627. * properly so we don't trip over this problem, and then break
  3628. * out of the loop if we hit an error.
  3629. */
  3630. if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
  3631. ret = btrfs_wait_ordered_range(rc->data_inode, 0,
  3632. (u64)-1);
  3633. if (ret)
  3634. err = ret;
  3635. invalidate_mapping_pages(rc->data_inode->i_mapping,
  3636. 0, -1);
  3637. rc->stage = UPDATE_DATA_PTRS;
  3638. }
  3639. if (err < 0)
  3640. goto out;
  3641. if (rc->extents_found == 0)
  3642. break;
  3643. btrfs_info(fs_info, "found %llu extents, stage: %s",
  3644. rc->extents_found, stage_to_string(finishes_stage));
  3645. }
  3646. WARN_ON(rc->block_group->pinned > 0);
  3647. WARN_ON(rc->block_group->reserved > 0);
  3648. WARN_ON(rc->block_group->used > 0);
  3649. out:
  3650. if (err && rw)
  3651. btrfs_dec_block_group_ro(rc->block_group);
  3652. iput(rc->data_inode);
  3653. out_put_bg:
  3654. btrfs_put_block_group(bg);
  3655. reloc_chunk_end(fs_info);
  3656. free_reloc_control(rc);
  3657. return err;
  3658. }
  3659. static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
  3660. {
  3661. struct btrfs_fs_info *fs_info = root->fs_info;
  3662. struct btrfs_trans_handle *trans;
  3663. int ret, err;
  3664. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  3665. if (IS_ERR(trans))
  3666. return PTR_ERR(trans);
  3667. memset(&root->root_item.drop_progress, 0,
  3668. sizeof(root->root_item.drop_progress));
  3669. btrfs_set_root_drop_level(&root->root_item, 0);
  3670. btrfs_set_root_refs(&root->root_item, 0);
  3671. ret = btrfs_update_root(trans, fs_info->tree_root,
  3672. &root->root_key, &root->root_item);
  3673. err = btrfs_end_transaction(trans);
  3674. if (err)
  3675. return err;
  3676. return ret;
  3677. }
  3678. /*
  3679. * recover relocation interrupted by system crash.
  3680. *
  3681. * this function resumes merging reloc trees with corresponding fs trees.
  3682. * this is important for keeping the sharing of tree blocks
  3683. */
  3684. int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)
  3685. {
  3686. LIST_HEAD(reloc_roots);
  3687. struct btrfs_key key;
  3688. struct btrfs_root *fs_root;
  3689. struct btrfs_root *reloc_root;
  3690. struct btrfs_path *path;
  3691. struct extent_buffer *leaf;
  3692. struct reloc_control *rc = NULL;
  3693. struct btrfs_trans_handle *trans;
  3694. int ret;
  3695. int err = 0;
  3696. path = btrfs_alloc_path();
  3697. if (!path)
  3698. return -ENOMEM;
  3699. path->reada = READA_BACK;
  3700. key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  3701. key.type = BTRFS_ROOT_ITEM_KEY;
  3702. key.offset = (u64)-1;
  3703. while (1) {
  3704. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
  3705. path, 0, 0);
  3706. if (ret < 0) {
  3707. err = ret;
  3708. goto out;
  3709. }
  3710. if (ret > 0) {
  3711. if (path->slots[0] == 0)
  3712. break;
  3713. path->slots[0]--;
  3714. }
  3715. leaf = path->nodes[0];
  3716. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3717. btrfs_release_path(path);
  3718. if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
  3719. key.type != BTRFS_ROOT_ITEM_KEY)
  3720. break;
  3721. reloc_root = btrfs_read_tree_root(fs_info->tree_root, &key);
  3722. if (IS_ERR(reloc_root)) {
  3723. err = PTR_ERR(reloc_root);
  3724. goto out;
  3725. }
  3726. set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
  3727. list_add(&reloc_root->root_list, &reloc_roots);
  3728. if (btrfs_root_refs(&reloc_root->root_item) > 0) {
  3729. fs_root = btrfs_get_fs_root(fs_info,
  3730. reloc_root->root_key.offset, false);
  3731. if (IS_ERR(fs_root)) {
  3732. ret = PTR_ERR(fs_root);
  3733. if (ret != -ENOENT) {
  3734. err = ret;
  3735. goto out;
  3736. }
  3737. ret = mark_garbage_root(reloc_root);
  3738. if (ret < 0) {
  3739. err = ret;
  3740. goto out;
  3741. }
  3742. } else {
  3743. btrfs_put_root(fs_root);
  3744. }
  3745. }
  3746. if (key.offset == 0)
  3747. break;
  3748. key.offset--;
  3749. }
  3750. btrfs_release_path(path);
  3751. if (list_empty(&reloc_roots))
  3752. goto out;
  3753. rc = alloc_reloc_control(fs_info);
  3754. if (!rc) {
  3755. err = -ENOMEM;
  3756. goto out;
  3757. }
  3758. ret = reloc_chunk_start(fs_info);
  3759. if (ret < 0) {
  3760. err = ret;
  3761. goto out_end;
  3762. }
  3763. rc->extent_root = btrfs_extent_root(fs_info, 0);
  3764. set_reloc_control(rc);
  3765. trans = btrfs_join_transaction(rc->extent_root);
  3766. if (IS_ERR(trans)) {
  3767. err = PTR_ERR(trans);
  3768. goto out_unset;
  3769. }
  3770. rc->merge_reloc_tree = 1;
  3771. while (!list_empty(&reloc_roots)) {
  3772. reloc_root = list_entry(reloc_roots.next,
  3773. struct btrfs_root, root_list);
  3774. list_del(&reloc_root->root_list);
  3775. if (btrfs_root_refs(&reloc_root->root_item) == 0) {
  3776. list_add_tail(&reloc_root->root_list,
  3777. &rc->reloc_roots);
  3778. continue;
  3779. }
  3780. fs_root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
  3781. false);
  3782. if (IS_ERR(fs_root)) {
  3783. err = PTR_ERR(fs_root);
  3784. list_add_tail(&reloc_root->root_list, &reloc_roots);
  3785. btrfs_end_transaction(trans);
  3786. goto out_unset;
  3787. }
  3788. err = __add_reloc_root(reloc_root);
  3789. ASSERT(err != -EEXIST);
  3790. if (err) {
  3791. list_add_tail(&reloc_root->root_list, &reloc_roots);
  3792. btrfs_put_root(fs_root);
  3793. btrfs_end_transaction(trans);
  3794. goto out_unset;
  3795. }
  3796. fs_root->reloc_root = btrfs_grab_root(reloc_root);
  3797. btrfs_put_root(fs_root);
  3798. }
  3799. err = btrfs_commit_transaction(trans);
  3800. if (err)
  3801. goto out_unset;
  3802. merge_reloc_roots(rc);
  3803. unset_reloc_control(rc);
  3804. trans = btrfs_join_transaction(rc->extent_root);
  3805. if (IS_ERR(trans)) {
  3806. err = PTR_ERR(trans);
  3807. goto out_clean;
  3808. }
  3809. err = btrfs_commit_transaction(trans);
  3810. out_clean:
  3811. ret = clean_dirty_subvols(rc);
  3812. if (ret < 0 && !err)
  3813. err = ret;
  3814. out_unset:
  3815. unset_reloc_control(rc);
  3816. out_end:
  3817. reloc_chunk_end(fs_info);
  3818. free_reloc_control(rc);
  3819. out:
  3820. free_reloc_roots(&reloc_roots);
  3821. btrfs_free_path(path);
  3822. if (err == 0) {
  3823. /* cleanup orphan inode in data relocation tree */
  3824. fs_root = btrfs_grab_root(fs_info->data_reloc_root);
  3825. ASSERT(fs_root);
  3826. err = btrfs_orphan_cleanup(fs_root);
  3827. btrfs_put_root(fs_root);
  3828. }
  3829. return err;
  3830. }
  3831. /*
  3832. * helper to add ordered checksum for data relocation.
  3833. *
  3834. * cloning checksum properly handles the nodatasum extents.
  3835. * it also saves CPU time to re-calculate the checksum.
  3836. */
  3837. int btrfs_reloc_clone_csums(struct btrfs_inode *inode, u64 file_pos, u64 len)
  3838. {
  3839. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  3840. struct btrfs_root *csum_root;
  3841. struct btrfs_ordered_sum *sums;
  3842. struct btrfs_ordered_extent *ordered;
  3843. int ret;
  3844. u64 disk_bytenr;
  3845. u64 new_bytenr;
  3846. LIST_HEAD(list);
  3847. ordered = btrfs_lookup_ordered_extent(inode, file_pos);
  3848. BUG_ON(ordered->file_offset != file_pos || ordered->num_bytes != len);
  3849. disk_bytenr = file_pos + inode->index_cnt;
  3850. csum_root = btrfs_csum_root(fs_info, disk_bytenr);
  3851. ret = btrfs_lookup_csums_range(csum_root, disk_bytenr,
  3852. disk_bytenr + len - 1, &list, 0, false);
  3853. if (ret)
  3854. goto out;
  3855. while (!list_empty(&list)) {
  3856. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  3857. list_del_init(&sums->list);
  3858. /*
  3859. * We need to offset the new_bytenr based on where the csum is.
  3860. * We need to do this because we will read in entire prealloc
  3861. * extents but we may have written to say the middle of the
  3862. * prealloc extent, so we need to make sure the csum goes with
  3863. * the right disk offset.
  3864. *
  3865. * We can do this because the data reloc inode refers strictly
  3866. * to the on disk bytes, so we don't have to worry about
  3867. * disk_len vs real len like with real inodes since it's all
  3868. * disk length.
  3869. */
  3870. new_bytenr = ordered->disk_bytenr + sums->bytenr - disk_bytenr;
  3871. sums->bytenr = new_bytenr;
  3872. btrfs_add_ordered_sum(ordered, sums);
  3873. }
  3874. out:
  3875. btrfs_put_ordered_extent(ordered);
  3876. return ret;
  3877. }
  3878. int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
  3879. struct btrfs_root *root, struct extent_buffer *buf,
  3880. struct extent_buffer *cow)
  3881. {
  3882. struct btrfs_fs_info *fs_info = root->fs_info;
  3883. struct reloc_control *rc;
  3884. struct btrfs_backref_node *node;
  3885. int first_cow = 0;
  3886. int level;
  3887. int ret = 0;
  3888. rc = fs_info->reloc_ctl;
  3889. if (!rc)
  3890. return 0;
  3891. BUG_ON(rc->stage == UPDATE_DATA_PTRS && btrfs_is_data_reloc_root(root));
  3892. level = btrfs_header_level(buf);
  3893. if (btrfs_header_generation(buf) <=
  3894. btrfs_root_last_snapshot(&root->root_item))
  3895. first_cow = 1;
  3896. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
  3897. rc->create_reloc_tree) {
  3898. WARN_ON(!first_cow && level == 0);
  3899. node = rc->backref_cache.path[level];
  3900. BUG_ON(node->bytenr != buf->start &&
  3901. node->new_bytenr != buf->start);
  3902. btrfs_backref_drop_node_buffer(node);
  3903. atomic_inc(&cow->refs);
  3904. node->eb = cow;
  3905. node->new_bytenr = cow->start;
  3906. if (!node->pending) {
  3907. list_move_tail(&node->list,
  3908. &rc->backref_cache.pending[level]);
  3909. node->pending = 1;
  3910. }
  3911. if (first_cow)
  3912. mark_block_processed(rc, node);
  3913. if (first_cow && level > 0)
  3914. rc->nodes_relocated += buf->len;
  3915. }
  3916. if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
  3917. ret = replace_file_extents(trans, rc, root, cow);
  3918. return ret;
  3919. }
  3920. /*
  3921. * called before creating snapshot. it calculates metadata reservation
  3922. * required for relocating tree blocks in the snapshot
  3923. */
  3924. void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
  3925. u64 *bytes_to_reserve)
  3926. {
  3927. struct btrfs_root *root = pending->root;
  3928. struct reloc_control *rc = root->fs_info->reloc_ctl;
  3929. if (!rc || !have_reloc_root(root))
  3930. return;
  3931. if (!rc->merge_reloc_tree)
  3932. return;
  3933. root = root->reloc_root;
  3934. BUG_ON(btrfs_root_refs(&root->root_item) == 0);
  3935. /*
  3936. * relocation is in the stage of merging trees. the space
  3937. * used by merging a reloc tree is twice the size of
  3938. * relocated tree nodes in the worst case. half for cowing
  3939. * the reloc tree, half for cowing the fs tree. the space
  3940. * used by cowing the reloc tree will be freed after the
  3941. * tree is dropped. if we create snapshot, cowing the fs
  3942. * tree may use more space than it frees. so we need
  3943. * reserve extra space.
  3944. */
  3945. *bytes_to_reserve += rc->nodes_relocated;
  3946. }
  3947. /*
  3948. * called after snapshot is created. migrate block reservation
  3949. * and create reloc root for the newly created snapshot
  3950. *
  3951. * This is similar to btrfs_init_reloc_root(), we come out of here with two
  3952. * references held on the reloc_root, one for root->reloc_root and one for
  3953. * rc->reloc_roots.
  3954. */
  3955. int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
  3956. struct btrfs_pending_snapshot *pending)
  3957. {
  3958. struct btrfs_root *root = pending->root;
  3959. struct btrfs_root *reloc_root;
  3960. struct btrfs_root *new_root;
  3961. struct reloc_control *rc = root->fs_info->reloc_ctl;
  3962. int ret;
  3963. if (!rc || !have_reloc_root(root))
  3964. return 0;
  3965. rc = root->fs_info->reloc_ctl;
  3966. rc->merging_rsv_size += rc->nodes_relocated;
  3967. if (rc->merge_reloc_tree) {
  3968. ret = btrfs_block_rsv_migrate(&pending->block_rsv,
  3969. rc->block_rsv,
  3970. rc->nodes_relocated, true);
  3971. if (ret)
  3972. return ret;
  3973. }
  3974. new_root = pending->snap;
  3975. reloc_root = create_reloc_root(trans, root->reloc_root,
  3976. new_root->root_key.objectid);
  3977. if (IS_ERR(reloc_root))
  3978. return PTR_ERR(reloc_root);
  3979. ret = __add_reloc_root(reloc_root);
  3980. ASSERT(ret != -EEXIST);
  3981. if (ret) {
  3982. /* Pairs with create_reloc_root */
  3983. btrfs_put_root(reloc_root);
  3984. return ret;
  3985. }
  3986. new_root->reloc_root = btrfs_grab_root(reloc_root);
  3987. if (rc->create_reloc_tree)
  3988. ret = clone_backref_node(trans, rc, root, reloc_root);
  3989. return ret;
  3990. }