extent-tree.c 138 KB

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