extent-tree.c 136 KB

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