extent-tree.c 302 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/sched/signal.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/writeback.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/sort.h>
  11. #include <linux/rcupdate.h>
  12. #include <linux/kthread.h>
  13. #include <linux/slab.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/percpu_counter.h>
  16. #include <linux/lockdep.h>
  17. #include <linux/crc32c.h>
  18. #include "tree-log.h"
  19. #include "disk-io.h"
  20. #include "print-tree.h"
  21. #include "volumes.h"
  22. #include "raid56.h"
  23. #include "locking.h"
  24. #include "free-space-cache.h"
  25. #include "free-space-tree.h"
  26. #include "math.h"
  27. #include "sysfs.h"
  28. #include "qgroup.h"
  29. #include "ref-verify.h"
  30. #undef SCRAMBLE_DELAYED_REFS
  31. /*
  32. * control flags for do_chunk_alloc's force field
  33. * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
  34. * if we really need one.
  35. *
  36. * CHUNK_ALLOC_LIMITED means to only try and allocate one
  37. * if we have very few chunks already allocated. This is
  38. * used as part of the clustering code to help make sure
  39. * we have a good pool of storage to cluster in, without
  40. * filling the FS with empty chunks
  41. *
  42. * CHUNK_ALLOC_FORCE means it must try to allocate one
  43. *
  44. */
  45. enum {
  46. CHUNK_ALLOC_NO_FORCE = 0,
  47. CHUNK_ALLOC_LIMITED = 1,
  48. CHUNK_ALLOC_FORCE = 2,
  49. };
  50. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  51. struct btrfs_delayed_ref_node *node, u64 parent,
  52. u64 root_objectid, u64 owner_objectid,
  53. u64 owner_offset, int refs_to_drop,
  54. struct btrfs_delayed_extent_op *extra_op);
  55. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  56. struct extent_buffer *leaf,
  57. struct btrfs_extent_item *ei);
  58. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  59. u64 parent, u64 root_objectid,
  60. u64 flags, u64 owner, u64 offset,
  61. struct btrfs_key *ins, int ref_mod);
  62. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  63. struct btrfs_delayed_ref_node *node,
  64. struct btrfs_delayed_extent_op *extent_op);
  65. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  66. struct btrfs_fs_info *fs_info, u64 flags,
  67. int force);
  68. static int find_next_key(struct btrfs_path *path, int level,
  69. struct btrfs_key *key);
  70. static void dump_space_info(struct btrfs_fs_info *fs_info,
  71. struct btrfs_space_info *info, u64 bytes,
  72. int dump_block_groups);
  73. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  74. u64 num_bytes);
  75. static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
  76. struct btrfs_space_info *space_info,
  77. u64 num_bytes);
  78. static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
  79. struct btrfs_space_info *space_info,
  80. u64 num_bytes);
  81. static noinline int
  82. block_group_cache_done(struct btrfs_block_group_cache *cache)
  83. {
  84. smp_mb();
  85. return cache->cached == BTRFS_CACHE_FINISHED ||
  86. cache->cached == BTRFS_CACHE_ERROR;
  87. }
  88. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  89. {
  90. return (cache->flags & bits) == bits;
  91. }
  92. void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
  93. {
  94. atomic_inc(&cache->count);
  95. }
  96. void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
  97. {
  98. if (atomic_dec_and_test(&cache->count)) {
  99. WARN_ON(cache->pinned > 0);
  100. WARN_ON(cache->reserved > 0);
  101. /*
  102. * If not empty, someone is still holding mutex of
  103. * full_stripe_lock, which can only be released by caller.
  104. * And it will definitely cause use-after-free when caller
  105. * tries to release full stripe lock.
  106. *
  107. * No better way to resolve, but only to warn.
  108. */
  109. WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
  110. kfree(cache->free_space_ctl);
  111. kfree(cache);
  112. }
  113. }
  114. /*
  115. * this adds the block group to the fs_info rb tree for the block group
  116. * cache
  117. */
  118. static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
  119. struct btrfs_block_group_cache *block_group)
  120. {
  121. struct rb_node **p;
  122. struct rb_node *parent = NULL;
  123. struct btrfs_block_group_cache *cache;
  124. spin_lock(&info->block_group_cache_lock);
  125. p = &info->block_group_cache_tree.rb_node;
  126. while (*p) {
  127. parent = *p;
  128. cache = rb_entry(parent, struct btrfs_block_group_cache,
  129. cache_node);
  130. if (block_group->key.objectid < cache->key.objectid) {
  131. p = &(*p)->rb_left;
  132. } else if (block_group->key.objectid > cache->key.objectid) {
  133. p = &(*p)->rb_right;
  134. } else {
  135. spin_unlock(&info->block_group_cache_lock);
  136. return -EEXIST;
  137. }
  138. }
  139. rb_link_node(&block_group->cache_node, parent, p);
  140. rb_insert_color(&block_group->cache_node,
  141. &info->block_group_cache_tree);
  142. if (info->first_logical_byte > block_group->key.objectid)
  143. info->first_logical_byte = block_group->key.objectid;
  144. spin_unlock(&info->block_group_cache_lock);
  145. return 0;
  146. }
  147. /*
  148. * This will return the block group at or after bytenr if contains is 0, else
  149. * it will return the block group that contains the bytenr
  150. */
  151. static struct btrfs_block_group_cache *
  152. block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
  153. int contains)
  154. {
  155. struct btrfs_block_group_cache *cache, *ret = NULL;
  156. struct rb_node *n;
  157. u64 end, start;
  158. spin_lock(&info->block_group_cache_lock);
  159. n = info->block_group_cache_tree.rb_node;
  160. while (n) {
  161. cache = rb_entry(n, struct btrfs_block_group_cache,
  162. cache_node);
  163. end = cache->key.objectid + cache->key.offset - 1;
  164. start = cache->key.objectid;
  165. if (bytenr < start) {
  166. if (!contains && (!ret || start < ret->key.objectid))
  167. ret = cache;
  168. n = n->rb_left;
  169. } else if (bytenr > start) {
  170. if (contains && bytenr <= end) {
  171. ret = cache;
  172. break;
  173. }
  174. n = n->rb_right;
  175. } else {
  176. ret = cache;
  177. break;
  178. }
  179. }
  180. if (ret) {
  181. btrfs_get_block_group(ret);
  182. if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
  183. info->first_logical_byte = ret->key.objectid;
  184. }
  185. spin_unlock(&info->block_group_cache_lock);
  186. return ret;
  187. }
  188. static int add_excluded_extent(struct btrfs_fs_info *fs_info,
  189. u64 start, u64 num_bytes)
  190. {
  191. u64 end = start + num_bytes - 1;
  192. set_extent_bits(&fs_info->freed_extents[0],
  193. start, end, EXTENT_UPTODATE);
  194. set_extent_bits(&fs_info->freed_extents[1],
  195. start, end, EXTENT_UPTODATE);
  196. return 0;
  197. }
  198. static void free_excluded_extents(struct btrfs_fs_info *fs_info,
  199. struct btrfs_block_group_cache *cache)
  200. {
  201. u64 start, end;
  202. start = cache->key.objectid;
  203. end = start + cache->key.offset - 1;
  204. clear_extent_bits(&fs_info->freed_extents[0],
  205. start, end, EXTENT_UPTODATE);
  206. clear_extent_bits(&fs_info->freed_extents[1],
  207. start, end, EXTENT_UPTODATE);
  208. }
  209. static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
  210. struct btrfs_block_group_cache *cache)
  211. {
  212. u64 bytenr;
  213. u64 *logical;
  214. int stripe_len;
  215. int i, nr, ret;
  216. if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
  217. stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
  218. cache->bytes_super += stripe_len;
  219. ret = add_excluded_extent(fs_info, cache->key.objectid,
  220. stripe_len);
  221. if (ret)
  222. return ret;
  223. }
  224. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  225. bytenr = btrfs_sb_offset(i);
  226. ret = btrfs_rmap_block(fs_info, cache->key.objectid,
  227. bytenr, &logical, &nr, &stripe_len);
  228. if (ret)
  229. return ret;
  230. while (nr--) {
  231. u64 start, len;
  232. if (logical[nr] > cache->key.objectid +
  233. cache->key.offset)
  234. continue;
  235. if (logical[nr] + stripe_len <= cache->key.objectid)
  236. continue;
  237. start = logical[nr];
  238. if (start < cache->key.objectid) {
  239. start = cache->key.objectid;
  240. len = (logical[nr] + stripe_len) - start;
  241. } else {
  242. len = min_t(u64, stripe_len,
  243. cache->key.objectid +
  244. cache->key.offset - start);
  245. }
  246. cache->bytes_super += len;
  247. ret = add_excluded_extent(fs_info, start, len);
  248. if (ret) {
  249. kfree(logical);
  250. return ret;
  251. }
  252. }
  253. kfree(logical);
  254. }
  255. return 0;
  256. }
  257. static struct btrfs_caching_control *
  258. get_caching_control(struct btrfs_block_group_cache *cache)
  259. {
  260. struct btrfs_caching_control *ctl;
  261. spin_lock(&cache->lock);
  262. if (!cache->caching_ctl) {
  263. spin_unlock(&cache->lock);
  264. return NULL;
  265. }
  266. ctl = cache->caching_ctl;
  267. refcount_inc(&ctl->count);
  268. spin_unlock(&cache->lock);
  269. return ctl;
  270. }
  271. static void put_caching_control(struct btrfs_caching_control *ctl)
  272. {
  273. if (refcount_dec_and_test(&ctl->count))
  274. kfree(ctl);
  275. }
  276. #ifdef CONFIG_BTRFS_DEBUG
  277. static void fragment_free_space(struct btrfs_block_group_cache *block_group)
  278. {
  279. struct btrfs_fs_info *fs_info = block_group->fs_info;
  280. u64 start = block_group->key.objectid;
  281. u64 len = block_group->key.offset;
  282. u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
  283. fs_info->nodesize : fs_info->sectorsize;
  284. u64 step = chunk << 1;
  285. while (len > chunk) {
  286. btrfs_remove_free_space(block_group, start, chunk);
  287. start += step;
  288. if (len < step)
  289. len = 0;
  290. else
  291. len -= step;
  292. }
  293. }
  294. #endif
  295. /*
  296. * this is only called by cache_block_group, since we could have freed extents
  297. * we need to check the pinned_extents for any extents that can't be used yet
  298. * since their free space will be released as soon as the transaction commits.
  299. */
  300. u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
  301. u64 start, u64 end)
  302. {
  303. struct btrfs_fs_info *info = block_group->fs_info;
  304. u64 extent_start, extent_end, size, total_added = 0;
  305. int ret;
  306. while (start < end) {
  307. ret = find_first_extent_bit(info->pinned_extents, start,
  308. &extent_start, &extent_end,
  309. EXTENT_DIRTY | EXTENT_UPTODATE,
  310. NULL);
  311. if (ret)
  312. break;
  313. if (extent_start <= start) {
  314. start = extent_end + 1;
  315. } else if (extent_start > start && extent_start < end) {
  316. size = extent_start - start;
  317. total_added += size;
  318. ret = btrfs_add_free_space(block_group, start,
  319. size);
  320. BUG_ON(ret); /* -ENOMEM or logic error */
  321. start = extent_end + 1;
  322. } else {
  323. break;
  324. }
  325. }
  326. if (start < end) {
  327. size = end - start;
  328. total_added += size;
  329. ret = btrfs_add_free_space(block_group, start, size);
  330. BUG_ON(ret); /* -ENOMEM or logic error */
  331. }
  332. return total_added;
  333. }
  334. static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
  335. {
  336. struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
  337. struct btrfs_fs_info *fs_info = block_group->fs_info;
  338. struct btrfs_root *extent_root = fs_info->extent_root;
  339. struct btrfs_path *path;
  340. struct extent_buffer *leaf;
  341. struct btrfs_key key;
  342. u64 total_found = 0;
  343. u64 last = 0;
  344. u32 nritems;
  345. int ret;
  346. bool wakeup = true;
  347. path = btrfs_alloc_path();
  348. if (!path)
  349. return -ENOMEM;
  350. last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
  351. #ifdef CONFIG_BTRFS_DEBUG
  352. /*
  353. * If we're fragmenting we don't want to make anybody think we can
  354. * allocate from this block group until we've had a chance to fragment
  355. * the free space.
  356. */
  357. if (btrfs_should_fragment_free_space(block_group))
  358. wakeup = false;
  359. #endif
  360. /*
  361. * We don't want to deadlock with somebody trying to allocate a new
  362. * extent for the extent root while also trying to search the extent
  363. * root to add free space. So we skip locking and search the commit
  364. * root, since its read-only
  365. */
  366. path->skip_locking = 1;
  367. path->search_commit_root = 1;
  368. path->reada = READA_FORWARD;
  369. key.objectid = last;
  370. key.offset = 0;
  371. key.type = BTRFS_EXTENT_ITEM_KEY;
  372. next:
  373. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  374. if (ret < 0)
  375. goto out;
  376. leaf = path->nodes[0];
  377. nritems = btrfs_header_nritems(leaf);
  378. while (1) {
  379. if (btrfs_fs_closing(fs_info) > 1) {
  380. last = (u64)-1;
  381. break;
  382. }
  383. if (path->slots[0] < nritems) {
  384. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  385. } else {
  386. ret = find_next_key(path, 0, &key);
  387. if (ret)
  388. break;
  389. if (need_resched() ||
  390. rwsem_is_contended(&fs_info->commit_root_sem)) {
  391. if (wakeup)
  392. caching_ctl->progress = last;
  393. btrfs_release_path(path);
  394. up_read(&fs_info->commit_root_sem);
  395. mutex_unlock(&caching_ctl->mutex);
  396. cond_resched();
  397. mutex_lock(&caching_ctl->mutex);
  398. down_read(&fs_info->commit_root_sem);
  399. goto next;
  400. }
  401. ret = btrfs_next_leaf(extent_root, path);
  402. if (ret < 0)
  403. goto out;
  404. if (ret)
  405. break;
  406. leaf = path->nodes[0];
  407. nritems = btrfs_header_nritems(leaf);
  408. continue;
  409. }
  410. if (key.objectid < last) {
  411. key.objectid = last;
  412. key.offset = 0;
  413. key.type = BTRFS_EXTENT_ITEM_KEY;
  414. if (wakeup)
  415. caching_ctl->progress = last;
  416. btrfs_release_path(path);
  417. goto next;
  418. }
  419. if (key.objectid < block_group->key.objectid) {
  420. path->slots[0]++;
  421. continue;
  422. }
  423. if (key.objectid >= block_group->key.objectid +
  424. block_group->key.offset)
  425. break;
  426. if (key.type == BTRFS_EXTENT_ITEM_KEY ||
  427. key.type == BTRFS_METADATA_ITEM_KEY) {
  428. total_found += add_new_free_space(block_group, last,
  429. key.objectid);
  430. if (key.type == BTRFS_METADATA_ITEM_KEY)
  431. last = key.objectid +
  432. fs_info->nodesize;
  433. else
  434. last = key.objectid + key.offset;
  435. if (total_found > CACHING_CTL_WAKE_UP) {
  436. total_found = 0;
  437. if (wakeup)
  438. wake_up(&caching_ctl->wait);
  439. }
  440. }
  441. path->slots[0]++;
  442. }
  443. ret = 0;
  444. total_found += add_new_free_space(block_group, last,
  445. block_group->key.objectid +
  446. block_group->key.offset);
  447. caching_ctl->progress = (u64)-1;
  448. out:
  449. btrfs_free_path(path);
  450. return ret;
  451. }
  452. static noinline void caching_thread(struct btrfs_work *work)
  453. {
  454. struct btrfs_block_group_cache *block_group;
  455. struct btrfs_fs_info *fs_info;
  456. struct btrfs_caching_control *caching_ctl;
  457. int ret;
  458. caching_ctl = container_of(work, struct btrfs_caching_control, work);
  459. block_group = caching_ctl->block_group;
  460. fs_info = block_group->fs_info;
  461. mutex_lock(&caching_ctl->mutex);
  462. down_read(&fs_info->commit_root_sem);
  463. if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
  464. ret = load_free_space_tree(caching_ctl);
  465. else
  466. ret = load_extent_tree_free(caching_ctl);
  467. spin_lock(&block_group->lock);
  468. block_group->caching_ctl = NULL;
  469. block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
  470. spin_unlock(&block_group->lock);
  471. #ifdef CONFIG_BTRFS_DEBUG
  472. if (btrfs_should_fragment_free_space(block_group)) {
  473. u64 bytes_used;
  474. spin_lock(&block_group->space_info->lock);
  475. spin_lock(&block_group->lock);
  476. bytes_used = block_group->key.offset -
  477. btrfs_block_group_used(&block_group->item);
  478. block_group->space_info->bytes_used += bytes_used >> 1;
  479. spin_unlock(&block_group->lock);
  480. spin_unlock(&block_group->space_info->lock);
  481. fragment_free_space(block_group);
  482. }
  483. #endif
  484. caching_ctl->progress = (u64)-1;
  485. up_read(&fs_info->commit_root_sem);
  486. free_excluded_extents(fs_info, block_group);
  487. mutex_unlock(&caching_ctl->mutex);
  488. wake_up(&caching_ctl->wait);
  489. put_caching_control(caching_ctl);
  490. btrfs_put_block_group(block_group);
  491. }
  492. static int cache_block_group(struct btrfs_block_group_cache *cache,
  493. int load_cache_only)
  494. {
  495. DEFINE_WAIT(wait);
  496. struct btrfs_fs_info *fs_info = cache->fs_info;
  497. struct btrfs_caching_control *caching_ctl;
  498. int ret = 0;
  499. caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
  500. if (!caching_ctl)
  501. return -ENOMEM;
  502. INIT_LIST_HEAD(&caching_ctl->list);
  503. mutex_init(&caching_ctl->mutex);
  504. init_waitqueue_head(&caching_ctl->wait);
  505. caching_ctl->block_group = cache;
  506. caching_ctl->progress = cache->key.objectid;
  507. refcount_set(&caching_ctl->count, 1);
  508. btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
  509. caching_thread, NULL, NULL);
  510. spin_lock(&cache->lock);
  511. /*
  512. * This should be a rare occasion, but this could happen I think in the
  513. * case where one thread starts to load the space cache info, and then
  514. * some other thread starts a transaction commit which tries to do an
  515. * allocation while the other thread is still loading the space cache
  516. * info. The previous loop should have kept us from choosing this block
  517. * group, but if we've moved to the state where we will wait on caching
  518. * block groups we need to first check if we're doing a fast load here,
  519. * so we can wait for it to finish, otherwise we could end up allocating
  520. * from a block group who's cache gets evicted for one reason or
  521. * another.
  522. */
  523. while (cache->cached == BTRFS_CACHE_FAST) {
  524. struct btrfs_caching_control *ctl;
  525. ctl = cache->caching_ctl;
  526. refcount_inc(&ctl->count);
  527. prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
  528. spin_unlock(&cache->lock);
  529. schedule();
  530. finish_wait(&ctl->wait, &wait);
  531. put_caching_control(ctl);
  532. spin_lock(&cache->lock);
  533. }
  534. if (cache->cached != BTRFS_CACHE_NO) {
  535. spin_unlock(&cache->lock);
  536. kfree(caching_ctl);
  537. return 0;
  538. }
  539. WARN_ON(cache->caching_ctl);
  540. cache->caching_ctl = caching_ctl;
  541. cache->cached = BTRFS_CACHE_FAST;
  542. spin_unlock(&cache->lock);
  543. if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
  544. mutex_lock(&caching_ctl->mutex);
  545. ret = load_free_space_cache(fs_info, cache);
  546. spin_lock(&cache->lock);
  547. if (ret == 1) {
  548. cache->caching_ctl = NULL;
  549. cache->cached = BTRFS_CACHE_FINISHED;
  550. cache->last_byte_to_unpin = (u64)-1;
  551. caching_ctl->progress = (u64)-1;
  552. } else {
  553. if (load_cache_only) {
  554. cache->caching_ctl = NULL;
  555. cache->cached = BTRFS_CACHE_NO;
  556. } else {
  557. cache->cached = BTRFS_CACHE_STARTED;
  558. cache->has_caching_ctl = 1;
  559. }
  560. }
  561. spin_unlock(&cache->lock);
  562. #ifdef CONFIG_BTRFS_DEBUG
  563. if (ret == 1 &&
  564. btrfs_should_fragment_free_space(cache)) {
  565. u64 bytes_used;
  566. spin_lock(&cache->space_info->lock);
  567. spin_lock(&cache->lock);
  568. bytes_used = cache->key.offset -
  569. btrfs_block_group_used(&cache->item);
  570. cache->space_info->bytes_used += bytes_used >> 1;
  571. spin_unlock(&cache->lock);
  572. spin_unlock(&cache->space_info->lock);
  573. fragment_free_space(cache);
  574. }
  575. #endif
  576. mutex_unlock(&caching_ctl->mutex);
  577. wake_up(&caching_ctl->wait);
  578. if (ret == 1) {
  579. put_caching_control(caching_ctl);
  580. free_excluded_extents(fs_info, cache);
  581. return 0;
  582. }
  583. } else {
  584. /*
  585. * We're either using the free space tree or no caching at all.
  586. * Set cached to the appropriate value and wakeup any waiters.
  587. */
  588. spin_lock(&cache->lock);
  589. if (load_cache_only) {
  590. cache->caching_ctl = NULL;
  591. cache->cached = BTRFS_CACHE_NO;
  592. } else {
  593. cache->cached = BTRFS_CACHE_STARTED;
  594. cache->has_caching_ctl = 1;
  595. }
  596. spin_unlock(&cache->lock);
  597. wake_up(&caching_ctl->wait);
  598. }
  599. if (load_cache_only) {
  600. put_caching_control(caching_ctl);
  601. return 0;
  602. }
  603. down_write(&fs_info->commit_root_sem);
  604. refcount_inc(&caching_ctl->count);
  605. list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
  606. up_write(&fs_info->commit_root_sem);
  607. btrfs_get_block_group(cache);
  608. btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
  609. return ret;
  610. }
  611. /*
  612. * return the block group that starts at or after bytenr
  613. */
  614. static struct btrfs_block_group_cache *
  615. btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
  616. {
  617. return block_group_cache_tree_search(info, bytenr, 0);
  618. }
  619. /*
  620. * return the block group that contains the given bytenr
  621. */
  622. struct btrfs_block_group_cache *btrfs_lookup_block_group(
  623. struct btrfs_fs_info *info,
  624. u64 bytenr)
  625. {
  626. return block_group_cache_tree_search(info, bytenr, 1);
  627. }
  628. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  629. u64 flags)
  630. {
  631. struct list_head *head = &info->space_info;
  632. struct btrfs_space_info *found;
  633. flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
  634. rcu_read_lock();
  635. list_for_each_entry_rcu(found, head, list) {
  636. if (found->flags & flags) {
  637. rcu_read_unlock();
  638. return found;
  639. }
  640. }
  641. rcu_read_unlock();
  642. return NULL;
  643. }
  644. static void add_pinned_bytes(struct btrfs_fs_info *fs_info, s64 num_bytes,
  645. bool metadata, u64 root_objectid)
  646. {
  647. struct btrfs_space_info *space_info;
  648. u64 flags;
  649. if (metadata) {
  650. if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
  651. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  652. else
  653. flags = BTRFS_BLOCK_GROUP_METADATA;
  654. } else {
  655. flags = BTRFS_BLOCK_GROUP_DATA;
  656. }
  657. space_info = __find_space_info(fs_info, flags);
  658. ASSERT(space_info);
  659. percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
  660. }
  661. /*
  662. * after adding space to the filesystem, we need to clear the full flags
  663. * on all the space infos.
  664. */
  665. void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
  666. {
  667. struct list_head *head = &info->space_info;
  668. struct btrfs_space_info *found;
  669. rcu_read_lock();
  670. list_for_each_entry_rcu(found, head, list)
  671. found->full = 0;
  672. rcu_read_unlock();
  673. }
  674. /* simple helper to search for an existing data extent at a given offset */
  675. int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
  676. {
  677. int ret;
  678. struct btrfs_key key;
  679. struct btrfs_path *path;
  680. path = btrfs_alloc_path();
  681. if (!path)
  682. return -ENOMEM;
  683. key.objectid = start;
  684. key.offset = len;
  685. key.type = BTRFS_EXTENT_ITEM_KEY;
  686. ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
  687. btrfs_free_path(path);
  688. return ret;
  689. }
  690. /*
  691. * helper function to lookup reference count and flags of a tree block.
  692. *
  693. * the head node for delayed ref is used to store the sum of all the
  694. * reference count modifications queued up in the rbtree. the head
  695. * node may also store the extent flags to set. This way you can check
  696. * to see what the reference count and extent flags would be if all of
  697. * the delayed refs are not processed.
  698. */
  699. int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
  700. struct btrfs_fs_info *fs_info, u64 bytenr,
  701. u64 offset, int metadata, u64 *refs, u64 *flags)
  702. {
  703. struct btrfs_delayed_ref_head *head;
  704. struct btrfs_delayed_ref_root *delayed_refs;
  705. struct btrfs_path *path;
  706. struct btrfs_extent_item *ei;
  707. struct extent_buffer *leaf;
  708. struct btrfs_key key;
  709. u32 item_size;
  710. u64 num_refs;
  711. u64 extent_flags;
  712. int ret;
  713. /*
  714. * If we don't have skinny metadata, don't bother doing anything
  715. * different
  716. */
  717. if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
  718. offset = fs_info->nodesize;
  719. metadata = 0;
  720. }
  721. path = btrfs_alloc_path();
  722. if (!path)
  723. return -ENOMEM;
  724. if (!trans) {
  725. path->skip_locking = 1;
  726. path->search_commit_root = 1;
  727. }
  728. search_again:
  729. key.objectid = bytenr;
  730. key.offset = offset;
  731. if (metadata)
  732. key.type = BTRFS_METADATA_ITEM_KEY;
  733. else
  734. key.type = BTRFS_EXTENT_ITEM_KEY;
  735. ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
  736. if (ret < 0)
  737. goto out_free;
  738. if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
  739. if (path->slots[0]) {
  740. path->slots[0]--;
  741. btrfs_item_key_to_cpu(path->nodes[0], &key,
  742. path->slots[0]);
  743. if (key.objectid == bytenr &&
  744. key.type == BTRFS_EXTENT_ITEM_KEY &&
  745. key.offset == fs_info->nodesize)
  746. ret = 0;
  747. }
  748. }
  749. if (ret == 0) {
  750. leaf = path->nodes[0];
  751. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  752. if (item_size >= sizeof(*ei)) {
  753. ei = btrfs_item_ptr(leaf, path->slots[0],
  754. struct btrfs_extent_item);
  755. num_refs = btrfs_extent_refs(leaf, ei);
  756. extent_flags = btrfs_extent_flags(leaf, ei);
  757. } else {
  758. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  759. struct btrfs_extent_item_v0 *ei0;
  760. BUG_ON(item_size != sizeof(*ei0));
  761. ei0 = btrfs_item_ptr(leaf, path->slots[0],
  762. struct btrfs_extent_item_v0);
  763. num_refs = btrfs_extent_refs_v0(leaf, ei0);
  764. /* FIXME: this isn't correct for data */
  765. extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  766. #else
  767. BUG();
  768. #endif
  769. }
  770. BUG_ON(num_refs == 0);
  771. } else {
  772. num_refs = 0;
  773. extent_flags = 0;
  774. ret = 0;
  775. }
  776. if (!trans)
  777. goto out;
  778. delayed_refs = &trans->transaction->delayed_refs;
  779. spin_lock(&delayed_refs->lock);
  780. head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
  781. if (head) {
  782. if (!mutex_trylock(&head->mutex)) {
  783. refcount_inc(&head->refs);
  784. spin_unlock(&delayed_refs->lock);
  785. btrfs_release_path(path);
  786. /*
  787. * Mutex was contended, block until it's released and try
  788. * again
  789. */
  790. mutex_lock(&head->mutex);
  791. mutex_unlock(&head->mutex);
  792. btrfs_put_delayed_ref_head(head);
  793. goto search_again;
  794. }
  795. spin_lock(&head->lock);
  796. if (head->extent_op && head->extent_op->update_flags)
  797. extent_flags |= head->extent_op->flags_to_set;
  798. else
  799. BUG_ON(num_refs == 0);
  800. num_refs += head->ref_mod;
  801. spin_unlock(&head->lock);
  802. mutex_unlock(&head->mutex);
  803. }
  804. spin_unlock(&delayed_refs->lock);
  805. out:
  806. WARN_ON(num_refs == 0);
  807. if (refs)
  808. *refs = num_refs;
  809. if (flags)
  810. *flags = extent_flags;
  811. out_free:
  812. btrfs_free_path(path);
  813. return ret;
  814. }
  815. /*
  816. * Back reference rules. Back refs have three main goals:
  817. *
  818. * 1) differentiate between all holders of references to an extent so that
  819. * when a reference is dropped we can make sure it was a valid reference
  820. * before freeing the extent.
  821. *
  822. * 2) Provide enough information to quickly find the holders of an extent
  823. * if we notice a given block is corrupted or bad.
  824. *
  825. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  826. * maintenance. This is actually the same as #2, but with a slightly
  827. * different use case.
  828. *
  829. * There are two kinds of back refs. The implicit back refs is optimized
  830. * for pointers in non-shared tree blocks. For a given pointer in a block,
  831. * back refs of this kind provide information about the block's owner tree
  832. * and the pointer's key. These information allow us to find the block by
  833. * b-tree searching. The full back refs is for pointers in tree blocks not
  834. * referenced by their owner trees. The location of tree block is recorded
  835. * in the back refs. Actually the full back refs is generic, and can be
  836. * used in all cases the implicit back refs is used. The major shortcoming
  837. * of the full back refs is its overhead. Every time a tree block gets
  838. * COWed, we have to update back refs entry for all pointers in it.
  839. *
  840. * For a newly allocated tree block, we use implicit back refs for
  841. * pointers in it. This means most tree related operations only involve
  842. * implicit back refs. For a tree block created in old transaction, the
  843. * only way to drop a reference to it is COW it. So we can detect the
  844. * event that tree block loses its owner tree's reference and do the
  845. * back refs conversion.
  846. *
  847. * When a tree block is COWed through a tree, there are four cases:
  848. *
  849. * The reference count of the block is one and the tree is the block's
  850. * owner tree. Nothing to do in this case.
  851. *
  852. * The reference count of the block is one and the tree is not the
  853. * block's owner tree. In this case, full back refs is used for pointers
  854. * in the block. Remove these full back refs, add implicit back refs for
  855. * every pointers in the new block.
  856. *
  857. * The reference count of the block is greater than one and the tree is
  858. * the block's owner tree. In this case, implicit back refs is used for
  859. * pointers in the block. Add full back refs for every pointers in the
  860. * block, increase lower level extents' reference counts. The original
  861. * implicit back refs are entailed to the new block.
  862. *
  863. * The reference count of the block is greater than one and the tree is
  864. * not the block's owner tree. Add implicit back refs for every pointer in
  865. * the new block, increase lower level extents' reference count.
  866. *
  867. * Back Reference Key composing:
  868. *
  869. * The key objectid corresponds to the first byte in the extent,
  870. * The key type is used to differentiate between types of back refs.
  871. * There are different meanings of the key offset for different types
  872. * of back refs.
  873. *
  874. * File extents can be referenced by:
  875. *
  876. * - multiple snapshots, subvolumes, or different generations in one subvol
  877. * - different files inside a single subvolume
  878. * - different offsets inside a file (bookend extents in file.c)
  879. *
  880. * The extent ref structure for the implicit back refs has fields for:
  881. *
  882. * - Objectid of the subvolume root
  883. * - objectid of the file holding the reference
  884. * - original offset in the file
  885. * - how many bookend extents
  886. *
  887. * The key offset for the implicit back refs is hash of the first
  888. * three fields.
  889. *
  890. * The extent ref structure for the full back refs has field for:
  891. *
  892. * - number of pointers in the tree leaf
  893. *
  894. * The key offset for the implicit back refs is the first byte of
  895. * the tree leaf
  896. *
  897. * When a file extent is allocated, The implicit back refs is used.
  898. * the fields are filled in:
  899. *
  900. * (root_key.objectid, inode objectid, offset in file, 1)
  901. *
  902. * When a file extent is removed file truncation, we find the
  903. * corresponding implicit back refs and check the following fields:
  904. *
  905. * (btrfs_header_owner(leaf), inode objectid, offset in file)
  906. *
  907. * Btree extents can be referenced by:
  908. *
  909. * - Different subvolumes
  910. *
  911. * Both the implicit back refs and the full back refs for tree blocks
  912. * only consist of key. The key offset for the implicit back refs is
  913. * objectid of block's owner tree. The key offset for the full back refs
  914. * is the first byte of parent block.
  915. *
  916. * When implicit back refs is used, information about the lowest key and
  917. * level of the tree block are required. These information are stored in
  918. * tree block info structure.
  919. */
  920. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  921. static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
  922. struct btrfs_fs_info *fs_info,
  923. struct btrfs_path *path,
  924. u64 owner, u32 extra_size)
  925. {
  926. struct btrfs_root *root = fs_info->extent_root;
  927. struct btrfs_extent_item *item;
  928. struct btrfs_extent_item_v0 *ei0;
  929. struct btrfs_extent_ref_v0 *ref0;
  930. struct btrfs_tree_block_info *bi;
  931. struct extent_buffer *leaf;
  932. struct btrfs_key key;
  933. struct btrfs_key found_key;
  934. u32 new_size = sizeof(*item);
  935. u64 refs;
  936. int ret;
  937. leaf = path->nodes[0];
  938. BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
  939. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  940. ei0 = btrfs_item_ptr(leaf, path->slots[0],
  941. struct btrfs_extent_item_v0);
  942. refs = btrfs_extent_refs_v0(leaf, ei0);
  943. if (owner == (u64)-1) {
  944. while (1) {
  945. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  946. ret = btrfs_next_leaf(root, path);
  947. if (ret < 0)
  948. return ret;
  949. BUG_ON(ret > 0); /* Corruption */
  950. leaf = path->nodes[0];
  951. }
  952. btrfs_item_key_to_cpu(leaf, &found_key,
  953. path->slots[0]);
  954. BUG_ON(key.objectid != found_key.objectid);
  955. if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
  956. path->slots[0]++;
  957. continue;
  958. }
  959. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  960. struct btrfs_extent_ref_v0);
  961. owner = btrfs_ref_objectid_v0(leaf, ref0);
  962. break;
  963. }
  964. }
  965. btrfs_release_path(path);
  966. if (owner < BTRFS_FIRST_FREE_OBJECTID)
  967. new_size += sizeof(*bi);
  968. new_size -= sizeof(*ei0);
  969. ret = btrfs_search_slot(trans, root, &key, path,
  970. new_size + extra_size, 1);
  971. if (ret < 0)
  972. return ret;
  973. BUG_ON(ret); /* Corruption */
  974. btrfs_extend_item(fs_info, path, new_size);
  975. leaf = path->nodes[0];
  976. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  977. btrfs_set_extent_refs(leaf, item, refs);
  978. /* FIXME: get real generation */
  979. btrfs_set_extent_generation(leaf, item, 0);
  980. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  981. btrfs_set_extent_flags(leaf, item,
  982. BTRFS_EXTENT_FLAG_TREE_BLOCK |
  983. BTRFS_BLOCK_FLAG_FULL_BACKREF);
  984. bi = (struct btrfs_tree_block_info *)(item + 1);
  985. /* FIXME: get first key of the block */
  986. memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
  987. btrfs_set_tree_block_level(leaf, bi, (int)owner);
  988. } else {
  989. btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
  990. }
  991. btrfs_mark_buffer_dirty(leaf);
  992. return 0;
  993. }
  994. #endif
  995. /*
  996. * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
  997. * is_data == BTRFS_REF_TYPE_DATA, data type is requried,
  998. * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
  999. */
  1000. int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
  1001. struct btrfs_extent_inline_ref *iref,
  1002. enum btrfs_inline_ref_type is_data)
  1003. {
  1004. int type = btrfs_extent_inline_ref_type(eb, iref);
  1005. u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
  1006. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  1007. type == BTRFS_SHARED_BLOCK_REF_KEY ||
  1008. type == BTRFS_SHARED_DATA_REF_KEY ||
  1009. type == BTRFS_EXTENT_DATA_REF_KEY) {
  1010. if (is_data == BTRFS_REF_TYPE_BLOCK) {
  1011. if (type == BTRFS_TREE_BLOCK_REF_KEY)
  1012. return type;
  1013. if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
  1014. ASSERT(eb->fs_info);
  1015. /*
  1016. * Every shared one has parent tree
  1017. * block, which must be aligned to
  1018. * nodesize.
  1019. */
  1020. if (offset &&
  1021. IS_ALIGNED(offset, eb->fs_info->nodesize))
  1022. return type;
  1023. }
  1024. } else if (is_data == BTRFS_REF_TYPE_DATA) {
  1025. if (type == BTRFS_EXTENT_DATA_REF_KEY)
  1026. return type;
  1027. if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1028. ASSERT(eb->fs_info);
  1029. /*
  1030. * Every shared one has parent tree
  1031. * block, which must be aligned to
  1032. * nodesize.
  1033. */
  1034. if (offset &&
  1035. IS_ALIGNED(offset, eb->fs_info->nodesize))
  1036. return type;
  1037. }
  1038. } else {
  1039. ASSERT(is_data == BTRFS_REF_TYPE_ANY);
  1040. return type;
  1041. }
  1042. }
  1043. btrfs_print_leaf((struct extent_buffer *)eb);
  1044. btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
  1045. eb->start, type);
  1046. WARN_ON(1);
  1047. return BTRFS_REF_TYPE_INVALID;
  1048. }
  1049. static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
  1050. {
  1051. u32 high_crc = ~(u32)0;
  1052. u32 low_crc = ~(u32)0;
  1053. __le64 lenum;
  1054. lenum = cpu_to_le64(root_objectid);
  1055. high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
  1056. lenum = cpu_to_le64(owner);
  1057. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  1058. lenum = cpu_to_le64(offset);
  1059. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  1060. return ((u64)high_crc << 31) ^ (u64)low_crc;
  1061. }
  1062. static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
  1063. struct btrfs_extent_data_ref *ref)
  1064. {
  1065. return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
  1066. btrfs_extent_data_ref_objectid(leaf, ref),
  1067. btrfs_extent_data_ref_offset(leaf, ref));
  1068. }
  1069. static int match_extent_data_ref(struct extent_buffer *leaf,
  1070. struct btrfs_extent_data_ref *ref,
  1071. u64 root_objectid, u64 owner, u64 offset)
  1072. {
  1073. if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
  1074. btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
  1075. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  1076. return 0;
  1077. return 1;
  1078. }
  1079. static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
  1080. struct btrfs_path *path,
  1081. u64 bytenr, u64 parent,
  1082. u64 root_objectid,
  1083. u64 owner, u64 offset)
  1084. {
  1085. struct btrfs_root *root = trans->fs_info->extent_root;
  1086. struct btrfs_key key;
  1087. struct btrfs_extent_data_ref *ref;
  1088. struct extent_buffer *leaf;
  1089. u32 nritems;
  1090. int ret;
  1091. int recow;
  1092. int err = -ENOENT;
  1093. key.objectid = bytenr;
  1094. if (parent) {
  1095. key.type = BTRFS_SHARED_DATA_REF_KEY;
  1096. key.offset = parent;
  1097. } else {
  1098. key.type = BTRFS_EXTENT_DATA_REF_KEY;
  1099. key.offset = hash_extent_data_ref(root_objectid,
  1100. owner, offset);
  1101. }
  1102. again:
  1103. recow = 0;
  1104. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1105. if (ret < 0) {
  1106. err = ret;
  1107. goto fail;
  1108. }
  1109. if (parent) {
  1110. if (!ret)
  1111. return 0;
  1112. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1113. key.type = BTRFS_EXTENT_REF_V0_KEY;
  1114. btrfs_release_path(path);
  1115. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1116. if (ret < 0) {
  1117. err = ret;
  1118. goto fail;
  1119. }
  1120. if (!ret)
  1121. return 0;
  1122. #endif
  1123. goto fail;
  1124. }
  1125. leaf = path->nodes[0];
  1126. nritems = btrfs_header_nritems(leaf);
  1127. while (1) {
  1128. if (path->slots[0] >= nritems) {
  1129. ret = btrfs_next_leaf(root, path);
  1130. if (ret < 0)
  1131. err = ret;
  1132. if (ret)
  1133. goto fail;
  1134. leaf = path->nodes[0];
  1135. nritems = btrfs_header_nritems(leaf);
  1136. recow = 1;
  1137. }
  1138. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1139. if (key.objectid != bytenr ||
  1140. key.type != BTRFS_EXTENT_DATA_REF_KEY)
  1141. goto fail;
  1142. ref = btrfs_item_ptr(leaf, path->slots[0],
  1143. struct btrfs_extent_data_ref);
  1144. if (match_extent_data_ref(leaf, ref, root_objectid,
  1145. owner, offset)) {
  1146. if (recow) {
  1147. btrfs_release_path(path);
  1148. goto again;
  1149. }
  1150. err = 0;
  1151. break;
  1152. }
  1153. path->slots[0]++;
  1154. }
  1155. fail:
  1156. return err;
  1157. }
  1158. static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
  1159. struct btrfs_path *path,
  1160. u64 bytenr, u64 parent,
  1161. u64 root_objectid, u64 owner,
  1162. u64 offset, int refs_to_add)
  1163. {
  1164. struct btrfs_root *root = trans->fs_info->extent_root;
  1165. struct btrfs_key key;
  1166. struct extent_buffer *leaf;
  1167. u32 size;
  1168. u32 num_refs;
  1169. int ret;
  1170. key.objectid = bytenr;
  1171. if (parent) {
  1172. key.type = BTRFS_SHARED_DATA_REF_KEY;
  1173. key.offset = parent;
  1174. size = sizeof(struct btrfs_shared_data_ref);
  1175. } else {
  1176. key.type = BTRFS_EXTENT_DATA_REF_KEY;
  1177. key.offset = hash_extent_data_ref(root_objectid,
  1178. owner, offset);
  1179. size = sizeof(struct btrfs_extent_data_ref);
  1180. }
  1181. ret = btrfs_insert_empty_item(trans, root, path, &key, size);
  1182. if (ret && ret != -EEXIST)
  1183. goto fail;
  1184. leaf = path->nodes[0];
  1185. if (parent) {
  1186. struct btrfs_shared_data_ref *ref;
  1187. ref = btrfs_item_ptr(leaf, path->slots[0],
  1188. struct btrfs_shared_data_ref);
  1189. if (ret == 0) {
  1190. btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
  1191. } else {
  1192. num_refs = btrfs_shared_data_ref_count(leaf, ref);
  1193. num_refs += refs_to_add;
  1194. btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
  1195. }
  1196. } else {
  1197. struct btrfs_extent_data_ref *ref;
  1198. while (ret == -EEXIST) {
  1199. ref = btrfs_item_ptr(leaf, path->slots[0],
  1200. struct btrfs_extent_data_ref);
  1201. if (match_extent_data_ref(leaf, ref, root_objectid,
  1202. owner, offset))
  1203. break;
  1204. btrfs_release_path(path);
  1205. key.offset++;
  1206. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1207. size);
  1208. if (ret && ret != -EEXIST)
  1209. goto fail;
  1210. leaf = path->nodes[0];
  1211. }
  1212. ref = btrfs_item_ptr(leaf, path->slots[0],
  1213. struct btrfs_extent_data_ref);
  1214. if (ret == 0) {
  1215. btrfs_set_extent_data_ref_root(leaf, ref,
  1216. root_objectid);
  1217. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  1218. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  1219. btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
  1220. } else {
  1221. num_refs = btrfs_extent_data_ref_count(leaf, ref);
  1222. num_refs += refs_to_add;
  1223. btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
  1224. }
  1225. }
  1226. btrfs_mark_buffer_dirty(leaf);
  1227. ret = 0;
  1228. fail:
  1229. btrfs_release_path(path);
  1230. return ret;
  1231. }
  1232. static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
  1233. struct btrfs_path *path,
  1234. int refs_to_drop, int *last_ref)
  1235. {
  1236. struct btrfs_key key;
  1237. struct btrfs_extent_data_ref *ref1 = NULL;
  1238. struct btrfs_shared_data_ref *ref2 = NULL;
  1239. struct extent_buffer *leaf;
  1240. u32 num_refs = 0;
  1241. int ret = 0;
  1242. leaf = path->nodes[0];
  1243. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1244. if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  1245. ref1 = btrfs_item_ptr(leaf, path->slots[0],
  1246. struct btrfs_extent_data_ref);
  1247. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  1248. } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  1249. ref2 = btrfs_item_ptr(leaf, path->slots[0],
  1250. struct btrfs_shared_data_ref);
  1251. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  1252. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1253. } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
  1254. struct btrfs_extent_ref_v0 *ref0;
  1255. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  1256. struct btrfs_extent_ref_v0);
  1257. num_refs = btrfs_ref_count_v0(leaf, ref0);
  1258. #endif
  1259. } else {
  1260. BUG();
  1261. }
  1262. BUG_ON(num_refs < refs_to_drop);
  1263. num_refs -= refs_to_drop;
  1264. if (num_refs == 0) {
  1265. ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
  1266. *last_ref = 1;
  1267. } else {
  1268. if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
  1269. btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
  1270. else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
  1271. btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
  1272. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1273. else {
  1274. struct btrfs_extent_ref_v0 *ref0;
  1275. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  1276. struct btrfs_extent_ref_v0);
  1277. btrfs_set_ref_count_v0(leaf, ref0, num_refs);
  1278. }
  1279. #endif
  1280. btrfs_mark_buffer_dirty(leaf);
  1281. }
  1282. return ret;
  1283. }
  1284. static noinline u32 extent_data_ref_count(struct btrfs_path *path,
  1285. struct btrfs_extent_inline_ref *iref)
  1286. {
  1287. struct btrfs_key key;
  1288. struct extent_buffer *leaf;
  1289. struct btrfs_extent_data_ref *ref1;
  1290. struct btrfs_shared_data_ref *ref2;
  1291. u32 num_refs = 0;
  1292. int type;
  1293. leaf = path->nodes[0];
  1294. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1295. if (iref) {
  1296. /*
  1297. * If type is invalid, we should have bailed out earlier than
  1298. * this call.
  1299. */
  1300. type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
  1301. ASSERT(type != BTRFS_REF_TYPE_INVALID);
  1302. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1303. ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
  1304. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  1305. } else {
  1306. ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
  1307. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  1308. }
  1309. } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  1310. ref1 = btrfs_item_ptr(leaf, path->slots[0],
  1311. struct btrfs_extent_data_ref);
  1312. num_refs = btrfs_extent_data_ref_count(leaf, ref1);
  1313. } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  1314. ref2 = btrfs_item_ptr(leaf, path->slots[0],
  1315. struct btrfs_shared_data_ref);
  1316. num_refs = btrfs_shared_data_ref_count(leaf, ref2);
  1317. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1318. } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
  1319. struct btrfs_extent_ref_v0 *ref0;
  1320. ref0 = btrfs_item_ptr(leaf, path->slots[0],
  1321. struct btrfs_extent_ref_v0);
  1322. num_refs = btrfs_ref_count_v0(leaf, ref0);
  1323. #endif
  1324. } else {
  1325. WARN_ON(1);
  1326. }
  1327. return num_refs;
  1328. }
  1329. static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
  1330. struct btrfs_path *path,
  1331. u64 bytenr, u64 parent,
  1332. u64 root_objectid)
  1333. {
  1334. struct btrfs_root *root = trans->fs_info->extent_root;
  1335. struct btrfs_key key;
  1336. int ret;
  1337. key.objectid = bytenr;
  1338. if (parent) {
  1339. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  1340. key.offset = parent;
  1341. } else {
  1342. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  1343. key.offset = root_objectid;
  1344. }
  1345. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1346. if (ret > 0)
  1347. ret = -ENOENT;
  1348. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1349. if (ret == -ENOENT && parent) {
  1350. btrfs_release_path(path);
  1351. key.type = BTRFS_EXTENT_REF_V0_KEY;
  1352. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1353. if (ret > 0)
  1354. ret = -ENOENT;
  1355. }
  1356. #endif
  1357. return ret;
  1358. }
  1359. static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
  1360. struct btrfs_path *path,
  1361. u64 bytenr, u64 parent,
  1362. u64 root_objectid)
  1363. {
  1364. struct btrfs_key key;
  1365. int ret;
  1366. key.objectid = bytenr;
  1367. if (parent) {
  1368. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  1369. key.offset = parent;
  1370. } else {
  1371. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  1372. key.offset = root_objectid;
  1373. }
  1374. ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
  1375. path, &key, 0);
  1376. btrfs_release_path(path);
  1377. return ret;
  1378. }
  1379. static inline int extent_ref_type(u64 parent, u64 owner)
  1380. {
  1381. int type;
  1382. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1383. if (parent > 0)
  1384. type = BTRFS_SHARED_BLOCK_REF_KEY;
  1385. else
  1386. type = BTRFS_TREE_BLOCK_REF_KEY;
  1387. } else {
  1388. if (parent > 0)
  1389. type = BTRFS_SHARED_DATA_REF_KEY;
  1390. else
  1391. type = BTRFS_EXTENT_DATA_REF_KEY;
  1392. }
  1393. return type;
  1394. }
  1395. static int find_next_key(struct btrfs_path *path, int level,
  1396. struct btrfs_key *key)
  1397. {
  1398. for (; level < BTRFS_MAX_LEVEL; level++) {
  1399. if (!path->nodes[level])
  1400. break;
  1401. if (path->slots[level] + 1 >=
  1402. btrfs_header_nritems(path->nodes[level]))
  1403. continue;
  1404. if (level == 0)
  1405. btrfs_item_key_to_cpu(path->nodes[level], key,
  1406. path->slots[level] + 1);
  1407. else
  1408. btrfs_node_key_to_cpu(path->nodes[level], key,
  1409. path->slots[level] + 1);
  1410. return 0;
  1411. }
  1412. return 1;
  1413. }
  1414. /*
  1415. * look for inline back ref. if back ref is found, *ref_ret is set
  1416. * to the address of inline back ref, and 0 is returned.
  1417. *
  1418. * if back ref isn't found, *ref_ret is set to the address where it
  1419. * should be inserted, and -ENOENT is returned.
  1420. *
  1421. * if insert is true and there are too many inline back refs, the path
  1422. * points to the extent item, and -EAGAIN is returned.
  1423. *
  1424. * NOTE: inline back refs are ordered in the same way that back ref
  1425. * items in the tree are ordered.
  1426. */
  1427. static noinline_for_stack
  1428. int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
  1429. struct btrfs_path *path,
  1430. struct btrfs_extent_inline_ref **ref_ret,
  1431. u64 bytenr, u64 num_bytes,
  1432. u64 parent, u64 root_objectid,
  1433. u64 owner, u64 offset, int insert)
  1434. {
  1435. struct btrfs_fs_info *fs_info = trans->fs_info;
  1436. struct btrfs_root *root = fs_info->extent_root;
  1437. struct btrfs_key key;
  1438. struct extent_buffer *leaf;
  1439. struct btrfs_extent_item *ei;
  1440. struct btrfs_extent_inline_ref *iref;
  1441. u64 flags;
  1442. u64 item_size;
  1443. unsigned long ptr;
  1444. unsigned long end;
  1445. int extra_size;
  1446. int type;
  1447. int want;
  1448. int ret;
  1449. int err = 0;
  1450. bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
  1451. int needed;
  1452. key.objectid = bytenr;
  1453. key.type = BTRFS_EXTENT_ITEM_KEY;
  1454. key.offset = num_bytes;
  1455. want = extent_ref_type(parent, owner);
  1456. if (insert) {
  1457. extra_size = btrfs_extent_inline_ref_size(want);
  1458. path->keep_locks = 1;
  1459. } else
  1460. extra_size = -1;
  1461. /*
  1462. * Owner is our level, so we can just add one to get the level for the
  1463. * block we are interested in.
  1464. */
  1465. if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
  1466. key.type = BTRFS_METADATA_ITEM_KEY;
  1467. key.offset = owner;
  1468. }
  1469. again:
  1470. ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
  1471. if (ret < 0) {
  1472. err = ret;
  1473. goto out;
  1474. }
  1475. /*
  1476. * We may be a newly converted file system which still has the old fat
  1477. * extent entries for metadata, so try and see if we have one of those.
  1478. */
  1479. if (ret > 0 && skinny_metadata) {
  1480. skinny_metadata = false;
  1481. if (path->slots[0]) {
  1482. path->slots[0]--;
  1483. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1484. path->slots[0]);
  1485. if (key.objectid == bytenr &&
  1486. key.type == BTRFS_EXTENT_ITEM_KEY &&
  1487. key.offset == num_bytes)
  1488. ret = 0;
  1489. }
  1490. if (ret) {
  1491. key.objectid = bytenr;
  1492. key.type = BTRFS_EXTENT_ITEM_KEY;
  1493. key.offset = num_bytes;
  1494. btrfs_release_path(path);
  1495. goto again;
  1496. }
  1497. }
  1498. if (ret && !insert) {
  1499. err = -ENOENT;
  1500. goto out;
  1501. } else if (WARN_ON(ret)) {
  1502. err = -EIO;
  1503. goto out;
  1504. }
  1505. leaf = path->nodes[0];
  1506. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1507. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1508. if (item_size < sizeof(*ei)) {
  1509. if (!insert) {
  1510. err = -ENOENT;
  1511. goto out;
  1512. }
  1513. ret = convert_extent_item_v0(trans, fs_info, path, owner,
  1514. extra_size);
  1515. if (ret < 0) {
  1516. err = ret;
  1517. goto out;
  1518. }
  1519. leaf = path->nodes[0];
  1520. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1521. }
  1522. #endif
  1523. BUG_ON(item_size < sizeof(*ei));
  1524. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1525. flags = btrfs_extent_flags(leaf, ei);
  1526. ptr = (unsigned long)(ei + 1);
  1527. end = (unsigned long)ei + item_size;
  1528. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
  1529. ptr += sizeof(struct btrfs_tree_block_info);
  1530. BUG_ON(ptr > end);
  1531. }
  1532. if (owner >= BTRFS_FIRST_FREE_OBJECTID)
  1533. needed = BTRFS_REF_TYPE_DATA;
  1534. else
  1535. needed = BTRFS_REF_TYPE_BLOCK;
  1536. err = -ENOENT;
  1537. while (1) {
  1538. if (ptr >= end) {
  1539. WARN_ON(ptr > end);
  1540. break;
  1541. }
  1542. iref = (struct btrfs_extent_inline_ref *)ptr;
  1543. type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
  1544. if (type == BTRFS_REF_TYPE_INVALID) {
  1545. err = -EINVAL;
  1546. goto out;
  1547. }
  1548. if (want < type)
  1549. break;
  1550. if (want > type) {
  1551. ptr += btrfs_extent_inline_ref_size(type);
  1552. continue;
  1553. }
  1554. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1555. struct btrfs_extent_data_ref *dref;
  1556. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1557. if (match_extent_data_ref(leaf, dref, root_objectid,
  1558. owner, offset)) {
  1559. err = 0;
  1560. break;
  1561. }
  1562. if (hash_extent_data_ref_item(leaf, dref) <
  1563. hash_extent_data_ref(root_objectid, owner, offset))
  1564. break;
  1565. } else {
  1566. u64 ref_offset;
  1567. ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
  1568. if (parent > 0) {
  1569. if (parent == ref_offset) {
  1570. err = 0;
  1571. break;
  1572. }
  1573. if (ref_offset < parent)
  1574. break;
  1575. } else {
  1576. if (root_objectid == ref_offset) {
  1577. err = 0;
  1578. break;
  1579. }
  1580. if (ref_offset < root_objectid)
  1581. break;
  1582. }
  1583. }
  1584. ptr += btrfs_extent_inline_ref_size(type);
  1585. }
  1586. if (err == -ENOENT && insert) {
  1587. if (item_size + extra_size >=
  1588. BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
  1589. err = -EAGAIN;
  1590. goto out;
  1591. }
  1592. /*
  1593. * To add new inline back ref, we have to make sure
  1594. * there is no corresponding back ref item.
  1595. * For simplicity, we just do not add new inline back
  1596. * ref if there is any kind of item for this block
  1597. */
  1598. if (find_next_key(path, 0, &key) == 0 &&
  1599. key.objectid == bytenr &&
  1600. key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
  1601. err = -EAGAIN;
  1602. goto out;
  1603. }
  1604. }
  1605. *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
  1606. out:
  1607. if (insert) {
  1608. path->keep_locks = 0;
  1609. btrfs_unlock_up_safe(path, 1);
  1610. }
  1611. return err;
  1612. }
  1613. /*
  1614. * helper to add new inline back ref
  1615. */
  1616. static noinline_for_stack
  1617. void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
  1618. struct btrfs_path *path,
  1619. struct btrfs_extent_inline_ref *iref,
  1620. u64 parent, u64 root_objectid,
  1621. u64 owner, u64 offset, int refs_to_add,
  1622. struct btrfs_delayed_extent_op *extent_op)
  1623. {
  1624. struct extent_buffer *leaf;
  1625. struct btrfs_extent_item *ei;
  1626. unsigned long ptr;
  1627. unsigned long end;
  1628. unsigned long item_offset;
  1629. u64 refs;
  1630. int size;
  1631. int type;
  1632. leaf = path->nodes[0];
  1633. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1634. item_offset = (unsigned long)iref - (unsigned long)ei;
  1635. type = extent_ref_type(parent, owner);
  1636. size = btrfs_extent_inline_ref_size(type);
  1637. btrfs_extend_item(fs_info, path, size);
  1638. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1639. refs = btrfs_extent_refs(leaf, ei);
  1640. refs += refs_to_add;
  1641. btrfs_set_extent_refs(leaf, ei, refs);
  1642. if (extent_op)
  1643. __run_delayed_extent_op(extent_op, leaf, ei);
  1644. ptr = (unsigned long)ei + item_offset;
  1645. end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
  1646. if (ptr < end - size)
  1647. memmove_extent_buffer(leaf, ptr + size, ptr,
  1648. end - size - ptr);
  1649. iref = (struct btrfs_extent_inline_ref *)ptr;
  1650. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  1651. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1652. struct btrfs_extent_data_ref *dref;
  1653. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1654. btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
  1655. btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
  1656. btrfs_set_extent_data_ref_offset(leaf, dref, offset);
  1657. btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
  1658. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1659. struct btrfs_shared_data_ref *sref;
  1660. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1661. btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
  1662. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  1663. } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
  1664. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  1665. } else {
  1666. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  1667. }
  1668. btrfs_mark_buffer_dirty(leaf);
  1669. }
  1670. static int lookup_extent_backref(struct btrfs_trans_handle *trans,
  1671. struct btrfs_path *path,
  1672. struct btrfs_extent_inline_ref **ref_ret,
  1673. u64 bytenr, u64 num_bytes, u64 parent,
  1674. u64 root_objectid, u64 owner, u64 offset)
  1675. {
  1676. int ret;
  1677. ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
  1678. num_bytes, parent, root_objectid,
  1679. owner, offset, 0);
  1680. if (ret != -ENOENT)
  1681. return ret;
  1682. btrfs_release_path(path);
  1683. *ref_ret = NULL;
  1684. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1685. ret = lookup_tree_block_ref(trans, path, bytenr, parent,
  1686. root_objectid);
  1687. } else {
  1688. ret = lookup_extent_data_ref(trans, path, bytenr, parent,
  1689. root_objectid, owner, offset);
  1690. }
  1691. return ret;
  1692. }
  1693. /*
  1694. * helper to update/remove inline back ref
  1695. */
  1696. static noinline_for_stack
  1697. void update_inline_extent_backref(struct btrfs_path *path,
  1698. struct btrfs_extent_inline_ref *iref,
  1699. int refs_to_mod,
  1700. struct btrfs_delayed_extent_op *extent_op,
  1701. int *last_ref)
  1702. {
  1703. struct extent_buffer *leaf = path->nodes[0];
  1704. struct btrfs_fs_info *fs_info = leaf->fs_info;
  1705. struct btrfs_extent_item *ei;
  1706. struct btrfs_extent_data_ref *dref = NULL;
  1707. struct btrfs_shared_data_ref *sref = NULL;
  1708. unsigned long ptr;
  1709. unsigned long end;
  1710. u32 item_size;
  1711. int size;
  1712. int type;
  1713. u64 refs;
  1714. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1715. refs = btrfs_extent_refs(leaf, ei);
  1716. WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
  1717. refs += refs_to_mod;
  1718. btrfs_set_extent_refs(leaf, ei, refs);
  1719. if (extent_op)
  1720. __run_delayed_extent_op(extent_op, leaf, ei);
  1721. /*
  1722. * If type is invalid, we should have bailed out after
  1723. * lookup_inline_extent_backref().
  1724. */
  1725. type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
  1726. ASSERT(type != BTRFS_REF_TYPE_INVALID);
  1727. if (type == BTRFS_EXTENT_DATA_REF_KEY) {
  1728. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1729. refs = btrfs_extent_data_ref_count(leaf, dref);
  1730. } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
  1731. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1732. refs = btrfs_shared_data_ref_count(leaf, sref);
  1733. } else {
  1734. refs = 1;
  1735. BUG_ON(refs_to_mod != -1);
  1736. }
  1737. BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
  1738. refs += refs_to_mod;
  1739. if (refs > 0) {
  1740. if (type == BTRFS_EXTENT_DATA_REF_KEY)
  1741. btrfs_set_extent_data_ref_count(leaf, dref, refs);
  1742. else
  1743. btrfs_set_shared_data_ref_count(leaf, sref, refs);
  1744. } else {
  1745. *last_ref = 1;
  1746. size = btrfs_extent_inline_ref_size(type);
  1747. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1748. ptr = (unsigned long)iref;
  1749. end = (unsigned long)ei + item_size;
  1750. if (ptr + size < end)
  1751. memmove_extent_buffer(leaf, ptr, ptr + size,
  1752. end - ptr - size);
  1753. item_size -= size;
  1754. btrfs_truncate_item(fs_info, path, item_size, 1);
  1755. }
  1756. btrfs_mark_buffer_dirty(leaf);
  1757. }
  1758. static noinline_for_stack
  1759. int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
  1760. struct btrfs_fs_info *fs_info,
  1761. struct btrfs_path *path,
  1762. u64 bytenr, u64 num_bytes, u64 parent,
  1763. u64 root_objectid, u64 owner,
  1764. u64 offset, int refs_to_add,
  1765. struct btrfs_delayed_extent_op *extent_op)
  1766. {
  1767. struct btrfs_extent_inline_ref *iref;
  1768. int ret;
  1769. ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
  1770. num_bytes, parent, root_objectid,
  1771. owner, offset, 1);
  1772. if (ret == 0) {
  1773. BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
  1774. update_inline_extent_backref(path, iref, refs_to_add,
  1775. extent_op, NULL);
  1776. } else if (ret == -ENOENT) {
  1777. setup_inline_extent_backref(fs_info, path, iref, parent,
  1778. root_objectid, owner, offset,
  1779. refs_to_add, extent_op);
  1780. ret = 0;
  1781. }
  1782. return ret;
  1783. }
  1784. static int insert_extent_backref(struct btrfs_trans_handle *trans,
  1785. struct btrfs_path *path,
  1786. u64 bytenr, u64 parent, u64 root_objectid,
  1787. u64 owner, u64 offset, int refs_to_add)
  1788. {
  1789. int ret;
  1790. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1791. BUG_ON(refs_to_add != 1);
  1792. ret = insert_tree_block_ref(trans, path, bytenr, parent,
  1793. root_objectid);
  1794. } else {
  1795. ret = insert_extent_data_ref(trans, path, bytenr, parent,
  1796. root_objectid, owner, offset,
  1797. refs_to_add);
  1798. }
  1799. return ret;
  1800. }
  1801. static int remove_extent_backref(struct btrfs_trans_handle *trans,
  1802. struct btrfs_fs_info *fs_info,
  1803. struct btrfs_path *path,
  1804. struct btrfs_extent_inline_ref *iref,
  1805. int refs_to_drop, int is_data, int *last_ref)
  1806. {
  1807. int ret = 0;
  1808. BUG_ON(!is_data && refs_to_drop != 1);
  1809. if (iref) {
  1810. update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
  1811. last_ref);
  1812. } else if (is_data) {
  1813. ret = remove_extent_data_ref(trans, path, refs_to_drop,
  1814. last_ref);
  1815. } else {
  1816. *last_ref = 1;
  1817. ret = btrfs_del_item(trans, fs_info->extent_root, path);
  1818. }
  1819. return ret;
  1820. }
  1821. #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
  1822. static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
  1823. u64 *discarded_bytes)
  1824. {
  1825. int j, ret = 0;
  1826. u64 bytes_left, end;
  1827. u64 aligned_start = ALIGN(start, 1 << 9);
  1828. if (WARN_ON(start != aligned_start)) {
  1829. len -= aligned_start - start;
  1830. len = round_down(len, 1 << 9);
  1831. start = aligned_start;
  1832. }
  1833. *discarded_bytes = 0;
  1834. if (!len)
  1835. return 0;
  1836. end = start + len;
  1837. bytes_left = len;
  1838. /* Skip any superblocks on this device. */
  1839. for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
  1840. u64 sb_start = btrfs_sb_offset(j);
  1841. u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
  1842. u64 size = sb_start - start;
  1843. if (!in_range(sb_start, start, bytes_left) &&
  1844. !in_range(sb_end, start, bytes_left) &&
  1845. !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
  1846. continue;
  1847. /*
  1848. * Superblock spans beginning of range. Adjust start and
  1849. * try again.
  1850. */
  1851. if (sb_start <= start) {
  1852. start += sb_end - start;
  1853. if (start > end) {
  1854. bytes_left = 0;
  1855. break;
  1856. }
  1857. bytes_left = end - start;
  1858. continue;
  1859. }
  1860. if (size) {
  1861. ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
  1862. GFP_NOFS, 0);
  1863. if (!ret)
  1864. *discarded_bytes += size;
  1865. else if (ret != -EOPNOTSUPP)
  1866. return ret;
  1867. }
  1868. start = sb_end;
  1869. if (start > end) {
  1870. bytes_left = 0;
  1871. break;
  1872. }
  1873. bytes_left = end - start;
  1874. }
  1875. if (bytes_left) {
  1876. ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
  1877. GFP_NOFS, 0);
  1878. if (!ret)
  1879. *discarded_bytes += bytes_left;
  1880. }
  1881. return ret;
  1882. }
  1883. int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
  1884. u64 num_bytes, u64 *actual_bytes)
  1885. {
  1886. int ret;
  1887. u64 discarded_bytes = 0;
  1888. struct btrfs_bio *bbio = NULL;
  1889. /*
  1890. * Avoid races with device replace and make sure our bbio has devices
  1891. * associated to its stripes that don't go away while we are discarding.
  1892. */
  1893. btrfs_bio_counter_inc_blocked(fs_info);
  1894. /* Tell the block device(s) that the sectors can be discarded */
  1895. ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
  1896. &bbio, 0);
  1897. /* Error condition is -ENOMEM */
  1898. if (!ret) {
  1899. struct btrfs_bio_stripe *stripe = bbio->stripes;
  1900. int i;
  1901. for (i = 0; i < bbio->num_stripes; i++, stripe++) {
  1902. u64 bytes;
  1903. struct request_queue *req_q;
  1904. if (!stripe->dev->bdev) {
  1905. ASSERT(btrfs_test_opt(fs_info, DEGRADED));
  1906. continue;
  1907. }
  1908. req_q = bdev_get_queue(stripe->dev->bdev);
  1909. if (!blk_queue_discard(req_q))
  1910. continue;
  1911. ret = btrfs_issue_discard(stripe->dev->bdev,
  1912. stripe->physical,
  1913. stripe->length,
  1914. &bytes);
  1915. if (!ret)
  1916. discarded_bytes += bytes;
  1917. else if (ret != -EOPNOTSUPP)
  1918. break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
  1919. /*
  1920. * Just in case we get back EOPNOTSUPP for some reason,
  1921. * just ignore the return value so we don't screw up
  1922. * people calling discard_extent.
  1923. */
  1924. ret = 0;
  1925. }
  1926. btrfs_put_bbio(bbio);
  1927. }
  1928. btrfs_bio_counter_dec(fs_info);
  1929. if (actual_bytes)
  1930. *actual_bytes = discarded_bytes;
  1931. if (ret == -EOPNOTSUPP)
  1932. ret = 0;
  1933. return ret;
  1934. }
  1935. /* Can return -ENOMEM */
  1936. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1937. struct btrfs_root *root,
  1938. u64 bytenr, u64 num_bytes, u64 parent,
  1939. u64 root_objectid, u64 owner, u64 offset)
  1940. {
  1941. struct btrfs_fs_info *fs_info = root->fs_info;
  1942. int old_ref_mod, new_ref_mod;
  1943. int ret;
  1944. BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
  1945. root_objectid == BTRFS_TREE_LOG_OBJECTID);
  1946. btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
  1947. owner, offset, BTRFS_ADD_DELAYED_REF);
  1948. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  1949. ret = btrfs_add_delayed_tree_ref(trans, bytenr,
  1950. num_bytes, parent,
  1951. root_objectid, (int)owner,
  1952. BTRFS_ADD_DELAYED_REF, NULL,
  1953. &old_ref_mod, &new_ref_mod);
  1954. } else {
  1955. ret = btrfs_add_delayed_data_ref(trans, bytenr,
  1956. num_bytes, parent,
  1957. root_objectid, owner, offset,
  1958. 0, BTRFS_ADD_DELAYED_REF,
  1959. &old_ref_mod, &new_ref_mod);
  1960. }
  1961. if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) {
  1962. bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
  1963. add_pinned_bytes(fs_info, -num_bytes, metadata, root_objectid);
  1964. }
  1965. return ret;
  1966. }
  1967. /*
  1968. * __btrfs_inc_extent_ref - insert backreference for a given extent
  1969. *
  1970. * @trans: Handle of transaction
  1971. *
  1972. * @node: The delayed ref node used to get the bytenr/length for
  1973. * extent whose references are incremented.
  1974. *
  1975. * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
  1976. * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
  1977. * bytenr of the parent block. Since new extents are always
  1978. * created with indirect references, this will only be the case
  1979. * when relocating a shared extent. In that case, root_objectid
  1980. * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
  1981. * be 0
  1982. *
  1983. * @root_objectid: The id of the root where this modification has originated,
  1984. * this can be either one of the well-known metadata trees or
  1985. * the subvolume id which references this extent.
  1986. *
  1987. * @owner: For data extents it is the inode number of the owning file.
  1988. * For metadata extents this parameter holds the level in the
  1989. * tree of the extent.
  1990. *
  1991. * @offset: For metadata extents the offset is ignored and is currently
  1992. * always passed as 0. For data extents it is the fileoffset
  1993. * this extent belongs to.
  1994. *
  1995. * @refs_to_add Number of references to add
  1996. *
  1997. * @extent_op Pointer to a structure, holding information necessary when
  1998. * updating a tree block's flags
  1999. *
  2000. */
  2001. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  2002. struct btrfs_delayed_ref_node *node,
  2003. u64 parent, u64 root_objectid,
  2004. u64 owner, u64 offset, int refs_to_add,
  2005. struct btrfs_delayed_extent_op *extent_op)
  2006. {
  2007. struct btrfs_fs_info *fs_info = trans->fs_info;
  2008. struct btrfs_path *path;
  2009. struct extent_buffer *leaf;
  2010. struct btrfs_extent_item *item;
  2011. struct btrfs_key key;
  2012. u64 bytenr = node->bytenr;
  2013. u64 num_bytes = node->num_bytes;
  2014. u64 refs;
  2015. int ret;
  2016. path = btrfs_alloc_path();
  2017. if (!path)
  2018. return -ENOMEM;
  2019. path->reada = READA_FORWARD;
  2020. path->leave_spinning = 1;
  2021. /* this will setup the path even if it fails to insert the back ref */
  2022. ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
  2023. num_bytes, parent, root_objectid,
  2024. owner, offset,
  2025. refs_to_add, extent_op);
  2026. if ((ret < 0 && ret != -EAGAIN) || !ret)
  2027. goto out;
  2028. /*
  2029. * Ok we had -EAGAIN which means we didn't have space to insert and
  2030. * inline extent ref, so just update the reference count and add a
  2031. * normal backref.
  2032. */
  2033. leaf = path->nodes[0];
  2034. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2035. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2036. refs = btrfs_extent_refs(leaf, item);
  2037. btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
  2038. if (extent_op)
  2039. __run_delayed_extent_op(extent_op, leaf, item);
  2040. btrfs_mark_buffer_dirty(leaf);
  2041. btrfs_release_path(path);
  2042. path->reada = READA_FORWARD;
  2043. path->leave_spinning = 1;
  2044. /* now insert the actual backref */
  2045. ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
  2046. owner, offset, refs_to_add);
  2047. if (ret)
  2048. btrfs_abort_transaction(trans, ret);
  2049. out:
  2050. btrfs_free_path(path);
  2051. return ret;
  2052. }
  2053. static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
  2054. struct btrfs_delayed_ref_node *node,
  2055. struct btrfs_delayed_extent_op *extent_op,
  2056. int insert_reserved)
  2057. {
  2058. int ret = 0;
  2059. struct btrfs_delayed_data_ref *ref;
  2060. struct btrfs_key ins;
  2061. u64 parent = 0;
  2062. u64 ref_root = 0;
  2063. u64 flags = 0;
  2064. ins.objectid = node->bytenr;
  2065. ins.offset = node->num_bytes;
  2066. ins.type = BTRFS_EXTENT_ITEM_KEY;
  2067. ref = btrfs_delayed_node_to_data_ref(node);
  2068. trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
  2069. if (node->type == BTRFS_SHARED_DATA_REF_KEY)
  2070. parent = ref->parent;
  2071. ref_root = ref->root;
  2072. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  2073. if (extent_op)
  2074. flags |= extent_op->flags_to_set;
  2075. ret = alloc_reserved_file_extent(trans, parent, ref_root,
  2076. flags, ref->objectid,
  2077. ref->offset, &ins,
  2078. node->ref_mod);
  2079. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  2080. ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
  2081. ref->objectid, ref->offset,
  2082. node->ref_mod, extent_op);
  2083. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  2084. ret = __btrfs_free_extent(trans, node, parent,
  2085. ref_root, ref->objectid,
  2086. ref->offset, node->ref_mod,
  2087. extent_op);
  2088. } else {
  2089. BUG();
  2090. }
  2091. return ret;
  2092. }
  2093. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  2094. struct extent_buffer *leaf,
  2095. struct btrfs_extent_item *ei)
  2096. {
  2097. u64 flags = btrfs_extent_flags(leaf, ei);
  2098. if (extent_op->update_flags) {
  2099. flags |= extent_op->flags_to_set;
  2100. btrfs_set_extent_flags(leaf, ei, flags);
  2101. }
  2102. if (extent_op->update_key) {
  2103. struct btrfs_tree_block_info *bi;
  2104. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
  2105. bi = (struct btrfs_tree_block_info *)(ei + 1);
  2106. btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
  2107. }
  2108. }
  2109. static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
  2110. struct btrfs_delayed_ref_head *head,
  2111. struct btrfs_delayed_extent_op *extent_op)
  2112. {
  2113. struct btrfs_fs_info *fs_info = trans->fs_info;
  2114. struct btrfs_key key;
  2115. struct btrfs_path *path;
  2116. struct btrfs_extent_item *ei;
  2117. struct extent_buffer *leaf;
  2118. u32 item_size;
  2119. int ret;
  2120. int err = 0;
  2121. int metadata = !extent_op->is_data;
  2122. if (trans->aborted)
  2123. return 0;
  2124. if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  2125. metadata = 0;
  2126. path = btrfs_alloc_path();
  2127. if (!path)
  2128. return -ENOMEM;
  2129. key.objectid = head->bytenr;
  2130. if (metadata) {
  2131. key.type = BTRFS_METADATA_ITEM_KEY;
  2132. key.offset = extent_op->level;
  2133. } else {
  2134. key.type = BTRFS_EXTENT_ITEM_KEY;
  2135. key.offset = head->num_bytes;
  2136. }
  2137. again:
  2138. path->reada = READA_FORWARD;
  2139. path->leave_spinning = 1;
  2140. ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
  2141. if (ret < 0) {
  2142. err = ret;
  2143. goto out;
  2144. }
  2145. if (ret > 0) {
  2146. if (metadata) {
  2147. if (path->slots[0] > 0) {
  2148. path->slots[0]--;
  2149. btrfs_item_key_to_cpu(path->nodes[0], &key,
  2150. path->slots[0]);
  2151. if (key.objectid == head->bytenr &&
  2152. key.type == BTRFS_EXTENT_ITEM_KEY &&
  2153. key.offset == head->num_bytes)
  2154. ret = 0;
  2155. }
  2156. if (ret > 0) {
  2157. btrfs_release_path(path);
  2158. metadata = 0;
  2159. key.objectid = head->bytenr;
  2160. key.offset = head->num_bytes;
  2161. key.type = BTRFS_EXTENT_ITEM_KEY;
  2162. goto again;
  2163. }
  2164. } else {
  2165. err = -EIO;
  2166. goto out;
  2167. }
  2168. }
  2169. leaf = path->nodes[0];
  2170. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2171. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2172. if (item_size < sizeof(*ei)) {
  2173. ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
  2174. if (ret < 0) {
  2175. err = ret;
  2176. goto out;
  2177. }
  2178. leaf = path->nodes[0];
  2179. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2180. }
  2181. #endif
  2182. BUG_ON(item_size < sizeof(*ei));
  2183. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2184. __run_delayed_extent_op(extent_op, leaf, ei);
  2185. btrfs_mark_buffer_dirty(leaf);
  2186. out:
  2187. btrfs_free_path(path);
  2188. return err;
  2189. }
  2190. static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
  2191. struct btrfs_fs_info *fs_info,
  2192. struct btrfs_delayed_ref_node *node,
  2193. struct btrfs_delayed_extent_op *extent_op,
  2194. int insert_reserved)
  2195. {
  2196. int ret = 0;
  2197. struct btrfs_delayed_tree_ref *ref;
  2198. u64 parent = 0;
  2199. u64 ref_root = 0;
  2200. ref = btrfs_delayed_node_to_tree_ref(node);
  2201. trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
  2202. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  2203. parent = ref->parent;
  2204. ref_root = ref->root;
  2205. if (node->ref_mod != 1) {
  2206. btrfs_err(fs_info,
  2207. "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
  2208. node->bytenr, node->ref_mod, node->action, ref_root,
  2209. parent);
  2210. return -EIO;
  2211. }
  2212. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  2213. BUG_ON(!extent_op || !extent_op->update_flags);
  2214. ret = alloc_reserved_tree_block(trans, node, extent_op);
  2215. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  2216. ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
  2217. ref->level, 0, 1, extent_op);
  2218. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  2219. ret = __btrfs_free_extent(trans, node, parent, ref_root,
  2220. ref->level, 0, 1, extent_op);
  2221. } else {
  2222. BUG();
  2223. }
  2224. return ret;
  2225. }
  2226. /* helper function to actually process a single delayed ref entry */
  2227. static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  2228. struct btrfs_fs_info *fs_info,
  2229. struct btrfs_delayed_ref_node *node,
  2230. struct btrfs_delayed_extent_op *extent_op,
  2231. int insert_reserved)
  2232. {
  2233. int ret = 0;
  2234. if (trans->aborted) {
  2235. if (insert_reserved)
  2236. btrfs_pin_extent(fs_info, node->bytenr,
  2237. node->num_bytes, 1);
  2238. return 0;
  2239. }
  2240. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  2241. node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  2242. ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
  2243. insert_reserved);
  2244. else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  2245. node->type == BTRFS_SHARED_DATA_REF_KEY)
  2246. ret = run_delayed_data_ref(trans, node, extent_op,
  2247. insert_reserved);
  2248. else
  2249. BUG();
  2250. return ret;
  2251. }
  2252. static inline struct btrfs_delayed_ref_node *
  2253. select_delayed_ref(struct btrfs_delayed_ref_head *head)
  2254. {
  2255. struct btrfs_delayed_ref_node *ref;
  2256. if (RB_EMPTY_ROOT(&head->ref_tree))
  2257. return NULL;
  2258. /*
  2259. * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
  2260. * This is to prevent a ref count from going down to zero, which deletes
  2261. * the extent item from the extent tree, when there still are references
  2262. * to add, which would fail because they would not find the extent item.
  2263. */
  2264. if (!list_empty(&head->ref_add_list))
  2265. return list_first_entry(&head->ref_add_list,
  2266. struct btrfs_delayed_ref_node, add_list);
  2267. ref = rb_entry(rb_first(&head->ref_tree),
  2268. struct btrfs_delayed_ref_node, ref_node);
  2269. ASSERT(list_empty(&ref->add_list));
  2270. return ref;
  2271. }
  2272. static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
  2273. struct btrfs_delayed_ref_head *head)
  2274. {
  2275. spin_lock(&delayed_refs->lock);
  2276. head->processing = 0;
  2277. delayed_refs->num_heads_ready++;
  2278. spin_unlock(&delayed_refs->lock);
  2279. btrfs_delayed_ref_unlock(head);
  2280. }
  2281. static int cleanup_extent_op(struct btrfs_trans_handle *trans,
  2282. struct btrfs_delayed_ref_head *head)
  2283. {
  2284. struct btrfs_delayed_extent_op *extent_op = head->extent_op;
  2285. int ret;
  2286. if (!extent_op)
  2287. return 0;
  2288. head->extent_op = NULL;
  2289. if (head->must_insert_reserved) {
  2290. btrfs_free_delayed_extent_op(extent_op);
  2291. return 0;
  2292. }
  2293. spin_unlock(&head->lock);
  2294. ret = run_delayed_extent_op(trans, head, extent_op);
  2295. btrfs_free_delayed_extent_op(extent_op);
  2296. return ret ? ret : 1;
  2297. }
  2298. static int cleanup_ref_head(struct btrfs_trans_handle *trans,
  2299. struct btrfs_delayed_ref_head *head)
  2300. {
  2301. struct btrfs_fs_info *fs_info = trans->fs_info;
  2302. struct btrfs_delayed_ref_root *delayed_refs;
  2303. int ret;
  2304. delayed_refs = &trans->transaction->delayed_refs;
  2305. ret = cleanup_extent_op(trans, head);
  2306. if (ret < 0) {
  2307. unselect_delayed_ref_head(delayed_refs, head);
  2308. btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
  2309. return ret;
  2310. } else if (ret) {
  2311. return ret;
  2312. }
  2313. /*
  2314. * Need to drop our head ref lock and re-acquire the delayed ref lock
  2315. * and then re-check to make sure nobody got added.
  2316. */
  2317. spin_unlock(&head->lock);
  2318. spin_lock(&delayed_refs->lock);
  2319. spin_lock(&head->lock);
  2320. if (!RB_EMPTY_ROOT(&head->ref_tree) || head->extent_op) {
  2321. spin_unlock(&head->lock);
  2322. spin_unlock(&delayed_refs->lock);
  2323. return 1;
  2324. }
  2325. delayed_refs->num_heads--;
  2326. rb_erase(&head->href_node, &delayed_refs->href_root);
  2327. RB_CLEAR_NODE(&head->href_node);
  2328. spin_unlock(&head->lock);
  2329. spin_unlock(&delayed_refs->lock);
  2330. atomic_dec(&delayed_refs->num_entries);
  2331. trace_run_delayed_ref_head(fs_info, head, 0);
  2332. if (head->total_ref_mod < 0) {
  2333. struct btrfs_space_info *space_info;
  2334. u64 flags;
  2335. if (head->is_data)
  2336. flags = BTRFS_BLOCK_GROUP_DATA;
  2337. else if (head->is_system)
  2338. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  2339. else
  2340. flags = BTRFS_BLOCK_GROUP_METADATA;
  2341. space_info = __find_space_info(fs_info, flags);
  2342. ASSERT(space_info);
  2343. percpu_counter_add(&space_info->total_bytes_pinned,
  2344. -head->num_bytes);
  2345. if (head->is_data) {
  2346. spin_lock(&delayed_refs->lock);
  2347. delayed_refs->pending_csums -= head->num_bytes;
  2348. spin_unlock(&delayed_refs->lock);
  2349. }
  2350. }
  2351. if (head->must_insert_reserved) {
  2352. btrfs_pin_extent(fs_info, head->bytenr,
  2353. head->num_bytes, 1);
  2354. if (head->is_data) {
  2355. ret = btrfs_del_csums(trans, fs_info, head->bytenr,
  2356. head->num_bytes);
  2357. }
  2358. }
  2359. /* Also free its reserved qgroup space */
  2360. btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
  2361. head->qgroup_reserved);
  2362. btrfs_delayed_ref_unlock(head);
  2363. btrfs_put_delayed_ref_head(head);
  2364. return 0;
  2365. }
  2366. /*
  2367. * Returns 0 on success or if called with an already aborted transaction.
  2368. * Returns -ENOMEM or -EIO on failure and will abort the transaction.
  2369. */
  2370. static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2371. unsigned long nr)
  2372. {
  2373. struct btrfs_fs_info *fs_info = trans->fs_info;
  2374. struct btrfs_delayed_ref_root *delayed_refs;
  2375. struct btrfs_delayed_ref_node *ref;
  2376. struct btrfs_delayed_ref_head *locked_ref = NULL;
  2377. struct btrfs_delayed_extent_op *extent_op;
  2378. ktime_t start = ktime_get();
  2379. int ret;
  2380. unsigned long count = 0;
  2381. unsigned long actual_count = 0;
  2382. int must_insert_reserved = 0;
  2383. delayed_refs = &trans->transaction->delayed_refs;
  2384. while (1) {
  2385. if (!locked_ref) {
  2386. if (count >= nr)
  2387. break;
  2388. spin_lock(&delayed_refs->lock);
  2389. locked_ref = btrfs_select_ref_head(trans);
  2390. if (!locked_ref) {
  2391. spin_unlock(&delayed_refs->lock);
  2392. break;
  2393. }
  2394. /* grab the lock that says we are going to process
  2395. * all the refs for this head */
  2396. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  2397. spin_unlock(&delayed_refs->lock);
  2398. /*
  2399. * we may have dropped the spin lock to get the head
  2400. * mutex lock, and that might have given someone else
  2401. * time to free the head. If that's true, it has been
  2402. * removed from our list and we can move on.
  2403. */
  2404. if (ret == -EAGAIN) {
  2405. locked_ref = NULL;
  2406. count++;
  2407. continue;
  2408. }
  2409. }
  2410. /*
  2411. * We need to try and merge add/drops of the same ref since we
  2412. * can run into issues with relocate dropping the implicit ref
  2413. * and then it being added back again before the drop can
  2414. * finish. If we merged anything we need to re-loop so we can
  2415. * get a good ref.
  2416. * Or we can get node references of the same type that weren't
  2417. * merged when created due to bumps in the tree mod seq, and
  2418. * we need to merge them to prevent adding an inline extent
  2419. * backref before dropping it (triggering a BUG_ON at
  2420. * insert_inline_extent_backref()).
  2421. */
  2422. spin_lock(&locked_ref->lock);
  2423. btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
  2424. ref = select_delayed_ref(locked_ref);
  2425. if (ref && ref->seq &&
  2426. btrfs_check_delayed_seq(fs_info, ref->seq)) {
  2427. spin_unlock(&locked_ref->lock);
  2428. unselect_delayed_ref_head(delayed_refs, locked_ref);
  2429. locked_ref = NULL;
  2430. cond_resched();
  2431. count++;
  2432. continue;
  2433. }
  2434. /*
  2435. * We're done processing refs in this ref_head, clean everything
  2436. * up and move on to the next ref_head.
  2437. */
  2438. if (!ref) {
  2439. ret = cleanup_ref_head(trans, locked_ref);
  2440. if (ret > 0 ) {
  2441. /* We dropped our lock, we need to loop. */
  2442. ret = 0;
  2443. continue;
  2444. } else if (ret) {
  2445. return ret;
  2446. }
  2447. locked_ref = NULL;
  2448. count++;
  2449. continue;
  2450. }
  2451. actual_count++;
  2452. ref->in_tree = 0;
  2453. rb_erase(&ref->ref_node, &locked_ref->ref_tree);
  2454. RB_CLEAR_NODE(&ref->ref_node);
  2455. if (!list_empty(&ref->add_list))
  2456. list_del(&ref->add_list);
  2457. /*
  2458. * When we play the delayed ref, also correct the ref_mod on
  2459. * head
  2460. */
  2461. switch (ref->action) {
  2462. case BTRFS_ADD_DELAYED_REF:
  2463. case BTRFS_ADD_DELAYED_EXTENT:
  2464. locked_ref->ref_mod -= ref->ref_mod;
  2465. break;
  2466. case BTRFS_DROP_DELAYED_REF:
  2467. locked_ref->ref_mod += ref->ref_mod;
  2468. break;
  2469. default:
  2470. WARN_ON(1);
  2471. }
  2472. atomic_dec(&delayed_refs->num_entries);
  2473. /*
  2474. * Record the must-insert_reserved flag before we drop the spin
  2475. * lock.
  2476. */
  2477. must_insert_reserved = locked_ref->must_insert_reserved;
  2478. locked_ref->must_insert_reserved = 0;
  2479. extent_op = locked_ref->extent_op;
  2480. locked_ref->extent_op = NULL;
  2481. spin_unlock(&locked_ref->lock);
  2482. ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
  2483. must_insert_reserved);
  2484. btrfs_free_delayed_extent_op(extent_op);
  2485. if (ret) {
  2486. unselect_delayed_ref_head(delayed_refs, locked_ref);
  2487. btrfs_put_delayed_ref(ref);
  2488. btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
  2489. ret);
  2490. return ret;
  2491. }
  2492. btrfs_put_delayed_ref(ref);
  2493. count++;
  2494. cond_resched();
  2495. }
  2496. /*
  2497. * We don't want to include ref heads since we can have empty ref heads
  2498. * and those will drastically skew our runtime down since we just do
  2499. * accounting, no actual extent tree updates.
  2500. */
  2501. if (actual_count > 0) {
  2502. u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
  2503. u64 avg;
  2504. /*
  2505. * We weigh the current average higher than our current runtime
  2506. * to avoid large swings in the average.
  2507. */
  2508. spin_lock(&delayed_refs->lock);
  2509. avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
  2510. fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
  2511. spin_unlock(&delayed_refs->lock);
  2512. }
  2513. return 0;
  2514. }
  2515. #ifdef SCRAMBLE_DELAYED_REFS
  2516. /*
  2517. * Normally delayed refs get processed in ascending bytenr order. This
  2518. * correlates in most cases to the order added. To expose dependencies on this
  2519. * order, we start to process the tree in the middle instead of the beginning
  2520. */
  2521. static u64 find_middle(struct rb_root *root)
  2522. {
  2523. struct rb_node *n = root->rb_node;
  2524. struct btrfs_delayed_ref_node *entry;
  2525. int alt = 1;
  2526. u64 middle;
  2527. u64 first = 0, last = 0;
  2528. n = rb_first(root);
  2529. if (n) {
  2530. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2531. first = entry->bytenr;
  2532. }
  2533. n = rb_last(root);
  2534. if (n) {
  2535. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2536. last = entry->bytenr;
  2537. }
  2538. n = root->rb_node;
  2539. while (n) {
  2540. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2541. WARN_ON(!entry->in_tree);
  2542. middle = entry->bytenr;
  2543. if (alt)
  2544. n = n->rb_left;
  2545. else
  2546. n = n->rb_right;
  2547. alt = 1 - alt;
  2548. }
  2549. return middle;
  2550. }
  2551. #endif
  2552. static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
  2553. {
  2554. u64 num_bytes;
  2555. num_bytes = heads * (sizeof(struct btrfs_extent_item) +
  2556. sizeof(struct btrfs_extent_inline_ref));
  2557. if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  2558. num_bytes += heads * sizeof(struct btrfs_tree_block_info);
  2559. /*
  2560. * We don't ever fill up leaves all the way so multiply by 2 just to be
  2561. * closer to what we're really going to want to use.
  2562. */
  2563. return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
  2564. }
  2565. /*
  2566. * Takes the number of bytes to be csumm'ed and figures out how many leaves it
  2567. * would require to store the csums for that many bytes.
  2568. */
  2569. u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
  2570. {
  2571. u64 csum_size;
  2572. u64 num_csums_per_leaf;
  2573. u64 num_csums;
  2574. csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
  2575. num_csums_per_leaf = div64_u64(csum_size,
  2576. (u64)btrfs_super_csum_size(fs_info->super_copy));
  2577. num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
  2578. num_csums += num_csums_per_leaf - 1;
  2579. num_csums = div64_u64(num_csums, num_csums_per_leaf);
  2580. return num_csums;
  2581. }
  2582. int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
  2583. struct btrfs_fs_info *fs_info)
  2584. {
  2585. struct btrfs_block_rsv *global_rsv;
  2586. u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
  2587. u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
  2588. unsigned int num_dirty_bgs = trans->transaction->num_dirty_bgs;
  2589. u64 num_bytes, num_dirty_bgs_bytes;
  2590. int ret = 0;
  2591. num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
  2592. num_heads = heads_to_leaves(fs_info, num_heads);
  2593. if (num_heads > 1)
  2594. num_bytes += (num_heads - 1) * fs_info->nodesize;
  2595. num_bytes <<= 1;
  2596. num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
  2597. fs_info->nodesize;
  2598. num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
  2599. num_dirty_bgs);
  2600. global_rsv = &fs_info->global_block_rsv;
  2601. /*
  2602. * If we can't allocate any more chunks lets make sure we have _lots_ of
  2603. * wiggle room since running delayed refs can create more delayed refs.
  2604. */
  2605. if (global_rsv->space_info->full) {
  2606. num_dirty_bgs_bytes <<= 1;
  2607. num_bytes <<= 1;
  2608. }
  2609. spin_lock(&global_rsv->lock);
  2610. if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
  2611. ret = 1;
  2612. spin_unlock(&global_rsv->lock);
  2613. return ret;
  2614. }
  2615. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
  2616. struct btrfs_fs_info *fs_info)
  2617. {
  2618. u64 num_entries =
  2619. atomic_read(&trans->transaction->delayed_refs.num_entries);
  2620. u64 avg_runtime;
  2621. u64 val;
  2622. smp_mb();
  2623. avg_runtime = fs_info->avg_delayed_ref_runtime;
  2624. val = num_entries * avg_runtime;
  2625. if (val >= NSEC_PER_SEC)
  2626. return 1;
  2627. if (val >= NSEC_PER_SEC / 2)
  2628. return 2;
  2629. return btrfs_check_space_for_delayed_refs(trans, fs_info);
  2630. }
  2631. struct async_delayed_refs {
  2632. struct btrfs_root *root;
  2633. u64 transid;
  2634. int count;
  2635. int error;
  2636. int sync;
  2637. struct completion wait;
  2638. struct btrfs_work work;
  2639. };
  2640. static inline struct async_delayed_refs *
  2641. to_async_delayed_refs(struct btrfs_work *work)
  2642. {
  2643. return container_of(work, struct async_delayed_refs, work);
  2644. }
  2645. static void delayed_ref_async_start(struct btrfs_work *work)
  2646. {
  2647. struct async_delayed_refs *async = to_async_delayed_refs(work);
  2648. struct btrfs_trans_handle *trans;
  2649. struct btrfs_fs_info *fs_info = async->root->fs_info;
  2650. int ret;
  2651. /* if the commit is already started, we don't need to wait here */
  2652. if (btrfs_transaction_blocked(fs_info))
  2653. goto done;
  2654. trans = btrfs_join_transaction(async->root);
  2655. if (IS_ERR(trans)) {
  2656. async->error = PTR_ERR(trans);
  2657. goto done;
  2658. }
  2659. /*
  2660. * trans->sync means that when we call end_transaction, we won't
  2661. * wait on delayed refs
  2662. */
  2663. trans->sync = true;
  2664. /* Don't bother flushing if we got into a different transaction */
  2665. if (trans->transid > async->transid)
  2666. goto end;
  2667. ret = btrfs_run_delayed_refs(trans, async->count);
  2668. if (ret)
  2669. async->error = ret;
  2670. end:
  2671. ret = btrfs_end_transaction(trans);
  2672. if (ret && !async->error)
  2673. async->error = ret;
  2674. done:
  2675. if (async->sync)
  2676. complete(&async->wait);
  2677. else
  2678. kfree(async);
  2679. }
  2680. int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
  2681. unsigned long count, u64 transid, int wait)
  2682. {
  2683. struct async_delayed_refs *async;
  2684. int ret;
  2685. async = kmalloc(sizeof(*async), GFP_NOFS);
  2686. if (!async)
  2687. return -ENOMEM;
  2688. async->root = fs_info->tree_root;
  2689. async->count = count;
  2690. async->error = 0;
  2691. async->transid = transid;
  2692. if (wait)
  2693. async->sync = 1;
  2694. else
  2695. async->sync = 0;
  2696. init_completion(&async->wait);
  2697. btrfs_init_work(&async->work, btrfs_extent_refs_helper,
  2698. delayed_ref_async_start, NULL, NULL);
  2699. btrfs_queue_work(fs_info->extent_workers, &async->work);
  2700. if (wait) {
  2701. wait_for_completion(&async->wait);
  2702. ret = async->error;
  2703. kfree(async);
  2704. return ret;
  2705. }
  2706. return 0;
  2707. }
  2708. /*
  2709. * this starts processing the delayed reference count updates and
  2710. * extent insertions we have queued up so far. count can be
  2711. * 0, which means to process everything in the tree at the start
  2712. * of the run (but not newly added entries), or it can be some target
  2713. * number you'd like to process.
  2714. *
  2715. * Returns 0 on success or if called with an aborted transaction
  2716. * Returns <0 on error and aborts the transaction
  2717. */
  2718. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2719. unsigned long count)
  2720. {
  2721. struct btrfs_fs_info *fs_info = trans->fs_info;
  2722. struct rb_node *node;
  2723. struct btrfs_delayed_ref_root *delayed_refs;
  2724. struct btrfs_delayed_ref_head *head;
  2725. int ret;
  2726. int run_all = count == (unsigned long)-1;
  2727. bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
  2728. /* We'll clean this up in btrfs_cleanup_transaction */
  2729. if (trans->aborted)
  2730. return 0;
  2731. if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
  2732. return 0;
  2733. delayed_refs = &trans->transaction->delayed_refs;
  2734. if (count == 0)
  2735. count = atomic_read(&delayed_refs->num_entries) * 2;
  2736. again:
  2737. #ifdef SCRAMBLE_DELAYED_REFS
  2738. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2739. #endif
  2740. trans->can_flush_pending_bgs = false;
  2741. ret = __btrfs_run_delayed_refs(trans, count);
  2742. if (ret < 0) {
  2743. btrfs_abort_transaction(trans, ret);
  2744. return ret;
  2745. }
  2746. if (run_all) {
  2747. if (!list_empty(&trans->new_bgs))
  2748. btrfs_create_pending_block_groups(trans);
  2749. spin_lock(&delayed_refs->lock);
  2750. node = rb_first(&delayed_refs->href_root);
  2751. if (!node) {
  2752. spin_unlock(&delayed_refs->lock);
  2753. goto out;
  2754. }
  2755. head = rb_entry(node, struct btrfs_delayed_ref_head,
  2756. href_node);
  2757. refcount_inc(&head->refs);
  2758. spin_unlock(&delayed_refs->lock);
  2759. /* Mutex was contended, block until it's released and retry. */
  2760. mutex_lock(&head->mutex);
  2761. mutex_unlock(&head->mutex);
  2762. btrfs_put_delayed_ref_head(head);
  2763. cond_resched();
  2764. goto again;
  2765. }
  2766. out:
  2767. trans->can_flush_pending_bgs = can_flush_pending_bgs;
  2768. return 0;
  2769. }
  2770. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2771. struct btrfs_fs_info *fs_info,
  2772. u64 bytenr, u64 num_bytes, u64 flags,
  2773. int level, int is_data)
  2774. {
  2775. struct btrfs_delayed_extent_op *extent_op;
  2776. int ret;
  2777. extent_op = btrfs_alloc_delayed_extent_op();
  2778. if (!extent_op)
  2779. return -ENOMEM;
  2780. extent_op->flags_to_set = flags;
  2781. extent_op->update_flags = true;
  2782. extent_op->update_key = false;
  2783. extent_op->is_data = is_data ? true : false;
  2784. extent_op->level = level;
  2785. ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
  2786. num_bytes, extent_op);
  2787. if (ret)
  2788. btrfs_free_delayed_extent_op(extent_op);
  2789. return ret;
  2790. }
  2791. static noinline int check_delayed_ref(struct btrfs_root *root,
  2792. struct btrfs_path *path,
  2793. u64 objectid, u64 offset, u64 bytenr)
  2794. {
  2795. struct btrfs_delayed_ref_head *head;
  2796. struct btrfs_delayed_ref_node *ref;
  2797. struct btrfs_delayed_data_ref *data_ref;
  2798. struct btrfs_delayed_ref_root *delayed_refs;
  2799. struct btrfs_transaction *cur_trans;
  2800. struct rb_node *node;
  2801. int ret = 0;
  2802. spin_lock(&root->fs_info->trans_lock);
  2803. cur_trans = root->fs_info->running_transaction;
  2804. if (cur_trans)
  2805. refcount_inc(&cur_trans->use_count);
  2806. spin_unlock(&root->fs_info->trans_lock);
  2807. if (!cur_trans)
  2808. return 0;
  2809. delayed_refs = &cur_trans->delayed_refs;
  2810. spin_lock(&delayed_refs->lock);
  2811. head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
  2812. if (!head) {
  2813. spin_unlock(&delayed_refs->lock);
  2814. btrfs_put_transaction(cur_trans);
  2815. return 0;
  2816. }
  2817. if (!mutex_trylock(&head->mutex)) {
  2818. refcount_inc(&head->refs);
  2819. spin_unlock(&delayed_refs->lock);
  2820. btrfs_release_path(path);
  2821. /*
  2822. * Mutex was contended, block until it's released and let
  2823. * caller try again
  2824. */
  2825. mutex_lock(&head->mutex);
  2826. mutex_unlock(&head->mutex);
  2827. btrfs_put_delayed_ref_head(head);
  2828. btrfs_put_transaction(cur_trans);
  2829. return -EAGAIN;
  2830. }
  2831. spin_unlock(&delayed_refs->lock);
  2832. spin_lock(&head->lock);
  2833. /*
  2834. * XXX: We should replace this with a proper search function in the
  2835. * future.
  2836. */
  2837. for (node = rb_first(&head->ref_tree); node; node = rb_next(node)) {
  2838. ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
  2839. /* If it's a shared ref we know a cross reference exists */
  2840. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
  2841. ret = 1;
  2842. break;
  2843. }
  2844. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2845. /*
  2846. * If our ref doesn't match the one we're currently looking at
  2847. * then we have a cross reference.
  2848. */
  2849. if (data_ref->root != root->root_key.objectid ||
  2850. data_ref->objectid != objectid ||
  2851. data_ref->offset != offset) {
  2852. ret = 1;
  2853. break;
  2854. }
  2855. }
  2856. spin_unlock(&head->lock);
  2857. mutex_unlock(&head->mutex);
  2858. btrfs_put_transaction(cur_trans);
  2859. return ret;
  2860. }
  2861. static noinline int check_committed_ref(struct btrfs_root *root,
  2862. struct btrfs_path *path,
  2863. u64 objectid, u64 offset, u64 bytenr)
  2864. {
  2865. struct btrfs_fs_info *fs_info = root->fs_info;
  2866. struct btrfs_root *extent_root = fs_info->extent_root;
  2867. struct extent_buffer *leaf;
  2868. struct btrfs_extent_data_ref *ref;
  2869. struct btrfs_extent_inline_ref *iref;
  2870. struct btrfs_extent_item *ei;
  2871. struct btrfs_key key;
  2872. u32 item_size;
  2873. int type;
  2874. int ret;
  2875. key.objectid = bytenr;
  2876. key.offset = (u64)-1;
  2877. key.type = BTRFS_EXTENT_ITEM_KEY;
  2878. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2879. if (ret < 0)
  2880. goto out;
  2881. BUG_ON(ret == 0); /* Corruption */
  2882. ret = -ENOENT;
  2883. if (path->slots[0] == 0)
  2884. goto out;
  2885. path->slots[0]--;
  2886. leaf = path->nodes[0];
  2887. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2888. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2889. goto out;
  2890. ret = 1;
  2891. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2892. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2893. if (item_size < sizeof(*ei)) {
  2894. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2895. goto out;
  2896. }
  2897. #endif
  2898. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2899. if (item_size != sizeof(*ei) +
  2900. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2901. goto out;
  2902. if (btrfs_extent_generation(leaf, ei) <=
  2903. btrfs_root_last_snapshot(&root->root_item))
  2904. goto out;
  2905. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2906. type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
  2907. if (type != BTRFS_EXTENT_DATA_REF_KEY)
  2908. goto out;
  2909. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2910. if (btrfs_extent_refs(leaf, ei) !=
  2911. btrfs_extent_data_ref_count(leaf, ref) ||
  2912. btrfs_extent_data_ref_root(leaf, ref) !=
  2913. root->root_key.objectid ||
  2914. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2915. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2916. goto out;
  2917. ret = 0;
  2918. out:
  2919. return ret;
  2920. }
  2921. int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
  2922. u64 bytenr)
  2923. {
  2924. struct btrfs_path *path;
  2925. int ret;
  2926. int ret2;
  2927. path = btrfs_alloc_path();
  2928. if (!path)
  2929. return -ENOMEM;
  2930. do {
  2931. ret = check_committed_ref(root, path, objectid,
  2932. offset, bytenr);
  2933. if (ret && ret != -ENOENT)
  2934. goto out;
  2935. ret2 = check_delayed_ref(root, path, objectid,
  2936. offset, bytenr);
  2937. } while (ret2 == -EAGAIN);
  2938. if (ret2 && ret2 != -ENOENT) {
  2939. ret = ret2;
  2940. goto out;
  2941. }
  2942. if (ret != -ENOENT || ret2 != -ENOENT)
  2943. ret = 0;
  2944. out:
  2945. btrfs_free_path(path);
  2946. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2947. WARN_ON(ret > 0);
  2948. return ret;
  2949. }
  2950. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2951. struct btrfs_root *root,
  2952. struct extent_buffer *buf,
  2953. int full_backref, int inc)
  2954. {
  2955. struct btrfs_fs_info *fs_info = root->fs_info;
  2956. u64 bytenr;
  2957. u64 num_bytes;
  2958. u64 parent;
  2959. u64 ref_root;
  2960. u32 nritems;
  2961. struct btrfs_key key;
  2962. struct btrfs_file_extent_item *fi;
  2963. int i;
  2964. int level;
  2965. int ret = 0;
  2966. int (*process_func)(struct btrfs_trans_handle *,
  2967. struct btrfs_root *,
  2968. u64, u64, u64, u64, u64, u64);
  2969. if (btrfs_is_testing(fs_info))
  2970. return 0;
  2971. ref_root = btrfs_header_owner(buf);
  2972. nritems = btrfs_header_nritems(buf);
  2973. level = btrfs_header_level(buf);
  2974. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
  2975. return 0;
  2976. if (inc)
  2977. process_func = btrfs_inc_extent_ref;
  2978. else
  2979. process_func = btrfs_free_extent;
  2980. if (full_backref)
  2981. parent = buf->start;
  2982. else
  2983. parent = 0;
  2984. for (i = 0; i < nritems; i++) {
  2985. if (level == 0) {
  2986. btrfs_item_key_to_cpu(buf, &key, i);
  2987. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2988. continue;
  2989. fi = btrfs_item_ptr(buf, i,
  2990. struct btrfs_file_extent_item);
  2991. if (btrfs_file_extent_type(buf, fi) ==
  2992. BTRFS_FILE_EXTENT_INLINE)
  2993. continue;
  2994. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2995. if (bytenr == 0)
  2996. continue;
  2997. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2998. key.offset -= btrfs_file_extent_offset(buf, fi);
  2999. ret = process_func(trans, root, bytenr, num_bytes,
  3000. parent, ref_root, key.objectid,
  3001. key.offset);
  3002. if (ret)
  3003. goto fail;
  3004. } else {
  3005. bytenr = btrfs_node_blockptr(buf, i);
  3006. num_bytes = fs_info->nodesize;
  3007. ret = process_func(trans, root, bytenr, num_bytes,
  3008. parent, ref_root, level - 1, 0);
  3009. if (ret)
  3010. goto fail;
  3011. }
  3012. }
  3013. return 0;
  3014. fail:
  3015. return ret;
  3016. }
  3017. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  3018. struct extent_buffer *buf, int full_backref)
  3019. {
  3020. return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
  3021. }
  3022. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  3023. struct extent_buffer *buf, int full_backref)
  3024. {
  3025. return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
  3026. }
  3027. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  3028. struct btrfs_fs_info *fs_info,
  3029. struct btrfs_path *path,
  3030. struct btrfs_block_group_cache *cache)
  3031. {
  3032. int ret;
  3033. struct btrfs_root *extent_root = fs_info->extent_root;
  3034. unsigned long bi;
  3035. struct extent_buffer *leaf;
  3036. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  3037. if (ret) {
  3038. if (ret > 0)
  3039. ret = -ENOENT;
  3040. goto fail;
  3041. }
  3042. leaf = path->nodes[0];
  3043. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  3044. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  3045. btrfs_mark_buffer_dirty(leaf);
  3046. fail:
  3047. btrfs_release_path(path);
  3048. return ret;
  3049. }
  3050. static struct btrfs_block_group_cache *
  3051. next_block_group(struct btrfs_fs_info *fs_info,
  3052. struct btrfs_block_group_cache *cache)
  3053. {
  3054. struct rb_node *node;
  3055. spin_lock(&fs_info->block_group_cache_lock);
  3056. /* If our block group was removed, we need a full search. */
  3057. if (RB_EMPTY_NODE(&cache->cache_node)) {
  3058. const u64 next_bytenr = cache->key.objectid + cache->key.offset;
  3059. spin_unlock(&fs_info->block_group_cache_lock);
  3060. btrfs_put_block_group(cache);
  3061. cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
  3062. }
  3063. node = rb_next(&cache->cache_node);
  3064. btrfs_put_block_group(cache);
  3065. if (node) {
  3066. cache = rb_entry(node, struct btrfs_block_group_cache,
  3067. cache_node);
  3068. btrfs_get_block_group(cache);
  3069. } else
  3070. cache = NULL;
  3071. spin_unlock(&fs_info->block_group_cache_lock);
  3072. return cache;
  3073. }
  3074. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  3075. struct btrfs_trans_handle *trans,
  3076. struct btrfs_path *path)
  3077. {
  3078. struct btrfs_fs_info *fs_info = block_group->fs_info;
  3079. struct btrfs_root *root = fs_info->tree_root;
  3080. struct inode *inode = NULL;
  3081. struct extent_changeset *data_reserved = NULL;
  3082. u64 alloc_hint = 0;
  3083. int dcs = BTRFS_DC_ERROR;
  3084. u64 num_pages = 0;
  3085. int retries = 0;
  3086. int ret = 0;
  3087. /*
  3088. * If this block group is smaller than 100 megs don't bother caching the
  3089. * block group.
  3090. */
  3091. if (block_group->key.offset < (100 * SZ_1M)) {
  3092. spin_lock(&block_group->lock);
  3093. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  3094. spin_unlock(&block_group->lock);
  3095. return 0;
  3096. }
  3097. if (trans->aborted)
  3098. return 0;
  3099. again:
  3100. inode = lookup_free_space_inode(fs_info, block_group, path);
  3101. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  3102. ret = PTR_ERR(inode);
  3103. btrfs_release_path(path);
  3104. goto out;
  3105. }
  3106. if (IS_ERR(inode)) {
  3107. BUG_ON(retries);
  3108. retries++;
  3109. if (block_group->ro)
  3110. goto out_free;
  3111. ret = create_free_space_inode(fs_info, trans, block_group,
  3112. path);
  3113. if (ret)
  3114. goto out_free;
  3115. goto again;
  3116. }
  3117. /*
  3118. * We want to set the generation to 0, that way if anything goes wrong
  3119. * from here on out we know not to trust this cache when we load up next
  3120. * time.
  3121. */
  3122. BTRFS_I(inode)->generation = 0;
  3123. ret = btrfs_update_inode(trans, root, inode);
  3124. if (ret) {
  3125. /*
  3126. * So theoretically we could recover from this, simply set the
  3127. * super cache generation to 0 so we know to invalidate the
  3128. * cache, but then we'd have to keep track of the block groups
  3129. * that fail this way so we know we _have_ to reset this cache
  3130. * before the next commit or risk reading stale cache. So to
  3131. * limit our exposure to horrible edge cases lets just abort the
  3132. * transaction, this only happens in really bad situations
  3133. * anyway.
  3134. */
  3135. btrfs_abort_transaction(trans, ret);
  3136. goto out_put;
  3137. }
  3138. WARN_ON(ret);
  3139. /* We've already setup this transaction, go ahead and exit */
  3140. if (block_group->cache_generation == trans->transid &&
  3141. i_size_read(inode)) {
  3142. dcs = BTRFS_DC_SETUP;
  3143. goto out_put;
  3144. }
  3145. if (i_size_read(inode) > 0) {
  3146. ret = btrfs_check_trunc_cache_free_space(fs_info,
  3147. &fs_info->global_block_rsv);
  3148. if (ret)
  3149. goto out_put;
  3150. ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
  3151. if (ret)
  3152. goto out_put;
  3153. }
  3154. spin_lock(&block_group->lock);
  3155. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  3156. !btrfs_test_opt(fs_info, SPACE_CACHE)) {
  3157. /*
  3158. * don't bother trying to write stuff out _if_
  3159. * a) we're not cached,
  3160. * b) we're with nospace_cache mount option,
  3161. * c) we're with v2 space_cache (FREE_SPACE_TREE).
  3162. */
  3163. dcs = BTRFS_DC_WRITTEN;
  3164. spin_unlock(&block_group->lock);
  3165. goto out_put;
  3166. }
  3167. spin_unlock(&block_group->lock);
  3168. /*
  3169. * We hit an ENOSPC when setting up the cache in this transaction, just
  3170. * skip doing the setup, we've already cleared the cache so we're safe.
  3171. */
  3172. if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
  3173. ret = -ENOSPC;
  3174. goto out_put;
  3175. }
  3176. /*
  3177. * Try to preallocate enough space based on how big the block group is.
  3178. * Keep in mind this has to include any pinned space which could end up
  3179. * taking up quite a bit since it's not folded into the other space
  3180. * cache.
  3181. */
  3182. num_pages = div_u64(block_group->key.offset, SZ_256M);
  3183. if (!num_pages)
  3184. num_pages = 1;
  3185. num_pages *= 16;
  3186. num_pages *= PAGE_SIZE;
  3187. ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages);
  3188. if (ret)
  3189. goto out_put;
  3190. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  3191. num_pages, num_pages,
  3192. &alloc_hint);
  3193. /*
  3194. * Our cache requires contiguous chunks so that we don't modify a bunch
  3195. * of metadata or split extents when writing the cache out, which means
  3196. * we can enospc if we are heavily fragmented in addition to just normal
  3197. * out of space conditions. So if we hit this just skip setting up any
  3198. * other block groups for this transaction, maybe we'll unpin enough
  3199. * space the next time around.
  3200. */
  3201. if (!ret)
  3202. dcs = BTRFS_DC_SETUP;
  3203. else if (ret == -ENOSPC)
  3204. set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
  3205. out_put:
  3206. iput(inode);
  3207. out_free:
  3208. btrfs_release_path(path);
  3209. out:
  3210. spin_lock(&block_group->lock);
  3211. if (!ret && dcs == BTRFS_DC_SETUP)
  3212. block_group->cache_generation = trans->transid;
  3213. block_group->disk_cache_state = dcs;
  3214. spin_unlock(&block_group->lock);
  3215. extent_changeset_free(data_reserved);
  3216. return ret;
  3217. }
  3218. int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
  3219. struct btrfs_fs_info *fs_info)
  3220. {
  3221. struct btrfs_block_group_cache *cache, *tmp;
  3222. struct btrfs_transaction *cur_trans = trans->transaction;
  3223. struct btrfs_path *path;
  3224. if (list_empty(&cur_trans->dirty_bgs) ||
  3225. !btrfs_test_opt(fs_info, SPACE_CACHE))
  3226. return 0;
  3227. path = btrfs_alloc_path();
  3228. if (!path)
  3229. return -ENOMEM;
  3230. /* Could add new block groups, use _safe just in case */
  3231. list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
  3232. dirty_list) {
  3233. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  3234. cache_save_setup(cache, trans, path);
  3235. }
  3236. btrfs_free_path(path);
  3237. return 0;
  3238. }
  3239. /*
  3240. * transaction commit does final block group cache writeback during a
  3241. * critical section where nothing is allowed to change the FS. This is
  3242. * required in order for the cache to actually match the block group,
  3243. * but can introduce a lot of latency into the commit.
  3244. *
  3245. * So, btrfs_start_dirty_block_groups is here to kick off block group
  3246. * cache IO. There's a chance we'll have to redo some of it if the
  3247. * block group changes again during the commit, but it greatly reduces
  3248. * the commit latency by getting rid of the easy block groups while
  3249. * we're still allowing others to join the commit.
  3250. */
  3251. int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
  3252. {
  3253. struct btrfs_fs_info *fs_info = trans->fs_info;
  3254. struct btrfs_block_group_cache *cache;
  3255. struct btrfs_transaction *cur_trans = trans->transaction;
  3256. int ret = 0;
  3257. int should_put;
  3258. struct btrfs_path *path = NULL;
  3259. LIST_HEAD(dirty);
  3260. struct list_head *io = &cur_trans->io_bgs;
  3261. int num_started = 0;
  3262. int loops = 0;
  3263. spin_lock(&cur_trans->dirty_bgs_lock);
  3264. if (list_empty(&cur_trans->dirty_bgs)) {
  3265. spin_unlock(&cur_trans->dirty_bgs_lock);
  3266. return 0;
  3267. }
  3268. list_splice_init(&cur_trans->dirty_bgs, &dirty);
  3269. spin_unlock(&cur_trans->dirty_bgs_lock);
  3270. again:
  3271. /*
  3272. * make sure all the block groups on our dirty list actually
  3273. * exist
  3274. */
  3275. btrfs_create_pending_block_groups(trans);
  3276. if (!path) {
  3277. path = btrfs_alloc_path();
  3278. if (!path)
  3279. return -ENOMEM;
  3280. }
  3281. /*
  3282. * cache_write_mutex is here only to save us from balance or automatic
  3283. * removal of empty block groups deleting this block group while we are
  3284. * writing out the cache
  3285. */
  3286. mutex_lock(&trans->transaction->cache_write_mutex);
  3287. while (!list_empty(&dirty)) {
  3288. cache = list_first_entry(&dirty,
  3289. struct btrfs_block_group_cache,
  3290. dirty_list);
  3291. /*
  3292. * this can happen if something re-dirties a block
  3293. * group that is already under IO. Just wait for it to
  3294. * finish and then do it all again
  3295. */
  3296. if (!list_empty(&cache->io_list)) {
  3297. list_del_init(&cache->io_list);
  3298. btrfs_wait_cache_io(trans, cache, path);
  3299. btrfs_put_block_group(cache);
  3300. }
  3301. /*
  3302. * btrfs_wait_cache_io uses the cache->dirty_list to decide
  3303. * if it should update the cache_state. Don't delete
  3304. * until after we wait.
  3305. *
  3306. * Since we're not running in the commit critical section
  3307. * we need the dirty_bgs_lock to protect from update_block_group
  3308. */
  3309. spin_lock(&cur_trans->dirty_bgs_lock);
  3310. list_del_init(&cache->dirty_list);
  3311. spin_unlock(&cur_trans->dirty_bgs_lock);
  3312. should_put = 1;
  3313. cache_save_setup(cache, trans, path);
  3314. if (cache->disk_cache_state == BTRFS_DC_SETUP) {
  3315. cache->io_ctl.inode = NULL;
  3316. ret = btrfs_write_out_cache(fs_info, trans,
  3317. cache, path);
  3318. if (ret == 0 && cache->io_ctl.inode) {
  3319. num_started++;
  3320. should_put = 0;
  3321. /*
  3322. * The cache_write_mutex is protecting the
  3323. * io_list, also refer to the definition of
  3324. * btrfs_transaction::io_bgs for more details
  3325. */
  3326. list_add_tail(&cache->io_list, io);
  3327. } else {
  3328. /*
  3329. * if we failed to write the cache, the
  3330. * generation will be bad and life goes on
  3331. */
  3332. ret = 0;
  3333. }
  3334. }
  3335. if (!ret) {
  3336. ret = write_one_cache_group(trans, fs_info,
  3337. path, cache);
  3338. /*
  3339. * Our block group might still be attached to the list
  3340. * of new block groups in the transaction handle of some
  3341. * other task (struct btrfs_trans_handle->new_bgs). This
  3342. * means its block group item isn't yet in the extent
  3343. * tree. If this happens ignore the error, as we will
  3344. * try again later in the critical section of the
  3345. * transaction commit.
  3346. */
  3347. if (ret == -ENOENT) {
  3348. ret = 0;
  3349. spin_lock(&cur_trans->dirty_bgs_lock);
  3350. if (list_empty(&cache->dirty_list)) {
  3351. list_add_tail(&cache->dirty_list,
  3352. &cur_trans->dirty_bgs);
  3353. btrfs_get_block_group(cache);
  3354. }
  3355. spin_unlock(&cur_trans->dirty_bgs_lock);
  3356. } else if (ret) {
  3357. btrfs_abort_transaction(trans, ret);
  3358. }
  3359. }
  3360. /* if its not on the io list, we need to put the block group */
  3361. if (should_put)
  3362. btrfs_put_block_group(cache);
  3363. if (ret)
  3364. break;
  3365. /*
  3366. * Avoid blocking other tasks for too long. It might even save
  3367. * us from writing caches for block groups that are going to be
  3368. * removed.
  3369. */
  3370. mutex_unlock(&trans->transaction->cache_write_mutex);
  3371. mutex_lock(&trans->transaction->cache_write_mutex);
  3372. }
  3373. mutex_unlock(&trans->transaction->cache_write_mutex);
  3374. /*
  3375. * go through delayed refs for all the stuff we've just kicked off
  3376. * and then loop back (just once)
  3377. */
  3378. ret = btrfs_run_delayed_refs(trans, 0);
  3379. if (!ret && loops == 0) {
  3380. loops++;
  3381. spin_lock(&cur_trans->dirty_bgs_lock);
  3382. list_splice_init(&cur_trans->dirty_bgs, &dirty);
  3383. /*
  3384. * dirty_bgs_lock protects us from concurrent block group
  3385. * deletes too (not just cache_write_mutex).
  3386. */
  3387. if (!list_empty(&dirty)) {
  3388. spin_unlock(&cur_trans->dirty_bgs_lock);
  3389. goto again;
  3390. }
  3391. spin_unlock(&cur_trans->dirty_bgs_lock);
  3392. } else if (ret < 0) {
  3393. btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
  3394. }
  3395. btrfs_free_path(path);
  3396. return ret;
  3397. }
  3398. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  3399. struct btrfs_fs_info *fs_info)
  3400. {
  3401. struct btrfs_block_group_cache *cache;
  3402. struct btrfs_transaction *cur_trans = trans->transaction;
  3403. int ret = 0;
  3404. int should_put;
  3405. struct btrfs_path *path;
  3406. struct list_head *io = &cur_trans->io_bgs;
  3407. int num_started = 0;
  3408. path = btrfs_alloc_path();
  3409. if (!path)
  3410. return -ENOMEM;
  3411. /*
  3412. * Even though we are in the critical section of the transaction commit,
  3413. * we can still have concurrent tasks adding elements to this
  3414. * transaction's list of dirty block groups. These tasks correspond to
  3415. * endio free space workers started when writeback finishes for a
  3416. * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
  3417. * allocate new block groups as a result of COWing nodes of the root
  3418. * tree when updating the free space inode. The writeback for the space
  3419. * caches is triggered by an earlier call to
  3420. * btrfs_start_dirty_block_groups() and iterations of the following
  3421. * loop.
  3422. * Also we want to do the cache_save_setup first and then run the
  3423. * delayed refs to make sure we have the best chance at doing this all
  3424. * in one shot.
  3425. */
  3426. spin_lock(&cur_trans->dirty_bgs_lock);
  3427. while (!list_empty(&cur_trans->dirty_bgs)) {
  3428. cache = list_first_entry(&cur_trans->dirty_bgs,
  3429. struct btrfs_block_group_cache,
  3430. dirty_list);
  3431. /*
  3432. * this can happen if cache_save_setup re-dirties a block
  3433. * group that is already under IO. Just wait for it to
  3434. * finish and then do it all again
  3435. */
  3436. if (!list_empty(&cache->io_list)) {
  3437. spin_unlock(&cur_trans->dirty_bgs_lock);
  3438. list_del_init(&cache->io_list);
  3439. btrfs_wait_cache_io(trans, cache, path);
  3440. btrfs_put_block_group(cache);
  3441. spin_lock(&cur_trans->dirty_bgs_lock);
  3442. }
  3443. /*
  3444. * don't remove from the dirty list until after we've waited
  3445. * on any pending IO
  3446. */
  3447. list_del_init(&cache->dirty_list);
  3448. spin_unlock(&cur_trans->dirty_bgs_lock);
  3449. should_put = 1;
  3450. cache_save_setup(cache, trans, path);
  3451. if (!ret)
  3452. ret = btrfs_run_delayed_refs(trans,
  3453. (unsigned long) -1);
  3454. if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
  3455. cache->io_ctl.inode = NULL;
  3456. ret = btrfs_write_out_cache(fs_info, trans,
  3457. cache, path);
  3458. if (ret == 0 && cache->io_ctl.inode) {
  3459. num_started++;
  3460. should_put = 0;
  3461. list_add_tail(&cache->io_list, io);
  3462. } else {
  3463. /*
  3464. * if we failed to write the cache, the
  3465. * generation will be bad and life goes on
  3466. */
  3467. ret = 0;
  3468. }
  3469. }
  3470. if (!ret) {
  3471. ret = write_one_cache_group(trans, fs_info,
  3472. path, cache);
  3473. /*
  3474. * One of the free space endio workers might have
  3475. * created a new block group while updating a free space
  3476. * cache's inode (at inode.c:btrfs_finish_ordered_io())
  3477. * and hasn't released its transaction handle yet, in
  3478. * which case the new block group is still attached to
  3479. * its transaction handle and its creation has not
  3480. * finished yet (no block group item in the extent tree
  3481. * yet, etc). If this is the case, wait for all free
  3482. * space endio workers to finish and retry. This is a
  3483. * a very rare case so no need for a more efficient and
  3484. * complex approach.
  3485. */
  3486. if (ret == -ENOENT) {
  3487. wait_event(cur_trans->writer_wait,
  3488. atomic_read(&cur_trans->num_writers) == 1);
  3489. ret = write_one_cache_group(trans, fs_info,
  3490. path, cache);
  3491. }
  3492. if (ret)
  3493. btrfs_abort_transaction(trans, ret);
  3494. }
  3495. /* if its not on the io list, we need to put the block group */
  3496. if (should_put)
  3497. btrfs_put_block_group(cache);
  3498. spin_lock(&cur_trans->dirty_bgs_lock);
  3499. }
  3500. spin_unlock(&cur_trans->dirty_bgs_lock);
  3501. /*
  3502. * Refer to the definition of io_bgs member for details why it's safe
  3503. * to use it without any locking
  3504. */
  3505. while (!list_empty(io)) {
  3506. cache = list_first_entry(io, struct btrfs_block_group_cache,
  3507. io_list);
  3508. list_del_init(&cache->io_list);
  3509. btrfs_wait_cache_io(trans, cache, path);
  3510. btrfs_put_block_group(cache);
  3511. }
  3512. btrfs_free_path(path);
  3513. return ret;
  3514. }
  3515. int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
  3516. {
  3517. struct btrfs_block_group_cache *block_group;
  3518. int readonly = 0;
  3519. block_group = btrfs_lookup_block_group(fs_info, bytenr);
  3520. if (!block_group || block_group->ro)
  3521. readonly = 1;
  3522. if (block_group)
  3523. btrfs_put_block_group(block_group);
  3524. return readonly;
  3525. }
  3526. bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
  3527. {
  3528. struct btrfs_block_group_cache *bg;
  3529. bool ret = true;
  3530. bg = btrfs_lookup_block_group(fs_info, bytenr);
  3531. if (!bg)
  3532. return false;
  3533. spin_lock(&bg->lock);
  3534. if (bg->ro)
  3535. ret = false;
  3536. else
  3537. atomic_inc(&bg->nocow_writers);
  3538. spin_unlock(&bg->lock);
  3539. /* no put on block group, done by btrfs_dec_nocow_writers */
  3540. if (!ret)
  3541. btrfs_put_block_group(bg);
  3542. return ret;
  3543. }
  3544. void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
  3545. {
  3546. struct btrfs_block_group_cache *bg;
  3547. bg = btrfs_lookup_block_group(fs_info, bytenr);
  3548. ASSERT(bg);
  3549. if (atomic_dec_and_test(&bg->nocow_writers))
  3550. wake_up_var(&bg->nocow_writers);
  3551. /*
  3552. * Once for our lookup and once for the lookup done by a previous call
  3553. * to btrfs_inc_nocow_writers()
  3554. */
  3555. btrfs_put_block_group(bg);
  3556. btrfs_put_block_group(bg);
  3557. }
  3558. void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg)
  3559. {
  3560. wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
  3561. }
  3562. static const char *alloc_name(u64 flags)
  3563. {
  3564. switch (flags) {
  3565. case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
  3566. return "mixed";
  3567. case BTRFS_BLOCK_GROUP_METADATA:
  3568. return "metadata";
  3569. case BTRFS_BLOCK_GROUP_DATA:
  3570. return "data";
  3571. case BTRFS_BLOCK_GROUP_SYSTEM:
  3572. return "system";
  3573. default:
  3574. WARN_ON(1);
  3575. return "invalid-combination";
  3576. };
  3577. }
  3578. static int create_space_info(struct btrfs_fs_info *info, u64 flags)
  3579. {
  3580. struct btrfs_space_info *space_info;
  3581. int i;
  3582. int ret;
  3583. space_info = kzalloc(sizeof(*space_info), GFP_NOFS);
  3584. if (!space_info)
  3585. return -ENOMEM;
  3586. ret = percpu_counter_init(&space_info->total_bytes_pinned, 0,
  3587. GFP_KERNEL);
  3588. if (ret) {
  3589. kfree(space_info);
  3590. return ret;
  3591. }
  3592. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  3593. INIT_LIST_HEAD(&space_info->block_groups[i]);
  3594. init_rwsem(&space_info->groups_sem);
  3595. spin_lock_init(&space_info->lock);
  3596. space_info->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  3597. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3598. init_waitqueue_head(&space_info->wait);
  3599. INIT_LIST_HEAD(&space_info->ro_bgs);
  3600. INIT_LIST_HEAD(&space_info->tickets);
  3601. INIT_LIST_HEAD(&space_info->priority_tickets);
  3602. ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
  3603. info->space_info_kobj, "%s",
  3604. alloc_name(space_info->flags));
  3605. if (ret) {
  3606. percpu_counter_destroy(&space_info->total_bytes_pinned);
  3607. kfree(space_info);
  3608. return ret;
  3609. }
  3610. list_add_rcu(&space_info->list, &info->space_info);
  3611. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3612. info->data_sinfo = space_info;
  3613. return ret;
  3614. }
  3615. static void update_space_info(struct btrfs_fs_info *info, u64 flags,
  3616. u64 total_bytes, u64 bytes_used,
  3617. u64 bytes_readonly,
  3618. struct btrfs_space_info **space_info)
  3619. {
  3620. struct btrfs_space_info *found;
  3621. int factor;
  3622. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  3623. BTRFS_BLOCK_GROUP_RAID10))
  3624. factor = 2;
  3625. else
  3626. factor = 1;
  3627. found = __find_space_info(info, flags);
  3628. ASSERT(found);
  3629. spin_lock(&found->lock);
  3630. found->total_bytes += total_bytes;
  3631. found->disk_total += total_bytes * factor;
  3632. found->bytes_used += bytes_used;
  3633. found->disk_used += bytes_used * factor;
  3634. found->bytes_readonly += bytes_readonly;
  3635. if (total_bytes > 0)
  3636. found->full = 0;
  3637. space_info_add_new_bytes(info, found, total_bytes -
  3638. bytes_used - bytes_readonly);
  3639. spin_unlock(&found->lock);
  3640. *space_info = found;
  3641. }
  3642. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  3643. {
  3644. u64 extra_flags = chunk_to_extended(flags) &
  3645. BTRFS_EXTENDED_PROFILE_MASK;
  3646. write_seqlock(&fs_info->profiles_lock);
  3647. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3648. fs_info->avail_data_alloc_bits |= extra_flags;
  3649. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3650. fs_info->avail_metadata_alloc_bits |= extra_flags;
  3651. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3652. fs_info->avail_system_alloc_bits |= extra_flags;
  3653. write_sequnlock(&fs_info->profiles_lock);
  3654. }
  3655. /*
  3656. * returns target flags in extended format or 0 if restripe for this
  3657. * chunk_type is not in progress
  3658. *
  3659. * should be called with balance_lock held
  3660. */
  3661. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  3662. {
  3663. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3664. u64 target = 0;
  3665. if (!bctl)
  3666. return 0;
  3667. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  3668. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3669. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  3670. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  3671. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3672. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  3673. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  3674. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3675. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  3676. }
  3677. return target;
  3678. }
  3679. /*
  3680. * @flags: available profiles in extended format (see ctree.h)
  3681. *
  3682. * Returns reduced profile in chunk format. If profile changing is in
  3683. * progress (either running or paused) picks the target profile (if it's
  3684. * already available), otherwise falls back to plain reducing.
  3685. */
  3686. static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
  3687. {
  3688. u64 num_devices = fs_info->fs_devices->rw_devices;
  3689. u64 target;
  3690. u64 raid_type;
  3691. u64 allowed = 0;
  3692. /*
  3693. * see if restripe for this chunk_type is in progress, if so
  3694. * try to reduce to the target profile
  3695. */
  3696. spin_lock(&fs_info->balance_lock);
  3697. target = get_restripe_target(fs_info, flags);
  3698. if (target) {
  3699. /* pick target profile only if it's already available */
  3700. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  3701. spin_unlock(&fs_info->balance_lock);
  3702. return extended_to_chunk(target);
  3703. }
  3704. }
  3705. spin_unlock(&fs_info->balance_lock);
  3706. /* First, mask out the RAID levels which aren't possible */
  3707. for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
  3708. if (num_devices >= btrfs_raid_array[raid_type].devs_min)
  3709. allowed |= btrfs_raid_array[raid_type].bg_flag;
  3710. }
  3711. allowed &= flags;
  3712. if (allowed & BTRFS_BLOCK_GROUP_RAID6)
  3713. allowed = BTRFS_BLOCK_GROUP_RAID6;
  3714. else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
  3715. allowed = BTRFS_BLOCK_GROUP_RAID5;
  3716. else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
  3717. allowed = BTRFS_BLOCK_GROUP_RAID10;
  3718. else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
  3719. allowed = BTRFS_BLOCK_GROUP_RAID1;
  3720. else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
  3721. allowed = BTRFS_BLOCK_GROUP_RAID0;
  3722. flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
  3723. return extended_to_chunk(flags | allowed);
  3724. }
  3725. static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
  3726. {
  3727. unsigned seq;
  3728. u64 flags;
  3729. do {
  3730. flags = orig_flags;
  3731. seq = read_seqbegin(&fs_info->profiles_lock);
  3732. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3733. flags |= fs_info->avail_data_alloc_bits;
  3734. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3735. flags |= fs_info->avail_system_alloc_bits;
  3736. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3737. flags |= fs_info->avail_metadata_alloc_bits;
  3738. } while (read_seqretry(&fs_info->profiles_lock, seq));
  3739. return btrfs_reduce_alloc_profile(fs_info, flags);
  3740. }
  3741. static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
  3742. {
  3743. struct btrfs_fs_info *fs_info = root->fs_info;
  3744. u64 flags;
  3745. u64 ret;
  3746. if (data)
  3747. flags = BTRFS_BLOCK_GROUP_DATA;
  3748. else if (root == fs_info->chunk_root)
  3749. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  3750. else
  3751. flags = BTRFS_BLOCK_GROUP_METADATA;
  3752. ret = get_alloc_profile(fs_info, flags);
  3753. return ret;
  3754. }
  3755. u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
  3756. {
  3757. return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
  3758. }
  3759. u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
  3760. {
  3761. return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  3762. }
  3763. u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info)
  3764. {
  3765. return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3766. }
  3767. static u64 btrfs_space_info_used(struct btrfs_space_info *s_info,
  3768. bool may_use_included)
  3769. {
  3770. ASSERT(s_info);
  3771. return s_info->bytes_used + s_info->bytes_reserved +
  3772. s_info->bytes_pinned + s_info->bytes_readonly +
  3773. (may_use_included ? s_info->bytes_may_use : 0);
  3774. }
  3775. int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
  3776. {
  3777. struct btrfs_root *root = inode->root;
  3778. struct btrfs_fs_info *fs_info = root->fs_info;
  3779. struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
  3780. u64 used;
  3781. int ret = 0;
  3782. int need_commit = 2;
  3783. int have_pinned_space;
  3784. /* make sure bytes are sectorsize aligned */
  3785. bytes = ALIGN(bytes, fs_info->sectorsize);
  3786. if (btrfs_is_free_space_inode(inode)) {
  3787. need_commit = 0;
  3788. ASSERT(current->journal_info);
  3789. }
  3790. again:
  3791. /* make sure we have enough space to handle the data first */
  3792. spin_lock(&data_sinfo->lock);
  3793. used = btrfs_space_info_used(data_sinfo, true);
  3794. if (used + bytes > data_sinfo->total_bytes) {
  3795. struct btrfs_trans_handle *trans;
  3796. /*
  3797. * if we don't have enough free bytes in this space then we need
  3798. * to alloc a new chunk.
  3799. */
  3800. if (!data_sinfo->full) {
  3801. u64 alloc_target;
  3802. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3803. spin_unlock(&data_sinfo->lock);
  3804. alloc_target = btrfs_data_alloc_profile(fs_info);
  3805. /*
  3806. * It is ugly that we don't call nolock join
  3807. * transaction for the free space inode case here.
  3808. * But it is safe because we only do the data space
  3809. * reservation for the free space cache in the
  3810. * transaction context, the common join transaction
  3811. * just increase the counter of the current transaction
  3812. * handler, doesn't try to acquire the trans_lock of
  3813. * the fs.
  3814. */
  3815. trans = btrfs_join_transaction(root);
  3816. if (IS_ERR(trans))
  3817. return PTR_ERR(trans);
  3818. ret = do_chunk_alloc(trans, fs_info, alloc_target,
  3819. CHUNK_ALLOC_NO_FORCE);
  3820. btrfs_end_transaction(trans);
  3821. if (ret < 0) {
  3822. if (ret != -ENOSPC)
  3823. return ret;
  3824. else {
  3825. have_pinned_space = 1;
  3826. goto commit_trans;
  3827. }
  3828. }
  3829. goto again;
  3830. }
  3831. /*
  3832. * If we don't have enough pinned space to deal with this
  3833. * allocation, and no removed chunk in current transaction,
  3834. * don't bother committing the transaction.
  3835. */
  3836. have_pinned_space = percpu_counter_compare(
  3837. &data_sinfo->total_bytes_pinned,
  3838. used + bytes - data_sinfo->total_bytes);
  3839. spin_unlock(&data_sinfo->lock);
  3840. /* commit the current transaction and try again */
  3841. commit_trans:
  3842. if (need_commit) {
  3843. need_commit--;
  3844. if (need_commit > 0) {
  3845. btrfs_start_delalloc_roots(fs_info, -1);
  3846. btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
  3847. (u64)-1);
  3848. }
  3849. trans = btrfs_join_transaction(root);
  3850. if (IS_ERR(trans))
  3851. return PTR_ERR(trans);
  3852. if (have_pinned_space >= 0 ||
  3853. test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
  3854. &trans->transaction->flags) ||
  3855. need_commit > 0) {
  3856. ret = btrfs_commit_transaction(trans);
  3857. if (ret)
  3858. return ret;
  3859. /*
  3860. * The cleaner kthread might still be doing iput
  3861. * operations. Wait for it to finish so that
  3862. * more space is released.
  3863. */
  3864. mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
  3865. mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
  3866. goto again;
  3867. } else {
  3868. btrfs_end_transaction(trans);
  3869. }
  3870. }
  3871. trace_btrfs_space_reservation(fs_info,
  3872. "space_info:enospc",
  3873. data_sinfo->flags, bytes, 1);
  3874. return -ENOSPC;
  3875. }
  3876. data_sinfo->bytes_may_use += bytes;
  3877. trace_btrfs_space_reservation(fs_info, "space_info",
  3878. data_sinfo->flags, bytes, 1);
  3879. spin_unlock(&data_sinfo->lock);
  3880. return ret;
  3881. }
  3882. int btrfs_check_data_free_space(struct inode *inode,
  3883. struct extent_changeset **reserved, u64 start, u64 len)
  3884. {
  3885. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3886. int ret;
  3887. /* align the range */
  3888. len = round_up(start + len, fs_info->sectorsize) -
  3889. round_down(start, fs_info->sectorsize);
  3890. start = round_down(start, fs_info->sectorsize);
  3891. ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
  3892. if (ret < 0)
  3893. return ret;
  3894. /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
  3895. ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
  3896. if (ret < 0)
  3897. btrfs_free_reserved_data_space_noquota(inode, start, len);
  3898. else
  3899. ret = 0;
  3900. return ret;
  3901. }
  3902. /*
  3903. * Called if we need to clear a data reservation for this inode
  3904. * Normally in a error case.
  3905. *
  3906. * This one will *NOT* use accurate qgroup reserved space API, just for case
  3907. * which we can't sleep and is sure it won't affect qgroup reserved space.
  3908. * Like clear_bit_hook().
  3909. */
  3910. void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
  3911. u64 len)
  3912. {
  3913. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3914. struct btrfs_space_info *data_sinfo;
  3915. /* Make sure the range is aligned to sectorsize */
  3916. len = round_up(start + len, fs_info->sectorsize) -
  3917. round_down(start, fs_info->sectorsize);
  3918. start = round_down(start, fs_info->sectorsize);
  3919. data_sinfo = fs_info->data_sinfo;
  3920. spin_lock(&data_sinfo->lock);
  3921. if (WARN_ON(data_sinfo->bytes_may_use < len))
  3922. data_sinfo->bytes_may_use = 0;
  3923. else
  3924. data_sinfo->bytes_may_use -= len;
  3925. trace_btrfs_space_reservation(fs_info, "space_info",
  3926. data_sinfo->flags, len, 0);
  3927. spin_unlock(&data_sinfo->lock);
  3928. }
  3929. /*
  3930. * Called if we need to clear a data reservation for this inode
  3931. * Normally in a error case.
  3932. *
  3933. * This one will handle the per-inode data rsv map for accurate reserved
  3934. * space framework.
  3935. */
  3936. void btrfs_free_reserved_data_space(struct inode *inode,
  3937. struct extent_changeset *reserved, u64 start, u64 len)
  3938. {
  3939. struct btrfs_root *root = BTRFS_I(inode)->root;
  3940. /* Make sure the range is aligned to sectorsize */
  3941. len = round_up(start + len, root->fs_info->sectorsize) -
  3942. round_down(start, root->fs_info->sectorsize);
  3943. start = round_down(start, root->fs_info->sectorsize);
  3944. btrfs_free_reserved_data_space_noquota(inode, start, len);
  3945. btrfs_qgroup_free_data(inode, reserved, start, len);
  3946. }
  3947. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3948. {
  3949. struct list_head *head = &info->space_info;
  3950. struct btrfs_space_info *found;
  3951. rcu_read_lock();
  3952. list_for_each_entry_rcu(found, head, list) {
  3953. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3954. found->force_alloc = CHUNK_ALLOC_FORCE;
  3955. }
  3956. rcu_read_unlock();
  3957. }
  3958. static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
  3959. {
  3960. return (global->size << 1);
  3961. }
  3962. static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
  3963. struct btrfs_space_info *sinfo, int force)
  3964. {
  3965. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  3966. u64 bytes_used = btrfs_space_info_used(sinfo, false);
  3967. u64 thresh;
  3968. if (force == CHUNK_ALLOC_FORCE)
  3969. return 1;
  3970. /*
  3971. * We need to take into account the global rsv because for all intents
  3972. * and purposes it's used space. Don't worry about locking the
  3973. * global_rsv, it doesn't change except when the transaction commits.
  3974. */
  3975. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3976. bytes_used += calc_global_rsv_need_space(global_rsv);
  3977. /*
  3978. * in limited mode, we want to have some free space up to
  3979. * about 1% of the FS size.
  3980. */
  3981. if (force == CHUNK_ALLOC_LIMITED) {
  3982. thresh = btrfs_super_total_bytes(fs_info->super_copy);
  3983. thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
  3984. if (sinfo->total_bytes - bytes_used < thresh)
  3985. return 1;
  3986. }
  3987. if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
  3988. return 0;
  3989. return 1;
  3990. }
  3991. static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
  3992. {
  3993. u64 num_dev;
  3994. if (type & (BTRFS_BLOCK_GROUP_RAID10 |
  3995. BTRFS_BLOCK_GROUP_RAID0 |
  3996. BTRFS_BLOCK_GROUP_RAID5 |
  3997. BTRFS_BLOCK_GROUP_RAID6))
  3998. num_dev = fs_info->fs_devices->rw_devices;
  3999. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  4000. num_dev = 2;
  4001. else
  4002. num_dev = 1; /* DUP or single */
  4003. return num_dev;
  4004. }
  4005. /*
  4006. * If @is_allocation is true, reserve space in the system space info necessary
  4007. * for allocating a chunk, otherwise if it's false, reserve space necessary for
  4008. * removing a chunk.
  4009. */
  4010. void check_system_chunk(struct btrfs_trans_handle *trans,
  4011. struct btrfs_fs_info *fs_info, u64 type)
  4012. {
  4013. struct btrfs_space_info *info;
  4014. u64 left;
  4015. u64 thresh;
  4016. int ret = 0;
  4017. u64 num_devs;
  4018. /*
  4019. * Needed because we can end up allocating a system chunk and for an
  4020. * atomic and race free space reservation in the chunk block reserve.
  4021. */
  4022. lockdep_assert_held(&fs_info->chunk_mutex);
  4023. info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  4024. spin_lock(&info->lock);
  4025. left = info->total_bytes - btrfs_space_info_used(info, true);
  4026. spin_unlock(&info->lock);
  4027. num_devs = get_profile_num_devs(fs_info, type);
  4028. /* num_devs device items to update and 1 chunk item to add or remove */
  4029. thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
  4030. btrfs_calc_trans_metadata_size(fs_info, 1);
  4031. if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
  4032. btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
  4033. left, thresh, type);
  4034. dump_space_info(fs_info, info, 0, 0);
  4035. }
  4036. if (left < thresh) {
  4037. u64 flags = btrfs_system_alloc_profile(fs_info);
  4038. /*
  4039. * Ignore failure to create system chunk. We might end up not
  4040. * needing it, as we might not need to COW all nodes/leafs from
  4041. * the paths we visit in the chunk tree (they were already COWed
  4042. * or created in the current transaction for example).
  4043. */
  4044. ret = btrfs_alloc_chunk(trans, fs_info, flags);
  4045. }
  4046. if (!ret) {
  4047. ret = btrfs_block_rsv_add(fs_info->chunk_root,
  4048. &fs_info->chunk_block_rsv,
  4049. thresh, BTRFS_RESERVE_NO_FLUSH);
  4050. if (!ret)
  4051. trans->chunk_bytes_reserved += thresh;
  4052. }
  4053. }
  4054. /*
  4055. * If force is CHUNK_ALLOC_FORCE:
  4056. * - return 1 if it successfully allocates a chunk,
  4057. * - return errors including -ENOSPC otherwise.
  4058. * If force is NOT CHUNK_ALLOC_FORCE:
  4059. * - return 0 if it doesn't need to allocate a new chunk,
  4060. * - return 1 if it successfully allocates a chunk,
  4061. * - return errors including -ENOSPC otherwise.
  4062. */
  4063. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  4064. struct btrfs_fs_info *fs_info, u64 flags, int force)
  4065. {
  4066. struct btrfs_space_info *space_info;
  4067. int wait_for_alloc = 0;
  4068. int ret = 0;
  4069. /* Don't re-enter if we're already allocating a chunk */
  4070. if (trans->allocating_chunk)
  4071. return -ENOSPC;
  4072. space_info = __find_space_info(fs_info, flags);
  4073. ASSERT(space_info);
  4074. again:
  4075. spin_lock(&space_info->lock);
  4076. if (force < space_info->force_alloc)
  4077. force = space_info->force_alloc;
  4078. if (space_info->full) {
  4079. if (should_alloc_chunk(fs_info, space_info, force))
  4080. ret = -ENOSPC;
  4081. else
  4082. ret = 0;
  4083. spin_unlock(&space_info->lock);
  4084. return ret;
  4085. }
  4086. if (!should_alloc_chunk(fs_info, space_info, force)) {
  4087. spin_unlock(&space_info->lock);
  4088. return 0;
  4089. } else if (space_info->chunk_alloc) {
  4090. wait_for_alloc = 1;
  4091. } else {
  4092. space_info->chunk_alloc = 1;
  4093. }
  4094. spin_unlock(&space_info->lock);
  4095. mutex_lock(&fs_info->chunk_mutex);
  4096. /*
  4097. * The chunk_mutex is held throughout the entirety of a chunk
  4098. * allocation, so once we've acquired the chunk_mutex we know that the
  4099. * other guy is done and we need to recheck and see if we should
  4100. * allocate.
  4101. */
  4102. if (wait_for_alloc) {
  4103. mutex_unlock(&fs_info->chunk_mutex);
  4104. wait_for_alloc = 0;
  4105. cond_resched();
  4106. goto again;
  4107. }
  4108. trans->allocating_chunk = true;
  4109. /*
  4110. * If we have mixed data/metadata chunks we want to make sure we keep
  4111. * allocating mixed chunks instead of individual chunks.
  4112. */
  4113. if (btrfs_mixed_space_info(space_info))
  4114. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  4115. /*
  4116. * if we're doing a data chunk, go ahead and make sure that
  4117. * we keep a reasonable number of metadata chunks allocated in the
  4118. * FS as well.
  4119. */
  4120. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  4121. fs_info->data_chunk_allocations++;
  4122. if (!(fs_info->data_chunk_allocations %
  4123. fs_info->metadata_ratio))
  4124. force_metadata_allocation(fs_info);
  4125. }
  4126. /*
  4127. * Check if we have enough space in SYSTEM chunk because we may need
  4128. * to update devices.
  4129. */
  4130. check_system_chunk(trans, fs_info, flags);
  4131. ret = btrfs_alloc_chunk(trans, fs_info, flags);
  4132. trans->allocating_chunk = false;
  4133. spin_lock(&space_info->lock);
  4134. if (ret < 0) {
  4135. if (ret == -ENOSPC)
  4136. space_info->full = 1;
  4137. else
  4138. goto out;
  4139. } else {
  4140. ret = 1;
  4141. }
  4142. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  4143. out:
  4144. space_info->chunk_alloc = 0;
  4145. spin_unlock(&space_info->lock);
  4146. mutex_unlock(&fs_info->chunk_mutex);
  4147. /*
  4148. * When we allocate a new chunk we reserve space in the chunk block
  4149. * reserve to make sure we can COW nodes/leafs in the chunk tree or
  4150. * add new nodes/leafs to it if we end up needing to do it when
  4151. * inserting the chunk item and updating device items as part of the
  4152. * second phase of chunk allocation, performed by
  4153. * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
  4154. * large number of new block groups to create in our transaction
  4155. * handle's new_bgs list to avoid exhausting the chunk block reserve
  4156. * in extreme cases - like having a single transaction create many new
  4157. * block groups when starting to write out the free space caches of all
  4158. * the block groups that were made dirty during the lifetime of the
  4159. * transaction.
  4160. */
  4161. if (trans->can_flush_pending_bgs &&
  4162. trans->chunk_bytes_reserved >= (u64)SZ_2M) {
  4163. btrfs_create_pending_block_groups(trans);
  4164. btrfs_trans_release_chunk_metadata(trans);
  4165. }
  4166. return ret;
  4167. }
  4168. static int can_overcommit(struct btrfs_fs_info *fs_info,
  4169. struct btrfs_space_info *space_info, u64 bytes,
  4170. enum btrfs_reserve_flush_enum flush,
  4171. bool system_chunk)
  4172. {
  4173. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4174. u64 profile;
  4175. u64 space_size;
  4176. u64 avail;
  4177. u64 used;
  4178. /* Don't overcommit when in mixed mode. */
  4179. if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
  4180. return 0;
  4181. if (system_chunk)
  4182. profile = btrfs_system_alloc_profile(fs_info);
  4183. else
  4184. profile = btrfs_metadata_alloc_profile(fs_info);
  4185. used = btrfs_space_info_used(space_info, false);
  4186. /*
  4187. * We only want to allow over committing if we have lots of actual space
  4188. * free, but if we don't have enough space to handle the global reserve
  4189. * space then we could end up having a real enospc problem when trying
  4190. * to allocate a chunk or some other such important allocation.
  4191. */
  4192. spin_lock(&global_rsv->lock);
  4193. space_size = calc_global_rsv_need_space(global_rsv);
  4194. spin_unlock(&global_rsv->lock);
  4195. if (used + space_size >= space_info->total_bytes)
  4196. return 0;
  4197. used += space_info->bytes_may_use;
  4198. avail = atomic64_read(&fs_info->free_chunk_space);
  4199. /*
  4200. * If we have dup, raid1 or raid10 then only half of the free
  4201. * space is actually useable. For raid56, the space info used
  4202. * doesn't include the parity drive, so we don't have to
  4203. * change the math
  4204. */
  4205. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  4206. BTRFS_BLOCK_GROUP_RAID1 |
  4207. BTRFS_BLOCK_GROUP_RAID10))
  4208. avail >>= 1;
  4209. /*
  4210. * If we aren't flushing all things, let us overcommit up to
  4211. * 1/2th of the space. If we can flush, don't let us overcommit
  4212. * too much, let it overcommit up to 1/8 of the space.
  4213. */
  4214. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  4215. avail >>= 3;
  4216. else
  4217. avail >>= 1;
  4218. if (used + bytes < space_info->total_bytes + avail)
  4219. return 1;
  4220. return 0;
  4221. }
  4222. static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
  4223. unsigned long nr_pages, int nr_items)
  4224. {
  4225. struct super_block *sb = fs_info->sb;
  4226. if (down_read_trylock(&sb->s_umount)) {
  4227. writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
  4228. up_read(&sb->s_umount);
  4229. } else {
  4230. /*
  4231. * We needn't worry the filesystem going from r/w to r/o though
  4232. * we don't acquire ->s_umount mutex, because the filesystem
  4233. * should guarantee the delalloc inodes list be empty after
  4234. * the filesystem is readonly(all dirty pages are written to
  4235. * the disk).
  4236. */
  4237. btrfs_start_delalloc_roots(fs_info, nr_items);
  4238. if (!current->journal_info)
  4239. btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
  4240. }
  4241. }
  4242. static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
  4243. u64 to_reclaim)
  4244. {
  4245. u64 bytes;
  4246. u64 nr;
  4247. bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
  4248. nr = div64_u64(to_reclaim, bytes);
  4249. if (!nr)
  4250. nr = 1;
  4251. return nr;
  4252. }
  4253. #define EXTENT_SIZE_PER_ITEM SZ_256K
  4254. /*
  4255. * shrink metadata reservation for delalloc
  4256. */
  4257. static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim,
  4258. u64 orig, bool wait_ordered)
  4259. {
  4260. struct btrfs_space_info *space_info;
  4261. struct btrfs_trans_handle *trans;
  4262. u64 delalloc_bytes;
  4263. u64 max_reclaim;
  4264. u64 items;
  4265. long time_left;
  4266. unsigned long nr_pages;
  4267. int loops;
  4268. /* Calc the number of the pages we need flush for space reservation */
  4269. items = calc_reclaim_items_nr(fs_info, to_reclaim);
  4270. to_reclaim = items * EXTENT_SIZE_PER_ITEM;
  4271. trans = (struct btrfs_trans_handle *)current->journal_info;
  4272. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4273. delalloc_bytes = percpu_counter_sum_positive(
  4274. &fs_info->delalloc_bytes);
  4275. if (delalloc_bytes == 0) {
  4276. if (trans)
  4277. return;
  4278. if (wait_ordered)
  4279. btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
  4280. return;
  4281. }
  4282. loops = 0;
  4283. while (delalloc_bytes && loops < 3) {
  4284. max_reclaim = min(delalloc_bytes, to_reclaim);
  4285. nr_pages = max_reclaim >> PAGE_SHIFT;
  4286. btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
  4287. /*
  4288. * We need to wait for the async pages to actually start before
  4289. * we do anything.
  4290. */
  4291. max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
  4292. if (!max_reclaim)
  4293. goto skip_async;
  4294. if (max_reclaim <= nr_pages)
  4295. max_reclaim = 0;
  4296. else
  4297. max_reclaim -= nr_pages;
  4298. wait_event(fs_info->async_submit_wait,
  4299. atomic_read(&fs_info->async_delalloc_pages) <=
  4300. (int)max_reclaim);
  4301. skip_async:
  4302. spin_lock(&space_info->lock);
  4303. if (list_empty(&space_info->tickets) &&
  4304. list_empty(&space_info->priority_tickets)) {
  4305. spin_unlock(&space_info->lock);
  4306. break;
  4307. }
  4308. spin_unlock(&space_info->lock);
  4309. loops++;
  4310. if (wait_ordered && !trans) {
  4311. btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
  4312. } else {
  4313. time_left = schedule_timeout_killable(1);
  4314. if (time_left)
  4315. break;
  4316. }
  4317. delalloc_bytes = percpu_counter_sum_positive(
  4318. &fs_info->delalloc_bytes);
  4319. }
  4320. }
  4321. struct reserve_ticket {
  4322. u64 bytes;
  4323. int error;
  4324. struct list_head list;
  4325. wait_queue_head_t wait;
  4326. };
  4327. /**
  4328. * maybe_commit_transaction - possibly commit the transaction if its ok to
  4329. * @root - the root we're allocating for
  4330. * @bytes - the number of bytes we want to reserve
  4331. * @force - force the commit
  4332. *
  4333. * This will check to make sure that committing the transaction will actually
  4334. * get us somewhere and then commit the transaction if it does. Otherwise it
  4335. * will return -ENOSPC.
  4336. */
  4337. static int may_commit_transaction(struct btrfs_fs_info *fs_info,
  4338. struct btrfs_space_info *space_info)
  4339. {
  4340. struct reserve_ticket *ticket = NULL;
  4341. struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
  4342. struct btrfs_trans_handle *trans;
  4343. u64 bytes;
  4344. trans = (struct btrfs_trans_handle *)current->journal_info;
  4345. if (trans)
  4346. return -EAGAIN;
  4347. spin_lock(&space_info->lock);
  4348. if (!list_empty(&space_info->priority_tickets))
  4349. ticket = list_first_entry(&space_info->priority_tickets,
  4350. struct reserve_ticket, list);
  4351. else if (!list_empty(&space_info->tickets))
  4352. ticket = list_first_entry(&space_info->tickets,
  4353. struct reserve_ticket, list);
  4354. bytes = (ticket) ? ticket->bytes : 0;
  4355. spin_unlock(&space_info->lock);
  4356. if (!bytes)
  4357. return 0;
  4358. /* See if there is enough pinned space to make this reservation */
  4359. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  4360. bytes) >= 0)
  4361. goto commit;
  4362. /*
  4363. * See if there is some space in the delayed insertion reservation for
  4364. * this reservation.
  4365. */
  4366. if (space_info != delayed_rsv->space_info)
  4367. return -ENOSPC;
  4368. spin_lock(&delayed_rsv->lock);
  4369. if (delayed_rsv->size > bytes)
  4370. bytes = 0;
  4371. else
  4372. bytes -= delayed_rsv->size;
  4373. spin_unlock(&delayed_rsv->lock);
  4374. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  4375. bytes) < 0) {
  4376. return -ENOSPC;
  4377. }
  4378. commit:
  4379. trans = btrfs_join_transaction(fs_info->extent_root);
  4380. if (IS_ERR(trans))
  4381. return -ENOSPC;
  4382. return btrfs_commit_transaction(trans);
  4383. }
  4384. /*
  4385. * Try to flush some data based on policy set by @state. This is only advisory
  4386. * and may fail for various reasons. The caller is supposed to examine the
  4387. * state of @space_info to detect the outcome.
  4388. */
  4389. static void flush_space(struct btrfs_fs_info *fs_info,
  4390. struct btrfs_space_info *space_info, u64 num_bytes,
  4391. int state)
  4392. {
  4393. struct btrfs_root *root = fs_info->extent_root;
  4394. struct btrfs_trans_handle *trans;
  4395. int nr;
  4396. int ret = 0;
  4397. switch (state) {
  4398. case FLUSH_DELAYED_ITEMS_NR:
  4399. case FLUSH_DELAYED_ITEMS:
  4400. if (state == FLUSH_DELAYED_ITEMS_NR)
  4401. nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
  4402. else
  4403. nr = -1;
  4404. trans = btrfs_join_transaction(root);
  4405. if (IS_ERR(trans)) {
  4406. ret = PTR_ERR(trans);
  4407. break;
  4408. }
  4409. ret = btrfs_run_delayed_items_nr(trans, nr);
  4410. btrfs_end_transaction(trans);
  4411. break;
  4412. case FLUSH_DELALLOC:
  4413. case FLUSH_DELALLOC_WAIT:
  4414. shrink_delalloc(fs_info, num_bytes * 2, num_bytes,
  4415. state == FLUSH_DELALLOC_WAIT);
  4416. break;
  4417. case ALLOC_CHUNK:
  4418. trans = btrfs_join_transaction(root);
  4419. if (IS_ERR(trans)) {
  4420. ret = PTR_ERR(trans);
  4421. break;
  4422. }
  4423. ret = do_chunk_alloc(trans, fs_info,
  4424. btrfs_metadata_alloc_profile(fs_info),
  4425. CHUNK_ALLOC_NO_FORCE);
  4426. btrfs_end_transaction(trans);
  4427. if (ret > 0 || ret == -ENOSPC)
  4428. ret = 0;
  4429. break;
  4430. case COMMIT_TRANS:
  4431. ret = may_commit_transaction(fs_info, space_info);
  4432. break;
  4433. default:
  4434. ret = -ENOSPC;
  4435. break;
  4436. }
  4437. trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
  4438. ret);
  4439. return;
  4440. }
  4441. static inline u64
  4442. btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
  4443. struct btrfs_space_info *space_info,
  4444. bool system_chunk)
  4445. {
  4446. struct reserve_ticket *ticket;
  4447. u64 used;
  4448. u64 expected;
  4449. u64 to_reclaim = 0;
  4450. list_for_each_entry(ticket, &space_info->tickets, list)
  4451. to_reclaim += ticket->bytes;
  4452. list_for_each_entry(ticket, &space_info->priority_tickets, list)
  4453. to_reclaim += ticket->bytes;
  4454. if (to_reclaim)
  4455. return to_reclaim;
  4456. to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
  4457. if (can_overcommit(fs_info, space_info, to_reclaim,
  4458. BTRFS_RESERVE_FLUSH_ALL, system_chunk))
  4459. return 0;
  4460. used = btrfs_space_info_used(space_info, true);
  4461. if (can_overcommit(fs_info, space_info, SZ_1M,
  4462. BTRFS_RESERVE_FLUSH_ALL, system_chunk))
  4463. expected = div_factor_fine(space_info->total_bytes, 95);
  4464. else
  4465. expected = div_factor_fine(space_info->total_bytes, 90);
  4466. if (used > expected)
  4467. to_reclaim = used - expected;
  4468. else
  4469. to_reclaim = 0;
  4470. to_reclaim = min(to_reclaim, space_info->bytes_may_use +
  4471. space_info->bytes_reserved);
  4472. return to_reclaim;
  4473. }
  4474. static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info,
  4475. struct btrfs_space_info *space_info,
  4476. u64 used, bool system_chunk)
  4477. {
  4478. u64 thresh = div_factor_fine(space_info->total_bytes, 98);
  4479. /* If we're just plain full then async reclaim just slows us down. */
  4480. if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
  4481. return 0;
  4482. if (!btrfs_calc_reclaim_metadata_size(fs_info, space_info,
  4483. system_chunk))
  4484. return 0;
  4485. return (used >= thresh && !btrfs_fs_closing(fs_info) &&
  4486. !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
  4487. }
  4488. static void wake_all_tickets(struct list_head *head)
  4489. {
  4490. struct reserve_ticket *ticket;
  4491. while (!list_empty(head)) {
  4492. ticket = list_first_entry(head, struct reserve_ticket, list);
  4493. list_del_init(&ticket->list);
  4494. ticket->error = -ENOSPC;
  4495. wake_up(&ticket->wait);
  4496. }
  4497. }
  4498. /*
  4499. * This is for normal flushers, we can wait all goddamned day if we want to. We
  4500. * will loop and continuously try to flush as long as we are making progress.
  4501. * We count progress as clearing off tickets each time we have to loop.
  4502. */
  4503. static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
  4504. {
  4505. struct btrfs_fs_info *fs_info;
  4506. struct btrfs_space_info *space_info;
  4507. u64 to_reclaim;
  4508. int flush_state;
  4509. int commit_cycles = 0;
  4510. u64 last_tickets_id;
  4511. fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
  4512. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4513. spin_lock(&space_info->lock);
  4514. to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
  4515. false);
  4516. if (!to_reclaim) {
  4517. space_info->flush = 0;
  4518. spin_unlock(&space_info->lock);
  4519. return;
  4520. }
  4521. last_tickets_id = space_info->tickets_id;
  4522. spin_unlock(&space_info->lock);
  4523. flush_state = FLUSH_DELAYED_ITEMS_NR;
  4524. do {
  4525. flush_space(fs_info, space_info, to_reclaim, flush_state);
  4526. spin_lock(&space_info->lock);
  4527. if (list_empty(&space_info->tickets)) {
  4528. space_info->flush = 0;
  4529. spin_unlock(&space_info->lock);
  4530. return;
  4531. }
  4532. to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
  4533. space_info,
  4534. false);
  4535. if (last_tickets_id == space_info->tickets_id) {
  4536. flush_state++;
  4537. } else {
  4538. last_tickets_id = space_info->tickets_id;
  4539. flush_state = FLUSH_DELAYED_ITEMS_NR;
  4540. if (commit_cycles)
  4541. commit_cycles--;
  4542. }
  4543. if (flush_state > COMMIT_TRANS) {
  4544. commit_cycles++;
  4545. if (commit_cycles > 2) {
  4546. wake_all_tickets(&space_info->tickets);
  4547. space_info->flush = 0;
  4548. } else {
  4549. flush_state = FLUSH_DELAYED_ITEMS_NR;
  4550. }
  4551. }
  4552. spin_unlock(&space_info->lock);
  4553. } while (flush_state <= COMMIT_TRANS);
  4554. }
  4555. void btrfs_init_async_reclaim_work(struct work_struct *work)
  4556. {
  4557. INIT_WORK(work, btrfs_async_reclaim_metadata_space);
  4558. }
  4559. static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
  4560. struct btrfs_space_info *space_info,
  4561. struct reserve_ticket *ticket)
  4562. {
  4563. u64 to_reclaim;
  4564. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  4565. spin_lock(&space_info->lock);
  4566. to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
  4567. false);
  4568. if (!to_reclaim) {
  4569. spin_unlock(&space_info->lock);
  4570. return;
  4571. }
  4572. spin_unlock(&space_info->lock);
  4573. do {
  4574. flush_space(fs_info, space_info, to_reclaim, flush_state);
  4575. flush_state++;
  4576. spin_lock(&space_info->lock);
  4577. if (ticket->bytes == 0) {
  4578. spin_unlock(&space_info->lock);
  4579. return;
  4580. }
  4581. spin_unlock(&space_info->lock);
  4582. /*
  4583. * Priority flushers can't wait on delalloc without
  4584. * deadlocking.
  4585. */
  4586. if (flush_state == FLUSH_DELALLOC ||
  4587. flush_state == FLUSH_DELALLOC_WAIT)
  4588. flush_state = ALLOC_CHUNK;
  4589. } while (flush_state < COMMIT_TRANS);
  4590. }
  4591. static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
  4592. struct btrfs_space_info *space_info,
  4593. struct reserve_ticket *ticket, u64 orig_bytes)
  4594. {
  4595. DEFINE_WAIT(wait);
  4596. int ret = 0;
  4597. spin_lock(&space_info->lock);
  4598. while (ticket->bytes > 0 && ticket->error == 0) {
  4599. ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
  4600. if (ret) {
  4601. ret = -EINTR;
  4602. break;
  4603. }
  4604. spin_unlock(&space_info->lock);
  4605. schedule();
  4606. finish_wait(&ticket->wait, &wait);
  4607. spin_lock(&space_info->lock);
  4608. }
  4609. if (!ret)
  4610. ret = ticket->error;
  4611. if (!list_empty(&ticket->list))
  4612. list_del_init(&ticket->list);
  4613. if (ticket->bytes && ticket->bytes < orig_bytes) {
  4614. u64 num_bytes = orig_bytes - ticket->bytes;
  4615. space_info->bytes_may_use -= num_bytes;
  4616. trace_btrfs_space_reservation(fs_info, "space_info",
  4617. space_info->flags, num_bytes, 0);
  4618. }
  4619. spin_unlock(&space_info->lock);
  4620. return ret;
  4621. }
  4622. /**
  4623. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  4624. * @root - the root we're allocating for
  4625. * @space_info - the space info we want to allocate from
  4626. * @orig_bytes - the number of bytes we want
  4627. * @flush - whether or not we can flush to make our reservation
  4628. *
  4629. * This will reserve orig_bytes number of bytes from the space info associated
  4630. * with the block_rsv. If there is not enough space it will make an attempt to
  4631. * flush out space to make room. It will do this by flushing delalloc if
  4632. * possible or committing the transaction. If flush is 0 then no attempts to
  4633. * regain reservations will be made and this will fail if there is not enough
  4634. * space already.
  4635. */
  4636. static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
  4637. struct btrfs_space_info *space_info,
  4638. u64 orig_bytes,
  4639. enum btrfs_reserve_flush_enum flush,
  4640. bool system_chunk)
  4641. {
  4642. struct reserve_ticket ticket;
  4643. u64 used;
  4644. int ret = 0;
  4645. ASSERT(orig_bytes);
  4646. ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
  4647. spin_lock(&space_info->lock);
  4648. ret = -ENOSPC;
  4649. used = btrfs_space_info_used(space_info, true);
  4650. /*
  4651. * If we have enough space then hooray, make our reservation and carry
  4652. * on. If not see if we can overcommit, and if we can, hooray carry on.
  4653. * If not things get more complicated.
  4654. */
  4655. if (used + orig_bytes <= space_info->total_bytes) {
  4656. space_info->bytes_may_use += orig_bytes;
  4657. trace_btrfs_space_reservation(fs_info, "space_info",
  4658. space_info->flags, orig_bytes, 1);
  4659. ret = 0;
  4660. } else if (can_overcommit(fs_info, space_info, orig_bytes, flush,
  4661. system_chunk)) {
  4662. space_info->bytes_may_use += orig_bytes;
  4663. trace_btrfs_space_reservation(fs_info, "space_info",
  4664. space_info->flags, orig_bytes, 1);
  4665. ret = 0;
  4666. }
  4667. /*
  4668. * If we couldn't make a reservation then setup our reservation ticket
  4669. * and kick the async worker if it's not already running.
  4670. *
  4671. * If we are a priority flusher then we just need to add our ticket to
  4672. * the list and we will do our own flushing further down.
  4673. */
  4674. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  4675. ticket.bytes = orig_bytes;
  4676. ticket.error = 0;
  4677. init_waitqueue_head(&ticket.wait);
  4678. if (flush == BTRFS_RESERVE_FLUSH_ALL) {
  4679. list_add_tail(&ticket.list, &space_info->tickets);
  4680. if (!space_info->flush) {
  4681. space_info->flush = 1;
  4682. trace_btrfs_trigger_flush(fs_info,
  4683. space_info->flags,
  4684. orig_bytes, flush,
  4685. "enospc");
  4686. queue_work(system_unbound_wq,
  4687. &fs_info->async_reclaim_work);
  4688. }
  4689. } else {
  4690. list_add_tail(&ticket.list,
  4691. &space_info->priority_tickets);
  4692. }
  4693. } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
  4694. used += orig_bytes;
  4695. /*
  4696. * We will do the space reservation dance during log replay,
  4697. * which means we won't have fs_info->fs_root set, so don't do
  4698. * the async reclaim as we will panic.
  4699. */
  4700. if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
  4701. need_do_async_reclaim(fs_info, space_info,
  4702. used, system_chunk) &&
  4703. !work_busy(&fs_info->async_reclaim_work)) {
  4704. trace_btrfs_trigger_flush(fs_info, space_info->flags,
  4705. orig_bytes, flush, "preempt");
  4706. queue_work(system_unbound_wq,
  4707. &fs_info->async_reclaim_work);
  4708. }
  4709. }
  4710. spin_unlock(&space_info->lock);
  4711. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  4712. return ret;
  4713. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  4714. return wait_reserve_ticket(fs_info, space_info, &ticket,
  4715. orig_bytes);
  4716. ret = 0;
  4717. priority_reclaim_metadata_space(fs_info, space_info, &ticket);
  4718. spin_lock(&space_info->lock);
  4719. if (ticket.bytes) {
  4720. if (ticket.bytes < orig_bytes) {
  4721. u64 num_bytes = orig_bytes - ticket.bytes;
  4722. space_info->bytes_may_use -= num_bytes;
  4723. trace_btrfs_space_reservation(fs_info, "space_info",
  4724. space_info->flags,
  4725. num_bytes, 0);
  4726. }
  4727. list_del_init(&ticket.list);
  4728. ret = -ENOSPC;
  4729. }
  4730. spin_unlock(&space_info->lock);
  4731. ASSERT(list_empty(&ticket.list));
  4732. return ret;
  4733. }
  4734. /**
  4735. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  4736. * @root - the root we're allocating for
  4737. * @block_rsv - the block_rsv we're allocating for
  4738. * @orig_bytes - the number of bytes we want
  4739. * @flush - whether or not we can flush to make our reservation
  4740. *
  4741. * This will reserve orgi_bytes number of bytes from the space info associated
  4742. * with the block_rsv. If there is not enough space it will make an attempt to
  4743. * flush out space to make room. It will do this by flushing delalloc if
  4744. * possible or committing the transaction. If flush is 0 then no attempts to
  4745. * regain reservations will be made and this will fail if there is not enough
  4746. * space already.
  4747. */
  4748. static int reserve_metadata_bytes(struct btrfs_root *root,
  4749. struct btrfs_block_rsv *block_rsv,
  4750. u64 orig_bytes,
  4751. enum btrfs_reserve_flush_enum flush)
  4752. {
  4753. struct btrfs_fs_info *fs_info = root->fs_info;
  4754. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4755. int ret;
  4756. bool system_chunk = (root == fs_info->chunk_root);
  4757. ret = __reserve_metadata_bytes(fs_info, block_rsv->space_info,
  4758. orig_bytes, flush, system_chunk);
  4759. if (ret == -ENOSPC &&
  4760. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  4761. if (block_rsv != global_rsv &&
  4762. !block_rsv_use_bytes(global_rsv, orig_bytes))
  4763. ret = 0;
  4764. }
  4765. if (ret == -ENOSPC) {
  4766. trace_btrfs_space_reservation(fs_info, "space_info:enospc",
  4767. block_rsv->space_info->flags,
  4768. orig_bytes, 1);
  4769. if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
  4770. dump_space_info(fs_info, block_rsv->space_info,
  4771. orig_bytes, 0);
  4772. }
  4773. return ret;
  4774. }
  4775. static struct btrfs_block_rsv *get_block_rsv(
  4776. const struct btrfs_trans_handle *trans,
  4777. const struct btrfs_root *root)
  4778. {
  4779. struct btrfs_fs_info *fs_info = root->fs_info;
  4780. struct btrfs_block_rsv *block_rsv = NULL;
  4781. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  4782. (root == fs_info->csum_root && trans->adding_csums) ||
  4783. (root == fs_info->uuid_root))
  4784. block_rsv = trans->block_rsv;
  4785. if (!block_rsv)
  4786. block_rsv = root->block_rsv;
  4787. if (!block_rsv)
  4788. block_rsv = &fs_info->empty_block_rsv;
  4789. return block_rsv;
  4790. }
  4791. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  4792. u64 num_bytes)
  4793. {
  4794. int ret = -ENOSPC;
  4795. spin_lock(&block_rsv->lock);
  4796. if (block_rsv->reserved >= num_bytes) {
  4797. block_rsv->reserved -= num_bytes;
  4798. if (block_rsv->reserved < block_rsv->size)
  4799. block_rsv->full = 0;
  4800. ret = 0;
  4801. }
  4802. spin_unlock(&block_rsv->lock);
  4803. return ret;
  4804. }
  4805. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  4806. u64 num_bytes, int update_size)
  4807. {
  4808. spin_lock(&block_rsv->lock);
  4809. block_rsv->reserved += num_bytes;
  4810. if (update_size)
  4811. block_rsv->size += num_bytes;
  4812. else if (block_rsv->reserved >= block_rsv->size)
  4813. block_rsv->full = 1;
  4814. spin_unlock(&block_rsv->lock);
  4815. }
  4816. int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
  4817. struct btrfs_block_rsv *dest, u64 num_bytes,
  4818. int min_factor)
  4819. {
  4820. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4821. u64 min_bytes;
  4822. if (global_rsv->space_info != dest->space_info)
  4823. return -ENOSPC;
  4824. spin_lock(&global_rsv->lock);
  4825. min_bytes = div_factor(global_rsv->size, min_factor);
  4826. if (global_rsv->reserved < min_bytes + num_bytes) {
  4827. spin_unlock(&global_rsv->lock);
  4828. return -ENOSPC;
  4829. }
  4830. global_rsv->reserved -= num_bytes;
  4831. if (global_rsv->reserved < global_rsv->size)
  4832. global_rsv->full = 0;
  4833. spin_unlock(&global_rsv->lock);
  4834. block_rsv_add_bytes(dest, num_bytes, 1);
  4835. return 0;
  4836. }
  4837. /*
  4838. * This is for space we already have accounted in space_info->bytes_may_use, so
  4839. * basically when we're returning space from block_rsv's.
  4840. */
  4841. static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
  4842. struct btrfs_space_info *space_info,
  4843. u64 num_bytes)
  4844. {
  4845. struct reserve_ticket *ticket;
  4846. struct list_head *head;
  4847. u64 used;
  4848. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
  4849. bool check_overcommit = false;
  4850. spin_lock(&space_info->lock);
  4851. head = &space_info->priority_tickets;
  4852. /*
  4853. * If we are over our limit then we need to check and see if we can
  4854. * overcommit, and if we can't then we just need to free up our space
  4855. * and not satisfy any requests.
  4856. */
  4857. used = btrfs_space_info_used(space_info, true);
  4858. if (used - num_bytes >= space_info->total_bytes)
  4859. check_overcommit = true;
  4860. again:
  4861. while (!list_empty(head) && num_bytes) {
  4862. ticket = list_first_entry(head, struct reserve_ticket,
  4863. list);
  4864. /*
  4865. * We use 0 bytes because this space is already reserved, so
  4866. * adding the ticket space would be a double count.
  4867. */
  4868. if (check_overcommit &&
  4869. !can_overcommit(fs_info, space_info, 0, flush, false))
  4870. break;
  4871. if (num_bytes >= ticket->bytes) {
  4872. list_del_init(&ticket->list);
  4873. num_bytes -= ticket->bytes;
  4874. ticket->bytes = 0;
  4875. space_info->tickets_id++;
  4876. wake_up(&ticket->wait);
  4877. } else {
  4878. ticket->bytes -= num_bytes;
  4879. num_bytes = 0;
  4880. }
  4881. }
  4882. if (num_bytes && head == &space_info->priority_tickets) {
  4883. head = &space_info->tickets;
  4884. flush = BTRFS_RESERVE_FLUSH_ALL;
  4885. goto again;
  4886. }
  4887. space_info->bytes_may_use -= num_bytes;
  4888. trace_btrfs_space_reservation(fs_info, "space_info",
  4889. space_info->flags, num_bytes, 0);
  4890. spin_unlock(&space_info->lock);
  4891. }
  4892. /*
  4893. * This is for newly allocated space that isn't accounted in
  4894. * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent
  4895. * we use this helper.
  4896. */
  4897. static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
  4898. struct btrfs_space_info *space_info,
  4899. u64 num_bytes)
  4900. {
  4901. struct reserve_ticket *ticket;
  4902. struct list_head *head = &space_info->priority_tickets;
  4903. again:
  4904. while (!list_empty(head) && num_bytes) {
  4905. ticket = list_first_entry(head, struct reserve_ticket,
  4906. list);
  4907. if (num_bytes >= ticket->bytes) {
  4908. trace_btrfs_space_reservation(fs_info, "space_info",
  4909. space_info->flags,
  4910. ticket->bytes, 1);
  4911. list_del_init(&ticket->list);
  4912. num_bytes -= ticket->bytes;
  4913. space_info->bytes_may_use += ticket->bytes;
  4914. ticket->bytes = 0;
  4915. space_info->tickets_id++;
  4916. wake_up(&ticket->wait);
  4917. } else {
  4918. trace_btrfs_space_reservation(fs_info, "space_info",
  4919. space_info->flags,
  4920. num_bytes, 1);
  4921. space_info->bytes_may_use += num_bytes;
  4922. ticket->bytes -= num_bytes;
  4923. num_bytes = 0;
  4924. }
  4925. }
  4926. if (num_bytes && head == &space_info->priority_tickets) {
  4927. head = &space_info->tickets;
  4928. goto again;
  4929. }
  4930. }
  4931. static u64 block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  4932. struct btrfs_block_rsv *block_rsv,
  4933. struct btrfs_block_rsv *dest, u64 num_bytes,
  4934. u64 *qgroup_to_release_ret)
  4935. {
  4936. struct btrfs_space_info *space_info = block_rsv->space_info;
  4937. u64 qgroup_to_release = 0;
  4938. u64 ret;
  4939. spin_lock(&block_rsv->lock);
  4940. if (num_bytes == (u64)-1) {
  4941. num_bytes = block_rsv->size;
  4942. qgroup_to_release = block_rsv->qgroup_rsv_size;
  4943. }
  4944. block_rsv->size -= num_bytes;
  4945. if (block_rsv->reserved >= block_rsv->size) {
  4946. num_bytes = block_rsv->reserved - block_rsv->size;
  4947. block_rsv->reserved = block_rsv->size;
  4948. block_rsv->full = 1;
  4949. } else {
  4950. num_bytes = 0;
  4951. }
  4952. if (block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) {
  4953. qgroup_to_release = block_rsv->qgroup_rsv_reserved -
  4954. block_rsv->qgroup_rsv_size;
  4955. block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size;
  4956. } else {
  4957. qgroup_to_release = 0;
  4958. }
  4959. spin_unlock(&block_rsv->lock);
  4960. ret = num_bytes;
  4961. if (num_bytes > 0) {
  4962. if (dest) {
  4963. spin_lock(&dest->lock);
  4964. if (!dest->full) {
  4965. u64 bytes_to_add;
  4966. bytes_to_add = dest->size - dest->reserved;
  4967. bytes_to_add = min(num_bytes, bytes_to_add);
  4968. dest->reserved += bytes_to_add;
  4969. if (dest->reserved >= dest->size)
  4970. dest->full = 1;
  4971. num_bytes -= bytes_to_add;
  4972. }
  4973. spin_unlock(&dest->lock);
  4974. }
  4975. if (num_bytes)
  4976. space_info_add_old_bytes(fs_info, space_info,
  4977. num_bytes);
  4978. }
  4979. if (qgroup_to_release_ret)
  4980. *qgroup_to_release_ret = qgroup_to_release;
  4981. return ret;
  4982. }
  4983. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
  4984. struct btrfs_block_rsv *dst, u64 num_bytes,
  4985. int update_size)
  4986. {
  4987. int ret;
  4988. ret = block_rsv_use_bytes(src, num_bytes);
  4989. if (ret)
  4990. return ret;
  4991. block_rsv_add_bytes(dst, num_bytes, update_size);
  4992. return 0;
  4993. }
  4994. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  4995. {
  4996. memset(rsv, 0, sizeof(*rsv));
  4997. spin_lock_init(&rsv->lock);
  4998. rsv->type = type;
  4999. }
  5000. void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
  5001. struct btrfs_block_rsv *rsv,
  5002. unsigned short type)
  5003. {
  5004. btrfs_init_block_rsv(rsv, type);
  5005. rsv->space_info = __find_space_info(fs_info,
  5006. BTRFS_BLOCK_GROUP_METADATA);
  5007. }
  5008. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
  5009. unsigned short type)
  5010. {
  5011. struct btrfs_block_rsv *block_rsv;
  5012. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  5013. if (!block_rsv)
  5014. return NULL;
  5015. btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
  5016. return block_rsv;
  5017. }
  5018. void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
  5019. struct btrfs_block_rsv *rsv)
  5020. {
  5021. if (!rsv)
  5022. return;
  5023. btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
  5024. kfree(rsv);
  5025. }
  5026. void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
  5027. {
  5028. kfree(rsv);
  5029. }
  5030. int btrfs_block_rsv_add(struct btrfs_root *root,
  5031. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  5032. enum btrfs_reserve_flush_enum flush)
  5033. {
  5034. int ret;
  5035. if (num_bytes == 0)
  5036. return 0;
  5037. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  5038. if (!ret) {
  5039. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  5040. return 0;
  5041. }
  5042. return ret;
  5043. }
  5044. int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
  5045. {
  5046. u64 num_bytes = 0;
  5047. int ret = -ENOSPC;
  5048. if (!block_rsv)
  5049. return 0;
  5050. spin_lock(&block_rsv->lock);
  5051. num_bytes = div_factor(block_rsv->size, min_factor);
  5052. if (block_rsv->reserved >= num_bytes)
  5053. ret = 0;
  5054. spin_unlock(&block_rsv->lock);
  5055. return ret;
  5056. }
  5057. int btrfs_block_rsv_refill(struct btrfs_root *root,
  5058. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  5059. enum btrfs_reserve_flush_enum flush)
  5060. {
  5061. u64 num_bytes = 0;
  5062. int ret = -ENOSPC;
  5063. if (!block_rsv)
  5064. return 0;
  5065. spin_lock(&block_rsv->lock);
  5066. num_bytes = min_reserved;
  5067. if (block_rsv->reserved >= num_bytes)
  5068. ret = 0;
  5069. else
  5070. num_bytes -= block_rsv->reserved;
  5071. spin_unlock(&block_rsv->lock);
  5072. if (!ret)
  5073. return 0;
  5074. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  5075. if (!ret) {
  5076. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  5077. return 0;
  5078. }
  5079. return ret;
  5080. }
  5081. /**
  5082. * btrfs_inode_rsv_refill - refill the inode block rsv.
  5083. * @inode - the inode we are refilling.
  5084. * @flush - the flusing restriction.
  5085. *
  5086. * Essentially the same as btrfs_block_rsv_refill, except it uses the
  5087. * block_rsv->size as the minimum size. We'll either refill the missing amount
  5088. * or return if we already have enough space. This will also handle the resreve
  5089. * tracepoint for the reserved amount.
  5090. */
  5091. static int btrfs_inode_rsv_refill(struct btrfs_inode *inode,
  5092. enum btrfs_reserve_flush_enum flush)
  5093. {
  5094. struct btrfs_root *root = inode->root;
  5095. struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
  5096. u64 num_bytes = 0;
  5097. u64 qgroup_num_bytes = 0;
  5098. int ret = -ENOSPC;
  5099. spin_lock(&block_rsv->lock);
  5100. if (block_rsv->reserved < block_rsv->size)
  5101. num_bytes = block_rsv->size - block_rsv->reserved;
  5102. if (block_rsv->qgroup_rsv_reserved < block_rsv->qgroup_rsv_size)
  5103. qgroup_num_bytes = block_rsv->qgroup_rsv_size -
  5104. block_rsv->qgroup_rsv_reserved;
  5105. spin_unlock(&block_rsv->lock);
  5106. if (num_bytes == 0)
  5107. return 0;
  5108. ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_num_bytes, true);
  5109. if (ret)
  5110. return ret;
  5111. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  5112. if (!ret) {
  5113. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  5114. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  5115. btrfs_ino(inode), num_bytes, 1);
  5116. /* Don't forget to increase qgroup_rsv_reserved */
  5117. spin_lock(&block_rsv->lock);
  5118. block_rsv->qgroup_rsv_reserved += qgroup_num_bytes;
  5119. spin_unlock(&block_rsv->lock);
  5120. } else
  5121. btrfs_qgroup_free_meta_prealloc(root, qgroup_num_bytes);
  5122. return ret;
  5123. }
  5124. /**
  5125. * btrfs_inode_rsv_release - release any excessive reservation.
  5126. * @inode - the inode we need to release from.
  5127. * @qgroup_free - free or convert qgroup meta.
  5128. * Unlike normal operation, qgroup meta reservation needs to know if we are
  5129. * freeing qgroup reservation or just converting it into per-trans. Normally
  5130. * @qgroup_free is true for error handling, and false for normal release.
  5131. *
  5132. * This is the same as btrfs_block_rsv_release, except that it handles the
  5133. * tracepoint for the reservation.
  5134. */
  5135. static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
  5136. {
  5137. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  5138. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  5139. struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
  5140. u64 released = 0;
  5141. u64 qgroup_to_release = 0;
  5142. /*
  5143. * Since we statically set the block_rsv->size we just want to say we
  5144. * are releasing 0 bytes, and then we'll just get the reservation over
  5145. * the size free'd.
  5146. */
  5147. released = block_rsv_release_bytes(fs_info, block_rsv, global_rsv, 0,
  5148. &qgroup_to_release);
  5149. if (released > 0)
  5150. trace_btrfs_space_reservation(fs_info, "delalloc",
  5151. btrfs_ino(inode), released, 0);
  5152. if (qgroup_free)
  5153. btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
  5154. else
  5155. btrfs_qgroup_convert_reserved_meta(inode->root,
  5156. qgroup_to_release);
  5157. }
  5158. void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
  5159. struct btrfs_block_rsv *block_rsv,
  5160. u64 num_bytes)
  5161. {
  5162. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  5163. if (global_rsv == block_rsv ||
  5164. block_rsv->space_info != global_rsv->space_info)
  5165. global_rsv = NULL;
  5166. block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes, NULL);
  5167. }
  5168. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  5169. {
  5170. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  5171. struct btrfs_space_info *sinfo = block_rsv->space_info;
  5172. u64 num_bytes;
  5173. /*
  5174. * The global block rsv is based on the size of the extent tree, the
  5175. * checksum tree and the root tree. If the fs is empty we want to set
  5176. * it to a minimal amount for safety.
  5177. */
  5178. num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
  5179. btrfs_root_used(&fs_info->csum_root->root_item) +
  5180. btrfs_root_used(&fs_info->tree_root->root_item);
  5181. num_bytes = max_t(u64, num_bytes, SZ_16M);
  5182. spin_lock(&sinfo->lock);
  5183. spin_lock(&block_rsv->lock);
  5184. block_rsv->size = min_t(u64, num_bytes, SZ_512M);
  5185. if (block_rsv->reserved < block_rsv->size) {
  5186. num_bytes = btrfs_space_info_used(sinfo, true);
  5187. if (sinfo->total_bytes > num_bytes) {
  5188. num_bytes = sinfo->total_bytes - num_bytes;
  5189. num_bytes = min(num_bytes,
  5190. block_rsv->size - block_rsv->reserved);
  5191. block_rsv->reserved += num_bytes;
  5192. sinfo->bytes_may_use += num_bytes;
  5193. trace_btrfs_space_reservation(fs_info, "space_info",
  5194. sinfo->flags, num_bytes,
  5195. 1);
  5196. }
  5197. } else if (block_rsv->reserved > block_rsv->size) {
  5198. num_bytes = block_rsv->reserved - block_rsv->size;
  5199. sinfo->bytes_may_use -= num_bytes;
  5200. trace_btrfs_space_reservation(fs_info, "space_info",
  5201. sinfo->flags, num_bytes, 0);
  5202. block_rsv->reserved = block_rsv->size;
  5203. }
  5204. if (block_rsv->reserved == block_rsv->size)
  5205. block_rsv->full = 1;
  5206. else
  5207. block_rsv->full = 0;
  5208. spin_unlock(&block_rsv->lock);
  5209. spin_unlock(&sinfo->lock);
  5210. }
  5211. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  5212. {
  5213. struct btrfs_space_info *space_info;
  5214. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  5215. fs_info->chunk_block_rsv.space_info = space_info;
  5216. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  5217. fs_info->global_block_rsv.space_info = space_info;
  5218. fs_info->trans_block_rsv.space_info = space_info;
  5219. fs_info->empty_block_rsv.space_info = space_info;
  5220. fs_info->delayed_block_rsv.space_info = space_info;
  5221. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  5222. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  5223. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  5224. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  5225. if (fs_info->quota_root)
  5226. fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
  5227. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  5228. update_global_block_rsv(fs_info);
  5229. }
  5230. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  5231. {
  5232. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  5233. (u64)-1, NULL);
  5234. WARN_ON(fs_info->trans_block_rsv.size > 0);
  5235. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  5236. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  5237. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  5238. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  5239. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  5240. }
  5241. /*
  5242. * To be called after all the new block groups attached to the transaction
  5243. * handle have been created (btrfs_create_pending_block_groups()).
  5244. */
  5245. void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
  5246. {
  5247. struct btrfs_fs_info *fs_info = trans->fs_info;
  5248. if (!trans->chunk_bytes_reserved)
  5249. return;
  5250. WARN_ON_ONCE(!list_empty(&trans->new_bgs));
  5251. block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
  5252. trans->chunk_bytes_reserved, NULL);
  5253. trans->chunk_bytes_reserved = 0;
  5254. }
  5255. /*
  5256. * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
  5257. * root: the root of the parent directory
  5258. * rsv: block reservation
  5259. * items: the number of items that we need do reservation
  5260. * qgroup_reserved: used to return the reserved size in qgroup
  5261. *
  5262. * This function is used to reserve the space for snapshot/subvolume
  5263. * creation and deletion. Those operations are different with the
  5264. * common file/directory operations, they change two fs/file trees
  5265. * and root tree, the number of items that the qgroup reserves is
  5266. * different with the free space reservation. So we can not use
  5267. * the space reservation mechanism in start_transaction().
  5268. */
  5269. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  5270. struct btrfs_block_rsv *rsv,
  5271. int items,
  5272. bool use_global_rsv)
  5273. {
  5274. u64 num_bytes;
  5275. int ret;
  5276. struct btrfs_fs_info *fs_info = root->fs_info;
  5277. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  5278. if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
  5279. /* One for parent inode, two for dir entries */
  5280. num_bytes = 3 * fs_info->nodesize;
  5281. ret = btrfs_qgroup_reserve_meta_prealloc(root, num_bytes, true);
  5282. if (ret)
  5283. return ret;
  5284. } else {
  5285. num_bytes = 0;
  5286. }
  5287. num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
  5288. rsv->space_info = __find_space_info(fs_info,
  5289. BTRFS_BLOCK_GROUP_METADATA);
  5290. ret = btrfs_block_rsv_add(root, rsv, num_bytes,
  5291. BTRFS_RESERVE_FLUSH_ALL);
  5292. if (ret == -ENOSPC && use_global_rsv)
  5293. ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
  5294. if (ret && num_bytes)
  5295. btrfs_qgroup_free_meta_prealloc(root, num_bytes);
  5296. return ret;
  5297. }
  5298. void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
  5299. struct btrfs_block_rsv *rsv)
  5300. {
  5301. btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
  5302. }
  5303. static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
  5304. struct btrfs_inode *inode)
  5305. {
  5306. struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
  5307. u64 reserve_size = 0;
  5308. u64 qgroup_rsv_size = 0;
  5309. u64 csum_leaves;
  5310. unsigned outstanding_extents;
  5311. lockdep_assert_held(&inode->lock);
  5312. outstanding_extents = inode->outstanding_extents;
  5313. if (outstanding_extents)
  5314. reserve_size = btrfs_calc_trans_metadata_size(fs_info,
  5315. outstanding_extents + 1);
  5316. csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
  5317. inode->csum_bytes);
  5318. reserve_size += btrfs_calc_trans_metadata_size(fs_info,
  5319. csum_leaves);
  5320. /*
  5321. * For qgroup rsv, the calculation is very simple:
  5322. * account one nodesize for each outstanding extent
  5323. *
  5324. * This is overestimating in most cases.
  5325. */
  5326. qgroup_rsv_size = outstanding_extents * fs_info->nodesize;
  5327. spin_lock(&block_rsv->lock);
  5328. block_rsv->size = reserve_size;
  5329. block_rsv->qgroup_rsv_size = qgroup_rsv_size;
  5330. spin_unlock(&block_rsv->lock);
  5331. }
  5332. int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
  5333. {
  5334. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  5335. unsigned nr_extents;
  5336. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  5337. int ret = 0;
  5338. bool delalloc_lock = true;
  5339. /* If we are a free space inode we need to not flush since we will be in
  5340. * the middle of a transaction commit. We also don't need the delalloc
  5341. * mutex since we won't race with anybody. We need this mostly to make
  5342. * lockdep shut its filthy mouth.
  5343. *
  5344. * If we have a transaction open (can happen if we call truncate_block
  5345. * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
  5346. */
  5347. if (btrfs_is_free_space_inode(inode)) {
  5348. flush = BTRFS_RESERVE_NO_FLUSH;
  5349. delalloc_lock = false;
  5350. } else {
  5351. if (current->journal_info)
  5352. flush = BTRFS_RESERVE_FLUSH_LIMIT;
  5353. if (btrfs_transaction_in_commit(fs_info))
  5354. schedule_timeout(1);
  5355. }
  5356. if (delalloc_lock)
  5357. mutex_lock(&inode->delalloc_mutex);
  5358. num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
  5359. /* Add our new extents and calculate the new rsv size. */
  5360. spin_lock(&inode->lock);
  5361. nr_extents = count_max_extents(num_bytes);
  5362. btrfs_mod_outstanding_extents(inode, nr_extents);
  5363. inode->csum_bytes += num_bytes;
  5364. btrfs_calculate_inode_block_rsv_size(fs_info, inode);
  5365. spin_unlock(&inode->lock);
  5366. ret = btrfs_inode_rsv_refill(inode, flush);
  5367. if (unlikely(ret))
  5368. goto out_fail;
  5369. if (delalloc_lock)
  5370. mutex_unlock(&inode->delalloc_mutex);
  5371. return 0;
  5372. out_fail:
  5373. spin_lock(&inode->lock);
  5374. nr_extents = count_max_extents(num_bytes);
  5375. btrfs_mod_outstanding_extents(inode, -nr_extents);
  5376. inode->csum_bytes -= num_bytes;
  5377. btrfs_calculate_inode_block_rsv_size(fs_info, inode);
  5378. spin_unlock(&inode->lock);
  5379. btrfs_inode_rsv_release(inode, true);
  5380. if (delalloc_lock)
  5381. mutex_unlock(&inode->delalloc_mutex);
  5382. return ret;
  5383. }
  5384. /**
  5385. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  5386. * @inode: the inode to release the reservation for.
  5387. * @num_bytes: the number of bytes we are releasing.
  5388. * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
  5389. *
  5390. * This will release the metadata reservation for an inode. This can be called
  5391. * once we complete IO for a given set of bytes to release their metadata
  5392. * reservations, or on error for the same reason.
  5393. */
  5394. void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
  5395. bool qgroup_free)
  5396. {
  5397. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  5398. num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
  5399. spin_lock(&inode->lock);
  5400. inode->csum_bytes -= num_bytes;
  5401. btrfs_calculate_inode_block_rsv_size(fs_info, inode);
  5402. spin_unlock(&inode->lock);
  5403. if (btrfs_is_testing(fs_info))
  5404. return;
  5405. btrfs_inode_rsv_release(inode, qgroup_free);
  5406. }
  5407. /**
  5408. * btrfs_delalloc_release_extents - release our outstanding_extents
  5409. * @inode: the inode to balance the reservation for.
  5410. * @num_bytes: the number of bytes we originally reserved with
  5411. * @qgroup_free: do we need to free qgroup meta reservation or convert them.
  5412. *
  5413. * When we reserve space we increase outstanding_extents for the extents we may
  5414. * add. Once we've set the range as delalloc or created our ordered extents we
  5415. * have outstanding_extents to track the real usage, so we use this to free our
  5416. * temporarily tracked outstanding_extents. This _must_ be used in conjunction
  5417. * with btrfs_delalloc_reserve_metadata.
  5418. */
  5419. void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
  5420. bool qgroup_free)
  5421. {
  5422. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  5423. unsigned num_extents;
  5424. spin_lock(&inode->lock);
  5425. num_extents = count_max_extents(num_bytes);
  5426. btrfs_mod_outstanding_extents(inode, -num_extents);
  5427. btrfs_calculate_inode_block_rsv_size(fs_info, inode);
  5428. spin_unlock(&inode->lock);
  5429. if (btrfs_is_testing(fs_info))
  5430. return;
  5431. btrfs_inode_rsv_release(inode, qgroup_free);
  5432. }
  5433. /**
  5434. * btrfs_delalloc_reserve_space - reserve data and metadata space for
  5435. * delalloc
  5436. * @inode: inode we're writing to
  5437. * @start: start range we are writing to
  5438. * @len: how long the range we are writing to
  5439. * @reserved: mandatory parameter, record actually reserved qgroup ranges of
  5440. * current reservation.
  5441. *
  5442. * This will do the following things
  5443. *
  5444. * o reserve space in data space info for num bytes
  5445. * and reserve precious corresponding qgroup space
  5446. * (Done in check_data_free_space)
  5447. *
  5448. * o reserve space for metadata space, based on the number of outstanding
  5449. * extents and how much csums will be needed
  5450. * also reserve metadata space in a per root over-reserve method.
  5451. * o add to the inodes->delalloc_bytes
  5452. * o add it to the fs_info's delalloc inodes list.
  5453. * (Above 3 all done in delalloc_reserve_metadata)
  5454. *
  5455. * Return 0 for success
  5456. * Return <0 for error(-ENOSPC or -EQUOT)
  5457. */
  5458. int btrfs_delalloc_reserve_space(struct inode *inode,
  5459. struct extent_changeset **reserved, u64 start, u64 len)
  5460. {
  5461. int ret;
  5462. ret = btrfs_check_data_free_space(inode, reserved, start, len);
  5463. if (ret < 0)
  5464. return ret;
  5465. ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
  5466. if (ret < 0)
  5467. btrfs_free_reserved_data_space(inode, *reserved, start, len);
  5468. return ret;
  5469. }
  5470. /**
  5471. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  5472. * @inode: inode we're releasing space for
  5473. * @start: start position of the space already reserved
  5474. * @len: the len of the space already reserved
  5475. * @release_bytes: the len of the space we consumed or didn't use
  5476. *
  5477. * This function will release the metadata space that was not used and will
  5478. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  5479. * list if there are no delalloc bytes left.
  5480. * Also it will handle the qgroup reserved space.
  5481. */
  5482. void btrfs_delalloc_release_space(struct inode *inode,
  5483. struct extent_changeset *reserved,
  5484. u64 start, u64 len, bool qgroup_free)
  5485. {
  5486. btrfs_delalloc_release_metadata(BTRFS_I(inode), len, qgroup_free);
  5487. btrfs_free_reserved_data_space(inode, reserved, start, len);
  5488. }
  5489. static int update_block_group(struct btrfs_trans_handle *trans,
  5490. struct btrfs_fs_info *info, u64 bytenr,
  5491. u64 num_bytes, int alloc)
  5492. {
  5493. struct btrfs_block_group_cache *cache = NULL;
  5494. u64 total = num_bytes;
  5495. u64 old_val;
  5496. u64 byte_in_group;
  5497. int factor;
  5498. /* block accounting for super block */
  5499. spin_lock(&info->delalloc_root_lock);
  5500. old_val = btrfs_super_bytes_used(info->super_copy);
  5501. if (alloc)
  5502. old_val += num_bytes;
  5503. else
  5504. old_val -= num_bytes;
  5505. btrfs_set_super_bytes_used(info->super_copy, old_val);
  5506. spin_unlock(&info->delalloc_root_lock);
  5507. while (total) {
  5508. cache = btrfs_lookup_block_group(info, bytenr);
  5509. if (!cache)
  5510. return -ENOENT;
  5511. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  5512. BTRFS_BLOCK_GROUP_RAID1 |
  5513. BTRFS_BLOCK_GROUP_RAID10))
  5514. factor = 2;
  5515. else
  5516. factor = 1;
  5517. /*
  5518. * If this block group has free space cache written out, we
  5519. * need to make sure to load it if we are removing space. This
  5520. * is because we need the unpinning stage to actually add the
  5521. * space back to the block group, otherwise we will leak space.
  5522. */
  5523. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  5524. cache_block_group(cache, 1);
  5525. byte_in_group = bytenr - cache->key.objectid;
  5526. WARN_ON(byte_in_group > cache->key.offset);
  5527. spin_lock(&cache->space_info->lock);
  5528. spin_lock(&cache->lock);
  5529. if (btrfs_test_opt(info, SPACE_CACHE) &&
  5530. cache->disk_cache_state < BTRFS_DC_CLEAR)
  5531. cache->disk_cache_state = BTRFS_DC_CLEAR;
  5532. old_val = btrfs_block_group_used(&cache->item);
  5533. num_bytes = min(total, cache->key.offset - byte_in_group);
  5534. if (alloc) {
  5535. old_val += num_bytes;
  5536. btrfs_set_block_group_used(&cache->item, old_val);
  5537. cache->reserved -= num_bytes;
  5538. cache->space_info->bytes_reserved -= num_bytes;
  5539. cache->space_info->bytes_used += num_bytes;
  5540. cache->space_info->disk_used += num_bytes * factor;
  5541. spin_unlock(&cache->lock);
  5542. spin_unlock(&cache->space_info->lock);
  5543. } else {
  5544. old_val -= num_bytes;
  5545. btrfs_set_block_group_used(&cache->item, old_val);
  5546. cache->pinned += num_bytes;
  5547. cache->space_info->bytes_pinned += num_bytes;
  5548. cache->space_info->bytes_used -= num_bytes;
  5549. cache->space_info->disk_used -= num_bytes * factor;
  5550. spin_unlock(&cache->lock);
  5551. spin_unlock(&cache->space_info->lock);
  5552. trace_btrfs_space_reservation(info, "pinned",
  5553. cache->space_info->flags,
  5554. num_bytes, 1);
  5555. percpu_counter_add(&cache->space_info->total_bytes_pinned,
  5556. num_bytes);
  5557. set_extent_dirty(info->pinned_extents,
  5558. bytenr, bytenr + num_bytes - 1,
  5559. GFP_NOFS | __GFP_NOFAIL);
  5560. }
  5561. spin_lock(&trans->transaction->dirty_bgs_lock);
  5562. if (list_empty(&cache->dirty_list)) {
  5563. list_add_tail(&cache->dirty_list,
  5564. &trans->transaction->dirty_bgs);
  5565. trans->transaction->num_dirty_bgs++;
  5566. btrfs_get_block_group(cache);
  5567. }
  5568. spin_unlock(&trans->transaction->dirty_bgs_lock);
  5569. /*
  5570. * No longer have used bytes in this block group, queue it for
  5571. * deletion. We do this after adding the block group to the
  5572. * dirty list to avoid races between cleaner kthread and space
  5573. * cache writeout.
  5574. */
  5575. if (!alloc && old_val == 0) {
  5576. spin_lock(&info->unused_bgs_lock);
  5577. if (list_empty(&cache->bg_list)) {
  5578. btrfs_get_block_group(cache);
  5579. trace_btrfs_add_unused_block_group(cache);
  5580. list_add_tail(&cache->bg_list,
  5581. &info->unused_bgs);
  5582. }
  5583. spin_unlock(&info->unused_bgs_lock);
  5584. }
  5585. btrfs_put_block_group(cache);
  5586. total -= num_bytes;
  5587. bytenr += num_bytes;
  5588. }
  5589. return 0;
  5590. }
  5591. static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
  5592. {
  5593. struct btrfs_block_group_cache *cache;
  5594. u64 bytenr;
  5595. spin_lock(&fs_info->block_group_cache_lock);
  5596. bytenr = fs_info->first_logical_byte;
  5597. spin_unlock(&fs_info->block_group_cache_lock);
  5598. if (bytenr < (u64)-1)
  5599. return bytenr;
  5600. cache = btrfs_lookup_first_block_group(fs_info, search_start);
  5601. if (!cache)
  5602. return 0;
  5603. bytenr = cache->key.objectid;
  5604. btrfs_put_block_group(cache);
  5605. return bytenr;
  5606. }
  5607. static int pin_down_extent(struct btrfs_fs_info *fs_info,
  5608. struct btrfs_block_group_cache *cache,
  5609. u64 bytenr, u64 num_bytes, int reserved)
  5610. {
  5611. spin_lock(&cache->space_info->lock);
  5612. spin_lock(&cache->lock);
  5613. cache->pinned += num_bytes;
  5614. cache->space_info->bytes_pinned += num_bytes;
  5615. if (reserved) {
  5616. cache->reserved -= num_bytes;
  5617. cache->space_info->bytes_reserved -= num_bytes;
  5618. }
  5619. spin_unlock(&cache->lock);
  5620. spin_unlock(&cache->space_info->lock);
  5621. trace_btrfs_space_reservation(fs_info, "pinned",
  5622. cache->space_info->flags, num_bytes, 1);
  5623. percpu_counter_add(&cache->space_info->total_bytes_pinned, num_bytes);
  5624. set_extent_dirty(fs_info->pinned_extents, bytenr,
  5625. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  5626. return 0;
  5627. }
  5628. /*
  5629. * this function must be called within transaction
  5630. */
  5631. int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
  5632. u64 bytenr, u64 num_bytes, int reserved)
  5633. {
  5634. struct btrfs_block_group_cache *cache;
  5635. cache = btrfs_lookup_block_group(fs_info, bytenr);
  5636. BUG_ON(!cache); /* Logic error */
  5637. pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
  5638. btrfs_put_block_group(cache);
  5639. return 0;
  5640. }
  5641. /*
  5642. * this function must be called within transaction
  5643. */
  5644. int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
  5645. u64 bytenr, u64 num_bytes)
  5646. {
  5647. struct btrfs_block_group_cache *cache;
  5648. int ret;
  5649. cache = btrfs_lookup_block_group(fs_info, bytenr);
  5650. if (!cache)
  5651. return -EINVAL;
  5652. /*
  5653. * pull in the free space cache (if any) so that our pin
  5654. * removes the free space from the cache. We have load_only set
  5655. * to one because the slow code to read in the free extents does check
  5656. * the pinned extents.
  5657. */
  5658. cache_block_group(cache, 1);
  5659. pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
  5660. /* remove us from the free space cache (if we're there at all) */
  5661. ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
  5662. btrfs_put_block_group(cache);
  5663. return ret;
  5664. }
  5665. static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
  5666. u64 start, u64 num_bytes)
  5667. {
  5668. int ret;
  5669. struct btrfs_block_group_cache *block_group;
  5670. struct btrfs_caching_control *caching_ctl;
  5671. block_group = btrfs_lookup_block_group(fs_info, start);
  5672. if (!block_group)
  5673. return -EINVAL;
  5674. cache_block_group(block_group, 0);
  5675. caching_ctl = get_caching_control(block_group);
  5676. if (!caching_ctl) {
  5677. /* Logic error */
  5678. BUG_ON(!block_group_cache_done(block_group));
  5679. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  5680. } else {
  5681. mutex_lock(&caching_ctl->mutex);
  5682. if (start >= caching_ctl->progress) {
  5683. ret = add_excluded_extent(fs_info, start, num_bytes);
  5684. } else if (start + num_bytes <= caching_ctl->progress) {
  5685. ret = btrfs_remove_free_space(block_group,
  5686. start, num_bytes);
  5687. } else {
  5688. num_bytes = caching_ctl->progress - start;
  5689. ret = btrfs_remove_free_space(block_group,
  5690. start, num_bytes);
  5691. if (ret)
  5692. goto out_lock;
  5693. num_bytes = (start + num_bytes) -
  5694. caching_ctl->progress;
  5695. start = caching_ctl->progress;
  5696. ret = add_excluded_extent(fs_info, start, num_bytes);
  5697. }
  5698. out_lock:
  5699. mutex_unlock(&caching_ctl->mutex);
  5700. put_caching_control(caching_ctl);
  5701. }
  5702. btrfs_put_block_group(block_group);
  5703. return ret;
  5704. }
  5705. int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
  5706. struct extent_buffer *eb)
  5707. {
  5708. struct btrfs_file_extent_item *item;
  5709. struct btrfs_key key;
  5710. int found_type;
  5711. int i;
  5712. int ret = 0;
  5713. if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
  5714. return 0;
  5715. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  5716. btrfs_item_key_to_cpu(eb, &key, i);
  5717. if (key.type != BTRFS_EXTENT_DATA_KEY)
  5718. continue;
  5719. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  5720. found_type = btrfs_file_extent_type(eb, item);
  5721. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  5722. continue;
  5723. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  5724. continue;
  5725. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  5726. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  5727. ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
  5728. if (ret)
  5729. break;
  5730. }
  5731. return ret;
  5732. }
  5733. static void
  5734. btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
  5735. {
  5736. atomic_inc(&bg->reservations);
  5737. }
  5738. void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
  5739. const u64 start)
  5740. {
  5741. struct btrfs_block_group_cache *bg;
  5742. bg = btrfs_lookup_block_group(fs_info, start);
  5743. ASSERT(bg);
  5744. if (atomic_dec_and_test(&bg->reservations))
  5745. wake_up_var(&bg->reservations);
  5746. btrfs_put_block_group(bg);
  5747. }
  5748. void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
  5749. {
  5750. struct btrfs_space_info *space_info = bg->space_info;
  5751. ASSERT(bg->ro);
  5752. if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
  5753. return;
  5754. /*
  5755. * Our block group is read only but before we set it to read only,
  5756. * some task might have had allocated an extent from it already, but it
  5757. * has not yet created a respective ordered extent (and added it to a
  5758. * root's list of ordered extents).
  5759. * Therefore wait for any task currently allocating extents, since the
  5760. * block group's reservations counter is incremented while a read lock
  5761. * on the groups' semaphore is held and decremented after releasing
  5762. * the read access on that semaphore and creating the ordered extent.
  5763. */
  5764. down_write(&space_info->groups_sem);
  5765. up_write(&space_info->groups_sem);
  5766. wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
  5767. }
  5768. /**
  5769. * btrfs_add_reserved_bytes - update the block_group and space info counters
  5770. * @cache: The cache we are manipulating
  5771. * @ram_bytes: The number of bytes of file content, and will be same to
  5772. * @num_bytes except for the compress path.
  5773. * @num_bytes: The number of bytes in question
  5774. * @delalloc: The blocks are allocated for the delalloc write
  5775. *
  5776. * This is called by the allocator when it reserves space. If this is a
  5777. * reservation and the block group has become read only we cannot make the
  5778. * reservation and return -EAGAIN, otherwise this function always succeeds.
  5779. */
  5780. static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
  5781. u64 ram_bytes, u64 num_bytes, int delalloc)
  5782. {
  5783. struct btrfs_space_info *space_info = cache->space_info;
  5784. int ret = 0;
  5785. spin_lock(&space_info->lock);
  5786. spin_lock(&cache->lock);
  5787. if (cache->ro) {
  5788. ret = -EAGAIN;
  5789. } else {
  5790. cache->reserved += num_bytes;
  5791. space_info->bytes_reserved += num_bytes;
  5792. trace_btrfs_space_reservation(cache->fs_info,
  5793. "space_info", space_info->flags,
  5794. ram_bytes, 0);
  5795. space_info->bytes_may_use -= ram_bytes;
  5796. if (delalloc)
  5797. cache->delalloc_bytes += num_bytes;
  5798. }
  5799. spin_unlock(&cache->lock);
  5800. spin_unlock(&space_info->lock);
  5801. return ret;
  5802. }
  5803. /**
  5804. * btrfs_free_reserved_bytes - update the block_group and space info counters
  5805. * @cache: The cache we are manipulating
  5806. * @num_bytes: The number of bytes in question
  5807. * @delalloc: The blocks are allocated for the delalloc write
  5808. *
  5809. * This is called by somebody who is freeing space that was never actually used
  5810. * on disk. For example if you reserve some space for a new leaf in transaction
  5811. * A and before transaction A commits you free that leaf, you call this with
  5812. * reserve set to 0 in order to clear the reservation.
  5813. */
  5814. static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
  5815. u64 num_bytes, int delalloc)
  5816. {
  5817. struct btrfs_space_info *space_info = cache->space_info;
  5818. int ret = 0;
  5819. spin_lock(&space_info->lock);
  5820. spin_lock(&cache->lock);
  5821. if (cache->ro)
  5822. space_info->bytes_readonly += num_bytes;
  5823. cache->reserved -= num_bytes;
  5824. space_info->bytes_reserved -= num_bytes;
  5825. if (delalloc)
  5826. cache->delalloc_bytes -= num_bytes;
  5827. spin_unlock(&cache->lock);
  5828. spin_unlock(&space_info->lock);
  5829. return ret;
  5830. }
  5831. void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
  5832. {
  5833. struct btrfs_caching_control *next;
  5834. struct btrfs_caching_control *caching_ctl;
  5835. struct btrfs_block_group_cache *cache;
  5836. down_write(&fs_info->commit_root_sem);
  5837. list_for_each_entry_safe(caching_ctl, next,
  5838. &fs_info->caching_block_groups, list) {
  5839. cache = caching_ctl->block_group;
  5840. if (block_group_cache_done(cache)) {
  5841. cache->last_byte_to_unpin = (u64)-1;
  5842. list_del_init(&caching_ctl->list);
  5843. put_caching_control(caching_ctl);
  5844. } else {
  5845. cache->last_byte_to_unpin = caching_ctl->progress;
  5846. }
  5847. }
  5848. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  5849. fs_info->pinned_extents = &fs_info->freed_extents[1];
  5850. else
  5851. fs_info->pinned_extents = &fs_info->freed_extents[0];
  5852. up_write(&fs_info->commit_root_sem);
  5853. update_global_block_rsv(fs_info);
  5854. }
  5855. /*
  5856. * Returns the free cluster for the given space info and sets empty_cluster to
  5857. * what it should be based on the mount options.
  5858. */
  5859. static struct btrfs_free_cluster *
  5860. fetch_cluster_info(struct btrfs_fs_info *fs_info,
  5861. struct btrfs_space_info *space_info, u64 *empty_cluster)
  5862. {
  5863. struct btrfs_free_cluster *ret = NULL;
  5864. *empty_cluster = 0;
  5865. if (btrfs_mixed_space_info(space_info))
  5866. return ret;
  5867. if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
  5868. ret = &fs_info->meta_alloc_cluster;
  5869. if (btrfs_test_opt(fs_info, SSD))
  5870. *empty_cluster = SZ_2M;
  5871. else
  5872. *empty_cluster = SZ_64K;
  5873. } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
  5874. btrfs_test_opt(fs_info, SSD_SPREAD)) {
  5875. *empty_cluster = SZ_2M;
  5876. ret = &fs_info->data_alloc_cluster;
  5877. }
  5878. return ret;
  5879. }
  5880. static int unpin_extent_range(struct btrfs_fs_info *fs_info,
  5881. u64 start, u64 end,
  5882. const bool return_free_space)
  5883. {
  5884. struct btrfs_block_group_cache *cache = NULL;
  5885. struct btrfs_space_info *space_info;
  5886. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  5887. struct btrfs_free_cluster *cluster = NULL;
  5888. u64 len;
  5889. u64 total_unpinned = 0;
  5890. u64 empty_cluster = 0;
  5891. bool readonly;
  5892. while (start <= end) {
  5893. readonly = false;
  5894. if (!cache ||
  5895. start >= cache->key.objectid + cache->key.offset) {
  5896. if (cache)
  5897. btrfs_put_block_group(cache);
  5898. total_unpinned = 0;
  5899. cache = btrfs_lookup_block_group(fs_info, start);
  5900. BUG_ON(!cache); /* Logic error */
  5901. cluster = fetch_cluster_info(fs_info,
  5902. cache->space_info,
  5903. &empty_cluster);
  5904. empty_cluster <<= 1;
  5905. }
  5906. len = cache->key.objectid + cache->key.offset - start;
  5907. len = min(len, end + 1 - start);
  5908. if (start < cache->last_byte_to_unpin) {
  5909. len = min(len, cache->last_byte_to_unpin - start);
  5910. if (return_free_space)
  5911. btrfs_add_free_space(cache, start, len);
  5912. }
  5913. start += len;
  5914. total_unpinned += len;
  5915. space_info = cache->space_info;
  5916. /*
  5917. * If this space cluster has been marked as fragmented and we've
  5918. * unpinned enough in this block group to potentially allow a
  5919. * cluster to be created inside of it go ahead and clear the
  5920. * fragmented check.
  5921. */
  5922. if (cluster && cluster->fragmented &&
  5923. total_unpinned > empty_cluster) {
  5924. spin_lock(&cluster->lock);
  5925. cluster->fragmented = 0;
  5926. spin_unlock(&cluster->lock);
  5927. }
  5928. spin_lock(&space_info->lock);
  5929. spin_lock(&cache->lock);
  5930. cache->pinned -= len;
  5931. space_info->bytes_pinned -= len;
  5932. trace_btrfs_space_reservation(fs_info, "pinned",
  5933. space_info->flags, len, 0);
  5934. space_info->max_extent_size = 0;
  5935. percpu_counter_add(&space_info->total_bytes_pinned, -len);
  5936. if (cache->ro) {
  5937. space_info->bytes_readonly += len;
  5938. readonly = true;
  5939. }
  5940. spin_unlock(&cache->lock);
  5941. if (!readonly && return_free_space &&
  5942. global_rsv->space_info == space_info) {
  5943. u64 to_add = len;
  5944. spin_lock(&global_rsv->lock);
  5945. if (!global_rsv->full) {
  5946. to_add = min(len, global_rsv->size -
  5947. global_rsv->reserved);
  5948. global_rsv->reserved += to_add;
  5949. space_info->bytes_may_use += to_add;
  5950. if (global_rsv->reserved >= global_rsv->size)
  5951. global_rsv->full = 1;
  5952. trace_btrfs_space_reservation(fs_info,
  5953. "space_info",
  5954. space_info->flags,
  5955. to_add, 1);
  5956. len -= to_add;
  5957. }
  5958. spin_unlock(&global_rsv->lock);
  5959. /* Add to any tickets we may have */
  5960. if (len)
  5961. space_info_add_new_bytes(fs_info, space_info,
  5962. len);
  5963. }
  5964. spin_unlock(&space_info->lock);
  5965. }
  5966. if (cache)
  5967. btrfs_put_block_group(cache);
  5968. return 0;
  5969. }
  5970. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
  5971. {
  5972. struct btrfs_fs_info *fs_info = trans->fs_info;
  5973. struct btrfs_block_group_cache *block_group, *tmp;
  5974. struct list_head *deleted_bgs;
  5975. struct extent_io_tree *unpin;
  5976. u64 start;
  5977. u64 end;
  5978. int ret;
  5979. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  5980. unpin = &fs_info->freed_extents[1];
  5981. else
  5982. unpin = &fs_info->freed_extents[0];
  5983. while (!trans->aborted) {
  5984. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  5985. ret = find_first_extent_bit(unpin, 0, &start, &end,
  5986. EXTENT_DIRTY, NULL);
  5987. if (ret) {
  5988. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  5989. break;
  5990. }
  5991. if (btrfs_test_opt(fs_info, DISCARD))
  5992. ret = btrfs_discard_extent(fs_info, start,
  5993. end + 1 - start, NULL);
  5994. clear_extent_dirty(unpin, start, end);
  5995. unpin_extent_range(fs_info, start, end, true);
  5996. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  5997. cond_resched();
  5998. }
  5999. /*
  6000. * Transaction is finished. We don't need the lock anymore. We
  6001. * do need to clean up the block groups in case of a transaction
  6002. * abort.
  6003. */
  6004. deleted_bgs = &trans->transaction->deleted_bgs;
  6005. list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
  6006. u64 trimmed = 0;
  6007. ret = -EROFS;
  6008. if (!trans->aborted)
  6009. ret = btrfs_discard_extent(fs_info,
  6010. block_group->key.objectid,
  6011. block_group->key.offset,
  6012. &trimmed);
  6013. list_del_init(&block_group->bg_list);
  6014. btrfs_put_block_group_trimming(block_group);
  6015. btrfs_put_block_group(block_group);
  6016. if (ret) {
  6017. const char *errstr = btrfs_decode_error(ret);
  6018. btrfs_warn(fs_info,
  6019. "discard failed while removing blockgroup: errno=%d %s",
  6020. ret, errstr);
  6021. }
  6022. }
  6023. return 0;
  6024. }
  6025. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  6026. struct btrfs_delayed_ref_node *node, u64 parent,
  6027. u64 root_objectid, u64 owner_objectid,
  6028. u64 owner_offset, int refs_to_drop,
  6029. struct btrfs_delayed_extent_op *extent_op)
  6030. {
  6031. struct btrfs_fs_info *info = trans->fs_info;
  6032. struct btrfs_key key;
  6033. struct btrfs_path *path;
  6034. struct btrfs_root *extent_root = info->extent_root;
  6035. struct extent_buffer *leaf;
  6036. struct btrfs_extent_item *ei;
  6037. struct btrfs_extent_inline_ref *iref;
  6038. int ret;
  6039. int is_data;
  6040. int extent_slot = 0;
  6041. int found_extent = 0;
  6042. int num_to_del = 1;
  6043. u32 item_size;
  6044. u64 refs;
  6045. u64 bytenr = node->bytenr;
  6046. u64 num_bytes = node->num_bytes;
  6047. int last_ref = 0;
  6048. bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
  6049. path = btrfs_alloc_path();
  6050. if (!path)
  6051. return -ENOMEM;
  6052. path->reada = READA_FORWARD;
  6053. path->leave_spinning = 1;
  6054. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  6055. BUG_ON(!is_data && refs_to_drop != 1);
  6056. if (is_data)
  6057. skinny_metadata = false;
  6058. ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
  6059. parent, root_objectid, owner_objectid,
  6060. owner_offset);
  6061. if (ret == 0) {
  6062. extent_slot = path->slots[0];
  6063. while (extent_slot >= 0) {
  6064. btrfs_item_key_to_cpu(path->nodes[0], &key,
  6065. extent_slot);
  6066. if (key.objectid != bytenr)
  6067. break;
  6068. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  6069. key.offset == num_bytes) {
  6070. found_extent = 1;
  6071. break;
  6072. }
  6073. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  6074. key.offset == owner_objectid) {
  6075. found_extent = 1;
  6076. break;
  6077. }
  6078. if (path->slots[0] - extent_slot > 5)
  6079. break;
  6080. extent_slot--;
  6081. }
  6082. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  6083. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  6084. if (found_extent && item_size < sizeof(*ei))
  6085. found_extent = 0;
  6086. #endif
  6087. if (!found_extent) {
  6088. BUG_ON(iref);
  6089. ret = remove_extent_backref(trans, info, path, NULL,
  6090. refs_to_drop,
  6091. is_data, &last_ref);
  6092. if (ret) {
  6093. btrfs_abort_transaction(trans, ret);
  6094. goto out;
  6095. }
  6096. btrfs_release_path(path);
  6097. path->leave_spinning = 1;
  6098. key.objectid = bytenr;
  6099. key.type = BTRFS_EXTENT_ITEM_KEY;
  6100. key.offset = num_bytes;
  6101. if (!is_data && skinny_metadata) {
  6102. key.type = BTRFS_METADATA_ITEM_KEY;
  6103. key.offset = owner_objectid;
  6104. }
  6105. ret = btrfs_search_slot(trans, extent_root,
  6106. &key, path, -1, 1);
  6107. if (ret > 0 && skinny_metadata && path->slots[0]) {
  6108. /*
  6109. * Couldn't find our skinny metadata item,
  6110. * see if we have ye olde extent item.
  6111. */
  6112. path->slots[0]--;
  6113. btrfs_item_key_to_cpu(path->nodes[0], &key,
  6114. path->slots[0]);
  6115. if (key.objectid == bytenr &&
  6116. key.type == BTRFS_EXTENT_ITEM_KEY &&
  6117. key.offset == num_bytes)
  6118. ret = 0;
  6119. }
  6120. if (ret > 0 && skinny_metadata) {
  6121. skinny_metadata = false;
  6122. key.objectid = bytenr;
  6123. key.type = BTRFS_EXTENT_ITEM_KEY;
  6124. key.offset = num_bytes;
  6125. btrfs_release_path(path);
  6126. ret = btrfs_search_slot(trans, extent_root,
  6127. &key, path, -1, 1);
  6128. }
  6129. if (ret) {
  6130. btrfs_err(info,
  6131. "umm, got %d back from search, was looking for %llu",
  6132. ret, bytenr);
  6133. if (ret > 0)
  6134. btrfs_print_leaf(path->nodes[0]);
  6135. }
  6136. if (ret < 0) {
  6137. btrfs_abort_transaction(trans, ret);
  6138. goto out;
  6139. }
  6140. extent_slot = path->slots[0];
  6141. }
  6142. } else if (WARN_ON(ret == -ENOENT)) {
  6143. btrfs_print_leaf(path->nodes[0]);
  6144. btrfs_err(info,
  6145. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
  6146. bytenr, parent, root_objectid, owner_objectid,
  6147. owner_offset);
  6148. btrfs_abort_transaction(trans, ret);
  6149. goto out;
  6150. } else {
  6151. btrfs_abort_transaction(trans, ret);
  6152. goto out;
  6153. }
  6154. leaf = path->nodes[0];
  6155. item_size = btrfs_item_size_nr(leaf, extent_slot);
  6156. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  6157. if (item_size < sizeof(*ei)) {
  6158. BUG_ON(found_extent || extent_slot != path->slots[0]);
  6159. ret = convert_extent_item_v0(trans, info, path, owner_objectid,
  6160. 0);
  6161. if (ret < 0) {
  6162. btrfs_abort_transaction(trans, ret);
  6163. goto out;
  6164. }
  6165. btrfs_release_path(path);
  6166. path->leave_spinning = 1;
  6167. key.objectid = bytenr;
  6168. key.type = BTRFS_EXTENT_ITEM_KEY;
  6169. key.offset = num_bytes;
  6170. ret = btrfs_search_slot(trans, extent_root, &key, path,
  6171. -1, 1);
  6172. if (ret) {
  6173. btrfs_err(info,
  6174. "umm, got %d back from search, was looking for %llu",
  6175. ret, bytenr);
  6176. btrfs_print_leaf(path->nodes[0]);
  6177. }
  6178. if (ret < 0) {
  6179. btrfs_abort_transaction(trans, ret);
  6180. goto out;
  6181. }
  6182. extent_slot = path->slots[0];
  6183. leaf = path->nodes[0];
  6184. item_size = btrfs_item_size_nr(leaf, extent_slot);
  6185. }
  6186. #endif
  6187. BUG_ON(item_size < sizeof(*ei));
  6188. ei = btrfs_item_ptr(leaf, extent_slot,
  6189. struct btrfs_extent_item);
  6190. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  6191. key.type == BTRFS_EXTENT_ITEM_KEY) {
  6192. struct btrfs_tree_block_info *bi;
  6193. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  6194. bi = (struct btrfs_tree_block_info *)(ei + 1);
  6195. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  6196. }
  6197. refs = btrfs_extent_refs(leaf, ei);
  6198. if (refs < refs_to_drop) {
  6199. btrfs_err(info,
  6200. "trying to drop %d refs but we only have %Lu for bytenr %Lu",
  6201. refs_to_drop, refs, bytenr);
  6202. ret = -EINVAL;
  6203. btrfs_abort_transaction(trans, ret);
  6204. goto out;
  6205. }
  6206. refs -= refs_to_drop;
  6207. if (refs > 0) {
  6208. if (extent_op)
  6209. __run_delayed_extent_op(extent_op, leaf, ei);
  6210. /*
  6211. * In the case of inline back ref, reference count will
  6212. * be updated by remove_extent_backref
  6213. */
  6214. if (iref) {
  6215. BUG_ON(!found_extent);
  6216. } else {
  6217. btrfs_set_extent_refs(leaf, ei, refs);
  6218. btrfs_mark_buffer_dirty(leaf);
  6219. }
  6220. if (found_extent) {
  6221. ret = remove_extent_backref(trans, info, path,
  6222. iref, refs_to_drop,
  6223. is_data, &last_ref);
  6224. if (ret) {
  6225. btrfs_abort_transaction(trans, ret);
  6226. goto out;
  6227. }
  6228. }
  6229. } else {
  6230. if (found_extent) {
  6231. BUG_ON(is_data && refs_to_drop !=
  6232. extent_data_ref_count(path, iref));
  6233. if (iref) {
  6234. BUG_ON(path->slots[0] != extent_slot);
  6235. } else {
  6236. BUG_ON(path->slots[0] != extent_slot + 1);
  6237. path->slots[0] = extent_slot;
  6238. num_to_del = 2;
  6239. }
  6240. }
  6241. last_ref = 1;
  6242. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  6243. num_to_del);
  6244. if (ret) {
  6245. btrfs_abort_transaction(trans, ret);
  6246. goto out;
  6247. }
  6248. btrfs_release_path(path);
  6249. if (is_data) {
  6250. ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
  6251. if (ret) {
  6252. btrfs_abort_transaction(trans, ret);
  6253. goto out;
  6254. }
  6255. }
  6256. ret = add_to_free_space_tree(trans, bytenr, num_bytes);
  6257. if (ret) {
  6258. btrfs_abort_transaction(trans, ret);
  6259. goto out;
  6260. }
  6261. ret = update_block_group(trans, info, bytenr, num_bytes, 0);
  6262. if (ret) {
  6263. btrfs_abort_transaction(trans, ret);
  6264. goto out;
  6265. }
  6266. }
  6267. btrfs_release_path(path);
  6268. out:
  6269. btrfs_free_path(path);
  6270. return ret;
  6271. }
  6272. /*
  6273. * when we free an block, it is possible (and likely) that we free the last
  6274. * delayed ref for that extent as well. This searches the delayed ref tree for
  6275. * a given extent, and if there are no other delayed refs to be processed, it
  6276. * removes it from the tree.
  6277. */
  6278. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  6279. u64 bytenr)
  6280. {
  6281. struct btrfs_delayed_ref_head *head;
  6282. struct btrfs_delayed_ref_root *delayed_refs;
  6283. int ret = 0;
  6284. delayed_refs = &trans->transaction->delayed_refs;
  6285. spin_lock(&delayed_refs->lock);
  6286. head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
  6287. if (!head)
  6288. goto out_delayed_unlock;
  6289. spin_lock(&head->lock);
  6290. if (!RB_EMPTY_ROOT(&head->ref_tree))
  6291. goto out;
  6292. if (head->extent_op) {
  6293. if (!head->must_insert_reserved)
  6294. goto out;
  6295. btrfs_free_delayed_extent_op(head->extent_op);
  6296. head->extent_op = NULL;
  6297. }
  6298. /*
  6299. * waiting for the lock here would deadlock. If someone else has it
  6300. * locked they are already in the process of dropping it anyway
  6301. */
  6302. if (!mutex_trylock(&head->mutex))
  6303. goto out;
  6304. /*
  6305. * at this point we have a head with no other entries. Go
  6306. * ahead and process it.
  6307. */
  6308. rb_erase(&head->href_node, &delayed_refs->href_root);
  6309. RB_CLEAR_NODE(&head->href_node);
  6310. atomic_dec(&delayed_refs->num_entries);
  6311. /*
  6312. * we don't take a ref on the node because we're removing it from the
  6313. * tree, so we just steal the ref the tree was holding.
  6314. */
  6315. delayed_refs->num_heads--;
  6316. if (head->processing == 0)
  6317. delayed_refs->num_heads_ready--;
  6318. head->processing = 0;
  6319. spin_unlock(&head->lock);
  6320. spin_unlock(&delayed_refs->lock);
  6321. BUG_ON(head->extent_op);
  6322. if (head->must_insert_reserved)
  6323. ret = 1;
  6324. mutex_unlock(&head->mutex);
  6325. btrfs_put_delayed_ref_head(head);
  6326. return ret;
  6327. out:
  6328. spin_unlock(&head->lock);
  6329. out_delayed_unlock:
  6330. spin_unlock(&delayed_refs->lock);
  6331. return 0;
  6332. }
  6333. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  6334. struct btrfs_root *root,
  6335. struct extent_buffer *buf,
  6336. u64 parent, int last_ref)
  6337. {
  6338. struct btrfs_fs_info *fs_info = root->fs_info;
  6339. int pin = 1;
  6340. int ret;
  6341. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  6342. int old_ref_mod, new_ref_mod;
  6343. btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
  6344. root->root_key.objectid,
  6345. btrfs_header_level(buf), 0,
  6346. BTRFS_DROP_DELAYED_REF);
  6347. ret = btrfs_add_delayed_tree_ref(trans, buf->start,
  6348. buf->len, parent,
  6349. root->root_key.objectid,
  6350. btrfs_header_level(buf),
  6351. BTRFS_DROP_DELAYED_REF, NULL,
  6352. &old_ref_mod, &new_ref_mod);
  6353. BUG_ON(ret); /* -ENOMEM */
  6354. pin = old_ref_mod >= 0 && new_ref_mod < 0;
  6355. }
  6356. if (last_ref && btrfs_header_generation(buf) == trans->transid) {
  6357. struct btrfs_block_group_cache *cache;
  6358. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  6359. ret = check_ref_cleanup(trans, buf->start);
  6360. if (!ret)
  6361. goto out;
  6362. }
  6363. pin = 0;
  6364. cache = btrfs_lookup_block_group(fs_info, buf->start);
  6365. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  6366. pin_down_extent(fs_info, cache, buf->start,
  6367. buf->len, 1);
  6368. btrfs_put_block_group(cache);
  6369. goto out;
  6370. }
  6371. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  6372. btrfs_add_free_space(cache, buf->start, buf->len);
  6373. btrfs_free_reserved_bytes(cache, buf->len, 0);
  6374. btrfs_put_block_group(cache);
  6375. trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
  6376. }
  6377. out:
  6378. if (pin)
  6379. add_pinned_bytes(fs_info, buf->len, true,
  6380. root->root_key.objectid);
  6381. if (last_ref) {
  6382. /*
  6383. * Deleting the buffer, clear the corrupt flag since it doesn't
  6384. * matter anymore.
  6385. */
  6386. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  6387. }
  6388. }
  6389. /* Can return -ENOMEM */
  6390. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  6391. struct btrfs_root *root,
  6392. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  6393. u64 owner, u64 offset)
  6394. {
  6395. struct btrfs_fs_info *fs_info = root->fs_info;
  6396. int old_ref_mod, new_ref_mod;
  6397. int ret;
  6398. if (btrfs_is_testing(fs_info))
  6399. return 0;
  6400. if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
  6401. btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
  6402. root_objectid, owner, offset,
  6403. BTRFS_DROP_DELAYED_REF);
  6404. /*
  6405. * tree log blocks never actually go into the extent allocation
  6406. * tree, just update pinning info and exit early.
  6407. */
  6408. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  6409. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  6410. /* unlocks the pinned mutex */
  6411. btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
  6412. old_ref_mod = new_ref_mod = 0;
  6413. ret = 0;
  6414. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  6415. ret = btrfs_add_delayed_tree_ref(trans, bytenr,
  6416. num_bytes, parent,
  6417. root_objectid, (int)owner,
  6418. BTRFS_DROP_DELAYED_REF, NULL,
  6419. &old_ref_mod, &new_ref_mod);
  6420. } else {
  6421. ret = btrfs_add_delayed_data_ref(trans, bytenr,
  6422. num_bytes, parent,
  6423. root_objectid, owner, offset,
  6424. 0, BTRFS_DROP_DELAYED_REF,
  6425. &old_ref_mod, &new_ref_mod);
  6426. }
  6427. if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) {
  6428. bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
  6429. add_pinned_bytes(fs_info, num_bytes, metadata, root_objectid);
  6430. }
  6431. return ret;
  6432. }
  6433. /*
  6434. * when we wait for progress in the block group caching, its because
  6435. * our allocation attempt failed at least once. So, we must sleep
  6436. * and let some progress happen before we try again.
  6437. *
  6438. * This function will sleep at least once waiting for new free space to
  6439. * show up, and then it will check the block group free space numbers
  6440. * for our min num_bytes. Another option is to have it go ahead
  6441. * and look in the rbtree for a free extent of a given size, but this
  6442. * is a good start.
  6443. *
  6444. * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
  6445. * any of the information in this block group.
  6446. */
  6447. static noinline void
  6448. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  6449. u64 num_bytes)
  6450. {
  6451. struct btrfs_caching_control *caching_ctl;
  6452. caching_ctl = get_caching_control(cache);
  6453. if (!caching_ctl)
  6454. return;
  6455. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  6456. (cache->free_space_ctl->free_space >= num_bytes));
  6457. put_caching_control(caching_ctl);
  6458. }
  6459. static noinline int
  6460. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  6461. {
  6462. struct btrfs_caching_control *caching_ctl;
  6463. int ret = 0;
  6464. caching_ctl = get_caching_control(cache);
  6465. if (!caching_ctl)
  6466. return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
  6467. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  6468. if (cache->cached == BTRFS_CACHE_ERROR)
  6469. ret = -EIO;
  6470. put_caching_control(caching_ctl);
  6471. return ret;
  6472. }
  6473. enum btrfs_loop_type {
  6474. LOOP_CACHING_NOWAIT = 0,
  6475. LOOP_CACHING_WAIT = 1,
  6476. LOOP_ALLOC_CHUNK = 2,
  6477. LOOP_NO_EMPTY_SIZE = 3,
  6478. };
  6479. static inline void
  6480. btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
  6481. int delalloc)
  6482. {
  6483. if (delalloc)
  6484. down_read(&cache->data_rwsem);
  6485. }
  6486. static inline void
  6487. btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
  6488. int delalloc)
  6489. {
  6490. btrfs_get_block_group(cache);
  6491. if (delalloc)
  6492. down_read(&cache->data_rwsem);
  6493. }
  6494. static struct btrfs_block_group_cache *
  6495. btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
  6496. struct btrfs_free_cluster *cluster,
  6497. int delalloc)
  6498. {
  6499. struct btrfs_block_group_cache *used_bg = NULL;
  6500. spin_lock(&cluster->refill_lock);
  6501. while (1) {
  6502. used_bg = cluster->block_group;
  6503. if (!used_bg)
  6504. return NULL;
  6505. if (used_bg == block_group)
  6506. return used_bg;
  6507. btrfs_get_block_group(used_bg);
  6508. if (!delalloc)
  6509. return used_bg;
  6510. if (down_read_trylock(&used_bg->data_rwsem))
  6511. return used_bg;
  6512. spin_unlock(&cluster->refill_lock);
  6513. /* We should only have one-level nested. */
  6514. down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
  6515. spin_lock(&cluster->refill_lock);
  6516. if (used_bg == cluster->block_group)
  6517. return used_bg;
  6518. up_read(&used_bg->data_rwsem);
  6519. btrfs_put_block_group(used_bg);
  6520. }
  6521. }
  6522. static inline void
  6523. btrfs_release_block_group(struct btrfs_block_group_cache *cache,
  6524. int delalloc)
  6525. {
  6526. if (delalloc)
  6527. up_read(&cache->data_rwsem);
  6528. btrfs_put_block_group(cache);
  6529. }
  6530. /*
  6531. * walks the btree of allocated extents and find a hole of a given size.
  6532. * The key ins is changed to record the hole:
  6533. * ins->objectid == start position
  6534. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  6535. * ins->offset == the size of the hole.
  6536. * Any available blocks before search_start are skipped.
  6537. *
  6538. * If there is no suitable free space, we will record the max size of
  6539. * the free space extent currently.
  6540. */
  6541. static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
  6542. u64 ram_bytes, u64 num_bytes, u64 empty_size,
  6543. u64 hint_byte, struct btrfs_key *ins,
  6544. u64 flags, int delalloc)
  6545. {
  6546. int ret = 0;
  6547. struct btrfs_root *root = fs_info->extent_root;
  6548. struct btrfs_free_cluster *last_ptr = NULL;
  6549. struct btrfs_block_group_cache *block_group = NULL;
  6550. u64 search_start = 0;
  6551. u64 max_extent_size = 0;
  6552. u64 empty_cluster = 0;
  6553. struct btrfs_space_info *space_info;
  6554. int loop = 0;
  6555. int index = btrfs_bg_flags_to_raid_index(flags);
  6556. bool failed_cluster_refill = false;
  6557. bool failed_alloc = false;
  6558. bool use_cluster = true;
  6559. bool have_caching_bg = false;
  6560. bool orig_have_caching_bg = false;
  6561. bool full_search = false;
  6562. WARN_ON(num_bytes < fs_info->sectorsize);
  6563. ins->type = BTRFS_EXTENT_ITEM_KEY;
  6564. ins->objectid = 0;
  6565. ins->offset = 0;
  6566. trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
  6567. space_info = __find_space_info(fs_info, flags);
  6568. if (!space_info) {
  6569. btrfs_err(fs_info, "No space info for %llu", flags);
  6570. return -ENOSPC;
  6571. }
  6572. /*
  6573. * If our free space is heavily fragmented we may not be able to make
  6574. * big contiguous allocations, so instead of doing the expensive search
  6575. * for free space, simply return ENOSPC with our max_extent_size so we
  6576. * can go ahead and search for a more manageable chunk.
  6577. *
  6578. * If our max_extent_size is large enough for our allocation simply
  6579. * disable clustering since we will likely not be able to find enough
  6580. * space to create a cluster and induce latency trying.
  6581. */
  6582. if (unlikely(space_info->max_extent_size)) {
  6583. spin_lock(&space_info->lock);
  6584. if (space_info->max_extent_size &&
  6585. num_bytes > space_info->max_extent_size) {
  6586. ins->offset = space_info->max_extent_size;
  6587. spin_unlock(&space_info->lock);
  6588. return -ENOSPC;
  6589. } else if (space_info->max_extent_size) {
  6590. use_cluster = false;
  6591. }
  6592. spin_unlock(&space_info->lock);
  6593. }
  6594. last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
  6595. if (last_ptr) {
  6596. spin_lock(&last_ptr->lock);
  6597. if (last_ptr->block_group)
  6598. hint_byte = last_ptr->window_start;
  6599. if (last_ptr->fragmented) {
  6600. /*
  6601. * We still set window_start so we can keep track of the
  6602. * last place we found an allocation to try and save
  6603. * some time.
  6604. */
  6605. hint_byte = last_ptr->window_start;
  6606. use_cluster = false;
  6607. }
  6608. spin_unlock(&last_ptr->lock);
  6609. }
  6610. search_start = max(search_start, first_logical_byte(fs_info, 0));
  6611. search_start = max(search_start, hint_byte);
  6612. if (search_start == hint_byte) {
  6613. block_group = btrfs_lookup_block_group(fs_info, search_start);
  6614. /*
  6615. * we don't want to use the block group if it doesn't match our
  6616. * allocation bits, or if its not cached.
  6617. *
  6618. * However if we are re-searching with an ideal block group
  6619. * picked out then we don't care that the block group is cached.
  6620. */
  6621. if (block_group && block_group_bits(block_group, flags) &&
  6622. block_group->cached != BTRFS_CACHE_NO) {
  6623. down_read(&space_info->groups_sem);
  6624. if (list_empty(&block_group->list) ||
  6625. block_group->ro) {
  6626. /*
  6627. * someone is removing this block group,
  6628. * we can't jump into the have_block_group
  6629. * target because our list pointers are not
  6630. * valid
  6631. */
  6632. btrfs_put_block_group(block_group);
  6633. up_read(&space_info->groups_sem);
  6634. } else {
  6635. index = btrfs_bg_flags_to_raid_index(
  6636. block_group->flags);
  6637. btrfs_lock_block_group(block_group, delalloc);
  6638. goto have_block_group;
  6639. }
  6640. } else if (block_group) {
  6641. btrfs_put_block_group(block_group);
  6642. }
  6643. }
  6644. search:
  6645. have_caching_bg = false;
  6646. if (index == 0 || index == btrfs_bg_flags_to_raid_index(flags))
  6647. full_search = true;
  6648. down_read(&space_info->groups_sem);
  6649. list_for_each_entry(block_group, &space_info->block_groups[index],
  6650. list) {
  6651. u64 offset;
  6652. int cached;
  6653. /* If the block group is read-only, we can skip it entirely. */
  6654. if (unlikely(block_group->ro))
  6655. continue;
  6656. btrfs_grab_block_group(block_group, delalloc);
  6657. search_start = block_group->key.objectid;
  6658. /*
  6659. * this can happen if we end up cycling through all the
  6660. * raid types, but we want to make sure we only allocate
  6661. * for the proper type.
  6662. */
  6663. if (!block_group_bits(block_group, flags)) {
  6664. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  6665. BTRFS_BLOCK_GROUP_RAID1 |
  6666. BTRFS_BLOCK_GROUP_RAID5 |
  6667. BTRFS_BLOCK_GROUP_RAID6 |
  6668. BTRFS_BLOCK_GROUP_RAID10;
  6669. /*
  6670. * if they asked for extra copies and this block group
  6671. * doesn't provide them, bail. This does allow us to
  6672. * fill raid0 from raid1.
  6673. */
  6674. if ((flags & extra) && !(block_group->flags & extra))
  6675. goto loop;
  6676. }
  6677. have_block_group:
  6678. cached = block_group_cache_done(block_group);
  6679. if (unlikely(!cached)) {
  6680. have_caching_bg = true;
  6681. ret = cache_block_group(block_group, 0);
  6682. BUG_ON(ret < 0);
  6683. ret = 0;
  6684. }
  6685. if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
  6686. goto loop;
  6687. /*
  6688. * Ok we want to try and use the cluster allocator, so
  6689. * lets look there
  6690. */
  6691. if (last_ptr && use_cluster) {
  6692. struct btrfs_block_group_cache *used_block_group;
  6693. unsigned long aligned_cluster;
  6694. /*
  6695. * the refill lock keeps out other
  6696. * people trying to start a new cluster
  6697. */
  6698. used_block_group = btrfs_lock_cluster(block_group,
  6699. last_ptr,
  6700. delalloc);
  6701. if (!used_block_group)
  6702. goto refill_cluster;
  6703. if (used_block_group != block_group &&
  6704. (used_block_group->ro ||
  6705. !block_group_bits(used_block_group, flags)))
  6706. goto release_cluster;
  6707. offset = btrfs_alloc_from_cluster(used_block_group,
  6708. last_ptr,
  6709. num_bytes,
  6710. used_block_group->key.objectid,
  6711. &max_extent_size);
  6712. if (offset) {
  6713. /* we have a block, we're done */
  6714. spin_unlock(&last_ptr->refill_lock);
  6715. trace_btrfs_reserve_extent_cluster(
  6716. used_block_group,
  6717. search_start, num_bytes);
  6718. if (used_block_group != block_group) {
  6719. btrfs_release_block_group(block_group,
  6720. delalloc);
  6721. block_group = used_block_group;
  6722. }
  6723. goto checks;
  6724. }
  6725. WARN_ON(last_ptr->block_group != used_block_group);
  6726. release_cluster:
  6727. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  6728. * set up a new clusters, so lets just skip it
  6729. * and let the allocator find whatever block
  6730. * it can find. If we reach this point, we
  6731. * will have tried the cluster allocator
  6732. * plenty of times and not have found
  6733. * anything, so we are likely way too
  6734. * fragmented for the clustering stuff to find
  6735. * anything.
  6736. *
  6737. * However, if the cluster is taken from the
  6738. * current block group, release the cluster
  6739. * first, so that we stand a better chance of
  6740. * succeeding in the unclustered
  6741. * allocation. */
  6742. if (loop >= LOOP_NO_EMPTY_SIZE &&
  6743. used_block_group != block_group) {
  6744. spin_unlock(&last_ptr->refill_lock);
  6745. btrfs_release_block_group(used_block_group,
  6746. delalloc);
  6747. goto unclustered_alloc;
  6748. }
  6749. /*
  6750. * this cluster didn't work out, free it and
  6751. * start over
  6752. */
  6753. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  6754. if (used_block_group != block_group)
  6755. btrfs_release_block_group(used_block_group,
  6756. delalloc);
  6757. refill_cluster:
  6758. if (loop >= LOOP_NO_EMPTY_SIZE) {
  6759. spin_unlock(&last_ptr->refill_lock);
  6760. goto unclustered_alloc;
  6761. }
  6762. aligned_cluster = max_t(unsigned long,
  6763. empty_cluster + empty_size,
  6764. block_group->full_stripe_len);
  6765. /* allocate a cluster in this block group */
  6766. ret = btrfs_find_space_cluster(fs_info, block_group,
  6767. last_ptr, search_start,
  6768. num_bytes,
  6769. aligned_cluster);
  6770. if (ret == 0) {
  6771. /*
  6772. * now pull our allocation out of this
  6773. * cluster
  6774. */
  6775. offset = btrfs_alloc_from_cluster(block_group,
  6776. last_ptr,
  6777. num_bytes,
  6778. search_start,
  6779. &max_extent_size);
  6780. if (offset) {
  6781. /* we found one, proceed */
  6782. spin_unlock(&last_ptr->refill_lock);
  6783. trace_btrfs_reserve_extent_cluster(
  6784. block_group, search_start,
  6785. num_bytes);
  6786. goto checks;
  6787. }
  6788. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  6789. && !failed_cluster_refill) {
  6790. spin_unlock(&last_ptr->refill_lock);
  6791. failed_cluster_refill = true;
  6792. wait_block_group_cache_progress(block_group,
  6793. num_bytes + empty_cluster + empty_size);
  6794. goto have_block_group;
  6795. }
  6796. /*
  6797. * at this point we either didn't find a cluster
  6798. * or we weren't able to allocate a block from our
  6799. * cluster. Free the cluster we've been trying
  6800. * to use, and go to the next block group
  6801. */
  6802. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  6803. spin_unlock(&last_ptr->refill_lock);
  6804. goto loop;
  6805. }
  6806. unclustered_alloc:
  6807. /*
  6808. * We are doing an unclustered alloc, set the fragmented flag so
  6809. * we don't bother trying to setup a cluster again until we get
  6810. * more space.
  6811. */
  6812. if (unlikely(last_ptr)) {
  6813. spin_lock(&last_ptr->lock);
  6814. last_ptr->fragmented = 1;
  6815. spin_unlock(&last_ptr->lock);
  6816. }
  6817. if (cached) {
  6818. struct btrfs_free_space_ctl *ctl =
  6819. block_group->free_space_ctl;
  6820. spin_lock(&ctl->tree_lock);
  6821. if (ctl->free_space <
  6822. num_bytes + empty_cluster + empty_size) {
  6823. if (ctl->free_space > max_extent_size)
  6824. max_extent_size = ctl->free_space;
  6825. spin_unlock(&ctl->tree_lock);
  6826. goto loop;
  6827. }
  6828. spin_unlock(&ctl->tree_lock);
  6829. }
  6830. offset = btrfs_find_space_for_alloc(block_group, search_start,
  6831. num_bytes, empty_size,
  6832. &max_extent_size);
  6833. /*
  6834. * If we didn't find a chunk, and we haven't failed on this
  6835. * block group before, and this block group is in the middle of
  6836. * caching and we are ok with waiting, then go ahead and wait
  6837. * for progress to be made, and set failed_alloc to true.
  6838. *
  6839. * If failed_alloc is true then we've already waited on this
  6840. * block group once and should move on to the next block group.
  6841. */
  6842. if (!offset && !failed_alloc && !cached &&
  6843. loop > LOOP_CACHING_NOWAIT) {
  6844. wait_block_group_cache_progress(block_group,
  6845. num_bytes + empty_size);
  6846. failed_alloc = true;
  6847. goto have_block_group;
  6848. } else if (!offset) {
  6849. goto loop;
  6850. }
  6851. checks:
  6852. search_start = ALIGN(offset, fs_info->stripesize);
  6853. /* move on to the next group */
  6854. if (search_start + num_bytes >
  6855. block_group->key.objectid + block_group->key.offset) {
  6856. btrfs_add_free_space(block_group, offset, num_bytes);
  6857. goto loop;
  6858. }
  6859. if (offset < search_start)
  6860. btrfs_add_free_space(block_group, offset,
  6861. search_start - offset);
  6862. BUG_ON(offset > search_start);
  6863. ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
  6864. num_bytes, delalloc);
  6865. if (ret == -EAGAIN) {
  6866. btrfs_add_free_space(block_group, offset, num_bytes);
  6867. goto loop;
  6868. }
  6869. btrfs_inc_block_group_reservations(block_group);
  6870. /* we are all good, lets return */
  6871. ins->objectid = search_start;
  6872. ins->offset = num_bytes;
  6873. trace_btrfs_reserve_extent(block_group, search_start, num_bytes);
  6874. btrfs_release_block_group(block_group, delalloc);
  6875. break;
  6876. loop:
  6877. failed_cluster_refill = false;
  6878. failed_alloc = false;
  6879. BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
  6880. index);
  6881. btrfs_release_block_group(block_group, delalloc);
  6882. cond_resched();
  6883. }
  6884. up_read(&space_info->groups_sem);
  6885. if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
  6886. && !orig_have_caching_bg)
  6887. orig_have_caching_bg = true;
  6888. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  6889. goto search;
  6890. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  6891. goto search;
  6892. /*
  6893. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  6894. * caching kthreads as we move along
  6895. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  6896. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  6897. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  6898. * again
  6899. */
  6900. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  6901. index = 0;
  6902. if (loop == LOOP_CACHING_NOWAIT) {
  6903. /*
  6904. * We want to skip the LOOP_CACHING_WAIT step if we
  6905. * don't have any uncached bgs and we've already done a
  6906. * full search through.
  6907. */
  6908. if (orig_have_caching_bg || !full_search)
  6909. loop = LOOP_CACHING_WAIT;
  6910. else
  6911. loop = LOOP_ALLOC_CHUNK;
  6912. } else {
  6913. loop++;
  6914. }
  6915. if (loop == LOOP_ALLOC_CHUNK) {
  6916. struct btrfs_trans_handle *trans;
  6917. int exist = 0;
  6918. trans = current->journal_info;
  6919. if (trans)
  6920. exist = 1;
  6921. else
  6922. trans = btrfs_join_transaction(root);
  6923. if (IS_ERR(trans)) {
  6924. ret = PTR_ERR(trans);
  6925. goto out;
  6926. }
  6927. ret = do_chunk_alloc(trans, fs_info, flags,
  6928. CHUNK_ALLOC_FORCE);
  6929. /*
  6930. * If we can't allocate a new chunk we've already looped
  6931. * through at least once, move on to the NO_EMPTY_SIZE
  6932. * case.
  6933. */
  6934. if (ret == -ENOSPC)
  6935. loop = LOOP_NO_EMPTY_SIZE;
  6936. /*
  6937. * Do not bail out on ENOSPC since we
  6938. * can do more things.
  6939. */
  6940. if (ret < 0 && ret != -ENOSPC)
  6941. btrfs_abort_transaction(trans, ret);
  6942. else
  6943. ret = 0;
  6944. if (!exist)
  6945. btrfs_end_transaction(trans);
  6946. if (ret)
  6947. goto out;
  6948. }
  6949. if (loop == LOOP_NO_EMPTY_SIZE) {
  6950. /*
  6951. * Don't loop again if we already have no empty_size and
  6952. * no empty_cluster.
  6953. */
  6954. if (empty_size == 0 &&
  6955. empty_cluster == 0) {
  6956. ret = -ENOSPC;
  6957. goto out;
  6958. }
  6959. empty_size = 0;
  6960. empty_cluster = 0;
  6961. }
  6962. goto search;
  6963. } else if (!ins->objectid) {
  6964. ret = -ENOSPC;
  6965. } else if (ins->objectid) {
  6966. if (!use_cluster && last_ptr) {
  6967. spin_lock(&last_ptr->lock);
  6968. last_ptr->window_start = ins->objectid;
  6969. spin_unlock(&last_ptr->lock);
  6970. }
  6971. ret = 0;
  6972. }
  6973. out:
  6974. if (ret == -ENOSPC) {
  6975. spin_lock(&space_info->lock);
  6976. space_info->max_extent_size = max_extent_size;
  6977. spin_unlock(&space_info->lock);
  6978. ins->offset = max_extent_size;
  6979. }
  6980. return ret;
  6981. }
  6982. static void dump_space_info(struct btrfs_fs_info *fs_info,
  6983. struct btrfs_space_info *info, u64 bytes,
  6984. int dump_block_groups)
  6985. {
  6986. struct btrfs_block_group_cache *cache;
  6987. int index = 0;
  6988. spin_lock(&info->lock);
  6989. btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull",
  6990. info->flags,
  6991. info->total_bytes - btrfs_space_info_used(info, true),
  6992. info->full ? "" : "not ");
  6993. btrfs_info(fs_info,
  6994. "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
  6995. info->total_bytes, info->bytes_used, info->bytes_pinned,
  6996. info->bytes_reserved, info->bytes_may_use,
  6997. info->bytes_readonly);
  6998. spin_unlock(&info->lock);
  6999. if (!dump_block_groups)
  7000. return;
  7001. down_read(&info->groups_sem);
  7002. again:
  7003. list_for_each_entry(cache, &info->block_groups[index], list) {
  7004. spin_lock(&cache->lock);
  7005. btrfs_info(fs_info,
  7006. "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
  7007. cache->key.objectid, cache->key.offset,
  7008. btrfs_block_group_used(&cache->item), cache->pinned,
  7009. cache->reserved, cache->ro ? "[readonly]" : "");
  7010. btrfs_dump_free_space(cache, bytes);
  7011. spin_unlock(&cache->lock);
  7012. }
  7013. if (++index < BTRFS_NR_RAID_TYPES)
  7014. goto again;
  7015. up_read(&info->groups_sem);
  7016. }
  7017. /*
  7018. * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
  7019. * hole that is at least as big as @num_bytes.
  7020. *
  7021. * @root - The root that will contain this extent
  7022. *
  7023. * @ram_bytes - The amount of space in ram that @num_bytes take. This
  7024. * is used for accounting purposes. This value differs
  7025. * from @num_bytes only in the case of compressed extents.
  7026. *
  7027. * @num_bytes - Number of bytes to allocate on-disk.
  7028. *
  7029. * @min_alloc_size - Indicates the minimum amount of space that the
  7030. * allocator should try to satisfy. In some cases
  7031. * @num_bytes may be larger than what is required and if
  7032. * the filesystem is fragmented then allocation fails.
  7033. * However, the presence of @min_alloc_size gives a
  7034. * chance to try and satisfy the smaller allocation.
  7035. *
  7036. * @empty_size - A hint that you plan on doing more COW. This is the
  7037. * size in bytes the allocator should try to find free
  7038. * next to the block it returns. This is just a hint and
  7039. * may be ignored by the allocator.
  7040. *
  7041. * @hint_byte - Hint to the allocator to start searching above the byte
  7042. * address passed. It might be ignored.
  7043. *
  7044. * @ins - This key is modified to record the found hole. It will
  7045. * have the following values:
  7046. * ins->objectid == start position
  7047. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  7048. * ins->offset == the size of the hole.
  7049. *
  7050. * @is_data - Boolean flag indicating whether an extent is
  7051. * allocated for data (true) or metadata (false)
  7052. *
  7053. * @delalloc - Boolean flag indicating whether this allocation is for
  7054. * delalloc or not. If 'true' data_rwsem of block groups
  7055. * is going to be acquired.
  7056. *
  7057. *
  7058. * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
  7059. * case -ENOSPC is returned then @ins->offset will contain the size of the
  7060. * largest available hole the allocator managed to find.
  7061. */
  7062. int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
  7063. u64 num_bytes, u64 min_alloc_size,
  7064. u64 empty_size, u64 hint_byte,
  7065. struct btrfs_key *ins, int is_data, int delalloc)
  7066. {
  7067. struct btrfs_fs_info *fs_info = root->fs_info;
  7068. bool final_tried = num_bytes == min_alloc_size;
  7069. u64 flags;
  7070. int ret;
  7071. flags = get_alloc_profile_by_root(root, is_data);
  7072. again:
  7073. WARN_ON(num_bytes < fs_info->sectorsize);
  7074. ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
  7075. hint_byte, ins, flags, delalloc);
  7076. if (!ret && !is_data) {
  7077. btrfs_dec_block_group_reservations(fs_info, ins->objectid);
  7078. } else if (ret == -ENOSPC) {
  7079. if (!final_tried && ins->offset) {
  7080. num_bytes = min(num_bytes >> 1, ins->offset);
  7081. num_bytes = round_down(num_bytes,
  7082. fs_info->sectorsize);
  7083. num_bytes = max(num_bytes, min_alloc_size);
  7084. ram_bytes = num_bytes;
  7085. if (num_bytes == min_alloc_size)
  7086. final_tried = true;
  7087. goto again;
  7088. } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
  7089. struct btrfs_space_info *sinfo;
  7090. sinfo = __find_space_info(fs_info, flags);
  7091. btrfs_err(fs_info,
  7092. "allocation failed flags %llu, wanted %llu",
  7093. flags, num_bytes);
  7094. if (sinfo)
  7095. dump_space_info(fs_info, sinfo, num_bytes, 1);
  7096. }
  7097. }
  7098. return ret;
  7099. }
  7100. static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
  7101. u64 start, u64 len,
  7102. int pin, int delalloc)
  7103. {
  7104. struct btrfs_block_group_cache *cache;
  7105. int ret = 0;
  7106. cache = btrfs_lookup_block_group(fs_info, start);
  7107. if (!cache) {
  7108. btrfs_err(fs_info, "Unable to find block group for %llu",
  7109. start);
  7110. return -ENOSPC;
  7111. }
  7112. if (pin)
  7113. pin_down_extent(fs_info, cache, start, len, 1);
  7114. else {
  7115. if (btrfs_test_opt(fs_info, DISCARD))
  7116. ret = btrfs_discard_extent(fs_info, start, len, NULL);
  7117. btrfs_add_free_space(cache, start, len);
  7118. btrfs_free_reserved_bytes(cache, len, delalloc);
  7119. trace_btrfs_reserved_extent_free(fs_info, start, len);
  7120. }
  7121. btrfs_put_block_group(cache);
  7122. return ret;
  7123. }
  7124. int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
  7125. u64 start, u64 len, int delalloc)
  7126. {
  7127. return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
  7128. }
  7129. int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
  7130. u64 start, u64 len)
  7131. {
  7132. return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
  7133. }
  7134. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  7135. u64 parent, u64 root_objectid,
  7136. u64 flags, u64 owner, u64 offset,
  7137. struct btrfs_key *ins, int ref_mod)
  7138. {
  7139. struct btrfs_fs_info *fs_info = trans->fs_info;
  7140. int ret;
  7141. struct btrfs_extent_item *extent_item;
  7142. struct btrfs_extent_inline_ref *iref;
  7143. struct btrfs_path *path;
  7144. struct extent_buffer *leaf;
  7145. int type;
  7146. u32 size;
  7147. if (parent > 0)
  7148. type = BTRFS_SHARED_DATA_REF_KEY;
  7149. else
  7150. type = BTRFS_EXTENT_DATA_REF_KEY;
  7151. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  7152. path = btrfs_alloc_path();
  7153. if (!path)
  7154. return -ENOMEM;
  7155. path->leave_spinning = 1;
  7156. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  7157. ins, size);
  7158. if (ret) {
  7159. btrfs_free_path(path);
  7160. return ret;
  7161. }
  7162. leaf = path->nodes[0];
  7163. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  7164. struct btrfs_extent_item);
  7165. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  7166. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  7167. btrfs_set_extent_flags(leaf, extent_item,
  7168. flags | BTRFS_EXTENT_FLAG_DATA);
  7169. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  7170. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  7171. if (parent > 0) {
  7172. struct btrfs_shared_data_ref *ref;
  7173. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  7174. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  7175. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  7176. } else {
  7177. struct btrfs_extent_data_ref *ref;
  7178. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  7179. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  7180. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  7181. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  7182. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  7183. }
  7184. btrfs_mark_buffer_dirty(path->nodes[0]);
  7185. btrfs_free_path(path);
  7186. ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
  7187. if (ret)
  7188. return ret;
  7189. ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
  7190. if (ret) { /* -ENOENT, logic error */
  7191. btrfs_err(fs_info, "update block group failed for %llu %llu",
  7192. ins->objectid, ins->offset);
  7193. BUG();
  7194. }
  7195. trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
  7196. return ret;
  7197. }
  7198. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  7199. struct btrfs_delayed_ref_node *node,
  7200. struct btrfs_delayed_extent_op *extent_op)
  7201. {
  7202. struct btrfs_fs_info *fs_info = trans->fs_info;
  7203. int ret;
  7204. struct btrfs_extent_item *extent_item;
  7205. struct btrfs_key extent_key;
  7206. struct btrfs_tree_block_info *block_info;
  7207. struct btrfs_extent_inline_ref *iref;
  7208. struct btrfs_path *path;
  7209. struct extent_buffer *leaf;
  7210. struct btrfs_delayed_tree_ref *ref;
  7211. u32 size = sizeof(*extent_item) + sizeof(*iref);
  7212. u64 num_bytes;
  7213. u64 flags = extent_op->flags_to_set;
  7214. bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
  7215. ref = btrfs_delayed_node_to_tree_ref(node);
  7216. extent_key.objectid = node->bytenr;
  7217. if (skinny_metadata) {
  7218. extent_key.offset = ref->level;
  7219. extent_key.type = BTRFS_METADATA_ITEM_KEY;
  7220. num_bytes = fs_info->nodesize;
  7221. } else {
  7222. extent_key.offset = node->num_bytes;
  7223. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  7224. size += sizeof(*block_info);
  7225. num_bytes = node->num_bytes;
  7226. }
  7227. path = btrfs_alloc_path();
  7228. if (!path) {
  7229. btrfs_free_and_pin_reserved_extent(fs_info,
  7230. extent_key.objectid,
  7231. fs_info->nodesize);
  7232. return -ENOMEM;
  7233. }
  7234. path->leave_spinning = 1;
  7235. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  7236. &extent_key, size);
  7237. if (ret) {
  7238. btrfs_free_path(path);
  7239. btrfs_free_and_pin_reserved_extent(fs_info,
  7240. extent_key.objectid,
  7241. fs_info->nodesize);
  7242. return ret;
  7243. }
  7244. leaf = path->nodes[0];
  7245. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  7246. struct btrfs_extent_item);
  7247. btrfs_set_extent_refs(leaf, extent_item, 1);
  7248. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  7249. btrfs_set_extent_flags(leaf, extent_item,
  7250. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  7251. if (skinny_metadata) {
  7252. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  7253. } else {
  7254. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  7255. btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
  7256. btrfs_set_tree_block_level(leaf, block_info, ref->level);
  7257. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  7258. }
  7259. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
  7260. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  7261. btrfs_set_extent_inline_ref_type(leaf, iref,
  7262. BTRFS_SHARED_BLOCK_REF_KEY);
  7263. btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
  7264. } else {
  7265. btrfs_set_extent_inline_ref_type(leaf, iref,
  7266. BTRFS_TREE_BLOCK_REF_KEY);
  7267. btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
  7268. }
  7269. btrfs_mark_buffer_dirty(leaf);
  7270. btrfs_free_path(path);
  7271. ret = remove_from_free_space_tree(trans, extent_key.objectid,
  7272. num_bytes);
  7273. if (ret)
  7274. return ret;
  7275. ret = update_block_group(trans, fs_info, extent_key.objectid,
  7276. fs_info->nodesize, 1);
  7277. if (ret) { /* -ENOENT, logic error */
  7278. btrfs_err(fs_info, "update block group failed for %llu %llu",
  7279. extent_key.objectid, extent_key.offset);
  7280. BUG();
  7281. }
  7282. trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
  7283. fs_info->nodesize);
  7284. return ret;
  7285. }
  7286. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  7287. struct btrfs_root *root, u64 owner,
  7288. u64 offset, u64 ram_bytes,
  7289. struct btrfs_key *ins)
  7290. {
  7291. int ret;
  7292. BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
  7293. btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
  7294. root->root_key.objectid, owner, offset,
  7295. BTRFS_ADD_DELAYED_EXTENT);
  7296. ret = btrfs_add_delayed_data_ref(trans, ins->objectid,
  7297. ins->offset, 0,
  7298. root->root_key.objectid, owner,
  7299. offset, ram_bytes,
  7300. BTRFS_ADD_DELAYED_EXTENT, NULL, NULL);
  7301. return ret;
  7302. }
  7303. /*
  7304. * this is used by the tree logging recovery code. It records that
  7305. * an extent has been allocated and makes sure to clear the free
  7306. * space cache bits as well
  7307. */
  7308. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  7309. struct btrfs_fs_info *fs_info,
  7310. u64 root_objectid, u64 owner, u64 offset,
  7311. struct btrfs_key *ins)
  7312. {
  7313. int ret;
  7314. struct btrfs_block_group_cache *block_group;
  7315. struct btrfs_space_info *space_info;
  7316. /*
  7317. * Mixed block groups will exclude before processing the log so we only
  7318. * need to do the exclude dance if this fs isn't mixed.
  7319. */
  7320. if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
  7321. ret = __exclude_logged_extent(fs_info, ins->objectid,
  7322. ins->offset);
  7323. if (ret)
  7324. return ret;
  7325. }
  7326. block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
  7327. if (!block_group)
  7328. return -EINVAL;
  7329. space_info = block_group->space_info;
  7330. spin_lock(&space_info->lock);
  7331. spin_lock(&block_group->lock);
  7332. space_info->bytes_reserved += ins->offset;
  7333. block_group->reserved += ins->offset;
  7334. spin_unlock(&block_group->lock);
  7335. spin_unlock(&space_info->lock);
  7336. ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
  7337. offset, ins, 1);
  7338. btrfs_put_block_group(block_group);
  7339. return ret;
  7340. }
  7341. static struct extent_buffer *
  7342. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  7343. u64 bytenr, int level)
  7344. {
  7345. struct btrfs_fs_info *fs_info = root->fs_info;
  7346. struct extent_buffer *buf;
  7347. buf = btrfs_find_create_tree_block(fs_info, bytenr);
  7348. if (IS_ERR(buf))
  7349. return buf;
  7350. btrfs_set_header_generation(buf, trans->transid);
  7351. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  7352. btrfs_tree_lock(buf);
  7353. clean_tree_block(fs_info, buf);
  7354. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  7355. btrfs_set_lock_blocking(buf);
  7356. set_extent_buffer_uptodate(buf);
  7357. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  7358. buf->log_index = root->log_transid % 2;
  7359. /*
  7360. * we allow two log transactions at a time, use different
  7361. * EXENT bit to differentiate dirty pages.
  7362. */
  7363. if (buf->log_index == 0)
  7364. set_extent_dirty(&root->dirty_log_pages, buf->start,
  7365. buf->start + buf->len - 1, GFP_NOFS);
  7366. else
  7367. set_extent_new(&root->dirty_log_pages, buf->start,
  7368. buf->start + buf->len - 1);
  7369. } else {
  7370. buf->log_index = -1;
  7371. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  7372. buf->start + buf->len - 1, GFP_NOFS);
  7373. }
  7374. trans->dirty = true;
  7375. /* this returns a buffer locked for blocking */
  7376. return buf;
  7377. }
  7378. static struct btrfs_block_rsv *
  7379. use_block_rsv(struct btrfs_trans_handle *trans,
  7380. struct btrfs_root *root, u32 blocksize)
  7381. {
  7382. struct btrfs_fs_info *fs_info = root->fs_info;
  7383. struct btrfs_block_rsv *block_rsv;
  7384. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  7385. int ret;
  7386. bool global_updated = false;
  7387. block_rsv = get_block_rsv(trans, root);
  7388. if (unlikely(block_rsv->size == 0))
  7389. goto try_reserve;
  7390. again:
  7391. ret = block_rsv_use_bytes(block_rsv, blocksize);
  7392. if (!ret)
  7393. return block_rsv;
  7394. if (block_rsv->failfast)
  7395. return ERR_PTR(ret);
  7396. if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
  7397. global_updated = true;
  7398. update_global_block_rsv(fs_info);
  7399. goto again;
  7400. }
  7401. if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
  7402. static DEFINE_RATELIMIT_STATE(_rs,
  7403. DEFAULT_RATELIMIT_INTERVAL * 10,
  7404. /*DEFAULT_RATELIMIT_BURST*/ 1);
  7405. if (__ratelimit(&_rs))
  7406. WARN(1, KERN_DEBUG
  7407. "BTRFS: block rsv returned %d\n", ret);
  7408. }
  7409. try_reserve:
  7410. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  7411. BTRFS_RESERVE_NO_FLUSH);
  7412. if (!ret)
  7413. return block_rsv;
  7414. /*
  7415. * If we couldn't reserve metadata bytes try and use some from
  7416. * the global reserve if its space type is the same as the global
  7417. * reservation.
  7418. */
  7419. if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
  7420. block_rsv->space_info == global_rsv->space_info) {
  7421. ret = block_rsv_use_bytes(global_rsv, blocksize);
  7422. if (!ret)
  7423. return global_rsv;
  7424. }
  7425. return ERR_PTR(ret);
  7426. }
  7427. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  7428. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  7429. {
  7430. block_rsv_add_bytes(block_rsv, blocksize, 0);
  7431. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0, NULL);
  7432. }
  7433. /*
  7434. * finds a free extent and does all the dirty work required for allocation
  7435. * returns the tree buffer or an ERR_PTR on error.
  7436. */
  7437. struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
  7438. struct btrfs_root *root,
  7439. u64 parent, u64 root_objectid,
  7440. const struct btrfs_disk_key *key,
  7441. int level, u64 hint,
  7442. u64 empty_size)
  7443. {
  7444. struct btrfs_fs_info *fs_info = root->fs_info;
  7445. struct btrfs_key ins;
  7446. struct btrfs_block_rsv *block_rsv;
  7447. struct extent_buffer *buf;
  7448. struct btrfs_delayed_extent_op *extent_op;
  7449. u64 flags = 0;
  7450. int ret;
  7451. u32 blocksize = fs_info->nodesize;
  7452. bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
  7453. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  7454. if (btrfs_is_testing(fs_info)) {
  7455. buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
  7456. level);
  7457. if (!IS_ERR(buf))
  7458. root->alloc_bytenr += blocksize;
  7459. return buf;
  7460. }
  7461. #endif
  7462. block_rsv = use_block_rsv(trans, root, blocksize);
  7463. if (IS_ERR(block_rsv))
  7464. return ERR_CAST(block_rsv);
  7465. ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
  7466. empty_size, hint, &ins, 0, 0);
  7467. if (ret)
  7468. goto out_unuse;
  7469. buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
  7470. if (IS_ERR(buf)) {
  7471. ret = PTR_ERR(buf);
  7472. goto out_free_reserved;
  7473. }
  7474. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  7475. if (parent == 0)
  7476. parent = ins.objectid;
  7477. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  7478. } else
  7479. BUG_ON(parent > 0);
  7480. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  7481. extent_op = btrfs_alloc_delayed_extent_op();
  7482. if (!extent_op) {
  7483. ret = -ENOMEM;
  7484. goto out_free_buf;
  7485. }
  7486. if (key)
  7487. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  7488. else
  7489. memset(&extent_op->key, 0, sizeof(extent_op->key));
  7490. extent_op->flags_to_set = flags;
  7491. extent_op->update_key = skinny_metadata ? false : true;
  7492. extent_op->update_flags = true;
  7493. extent_op->is_data = false;
  7494. extent_op->level = level;
  7495. btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
  7496. root_objectid, level, 0,
  7497. BTRFS_ADD_DELAYED_EXTENT);
  7498. ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
  7499. ins.offset, parent,
  7500. root_objectid, level,
  7501. BTRFS_ADD_DELAYED_EXTENT,
  7502. extent_op, NULL, NULL);
  7503. if (ret)
  7504. goto out_free_delayed;
  7505. }
  7506. return buf;
  7507. out_free_delayed:
  7508. btrfs_free_delayed_extent_op(extent_op);
  7509. out_free_buf:
  7510. free_extent_buffer(buf);
  7511. out_free_reserved:
  7512. btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
  7513. out_unuse:
  7514. unuse_block_rsv(fs_info, block_rsv, blocksize);
  7515. return ERR_PTR(ret);
  7516. }
  7517. struct walk_control {
  7518. u64 refs[BTRFS_MAX_LEVEL];
  7519. u64 flags[BTRFS_MAX_LEVEL];
  7520. struct btrfs_key update_progress;
  7521. int stage;
  7522. int level;
  7523. int shared_level;
  7524. int update_ref;
  7525. int keep_locks;
  7526. int reada_slot;
  7527. int reada_count;
  7528. int for_reloc;
  7529. };
  7530. #define DROP_REFERENCE 1
  7531. #define UPDATE_BACKREF 2
  7532. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  7533. struct btrfs_root *root,
  7534. struct walk_control *wc,
  7535. struct btrfs_path *path)
  7536. {
  7537. struct btrfs_fs_info *fs_info = root->fs_info;
  7538. u64 bytenr;
  7539. u64 generation;
  7540. u64 refs;
  7541. u64 flags;
  7542. u32 nritems;
  7543. struct btrfs_key key;
  7544. struct extent_buffer *eb;
  7545. int ret;
  7546. int slot;
  7547. int nread = 0;
  7548. if (path->slots[wc->level] < wc->reada_slot) {
  7549. wc->reada_count = wc->reada_count * 2 / 3;
  7550. wc->reada_count = max(wc->reada_count, 2);
  7551. } else {
  7552. wc->reada_count = wc->reada_count * 3 / 2;
  7553. wc->reada_count = min_t(int, wc->reada_count,
  7554. BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  7555. }
  7556. eb = path->nodes[wc->level];
  7557. nritems = btrfs_header_nritems(eb);
  7558. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  7559. if (nread >= wc->reada_count)
  7560. break;
  7561. cond_resched();
  7562. bytenr = btrfs_node_blockptr(eb, slot);
  7563. generation = btrfs_node_ptr_generation(eb, slot);
  7564. if (slot == path->slots[wc->level])
  7565. goto reada;
  7566. if (wc->stage == UPDATE_BACKREF &&
  7567. generation <= root->root_key.offset)
  7568. continue;
  7569. /* We don't lock the tree block, it's OK to be racy here */
  7570. ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
  7571. wc->level - 1, 1, &refs,
  7572. &flags);
  7573. /* We don't care about errors in readahead. */
  7574. if (ret < 0)
  7575. continue;
  7576. BUG_ON(refs == 0);
  7577. if (wc->stage == DROP_REFERENCE) {
  7578. if (refs == 1)
  7579. goto reada;
  7580. if (wc->level == 1 &&
  7581. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  7582. continue;
  7583. if (!wc->update_ref ||
  7584. generation <= root->root_key.offset)
  7585. continue;
  7586. btrfs_node_key_to_cpu(eb, &key, slot);
  7587. ret = btrfs_comp_cpu_keys(&key,
  7588. &wc->update_progress);
  7589. if (ret < 0)
  7590. continue;
  7591. } else {
  7592. if (wc->level == 1 &&
  7593. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  7594. continue;
  7595. }
  7596. reada:
  7597. readahead_tree_block(fs_info, bytenr);
  7598. nread++;
  7599. }
  7600. wc->reada_slot = slot;
  7601. }
  7602. /*
  7603. * helper to process tree block while walking down the tree.
  7604. *
  7605. * when wc->stage == UPDATE_BACKREF, this function updates
  7606. * back refs for pointers in the block.
  7607. *
  7608. * NOTE: return value 1 means we should stop walking down.
  7609. */
  7610. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  7611. struct btrfs_root *root,
  7612. struct btrfs_path *path,
  7613. struct walk_control *wc, int lookup_info)
  7614. {
  7615. struct btrfs_fs_info *fs_info = root->fs_info;
  7616. int level = wc->level;
  7617. struct extent_buffer *eb = path->nodes[level];
  7618. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  7619. int ret;
  7620. if (wc->stage == UPDATE_BACKREF &&
  7621. btrfs_header_owner(eb) != root->root_key.objectid)
  7622. return 1;
  7623. /*
  7624. * when reference count of tree block is 1, it won't increase
  7625. * again. once full backref flag is set, we never clear it.
  7626. */
  7627. if (lookup_info &&
  7628. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  7629. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  7630. BUG_ON(!path->locks[level]);
  7631. ret = btrfs_lookup_extent_info(trans, fs_info,
  7632. eb->start, level, 1,
  7633. &wc->refs[level],
  7634. &wc->flags[level]);
  7635. BUG_ON(ret == -ENOMEM);
  7636. if (ret)
  7637. return ret;
  7638. BUG_ON(wc->refs[level] == 0);
  7639. }
  7640. if (wc->stage == DROP_REFERENCE) {
  7641. if (wc->refs[level] > 1)
  7642. return 1;
  7643. if (path->locks[level] && !wc->keep_locks) {
  7644. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7645. path->locks[level] = 0;
  7646. }
  7647. return 0;
  7648. }
  7649. /* wc->stage == UPDATE_BACKREF */
  7650. if (!(wc->flags[level] & flag)) {
  7651. BUG_ON(!path->locks[level]);
  7652. ret = btrfs_inc_ref(trans, root, eb, 1);
  7653. BUG_ON(ret); /* -ENOMEM */
  7654. ret = btrfs_dec_ref(trans, root, eb, 0);
  7655. BUG_ON(ret); /* -ENOMEM */
  7656. ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
  7657. eb->len, flag,
  7658. btrfs_header_level(eb), 0);
  7659. BUG_ON(ret); /* -ENOMEM */
  7660. wc->flags[level] |= flag;
  7661. }
  7662. /*
  7663. * the block is shared by multiple trees, so it's not good to
  7664. * keep the tree lock
  7665. */
  7666. if (path->locks[level] && level > 0) {
  7667. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7668. path->locks[level] = 0;
  7669. }
  7670. return 0;
  7671. }
  7672. /*
  7673. * helper to process tree block pointer.
  7674. *
  7675. * when wc->stage == DROP_REFERENCE, this function checks
  7676. * reference count of the block pointed to. if the block
  7677. * is shared and we need update back refs for the subtree
  7678. * rooted at the block, this function changes wc->stage to
  7679. * UPDATE_BACKREF. if the block is shared and there is no
  7680. * need to update back, this function drops the reference
  7681. * to the block.
  7682. *
  7683. * NOTE: return value 1 means we should stop walking down.
  7684. */
  7685. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  7686. struct btrfs_root *root,
  7687. struct btrfs_path *path,
  7688. struct walk_control *wc, int *lookup_info)
  7689. {
  7690. struct btrfs_fs_info *fs_info = root->fs_info;
  7691. u64 bytenr;
  7692. u64 generation;
  7693. u64 parent;
  7694. u32 blocksize;
  7695. struct btrfs_key key;
  7696. struct btrfs_key first_key;
  7697. struct extent_buffer *next;
  7698. int level = wc->level;
  7699. int reada = 0;
  7700. int ret = 0;
  7701. bool need_account = false;
  7702. generation = btrfs_node_ptr_generation(path->nodes[level],
  7703. path->slots[level]);
  7704. /*
  7705. * if the lower level block was created before the snapshot
  7706. * was created, we know there is no need to update back refs
  7707. * for the subtree
  7708. */
  7709. if (wc->stage == UPDATE_BACKREF &&
  7710. generation <= root->root_key.offset) {
  7711. *lookup_info = 1;
  7712. return 1;
  7713. }
  7714. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  7715. btrfs_node_key_to_cpu(path->nodes[level], &first_key,
  7716. path->slots[level]);
  7717. blocksize = fs_info->nodesize;
  7718. next = find_extent_buffer(fs_info, bytenr);
  7719. if (!next) {
  7720. next = btrfs_find_create_tree_block(fs_info, bytenr);
  7721. if (IS_ERR(next))
  7722. return PTR_ERR(next);
  7723. btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
  7724. level - 1);
  7725. reada = 1;
  7726. }
  7727. btrfs_tree_lock(next);
  7728. btrfs_set_lock_blocking(next);
  7729. ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
  7730. &wc->refs[level - 1],
  7731. &wc->flags[level - 1]);
  7732. if (ret < 0)
  7733. goto out_unlock;
  7734. if (unlikely(wc->refs[level - 1] == 0)) {
  7735. btrfs_err(fs_info, "Missing references.");
  7736. ret = -EIO;
  7737. goto out_unlock;
  7738. }
  7739. *lookup_info = 0;
  7740. if (wc->stage == DROP_REFERENCE) {
  7741. if (wc->refs[level - 1] > 1) {
  7742. need_account = true;
  7743. if (level == 1 &&
  7744. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  7745. goto skip;
  7746. if (!wc->update_ref ||
  7747. generation <= root->root_key.offset)
  7748. goto skip;
  7749. btrfs_node_key_to_cpu(path->nodes[level], &key,
  7750. path->slots[level]);
  7751. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  7752. if (ret < 0)
  7753. goto skip;
  7754. wc->stage = UPDATE_BACKREF;
  7755. wc->shared_level = level - 1;
  7756. }
  7757. } else {
  7758. if (level == 1 &&
  7759. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  7760. goto skip;
  7761. }
  7762. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  7763. btrfs_tree_unlock(next);
  7764. free_extent_buffer(next);
  7765. next = NULL;
  7766. *lookup_info = 1;
  7767. }
  7768. if (!next) {
  7769. if (reada && level == 1)
  7770. reada_walk_down(trans, root, wc, path);
  7771. next = read_tree_block(fs_info, bytenr, generation, level - 1,
  7772. &first_key);
  7773. if (IS_ERR(next)) {
  7774. return PTR_ERR(next);
  7775. } else if (!extent_buffer_uptodate(next)) {
  7776. free_extent_buffer(next);
  7777. return -EIO;
  7778. }
  7779. btrfs_tree_lock(next);
  7780. btrfs_set_lock_blocking(next);
  7781. }
  7782. level--;
  7783. ASSERT(level == btrfs_header_level(next));
  7784. if (level != btrfs_header_level(next)) {
  7785. btrfs_err(root->fs_info, "mismatched level");
  7786. ret = -EIO;
  7787. goto out_unlock;
  7788. }
  7789. path->nodes[level] = next;
  7790. path->slots[level] = 0;
  7791. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7792. wc->level = level;
  7793. if (wc->level == 1)
  7794. wc->reada_slot = 0;
  7795. return 0;
  7796. skip:
  7797. wc->refs[level - 1] = 0;
  7798. wc->flags[level - 1] = 0;
  7799. if (wc->stage == DROP_REFERENCE) {
  7800. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  7801. parent = path->nodes[level]->start;
  7802. } else {
  7803. ASSERT(root->root_key.objectid ==
  7804. btrfs_header_owner(path->nodes[level]));
  7805. if (root->root_key.objectid !=
  7806. btrfs_header_owner(path->nodes[level])) {
  7807. btrfs_err(root->fs_info,
  7808. "mismatched block owner");
  7809. ret = -EIO;
  7810. goto out_unlock;
  7811. }
  7812. parent = 0;
  7813. }
  7814. if (need_account) {
  7815. ret = btrfs_qgroup_trace_subtree(trans, root, next,
  7816. generation, level - 1);
  7817. if (ret) {
  7818. btrfs_err_rl(fs_info,
  7819. "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
  7820. ret);
  7821. }
  7822. }
  7823. ret = btrfs_free_extent(trans, root, bytenr, blocksize,
  7824. parent, root->root_key.objectid,
  7825. level - 1, 0);
  7826. if (ret)
  7827. goto out_unlock;
  7828. }
  7829. *lookup_info = 1;
  7830. ret = 1;
  7831. out_unlock:
  7832. btrfs_tree_unlock(next);
  7833. free_extent_buffer(next);
  7834. return ret;
  7835. }
  7836. /*
  7837. * helper to process tree block while walking up the tree.
  7838. *
  7839. * when wc->stage == DROP_REFERENCE, this function drops
  7840. * reference count on the block.
  7841. *
  7842. * when wc->stage == UPDATE_BACKREF, this function changes
  7843. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  7844. * to UPDATE_BACKREF previously while processing the block.
  7845. *
  7846. * NOTE: return value 1 means we should stop walking up.
  7847. */
  7848. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  7849. struct btrfs_root *root,
  7850. struct btrfs_path *path,
  7851. struct walk_control *wc)
  7852. {
  7853. struct btrfs_fs_info *fs_info = root->fs_info;
  7854. int ret;
  7855. int level = wc->level;
  7856. struct extent_buffer *eb = path->nodes[level];
  7857. u64 parent = 0;
  7858. if (wc->stage == UPDATE_BACKREF) {
  7859. BUG_ON(wc->shared_level < level);
  7860. if (level < wc->shared_level)
  7861. goto out;
  7862. ret = find_next_key(path, level + 1, &wc->update_progress);
  7863. if (ret > 0)
  7864. wc->update_ref = 0;
  7865. wc->stage = DROP_REFERENCE;
  7866. wc->shared_level = -1;
  7867. path->slots[level] = 0;
  7868. /*
  7869. * check reference count again if the block isn't locked.
  7870. * we should start walking down the tree again if reference
  7871. * count is one.
  7872. */
  7873. if (!path->locks[level]) {
  7874. BUG_ON(level == 0);
  7875. btrfs_tree_lock(eb);
  7876. btrfs_set_lock_blocking(eb);
  7877. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7878. ret = btrfs_lookup_extent_info(trans, fs_info,
  7879. eb->start, level, 1,
  7880. &wc->refs[level],
  7881. &wc->flags[level]);
  7882. if (ret < 0) {
  7883. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7884. path->locks[level] = 0;
  7885. return ret;
  7886. }
  7887. BUG_ON(wc->refs[level] == 0);
  7888. if (wc->refs[level] == 1) {
  7889. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7890. path->locks[level] = 0;
  7891. return 1;
  7892. }
  7893. }
  7894. }
  7895. /* wc->stage == DROP_REFERENCE */
  7896. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  7897. if (wc->refs[level] == 1) {
  7898. if (level == 0) {
  7899. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  7900. ret = btrfs_dec_ref(trans, root, eb, 1);
  7901. else
  7902. ret = btrfs_dec_ref(trans, root, eb, 0);
  7903. BUG_ON(ret); /* -ENOMEM */
  7904. ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
  7905. if (ret) {
  7906. btrfs_err_rl(fs_info,
  7907. "error %d accounting leaf items. Quota is out of sync, rescan required.",
  7908. ret);
  7909. }
  7910. }
  7911. /* make block locked assertion in clean_tree_block happy */
  7912. if (!path->locks[level] &&
  7913. btrfs_header_generation(eb) == trans->transid) {
  7914. btrfs_tree_lock(eb);
  7915. btrfs_set_lock_blocking(eb);
  7916. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7917. }
  7918. clean_tree_block(fs_info, eb);
  7919. }
  7920. if (eb == root->node) {
  7921. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  7922. parent = eb->start;
  7923. else
  7924. BUG_ON(root->root_key.objectid !=
  7925. btrfs_header_owner(eb));
  7926. } else {
  7927. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  7928. parent = path->nodes[level + 1]->start;
  7929. else
  7930. BUG_ON(root->root_key.objectid !=
  7931. btrfs_header_owner(path->nodes[level + 1]));
  7932. }
  7933. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  7934. out:
  7935. wc->refs[level] = 0;
  7936. wc->flags[level] = 0;
  7937. return 0;
  7938. }
  7939. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  7940. struct btrfs_root *root,
  7941. struct btrfs_path *path,
  7942. struct walk_control *wc)
  7943. {
  7944. int level = wc->level;
  7945. int lookup_info = 1;
  7946. int ret;
  7947. while (level >= 0) {
  7948. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  7949. if (ret > 0)
  7950. break;
  7951. if (level == 0)
  7952. break;
  7953. if (path->slots[level] >=
  7954. btrfs_header_nritems(path->nodes[level]))
  7955. break;
  7956. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  7957. if (ret > 0) {
  7958. path->slots[level]++;
  7959. continue;
  7960. } else if (ret < 0)
  7961. return ret;
  7962. level = wc->level;
  7963. }
  7964. return 0;
  7965. }
  7966. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  7967. struct btrfs_root *root,
  7968. struct btrfs_path *path,
  7969. struct walk_control *wc, int max_level)
  7970. {
  7971. int level = wc->level;
  7972. int ret;
  7973. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  7974. while (level < max_level && path->nodes[level]) {
  7975. wc->level = level;
  7976. if (path->slots[level] + 1 <
  7977. btrfs_header_nritems(path->nodes[level])) {
  7978. path->slots[level]++;
  7979. return 0;
  7980. } else {
  7981. ret = walk_up_proc(trans, root, path, wc);
  7982. if (ret > 0)
  7983. return 0;
  7984. if (path->locks[level]) {
  7985. btrfs_tree_unlock_rw(path->nodes[level],
  7986. path->locks[level]);
  7987. path->locks[level] = 0;
  7988. }
  7989. free_extent_buffer(path->nodes[level]);
  7990. path->nodes[level] = NULL;
  7991. level++;
  7992. }
  7993. }
  7994. return 1;
  7995. }
  7996. /*
  7997. * drop a subvolume tree.
  7998. *
  7999. * this function traverses the tree freeing any blocks that only
  8000. * referenced by the tree.
  8001. *
  8002. * when a shared tree block is found. this function decreases its
  8003. * reference count by one. if update_ref is true, this function
  8004. * also make sure backrefs for the shared block and all lower level
  8005. * blocks are properly updated.
  8006. *
  8007. * If called with for_reloc == 0, may exit early with -EAGAIN
  8008. */
  8009. int btrfs_drop_snapshot(struct btrfs_root *root,
  8010. struct btrfs_block_rsv *block_rsv, int update_ref,
  8011. int for_reloc)
  8012. {
  8013. struct btrfs_fs_info *fs_info = root->fs_info;
  8014. struct btrfs_path *path;
  8015. struct btrfs_trans_handle *trans;
  8016. struct btrfs_root *tree_root = fs_info->tree_root;
  8017. struct btrfs_root_item *root_item = &root->root_item;
  8018. struct walk_control *wc;
  8019. struct btrfs_key key;
  8020. int err = 0;
  8021. int ret;
  8022. int level;
  8023. bool root_dropped = false;
  8024. btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
  8025. path = btrfs_alloc_path();
  8026. if (!path) {
  8027. err = -ENOMEM;
  8028. goto out;
  8029. }
  8030. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  8031. if (!wc) {
  8032. btrfs_free_path(path);
  8033. err = -ENOMEM;
  8034. goto out;
  8035. }
  8036. trans = btrfs_start_transaction(tree_root, 0);
  8037. if (IS_ERR(trans)) {
  8038. err = PTR_ERR(trans);
  8039. goto out_free;
  8040. }
  8041. if (block_rsv)
  8042. trans->block_rsv = block_rsv;
  8043. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  8044. level = btrfs_header_level(root->node);
  8045. path->nodes[level] = btrfs_lock_root_node(root);
  8046. btrfs_set_lock_blocking(path->nodes[level]);
  8047. path->slots[level] = 0;
  8048. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  8049. memset(&wc->update_progress, 0,
  8050. sizeof(wc->update_progress));
  8051. } else {
  8052. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  8053. memcpy(&wc->update_progress, &key,
  8054. sizeof(wc->update_progress));
  8055. level = root_item->drop_level;
  8056. BUG_ON(level == 0);
  8057. path->lowest_level = level;
  8058. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  8059. path->lowest_level = 0;
  8060. if (ret < 0) {
  8061. err = ret;
  8062. goto out_end_trans;
  8063. }
  8064. WARN_ON(ret > 0);
  8065. /*
  8066. * unlock our path, this is safe because only this
  8067. * function is allowed to delete this snapshot
  8068. */
  8069. btrfs_unlock_up_safe(path, 0);
  8070. level = btrfs_header_level(root->node);
  8071. while (1) {
  8072. btrfs_tree_lock(path->nodes[level]);
  8073. btrfs_set_lock_blocking(path->nodes[level]);
  8074. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  8075. ret = btrfs_lookup_extent_info(trans, fs_info,
  8076. path->nodes[level]->start,
  8077. level, 1, &wc->refs[level],
  8078. &wc->flags[level]);
  8079. if (ret < 0) {
  8080. err = ret;
  8081. goto out_end_trans;
  8082. }
  8083. BUG_ON(wc->refs[level] == 0);
  8084. if (level == root_item->drop_level)
  8085. break;
  8086. btrfs_tree_unlock(path->nodes[level]);
  8087. path->locks[level] = 0;
  8088. WARN_ON(wc->refs[level] != 1);
  8089. level--;
  8090. }
  8091. }
  8092. wc->level = level;
  8093. wc->shared_level = -1;
  8094. wc->stage = DROP_REFERENCE;
  8095. wc->update_ref = update_ref;
  8096. wc->keep_locks = 0;
  8097. wc->for_reloc = for_reloc;
  8098. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
  8099. while (1) {
  8100. ret = walk_down_tree(trans, root, path, wc);
  8101. if (ret < 0) {
  8102. err = ret;
  8103. break;
  8104. }
  8105. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  8106. if (ret < 0) {
  8107. err = ret;
  8108. break;
  8109. }
  8110. if (ret > 0) {
  8111. BUG_ON(wc->stage != DROP_REFERENCE);
  8112. break;
  8113. }
  8114. if (wc->stage == DROP_REFERENCE) {
  8115. level = wc->level;
  8116. btrfs_node_key(path->nodes[level],
  8117. &root_item->drop_progress,
  8118. path->slots[level]);
  8119. root_item->drop_level = level;
  8120. }
  8121. BUG_ON(wc->level == 0);
  8122. if (btrfs_should_end_transaction(trans) ||
  8123. (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
  8124. ret = btrfs_update_root(trans, tree_root,
  8125. &root->root_key,
  8126. root_item);
  8127. if (ret) {
  8128. btrfs_abort_transaction(trans, ret);
  8129. err = ret;
  8130. goto out_end_trans;
  8131. }
  8132. btrfs_end_transaction_throttle(trans);
  8133. if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
  8134. btrfs_debug(fs_info,
  8135. "drop snapshot early exit");
  8136. err = -EAGAIN;
  8137. goto out_free;
  8138. }
  8139. trans = btrfs_start_transaction(tree_root, 0);
  8140. if (IS_ERR(trans)) {
  8141. err = PTR_ERR(trans);
  8142. goto out_free;
  8143. }
  8144. if (block_rsv)
  8145. trans->block_rsv = block_rsv;
  8146. }
  8147. }
  8148. btrfs_release_path(path);
  8149. if (err)
  8150. goto out_end_trans;
  8151. ret = btrfs_del_root(trans, fs_info, &root->root_key);
  8152. if (ret) {
  8153. btrfs_abort_transaction(trans, ret);
  8154. err = ret;
  8155. goto out_end_trans;
  8156. }
  8157. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  8158. ret = btrfs_find_root(tree_root, &root->root_key, path,
  8159. NULL, NULL);
  8160. if (ret < 0) {
  8161. btrfs_abort_transaction(trans, ret);
  8162. err = ret;
  8163. goto out_end_trans;
  8164. } else if (ret > 0) {
  8165. /* if we fail to delete the orphan item this time
  8166. * around, it'll get picked up the next time.
  8167. *
  8168. * The most common failure here is just -ENOENT.
  8169. */
  8170. btrfs_del_orphan_item(trans, tree_root,
  8171. root->root_key.objectid);
  8172. }
  8173. }
  8174. if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
  8175. btrfs_add_dropped_root(trans, root);
  8176. } else {
  8177. free_extent_buffer(root->node);
  8178. free_extent_buffer(root->commit_root);
  8179. btrfs_put_fs_root(root);
  8180. }
  8181. root_dropped = true;
  8182. out_end_trans:
  8183. btrfs_end_transaction_throttle(trans);
  8184. out_free:
  8185. kfree(wc);
  8186. btrfs_free_path(path);
  8187. out:
  8188. /*
  8189. * So if we need to stop dropping the snapshot for whatever reason we
  8190. * need to make sure to add it back to the dead root list so that we
  8191. * keep trying to do the work later. This also cleans up roots if we
  8192. * don't have it in the radix (like when we recover after a power fail
  8193. * or unmount) so we don't leak memory.
  8194. */
  8195. if (!for_reloc && !root_dropped)
  8196. btrfs_add_dead_root(root);
  8197. if (err && err != -EAGAIN)
  8198. btrfs_handle_fs_error(fs_info, err, NULL);
  8199. return err;
  8200. }
  8201. /*
  8202. * drop subtree rooted at tree block 'node'.
  8203. *
  8204. * NOTE: this function will unlock and release tree block 'node'
  8205. * only used by relocation code
  8206. */
  8207. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  8208. struct btrfs_root *root,
  8209. struct extent_buffer *node,
  8210. struct extent_buffer *parent)
  8211. {
  8212. struct btrfs_fs_info *fs_info = root->fs_info;
  8213. struct btrfs_path *path;
  8214. struct walk_control *wc;
  8215. int level;
  8216. int parent_level;
  8217. int ret = 0;
  8218. int wret;
  8219. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  8220. path = btrfs_alloc_path();
  8221. if (!path)
  8222. return -ENOMEM;
  8223. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  8224. if (!wc) {
  8225. btrfs_free_path(path);
  8226. return -ENOMEM;
  8227. }
  8228. btrfs_assert_tree_locked(parent);
  8229. parent_level = btrfs_header_level(parent);
  8230. extent_buffer_get(parent);
  8231. path->nodes[parent_level] = parent;
  8232. path->slots[parent_level] = btrfs_header_nritems(parent);
  8233. btrfs_assert_tree_locked(node);
  8234. level = btrfs_header_level(node);
  8235. path->nodes[level] = node;
  8236. path->slots[level] = 0;
  8237. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  8238. wc->refs[parent_level] = 1;
  8239. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  8240. wc->level = level;
  8241. wc->shared_level = -1;
  8242. wc->stage = DROP_REFERENCE;
  8243. wc->update_ref = 0;
  8244. wc->keep_locks = 1;
  8245. wc->for_reloc = 1;
  8246. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
  8247. while (1) {
  8248. wret = walk_down_tree(trans, root, path, wc);
  8249. if (wret < 0) {
  8250. ret = wret;
  8251. break;
  8252. }
  8253. wret = walk_up_tree(trans, root, path, wc, parent_level);
  8254. if (wret < 0)
  8255. ret = wret;
  8256. if (wret != 0)
  8257. break;
  8258. }
  8259. kfree(wc);
  8260. btrfs_free_path(path);
  8261. return ret;
  8262. }
  8263. static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
  8264. {
  8265. u64 num_devices;
  8266. u64 stripped;
  8267. /*
  8268. * if restripe for this chunk_type is on pick target profile and
  8269. * return, otherwise do the usual balance
  8270. */
  8271. stripped = get_restripe_target(fs_info, flags);
  8272. if (stripped)
  8273. return extended_to_chunk(stripped);
  8274. num_devices = fs_info->fs_devices->rw_devices;
  8275. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  8276. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  8277. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  8278. if (num_devices == 1) {
  8279. stripped |= BTRFS_BLOCK_GROUP_DUP;
  8280. stripped = flags & ~stripped;
  8281. /* turn raid0 into single device chunks */
  8282. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  8283. return stripped;
  8284. /* turn mirroring into duplication */
  8285. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  8286. BTRFS_BLOCK_GROUP_RAID10))
  8287. return stripped | BTRFS_BLOCK_GROUP_DUP;
  8288. } else {
  8289. /* they already had raid on here, just return */
  8290. if (flags & stripped)
  8291. return flags;
  8292. stripped |= BTRFS_BLOCK_GROUP_DUP;
  8293. stripped = flags & ~stripped;
  8294. /* switch duplicated blocks with raid1 */
  8295. if (flags & BTRFS_BLOCK_GROUP_DUP)
  8296. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  8297. /* this is drive concat, leave it alone */
  8298. }
  8299. return flags;
  8300. }
  8301. static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  8302. {
  8303. struct btrfs_space_info *sinfo = cache->space_info;
  8304. u64 num_bytes;
  8305. u64 min_allocable_bytes;
  8306. int ret = -ENOSPC;
  8307. /*
  8308. * We need some metadata space and system metadata space for
  8309. * allocating chunks in some corner cases until we force to set
  8310. * it to be readonly.
  8311. */
  8312. if ((sinfo->flags &
  8313. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  8314. !force)
  8315. min_allocable_bytes = SZ_1M;
  8316. else
  8317. min_allocable_bytes = 0;
  8318. spin_lock(&sinfo->lock);
  8319. spin_lock(&cache->lock);
  8320. if (cache->ro) {
  8321. cache->ro++;
  8322. ret = 0;
  8323. goto out;
  8324. }
  8325. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  8326. cache->bytes_super - btrfs_block_group_used(&cache->item);
  8327. if (btrfs_space_info_used(sinfo, true) + num_bytes +
  8328. min_allocable_bytes <= sinfo->total_bytes) {
  8329. sinfo->bytes_readonly += num_bytes;
  8330. cache->ro++;
  8331. list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
  8332. ret = 0;
  8333. }
  8334. out:
  8335. spin_unlock(&cache->lock);
  8336. spin_unlock(&sinfo->lock);
  8337. return ret;
  8338. }
  8339. int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
  8340. struct btrfs_block_group_cache *cache)
  8341. {
  8342. struct btrfs_trans_handle *trans;
  8343. u64 alloc_flags;
  8344. int ret;
  8345. again:
  8346. trans = btrfs_join_transaction(fs_info->extent_root);
  8347. if (IS_ERR(trans))
  8348. return PTR_ERR(trans);
  8349. /*
  8350. * we're not allowed to set block groups readonly after the dirty
  8351. * block groups cache has started writing. If it already started,
  8352. * back off and let this transaction commit
  8353. */
  8354. mutex_lock(&fs_info->ro_block_group_mutex);
  8355. if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
  8356. u64 transid = trans->transid;
  8357. mutex_unlock(&fs_info->ro_block_group_mutex);
  8358. btrfs_end_transaction(trans);
  8359. ret = btrfs_wait_for_commit(fs_info, transid);
  8360. if (ret)
  8361. return ret;
  8362. goto again;
  8363. }
  8364. /*
  8365. * if we are changing raid levels, try to allocate a corresponding
  8366. * block group with the new raid level.
  8367. */
  8368. alloc_flags = update_block_group_flags(fs_info, cache->flags);
  8369. if (alloc_flags != cache->flags) {
  8370. ret = do_chunk_alloc(trans, fs_info, alloc_flags,
  8371. CHUNK_ALLOC_FORCE);
  8372. /*
  8373. * ENOSPC is allowed here, we may have enough space
  8374. * already allocated at the new raid level to
  8375. * carry on
  8376. */
  8377. if (ret == -ENOSPC)
  8378. ret = 0;
  8379. if (ret < 0)
  8380. goto out;
  8381. }
  8382. ret = inc_block_group_ro(cache, 0);
  8383. if (!ret)
  8384. goto out;
  8385. alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
  8386. ret = do_chunk_alloc(trans, fs_info, alloc_flags,
  8387. CHUNK_ALLOC_FORCE);
  8388. if (ret < 0)
  8389. goto out;
  8390. ret = inc_block_group_ro(cache, 0);
  8391. out:
  8392. if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
  8393. alloc_flags = update_block_group_flags(fs_info, cache->flags);
  8394. mutex_lock(&fs_info->chunk_mutex);
  8395. check_system_chunk(trans, fs_info, alloc_flags);
  8396. mutex_unlock(&fs_info->chunk_mutex);
  8397. }
  8398. mutex_unlock(&fs_info->ro_block_group_mutex);
  8399. btrfs_end_transaction(trans);
  8400. return ret;
  8401. }
  8402. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  8403. struct btrfs_fs_info *fs_info, u64 type)
  8404. {
  8405. u64 alloc_flags = get_alloc_profile(fs_info, type);
  8406. return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
  8407. }
  8408. /*
  8409. * helper to account the unused space of all the readonly block group in the
  8410. * space_info. takes mirrors into account.
  8411. */
  8412. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  8413. {
  8414. struct btrfs_block_group_cache *block_group;
  8415. u64 free_bytes = 0;
  8416. int factor;
  8417. /* It's df, we don't care if it's racy */
  8418. if (list_empty(&sinfo->ro_bgs))
  8419. return 0;
  8420. spin_lock(&sinfo->lock);
  8421. list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
  8422. spin_lock(&block_group->lock);
  8423. if (!block_group->ro) {
  8424. spin_unlock(&block_group->lock);
  8425. continue;
  8426. }
  8427. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  8428. BTRFS_BLOCK_GROUP_RAID10 |
  8429. BTRFS_BLOCK_GROUP_DUP))
  8430. factor = 2;
  8431. else
  8432. factor = 1;
  8433. free_bytes += (block_group->key.offset -
  8434. btrfs_block_group_used(&block_group->item)) *
  8435. factor;
  8436. spin_unlock(&block_group->lock);
  8437. }
  8438. spin_unlock(&sinfo->lock);
  8439. return free_bytes;
  8440. }
  8441. void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
  8442. {
  8443. struct btrfs_space_info *sinfo = cache->space_info;
  8444. u64 num_bytes;
  8445. BUG_ON(!cache->ro);
  8446. spin_lock(&sinfo->lock);
  8447. spin_lock(&cache->lock);
  8448. if (!--cache->ro) {
  8449. num_bytes = cache->key.offset - cache->reserved -
  8450. cache->pinned - cache->bytes_super -
  8451. btrfs_block_group_used(&cache->item);
  8452. sinfo->bytes_readonly -= num_bytes;
  8453. list_del_init(&cache->ro_list);
  8454. }
  8455. spin_unlock(&cache->lock);
  8456. spin_unlock(&sinfo->lock);
  8457. }
  8458. /*
  8459. * checks to see if its even possible to relocate this block group.
  8460. *
  8461. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  8462. * ok to go ahead and try.
  8463. */
  8464. int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
  8465. {
  8466. struct btrfs_root *root = fs_info->extent_root;
  8467. struct btrfs_block_group_cache *block_group;
  8468. struct btrfs_space_info *space_info;
  8469. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  8470. struct btrfs_device *device;
  8471. struct btrfs_trans_handle *trans;
  8472. u64 min_free;
  8473. u64 dev_min = 1;
  8474. u64 dev_nr = 0;
  8475. u64 target;
  8476. int debug;
  8477. int index;
  8478. int full = 0;
  8479. int ret = 0;
  8480. debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
  8481. block_group = btrfs_lookup_block_group(fs_info, bytenr);
  8482. /* odd, couldn't find the block group, leave it alone */
  8483. if (!block_group) {
  8484. if (debug)
  8485. btrfs_warn(fs_info,
  8486. "can't find block group for bytenr %llu",
  8487. bytenr);
  8488. return -1;
  8489. }
  8490. min_free = btrfs_block_group_used(&block_group->item);
  8491. /* no bytes used, we're good */
  8492. if (!min_free)
  8493. goto out;
  8494. space_info = block_group->space_info;
  8495. spin_lock(&space_info->lock);
  8496. full = space_info->full;
  8497. /*
  8498. * if this is the last block group we have in this space, we can't
  8499. * relocate it unless we're able to allocate a new chunk below.
  8500. *
  8501. * Otherwise, we need to make sure we have room in the space to handle
  8502. * all of the extents from this block group. If we can, we're good
  8503. */
  8504. if ((space_info->total_bytes != block_group->key.offset) &&
  8505. (btrfs_space_info_used(space_info, false) + min_free <
  8506. space_info->total_bytes)) {
  8507. spin_unlock(&space_info->lock);
  8508. goto out;
  8509. }
  8510. spin_unlock(&space_info->lock);
  8511. /*
  8512. * ok we don't have enough space, but maybe we have free space on our
  8513. * devices to allocate new chunks for relocation, so loop through our
  8514. * alloc devices and guess if we have enough space. if this block
  8515. * group is going to be restriped, run checks against the target
  8516. * profile instead of the current one.
  8517. */
  8518. ret = -1;
  8519. /*
  8520. * index:
  8521. * 0: raid10
  8522. * 1: raid1
  8523. * 2: dup
  8524. * 3: raid0
  8525. * 4: single
  8526. */
  8527. target = get_restripe_target(fs_info, block_group->flags);
  8528. if (target) {
  8529. index = btrfs_bg_flags_to_raid_index(extended_to_chunk(target));
  8530. } else {
  8531. /*
  8532. * this is just a balance, so if we were marked as full
  8533. * we know there is no space for a new chunk
  8534. */
  8535. if (full) {
  8536. if (debug)
  8537. btrfs_warn(fs_info,
  8538. "no space to alloc new chunk for block group %llu",
  8539. block_group->key.objectid);
  8540. goto out;
  8541. }
  8542. index = btrfs_bg_flags_to_raid_index(block_group->flags);
  8543. }
  8544. if (index == BTRFS_RAID_RAID10) {
  8545. dev_min = 4;
  8546. /* Divide by 2 */
  8547. min_free >>= 1;
  8548. } else if (index == BTRFS_RAID_RAID1) {
  8549. dev_min = 2;
  8550. } else if (index == BTRFS_RAID_DUP) {
  8551. /* Multiply by 2 */
  8552. min_free <<= 1;
  8553. } else if (index == BTRFS_RAID_RAID0) {
  8554. dev_min = fs_devices->rw_devices;
  8555. min_free = div64_u64(min_free, dev_min);
  8556. }
  8557. /* We need to do this so that we can look at pending chunks */
  8558. trans = btrfs_join_transaction(root);
  8559. if (IS_ERR(trans)) {
  8560. ret = PTR_ERR(trans);
  8561. goto out;
  8562. }
  8563. mutex_lock(&fs_info->chunk_mutex);
  8564. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  8565. u64 dev_offset;
  8566. /*
  8567. * check to make sure we can actually find a chunk with enough
  8568. * space to fit our block group in.
  8569. */
  8570. if (device->total_bytes > device->bytes_used + min_free &&
  8571. !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
  8572. ret = find_free_dev_extent(trans, device, min_free,
  8573. &dev_offset, NULL);
  8574. if (!ret)
  8575. dev_nr++;
  8576. if (dev_nr >= dev_min)
  8577. break;
  8578. ret = -1;
  8579. }
  8580. }
  8581. if (debug && ret == -1)
  8582. btrfs_warn(fs_info,
  8583. "no space to allocate a new chunk for block group %llu",
  8584. block_group->key.objectid);
  8585. mutex_unlock(&fs_info->chunk_mutex);
  8586. btrfs_end_transaction(trans);
  8587. out:
  8588. btrfs_put_block_group(block_group);
  8589. return ret;
  8590. }
  8591. static int find_first_block_group(struct btrfs_fs_info *fs_info,
  8592. struct btrfs_path *path,
  8593. struct btrfs_key *key)
  8594. {
  8595. struct btrfs_root *root = fs_info->extent_root;
  8596. int ret = 0;
  8597. struct btrfs_key found_key;
  8598. struct extent_buffer *leaf;
  8599. int slot;
  8600. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  8601. if (ret < 0)
  8602. goto out;
  8603. while (1) {
  8604. slot = path->slots[0];
  8605. leaf = path->nodes[0];
  8606. if (slot >= btrfs_header_nritems(leaf)) {
  8607. ret = btrfs_next_leaf(root, path);
  8608. if (ret == 0)
  8609. continue;
  8610. if (ret < 0)
  8611. goto out;
  8612. break;
  8613. }
  8614. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  8615. if (found_key.objectid >= key->objectid &&
  8616. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  8617. struct extent_map_tree *em_tree;
  8618. struct extent_map *em;
  8619. em_tree = &root->fs_info->mapping_tree.map_tree;
  8620. read_lock(&em_tree->lock);
  8621. em = lookup_extent_mapping(em_tree, found_key.objectid,
  8622. found_key.offset);
  8623. read_unlock(&em_tree->lock);
  8624. if (!em) {
  8625. btrfs_err(fs_info,
  8626. "logical %llu len %llu found bg but no related chunk",
  8627. found_key.objectid, found_key.offset);
  8628. ret = -ENOENT;
  8629. } else {
  8630. ret = 0;
  8631. }
  8632. free_extent_map(em);
  8633. goto out;
  8634. }
  8635. path->slots[0]++;
  8636. }
  8637. out:
  8638. return ret;
  8639. }
  8640. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  8641. {
  8642. struct btrfs_block_group_cache *block_group;
  8643. u64 last = 0;
  8644. while (1) {
  8645. struct inode *inode;
  8646. block_group = btrfs_lookup_first_block_group(info, last);
  8647. while (block_group) {
  8648. spin_lock(&block_group->lock);
  8649. if (block_group->iref)
  8650. break;
  8651. spin_unlock(&block_group->lock);
  8652. block_group = next_block_group(info, block_group);
  8653. }
  8654. if (!block_group) {
  8655. if (last == 0)
  8656. break;
  8657. last = 0;
  8658. continue;
  8659. }
  8660. inode = block_group->inode;
  8661. block_group->iref = 0;
  8662. block_group->inode = NULL;
  8663. spin_unlock(&block_group->lock);
  8664. ASSERT(block_group->io_ctl.inode == NULL);
  8665. iput(inode);
  8666. last = block_group->key.objectid + block_group->key.offset;
  8667. btrfs_put_block_group(block_group);
  8668. }
  8669. }
  8670. /*
  8671. * Must be called only after stopping all workers, since we could have block
  8672. * group caching kthreads running, and therefore they could race with us if we
  8673. * freed the block groups before stopping them.
  8674. */
  8675. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  8676. {
  8677. struct btrfs_block_group_cache *block_group;
  8678. struct btrfs_space_info *space_info;
  8679. struct btrfs_caching_control *caching_ctl;
  8680. struct rb_node *n;
  8681. down_write(&info->commit_root_sem);
  8682. while (!list_empty(&info->caching_block_groups)) {
  8683. caching_ctl = list_entry(info->caching_block_groups.next,
  8684. struct btrfs_caching_control, list);
  8685. list_del(&caching_ctl->list);
  8686. put_caching_control(caching_ctl);
  8687. }
  8688. up_write(&info->commit_root_sem);
  8689. spin_lock(&info->unused_bgs_lock);
  8690. while (!list_empty(&info->unused_bgs)) {
  8691. block_group = list_first_entry(&info->unused_bgs,
  8692. struct btrfs_block_group_cache,
  8693. bg_list);
  8694. list_del_init(&block_group->bg_list);
  8695. btrfs_put_block_group(block_group);
  8696. }
  8697. spin_unlock(&info->unused_bgs_lock);
  8698. spin_lock(&info->block_group_cache_lock);
  8699. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  8700. block_group = rb_entry(n, struct btrfs_block_group_cache,
  8701. cache_node);
  8702. rb_erase(&block_group->cache_node,
  8703. &info->block_group_cache_tree);
  8704. RB_CLEAR_NODE(&block_group->cache_node);
  8705. spin_unlock(&info->block_group_cache_lock);
  8706. down_write(&block_group->space_info->groups_sem);
  8707. list_del(&block_group->list);
  8708. up_write(&block_group->space_info->groups_sem);
  8709. /*
  8710. * We haven't cached this block group, which means we could
  8711. * possibly have excluded extents on this block group.
  8712. */
  8713. if (block_group->cached == BTRFS_CACHE_NO ||
  8714. block_group->cached == BTRFS_CACHE_ERROR)
  8715. free_excluded_extents(info, block_group);
  8716. btrfs_remove_free_space_cache(block_group);
  8717. ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
  8718. ASSERT(list_empty(&block_group->dirty_list));
  8719. ASSERT(list_empty(&block_group->io_list));
  8720. ASSERT(list_empty(&block_group->bg_list));
  8721. ASSERT(atomic_read(&block_group->count) == 1);
  8722. btrfs_put_block_group(block_group);
  8723. spin_lock(&info->block_group_cache_lock);
  8724. }
  8725. spin_unlock(&info->block_group_cache_lock);
  8726. /* now that all the block groups are freed, go through and
  8727. * free all the space_info structs. This is only called during
  8728. * the final stages of unmount, and so we know nobody is
  8729. * using them. We call synchronize_rcu() once before we start,
  8730. * just to be on the safe side.
  8731. */
  8732. synchronize_rcu();
  8733. release_global_block_rsv(info);
  8734. while (!list_empty(&info->space_info)) {
  8735. int i;
  8736. space_info = list_entry(info->space_info.next,
  8737. struct btrfs_space_info,
  8738. list);
  8739. /*
  8740. * Do not hide this behind enospc_debug, this is actually
  8741. * important and indicates a real bug if this happens.
  8742. */
  8743. if (WARN_ON(space_info->bytes_pinned > 0 ||
  8744. space_info->bytes_reserved > 0 ||
  8745. space_info->bytes_may_use > 0))
  8746. dump_space_info(info, space_info, 0, 0);
  8747. list_del(&space_info->list);
  8748. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  8749. struct kobject *kobj;
  8750. kobj = space_info->block_group_kobjs[i];
  8751. space_info->block_group_kobjs[i] = NULL;
  8752. if (kobj) {
  8753. kobject_del(kobj);
  8754. kobject_put(kobj);
  8755. }
  8756. }
  8757. kobject_del(&space_info->kobj);
  8758. kobject_put(&space_info->kobj);
  8759. }
  8760. return 0;
  8761. }
  8762. /* link_block_group will queue up kobjects to add when we're reclaim-safe */
  8763. void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
  8764. {
  8765. struct btrfs_space_info *space_info;
  8766. struct raid_kobject *rkobj;
  8767. LIST_HEAD(list);
  8768. int index;
  8769. int ret = 0;
  8770. spin_lock(&fs_info->pending_raid_kobjs_lock);
  8771. list_splice_init(&fs_info->pending_raid_kobjs, &list);
  8772. spin_unlock(&fs_info->pending_raid_kobjs_lock);
  8773. list_for_each_entry(rkobj, &list, list) {
  8774. space_info = __find_space_info(fs_info, rkobj->flags);
  8775. index = btrfs_bg_flags_to_raid_index(rkobj->flags);
  8776. ret = kobject_add(&rkobj->kobj, &space_info->kobj,
  8777. "%s", get_raid_name(index));
  8778. if (ret) {
  8779. kobject_put(&rkobj->kobj);
  8780. break;
  8781. }
  8782. }
  8783. if (ret)
  8784. btrfs_warn(fs_info,
  8785. "failed to add kobject for block cache, ignoring");
  8786. }
  8787. static void link_block_group(struct btrfs_block_group_cache *cache)
  8788. {
  8789. struct btrfs_space_info *space_info = cache->space_info;
  8790. struct btrfs_fs_info *fs_info = cache->fs_info;
  8791. int index = btrfs_bg_flags_to_raid_index(cache->flags);
  8792. bool first = false;
  8793. down_write(&space_info->groups_sem);
  8794. if (list_empty(&space_info->block_groups[index]))
  8795. first = true;
  8796. list_add_tail(&cache->list, &space_info->block_groups[index]);
  8797. up_write(&space_info->groups_sem);
  8798. if (first) {
  8799. struct raid_kobject *rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
  8800. if (!rkobj) {
  8801. btrfs_warn(cache->fs_info,
  8802. "couldn't alloc memory for raid level kobject");
  8803. return;
  8804. }
  8805. rkobj->flags = cache->flags;
  8806. kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
  8807. spin_lock(&fs_info->pending_raid_kobjs_lock);
  8808. list_add_tail(&rkobj->list, &fs_info->pending_raid_kobjs);
  8809. spin_unlock(&fs_info->pending_raid_kobjs_lock);
  8810. space_info->block_group_kobjs[index] = &rkobj->kobj;
  8811. }
  8812. }
  8813. static struct btrfs_block_group_cache *
  8814. btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
  8815. u64 start, u64 size)
  8816. {
  8817. struct btrfs_block_group_cache *cache;
  8818. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  8819. if (!cache)
  8820. return NULL;
  8821. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  8822. GFP_NOFS);
  8823. if (!cache->free_space_ctl) {
  8824. kfree(cache);
  8825. return NULL;
  8826. }
  8827. cache->key.objectid = start;
  8828. cache->key.offset = size;
  8829. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  8830. cache->fs_info = fs_info;
  8831. cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
  8832. set_free_space_tree_thresholds(cache);
  8833. atomic_set(&cache->count, 1);
  8834. spin_lock_init(&cache->lock);
  8835. init_rwsem(&cache->data_rwsem);
  8836. INIT_LIST_HEAD(&cache->list);
  8837. INIT_LIST_HEAD(&cache->cluster_list);
  8838. INIT_LIST_HEAD(&cache->bg_list);
  8839. INIT_LIST_HEAD(&cache->ro_list);
  8840. INIT_LIST_HEAD(&cache->dirty_list);
  8841. INIT_LIST_HEAD(&cache->io_list);
  8842. btrfs_init_free_space_ctl(cache);
  8843. atomic_set(&cache->trimming, 0);
  8844. mutex_init(&cache->free_space_lock);
  8845. btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
  8846. return cache;
  8847. }
  8848. int btrfs_read_block_groups(struct btrfs_fs_info *info)
  8849. {
  8850. struct btrfs_path *path;
  8851. int ret;
  8852. struct btrfs_block_group_cache *cache;
  8853. struct btrfs_space_info *space_info;
  8854. struct btrfs_key key;
  8855. struct btrfs_key found_key;
  8856. struct extent_buffer *leaf;
  8857. int need_clear = 0;
  8858. u64 cache_gen;
  8859. u64 feature;
  8860. int mixed;
  8861. feature = btrfs_super_incompat_flags(info->super_copy);
  8862. mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
  8863. key.objectid = 0;
  8864. key.offset = 0;
  8865. key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  8866. path = btrfs_alloc_path();
  8867. if (!path)
  8868. return -ENOMEM;
  8869. path->reada = READA_FORWARD;
  8870. cache_gen = btrfs_super_cache_generation(info->super_copy);
  8871. if (btrfs_test_opt(info, SPACE_CACHE) &&
  8872. btrfs_super_generation(info->super_copy) != cache_gen)
  8873. need_clear = 1;
  8874. if (btrfs_test_opt(info, CLEAR_CACHE))
  8875. need_clear = 1;
  8876. while (1) {
  8877. ret = find_first_block_group(info, path, &key);
  8878. if (ret > 0)
  8879. break;
  8880. if (ret != 0)
  8881. goto error;
  8882. leaf = path->nodes[0];
  8883. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  8884. cache = btrfs_create_block_group_cache(info, found_key.objectid,
  8885. found_key.offset);
  8886. if (!cache) {
  8887. ret = -ENOMEM;
  8888. goto error;
  8889. }
  8890. if (need_clear) {
  8891. /*
  8892. * When we mount with old space cache, we need to
  8893. * set BTRFS_DC_CLEAR and set dirty flag.
  8894. *
  8895. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  8896. * truncate the old free space cache inode and
  8897. * setup a new one.
  8898. * b) Setting 'dirty flag' makes sure that we flush
  8899. * the new space cache info onto disk.
  8900. */
  8901. if (btrfs_test_opt(info, SPACE_CACHE))
  8902. cache->disk_cache_state = BTRFS_DC_CLEAR;
  8903. }
  8904. read_extent_buffer(leaf, &cache->item,
  8905. btrfs_item_ptr_offset(leaf, path->slots[0]),
  8906. sizeof(cache->item));
  8907. cache->flags = btrfs_block_group_flags(&cache->item);
  8908. if (!mixed &&
  8909. ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
  8910. (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
  8911. btrfs_err(info,
  8912. "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
  8913. cache->key.objectid);
  8914. ret = -EINVAL;
  8915. goto error;
  8916. }
  8917. key.objectid = found_key.objectid + found_key.offset;
  8918. btrfs_release_path(path);
  8919. /*
  8920. * We need to exclude the super stripes now so that the space
  8921. * info has super bytes accounted for, otherwise we'll think
  8922. * we have more space than we actually do.
  8923. */
  8924. ret = exclude_super_stripes(info, cache);
  8925. if (ret) {
  8926. /*
  8927. * We may have excluded something, so call this just in
  8928. * case.
  8929. */
  8930. free_excluded_extents(info, cache);
  8931. btrfs_put_block_group(cache);
  8932. goto error;
  8933. }
  8934. /*
  8935. * check for two cases, either we are full, and therefore
  8936. * don't need to bother with the caching work since we won't
  8937. * find any space, or we are empty, and we can just add all
  8938. * the space in and be done with it. This saves us _alot_ of
  8939. * time, particularly in the full case.
  8940. */
  8941. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  8942. cache->last_byte_to_unpin = (u64)-1;
  8943. cache->cached = BTRFS_CACHE_FINISHED;
  8944. free_excluded_extents(info, cache);
  8945. } else if (btrfs_block_group_used(&cache->item) == 0) {
  8946. cache->last_byte_to_unpin = (u64)-1;
  8947. cache->cached = BTRFS_CACHE_FINISHED;
  8948. add_new_free_space(cache, found_key.objectid,
  8949. found_key.objectid +
  8950. found_key.offset);
  8951. free_excluded_extents(info, cache);
  8952. }
  8953. ret = btrfs_add_block_group_cache(info, cache);
  8954. if (ret) {
  8955. btrfs_remove_free_space_cache(cache);
  8956. btrfs_put_block_group(cache);
  8957. goto error;
  8958. }
  8959. trace_btrfs_add_block_group(info, cache, 0);
  8960. update_space_info(info, cache->flags, found_key.offset,
  8961. btrfs_block_group_used(&cache->item),
  8962. cache->bytes_super, &space_info);
  8963. cache->space_info = space_info;
  8964. link_block_group(cache);
  8965. set_avail_alloc_bits(info, cache->flags);
  8966. if (btrfs_chunk_readonly(info, cache->key.objectid)) {
  8967. inc_block_group_ro(cache, 1);
  8968. } else if (btrfs_block_group_used(&cache->item) == 0) {
  8969. spin_lock(&info->unused_bgs_lock);
  8970. /* Should always be true but just in case. */
  8971. if (list_empty(&cache->bg_list)) {
  8972. btrfs_get_block_group(cache);
  8973. trace_btrfs_add_unused_block_group(cache);
  8974. list_add_tail(&cache->bg_list,
  8975. &info->unused_bgs);
  8976. }
  8977. spin_unlock(&info->unused_bgs_lock);
  8978. }
  8979. }
  8980. list_for_each_entry_rcu(space_info, &info->space_info, list) {
  8981. if (!(get_alloc_profile(info, space_info->flags) &
  8982. (BTRFS_BLOCK_GROUP_RAID10 |
  8983. BTRFS_BLOCK_GROUP_RAID1 |
  8984. BTRFS_BLOCK_GROUP_RAID5 |
  8985. BTRFS_BLOCK_GROUP_RAID6 |
  8986. BTRFS_BLOCK_GROUP_DUP)))
  8987. continue;
  8988. /*
  8989. * avoid allocating from un-mirrored block group if there are
  8990. * mirrored block groups.
  8991. */
  8992. list_for_each_entry(cache,
  8993. &space_info->block_groups[BTRFS_RAID_RAID0],
  8994. list)
  8995. inc_block_group_ro(cache, 1);
  8996. list_for_each_entry(cache,
  8997. &space_info->block_groups[BTRFS_RAID_SINGLE],
  8998. list)
  8999. inc_block_group_ro(cache, 1);
  9000. }
  9001. btrfs_add_raid_kobjects(info);
  9002. init_global_block_rsv(info);
  9003. ret = 0;
  9004. error:
  9005. btrfs_free_path(path);
  9006. return ret;
  9007. }
  9008. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
  9009. {
  9010. struct btrfs_fs_info *fs_info = trans->fs_info;
  9011. struct btrfs_block_group_cache *block_group, *tmp;
  9012. struct btrfs_root *extent_root = fs_info->extent_root;
  9013. struct btrfs_block_group_item item;
  9014. struct btrfs_key key;
  9015. int ret = 0;
  9016. bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
  9017. trans->can_flush_pending_bgs = false;
  9018. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
  9019. if (ret)
  9020. goto next;
  9021. spin_lock(&block_group->lock);
  9022. memcpy(&item, &block_group->item, sizeof(item));
  9023. memcpy(&key, &block_group->key, sizeof(key));
  9024. spin_unlock(&block_group->lock);
  9025. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  9026. sizeof(item));
  9027. if (ret)
  9028. btrfs_abort_transaction(trans, ret);
  9029. ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
  9030. key.offset);
  9031. if (ret)
  9032. btrfs_abort_transaction(trans, ret);
  9033. add_block_group_free_space(trans, block_group);
  9034. /* already aborted the transaction if it failed. */
  9035. next:
  9036. list_del_init(&block_group->bg_list);
  9037. }
  9038. trans->can_flush_pending_bgs = can_flush_pending_bgs;
  9039. }
  9040. int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
  9041. u64 type, u64 chunk_offset, u64 size)
  9042. {
  9043. struct btrfs_fs_info *fs_info = trans->fs_info;
  9044. struct btrfs_block_group_cache *cache;
  9045. int ret;
  9046. btrfs_set_log_full_commit(fs_info, trans);
  9047. cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
  9048. if (!cache)
  9049. return -ENOMEM;
  9050. btrfs_set_block_group_used(&cache->item, bytes_used);
  9051. btrfs_set_block_group_chunk_objectid(&cache->item,
  9052. BTRFS_FIRST_CHUNK_TREE_OBJECTID);
  9053. btrfs_set_block_group_flags(&cache->item, type);
  9054. cache->flags = type;
  9055. cache->last_byte_to_unpin = (u64)-1;
  9056. cache->cached = BTRFS_CACHE_FINISHED;
  9057. cache->needs_free_space = 1;
  9058. ret = exclude_super_stripes(fs_info, cache);
  9059. if (ret) {
  9060. /*
  9061. * We may have excluded something, so call this just in
  9062. * case.
  9063. */
  9064. free_excluded_extents(fs_info, cache);
  9065. btrfs_put_block_group(cache);
  9066. return ret;
  9067. }
  9068. add_new_free_space(cache, chunk_offset, chunk_offset + size);
  9069. free_excluded_extents(fs_info, cache);
  9070. #ifdef CONFIG_BTRFS_DEBUG
  9071. if (btrfs_should_fragment_free_space(cache)) {
  9072. u64 new_bytes_used = size - bytes_used;
  9073. bytes_used += new_bytes_used >> 1;
  9074. fragment_free_space(cache);
  9075. }
  9076. #endif
  9077. /*
  9078. * Ensure the corresponding space_info object is created and
  9079. * assigned to our block group. We want our bg to be added to the rbtree
  9080. * with its ->space_info set.
  9081. */
  9082. cache->space_info = __find_space_info(fs_info, cache->flags);
  9083. ASSERT(cache->space_info);
  9084. ret = btrfs_add_block_group_cache(fs_info, cache);
  9085. if (ret) {
  9086. btrfs_remove_free_space_cache(cache);
  9087. btrfs_put_block_group(cache);
  9088. return ret;
  9089. }
  9090. /*
  9091. * Now that our block group has its ->space_info set and is inserted in
  9092. * the rbtree, update the space info's counters.
  9093. */
  9094. trace_btrfs_add_block_group(fs_info, cache, 1);
  9095. update_space_info(fs_info, cache->flags, size, bytes_used,
  9096. cache->bytes_super, &cache->space_info);
  9097. update_global_block_rsv(fs_info);
  9098. link_block_group(cache);
  9099. list_add_tail(&cache->bg_list, &trans->new_bgs);
  9100. set_avail_alloc_bits(fs_info, type);
  9101. return 0;
  9102. }
  9103. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  9104. {
  9105. u64 extra_flags = chunk_to_extended(flags) &
  9106. BTRFS_EXTENDED_PROFILE_MASK;
  9107. write_seqlock(&fs_info->profiles_lock);
  9108. if (flags & BTRFS_BLOCK_GROUP_DATA)
  9109. fs_info->avail_data_alloc_bits &= ~extra_flags;
  9110. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  9111. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  9112. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  9113. fs_info->avail_system_alloc_bits &= ~extra_flags;
  9114. write_sequnlock(&fs_info->profiles_lock);
  9115. }
  9116. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  9117. u64 group_start, struct extent_map *em)
  9118. {
  9119. struct btrfs_fs_info *fs_info = trans->fs_info;
  9120. struct btrfs_root *root = fs_info->extent_root;
  9121. struct btrfs_path *path;
  9122. struct btrfs_block_group_cache *block_group;
  9123. struct btrfs_free_cluster *cluster;
  9124. struct btrfs_root *tree_root = fs_info->tree_root;
  9125. struct btrfs_key key;
  9126. struct inode *inode;
  9127. struct kobject *kobj = NULL;
  9128. int ret;
  9129. int index;
  9130. int factor;
  9131. struct btrfs_caching_control *caching_ctl = NULL;
  9132. bool remove_em;
  9133. block_group = btrfs_lookup_block_group(fs_info, group_start);
  9134. BUG_ON(!block_group);
  9135. BUG_ON(!block_group->ro);
  9136. trace_btrfs_remove_block_group(block_group);
  9137. /*
  9138. * Free the reserved super bytes from this block group before
  9139. * remove it.
  9140. */
  9141. free_excluded_extents(fs_info, block_group);
  9142. btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
  9143. block_group->key.offset);
  9144. memcpy(&key, &block_group->key, sizeof(key));
  9145. index = btrfs_bg_flags_to_raid_index(block_group->flags);
  9146. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  9147. BTRFS_BLOCK_GROUP_RAID1 |
  9148. BTRFS_BLOCK_GROUP_RAID10))
  9149. factor = 2;
  9150. else
  9151. factor = 1;
  9152. /* make sure this block group isn't part of an allocation cluster */
  9153. cluster = &fs_info->data_alloc_cluster;
  9154. spin_lock(&cluster->refill_lock);
  9155. btrfs_return_cluster_to_free_space(block_group, cluster);
  9156. spin_unlock(&cluster->refill_lock);
  9157. /*
  9158. * make sure this block group isn't part of a metadata
  9159. * allocation cluster
  9160. */
  9161. cluster = &fs_info->meta_alloc_cluster;
  9162. spin_lock(&cluster->refill_lock);
  9163. btrfs_return_cluster_to_free_space(block_group, cluster);
  9164. spin_unlock(&cluster->refill_lock);
  9165. path = btrfs_alloc_path();
  9166. if (!path) {
  9167. ret = -ENOMEM;
  9168. goto out;
  9169. }
  9170. /*
  9171. * get the inode first so any iput calls done for the io_list
  9172. * aren't the final iput (no unlinks allowed now)
  9173. */
  9174. inode = lookup_free_space_inode(fs_info, block_group, path);
  9175. mutex_lock(&trans->transaction->cache_write_mutex);
  9176. /*
  9177. * make sure our free spache cache IO is done before remove the
  9178. * free space inode
  9179. */
  9180. spin_lock(&trans->transaction->dirty_bgs_lock);
  9181. if (!list_empty(&block_group->io_list)) {
  9182. list_del_init(&block_group->io_list);
  9183. WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
  9184. spin_unlock(&trans->transaction->dirty_bgs_lock);
  9185. btrfs_wait_cache_io(trans, block_group, path);
  9186. btrfs_put_block_group(block_group);
  9187. spin_lock(&trans->transaction->dirty_bgs_lock);
  9188. }
  9189. if (!list_empty(&block_group->dirty_list)) {
  9190. list_del_init(&block_group->dirty_list);
  9191. btrfs_put_block_group(block_group);
  9192. }
  9193. spin_unlock(&trans->transaction->dirty_bgs_lock);
  9194. mutex_unlock(&trans->transaction->cache_write_mutex);
  9195. if (!IS_ERR(inode)) {
  9196. ret = btrfs_orphan_add(trans, BTRFS_I(inode));
  9197. if (ret) {
  9198. btrfs_add_delayed_iput(inode);
  9199. goto out;
  9200. }
  9201. clear_nlink(inode);
  9202. /* One for the block groups ref */
  9203. spin_lock(&block_group->lock);
  9204. if (block_group->iref) {
  9205. block_group->iref = 0;
  9206. block_group->inode = NULL;
  9207. spin_unlock(&block_group->lock);
  9208. iput(inode);
  9209. } else {
  9210. spin_unlock(&block_group->lock);
  9211. }
  9212. /* One for our lookup ref */
  9213. btrfs_add_delayed_iput(inode);
  9214. }
  9215. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  9216. key.offset = block_group->key.objectid;
  9217. key.type = 0;
  9218. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  9219. if (ret < 0)
  9220. goto out;
  9221. if (ret > 0)
  9222. btrfs_release_path(path);
  9223. if (ret == 0) {
  9224. ret = btrfs_del_item(trans, tree_root, path);
  9225. if (ret)
  9226. goto out;
  9227. btrfs_release_path(path);
  9228. }
  9229. spin_lock(&fs_info->block_group_cache_lock);
  9230. rb_erase(&block_group->cache_node,
  9231. &fs_info->block_group_cache_tree);
  9232. RB_CLEAR_NODE(&block_group->cache_node);
  9233. if (fs_info->first_logical_byte == block_group->key.objectid)
  9234. fs_info->first_logical_byte = (u64)-1;
  9235. spin_unlock(&fs_info->block_group_cache_lock);
  9236. down_write(&block_group->space_info->groups_sem);
  9237. /*
  9238. * we must use list_del_init so people can check to see if they
  9239. * are still on the list after taking the semaphore
  9240. */
  9241. list_del_init(&block_group->list);
  9242. if (list_empty(&block_group->space_info->block_groups[index])) {
  9243. kobj = block_group->space_info->block_group_kobjs[index];
  9244. block_group->space_info->block_group_kobjs[index] = NULL;
  9245. clear_avail_alloc_bits(fs_info, block_group->flags);
  9246. }
  9247. up_write(&block_group->space_info->groups_sem);
  9248. if (kobj) {
  9249. kobject_del(kobj);
  9250. kobject_put(kobj);
  9251. }
  9252. if (block_group->has_caching_ctl)
  9253. caching_ctl = get_caching_control(block_group);
  9254. if (block_group->cached == BTRFS_CACHE_STARTED)
  9255. wait_block_group_cache_done(block_group);
  9256. if (block_group->has_caching_ctl) {
  9257. down_write(&fs_info->commit_root_sem);
  9258. if (!caching_ctl) {
  9259. struct btrfs_caching_control *ctl;
  9260. list_for_each_entry(ctl,
  9261. &fs_info->caching_block_groups, list)
  9262. if (ctl->block_group == block_group) {
  9263. caching_ctl = ctl;
  9264. refcount_inc(&caching_ctl->count);
  9265. break;
  9266. }
  9267. }
  9268. if (caching_ctl)
  9269. list_del_init(&caching_ctl->list);
  9270. up_write(&fs_info->commit_root_sem);
  9271. if (caching_ctl) {
  9272. /* Once for the caching bgs list and once for us. */
  9273. put_caching_control(caching_ctl);
  9274. put_caching_control(caching_ctl);
  9275. }
  9276. }
  9277. spin_lock(&trans->transaction->dirty_bgs_lock);
  9278. if (!list_empty(&block_group->dirty_list)) {
  9279. WARN_ON(1);
  9280. }
  9281. if (!list_empty(&block_group->io_list)) {
  9282. WARN_ON(1);
  9283. }
  9284. spin_unlock(&trans->transaction->dirty_bgs_lock);
  9285. btrfs_remove_free_space_cache(block_group);
  9286. spin_lock(&block_group->space_info->lock);
  9287. list_del_init(&block_group->ro_list);
  9288. if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
  9289. WARN_ON(block_group->space_info->total_bytes
  9290. < block_group->key.offset);
  9291. WARN_ON(block_group->space_info->bytes_readonly
  9292. < block_group->key.offset);
  9293. WARN_ON(block_group->space_info->disk_total
  9294. < block_group->key.offset * factor);
  9295. }
  9296. block_group->space_info->total_bytes -= block_group->key.offset;
  9297. block_group->space_info->bytes_readonly -= block_group->key.offset;
  9298. block_group->space_info->disk_total -= block_group->key.offset * factor;
  9299. spin_unlock(&block_group->space_info->lock);
  9300. memcpy(&key, &block_group->key, sizeof(key));
  9301. mutex_lock(&fs_info->chunk_mutex);
  9302. if (!list_empty(&em->list)) {
  9303. /* We're in the transaction->pending_chunks list. */
  9304. free_extent_map(em);
  9305. }
  9306. spin_lock(&block_group->lock);
  9307. block_group->removed = 1;
  9308. /*
  9309. * At this point trimming can't start on this block group, because we
  9310. * removed the block group from the tree fs_info->block_group_cache_tree
  9311. * so no one can't find it anymore and even if someone already got this
  9312. * block group before we removed it from the rbtree, they have already
  9313. * incremented block_group->trimming - if they didn't, they won't find
  9314. * any free space entries because we already removed them all when we
  9315. * called btrfs_remove_free_space_cache().
  9316. *
  9317. * And we must not remove the extent map from the fs_info->mapping_tree
  9318. * to prevent the same logical address range and physical device space
  9319. * ranges from being reused for a new block group. This is because our
  9320. * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
  9321. * completely transactionless, so while it is trimming a range the
  9322. * currently running transaction might finish and a new one start,
  9323. * allowing for new block groups to be created that can reuse the same
  9324. * physical device locations unless we take this special care.
  9325. *
  9326. * There may also be an implicit trim operation if the file system
  9327. * is mounted with -odiscard. The same protections must remain
  9328. * in place until the extents have been discarded completely when
  9329. * the transaction commit has completed.
  9330. */
  9331. remove_em = (atomic_read(&block_group->trimming) == 0);
  9332. /*
  9333. * Make sure a trimmer task always sees the em in the pinned_chunks list
  9334. * if it sees block_group->removed == 1 (needs to lock block_group->lock
  9335. * before checking block_group->removed).
  9336. */
  9337. if (!remove_em) {
  9338. /*
  9339. * Our em might be in trans->transaction->pending_chunks which
  9340. * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
  9341. * and so is the fs_info->pinned_chunks list.
  9342. *
  9343. * So at this point we must be holding the chunk_mutex to avoid
  9344. * any races with chunk allocation (more specifically at
  9345. * volumes.c:contains_pending_extent()), to ensure it always
  9346. * sees the em, either in the pending_chunks list or in the
  9347. * pinned_chunks list.
  9348. */
  9349. list_move_tail(&em->list, &fs_info->pinned_chunks);
  9350. }
  9351. spin_unlock(&block_group->lock);
  9352. if (remove_em) {
  9353. struct extent_map_tree *em_tree;
  9354. em_tree = &fs_info->mapping_tree.map_tree;
  9355. write_lock(&em_tree->lock);
  9356. /*
  9357. * The em might be in the pending_chunks list, so make sure the
  9358. * chunk mutex is locked, since remove_extent_mapping() will
  9359. * delete us from that list.
  9360. */
  9361. remove_extent_mapping(em_tree, em);
  9362. write_unlock(&em_tree->lock);
  9363. /* once for the tree */
  9364. free_extent_map(em);
  9365. }
  9366. mutex_unlock(&fs_info->chunk_mutex);
  9367. ret = remove_block_group_free_space(trans, block_group);
  9368. if (ret)
  9369. goto out;
  9370. btrfs_put_block_group(block_group);
  9371. btrfs_put_block_group(block_group);
  9372. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  9373. if (ret > 0)
  9374. ret = -EIO;
  9375. if (ret < 0)
  9376. goto out;
  9377. ret = btrfs_del_item(trans, root, path);
  9378. out:
  9379. btrfs_free_path(path);
  9380. return ret;
  9381. }
  9382. struct btrfs_trans_handle *
  9383. btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
  9384. const u64 chunk_offset)
  9385. {
  9386. struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
  9387. struct extent_map *em;
  9388. struct map_lookup *map;
  9389. unsigned int num_items;
  9390. read_lock(&em_tree->lock);
  9391. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  9392. read_unlock(&em_tree->lock);
  9393. ASSERT(em && em->start == chunk_offset);
  9394. /*
  9395. * We need to reserve 3 + N units from the metadata space info in order
  9396. * to remove a block group (done at btrfs_remove_chunk() and at
  9397. * btrfs_remove_block_group()), which are used for:
  9398. *
  9399. * 1 unit for adding the free space inode's orphan (located in the tree
  9400. * of tree roots).
  9401. * 1 unit for deleting the block group item (located in the extent
  9402. * tree).
  9403. * 1 unit for deleting the free space item (located in tree of tree
  9404. * roots).
  9405. * N units for deleting N device extent items corresponding to each
  9406. * stripe (located in the device tree).
  9407. *
  9408. * In order to remove a block group we also need to reserve units in the
  9409. * system space info in order to update the chunk tree (update one or
  9410. * more device items and remove one chunk item), but this is done at
  9411. * btrfs_remove_chunk() through a call to check_system_chunk().
  9412. */
  9413. map = em->map_lookup;
  9414. num_items = 3 + map->num_stripes;
  9415. free_extent_map(em);
  9416. return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
  9417. num_items, 1);
  9418. }
  9419. /*
  9420. * Process the unused_bgs list and remove any that don't have any allocated
  9421. * space inside of them.
  9422. */
  9423. void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
  9424. {
  9425. struct btrfs_block_group_cache *block_group;
  9426. struct btrfs_space_info *space_info;
  9427. struct btrfs_trans_handle *trans;
  9428. int ret = 0;
  9429. if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
  9430. return;
  9431. spin_lock(&fs_info->unused_bgs_lock);
  9432. while (!list_empty(&fs_info->unused_bgs)) {
  9433. u64 start, end;
  9434. int trimming;
  9435. block_group = list_first_entry(&fs_info->unused_bgs,
  9436. struct btrfs_block_group_cache,
  9437. bg_list);
  9438. list_del_init(&block_group->bg_list);
  9439. space_info = block_group->space_info;
  9440. if (ret || btrfs_mixed_space_info(space_info)) {
  9441. btrfs_put_block_group(block_group);
  9442. continue;
  9443. }
  9444. spin_unlock(&fs_info->unused_bgs_lock);
  9445. mutex_lock(&fs_info->delete_unused_bgs_mutex);
  9446. /* Don't want to race with allocators so take the groups_sem */
  9447. down_write(&space_info->groups_sem);
  9448. spin_lock(&block_group->lock);
  9449. if (block_group->reserved ||
  9450. btrfs_block_group_used(&block_group->item) ||
  9451. block_group->ro ||
  9452. list_is_singular(&block_group->list)) {
  9453. /*
  9454. * We want to bail if we made new allocations or have
  9455. * outstanding allocations in this block group. We do
  9456. * the ro check in case balance is currently acting on
  9457. * this block group.
  9458. */
  9459. trace_btrfs_skip_unused_block_group(block_group);
  9460. spin_unlock(&block_group->lock);
  9461. up_write(&space_info->groups_sem);
  9462. goto next;
  9463. }
  9464. spin_unlock(&block_group->lock);
  9465. /* We don't want to force the issue, only flip if it's ok. */
  9466. ret = inc_block_group_ro(block_group, 0);
  9467. up_write(&space_info->groups_sem);
  9468. if (ret < 0) {
  9469. ret = 0;
  9470. goto next;
  9471. }
  9472. /*
  9473. * Want to do this before we do anything else so we can recover
  9474. * properly if we fail to join the transaction.
  9475. */
  9476. trans = btrfs_start_trans_remove_block_group(fs_info,
  9477. block_group->key.objectid);
  9478. if (IS_ERR(trans)) {
  9479. btrfs_dec_block_group_ro(block_group);
  9480. ret = PTR_ERR(trans);
  9481. goto next;
  9482. }
  9483. /*
  9484. * We could have pending pinned extents for this block group,
  9485. * just delete them, we don't care about them anymore.
  9486. */
  9487. start = block_group->key.objectid;
  9488. end = start + block_group->key.offset - 1;
  9489. /*
  9490. * Hold the unused_bg_unpin_mutex lock to avoid racing with
  9491. * btrfs_finish_extent_commit(). If we are at transaction N,
  9492. * another task might be running finish_extent_commit() for the
  9493. * previous transaction N - 1, and have seen a range belonging
  9494. * to the block group in freed_extents[] before we were able to
  9495. * clear the whole block group range from freed_extents[]. This
  9496. * means that task can lookup for the block group after we
  9497. * unpinned it from freed_extents[] and removed it, leading to
  9498. * a BUG_ON() at btrfs_unpin_extent_range().
  9499. */
  9500. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  9501. ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
  9502. EXTENT_DIRTY);
  9503. if (ret) {
  9504. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  9505. btrfs_dec_block_group_ro(block_group);
  9506. goto end_trans;
  9507. }
  9508. ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
  9509. EXTENT_DIRTY);
  9510. if (ret) {
  9511. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  9512. btrfs_dec_block_group_ro(block_group);
  9513. goto end_trans;
  9514. }
  9515. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  9516. /* Reset pinned so btrfs_put_block_group doesn't complain */
  9517. spin_lock(&space_info->lock);
  9518. spin_lock(&block_group->lock);
  9519. space_info->bytes_pinned -= block_group->pinned;
  9520. space_info->bytes_readonly += block_group->pinned;
  9521. percpu_counter_add(&space_info->total_bytes_pinned,
  9522. -block_group->pinned);
  9523. block_group->pinned = 0;
  9524. spin_unlock(&block_group->lock);
  9525. spin_unlock(&space_info->lock);
  9526. /* DISCARD can flip during remount */
  9527. trimming = btrfs_test_opt(fs_info, DISCARD);
  9528. /* Implicit trim during transaction commit. */
  9529. if (trimming)
  9530. btrfs_get_block_group_trimming(block_group);
  9531. /*
  9532. * Btrfs_remove_chunk will abort the transaction if things go
  9533. * horribly wrong.
  9534. */
  9535. ret = btrfs_remove_chunk(trans, fs_info,
  9536. block_group->key.objectid);
  9537. if (ret) {
  9538. if (trimming)
  9539. btrfs_put_block_group_trimming(block_group);
  9540. goto end_trans;
  9541. }
  9542. /*
  9543. * If we're not mounted with -odiscard, we can just forget
  9544. * about this block group. Otherwise we'll need to wait
  9545. * until transaction commit to do the actual discard.
  9546. */
  9547. if (trimming) {
  9548. spin_lock(&fs_info->unused_bgs_lock);
  9549. /*
  9550. * A concurrent scrub might have added us to the list
  9551. * fs_info->unused_bgs, so use a list_move operation
  9552. * to add the block group to the deleted_bgs list.
  9553. */
  9554. list_move(&block_group->bg_list,
  9555. &trans->transaction->deleted_bgs);
  9556. spin_unlock(&fs_info->unused_bgs_lock);
  9557. btrfs_get_block_group(block_group);
  9558. }
  9559. end_trans:
  9560. btrfs_end_transaction(trans);
  9561. next:
  9562. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  9563. btrfs_put_block_group(block_group);
  9564. spin_lock(&fs_info->unused_bgs_lock);
  9565. }
  9566. spin_unlock(&fs_info->unused_bgs_lock);
  9567. }
  9568. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  9569. {
  9570. struct btrfs_super_block *disk_super;
  9571. u64 features;
  9572. u64 flags;
  9573. int mixed = 0;
  9574. int ret;
  9575. disk_super = fs_info->super_copy;
  9576. if (!btrfs_super_root(disk_super))
  9577. return -EINVAL;
  9578. features = btrfs_super_incompat_flags(disk_super);
  9579. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  9580. mixed = 1;
  9581. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  9582. ret = create_space_info(fs_info, flags);
  9583. if (ret)
  9584. goto out;
  9585. if (mixed) {
  9586. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  9587. ret = create_space_info(fs_info, flags);
  9588. } else {
  9589. flags = BTRFS_BLOCK_GROUP_METADATA;
  9590. ret = create_space_info(fs_info, flags);
  9591. if (ret)
  9592. goto out;
  9593. flags = BTRFS_BLOCK_GROUP_DATA;
  9594. ret = create_space_info(fs_info, flags);
  9595. }
  9596. out:
  9597. return ret;
  9598. }
  9599. int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
  9600. u64 start, u64 end)
  9601. {
  9602. return unpin_extent_range(fs_info, start, end, false);
  9603. }
  9604. /*
  9605. * It used to be that old block groups would be left around forever.
  9606. * Iterating over them would be enough to trim unused space. Since we
  9607. * now automatically remove them, we also need to iterate over unallocated
  9608. * space.
  9609. *
  9610. * We don't want a transaction for this since the discard may take a
  9611. * substantial amount of time. We don't require that a transaction be
  9612. * running, but we do need to take a running transaction into account
  9613. * to ensure that we're not discarding chunks that were released in
  9614. * the current transaction.
  9615. *
  9616. * Holding the chunks lock will prevent other threads from allocating
  9617. * or releasing chunks, but it won't prevent a running transaction
  9618. * from committing and releasing the memory that the pending chunks
  9619. * list head uses. For that, we need to take a reference to the
  9620. * transaction.
  9621. */
  9622. static int btrfs_trim_free_extents(struct btrfs_device *device,
  9623. u64 minlen, u64 *trimmed)
  9624. {
  9625. u64 start = 0, len = 0;
  9626. int ret;
  9627. *trimmed = 0;
  9628. /* Not writeable = nothing to do. */
  9629. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
  9630. return 0;
  9631. /* No free space = nothing to do. */
  9632. if (device->total_bytes <= device->bytes_used)
  9633. return 0;
  9634. ret = 0;
  9635. while (1) {
  9636. struct btrfs_fs_info *fs_info = device->fs_info;
  9637. struct btrfs_transaction *trans;
  9638. u64 bytes;
  9639. ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
  9640. if (ret)
  9641. return ret;
  9642. down_read(&fs_info->commit_root_sem);
  9643. spin_lock(&fs_info->trans_lock);
  9644. trans = fs_info->running_transaction;
  9645. if (trans)
  9646. refcount_inc(&trans->use_count);
  9647. spin_unlock(&fs_info->trans_lock);
  9648. ret = find_free_dev_extent_start(trans, device, minlen, start,
  9649. &start, &len);
  9650. if (trans)
  9651. btrfs_put_transaction(trans);
  9652. if (ret) {
  9653. up_read(&fs_info->commit_root_sem);
  9654. mutex_unlock(&fs_info->chunk_mutex);
  9655. if (ret == -ENOSPC)
  9656. ret = 0;
  9657. break;
  9658. }
  9659. ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
  9660. up_read(&fs_info->commit_root_sem);
  9661. mutex_unlock(&fs_info->chunk_mutex);
  9662. if (ret)
  9663. break;
  9664. start += len;
  9665. *trimmed += bytes;
  9666. if (fatal_signal_pending(current)) {
  9667. ret = -ERESTARTSYS;
  9668. break;
  9669. }
  9670. cond_resched();
  9671. }
  9672. return ret;
  9673. }
  9674. int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
  9675. {
  9676. struct btrfs_block_group_cache *cache = NULL;
  9677. struct btrfs_device *device;
  9678. struct list_head *devices;
  9679. u64 group_trimmed;
  9680. u64 start;
  9681. u64 end;
  9682. u64 trimmed = 0;
  9683. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  9684. int ret = 0;
  9685. /*
  9686. * try to trim all FS space, our block group may start from non-zero.
  9687. */
  9688. if (range->len == total_bytes)
  9689. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  9690. else
  9691. cache = btrfs_lookup_block_group(fs_info, range->start);
  9692. while (cache) {
  9693. if (cache->key.objectid >= (range->start + range->len)) {
  9694. btrfs_put_block_group(cache);
  9695. break;
  9696. }
  9697. start = max(range->start, cache->key.objectid);
  9698. end = min(range->start + range->len,
  9699. cache->key.objectid + cache->key.offset);
  9700. if (end - start >= range->minlen) {
  9701. if (!block_group_cache_done(cache)) {
  9702. ret = cache_block_group(cache, 0);
  9703. if (ret) {
  9704. btrfs_put_block_group(cache);
  9705. break;
  9706. }
  9707. ret = wait_block_group_cache_done(cache);
  9708. if (ret) {
  9709. btrfs_put_block_group(cache);
  9710. break;
  9711. }
  9712. }
  9713. ret = btrfs_trim_block_group(cache,
  9714. &group_trimmed,
  9715. start,
  9716. end,
  9717. range->minlen);
  9718. trimmed += group_trimmed;
  9719. if (ret) {
  9720. btrfs_put_block_group(cache);
  9721. break;
  9722. }
  9723. }
  9724. cache = next_block_group(fs_info, cache);
  9725. }
  9726. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  9727. devices = &fs_info->fs_devices->alloc_list;
  9728. list_for_each_entry(device, devices, dev_alloc_list) {
  9729. ret = btrfs_trim_free_extents(device, range->minlen,
  9730. &group_trimmed);
  9731. if (ret)
  9732. break;
  9733. trimmed += group_trimmed;
  9734. }
  9735. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  9736. range->len = trimmed;
  9737. return ret;
  9738. }
  9739. /*
  9740. * btrfs_{start,end}_write_no_snapshotting() are similar to
  9741. * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
  9742. * data into the page cache through nocow before the subvolume is snapshoted,
  9743. * but flush the data into disk after the snapshot creation, or to prevent
  9744. * operations while snapshotting is ongoing and that cause the snapshot to be
  9745. * inconsistent (writes followed by expanding truncates for example).
  9746. */
  9747. void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
  9748. {
  9749. percpu_counter_dec(&root->subv_writers->counter);
  9750. cond_wake_up(&root->subv_writers->wait);
  9751. }
  9752. int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
  9753. {
  9754. if (atomic_read(&root->will_be_snapshotted))
  9755. return 0;
  9756. percpu_counter_inc(&root->subv_writers->counter);
  9757. /*
  9758. * Make sure counter is updated before we check for snapshot creation.
  9759. */
  9760. smp_mb();
  9761. if (atomic_read(&root->will_be_snapshotted)) {
  9762. btrfs_end_write_no_snapshotting(root);
  9763. return 0;
  9764. }
  9765. return 1;
  9766. }
  9767. void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
  9768. {
  9769. while (true) {
  9770. int ret;
  9771. ret = btrfs_start_write_no_snapshotting(root);
  9772. if (ret)
  9773. break;
  9774. wait_var_event(&root->will_be_snapshotted,
  9775. !atomic_read(&root->will_be_snapshotted));
  9776. }
  9777. }