extent-tree.c 257 KB

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