extent-tree.c 233 KB

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