extent-tree.c 236 KB

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