extent-tree.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196
  1. /*
  2. * Copyright (C) 2007 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 "hash.h"
  23. #include "crc32c.h"
  24. #include "ctree.h"
  25. #include "disk-io.h"
  26. #include "print-tree.h"
  27. #include "transaction.h"
  28. #include "volumes.h"
  29. #include "locking.h"
  30. #include "ref-cache.h"
  31. #define PENDING_EXTENT_INSERT 0
  32. #define PENDING_EXTENT_DELETE 1
  33. #define PENDING_BACKREF_UPDATE 2
  34. struct pending_extent_op {
  35. int type;
  36. u64 bytenr;
  37. u64 num_bytes;
  38. u64 parent;
  39. u64 orig_parent;
  40. u64 generation;
  41. u64 orig_generation;
  42. int level;
  43. };
  44. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  45. btrfs_root *extent_root, int all);
  46. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  47. btrfs_root *extent_root, int all);
  48. static struct btrfs_block_group_cache *
  49. __btrfs_find_block_group(struct btrfs_root *root,
  50. struct btrfs_block_group_cache *hint,
  51. u64 search_start, int data, int owner);
  52. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  53. {
  54. return (cache->flags & bits) == bits;
  55. }
  56. /*
  57. * this adds the block group to the fs_info rb tree for the block group
  58. * cache
  59. */
  60. int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
  61. struct btrfs_block_group_cache *block_group)
  62. {
  63. struct rb_node **p;
  64. struct rb_node *parent = NULL;
  65. struct btrfs_block_group_cache *cache;
  66. spin_lock(&info->block_group_cache_lock);
  67. p = &info->block_group_cache_tree.rb_node;
  68. while (*p) {
  69. parent = *p;
  70. cache = rb_entry(parent, struct btrfs_block_group_cache,
  71. cache_node);
  72. if (block_group->key.objectid < cache->key.objectid) {
  73. p = &(*p)->rb_left;
  74. } else if (block_group->key.objectid > cache->key.objectid) {
  75. p = &(*p)->rb_right;
  76. } else {
  77. spin_unlock(&info->block_group_cache_lock);
  78. return -EEXIST;
  79. }
  80. }
  81. rb_link_node(&block_group->cache_node, parent, p);
  82. rb_insert_color(&block_group->cache_node,
  83. &info->block_group_cache_tree);
  84. spin_unlock(&info->block_group_cache_lock);
  85. return 0;
  86. }
  87. /*
  88. * This will return the block group at or after bytenr if contains is 0, else
  89. * it will return the block group that contains the bytenr
  90. */
  91. static struct btrfs_block_group_cache *
  92. block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
  93. int contains)
  94. {
  95. struct btrfs_block_group_cache *cache, *ret = NULL;
  96. struct rb_node *n;
  97. u64 end, start;
  98. spin_lock(&info->block_group_cache_lock);
  99. n = info->block_group_cache_tree.rb_node;
  100. while (n) {
  101. cache = rb_entry(n, struct btrfs_block_group_cache,
  102. cache_node);
  103. end = cache->key.objectid + cache->key.offset - 1;
  104. start = cache->key.objectid;
  105. if (bytenr < start) {
  106. if (!contains && (!ret || start < ret->key.objectid))
  107. ret = cache;
  108. n = n->rb_left;
  109. } else if (bytenr > start) {
  110. if (contains && bytenr <= end) {
  111. ret = cache;
  112. break;
  113. }
  114. n = n->rb_right;
  115. } else {
  116. ret = cache;
  117. break;
  118. }
  119. }
  120. spin_unlock(&info->block_group_cache_lock);
  121. return ret;
  122. }
  123. /*
  124. * this is only called by cache_block_group, since we could have freed extents
  125. * we need to check the pinned_extents for any extents that can't be used yet
  126. * since their free space will be released as soon as the transaction commits.
  127. */
  128. static int add_new_free_space(struct btrfs_block_group_cache *block_group,
  129. struct btrfs_fs_info *info, u64 start, u64 end)
  130. {
  131. u64 extent_start, extent_end, size;
  132. int ret;
  133. mutex_lock(&info->pinned_mutex);
  134. while (start < end) {
  135. ret = find_first_extent_bit(&info->pinned_extents, start,
  136. &extent_start, &extent_end,
  137. EXTENT_DIRTY);
  138. if (ret)
  139. break;
  140. if (extent_start == start) {
  141. start = extent_end + 1;
  142. } else if (extent_start > start && extent_start < end) {
  143. size = extent_start - start;
  144. ret = btrfs_add_free_space_lock(block_group, start,
  145. size);
  146. BUG_ON(ret);
  147. start = extent_end + 1;
  148. } else {
  149. break;
  150. }
  151. }
  152. if (start < end) {
  153. size = end - start;
  154. ret = btrfs_add_free_space_lock(block_group, start, size);
  155. BUG_ON(ret);
  156. }
  157. mutex_unlock(&info->pinned_mutex);
  158. return 0;
  159. }
  160. static int cache_block_group(struct btrfs_root *root,
  161. struct btrfs_block_group_cache *block_group)
  162. {
  163. struct btrfs_path *path;
  164. int ret = 0;
  165. struct btrfs_key key;
  166. struct extent_buffer *leaf;
  167. int slot;
  168. u64 last = 0;
  169. u64 first_free;
  170. int found = 0;
  171. if (!block_group)
  172. return 0;
  173. root = root->fs_info->extent_root;
  174. if (block_group->cached)
  175. return 0;
  176. path = btrfs_alloc_path();
  177. if (!path)
  178. return -ENOMEM;
  179. path->reada = 2;
  180. /*
  181. * we get into deadlocks with paths held by callers of this function.
  182. * since the alloc_mutex is protecting things right now, just
  183. * skip the locking here
  184. */
  185. path->skip_locking = 1;
  186. first_free = max_t(u64, block_group->key.objectid,
  187. BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
  188. key.objectid = block_group->key.objectid;
  189. key.offset = 0;
  190. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  191. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  192. if (ret < 0)
  193. goto err;
  194. ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
  195. if (ret < 0)
  196. goto err;
  197. if (ret == 0) {
  198. leaf = path->nodes[0];
  199. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  200. if (key.objectid + key.offset > first_free)
  201. first_free = key.objectid + key.offset;
  202. }
  203. while(1) {
  204. leaf = path->nodes[0];
  205. slot = path->slots[0];
  206. if (slot >= btrfs_header_nritems(leaf)) {
  207. ret = btrfs_next_leaf(root, path);
  208. if (ret < 0)
  209. goto err;
  210. if (ret == 0)
  211. continue;
  212. else
  213. break;
  214. }
  215. btrfs_item_key_to_cpu(leaf, &key, slot);
  216. if (key.objectid < block_group->key.objectid)
  217. goto next;
  218. if (key.objectid >= block_group->key.objectid +
  219. block_group->key.offset)
  220. break;
  221. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  222. if (!found) {
  223. last = first_free;
  224. found = 1;
  225. }
  226. add_new_free_space(block_group, root->fs_info, last,
  227. key.objectid);
  228. last = key.objectid + key.offset;
  229. }
  230. next:
  231. path->slots[0]++;
  232. }
  233. if (!found)
  234. last = first_free;
  235. add_new_free_space(block_group, root->fs_info, last,
  236. block_group->key.objectid +
  237. block_group->key.offset);
  238. block_group->cached = 1;
  239. ret = 0;
  240. err:
  241. btrfs_free_path(path);
  242. return ret;
  243. }
  244. /*
  245. * return the block group that starts at or after bytenr
  246. */
  247. struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
  248. btrfs_fs_info *info,
  249. u64 bytenr)
  250. {
  251. struct btrfs_block_group_cache *cache;
  252. cache = block_group_cache_tree_search(info, bytenr, 0);
  253. return cache;
  254. }
  255. /*
  256. * return the block group that contains teh given bytenr
  257. */
  258. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  259. btrfs_fs_info *info,
  260. u64 bytenr)
  261. {
  262. struct btrfs_block_group_cache *cache;
  263. cache = block_group_cache_tree_search(info, bytenr, 1);
  264. return cache;
  265. }
  266. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  267. u64 flags)
  268. {
  269. struct list_head *head = &info->space_info;
  270. struct list_head *cur;
  271. struct btrfs_space_info *found;
  272. list_for_each(cur, head) {
  273. found = list_entry(cur, struct btrfs_space_info, list);
  274. if (found->flags == flags)
  275. return found;
  276. }
  277. return NULL;
  278. }
  279. static u64 div_factor(u64 num, int factor)
  280. {
  281. if (factor == 10)
  282. return num;
  283. num *= factor;
  284. do_div(num, 10);
  285. return num;
  286. }
  287. static struct btrfs_block_group_cache *
  288. __btrfs_find_block_group(struct btrfs_root *root,
  289. struct btrfs_block_group_cache *hint,
  290. u64 search_start, int data, int owner)
  291. {
  292. struct btrfs_block_group_cache *cache;
  293. struct btrfs_block_group_cache *found_group = NULL;
  294. struct btrfs_fs_info *info = root->fs_info;
  295. u64 used;
  296. u64 last = 0;
  297. u64 free_check;
  298. int full_search = 0;
  299. int factor = 10;
  300. int wrapped = 0;
  301. if (data & BTRFS_BLOCK_GROUP_METADATA)
  302. factor = 9;
  303. if (search_start) {
  304. struct btrfs_block_group_cache *shint;
  305. shint = btrfs_lookup_first_block_group(info, search_start);
  306. if (shint && block_group_bits(shint, data) && !shint->ro) {
  307. spin_lock(&shint->lock);
  308. used = btrfs_block_group_used(&shint->item);
  309. if (used + shint->pinned + shint->reserved <
  310. div_factor(shint->key.offset, factor)) {
  311. spin_unlock(&shint->lock);
  312. return shint;
  313. }
  314. spin_unlock(&shint->lock);
  315. }
  316. }
  317. if (hint && !hint->ro && block_group_bits(hint, data)) {
  318. spin_lock(&hint->lock);
  319. used = btrfs_block_group_used(&hint->item);
  320. if (used + hint->pinned + hint->reserved <
  321. div_factor(hint->key.offset, factor)) {
  322. spin_unlock(&hint->lock);
  323. return hint;
  324. }
  325. spin_unlock(&hint->lock);
  326. last = hint->key.objectid + hint->key.offset;
  327. } else {
  328. if (hint)
  329. last = max(hint->key.objectid, search_start);
  330. else
  331. last = search_start;
  332. }
  333. again:
  334. while (1) {
  335. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  336. if (!cache)
  337. break;
  338. spin_lock(&cache->lock);
  339. last = cache->key.objectid + cache->key.offset;
  340. used = btrfs_block_group_used(&cache->item);
  341. if (!cache->ro && block_group_bits(cache, data)) {
  342. free_check = div_factor(cache->key.offset, factor);
  343. if (used + cache->pinned + cache->reserved <
  344. free_check) {
  345. found_group = cache;
  346. spin_unlock(&cache->lock);
  347. goto found;
  348. }
  349. }
  350. spin_unlock(&cache->lock);
  351. cond_resched();
  352. }
  353. if (!wrapped) {
  354. last = search_start;
  355. wrapped = 1;
  356. goto again;
  357. }
  358. if (!full_search && factor < 10) {
  359. last = search_start;
  360. full_search = 1;
  361. factor = 10;
  362. goto again;
  363. }
  364. found:
  365. return found_group;
  366. }
  367. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  368. struct btrfs_block_group_cache
  369. *hint, u64 search_start,
  370. int data, int owner)
  371. {
  372. struct btrfs_block_group_cache *ret;
  373. ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
  374. return ret;
  375. }
  376. /* simple helper to search for an existing extent at a given offset */
  377. int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
  378. {
  379. int ret;
  380. struct btrfs_key key;
  381. struct btrfs_path *path;
  382. path = btrfs_alloc_path();
  383. BUG_ON(!path);
  384. key.objectid = start;
  385. key.offset = len;
  386. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  387. ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
  388. 0, 0);
  389. btrfs_free_path(path);
  390. return ret;
  391. }
  392. /*
  393. * Back reference rules. Back refs have three main goals:
  394. *
  395. * 1) differentiate between all holders of references to an extent so that
  396. * when a reference is dropped we can make sure it was a valid reference
  397. * before freeing the extent.
  398. *
  399. * 2) Provide enough information to quickly find the holders of an extent
  400. * if we notice a given block is corrupted or bad.
  401. *
  402. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  403. * maintenance. This is actually the same as #2, but with a slightly
  404. * different use case.
  405. *
  406. * File extents can be referenced by:
  407. *
  408. * - multiple snapshots, subvolumes, or different generations in one subvol
  409. * - different files inside a single subvolume
  410. * - different offsets inside a file (bookend extents in file.c)
  411. *
  412. * The extent ref structure has fields for:
  413. *
  414. * - Objectid of the subvolume root
  415. * - Generation number of the tree holding the reference
  416. * - objectid of the file holding the reference
  417. * - number of references holding by parent node (alway 1 for tree blocks)
  418. *
  419. * Btree leaf may hold multiple references to a file extent. In most cases,
  420. * these references are from same file and the corresponding offsets inside
  421. * the file are close together.
  422. *
  423. * When a file extent is allocated the fields are filled in:
  424. * (root_key.objectid, trans->transid, inode objectid, 1)
  425. *
  426. * When a leaf is cow'd new references are added for every file extent found
  427. * in the leaf. It looks similar to the create case, but trans->transid will
  428. * be different when the block is cow'd.
  429. *
  430. * (root_key.objectid, trans->transid, inode objectid,
  431. * number of references in the leaf)
  432. *
  433. * When a file extent is removed either during snapshot deletion or
  434. * file truncation, we find the corresponding back reference and check
  435. * the following fields:
  436. *
  437. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  438. * inode objectid)
  439. *
  440. * Btree extents can be referenced by:
  441. *
  442. * - Different subvolumes
  443. * - Different generations of the same subvolume
  444. *
  445. * When a tree block is created, back references are inserted:
  446. *
  447. * (root->root_key.objectid, trans->transid, level, 1)
  448. *
  449. * When a tree block is cow'd, new back references are added for all the
  450. * blocks it points to. If the tree block isn't in reference counted root,
  451. * the old back references are removed. These new back references are of
  452. * the form (trans->transid will have increased since creation):
  453. *
  454. * (root->root_key.objectid, trans->transid, level, 1)
  455. *
  456. * When a backref is in deleting, the following fields are checked:
  457. *
  458. * if backref was for a tree root:
  459. * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
  460. * else
  461. * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
  462. *
  463. * Back Reference Key composing:
  464. *
  465. * The key objectid corresponds to the first byte in the extent, the key
  466. * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
  467. * byte of parent extent. If a extent is tree root, the key offset is set
  468. * to the key objectid.
  469. */
  470. static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
  471. struct btrfs_root *root,
  472. struct btrfs_path *path,
  473. u64 bytenr, u64 parent,
  474. u64 ref_root, u64 ref_generation,
  475. u64 owner_objectid, int del)
  476. {
  477. struct btrfs_key key;
  478. struct btrfs_extent_ref *ref;
  479. struct extent_buffer *leaf;
  480. u64 ref_objectid;
  481. int ret;
  482. key.objectid = bytenr;
  483. key.type = BTRFS_EXTENT_REF_KEY;
  484. key.offset = parent;
  485. ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
  486. if (ret < 0)
  487. goto out;
  488. if (ret > 0) {
  489. ret = -ENOENT;
  490. goto out;
  491. }
  492. leaf = path->nodes[0];
  493. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  494. ref_objectid = btrfs_ref_objectid(leaf, ref);
  495. if (btrfs_ref_root(leaf, ref) != ref_root ||
  496. btrfs_ref_generation(leaf, ref) != ref_generation ||
  497. (ref_objectid != owner_objectid &&
  498. ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
  499. ret = -EIO;
  500. WARN_ON(1);
  501. goto out;
  502. }
  503. ret = 0;
  504. out:
  505. return ret;
  506. }
  507. static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
  508. struct btrfs_root *root,
  509. struct btrfs_path *path,
  510. u64 bytenr, u64 parent,
  511. u64 ref_root, u64 ref_generation,
  512. u64 owner_objectid)
  513. {
  514. struct btrfs_key key;
  515. struct extent_buffer *leaf;
  516. struct btrfs_extent_ref *ref;
  517. u32 num_refs;
  518. int ret;
  519. key.objectid = bytenr;
  520. key.type = BTRFS_EXTENT_REF_KEY;
  521. key.offset = parent;
  522. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
  523. if (ret == 0) {
  524. leaf = path->nodes[0];
  525. ref = btrfs_item_ptr(leaf, path->slots[0],
  526. struct btrfs_extent_ref);
  527. btrfs_set_ref_root(leaf, ref, ref_root);
  528. btrfs_set_ref_generation(leaf, ref, ref_generation);
  529. btrfs_set_ref_objectid(leaf, ref, owner_objectid);
  530. btrfs_set_ref_num_refs(leaf, ref, 1);
  531. } else if (ret == -EEXIST) {
  532. u64 existing_owner;
  533. BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
  534. leaf = path->nodes[0];
  535. ref = btrfs_item_ptr(leaf, path->slots[0],
  536. struct btrfs_extent_ref);
  537. if (btrfs_ref_root(leaf, ref) != ref_root ||
  538. btrfs_ref_generation(leaf, ref) != ref_generation) {
  539. ret = -EIO;
  540. WARN_ON(1);
  541. goto out;
  542. }
  543. num_refs = btrfs_ref_num_refs(leaf, ref);
  544. BUG_ON(num_refs == 0);
  545. btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
  546. existing_owner = btrfs_ref_objectid(leaf, ref);
  547. if (existing_owner != owner_objectid &&
  548. existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
  549. btrfs_set_ref_objectid(leaf, ref,
  550. BTRFS_MULTIPLE_OBJECTIDS);
  551. }
  552. ret = 0;
  553. } else {
  554. goto out;
  555. }
  556. btrfs_mark_buffer_dirty(path->nodes[0]);
  557. out:
  558. btrfs_release_path(root, path);
  559. return ret;
  560. }
  561. static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
  562. struct btrfs_root *root,
  563. struct btrfs_path *path)
  564. {
  565. struct extent_buffer *leaf;
  566. struct btrfs_extent_ref *ref;
  567. u32 num_refs;
  568. int ret = 0;
  569. leaf = path->nodes[0];
  570. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  571. num_refs = btrfs_ref_num_refs(leaf, ref);
  572. BUG_ON(num_refs == 0);
  573. num_refs -= 1;
  574. if (num_refs == 0) {
  575. ret = btrfs_del_item(trans, root, path);
  576. } else {
  577. btrfs_set_ref_num_refs(leaf, ref, num_refs);
  578. btrfs_mark_buffer_dirty(leaf);
  579. }
  580. btrfs_release_path(root, path);
  581. return ret;
  582. }
  583. static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  584. struct btrfs_root *root, u64 bytenr,
  585. u64 orig_parent, u64 parent,
  586. u64 orig_root, u64 ref_root,
  587. u64 orig_generation, u64 ref_generation,
  588. u64 owner_objectid)
  589. {
  590. int ret;
  591. struct btrfs_root *extent_root = root->fs_info->extent_root;
  592. struct btrfs_path *path;
  593. if (root == root->fs_info->extent_root) {
  594. struct pending_extent_op *extent_op;
  595. u64 num_bytes;
  596. BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
  597. num_bytes = btrfs_level_size(root, (int)owner_objectid);
  598. mutex_lock(&root->fs_info->extent_ins_mutex);
  599. if (test_range_bit(&root->fs_info->extent_ins, bytenr,
  600. bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
  601. u64 priv;
  602. ret = get_state_private(&root->fs_info->extent_ins,
  603. bytenr, &priv);
  604. BUG_ON(ret);
  605. extent_op = (struct pending_extent_op *)
  606. (unsigned long)priv;
  607. BUG_ON(extent_op->parent != orig_parent);
  608. BUG_ON(extent_op->generation != orig_generation);
  609. extent_op->parent = parent;
  610. extent_op->generation = ref_generation;
  611. } else {
  612. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  613. BUG_ON(!extent_op);
  614. extent_op->type = PENDING_BACKREF_UPDATE;
  615. extent_op->bytenr = bytenr;
  616. extent_op->num_bytes = num_bytes;
  617. extent_op->parent = parent;
  618. extent_op->orig_parent = orig_parent;
  619. extent_op->generation = ref_generation;
  620. extent_op->orig_generation = orig_generation;
  621. extent_op->level = (int)owner_objectid;
  622. set_extent_bits(&root->fs_info->extent_ins,
  623. bytenr, bytenr + num_bytes - 1,
  624. EXTENT_WRITEBACK, GFP_NOFS);
  625. set_state_private(&root->fs_info->extent_ins,
  626. bytenr, (unsigned long)extent_op);
  627. }
  628. mutex_unlock(&root->fs_info->extent_ins_mutex);
  629. return 0;
  630. }
  631. path = btrfs_alloc_path();
  632. if (!path)
  633. return -ENOMEM;
  634. ret = lookup_extent_backref(trans, extent_root, path,
  635. bytenr, orig_parent, orig_root,
  636. orig_generation, owner_objectid, 1);
  637. if (ret)
  638. goto out;
  639. ret = remove_extent_backref(trans, extent_root, path);
  640. if (ret)
  641. goto out;
  642. ret = insert_extent_backref(trans, extent_root, path, bytenr,
  643. parent, ref_root, ref_generation,
  644. owner_objectid);
  645. BUG_ON(ret);
  646. finish_current_insert(trans, extent_root, 0);
  647. del_pending_extents(trans, extent_root, 0);
  648. out:
  649. btrfs_free_path(path);
  650. return ret;
  651. }
  652. int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  653. struct btrfs_root *root, u64 bytenr,
  654. u64 orig_parent, u64 parent,
  655. u64 ref_root, u64 ref_generation,
  656. u64 owner_objectid)
  657. {
  658. int ret;
  659. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  660. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  661. return 0;
  662. ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
  663. parent, ref_root, ref_root,
  664. ref_generation, ref_generation,
  665. owner_objectid);
  666. return ret;
  667. }
  668. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  669. struct btrfs_root *root, u64 bytenr,
  670. u64 orig_parent, u64 parent,
  671. u64 orig_root, u64 ref_root,
  672. u64 orig_generation, u64 ref_generation,
  673. u64 owner_objectid)
  674. {
  675. struct btrfs_path *path;
  676. int ret;
  677. struct btrfs_key key;
  678. struct extent_buffer *l;
  679. struct btrfs_extent_item *item;
  680. u32 refs;
  681. path = btrfs_alloc_path();
  682. if (!path)
  683. return -ENOMEM;
  684. path->reada = 1;
  685. key.objectid = bytenr;
  686. key.type = BTRFS_EXTENT_ITEM_KEY;
  687. key.offset = (u64)-1;
  688. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  689. 0, 1);
  690. if (ret < 0)
  691. return ret;
  692. BUG_ON(ret == 0 || path->slots[0] == 0);
  693. path->slots[0]--;
  694. l = path->nodes[0];
  695. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  696. BUG_ON(key.objectid != bytenr);
  697. BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
  698. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  699. refs = btrfs_extent_refs(l, item);
  700. btrfs_set_extent_refs(l, item, refs + 1);
  701. btrfs_mark_buffer_dirty(path->nodes[0]);
  702. btrfs_release_path(root->fs_info->extent_root, path);
  703. path->reada = 1;
  704. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  705. path, bytenr, parent,
  706. ref_root, ref_generation,
  707. owner_objectid);
  708. BUG_ON(ret);
  709. finish_current_insert(trans, root->fs_info->extent_root, 0);
  710. del_pending_extents(trans, root->fs_info->extent_root, 0);
  711. btrfs_free_path(path);
  712. return 0;
  713. }
  714. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  715. struct btrfs_root *root,
  716. u64 bytenr, u64 num_bytes, u64 parent,
  717. u64 ref_root, u64 ref_generation,
  718. u64 owner_objectid)
  719. {
  720. int ret;
  721. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  722. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  723. return 0;
  724. ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
  725. 0, ref_root, 0, ref_generation,
  726. owner_objectid);
  727. return ret;
  728. }
  729. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  730. struct btrfs_root *root)
  731. {
  732. finish_current_insert(trans, root->fs_info->extent_root, 1);
  733. del_pending_extents(trans, root->fs_info->extent_root, 1);
  734. return 0;
  735. }
  736. int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
  737. struct btrfs_root *root, u64 bytenr,
  738. u64 num_bytes, u32 *refs)
  739. {
  740. struct btrfs_path *path;
  741. int ret;
  742. struct btrfs_key key;
  743. struct extent_buffer *l;
  744. struct btrfs_extent_item *item;
  745. WARN_ON(num_bytes < root->sectorsize);
  746. path = btrfs_alloc_path();
  747. path->reada = 1;
  748. key.objectid = bytenr;
  749. key.offset = num_bytes;
  750. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  751. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  752. 0, 0);
  753. if (ret < 0)
  754. goto out;
  755. if (ret != 0) {
  756. btrfs_print_leaf(root, path->nodes[0]);
  757. printk("failed to find block number %Lu\n", bytenr);
  758. BUG();
  759. }
  760. l = path->nodes[0];
  761. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  762. *refs = btrfs_extent_refs(l, item);
  763. out:
  764. btrfs_free_path(path);
  765. return 0;
  766. }
  767. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  768. struct btrfs_root *root, u64 bytenr)
  769. {
  770. struct btrfs_root *extent_root = root->fs_info->extent_root;
  771. struct btrfs_path *path;
  772. struct extent_buffer *leaf;
  773. struct btrfs_extent_ref *ref_item;
  774. struct btrfs_key key;
  775. struct btrfs_key found_key;
  776. u64 ref_root;
  777. u64 last_snapshot;
  778. u32 nritems;
  779. int ret;
  780. key.objectid = bytenr;
  781. key.offset = (u64)-1;
  782. key.type = BTRFS_EXTENT_ITEM_KEY;
  783. path = btrfs_alloc_path();
  784. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  785. if (ret < 0)
  786. goto out;
  787. BUG_ON(ret == 0);
  788. ret = -ENOENT;
  789. if (path->slots[0] == 0)
  790. goto out;
  791. path->slots[0]--;
  792. leaf = path->nodes[0];
  793. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  794. if (found_key.objectid != bytenr ||
  795. found_key.type != BTRFS_EXTENT_ITEM_KEY)
  796. goto out;
  797. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  798. while (1) {
  799. leaf = path->nodes[0];
  800. nritems = btrfs_header_nritems(leaf);
  801. if (path->slots[0] >= nritems) {
  802. ret = btrfs_next_leaf(extent_root, path);
  803. if (ret < 0)
  804. goto out;
  805. if (ret == 0)
  806. continue;
  807. break;
  808. }
  809. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  810. if (found_key.objectid != bytenr)
  811. break;
  812. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  813. path->slots[0]++;
  814. continue;
  815. }
  816. ref_item = btrfs_item_ptr(leaf, path->slots[0],
  817. struct btrfs_extent_ref);
  818. ref_root = btrfs_ref_root(leaf, ref_item);
  819. if (ref_root != root->root_key.objectid &&
  820. ref_root != BTRFS_TREE_LOG_OBJECTID) {
  821. ret = 1;
  822. goto out;
  823. }
  824. if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
  825. ret = 1;
  826. goto out;
  827. }
  828. path->slots[0]++;
  829. }
  830. ret = 0;
  831. out:
  832. btrfs_free_path(path);
  833. return ret;
  834. }
  835. int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  836. struct extent_buffer *buf, u32 nr_extents)
  837. {
  838. struct btrfs_key key;
  839. struct btrfs_file_extent_item *fi;
  840. u64 root_gen;
  841. u32 nritems;
  842. int i;
  843. int level;
  844. int ret = 0;
  845. int shared = 0;
  846. if (!root->ref_cows)
  847. return 0;
  848. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  849. shared = 0;
  850. root_gen = root->root_key.offset;
  851. } else {
  852. shared = 1;
  853. root_gen = trans->transid - 1;
  854. }
  855. level = btrfs_header_level(buf);
  856. nritems = btrfs_header_nritems(buf);
  857. if (level == 0) {
  858. struct btrfs_leaf_ref *ref;
  859. struct btrfs_extent_info *info;
  860. ref = btrfs_alloc_leaf_ref(root, nr_extents);
  861. if (!ref) {
  862. ret = -ENOMEM;
  863. goto out;
  864. }
  865. ref->root_gen = root_gen;
  866. ref->bytenr = buf->start;
  867. ref->owner = btrfs_header_owner(buf);
  868. ref->generation = btrfs_header_generation(buf);
  869. ref->nritems = nr_extents;
  870. info = ref->extents;
  871. for (i = 0; nr_extents > 0 && i < nritems; i++) {
  872. u64 disk_bytenr;
  873. btrfs_item_key_to_cpu(buf, &key, i);
  874. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  875. continue;
  876. fi = btrfs_item_ptr(buf, i,
  877. struct btrfs_file_extent_item);
  878. if (btrfs_file_extent_type(buf, fi) ==
  879. BTRFS_FILE_EXTENT_INLINE)
  880. continue;
  881. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  882. if (disk_bytenr == 0)
  883. continue;
  884. info->bytenr = disk_bytenr;
  885. info->num_bytes =
  886. btrfs_file_extent_disk_num_bytes(buf, fi);
  887. info->objectid = key.objectid;
  888. info->offset = key.offset;
  889. info++;
  890. }
  891. ret = btrfs_add_leaf_ref(root, ref, shared);
  892. if (ret == -EEXIST && shared) {
  893. struct btrfs_leaf_ref *old;
  894. old = btrfs_lookup_leaf_ref(root, ref->bytenr);
  895. BUG_ON(!old);
  896. btrfs_remove_leaf_ref(root, old);
  897. btrfs_free_leaf_ref(root, old);
  898. ret = btrfs_add_leaf_ref(root, ref, shared);
  899. }
  900. WARN_ON(ret);
  901. btrfs_free_leaf_ref(root, ref);
  902. }
  903. out:
  904. return ret;
  905. }
  906. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  907. struct extent_buffer *orig_buf, struct extent_buffer *buf,
  908. u32 *nr_extents)
  909. {
  910. u64 bytenr;
  911. u64 ref_root;
  912. u64 orig_root;
  913. u64 ref_generation;
  914. u64 orig_generation;
  915. u32 nritems;
  916. u32 nr_file_extents = 0;
  917. struct btrfs_key key;
  918. struct btrfs_file_extent_item *fi;
  919. int i;
  920. int level;
  921. int ret = 0;
  922. int faili = 0;
  923. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  924. u64, u64, u64, u64, u64, u64, u64, u64);
  925. ref_root = btrfs_header_owner(buf);
  926. ref_generation = btrfs_header_generation(buf);
  927. orig_root = btrfs_header_owner(orig_buf);
  928. orig_generation = btrfs_header_generation(orig_buf);
  929. nritems = btrfs_header_nritems(buf);
  930. level = btrfs_header_level(buf);
  931. if (root->ref_cows) {
  932. process_func = __btrfs_inc_extent_ref;
  933. } else {
  934. if (level == 0 &&
  935. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  936. goto out;
  937. if (level != 0 &&
  938. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  939. goto out;
  940. process_func = __btrfs_update_extent_ref;
  941. }
  942. for (i = 0; i < nritems; i++) {
  943. cond_resched();
  944. if (level == 0) {
  945. btrfs_item_key_to_cpu(buf, &key, i);
  946. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  947. continue;
  948. fi = btrfs_item_ptr(buf, i,
  949. struct btrfs_file_extent_item);
  950. if (btrfs_file_extent_type(buf, fi) ==
  951. BTRFS_FILE_EXTENT_INLINE)
  952. continue;
  953. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  954. if (bytenr == 0)
  955. continue;
  956. nr_file_extents++;
  957. ret = process_func(trans, root, bytenr,
  958. orig_buf->start, buf->start,
  959. orig_root, ref_root,
  960. orig_generation, ref_generation,
  961. key.objectid);
  962. if (ret) {
  963. faili = i;
  964. WARN_ON(1);
  965. goto fail;
  966. }
  967. } else {
  968. bytenr = btrfs_node_blockptr(buf, i);
  969. ret = process_func(trans, root, bytenr,
  970. orig_buf->start, buf->start,
  971. orig_root, ref_root,
  972. orig_generation, ref_generation,
  973. level - 1);
  974. if (ret) {
  975. faili = i;
  976. WARN_ON(1);
  977. goto fail;
  978. }
  979. }
  980. }
  981. out:
  982. if (nr_extents) {
  983. if (level == 0)
  984. *nr_extents = nr_file_extents;
  985. else
  986. *nr_extents = nritems;
  987. }
  988. return 0;
  989. fail:
  990. WARN_ON(1);
  991. return ret;
  992. }
  993. int btrfs_update_ref(struct btrfs_trans_handle *trans,
  994. struct btrfs_root *root, struct extent_buffer *orig_buf,
  995. struct extent_buffer *buf, int start_slot, int nr)
  996. {
  997. u64 bytenr;
  998. u64 ref_root;
  999. u64 orig_root;
  1000. u64 ref_generation;
  1001. u64 orig_generation;
  1002. struct btrfs_key key;
  1003. struct btrfs_file_extent_item *fi;
  1004. int i;
  1005. int ret;
  1006. int slot;
  1007. int level;
  1008. BUG_ON(start_slot < 0);
  1009. BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
  1010. ref_root = btrfs_header_owner(buf);
  1011. ref_generation = btrfs_header_generation(buf);
  1012. orig_root = btrfs_header_owner(orig_buf);
  1013. orig_generation = btrfs_header_generation(orig_buf);
  1014. level = btrfs_header_level(buf);
  1015. if (!root->ref_cows) {
  1016. if (level == 0 &&
  1017. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1018. return 0;
  1019. if (level != 0 &&
  1020. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1021. return 0;
  1022. }
  1023. for (i = 0, slot = start_slot; i < nr; i++, slot++) {
  1024. cond_resched();
  1025. if (level == 0) {
  1026. btrfs_item_key_to_cpu(buf, &key, slot);
  1027. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1028. continue;
  1029. fi = btrfs_item_ptr(buf, slot,
  1030. struct btrfs_file_extent_item);
  1031. if (btrfs_file_extent_type(buf, fi) ==
  1032. BTRFS_FILE_EXTENT_INLINE)
  1033. continue;
  1034. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1035. if (bytenr == 0)
  1036. continue;
  1037. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1038. orig_buf->start, buf->start,
  1039. orig_root, ref_root,
  1040. orig_generation, ref_generation,
  1041. key.objectid);
  1042. if (ret)
  1043. goto fail;
  1044. } else {
  1045. bytenr = btrfs_node_blockptr(buf, slot);
  1046. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1047. orig_buf->start, buf->start,
  1048. orig_root, ref_root,
  1049. orig_generation, ref_generation,
  1050. level - 1);
  1051. if (ret)
  1052. goto fail;
  1053. }
  1054. }
  1055. return 0;
  1056. fail:
  1057. WARN_ON(1);
  1058. return -1;
  1059. }
  1060. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  1061. struct btrfs_root *root,
  1062. struct btrfs_path *path,
  1063. struct btrfs_block_group_cache *cache)
  1064. {
  1065. int ret;
  1066. int pending_ret;
  1067. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1068. unsigned long bi;
  1069. struct extent_buffer *leaf;
  1070. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  1071. if (ret < 0)
  1072. goto fail;
  1073. BUG_ON(ret);
  1074. leaf = path->nodes[0];
  1075. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1076. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  1077. btrfs_mark_buffer_dirty(leaf);
  1078. btrfs_release_path(extent_root, path);
  1079. fail:
  1080. finish_current_insert(trans, extent_root, 0);
  1081. pending_ret = del_pending_extents(trans, extent_root, 0);
  1082. if (ret)
  1083. return ret;
  1084. if (pending_ret)
  1085. return pending_ret;
  1086. return 0;
  1087. }
  1088. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  1089. struct btrfs_root *root)
  1090. {
  1091. struct btrfs_block_group_cache *cache, *entry;
  1092. struct rb_node *n;
  1093. int err = 0;
  1094. int werr = 0;
  1095. struct btrfs_path *path;
  1096. u64 last = 0;
  1097. path = btrfs_alloc_path();
  1098. if (!path)
  1099. return -ENOMEM;
  1100. while(1) {
  1101. cache = NULL;
  1102. spin_lock(&root->fs_info->block_group_cache_lock);
  1103. for (n = rb_first(&root->fs_info->block_group_cache_tree);
  1104. n; n = rb_next(n)) {
  1105. entry = rb_entry(n, struct btrfs_block_group_cache,
  1106. cache_node);
  1107. if (entry->dirty) {
  1108. cache = entry;
  1109. break;
  1110. }
  1111. }
  1112. spin_unlock(&root->fs_info->block_group_cache_lock);
  1113. if (!cache)
  1114. break;
  1115. cache->dirty = 0;
  1116. last += cache->key.offset;
  1117. err = write_one_cache_group(trans, root,
  1118. path, cache);
  1119. /*
  1120. * if we fail to write the cache group, we want
  1121. * to keep it marked dirty in hopes that a later
  1122. * write will work
  1123. */
  1124. if (err) {
  1125. werr = err;
  1126. continue;
  1127. }
  1128. }
  1129. btrfs_free_path(path);
  1130. return werr;
  1131. }
  1132. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  1133. u64 total_bytes, u64 bytes_used,
  1134. struct btrfs_space_info **space_info)
  1135. {
  1136. struct btrfs_space_info *found;
  1137. found = __find_space_info(info, flags);
  1138. if (found) {
  1139. spin_lock(&found->lock);
  1140. found->total_bytes += total_bytes;
  1141. found->bytes_used += bytes_used;
  1142. found->full = 0;
  1143. spin_unlock(&found->lock);
  1144. *space_info = found;
  1145. return 0;
  1146. }
  1147. found = kmalloc(sizeof(*found), GFP_NOFS);
  1148. if (!found)
  1149. return -ENOMEM;
  1150. list_add(&found->list, &info->space_info);
  1151. INIT_LIST_HEAD(&found->block_groups);
  1152. init_rwsem(&found->groups_sem);
  1153. spin_lock_init(&found->lock);
  1154. found->flags = flags;
  1155. found->total_bytes = total_bytes;
  1156. found->bytes_used = bytes_used;
  1157. found->bytes_pinned = 0;
  1158. found->bytes_reserved = 0;
  1159. found->full = 0;
  1160. found->force_alloc = 0;
  1161. *space_info = found;
  1162. return 0;
  1163. }
  1164. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  1165. {
  1166. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  1167. BTRFS_BLOCK_GROUP_RAID1 |
  1168. BTRFS_BLOCK_GROUP_RAID10 |
  1169. BTRFS_BLOCK_GROUP_DUP);
  1170. if (extra_flags) {
  1171. if (flags & BTRFS_BLOCK_GROUP_DATA)
  1172. fs_info->avail_data_alloc_bits |= extra_flags;
  1173. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  1174. fs_info->avail_metadata_alloc_bits |= extra_flags;
  1175. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  1176. fs_info->avail_system_alloc_bits |= extra_flags;
  1177. }
  1178. }
  1179. static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  1180. {
  1181. u64 num_devices = root->fs_info->fs_devices->num_devices;
  1182. if (num_devices == 1)
  1183. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  1184. if (num_devices < 4)
  1185. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  1186. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  1187. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  1188. BTRFS_BLOCK_GROUP_RAID10))) {
  1189. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  1190. }
  1191. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  1192. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  1193. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  1194. }
  1195. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  1196. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  1197. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  1198. (flags & BTRFS_BLOCK_GROUP_DUP)))
  1199. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  1200. return flags;
  1201. }
  1202. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  1203. struct btrfs_root *extent_root, u64 alloc_bytes,
  1204. u64 flags, int force)
  1205. {
  1206. struct btrfs_space_info *space_info;
  1207. u64 thresh;
  1208. u64 start;
  1209. u64 num_bytes;
  1210. int ret = 0, waited = 0;
  1211. flags = reduce_alloc_profile(extent_root, flags);
  1212. space_info = __find_space_info(extent_root->fs_info, flags);
  1213. if (!space_info) {
  1214. ret = update_space_info(extent_root->fs_info, flags,
  1215. 0, 0, &space_info);
  1216. BUG_ON(ret);
  1217. }
  1218. BUG_ON(!space_info);
  1219. spin_lock(&space_info->lock);
  1220. if (space_info->force_alloc) {
  1221. force = 1;
  1222. space_info->force_alloc = 0;
  1223. }
  1224. if (space_info->full) {
  1225. spin_unlock(&space_info->lock);
  1226. goto out;
  1227. }
  1228. thresh = div_factor(space_info->total_bytes, 6);
  1229. if (!force &&
  1230. (space_info->bytes_used + space_info->bytes_pinned +
  1231. space_info->bytes_reserved + alloc_bytes) < thresh) {
  1232. spin_unlock(&space_info->lock);
  1233. goto out;
  1234. }
  1235. spin_unlock(&space_info->lock);
  1236. ret = mutex_trylock(&extent_root->fs_info->chunk_mutex);
  1237. if (!ret && !force) {
  1238. goto out;
  1239. } else if (!ret) {
  1240. mutex_lock(&extent_root->fs_info->chunk_mutex);
  1241. waited = 1;
  1242. }
  1243. if (waited) {
  1244. spin_lock(&space_info->lock);
  1245. if (space_info->full) {
  1246. spin_unlock(&space_info->lock);
  1247. goto out_unlock;
  1248. }
  1249. spin_unlock(&space_info->lock);
  1250. }
  1251. ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
  1252. if (ret) {
  1253. printk("space info full %Lu\n", flags);
  1254. space_info->full = 1;
  1255. goto out_unlock;
  1256. }
  1257. ret = btrfs_make_block_group(trans, extent_root, 0, flags,
  1258. BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
  1259. BUG_ON(ret);
  1260. out_unlock:
  1261. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  1262. out:
  1263. return ret;
  1264. }
  1265. static int update_block_group(struct btrfs_trans_handle *trans,
  1266. struct btrfs_root *root,
  1267. u64 bytenr, u64 num_bytes, int alloc,
  1268. int mark_free)
  1269. {
  1270. struct btrfs_block_group_cache *cache;
  1271. struct btrfs_fs_info *info = root->fs_info;
  1272. u64 total = num_bytes;
  1273. u64 old_val;
  1274. u64 byte_in_group;
  1275. while(total) {
  1276. cache = btrfs_lookup_block_group(info, bytenr);
  1277. if (!cache) {
  1278. return -1;
  1279. }
  1280. byte_in_group = bytenr - cache->key.objectid;
  1281. WARN_ON(byte_in_group > cache->key.offset);
  1282. spin_lock(&cache->space_info->lock);
  1283. spin_lock(&cache->lock);
  1284. cache->dirty = 1;
  1285. old_val = btrfs_block_group_used(&cache->item);
  1286. num_bytes = min(total, cache->key.offset - byte_in_group);
  1287. if (alloc) {
  1288. old_val += num_bytes;
  1289. cache->space_info->bytes_used += num_bytes;
  1290. btrfs_set_block_group_used(&cache->item, old_val);
  1291. spin_unlock(&cache->lock);
  1292. spin_unlock(&cache->space_info->lock);
  1293. } else {
  1294. old_val -= num_bytes;
  1295. cache->space_info->bytes_used -= num_bytes;
  1296. btrfs_set_block_group_used(&cache->item, old_val);
  1297. spin_unlock(&cache->lock);
  1298. spin_unlock(&cache->space_info->lock);
  1299. if (mark_free) {
  1300. int ret;
  1301. ret = btrfs_add_free_space(cache, bytenr,
  1302. num_bytes);
  1303. if (ret)
  1304. return -1;
  1305. }
  1306. }
  1307. total -= num_bytes;
  1308. bytenr += num_bytes;
  1309. }
  1310. return 0;
  1311. }
  1312. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  1313. {
  1314. struct btrfs_block_group_cache *cache;
  1315. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  1316. if (!cache)
  1317. return 0;
  1318. return cache->key.objectid;
  1319. }
  1320. int btrfs_update_pinned_extents(struct btrfs_root *root,
  1321. u64 bytenr, u64 num, int pin)
  1322. {
  1323. u64 len;
  1324. struct btrfs_block_group_cache *cache;
  1325. struct btrfs_fs_info *fs_info = root->fs_info;
  1326. WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
  1327. if (pin) {
  1328. set_extent_dirty(&fs_info->pinned_extents,
  1329. bytenr, bytenr + num - 1, GFP_NOFS);
  1330. } else {
  1331. clear_extent_dirty(&fs_info->pinned_extents,
  1332. bytenr, bytenr + num - 1, GFP_NOFS);
  1333. }
  1334. while (num > 0) {
  1335. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1336. BUG_ON(!cache);
  1337. len = min(num, cache->key.offset -
  1338. (bytenr - cache->key.objectid));
  1339. if (pin) {
  1340. spin_lock(&cache->space_info->lock);
  1341. spin_lock(&cache->lock);
  1342. cache->pinned += len;
  1343. cache->space_info->bytes_pinned += len;
  1344. spin_unlock(&cache->lock);
  1345. spin_unlock(&cache->space_info->lock);
  1346. fs_info->total_pinned += len;
  1347. } else {
  1348. spin_lock(&cache->space_info->lock);
  1349. spin_lock(&cache->lock);
  1350. cache->pinned -= len;
  1351. cache->space_info->bytes_pinned -= len;
  1352. spin_unlock(&cache->lock);
  1353. spin_unlock(&cache->space_info->lock);
  1354. fs_info->total_pinned -= len;
  1355. }
  1356. bytenr += len;
  1357. num -= len;
  1358. }
  1359. return 0;
  1360. }
  1361. static int update_reserved_extents(struct btrfs_root *root,
  1362. u64 bytenr, u64 num, int reserve)
  1363. {
  1364. u64 len;
  1365. struct btrfs_block_group_cache *cache;
  1366. struct btrfs_fs_info *fs_info = root->fs_info;
  1367. while (num > 0) {
  1368. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1369. BUG_ON(!cache);
  1370. len = min(num, cache->key.offset -
  1371. (bytenr - cache->key.objectid));
  1372. spin_lock(&cache->space_info->lock);
  1373. spin_lock(&cache->lock);
  1374. if (reserve) {
  1375. cache->reserved += len;
  1376. cache->space_info->bytes_reserved += len;
  1377. } else {
  1378. cache->reserved -= len;
  1379. cache->space_info->bytes_reserved -= len;
  1380. }
  1381. spin_unlock(&cache->lock);
  1382. spin_unlock(&cache->space_info->lock);
  1383. bytenr += len;
  1384. num -= len;
  1385. }
  1386. return 0;
  1387. }
  1388. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  1389. {
  1390. u64 last = 0;
  1391. u64 start;
  1392. u64 end;
  1393. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  1394. int ret;
  1395. mutex_lock(&root->fs_info->pinned_mutex);
  1396. while(1) {
  1397. ret = find_first_extent_bit(pinned_extents, last,
  1398. &start, &end, EXTENT_DIRTY);
  1399. if (ret)
  1400. break;
  1401. set_extent_dirty(copy, start, end, GFP_NOFS);
  1402. last = end + 1;
  1403. }
  1404. mutex_unlock(&root->fs_info->pinned_mutex);
  1405. return 0;
  1406. }
  1407. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  1408. struct btrfs_root *root,
  1409. struct extent_io_tree *unpin)
  1410. {
  1411. u64 start;
  1412. u64 end;
  1413. int ret;
  1414. struct btrfs_block_group_cache *cache;
  1415. mutex_lock(&root->fs_info->pinned_mutex);
  1416. while(1) {
  1417. ret = find_first_extent_bit(unpin, 0, &start, &end,
  1418. EXTENT_DIRTY);
  1419. if (ret)
  1420. break;
  1421. btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
  1422. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  1423. cache = btrfs_lookup_block_group(root->fs_info, start);
  1424. if (cache->cached)
  1425. btrfs_add_free_space(cache, start, end - start + 1);
  1426. if (need_resched()) {
  1427. mutex_unlock(&root->fs_info->pinned_mutex);
  1428. cond_resched();
  1429. mutex_lock(&root->fs_info->pinned_mutex);
  1430. }
  1431. }
  1432. mutex_unlock(&root->fs_info->pinned_mutex);
  1433. return 0;
  1434. }
  1435. static int finish_current_insert(struct btrfs_trans_handle *trans,
  1436. struct btrfs_root *extent_root, int all)
  1437. {
  1438. u64 start;
  1439. u64 end;
  1440. u64 priv;
  1441. u64 search = 0;
  1442. struct btrfs_fs_info *info = extent_root->fs_info;
  1443. struct btrfs_path *path;
  1444. struct btrfs_extent_ref *ref;
  1445. struct pending_extent_op *extent_op;
  1446. struct btrfs_key key;
  1447. struct btrfs_extent_item extent_item;
  1448. int ret;
  1449. int err = 0;
  1450. btrfs_set_stack_extent_refs(&extent_item, 1);
  1451. path = btrfs_alloc_path();
  1452. while(1) {
  1453. mutex_lock(&info->extent_ins_mutex);
  1454. ret = find_first_extent_bit(&info->extent_ins, search, &start,
  1455. &end, EXTENT_WRITEBACK);
  1456. if (ret) {
  1457. mutex_unlock(&info->extent_ins_mutex);
  1458. if (search && all) {
  1459. search = 0;
  1460. continue;
  1461. }
  1462. break;
  1463. }
  1464. ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
  1465. if (!ret) {
  1466. search = end + 1;
  1467. mutex_unlock(&info->extent_ins_mutex);
  1468. cond_resched();
  1469. continue;
  1470. }
  1471. BUG_ON(ret < 0);
  1472. ret = get_state_private(&info->extent_ins, start, &priv);
  1473. BUG_ON(ret);
  1474. extent_op = (struct pending_extent_op *)(unsigned long)priv;
  1475. mutex_unlock(&info->extent_ins_mutex);
  1476. if (extent_op->type == PENDING_EXTENT_INSERT) {
  1477. key.objectid = start;
  1478. key.offset = end + 1 - start;
  1479. key.type = BTRFS_EXTENT_ITEM_KEY;
  1480. err = btrfs_insert_item(trans, extent_root, &key,
  1481. &extent_item, sizeof(extent_item));
  1482. BUG_ON(err);
  1483. mutex_lock(&info->extent_ins_mutex);
  1484. clear_extent_bits(&info->extent_ins, start, end,
  1485. EXTENT_WRITEBACK, GFP_NOFS);
  1486. mutex_unlock(&info->extent_ins_mutex);
  1487. err = insert_extent_backref(trans, extent_root, path,
  1488. start, extent_op->parent,
  1489. extent_root->root_key.objectid,
  1490. extent_op->generation,
  1491. extent_op->level);
  1492. BUG_ON(err);
  1493. } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
  1494. err = lookup_extent_backref(trans, extent_root, path,
  1495. start, extent_op->orig_parent,
  1496. extent_root->root_key.objectid,
  1497. extent_op->orig_generation,
  1498. extent_op->level, 0);
  1499. BUG_ON(err);
  1500. mutex_lock(&info->extent_ins_mutex);
  1501. clear_extent_bits(&info->extent_ins, start, end,
  1502. EXTENT_WRITEBACK, GFP_NOFS);
  1503. mutex_unlock(&info->extent_ins_mutex);
  1504. key.objectid = start;
  1505. key.offset = extent_op->parent;
  1506. key.type = BTRFS_EXTENT_REF_KEY;
  1507. err = btrfs_set_item_key_safe(trans, extent_root, path,
  1508. &key);
  1509. BUG_ON(err);
  1510. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1511. struct btrfs_extent_ref);
  1512. btrfs_set_ref_generation(path->nodes[0], ref,
  1513. extent_op->generation);
  1514. btrfs_mark_buffer_dirty(path->nodes[0]);
  1515. btrfs_release_path(extent_root, path);
  1516. } else {
  1517. BUG_ON(1);
  1518. }
  1519. kfree(extent_op);
  1520. unlock_extent(&info->extent_ins, start, end, GFP_NOFS);
  1521. if (all)
  1522. search = 0;
  1523. else
  1524. search = end + 1;
  1525. cond_resched();
  1526. }
  1527. btrfs_free_path(path);
  1528. return 0;
  1529. }
  1530. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  1531. struct btrfs_root *root,
  1532. u64 bytenr, u64 num_bytes, int is_data)
  1533. {
  1534. int err = 0;
  1535. struct extent_buffer *buf;
  1536. if (is_data)
  1537. goto pinit;
  1538. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  1539. if (!buf)
  1540. goto pinit;
  1541. /* we can reuse a block if it hasn't been written
  1542. * and it is from this transaction. We can't
  1543. * reuse anything from the tree log root because
  1544. * it has tiny sub-transactions.
  1545. */
  1546. if (btrfs_buffer_uptodate(buf, 0) &&
  1547. btrfs_try_tree_lock(buf)) {
  1548. u64 header_owner = btrfs_header_owner(buf);
  1549. u64 header_transid = btrfs_header_generation(buf);
  1550. if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
  1551. header_owner != BTRFS_TREE_RELOC_OBJECTID &&
  1552. header_transid == trans->transid &&
  1553. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  1554. clean_tree_block(NULL, root, buf);
  1555. btrfs_tree_unlock(buf);
  1556. free_extent_buffer(buf);
  1557. return 1;
  1558. }
  1559. btrfs_tree_unlock(buf);
  1560. }
  1561. free_extent_buffer(buf);
  1562. pinit:
  1563. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  1564. BUG_ON(err < 0);
  1565. return 0;
  1566. }
  1567. /*
  1568. * remove an extent from the root, returns 0 on success
  1569. */
  1570. static int __free_extent(struct btrfs_trans_handle *trans,
  1571. struct btrfs_root *root,
  1572. u64 bytenr, u64 num_bytes, u64 parent,
  1573. u64 root_objectid, u64 ref_generation,
  1574. u64 owner_objectid, int pin, int mark_free)
  1575. {
  1576. struct btrfs_path *path;
  1577. struct btrfs_key key;
  1578. struct btrfs_fs_info *info = root->fs_info;
  1579. struct btrfs_root *extent_root = info->extent_root;
  1580. struct extent_buffer *leaf;
  1581. int ret;
  1582. int extent_slot = 0;
  1583. int found_extent = 0;
  1584. int num_to_del = 1;
  1585. struct btrfs_extent_item *ei;
  1586. u32 refs;
  1587. key.objectid = bytenr;
  1588. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1589. key.offset = num_bytes;
  1590. path = btrfs_alloc_path();
  1591. if (!path)
  1592. return -ENOMEM;
  1593. path->reada = 1;
  1594. ret = lookup_extent_backref(trans, extent_root, path,
  1595. bytenr, parent, root_objectid,
  1596. ref_generation, owner_objectid, 1);
  1597. if (ret == 0) {
  1598. struct btrfs_key found_key;
  1599. extent_slot = path->slots[0];
  1600. while(extent_slot > 0) {
  1601. extent_slot--;
  1602. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1603. extent_slot);
  1604. if (found_key.objectid != bytenr)
  1605. break;
  1606. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  1607. found_key.offset == num_bytes) {
  1608. found_extent = 1;
  1609. break;
  1610. }
  1611. if (path->slots[0] - extent_slot > 5)
  1612. break;
  1613. }
  1614. if (!found_extent) {
  1615. ret = remove_extent_backref(trans, extent_root, path);
  1616. BUG_ON(ret);
  1617. btrfs_release_path(extent_root, path);
  1618. ret = btrfs_search_slot(trans, extent_root,
  1619. &key, path, -1, 1);
  1620. BUG_ON(ret);
  1621. extent_slot = path->slots[0];
  1622. }
  1623. } else {
  1624. btrfs_print_leaf(extent_root, path->nodes[0]);
  1625. WARN_ON(1);
  1626. printk("Unable to find ref byte nr %Lu root %Lu "
  1627. "gen %Lu owner %Lu\n", bytenr,
  1628. root_objectid, ref_generation, owner_objectid);
  1629. }
  1630. leaf = path->nodes[0];
  1631. ei = btrfs_item_ptr(leaf, extent_slot,
  1632. struct btrfs_extent_item);
  1633. refs = btrfs_extent_refs(leaf, ei);
  1634. BUG_ON(refs == 0);
  1635. refs -= 1;
  1636. btrfs_set_extent_refs(leaf, ei, refs);
  1637. btrfs_mark_buffer_dirty(leaf);
  1638. if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
  1639. struct btrfs_extent_ref *ref;
  1640. ref = btrfs_item_ptr(leaf, path->slots[0],
  1641. struct btrfs_extent_ref);
  1642. BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
  1643. /* if the back ref and the extent are next to each other
  1644. * they get deleted below in one shot
  1645. */
  1646. path->slots[0] = extent_slot;
  1647. num_to_del = 2;
  1648. } else if (found_extent) {
  1649. /* otherwise delete the extent back ref */
  1650. ret = remove_extent_backref(trans, extent_root, path);
  1651. BUG_ON(ret);
  1652. /* if refs are 0, we need to setup the path for deletion */
  1653. if (refs == 0) {
  1654. btrfs_release_path(extent_root, path);
  1655. ret = btrfs_search_slot(trans, extent_root, &key, path,
  1656. -1, 1);
  1657. BUG_ON(ret);
  1658. }
  1659. }
  1660. if (refs == 0) {
  1661. u64 super_used;
  1662. u64 root_used;
  1663. #ifdef BIO_RW_DISCARD
  1664. u64 map_length = num_bytes;
  1665. struct btrfs_multi_bio *multi = NULL;
  1666. #endif
  1667. if (pin) {
  1668. mutex_lock(&root->fs_info->pinned_mutex);
  1669. ret = pin_down_bytes(trans, root, bytenr, num_bytes,
  1670. owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
  1671. mutex_unlock(&root->fs_info->pinned_mutex);
  1672. if (ret > 0)
  1673. mark_free = 1;
  1674. BUG_ON(ret < 0);
  1675. }
  1676. /* block accounting for super block */
  1677. spin_lock_irq(&info->delalloc_lock);
  1678. super_used = btrfs_super_bytes_used(&info->super_copy);
  1679. btrfs_set_super_bytes_used(&info->super_copy,
  1680. super_used - num_bytes);
  1681. spin_unlock_irq(&info->delalloc_lock);
  1682. /* block accounting for root item */
  1683. root_used = btrfs_root_used(&root->root_item);
  1684. btrfs_set_root_used(&root->root_item,
  1685. root_used - num_bytes);
  1686. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  1687. num_to_del);
  1688. BUG_ON(ret);
  1689. btrfs_release_path(extent_root, path);
  1690. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  1691. mark_free);
  1692. BUG_ON(ret);
  1693. #ifdef BIO_RW_DISCARD
  1694. /* Tell the block device(s) that the sectors can be discarded */
  1695. ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
  1696. bytenr, &map_length, &multi, 0);
  1697. if (!ret) {
  1698. struct btrfs_bio_stripe *stripe = multi->stripes;
  1699. int i;
  1700. if (map_length > num_bytes)
  1701. map_length = num_bytes;
  1702. for (i = 0; i < multi->num_stripes; i++, stripe++) {
  1703. blkdev_issue_discard(stripe->dev->bdev,
  1704. stripe->physical >> 9,
  1705. map_length >> 9);
  1706. }
  1707. kfree(multi);
  1708. }
  1709. #endif
  1710. }
  1711. btrfs_free_path(path);
  1712. finish_current_insert(trans, extent_root, 0);
  1713. return ret;
  1714. }
  1715. /*
  1716. * find all the blocks marked as pending in the radix tree and remove
  1717. * them from the extent map
  1718. */
  1719. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  1720. btrfs_root *extent_root, int all)
  1721. {
  1722. int ret;
  1723. int err = 0;
  1724. u64 start;
  1725. u64 end;
  1726. u64 priv;
  1727. u64 search = 0;
  1728. struct extent_io_tree *pending_del;
  1729. struct extent_io_tree *extent_ins;
  1730. struct pending_extent_op *extent_op;
  1731. struct btrfs_fs_info *info = extent_root->fs_info;
  1732. extent_ins = &extent_root->fs_info->extent_ins;
  1733. pending_del = &extent_root->fs_info->pending_del;
  1734. while(1) {
  1735. mutex_lock(&info->extent_ins_mutex);
  1736. ret = find_first_extent_bit(pending_del, search, &start, &end,
  1737. EXTENT_WRITEBACK);
  1738. if (ret) {
  1739. mutex_unlock(&info->extent_ins_mutex);
  1740. if (all && search) {
  1741. search = 0;
  1742. continue;
  1743. }
  1744. break;
  1745. }
  1746. ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
  1747. if (!ret) {
  1748. search = end+1;
  1749. mutex_unlock(&info->extent_ins_mutex);
  1750. cond_resched();
  1751. continue;
  1752. }
  1753. BUG_ON(ret < 0);
  1754. ret = get_state_private(pending_del, start, &priv);
  1755. BUG_ON(ret);
  1756. extent_op = (struct pending_extent_op *)(unsigned long)priv;
  1757. clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
  1758. GFP_NOFS);
  1759. if (!test_range_bit(extent_ins, start, end,
  1760. EXTENT_WRITEBACK, 0)) {
  1761. mutex_unlock(&info->extent_ins_mutex);
  1762. free_extent:
  1763. ret = __free_extent(trans, extent_root,
  1764. start, end + 1 - start,
  1765. extent_op->orig_parent,
  1766. extent_root->root_key.objectid,
  1767. extent_op->orig_generation,
  1768. extent_op->level, 1, 0);
  1769. kfree(extent_op);
  1770. } else {
  1771. kfree(extent_op);
  1772. ret = get_state_private(&info->extent_ins, start,
  1773. &priv);
  1774. BUG_ON(ret);
  1775. extent_op = (struct pending_extent_op *)
  1776. (unsigned long)priv;
  1777. clear_extent_bits(&info->extent_ins, start, end,
  1778. EXTENT_WRITEBACK, GFP_NOFS);
  1779. mutex_unlock(&info->extent_ins_mutex);
  1780. if (extent_op->type == PENDING_BACKREF_UPDATE)
  1781. goto free_extent;
  1782. mutex_lock(&extent_root->fs_info->pinned_mutex);
  1783. ret = pin_down_bytes(trans, extent_root, start,
  1784. end + 1 - start, 0);
  1785. mutex_unlock(&extent_root->fs_info->pinned_mutex);
  1786. ret = update_block_group(trans, extent_root, start,
  1787. end + 1 - start, 0, ret > 0);
  1788. BUG_ON(ret);
  1789. kfree(extent_op);
  1790. }
  1791. if (ret)
  1792. err = ret;
  1793. unlock_extent(extent_ins, start, end, GFP_NOFS);
  1794. if (all)
  1795. search = 0;
  1796. else
  1797. search = end + 1;
  1798. cond_resched();
  1799. }
  1800. return err;
  1801. }
  1802. /*
  1803. * remove an extent from the root, returns 0 on success
  1804. */
  1805. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  1806. struct btrfs_root *root,
  1807. u64 bytenr, u64 num_bytes, u64 parent,
  1808. u64 root_objectid, u64 ref_generation,
  1809. u64 owner_objectid, int pin)
  1810. {
  1811. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1812. int pending_ret;
  1813. int ret;
  1814. WARN_ON(num_bytes < root->sectorsize);
  1815. if (root == extent_root) {
  1816. struct pending_extent_op *extent_op;
  1817. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  1818. BUG_ON(!extent_op);
  1819. extent_op->type = PENDING_EXTENT_DELETE;
  1820. extent_op->bytenr = bytenr;
  1821. extent_op->num_bytes = num_bytes;
  1822. extent_op->parent = parent;
  1823. extent_op->orig_parent = parent;
  1824. extent_op->generation = ref_generation;
  1825. extent_op->orig_generation = ref_generation;
  1826. extent_op->level = (int)owner_objectid;
  1827. mutex_lock(&root->fs_info->extent_ins_mutex);
  1828. set_extent_bits(&root->fs_info->pending_del,
  1829. bytenr, bytenr + num_bytes - 1,
  1830. EXTENT_WRITEBACK, GFP_NOFS);
  1831. set_state_private(&root->fs_info->pending_del,
  1832. bytenr, (unsigned long)extent_op);
  1833. mutex_unlock(&root->fs_info->extent_ins_mutex);
  1834. return 0;
  1835. }
  1836. /* if metadata always pin */
  1837. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  1838. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  1839. struct btrfs_block_group_cache *cache;
  1840. /* btrfs_free_reserved_extent */
  1841. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  1842. BUG_ON(!cache);
  1843. btrfs_add_free_space(cache, bytenr, num_bytes);
  1844. update_reserved_extents(root, bytenr, num_bytes, 0);
  1845. return 0;
  1846. }
  1847. pin = 1;
  1848. }
  1849. /* if data pin when any transaction has committed this */
  1850. if (ref_generation != trans->transid)
  1851. pin = 1;
  1852. ret = __free_extent(trans, root, bytenr, num_bytes, parent,
  1853. root_objectid, ref_generation,
  1854. owner_objectid, pin, pin == 0);
  1855. finish_current_insert(trans, root->fs_info->extent_root, 0);
  1856. pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
  1857. return ret ? ret : pending_ret;
  1858. }
  1859. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  1860. struct btrfs_root *root,
  1861. u64 bytenr, u64 num_bytes, u64 parent,
  1862. u64 root_objectid, u64 ref_generation,
  1863. u64 owner_objectid, int pin)
  1864. {
  1865. int ret;
  1866. ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
  1867. root_objectid, ref_generation,
  1868. owner_objectid, pin);
  1869. return ret;
  1870. }
  1871. static u64 stripe_align(struct btrfs_root *root, u64 val)
  1872. {
  1873. u64 mask = ((u64)root->stripesize - 1);
  1874. u64 ret = (val + mask) & ~mask;
  1875. return ret;
  1876. }
  1877. /*
  1878. * walks the btree of allocated extents and find a hole of a given size.
  1879. * The key ins is changed to record the hole:
  1880. * ins->objectid == block start
  1881. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  1882. * ins->offset == number of blocks
  1883. * Any available blocks before search_start are skipped.
  1884. */
  1885. static int noinline find_free_extent(struct btrfs_trans_handle *trans,
  1886. struct btrfs_root *orig_root,
  1887. u64 num_bytes, u64 empty_size,
  1888. u64 search_start, u64 search_end,
  1889. u64 hint_byte, struct btrfs_key *ins,
  1890. u64 exclude_start, u64 exclude_nr,
  1891. int data)
  1892. {
  1893. int ret = 0;
  1894. struct btrfs_root * root = orig_root->fs_info->extent_root;
  1895. u64 total_needed = num_bytes;
  1896. u64 *last_ptr = NULL;
  1897. struct btrfs_block_group_cache *block_group = NULL;
  1898. int chunk_alloc_done = 0;
  1899. int empty_cluster = 2 * 1024 * 1024;
  1900. int allowed_chunk_alloc = 0;
  1901. struct list_head *head = NULL, *cur = NULL;
  1902. int loop = 0;
  1903. struct btrfs_space_info *space_info;
  1904. WARN_ON(num_bytes < root->sectorsize);
  1905. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  1906. ins->objectid = 0;
  1907. ins->offset = 0;
  1908. if (orig_root->ref_cows || empty_size)
  1909. allowed_chunk_alloc = 1;
  1910. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  1911. last_ptr = &root->fs_info->last_alloc;
  1912. empty_cluster = 256 * 1024;
  1913. }
  1914. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
  1915. last_ptr = &root->fs_info->last_data_alloc;
  1916. if (last_ptr) {
  1917. if (*last_ptr)
  1918. hint_byte = *last_ptr;
  1919. else
  1920. empty_size += empty_cluster;
  1921. }
  1922. search_start = max(search_start, first_logical_byte(root, 0));
  1923. search_start = max(search_start, hint_byte);
  1924. total_needed += empty_size;
  1925. block_group = btrfs_lookup_block_group(root->fs_info, search_start);
  1926. if (!block_group)
  1927. block_group = btrfs_lookup_first_block_group(root->fs_info,
  1928. search_start);
  1929. space_info = __find_space_info(root->fs_info, data);
  1930. down_read(&space_info->groups_sem);
  1931. while (1) {
  1932. struct btrfs_free_space *free_space;
  1933. /*
  1934. * the only way this happens if our hint points to a block
  1935. * group thats not of the proper type, while looping this
  1936. * should never happen
  1937. */
  1938. WARN_ON(!block_group);
  1939. mutex_lock(&block_group->alloc_mutex);
  1940. if (unlikely(!block_group_bits(block_group, data)))
  1941. goto new_group;
  1942. ret = cache_block_group(root, block_group);
  1943. if (ret) {
  1944. mutex_unlock(&block_group->alloc_mutex);
  1945. break;
  1946. }
  1947. if (block_group->ro)
  1948. goto new_group;
  1949. free_space = btrfs_find_free_space(block_group, search_start,
  1950. total_needed);
  1951. if (free_space) {
  1952. u64 start = block_group->key.objectid;
  1953. u64 end = block_group->key.objectid +
  1954. block_group->key.offset;
  1955. search_start = stripe_align(root, free_space->offset);
  1956. /* move on to the next group */
  1957. if (search_start + num_bytes >= search_end)
  1958. goto new_group;
  1959. /* move on to the next group */
  1960. if (search_start + num_bytes > end)
  1961. goto new_group;
  1962. if (exclude_nr > 0 &&
  1963. (search_start + num_bytes > exclude_start &&
  1964. search_start < exclude_start + exclude_nr)) {
  1965. search_start = exclude_start + exclude_nr;
  1966. /*
  1967. * if search_start is still in this block group
  1968. * then we just re-search this block group
  1969. */
  1970. if (search_start >= start &&
  1971. search_start < end) {
  1972. mutex_unlock(&block_group->alloc_mutex);
  1973. continue;
  1974. }
  1975. /* else we go to the next block group */
  1976. goto new_group;
  1977. }
  1978. ins->objectid = search_start;
  1979. ins->offset = num_bytes;
  1980. btrfs_remove_free_space_lock(block_group, search_start,
  1981. num_bytes);
  1982. /* we are all good, lets return */
  1983. mutex_unlock(&block_group->alloc_mutex);
  1984. break;
  1985. }
  1986. new_group:
  1987. mutex_unlock(&block_group->alloc_mutex);
  1988. /*
  1989. * Here's how this works.
  1990. * loop == 0: we were searching a block group via a hint
  1991. * and didn't find anything, so we start at
  1992. * the head of the block groups and keep searching
  1993. * loop == 1: we're searching through all of the block groups
  1994. * if we hit the head again we have searched
  1995. * all of the block groups for this space and we
  1996. * need to try and allocate, if we cant error out.
  1997. * loop == 2: we allocated more space and are looping through
  1998. * all of the block groups again.
  1999. */
  2000. if (loop == 0) {
  2001. head = &space_info->block_groups;
  2002. cur = head->next;
  2003. if (last_ptr && *last_ptr) {
  2004. total_needed += empty_cluster;
  2005. *last_ptr = 0;
  2006. }
  2007. loop++;
  2008. } else if (loop == 1 && cur == head) {
  2009. if (allowed_chunk_alloc && !chunk_alloc_done) {
  2010. up_read(&space_info->groups_sem);
  2011. ret = do_chunk_alloc(trans, root, num_bytes +
  2012. 2 * 1024 * 1024, data, 1);
  2013. if (ret < 0)
  2014. break;
  2015. down_read(&space_info->groups_sem);
  2016. loop++;
  2017. head = &space_info->block_groups;
  2018. cur = head->next;
  2019. chunk_alloc_done = 1;
  2020. } else if (!allowed_chunk_alloc) {
  2021. space_info->force_alloc = 1;
  2022. break;
  2023. } else {
  2024. break;
  2025. }
  2026. } else if (cur == head) {
  2027. break;
  2028. }
  2029. block_group = list_entry(cur, struct btrfs_block_group_cache,
  2030. list);
  2031. search_start = block_group->key.objectid;
  2032. cur = cur->next;
  2033. }
  2034. /* we found what we needed */
  2035. if (ins->objectid) {
  2036. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  2037. trans->block_group = block_group;
  2038. if (last_ptr)
  2039. *last_ptr = ins->objectid + ins->offset;
  2040. ret = 0;
  2041. } else if (!ret) {
  2042. ret = -ENOSPC;
  2043. }
  2044. up_read(&space_info->groups_sem);
  2045. return ret;
  2046. }
  2047. static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
  2048. {
  2049. struct btrfs_block_group_cache *cache;
  2050. struct list_head *l;
  2051. printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
  2052. info->total_bytes - info->bytes_used - info->bytes_pinned -
  2053. info->bytes_reserved, (info->full) ? "" : "not ");
  2054. down_read(&info->groups_sem);
  2055. list_for_each(l, &info->block_groups) {
  2056. cache = list_entry(l, struct btrfs_block_group_cache, list);
  2057. spin_lock(&cache->lock);
  2058. printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
  2059. "%Lu pinned %Lu reserved\n",
  2060. cache->key.objectid, cache->key.offset,
  2061. btrfs_block_group_used(&cache->item),
  2062. cache->pinned, cache->reserved);
  2063. btrfs_dump_free_space(cache, bytes);
  2064. spin_unlock(&cache->lock);
  2065. }
  2066. up_read(&info->groups_sem);
  2067. }
  2068. static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2069. struct btrfs_root *root,
  2070. u64 num_bytes, u64 min_alloc_size,
  2071. u64 empty_size, u64 hint_byte,
  2072. u64 search_end, struct btrfs_key *ins,
  2073. u64 data)
  2074. {
  2075. int ret;
  2076. u64 search_start = 0;
  2077. u64 alloc_profile;
  2078. struct btrfs_fs_info *info = root->fs_info;
  2079. if (data) {
  2080. alloc_profile = info->avail_data_alloc_bits &
  2081. info->data_alloc_profile;
  2082. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  2083. } else if (root == root->fs_info->chunk_root) {
  2084. alloc_profile = info->avail_system_alloc_bits &
  2085. info->system_alloc_profile;
  2086. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  2087. } else {
  2088. alloc_profile = info->avail_metadata_alloc_bits &
  2089. info->metadata_alloc_profile;
  2090. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  2091. }
  2092. again:
  2093. data = reduce_alloc_profile(root, data);
  2094. /*
  2095. * the only place that sets empty_size is btrfs_realloc_node, which
  2096. * is not called recursively on allocations
  2097. */
  2098. if (empty_size || root->ref_cows) {
  2099. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  2100. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2101. 2 * 1024 * 1024,
  2102. BTRFS_BLOCK_GROUP_METADATA |
  2103. (info->metadata_alloc_profile &
  2104. info->avail_metadata_alloc_bits), 0);
  2105. }
  2106. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2107. num_bytes + 2 * 1024 * 1024, data, 0);
  2108. }
  2109. WARN_ON(num_bytes < root->sectorsize);
  2110. ret = find_free_extent(trans, root, num_bytes, empty_size,
  2111. search_start, search_end, hint_byte, ins,
  2112. trans->alloc_exclude_start,
  2113. trans->alloc_exclude_nr, data);
  2114. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  2115. num_bytes = num_bytes >> 1;
  2116. num_bytes = num_bytes & ~(root->sectorsize - 1);
  2117. num_bytes = max(num_bytes, min_alloc_size);
  2118. do_chunk_alloc(trans, root->fs_info->extent_root,
  2119. num_bytes, data, 1);
  2120. goto again;
  2121. }
  2122. if (ret) {
  2123. struct btrfs_space_info *sinfo;
  2124. sinfo = __find_space_info(root->fs_info, data);
  2125. printk("allocation failed flags %Lu, wanted %Lu\n",
  2126. data, num_bytes);
  2127. dump_space_info(sinfo, num_bytes);
  2128. BUG();
  2129. }
  2130. return ret;
  2131. }
  2132. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  2133. {
  2134. struct btrfs_block_group_cache *cache;
  2135. cache = btrfs_lookup_block_group(root->fs_info, start);
  2136. if (!cache) {
  2137. printk(KERN_ERR "Unable to find block group for %Lu\n", start);
  2138. return -ENOSPC;
  2139. }
  2140. btrfs_add_free_space(cache, start, len);
  2141. update_reserved_extents(root, start, len, 0);
  2142. return 0;
  2143. }
  2144. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2145. struct btrfs_root *root,
  2146. u64 num_bytes, u64 min_alloc_size,
  2147. u64 empty_size, u64 hint_byte,
  2148. u64 search_end, struct btrfs_key *ins,
  2149. u64 data)
  2150. {
  2151. int ret;
  2152. ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
  2153. empty_size, hint_byte, search_end, ins,
  2154. data);
  2155. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2156. return ret;
  2157. }
  2158. static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2159. struct btrfs_root *root, u64 parent,
  2160. u64 root_objectid, u64 ref_generation,
  2161. u64 owner, struct btrfs_key *ins)
  2162. {
  2163. int ret;
  2164. int pending_ret;
  2165. u64 super_used;
  2166. u64 root_used;
  2167. u64 num_bytes = ins->offset;
  2168. u32 sizes[2];
  2169. struct btrfs_fs_info *info = root->fs_info;
  2170. struct btrfs_root *extent_root = info->extent_root;
  2171. struct btrfs_extent_item *extent_item;
  2172. struct btrfs_extent_ref *ref;
  2173. struct btrfs_path *path;
  2174. struct btrfs_key keys[2];
  2175. if (parent == 0)
  2176. parent = ins->objectid;
  2177. /* block accounting for super block */
  2178. spin_lock_irq(&info->delalloc_lock);
  2179. super_used = btrfs_super_bytes_used(&info->super_copy);
  2180. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  2181. spin_unlock_irq(&info->delalloc_lock);
  2182. /* block accounting for root item */
  2183. root_used = btrfs_root_used(&root->root_item);
  2184. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  2185. if (root == extent_root) {
  2186. struct pending_extent_op *extent_op;
  2187. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2188. BUG_ON(!extent_op);
  2189. extent_op->type = PENDING_EXTENT_INSERT;
  2190. extent_op->bytenr = ins->objectid;
  2191. extent_op->num_bytes = ins->offset;
  2192. extent_op->parent = parent;
  2193. extent_op->orig_parent = 0;
  2194. extent_op->generation = ref_generation;
  2195. extent_op->orig_generation = 0;
  2196. extent_op->level = (int)owner;
  2197. mutex_lock(&root->fs_info->extent_ins_mutex);
  2198. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  2199. ins->objectid + ins->offset - 1,
  2200. EXTENT_WRITEBACK, GFP_NOFS);
  2201. set_state_private(&root->fs_info->extent_ins,
  2202. ins->objectid, (unsigned long)extent_op);
  2203. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2204. goto update_block;
  2205. }
  2206. memcpy(&keys[0], ins, sizeof(*ins));
  2207. keys[1].objectid = ins->objectid;
  2208. keys[1].type = BTRFS_EXTENT_REF_KEY;
  2209. keys[1].offset = parent;
  2210. sizes[0] = sizeof(*extent_item);
  2211. sizes[1] = sizeof(*ref);
  2212. path = btrfs_alloc_path();
  2213. BUG_ON(!path);
  2214. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  2215. sizes, 2);
  2216. BUG_ON(ret);
  2217. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2218. struct btrfs_extent_item);
  2219. btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
  2220. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  2221. struct btrfs_extent_ref);
  2222. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  2223. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  2224. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  2225. btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
  2226. btrfs_mark_buffer_dirty(path->nodes[0]);
  2227. trans->alloc_exclude_start = 0;
  2228. trans->alloc_exclude_nr = 0;
  2229. btrfs_free_path(path);
  2230. finish_current_insert(trans, extent_root, 0);
  2231. pending_ret = del_pending_extents(trans, extent_root, 0);
  2232. if (ret)
  2233. goto out;
  2234. if (pending_ret) {
  2235. ret = pending_ret;
  2236. goto out;
  2237. }
  2238. update_block:
  2239. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
  2240. if (ret) {
  2241. printk("update block group failed for %Lu %Lu\n",
  2242. ins->objectid, ins->offset);
  2243. BUG();
  2244. }
  2245. out:
  2246. return ret;
  2247. }
  2248. int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2249. struct btrfs_root *root, u64 parent,
  2250. u64 root_objectid, u64 ref_generation,
  2251. u64 owner, struct btrfs_key *ins)
  2252. {
  2253. int ret;
  2254. if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
  2255. return 0;
  2256. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  2257. ref_generation, owner, ins);
  2258. update_reserved_extents(root, ins->objectid, ins->offset, 0);
  2259. return ret;
  2260. }
  2261. /*
  2262. * this is used by the tree logging recovery code. It records that
  2263. * an extent has been allocated and makes sure to clear the free
  2264. * space cache bits as well
  2265. */
  2266. int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
  2267. struct btrfs_root *root, u64 parent,
  2268. u64 root_objectid, u64 ref_generation,
  2269. u64 owner, struct btrfs_key *ins)
  2270. {
  2271. int ret;
  2272. struct btrfs_block_group_cache *block_group;
  2273. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  2274. mutex_lock(&block_group->alloc_mutex);
  2275. cache_block_group(root, block_group);
  2276. ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
  2277. ins->offset);
  2278. mutex_unlock(&block_group->alloc_mutex);
  2279. BUG_ON(ret);
  2280. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  2281. ref_generation, owner, ins);
  2282. return ret;
  2283. }
  2284. /*
  2285. * finds a free extent and does all the dirty work required for allocation
  2286. * returns the key for the extent through ins, and a tree buffer for
  2287. * the first block of the extent through buf.
  2288. *
  2289. * returns 0 if everything worked, non-zero otherwise.
  2290. */
  2291. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  2292. struct btrfs_root *root,
  2293. u64 num_bytes, u64 parent, u64 min_alloc_size,
  2294. u64 root_objectid, u64 ref_generation,
  2295. u64 owner_objectid, u64 empty_size, u64 hint_byte,
  2296. u64 search_end, struct btrfs_key *ins, u64 data)
  2297. {
  2298. int ret;
  2299. ret = __btrfs_reserve_extent(trans, root, num_bytes,
  2300. min_alloc_size, empty_size, hint_byte,
  2301. search_end, ins, data);
  2302. BUG_ON(ret);
  2303. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  2304. ret = __btrfs_alloc_reserved_extent(trans, root, parent,
  2305. root_objectid, ref_generation,
  2306. owner_objectid, ins);
  2307. BUG_ON(ret);
  2308. } else {
  2309. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2310. }
  2311. return ret;
  2312. }
  2313. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  2314. struct btrfs_root *root,
  2315. u64 bytenr, u32 blocksize)
  2316. {
  2317. struct extent_buffer *buf;
  2318. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  2319. if (!buf)
  2320. return ERR_PTR(-ENOMEM);
  2321. btrfs_set_header_generation(buf, trans->transid);
  2322. btrfs_tree_lock(buf);
  2323. clean_tree_block(trans, root, buf);
  2324. btrfs_set_buffer_uptodate(buf);
  2325. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  2326. set_extent_dirty(&root->dirty_log_pages, buf->start,
  2327. buf->start + buf->len - 1, GFP_NOFS);
  2328. } else {
  2329. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  2330. buf->start + buf->len - 1, GFP_NOFS);
  2331. }
  2332. trans->blocks_used++;
  2333. return buf;
  2334. }
  2335. /*
  2336. * helper function to allocate a block for a given tree
  2337. * returns the tree buffer or NULL.
  2338. */
  2339. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  2340. struct btrfs_root *root,
  2341. u32 blocksize, u64 parent,
  2342. u64 root_objectid,
  2343. u64 ref_generation,
  2344. int level,
  2345. u64 hint,
  2346. u64 empty_size)
  2347. {
  2348. struct btrfs_key ins;
  2349. int ret;
  2350. struct extent_buffer *buf;
  2351. ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
  2352. root_objectid, ref_generation, level,
  2353. empty_size, hint, (u64)-1, &ins, 0);
  2354. if (ret) {
  2355. BUG_ON(ret > 0);
  2356. return ERR_PTR(ret);
  2357. }
  2358. buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
  2359. return buf;
  2360. }
  2361. int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
  2362. struct btrfs_root *root, struct extent_buffer *leaf)
  2363. {
  2364. u64 leaf_owner;
  2365. u64 leaf_generation;
  2366. struct btrfs_key key;
  2367. struct btrfs_file_extent_item *fi;
  2368. int i;
  2369. int nritems;
  2370. int ret;
  2371. BUG_ON(!btrfs_is_leaf(leaf));
  2372. nritems = btrfs_header_nritems(leaf);
  2373. leaf_owner = btrfs_header_owner(leaf);
  2374. leaf_generation = btrfs_header_generation(leaf);
  2375. for (i = 0; i < nritems; i++) {
  2376. u64 disk_bytenr;
  2377. cond_resched();
  2378. btrfs_item_key_to_cpu(leaf, &key, i);
  2379. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2380. continue;
  2381. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  2382. if (btrfs_file_extent_type(leaf, fi) ==
  2383. BTRFS_FILE_EXTENT_INLINE)
  2384. continue;
  2385. /*
  2386. * FIXME make sure to insert a trans record that
  2387. * repeats the snapshot del on crash
  2388. */
  2389. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  2390. if (disk_bytenr == 0)
  2391. continue;
  2392. ret = __btrfs_free_extent(trans, root, disk_bytenr,
  2393. btrfs_file_extent_disk_num_bytes(leaf, fi),
  2394. leaf->start, leaf_owner, leaf_generation,
  2395. key.objectid, 0);
  2396. BUG_ON(ret);
  2397. atomic_inc(&root->fs_info->throttle_gen);
  2398. wake_up(&root->fs_info->transaction_throttle);
  2399. cond_resched();
  2400. }
  2401. return 0;
  2402. }
  2403. static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
  2404. struct btrfs_root *root,
  2405. struct btrfs_leaf_ref *ref)
  2406. {
  2407. int i;
  2408. int ret;
  2409. struct btrfs_extent_info *info = ref->extents;
  2410. for (i = 0; i < ref->nritems; i++) {
  2411. ret = __btrfs_free_extent(trans, root, info->bytenr,
  2412. info->num_bytes, ref->bytenr,
  2413. ref->owner, ref->generation,
  2414. info->objectid, 0);
  2415. atomic_inc(&root->fs_info->throttle_gen);
  2416. wake_up(&root->fs_info->transaction_throttle);
  2417. cond_resched();
  2418. BUG_ON(ret);
  2419. info++;
  2420. }
  2421. return 0;
  2422. }
  2423. int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
  2424. u32 *refs)
  2425. {
  2426. int ret;
  2427. ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
  2428. BUG_ON(ret);
  2429. #if 0 // some debugging code in case we see problems here
  2430. /* if the refs count is one, it won't get increased again. But
  2431. * if the ref count is > 1, someone may be decreasing it at
  2432. * the same time we are.
  2433. */
  2434. if (*refs != 1) {
  2435. struct extent_buffer *eb = NULL;
  2436. eb = btrfs_find_create_tree_block(root, start, len);
  2437. if (eb)
  2438. btrfs_tree_lock(eb);
  2439. mutex_lock(&root->fs_info->alloc_mutex);
  2440. ret = lookup_extent_ref(NULL, root, start, len, refs);
  2441. BUG_ON(ret);
  2442. mutex_unlock(&root->fs_info->alloc_mutex);
  2443. if (eb) {
  2444. btrfs_tree_unlock(eb);
  2445. free_extent_buffer(eb);
  2446. }
  2447. if (*refs == 1) {
  2448. printk("block %llu went down to one during drop_snap\n",
  2449. (unsigned long long)start);
  2450. }
  2451. }
  2452. #endif
  2453. cond_resched();
  2454. return ret;
  2455. }
  2456. /*
  2457. * helper function for drop_snapshot, this walks down the tree dropping ref
  2458. * counts as it goes.
  2459. */
  2460. static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
  2461. struct btrfs_root *root,
  2462. struct btrfs_path *path, int *level)
  2463. {
  2464. u64 root_owner;
  2465. u64 root_gen;
  2466. u64 bytenr;
  2467. u64 ptr_gen;
  2468. struct extent_buffer *next;
  2469. struct extent_buffer *cur;
  2470. struct extent_buffer *parent;
  2471. struct btrfs_leaf_ref *ref;
  2472. u32 blocksize;
  2473. int ret;
  2474. u32 refs;
  2475. WARN_ON(*level < 0);
  2476. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2477. ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
  2478. path->nodes[*level]->len, &refs);
  2479. BUG_ON(ret);
  2480. if (refs > 1)
  2481. goto out;
  2482. /*
  2483. * walk down to the last node level and free all the leaves
  2484. */
  2485. while(*level >= 0) {
  2486. WARN_ON(*level < 0);
  2487. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2488. cur = path->nodes[*level];
  2489. if (btrfs_header_level(cur) != *level)
  2490. WARN_ON(1);
  2491. if (path->slots[*level] >=
  2492. btrfs_header_nritems(cur))
  2493. break;
  2494. if (*level == 0) {
  2495. ret = btrfs_drop_leaf_ref(trans, root, cur);
  2496. BUG_ON(ret);
  2497. break;
  2498. }
  2499. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2500. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2501. blocksize = btrfs_level_size(root, *level - 1);
  2502. ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
  2503. BUG_ON(ret);
  2504. if (refs != 1) {
  2505. parent = path->nodes[*level];
  2506. root_owner = btrfs_header_owner(parent);
  2507. root_gen = btrfs_header_generation(parent);
  2508. path->slots[*level]++;
  2509. ret = __btrfs_free_extent(trans, root, bytenr,
  2510. blocksize, parent->start,
  2511. root_owner, root_gen,
  2512. *level - 1, 1);
  2513. BUG_ON(ret);
  2514. atomic_inc(&root->fs_info->throttle_gen);
  2515. wake_up(&root->fs_info->transaction_throttle);
  2516. cond_resched();
  2517. continue;
  2518. }
  2519. /*
  2520. * at this point, we have a single ref, and since the
  2521. * only place referencing this extent is a dead root
  2522. * the reference count should never go higher.
  2523. * So, we don't need to check it again
  2524. */
  2525. if (*level == 1) {
  2526. ref = btrfs_lookup_leaf_ref(root, bytenr);
  2527. if (ref && ref->generation != ptr_gen) {
  2528. btrfs_free_leaf_ref(root, ref);
  2529. ref = NULL;
  2530. }
  2531. if (ref) {
  2532. ret = cache_drop_leaf_ref(trans, root, ref);
  2533. BUG_ON(ret);
  2534. btrfs_remove_leaf_ref(root, ref);
  2535. btrfs_free_leaf_ref(root, ref);
  2536. *level = 0;
  2537. break;
  2538. }
  2539. if (printk_ratelimit()) {
  2540. printk("leaf ref miss for bytenr %llu\n",
  2541. (unsigned long long)bytenr);
  2542. }
  2543. }
  2544. next = btrfs_find_tree_block(root, bytenr, blocksize);
  2545. if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
  2546. free_extent_buffer(next);
  2547. next = read_tree_block(root, bytenr, blocksize,
  2548. ptr_gen);
  2549. cond_resched();
  2550. #if 0
  2551. /*
  2552. * this is a debugging check and can go away
  2553. * the ref should never go all the way down to 1
  2554. * at this point
  2555. */
  2556. ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
  2557. &refs);
  2558. BUG_ON(ret);
  2559. WARN_ON(refs != 1);
  2560. #endif
  2561. }
  2562. WARN_ON(*level <= 0);
  2563. if (path->nodes[*level-1])
  2564. free_extent_buffer(path->nodes[*level-1]);
  2565. path->nodes[*level-1] = next;
  2566. *level = btrfs_header_level(next);
  2567. path->slots[*level] = 0;
  2568. cond_resched();
  2569. }
  2570. out:
  2571. WARN_ON(*level < 0);
  2572. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2573. if (path->nodes[*level] == root->node) {
  2574. parent = path->nodes[*level];
  2575. bytenr = path->nodes[*level]->start;
  2576. } else {
  2577. parent = path->nodes[*level + 1];
  2578. bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
  2579. }
  2580. blocksize = btrfs_level_size(root, *level);
  2581. root_owner = btrfs_header_owner(parent);
  2582. root_gen = btrfs_header_generation(parent);
  2583. ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
  2584. parent->start, root_owner, root_gen,
  2585. *level, 1);
  2586. free_extent_buffer(path->nodes[*level]);
  2587. path->nodes[*level] = NULL;
  2588. *level += 1;
  2589. BUG_ON(ret);
  2590. cond_resched();
  2591. return 0;
  2592. }
  2593. /*
  2594. * helper function for drop_subtree, this function is similar to
  2595. * walk_down_tree. The main difference is that it checks reference
  2596. * counts while tree blocks are locked.
  2597. */
  2598. static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
  2599. struct btrfs_root *root,
  2600. struct btrfs_path *path, int *level)
  2601. {
  2602. struct extent_buffer *next;
  2603. struct extent_buffer *cur;
  2604. struct extent_buffer *parent;
  2605. u64 bytenr;
  2606. u64 ptr_gen;
  2607. u32 blocksize;
  2608. u32 refs;
  2609. int ret;
  2610. cur = path->nodes[*level];
  2611. ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
  2612. &refs);
  2613. BUG_ON(ret);
  2614. if (refs > 1)
  2615. goto out;
  2616. while (*level >= 0) {
  2617. cur = path->nodes[*level];
  2618. if (*level == 0) {
  2619. ret = btrfs_drop_leaf_ref(trans, root, cur);
  2620. BUG_ON(ret);
  2621. clean_tree_block(trans, root, cur);
  2622. break;
  2623. }
  2624. if (path->slots[*level] >= btrfs_header_nritems(cur)) {
  2625. clean_tree_block(trans, root, cur);
  2626. break;
  2627. }
  2628. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2629. blocksize = btrfs_level_size(root, *level - 1);
  2630. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2631. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  2632. btrfs_tree_lock(next);
  2633. ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
  2634. &refs);
  2635. BUG_ON(ret);
  2636. if (refs > 1) {
  2637. parent = path->nodes[*level];
  2638. ret = btrfs_free_extent(trans, root, bytenr,
  2639. blocksize, parent->start,
  2640. btrfs_header_owner(parent),
  2641. btrfs_header_generation(parent),
  2642. *level - 1, 1);
  2643. BUG_ON(ret);
  2644. path->slots[*level]++;
  2645. btrfs_tree_unlock(next);
  2646. free_extent_buffer(next);
  2647. continue;
  2648. }
  2649. *level = btrfs_header_level(next);
  2650. path->nodes[*level] = next;
  2651. path->slots[*level] = 0;
  2652. path->locks[*level] = 1;
  2653. cond_resched();
  2654. }
  2655. out:
  2656. parent = path->nodes[*level + 1];
  2657. bytenr = path->nodes[*level]->start;
  2658. blocksize = path->nodes[*level]->len;
  2659. ret = btrfs_free_extent(trans, root, bytenr, blocksize,
  2660. parent->start, btrfs_header_owner(parent),
  2661. btrfs_header_generation(parent), *level, 1);
  2662. BUG_ON(ret);
  2663. if (path->locks[*level]) {
  2664. btrfs_tree_unlock(path->nodes[*level]);
  2665. path->locks[*level] = 0;
  2666. }
  2667. free_extent_buffer(path->nodes[*level]);
  2668. path->nodes[*level] = NULL;
  2669. *level += 1;
  2670. cond_resched();
  2671. return 0;
  2672. }
  2673. /*
  2674. * helper for dropping snapshots. This walks back up the tree in the path
  2675. * to find the first node higher up where we haven't yet gone through
  2676. * all the slots
  2677. */
  2678. static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
  2679. struct btrfs_root *root,
  2680. struct btrfs_path *path,
  2681. int *level, int max_level)
  2682. {
  2683. u64 root_owner;
  2684. u64 root_gen;
  2685. struct btrfs_root_item *root_item = &root->root_item;
  2686. int i;
  2687. int slot;
  2688. int ret;
  2689. for (i = *level; i < max_level && path->nodes[i]; i++) {
  2690. slot = path->slots[i];
  2691. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  2692. struct extent_buffer *node;
  2693. struct btrfs_disk_key disk_key;
  2694. node = path->nodes[i];
  2695. path->slots[i]++;
  2696. *level = i;
  2697. WARN_ON(*level == 0);
  2698. btrfs_node_key(node, &disk_key, path->slots[i]);
  2699. memcpy(&root_item->drop_progress,
  2700. &disk_key, sizeof(disk_key));
  2701. root_item->drop_level = i;
  2702. return 0;
  2703. } else {
  2704. struct extent_buffer *parent;
  2705. if (path->nodes[*level] == root->node)
  2706. parent = path->nodes[*level];
  2707. else
  2708. parent = path->nodes[*level + 1];
  2709. root_owner = btrfs_header_owner(parent);
  2710. root_gen = btrfs_header_generation(parent);
  2711. clean_tree_block(trans, root, path->nodes[*level]);
  2712. ret = btrfs_free_extent(trans, root,
  2713. path->nodes[*level]->start,
  2714. path->nodes[*level]->len,
  2715. parent->start, root_owner,
  2716. root_gen, *level, 1);
  2717. BUG_ON(ret);
  2718. if (path->locks[*level]) {
  2719. btrfs_tree_unlock(path->nodes[*level]);
  2720. path->locks[*level] = 0;
  2721. }
  2722. free_extent_buffer(path->nodes[*level]);
  2723. path->nodes[*level] = NULL;
  2724. *level = i + 1;
  2725. }
  2726. }
  2727. return 1;
  2728. }
  2729. /*
  2730. * drop the reference count on the tree rooted at 'snap'. This traverses
  2731. * the tree freeing any blocks that have a ref count of zero after being
  2732. * decremented.
  2733. */
  2734. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  2735. *root)
  2736. {
  2737. int ret = 0;
  2738. int wret;
  2739. int level;
  2740. struct btrfs_path *path;
  2741. int i;
  2742. int orig_level;
  2743. struct btrfs_root_item *root_item = &root->root_item;
  2744. WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
  2745. path = btrfs_alloc_path();
  2746. BUG_ON(!path);
  2747. level = btrfs_header_level(root->node);
  2748. orig_level = level;
  2749. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  2750. path->nodes[level] = root->node;
  2751. extent_buffer_get(root->node);
  2752. path->slots[level] = 0;
  2753. } else {
  2754. struct btrfs_key key;
  2755. struct btrfs_disk_key found_key;
  2756. struct extent_buffer *node;
  2757. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  2758. level = root_item->drop_level;
  2759. path->lowest_level = level;
  2760. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2761. if (wret < 0) {
  2762. ret = wret;
  2763. goto out;
  2764. }
  2765. node = path->nodes[level];
  2766. btrfs_node_key(node, &found_key, path->slots[level]);
  2767. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  2768. sizeof(found_key)));
  2769. /*
  2770. * unlock our path, this is safe because only this
  2771. * function is allowed to delete this snapshot
  2772. */
  2773. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  2774. if (path->nodes[i] && path->locks[i]) {
  2775. path->locks[i] = 0;
  2776. btrfs_tree_unlock(path->nodes[i]);
  2777. }
  2778. }
  2779. }
  2780. while(1) {
  2781. wret = walk_down_tree(trans, root, path, &level);
  2782. if (wret > 0)
  2783. break;
  2784. if (wret < 0)
  2785. ret = wret;
  2786. wret = walk_up_tree(trans, root, path, &level,
  2787. BTRFS_MAX_LEVEL);
  2788. if (wret > 0)
  2789. break;
  2790. if (wret < 0)
  2791. ret = wret;
  2792. if (trans->transaction->in_commit) {
  2793. ret = -EAGAIN;
  2794. break;
  2795. }
  2796. atomic_inc(&root->fs_info->throttle_gen);
  2797. wake_up(&root->fs_info->transaction_throttle);
  2798. }
  2799. for (i = 0; i <= orig_level; i++) {
  2800. if (path->nodes[i]) {
  2801. free_extent_buffer(path->nodes[i]);
  2802. path->nodes[i] = NULL;
  2803. }
  2804. }
  2805. out:
  2806. btrfs_free_path(path);
  2807. return ret;
  2808. }
  2809. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  2810. struct btrfs_root *root,
  2811. struct extent_buffer *node,
  2812. struct extent_buffer *parent)
  2813. {
  2814. struct btrfs_path *path;
  2815. int level;
  2816. int parent_level;
  2817. int ret = 0;
  2818. int wret;
  2819. path = btrfs_alloc_path();
  2820. BUG_ON(!path);
  2821. BUG_ON(!btrfs_tree_locked(parent));
  2822. parent_level = btrfs_header_level(parent);
  2823. extent_buffer_get(parent);
  2824. path->nodes[parent_level] = parent;
  2825. path->slots[parent_level] = btrfs_header_nritems(parent);
  2826. BUG_ON(!btrfs_tree_locked(node));
  2827. level = btrfs_header_level(node);
  2828. extent_buffer_get(node);
  2829. path->nodes[level] = node;
  2830. path->slots[level] = 0;
  2831. while (1) {
  2832. wret = walk_down_subtree(trans, root, path, &level);
  2833. if (wret < 0)
  2834. ret = wret;
  2835. if (wret != 0)
  2836. break;
  2837. wret = walk_up_tree(trans, root, path, &level, parent_level);
  2838. if (wret < 0)
  2839. ret = wret;
  2840. if (wret != 0)
  2841. break;
  2842. }
  2843. btrfs_free_path(path);
  2844. return ret;
  2845. }
  2846. static unsigned long calc_ra(unsigned long start, unsigned long last,
  2847. unsigned long nr)
  2848. {
  2849. return min(last, start + nr - 1);
  2850. }
  2851. static int noinline relocate_inode_pages(struct inode *inode, u64 start,
  2852. u64 len)
  2853. {
  2854. u64 page_start;
  2855. u64 page_end;
  2856. unsigned long first_index;
  2857. unsigned long last_index;
  2858. unsigned long i;
  2859. struct page *page;
  2860. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2861. struct file_ra_state *ra;
  2862. struct btrfs_ordered_extent *ordered;
  2863. unsigned int total_read = 0;
  2864. unsigned int total_dirty = 0;
  2865. int ret = 0;
  2866. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  2867. mutex_lock(&inode->i_mutex);
  2868. first_index = start >> PAGE_CACHE_SHIFT;
  2869. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  2870. /* make sure the dirty trick played by the caller work */
  2871. ret = invalidate_inode_pages2_range(inode->i_mapping,
  2872. first_index, last_index);
  2873. if (ret)
  2874. goto out_unlock;
  2875. file_ra_state_init(ra, inode->i_mapping);
  2876. for (i = first_index ; i <= last_index; i++) {
  2877. if (total_read % ra->ra_pages == 0) {
  2878. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  2879. calc_ra(i, last_index, ra->ra_pages));
  2880. }
  2881. total_read++;
  2882. again:
  2883. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  2884. BUG_ON(1);
  2885. page = grab_cache_page(inode->i_mapping, i);
  2886. if (!page) {
  2887. ret = -ENOMEM;
  2888. goto out_unlock;
  2889. }
  2890. if (!PageUptodate(page)) {
  2891. btrfs_readpage(NULL, page);
  2892. lock_page(page);
  2893. if (!PageUptodate(page)) {
  2894. unlock_page(page);
  2895. page_cache_release(page);
  2896. ret = -EIO;
  2897. goto out_unlock;
  2898. }
  2899. }
  2900. wait_on_page_writeback(page);
  2901. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  2902. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2903. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2904. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2905. if (ordered) {
  2906. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2907. unlock_page(page);
  2908. page_cache_release(page);
  2909. btrfs_start_ordered_extent(inode, ordered, 1);
  2910. btrfs_put_ordered_extent(ordered);
  2911. goto again;
  2912. }
  2913. set_page_extent_mapped(page);
  2914. btrfs_set_extent_delalloc(inode, page_start, page_end);
  2915. if (i == first_index)
  2916. set_extent_bits(io_tree, page_start, page_end,
  2917. EXTENT_BOUNDARY, GFP_NOFS);
  2918. set_page_dirty(page);
  2919. total_dirty++;
  2920. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2921. unlock_page(page);
  2922. page_cache_release(page);
  2923. }
  2924. out_unlock:
  2925. kfree(ra);
  2926. mutex_unlock(&inode->i_mutex);
  2927. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  2928. return ret;
  2929. }
  2930. static int noinline relocate_data_extent(struct inode *reloc_inode,
  2931. struct btrfs_key *extent_key,
  2932. u64 offset)
  2933. {
  2934. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  2935. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  2936. struct extent_map *em;
  2937. u64 start = extent_key->objectid - offset;
  2938. u64 end = start + extent_key->offset - 1;
  2939. em = alloc_extent_map(GFP_NOFS);
  2940. BUG_ON(!em || IS_ERR(em));
  2941. em->start = start;
  2942. em->len = extent_key->offset;
  2943. em->block_len = extent_key->offset;
  2944. em->block_start = extent_key->objectid;
  2945. em->bdev = root->fs_info->fs_devices->latest_bdev;
  2946. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  2947. /* setup extent map to cheat btrfs_readpage */
  2948. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  2949. while (1) {
  2950. int ret;
  2951. spin_lock(&em_tree->lock);
  2952. ret = add_extent_mapping(em_tree, em);
  2953. spin_unlock(&em_tree->lock);
  2954. if (ret != -EEXIST) {
  2955. free_extent_map(em);
  2956. break;
  2957. }
  2958. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  2959. }
  2960. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  2961. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  2962. }
  2963. struct btrfs_ref_path {
  2964. u64 extent_start;
  2965. u64 nodes[BTRFS_MAX_LEVEL];
  2966. u64 root_objectid;
  2967. u64 root_generation;
  2968. u64 owner_objectid;
  2969. u32 num_refs;
  2970. int lowest_level;
  2971. int current_level;
  2972. int shared_level;
  2973. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  2974. u64 new_nodes[BTRFS_MAX_LEVEL];
  2975. };
  2976. struct disk_extent {
  2977. u64 ram_bytes;
  2978. u64 disk_bytenr;
  2979. u64 disk_num_bytes;
  2980. u64 offset;
  2981. u64 num_bytes;
  2982. u8 compression;
  2983. u8 encryption;
  2984. u16 other_encoding;
  2985. };
  2986. static int is_cowonly_root(u64 root_objectid)
  2987. {
  2988. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  2989. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  2990. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  2991. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  2992. root_objectid == BTRFS_TREE_LOG_OBJECTID)
  2993. return 1;
  2994. return 0;
  2995. }
  2996. static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
  2997. struct btrfs_root *extent_root,
  2998. struct btrfs_ref_path *ref_path,
  2999. int first_time)
  3000. {
  3001. struct extent_buffer *leaf;
  3002. struct btrfs_path *path;
  3003. struct btrfs_extent_ref *ref;
  3004. struct btrfs_key key;
  3005. struct btrfs_key found_key;
  3006. u64 bytenr;
  3007. u32 nritems;
  3008. int level;
  3009. int ret = 1;
  3010. path = btrfs_alloc_path();
  3011. if (!path)
  3012. return -ENOMEM;
  3013. if (first_time) {
  3014. ref_path->lowest_level = -1;
  3015. ref_path->current_level = -1;
  3016. ref_path->shared_level = -1;
  3017. goto walk_up;
  3018. }
  3019. walk_down:
  3020. level = ref_path->current_level - 1;
  3021. while (level >= -1) {
  3022. u64 parent;
  3023. if (level < ref_path->lowest_level)
  3024. break;
  3025. if (level >= 0) {
  3026. bytenr = ref_path->nodes[level];
  3027. } else {
  3028. bytenr = ref_path->extent_start;
  3029. }
  3030. BUG_ON(bytenr == 0);
  3031. parent = ref_path->nodes[level + 1];
  3032. ref_path->nodes[level + 1] = 0;
  3033. ref_path->current_level = level;
  3034. BUG_ON(parent == 0);
  3035. key.objectid = bytenr;
  3036. key.offset = parent + 1;
  3037. key.type = BTRFS_EXTENT_REF_KEY;
  3038. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3039. if (ret < 0)
  3040. goto out;
  3041. BUG_ON(ret == 0);
  3042. leaf = path->nodes[0];
  3043. nritems = btrfs_header_nritems(leaf);
  3044. if (path->slots[0] >= nritems) {
  3045. ret = btrfs_next_leaf(extent_root, path);
  3046. if (ret < 0)
  3047. goto out;
  3048. if (ret > 0)
  3049. goto next;
  3050. leaf = path->nodes[0];
  3051. }
  3052. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3053. if (found_key.objectid == bytenr &&
  3054. found_key.type == BTRFS_EXTENT_REF_KEY) {
  3055. if (level < ref_path->shared_level)
  3056. ref_path->shared_level = level;
  3057. goto found;
  3058. }
  3059. next:
  3060. level--;
  3061. btrfs_release_path(extent_root, path);
  3062. cond_resched();
  3063. }
  3064. /* reached lowest level */
  3065. ret = 1;
  3066. goto out;
  3067. walk_up:
  3068. level = ref_path->current_level;
  3069. while (level < BTRFS_MAX_LEVEL - 1) {
  3070. u64 ref_objectid;
  3071. if (level >= 0) {
  3072. bytenr = ref_path->nodes[level];
  3073. } else {
  3074. bytenr = ref_path->extent_start;
  3075. }
  3076. BUG_ON(bytenr == 0);
  3077. key.objectid = bytenr;
  3078. key.offset = 0;
  3079. key.type = BTRFS_EXTENT_REF_KEY;
  3080. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3081. if (ret < 0)
  3082. goto out;
  3083. leaf = path->nodes[0];
  3084. nritems = btrfs_header_nritems(leaf);
  3085. if (path->slots[0] >= nritems) {
  3086. ret = btrfs_next_leaf(extent_root, path);
  3087. if (ret < 0)
  3088. goto out;
  3089. if (ret > 0) {
  3090. /* the extent was freed by someone */
  3091. if (ref_path->lowest_level == level)
  3092. goto out;
  3093. btrfs_release_path(extent_root, path);
  3094. goto walk_down;
  3095. }
  3096. leaf = path->nodes[0];
  3097. }
  3098. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3099. if (found_key.objectid != bytenr ||
  3100. found_key.type != BTRFS_EXTENT_REF_KEY) {
  3101. /* the extent was freed by someone */
  3102. if (ref_path->lowest_level == level) {
  3103. ret = 1;
  3104. goto out;
  3105. }
  3106. btrfs_release_path(extent_root, path);
  3107. goto walk_down;
  3108. }
  3109. found:
  3110. ref = btrfs_item_ptr(leaf, path->slots[0],
  3111. struct btrfs_extent_ref);
  3112. ref_objectid = btrfs_ref_objectid(leaf, ref);
  3113. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  3114. if (first_time) {
  3115. level = (int)ref_objectid;
  3116. BUG_ON(level >= BTRFS_MAX_LEVEL);
  3117. ref_path->lowest_level = level;
  3118. ref_path->current_level = level;
  3119. ref_path->nodes[level] = bytenr;
  3120. } else {
  3121. WARN_ON(ref_objectid != level);
  3122. }
  3123. } else {
  3124. WARN_ON(level != -1);
  3125. }
  3126. first_time = 0;
  3127. if (ref_path->lowest_level == level) {
  3128. ref_path->owner_objectid = ref_objectid;
  3129. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  3130. }
  3131. /*
  3132. * the block is tree root or the block isn't in reference
  3133. * counted tree.
  3134. */
  3135. if (found_key.objectid == found_key.offset ||
  3136. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  3137. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  3138. ref_path->root_generation =
  3139. btrfs_ref_generation(leaf, ref);
  3140. if (level < 0) {
  3141. /* special reference from the tree log */
  3142. ref_path->nodes[0] = found_key.offset;
  3143. ref_path->current_level = 0;
  3144. }
  3145. ret = 0;
  3146. goto out;
  3147. }
  3148. level++;
  3149. BUG_ON(ref_path->nodes[level] != 0);
  3150. ref_path->nodes[level] = found_key.offset;
  3151. ref_path->current_level = level;
  3152. /*
  3153. * the reference was created in the running transaction,
  3154. * no need to continue walking up.
  3155. */
  3156. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  3157. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  3158. ref_path->root_generation =
  3159. btrfs_ref_generation(leaf, ref);
  3160. ret = 0;
  3161. goto out;
  3162. }
  3163. btrfs_release_path(extent_root, path);
  3164. cond_resched();
  3165. }
  3166. /* reached max tree level, but no tree root found. */
  3167. BUG();
  3168. out:
  3169. btrfs_free_path(path);
  3170. return ret;
  3171. }
  3172. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  3173. struct btrfs_root *extent_root,
  3174. struct btrfs_ref_path *ref_path,
  3175. u64 extent_start)
  3176. {
  3177. memset(ref_path, 0, sizeof(*ref_path));
  3178. ref_path->extent_start = extent_start;
  3179. return __next_ref_path(trans, extent_root, ref_path, 1);
  3180. }
  3181. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  3182. struct btrfs_root *extent_root,
  3183. struct btrfs_ref_path *ref_path)
  3184. {
  3185. return __next_ref_path(trans, extent_root, ref_path, 0);
  3186. }
  3187. static int noinline get_new_locations(struct inode *reloc_inode,
  3188. struct btrfs_key *extent_key,
  3189. u64 offset, int no_fragment,
  3190. struct disk_extent **extents,
  3191. int *nr_extents)
  3192. {
  3193. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  3194. struct btrfs_path *path;
  3195. struct btrfs_file_extent_item *fi;
  3196. struct extent_buffer *leaf;
  3197. struct disk_extent *exts = *extents;
  3198. struct btrfs_key found_key;
  3199. u64 cur_pos;
  3200. u64 last_byte;
  3201. u32 nritems;
  3202. int nr = 0;
  3203. int max = *nr_extents;
  3204. int ret;
  3205. WARN_ON(!no_fragment && *extents);
  3206. if (!exts) {
  3207. max = 1;
  3208. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  3209. if (!exts)
  3210. return -ENOMEM;
  3211. }
  3212. path = btrfs_alloc_path();
  3213. BUG_ON(!path);
  3214. cur_pos = extent_key->objectid - offset;
  3215. last_byte = extent_key->objectid + extent_key->offset;
  3216. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  3217. cur_pos, 0);
  3218. if (ret < 0)
  3219. goto out;
  3220. if (ret > 0) {
  3221. ret = -ENOENT;
  3222. goto out;
  3223. }
  3224. while (1) {
  3225. leaf = path->nodes[0];
  3226. nritems = btrfs_header_nritems(leaf);
  3227. if (path->slots[0] >= nritems) {
  3228. ret = btrfs_next_leaf(root, path);
  3229. if (ret < 0)
  3230. goto out;
  3231. if (ret > 0)
  3232. break;
  3233. leaf = path->nodes[0];
  3234. }
  3235. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3236. if (found_key.offset != cur_pos ||
  3237. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  3238. found_key.objectid != reloc_inode->i_ino)
  3239. break;
  3240. fi = btrfs_item_ptr(leaf, path->slots[0],
  3241. struct btrfs_file_extent_item);
  3242. if (btrfs_file_extent_type(leaf, fi) !=
  3243. BTRFS_FILE_EXTENT_REG ||
  3244. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  3245. break;
  3246. if (nr == max) {
  3247. struct disk_extent *old = exts;
  3248. max *= 2;
  3249. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  3250. memcpy(exts, old, sizeof(*exts) * nr);
  3251. if (old != *extents)
  3252. kfree(old);
  3253. }
  3254. exts[nr].disk_bytenr =
  3255. btrfs_file_extent_disk_bytenr(leaf, fi);
  3256. exts[nr].disk_num_bytes =
  3257. btrfs_file_extent_disk_num_bytes(leaf, fi);
  3258. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  3259. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  3260. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  3261. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  3262. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  3263. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  3264. fi);
  3265. BUG_ON(exts[nr].offset > 0);
  3266. BUG_ON(exts[nr].compression || exts[nr].encryption);
  3267. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  3268. cur_pos += exts[nr].num_bytes;
  3269. nr++;
  3270. if (cur_pos + offset >= last_byte)
  3271. break;
  3272. if (no_fragment) {
  3273. ret = 1;
  3274. goto out;
  3275. }
  3276. path->slots[0]++;
  3277. }
  3278. WARN_ON(cur_pos + offset > last_byte);
  3279. if (cur_pos + offset < last_byte) {
  3280. ret = -ENOENT;
  3281. goto out;
  3282. }
  3283. ret = 0;
  3284. out:
  3285. btrfs_free_path(path);
  3286. if (ret) {
  3287. if (exts != *extents)
  3288. kfree(exts);
  3289. } else {
  3290. *extents = exts;
  3291. *nr_extents = nr;
  3292. }
  3293. return ret;
  3294. }
  3295. static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
  3296. struct btrfs_root *root,
  3297. struct btrfs_path *path,
  3298. struct btrfs_key *extent_key,
  3299. struct btrfs_key *leaf_key,
  3300. struct btrfs_ref_path *ref_path,
  3301. struct disk_extent *new_extents,
  3302. int nr_extents)
  3303. {
  3304. struct extent_buffer *leaf;
  3305. struct btrfs_file_extent_item *fi;
  3306. struct inode *inode = NULL;
  3307. struct btrfs_key key;
  3308. u64 lock_start = 0;
  3309. u64 lock_end = 0;
  3310. u64 num_bytes;
  3311. u64 ext_offset;
  3312. u64 first_pos;
  3313. u32 nritems;
  3314. int nr_scaned = 0;
  3315. int extent_locked = 0;
  3316. int extent_type;
  3317. int ret;
  3318. memcpy(&key, leaf_key, sizeof(key));
  3319. first_pos = INT_LIMIT(loff_t) - extent_key->offset;
  3320. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  3321. if (key.objectid < ref_path->owner_objectid ||
  3322. (key.objectid == ref_path->owner_objectid &&
  3323. key.type < BTRFS_EXTENT_DATA_KEY)) {
  3324. key.objectid = ref_path->owner_objectid;
  3325. key.type = BTRFS_EXTENT_DATA_KEY;
  3326. key.offset = 0;
  3327. }
  3328. }
  3329. while (1) {
  3330. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3331. if (ret < 0)
  3332. goto out;
  3333. leaf = path->nodes[0];
  3334. nritems = btrfs_header_nritems(leaf);
  3335. next:
  3336. if (extent_locked && ret > 0) {
  3337. /*
  3338. * the file extent item was modified by someone
  3339. * before the extent got locked.
  3340. */
  3341. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3342. lock_end, GFP_NOFS);
  3343. extent_locked = 0;
  3344. }
  3345. if (path->slots[0] >= nritems) {
  3346. if (++nr_scaned > 2)
  3347. break;
  3348. BUG_ON(extent_locked);
  3349. ret = btrfs_next_leaf(root, path);
  3350. if (ret < 0)
  3351. goto out;
  3352. if (ret > 0)
  3353. break;
  3354. leaf = path->nodes[0];
  3355. nritems = btrfs_header_nritems(leaf);
  3356. }
  3357. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3358. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  3359. if ((key.objectid > ref_path->owner_objectid) ||
  3360. (key.objectid == ref_path->owner_objectid &&
  3361. key.type > BTRFS_EXTENT_DATA_KEY) ||
  3362. (key.offset >= first_pos + extent_key->offset))
  3363. break;
  3364. }
  3365. if (inode && key.objectid != inode->i_ino) {
  3366. BUG_ON(extent_locked);
  3367. btrfs_release_path(root, path);
  3368. mutex_unlock(&inode->i_mutex);
  3369. iput(inode);
  3370. inode = NULL;
  3371. continue;
  3372. }
  3373. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  3374. path->slots[0]++;
  3375. ret = 1;
  3376. goto next;
  3377. }
  3378. fi = btrfs_item_ptr(leaf, path->slots[0],
  3379. struct btrfs_file_extent_item);
  3380. extent_type = btrfs_file_extent_type(leaf, fi);
  3381. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  3382. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  3383. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  3384. extent_key->objectid)) {
  3385. path->slots[0]++;
  3386. ret = 1;
  3387. goto next;
  3388. }
  3389. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  3390. ext_offset = btrfs_file_extent_offset(leaf, fi);
  3391. if (first_pos > key.offset - ext_offset)
  3392. first_pos = key.offset - ext_offset;
  3393. if (!extent_locked) {
  3394. lock_start = key.offset;
  3395. lock_end = lock_start + num_bytes - 1;
  3396. } else {
  3397. if (lock_start > key.offset ||
  3398. lock_end + 1 < key.offset + num_bytes) {
  3399. unlock_extent(&BTRFS_I(inode)->io_tree,
  3400. lock_start, lock_end, GFP_NOFS);
  3401. extent_locked = 0;
  3402. }
  3403. }
  3404. if (!inode) {
  3405. btrfs_release_path(root, path);
  3406. inode = btrfs_iget_locked(root->fs_info->sb,
  3407. key.objectid, root);
  3408. if (inode->i_state & I_NEW) {
  3409. BTRFS_I(inode)->root = root;
  3410. BTRFS_I(inode)->location.objectid =
  3411. key.objectid;
  3412. BTRFS_I(inode)->location.type =
  3413. BTRFS_INODE_ITEM_KEY;
  3414. BTRFS_I(inode)->location.offset = 0;
  3415. btrfs_read_locked_inode(inode);
  3416. unlock_new_inode(inode);
  3417. }
  3418. /*
  3419. * some code call btrfs_commit_transaction while
  3420. * holding the i_mutex, so we can't use mutex_lock
  3421. * here.
  3422. */
  3423. if (is_bad_inode(inode) ||
  3424. !mutex_trylock(&inode->i_mutex)) {
  3425. iput(inode);
  3426. inode = NULL;
  3427. key.offset = (u64)-1;
  3428. goto skip;
  3429. }
  3430. }
  3431. if (!extent_locked) {
  3432. struct btrfs_ordered_extent *ordered;
  3433. btrfs_release_path(root, path);
  3434. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3435. lock_end, GFP_NOFS);
  3436. ordered = btrfs_lookup_first_ordered_extent(inode,
  3437. lock_end);
  3438. if (ordered &&
  3439. ordered->file_offset <= lock_end &&
  3440. ordered->file_offset + ordered->len > lock_start) {
  3441. unlock_extent(&BTRFS_I(inode)->io_tree,
  3442. lock_start, lock_end, GFP_NOFS);
  3443. btrfs_start_ordered_extent(inode, ordered, 1);
  3444. btrfs_put_ordered_extent(ordered);
  3445. key.offset += num_bytes;
  3446. goto skip;
  3447. }
  3448. if (ordered)
  3449. btrfs_put_ordered_extent(ordered);
  3450. extent_locked = 1;
  3451. continue;
  3452. }
  3453. if (nr_extents == 1) {
  3454. /* update extent pointer in place */
  3455. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  3456. new_extents[0].disk_bytenr);
  3457. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  3458. new_extents[0].disk_num_bytes);
  3459. btrfs_mark_buffer_dirty(leaf);
  3460. btrfs_drop_extent_cache(inode, key.offset,
  3461. key.offset + num_bytes - 1, 0);
  3462. ret = btrfs_inc_extent_ref(trans, root,
  3463. new_extents[0].disk_bytenr,
  3464. new_extents[0].disk_num_bytes,
  3465. leaf->start,
  3466. root->root_key.objectid,
  3467. trans->transid,
  3468. key.objectid);
  3469. BUG_ON(ret);
  3470. ret = btrfs_free_extent(trans, root,
  3471. extent_key->objectid,
  3472. extent_key->offset,
  3473. leaf->start,
  3474. btrfs_header_owner(leaf),
  3475. btrfs_header_generation(leaf),
  3476. key.objectid, 0);
  3477. BUG_ON(ret);
  3478. btrfs_release_path(root, path);
  3479. key.offset += num_bytes;
  3480. } else {
  3481. BUG_ON(1);
  3482. #if 0
  3483. u64 alloc_hint;
  3484. u64 extent_len;
  3485. int i;
  3486. /*
  3487. * drop old extent pointer at first, then insert the
  3488. * new pointers one bye one
  3489. */
  3490. btrfs_release_path(root, path);
  3491. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  3492. key.offset + num_bytes,
  3493. key.offset, &alloc_hint);
  3494. BUG_ON(ret);
  3495. for (i = 0; i < nr_extents; i++) {
  3496. if (ext_offset >= new_extents[i].num_bytes) {
  3497. ext_offset -= new_extents[i].num_bytes;
  3498. continue;
  3499. }
  3500. extent_len = min(new_extents[i].num_bytes -
  3501. ext_offset, num_bytes);
  3502. ret = btrfs_insert_empty_item(trans, root,
  3503. path, &key,
  3504. sizeof(*fi));
  3505. BUG_ON(ret);
  3506. leaf = path->nodes[0];
  3507. fi = btrfs_item_ptr(leaf, path->slots[0],
  3508. struct btrfs_file_extent_item);
  3509. btrfs_set_file_extent_generation(leaf, fi,
  3510. trans->transid);
  3511. btrfs_set_file_extent_type(leaf, fi,
  3512. BTRFS_FILE_EXTENT_REG);
  3513. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  3514. new_extents[i].disk_bytenr);
  3515. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  3516. new_extents[i].disk_num_bytes);
  3517. btrfs_set_file_extent_ram_bytes(leaf, fi,
  3518. new_extents[i].ram_bytes);
  3519. btrfs_set_file_extent_compression(leaf, fi,
  3520. new_extents[i].compression);
  3521. btrfs_set_file_extent_encryption(leaf, fi,
  3522. new_extents[i].encryption);
  3523. btrfs_set_file_extent_other_encoding(leaf, fi,
  3524. new_extents[i].other_encoding);
  3525. btrfs_set_file_extent_num_bytes(leaf, fi,
  3526. extent_len);
  3527. ext_offset += new_extents[i].offset;
  3528. btrfs_set_file_extent_offset(leaf, fi,
  3529. ext_offset);
  3530. btrfs_mark_buffer_dirty(leaf);
  3531. btrfs_drop_extent_cache(inode, key.offset,
  3532. key.offset + extent_len - 1, 0);
  3533. ret = btrfs_inc_extent_ref(trans, root,
  3534. new_extents[i].disk_bytenr,
  3535. new_extents[i].disk_num_bytes,
  3536. leaf->start,
  3537. root->root_key.objectid,
  3538. trans->transid, key.objectid);
  3539. BUG_ON(ret);
  3540. btrfs_release_path(root, path);
  3541. inode_add_bytes(inode, extent_len);
  3542. ext_offset = 0;
  3543. num_bytes -= extent_len;
  3544. key.offset += extent_len;
  3545. if (num_bytes == 0)
  3546. break;
  3547. }
  3548. BUG_ON(i >= nr_extents);
  3549. #endif
  3550. }
  3551. if (extent_locked) {
  3552. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3553. lock_end, GFP_NOFS);
  3554. extent_locked = 0;
  3555. }
  3556. skip:
  3557. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  3558. key.offset >= first_pos + extent_key->offset)
  3559. break;
  3560. cond_resched();
  3561. }
  3562. ret = 0;
  3563. out:
  3564. btrfs_release_path(root, path);
  3565. if (inode) {
  3566. mutex_unlock(&inode->i_mutex);
  3567. if (extent_locked) {
  3568. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3569. lock_end, GFP_NOFS);
  3570. }
  3571. iput(inode);
  3572. }
  3573. return ret;
  3574. }
  3575. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  3576. struct btrfs_root *root,
  3577. struct extent_buffer *buf, u64 orig_start)
  3578. {
  3579. int level;
  3580. int ret;
  3581. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  3582. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  3583. level = btrfs_header_level(buf);
  3584. if (level == 0) {
  3585. struct btrfs_leaf_ref *ref;
  3586. struct btrfs_leaf_ref *orig_ref;
  3587. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  3588. if (!orig_ref)
  3589. return -ENOENT;
  3590. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  3591. if (!ref) {
  3592. btrfs_free_leaf_ref(root, orig_ref);
  3593. return -ENOMEM;
  3594. }
  3595. ref->nritems = orig_ref->nritems;
  3596. memcpy(ref->extents, orig_ref->extents,
  3597. sizeof(ref->extents[0]) * ref->nritems);
  3598. btrfs_free_leaf_ref(root, orig_ref);
  3599. ref->root_gen = trans->transid;
  3600. ref->bytenr = buf->start;
  3601. ref->owner = btrfs_header_owner(buf);
  3602. ref->generation = btrfs_header_generation(buf);
  3603. ret = btrfs_add_leaf_ref(root, ref, 0);
  3604. WARN_ON(ret);
  3605. btrfs_free_leaf_ref(root, ref);
  3606. }
  3607. return 0;
  3608. }
  3609. static int noinline invalidate_extent_cache(struct btrfs_root *root,
  3610. struct extent_buffer *leaf,
  3611. struct btrfs_block_group_cache *group,
  3612. struct btrfs_root *target_root)
  3613. {
  3614. struct btrfs_key key;
  3615. struct inode *inode = NULL;
  3616. struct btrfs_file_extent_item *fi;
  3617. u64 num_bytes;
  3618. u64 skip_objectid = 0;
  3619. u32 nritems;
  3620. u32 i;
  3621. nritems = btrfs_header_nritems(leaf);
  3622. for (i = 0; i < nritems; i++) {
  3623. btrfs_item_key_to_cpu(leaf, &key, i);
  3624. if (key.objectid == skip_objectid ||
  3625. key.type != BTRFS_EXTENT_DATA_KEY)
  3626. continue;
  3627. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  3628. if (btrfs_file_extent_type(leaf, fi) ==
  3629. BTRFS_FILE_EXTENT_INLINE)
  3630. continue;
  3631. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  3632. continue;
  3633. if (!inode || inode->i_ino != key.objectid) {
  3634. iput(inode);
  3635. inode = btrfs_ilookup(target_root->fs_info->sb,
  3636. key.objectid, target_root, 1);
  3637. }
  3638. if (!inode) {
  3639. skip_objectid = key.objectid;
  3640. continue;
  3641. }
  3642. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  3643. lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  3644. key.offset + num_bytes - 1, GFP_NOFS);
  3645. btrfs_drop_extent_cache(inode, key.offset,
  3646. key.offset + num_bytes - 1, 1);
  3647. unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  3648. key.offset + num_bytes - 1, GFP_NOFS);
  3649. cond_resched();
  3650. }
  3651. iput(inode);
  3652. return 0;
  3653. }
  3654. static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  3655. struct btrfs_root *root,
  3656. struct extent_buffer *leaf,
  3657. struct btrfs_block_group_cache *group,
  3658. struct inode *reloc_inode)
  3659. {
  3660. struct btrfs_key key;
  3661. struct btrfs_key extent_key;
  3662. struct btrfs_file_extent_item *fi;
  3663. struct btrfs_leaf_ref *ref;
  3664. struct disk_extent *new_extent;
  3665. u64 bytenr;
  3666. u64 num_bytes;
  3667. u32 nritems;
  3668. u32 i;
  3669. int ext_index;
  3670. int nr_extent;
  3671. int ret;
  3672. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  3673. BUG_ON(!new_extent);
  3674. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  3675. BUG_ON(!ref);
  3676. ext_index = -1;
  3677. nritems = btrfs_header_nritems(leaf);
  3678. for (i = 0; i < nritems; i++) {
  3679. btrfs_item_key_to_cpu(leaf, &key, i);
  3680. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  3681. continue;
  3682. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  3683. if (btrfs_file_extent_type(leaf, fi) ==
  3684. BTRFS_FILE_EXTENT_INLINE)
  3685. continue;
  3686. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  3687. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  3688. if (bytenr == 0)
  3689. continue;
  3690. ext_index++;
  3691. if (bytenr >= group->key.objectid + group->key.offset ||
  3692. bytenr + num_bytes <= group->key.objectid)
  3693. continue;
  3694. extent_key.objectid = bytenr;
  3695. extent_key.offset = num_bytes;
  3696. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  3697. nr_extent = 1;
  3698. ret = get_new_locations(reloc_inode, &extent_key,
  3699. group->key.objectid, 1,
  3700. &new_extent, &nr_extent);
  3701. if (ret > 0)
  3702. continue;
  3703. BUG_ON(ret < 0);
  3704. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  3705. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  3706. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  3707. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  3708. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  3709. new_extent->disk_bytenr);
  3710. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  3711. new_extent->disk_num_bytes);
  3712. btrfs_mark_buffer_dirty(leaf);
  3713. ret = btrfs_inc_extent_ref(trans, root,
  3714. new_extent->disk_bytenr,
  3715. new_extent->disk_num_bytes,
  3716. leaf->start,
  3717. root->root_key.objectid,
  3718. trans->transid, key.objectid);
  3719. BUG_ON(ret);
  3720. ret = btrfs_free_extent(trans, root,
  3721. bytenr, num_bytes, leaf->start,
  3722. btrfs_header_owner(leaf),
  3723. btrfs_header_generation(leaf),
  3724. key.objectid, 0);
  3725. BUG_ON(ret);
  3726. cond_resched();
  3727. }
  3728. kfree(new_extent);
  3729. BUG_ON(ext_index + 1 != ref->nritems);
  3730. btrfs_free_leaf_ref(root, ref);
  3731. return 0;
  3732. }
  3733. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  3734. struct btrfs_root *root)
  3735. {
  3736. struct btrfs_root *reloc_root;
  3737. int ret;
  3738. if (root->reloc_root) {
  3739. reloc_root = root->reloc_root;
  3740. root->reloc_root = NULL;
  3741. list_add(&reloc_root->dead_list,
  3742. &root->fs_info->dead_reloc_roots);
  3743. btrfs_set_root_bytenr(&reloc_root->root_item,
  3744. reloc_root->node->start);
  3745. btrfs_set_root_level(&root->root_item,
  3746. btrfs_header_level(reloc_root->node));
  3747. memset(&reloc_root->root_item.drop_progress, 0,
  3748. sizeof(struct btrfs_disk_key));
  3749. reloc_root->root_item.drop_level = 0;
  3750. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3751. &reloc_root->root_key,
  3752. &reloc_root->root_item);
  3753. BUG_ON(ret);
  3754. }
  3755. return 0;
  3756. }
  3757. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  3758. {
  3759. struct btrfs_trans_handle *trans;
  3760. struct btrfs_root *reloc_root;
  3761. struct btrfs_root *prev_root = NULL;
  3762. struct list_head dead_roots;
  3763. int ret;
  3764. unsigned long nr;
  3765. INIT_LIST_HEAD(&dead_roots);
  3766. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  3767. while (!list_empty(&dead_roots)) {
  3768. reloc_root = list_entry(dead_roots.prev,
  3769. struct btrfs_root, dead_list);
  3770. list_del_init(&reloc_root->dead_list);
  3771. BUG_ON(reloc_root->commit_root != NULL);
  3772. while (1) {
  3773. trans = btrfs_join_transaction(root, 1);
  3774. BUG_ON(!trans);
  3775. mutex_lock(&root->fs_info->drop_mutex);
  3776. ret = btrfs_drop_snapshot(trans, reloc_root);
  3777. if (ret != -EAGAIN)
  3778. break;
  3779. mutex_unlock(&root->fs_info->drop_mutex);
  3780. nr = trans->blocks_used;
  3781. ret = btrfs_end_transaction(trans, root);
  3782. BUG_ON(ret);
  3783. btrfs_btree_balance_dirty(root, nr);
  3784. }
  3785. free_extent_buffer(reloc_root->node);
  3786. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  3787. &reloc_root->root_key);
  3788. BUG_ON(ret);
  3789. mutex_unlock(&root->fs_info->drop_mutex);
  3790. nr = trans->blocks_used;
  3791. ret = btrfs_end_transaction(trans, root);
  3792. BUG_ON(ret);
  3793. btrfs_btree_balance_dirty(root, nr);
  3794. kfree(prev_root);
  3795. prev_root = reloc_root;
  3796. }
  3797. if (prev_root) {
  3798. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  3799. kfree(prev_root);
  3800. }
  3801. return 0;
  3802. }
  3803. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  3804. {
  3805. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  3806. return 0;
  3807. }
  3808. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  3809. {
  3810. struct btrfs_root *reloc_root;
  3811. struct btrfs_trans_handle *trans;
  3812. struct btrfs_key location;
  3813. int found;
  3814. int ret;
  3815. mutex_lock(&root->fs_info->tree_reloc_mutex);
  3816. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  3817. BUG_ON(ret);
  3818. found = !list_empty(&root->fs_info->dead_reloc_roots);
  3819. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  3820. if (found) {
  3821. trans = btrfs_start_transaction(root, 1);
  3822. BUG_ON(!trans);
  3823. ret = btrfs_commit_transaction(trans, root);
  3824. BUG_ON(ret);
  3825. }
  3826. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  3827. location.offset = (u64)-1;
  3828. location.type = BTRFS_ROOT_ITEM_KEY;
  3829. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  3830. BUG_ON(!reloc_root);
  3831. btrfs_orphan_cleanup(reloc_root);
  3832. return 0;
  3833. }
  3834. static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
  3835. struct btrfs_root *root)
  3836. {
  3837. struct btrfs_root *reloc_root;
  3838. struct extent_buffer *eb;
  3839. struct btrfs_root_item *root_item;
  3840. struct btrfs_key root_key;
  3841. int ret;
  3842. BUG_ON(!root->ref_cows);
  3843. if (root->reloc_root)
  3844. return 0;
  3845. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  3846. BUG_ON(!root_item);
  3847. ret = btrfs_copy_root(trans, root, root->commit_root,
  3848. &eb, BTRFS_TREE_RELOC_OBJECTID);
  3849. BUG_ON(ret);
  3850. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  3851. root_key.offset = root->root_key.objectid;
  3852. root_key.type = BTRFS_ROOT_ITEM_KEY;
  3853. memcpy(root_item, &root->root_item, sizeof(root_item));
  3854. btrfs_set_root_refs(root_item, 0);
  3855. btrfs_set_root_bytenr(root_item, eb->start);
  3856. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  3857. btrfs_set_root_generation(root_item, trans->transid);
  3858. btrfs_tree_unlock(eb);
  3859. free_extent_buffer(eb);
  3860. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  3861. &root_key, root_item);
  3862. BUG_ON(ret);
  3863. kfree(root_item);
  3864. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  3865. &root_key);
  3866. BUG_ON(!reloc_root);
  3867. reloc_root->last_trans = trans->transid;
  3868. reloc_root->commit_root = NULL;
  3869. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  3870. root->reloc_root = reloc_root;
  3871. return 0;
  3872. }
  3873. /*
  3874. * Core function of space balance.
  3875. *
  3876. * The idea is using reloc trees to relocate tree blocks in reference
  3877. * counted roots. There is one reloc tree for each subvol, and all
  3878. * reloc trees share same root key objectid. Reloc trees are snapshots
  3879. * of the latest committed roots of subvols (root->commit_root).
  3880. *
  3881. * To relocate a tree block referenced by a subvol, there are two steps.
  3882. * COW the block through subvol's reloc tree, then update block pointer
  3883. * in the subvol to point to the new block. Since all reloc trees share
  3884. * same root key objectid, doing special handing for tree blocks owned
  3885. * by them is easy. Once a tree block has been COWed in one reloc tree,
  3886. * we can use the resulting new block directly when the same block is
  3887. * required to COW again through other reloc trees. By this way, relocated
  3888. * tree blocks are shared between reloc trees, so they are also shared
  3889. * between subvols.
  3890. */
  3891. static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
  3892. struct btrfs_root *root,
  3893. struct btrfs_path *path,
  3894. struct btrfs_key *first_key,
  3895. struct btrfs_ref_path *ref_path,
  3896. struct btrfs_block_group_cache *group,
  3897. struct inode *reloc_inode)
  3898. {
  3899. struct btrfs_root *reloc_root;
  3900. struct extent_buffer *eb = NULL;
  3901. struct btrfs_key *keys;
  3902. u64 *nodes;
  3903. int level;
  3904. int shared_level;
  3905. int lowest_level = 0;
  3906. int ret;
  3907. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  3908. lowest_level = ref_path->owner_objectid;
  3909. if (!root->ref_cows) {
  3910. path->lowest_level = lowest_level;
  3911. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  3912. BUG_ON(ret < 0);
  3913. path->lowest_level = 0;
  3914. btrfs_release_path(root, path);
  3915. return 0;
  3916. }
  3917. mutex_lock(&root->fs_info->tree_reloc_mutex);
  3918. ret = init_reloc_tree(trans, root);
  3919. BUG_ON(ret);
  3920. reloc_root = root->reloc_root;
  3921. shared_level = ref_path->shared_level;
  3922. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  3923. keys = ref_path->node_keys;
  3924. nodes = ref_path->new_nodes;
  3925. memset(&keys[shared_level + 1], 0,
  3926. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  3927. memset(&nodes[shared_level + 1], 0,
  3928. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  3929. if (nodes[lowest_level] == 0) {
  3930. path->lowest_level = lowest_level;
  3931. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  3932. 0, 1);
  3933. BUG_ON(ret);
  3934. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  3935. eb = path->nodes[level];
  3936. if (!eb || eb == reloc_root->node)
  3937. break;
  3938. nodes[level] = eb->start;
  3939. if (level == 0)
  3940. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  3941. else
  3942. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  3943. }
  3944. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  3945. eb = path->nodes[0];
  3946. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  3947. group, reloc_inode);
  3948. BUG_ON(ret);
  3949. }
  3950. btrfs_release_path(reloc_root, path);
  3951. } else {
  3952. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  3953. lowest_level);
  3954. BUG_ON(ret);
  3955. }
  3956. /*
  3957. * replace tree blocks in the fs tree with tree blocks in
  3958. * the reloc tree.
  3959. */
  3960. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  3961. BUG_ON(ret < 0);
  3962. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  3963. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  3964. 0, 0);
  3965. BUG_ON(ret);
  3966. extent_buffer_get(path->nodes[0]);
  3967. eb = path->nodes[0];
  3968. btrfs_release_path(reloc_root, path);
  3969. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  3970. BUG_ON(ret);
  3971. free_extent_buffer(eb);
  3972. }
  3973. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  3974. path->lowest_level = 0;
  3975. return 0;
  3976. }
  3977. static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
  3978. struct btrfs_root *root,
  3979. struct btrfs_path *path,
  3980. struct btrfs_key *first_key,
  3981. struct btrfs_ref_path *ref_path)
  3982. {
  3983. int ret;
  3984. ret = relocate_one_path(trans, root, path, first_key,
  3985. ref_path, NULL, NULL);
  3986. BUG_ON(ret);
  3987. if (root == root->fs_info->extent_root)
  3988. btrfs_extent_post_op(trans, root);
  3989. return 0;
  3990. }
  3991. static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
  3992. struct btrfs_root *extent_root,
  3993. struct btrfs_path *path,
  3994. struct btrfs_key *extent_key)
  3995. {
  3996. int ret;
  3997. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  3998. if (ret)
  3999. goto out;
  4000. ret = btrfs_del_item(trans, extent_root, path);
  4001. out:
  4002. btrfs_release_path(extent_root, path);
  4003. return ret;
  4004. }
  4005. static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
  4006. struct btrfs_ref_path *ref_path)
  4007. {
  4008. struct btrfs_key root_key;
  4009. root_key.objectid = ref_path->root_objectid;
  4010. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4011. if (is_cowonly_root(ref_path->root_objectid))
  4012. root_key.offset = 0;
  4013. else
  4014. root_key.offset = (u64)-1;
  4015. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  4016. }
  4017. static int noinline relocate_one_extent(struct btrfs_root *extent_root,
  4018. struct btrfs_path *path,
  4019. struct btrfs_key *extent_key,
  4020. struct btrfs_block_group_cache *group,
  4021. struct inode *reloc_inode, int pass)
  4022. {
  4023. struct btrfs_trans_handle *trans;
  4024. struct btrfs_root *found_root;
  4025. struct btrfs_ref_path *ref_path = NULL;
  4026. struct disk_extent *new_extents = NULL;
  4027. int nr_extents = 0;
  4028. int loops;
  4029. int ret;
  4030. int level;
  4031. struct btrfs_key first_key;
  4032. u64 prev_block = 0;
  4033. trans = btrfs_start_transaction(extent_root, 1);
  4034. BUG_ON(!trans);
  4035. if (extent_key->objectid == 0) {
  4036. ret = del_extent_zero(trans, extent_root, path, extent_key);
  4037. goto out;
  4038. }
  4039. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  4040. if (!ref_path) {
  4041. ret = -ENOMEM;
  4042. goto out;
  4043. }
  4044. for (loops = 0; ; loops++) {
  4045. if (loops == 0) {
  4046. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  4047. extent_key->objectid);
  4048. } else {
  4049. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  4050. }
  4051. if (ret < 0)
  4052. goto out;
  4053. if (ret > 0)
  4054. break;
  4055. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  4056. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  4057. continue;
  4058. found_root = read_ref_root(extent_root->fs_info, ref_path);
  4059. BUG_ON(!found_root);
  4060. /*
  4061. * for reference counted tree, only process reference paths
  4062. * rooted at the latest committed root.
  4063. */
  4064. if (found_root->ref_cows &&
  4065. ref_path->root_generation != found_root->root_key.offset)
  4066. continue;
  4067. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4068. if (pass == 0) {
  4069. /*
  4070. * copy data extents to new locations
  4071. */
  4072. u64 group_start = group->key.objectid;
  4073. ret = relocate_data_extent(reloc_inode,
  4074. extent_key,
  4075. group_start);
  4076. if (ret < 0)
  4077. goto out;
  4078. break;
  4079. }
  4080. level = 0;
  4081. } else {
  4082. level = ref_path->owner_objectid;
  4083. }
  4084. if (prev_block != ref_path->nodes[level]) {
  4085. struct extent_buffer *eb;
  4086. u64 block_start = ref_path->nodes[level];
  4087. u64 block_size = btrfs_level_size(found_root, level);
  4088. eb = read_tree_block(found_root, block_start,
  4089. block_size, 0);
  4090. btrfs_tree_lock(eb);
  4091. BUG_ON(level != btrfs_header_level(eb));
  4092. if (level == 0)
  4093. btrfs_item_key_to_cpu(eb, &first_key, 0);
  4094. else
  4095. btrfs_node_key_to_cpu(eb, &first_key, 0);
  4096. btrfs_tree_unlock(eb);
  4097. free_extent_buffer(eb);
  4098. prev_block = block_start;
  4099. }
  4100. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
  4101. pass >= 2) {
  4102. /*
  4103. * use fallback method to process the remaining
  4104. * references.
  4105. */
  4106. if (!new_extents) {
  4107. u64 group_start = group->key.objectid;
  4108. new_extents = kmalloc(sizeof(*new_extents),
  4109. GFP_NOFS);
  4110. nr_extents = 1;
  4111. ret = get_new_locations(reloc_inode,
  4112. extent_key,
  4113. group_start, 1,
  4114. &new_extents,
  4115. &nr_extents);
  4116. if (ret)
  4117. goto out;
  4118. }
  4119. btrfs_record_root_in_trans(found_root);
  4120. ret = replace_one_extent(trans, found_root,
  4121. path, extent_key,
  4122. &first_key, ref_path,
  4123. new_extents, nr_extents);
  4124. if (ret < 0)
  4125. goto out;
  4126. continue;
  4127. }
  4128. btrfs_record_root_in_trans(found_root);
  4129. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4130. ret = relocate_tree_block(trans, found_root, path,
  4131. &first_key, ref_path);
  4132. } else {
  4133. /*
  4134. * try to update data extent references while
  4135. * keeping metadata shared between snapshots.
  4136. */
  4137. ret = relocate_one_path(trans, found_root, path,
  4138. &first_key, ref_path,
  4139. group, reloc_inode);
  4140. }
  4141. if (ret < 0)
  4142. goto out;
  4143. }
  4144. ret = 0;
  4145. out:
  4146. btrfs_end_transaction(trans, extent_root);
  4147. kfree(new_extents);
  4148. kfree(ref_path);
  4149. return ret;
  4150. }
  4151. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  4152. {
  4153. u64 num_devices;
  4154. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  4155. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  4156. num_devices = root->fs_info->fs_devices->num_devices;
  4157. if (num_devices == 1) {
  4158. stripped |= BTRFS_BLOCK_GROUP_DUP;
  4159. stripped = flags & ~stripped;
  4160. /* turn raid0 into single device chunks */
  4161. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  4162. return stripped;
  4163. /* turn mirroring into duplication */
  4164. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  4165. BTRFS_BLOCK_GROUP_RAID10))
  4166. return stripped | BTRFS_BLOCK_GROUP_DUP;
  4167. return flags;
  4168. } else {
  4169. /* they already had raid on here, just return */
  4170. if (flags & stripped)
  4171. return flags;
  4172. stripped |= BTRFS_BLOCK_GROUP_DUP;
  4173. stripped = flags & ~stripped;
  4174. /* switch duplicated blocks with raid1 */
  4175. if (flags & BTRFS_BLOCK_GROUP_DUP)
  4176. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  4177. /* turn single device chunks into raid0 */
  4178. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  4179. }
  4180. return flags;
  4181. }
  4182. int __alloc_chunk_for_shrink(struct btrfs_root *root,
  4183. struct btrfs_block_group_cache *shrink_block_group,
  4184. int force)
  4185. {
  4186. struct btrfs_trans_handle *trans;
  4187. u64 new_alloc_flags;
  4188. u64 calc;
  4189. spin_lock(&shrink_block_group->lock);
  4190. if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
  4191. spin_unlock(&shrink_block_group->lock);
  4192. trans = btrfs_start_transaction(root, 1);
  4193. spin_lock(&shrink_block_group->lock);
  4194. new_alloc_flags = update_block_group_flags(root,
  4195. shrink_block_group->flags);
  4196. if (new_alloc_flags != shrink_block_group->flags) {
  4197. calc =
  4198. btrfs_block_group_used(&shrink_block_group->item);
  4199. } else {
  4200. calc = shrink_block_group->key.offset;
  4201. }
  4202. spin_unlock(&shrink_block_group->lock);
  4203. do_chunk_alloc(trans, root->fs_info->extent_root,
  4204. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  4205. btrfs_end_transaction(trans, root);
  4206. } else
  4207. spin_unlock(&shrink_block_group->lock);
  4208. return 0;
  4209. }
  4210. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  4211. struct btrfs_root *root,
  4212. u64 objectid, u64 size)
  4213. {
  4214. struct btrfs_path *path;
  4215. struct btrfs_inode_item *item;
  4216. struct extent_buffer *leaf;
  4217. int ret;
  4218. path = btrfs_alloc_path();
  4219. if (!path)
  4220. return -ENOMEM;
  4221. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  4222. if (ret)
  4223. goto out;
  4224. leaf = path->nodes[0];
  4225. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  4226. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  4227. btrfs_set_inode_generation(leaf, item, 1);
  4228. btrfs_set_inode_size(leaf, item, size);
  4229. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  4230. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
  4231. BTRFS_INODE_NOCOMPRESS);
  4232. btrfs_mark_buffer_dirty(leaf);
  4233. btrfs_release_path(root, path);
  4234. out:
  4235. btrfs_free_path(path);
  4236. return ret;
  4237. }
  4238. static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
  4239. struct btrfs_block_group_cache *group)
  4240. {
  4241. struct inode *inode = NULL;
  4242. struct btrfs_trans_handle *trans;
  4243. struct btrfs_root *root;
  4244. struct btrfs_key root_key;
  4245. u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
  4246. int err = 0;
  4247. root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  4248. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4249. root_key.offset = (u64)-1;
  4250. root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  4251. if (IS_ERR(root))
  4252. return ERR_CAST(root);
  4253. trans = btrfs_start_transaction(root, 1);
  4254. BUG_ON(!trans);
  4255. err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
  4256. if (err)
  4257. goto out;
  4258. err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
  4259. BUG_ON(err);
  4260. err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
  4261. group->key.offset, 0, group->key.offset,
  4262. 0, 0, 0);
  4263. BUG_ON(err);
  4264. inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
  4265. if (inode->i_state & I_NEW) {
  4266. BTRFS_I(inode)->root = root;
  4267. BTRFS_I(inode)->location.objectid = objectid;
  4268. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  4269. BTRFS_I(inode)->location.offset = 0;
  4270. btrfs_read_locked_inode(inode);
  4271. unlock_new_inode(inode);
  4272. BUG_ON(is_bad_inode(inode));
  4273. } else {
  4274. BUG_ON(1);
  4275. }
  4276. err = btrfs_orphan_add(trans, inode);
  4277. out:
  4278. btrfs_end_transaction(trans, root);
  4279. if (err) {
  4280. if (inode)
  4281. iput(inode);
  4282. inode = ERR_PTR(err);
  4283. }
  4284. return inode;
  4285. }
  4286. int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
  4287. {
  4288. struct btrfs_trans_handle *trans;
  4289. struct btrfs_path *path;
  4290. struct btrfs_fs_info *info = root->fs_info;
  4291. struct extent_buffer *leaf;
  4292. struct inode *reloc_inode;
  4293. struct btrfs_block_group_cache *block_group;
  4294. struct btrfs_key key;
  4295. u64 skipped;
  4296. u64 cur_byte;
  4297. u64 total_found;
  4298. u32 nritems;
  4299. int ret;
  4300. int progress;
  4301. int pass = 0;
  4302. root = root->fs_info->extent_root;
  4303. block_group = btrfs_lookup_block_group(info, group_start);
  4304. BUG_ON(!block_group);
  4305. printk("btrfs relocating block group %llu flags %llu\n",
  4306. (unsigned long long)block_group->key.objectid,
  4307. (unsigned long long)block_group->flags);
  4308. path = btrfs_alloc_path();
  4309. BUG_ON(!path);
  4310. reloc_inode = create_reloc_inode(info, block_group);
  4311. BUG_ON(IS_ERR(reloc_inode));
  4312. __alloc_chunk_for_shrink(root, block_group, 1);
  4313. block_group->ro = 1;
  4314. block_group->space_info->total_bytes -= block_group->key.offset;
  4315. btrfs_start_delalloc_inodes(info->tree_root);
  4316. btrfs_wait_ordered_extents(info->tree_root, 0);
  4317. again:
  4318. skipped = 0;
  4319. total_found = 0;
  4320. progress = 0;
  4321. key.objectid = block_group->key.objectid;
  4322. key.offset = 0;
  4323. key.type = 0;
  4324. cur_byte = key.objectid;
  4325. trans = btrfs_start_transaction(info->tree_root, 1);
  4326. btrfs_commit_transaction(trans, info->tree_root);
  4327. mutex_lock(&root->fs_info->cleaner_mutex);
  4328. btrfs_clean_old_snapshots(info->tree_root);
  4329. btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
  4330. mutex_unlock(&root->fs_info->cleaner_mutex);
  4331. while(1) {
  4332. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4333. if (ret < 0)
  4334. goto out;
  4335. next:
  4336. leaf = path->nodes[0];
  4337. nritems = btrfs_header_nritems(leaf);
  4338. if (path->slots[0] >= nritems) {
  4339. ret = btrfs_next_leaf(root, path);
  4340. if (ret < 0)
  4341. goto out;
  4342. if (ret == 1) {
  4343. ret = 0;
  4344. break;
  4345. }
  4346. leaf = path->nodes[0];
  4347. nritems = btrfs_header_nritems(leaf);
  4348. }
  4349. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4350. if (key.objectid >= block_group->key.objectid +
  4351. block_group->key.offset)
  4352. break;
  4353. if (progress && need_resched()) {
  4354. btrfs_release_path(root, path);
  4355. cond_resched();
  4356. progress = 0;
  4357. continue;
  4358. }
  4359. progress = 1;
  4360. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
  4361. key.objectid + key.offset <= cur_byte) {
  4362. path->slots[0]++;
  4363. goto next;
  4364. }
  4365. total_found++;
  4366. cur_byte = key.objectid + key.offset;
  4367. btrfs_release_path(root, path);
  4368. __alloc_chunk_for_shrink(root, block_group, 0);
  4369. ret = relocate_one_extent(root, path, &key, block_group,
  4370. reloc_inode, pass);
  4371. BUG_ON(ret < 0);
  4372. if (ret > 0)
  4373. skipped++;
  4374. key.objectid = cur_byte;
  4375. key.type = 0;
  4376. key.offset = 0;
  4377. }
  4378. btrfs_release_path(root, path);
  4379. if (pass == 0) {
  4380. btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
  4381. invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
  4382. WARN_ON(reloc_inode->i_mapping->nrpages);
  4383. }
  4384. if (total_found > 0) {
  4385. printk("btrfs found %llu extents in pass %d\n",
  4386. (unsigned long long)total_found, pass);
  4387. pass++;
  4388. if (total_found == skipped && pass > 2) {
  4389. iput(reloc_inode);
  4390. reloc_inode = create_reloc_inode(info, block_group);
  4391. pass = 0;
  4392. }
  4393. goto again;
  4394. }
  4395. /* delete reloc_inode */
  4396. iput(reloc_inode);
  4397. /* unpin extents in this range */
  4398. trans = btrfs_start_transaction(info->tree_root, 1);
  4399. btrfs_commit_transaction(trans, info->tree_root);
  4400. spin_lock(&block_group->lock);
  4401. WARN_ON(block_group->pinned > 0);
  4402. WARN_ON(block_group->reserved > 0);
  4403. WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
  4404. spin_unlock(&block_group->lock);
  4405. ret = 0;
  4406. out:
  4407. btrfs_free_path(path);
  4408. return ret;
  4409. }
  4410. int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
  4411. struct btrfs_key *key)
  4412. {
  4413. int ret = 0;
  4414. struct btrfs_key found_key;
  4415. struct extent_buffer *leaf;
  4416. int slot;
  4417. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  4418. if (ret < 0)
  4419. goto out;
  4420. while(1) {
  4421. slot = path->slots[0];
  4422. leaf = path->nodes[0];
  4423. if (slot >= btrfs_header_nritems(leaf)) {
  4424. ret = btrfs_next_leaf(root, path);
  4425. if (ret == 0)
  4426. continue;
  4427. if (ret < 0)
  4428. goto out;
  4429. break;
  4430. }
  4431. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  4432. if (found_key.objectid >= key->objectid &&
  4433. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  4434. ret = 0;
  4435. goto out;
  4436. }
  4437. path->slots[0]++;
  4438. }
  4439. ret = -ENOENT;
  4440. out:
  4441. return ret;
  4442. }
  4443. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  4444. {
  4445. struct btrfs_block_group_cache *block_group;
  4446. struct rb_node *n;
  4447. spin_lock(&info->block_group_cache_lock);
  4448. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  4449. block_group = rb_entry(n, struct btrfs_block_group_cache,
  4450. cache_node);
  4451. rb_erase(&block_group->cache_node,
  4452. &info->block_group_cache_tree);
  4453. spin_unlock(&info->block_group_cache_lock);
  4454. btrfs_remove_free_space_cache(block_group);
  4455. down_write(&block_group->space_info->groups_sem);
  4456. list_del(&block_group->list);
  4457. up_write(&block_group->space_info->groups_sem);
  4458. kfree(block_group);
  4459. spin_lock(&info->block_group_cache_lock);
  4460. }
  4461. spin_unlock(&info->block_group_cache_lock);
  4462. return 0;
  4463. }
  4464. int btrfs_read_block_groups(struct btrfs_root *root)
  4465. {
  4466. struct btrfs_path *path;
  4467. int ret;
  4468. struct btrfs_block_group_cache *cache;
  4469. struct btrfs_fs_info *info = root->fs_info;
  4470. struct btrfs_space_info *space_info;
  4471. struct btrfs_key key;
  4472. struct btrfs_key found_key;
  4473. struct extent_buffer *leaf;
  4474. root = info->extent_root;
  4475. key.objectid = 0;
  4476. key.offset = 0;
  4477. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  4478. path = btrfs_alloc_path();
  4479. if (!path)
  4480. return -ENOMEM;
  4481. while(1) {
  4482. ret = find_first_block_group(root, path, &key);
  4483. if (ret > 0) {
  4484. ret = 0;
  4485. goto error;
  4486. }
  4487. if (ret != 0)
  4488. goto error;
  4489. leaf = path->nodes[0];
  4490. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4491. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  4492. if (!cache) {
  4493. ret = -ENOMEM;
  4494. break;
  4495. }
  4496. spin_lock_init(&cache->lock);
  4497. mutex_init(&cache->alloc_mutex);
  4498. INIT_LIST_HEAD(&cache->list);
  4499. read_extent_buffer(leaf, &cache->item,
  4500. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4501. sizeof(cache->item));
  4502. memcpy(&cache->key, &found_key, sizeof(found_key));
  4503. key.objectid = found_key.objectid + found_key.offset;
  4504. btrfs_release_path(root, path);
  4505. cache->flags = btrfs_block_group_flags(&cache->item);
  4506. ret = update_space_info(info, cache->flags, found_key.offset,
  4507. btrfs_block_group_used(&cache->item),
  4508. &space_info);
  4509. BUG_ON(ret);
  4510. cache->space_info = space_info;
  4511. down_write(&space_info->groups_sem);
  4512. list_add_tail(&cache->list, &space_info->block_groups);
  4513. up_write(&space_info->groups_sem);
  4514. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  4515. BUG_ON(ret);
  4516. set_avail_alloc_bits(root->fs_info, cache->flags);
  4517. }
  4518. ret = 0;
  4519. error:
  4520. btrfs_free_path(path);
  4521. return ret;
  4522. }
  4523. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  4524. struct btrfs_root *root, u64 bytes_used,
  4525. u64 type, u64 chunk_objectid, u64 chunk_offset,
  4526. u64 size)
  4527. {
  4528. int ret;
  4529. struct btrfs_root *extent_root;
  4530. struct btrfs_block_group_cache *cache;
  4531. extent_root = root->fs_info->extent_root;
  4532. root->fs_info->last_trans_new_blockgroup = trans->transid;
  4533. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  4534. if (!cache)
  4535. return -ENOMEM;
  4536. cache->key.objectid = chunk_offset;
  4537. cache->key.offset = size;
  4538. spin_lock_init(&cache->lock);
  4539. mutex_init(&cache->alloc_mutex);
  4540. INIT_LIST_HEAD(&cache->list);
  4541. btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  4542. btrfs_set_block_group_used(&cache->item, bytes_used);
  4543. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  4544. cache->flags = type;
  4545. btrfs_set_block_group_flags(&cache->item, type);
  4546. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  4547. &cache->space_info);
  4548. BUG_ON(ret);
  4549. down_write(&cache->space_info->groups_sem);
  4550. list_add_tail(&cache->list, &cache->space_info->block_groups);
  4551. up_write(&cache->space_info->groups_sem);
  4552. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  4553. BUG_ON(ret);
  4554. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  4555. sizeof(cache->item));
  4556. BUG_ON(ret);
  4557. finish_current_insert(trans, extent_root, 0);
  4558. ret = del_pending_extents(trans, extent_root, 0);
  4559. BUG_ON(ret);
  4560. set_avail_alloc_bits(extent_root->fs_info, type);
  4561. return 0;
  4562. }
  4563. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  4564. struct btrfs_root *root, u64 group_start)
  4565. {
  4566. struct btrfs_path *path;
  4567. struct btrfs_block_group_cache *block_group;
  4568. struct btrfs_key key;
  4569. int ret;
  4570. root = root->fs_info->extent_root;
  4571. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  4572. BUG_ON(!block_group);
  4573. memcpy(&key, &block_group->key, sizeof(key));
  4574. path = btrfs_alloc_path();
  4575. BUG_ON(!path);
  4576. btrfs_remove_free_space_cache(block_group);
  4577. rb_erase(&block_group->cache_node,
  4578. &root->fs_info->block_group_cache_tree);
  4579. down_write(&block_group->space_info->groups_sem);
  4580. list_del(&block_group->list);
  4581. up_write(&block_group->space_info->groups_sem);
  4582. /*
  4583. memset(shrink_block_group, 0, sizeof(*shrink_block_group));
  4584. kfree(shrink_block_group);
  4585. */
  4586. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  4587. if (ret > 0)
  4588. ret = -EIO;
  4589. if (ret < 0)
  4590. goto out;
  4591. ret = btrfs_del_item(trans, root, path);
  4592. out:
  4593. btrfs_free_path(path);
  4594. return ret;
  4595. }