extent-tree.c 136 KB

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