extent-tree.c 136 KB

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