extent-tree.c 134 KB

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