extent-tree.c 135 KB

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