relocation.c 110 KB

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