extent-tree.c 136 KB

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