relocation.c 110 KB

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