extent-tree.c 213 KB

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