extent-tree.c 137 KB

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