relocation.c 110 KB

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