extent-tree.c 224 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548
  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 <linux/sort.h>
  23. #include <linux/rcupdate.h>
  24. #include <linux/kthread.h>
  25. #include <linux/slab.h>
  26. #include "compat.h"
  27. #include "hash.h"
  28. #include "ctree.h"
  29. #include "disk-io.h"
  30. #include "print-tree.h"
  31. #include "transaction.h"
  32. #include "volumes.h"
  33. #include "locking.h"
  34. #include "free-space-cache.h"
  35. static int update_block_group(struct btrfs_trans_handle *trans,
  36. struct btrfs_root *root,
  37. u64 bytenr, u64 num_bytes, int alloc);
  38. static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
  39. u64 num_bytes, int reserve, int sinfo);
  40. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  41. struct btrfs_root *root,
  42. u64 bytenr, u64 num_bytes, u64 parent,
  43. u64 root_objectid, u64 owner_objectid,
  44. u64 owner_offset, int refs_to_drop,
  45. struct btrfs_delayed_extent_op *extra_op);
  46. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  47. struct extent_buffer *leaf,
  48. struct btrfs_extent_item *ei);
  49. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  50. struct btrfs_root *root,
  51. u64 parent, u64 root_objectid,
  52. u64 flags, u64 owner, u64 offset,
  53. struct btrfs_key *ins, int ref_mod);
  54. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  55. struct btrfs_root *root,
  56. u64 parent, u64 root_objectid,
  57. u64 flags, struct btrfs_disk_key *key,
  58. int level, struct btrfs_key *ins);
  59. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  60. struct btrfs_root *extent_root, u64 alloc_bytes,
  61. u64 flags, int force);
  62. static int find_next_key(struct btrfs_path *path, int level,
  63. struct btrfs_key *key);
  64. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  65. int dump_block_groups);
  66. static noinline int
  67. block_group_cache_done(struct btrfs_block_group_cache *cache)
  68. {
  69. smp_mb();
  70. return cache->cached == BTRFS_CACHE_FINISHED;
  71. }
  72. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  73. {
  74. return (cache->flags & bits) == bits;
  75. }
  76. void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
  77. {
  78. atomic_inc(&cache->count);
  79. }
  80. void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
  81. {
  82. if (atomic_dec_and_test(&cache->count)) {
  83. WARN_ON(cache->pinned > 0);
  84. WARN_ON(cache->reserved > 0);
  85. WARN_ON(cache->reserved_pinned > 0);
  86. kfree(cache);
  87. }
  88. }
  89. /*
  90. * this adds the block group to the fs_info rb tree for the block group
  91. * cache
  92. */
  93. static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
  94. struct btrfs_block_group_cache *block_group)
  95. {
  96. struct rb_node **p;
  97. struct rb_node *parent = NULL;
  98. struct btrfs_block_group_cache *cache;
  99. spin_lock(&info->block_group_cache_lock);
  100. p = &info->block_group_cache_tree.rb_node;
  101. while (*p) {
  102. parent = *p;
  103. cache = rb_entry(parent, struct btrfs_block_group_cache,
  104. cache_node);
  105. if (block_group->key.objectid < cache->key.objectid) {
  106. p = &(*p)->rb_left;
  107. } else if (block_group->key.objectid > cache->key.objectid) {
  108. p = &(*p)->rb_right;
  109. } else {
  110. spin_unlock(&info->block_group_cache_lock);
  111. return -EEXIST;
  112. }
  113. }
  114. rb_link_node(&block_group->cache_node, parent, p);
  115. rb_insert_color(&block_group->cache_node,
  116. &info->block_group_cache_tree);
  117. spin_unlock(&info->block_group_cache_lock);
  118. return 0;
  119. }
  120. /*
  121. * This will return the block group at or after bytenr if contains is 0, else
  122. * it will return the block group that contains the bytenr
  123. */
  124. static struct btrfs_block_group_cache *
  125. block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
  126. int contains)
  127. {
  128. struct btrfs_block_group_cache *cache, *ret = NULL;
  129. struct rb_node *n;
  130. u64 end, start;
  131. spin_lock(&info->block_group_cache_lock);
  132. n = info->block_group_cache_tree.rb_node;
  133. while (n) {
  134. cache = rb_entry(n, struct btrfs_block_group_cache,
  135. cache_node);
  136. end = cache->key.objectid + cache->key.offset - 1;
  137. start = cache->key.objectid;
  138. if (bytenr < start) {
  139. if (!contains && (!ret || start < ret->key.objectid))
  140. ret = cache;
  141. n = n->rb_left;
  142. } else if (bytenr > start) {
  143. if (contains && bytenr <= end) {
  144. ret = cache;
  145. break;
  146. }
  147. n = n->rb_right;
  148. } else {
  149. ret = cache;
  150. break;
  151. }
  152. }
  153. if (ret)
  154. btrfs_get_block_group(ret);
  155. spin_unlock(&info->block_group_cache_lock);
  156. return ret;
  157. }
  158. static int add_excluded_extent(struct btrfs_root *root,
  159. u64 start, u64 num_bytes)
  160. {
  161. u64 end = start + num_bytes - 1;
  162. set_extent_bits(&root->fs_info->freed_extents[0],
  163. start, end, EXTENT_UPTODATE, GFP_NOFS);
  164. set_extent_bits(&root->fs_info->freed_extents[1],
  165. start, end, EXTENT_UPTODATE, GFP_NOFS);
  166. return 0;
  167. }
  168. static void free_excluded_extents(struct btrfs_root *root,
  169. struct btrfs_block_group_cache *cache)
  170. {
  171. u64 start, end;
  172. start = cache->key.objectid;
  173. end = start + cache->key.offset - 1;
  174. clear_extent_bits(&root->fs_info->freed_extents[0],
  175. start, end, EXTENT_UPTODATE, GFP_NOFS);
  176. clear_extent_bits(&root->fs_info->freed_extents[1],
  177. start, end, EXTENT_UPTODATE, GFP_NOFS);
  178. }
  179. static int exclude_super_stripes(struct btrfs_root *root,
  180. struct btrfs_block_group_cache *cache)
  181. {
  182. u64 bytenr;
  183. u64 *logical;
  184. int stripe_len;
  185. int i, nr, ret;
  186. if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
  187. stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
  188. cache->bytes_super += stripe_len;
  189. ret = add_excluded_extent(root, cache->key.objectid,
  190. stripe_len);
  191. BUG_ON(ret);
  192. }
  193. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  194. bytenr = btrfs_sb_offset(i);
  195. ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
  196. cache->key.objectid, bytenr,
  197. 0, &logical, &nr, &stripe_len);
  198. BUG_ON(ret);
  199. while (nr--) {
  200. cache->bytes_super += stripe_len;
  201. ret = add_excluded_extent(root, logical[nr],
  202. stripe_len);
  203. BUG_ON(ret);
  204. }
  205. kfree(logical);
  206. }
  207. return 0;
  208. }
  209. static struct btrfs_caching_control *
  210. get_caching_control(struct btrfs_block_group_cache *cache)
  211. {
  212. struct btrfs_caching_control *ctl;
  213. spin_lock(&cache->lock);
  214. if (cache->cached != BTRFS_CACHE_STARTED) {
  215. spin_unlock(&cache->lock);
  216. return NULL;
  217. }
  218. /* We're loading it the fast way, so we don't have a caching_ctl. */
  219. if (!cache->caching_ctl) {
  220. spin_unlock(&cache->lock);
  221. return NULL;
  222. }
  223. ctl = cache->caching_ctl;
  224. atomic_inc(&ctl->count);
  225. spin_unlock(&cache->lock);
  226. return ctl;
  227. }
  228. static void put_caching_control(struct btrfs_caching_control *ctl)
  229. {
  230. if (atomic_dec_and_test(&ctl->count))
  231. kfree(ctl);
  232. }
  233. /*
  234. * this is only called by cache_block_group, since we could have freed extents
  235. * we need to check the pinned_extents for any extents that can't be used yet
  236. * since their free space will be released as soon as the transaction commits.
  237. */
  238. static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
  239. struct btrfs_fs_info *info, u64 start, u64 end)
  240. {
  241. u64 extent_start, extent_end, size, total_added = 0;
  242. int ret;
  243. while (start < end) {
  244. ret = find_first_extent_bit(info->pinned_extents, start,
  245. &extent_start, &extent_end,
  246. EXTENT_DIRTY | EXTENT_UPTODATE);
  247. if (ret)
  248. break;
  249. if (extent_start <= start) {
  250. start = extent_end + 1;
  251. } else if (extent_start > start && extent_start < end) {
  252. size = extent_start - start;
  253. total_added += size;
  254. ret = btrfs_add_free_space(block_group, start,
  255. size);
  256. BUG_ON(ret);
  257. start = extent_end + 1;
  258. } else {
  259. break;
  260. }
  261. }
  262. if (start < end) {
  263. size = end - start;
  264. total_added += size;
  265. ret = btrfs_add_free_space(block_group, start, size);
  266. BUG_ON(ret);
  267. }
  268. return total_added;
  269. }
  270. static int caching_kthread(void *data)
  271. {
  272. struct btrfs_block_group_cache *block_group = data;
  273. struct btrfs_fs_info *fs_info = block_group->fs_info;
  274. struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
  275. struct btrfs_root *extent_root = fs_info->extent_root;
  276. struct btrfs_path *path;
  277. struct extent_buffer *leaf;
  278. struct btrfs_key key;
  279. u64 total_found = 0;
  280. u64 last = 0;
  281. u32 nritems;
  282. int ret = 0;
  283. path = btrfs_alloc_path();
  284. if (!path)
  285. return -ENOMEM;
  286. exclude_super_stripes(extent_root, block_group);
  287. spin_lock(&block_group->space_info->lock);
  288. block_group->space_info->bytes_readonly += block_group->bytes_super;
  289. spin_unlock(&block_group->space_info->lock);
  290. last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
  291. /*
  292. * We don't want to deadlock with somebody trying to allocate a new
  293. * extent for the extent root while also trying to search the extent
  294. * root to add free space. So we skip locking and search the commit
  295. * root, since its read-only
  296. */
  297. path->skip_locking = 1;
  298. path->search_commit_root = 1;
  299. path->reada = 2;
  300. key.objectid = last;
  301. key.offset = 0;
  302. key.type = BTRFS_EXTENT_ITEM_KEY;
  303. again:
  304. mutex_lock(&caching_ctl->mutex);
  305. /* need to make sure the commit_root doesn't disappear */
  306. down_read(&fs_info->extent_commit_sem);
  307. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  308. if (ret < 0)
  309. goto err;
  310. leaf = path->nodes[0];
  311. nritems = btrfs_header_nritems(leaf);
  312. while (1) {
  313. smp_mb();
  314. if (fs_info->closing > 1) {
  315. last = (u64)-1;
  316. break;
  317. }
  318. if (path->slots[0] < nritems) {
  319. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  320. } else {
  321. ret = find_next_key(path, 0, &key);
  322. if (ret)
  323. break;
  324. caching_ctl->progress = last;
  325. btrfs_release_path(extent_root, path);
  326. up_read(&fs_info->extent_commit_sem);
  327. mutex_unlock(&caching_ctl->mutex);
  328. if (btrfs_transaction_in_commit(fs_info))
  329. schedule_timeout(1);
  330. else
  331. cond_resched();
  332. goto again;
  333. }
  334. if (key.objectid < block_group->key.objectid) {
  335. path->slots[0]++;
  336. continue;
  337. }
  338. if (key.objectid >= block_group->key.objectid +
  339. block_group->key.offset)
  340. break;
  341. if (key.type == BTRFS_EXTENT_ITEM_KEY) {
  342. total_found += add_new_free_space(block_group,
  343. fs_info, last,
  344. key.objectid);
  345. last = key.objectid + key.offset;
  346. if (total_found > (1024 * 1024 * 2)) {
  347. total_found = 0;
  348. wake_up(&caching_ctl->wait);
  349. }
  350. }
  351. path->slots[0]++;
  352. }
  353. ret = 0;
  354. total_found += add_new_free_space(block_group, fs_info, last,
  355. block_group->key.objectid +
  356. block_group->key.offset);
  357. caching_ctl->progress = (u64)-1;
  358. spin_lock(&block_group->lock);
  359. block_group->caching_ctl = NULL;
  360. block_group->cached = BTRFS_CACHE_FINISHED;
  361. spin_unlock(&block_group->lock);
  362. err:
  363. btrfs_free_path(path);
  364. up_read(&fs_info->extent_commit_sem);
  365. free_excluded_extents(extent_root, block_group);
  366. mutex_unlock(&caching_ctl->mutex);
  367. wake_up(&caching_ctl->wait);
  368. put_caching_control(caching_ctl);
  369. atomic_dec(&block_group->space_info->caching_threads);
  370. btrfs_put_block_group(block_group);
  371. return 0;
  372. }
  373. static int cache_block_group(struct btrfs_block_group_cache *cache,
  374. struct btrfs_trans_handle *trans,
  375. int load_cache_only)
  376. {
  377. struct btrfs_fs_info *fs_info = cache->fs_info;
  378. struct btrfs_caching_control *caching_ctl;
  379. struct task_struct *tsk;
  380. int ret = 0;
  381. smp_mb();
  382. if (cache->cached != BTRFS_CACHE_NO)
  383. return 0;
  384. /*
  385. * We can't do the read from on-disk cache during a commit since we need
  386. * to have the normal tree locking.
  387. */
  388. if (!trans->transaction->in_commit) {
  389. spin_lock(&cache->lock);
  390. if (cache->cached != BTRFS_CACHE_NO) {
  391. spin_unlock(&cache->lock);
  392. return 0;
  393. }
  394. cache->cached = BTRFS_CACHE_STARTED;
  395. spin_unlock(&cache->lock);
  396. ret = load_free_space_cache(fs_info, cache);
  397. spin_lock(&cache->lock);
  398. if (ret == 1) {
  399. cache->cached = BTRFS_CACHE_FINISHED;
  400. cache->last_byte_to_unpin = (u64)-1;
  401. } else {
  402. cache->cached = BTRFS_CACHE_NO;
  403. }
  404. spin_unlock(&cache->lock);
  405. if (ret == 1)
  406. return 0;
  407. }
  408. if (load_cache_only)
  409. return 0;
  410. caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
  411. BUG_ON(!caching_ctl);
  412. INIT_LIST_HEAD(&caching_ctl->list);
  413. mutex_init(&caching_ctl->mutex);
  414. init_waitqueue_head(&caching_ctl->wait);
  415. caching_ctl->block_group = cache;
  416. caching_ctl->progress = cache->key.objectid;
  417. /* one for caching kthread, one for caching block group list */
  418. atomic_set(&caching_ctl->count, 2);
  419. spin_lock(&cache->lock);
  420. if (cache->cached != BTRFS_CACHE_NO) {
  421. spin_unlock(&cache->lock);
  422. kfree(caching_ctl);
  423. return 0;
  424. }
  425. cache->caching_ctl = caching_ctl;
  426. cache->cached = BTRFS_CACHE_STARTED;
  427. spin_unlock(&cache->lock);
  428. down_write(&fs_info->extent_commit_sem);
  429. list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
  430. up_write(&fs_info->extent_commit_sem);
  431. atomic_inc(&cache->space_info->caching_threads);
  432. btrfs_get_block_group(cache);
  433. tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
  434. cache->key.objectid);
  435. if (IS_ERR(tsk)) {
  436. ret = PTR_ERR(tsk);
  437. printk(KERN_ERR "error running thread %d\n", ret);
  438. BUG();
  439. }
  440. return ret;
  441. }
  442. /*
  443. * return the block group that starts at or after bytenr
  444. */
  445. static struct btrfs_block_group_cache *
  446. btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
  447. {
  448. struct btrfs_block_group_cache *cache;
  449. cache = block_group_cache_tree_search(info, bytenr, 0);
  450. return cache;
  451. }
  452. /*
  453. * return the block group that contains the given bytenr
  454. */
  455. struct btrfs_block_group_cache *btrfs_lookup_block_group(
  456. struct btrfs_fs_info *info,
  457. u64 bytenr)
  458. {
  459. struct btrfs_block_group_cache *cache;
  460. cache = block_group_cache_tree_search(info, bytenr, 1);
  461. return cache;
  462. }
  463. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  464. u64 flags)
  465. {
  466. struct list_head *head = &info->space_info;
  467. struct btrfs_space_info *found;
  468. flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
  469. BTRFS_BLOCK_GROUP_METADATA;
  470. rcu_read_lock();
  471. list_for_each_entry_rcu(found, head, list) {
  472. if (found->flags & flags) {
  473. rcu_read_unlock();
  474. return found;
  475. }
  476. }
  477. rcu_read_unlock();
  478. return NULL;
  479. }
  480. /*
  481. * after adding space to the filesystem, we need to clear the full flags
  482. * on all the space infos.
  483. */
  484. void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
  485. {
  486. struct list_head *head = &info->space_info;
  487. struct btrfs_space_info *found;
  488. rcu_read_lock();
  489. list_for_each_entry_rcu(found, head, list)
  490. found->full = 0;
  491. rcu_read_unlock();
  492. }
  493. static u64 div_factor(u64 num, int factor)
  494. {
  495. if (factor == 10)
  496. return num;
  497. num *= factor;
  498. do_div(num, 10);
  499. return num;
  500. }
  501. static u64 div_factor_fine(u64 num, int factor)
  502. {
  503. if (factor == 100)
  504. return num;
  505. num *= factor;
  506. do_div(num, 100);
  507. return num;
  508. }
  509. u64 btrfs_find_block_group(struct btrfs_root *root,
  510. u64 search_start, u64 search_hint, int owner)
  511. {
  512. struct btrfs_block_group_cache *cache;
  513. u64 used;
  514. u64 last = max(search_hint, search_start);
  515. u64 group_start = 0;
  516. int full_search = 0;
  517. int factor = 9;
  518. int wrapped = 0;
  519. again:
  520. while (1) {
  521. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  522. if (!cache)
  523. break;
  524. spin_lock(&cache->lock);
  525. last = cache->key.objectid + cache->key.offset;
  526. used = btrfs_block_group_used(&cache->item);
  527. if ((full_search || !cache->ro) &&
  528. block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
  529. if (used + cache->pinned + cache->reserved <
  530. div_factor(cache->key.offset, factor)) {
  531. group_start = cache->key.objectid;
  532. spin_unlock(&cache->lock);
  533. btrfs_put_block_group(cache);
  534. goto found;
  535. }
  536. }
  537. spin_unlock(&cache->lock);
  538. btrfs_put_block_group(cache);
  539. cond_resched();
  540. }
  541. if (!wrapped) {
  542. last = search_start;
  543. wrapped = 1;
  544. goto again;
  545. }
  546. if (!full_search && factor < 10) {
  547. last = search_start;
  548. full_search = 1;
  549. factor = 10;
  550. goto again;
  551. }
  552. found:
  553. return group_start;
  554. }
  555. /* simple helper to search for an existing extent at a given offset */
  556. int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
  557. {
  558. int ret;
  559. struct btrfs_key key;
  560. struct btrfs_path *path;
  561. path = btrfs_alloc_path();
  562. BUG_ON(!path);
  563. key.objectid = start;
  564. key.offset = len;
  565. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  566. ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
  567. 0, 0);
  568. btrfs_free_path(path);
  569. return ret;
  570. }
  571. /*
  572. * helper function to lookup reference count and flags of extent.
  573. *
  574. * the head node for delayed ref is used to store the sum of all the
  575. * reference count modifications queued up in the rbtree. the head
  576. * node may also store the extent flags to set. This way you can check
  577. * to see what the reference count and extent flags would be if all of
  578. * the delayed refs are not processed.
  579. */
  580. int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
  581. struct btrfs_root *root, u64 bytenr,
  582. u64 num_bytes, u64 *refs, u64 *flags)
  583. {
  584. struct btrfs_delayed_ref_head *head;
  585. struct btrfs_delayed_ref_root *delayed_refs;
  586. struct btrfs_path *path;
  587. struct btrfs_extent_item *ei;
  588. struct extent_buffer *leaf;
  589. struct btrfs_key key;
  590. u32 item_size;
  591. u64 num_refs;
  592. u64 extent_flags;
  593. int ret;
  594. path = btrfs_alloc_path();
  595. if (!path)
  596. return -ENOMEM;
  597. key.objectid = bytenr;
  598. key.type = BTRFS_EXTENT_ITEM_KEY;
  599. key.offset = num_bytes;
  600. if (!trans) {
  601. path->skip_locking = 1;
  602. path->search_commit_root = 1;
  603. }
  604. again:
  605. ret = btrfs_search_slot(trans, root->fs_info->extent_root,
  606. &key, path, 0, 0);
  607. if (ret < 0)
  608. goto out_free;
  609. if (ret == 0) {
  610. leaf = path->nodes[0];
  611. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  612. if (item_size >= sizeof(*ei)) {
  613. ei = btrfs_item_ptr(leaf, path->slots[0],
  614. struct btrfs_extent_item);
  615. num_refs = btrfs_extent_refs(leaf, ei);
  616. extent_flags = btrfs_extent_flags(leaf, ei);
  617. } else {
  618. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  619. struct btrfs_extent_item_v0 *ei0;
  620. BUG_ON(item_size != sizeof(*ei0));
  621. ei0 = btrfs_item_ptr(leaf, path->slots[0],
  622. struct btrfs_extent_item_v0);
  623. num_refs = btrfs_extent_refs_v0(leaf, ei0);
  624. /* FIXME: this isn't correct for data */
  625. extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  626. #else
  627. BUG();
  628. #endif
  629. }
  630. BUG_ON(num_refs == 0);
  631. } else {
  632. num_refs = 0;
  633. extent_flags = 0;
  634. ret = 0;
  635. }
  636. if (!trans)
  637. goto out;
  638. delayed_refs = &trans->transaction->delayed_refs;
  639. spin_lock(&delayed_refs->lock);
  640. head = btrfs_find_delayed_ref_head(trans, bytenr);
  641. if (head) {
  642. if (!mutex_trylock(&head->mutex)) {
  643. atomic_inc(&head->node.refs);
  644. spin_unlock(&delayed_refs->lock);
  645. btrfs_release_path(root->fs_info->extent_root, path);
  646. mutex_lock(&head->mutex);
  647. mutex_unlock(&head->mutex);
  648. btrfs_put_delayed_ref(&head->node);
  649. goto again;
  650. }
  651. if (head->extent_op && head->extent_op->update_flags)
  652. extent_flags |= head->extent_op->flags_to_set;
  653. else
  654. BUG_ON(num_refs == 0);
  655. num_refs += head->node.ref_mod;
  656. mutex_unlock(&head->mutex);
  657. }
  658. spin_unlock(&delayed_refs->lock);
  659. out:
  660. WARN_ON(num_refs == 0);
  661. if (refs)
  662. *refs = num_refs;
  663. if (flags)
  664. *flags = extent_flags;
  665. out_free:
  666. btrfs_free_path(path);
  667. return ret;
  668. }
  669. /*
  670. * Back reference rules. Back refs have three main goals:
  671. *
  672. * 1) differentiate between all holders of references to an extent so that
  673. * when a reference is dropped we can make sure it was a valid reference
  674. * before freeing the extent.
  675. *
  676. * 2) Provide enough information to quickly find the holders of an extent
  677. * if we notice a given block is corrupted or bad.
  678. *
  679. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  680. * maintenance. This is actually the same as #2, but with a slightly
  681. * different use case.
  682. *
  683. * There are two kinds of back refs. The implicit back refs is optimized
  684. * for pointers in non-shared tree blocks. For a given pointer in a block,
  685. * back refs of this kind provide information about the block's owner tree
  686. * and the pointer's key. These information allow us to find the block by
  687. * b-tree searching. The full back refs is for pointers in tree blocks not
  688. * referenced by their owner trees. The location of tree block is recorded
  689. * in the back refs. Actually the full back refs is generic, and can be
  690. * used in all cases the implicit back refs is used. The major shortcoming
  691. * of the full back refs is its overhead. Every time a tree block gets
  692. * COWed, we have to update back refs entry for all pointers in it.
  693. *
  694. * For a newly allocated tree block, we use implicit back refs for
  695. * pointers in it. This means most tree related operations only involve
  696. * implicit back refs. For a tree block created in old transaction, the
  697. * only way to drop a reference to it is COW it. So we can detect the
  698. * event that tree block loses its owner tree's reference and do the
  699. * back refs conversion.
  700. *
  701. * When a tree block is COW'd through a tree, there are four cases:
  702. *
  703. * The reference count of the block is one and the tree is the block's
  704. * owner tree. Nothing to do in this case.
  705. *
  706. * The reference count of the block is one and the tree is not the
  707. * block's owner tree. In this case, full back refs is used for pointers
  708. * in the block. Remove these full back refs, add implicit back refs for
  709. * every pointers in the new block.
  710. *
  711. * The reference count of the block is greater than one and the tree is
  712. * the block's owner tree. In this case, implicit back refs is used for
  713. * pointers in the block. Add full back refs for every pointers in the
  714. * block, increase lower level extents' reference counts. The original
  715. * implicit back refs are entailed to the new block.
  716. *
  717. * The reference count of the block is greater than one and the tree is
  718. * not the block's owner tree. Add implicit back refs for every pointer in
  719. * the new block, increase lower level extents' reference count.
  720. *
  721. * Back Reference Key composing:
  722. *
  723. * The key objectid corresponds to the first byte in the extent,
  724. * The key type is used to differentiate between types of back refs.
  725. * There are different meanings of the key offset for different types
  726. * of back refs.
  727. *
  728. * File extents can be referenced by:
  729. *
  730. * - multiple snapshots, subvolumes, or different generations in one subvol
  731. * - different files inside a single subvolume
  732. * - different offsets inside a file (bookend extents in file.c)
  733. *
  734. * The extent ref structure for the implicit back refs has fields for:
  735. *
  736. * - Objectid of the subvolume root
  737. * - objectid of the file holding the reference
  738. * - original offset in the file
  739. * - how many bookend extents
  740. *
  741. * The key offset for the implicit back refs is hash of the first
  742. * three fields.
  743. *
  744. * The extent ref structure for the full back refs has field for:
  745. *
  746. * - number of pointers in the tree leaf
  747. *
  748. * The key offset for the implicit back refs is the first byte of
  749. * the tree leaf
  750. *
  751. * When a file extent is allocated, The implicit back refs is used.
  752. * the fields are filled in:
  753. *
  754. * (root_key.objectid, inode objectid, offset in file, 1)
  755. *
  756. * When a file extent is removed file truncation, we find the
  757. * corresponding implicit back refs and check the following fields:
  758. *
  759. * (btrfs_header_owner(leaf), inode objectid, offset in file)
  760. *
  761. * Btree extents can be referenced by:
  762. *
  763. * - Different subvolumes
  764. *
  765. * Both the implicit back refs and the full back refs for tree blocks
  766. * only consist of key. The key offset for the implicit back refs is
  767. * objectid of block's owner tree. The key offset for the full back refs
  768. * is the first byte of parent block.
  769. *
  770. * When implicit back refs is used, information about the lowest key and
  771. * level of the tree block are required. These information are stored in
  772. * tree block info structure.
  773. */
  774. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  775. static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
  776. struct btrfs_root *root,
  777. struct btrfs_path *path,
  778. u64 owner, u32 extra_size)
  779. {
  780. struct btrfs_extent_item *item;
  781. struct btrfs_extent_item_v0 *ei0;
  782. struct btrfs_extent_ref_v0 *ref0;
  783. struct btrfs_tree_block_info *bi;
  784. struct extent_buffer *leaf;
  785. struct btrfs_key key;
  786. struct btrfs_key found_key;
  787. u32 new_size = sizeof(*item);
  788. u64 refs;
  789. int ret;
  790. leaf = path->nodes[0];
  791. BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
  792. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  793. ei0 = btrfs_item_ptr(leaf, path->slots[0],
  794. struct btrfs_extent_item_v0);
  795. refs = btrfs_extent_refs_v0(leaf, ei0);
  796. if (owner == (u64)-1) {
  797. while (1) {
  798. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  799. ret = btrfs_next_leaf(root, path);
  800. if (ret < 0)
  801. return ret;
  802. BUG_ON(ret > 0);
  803. leaf = path->nodes[0];
  804. }
  805. btrfs_item_key_to_cpu(leaf, &found_key,
  806. path->slots[0]);
  807. BUG_ON(key.objectid != found_key.objectid);
  808. if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
  809. path->slots[0]++;
  810. continue;
  811. }
  812. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  813. struct btrfs_extent_ref_v0);
  814. owner = btrfs_ref_objectid_v0(leaf, ref0);
  815. break;
  816. }
  817. }
  818. btrfs_release_path(root, path);
  819. if (owner < BTRFS_FIRST_FREE_OBJECTID)
  820. new_size += sizeof(*bi);
  821. new_size -= sizeof(*ei0);
  822. ret = btrfs_search_slot(trans, root, &key, path,
  823. new_size + extra_size, 1);
  824. if (ret < 0)
  825. return ret;
  826. BUG_ON(ret);
  827. ret = btrfs_extend_item(trans, root, path, new_size);
  828. BUG_ON(ret);
  829. leaf = path->nodes[0];
  830. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  831. btrfs_set_extent_refs(leaf, item, refs);
  832. /* FIXME: get real generation */
  833. btrfs_set_extent_generation(leaf, item, 0);
  834. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  835. btrfs_set_extent_flags(leaf, item,
  836. BTRFS_EXTENT_FLAG_TREE_BLOCK |
  837. BTRFS_BLOCK_FLAG_FULL_BACKREF);
  838. bi = (struct btrfs_tree_block_info *)(item + 1);
  839. /* FIXME: get first key of the block */
  840. memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
  841. btrfs_set_tree_block_level(leaf, bi, (int)owner);
  842. } else {
  843. btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
  844. }
  845. btrfs_mark_buffer_dirty(leaf);
  846. return 0;
  847. }
  848. #endif
  849. static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
  850. {
  851. u32 high_crc = ~(u32)0;
  852. u32 low_crc = ~(u32)0;
  853. __le64 lenum;
  854. lenum = cpu_to_le64(root_objectid);
  855. high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
  856. lenum = cpu_to_le64(owner);
  857. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  858. lenum = cpu_to_le64(offset);
  859. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  860. return ((u64)high_crc << 31) ^ (u64)low_crc;
  861. }
  862. static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
  863. struct btrfs_extent_data_ref *ref)
  864. {
  865. return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
  866. btrfs_extent_data_ref_objectid(leaf, ref),
  867. btrfs_extent_data_ref_offset(leaf, ref));
  868. }
  869. static int match_extent_data_ref(struct extent_buffer *leaf,
  870. struct btrfs_extent_data_ref *ref,
  871. u64 root_objectid, u64 owner, u64 offset)
  872. {
  873. if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
  874. btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
  875. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  876. return 0;
  877. return 1;
  878. }
  879. static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
  880. struct btrfs_root *root,
  881. struct btrfs_path *path,
  882. u64 bytenr, u64 parent,
  883. u64 root_objectid,
  884. u64 owner, u64 offset)
  885. {
  886. struct btrfs_key key;
  887. struct btrfs_extent_data_ref *ref;
  888. struct extent_buffer *leaf;
  889. u32 nritems;
  890. int ret;
  891. int recow;
  892. int err = -ENOENT;
  893. key.objectid = bytenr;
  894. if (parent) {
  895. key.type = BTRFS_SHARED_DATA_REF_KEY;
  896. key.offset = parent;
  897. } else {
  898. key.type = BTRFS_EXTENT_DATA_REF_KEY;
  899. key.offset = hash_extent_data_ref(root_objectid,
  900. owner, offset);
  901. }
  902. again:
  903. recow = 0;
  904. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  905. if (ret < 0) {
  906. err = ret;
  907. goto fail;
  908. }
  909. if (parent) {
  910. if (!ret)
  911. return 0;
  912. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  913. key.type = BTRFS_EXTENT_REF_V0_KEY;
  914. btrfs_release_path(root, path);
  915. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  916. if (ret < 0) {
  917. err = ret;
  918. goto fail;
  919. }
  920. if (!ret)
  921. return 0;
  922. #endif
  923. goto fail;
  924. }
  925. leaf = path->nodes[0];
  926. nritems = btrfs_header_nritems(leaf);
  927. while (1) {
  928. if (path->slots[0] >= nritems) {
  929. ret = btrfs_next_leaf(root, path);
  930. if (ret < 0)
  931. err = ret;
  932. if (ret)
  933. goto fail;
  934. leaf = path->nodes[0];
  935. nritems = btrfs_header_nritems(leaf);
  936. recow = 1;
  937. }
  938. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  939. if (key.objectid != bytenr ||
  940. key.type != BTRFS_EXTENT_DATA_REF_KEY)
  941. goto fail;
  942. ref = btrfs_item_ptr(leaf, path->slots[0],
  943. struct btrfs_extent_data_ref);
  944. if (match_extent_data_ref(leaf, ref, root_objectid,
  945. owner, offset)) {
  946. if (recow) {
  947. btrfs_release_path(root, path);
  948. goto again;
  949. }
  950. err = 0;
  951. break;
  952. }
  953. path->slots[0]++;
  954. }
  955. fail:
  956. return err;
  957. }
  958. static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
  959. struct btrfs_root *root,
  960. struct btrfs_path *path,
  961. u64 bytenr, u64 parent,
  962. u64 root_objectid, u64 owner,
  963. u64 offset, int refs_to_add)
  964. {
  965. struct btrfs_key key;
  966. struct extent_buffer *leaf;
  967. u32 size;
  968. u32 num_refs;
  969. int ret;
  970. key.objectid = bytenr;
  971. if (parent) {
  972. key.type = BTRFS_SHARED_DATA_REF_KEY;
  973. key.offset = parent;
  974. size = sizeof(struct btrfs_shared_data_ref);
  975. } else {
  976. key.type = BTRFS_EXTENT_DATA_REF_KEY;
  977. key.offset = hash_extent_data_ref(root_objectid,
  978. owner, offset);
  979. size = sizeof(struct btrfs_extent_data_ref);
  980. }
  981. ret = btrfs_insert_empty_item(trans, root, path, &key, size);
  982. if (ret && ret != -EEXIST)
  983. goto fail;
  984. leaf = path->nodes[0];
  985. if (parent) {
  986. struct btrfs_shared_data_ref *ref;
  987. ref = btrfs_item_ptr(leaf, path->slots[0],
  988. struct btrfs_shared_data_ref);
  989. if (ret == 0) {
  990. btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
  991. } else {
  992. num_refs = btrfs_shared_data_ref_count(leaf, ref);
  993. num_refs += refs_to_add;
  994. btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
  995. }
  996. } else {
  997. struct btrfs_extent_data_ref *ref;
  998. while (ret == -EEXIST) {
  999. ref = btrfs_item_ptr(leaf, path->slots[0],
  1000. struct btrfs_extent_data_ref);
  1001. if (match_extent_data_ref(leaf, ref, root_objectid,
  1002. owner, offset))
  1003. break;
  1004. btrfs_release_path(root, path);
  1005. key.offset++;
  1006. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1007. size);
  1008. if (ret && ret != -EEXIST)
  1009. goto fail;
  1010. leaf = path->nodes[0];
  1011. }
  1012. ref = btrfs_item_ptr(leaf, path->slots[0],
  1013. struct btrfs_extent_data_ref);
  1014. if (ret == 0) {
  1015. btrfs_set_extent_data_ref_root(leaf, ref,
  1016. root_objectid);
  1017. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  1018. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  1019. btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
  1020. } else {
  1021. num_refs = btrfs_extent_data_ref_count(leaf, ref);
  1022. num_refs += refs_to_add;
  1023. btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
  1024. }
  1025. }
  1026. btrfs_mark_buffer_dirty(leaf);
  1027. ret = 0;
  1028. fail:
  1029. btrfs_release_path(root, path);
  1030. return ret;
  1031. }
  1032. static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
  1033. struct btrfs_root *root,
  1034. struct btrfs_path *path,
  1035. int refs_to_drop)
  1036. {
  1037. struct btrfs_key key;
  1038. struct btrfs_extent_data_ref *ref1 = NULL;
  1039. struct btrfs_shared_data_ref *ref2 = NULL;
  1040. struct extent_buffer *leaf;
  1041. u32 num_refs = 0;
  1042. int ret = 0;
  1043. leaf = path->nodes[0];
  1044. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1045. if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  1046. ref1 = btrfs_item_ptr(leaf, path->slots[0],
  1047. struct btrfs_extent_data_ref);
  1048. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  1049. } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  1050. ref2 = btrfs_item_ptr(leaf, path->slots[0],
  1051. struct btrfs_shared_data_ref);
  1052. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  1053. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1054. } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
  1055. struct btrfs_extent_ref_v0 *ref0;
  1056. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  1057. struct btrfs_extent_ref_v0);
  1058. num_refs = btrfs_ref_count_v0(leaf, ref0);
  1059. #endif
  1060. } else {
  1061. BUG();
  1062. }
  1063. BUG_ON(num_refs < refs_to_drop);
  1064. num_refs -= refs_to_drop;
  1065. if (num_refs == 0) {
  1066. ret = btrfs_del_item(trans, root, path);
  1067. } else {
  1068. if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
  1069. btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
  1070. else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
  1071. btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
  1072. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1073. else {
  1074. struct btrfs_extent_ref_v0 *ref0;
  1075. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  1076. struct btrfs_extent_ref_v0);
  1077. btrfs_set_ref_count_v0(leaf, ref0, num_refs);
  1078. }
  1079. #endif
  1080. btrfs_mark_buffer_dirty(leaf);
  1081. }
  1082. return ret;
  1083. }
  1084. static noinline u32 extent_data_ref_count(struct btrfs_root *root,
  1085. struct btrfs_path *path,
  1086. struct btrfs_extent_inline_ref *iref)
  1087. {
  1088. struct btrfs_key key;
  1089. struct extent_buffer *leaf;
  1090. struct btrfs_extent_data_ref *ref1;
  1091. struct btrfs_shared_data_ref *ref2;
  1092. u32 num_refs = 0;
  1093. leaf = path->nodes[0];
  1094. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1095. if (iref) {
  1096. if (btrfs_extent_inline_ref_type(leaf, iref) ==
  1097. BTRFS_EXTENT_DATA_REF_KEY) {
  1098. ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
  1099. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  1100. } else {
  1101. ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
  1102. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  1103. }
  1104. } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  1105. ref1 = btrfs_item_ptr(leaf, path->slots[0],
  1106. struct btrfs_extent_data_ref);
  1107. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  1108. } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  1109. ref2 = btrfs_item_ptr(leaf, path->slots[0],
  1110. struct btrfs_shared_data_ref);
  1111. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  1112. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1113. } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
  1114. struct btrfs_extent_ref_v0 *ref0;
  1115. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  1116. struct btrfs_extent_ref_v0);
  1117. num_refs = btrfs_ref_count_v0(leaf, ref0);
  1118. #endif
  1119. } else {
  1120. WARN_ON(1);
  1121. }
  1122. return num_refs;
  1123. }
  1124. static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
  1125. struct btrfs_root *root,
  1126. struct btrfs_path *path,
  1127. u64 bytenr, u64 parent,
  1128. u64 root_objectid)
  1129. {
  1130. struct btrfs_key key;
  1131. int ret;
  1132. key.objectid = bytenr;
  1133. if (parent) {
  1134. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  1135. key.offset = parent;
  1136. } else {
  1137. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  1138. key.offset = root_objectid;
  1139. }
  1140. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1141. if (ret > 0)
  1142. ret = -ENOENT;
  1143. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1144. if (ret == -ENOENT && parent) {
  1145. btrfs_release_path(root, path);
  1146. key.type = BTRFS_EXTENT_REF_V0_KEY;
  1147. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1148. if (ret > 0)
  1149. ret = -ENOENT;
  1150. }
  1151. #endif
  1152. return ret;
  1153. }
  1154. static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
  1155. struct btrfs_root *root,
  1156. struct btrfs_path *path,
  1157. u64 bytenr, u64 parent,
  1158. u64 root_objectid)
  1159. {
  1160. struct btrfs_key key;
  1161. int ret;
  1162. key.objectid = bytenr;
  1163. if (parent) {
  1164. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  1165. key.offset = parent;
  1166. } else {
  1167. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  1168. key.offset = root_objectid;
  1169. }
  1170. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1171. btrfs_release_path(root, path);
  1172. return ret;
  1173. }
  1174. static inline int extent_ref_type(u64 parent, u64 owner)
  1175. {
  1176. int type;
  1177. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1178. if (parent > 0)
  1179. type = BTRFS_SHARED_BLOCK_REF_KEY;
  1180. else
  1181. type = BTRFS_TREE_BLOCK_REF_KEY;
  1182. } else {
  1183. if (parent > 0)
  1184. type = BTRFS_SHARED_DATA_REF_KEY;
  1185. else
  1186. type = BTRFS_EXTENT_DATA_REF_KEY;
  1187. }
  1188. return type;
  1189. }
  1190. static int find_next_key(struct btrfs_path *path, int level,
  1191. struct btrfs_key *key)
  1192. {
  1193. for (; level < BTRFS_MAX_LEVEL; level++) {
  1194. if (!path->nodes[level])
  1195. break;
  1196. if (path->slots[level] + 1 >=
  1197. btrfs_header_nritems(path->nodes[level]))
  1198. continue;
  1199. if (level == 0)
  1200. btrfs_item_key_to_cpu(path->nodes[level], key,
  1201. path->slots[level] + 1);
  1202. else
  1203. btrfs_node_key_to_cpu(path->nodes[level], key,
  1204. path->slots[level] + 1);
  1205. return 0;
  1206. }
  1207. return 1;
  1208. }
  1209. /*
  1210. * look for inline back ref. if back ref is found, *ref_ret is set
  1211. * to the address of inline back ref, and 0 is returned.
  1212. *
  1213. * if back ref isn't found, *ref_ret is set to the address where it
  1214. * should be inserted, and -ENOENT is returned.
  1215. *
  1216. * if insert is true and there are too many inline back refs, the path
  1217. * points to the extent item, and -EAGAIN is returned.
  1218. *
  1219. * NOTE: inline back refs are ordered in the same way that back ref
  1220. * items in the tree are ordered.
  1221. */
  1222. static noinline_for_stack
  1223. int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
  1224. struct btrfs_root *root,
  1225. struct btrfs_path *path,
  1226. struct btrfs_extent_inline_ref **ref_ret,
  1227. u64 bytenr, u64 num_bytes,
  1228. u64 parent, u64 root_objectid,
  1229. u64 owner, u64 offset, int insert)
  1230. {
  1231. struct btrfs_key key;
  1232. struct extent_buffer *leaf;
  1233. struct btrfs_extent_item *ei;
  1234. struct btrfs_extent_inline_ref *iref;
  1235. u64 flags;
  1236. u64 item_size;
  1237. unsigned long ptr;
  1238. unsigned long end;
  1239. int extra_size;
  1240. int type;
  1241. int want;
  1242. int ret;
  1243. int err = 0;
  1244. key.objectid = bytenr;
  1245. key.type = BTRFS_EXTENT_ITEM_KEY;
  1246. key.offset = num_bytes;
  1247. want = extent_ref_type(parent, owner);
  1248. if (insert) {
  1249. extra_size = btrfs_extent_inline_ref_size(want);
  1250. path->keep_locks = 1;
  1251. } else
  1252. extra_size = -1;
  1253. ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
  1254. if (ret < 0) {
  1255. err = ret;
  1256. goto out;
  1257. }
  1258. BUG_ON(ret);
  1259. leaf = path->nodes[0];
  1260. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1261. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1262. if (item_size < sizeof(*ei)) {
  1263. if (!insert) {
  1264. err = -ENOENT;
  1265. goto out;
  1266. }
  1267. ret = convert_extent_item_v0(trans, root, path, owner,
  1268. extra_size);
  1269. if (ret < 0) {
  1270. err = ret;
  1271. goto out;
  1272. }
  1273. leaf = path->nodes[0];
  1274. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1275. }
  1276. #endif
  1277. BUG_ON(item_size < sizeof(*ei));
  1278. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1279. flags = btrfs_extent_flags(leaf, ei);
  1280. ptr = (unsigned long)(ei + 1);
  1281. end = (unsigned long)ei + item_size;
  1282. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1283. ptr += sizeof(struct btrfs_tree_block_info);
  1284. BUG_ON(ptr > end);
  1285. } else {
  1286. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
  1287. }
  1288. err = -ENOENT;
  1289. while (1) {
  1290. if (ptr >= end) {
  1291. WARN_ON(ptr > end);
  1292. break;
  1293. }
  1294. iref = (struct btrfs_extent_inline_ref *)ptr;
  1295. type = btrfs_extent_inline_ref_type(leaf, iref);
  1296. if (want < type)
  1297. break;
  1298. if (want > type) {
  1299. ptr += btrfs_extent_inline_ref_size(type);
  1300. continue;
  1301. }
  1302. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1303. struct btrfs_extent_data_ref *dref;
  1304. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1305. if (match_extent_data_ref(leaf, dref, root_objectid,
  1306. owner, offset)) {
  1307. err = 0;
  1308. break;
  1309. }
  1310. if (hash_extent_data_ref_item(leaf, dref) <
  1311. hash_extent_data_ref(root_objectid, owner, offset))
  1312. break;
  1313. } else {
  1314. u64 ref_offset;
  1315. ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
  1316. if (parent > 0) {
  1317. if (parent == ref_offset) {
  1318. err = 0;
  1319. break;
  1320. }
  1321. if (ref_offset < parent)
  1322. break;
  1323. } else {
  1324. if (root_objectid == ref_offset) {
  1325. err = 0;
  1326. break;
  1327. }
  1328. if (ref_offset < root_objectid)
  1329. break;
  1330. }
  1331. }
  1332. ptr += btrfs_extent_inline_ref_size(type);
  1333. }
  1334. if (err == -ENOENT && insert) {
  1335. if (item_size + extra_size >=
  1336. BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
  1337. err = -EAGAIN;
  1338. goto out;
  1339. }
  1340. /*
  1341. * To add new inline back ref, we have to make sure
  1342. * there is no corresponding back ref item.
  1343. * For simplicity, we just do not add new inline back
  1344. * ref if there is any kind of item for this block
  1345. */
  1346. if (find_next_key(path, 0, &key) == 0 &&
  1347. key.objectid == bytenr &&
  1348. key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
  1349. err = -EAGAIN;
  1350. goto out;
  1351. }
  1352. }
  1353. *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
  1354. out:
  1355. if (insert) {
  1356. path->keep_locks = 0;
  1357. btrfs_unlock_up_safe(path, 1);
  1358. }
  1359. return err;
  1360. }
  1361. /*
  1362. * helper to add new inline back ref
  1363. */
  1364. static noinline_for_stack
  1365. int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
  1366. struct btrfs_root *root,
  1367. struct btrfs_path *path,
  1368. struct btrfs_extent_inline_ref *iref,
  1369. u64 parent, u64 root_objectid,
  1370. u64 owner, u64 offset, int refs_to_add,
  1371. struct btrfs_delayed_extent_op *extent_op)
  1372. {
  1373. struct extent_buffer *leaf;
  1374. struct btrfs_extent_item *ei;
  1375. unsigned long ptr;
  1376. unsigned long end;
  1377. unsigned long item_offset;
  1378. u64 refs;
  1379. int size;
  1380. int type;
  1381. int ret;
  1382. leaf = path->nodes[0];
  1383. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1384. item_offset = (unsigned long)iref - (unsigned long)ei;
  1385. type = extent_ref_type(parent, owner);
  1386. size = btrfs_extent_inline_ref_size(type);
  1387. ret = btrfs_extend_item(trans, root, path, size);
  1388. BUG_ON(ret);
  1389. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1390. refs = btrfs_extent_refs(leaf, ei);
  1391. refs += refs_to_add;
  1392. btrfs_set_extent_refs(leaf, ei, refs);
  1393. if (extent_op)
  1394. __run_delayed_extent_op(extent_op, leaf, ei);
  1395. ptr = (unsigned long)ei + item_offset;
  1396. end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
  1397. if (ptr < end - size)
  1398. memmove_extent_buffer(leaf, ptr + size, ptr,
  1399. end - size - ptr);
  1400. iref = (struct btrfs_extent_inline_ref *)ptr;
  1401. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  1402. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1403. struct btrfs_extent_data_ref *dref;
  1404. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1405. btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
  1406. btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
  1407. btrfs_set_extent_data_ref_offset(leaf, dref, offset);
  1408. btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
  1409. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1410. struct btrfs_shared_data_ref *sref;
  1411. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1412. btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
  1413. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  1414. } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
  1415. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  1416. } else {
  1417. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  1418. }
  1419. btrfs_mark_buffer_dirty(leaf);
  1420. return 0;
  1421. }
  1422. static int lookup_extent_backref(struct btrfs_trans_handle *trans,
  1423. struct btrfs_root *root,
  1424. struct btrfs_path *path,
  1425. struct btrfs_extent_inline_ref **ref_ret,
  1426. u64 bytenr, u64 num_bytes, u64 parent,
  1427. u64 root_objectid, u64 owner, u64 offset)
  1428. {
  1429. int ret;
  1430. ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
  1431. bytenr, num_bytes, parent,
  1432. root_objectid, owner, offset, 0);
  1433. if (ret != -ENOENT)
  1434. return ret;
  1435. btrfs_release_path(root, path);
  1436. *ref_ret = NULL;
  1437. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1438. ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
  1439. root_objectid);
  1440. } else {
  1441. ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
  1442. root_objectid, owner, offset);
  1443. }
  1444. return ret;
  1445. }
  1446. /*
  1447. * helper to update/remove inline back ref
  1448. */
  1449. static noinline_for_stack
  1450. int update_inline_extent_backref(struct btrfs_trans_handle *trans,
  1451. struct btrfs_root *root,
  1452. struct btrfs_path *path,
  1453. struct btrfs_extent_inline_ref *iref,
  1454. int refs_to_mod,
  1455. struct btrfs_delayed_extent_op *extent_op)
  1456. {
  1457. struct extent_buffer *leaf;
  1458. struct btrfs_extent_item *ei;
  1459. struct btrfs_extent_data_ref *dref = NULL;
  1460. struct btrfs_shared_data_ref *sref = NULL;
  1461. unsigned long ptr;
  1462. unsigned long end;
  1463. u32 item_size;
  1464. int size;
  1465. int type;
  1466. int ret;
  1467. u64 refs;
  1468. leaf = path->nodes[0];
  1469. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1470. refs = btrfs_extent_refs(leaf, ei);
  1471. WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
  1472. refs += refs_to_mod;
  1473. btrfs_set_extent_refs(leaf, ei, refs);
  1474. if (extent_op)
  1475. __run_delayed_extent_op(extent_op, leaf, ei);
  1476. type = btrfs_extent_inline_ref_type(leaf, iref);
  1477. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1478. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1479. refs = btrfs_extent_data_ref_count(leaf, dref);
  1480. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1481. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1482. refs = btrfs_shared_data_ref_count(leaf, sref);
  1483. } else {
  1484. refs = 1;
  1485. BUG_ON(refs_to_mod != -1);
  1486. }
  1487. BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
  1488. refs += refs_to_mod;
  1489. if (refs > 0) {
  1490. if (type == BTRFS_EXTENT_DATA_REF_KEY)
  1491. btrfs_set_extent_data_ref_count(leaf, dref, refs);
  1492. else
  1493. btrfs_set_shared_data_ref_count(leaf, sref, refs);
  1494. } else {
  1495. size = btrfs_extent_inline_ref_size(type);
  1496. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1497. ptr = (unsigned long)iref;
  1498. end = (unsigned long)ei + item_size;
  1499. if (ptr + size < end)
  1500. memmove_extent_buffer(leaf, ptr, ptr + size,
  1501. end - ptr - size);
  1502. item_size -= size;
  1503. ret = btrfs_truncate_item(trans, root, path, item_size, 1);
  1504. BUG_ON(ret);
  1505. }
  1506. btrfs_mark_buffer_dirty(leaf);
  1507. return 0;
  1508. }
  1509. static noinline_for_stack
  1510. int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
  1511. struct btrfs_root *root,
  1512. struct btrfs_path *path,
  1513. u64 bytenr, u64 num_bytes, u64 parent,
  1514. u64 root_objectid, u64 owner,
  1515. u64 offset, int refs_to_add,
  1516. struct btrfs_delayed_extent_op *extent_op)
  1517. {
  1518. struct btrfs_extent_inline_ref *iref;
  1519. int ret;
  1520. ret = lookup_inline_extent_backref(trans, root, path, &iref,
  1521. bytenr, num_bytes, parent,
  1522. root_objectid, owner, offset, 1);
  1523. if (ret == 0) {
  1524. BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
  1525. ret = update_inline_extent_backref(trans, root, path, iref,
  1526. refs_to_add, extent_op);
  1527. } else if (ret == -ENOENT) {
  1528. ret = setup_inline_extent_backref(trans, root, path, iref,
  1529. parent, root_objectid,
  1530. owner, offset, refs_to_add,
  1531. extent_op);
  1532. }
  1533. return ret;
  1534. }
  1535. static int insert_extent_backref(struct btrfs_trans_handle *trans,
  1536. struct btrfs_root *root,
  1537. struct btrfs_path *path,
  1538. u64 bytenr, u64 parent, u64 root_objectid,
  1539. u64 owner, u64 offset, int refs_to_add)
  1540. {
  1541. int ret;
  1542. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1543. BUG_ON(refs_to_add != 1);
  1544. ret = insert_tree_block_ref(trans, root, path, bytenr,
  1545. parent, root_objectid);
  1546. } else {
  1547. ret = insert_extent_data_ref(trans, root, path, bytenr,
  1548. parent, root_objectid,
  1549. owner, offset, refs_to_add);
  1550. }
  1551. return ret;
  1552. }
  1553. static int remove_extent_backref(struct btrfs_trans_handle *trans,
  1554. struct btrfs_root *root,
  1555. struct btrfs_path *path,
  1556. struct btrfs_extent_inline_ref *iref,
  1557. int refs_to_drop, int is_data)
  1558. {
  1559. int ret;
  1560. BUG_ON(!is_data && refs_to_drop != 1);
  1561. if (iref) {
  1562. ret = update_inline_extent_backref(trans, root, path, iref,
  1563. -refs_to_drop, NULL);
  1564. } else if (is_data) {
  1565. ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
  1566. } else {
  1567. ret = btrfs_del_item(trans, root, path);
  1568. }
  1569. return ret;
  1570. }
  1571. static void btrfs_issue_discard(struct block_device *bdev,
  1572. u64 start, u64 len)
  1573. {
  1574. blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
  1575. BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
  1576. }
  1577. static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
  1578. u64 num_bytes)
  1579. {
  1580. int ret;
  1581. u64 map_length = num_bytes;
  1582. struct btrfs_multi_bio *multi = NULL;
  1583. if (!btrfs_test_opt(root, DISCARD))
  1584. return 0;
  1585. /* Tell the block device(s) that the sectors can be discarded */
  1586. ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
  1587. bytenr, &map_length, &multi, 0);
  1588. if (!ret) {
  1589. struct btrfs_bio_stripe *stripe = multi->stripes;
  1590. int i;
  1591. if (map_length > num_bytes)
  1592. map_length = num_bytes;
  1593. for (i = 0; i < multi->num_stripes; i++, stripe++) {
  1594. btrfs_issue_discard(stripe->dev->bdev,
  1595. stripe->physical,
  1596. map_length);
  1597. }
  1598. kfree(multi);
  1599. }
  1600. return ret;
  1601. }
  1602. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1603. struct btrfs_root *root,
  1604. u64 bytenr, u64 num_bytes, u64 parent,
  1605. u64 root_objectid, u64 owner, u64 offset)
  1606. {
  1607. int ret;
  1608. BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
  1609. root_objectid == BTRFS_TREE_LOG_OBJECTID);
  1610. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1611. ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
  1612. parent, root_objectid, (int)owner,
  1613. BTRFS_ADD_DELAYED_REF, NULL);
  1614. } else {
  1615. ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
  1616. parent, root_objectid, owner, offset,
  1617. BTRFS_ADD_DELAYED_REF, NULL);
  1618. }
  1619. return ret;
  1620. }
  1621. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1622. struct btrfs_root *root,
  1623. u64 bytenr, u64 num_bytes,
  1624. u64 parent, u64 root_objectid,
  1625. u64 owner, u64 offset, int refs_to_add,
  1626. struct btrfs_delayed_extent_op *extent_op)
  1627. {
  1628. struct btrfs_path *path;
  1629. struct extent_buffer *leaf;
  1630. struct btrfs_extent_item *item;
  1631. u64 refs;
  1632. int ret;
  1633. int err = 0;
  1634. path = btrfs_alloc_path();
  1635. if (!path)
  1636. return -ENOMEM;
  1637. path->reada = 1;
  1638. path->leave_spinning = 1;
  1639. /* this will setup the path even if it fails to insert the back ref */
  1640. ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
  1641. path, bytenr, num_bytes, parent,
  1642. root_objectid, owner, offset,
  1643. refs_to_add, extent_op);
  1644. if (ret == 0)
  1645. goto out;
  1646. if (ret != -EAGAIN) {
  1647. err = ret;
  1648. goto out;
  1649. }
  1650. leaf = path->nodes[0];
  1651. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1652. refs = btrfs_extent_refs(leaf, item);
  1653. btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
  1654. if (extent_op)
  1655. __run_delayed_extent_op(extent_op, leaf, item);
  1656. btrfs_mark_buffer_dirty(leaf);
  1657. btrfs_release_path(root->fs_info->extent_root, path);
  1658. path->reada = 1;
  1659. path->leave_spinning = 1;
  1660. /* now insert the actual backref */
  1661. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  1662. path, bytenr, parent, root_objectid,
  1663. owner, offset, refs_to_add);
  1664. BUG_ON(ret);
  1665. out:
  1666. btrfs_free_path(path);
  1667. return err;
  1668. }
  1669. static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
  1670. struct btrfs_root *root,
  1671. struct btrfs_delayed_ref_node *node,
  1672. struct btrfs_delayed_extent_op *extent_op,
  1673. int insert_reserved)
  1674. {
  1675. int ret = 0;
  1676. struct btrfs_delayed_data_ref *ref;
  1677. struct btrfs_key ins;
  1678. u64 parent = 0;
  1679. u64 ref_root = 0;
  1680. u64 flags = 0;
  1681. ins.objectid = node->bytenr;
  1682. ins.offset = node->num_bytes;
  1683. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1684. ref = btrfs_delayed_node_to_data_ref(node);
  1685. if (node->type == BTRFS_SHARED_DATA_REF_KEY)
  1686. parent = ref->parent;
  1687. else
  1688. ref_root = ref->root;
  1689. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1690. if (extent_op) {
  1691. BUG_ON(extent_op->update_key);
  1692. flags |= extent_op->flags_to_set;
  1693. }
  1694. ret = alloc_reserved_file_extent(trans, root,
  1695. parent, ref_root, flags,
  1696. ref->objectid, ref->offset,
  1697. &ins, node->ref_mod);
  1698. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1699. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1700. node->num_bytes, parent,
  1701. ref_root, ref->objectid,
  1702. ref->offset, node->ref_mod,
  1703. extent_op);
  1704. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1705. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1706. node->num_bytes, parent,
  1707. ref_root, ref->objectid,
  1708. ref->offset, node->ref_mod,
  1709. extent_op);
  1710. } else {
  1711. BUG();
  1712. }
  1713. return ret;
  1714. }
  1715. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  1716. struct extent_buffer *leaf,
  1717. struct btrfs_extent_item *ei)
  1718. {
  1719. u64 flags = btrfs_extent_flags(leaf, ei);
  1720. if (extent_op->update_flags) {
  1721. flags |= extent_op->flags_to_set;
  1722. btrfs_set_extent_flags(leaf, ei, flags);
  1723. }
  1724. if (extent_op->update_key) {
  1725. struct btrfs_tree_block_info *bi;
  1726. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
  1727. bi = (struct btrfs_tree_block_info *)(ei + 1);
  1728. btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
  1729. }
  1730. }
  1731. static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
  1732. struct btrfs_root *root,
  1733. struct btrfs_delayed_ref_node *node,
  1734. struct btrfs_delayed_extent_op *extent_op)
  1735. {
  1736. struct btrfs_key key;
  1737. struct btrfs_path *path;
  1738. struct btrfs_extent_item *ei;
  1739. struct extent_buffer *leaf;
  1740. u32 item_size;
  1741. int ret;
  1742. int err = 0;
  1743. path = btrfs_alloc_path();
  1744. if (!path)
  1745. return -ENOMEM;
  1746. key.objectid = node->bytenr;
  1747. key.type = BTRFS_EXTENT_ITEM_KEY;
  1748. key.offset = node->num_bytes;
  1749. path->reada = 1;
  1750. path->leave_spinning = 1;
  1751. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
  1752. path, 0, 1);
  1753. if (ret < 0) {
  1754. err = ret;
  1755. goto out;
  1756. }
  1757. if (ret > 0) {
  1758. err = -EIO;
  1759. goto out;
  1760. }
  1761. leaf = path->nodes[0];
  1762. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1763. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1764. if (item_size < sizeof(*ei)) {
  1765. ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
  1766. path, (u64)-1, 0);
  1767. if (ret < 0) {
  1768. err = ret;
  1769. goto out;
  1770. }
  1771. leaf = path->nodes[0];
  1772. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1773. }
  1774. #endif
  1775. BUG_ON(item_size < sizeof(*ei));
  1776. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1777. __run_delayed_extent_op(extent_op, leaf, ei);
  1778. btrfs_mark_buffer_dirty(leaf);
  1779. out:
  1780. btrfs_free_path(path);
  1781. return err;
  1782. }
  1783. static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
  1784. struct btrfs_root *root,
  1785. struct btrfs_delayed_ref_node *node,
  1786. struct btrfs_delayed_extent_op *extent_op,
  1787. int insert_reserved)
  1788. {
  1789. int ret = 0;
  1790. struct btrfs_delayed_tree_ref *ref;
  1791. struct btrfs_key ins;
  1792. u64 parent = 0;
  1793. u64 ref_root = 0;
  1794. ins.objectid = node->bytenr;
  1795. ins.offset = node->num_bytes;
  1796. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1797. ref = btrfs_delayed_node_to_tree_ref(node);
  1798. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1799. parent = ref->parent;
  1800. else
  1801. ref_root = ref->root;
  1802. BUG_ON(node->ref_mod != 1);
  1803. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1804. BUG_ON(!extent_op || !extent_op->update_flags ||
  1805. !extent_op->update_key);
  1806. ret = alloc_reserved_tree_block(trans, root,
  1807. parent, ref_root,
  1808. extent_op->flags_to_set,
  1809. &extent_op->key,
  1810. ref->level, &ins);
  1811. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1812. ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
  1813. node->num_bytes, parent, ref_root,
  1814. ref->level, 0, 1, extent_op);
  1815. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1816. ret = __btrfs_free_extent(trans, root, node->bytenr,
  1817. node->num_bytes, parent, ref_root,
  1818. ref->level, 0, 1, extent_op);
  1819. } else {
  1820. BUG();
  1821. }
  1822. return ret;
  1823. }
  1824. /* helper function to actually process a single delayed ref entry */
  1825. static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  1826. struct btrfs_root *root,
  1827. struct btrfs_delayed_ref_node *node,
  1828. struct btrfs_delayed_extent_op *extent_op,
  1829. int insert_reserved)
  1830. {
  1831. int ret;
  1832. if (btrfs_delayed_ref_is_head(node)) {
  1833. struct btrfs_delayed_ref_head *head;
  1834. /*
  1835. * we've hit the end of the chain and we were supposed
  1836. * to insert this extent into the tree. But, it got
  1837. * deleted before we ever needed to insert it, so all
  1838. * we have to do is clean up the accounting
  1839. */
  1840. BUG_ON(extent_op);
  1841. head = btrfs_delayed_node_to_head(node);
  1842. if (insert_reserved) {
  1843. btrfs_pin_extent(root, node->bytenr,
  1844. node->num_bytes, 1);
  1845. if (head->is_data) {
  1846. ret = btrfs_del_csums(trans, root,
  1847. node->bytenr,
  1848. node->num_bytes);
  1849. BUG_ON(ret);
  1850. }
  1851. }
  1852. mutex_unlock(&head->mutex);
  1853. return 0;
  1854. }
  1855. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  1856. node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1857. ret = run_delayed_tree_ref(trans, root, node, extent_op,
  1858. insert_reserved);
  1859. else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  1860. node->type == BTRFS_SHARED_DATA_REF_KEY)
  1861. ret = run_delayed_data_ref(trans, root, node, extent_op,
  1862. insert_reserved);
  1863. else
  1864. BUG();
  1865. return ret;
  1866. }
  1867. static noinline struct btrfs_delayed_ref_node *
  1868. select_delayed_ref(struct btrfs_delayed_ref_head *head)
  1869. {
  1870. struct rb_node *node;
  1871. struct btrfs_delayed_ref_node *ref;
  1872. int action = BTRFS_ADD_DELAYED_REF;
  1873. again:
  1874. /*
  1875. * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
  1876. * this prevents ref count from going down to zero when
  1877. * there still are pending delayed ref.
  1878. */
  1879. node = rb_prev(&head->node.rb_node);
  1880. while (1) {
  1881. if (!node)
  1882. break;
  1883. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  1884. rb_node);
  1885. if (ref->bytenr != head->node.bytenr)
  1886. break;
  1887. if (ref->action == action)
  1888. return ref;
  1889. node = rb_prev(node);
  1890. }
  1891. if (action == BTRFS_ADD_DELAYED_REF) {
  1892. action = BTRFS_DROP_DELAYED_REF;
  1893. goto again;
  1894. }
  1895. return NULL;
  1896. }
  1897. static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
  1898. struct btrfs_root *root,
  1899. struct list_head *cluster)
  1900. {
  1901. struct btrfs_delayed_ref_root *delayed_refs;
  1902. struct btrfs_delayed_ref_node *ref;
  1903. struct btrfs_delayed_ref_head *locked_ref = NULL;
  1904. struct btrfs_delayed_extent_op *extent_op;
  1905. int ret;
  1906. int count = 0;
  1907. int must_insert_reserved = 0;
  1908. delayed_refs = &trans->transaction->delayed_refs;
  1909. while (1) {
  1910. if (!locked_ref) {
  1911. /* pick a new head ref from the cluster list */
  1912. if (list_empty(cluster))
  1913. break;
  1914. locked_ref = list_entry(cluster->next,
  1915. struct btrfs_delayed_ref_head, cluster);
  1916. /* grab the lock that says we are going to process
  1917. * all the refs for this head */
  1918. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  1919. /*
  1920. * we may have dropped the spin lock to get the head
  1921. * mutex lock, and that might have given someone else
  1922. * time to free the head. If that's true, it has been
  1923. * removed from our list and we can move on.
  1924. */
  1925. if (ret == -EAGAIN) {
  1926. locked_ref = NULL;
  1927. count++;
  1928. continue;
  1929. }
  1930. }
  1931. /*
  1932. * record the must insert reserved flag before we
  1933. * drop the spin lock.
  1934. */
  1935. must_insert_reserved = locked_ref->must_insert_reserved;
  1936. locked_ref->must_insert_reserved = 0;
  1937. extent_op = locked_ref->extent_op;
  1938. locked_ref->extent_op = NULL;
  1939. /*
  1940. * locked_ref is the head node, so we have to go one
  1941. * node back for any delayed ref updates
  1942. */
  1943. ref = select_delayed_ref(locked_ref);
  1944. if (!ref) {
  1945. /* All delayed refs have been processed, Go ahead
  1946. * and send the head node to run_one_delayed_ref,
  1947. * so that any accounting fixes can happen
  1948. */
  1949. ref = &locked_ref->node;
  1950. if (extent_op && must_insert_reserved) {
  1951. kfree(extent_op);
  1952. extent_op = NULL;
  1953. }
  1954. if (extent_op) {
  1955. spin_unlock(&delayed_refs->lock);
  1956. ret = run_delayed_extent_op(trans, root,
  1957. ref, extent_op);
  1958. BUG_ON(ret);
  1959. kfree(extent_op);
  1960. cond_resched();
  1961. spin_lock(&delayed_refs->lock);
  1962. continue;
  1963. }
  1964. list_del_init(&locked_ref->cluster);
  1965. locked_ref = NULL;
  1966. }
  1967. ref->in_tree = 0;
  1968. rb_erase(&ref->rb_node, &delayed_refs->root);
  1969. delayed_refs->num_entries--;
  1970. spin_unlock(&delayed_refs->lock);
  1971. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  1972. must_insert_reserved);
  1973. BUG_ON(ret);
  1974. btrfs_put_delayed_ref(ref);
  1975. kfree(extent_op);
  1976. count++;
  1977. cond_resched();
  1978. spin_lock(&delayed_refs->lock);
  1979. }
  1980. return count;
  1981. }
  1982. /*
  1983. * this starts processing the delayed reference count updates and
  1984. * extent insertions we have queued up so far. count can be
  1985. * 0, which means to process everything in the tree at the start
  1986. * of the run (but not newly added entries), or it can be some target
  1987. * number you'd like to process.
  1988. */
  1989. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  1990. struct btrfs_root *root, unsigned long count)
  1991. {
  1992. struct rb_node *node;
  1993. struct btrfs_delayed_ref_root *delayed_refs;
  1994. struct btrfs_delayed_ref_node *ref;
  1995. struct list_head cluster;
  1996. int ret;
  1997. int run_all = count == (unsigned long)-1;
  1998. int run_most = 0;
  1999. if (root == root->fs_info->extent_root)
  2000. root = root->fs_info->tree_root;
  2001. delayed_refs = &trans->transaction->delayed_refs;
  2002. INIT_LIST_HEAD(&cluster);
  2003. again:
  2004. spin_lock(&delayed_refs->lock);
  2005. if (count == 0) {
  2006. count = delayed_refs->num_entries * 2;
  2007. run_most = 1;
  2008. }
  2009. while (1) {
  2010. if (!(run_all || run_most) &&
  2011. delayed_refs->num_heads_ready < 64)
  2012. break;
  2013. /*
  2014. * go find something we can process in the rbtree. We start at
  2015. * the beginning of the tree, and then build a cluster
  2016. * of refs to process starting at the first one we are able to
  2017. * lock
  2018. */
  2019. ret = btrfs_find_ref_cluster(trans, &cluster,
  2020. delayed_refs->run_delayed_start);
  2021. if (ret)
  2022. break;
  2023. ret = run_clustered_refs(trans, root, &cluster);
  2024. BUG_ON(ret < 0);
  2025. count -= min_t(unsigned long, ret, count);
  2026. if (count == 0)
  2027. break;
  2028. }
  2029. if (run_all) {
  2030. node = rb_first(&delayed_refs->root);
  2031. if (!node)
  2032. goto out;
  2033. count = (unsigned long)-1;
  2034. while (node) {
  2035. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2036. rb_node);
  2037. if (btrfs_delayed_ref_is_head(ref)) {
  2038. struct btrfs_delayed_ref_head *head;
  2039. head = btrfs_delayed_node_to_head(ref);
  2040. atomic_inc(&ref->refs);
  2041. spin_unlock(&delayed_refs->lock);
  2042. mutex_lock(&head->mutex);
  2043. mutex_unlock(&head->mutex);
  2044. btrfs_put_delayed_ref(ref);
  2045. cond_resched();
  2046. goto again;
  2047. }
  2048. node = rb_next(node);
  2049. }
  2050. spin_unlock(&delayed_refs->lock);
  2051. schedule_timeout(1);
  2052. goto again;
  2053. }
  2054. out:
  2055. spin_unlock(&delayed_refs->lock);
  2056. return 0;
  2057. }
  2058. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2059. struct btrfs_root *root,
  2060. u64 bytenr, u64 num_bytes, u64 flags,
  2061. int is_data)
  2062. {
  2063. struct btrfs_delayed_extent_op *extent_op;
  2064. int ret;
  2065. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2066. if (!extent_op)
  2067. return -ENOMEM;
  2068. extent_op->flags_to_set = flags;
  2069. extent_op->update_flags = 1;
  2070. extent_op->update_key = 0;
  2071. extent_op->is_data = is_data ? 1 : 0;
  2072. ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
  2073. if (ret)
  2074. kfree(extent_op);
  2075. return ret;
  2076. }
  2077. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2078. struct btrfs_root *root,
  2079. struct btrfs_path *path,
  2080. u64 objectid, u64 offset, u64 bytenr)
  2081. {
  2082. struct btrfs_delayed_ref_head *head;
  2083. struct btrfs_delayed_ref_node *ref;
  2084. struct btrfs_delayed_data_ref *data_ref;
  2085. struct btrfs_delayed_ref_root *delayed_refs;
  2086. struct rb_node *node;
  2087. int ret = 0;
  2088. ret = -ENOENT;
  2089. delayed_refs = &trans->transaction->delayed_refs;
  2090. spin_lock(&delayed_refs->lock);
  2091. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2092. if (!head)
  2093. goto out;
  2094. if (!mutex_trylock(&head->mutex)) {
  2095. atomic_inc(&head->node.refs);
  2096. spin_unlock(&delayed_refs->lock);
  2097. btrfs_release_path(root->fs_info->extent_root, path);
  2098. mutex_lock(&head->mutex);
  2099. mutex_unlock(&head->mutex);
  2100. btrfs_put_delayed_ref(&head->node);
  2101. return -EAGAIN;
  2102. }
  2103. node = rb_prev(&head->node.rb_node);
  2104. if (!node)
  2105. goto out_unlock;
  2106. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2107. if (ref->bytenr != bytenr)
  2108. goto out_unlock;
  2109. ret = 1;
  2110. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
  2111. goto out_unlock;
  2112. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2113. node = rb_prev(node);
  2114. if (node) {
  2115. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2116. if (ref->bytenr == bytenr)
  2117. goto out_unlock;
  2118. }
  2119. if (data_ref->root != root->root_key.objectid ||
  2120. data_ref->objectid != objectid || data_ref->offset != offset)
  2121. goto out_unlock;
  2122. ret = 0;
  2123. out_unlock:
  2124. mutex_unlock(&head->mutex);
  2125. out:
  2126. spin_unlock(&delayed_refs->lock);
  2127. return ret;
  2128. }
  2129. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2130. struct btrfs_root *root,
  2131. struct btrfs_path *path,
  2132. u64 objectid, u64 offset, u64 bytenr)
  2133. {
  2134. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2135. struct extent_buffer *leaf;
  2136. struct btrfs_extent_data_ref *ref;
  2137. struct btrfs_extent_inline_ref *iref;
  2138. struct btrfs_extent_item *ei;
  2139. struct btrfs_key key;
  2140. u32 item_size;
  2141. int ret;
  2142. key.objectid = bytenr;
  2143. key.offset = (u64)-1;
  2144. key.type = BTRFS_EXTENT_ITEM_KEY;
  2145. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2146. if (ret < 0)
  2147. goto out;
  2148. BUG_ON(ret == 0);
  2149. ret = -ENOENT;
  2150. if (path->slots[0] == 0)
  2151. goto out;
  2152. path->slots[0]--;
  2153. leaf = path->nodes[0];
  2154. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2155. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2156. goto out;
  2157. ret = 1;
  2158. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2159. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2160. if (item_size < sizeof(*ei)) {
  2161. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2162. goto out;
  2163. }
  2164. #endif
  2165. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2166. if (item_size != sizeof(*ei) +
  2167. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2168. goto out;
  2169. if (btrfs_extent_generation(leaf, ei) <=
  2170. btrfs_root_last_snapshot(&root->root_item))
  2171. goto out;
  2172. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2173. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2174. BTRFS_EXTENT_DATA_REF_KEY)
  2175. goto out;
  2176. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2177. if (btrfs_extent_refs(leaf, ei) !=
  2178. btrfs_extent_data_ref_count(leaf, ref) ||
  2179. btrfs_extent_data_ref_root(leaf, ref) !=
  2180. root->root_key.objectid ||
  2181. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2182. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2183. goto out;
  2184. ret = 0;
  2185. out:
  2186. return ret;
  2187. }
  2188. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2189. struct btrfs_root *root,
  2190. u64 objectid, u64 offset, u64 bytenr)
  2191. {
  2192. struct btrfs_path *path;
  2193. int ret;
  2194. int ret2;
  2195. path = btrfs_alloc_path();
  2196. if (!path)
  2197. return -ENOENT;
  2198. do {
  2199. ret = check_committed_ref(trans, root, path, objectid,
  2200. offset, bytenr);
  2201. if (ret && ret != -ENOENT)
  2202. goto out;
  2203. ret2 = check_delayed_ref(trans, root, path, objectid,
  2204. offset, bytenr);
  2205. } while (ret2 == -EAGAIN);
  2206. if (ret2 && ret2 != -ENOENT) {
  2207. ret = ret2;
  2208. goto out;
  2209. }
  2210. if (ret != -ENOENT || ret2 != -ENOENT)
  2211. ret = 0;
  2212. out:
  2213. btrfs_free_path(path);
  2214. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2215. WARN_ON(ret > 0);
  2216. return ret;
  2217. }
  2218. #if 0
  2219. int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2220. struct extent_buffer *buf, u32 nr_extents)
  2221. {
  2222. struct btrfs_key key;
  2223. struct btrfs_file_extent_item *fi;
  2224. u64 root_gen;
  2225. u32 nritems;
  2226. int i;
  2227. int level;
  2228. int ret = 0;
  2229. int shared = 0;
  2230. if (!root->ref_cows)
  2231. return 0;
  2232. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  2233. shared = 0;
  2234. root_gen = root->root_key.offset;
  2235. } else {
  2236. shared = 1;
  2237. root_gen = trans->transid - 1;
  2238. }
  2239. level = btrfs_header_level(buf);
  2240. nritems = btrfs_header_nritems(buf);
  2241. if (level == 0) {
  2242. struct btrfs_leaf_ref *ref;
  2243. struct btrfs_extent_info *info;
  2244. ref = btrfs_alloc_leaf_ref(root, nr_extents);
  2245. if (!ref) {
  2246. ret = -ENOMEM;
  2247. goto out;
  2248. }
  2249. ref->root_gen = root_gen;
  2250. ref->bytenr = buf->start;
  2251. ref->owner = btrfs_header_owner(buf);
  2252. ref->generation = btrfs_header_generation(buf);
  2253. ref->nritems = nr_extents;
  2254. info = ref->extents;
  2255. for (i = 0; nr_extents > 0 && i < nritems; i++) {
  2256. u64 disk_bytenr;
  2257. btrfs_item_key_to_cpu(buf, &key, i);
  2258. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2259. continue;
  2260. fi = btrfs_item_ptr(buf, i,
  2261. struct btrfs_file_extent_item);
  2262. if (btrfs_file_extent_type(buf, fi) ==
  2263. BTRFS_FILE_EXTENT_INLINE)
  2264. continue;
  2265. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2266. if (disk_bytenr == 0)
  2267. continue;
  2268. info->bytenr = disk_bytenr;
  2269. info->num_bytes =
  2270. btrfs_file_extent_disk_num_bytes(buf, fi);
  2271. info->objectid = key.objectid;
  2272. info->offset = key.offset;
  2273. info++;
  2274. }
  2275. ret = btrfs_add_leaf_ref(root, ref, shared);
  2276. if (ret == -EEXIST && shared) {
  2277. struct btrfs_leaf_ref *old;
  2278. old = btrfs_lookup_leaf_ref(root, ref->bytenr);
  2279. BUG_ON(!old);
  2280. btrfs_remove_leaf_ref(root, old);
  2281. btrfs_free_leaf_ref(root, old);
  2282. ret = btrfs_add_leaf_ref(root, ref, shared);
  2283. }
  2284. WARN_ON(ret);
  2285. btrfs_free_leaf_ref(root, ref);
  2286. }
  2287. out:
  2288. return ret;
  2289. }
  2290. /* when a block goes through cow, we update the reference counts of
  2291. * everything that block points to. The internal pointers of the block
  2292. * can be in just about any order, and it is likely to have clusters of
  2293. * things that are close together and clusters of things that are not.
  2294. *
  2295. * To help reduce the seeks that come with updating all of these reference
  2296. * counts, sort them by byte number before actual updates are done.
  2297. *
  2298. * struct refsort is used to match byte number to slot in the btree block.
  2299. * we sort based on the byte number and then use the slot to actually
  2300. * find the item.
  2301. *
  2302. * struct refsort is smaller than strcut btrfs_item and smaller than
  2303. * struct btrfs_key_ptr. Since we're currently limited to the page size
  2304. * for a btree block, there's no way for a kmalloc of refsorts for a
  2305. * single node to be bigger than a page.
  2306. */
  2307. struct refsort {
  2308. u64 bytenr;
  2309. u32 slot;
  2310. };
  2311. /*
  2312. * for passing into sort()
  2313. */
  2314. static int refsort_cmp(const void *a_void, const void *b_void)
  2315. {
  2316. const struct refsort *a = a_void;
  2317. const struct refsort *b = b_void;
  2318. if (a->bytenr < b->bytenr)
  2319. return -1;
  2320. if (a->bytenr > b->bytenr)
  2321. return 1;
  2322. return 0;
  2323. }
  2324. #endif
  2325. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2326. struct btrfs_root *root,
  2327. struct extent_buffer *buf,
  2328. int full_backref, int inc)
  2329. {
  2330. u64 bytenr;
  2331. u64 num_bytes;
  2332. u64 parent;
  2333. u64 ref_root;
  2334. u32 nritems;
  2335. struct btrfs_key key;
  2336. struct btrfs_file_extent_item *fi;
  2337. int i;
  2338. int level;
  2339. int ret = 0;
  2340. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2341. u64, u64, u64, u64, u64, u64);
  2342. ref_root = btrfs_header_owner(buf);
  2343. nritems = btrfs_header_nritems(buf);
  2344. level = btrfs_header_level(buf);
  2345. if (!root->ref_cows && level == 0)
  2346. return 0;
  2347. if (inc)
  2348. process_func = btrfs_inc_extent_ref;
  2349. else
  2350. process_func = btrfs_free_extent;
  2351. if (full_backref)
  2352. parent = buf->start;
  2353. else
  2354. parent = 0;
  2355. for (i = 0; i < nritems; i++) {
  2356. if (level == 0) {
  2357. btrfs_item_key_to_cpu(buf, &key, i);
  2358. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2359. continue;
  2360. fi = btrfs_item_ptr(buf, i,
  2361. struct btrfs_file_extent_item);
  2362. if (btrfs_file_extent_type(buf, fi) ==
  2363. BTRFS_FILE_EXTENT_INLINE)
  2364. continue;
  2365. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2366. if (bytenr == 0)
  2367. continue;
  2368. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2369. key.offset -= btrfs_file_extent_offset(buf, fi);
  2370. ret = process_func(trans, root, bytenr, num_bytes,
  2371. parent, ref_root, key.objectid,
  2372. key.offset);
  2373. if (ret)
  2374. goto fail;
  2375. } else {
  2376. bytenr = btrfs_node_blockptr(buf, i);
  2377. num_bytes = btrfs_level_size(root, level - 1);
  2378. ret = process_func(trans, root, bytenr, num_bytes,
  2379. parent, ref_root, level - 1, 0);
  2380. if (ret)
  2381. goto fail;
  2382. }
  2383. }
  2384. return 0;
  2385. fail:
  2386. BUG();
  2387. return ret;
  2388. }
  2389. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2390. struct extent_buffer *buf, int full_backref)
  2391. {
  2392. return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
  2393. }
  2394. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2395. struct extent_buffer *buf, int full_backref)
  2396. {
  2397. return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
  2398. }
  2399. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2400. struct btrfs_root *root,
  2401. struct btrfs_path *path,
  2402. struct btrfs_block_group_cache *cache)
  2403. {
  2404. int ret;
  2405. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2406. unsigned long bi;
  2407. struct extent_buffer *leaf;
  2408. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2409. if (ret < 0)
  2410. goto fail;
  2411. BUG_ON(ret);
  2412. leaf = path->nodes[0];
  2413. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2414. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2415. btrfs_mark_buffer_dirty(leaf);
  2416. btrfs_release_path(extent_root, path);
  2417. fail:
  2418. if (ret)
  2419. return ret;
  2420. return 0;
  2421. }
  2422. static struct btrfs_block_group_cache *
  2423. next_block_group(struct btrfs_root *root,
  2424. struct btrfs_block_group_cache *cache)
  2425. {
  2426. struct rb_node *node;
  2427. spin_lock(&root->fs_info->block_group_cache_lock);
  2428. node = rb_next(&cache->cache_node);
  2429. btrfs_put_block_group(cache);
  2430. if (node) {
  2431. cache = rb_entry(node, struct btrfs_block_group_cache,
  2432. cache_node);
  2433. btrfs_get_block_group(cache);
  2434. } else
  2435. cache = NULL;
  2436. spin_unlock(&root->fs_info->block_group_cache_lock);
  2437. return cache;
  2438. }
  2439. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2440. struct btrfs_trans_handle *trans,
  2441. struct btrfs_path *path)
  2442. {
  2443. struct btrfs_root *root = block_group->fs_info->tree_root;
  2444. struct inode *inode = NULL;
  2445. u64 alloc_hint = 0;
  2446. int num_pages = 0;
  2447. int retries = 0;
  2448. int ret = 0;
  2449. /*
  2450. * If this block group is smaller than 100 megs don't bother caching the
  2451. * block group.
  2452. */
  2453. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2454. spin_lock(&block_group->lock);
  2455. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2456. spin_unlock(&block_group->lock);
  2457. return 0;
  2458. }
  2459. again:
  2460. inode = lookup_free_space_inode(root, block_group, path);
  2461. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2462. ret = PTR_ERR(inode);
  2463. btrfs_release_path(root, path);
  2464. goto out;
  2465. }
  2466. if (IS_ERR(inode)) {
  2467. BUG_ON(retries);
  2468. retries++;
  2469. if (block_group->ro)
  2470. goto out_free;
  2471. ret = create_free_space_inode(root, trans, block_group, path);
  2472. if (ret)
  2473. goto out_free;
  2474. goto again;
  2475. }
  2476. /*
  2477. * We want to set the generation to 0, that way if anything goes wrong
  2478. * from here on out we know not to trust this cache when we load up next
  2479. * time.
  2480. */
  2481. BTRFS_I(inode)->generation = 0;
  2482. ret = btrfs_update_inode(trans, root, inode);
  2483. WARN_ON(ret);
  2484. if (i_size_read(inode) > 0) {
  2485. ret = btrfs_truncate_free_space_cache(root, trans, path,
  2486. inode);
  2487. if (ret)
  2488. goto out_put;
  2489. }
  2490. spin_lock(&block_group->lock);
  2491. if (block_group->cached != BTRFS_CACHE_FINISHED) {
  2492. spin_unlock(&block_group->lock);
  2493. goto out_put;
  2494. }
  2495. spin_unlock(&block_group->lock);
  2496. num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
  2497. if (!num_pages)
  2498. num_pages = 1;
  2499. /*
  2500. * Just to make absolutely sure we have enough space, we're going to
  2501. * preallocate 12 pages worth of space for each block group. In
  2502. * practice we ought to use at most 8, but we need extra space so we can
  2503. * add our header and have a terminator between the extents and the
  2504. * bitmaps.
  2505. */
  2506. num_pages *= 16;
  2507. num_pages *= PAGE_CACHE_SIZE;
  2508. ret = btrfs_check_data_free_space(inode, num_pages);
  2509. if (ret)
  2510. goto out_put;
  2511. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2512. num_pages, num_pages,
  2513. &alloc_hint);
  2514. btrfs_free_reserved_data_space(inode, num_pages);
  2515. out_put:
  2516. iput(inode);
  2517. out_free:
  2518. btrfs_release_path(root, path);
  2519. out:
  2520. spin_lock(&block_group->lock);
  2521. if (ret)
  2522. block_group->disk_cache_state = BTRFS_DC_ERROR;
  2523. else
  2524. block_group->disk_cache_state = BTRFS_DC_SETUP;
  2525. spin_unlock(&block_group->lock);
  2526. return ret;
  2527. }
  2528. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2529. struct btrfs_root *root)
  2530. {
  2531. struct btrfs_block_group_cache *cache;
  2532. int err = 0;
  2533. struct btrfs_path *path;
  2534. u64 last = 0;
  2535. path = btrfs_alloc_path();
  2536. if (!path)
  2537. return -ENOMEM;
  2538. again:
  2539. while (1) {
  2540. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2541. while (cache) {
  2542. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2543. break;
  2544. cache = next_block_group(root, cache);
  2545. }
  2546. if (!cache) {
  2547. if (last == 0)
  2548. break;
  2549. last = 0;
  2550. continue;
  2551. }
  2552. err = cache_save_setup(cache, trans, path);
  2553. last = cache->key.objectid + cache->key.offset;
  2554. btrfs_put_block_group(cache);
  2555. }
  2556. while (1) {
  2557. if (last == 0) {
  2558. err = btrfs_run_delayed_refs(trans, root,
  2559. (unsigned long)-1);
  2560. BUG_ON(err);
  2561. }
  2562. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2563. while (cache) {
  2564. if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
  2565. btrfs_put_block_group(cache);
  2566. goto again;
  2567. }
  2568. if (cache->dirty)
  2569. break;
  2570. cache = next_block_group(root, cache);
  2571. }
  2572. if (!cache) {
  2573. if (last == 0)
  2574. break;
  2575. last = 0;
  2576. continue;
  2577. }
  2578. if (cache->disk_cache_state == BTRFS_DC_SETUP)
  2579. cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
  2580. cache->dirty = 0;
  2581. last = cache->key.objectid + cache->key.offset;
  2582. err = write_one_cache_group(trans, root, path, cache);
  2583. BUG_ON(err);
  2584. btrfs_put_block_group(cache);
  2585. }
  2586. while (1) {
  2587. /*
  2588. * I don't think this is needed since we're just marking our
  2589. * preallocated extent as written, but just in case it can't
  2590. * hurt.
  2591. */
  2592. if (last == 0) {
  2593. err = btrfs_run_delayed_refs(trans, root,
  2594. (unsigned long)-1);
  2595. BUG_ON(err);
  2596. }
  2597. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2598. while (cache) {
  2599. /*
  2600. * Really this shouldn't happen, but it could if we
  2601. * couldn't write the entire preallocated extent and
  2602. * splitting the extent resulted in a new block.
  2603. */
  2604. if (cache->dirty) {
  2605. btrfs_put_block_group(cache);
  2606. goto again;
  2607. }
  2608. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2609. break;
  2610. cache = next_block_group(root, cache);
  2611. }
  2612. if (!cache) {
  2613. if (last == 0)
  2614. break;
  2615. last = 0;
  2616. continue;
  2617. }
  2618. btrfs_write_out_cache(root, trans, cache, path);
  2619. /*
  2620. * If we didn't have an error then the cache state is still
  2621. * NEED_WRITE, so we can set it to WRITTEN.
  2622. */
  2623. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2624. cache->disk_cache_state = BTRFS_DC_WRITTEN;
  2625. last = cache->key.objectid + cache->key.offset;
  2626. btrfs_put_block_group(cache);
  2627. }
  2628. btrfs_free_path(path);
  2629. return 0;
  2630. }
  2631. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2632. {
  2633. struct btrfs_block_group_cache *block_group;
  2634. int readonly = 0;
  2635. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2636. if (!block_group || block_group->ro)
  2637. readonly = 1;
  2638. if (block_group)
  2639. btrfs_put_block_group(block_group);
  2640. return readonly;
  2641. }
  2642. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  2643. u64 total_bytes, u64 bytes_used,
  2644. struct btrfs_space_info **space_info)
  2645. {
  2646. struct btrfs_space_info *found;
  2647. int i;
  2648. int factor;
  2649. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2650. BTRFS_BLOCK_GROUP_RAID10))
  2651. factor = 2;
  2652. else
  2653. factor = 1;
  2654. found = __find_space_info(info, flags);
  2655. if (found) {
  2656. spin_lock(&found->lock);
  2657. found->total_bytes += total_bytes;
  2658. found->disk_total += total_bytes * factor;
  2659. found->bytes_used += bytes_used;
  2660. found->disk_used += bytes_used * factor;
  2661. found->full = 0;
  2662. spin_unlock(&found->lock);
  2663. *space_info = found;
  2664. return 0;
  2665. }
  2666. found = kzalloc(sizeof(*found), GFP_NOFS);
  2667. if (!found)
  2668. return -ENOMEM;
  2669. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  2670. INIT_LIST_HEAD(&found->block_groups[i]);
  2671. init_rwsem(&found->groups_sem);
  2672. spin_lock_init(&found->lock);
  2673. found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
  2674. BTRFS_BLOCK_GROUP_SYSTEM |
  2675. BTRFS_BLOCK_GROUP_METADATA);
  2676. found->total_bytes = total_bytes;
  2677. found->disk_total = total_bytes * factor;
  2678. found->bytes_used = bytes_used;
  2679. found->disk_used = bytes_used * factor;
  2680. found->bytes_pinned = 0;
  2681. found->bytes_reserved = 0;
  2682. found->bytes_readonly = 0;
  2683. found->bytes_may_use = 0;
  2684. found->full = 0;
  2685. found->force_alloc = 0;
  2686. *space_info = found;
  2687. list_add_rcu(&found->list, &info->space_info);
  2688. atomic_set(&found->caching_threads, 0);
  2689. return 0;
  2690. }
  2691. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  2692. {
  2693. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  2694. BTRFS_BLOCK_GROUP_RAID1 |
  2695. BTRFS_BLOCK_GROUP_RAID10 |
  2696. BTRFS_BLOCK_GROUP_DUP);
  2697. if (extra_flags) {
  2698. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2699. fs_info->avail_data_alloc_bits |= extra_flags;
  2700. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2701. fs_info->avail_metadata_alloc_bits |= extra_flags;
  2702. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2703. fs_info->avail_system_alloc_bits |= extra_flags;
  2704. }
  2705. }
  2706. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  2707. {
  2708. u64 num_devices = root->fs_info->fs_devices->rw_devices;
  2709. if (num_devices == 1)
  2710. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  2711. if (num_devices < 4)
  2712. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  2713. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  2714. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  2715. BTRFS_BLOCK_GROUP_RAID10))) {
  2716. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  2717. }
  2718. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  2719. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  2720. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  2721. }
  2722. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  2723. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  2724. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  2725. (flags & BTRFS_BLOCK_GROUP_DUP)))
  2726. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  2727. return flags;
  2728. }
  2729. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  2730. {
  2731. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2732. flags |= root->fs_info->avail_data_alloc_bits &
  2733. root->fs_info->data_alloc_profile;
  2734. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2735. flags |= root->fs_info->avail_system_alloc_bits &
  2736. root->fs_info->system_alloc_profile;
  2737. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2738. flags |= root->fs_info->avail_metadata_alloc_bits &
  2739. root->fs_info->metadata_alloc_profile;
  2740. return btrfs_reduce_alloc_profile(root, flags);
  2741. }
  2742. static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  2743. {
  2744. u64 flags;
  2745. if (data)
  2746. flags = BTRFS_BLOCK_GROUP_DATA;
  2747. else if (root == root->fs_info->chunk_root)
  2748. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  2749. else
  2750. flags = BTRFS_BLOCK_GROUP_METADATA;
  2751. return get_alloc_profile(root, flags);
  2752. }
  2753. void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
  2754. {
  2755. BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
  2756. BTRFS_BLOCK_GROUP_DATA);
  2757. }
  2758. /*
  2759. * This will check the space that the inode allocates from to make sure we have
  2760. * enough space for bytes.
  2761. */
  2762. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  2763. {
  2764. struct btrfs_space_info *data_sinfo;
  2765. struct btrfs_root *root = BTRFS_I(inode)->root;
  2766. u64 used;
  2767. int ret = 0, committed = 0, alloc_chunk = 1;
  2768. /* make sure bytes are sectorsize aligned */
  2769. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2770. if (root == root->fs_info->tree_root) {
  2771. alloc_chunk = 0;
  2772. committed = 1;
  2773. }
  2774. data_sinfo = BTRFS_I(inode)->space_info;
  2775. if (!data_sinfo)
  2776. goto alloc;
  2777. again:
  2778. /* make sure we have enough space to handle the data first */
  2779. spin_lock(&data_sinfo->lock);
  2780. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  2781. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  2782. data_sinfo->bytes_may_use;
  2783. if (used + bytes > data_sinfo->total_bytes) {
  2784. struct btrfs_trans_handle *trans;
  2785. /*
  2786. * if we don't have enough free bytes in this space then we need
  2787. * to alloc a new chunk.
  2788. */
  2789. if (!data_sinfo->full && alloc_chunk) {
  2790. u64 alloc_target;
  2791. data_sinfo->force_alloc = 1;
  2792. spin_unlock(&data_sinfo->lock);
  2793. alloc:
  2794. alloc_target = btrfs_get_alloc_profile(root, 1);
  2795. trans = btrfs_join_transaction(root, 1);
  2796. if (IS_ERR(trans))
  2797. return PTR_ERR(trans);
  2798. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2799. bytes + 2 * 1024 * 1024,
  2800. alloc_target, 0);
  2801. btrfs_end_transaction(trans, root);
  2802. if (ret < 0)
  2803. return ret;
  2804. if (!data_sinfo) {
  2805. btrfs_set_inode_space_info(root, inode);
  2806. data_sinfo = BTRFS_I(inode)->space_info;
  2807. }
  2808. goto again;
  2809. }
  2810. spin_unlock(&data_sinfo->lock);
  2811. /* commit the current transaction and try again */
  2812. if (!committed && !root->fs_info->open_ioctl_trans) {
  2813. committed = 1;
  2814. trans = btrfs_join_transaction(root, 1);
  2815. if (IS_ERR(trans))
  2816. return PTR_ERR(trans);
  2817. ret = btrfs_commit_transaction(trans, root);
  2818. if (ret)
  2819. return ret;
  2820. goto again;
  2821. }
  2822. #if 0 /* I hope we never need this code again, just in case */
  2823. printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
  2824. "%llu bytes_reserved, " "%llu bytes_pinned, "
  2825. "%llu bytes_readonly, %llu may use %llu total\n",
  2826. (unsigned long long)bytes,
  2827. (unsigned long long)data_sinfo->bytes_used,
  2828. (unsigned long long)data_sinfo->bytes_reserved,
  2829. (unsigned long long)data_sinfo->bytes_pinned,
  2830. (unsigned long long)data_sinfo->bytes_readonly,
  2831. (unsigned long long)data_sinfo->bytes_may_use,
  2832. (unsigned long long)data_sinfo->total_bytes);
  2833. #endif
  2834. return -ENOSPC;
  2835. }
  2836. data_sinfo->bytes_may_use += bytes;
  2837. BTRFS_I(inode)->reserved_bytes += bytes;
  2838. spin_unlock(&data_sinfo->lock);
  2839. return 0;
  2840. }
  2841. /*
  2842. * called when we are clearing an delalloc extent from the
  2843. * inode's io_tree or there was an error for whatever reason
  2844. * after calling btrfs_check_data_free_space
  2845. */
  2846. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  2847. {
  2848. struct btrfs_root *root = BTRFS_I(inode)->root;
  2849. struct btrfs_space_info *data_sinfo;
  2850. /* make sure bytes are sectorsize aligned */
  2851. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2852. data_sinfo = BTRFS_I(inode)->space_info;
  2853. spin_lock(&data_sinfo->lock);
  2854. data_sinfo->bytes_may_use -= bytes;
  2855. BTRFS_I(inode)->reserved_bytes -= bytes;
  2856. spin_unlock(&data_sinfo->lock);
  2857. }
  2858. static void force_metadata_allocation(struct btrfs_fs_info *info)
  2859. {
  2860. struct list_head *head = &info->space_info;
  2861. struct btrfs_space_info *found;
  2862. rcu_read_lock();
  2863. list_for_each_entry_rcu(found, head, list) {
  2864. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  2865. found->force_alloc = 1;
  2866. }
  2867. rcu_read_unlock();
  2868. }
  2869. static int should_alloc_chunk(struct btrfs_root *root,
  2870. struct btrfs_space_info *sinfo, u64 alloc_bytes)
  2871. {
  2872. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  2873. u64 thresh;
  2874. if (sinfo->bytes_used + sinfo->bytes_reserved +
  2875. alloc_bytes + 256 * 1024 * 1024 < num_bytes)
  2876. return 0;
  2877. if (sinfo->bytes_used + sinfo->bytes_reserved +
  2878. alloc_bytes < div_factor(num_bytes, 8))
  2879. return 0;
  2880. thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
  2881. thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
  2882. if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
  2883. return 0;
  2884. return 1;
  2885. }
  2886. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  2887. struct btrfs_root *extent_root, u64 alloc_bytes,
  2888. u64 flags, int force)
  2889. {
  2890. struct btrfs_space_info *space_info;
  2891. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  2892. int ret = 0;
  2893. mutex_lock(&fs_info->chunk_mutex);
  2894. flags = btrfs_reduce_alloc_profile(extent_root, flags);
  2895. space_info = __find_space_info(extent_root->fs_info, flags);
  2896. if (!space_info) {
  2897. ret = update_space_info(extent_root->fs_info, flags,
  2898. 0, 0, &space_info);
  2899. BUG_ON(ret);
  2900. }
  2901. BUG_ON(!space_info);
  2902. spin_lock(&space_info->lock);
  2903. if (space_info->force_alloc)
  2904. force = 1;
  2905. if (space_info->full) {
  2906. spin_unlock(&space_info->lock);
  2907. goto out;
  2908. }
  2909. if (!force && !should_alloc_chunk(extent_root, space_info,
  2910. alloc_bytes)) {
  2911. spin_unlock(&space_info->lock);
  2912. goto out;
  2913. }
  2914. spin_unlock(&space_info->lock);
  2915. /*
  2916. * If we have mixed data/metadata chunks we want to make sure we keep
  2917. * allocating mixed chunks instead of individual chunks.
  2918. */
  2919. if (btrfs_mixed_space_info(space_info))
  2920. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  2921. /*
  2922. * if we're doing a data chunk, go ahead and make sure that
  2923. * we keep a reasonable number of metadata chunks allocated in the
  2924. * FS as well.
  2925. */
  2926. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  2927. fs_info->data_chunk_allocations++;
  2928. if (!(fs_info->data_chunk_allocations %
  2929. fs_info->metadata_ratio))
  2930. force_metadata_allocation(fs_info);
  2931. }
  2932. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  2933. spin_lock(&space_info->lock);
  2934. if (ret)
  2935. space_info->full = 1;
  2936. else
  2937. ret = 1;
  2938. space_info->force_alloc = 0;
  2939. spin_unlock(&space_info->lock);
  2940. out:
  2941. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  2942. return ret;
  2943. }
  2944. /*
  2945. * shrink metadata reservation for delalloc
  2946. */
  2947. static int shrink_delalloc(struct btrfs_trans_handle *trans,
  2948. struct btrfs_root *root, u64 to_reclaim, int sync)
  2949. {
  2950. struct btrfs_block_rsv *block_rsv;
  2951. struct btrfs_space_info *space_info;
  2952. u64 reserved;
  2953. u64 max_reclaim;
  2954. u64 reclaimed = 0;
  2955. int pause = 1;
  2956. int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
  2957. block_rsv = &root->fs_info->delalloc_block_rsv;
  2958. space_info = block_rsv->space_info;
  2959. smp_mb();
  2960. reserved = space_info->bytes_reserved;
  2961. if (reserved == 0)
  2962. return 0;
  2963. max_reclaim = min(reserved, to_reclaim);
  2964. while (1) {
  2965. /* have the flusher threads jump in and do some IO */
  2966. smp_mb();
  2967. nr_pages = min_t(unsigned long, nr_pages,
  2968. root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
  2969. writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
  2970. spin_lock(&space_info->lock);
  2971. if (reserved > space_info->bytes_reserved)
  2972. reclaimed += reserved - space_info->bytes_reserved;
  2973. reserved = space_info->bytes_reserved;
  2974. spin_unlock(&space_info->lock);
  2975. if (reserved == 0 || reclaimed >= max_reclaim)
  2976. break;
  2977. if (trans && trans->transaction->blocked)
  2978. return -EAGAIN;
  2979. __set_current_state(TASK_INTERRUPTIBLE);
  2980. schedule_timeout(pause);
  2981. pause <<= 1;
  2982. if (pause > HZ / 10)
  2983. pause = HZ / 10;
  2984. }
  2985. return reclaimed >= to_reclaim;
  2986. }
  2987. /*
  2988. * Retries tells us how many times we've called reserve_metadata_bytes. The
  2989. * idea is if this is the first call (retries == 0) then we will add to our
  2990. * reserved count if we can't make the allocation in order to hold our place
  2991. * while we go and try and free up space. That way for retries > 1 we don't try
  2992. * and add space, we just check to see if the amount of unused space is >= the
  2993. * total space, meaning that our reservation is valid.
  2994. *
  2995. * However if we don't intend to retry this reservation, pass -1 as retries so
  2996. * that it short circuits this logic.
  2997. */
  2998. static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
  2999. struct btrfs_root *root,
  3000. struct btrfs_block_rsv *block_rsv,
  3001. u64 orig_bytes, int flush)
  3002. {
  3003. struct btrfs_space_info *space_info = block_rsv->space_info;
  3004. u64 unused;
  3005. u64 num_bytes = orig_bytes;
  3006. int retries = 0;
  3007. int ret = 0;
  3008. bool reserved = false;
  3009. bool committed = false;
  3010. again:
  3011. ret = -ENOSPC;
  3012. if (reserved)
  3013. num_bytes = 0;
  3014. spin_lock(&space_info->lock);
  3015. unused = space_info->bytes_used + space_info->bytes_reserved +
  3016. space_info->bytes_pinned + space_info->bytes_readonly +
  3017. space_info->bytes_may_use;
  3018. /*
  3019. * The idea here is that we've not already over-reserved the block group
  3020. * then we can go ahead and save our reservation first and then start
  3021. * flushing if we need to. Otherwise if we've already overcommitted
  3022. * lets start flushing stuff first and then come back and try to make
  3023. * our reservation.
  3024. */
  3025. if (unused <= space_info->total_bytes) {
  3026. unused -= space_info->total_bytes;
  3027. if (unused >= num_bytes) {
  3028. if (!reserved)
  3029. space_info->bytes_reserved += orig_bytes;
  3030. ret = 0;
  3031. } else {
  3032. /*
  3033. * Ok set num_bytes to orig_bytes since we aren't
  3034. * overocmmitted, this way we only try and reclaim what
  3035. * we need.
  3036. */
  3037. num_bytes = orig_bytes;
  3038. }
  3039. } else {
  3040. /*
  3041. * Ok we're over committed, set num_bytes to the overcommitted
  3042. * amount plus the amount of bytes that we need for this
  3043. * reservation.
  3044. */
  3045. num_bytes = unused - space_info->total_bytes +
  3046. (orig_bytes * (retries + 1));
  3047. }
  3048. /*
  3049. * Couldn't make our reservation, save our place so while we're trying
  3050. * to reclaim space we can actually use it instead of somebody else
  3051. * stealing it from us.
  3052. */
  3053. if (ret && !reserved) {
  3054. space_info->bytes_reserved += orig_bytes;
  3055. reserved = true;
  3056. }
  3057. spin_unlock(&space_info->lock);
  3058. if (!ret)
  3059. return 0;
  3060. if (!flush)
  3061. goto out;
  3062. /*
  3063. * We do synchronous shrinking since we don't actually unreserve
  3064. * metadata until after the IO is completed.
  3065. */
  3066. ret = shrink_delalloc(trans, root, num_bytes, 1);
  3067. if (ret > 0)
  3068. return 0;
  3069. else if (ret < 0)
  3070. goto out;
  3071. /*
  3072. * So if we were overcommitted it's possible that somebody else flushed
  3073. * out enough space and we simply didn't have enough space to reclaim,
  3074. * so go back around and try again.
  3075. */
  3076. if (retries < 2) {
  3077. retries++;
  3078. goto again;
  3079. }
  3080. spin_lock(&space_info->lock);
  3081. /*
  3082. * Not enough space to be reclaimed, don't bother committing the
  3083. * transaction.
  3084. */
  3085. if (space_info->bytes_pinned < orig_bytes)
  3086. ret = -ENOSPC;
  3087. spin_unlock(&space_info->lock);
  3088. if (ret)
  3089. goto out;
  3090. ret = -EAGAIN;
  3091. if (trans || committed)
  3092. goto out;
  3093. ret = -ENOSPC;
  3094. trans = btrfs_join_transaction(root, 1);
  3095. if (IS_ERR(trans))
  3096. goto out;
  3097. ret = btrfs_commit_transaction(trans, root);
  3098. if (!ret) {
  3099. trans = NULL;
  3100. committed = true;
  3101. goto again;
  3102. }
  3103. out:
  3104. if (reserved) {
  3105. spin_lock(&space_info->lock);
  3106. space_info->bytes_reserved -= orig_bytes;
  3107. spin_unlock(&space_info->lock);
  3108. }
  3109. return ret;
  3110. }
  3111. static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
  3112. struct btrfs_root *root)
  3113. {
  3114. struct btrfs_block_rsv *block_rsv;
  3115. if (root->ref_cows)
  3116. block_rsv = trans->block_rsv;
  3117. else
  3118. block_rsv = root->block_rsv;
  3119. if (!block_rsv)
  3120. block_rsv = &root->fs_info->empty_block_rsv;
  3121. return block_rsv;
  3122. }
  3123. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3124. u64 num_bytes)
  3125. {
  3126. int ret = -ENOSPC;
  3127. spin_lock(&block_rsv->lock);
  3128. if (block_rsv->reserved >= num_bytes) {
  3129. block_rsv->reserved -= num_bytes;
  3130. if (block_rsv->reserved < block_rsv->size)
  3131. block_rsv->full = 0;
  3132. ret = 0;
  3133. }
  3134. spin_unlock(&block_rsv->lock);
  3135. return ret;
  3136. }
  3137. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3138. u64 num_bytes, int update_size)
  3139. {
  3140. spin_lock(&block_rsv->lock);
  3141. block_rsv->reserved += num_bytes;
  3142. if (update_size)
  3143. block_rsv->size += num_bytes;
  3144. else if (block_rsv->reserved >= block_rsv->size)
  3145. block_rsv->full = 1;
  3146. spin_unlock(&block_rsv->lock);
  3147. }
  3148. void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
  3149. struct btrfs_block_rsv *dest, u64 num_bytes)
  3150. {
  3151. struct btrfs_space_info *space_info = block_rsv->space_info;
  3152. spin_lock(&block_rsv->lock);
  3153. if (num_bytes == (u64)-1)
  3154. num_bytes = block_rsv->size;
  3155. block_rsv->size -= num_bytes;
  3156. if (block_rsv->reserved >= block_rsv->size) {
  3157. num_bytes = block_rsv->reserved - block_rsv->size;
  3158. block_rsv->reserved = block_rsv->size;
  3159. block_rsv->full = 1;
  3160. } else {
  3161. num_bytes = 0;
  3162. }
  3163. spin_unlock(&block_rsv->lock);
  3164. if (num_bytes > 0) {
  3165. if (dest) {
  3166. block_rsv_add_bytes(dest, num_bytes, 0);
  3167. } else {
  3168. spin_lock(&space_info->lock);
  3169. space_info->bytes_reserved -= num_bytes;
  3170. spin_unlock(&space_info->lock);
  3171. }
  3172. }
  3173. }
  3174. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3175. struct btrfs_block_rsv *dst, u64 num_bytes)
  3176. {
  3177. int ret;
  3178. ret = block_rsv_use_bytes(src, num_bytes);
  3179. if (ret)
  3180. return ret;
  3181. block_rsv_add_bytes(dst, num_bytes, 1);
  3182. return 0;
  3183. }
  3184. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
  3185. {
  3186. memset(rsv, 0, sizeof(*rsv));
  3187. spin_lock_init(&rsv->lock);
  3188. atomic_set(&rsv->usage, 1);
  3189. rsv->priority = 6;
  3190. INIT_LIST_HEAD(&rsv->list);
  3191. }
  3192. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
  3193. {
  3194. struct btrfs_block_rsv *block_rsv;
  3195. struct btrfs_fs_info *fs_info = root->fs_info;
  3196. u64 alloc_target;
  3197. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3198. if (!block_rsv)
  3199. return NULL;
  3200. btrfs_init_block_rsv(block_rsv);
  3201. alloc_target = btrfs_get_alloc_profile(root, 0);
  3202. block_rsv->space_info = __find_space_info(fs_info,
  3203. BTRFS_BLOCK_GROUP_METADATA);
  3204. return block_rsv;
  3205. }
  3206. void btrfs_free_block_rsv(struct btrfs_root *root,
  3207. struct btrfs_block_rsv *rsv)
  3208. {
  3209. if (rsv && atomic_dec_and_test(&rsv->usage)) {
  3210. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3211. if (!rsv->durable)
  3212. kfree(rsv);
  3213. }
  3214. }
  3215. /*
  3216. * make the block_rsv struct be able to capture freed space.
  3217. * the captured space will re-add to the the block_rsv struct
  3218. * after transaction commit
  3219. */
  3220. void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
  3221. struct btrfs_block_rsv *block_rsv)
  3222. {
  3223. block_rsv->durable = 1;
  3224. mutex_lock(&fs_info->durable_block_rsv_mutex);
  3225. list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
  3226. mutex_unlock(&fs_info->durable_block_rsv_mutex);
  3227. }
  3228. int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
  3229. struct btrfs_root *root,
  3230. struct btrfs_block_rsv *block_rsv,
  3231. u64 num_bytes)
  3232. {
  3233. int ret;
  3234. if (num_bytes == 0)
  3235. return 0;
  3236. ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
  3237. if (!ret) {
  3238. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3239. return 0;
  3240. }
  3241. return ret;
  3242. }
  3243. int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
  3244. struct btrfs_root *root,
  3245. struct btrfs_block_rsv *block_rsv,
  3246. u64 min_reserved, int min_factor)
  3247. {
  3248. u64 num_bytes = 0;
  3249. int commit_trans = 0;
  3250. int ret = -ENOSPC;
  3251. if (!block_rsv)
  3252. return 0;
  3253. spin_lock(&block_rsv->lock);
  3254. if (min_factor > 0)
  3255. num_bytes = div_factor(block_rsv->size, min_factor);
  3256. if (min_reserved > num_bytes)
  3257. num_bytes = min_reserved;
  3258. if (block_rsv->reserved >= num_bytes) {
  3259. ret = 0;
  3260. } else {
  3261. num_bytes -= block_rsv->reserved;
  3262. if (block_rsv->durable &&
  3263. block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
  3264. commit_trans = 1;
  3265. }
  3266. spin_unlock(&block_rsv->lock);
  3267. if (!ret)
  3268. return 0;
  3269. if (block_rsv->refill_used) {
  3270. ret = reserve_metadata_bytes(trans, root, block_rsv,
  3271. num_bytes, 0);
  3272. if (!ret) {
  3273. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  3274. return 0;
  3275. }
  3276. }
  3277. if (commit_trans) {
  3278. if (trans)
  3279. return -EAGAIN;
  3280. trans = btrfs_join_transaction(root, 1);
  3281. BUG_ON(IS_ERR(trans));
  3282. ret = btrfs_commit_transaction(trans, root);
  3283. return 0;
  3284. }
  3285. WARN_ON(1);
  3286. printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
  3287. block_rsv->size, block_rsv->reserved,
  3288. block_rsv->freed[0], block_rsv->freed[1]);
  3289. return -ENOSPC;
  3290. }
  3291. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  3292. struct btrfs_block_rsv *dst_rsv,
  3293. u64 num_bytes)
  3294. {
  3295. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3296. }
  3297. void btrfs_block_rsv_release(struct btrfs_root *root,
  3298. struct btrfs_block_rsv *block_rsv,
  3299. u64 num_bytes)
  3300. {
  3301. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3302. if (global_rsv->full || global_rsv == block_rsv ||
  3303. block_rsv->space_info != global_rsv->space_info)
  3304. global_rsv = NULL;
  3305. block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
  3306. }
  3307. /*
  3308. * helper to calculate size of global block reservation.
  3309. * the desired value is sum of space used by extent tree,
  3310. * checksum tree and root tree
  3311. */
  3312. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  3313. {
  3314. struct btrfs_space_info *sinfo;
  3315. u64 num_bytes;
  3316. u64 meta_used;
  3317. u64 data_used;
  3318. int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
  3319. #if 0
  3320. /*
  3321. * per tree used space accounting can be inaccuracy, so we
  3322. * can't rely on it.
  3323. */
  3324. spin_lock(&fs_info->extent_root->accounting_lock);
  3325. num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
  3326. spin_unlock(&fs_info->extent_root->accounting_lock);
  3327. spin_lock(&fs_info->csum_root->accounting_lock);
  3328. num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
  3329. spin_unlock(&fs_info->csum_root->accounting_lock);
  3330. spin_lock(&fs_info->tree_root->accounting_lock);
  3331. num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
  3332. spin_unlock(&fs_info->tree_root->accounting_lock);
  3333. #endif
  3334. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  3335. spin_lock(&sinfo->lock);
  3336. data_used = sinfo->bytes_used;
  3337. spin_unlock(&sinfo->lock);
  3338. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  3339. spin_lock(&sinfo->lock);
  3340. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  3341. data_used = 0;
  3342. meta_used = sinfo->bytes_used;
  3343. spin_unlock(&sinfo->lock);
  3344. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  3345. csum_size * 2;
  3346. num_bytes += div64_u64(data_used + meta_used, 50);
  3347. if (num_bytes * 3 > meta_used)
  3348. num_bytes = div64_u64(meta_used, 3);
  3349. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  3350. }
  3351. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  3352. {
  3353. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  3354. struct btrfs_space_info *sinfo = block_rsv->space_info;
  3355. u64 num_bytes;
  3356. num_bytes = calc_global_metadata_size(fs_info);
  3357. spin_lock(&block_rsv->lock);
  3358. spin_lock(&sinfo->lock);
  3359. block_rsv->size = num_bytes;
  3360. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  3361. sinfo->bytes_reserved + sinfo->bytes_readonly +
  3362. sinfo->bytes_may_use;
  3363. if (sinfo->total_bytes > num_bytes) {
  3364. num_bytes = sinfo->total_bytes - num_bytes;
  3365. block_rsv->reserved += num_bytes;
  3366. sinfo->bytes_reserved += num_bytes;
  3367. }
  3368. if (block_rsv->reserved >= block_rsv->size) {
  3369. num_bytes = block_rsv->reserved - block_rsv->size;
  3370. sinfo->bytes_reserved -= num_bytes;
  3371. block_rsv->reserved = block_rsv->size;
  3372. block_rsv->full = 1;
  3373. }
  3374. #if 0
  3375. printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
  3376. block_rsv->size, block_rsv->reserved);
  3377. #endif
  3378. spin_unlock(&sinfo->lock);
  3379. spin_unlock(&block_rsv->lock);
  3380. }
  3381. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  3382. {
  3383. struct btrfs_space_info *space_info;
  3384. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3385. fs_info->chunk_block_rsv.space_info = space_info;
  3386. fs_info->chunk_block_rsv.priority = 10;
  3387. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  3388. fs_info->global_block_rsv.space_info = space_info;
  3389. fs_info->global_block_rsv.priority = 10;
  3390. fs_info->global_block_rsv.refill_used = 1;
  3391. fs_info->delalloc_block_rsv.space_info = space_info;
  3392. fs_info->trans_block_rsv.space_info = space_info;
  3393. fs_info->empty_block_rsv.space_info = space_info;
  3394. fs_info->empty_block_rsv.priority = 10;
  3395. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  3396. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  3397. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  3398. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  3399. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  3400. btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
  3401. btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
  3402. update_global_block_rsv(fs_info);
  3403. }
  3404. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  3405. {
  3406. block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
  3407. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  3408. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  3409. WARN_ON(fs_info->trans_block_rsv.size > 0);
  3410. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  3411. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  3412. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  3413. }
  3414. static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
  3415. {
  3416. return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
  3417. 3 * num_items;
  3418. }
  3419. int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
  3420. struct btrfs_root *root,
  3421. int num_items)
  3422. {
  3423. u64 num_bytes;
  3424. int ret;
  3425. if (num_items == 0 || root->fs_info->chunk_root == root)
  3426. return 0;
  3427. num_bytes = calc_trans_metadata_size(root, num_items);
  3428. ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
  3429. num_bytes);
  3430. if (!ret) {
  3431. trans->bytes_reserved += num_bytes;
  3432. trans->block_rsv = &root->fs_info->trans_block_rsv;
  3433. }
  3434. return ret;
  3435. }
  3436. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  3437. struct btrfs_root *root)
  3438. {
  3439. if (!trans->bytes_reserved)
  3440. return;
  3441. BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
  3442. btrfs_block_rsv_release(root, trans->block_rsv,
  3443. trans->bytes_reserved);
  3444. trans->bytes_reserved = 0;
  3445. }
  3446. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  3447. struct inode *inode)
  3448. {
  3449. struct btrfs_root *root = BTRFS_I(inode)->root;
  3450. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  3451. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  3452. /*
  3453. * one for deleting orphan item, one for updating inode and
  3454. * two for calling btrfs_truncate_inode_items.
  3455. *
  3456. * btrfs_truncate_inode_items is a delete operation, it frees
  3457. * more space than it uses in most cases. So two units of
  3458. * metadata space should be enough for calling it many times.
  3459. * If all of the metadata space is used, we can commit
  3460. * transaction and use space it freed.
  3461. */
  3462. u64 num_bytes = calc_trans_metadata_size(root, 4);
  3463. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3464. }
  3465. void btrfs_orphan_release_metadata(struct inode *inode)
  3466. {
  3467. struct btrfs_root *root = BTRFS_I(inode)->root;
  3468. u64 num_bytes = calc_trans_metadata_size(root, 4);
  3469. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  3470. }
  3471. int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
  3472. struct btrfs_pending_snapshot *pending)
  3473. {
  3474. struct btrfs_root *root = pending->root;
  3475. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  3476. struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
  3477. /*
  3478. * two for root back/forward refs, two for directory entries
  3479. * and one for root of the snapshot.
  3480. */
  3481. u64 num_bytes = calc_trans_metadata_size(root, 5);
  3482. dst_rsv->space_info = src_rsv->space_info;
  3483. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3484. }
  3485. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
  3486. {
  3487. return num_bytes >>= 3;
  3488. }
  3489. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  3490. {
  3491. struct btrfs_root *root = BTRFS_I(inode)->root;
  3492. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  3493. u64 to_reserve;
  3494. int nr_extents;
  3495. int ret;
  3496. if (btrfs_transaction_in_commit(root->fs_info))
  3497. schedule_timeout(1);
  3498. num_bytes = ALIGN(num_bytes, root->sectorsize);
  3499. spin_lock(&BTRFS_I(inode)->accounting_lock);
  3500. nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
  3501. if (nr_extents > BTRFS_I(inode)->reserved_extents) {
  3502. nr_extents -= BTRFS_I(inode)->reserved_extents;
  3503. to_reserve = calc_trans_metadata_size(root, nr_extents);
  3504. } else {
  3505. nr_extents = 0;
  3506. to_reserve = 0;
  3507. }
  3508. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  3509. to_reserve += calc_csum_metadata_size(inode, num_bytes);
  3510. ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
  3511. if (ret)
  3512. return ret;
  3513. spin_lock(&BTRFS_I(inode)->accounting_lock);
  3514. BTRFS_I(inode)->reserved_extents += nr_extents;
  3515. atomic_inc(&BTRFS_I(inode)->outstanding_extents);
  3516. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  3517. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  3518. if (block_rsv->size > 512 * 1024 * 1024)
  3519. shrink_delalloc(NULL, root, to_reserve, 0);
  3520. return 0;
  3521. }
  3522. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  3523. {
  3524. struct btrfs_root *root = BTRFS_I(inode)->root;
  3525. u64 to_free;
  3526. int nr_extents;
  3527. num_bytes = ALIGN(num_bytes, root->sectorsize);
  3528. atomic_dec(&BTRFS_I(inode)->outstanding_extents);
  3529. spin_lock(&BTRFS_I(inode)->accounting_lock);
  3530. nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
  3531. if (nr_extents < BTRFS_I(inode)->reserved_extents) {
  3532. nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents;
  3533. BTRFS_I(inode)->reserved_extents -= nr_extents;
  3534. } else {
  3535. nr_extents = 0;
  3536. }
  3537. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  3538. to_free = calc_csum_metadata_size(inode, num_bytes);
  3539. if (nr_extents > 0)
  3540. to_free += calc_trans_metadata_size(root, nr_extents);
  3541. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  3542. to_free);
  3543. }
  3544. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  3545. {
  3546. int ret;
  3547. ret = btrfs_check_data_free_space(inode, num_bytes);
  3548. if (ret)
  3549. return ret;
  3550. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  3551. if (ret) {
  3552. btrfs_free_reserved_data_space(inode, num_bytes);
  3553. return ret;
  3554. }
  3555. return 0;
  3556. }
  3557. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  3558. {
  3559. btrfs_delalloc_release_metadata(inode, num_bytes);
  3560. btrfs_free_reserved_data_space(inode, num_bytes);
  3561. }
  3562. static int update_block_group(struct btrfs_trans_handle *trans,
  3563. struct btrfs_root *root,
  3564. u64 bytenr, u64 num_bytes, int alloc)
  3565. {
  3566. struct btrfs_block_group_cache *cache = NULL;
  3567. struct btrfs_fs_info *info = root->fs_info;
  3568. u64 total = num_bytes;
  3569. u64 old_val;
  3570. u64 byte_in_group;
  3571. int factor;
  3572. /* block accounting for super block */
  3573. spin_lock(&info->delalloc_lock);
  3574. old_val = btrfs_super_bytes_used(&info->super_copy);
  3575. if (alloc)
  3576. old_val += num_bytes;
  3577. else
  3578. old_val -= num_bytes;
  3579. btrfs_set_super_bytes_used(&info->super_copy, old_val);
  3580. spin_unlock(&info->delalloc_lock);
  3581. while (total) {
  3582. cache = btrfs_lookup_block_group(info, bytenr);
  3583. if (!cache)
  3584. return -1;
  3585. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  3586. BTRFS_BLOCK_GROUP_RAID1 |
  3587. BTRFS_BLOCK_GROUP_RAID10))
  3588. factor = 2;
  3589. else
  3590. factor = 1;
  3591. /*
  3592. * If this block group has free space cache written out, we
  3593. * need to make sure to load it if we are removing space. This
  3594. * is because we need the unpinning stage to actually add the
  3595. * space back to the block group, otherwise we will leak space.
  3596. */
  3597. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  3598. cache_block_group(cache, trans, 1);
  3599. byte_in_group = bytenr - cache->key.objectid;
  3600. WARN_ON(byte_in_group > cache->key.offset);
  3601. spin_lock(&cache->space_info->lock);
  3602. spin_lock(&cache->lock);
  3603. if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
  3604. cache->disk_cache_state < BTRFS_DC_CLEAR)
  3605. cache->disk_cache_state = BTRFS_DC_CLEAR;
  3606. cache->dirty = 1;
  3607. old_val = btrfs_block_group_used(&cache->item);
  3608. num_bytes = min(total, cache->key.offset - byte_in_group);
  3609. if (alloc) {
  3610. old_val += num_bytes;
  3611. btrfs_set_block_group_used(&cache->item, old_val);
  3612. cache->reserved -= num_bytes;
  3613. cache->space_info->bytes_reserved -= num_bytes;
  3614. cache->space_info->bytes_used += num_bytes;
  3615. cache->space_info->disk_used += num_bytes * factor;
  3616. spin_unlock(&cache->lock);
  3617. spin_unlock(&cache->space_info->lock);
  3618. } else {
  3619. old_val -= num_bytes;
  3620. btrfs_set_block_group_used(&cache->item, old_val);
  3621. cache->pinned += num_bytes;
  3622. cache->space_info->bytes_pinned += num_bytes;
  3623. cache->space_info->bytes_used -= num_bytes;
  3624. cache->space_info->disk_used -= num_bytes * factor;
  3625. spin_unlock(&cache->lock);
  3626. spin_unlock(&cache->space_info->lock);
  3627. set_extent_dirty(info->pinned_extents,
  3628. bytenr, bytenr + num_bytes - 1,
  3629. GFP_NOFS | __GFP_NOFAIL);
  3630. }
  3631. btrfs_put_block_group(cache);
  3632. total -= num_bytes;
  3633. bytenr += num_bytes;
  3634. }
  3635. return 0;
  3636. }
  3637. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  3638. {
  3639. struct btrfs_block_group_cache *cache;
  3640. u64 bytenr;
  3641. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  3642. if (!cache)
  3643. return 0;
  3644. bytenr = cache->key.objectid;
  3645. btrfs_put_block_group(cache);
  3646. return bytenr;
  3647. }
  3648. static int pin_down_extent(struct btrfs_root *root,
  3649. struct btrfs_block_group_cache *cache,
  3650. u64 bytenr, u64 num_bytes, int reserved)
  3651. {
  3652. spin_lock(&cache->space_info->lock);
  3653. spin_lock(&cache->lock);
  3654. cache->pinned += num_bytes;
  3655. cache->space_info->bytes_pinned += num_bytes;
  3656. if (reserved) {
  3657. cache->reserved -= num_bytes;
  3658. cache->space_info->bytes_reserved -= num_bytes;
  3659. }
  3660. spin_unlock(&cache->lock);
  3661. spin_unlock(&cache->space_info->lock);
  3662. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  3663. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  3664. return 0;
  3665. }
  3666. /*
  3667. * this function must be called within transaction
  3668. */
  3669. int btrfs_pin_extent(struct btrfs_root *root,
  3670. u64 bytenr, u64 num_bytes, int reserved)
  3671. {
  3672. struct btrfs_block_group_cache *cache;
  3673. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  3674. BUG_ON(!cache);
  3675. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  3676. btrfs_put_block_group(cache);
  3677. return 0;
  3678. }
  3679. /*
  3680. * update size of reserved extents. this function may return -EAGAIN
  3681. * if 'reserve' is true or 'sinfo' is false.
  3682. */
  3683. static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
  3684. u64 num_bytes, int reserve, int sinfo)
  3685. {
  3686. int ret = 0;
  3687. if (sinfo) {
  3688. struct btrfs_space_info *space_info = cache->space_info;
  3689. spin_lock(&space_info->lock);
  3690. spin_lock(&cache->lock);
  3691. if (reserve) {
  3692. if (cache->ro) {
  3693. ret = -EAGAIN;
  3694. } else {
  3695. cache->reserved += num_bytes;
  3696. space_info->bytes_reserved += num_bytes;
  3697. }
  3698. } else {
  3699. if (cache->ro)
  3700. space_info->bytes_readonly += num_bytes;
  3701. cache->reserved -= num_bytes;
  3702. space_info->bytes_reserved -= num_bytes;
  3703. }
  3704. spin_unlock(&cache->lock);
  3705. spin_unlock(&space_info->lock);
  3706. } else {
  3707. spin_lock(&cache->lock);
  3708. if (cache->ro) {
  3709. ret = -EAGAIN;
  3710. } else {
  3711. if (reserve)
  3712. cache->reserved += num_bytes;
  3713. else
  3714. cache->reserved -= num_bytes;
  3715. }
  3716. spin_unlock(&cache->lock);
  3717. }
  3718. return ret;
  3719. }
  3720. int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  3721. struct btrfs_root *root)
  3722. {
  3723. struct btrfs_fs_info *fs_info = root->fs_info;
  3724. struct btrfs_caching_control *next;
  3725. struct btrfs_caching_control *caching_ctl;
  3726. struct btrfs_block_group_cache *cache;
  3727. down_write(&fs_info->extent_commit_sem);
  3728. list_for_each_entry_safe(caching_ctl, next,
  3729. &fs_info->caching_block_groups, list) {
  3730. cache = caching_ctl->block_group;
  3731. if (block_group_cache_done(cache)) {
  3732. cache->last_byte_to_unpin = (u64)-1;
  3733. list_del_init(&caching_ctl->list);
  3734. put_caching_control(caching_ctl);
  3735. } else {
  3736. cache->last_byte_to_unpin = caching_ctl->progress;
  3737. }
  3738. }
  3739. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  3740. fs_info->pinned_extents = &fs_info->freed_extents[1];
  3741. else
  3742. fs_info->pinned_extents = &fs_info->freed_extents[0];
  3743. up_write(&fs_info->extent_commit_sem);
  3744. update_global_block_rsv(fs_info);
  3745. return 0;
  3746. }
  3747. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  3748. {
  3749. struct btrfs_fs_info *fs_info = root->fs_info;
  3750. struct btrfs_block_group_cache *cache = NULL;
  3751. u64 len;
  3752. while (start <= end) {
  3753. if (!cache ||
  3754. start >= cache->key.objectid + cache->key.offset) {
  3755. if (cache)
  3756. btrfs_put_block_group(cache);
  3757. cache = btrfs_lookup_block_group(fs_info, start);
  3758. BUG_ON(!cache);
  3759. }
  3760. len = cache->key.objectid + cache->key.offset - start;
  3761. len = min(len, end + 1 - start);
  3762. if (start < cache->last_byte_to_unpin) {
  3763. len = min(len, cache->last_byte_to_unpin - start);
  3764. btrfs_add_free_space(cache, start, len);
  3765. }
  3766. start += len;
  3767. spin_lock(&cache->space_info->lock);
  3768. spin_lock(&cache->lock);
  3769. cache->pinned -= len;
  3770. cache->space_info->bytes_pinned -= len;
  3771. if (cache->ro) {
  3772. cache->space_info->bytes_readonly += len;
  3773. } else if (cache->reserved_pinned > 0) {
  3774. len = min(len, cache->reserved_pinned);
  3775. cache->reserved_pinned -= len;
  3776. cache->space_info->bytes_reserved += len;
  3777. }
  3778. spin_unlock(&cache->lock);
  3779. spin_unlock(&cache->space_info->lock);
  3780. }
  3781. if (cache)
  3782. btrfs_put_block_group(cache);
  3783. return 0;
  3784. }
  3785. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  3786. struct btrfs_root *root)
  3787. {
  3788. struct btrfs_fs_info *fs_info = root->fs_info;
  3789. struct extent_io_tree *unpin;
  3790. struct btrfs_block_rsv *block_rsv;
  3791. struct btrfs_block_rsv *next_rsv;
  3792. u64 start;
  3793. u64 end;
  3794. int idx;
  3795. int ret;
  3796. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  3797. unpin = &fs_info->freed_extents[1];
  3798. else
  3799. unpin = &fs_info->freed_extents[0];
  3800. while (1) {
  3801. ret = find_first_extent_bit(unpin, 0, &start, &end,
  3802. EXTENT_DIRTY);
  3803. if (ret)
  3804. break;
  3805. ret = btrfs_discard_extent(root, start, end + 1 - start);
  3806. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  3807. unpin_extent_range(root, start, end);
  3808. cond_resched();
  3809. }
  3810. mutex_lock(&fs_info->durable_block_rsv_mutex);
  3811. list_for_each_entry_safe(block_rsv, next_rsv,
  3812. &fs_info->durable_block_rsv_list, list) {
  3813. idx = trans->transid & 0x1;
  3814. if (block_rsv->freed[idx] > 0) {
  3815. block_rsv_add_bytes(block_rsv,
  3816. block_rsv->freed[idx], 0);
  3817. block_rsv->freed[idx] = 0;
  3818. }
  3819. if (atomic_read(&block_rsv->usage) == 0) {
  3820. btrfs_block_rsv_release(root, block_rsv, (u64)-1);
  3821. if (block_rsv->freed[0] == 0 &&
  3822. block_rsv->freed[1] == 0) {
  3823. list_del_init(&block_rsv->list);
  3824. kfree(block_rsv);
  3825. }
  3826. } else {
  3827. btrfs_block_rsv_release(root, block_rsv, 0);
  3828. }
  3829. }
  3830. mutex_unlock(&fs_info->durable_block_rsv_mutex);
  3831. return 0;
  3832. }
  3833. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  3834. struct btrfs_root *root,
  3835. u64 bytenr, u64 num_bytes, u64 parent,
  3836. u64 root_objectid, u64 owner_objectid,
  3837. u64 owner_offset, int refs_to_drop,
  3838. struct btrfs_delayed_extent_op *extent_op)
  3839. {
  3840. struct btrfs_key key;
  3841. struct btrfs_path *path;
  3842. struct btrfs_fs_info *info = root->fs_info;
  3843. struct btrfs_root *extent_root = info->extent_root;
  3844. struct extent_buffer *leaf;
  3845. struct btrfs_extent_item *ei;
  3846. struct btrfs_extent_inline_ref *iref;
  3847. int ret;
  3848. int is_data;
  3849. int extent_slot = 0;
  3850. int found_extent = 0;
  3851. int num_to_del = 1;
  3852. u32 item_size;
  3853. u64 refs;
  3854. path = btrfs_alloc_path();
  3855. if (!path)
  3856. return -ENOMEM;
  3857. path->reada = 1;
  3858. path->leave_spinning = 1;
  3859. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  3860. BUG_ON(!is_data && refs_to_drop != 1);
  3861. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  3862. bytenr, num_bytes, parent,
  3863. root_objectid, owner_objectid,
  3864. owner_offset);
  3865. if (ret == 0) {
  3866. extent_slot = path->slots[0];
  3867. while (extent_slot >= 0) {
  3868. btrfs_item_key_to_cpu(path->nodes[0], &key,
  3869. extent_slot);
  3870. if (key.objectid != bytenr)
  3871. break;
  3872. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  3873. key.offset == num_bytes) {
  3874. found_extent = 1;
  3875. break;
  3876. }
  3877. if (path->slots[0] - extent_slot > 5)
  3878. break;
  3879. extent_slot--;
  3880. }
  3881. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  3882. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  3883. if (found_extent && item_size < sizeof(*ei))
  3884. found_extent = 0;
  3885. #endif
  3886. if (!found_extent) {
  3887. BUG_ON(iref);
  3888. ret = remove_extent_backref(trans, extent_root, path,
  3889. NULL, refs_to_drop,
  3890. is_data);
  3891. BUG_ON(ret);
  3892. btrfs_release_path(extent_root, path);
  3893. path->leave_spinning = 1;
  3894. key.objectid = bytenr;
  3895. key.type = BTRFS_EXTENT_ITEM_KEY;
  3896. key.offset = num_bytes;
  3897. ret = btrfs_search_slot(trans, extent_root,
  3898. &key, path, -1, 1);
  3899. if (ret) {
  3900. printk(KERN_ERR "umm, got %d back from search"
  3901. ", was looking for %llu\n", ret,
  3902. (unsigned long long)bytenr);
  3903. btrfs_print_leaf(extent_root, path->nodes[0]);
  3904. }
  3905. BUG_ON(ret);
  3906. extent_slot = path->slots[0];
  3907. }
  3908. } else {
  3909. btrfs_print_leaf(extent_root, path->nodes[0]);
  3910. WARN_ON(1);
  3911. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  3912. "parent %llu root %llu owner %llu offset %llu\n",
  3913. (unsigned long long)bytenr,
  3914. (unsigned long long)parent,
  3915. (unsigned long long)root_objectid,
  3916. (unsigned long long)owner_objectid,
  3917. (unsigned long long)owner_offset);
  3918. }
  3919. leaf = path->nodes[0];
  3920. item_size = btrfs_item_size_nr(leaf, extent_slot);
  3921. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  3922. if (item_size < sizeof(*ei)) {
  3923. BUG_ON(found_extent || extent_slot != path->slots[0]);
  3924. ret = convert_extent_item_v0(trans, extent_root, path,
  3925. owner_objectid, 0);
  3926. BUG_ON(ret < 0);
  3927. btrfs_release_path(extent_root, path);
  3928. path->leave_spinning = 1;
  3929. key.objectid = bytenr;
  3930. key.type = BTRFS_EXTENT_ITEM_KEY;
  3931. key.offset = num_bytes;
  3932. ret = btrfs_search_slot(trans, extent_root, &key, path,
  3933. -1, 1);
  3934. if (ret) {
  3935. printk(KERN_ERR "umm, got %d back from search"
  3936. ", was looking for %llu\n", ret,
  3937. (unsigned long long)bytenr);
  3938. btrfs_print_leaf(extent_root, path->nodes[0]);
  3939. }
  3940. BUG_ON(ret);
  3941. extent_slot = path->slots[0];
  3942. leaf = path->nodes[0];
  3943. item_size = btrfs_item_size_nr(leaf, extent_slot);
  3944. }
  3945. #endif
  3946. BUG_ON(item_size < sizeof(*ei));
  3947. ei = btrfs_item_ptr(leaf, extent_slot,
  3948. struct btrfs_extent_item);
  3949. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  3950. struct btrfs_tree_block_info *bi;
  3951. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  3952. bi = (struct btrfs_tree_block_info *)(ei + 1);
  3953. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  3954. }
  3955. refs = btrfs_extent_refs(leaf, ei);
  3956. BUG_ON(refs < refs_to_drop);
  3957. refs -= refs_to_drop;
  3958. if (refs > 0) {
  3959. if (extent_op)
  3960. __run_delayed_extent_op(extent_op, leaf, ei);
  3961. /*
  3962. * In the case of inline back ref, reference count will
  3963. * be updated by remove_extent_backref
  3964. */
  3965. if (iref) {
  3966. BUG_ON(!found_extent);
  3967. } else {
  3968. btrfs_set_extent_refs(leaf, ei, refs);
  3969. btrfs_mark_buffer_dirty(leaf);
  3970. }
  3971. if (found_extent) {
  3972. ret = remove_extent_backref(trans, extent_root, path,
  3973. iref, refs_to_drop,
  3974. is_data);
  3975. BUG_ON(ret);
  3976. }
  3977. } else {
  3978. if (found_extent) {
  3979. BUG_ON(is_data && refs_to_drop !=
  3980. extent_data_ref_count(root, path, iref));
  3981. if (iref) {
  3982. BUG_ON(path->slots[0] != extent_slot);
  3983. } else {
  3984. BUG_ON(path->slots[0] != extent_slot + 1);
  3985. path->slots[0] = extent_slot;
  3986. num_to_del = 2;
  3987. }
  3988. }
  3989. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  3990. num_to_del);
  3991. BUG_ON(ret);
  3992. btrfs_release_path(extent_root, path);
  3993. if (is_data) {
  3994. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  3995. BUG_ON(ret);
  3996. } else {
  3997. invalidate_mapping_pages(info->btree_inode->i_mapping,
  3998. bytenr >> PAGE_CACHE_SHIFT,
  3999. (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
  4000. }
  4001. ret = update_block_group(trans, root, bytenr, num_bytes, 0);
  4002. BUG_ON(ret);
  4003. }
  4004. btrfs_free_path(path);
  4005. return ret;
  4006. }
  4007. /*
  4008. * when we free an block, it is possible (and likely) that we free the last
  4009. * delayed ref for that extent as well. This searches the delayed ref tree for
  4010. * a given extent, and if there are no other delayed refs to be processed, it
  4011. * removes it from the tree.
  4012. */
  4013. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  4014. struct btrfs_root *root, u64 bytenr)
  4015. {
  4016. struct btrfs_delayed_ref_head *head;
  4017. struct btrfs_delayed_ref_root *delayed_refs;
  4018. struct btrfs_delayed_ref_node *ref;
  4019. struct rb_node *node;
  4020. int ret = 0;
  4021. delayed_refs = &trans->transaction->delayed_refs;
  4022. spin_lock(&delayed_refs->lock);
  4023. head = btrfs_find_delayed_ref_head(trans, bytenr);
  4024. if (!head)
  4025. goto out;
  4026. node = rb_prev(&head->node.rb_node);
  4027. if (!node)
  4028. goto out;
  4029. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  4030. /* there are still entries for this ref, we can't drop it */
  4031. if (ref->bytenr == bytenr)
  4032. goto out;
  4033. if (head->extent_op) {
  4034. if (!head->must_insert_reserved)
  4035. goto out;
  4036. kfree(head->extent_op);
  4037. head->extent_op = NULL;
  4038. }
  4039. /*
  4040. * waiting for the lock here would deadlock. If someone else has it
  4041. * locked they are already in the process of dropping it anyway
  4042. */
  4043. if (!mutex_trylock(&head->mutex))
  4044. goto out;
  4045. /*
  4046. * at this point we have a head with no other entries. Go
  4047. * ahead and process it.
  4048. */
  4049. head->node.in_tree = 0;
  4050. rb_erase(&head->node.rb_node, &delayed_refs->root);
  4051. delayed_refs->num_entries--;
  4052. /*
  4053. * we don't take a ref on the node because we're removing it from the
  4054. * tree, so we just steal the ref the tree was holding.
  4055. */
  4056. delayed_refs->num_heads--;
  4057. if (list_empty(&head->cluster))
  4058. delayed_refs->num_heads_ready--;
  4059. list_del_init(&head->cluster);
  4060. spin_unlock(&delayed_refs->lock);
  4061. BUG_ON(head->extent_op);
  4062. if (head->must_insert_reserved)
  4063. ret = 1;
  4064. mutex_unlock(&head->mutex);
  4065. btrfs_put_delayed_ref(&head->node);
  4066. return ret;
  4067. out:
  4068. spin_unlock(&delayed_refs->lock);
  4069. return 0;
  4070. }
  4071. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  4072. struct btrfs_root *root,
  4073. struct extent_buffer *buf,
  4074. u64 parent, int last_ref)
  4075. {
  4076. struct btrfs_block_rsv *block_rsv;
  4077. struct btrfs_block_group_cache *cache = NULL;
  4078. int ret;
  4079. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4080. ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
  4081. parent, root->root_key.objectid,
  4082. btrfs_header_level(buf),
  4083. BTRFS_DROP_DELAYED_REF, NULL);
  4084. BUG_ON(ret);
  4085. }
  4086. if (!last_ref)
  4087. return;
  4088. block_rsv = get_block_rsv(trans, root);
  4089. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  4090. if (block_rsv->space_info != cache->space_info)
  4091. goto out;
  4092. if (btrfs_header_generation(buf) == trans->transid) {
  4093. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4094. ret = check_ref_cleanup(trans, root, buf->start);
  4095. if (!ret)
  4096. goto pin;
  4097. }
  4098. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  4099. pin_down_extent(root, cache, buf->start, buf->len, 1);
  4100. goto pin;
  4101. }
  4102. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  4103. btrfs_add_free_space(cache, buf->start, buf->len);
  4104. ret = update_reserved_bytes(cache, buf->len, 0, 0);
  4105. if (ret == -EAGAIN) {
  4106. /* block group became read-only */
  4107. update_reserved_bytes(cache, buf->len, 0, 1);
  4108. goto out;
  4109. }
  4110. ret = 1;
  4111. spin_lock(&block_rsv->lock);
  4112. if (block_rsv->reserved < block_rsv->size) {
  4113. block_rsv->reserved += buf->len;
  4114. ret = 0;
  4115. }
  4116. spin_unlock(&block_rsv->lock);
  4117. if (ret) {
  4118. spin_lock(&cache->space_info->lock);
  4119. cache->space_info->bytes_reserved -= buf->len;
  4120. spin_unlock(&cache->space_info->lock);
  4121. }
  4122. goto out;
  4123. }
  4124. pin:
  4125. if (block_rsv->durable && !cache->ro) {
  4126. ret = 0;
  4127. spin_lock(&cache->lock);
  4128. if (!cache->ro) {
  4129. cache->reserved_pinned += buf->len;
  4130. ret = 1;
  4131. }
  4132. spin_unlock(&cache->lock);
  4133. if (ret) {
  4134. spin_lock(&block_rsv->lock);
  4135. block_rsv->freed[trans->transid & 0x1] += buf->len;
  4136. spin_unlock(&block_rsv->lock);
  4137. }
  4138. }
  4139. out:
  4140. btrfs_put_block_group(cache);
  4141. }
  4142. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  4143. struct btrfs_root *root,
  4144. u64 bytenr, u64 num_bytes, u64 parent,
  4145. u64 root_objectid, u64 owner, u64 offset)
  4146. {
  4147. int ret;
  4148. /*
  4149. * tree log blocks never actually go into the extent allocation
  4150. * tree, just update pinning info and exit early.
  4151. */
  4152. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  4153. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  4154. /* unlocks the pinned mutex */
  4155. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  4156. ret = 0;
  4157. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4158. ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
  4159. parent, root_objectid, (int)owner,
  4160. BTRFS_DROP_DELAYED_REF, NULL);
  4161. BUG_ON(ret);
  4162. } else {
  4163. ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
  4164. parent, root_objectid, owner,
  4165. offset, BTRFS_DROP_DELAYED_REF, NULL);
  4166. BUG_ON(ret);
  4167. }
  4168. return ret;
  4169. }
  4170. static u64 stripe_align(struct btrfs_root *root, u64 val)
  4171. {
  4172. u64 mask = ((u64)root->stripesize - 1);
  4173. u64 ret = (val + mask) & ~mask;
  4174. return ret;
  4175. }
  4176. /*
  4177. * when we wait for progress in the block group caching, its because
  4178. * our allocation attempt failed at least once. So, we must sleep
  4179. * and let some progress happen before we try again.
  4180. *
  4181. * This function will sleep at least once waiting for new free space to
  4182. * show up, and then it will check the block group free space numbers
  4183. * for our min num_bytes. Another option is to have it go ahead
  4184. * and look in the rbtree for a free extent of a given size, but this
  4185. * is a good start.
  4186. */
  4187. static noinline int
  4188. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  4189. u64 num_bytes)
  4190. {
  4191. struct btrfs_caching_control *caching_ctl;
  4192. DEFINE_WAIT(wait);
  4193. caching_ctl = get_caching_control(cache);
  4194. if (!caching_ctl)
  4195. return 0;
  4196. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  4197. (cache->free_space >= num_bytes));
  4198. put_caching_control(caching_ctl);
  4199. return 0;
  4200. }
  4201. static noinline int
  4202. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  4203. {
  4204. struct btrfs_caching_control *caching_ctl;
  4205. DEFINE_WAIT(wait);
  4206. caching_ctl = get_caching_control(cache);
  4207. if (!caching_ctl)
  4208. return 0;
  4209. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  4210. put_caching_control(caching_ctl);
  4211. return 0;
  4212. }
  4213. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  4214. {
  4215. int index;
  4216. if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
  4217. index = 0;
  4218. else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
  4219. index = 1;
  4220. else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
  4221. index = 2;
  4222. else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
  4223. index = 3;
  4224. else
  4225. index = 4;
  4226. return index;
  4227. }
  4228. enum btrfs_loop_type {
  4229. LOOP_FIND_IDEAL = 0,
  4230. LOOP_CACHING_NOWAIT = 1,
  4231. LOOP_CACHING_WAIT = 2,
  4232. LOOP_ALLOC_CHUNK = 3,
  4233. LOOP_NO_EMPTY_SIZE = 4,
  4234. };
  4235. /*
  4236. * walks the btree of allocated extents and find a hole of a given size.
  4237. * The key ins is changed to record the hole:
  4238. * ins->objectid == block start
  4239. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  4240. * ins->offset == number of blocks
  4241. * Any available blocks before search_start are skipped.
  4242. */
  4243. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  4244. struct btrfs_root *orig_root,
  4245. u64 num_bytes, u64 empty_size,
  4246. u64 search_start, u64 search_end,
  4247. u64 hint_byte, struct btrfs_key *ins,
  4248. int data)
  4249. {
  4250. int ret = 0;
  4251. struct btrfs_root *root = orig_root->fs_info->extent_root;
  4252. struct btrfs_free_cluster *last_ptr = NULL;
  4253. struct btrfs_block_group_cache *block_group = NULL;
  4254. int empty_cluster = 2 * 1024 * 1024;
  4255. int allowed_chunk_alloc = 0;
  4256. int done_chunk_alloc = 0;
  4257. struct btrfs_space_info *space_info;
  4258. int last_ptr_loop = 0;
  4259. int loop = 0;
  4260. int index = 0;
  4261. bool found_uncached_bg = false;
  4262. bool failed_cluster_refill = false;
  4263. bool failed_alloc = false;
  4264. bool use_cluster = true;
  4265. u64 ideal_cache_percent = 0;
  4266. u64 ideal_cache_offset = 0;
  4267. WARN_ON(num_bytes < root->sectorsize);
  4268. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  4269. ins->objectid = 0;
  4270. ins->offset = 0;
  4271. space_info = __find_space_info(root->fs_info, data);
  4272. if (!space_info) {
  4273. printk(KERN_ERR "No space info for %d\n", data);
  4274. return -ENOSPC;
  4275. }
  4276. /*
  4277. * If the space info is for both data and metadata it means we have a
  4278. * small filesystem and we can't use the clustering stuff.
  4279. */
  4280. if (btrfs_mixed_space_info(space_info))
  4281. use_cluster = false;
  4282. if (orig_root->ref_cows || empty_size)
  4283. allowed_chunk_alloc = 1;
  4284. if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  4285. last_ptr = &root->fs_info->meta_alloc_cluster;
  4286. if (!btrfs_test_opt(root, SSD))
  4287. empty_cluster = 64 * 1024;
  4288. }
  4289. if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  4290. btrfs_test_opt(root, SSD)) {
  4291. last_ptr = &root->fs_info->data_alloc_cluster;
  4292. }
  4293. if (last_ptr) {
  4294. spin_lock(&last_ptr->lock);
  4295. if (last_ptr->block_group)
  4296. hint_byte = last_ptr->window_start;
  4297. spin_unlock(&last_ptr->lock);
  4298. }
  4299. search_start = max(search_start, first_logical_byte(root, 0));
  4300. search_start = max(search_start, hint_byte);
  4301. if (!last_ptr)
  4302. empty_cluster = 0;
  4303. if (search_start == hint_byte) {
  4304. ideal_cache:
  4305. block_group = btrfs_lookup_block_group(root->fs_info,
  4306. search_start);
  4307. /*
  4308. * we don't want to use the block group if it doesn't match our
  4309. * allocation bits, or if its not cached.
  4310. *
  4311. * However if we are re-searching with an ideal block group
  4312. * picked out then we don't care that the block group is cached.
  4313. */
  4314. if (block_group && block_group_bits(block_group, data) &&
  4315. (block_group->cached != BTRFS_CACHE_NO ||
  4316. search_start == ideal_cache_offset)) {
  4317. down_read(&space_info->groups_sem);
  4318. if (list_empty(&block_group->list) ||
  4319. block_group->ro) {
  4320. /*
  4321. * someone is removing this block group,
  4322. * we can't jump into the have_block_group
  4323. * target because our list pointers are not
  4324. * valid
  4325. */
  4326. btrfs_put_block_group(block_group);
  4327. up_read(&space_info->groups_sem);
  4328. } else {
  4329. index = get_block_group_index(block_group);
  4330. goto have_block_group;
  4331. }
  4332. } else if (block_group) {
  4333. btrfs_put_block_group(block_group);
  4334. }
  4335. }
  4336. search:
  4337. down_read(&space_info->groups_sem);
  4338. list_for_each_entry(block_group, &space_info->block_groups[index],
  4339. list) {
  4340. u64 offset;
  4341. int cached;
  4342. btrfs_get_block_group(block_group);
  4343. search_start = block_group->key.objectid;
  4344. have_block_group:
  4345. if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
  4346. u64 free_percent;
  4347. ret = cache_block_group(block_group, trans, 1);
  4348. if (block_group->cached == BTRFS_CACHE_FINISHED)
  4349. goto have_block_group;
  4350. free_percent = btrfs_block_group_used(&block_group->item);
  4351. free_percent *= 100;
  4352. free_percent = div64_u64(free_percent,
  4353. block_group->key.offset);
  4354. free_percent = 100 - free_percent;
  4355. if (free_percent > ideal_cache_percent &&
  4356. likely(!block_group->ro)) {
  4357. ideal_cache_offset = block_group->key.objectid;
  4358. ideal_cache_percent = free_percent;
  4359. }
  4360. /*
  4361. * We only want to start kthread caching if we are at
  4362. * the point where we will wait for caching to make
  4363. * progress, or if our ideal search is over and we've
  4364. * found somebody to start caching.
  4365. */
  4366. if (loop > LOOP_CACHING_NOWAIT ||
  4367. (loop > LOOP_FIND_IDEAL &&
  4368. atomic_read(&space_info->caching_threads) < 2)) {
  4369. ret = cache_block_group(block_group, trans, 0);
  4370. BUG_ON(ret);
  4371. }
  4372. found_uncached_bg = true;
  4373. /*
  4374. * If loop is set for cached only, try the next block
  4375. * group.
  4376. */
  4377. if (loop == LOOP_FIND_IDEAL)
  4378. goto loop;
  4379. }
  4380. cached = block_group_cache_done(block_group);
  4381. if (unlikely(!cached))
  4382. found_uncached_bg = true;
  4383. if (unlikely(block_group->ro))
  4384. goto loop;
  4385. /*
  4386. * Ok we want to try and use the cluster allocator, so lets look
  4387. * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
  4388. * have tried the cluster allocator plenty of times at this
  4389. * point and not have found anything, so we are likely way too
  4390. * fragmented for the clustering stuff to find anything, so lets
  4391. * just skip it and let the allocator find whatever block it can
  4392. * find
  4393. */
  4394. if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
  4395. /*
  4396. * the refill lock keeps out other
  4397. * people trying to start a new cluster
  4398. */
  4399. spin_lock(&last_ptr->refill_lock);
  4400. if (last_ptr->block_group &&
  4401. (last_ptr->block_group->ro ||
  4402. !block_group_bits(last_ptr->block_group, data))) {
  4403. offset = 0;
  4404. goto refill_cluster;
  4405. }
  4406. offset = btrfs_alloc_from_cluster(block_group, last_ptr,
  4407. num_bytes, search_start);
  4408. if (offset) {
  4409. /* we have a block, we're done */
  4410. spin_unlock(&last_ptr->refill_lock);
  4411. goto checks;
  4412. }
  4413. spin_lock(&last_ptr->lock);
  4414. /*
  4415. * whoops, this cluster doesn't actually point to
  4416. * this block group. Get a ref on the block
  4417. * group is does point to and try again
  4418. */
  4419. if (!last_ptr_loop && last_ptr->block_group &&
  4420. last_ptr->block_group != block_group) {
  4421. btrfs_put_block_group(block_group);
  4422. block_group = last_ptr->block_group;
  4423. btrfs_get_block_group(block_group);
  4424. spin_unlock(&last_ptr->lock);
  4425. spin_unlock(&last_ptr->refill_lock);
  4426. last_ptr_loop = 1;
  4427. search_start = block_group->key.objectid;
  4428. /*
  4429. * we know this block group is properly
  4430. * in the list because
  4431. * btrfs_remove_block_group, drops the
  4432. * cluster before it removes the block
  4433. * group from the list
  4434. */
  4435. goto have_block_group;
  4436. }
  4437. spin_unlock(&last_ptr->lock);
  4438. refill_cluster:
  4439. /*
  4440. * this cluster didn't work out, free it and
  4441. * start over
  4442. */
  4443. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  4444. last_ptr_loop = 0;
  4445. /* allocate a cluster in this block group */
  4446. ret = btrfs_find_space_cluster(trans, root,
  4447. block_group, last_ptr,
  4448. offset, num_bytes,
  4449. empty_cluster + empty_size);
  4450. if (ret == 0) {
  4451. /*
  4452. * now pull our allocation out of this
  4453. * cluster
  4454. */
  4455. offset = btrfs_alloc_from_cluster(block_group,
  4456. last_ptr, num_bytes,
  4457. search_start);
  4458. if (offset) {
  4459. /* we found one, proceed */
  4460. spin_unlock(&last_ptr->refill_lock);
  4461. goto checks;
  4462. }
  4463. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  4464. && !failed_cluster_refill) {
  4465. spin_unlock(&last_ptr->refill_lock);
  4466. failed_cluster_refill = true;
  4467. wait_block_group_cache_progress(block_group,
  4468. num_bytes + empty_cluster + empty_size);
  4469. goto have_block_group;
  4470. }
  4471. /*
  4472. * at this point we either didn't find a cluster
  4473. * or we weren't able to allocate a block from our
  4474. * cluster. Free the cluster we've been trying
  4475. * to use, and go to the next block group
  4476. */
  4477. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  4478. spin_unlock(&last_ptr->refill_lock);
  4479. goto loop;
  4480. }
  4481. offset = btrfs_find_space_for_alloc(block_group, search_start,
  4482. num_bytes, empty_size);
  4483. /*
  4484. * If we didn't find a chunk, and we haven't failed on this
  4485. * block group before, and this block group is in the middle of
  4486. * caching and we are ok with waiting, then go ahead and wait
  4487. * for progress to be made, and set failed_alloc to true.
  4488. *
  4489. * If failed_alloc is true then we've already waited on this
  4490. * block group once and should move on to the next block group.
  4491. */
  4492. if (!offset && !failed_alloc && !cached &&
  4493. loop > LOOP_CACHING_NOWAIT) {
  4494. wait_block_group_cache_progress(block_group,
  4495. num_bytes + empty_size);
  4496. failed_alloc = true;
  4497. goto have_block_group;
  4498. } else if (!offset) {
  4499. goto loop;
  4500. }
  4501. checks:
  4502. search_start = stripe_align(root, offset);
  4503. /* move on to the next group */
  4504. if (search_start + num_bytes >= search_end) {
  4505. btrfs_add_free_space(block_group, offset, num_bytes);
  4506. goto loop;
  4507. }
  4508. /* move on to the next group */
  4509. if (search_start + num_bytes >
  4510. block_group->key.objectid + block_group->key.offset) {
  4511. btrfs_add_free_space(block_group, offset, num_bytes);
  4512. goto loop;
  4513. }
  4514. ins->objectid = search_start;
  4515. ins->offset = num_bytes;
  4516. if (offset < search_start)
  4517. btrfs_add_free_space(block_group, offset,
  4518. search_start - offset);
  4519. BUG_ON(offset > search_start);
  4520. ret = update_reserved_bytes(block_group, num_bytes, 1,
  4521. (data & BTRFS_BLOCK_GROUP_DATA));
  4522. if (ret == -EAGAIN) {
  4523. btrfs_add_free_space(block_group, offset, num_bytes);
  4524. goto loop;
  4525. }
  4526. /* we are all good, lets return */
  4527. ins->objectid = search_start;
  4528. ins->offset = num_bytes;
  4529. if (offset < search_start)
  4530. btrfs_add_free_space(block_group, offset,
  4531. search_start - offset);
  4532. BUG_ON(offset > search_start);
  4533. break;
  4534. loop:
  4535. failed_cluster_refill = false;
  4536. failed_alloc = false;
  4537. BUG_ON(index != get_block_group_index(block_group));
  4538. btrfs_put_block_group(block_group);
  4539. }
  4540. up_read(&space_info->groups_sem);
  4541. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  4542. goto search;
  4543. /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
  4544. * for them to make caching progress. Also
  4545. * determine the best possible bg to cache
  4546. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  4547. * caching kthreads as we move along
  4548. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  4549. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  4550. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  4551. * again
  4552. */
  4553. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
  4554. (found_uncached_bg || empty_size || empty_cluster ||
  4555. allowed_chunk_alloc)) {
  4556. index = 0;
  4557. if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
  4558. found_uncached_bg = false;
  4559. loop++;
  4560. if (!ideal_cache_percent &&
  4561. atomic_read(&space_info->caching_threads))
  4562. goto search;
  4563. /*
  4564. * 1 of the following 2 things have happened so far
  4565. *
  4566. * 1) We found an ideal block group for caching that
  4567. * is mostly full and will cache quickly, so we might
  4568. * as well wait for it.
  4569. *
  4570. * 2) We searched for cached only and we didn't find
  4571. * anything, and we didn't start any caching kthreads
  4572. * either, so chances are we will loop through and
  4573. * start a couple caching kthreads, and then come back
  4574. * around and just wait for them. This will be slower
  4575. * because we will have 2 caching kthreads reading at
  4576. * the same time when we could have just started one
  4577. * and waited for it to get far enough to give us an
  4578. * allocation, so go ahead and go to the wait caching
  4579. * loop.
  4580. */
  4581. loop = LOOP_CACHING_WAIT;
  4582. search_start = ideal_cache_offset;
  4583. ideal_cache_percent = 0;
  4584. goto ideal_cache;
  4585. } else if (loop == LOOP_FIND_IDEAL) {
  4586. /*
  4587. * Didn't find a uncached bg, wait on anything we find
  4588. * next.
  4589. */
  4590. loop = LOOP_CACHING_WAIT;
  4591. goto search;
  4592. }
  4593. if (loop < LOOP_CACHING_WAIT) {
  4594. loop++;
  4595. goto search;
  4596. }
  4597. if (loop == LOOP_ALLOC_CHUNK) {
  4598. empty_size = 0;
  4599. empty_cluster = 0;
  4600. }
  4601. if (allowed_chunk_alloc) {
  4602. ret = do_chunk_alloc(trans, root, num_bytes +
  4603. 2 * 1024 * 1024, data, 1);
  4604. allowed_chunk_alloc = 0;
  4605. done_chunk_alloc = 1;
  4606. } else if (!done_chunk_alloc) {
  4607. space_info->force_alloc = 1;
  4608. }
  4609. if (loop < LOOP_NO_EMPTY_SIZE) {
  4610. loop++;
  4611. goto search;
  4612. }
  4613. ret = -ENOSPC;
  4614. } else if (!ins->objectid) {
  4615. ret = -ENOSPC;
  4616. }
  4617. /* we found what we needed */
  4618. if (ins->objectid) {
  4619. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  4620. trans->block_group = block_group->key.objectid;
  4621. btrfs_put_block_group(block_group);
  4622. ret = 0;
  4623. }
  4624. return ret;
  4625. }
  4626. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  4627. int dump_block_groups)
  4628. {
  4629. struct btrfs_block_group_cache *cache;
  4630. int index = 0;
  4631. spin_lock(&info->lock);
  4632. printk(KERN_INFO "space_info has %llu free, is %sfull\n",
  4633. (unsigned long long)(info->total_bytes - info->bytes_used -
  4634. info->bytes_pinned - info->bytes_reserved -
  4635. info->bytes_readonly),
  4636. (info->full) ? "" : "not ");
  4637. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  4638. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  4639. (unsigned long long)info->total_bytes,
  4640. (unsigned long long)info->bytes_used,
  4641. (unsigned long long)info->bytes_pinned,
  4642. (unsigned long long)info->bytes_reserved,
  4643. (unsigned long long)info->bytes_may_use,
  4644. (unsigned long long)info->bytes_readonly);
  4645. spin_unlock(&info->lock);
  4646. if (!dump_block_groups)
  4647. return;
  4648. down_read(&info->groups_sem);
  4649. again:
  4650. list_for_each_entry(cache, &info->block_groups[index], list) {
  4651. spin_lock(&cache->lock);
  4652. printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
  4653. "%llu pinned %llu reserved\n",
  4654. (unsigned long long)cache->key.objectid,
  4655. (unsigned long long)cache->key.offset,
  4656. (unsigned long long)btrfs_block_group_used(&cache->item),
  4657. (unsigned long long)cache->pinned,
  4658. (unsigned long long)cache->reserved);
  4659. btrfs_dump_free_space(cache, bytes);
  4660. spin_unlock(&cache->lock);
  4661. }
  4662. if (++index < BTRFS_NR_RAID_TYPES)
  4663. goto again;
  4664. up_read(&info->groups_sem);
  4665. }
  4666. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  4667. struct btrfs_root *root,
  4668. u64 num_bytes, u64 min_alloc_size,
  4669. u64 empty_size, u64 hint_byte,
  4670. u64 search_end, struct btrfs_key *ins,
  4671. u64 data)
  4672. {
  4673. int ret;
  4674. u64 search_start = 0;
  4675. data = btrfs_get_alloc_profile(root, data);
  4676. again:
  4677. /*
  4678. * the only place that sets empty_size is btrfs_realloc_node, which
  4679. * is not called recursively on allocations
  4680. */
  4681. if (empty_size || root->ref_cows)
  4682. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  4683. num_bytes + 2 * 1024 * 1024, data, 0);
  4684. WARN_ON(num_bytes < root->sectorsize);
  4685. ret = find_free_extent(trans, root, num_bytes, empty_size,
  4686. search_start, search_end, hint_byte,
  4687. ins, data);
  4688. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  4689. num_bytes = num_bytes >> 1;
  4690. num_bytes = num_bytes & ~(root->sectorsize - 1);
  4691. num_bytes = max(num_bytes, min_alloc_size);
  4692. do_chunk_alloc(trans, root->fs_info->extent_root,
  4693. num_bytes, data, 1);
  4694. goto again;
  4695. }
  4696. if (ret == -ENOSPC) {
  4697. struct btrfs_space_info *sinfo;
  4698. sinfo = __find_space_info(root->fs_info, data);
  4699. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  4700. "wanted %llu\n", (unsigned long long)data,
  4701. (unsigned long long)num_bytes);
  4702. dump_space_info(sinfo, num_bytes, 1);
  4703. }
  4704. return ret;
  4705. }
  4706. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  4707. {
  4708. struct btrfs_block_group_cache *cache;
  4709. int ret = 0;
  4710. cache = btrfs_lookup_block_group(root->fs_info, start);
  4711. if (!cache) {
  4712. printk(KERN_ERR "Unable to find block group for %llu\n",
  4713. (unsigned long long)start);
  4714. return -ENOSPC;
  4715. }
  4716. ret = btrfs_discard_extent(root, start, len);
  4717. btrfs_add_free_space(cache, start, len);
  4718. update_reserved_bytes(cache, len, 0, 1);
  4719. btrfs_put_block_group(cache);
  4720. return ret;
  4721. }
  4722. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  4723. struct btrfs_root *root,
  4724. u64 parent, u64 root_objectid,
  4725. u64 flags, u64 owner, u64 offset,
  4726. struct btrfs_key *ins, int ref_mod)
  4727. {
  4728. int ret;
  4729. struct btrfs_fs_info *fs_info = root->fs_info;
  4730. struct btrfs_extent_item *extent_item;
  4731. struct btrfs_extent_inline_ref *iref;
  4732. struct btrfs_path *path;
  4733. struct extent_buffer *leaf;
  4734. int type;
  4735. u32 size;
  4736. if (parent > 0)
  4737. type = BTRFS_SHARED_DATA_REF_KEY;
  4738. else
  4739. type = BTRFS_EXTENT_DATA_REF_KEY;
  4740. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  4741. path = btrfs_alloc_path();
  4742. BUG_ON(!path);
  4743. path->leave_spinning = 1;
  4744. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  4745. ins, size);
  4746. BUG_ON(ret);
  4747. leaf = path->nodes[0];
  4748. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  4749. struct btrfs_extent_item);
  4750. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  4751. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  4752. btrfs_set_extent_flags(leaf, extent_item,
  4753. flags | BTRFS_EXTENT_FLAG_DATA);
  4754. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  4755. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  4756. if (parent > 0) {
  4757. struct btrfs_shared_data_ref *ref;
  4758. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  4759. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  4760. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  4761. } else {
  4762. struct btrfs_extent_data_ref *ref;
  4763. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  4764. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  4765. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  4766. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  4767. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  4768. }
  4769. btrfs_mark_buffer_dirty(path->nodes[0]);
  4770. btrfs_free_path(path);
  4771. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  4772. if (ret) {
  4773. printk(KERN_ERR "btrfs update block group failed for %llu "
  4774. "%llu\n", (unsigned long long)ins->objectid,
  4775. (unsigned long long)ins->offset);
  4776. BUG();
  4777. }
  4778. return ret;
  4779. }
  4780. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  4781. struct btrfs_root *root,
  4782. u64 parent, u64 root_objectid,
  4783. u64 flags, struct btrfs_disk_key *key,
  4784. int level, struct btrfs_key *ins)
  4785. {
  4786. int ret;
  4787. struct btrfs_fs_info *fs_info = root->fs_info;
  4788. struct btrfs_extent_item *extent_item;
  4789. struct btrfs_tree_block_info *block_info;
  4790. struct btrfs_extent_inline_ref *iref;
  4791. struct btrfs_path *path;
  4792. struct extent_buffer *leaf;
  4793. u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
  4794. path = btrfs_alloc_path();
  4795. BUG_ON(!path);
  4796. path->leave_spinning = 1;
  4797. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  4798. ins, size);
  4799. BUG_ON(ret);
  4800. leaf = path->nodes[0];
  4801. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  4802. struct btrfs_extent_item);
  4803. btrfs_set_extent_refs(leaf, extent_item, 1);
  4804. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  4805. btrfs_set_extent_flags(leaf, extent_item,
  4806. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  4807. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  4808. btrfs_set_tree_block_key(leaf, block_info, key);
  4809. btrfs_set_tree_block_level(leaf, block_info, level);
  4810. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  4811. if (parent > 0) {
  4812. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  4813. btrfs_set_extent_inline_ref_type(leaf, iref,
  4814. BTRFS_SHARED_BLOCK_REF_KEY);
  4815. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  4816. } else {
  4817. btrfs_set_extent_inline_ref_type(leaf, iref,
  4818. BTRFS_TREE_BLOCK_REF_KEY);
  4819. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  4820. }
  4821. btrfs_mark_buffer_dirty(leaf);
  4822. btrfs_free_path(path);
  4823. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  4824. if (ret) {
  4825. printk(KERN_ERR "btrfs update block group failed for %llu "
  4826. "%llu\n", (unsigned long long)ins->objectid,
  4827. (unsigned long long)ins->offset);
  4828. BUG();
  4829. }
  4830. return ret;
  4831. }
  4832. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  4833. struct btrfs_root *root,
  4834. u64 root_objectid, u64 owner,
  4835. u64 offset, struct btrfs_key *ins)
  4836. {
  4837. int ret;
  4838. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  4839. ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
  4840. 0, root_objectid, owner, offset,
  4841. BTRFS_ADD_DELAYED_EXTENT, NULL);
  4842. return ret;
  4843. }
  4844. /*
  4845. * this is used by the tree logging recovery code. It records that
  4846. * an extent has been allocated and makes sure to clear the free
  4847. * space cache bits as well
  4848. */
  4849. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  4850. struct btrfs_root *root,
  4851. u64 root_objectid, u64 owner, u64 offset,
  4852. struct btrfs_key *ins)
  4853. {
  4854. int ret;
  4855. struct btrfs_block_group_cache *block_group;
  4856. struct btrfs_caching_control *caching_ctl;
  4857. u64 start = ins->objectid;
  4858. u64 num_bytes = ins->offset;
  4859. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  4860. cache_block_group(block_group, trans, 0);
  4861. caching_ctl = get_caching_control(block_group);
  4862. if (!caching_ctl) {
  4863. BUG_ON(!block_group_cache_done(block_group));
  4864. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  4865. BUG_ON(ret);
  4866. } else {
  4867. mutex_lock(&caching_ctl->mutex);
  4868. if (start >= caching_ctl->progress) {
  4869. ret = add_excluded_extent(root, start, num_bytes);
  4870. BUG_ON(ret);
  4871. } else if (start + num_bytes <= caching_ctl->progress) {
  4872. ret = btrfs_remove_free_space(block_group,
  4873. start, num_bytes);
  4874. BUG_ON(ret);
  4875. } else {
  4876. num_bytes = caching_ctl->progress - start;
  4877. ret = btrfs_remove_free_space(block_group,
  4878. start, num_bytes);
  4879. BUG_ON(ret);
  4880. start = caching_ctl->progress;
  4881. num_bytes = ins->objectid + ins->offset -
  4882. caching_ctl->progress;
  4883. ret = add_excluded_extent(root, start, num_bytes);
  4884. BUG_ON(ret);
  4885. }
  4886. mutex_unlock(&caching_ctl->mutex);
  4887. put_caching_control(caching_ctl);
  4888. }
  4889. ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
  4890. BUG_ON(ret);
  4891. btrfs_put_block_group(block_group);
  4892. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  4893. 0, owner, offset, ins, 1);
  4894. return ret;
  4895. }
  4896. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  4897. struct btrfs_root *root,
  4898. u64 bytenr, u32 blocksize,
  4899. int level)
  4900. {
  4901. struct extent_buffer *buf;
  4902. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  4903. if (!buf)
  4904. return ERR_PTR(-ENOMEM);
  4905. btrfs_set_header_generation(buf, trans->transid);
  4906. btrfs_set_buffer_lockdep_class(buf, level);
  4907. btrfs_tree_lock(buf);
  4908. clean_tree_block(trans, root, buf);
  4909. btrfs_set_lock_blocking(buf);
  4910. btrfs_set_buffer_uptodate(buf);
  4911. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  4912. /*
  4913. * we allow two log transactions at a time, use different
  4914. * EXENT bit to differentiate dirty pages.
  4915. */
  4916. if (root->log_transid % 2 == 0)
  4917. set_extent_dirty(&root->dirty_log_pages, buf->start,
  4918. buf->start + buf->len - 1, GFP_NOFS);
  4919. else
  4920. set_extent_new(&root->dirty_log_pages, buf->start,
  4921. buf->start + buf->len - 1, GFP_NOFS);
  4922. } else {
  4923. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  4924. buf->start + buf->len - 1, GFP_NOFS);
  4925. }
  4926. trans->blocks_used++;
  4927. /* this returns a buffer locked for blocking */
  4928. return buf;
  4929. }
  4930. static struct btrfs_block_rsv *
  4931. use_block_rsv(struct btrfs_trans_handle *trans,
  4932. struct btrfs_root *root, u32 blocksize)
  4933. {
  4934. struct btrfs_block_rsv *block_rsv;
  4935. int ret;
  4936. block_rsv = get_block_rsv(trans, root);
  4937. if (block_rsv->size == 0) {
  4938. ret = reserve_metadata_bytes(trans, root, block_rsv,
  4939. blocksize, 0);
  4940. if (ret)
  4941. return ERR_PTR(ret);
  4942. return block_rsv;
  4943. }
  4944. ret = block_rsv_use_bytes(block_rsv, blocksize);
  4945. if (!ret)
  4946. return block_rsv;
  4947. return ERR_PTR(-ENOSPC);
  4948. }
  4949. static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
  4950. {
  4951. block_rsv_add_bytes(block_rsv, blocksize, 0);
  4952. block_rsv_release_bytes(block_rsv, NULL, 0);
  4953. }
  4954. /*
  4955. * finds a free extent and does all the dirty work required for allocation
  4956. * returns the key for the extent through ins, and a tree buffer for
  4957. * the first block of the extent through buf.
  4958. *
  4959. * returns the tree buffer or NULL.
  4960. */
  4961. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  4962. struct btrfs_root *root, u32 blocksize,
  4963. u64 parent, u64 root_objectid,
  4964. struct btrfs_disk_key *key, int level,
  4965. u64 hint, u64 empty_size)
  4966. {
  4967. struct btrfs_key ins;
  4968. struct btrfs_block_rsv *block_rsv;
  4969. struct extent_buffer *buf;
  4970. u64 flags = 0;
  4971. int ret;
  4972. block_rsv = use_block_rsv(trans, root, blocksize);
  4973. if (IS_ERR(block_rsv))
  4974. return ERR_CAST(block_rsv);
  4975. ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
  4976. empty_size, hint, (u64)-1, &ins, 0);
  4977. if (ret) {
  4978. unuse_block_rsv(block_rsv, blocksize);
  4979. return ERR_PTR(ret);
  4980. }
  4981. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  4982. blocksize, level);
  4983. BUG_ON(IS_ERR(buf));
  4984. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  4985. if (parent == 0)
  4986. parent = ins.objectid;
  4987. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  4988. } else
  4989. BUG_ON(parent > 0);
  4990. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  4991. struct btrfs_delayed_extent_op *extent_op;
  4992. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  4993. BUG_ON(!extent_op);
  4994. if (key)
  4995. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  4996. else
  4997. memset(&extent_op->key, 0, sizeof(extent_op->key));
  4998. extent_op->flags_to_set = flags;
  4999. extent_op->update_key = 1;
  5000. extent_op->update_flags = 1;
  5001. extent_op->is_data = 0;
  5002. ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
  5003. ins.offset, parent, root_objectid,
  5004. level, BTRFS_ADD_DELAYED_EXTENT,
  5005. extent_op);
  5006. BUG_ON(ret);
  5007. }
  5008. return buf;
  5009. }
  5010. struct walk_control {
  5011. u64 refs[BTRFS_MAX_LEVEL];
  5012. u64 flags[BTRFS_MAX_LEVEL];
  5013. struct btrfs_key update_progress;
  5014. int stage;
  5015. int level;
  5016. int shared_level;
  5017. int update_ref;
  5018. int keep_locks;
  5019. int reada_slot;
  5020. int reada_count;
  5021. };
  5022. #define DROP_REFERENCE 1
  5023. #define UPDATE_BACKREF 2
  5024. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  5025. struct btrfs_root *root,
  5026. struct walk_control *wc,
  5027. struct btrfs_path *path)
  5028. {
  5029. u64 bytenr;
  5030. u64 generation;
  5031. u64 refs;
  5032. u64 flags;
  5033. u32 nritems;
  5034. u32 blocksize;
  5035. struct btrfs_key key;
  5036. struct extent_buffer *eb;
  5037. int ret;
  5038. int slot;
  5039. int nread = 0;
  5040. if (path->slots[wc->level] < wc->reada_slot) {
  5041. wc->reada_count = wc->reada_count * 2 / 3;
  5042. wc->reada_count = max(wc->reada_count, 2);
  5043. } else {
  5044. wc->reada_count = wc->reada_count * 3 / 2;
  5045. wc->reada_count = min_t(int, wc->reada_count,
  5046. BTRFS_NODEPTRS_PER_BLOCK(root));
  5047. }
  5048. eb = path->nodes[wc->level];
  5049. nritems = btrfs_header_nritems(eb);
  5050. blocksize = btrfs_level_size(root, wc->level - 1);
  5051. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  5052. if (nread >= wc->reada_count)
  5053. break;
  5054. cond_resched();
  5055. bytenr = btrfs_node_blockptr(eb, slot);
  5056. generation = btrfs_node_ptr_generation(eb, slot);
  5057. if (slot == path->slots[wc->level])
  5058. goto reada;
  5059. if (wc->stage == UPDATE_BACKREF &&
  5060. generation <= root->root_key.offset)
  5061. continue;
  5062. /* We don't lock the tree block, it's OK to be racy here */
  5063. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5064. &refs, &flags);
  5065. BUG_ON(ret);
  5066. BUG_ON(refs == 0);
  5067. if (wc->stage == DROP_REFERENCE) {
  5068. if (refs == 1)
  5069. goto reada;
  5070. if (wc->level == 1 &&
  5071. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5072. continue;
  5073. if (!wc->update_ref ||
  5074. generation <= root->root_key.offset)
  5075. continue;
  5076. btrfs_node_key_to_cpu(eb, &key, slot);
  5077. ret = btrfs_comp_cpu_keys(&key,
  5078. &wc->update_progress);
  5079. if (ret < 0)
  5080. continue;
  5081. } else {
  5082. if (wc->level == 1 &&
  5083. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5084. continue;
  5085. }
  5086. reada:
  5087. ret = readahead_tree_block(root, bytenr, blocksize,
  5088. generation);
  5089. if (ret)
  5090. break;
  5091. nread++;
  5092. }
  5093. wc->reada_slot = slot;
  5094. }
  5095. /*
  5096. * hepler to process tree block while walking down the tree.
  5097. *
  5098. * when wc->stage == UPDATE_BACKREF, this function updates
  5099. * back refs for pointers in the block.
  5100. *
  5101. * NOTE: return value 1 means we should stop walking down.
  5102. */
  5103. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  5104. struct btrfs_root *root,
  5105. struct btrfs_path *path,
  5106. struct walk_control *wc, int lookup_info)
  5107. {
  5108. int level = wc->level;
  5109. struct extent_buffer *eb = path->nodes[level];
  5110. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5111. int ret;
  5112. if (wc->stage == UPDATE_BACKREF &&
  5113. btrfs_header_owner(eb) != root->root_key.objectid)
  5114. return 1;
  5115. /*
  5116. * when reference count of tree block is 1, it won't increase
  5117. * again. once full backref flag is set, we never clear it.
  5118. */
  5119. if (lookup_info &&
  5120. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  5121. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  5122. BUG_ON(!path->locks[level]);
  5123. ret = btrfs_lookup_extent_info(trans, root,
  5124. eb->start, eb->len,
  5125. &wc->refs[level],
  5126. &wc->flags[level]);
  5127. BUG_ON(ret);
  5128. BUG_ON(wc->refs[level] == 0);
  5129. }
  5130. if (wc->stage == DROP_REFERENCE) {
  5131. if (wc->refs[level] > 1)
  5132. return 1;
  5133. if (path->locks[level] && !wc->keep_locks) {
  5134. btrfs_tree_unlock(eb);
  5135. path->locks[level] = 0;
  5136. }
  5137. return 0;
  5138. }
  5139. /* wc->stage == UPDATE_BACKREF */
  5140. if (!(wc->flags[level] & flag)) {
  5141. BUG_ON(!path->locks[level]);
  5142. ret = btrfs_inc_ref(trans, root, eb, 1);
  5143. BUG_ON(ret);
  5144. ret = btrfs_dec_ref(trans, root, eb, 0);
  5145. BUG_ON(ret);
  5146. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  5147. eb->len, flag, 0);
  5148. BUG_ON(ret);
  5149. wc->flags[level] |= flag;
  5150. }
  5151. /*
  5152. * the block is shared by multiple trees, so it's not good to
  5153. * keep the tree lock
  5154. */
  5155. if (path->locks[level] && level > 0) {
  5156. btrfs_tree_unlock(eb);
  5157. path->locks[level] = 0;
  5158. }
  5159. return 0;
  5160. }
  5161. /*
  5162. * hepler to process tree block pointer.
  5163. *
  5164. * when wc->stage == DROP_REFERENCE, this function checks
  5165. * reference count of the block pointed to. if the block
  5166. * is shared and we need update back refs for the subtree
  5167. * rooted at the block, this function changes wc->stage to
  5168. * UPDATE_BACKREF. if the block is shared and there is no
  5169. * need to update back, this function drops the reference
  5170. * to the block.
  5171. *
  5172. * NOTE: return value 1 means we should stop walking down.
  5173. */
  5174. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  5175. struct btrfs_root *root,
  5176. struct btrfs_path *path,
  5177. struct walk_control *wc, int *lookup_info)
  5178. {
  5179. u64 bytenr;
  5180. u64 generation;
  5181. u64 parent;
  5182. u32 blocksize;
  5183. struct btrfs_key key;
  5184. struct extent_buffer *next;
  5185. int level = wc->level;
  5186. int reada = 0;
  5187. int ret = 0;
  5188. generation = btrfs_node_ptr_generation(path->nodes[level],
  5189. path->slots[level]);
  5190. /*
  5191. * if the lower level block was created before the snapshot
  5192. * was created, we know there is no need to update back refs
  5193. * for the subtree
  5194. */
  5195. if (wc->stage == UPDATE_BACKREF &&
  5196. generation <= root->root_key.offset) {
  5197. *lookup_info = 1;
  5198. return 1;
  5199. }
  5200. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  5201. blocksize = btrfs_level_size(root, level - 1);
  5202. next = btrfs_find_tree_block(root, bytenr, blocksize);
  5203. if (!next) {
  5204. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5205. if (!next)
  5206. return -ENOMEM;
  5207. reada = 1;
  5208. }
  5209. btrfs_tree_lock(next);
  5210. btrfs_set_lock_blocking(next);
  5211. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5212. &wc->refs[level - 1],
  5213. &wc->flags[level - 1]);
  5214. BUG_ON(ret);
  5215. BUG_ON(wc->refs[level - 1] == 0);
  5216. *lookup_info = 0;
  5217. if (wc->stage == DROP_REFERENCE) {
  5218. if (wc->refs[level - 1] > 1) {
  5219. if (level == 1 &&
  5220. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5221. goto skip;
  5222. if (!wc->update_ref ||
  5223. generation <= root->root_key.offset)
  5224. goto skip;
  5225. btrfs_node_key_to_cpu(path->nodes[level], &key,
  5226. path->slots[level]);
  5227. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  5228. if (ret < 0)
  5229. goto skip;
  5230. wc->stage = UPDATE_BACKREF;
  5231. wc->shared_level = level - 1;
  5232. }
  5233. } else {
  5234. if (level == 1 &&
  5235. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5236. goto skip;
  5237. }
  5238. if (!btrfs_buffer_uptodate(next, generation)) {
  5239. btrfs_tree_unlock(next);
  5240. free_extent_buffer(next);
  5241. next = NULL;
  5242. *lookup_info = 1;
  5243. }
  5244. if (!next) {
  5245. if (reada && level == 1)
  5246. reada_walk_down(trans, root, wc, path);
  5247. next = read_tree_block(root, bytenr, blocksize, generation);
  5248. btrfs_tree_lock(next);
  5249. btrfs_set_lock_blocking(next);
  5250. }
  5251. level--;
  5252. BUG_ON(level != btrfs_header_level(next));
  5253. path->nodes[level] = next;
  5254. path->slots[level] = 0;
  5255. path->locks[level] = 1;
  5256. wc->level = level;
  5257. if (wc->level == 1)
  5258. wc->reada_slot = 0;
  5259. return 0;
  5260. skip:
  5261. wc->refs[level - 1] = 0;
  5262. wc->flags[level - 1] = 0;
  5263. if (wc->stage == DROP_REFERENCE) {
  5264. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  5265. parent = path->nodes[level]->start;
  5266. } else {
  5267. BUG_ON(root->root_key.objectid !=
  5268. btrfs_header_owner(path->nodes[level]));
  5269. parent = 0;
  5270. }
  5271. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  5272. root->root_key.objectid, level - 1, 0);
  5273. BUG_ON(ret);
  5274. }
  5275. btrfs_tree_unlock(next);
  5276. free_extent_buffer(next);
  5277. *lookup_info = 1;
  5278. return 1;
  5279. }
  5280. /*
  5281. * hepler to process tree block while walking up the tree.
  5282. *
  5283. * when wc->stage == DROP_REFERENCE, this function drops
  5284. * reference count on the block.
  5285. *
  5286. * when wc->stage == UPDATE_BACKREF, this function changes
  5287. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  5288. * to UPDATE_BACKREF previously while processing the block.
  5289. *
  5290. * NOTE: return value 1 means we should stop walking up.
  5291. */
  5292. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  5293. struct btrfs_root *root,
  5294. struct btrfs_path *path,
  5295. struct walk_control *wc)
  5296. {
  5297. int ret;
  5298. int level = wc->level;
  5299. struct extent_buffer *eb = path->nodes[level];
  5300. u64 parent = 0;
  5301. if (wc->stage == UPDATE_BACKREF) {
  5302. BUG_ON(wc->shared_level < level);
  5303. if (level < wc->shared_level)
  5304. goto out;
  5305. ret = find_next_key(path, level + 1, &wc->update_progress);
  5306. if (ret > 0)
  5307. wc->update_ref = 0;
  5308. wc->stage = DROP_REFERENCE;
  5309. wc->shared_level = -1;
  5310. path->slots[level] = 0;
  5311. /*
  5312. * check reference count again if the block isn't locked.
  5313. * we should start walking down the tree again if reference
  5314. * count is one.
  5315. */
  5316. if (!path->locks[level]) {
  5317. BUG_ON(level == 0);
  5318. btrfs_tree_lock(eb);
  5319. btrfs_set_lock_blocking(eb);
  5320. path->locks[level] = 1;
  5321. ret = btrfs_lookup_extent_info(trans, root,
  5322. eb->start, eb->len,
  5323. &wc->refs[level],
  5324. &wc->flags[level]);
  5325. BUG_ON(ret);
  5326. BUG_ON(wc->refs[level] == 0);
  5327. if (wc->refs[level] == 1) {
  5328. btrfs_tree_unlock(eb);
  5329. path->locks[level] = 0;
  5330. return 1;
  5331. }
  5332. }
  5333. }
  5334. /* wc->stage == DROP_REFERENCE */
  5335. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  5336. if (wc->refs[level] == 1) {
  5337. if (level == 0) {
  5338. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5339. ret = btrfs_dec_ref(trans, root, eb, 1);
  5340. else
  5341. ret = btrfs_dec_ref(trans, root, eb, 0);
  5342. BUG_ON(ret);
  5343. }
  5344. /* make block locked assertion in clean_tree_block happy */
  5345. if (!path->locks[level] &&
  5346. btrfs_header_generation(eb) == trans->transid) {
  5347. btrfs_tree_lock(eb);
  5348. btrfs_set_lock_blocking(eb);
  5349. path->locks[level] = 1;
  5350. }
  5351. clean_tree_block(trans, root, eb);
  5352. }
  5353. if (eb == root->node) {
  5354. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5355. parent = eb->start;
  5356. else
  5357. BUG_ON(root->root_key.objectid !=
  5358. btrfs_header_owner(eb));
  5359. } else {
  5360. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5361. parent = path->nodes[level + 1]->start;
  5362. else
  5363. BUG_ON(root->root_key.objectid !=
  5364. btrfs_header_owner(path->nodes[level + 1]));
  5365. }
  5366. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  5367. out:
  5368. wc->refs[level] = 0;
  5369. wc->flags[level] = 0;
  5370. return 0;
  5371. }
  5372. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  5373. struct btrfs_root *root,
  5374. struct btrfs_path *path,
  5375. struct walk_control *wc)
  5376. {
  5377. int level = wc->level;
  5378. int lookup_info = 1;
  5379. int ret;
  5380. while (level >= 0) {
  5381. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  5382. if (ret > 0)
  5383. break;
  5384. if (level == 0)
  5385. break;
  5386. if (path->slots[level] >=
  5387. btrfs_header_nritems(path->nodes[level]))
  5388. break;
  5389. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  5390. if (ret > 0) {
  5391. path->slots[level]++;
  5392. continue;
  5393. } else if (ret < 0)
  5394. return ret;
  5395. level = wc->level;
  5396. }
  5397. return 0;
  5398. }
  5399. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  5400. struct btrfs_root *root,
  5401. struct btrfs_path *path,
  5402. struct walk_control *wc, int max_level)
  5403. {
  5404. int level = wc->level;
  5405. int ret;
  5406. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  5407. while (level < max_level && path->nodes[level]) {
  5408. wc->level = level;
  5409. if (path->slots[level] + 1 <
  5410. btrfs_header_nritems(path->nodes[level])) {
  5411. path->slots[level]++;
  5412. return 0;
  5413. } else {
  5414. ret = walk_up_proc(trans, root, path, wc);
  5415. if (ret > 0)
  5416. return 0;
  5417. if (path->locks[level]) {
  5418. btrfs_tree_unlock(path->nodes[level]);
  5419. path->locks[level] = 0;
  5420. }
  5421. free_extent_buffer(path->nodes[level]);
  5422. path->nodes[level] = NULL;
  5423. level++;
  5424. }
  5425. }
  5426. return 1;
  5427. }
  5428. /*
  5429. * drop a subvolume tree.
  5430. *
  5431. * this function traverses the tree freeing any blocks that only
  5432. * referenced by the tree.
  5433. *
  5434. * when a shared tree block is found. this function decreases its
  5435. * reference count by one. if update_ref is true, this function
  5436. * also make sure backrefs for the shared block and all lower level
  5437. * blocks are properly updated.
  5438. */
  5439. int btrfs_drop_snapshot(struct btrfs_root *root,
  5440. struct btrfs_block_rsv *block_rsv, int update_ref)
  5441. {
  5442. struct btrfs_path *path;
  5443. struct btrfs_trans_handle *trans;
  5444. struct btrfs_root *tree_root = root->fs_info->tree_root;
  5445. struct btrfs_root_item *root_item = &root->root_item;
  5446. struct walk_control *wc;
  5447. struct btrfs_key key;
  5448. int err = 0;
  5449. int ret;
  5450. int level;
  5451. path = btrfs_alloc_path();
  5452. BUG_ON(!path);
  5453. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  5454. BUG_ON(!wc);
  5455. trans = btrfs_start_transaction(tree_root, 0);
  5456. if (block_rsv)
  5457. trans->block_rsv = block_rsv;
  5458. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  5459. level = btrfs_header_level(root->node);
  5460. path->nodes[level] = btrfs_lock_root_node(root);
  5461. btrfs_set_lock_blocking(path->nodes[level]);
  5462. path->slots[level] = 0;
  5463. path->locks[level] = 1;
  5464. memset(&wc->update_progress, 0,
  5465. sizeof(wc->update_progress));
  5466. } else {
  5467. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  5468. memcpy(&wc->update_progress, &key,
  5469. sizeof(wc->update_progress));
  5470. level = root_item->drop_level;
  5471. BUG_ON(level == 0);
  5472. path->lowest_level = level;
  5473. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5474. path->lowest_level = 0;
  5475. if (ret < 0) {
  5476. err = ret;
  5477. goto out;
  5478. }
  5479. WARN_ON(ret > 0);
  5480. /*
  5481. * unlock our path, this is safe because only this
  5482. * function is allowed to delete this snapshot
  5483. */
  5484. btrfs_unlock_up_safe(path, 0);
  5485. level = btrfs_header_level(root->node);
  5486. while (1) {
  5487. btrfs_tree_lock(path->nodes[level]);
  5488. btrfs_set_lock_blocking(path->nodes[level]);
  5489. ret = btrfs_lookup_extent_info(trans, root,
  5490. path->nodes[level]->start,
  5491. path->nodes[level]->len,
  5492. &wc->refs[level],
  5493. &wc->flags[level]);
  5494. BUG_ON(ret);
  5495. BUG_ON(wc->refs[level] == 0);
  5496. if (level == root_item->drop_level)
  5497. break;
  5498. btrfs_tree_unlock(path->nodes[level]);
  5499. WARN_ON(wc->refs[level] != 1);
  5500. level--;
  5501. }
  5502. }
  5503. wc->level = level;
  5504. wc->shared_level = -1;
  5505. wc->stage = DROP_REFERENCE;
  5506. wc->update_ref = update_ref;
  5507. wc->keep_locks = 0;
  5508. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  5509. while (1) {
  5510. ret = walk_down_tree(trans, root, path, wc);
  5511. if (ret < 0) {
  5512. err = ret;
  5513. break;
  5514. }
  5515. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  5516. if (ret < 0) {
  5517. err = ret;
  5518. break;
  5519. }
  5520. if (ret > 0) {
  5521. BUG_ON(wc->stage != DROP_REFERENCE);
  5522. break;
  5523. }
  5524. if (wc->stage == DROP_REFERENCE) {
  5525. level = wc->level;
  5526. btrfs_node_key(path->nodes[level],
  5527. &root_item->drop_progress,
  5528. path->slots[level]);
  5529. root_item->drop_level = level;
  5530. }
  5531. BUG_ON(wc->level == 0);
  5532. if (btrfs_should_end_transaction(trans, tree_root)) {
  5533. ret = btrfs_update_root(trans, tree_root,
  5534. &root->root_key,
  5535. root_item);
  5536. BUG_ON(ret);
  5537. btrfs_end_transaction_throttle(trans, tree_root);
  5538. trans = btrfs_start_transaction(tree_root, 0);
  5539. if (block_rsv)
  5540. trans->block_rsv = block_rsv;
  5541. }
  5542. }
  5543. btrfs_release_path(root, path);
  5544. BUG_ON(err);
  5545. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  5546. BUG_ON(ret);
  5547. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  5548. ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
  5549. NULL, NULL);
  5550. BUG_ON(ret < 0);
  5551. if (ret > 0) {
  5552. ret = btrfs_del_orphan_item(trans, tree_root,
  5553. root->root_key.objectid);
  5554. BUG_ON(ret);
  5555. }
  5556. }
  5557. if (root->in_radix) {
  5558. btrfs_free_fs_root(tree_root->fs_info, root);
  5559. } else {
  5560. free_extent_buffer(root->node);
  5561. free_extent_buffer(root->commit_root);
  5562. kfree(root);
  5563. }
  5564. out:
  5565. btrfs_end_transaction_throttle(trans, tree_root);
  5566. kfree(wc);
  5567. btrfs_free_path(path);
  5568. return err;
  5569. }
  5570. /*
  5571. * drop subtree rooted at tree block 'node'.
  5572. *
  5573. * NOTE: this function will unlock and release tree block 'node'
  5574. */
  5575. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  5576. struct btrfs_root *root,
  5577. struct extent_buffer *node,
  5578. struct extent_buffer *parent)
  5579. {
  5580. struct btrfs_path *path;
  5581. struct walk_control *wc;
  5582. int level;
  5583. int parent_level;
  5584. int ret = 0;
  5585. int wret;
  5586. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  5587. path = btrfs_alloc_path();
  5588. BUG_ON(!path);
  5589. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  5590. BUG_ON(!wc);
  5591. btrfs_assert_tree_locked(parent);
  5592. parent_level = btrfs_header_level(parent);
  5593. extent_buffer_get(parent);
  5594. path->nodes[parent_level] = parent;
  5595. path->slots[parent_level] = btrfs_header_nritems(parent);
  5596. btrfs_assert_tree_locked(node);
  5597. level = btrfs_header_level(node);
  5598. path->nodes[level] = node;
  5599. path->slots[level] = 0;
  5600. path->locks[level] = 1;
  5601. wc->refs[parent_level] = 1;
  5602. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5603. wc->level = level;
  5604. wc->shared_level = -1;
  5605. wc->stage = DROP_REFERENCE;
  5606. wc->update_ref = 0;
  5607. wc->keep_locks = 1;
  5608. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  5609. while (1) {
  5610. wret = walk_down_tree(trans, root, path, wc);
  5611. if (wret < 0) {
  5612. ret = wret;
  5613. break;
  5614. }
  5615. wret = walk_up_tree(trans, root, path, wc, parent_level);
  5616. if (wret < 0)
  5617. ret = wret;
  5618. if (wret != 0)
  5619. break;
  5620. }
  5621. kfree(wc);
  5622. btrfs_free_path(path);
  5623. return ret;
  5624. }
  5625. #if 0
  5626. static unsigned long calc_ra(unsigned long start, unsigned long last,
  5627. unsigned long nr)
  5628. {
  5629. return min(last, start + nr - 1);
  5630. }
  5631. static noinline int relocate_inode_pages(struct inode *inode, u64 start,
  5632. u64 len)
  5633. {
  5634. u64 page_start;
  5635. u64 page_end;
  5636. unsigned long first_index;
  5637. unsigned long last_index;
  5638. unsigned long i;
  5639. struct page *page;
  5640. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5641. struct file_ra_state *ra;
  5642. struct btrfs_ordered_extent *ordered;
  5643. unsigned int total_read = 0;
  5644. unsigned int total_dirty = 0;
  5645. int ret = 0;
  5646. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  5647. mutex_lock(&inode->i_mutex);
  5648. first_index = start >> PAGE_CACHE_SHIFT;
  5649. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  5650. /* make sure the dirty trick played by the caller work */
  5651. ret = invalidate_inode_pages2_range(inode->i_mapping,
  5652. first_index, last_index);
  5653. if (ret)
  5654. goto out_unlock;
  5655. file_ra_state_init(ra, inode->i_mapping);
  5656. for (i = first_index ; i <= last_index; i++) {
  5657. if (total_read % ra->ra_pages == 0) {
  5658. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  5659. calc_ra(i, last_index, ra->ra_pages));
  5660. }
  5661. total_read++;
  5662. again:
  5663. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  5664. BUG_ON(1);
  5665. page = grab_cache_page(inode->i_mapping, i);
  5666. if (!page) {
  5667. ret = -ENOMEM;
  5668. goto out_unlock;
  5669. }
  5670. if (!PageUptodate(page)) {
  5671. btrfs_readpage(NULL, page);
  5672. lock_page(page);
  5673. if (!PageUptodate(page)) {
  5674. unlock_page(page);
  5675. page_cache_release(page);
  5676. ret = -EIO;
  5677. goto out_unlock;
  5678. }
  5679. }
  5680. wait_on_page_writeback(page);
  5681. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  5682. page_end = page_start + PAGE_CACHE_SIZE - 1;
  5683. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  5684. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  5685. if (ordered) {
  5686. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  5687. unlock_page(page);
  5688. page_cache_release(page);
  5689. btrfs_start_ordered_extent(inode, ordered, 1);
  5690. btrfs_put_ordered_extent(ordered);
  5691. goto again;
  5692. }
  5693. set_page_extent_mapped(page);
  5694. if (i == first_index)
  5695. set_extent_bits(io_tree, page_start, page_end,
  5696. EXTENT_BOUNDARY, GFP_NOFS);
  5697. btrfs_set_extent_delalloc(inode, page_start, page_end);
  5698. set_page_dirty(page);
  5699. total_dirty++;
  5700. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  5701. unlock_page(page);
  5702. page_cache_release(page);
  5703. }
  5704. out_unlock:
  5705. kfree(ra);
  5706. mutex_unlock(&inode->i_mutex);
  5707. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  5708. return ret;
  5709. }
  5710. static noinline int relocate_data_extent(struct inode *reloc_inode,
  5711. struct btrfs_key *extent_key,
  5712. u64 offset)
  5713. {
  5714. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  5715. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  5716. struct extent_map *em;
  5717. u64 start = extent_key->objectid - offset;
  5718. u64 end = start + extent_key->offset - 1;
  5719. em = alloc_extent_map(GFP_NOFS);
  5720. BUG_ON(!em || IS_ERR(em));
  5721. em->start = start;
  5722. em->len = extent_key->offset;
  5723. em->block_len = extent_key->offset;
  5724. em->block_start = extent_key->objectid;
  5725. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5726. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  5727. /* setup extent map to cheat btrfs_readpage */
  5728. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  5729. while (1) {
  5730. int ret;
  5731. write_lock(&em_tree->lock);
  5732. ret = add_extent_mapping(em_tree, em);
  5733. write_unlock(&em_tree->lock);
  5734. if (ret != -EEXIST) {
  5735. free_extent_map(em);
  5736. break;
  5737. }
  5738. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  5739. }
  5740. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  5741. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  5742. }
  5743. struct btrfs_ref_path {
  5744. u64 extent_start;
  5745. u64 nodes[BTRFS_MAX_LEVEL];
  5746. u64 root_objectid;
  5747. u64 root_generation;
  5748. u64 owner_objectid;
  5749. u32 num_refs;
  5750. int lowest_level;
  5751. int current_level;
  5752. int shared_level;
  5753. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  5754. u64 new_nodes[BTRFS_MAX_LEVEL];
  5755. };
  5756. struct disk_extent {
  5757. u64 ram_bytes;
  5758. u64 disk_bytenr;
  5759. u64 disk_num_bytes;
  5760. u64 offset;
  5761. u64 num_bytes;
  5762. u8 compression;
  5763. u8 encryption;
  5764. u16 other_encoding;
  5765. };
  5766. static int is_cowonly_root(u64 root_objectid)
  5767. {
  5768. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  5769. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  5770. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  5771. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  5772. root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  5773. root_objectid == BTRFS_CSUM_TREE_OBJECTID)
  5774. return 1;
  5775. return 0;
  5776. }
  5777. static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
  5778. struct btrfs_root *extent_root,
  5779. struct btrfs_ref_path *ref_path,
  5780. int first_time)
  5781. {
  5782. struct extent_buffer *leaf;
  5783. struct btrfs_path *path;
  5784. struct btrfs_extent_ref *ref;
  5785. struct btrfs_key key;
  5786. struct btrfs_key found_key;
  5787. u64 bytenr;
  5788. u32 nritems;
  5789. int level;
  5790. int ret = 1;
  5791. path = btrfs_alloc_path();
  5792. if (!path)
  5793. return -ENOMEM;
  5794. if (first_time) {
  5795. ref_path->lowest_level = -1;
  5796. ref_path->current_level = -1;
  5797. ref_path->shared_level = -1;
  5798. goto walk_up;
  5799. }
  5800. walk_down:
  5801. level = ref_path->current_level - 1;
  5802. while (level >= -1) {
  5803. u64 parent;
  5804. if (level < ref_path->lowest_level)
  5805. break;
  5806. if (level >= 0)
  5807. bytenr = ref_path->nodes[level];
  5808. else
  5809. bytenr = ref_path->extent_start;
  5810. BUG_ON(bytenr == 0);
  5811. parent = ref_path->nodes[level + 1];
  5812. ref_path->nodes[level + 1] = 0;
  5813. ref_path->current_level = level;
  5814. BUG_ON(parent == 0);
  5815. key.objectid = bytenr;
  5816. key.offset = parent + 1;
  5817. key.type = BTRFS_EXTENT_REF_KEY;
  5818. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  5819. if (ret < 0)
  5820. goto out;
  5821. BUG_ON(ret == 0);
  5822. leaf = path->nodes[0];
  5823. nritems = btrfs_header_nritems(leaf);
  5824. if (path->slots[0] >= nritems) {
  5825. ret = btrfs_next_leaf(extent_root, path);
  5826. if (ret < 0)
  5827. goto out;
  5828. if (ret > 0)
  5829. goto next;
  5830. leaf = path->nodes[0];
  5831. }
  5832. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5833. if (found_key.objectid == bytenr &&
  5834. found_key.type == BTRFS_EXTENT_REF_KEY) {
  5835. if (level < ref_path->shared_level)
  5836. ref_path->shared_level = level;
  5837. goto found;
  5838. }
  5839. next:
  5840. level--;
  5841. btrfs_release_path(extent_root, path);
  5842. cond_resched();
  5843. }
  5844. /* reached lowest level */
  5845. ret = 1;
  5846. goto out;
  5847. walk_up:
  5848. level = ref_path->current_level;
  5849. while (level < BTRFS_MAX_LEVEL - 1) {
  5850. u64 ref_objectid;
  5851. if (level >= 0)
  5852. bytenr = ref_path->nodes[level];
  5853. else
  5854. bytenr = ref_path->extent_start;
  5855. BUG_ON(bytenr == 0);
  5856. key.objectid = bytenr;
  5857. key.offset = 0;
  5858. key.type = BTRFS_EXTENT_REF_KEY;
  5859. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  5860. if (ret < 0)
  5861. goto out;
  5862. leaf = path->nodes[0];
  5863. nritems = btrfs_header_nritems(leaf);
  5864. if (path->slots[0] >= nritems) {
  5865. ret = btrfs_next_leaf(extent_root, path);
  5866. if (ret < 0)
  5867. goto out;
  5868. if (ret > 0) {
  5869. /* the extent was freed by someone */
  5870. if (ref_path->lowest_level == level)
  5871. goto out;
  5872. btrfs_release_path(extent_root, path);
  5873. goto walk_down;
  5874. }
  5875. leaf = path->nodes[0];
  5876. }
  5877. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5878. if (found_key.objectid != bytenr ||
  5879. found_key.type != BTRFS_EXTENT_REF_KEY) {
  5880. /* the extent was freed by someone */
  5881. if (ref_path->lowest_level == level) {
  5882. ret = 1;
  5883. goto out;
  5884. }
  5885. btrfs_release_path(extent_root, path);
  5886. goto walk_down;
  5887. }
  5888. found:
  5889. ref = btrfs_item_ptr(leaf, path->slots[0],
  5890. struct btrfs_extent_ref);
  5891. ref_objectid = btrfs_ref_objectid(leaf, ref);
  5892. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  5893. if (first_time) {
  5894. level = (int)ref_objectid;
  5895. BUG_ON(level >= BTRFS_MAX_LEVEL);
  5896. ref_path->lowest_level = level;
  5897. ref_path->current_level = level;
  5898. ref_path->nodes[level] = bytenr;
  5899. } else {
  5900. WARN_ON(ref_objectid != level);
  5901. }
  5902. } else {
  5903. WARN_ON(level != -1);
  5904. }
  5905. first_time = 0;
  5906. if (ref_path->lowest_level == level) {
  5907. ref_path->owner_objectid = ref_objectid;
  5908. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  5909. }
  5910. /*
  5911. * the block is tree root or the block isn't in reference
  5912. * counted tree.
  5913. */
  5914. if (found_key.objectid == found_key.offset ||
  5915. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  5916. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  5917. ref_path->root_generation =
  5918. btrfs_ref_generation(leaf, ref);
  5919. if (level < 0) {
  5920. /* special reference from the tree log */
  5921. ref_path->nodes[0] = found_key.offset;
  5922. ref_path->current_level = 0;
  5923. }
  5924. ret = 0;
  5925. goto out;
  5926. }
  5927. level++;
  5928. BUG_ON(ref_path->nodes[level] != 0);
  5929. ref_path->nodes[level] = found_key.offset;
  5930. ref_path->current_level = level;
  5931. /*
  5932. * the reference was created in the running transaction,
  5933. * no need to continue walking up.
  5934. */
  5935. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  5936. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  5937. ref_path->root_generation =
  5938. btrfs_ref_generation(leaf, ref);
  5939. ret = 0;
  5940. goto out;
  5941. }
  5942. btrfs_release_path(extent_root, path);
  5943. cond_resched();
  5944. }
  5945. /* reached max tree level, but no tree root found. */
  5946. BUG();
  5947. out:
  5948. btrfs_free_path(path);
  5949. return ret;
  5950. }
  5951. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  5952. struct btrfs_root *extent_root,
  5953. struct btrfs_ref_path *ref_path,
  5954. u64 extent_start)
  5955. {
  5956. memset(ref_path, 0, sizeof(*ref_path));
  5957. ref_path->extent_start = extent_start;
  5958. return __next_ref_path(trans, extent_root, ref_path, 1);
  5959. }
  5960. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  5961. struct btrfs_root *extent_root,
  5962. struct btrfs_ref_path *ref_path)
  5963. {
  5964. return __next_ref_path(trans, extent_root, ref_path, 0);
  5965. }
  5966. static noinline int get_new_locations(struct inode *reloc_inode,
  5967. struct btrfs_key *extent_key,
  5968. u64 offset, int no_fragment,
  5969. struct disk_extent **extents,
  5970. int *nr_extents)
  5971. {
  5972. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  5973. struct btrfs_path *path;
  5974. struct btrfs_file_extent_item *fi;
  5975. struct extent_buffer *leaf;
  5976. struct disk_extent *exts = *extents;
  5977. struct btrfs_key found_key;
  5978. u64 cur_pos;
  5979. u64 last_byte;
  5980. u32 nritems;
  5981. int nr = 0;
  5982. int max = *nr_extents;
  5983. int ret;
  5984. WARN_ON(!no_fragment && *extents);
  5985. if (!exts) {
  5986. max = 1;
  5987. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  5988. if (!exts)
  5989. return -ENOMEM;
  5990. }
  5991. path = btrfs_alloc_path();
  5992. BUG_ON(!path);
  5993. cur_pos = extent_key->objectid - offset;
  5994. last_byte = extent_key->objectid + extent_key->offset;
  5995. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  5996. cur_pos, 0);
  5997. if (ret < 0)
  5998. goto out;
  5999. if (ret > 0) {
  6000. ret = -ENOENT;
  6001. goto out;
  6002. }
  6003. while (1) {
  6004. leaf = path->nodes[0];
  6005. nritems = btrfs_header_nritems(leaf);
  6006. if (path->slots[0] >= nritems) {
  6007. ret = btrfs_next_leaf(root, path);
  6008. if (ret < 0)
  6009. goto out;
  6010. if (ret > 0)
  6011. break;
  6012. leaf = path->nodes[0];
  6013. }
  6014. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6015. if (found_key.offset != cur_pos ||
  6016. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  6017. found_key.objectid != reloc_inode->i_ino)
  6018. break;
  6019. fi = btrfs_item_ptr(leaf, path->slots[0],
  6020. struct btrfs_file_extent_item);
  6021. if (btrfs_file_extent_type(leaf, fi) !=
  6022. BTRFS_FILE_EXTENT_REG ||
  6023. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  6024. break;
  6025. if (nr == max) {
  6026. struct disk_extent *old = exts;
  6027. max *= 2;
  6028. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  6029. memcpy(exts, old, sizeof(*exts) * nr);
  6030. if (old != *extents)
  6031. kfree(old);
  6032. }
  6033. exts[nr].disk_bytenr =
  6034. btrfs_file_extent_disk_bytenr(leaf, fi);
  6035. exts[nr].disk_num_bytes =
  6036. btrfs_file_extent_disk_num_bytes(leaf, fi);
  6037. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  6038. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  6039. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  6040. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  6041. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  6042. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  6043. fi);
  6044. BUG_ON(exts[nr].offset > 0);
  6045. BUG_ON(exts[nr].compression || exts[nr].encryption);
  6046. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  6047. cur_pos += exts[nr].num_bytes;
  6048. nr++;
  6049. if (cur_pos + offset >= last_byte)
  6050. break;
  6051. if (no_fragment) {
  6052. ret = 1;
  6053. goto out;
  6054. }
  6055. path->slots[0]++;
  6056. }
  6057. BUG_ON(cur_pos + offset > last_byte);
  6058. if (cur_pos + offset < last_byte) {
  6059. ret = -ENOENT;
  6060. goto out;
  6061. }
  6062. ret = 0;
  6063. out:
  6064. btrfs_free_path(path);
  6065. if (ret) {
  6066. if (exts != *extents)
  6067. kfree(exts);
  6068. } else {
  6069. *extents = exts;
  6070. *nr_extents = nr;
  6071. }
  6072. return ret;
  6073. }
  6074. static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
  6075. struct btrfs_root *root,
  6076. struct btrfs_path *path,
  6077. struct btrfs_key *extent_key,
  6078. struct btrfs_key *leaf_key,
  6079. struct btrfs_ref_path *ref_path,
  6080. struct disk_extent *new_extents,
  6081. int nr_extents)
  6082. {
  6083. struct extent_buffer *leaf;
  6084. struct btrfs_file_extent_item *fi;
  6085. struct inode *inode = NULL;
  6086. struct btrfs_key key;
  6087. u64 lock_start = 0;
  6088. u64 lock_end = 0;
  6089. u64 num_bytes;
  6090. u64 ext_offset;
  6091. u64 search_end = (u64)-1;
  6092. u32 nritems;
  6093. int nr_scaned = 0;
  6094. int extent_locked = 0;
  6095. int extent_type;
  6096. int ret;
  6097. memcpy(&key, leaf_key, sizeof(key));
  6098. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  6099. if (key.objectid < ref_path->owner_objectid ||
  6100. (key.objectid == ref_path->owner_objectid &&
  6101. key.type < BTRFS_EXTENT_DATA_KEY)) {
  6102. key.objectid = ref_path->owner_objectid;
  6103. key.type = BTRFS_EXTENT_DATA_KEY;
  6104. key.offset = 0;
  6105. }
  6106. }
  6107. while (1) {
  6108. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  6109. if (ret < 0)
  6110. goto out;
  6111. leaf = path->nodes[0];
  6112. nritems = btrfs_header_nritems(leaf);
  6113. next:
  6114. if (extent_locked && ret > 0) {
  6115. /*
  6116. * the file extent item was modified by someone
  6117. * before the extent got locked.
  6118. */
  6119. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  6120. lock_end, GFP_NOFS);
  6121. extent_locked = 0;
  6122. }
  6123. if (path->slots[0] >= nritems) {
  6124. if (++nr_scaned > 2)
  6125. break;
  6126. BUG_ON(extent_locked);
  6127. ret = btrfs_next_leaf(root, path);
  6128. if (ret < 0)
  6129. goto out;
  6130. if (ret > 0)
  6131. break;
  6132. leaf = path->nodes[0];
  6133. nritems = btrfs_header_nritems(leaf);
  6134. }
  6135. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  6136. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  6137. if ((key.objectid > ref_path->owner_objectid) ||
  6138. (key.objectid == ref_path->owner_objectid &&
  6139. key.type > BTRFS_EXTENT_DATA_KEY) ||
  6140. key.offset >= search_end)
  6141. break;
  6142. }
  6143. if (inode && key.objectid != inode->i_ino) {
  6144. BUG_ON(extent_locked);
  6145. btrfs_release_path(root, path);
  6146. mutex_unlock(&inode->i_mutex);
  6147. iput(inode);
  6148. inode = NULL;
  6149. continue;
  6150. }
  6151. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  6152. path->slots[0]++;
  6153. ret = 1;
  6154. goto next;
  6155. }
  6156. fi = btrfs_item_ptr(leaf, path->slots[0],
  6157. struct btrfs_file_extent_item);
  6158. extent_type = btrfs_file_extent_type(leaf, fi);
  6159. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  6160. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  6161. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  6162. extent_key->objectid)) {
  6163. path->slots[0]++;
  6164. ret = 1;
  6165. goto next;
  6166. }
  6167. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  6168. ext_offset = btrfs_file_extent_offset(leaf, fi);
  6169. if (search_end == (u64)-1) {
  6170. search_end = key.offset - ext_offset +
  6171. btrfs_file_extent_ram_bytes(leaf, fi);
  6172. }
  6173. if (!extent_locked) {
  6174. lock_start = key.offset;
  6175. lock_end = lock_start + num_bytes - 1;
  6176. } else {
  6177. if (lock_start > key.offset ||
  6178. lock_end + 1 < key.offset + num_bytes) {
  6179. unlock_extent(&BTRFS_I(inode)->io_tree,
  6180. lock_start, lock_end, GFP_NOFS);
  6181. extent_locked = 0;
  6182. }
  6183. }
  6184. if (!inode) {
  6185. btrfs_release_path(root, path);
  6186. inode = btrfs_iget_locked(root->fs_info->sb,
  6187. key.objectid, root);
  6188. if (inode->i_state & I_NEW) {
  6189. BTRFS_I(inode)->root = root;
  6190. BTRFS_I(inode)->location.objectid =
  6191. key.objectid;
  6192. BTRFS_I(inode)->location.type =
  6193. BTRFS_INODE_ITEM_KEY;
  6194. BTRFS_I(inode)->location.offset = 0;
  6195. btrfs_read_locked_inode(inode);
  6196. unlock_new_inode(inode);
  6197. }
  6198. /*
  6199. * some code call btrfs_commit_transaction while
  6200. * holding the i_mutex, so we can't use mutex_lock
  6201. * here.
  6202. */
  6203. if (is_bad_inode(inode) ||
  6204. !mutex_trylock(&inode->i_mutex)) {
  6205. iput(inode);
  6206. inode = NULL;
  6207. key.offset = (u64)-1;
  6208. goto skip;
  6209. }
  6210. }
  6211. if (!extent_locked) {
  6212. struct btrfs_ordered_extent *ordered;
  6213. btrfs_release_path(root, path);
  6214. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  6215. lock_end, GFP_NOFS);
  6216. ordered = btrfs_lookup_first_ordered_extent(inode,
  6217. lock_end);
  6218. if (ordered &&
  6219. ordered->file_offset <= lock_end &&
  6220. ordered->file_offset + ordered->len > lock_start) {
  6221. unlock_extent(&BTRFS_I(inode)->io_tree,
  6222. lock_start, lock_end, GFP_NOFS);
  6223. btrfs_start_ordered_extent(inode, ordered, 1);
  6224. btrfs_put_ordered_extent(ordered);
  6225. key.offset += num_bytes;
  6226. goto skip;
  6227. }
  6228. if (ordered)
  6229. btrfs_put_ordered_extent(ordered);
  6230. extent_locked = 1;
  6231. continue;
  6232. }
  6233. if (nr_extents == 1) {
  6234. /* update extent pointer in place */
  6235. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  6236. new_extents[0].disk_bytenr);
  6237. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  6238. new_extents[0].disk_num_bytes);
  6239. btrfs_mark_buffer_dirty(leaf);
  6240. btrfs_drop_extent_cache(inode, key.offset,
  6241. key.offset + num_bytes - 1, 0);
  6242. ret = btrfs_inc_extent_ref(trans, root,
  6243. new_extents[0].disk_bytenr,
  6244. new_extents[0].disk_num_bytes,
  6245. leaf->start,
  6246. root->root_key.objectid,
  6247. trans->transid,
  6248. key.objectid);
  6249. BUG_ON(ret);
  6250. ret = btrfs_free_extent(trans, root,
  6251. extent_key->objectid,
  6252. extent_key->offset,
  6253. leaf->start,
  6254. btrfs_header_owner(leaf),
  6255. btrfs_header_generation(leaf),
  6256. key.objectid, 0);
  6257. BUG_ON(ret);
  6258. btrfs_release_path(root, path);
  6259. key.offset += num_bytes;
  6260. } else {
  6261. BUG_ON(1);
  6262. #if 0
  6263. u64 alloc_hint;
  6264. u64 extent_len;
  6265. int i;
  6266. /*
  6267. * drop old extent pointer at first, then insert the
  6268. * new pointers one bye one
  6269. */
  6270. btrfs_release_path(root, path);
  6271. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  6272. key.offset + num_bytes,
  6273. key.offset, &alloc_hint);
  6274. BUG_ON(ret);
  6275. for (i = 0; i < nr_extents; i++) {
  6276. if (ext_offset >= new_extents[i].num_bytes) {
  6277. ext_offset -= new_extents[i].num_bytes;
  6278. continue;
  6279. }
  6280. extent_len = min(new_extents[i].num_bytes -
  6281. ext_offset, num_bytes);
  6282. ret = btrfs_insert_empty_item(trans, root,
  6283. path, &key,
  6284. sizeof(*fi));
  6285. BUG_ON(ret);
  6286. leaf = path->nodes[0];
  6287. fi = btrfs_item_ptr(leaf, path->slots[0],
  6288. struct btrfs_file_extent_item);
  6289. btrfs_set_file_extent_generation(leaf, fi,
  6290. trans->transid);
  6291. btrfs_set_file_extent_type(leaf, fi,
  6292. BTRFS_FILE_EXTENT_REG);
  6293. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  6294. new_extents[i].disk_bytenr);
  6295. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  6296. new_extents[i].disk_num_bytes);
  6297. btrfs_set_file_extent_ram_bytes(leaf, fi,
  6298. new_extents[i].ram_bytes);
  6299. btrfs_set_file_extent_compression(leaf, fi,
  6300. new_extents[i].compression);
  6301. btrfs_set_file_extent_encryption(leaf, fi,
  6302. new_extents[i].encryption);
  6303. btrfs_set_file_extent_other_encoding(leaf, fi,
  6304. new_extents[i].other_encoding);
  6305. btrfs_set_file_extent_num_bytes(leaf, fi,
  6306. extent_len);
  6307. ext_offset += new_extents[i].offset;
  6308. btrfs_set_file_extent_offset(leaf, fi,
  6309. ext_offset);
  6310. btrfs_mark_buffer_dirty(leaf);
  6311. btrfs_drop_extent_cache(inode, key.offset,
  6312. key.offset + extent_len - 1, 0);
  6313. ret = btrfs_inc_extent_ref(trans, root,
  6314. new_extents[i].disk_bytenr,
  6315. new_extents[i].disk_num_bytes,
  6316. leaf->start,
  6317. root->root_key.objectid,
  6318. trans->transid, key.objectid);
  6319. BUG_ON(ret);
  6320. btrfs_release_path(root, path);
  6321. inode_add_bytes(inode, extent_len);
  6322. ext_offset = 0;
  6323. num_bytes -= extent_len;
  6324. key.offset += extent_len;
  6325. if (num_bytes == 0)
  6326. break;
  6327. }
  6328. BUG_ON(i >= nr_extents);
  6329. #endif
  6330. }
  6331. if (extent_locked) {
  6332. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  6333. lock_end, GFP_NOFS);
  6334. extent_locked = 0;
  6335. }
  6336. skip:
  6337. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  6338. key.offset >= search_end)
  6339. break;
  6340. cond_resched();
  6341. }
  6342. ret = 0;
  6343. out:
  6344. btrfs_release_path(root, path);
  6345. if (inode) {
  6346. mutex_unlock(&inode->i_mutex);
  6347. if (extent_locked) {
  6348. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  6349. lock_end, GFP_NOFS);
  6350. }
  6351. iput(inode);
  6352. }
  6353. return ret;
  6354. }
  6355. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  6356. struct btrfs_root *root,
  6357. struct extent_buffer *buf, u64 orig_start)
  6358. {
  6359. int level;
  6360. int ret;
  6361. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  6362. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6363. level = btrfs_header_level(buf);
  6364. if (level == 0) {
  6365. struct btrfs_leaf_ref *ref;
  6366. struct btrfs_leaf_ref *orig_ref;
  6367. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  6368. if (!orig_ref)
  6369. return -ENOENT;
  6370. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  6371. if (!ref) {
  6372. btrfs_free_leaf_ref(root, orig_ref);
  6373. return -ENOMEM;
  6374. }
  6375. ref->nritems = orig_ref->nritems;
  6376. memcpy(ref->extents, orig_ref->extents,
  6377. sizeof(ref->extents[0]) * ref->nritems);
  6378. btrfs_free_leaf_ref(root, orig_ref);
  6379. ref->root_gen = trans->transid;
  6380. ref->bytenr = buf->start;
  6381. ref->owner = btrfs_header_owner(buf);
  6382. ref->generation = btrfs_header_generation(buf);
  6383. ret = btrfs_add_leaf_ref(root, ref, 0);
  6384. WARN_ON(ret);
  6385. btrfs_free_leaf_ref(root, ref);
  6386. }
  6387. return 0;
  6388. }
  6389. static noinline int invalidate_extent_cache(struct btrfs_root *root,
  6390. struct extent_buffer *leaf,
  6391. struct btrfs_block_group_cache *group,
  6392. struct btrfs_root *target_root)
  6393. {
  6394. struct btrfs_key key;
  6395. struct inode *inode = NULL;
  6396. struct btrfs_file_extent_item *fi;
  6397. struct extent_state *cached_state = NULL;
  6398. u64 num_bytes;
  6399. u64 skip_objectid = 0;
  6400. u32 nritems;
  6401. u32 i;
  6402. nritems = btrfs_header_nritems(leaf);
  6403. for (i = 0; i < nritems; i++) {
  6404. btrfs_item_key_to_cpu(leaf, &key, i);
  6405. if (key.objectid == skip_objectid ||
  6406. key.type != BTRFS_EXTENT_DATA_KEY)
  6407. continue;
  6408. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  6409. if (btrfs_file_extent_type(leaf, fi) ==
  6410. BTRFS_FILE_EXTENT_INLINE)
  6411. continue;
  6412. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  6413. continue;
  6414. if (!inode || inode->i_ino != key.objectid) {
  6415. iput(inode);
  6416. inode = btrfs_ilookup(target_root->fs_info->sb,
  6417. key.objectid, target_root, 1);
  6418. }
  6419. if (!inode) {
  6420. skip_objectid = key.objectid;
  6421. continue;
  6422. }
  6423. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  6424. lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
  6425. key.offset + num_bytes - 1, 0, &cached_state,
  6426. GFP_NOFS);
  6427. btrfs_drop_extent_cache(inode, key.offset,
  6428. key.offset + num_bytes - 1, 1);
  6429. unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
  6430. key.offset + num_bytes - 1, &cached_state,
  6431. GFP_NOFS);
  6432. cond_resched();
  6433. }
  6434. iput(inode);
  6435. return 0;
  6436. }
  6437. static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  6438. struct btrfs_root *root,
  6439. struct extent_buffer *leaf,
  6440. struct btrfs_block_group_cache *group,
  6441. struct inode *reloc_inode)
  6442. {
  6443. struct btrfs_key key;
  6444. struct btrfs_key extent_key;
  6445. struct btrfs_file_extent_item *fi;
  6446. struct btrfs_leaf_ref *ref;
  6447. struct disk_extent *new_extent;
  6448. u64 bytenr;
  6449. u64 num_bytes;
  6450. u32 nritems;
  6451. u32 i;
  6452. int ext_index;
  6453. int nr_extent;
  6454. int ret;
  6455. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  6456. BUG_ON(!new_extent);
  6457. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  6458. BUG_ON(!ref);
  6459. ext_index = -1;
  6460. nritems = btrfs_header_nritems(leaf);
  6461. for (i = 0; i < nritems; i++) {
  6462. btrfs_item_key_to_cpu(leaf, &key, i);
  6463. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  6464. continue;
  6465. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  6466. if (btrfs_file_extent_type(leaf, fi) ==
  6467. BTRFS_FILE_EXTENT_INLINE)
  6468. continue;
  6469. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  6470. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  6471. if (bytenr == 0)
  6472. continue;
  6473. ext_index++;
  6474. if (bytenr >= group->key.objectid + group->key.offset ||
  6475. bytenr + num_bytes <= group->key.objectid)
  6476. continue;
  6477. extent_key.objectid = bytenr;
  6478. extent_key.offset = num_bytes;
  6479. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  6480. nr_extent = 1;
  6481. ret = get_new_locations(reloc_inode, &extent_key,
  6482. group->key.objectid, 1,
  6483. &new_extent, &nr_extent);
  6484. if (ret > 0)
  6485. continue;
  6486. BUG_ON(ret < 0);
  6487. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  6488. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  6489. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  6490. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  6491. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  6492. new_extent->disk_bytenr);
  6493. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  6494. new_extent->disk_num_bytes);
  6495. btrfs_mark_buffer_dirty(leaf);
  6496. ret = btrfs_inc_extent_ref(trans, root,
  6497. new_extent->disk_bytenr,
  6498. new_extent->disk_num_bytes,
  6499. leaf->start,
  6500. root->root_key.objectid,
  6501. trans->transid, key.objectid);
  6502. BUG_ON(ret);
  6503. ret = btrfs_free_extent(trans, root,
  6504. bytenr, num_bytes, leaf->start,
  6505. btrfs_header_owner(leaf),
  6506. btrfs_header_generation(leaf),
  6507. key.objectid, 0);
  6508. BUG_ON(ret);
  6509. cond_resched();
  6510. }
  6511. kfree(new_extent);
  6512. BUG_ON(ext_index + 1 != ref->nritems);
  6513. btrfs_free_leaf_ref(root, ref);
  6514. return 0;
  6515. }
  6516. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  6517. struct btrfs_root *root)
  6518. {
  6519. struct btrfs_root *reloc_root;
  6520. int ret;
  6521. if (root->reloc_root) {
  6522. reloc_root = root->reloc_root;
  6523. root->reloc_root = NULL;
  6524. list_add(&reloc_root->dead_list,
  6525. &root->fs_info->dead_reloc_roots);
  6526. btrfs_set_root_bytenr(&reloc_root->root_item,
  6527. reloc_root->node->start);
  6528. btrfs_set_root_level(&root->root_item,
  6529. btrfs_header_level(reloc_root->node));
  6530. memset(&reloc_root->root_item.drop_progress, 0,
  6531. sizeof(struct btrfs_disk_key));
  6532. reloc_root->root_item.drop_level = 0;
  6533. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  6534. &reloc_root->root_key,
  6535. &reloc_root->root_item);
  6536. BUG_ON(ret);
  6537. }
  6538. return 0;
  6539. }
  6540. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  6541. {
  6542. struct btrfs_trans_handle *trans;
  6543. struct btrfs_root *reloc_root;
  6544. struct btrfs_root *prev_root = NULL;
  6545. struct list_head dead_roots;
  6546. int ret;
  6547. unsigned long nr;
  6548. INIT_LIST_HEAD(&dead_roots);
  6549. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  6550. while (!list_empty(&dead_roots)) {
  6551. reloc_root = list_entry(dead_roots.prev,
  6552. struct btrfs_root, dead_list);
  6553. list_del_init(&reloc_root->dead_list);
  6554. BUG_ON(reloc_root->commit_root != NULL);
  6555. while (1) {
  6556. trans = btrfs_join_transaction(root, 1);
  6557. BUG_ON(!trans);
  6558. mutex_lock(&root->fs_info->drop_mutex);
  6559. ret = btrfs_drop_snapshot(trans, reloc_root);
  6560. if (ret != -EAGAIN)
  6561. break;
  6562. mutex_unlock(&root->fs_info->drop_mutex);
  6563. nr = trans->blocks_used;
  6564. ret = btrfs_end_transaction(trans, root);
  6565. BUG_ON(ret);
  6566. btrfs_btree_balance_dirty(root, nr);
  6567. }
  6568. free_extent_buffer(reloc_root->node);
  6569. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  6570. &reloc_root->root_key);
  6571. BUG_ON(ret);
  6572. mutex_unlock(&root->fs_info->drop_mutex);
  6573. nr = trans->blocks_used;
  6574. ret = btrfs_end_transaction(trans, root);
  6575. BUG_ON(ret);
  6576. btrfs_btree_balance_dirty(root, nr);
  6577. kfree(prev_root);
  6578. prev_root = reloc_root;
  6579. }
  6580. if (prev_root) {
  6581. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  6582. kfree(prev_root);
  6583. }
  6584. return 0;
  6585. }
  6586. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  6587. {
  6588. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  6589. return 0;
  6590. }
  6591. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  6592. {
  6593. struct btrfs_root *reloc_root;
  6594. struct btrfs_trans_handle *trans;
  6595. struct btrfs_key location;
  6596. int found;
  6597. int ret;
  6598. mutex_lock(&root->fs_info->tree_reloc_mutex);
  6599. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  6600. BUG_ON(ret);
  6601. found = !list_empty(&root->fs_info->dead_reloc_roots);
  6602. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  6603. if (found) {
  6604. trans = btrfs_start_transaction(root, 1);
  6605. BUG_ON(!trans);
  6606. ret = btrfs_commit_transaction(trans, root);
  6607. BUG_ON(ret);
  6608. }
  6609. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  6610. location.offset = (u64)-1;
  6611. location.type = BTRFS_ROOT_ITEM_KEY;
  6612. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  6613. BUG_ON(!reloc_root);
  6614. btrfs_orphan_cleanup(reloc_root);
  6615. return 0;
  6616. }
  6617. static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
  6618. struct btrfs_root *root)
  6619. {
  6620. struct btrfs_root *reloc_root;
  6621. struct extent_buffer *eb;
  6622. struct btrfs_root_item *root_item;
  6623. struct btrfs_key root_key;
  6624. int ret;
  6625. BUG_ON(!root->ref_cows);
  6626. if (root->reloc_root)
  6627. return 0;
  6628. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  6629. BUG_ON(!root_item);
  6630. ret = btrfs_copy_root(trans, root, root->commit_root,
  6631. &eb, BTRFS_TREE_RELOC_OBJECTID);
  6632. BUG_ON(ret);
  6633. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  6634. root_key.offset = root->root_key.objectid;
  6635. root_key.type = BTRFS_ROOT_ITEM_KEY;
  6636. memcpy(root_item, &root->root_item, sizeof(root_item));
  6637. btrfs_set_root_refs(root_item, 0);
  6638. btrfs_set_root_bytenr(root_item, eb->start);
  6639. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  6640. btrfs_set_root_generation(root_item, trans->transid);
  6641. btrfs_tree_unlock(eb);
  6642. free_extent_buffer(eb);
  6643. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  6644. &root_key, root_item);
  6645. BUG_ON(ret);
  6646. kfree(root_item);
  6647. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  6648. &root_key);
  6649. BUG_ON(!reloc_root);
  6650. reloc_root->last_trans = trans->transid;
  6651. reloc_root->commit_root = NULL;
  6652. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  6653. root->reloc_root = reloc_root;
  6654. return 0;
  6655. }
  6656. /*
  6657. * Core function of space balance.
  6658. *
  6659. * The idea is using reloc trees to relocate tree blocks in reference
  6660. * counted roots. There is one reloc tree for each subvol, and all
  6661. * reloc trees share same root key objectid. Reloc trees are snapshots
  6662. * of the latest committed roots of subvols (root->commit_root).
  6663. *
  6664. * To relocate a tree block referenced by a subvol, there are two steps.
  6665. * COW the block through subvol's reloc tree, then update block pointer
  6666. * in the subvol to point to the new block. Since all reloc trees share
  6667. * same root key objectid, doing special handing for tree blocks owned
  6668. * by them is easy. Once a tree block has been COWed in one reloc tree,
  6669. * we can use the resulting new block directly when the same block is
  6670. * required to COW again through other reloc trees. By this way, relocated
  6671. * tree blocks are shared between reloc trees, so they are also shared
  6672. * between subvols.
  6673. */
  6674. static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
  6675. struct btrfs_root *root,
  6676. struct btrfs_path *path,
  6677. struct btrfs_key *first_key,
  6678. struct btrfs_ref_path *ref_path,
  6679. struct btrfs_block_group_cache *group,
  6680. struct inode *reloc_inode)
  6681. {
  6682. struct btrfs_root *reloc_root;
  6683. struct extent_buffer *eb = NULL;
  6684. struct btrfs_key *keys;
  6685. u64 *nodes;
  6686. int level;
  6687. int shared_level;
  6688. int lowest_level = 0;
  6689. int ret;
  6690. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  6691. lowest_level = ref_path->owner_objectid;
  6692. if (!root->ref_cows) {
  6693. path->lowest_level = lowest_level;
  6694. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  6695. BUG_ON(ret < 0);
  6696. path->lowest_level = 0;
  6697. btrfs_release_path(root, path);
  6698. return 0;
  6699. }
  6700. mutex_lock(&root->fs_info->tree_reloc_mutex);
  6701. ret = init_reloc_tree(trans, root);
  6702. BUG_ON(ret);
  6703. reloc_root = root->reloc_root;
  6704. shared_level = ref_path->shared_level;
  6705. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  6706. keys = ref_path->node_keys;
  6707. nodes = ref_path->new_nodes;
  6708. memset(&keys[shared_level + 1], 0,
  6709. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  6710. memset(&nodes[shared_level + 1], 0,
  6711. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  6712. if (nodes[lowest_level] == 0) {
  6713. path->lowest_level = lowest_level;
  6714. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  6715. 0, 1);
  6716. BUG_ON(ret);
  6717. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  6718. eb = path->nodes[level];
  6719. if (!eb || eb == reloc_root->node)
  6720. break;
  6721. nodes[level] = eb->start;
  6722. if (level == 0)
  6723. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  6724. else
  6725. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  6726. }
  6727. if (nodes[0] &&
  6728. ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6729. eb = path->nodes[0];
  6730. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  6731. group, reloc_inode);
  6732. BUG_ON(ret);
  6733. }
  6734. btrfs_release_path(reloc_root, path);
  6735. } else {
  6736. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  6737. lowest_level);
  6738. BUG_ON(ret);
  6739. }
  6740. /*
  6741. * replace tree blocks in the fs tree with tree blocks in
  6742. * the reloc tree.
  6743. */
  6744. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  6745. BUG_ON(ret < 0);
  6746. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6747. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  6748. 0, 0);
  6749. BUG_ON(ret);
  6750. extent_buffer_get(path->nodes[0]);
  6751. eb = path->nodes[0];
  6752. btrfs_release_path(reloc_root, path);
  6753. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  6754. BUG_ON(ret);
  6755. free_extent_buffer(eb);
  6756. }
  6757. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  6758. path->lowest_level = 0;
  6759. return 0;
  6760. }
  6761. static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
  6762. struct btrfs_root *root,
  6763. struct btrfs_path *path,
  6764. struct btrfs_key *first_key,
  6765. struct btrfs_ref_path *ref_path)
  6766. {
  6767. int ret;
  6768. ret = relocate_one_path(trans, root, path, first_key,
  6769. ref_path, NULL, NULL);
  6770. BUG_ON(ret);
  6771. return 0;
  6772. }
  6773. static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
  6774. struct btrfs_root *extent_root,
  6775. struct btrfs_path *path,
  6776. struct btrfs_key *extent_key)
  6777. {
  6778. int ret;
  6779. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  6780. if (ret)
  6781. goto out;
  6782. ret = btrfs_del_item(trans, extent_root, path);
  6783. out:
  6784. btrfs_release_path(extent_root, path);
  6785. return ret;
  6786. }
  6787. static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
  6788. struct btrfs_ref_path *ref_path)
  6789. {
  6790. struct btrfs_key root_key;
  6791. root_key.objectid = ref_path->root_objectid;
  6792. root_key.type = BTRFS_ROOT_ITEM_KEY;
  6793. if (is_cowonly_root(ref_path->root_objectid))
  6794. root_key.offset = 0;
  6795. else
  6796. root_key.offset = (u64)-1;
  6797. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  6798. }
  6799. static noinline int relocate_one_extent(struct btrfs_root *extent_root,
  6800. struct btrfs_path *path,
  6801. struct btrfs_key *extent_key,
  6802. struct btrfs_block_group_cache *group,
  6803. struct inode *reloc_inode, int pass)
  6804. {
  6805. struct btrfs_trans_handle *trans;
  6806. struct btrfs_root *found_root;
  6807. struct btrfs_ref_path *ref_path = NULL;
  6808. struct disk_extent *new_extents = NULL;
  6809. int nr_extents = 0;
  6810. int loops;
  6811. int ret;
  6812. int level;
  6813. struct btrfs_key first_key;
  6814. u64 prev_block = 0;
  6815. trans = btrfs_start_transaction(extent_root, 1);
  6816. BUG_ON(!trans);
  6817. if (extent_key->objectid == 0) {
  6818. ret = del_extent_zero(trans, extent_root, path, extent_key);
  6819. goto out;
  6820. }
  6821. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  6822. if (!ref_path) {
  6823. ret = -ENOMEM;
  6824. goto out;
  6825. }
  6826. for (loops = 0; ; loops++) {
  6827. if (loops == 0) {
  6828. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  6829. extent_key->objectid);
  6830. } else {
  6831. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  6832. }
  6833. if (ret < 0)
  6834. goto out;
  6835. if (ret > 0)
  6836. break;
  6837. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  6838. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  6839. continue;
  6840. found_root = read_ref_root(extent_root->fs_info, ref_path);
  6841. BUG_ON(!found_root);
  6842. /*
  6843. * for reference counted tree, only process reference paths
  6844. * rooted at the latest committed root.
  6845. */
  6846. if (found_root->ref_cows &&
  6847. ref_path->root_generation != found_root->root_key.offset)
  6848. continue;
  6849. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6850. if (pass == 0) {
  6851. /*
  6852. * copy data extents to new locations
  6853. */
  6854. u64 group_start = group->key.objectid;
  6855. ret = relocate_data_extent(reloc_inode,
  6856. extent_key,
  6857. group_start);
  6858. if (ret < 0)
  6859. goto out;
  6860. break;
  6861. }
  6862. level = 0;
  6863. } else {
  6864. level = ref_path->owner_objectid;
  6865. }
  6866. if (prev_block != ref_path->nodes[level]) {
  6867. struct extent_buffer *eb;
  6868. u64 block_start = ref_path->nodes[level];
  6869. u64 block_size = btrfs_level_size(found_root, level);
  6870. eb = read_tree_block(found_root, block_start,
  6871. block_size, 0);
  6872. btrfs_tree_lock(eb);
  6873. BUG_ON(level != btrfs_header_level(eb));
  6874. if (level == 0)
  6875. btrfs_item_key_to_cpu(eb, &first_key, 0);
  6876. else
  6877. btrfs_node_key_to_cpu(eb, &first_key, 0);
  6878. btrfs_tree_unlock(eb);
  6879. free_extent_buffer(eb);
  6880. prev_block = block_start;
  6881. }
  6882. mutex_lock(&extent_root->fs_info->trans_mutex);
  6883. btrfs_record_root_in_trans(found_root);
  6884. mutex_unlock(&extent_root->fs_info->trans_mutex);
  6885. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6886. /*
  6887. * try to update data extent references while
  6888. * keeping metadata shared between snapshots.
  6889. */
  6890. if (pass == 1) {
  6891. ret = relocate_one_path(trans, found_root,
  6892. path, &first_key, ref_path,
  6893. group, reloc_inode);
  6894. if (ret < 0)
  6895. goto out;
  6896. continue;
  6897. }
  6898. /*
  6899. * use fallback method to process the remaining
  6900. * references.
  6901. */
  6902. if (!new_extents) {
  6903. u64 group_start = group->key.objectid;
  6904. new_extents = kmalloc(sizeof(*new_extents),
  6905. GFP_NOFS);
  6906. nr_extents = 1;
  6907. ret = get_new_locations(reloc_inode,
  6908. extent_key,
  6909. group_start, 1,
  6910. &new_extents,
  6911. &nr_extents);
  6912. if (ret)
  6913. goto out;
  6914. }
  6915. ret = replace_one_extent(trans, found_root,
  6916. path, extent_key,
  6917. &first_key, ref_path,
  6918. new_extents, nr_extents);
  6919. } else {
  6920. ret = relocate_tree_block(trans, found_root, path,
  6921. &first_key, ref_path);
  6922. }
  6923. if (ret < 0)
  6924. goto out;
  6925. }
  6926. ret = 0;
  6927. out:
  6928. btrfs_end_transaction(trans, extent_root);
  6929. kfree(new_extents);
  6930. kfree(ref_path);
  6931. return ret;
  6932. }
  6933. #endif
  6934. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6935. {
  6936. u64 num_devices;
  6937. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6938. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6939. num_devices = root->fs_info->fs_devices->rw_devices;
  6940. if (num_devices == 1) {
  6941. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6942. stripped = flags & ~stripped;
  6943. /* turn raid0 into single device chunks */
  6944. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6945. return stripped;
  6946. /* turn mirroring into duplication */
  6947. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6948. BTRFS_BLOCK_GROUP_RAID10))
  6949. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6950. return flags;
  6951. } else {
  6952. /* they already had raid on here, just return */
  6953. if (flags & stripped)
  6954. return flags;
  6955. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6956. stripped = flags & ~stripped;
  6957. /* switch duplicated blocks with raid1 */
  6958. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6959. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6960. /* turn single device chunks into raid0 */
  6961. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  6962. }
  6963. return flags;
  6964. }
  6965. static int set_block_group_ro(struct btrfs_block_group_cache *cache)
  6966. {
  6967. struct btrfs_space_info *sinfo = cache->space_info;
  6968. u64 num_bytes;
  6969. int ret = -ENOSPC;
  6970. if (cache->ro)
  6971. return 0;
  6972. spin_lock(&sinfo->lock);
  6973. spin_lock(&cache->lock);
  6974. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6975. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6976. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6977. sinfo->bytes_may_use + sinfo->bytes_readonly +
  6978. cache->reserved_pinned + num_bytes < sinfo->total_bytes) {
  6979. sinfo->bytes_readonly += num_bytes;
  6980. sinfo->bytes_reserved += cache->reserved_pinned;
  6981. cache->reserved_pinned = 0;
  6982. cache->ro = 1;
  6983. ret = 0;
  6984. }
  6985. spin_unlock(&cache->lock);
  6986. spin_unlock(&sinfo->lock);
  6987. return ret;
  6988. }
  6989. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6990. struct btrfs_block_group_cache *cache)
  6991. {
  6992. struct btrfs_trans_handle *trans;
  6993. u64 alloc_flags;
  6994. int ret;
  6995. BUG_ON(cache->ro);
  6996. trans = btrfs_join_transaction(root, 1);
  6997. BUG_ON(IS_ERR(trans));
  6998. alloc_flags = update_block_group_flags(root, cache->flags);
  6999. if (alloc_flags != cache->flags)
  7000. do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
  7001. ret = set_block_group_ro(cache);
  7002. if (!ret)
  7003. goto out;
  7004. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  7005. ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
  7006. if (ret < 0)
  7007. goto out;
  7008. ret = set_block_group_ro(cache);
  7009. out:
  7010. btrfs_end_transaction(trans, root);
  7011. return ret;
  7012. }
  7013. int btrfs_set_block_group_rw(struct btrfs_root *root,
  7014. struct btrfs_block_group_cache *cache)
  7015. {
  7016. struct btrfs_space_info *sinfo = cache->space_info;
  7017. u64 num_bytes;
  7018. BUG_ON(!cache->ro);
  7019. spin_lock(&sinfo->lock);
  7020. spin_lock(&cache->lock);
  7021. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7022. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7023. sinfo->bytes_readonly -= num_bytes;
  7024. cache->ro = 0;
  7025. spin_unlock(&cache->lock);
  7026. spin_unlock(&sinfo->lock);
  7027. return 0;
  7028. }
  7029. /*
  7030. * checks to see if its even possible to relocate this block group.
  7031. *
  7032. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  7033. * ok to go ahead and try.
  7034. */
  7035. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  7036. {
  7037. struct btrfs_block_group_cache *block_group;
  7038. struct btrfs_space_info *space_info;
  7039. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  7040. struct btrfs_device *device;
  7041. int full = 0;
  7042. int ret = 0;
  7043. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  7044. /* odd, couldn't find the block group, leave it alone */
  7045. if (!block_group)
  7046. return -1;
  7047. /* no bytes used, we're good */
  7048. if (!btrfs_block_group_used(&block_group->item))
  7049. goto out;
  7050. space_info = block_group->space_info;
  7051. spin_lock(&space_info->lock);
  7052. full = space_info->full;
  7053. /*
  7054. * if this is the last block group we have in this space, we can't
  7055. * relocate it unless we're able to allocate a new chunk below.
  7056. *
  7057. * Otherwise, we need to make sure we have room in the space to handle
  7058. * all of the extents from this block group. If we can, we're good
  7059. */
  7060. if ((space_info->total_bytes != block_group->key.offset) &&
  7061. (space_info->bytes_used + space_info->bytes_reserved +
  7062. space_info->bytes_pinned + space_info->bytes_readonly +
  7063. btrfs_block_group_used(&block_group->item) <
  7064. space_info->total_bytes)) {
  7065. spin_unlock(&space_info->lock);
  7066. goto out;
  7067. }
  7068. spin_unlock(&space_info->lock);
  7069. /*
  7070. * ok we don't have enough space, but maybe we have free space on our
  7071. * devices to allocate new chunks for relocation, so loop through our
  7072. * alloc devices and guess if we have enough space. However, if we
  7073. * were marked as full, then we know there aren't enough chunks, and we
  7074. * can just return.
  7075. */
  7076. ret = -1;
  7077. if (full)
  7078. goto out;
  7079. mutex_lock(&root->fs_info->chunk_mutex);
  7080. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7081. u64 min_free = btrfs_block_group_used(&block_group->item);
  7082. u64 dev_offset, max_avail;
  7083. /*
  7084. * check to make sure we can actually find a chunk with enough
  7085. * space to fit our block group in.
  7086. */
  7087. if (device->total_bytes > device->bytes_used + min_free) {
  7088. ret = find_free_dev_extent(NULL, device, min_free,
  7089. &dev_offset, &max_avail);
  7090. if (!ret)
  7091. break;
  7092. ret = -1;
  7093. }
  7094. }
  7095. mutex_unlock(&root->fs_info->chunk_mutex);
  7096. out:
  7097. btrfs_put_block_group(block_group);
  7098. return ret;
  7099. }
  7100. static int find_first_block_group(struct btrfs_root *root,
  7101. struct btrfs_path *path, struct btrfs_key *key)
  7102. {
  7103. int ret = 0;
  7104. struct btrfs_key found_key;
  7105. struct extent_buffer *leaf;
  7106. int slot;
  7107. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7108. if (ret < 0)
  7109. goto out;
  7110. while (1) {
  7111. slot = path->slots[0];
  7112. leaf = path->nodes[0];
  7113. if (slot >= btrfs_header_nritems(leaf)) {
  7114. ret = btrfs_next_leaf(root, path);
  7115. if (ret == 0)
  7116. continue;
  7117. if (ret < 0)
  7118. goto out;
  7119. break;
  7120. }
  7121. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  7122. if (found_key.objectid >= key->objectid &&
  7123. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  7124. ret = 0;
  7125. goto out;
  7126. }
  7127. path->slots[0]++;
  7128. }
  7129. out:
  7130. return ret;
  7131. }
  7132. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  7133. {
  7134. struct btrfs_block_group_cache *block_group;
  7135. u64 last = 0;
  7136. while (1) {
  7137. struct inode *inode;
  7138. block_group = btrfs_lookup_first_block_group(info, last);
  7139. while (block_group) {
  7140. spin_lock(&block_group->lock);
  7141. if (block_group->iref)
  7142. break;
  7143. spin_unlock(&block_group->lock);
  7144. block_group = next_block_group(info->tree_root,
  7145. block_group);
  7146. }
  7147. if (!block_group) {
  7148. if (last == 0)
  7149. break;
  7150. last = 0;
  7151. continue;
  7152. }
  7153. inode = block_group->inode;
  7154. block_group->iref = 0;
  7155. block_group->inode = NULL;
  7156. spin_unlock(&block_group->lock);
  7157. iput(inode);
  7158. last = block_group->key.objectid + block_group->key.offset;
  7159. btrfs_put_block_group(block_group);
  7160. }
  7161. }
  7162. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  7163. {
  7164. struct btrfs_block_group_cache *block_group;
  7165. struct btrfs_space_info *space_info;
  7166. struct btrfs_caching_control *caching_ctl;
  7167. struct rb_node *n;
  7168. down_write(&info->extent_commit_sem);
  7169. while (!list_empty(&info->caching_block_groups)) {
  7170. caching_ctl = list_entry(info->caching_block_groups.next,
  7171. struct btrfs_caching_control, list);
  7172. list_del(&caching_ctl->list);
  7173. put_caching_control(caching_ctl);
  7174. }
  7175. up_write(&info->extent_commit_sem);
  7176. spin_lock(&info->block_group_cache_lock);
  7177. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  7178. block_group = rb_entry(n, struct btrfs_block_group_cache,
  7179. cache_node);
  7180. rb_erase(&block_group->cache_node,
  7181. &info->block_group_cache_tree);
  7182. spin_unlock(&info->block_group_cache_lock);
  7183. down_write(&block_group->space_info->groups_sem);
  7184. list_del(&block_group->list);
  7185. up_write(&block_group->space_info->groups_sem);
  7186. if (block_group->cached == BTRFS_CACHE_STARTED)
  7187. wait_block_group_cache_done(block_group);
  7188. btrfs_remove_free_space_cache(block_group);
  7189. btrfs_put_block_group(block_group);
  7190. spin_lock(&info->block_group_cache_lock);
  7191. }
  7192. spin_unlock(&info->block_group_cache_lock);
  7193. /* now that all the block groups are freed, go through and
  7194. * free all the space_info structs. This is only called during
  7195. * the final stages of unmount, and so we know nobody is
  7196. * using them. We call synchronize_rcu() once before we start,
  7197. * just to be on the safe side.
  7198. */
  7199. synchronize_rcu();
  7200. release_global_block_rsv(info);
  7201. while(!list_empty(&info->space_info)) {
  7202. space_info = list_entry(info->space_info.next,
  7203. struct btrfs_space_info,
  7204. list);
  7205. if (space_info->bytes_pinned > 0 ||
  7206. space_info->bytes_reserved > 0) {
  7207. WARN_ON(1);
  7208. dump_space_info(space_info, 0, 0);
  7209. }
  7210. list_del(&space_info->list);
  7211. kfree(space_info);
  7212. }
  7213. return 0;
  7214. }
  7215. static void __link_block_group(struct btrfs_space_info *space_info,
  7216. struct btrfs_block_group_cache *cache)
  7217. {
  7218. int index = get_block_group_index(cache);
  7219. down_write(&space_info->groups_sem);
  7220. list_add_tail(&cache->list, &space_info->block_groups[index]);
  7221. up_write(&space_info->groups_sem);
  7222. }
  7223. int btrfs_read_block_groups(struct btrfs_root *root)
  7224. {
  7225. struct btrfs_path *path;
  7226. int ret;
  7227. struct btrfs_block_group_cache *cache;
  7228. struct btrfs_fs_info *info = root->fs_info;
  7229. struct btrfs_space_info *space_info;
  7230. struct btrfs_key key;
  7231. struct btrfs_key found_key;
  7232. struct extent_buffer *leaf;
  7233. int need_clear = 0;
  7234. u64 cache_gen;
  7235. root = info->extent_root;
  7236. key.objectid = 0;
  7237. key.offset = 0;
  7238. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  7239. path = btrfs_alloc_path();
  7240. if (!path)
  7241. return -ENOMEM;
  7242. cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
  7243. if (cache_gen != 0 &&
  7244. btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
  7245. need_clear = 1;
  7246. if (btrfs_test_opt(root, CLEAR_CACHE))
  7247. need_clear = 1;
  7248. if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
  7249. printk(KERN_INFO "btrfs: disk space caching is enabled\n");
  7250. while (1) {
  7251. ret = find_first_block_group(root, path, &key);
  7252. if (ret > 0)
  7253. break;
  7254. if (ret != 0)
  7255. goto error;
  7256. leaf = path->nodes[0];
  7257. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  7258. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7259. if (!cache) {
  7260. ret = -ENOMEM;
  7261. goto error;
  7262. }
  7263. atomic_set(&cache->count, 1);
  7264. spin_lock_init(&cache->lock);
  7265. spin_lock_init(&cache->tree_lock);
  7266. cache->fs_info = info;
  7267. INIT_LIST_HEAD(&cache->list);
  7268. INIT_LIST_HEAD(&cache->cluster_list);
  7269. if (need_clear)
  7270. cache->disk_cache_state = BTRFS_DC_CLEAR;
  7271. /*
  7272. * we only want to have 32k of ram per block group for keeping
  7273. * track of free space, and if we pass 1/2 of that we want to
  7274. * start converting things over to using bitmaps
  7275. */
  7276. cache->extents_thresh = ((1024 * 32) / 2) /
  7277. sizeof(struct btrfs_free_space);
  7278. read_extent_buffer(leaf, &cache->item,
  7279. btrfs_item_ptr_offset(leaf, path->slots[0]),
  7280. sizeof(cache->item));
  7281. memcpy(&cache->key, &found_key, sizeof(found_key));
  7282. key.objectid = found_key.objectid + found_key.offset;
  7283. btrfs_release_path(root, path);
  7284. cache->flags = btrfs_block_group_flags(&cache->item);
  7285. cache->sectorsize = root->sectorsize;
  7286. /*
  7287. * check for two cases, either we are full, and therefore
  7288. * don't need to bother with the caching work since we won't
  7289. * find any space, or we are empty, and we can just add all
  7290. * the space in and be done with it. This saves us _alot_ of
  7291. * time, particularly in the full case.
  7292. */
  7293. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  7294. exclude_super_stripes(root, cache);
  7295. cache->last_byte_to_unpin = (u64)-1;
  7296. cache->cached = BTRFS_CACHE_FINISHED;
  7297. free_excluded_extents(root, cache);
  7298. } else if (btrfs_block_group_used(&cache->item) == 0) {
  7299. exclude_super_stripes(root, cache);
  7300. cache->last_byte_to_unpin = (u64)-1;
  7301. cache->cached = BTRFS_CACHE_FINISHED;
  7302. add_new_free_space(cache, root->fs_info,
  7303. found_key.objectid,
  7304. found_key.objectid +
  7305. found_key.offset);
  7306. free_excluded_extents(root, cache);
  7307. }
  7308. ret = update_space_info(info, cache->flags, found_key.offset,
  7309. btrfs_block_group_used(&cache->item),
  7310. &space_info);
  7311. BUG_ON(ret);
  7312. cache->space_info = space_info;
  7313. spin_lock(&cache->space_info->lock);
  7314. cache->space_info->bytes_readonly += cache->bytes_super;
  7315. spin_unlock(&cache->space_info->lock);
  7316. __link_block_group(space_info, cache);
  7317. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7318. BUG_ON(ret);
  7319. set_avail_alloc_bits(root->fs_info, cache->flags);
  7320. if (btrfs_chunk_readonly(root, cache->key.objectid))
  7321. set_block_group_ro(cache);
  7322. }
  7323. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  7324. if (!(get_alloc_profile(root, space_info->flags) &
  7325. (BTRFS_BLOCK_GROUP_RAID10 |
  7326. BTRFS_BLOCK_GROUP_RAID1 |
  7327. BTRFS_BLOCK_GROUP_DUP)))
  7328. continue;
  7329. /*
  7330. * avoid allocating from un-mirrored block group if there are
  7331. * mirrored block groups.
  7332. */
  7333. list_for_each_entry(cache, &space_info->block_groups[3], list)
  7334. set_block_group_ro(cache);
  7335. list_for_each_entry(cache, &space_info->block_groups[4], list)
  7336. set_block_group_ro(cache);
  7337. }
  7338. init_global_block_rsv(info);
  7339. ret = 0;
  7340. error:
  7341. btrfs_free_path(path);
  7342. return ret;
  7343. }
  7344. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  7345. struct btrfs_root *root, u64 bytes_used,
  7346. u64 type, u64 chunk_objectid, u64 chunk_offset,
  7347. u64 size)
  7348. {
  7349. int ret;
  7350. struct btrfs_root *extent_root;
  7351. struct btrfs_block_group_cache *cache;
  7352. extent_root = root->fs_info->extent_root;
  7353. root->fs_info->last_trans_log_full_commit = trans->transid;
  7354. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7355. if (!cache)
  7356. return -ENOMEM;
  7357. cache->key.objectid = chunk_offset;
  7358. cache->key.offset = size;
  7359. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  7360. cache->sectorsize = root->sectorsize;
  7361. cache->fs_info = root->fs_info;
  7362. /*
  7363. * we only want to have 32k of ram per block group for keeping track
  7364. * of free space, and if we pass 1/2 of that we want to start
  7365. * converting things over to using bitmaps
  7366. */
  7367. cache->extents_thresh = ((1024 * 32) / 2) /
  7368. sizeof(struct btrfs_free_space);
  7369. atomic_set(&cache->count, 1);
  7370. spin_lock_init(&cache->lock);
  7371. spin_lock_init(&cache->tree_lock);
  7372. INIT_LIST_HEAD(&cache->list);
  7373. INIT_LIST_HEAD(&cache->cluster_list);
  7374. btrfs_set_block_group_used(&cache->item, bytes_used);
  7375. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  7376. cache->flags = type;
  7377. btrfs_set_block_group_flags(&cache->item, type);
  7378. cache->last_byte_to_unpin = (u64)-1;
  7379. cache->cached = BTRFS_CACHE_FINISHED;
  7380. exclude_super_stripes(root, cache);
  7381. add_new_free_space(cache, root->fs_info, chunk_offset,
  7382. chunk_offset + size);
  7383. free_excluded_extents(root, cache);
  7384. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  7385. &cache->space_info);
  7386. BUG_ON(ret);
  7387. spin_lock(&cache->space_info->lock);
  7388. cache->space_info->bytes_readonly += cache->bytes_super;
  7389. spin_unlock(&cache->space_info->lock);
  7390. __link_block_group(cache->space_info, cache);
  7391. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7392. BUG_ON(ret);
  7393. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  7394. sizeof(cache->item));
  7395. BUG_ON(ret);
  7396. set_avail_alloc_bits(extent_root->fs_info, type);
  7397. return 0;
  7398. }
  7399. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  7400. struct btrfs_root *root, u64 group_start)
  7401. {
  7402. struct btrfs_path *path;
  7403. struct btrfs_block_group_cache *block_group;
  7404. struct btrfs_free_cluster *cluster;
  7405. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7406. struct btrfs_key key;
  7407. struct inode *inode;
  7408. int ret;
  7409. int factor;
  7410. root = root->fs_info->extent_root;
  7411. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  7412. BUG_ON(!block_group);
  7413. BUG_ON(!block_group->ro);
  7414. memcpy(&key, &block_group->key, sizeof(key));
  7415. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7416. BTRFS_BLOCK_GROUP_RAID1 |
  7417. BTRFS_BLOCK_GROUP_RAID10))
  7418. factor = 2;
  7419. else
  7420. factor = 1;
  7421. /* make sure this block group isn't part of an allocation cluster */
  7422. cluster = &root->fs_info->data_alloc_cluster;
  7423. spin_lock(&cluster->refill_lock);
  7424. btrfs_return_cluster_to_free_space(block_group, cluster);
  7425. spin_unlock(&cluster->refill_lock);
  7426. /*
  7427. * make sure this block group isn't part of a metadata
  7428. * allocation cluster
  7429. */
  7430. cluster = &root->fs_info->meta_alloc_cluster;
  7431. spin_lock(&cluster->refill_lock);
  7432. btrfs_return_cluster_to_free_space(block_group, cluster);
  7433. spin_unlock(&cluster->refill_lock);
  7434. path = btrfs_alloc_path();
  7435. BUG_ON(!path);
  7436. inode = lookup_free_space_inode(root, block_group, path);
  7437. if (!IS_ERR(inode)) {
  7438. btrfs_orphan_add(trans, inode);
  7439. clear_nlink(inode);
  7440. /* One for the block groups ref */
  7441. spin_lock(&block_group->lock);
  7442. if (block_group->iref) {
  7443. block_group->iref = 0;
  7444. block_group->inode = NULL;
  7445. spin_unlock(&block_group->lock);
  7446. iput(inode);
  7447. } else {
  7448. spin_unlock(&block_group->lock);
  7449. }
  7450. /* One for our lookup ref */
  7451. iput(inode);
  7452. }
  7453. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7454. key.offset = block_group->key.objectid;
  7455. key.type = 0;
  7456. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7457. if (ret < 0)
  7458. goto out;
  7459. if (ret > 0)
  7460. btrfs_release_path(tree_root, path);
  7461. if (ret == 0) {
  7462. ret = btrfs_del_item(trans, tree_root, path);
  7463. if (ret)
  7464. goto out;
  7465. btrfs_release_path(tree_root, path);
  7466. }
  7467. spin_lock(&root->fs_info->block_group_cache_lock);
  7468. rb_erase(&block_group->cache_node,
  7469. &root->fs_info->block_group_cache_tree);
  7470. spin_unlock(&root->fs_info->block_group_cache_lock);
  7471. down_write(&block_group->space_info->groups_sem);
  7472. /*
  7473. * we must use list_del_init so people can check to see if they
  7474. * are still on the list after taking the semaphore
  7475. */
  7476. list_del_init(&block_group->list);
  7477. up_write(&block_group->space_info->groups_sem);
  7478. if (block_group->cached == BTRFS_CACHE_STARTED)
  7479. wait_block_group_cache_done(block_group);
  7480. btrfs_remove_free_space_cache(block_group);
  7481. spin_lock(&block_group->space_info->lock);
  7482. block_group->space_info->total_bytes -= block_group->key.offset;
  7483. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7484. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7485. spin_unlock(&block_group->space_info->lock);
  7486. memcpy(&key, &block_group->key, sizeof(key));
  7487. btrfs_clear_space_info_full(root->fs_info);
  7488. btrfs_put_block_group(block_group);
  7489. btrfs_put_block_group(block_group);
  7490. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7491. if (ret > 0)
  7492. ret = -EIO;
  7493. if (ret < 0)
  7494. goto out;
  7495. ret = btrfs_del_item(trans, root, path);
  7496. out:
  7497. btrfs_free_path(path);
  7498. return ret;
  7499. }