extent-tree.c 273 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166
  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_trans_handle *trans,
  74. struct btrfs_root *root, u64 bytenr,
  75. u64 num_bytes, int alloc);
  76. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  77. struct btrfs_root *root,
  78. struct btrfs_delayed_ref_node *node, u64 parent,
  79. u64 root_objectid, u64 owner_objectid,
  80. u64 owner_offset, int refs_to_drop,
  81. struct btrfs_delayed_extent_op *extra_op);
  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->caching_ctl) {
  284. spin_unlock(&cache->lock);
  285. return NULL;
  286. }
  287. ctl = cache->caching_ctl;
  288. atomic_inc(&ctl->count);
  289. spin_unlock(&cache->lock);
  290. return ctl;
  291. }
  292. static void put_caching_control(struct btrfs_caching_control *ctl)
  293. {
  294. if (atomic_dec_and_test(&ctl->count))
  295. kfree(ctl);
  296. }
  297. /*
  298. * this is only called by cache_block_group, since we could have freed extents
  299. * we need to check the pinned_extents for any extents that can't be used yet
  300. * since their free space will be released as soon as the transaction commits.
  301. */
  302. static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
  303. struct btrfs_fs_info *info, u64 start, u64 end)
  304. {
  305. u64 extent_start, extent_end, size, total_added = 0;
  306. int ret;
  307. while (start < end) {
  308. ret = find_first_extent_bit(info->pinned_extents, start,
  309. &extent_start, &extent_end,
  310. EXTENT_DIRTY | EXTENT_UPTODATE,
  311. NULL);
  312. if (ret)
  313. break;
  314. if (extent_start <= start) {
  315. start = extent_end + 1;
  316. } else if (extent_start > start && extent_start < end) {
  317. size = extent_start - start;
  318. total_added += size;
  319. ret = btrfs_add_free_space(block_group, start,
  320. size);
  321. BUG_ON(ret); /* -ENOMEM or logic error */
  322. start = extent_end + 1;
  323. } else {
  324. break;
  325. }
  326. }
  327. if (start < end) {
  328. size = end - start;
  329. total_added += size;
  330. ret = btrfs_add_free_space(block_group, start, size);
  331. BUG_ON(ret); /* -ENOMEM or logic error */
  332. }
  333. return total_added;
  334. }
  335. static noinline void caching_thread(struct btrfs_work *work)
  336. {
  337. struct btrfs_block_group_cache *block_group;
  338. struct btrfs_fs_info *fs_info;
  339. struct btrfs_caching_control *caching_ctl;
  340. struct btrfs_root *extent_root;
  341. struct btrfs_path *path;
  342. struct extent_buffer *leaf;
  343. struct btrfs_key key;
  344. u64 total_found = 0;
  345. u64 last = 0;
  346. u32 nritems;
  347. int ret = -ENOMEM;
  348. caching_ctl = container_of(work, struct btrfs_caching_control, work);
  349. block_group = caching_ctl->block_group;
  350. fs_info = block_group->fs_info;
  351. extent_root = fs_info->extent_root;
  352. path = btrfs_alloc_path();
  353. if (!path)
  354. goto out;
  355. last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
  356. /*
  357. * We don't want to deadlock with somebody trying to allocate a new
  358. * extent for the extent root while also trying to search the extent
  359. * root to add free space. So we skip locking and search the commit
  360. * root, since its read-only
  361. */
  362. path->skip_locking = 1;
  363. path->search_commit_root = 1;
  364. path->reada = 1;
  365. key.objectid = last;
  366. key.offset = 0;
  367. key.type = BTRFS_EXTENT_ITEM_KEY;
  368. again:
  369. mutex_lock(&caching_ctl->mutex);
  370. /* need to make sure the commit_root doesn't disappear */
  371. down_read(&fs_info->commit_root_sem);
  372. next:
  373. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  374. if (ret < 0)
  375. goto err;
  376. leaf = path->nodes[0];
  377. nritems = btrfs_header_nritems(leaf);
  378. while (1) {
  379. if (btrfs_fs_closing(fs_info) > 1) {
  380. last = (u64)-1;
  381. break;
  382. }
  383. if (path->slots[0] < nritems) {
  384. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  385. } else {
  386. ret = find_next_key(path, 0, &key);
  387. if (ret)
  388. break;
  389. if (need_resched() ||
  390. rwsem_is_contended(&fs_info->commit_root_sem)) {
  391. caching_ctl->progress = last;
  392. btrfs_release_path(path);
  393. up_read(&fs_info->commit_root_sem);
  394. mutex_unlock(&caching_ctl->mutex);
  395. cond_resched();
  396. goto again;
  397. }
  398. ret = btrfs_next_leaf(extent_root, path);
  399. if (ret < 0)
  400. goto err;
  401. if (ret)
  402. break;
  403. leaf = path->nodes[0];
  404. nritems = btrfs_header_nritems(leaf);
  405. continue;
  406. }
  407. if (key.objectid < last) {
  408. key.objectid = last;
  409. key.offset = 0;
  410. key.type = BTRFS_EXTENT_ITEM_KEY;
  411. caching_ctl->progress = last;
  412. btrfs_release_path(path);
  413. goto next;
  414. }
  415. if (key.objectid < block_group->key.objectid) {
  416. path->slots[0]++;
  417. continue;
  418. }
  419. if (key.objectid >= block_group->key.objectid +
  420. block_group->key.offset)
  421. break;
  422. if (key.type == BTRFS_EXTENT_ITEM_KEY ||
  423. key.type == BTRFS_METADATA_ITEM_KEY) {
  424. total_found += add_new_free_space(block_group,
  425. fs_info, last,
  426. key.objectid);
  427. if (key.type == BTRFS_METADATA_ITEM_KEY)
  428. last = key.objectid +
  429. fs_info->tree_root->nodesize;
  430. else
  431. last = key.objectid + key.offset;
  432. if (total_found > (1024 * 1024 * 2)) {
  433. total_found = 0;
  434. wake_up(&caching_ctl->wait);
  435. }
  436. }
  437. path->slots[0]++;
  438. }
  439. ret = 0;
  440. total_found += add_new_free_space(block_group, fs_info, last,
  441. block_group->key.objectid +
  442. block_group->key.offset);
  443. caching_ctl->progress = (u64)-1;
  444. spin_lock(&block_group->lock);
  445. block_group->caching_ctl = NULL;
  446. block_group->cached = BTRFS_CACHE_FINISHED;
  447. spin_unlock(&block_group->lock);
  448. err:
  449. btrfs_free_path(path);
  450. up_read(&fs_info->commit_root_sem);
  451. free_excluded_extents(extent_root, block_group);
  452. mutex_unlock(&caching_ctl->mutex);
  453. out:
  454. if (ret) {
  455. spin_lock(&block_group->lock);
  456. block_group->caching_ctl = NULL;
  457. block_group->cached = BTRFS_CACHE_ERROR;
  458. spin_unlock(&block_group->lock);
  459. }
  460. wake_up(&caching_ctl->wait);
  461. put_caching_control(caching_ctl);
  462. btrfs_put_block_group(block_group);
  463. }
  464. static int cache_block_group(struct btrfs_block_group_cache *cache,
  465. int load_cache_only)
  466. {
  467. DEFINE_WAIT(wait);
  468. struct btrfs_fs_info *fs_info = cache->fs_info;
  469. struct btrfs_caching_control *caching_ctl;
  470. int ret = 0;
  471. caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
  472. if (!caching_ctl)
  473. return -ENOMEM;
  474. INIT_LIST_HEAD(&caching_ctl->list);
  475. mutex_init(&caching_ctl->mutex);
  476. init_waitqueue_head(&caching_ctl->wait);
  477. caching_ctl->block_group = cache;
  478. caching_ctl->progress = cache->key.objectid;
  479. atomic_set(&caching_ctl->count, 1);
  480. btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
  481. caching_thread, NULL, NULL);
  482. spin_lock(&cache->lock);
  483. /*
  484. * This should be a rare occasion, but this could happen I think in the
  485. * case where one thread starts to load the space cache info, and then
  486. * some other thread starts a transaction commit which tries to do an
  487. * allocation while the other thread is still loading the space cache
  488. * info. The previous loop should have kept us from choosing this block
  489. * group, but if we've moved to the state where we will wait on caching
  490. * block groups we need to first check if we're doing a fast load here,
  491. * so we can wait for it to finish, otherwise we could end up allocating
  492. * from a block group who's cache gets evicted for one reason or
  493. * another.
  494. */
  495. while (cache->cached == BTRFS_CACHE_FAST) {
  496. struct btrfs_caching_control *ctl;
  497. ctl = cache->caching_ctl;
  498. atomic_inc(&ctl->count);
  499. prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
  500. spin_unlock(&cache->lock);
  501. schedule();
  502. finish_wait(&ctl->wait, &wait);
  503. put_caching_control(ctl);
  504. spin_lock(&cache->lock);
  505. }
  506. if (cache->cached != BTRFS_CACHE_NO) {
  507. spin_unlock(&cache->lock);
  508. kfree(caching_ctl);
  509. return 0;
  510. }
  511. WARN_ON(cache->caching_ctl);
  512. cache->caching_ctl = caching_ctl;
  513. cache->cached = BTRFS_CACHE_FAST;
  514. spin_unlock(&cache->lock);
  515. if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
  516. mutex_lock(&caching_ctl->mutex);
  517. ret = load_free_space_cache(fs_info, cache);
  518. spin_lock(&cache->lock);
  519. if (ret == 1) {
  520. cache->caching_ctl = NULL;
  521. cache->cached = BTRFS_CACHE_FINISHED;
  522. cache->last_byte_to_unpin = (u64)-1;
  523. caching_ctl->progress = (u64)-1;
  524. } else {
  525. if (load_cache_only) {
  526. cache->caching_ctl = NULL;
  527. cache->cached = BTRFS_CACHE_NO;
  528. } else {
  529. cache->cached = BTRFS_CACHE_STARTED;
  530. cache->has_caching_ctl = 1;
  531. }
  532. }
  533. spin_unlock(&cache->lock);
  534. mutex_unlock(&caching_ctl->mutex);
  535. wake_up(&caching_ctl->wait);
  536. if (ret == 1) {
  537. put_caching_control(caching_ctl);
  538. free_excluded_extents(fs_info->extent_root, cache);
  539. return 0;
  540. }
  541. } else {
  542. /*
  543. * We are not going to do the fast caching, set cached to the
  544. * appropriate value and wakeup any waiters.
  545. */
  546. spin_lock(&cache->lock);
  547. if (load_cache_only) {
  548. cache->caching_ctl = NULL;
  549. cache->cached = BTRFS_CACHE_NO;
  550. } else {
  551. cache->cached = BTRFS_CACHE_STARTED;
  552. cache->has_caching_ctl = 1;
  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. 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. btrfs_put_bbio(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. struct btrfs_delayed_ref_node *node,
  1768. u64 parent, u64 root_objectid,
  1769. u64 owner, u64 offset, int refs_to_add,
  1770. struct btrfs_delayed_extent_op *extent_op)
  1771. {
  1772. struct btrfs_fs_info *fs_info = root->fs_info;
  1773. struct btrfs_path *path;
  1774. struct extent_buffer *leaf;
  1775. struct btrfs_extent_item *item;
  1776. struct btrfs_key key;
  1777. u64 bytenr = node->bytenr;
  1778. u64 num_bytes = node->num_bytes;
  1779. u64 refs;
  1780. int ret;
  1781. int no_quota = node->no_quota;
  1782. path = btrfs_alloc_path();
  1783. if (!path)
  1784. return -ENOMEM;
  1785. if (!is_fstree(root_objectid) || !root->fs_info->quota_enabled)
  1786. no_quota = 1;
  1787. path->reada = 1;
  1788. path->leave_spinning = 1;
  1789. /* this will setup the path even if it fails to insert the back ref */
  1790. ret = insert_inline_extent_backref(trans, fs_info->extent_root, path,
  1791. bytenr, num_bytes, parent,
  1792. root_objectid, owner, offset,
  1793. refs_to_add, extent_op);
  1794. if ((ret < 0 && ret != -EAGAIN) || !ret)
  1795. goto out;
  1796. /*
  1797. * Ok we had -EAGAIN which means we didn't have space to insert and
  1798. * inline extent ref, so just update the reference count and add a
  1799. * normal backref.
  1800. */
  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], struct btrfs_extent_item);
  1804. refs = btrfs_extent_refs(leaf, item);
  1805. btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
  1806. if (extent_op)
  1807. __run_delayed_extent_op(extent_op, leaf, item);
  1808. btrfs_mark_buffer_dirty(leaf);
  1809. btrfs_release_path(path);
  1810. path->reada = 1;
  1811. path->leave_spinning = 1;
  1812. /* now insert the actual backref */
  1813. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  1814. path, bytenr, parent, root_objectid,
  1815. owner, offset, refs_to_add);
  1816. if (ret)
  1817. btrfs_abort_transaction(trans, root, ret);
  1818. out:
  1819. btrfs_free_path(path);
  1820. return ret;
  1821. }
  1822. static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
  1823. struct btrfs_root *root,
  1824. struct btrfs_delayed_ref_node *node,
  1825. struct btrfs_delayed_extent_op *extent_op,
  1826. int insert_reserved)
  1827. {
  1828. int ret = 0;
  1829. struct btrfs_delayed_data_ref *ref;
  1830. struct btrfs_key ins;
  1831. u64 parent = 0;
  1832. u64 ref_root = 0;
  1833. u64 flags = 0;
  1834. ins.objectid = node->bytenr;
  1835. ins.offset = node->num_bytes;
  1836. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1837. ref = btrfs_delayed_node_to_data_ref(node);
  1838. trace_run_delayed_data_ref(node, ref, node->action);
  1839. if (node->type == BTRFS_SHARED_DATA_REF_KEY)
  1840. parent = ref->parent;
  1841. ref_root = ref->root;
  1842. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1843. if (extent_op)
  1844. flags |= extent_op->flags_to_set;
  1845. ret = alloc_reserved_file_extent(trans, root,
  1846. parent, ref_root, flags,
  1847. ref->objectid, ref->offset,
  1848. &ins, node->ref_mod);
  1849. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1850. ret = __btrfs_inc_extent_ref(trans, root, node, parent,
  1851. ref_root, ref->objectid,
  1852. ref->offset, node->ref_mod,
  1853. extent_op);
  1854. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  1855. ret = __btrfs_free_extent(trans, root, node, parent,
  1856. ref_root, ref->objectid,
  1857. ref->offset, node->ref_mod,
  1858. extent_op);
  1859. } else {
  1860. BUG();
  1861. }
  1862. return ret;
  1863. }
  1864. static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
  1865. struct extent_buffer *leaf,
  1866. struct btrfs_extent_item *ei)
  1867. {
  1868. u64 flags = btrfs_extent_flags(leaf, ei);
  1869. if (extent_op->update_flags) {
  1870. flags |= extent_op->flags_to_set;
  1871. btrfs_set_extent_flags(leaf, ei, flags);
  1872. }
  1873. if (extent_op->update_key) {
  1874. struct btrfs_tree_block_info *bi;
  1875. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
  1876. bi = (struct btrfs_tree_block_info *)(ei + 1);
  1877. btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
  1878. }
  1879. }
  1880. static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
  1881. struct btrfs_root *root,
  1882. struct btrfs_delayed_ref_node *node,
  1883. struct btrfs_delayed_extent_op *extent_op)
  1884. {
  1885. struct btrfs_key key;
  1886. struct btrfs_path *path;
  1887. struct btrfs_extent_item *ei;
  1888. struct extent_buffer *leaf;
  1889. u32 item_size;
  1890. int ret;
  1891. int err = 0;
  1892. int metadata = !extent_op->is_data;
  1893. if (trans->aborted)
  1894. return 0;
  1895. if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  1896. metadata = 0;
  1897. path = btrfs_alloc_path();
  1898. if (!path)
  1899. return -ENOMEM;
  1900. key.objectid = node->bytenr;
  1901. if (metadata) {
  1902. key.type = BTRFS_METADATA_ITEM_KEY;
  1903. key.offset = extent_op->level;
  1904. } else {
  1905. key.type = BTRFS_EXTENT_ITEM_KEY;
  1906. key.offset = node->num_bytes;
  1907. }
  1908. again:
  1909. path->reada = 1;
  1910. path->leave_spinning = 1;
  1911. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
  1912. path, 0, 1);
  1913. if (ret < 0) {
  1914. err = ret;
  1915. goto out;
  1916. }
  1917. if (ret > 0) {
  1918. if (metadata) {
  1919. if (path->slots[0] > 0) {
  1920. path->slots[0]--;
  1921. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1922. path->slots[0]);
  1923. if (key.objectid == node->bytenr &&
  1924. key.type == BTRFS_EXTENT_ITEM_KEY &&
  1925. key.offset == node->num_bytes)
  1926. ret = 0;
  1927. }
  1928. if (ret > 0) {
  1929. btrfs_release_path(path);
  1930. metadata = 0;
  1931. key.objectid = node->bytenr;
  1932. key.offset = node->num_bytes;
  1933. key.type = BTRFS_EXTENT_ITEM_KEY;
  1934. goto again;
  1935. }
  1936. } else {
  1937. err = -EIO;
  1938. goto out;
  1939. }
  1940. }
  1941. leaf = path->nodes[0];
  1942. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1943. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  1944. if (item_size < sizeof(*ei)) {
  1945. ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
  1946. path, (u64)-1, 0);
  1947. if (ret < 0) {
  1948. err = ret;
  1949. goto out;
  1950. }
  1951. leaf = path->nodes[0];
  1952. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1953. }
  1954. #endif
  1955. BUG_ON(item_size < sizeof(*ei));
  1956. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  1957. __run_delayed_extent_op(extent_op, leaf, ei);
  1958. btrfs_mark_buffer_dirty(leaf);
  1959. out:
  1960. btrfs_free_path(path);
  1961. return err;
  1962. }
  1963. static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
  1964. struct btrfs_root *root,
  1965. struct btrfs_delayed_ref_node *node,
  1966. struct btrfs_delayed_extent_op *extent_op,
  1967. int insert_reserved)
  1968. {
  1969. int ret = 0;
  1970. struct btrfs_delayed_tree_ref *ref;
  1971. struct btrfs_key ins;
  1972. u64 parent = 0;
  1973. u64 ref_root = 0;
  1974. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  1975. SKINNY_METADATA);
  1976. ref = btrfs_delayed_node_to_tree_ref(node);
  1977. trace_run_delayed_tree_ref(node, ref, node->action);
  1978. if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  1979. parent = ref->parent;
  1980. ref_root = ref->root;
  1981. ins.objectid = node->bytenr;
  1982. if (skinny_metadata) {
  1983. ins.offset = ref->level;
  1984. ins.type = BTRFS_METADATA_ITEM_KEY;
  1985. } else {
  1986. ins.offset = node->num_bytes;
  1987. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1988. }
  1989. BUG_ON(node->ref_mod != 1);
  1990. if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
  1991. BUG_ON(!extent_op || !extent_op->update_flags);
  1992. ret = alloc_reserved_tree_block(trans, root,
  1993. parent, ref_root,
  1994. extent_op->flags_to_set,
  1995. &extent_op->key,
  1996. ref->level, &ins,
  1997. node->no_quota);
  1998. } else if (node->action == BTRFS_ADD_DELAYED_REF) {
  1999. ret = __btrfs_inc_extent_ref(trans, root, node,
  2000. parent, ref_root,
  2001. ref->level, 0, 1,
  2002. extent_op);
  2003. } else if (node->action == BTRFS_DROP_DELAYED_REF) {
  2004. ret = __btrfs_free_extent(trans, root, node,
  2005. parent, ref_root,
  2006. ref->level, 0, 1, extent_op);
  2007. } else {
  2008. BUG();
  2009. }
  2010. return ret;
  2011. }
  2012. /* helper function to actually process a single delayed ref entry */
  2013. static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  2014. struct btrfs_root *root,
  2015. struct btrfs_delayed_ref_node *node,
  2016. struct btrfs_delayed_extent_op *extent_op,
  2017. int insert_reserved)
  2018. {
  2019. int ret = 0;
  2020. if (trans->aborted) {
  2021. if (insert_reserved)
  2022. btrfs_pin_extent(root, node->bytenr,
  2023. node->num_bytes, 1);
  2024. return 0;
  2025. }
  2026. if (btrfs_delayed_ref_is_head(node)) {
  2027. struct btrfs_delayed_ref_head *head;
  2028. /*
  2029. * we've hit the end of the chain and we were supposed
  2030. * to insert this extent into the tree. But, it got
  2031. * deleted before we ever needed to insert it, so all
  2032. * we have to do is clean up the accounting
  2033. */
  2034. BUG_ON(extent_op);
  2035. head = btrfs_delayed_node_to_head(node);
  2036. trace_run_delayed_ref_head(node, head, node->action);
  2037. if (insert_reserved) {
  2038. btrfs_pin_extent(root, node->bytenr,
  2039. node->num_bytes, 1);
  2040. if (head->is_data) {
  2041. ret = btrfs_del_csums(trans, root,
  2042. node->bytenr,
  2043. node->num_bytes);
  2044. }
  2045. }
  2046. return ret;
  2047. }
  2048. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  2049. node->type == BTRFS_SHARED_BLOCK_REF_KEY)
  2050. ret = run_delayed_tree_ref(trans, root, node, extent_op,
  2051. insert_reserved);
  2052. else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  2053. node->type == BTRFS_SHARED_DATA_REF_KEY)
  2054. ret = run_delayed_data_ref(trans, root, node, extent_op,
  2055. insert_reserved);
  2056. else
  2057. BUG();
  2058. return ret;
  2059. }
  2060. static inline struct btrfs_delayed_ref_node *
  2061. select_delayed_ref(struct btrfs_delayed_ref_head *head)
  2062. {
  2063. struct btrfs_delayed_ref_node *ref;
  2064. if (list_empty(&head->ref_list))
  2065. return NULL;
  2066. /*
  2067. * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
  2068. * This is to prevent a ref count from going down to zero, which deletes
  2069. * the extent item from the extent tree, when there still are references
  2070. * to add, which would fail because they would not find the extent item.
  2071. */
  2072. list_for_each_entry(ref, &head->ref_list, list) {
  2073. if (ref->action == BTRFS_ADD_DELAYED_REF)
  2074. return ref;
  2075. }
  2076. return list_entry(head->ref_list.next, struct btrfs_delayed_ref_node,
  2077. list);
  2078. }
  2079. /*
  2080. * Returns 0 on success or if called with an already aborted transaction.
  2081. * Returns -ENOMEM or -EIO on failure and will abort the transaction.
  2082. */
  2083. static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2084. struct btrfs_root *root,
  2085. unsigned long nr)
  2086. {
  2087. struct btrfs_delayed_ref_root *delayed_refs;
  2088. struct btrfs_delayed_ref_node *ref;
  2089. struct btrfs_delayed_ref_head *locked_ref = NULL;
  2090. struct btrfs_delayed_extent_op *extent_op;
  2091. struct btrfs_fs_info *fs_info = root->fs_info;
  2092. ktime_t start = ktime_get();
  2093. int ret;
  2094. unsigned long count = 0;
  2095. unsigned long actual_count = 0;
  2096. int must_insert_reserved = 0;
  2097. delayed_refs = &trans->transaction->delayed_refs;
  2098. while (1) {
  2099. if (!locked_ref) {
  2100. if (count >= nr)
  2101. break;
  2102. spin_lock(&delayed_refs->lock);
  2103. locked_ref = btrfs_select_ref_head(trans);
  2104. if (!locked_ref) {
  2105. spin_unlock(&delayed_refs->lock);
  2106. break;
  2107. }
  2108. /* grab the lock that says we are going to process
  2109. * all the refs for this head */
  2110. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  2111. spin_unlock(&delayed_refs->lock);
  2112. /*
  2113. * we may have dropped the spin lock to get the head
  2114. * mutex lock, and that might have given someone else
  2115. * time to free the head. If that's true, it has been
  2116. * removed from our list and we can move on.
  2117. */
  2118. if (ret == -EAGAIN) {
  2119. locked_ref = NULL;
  2120. count++;
  2121. continue;
  2122. }
  2123. }
  2124. spin_lock(&locked_ref->lock);
  2125. /*
  2126. * locked_ref is the head node, so we have to go one
  2127. * node back for any delayed ref updates
  2128. */
  2129. ref = select_delayed_ref(locked_ref);
  2130. if (ref && ref->seq &&
  2131. btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
  2132. spin_unlock(&locked_ref->lock);
  2133. btrfs_delayed_ref_unlock(locked_ref);
  2134. spin_lock(&delayed_refs->lock);
  2135. locked_ref->processing = 0;
  2136. delayed_refs->num_heads_ready++;
  2137. spin_unlock(&delayed_refs->lock);
  2138. locked_ref = NULL;
  2139. cond_resched();
  2140. count++;
  2141. continue;
  2142. }
  2143. /*
  2144. * record the must insert reserved flag before we
  2145. * drop the spin lock.
  2146. */
  2147. must_insert_reserved = locked_ref->must_insert_reserved;
  2148. locked_ref->must_insert_reserved = 0;
  2149. extent_op = locked_ref->extent_op;
  2150. locked_ref->extent_op = NULL;
  2151. if (!ref) {
  2152. /* All delayed refs have been processed, Go ahead
  2153. * and send the head node to run_one_delayed_ref,
  2154. * so that any accounting fixes can happen
  2155. */
  2156. ref = &locked_ref->node;
  2157. if (extent_op && must_insert_reserved) {
  2158. btrfs_free_delayed_extent_op(extent_op);
  2159. extent_op = NULL;
  2160. }
  2161. if (extent_op) {
  2162. spin_unlock(&locked_ref->lock);
  2163. ret = run_delayed_extent_op(trans, root,
  2164. ref, extent_op);
  2165. btrfs_free_delayed_extent_op(extent_op);
  2166. if (ret) {
  2167. /*
  2168. * Need to reset must_insert_reserved if
  2169. * there was an error so the abort stuff
  2170. * can cleanup the reserved space
  2171. * properly.
  2172. */
  2173. if (must_insert_reserved)
  2174. locked_ref->must_insert_reserved = 1;
  2175. locked_ref->processing = 0;
  2176. btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
  2177. btrfs_delayed_ref_unlock(locked_ref);
  2178. return ret;
  2179. }
  2180. continue;
  2181. }
  2182. /*
  2183. * Need to drop our head ref lock and re-aqcuire the
  2184. * delayed ref lock and then re-check to make sure
  2185. * nobody got added.
  2186. */
  2187. spin_unlock(&locked_ref->lock);
  2188. spin_lock(&delayed_refs->lock);
  2189. spin_lock(&locked_ref->lock);
  2190. if (!list_empty(&locked_ref->ref_list) ||
  2191. locked_ref->extent_op) {
  2192. spin_unlock(&locked_ref->lock);
  2193. spin_unlock(&delayed_refs->lock);
  2194. continue;
  2195. }
  2196. ref->in_tree = 0;
  2197. delayed_refs->num_heads--;
  2198. rb_erase(&locked_ref->href_node,
  2199. &delayed_refs->href_root);
  2200. spin_unlock(&delayed_refs->lock);
  2201. } else {
  2202. actual_count++;
  2203. ref->in_tree = 0;
  2204. list_del(&ref->list);
  2205. }
  2206. atomic_dec(&delayed_refs->num_entries);
  2207. if (!btrfs_delayed_ref_is_head(ref)) {
  2208. /*
  2209. * when we play the delayed ref, also correct the
  2210. * ref_mod on head
  2211. */
  2212. switch (ref->action) {
  2213. case BTRFS_ADD_DELAYED_REF:
  2214. case BTRFS_ADD_DELAYED_EXTENT:
  2215. locked_ref->node.ref_mod -= ref->ref_mod;
  2216. break;
  2217. case BTRFS_DROP_DELAYED_REF:
  2218. locked_ref->node.ref_mod += ref->ref_mod;
  2219. break;
  2220. default:
  2221. WARN_ON(1);
  2222. }
  2223. }
  2224. spin_unlock(&locked_ref->lock);
  2225. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  2226. must_insert_reserved);
  2227. btrfs_free_delayed_extent_op(extent_op);
  2228. if (ret) {
  2229. locked_ref->processing = 0;
  2230. btrfs_delayed_ref_unlock(locked_ref);
  2231. btrfs_put_delayed_ref(ref);
  2232. btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
  2233. return ret;
  2234. }
  2235. /*
  2236. * If this node is a head, that means all the refs in this head
  2237. * have been dealt with, and we will pick the next head to deal
  2238. * with, so we must unlock the head and drop it from the cluster
  2239. * list before we release it.
  2240. */
  2241. if (btrfs_delayed_ref_is_head(ref)) {
  2242. if (locked_ref->is_data &&
  2243. locked_ref->total_ref_mod < 0) {
  2244. spin_lock(&delayed_refs->lock);
  2245. delayed_refs->pending_csums -= ref->num_bytes;
  2246. spin_unlock(&delayed_refs->lock);
  2247. }
  2248. btrfs_delayed_ref_unlock(locked_ref);
  2249. locked_ref = NULL;
  2250. }
  2251. btrfs_put_delayed_ref(ref);
  2252. count++;
  2253. cond_resched();
  2254. }
  2255. /*
  2256. * We don't want to include ref heads since we can have empty ref heads
  2257. * and those will drastically skew our runtime down since we just do
  2258. * accounting, no actual extent tree updates.
  2259. */
  2260. if (actual_count > 0) {
  2261. u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
  2262. u64 avg;
  2263. /*
  2264. * We weigh the current average higher than our current runtime
  2265. * to avoid large swings in the average.
  2266. */
  2267. spin_lock(&delayed_refs->lock);
  2268. avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
  2269. fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
  2270. spin_unlock(&delayed_refs->lock);
  2271. }
  2272. return 0;
  2273. }
  2274. #ifdef SCRAMBLE_DELAYED_REFS
  2275. /*
  2276. * Normally delayed refs get processed in ascending bytenr order. This
  2277. * correlates in most cases to the order added. To expose dependencies on this
  2278. * order, we start to process the tree in the middle instead of the beginning
  2279. */
  2280. static u64 find_middle(struct rb_root *root)
  2281. {
  2282. struct rb_node *n = root->rb_node;
  2283. struct btrfs_delayed_ref_node *entry;
  2284. int alt = 1;
  2285. u64 middle;
  2286. u64 first = 0, last = 0;
  2287. n = rb_first(root);
  2288. if (n) {
  2289. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2290. first = entry->bytenr;
  2291. }
  2292. n = rb_last(root);
  2293. if (n) {
  2294. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2295. last = entry->bytenr;
  2296. }
  2297. n = root->rb_node;
  2298. while (n) {
  2299. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2300. WARN_ON(!entry->in_tree);
  2301. middle = entry->bytenr;
  2302. if (alt)
  2303. n = n->rb_left;
  2304. else
  2305. n = n->rb_right;
  2306. alt = 1 - alt;
  2307. }
  2308. return middle;
  2309. }
  2310. #endif
  2311. static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
  2312. {
  2313. u64 num_bytes;
  2314. num_bytes = heads * (sizeof(struct btrfs_extent_item) +
  2315. sizeof(struct btrfs_extent_inline_ref));
  2316. if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
  2317. num_bytes += heads * sizeof(struct btrfs_tree_block_info);
  2318. /*
  2319. * We don't ever fill up leaves all the way so multiply by 2 just to be
  2320. * closer to what we're really going to want to ouse.
  2321. */
  2322. return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
  2323. }
  2324. /*
  2325. * Takes the number of bytes to be csumm'ed and figures out how many leaves it
  2326. * would require to store the csums for that many bytes.
  2327. */
  2328. u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes)
  2329. {
  2330. u64 csum_size;
  2331. u64 num_csums_per_leaf;
  2332. u64 num_csums;
  2333. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  2334. num_csums_per_leaf = div64_u64(csum_size,
  2335. (u64)btrfs_super_csum_size(root->fs_info->super_copy));
  2336. num_csums = div64_u64(csum_bytes, root->sectorsize);
  2337. num_csums += num_csums_per_leaf - 1;
  2338. num_csums = div64_u64(num_csums, num_csums_per_leaf);
  2339. return num_csums;
  2340. }
  2341. int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
  2342. struct btrfs_root *root)
  2343. {
  2344. struct btrfs_block_rsv *global_rsv;
  2345. u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
  2346. u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
  2347. u64 num_dirty_bgs = trans->transaction->num_dirty_bgs;
  2348. u64 num_bytes, num_dirty_bgs_bytes;
  2349. int ret = 0;
  2350. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  2351. num_heads = heads_to_leaves(root, num_heads);
  2352. if (num_heads > 1)
  2353. num_bytes += (num_heads - 1) * root->nodesize;
  2354. num_bytes <<= 1;
  2355. num_bytes += btrfs_csum_bytes_to_leaves(root, csum_bytes) * root->nodesize;
  2356. num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(root,
  2357. num_dirty_bgs);
  2358. global_rsv = &root->fs_info->global_block_rsv;
  2359. /*
  2360. * If we can't allocate any more chunks lets make sure we have _lots_ of
  2361. * wiggle room since running delayed refs can create more delayed refs.
  2362. */
  2363. if (global_rsv->space_info->full) {
  2364. num_dirty_bgs_bytes <<= 1;
  2365. num_bytes <<= 1;
  2366. }
  2367. spin_lock(&global_rsv->lock);
  2368. if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
  2369. ret = 1;
  2370. spin_unlock(&global_rsv->lock);
  2371. return ret;
  2372. }
  2373. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
  2374. struct btrfs_root *root)
  2375. {
  2376. struct btrfs_fs_info *fs_info = root->fs_info;
  2377. u64 num_entries =
  2378. atomic_read(&trans->transaction->delayed_refs.num_entries);
  2379. u64 avg_runtime;
  2380. u64 val;
  2381. smp_mb();
  2382. avg_runtime = fs_info->avg_delayed_ref_runtime;
  2383. val = num_entries * avg_runtime;
  2384. if (num_entries * avg_runtime >= NSEC_PER_SEC)
  2385. return 1;
  2386. if (val >= NSEC_PER_SEC / 2)
  2387. return 2;
  2388. return btrfs_check_space_for_delayed_refs(trans, root);
  2389. }
  2390. struct async_delayed_refs {
  2391. struct btrfs_root *root;
  2392. int count;
  2393. int error;
  2394. int sync;
  2395. struct completion wait;
  2396. struct btrfs_work work;
  2397. };
  2398. static void delayed_ref_async_start(struct btrfs_work *work)
  2399. {
  2400. struct async_delayed_refs *async;
  2401. struct btrfs_trans_handle *trans;
  2402. int ret;
  2403. async = container_of(work, struct async_delayed_refs, work);
  2404. trans = btrfs_join_transaction(async->root);
  2405. if (IS_ERR(trans)) {
  2406. async->error = PTR_ERR(trans);
  2407. goto done;
  2408. }
  2409. /*
  2410. * trans->sync means that when we call end_transaciton, we won't
  2411. * wait on delayed refs
  2412. */
  2413. trans->sync = true;
  2414. ret = btrfs_run_delayed_refs(trans, async->root, async->count);
  2415. if (ret)
  2416. async->error = ret;
  2417. ret = btrfs_end_transaction(trans, async->root);
  2418. if (ret && !async->error)
  2419. async->error = ret;
  2420. done:
  2421. if (async->sync)
  2422. complete(&async->wait);
  2423. else
  2424. kfree(async);
  2425. }
  2426. int btrfs_async_run_delayed_refs(struct btrfs_root *root,
  2427. unsigned long count, int wait)
  2428. {
  2429. struct async_delayed_refs *async;
  2430. int ret;
  2431. async = kmalloc(sizeof(*async), GFP_NOFS);
  2432. if (!async)
  2433. return -ENOMEM;
  2434. async->root = root->fs_info->tree_root;
  2435. async->count = count;
  2436. async->error = 0;
  2437. if (wait)
  2438. async->sync = 1;
  2439. else
  2440. async->sync = 0;
  2441. init_completion(&async->wait);
  2442. btrfs_init_work(&async->work, btrfs_extent_refs_helper,
  2443. delayed_ref_async_start, NULL, NULL);
  2444. btrfs_queue_work(root->fs_info->extent_workers, &async->work);
  2445. if (wait) {
  2446. wait_for_completion(&async->wait);
  2447. ret = async->error;
  2448. kfree(async);
  2449. return ret;
  2450. }
  2451. return 0;
  2452. }
  2453. /*
  2454. * this starts processing the delayed reference count updates and
  2455. * extent insertions we have queued up so far. count can be
  2456. * 0, which means to process everything in the tree at the start
  2457. * of the run (but not newly added entries), or it can be some target
  2458. * number you'd like to process.
  2459. *
  2460. * Returns 0 on success or if called with an aborted transaction
  2461. * Returns <0 on error and aborts the transaction
  2462. */
  2463. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2464. struct btrfs_root *root, unsigned long count)
  2465. {
  2466. struct rb_node *node;
  2467. struct btrfs_delayed_ref_root *delayed_refs;
  2468. struct btrfs_delayed_ref_head *head;
  2469. int ret;
  2470. int run_all = count == (unsigned long)-1;
  2471. /* We'll clean this up in btrfs_cleanup_transaction */
  2472. if (trans->aborted)
  2473. return 0;
  2474. if (root == root->fs_info->extent_root)
  2475. root = root->fs_info->tree_root;
  2476. delayed_refs = &trans->transaction->delayed_refs;
  2477. if (count == 0)
  2478. count = atomic_read(&delayed_refs->num_entries) * 2;
  2479. again:
  2480. #ifdef SCRAMBLE_DELAYED_REFS
  2481. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2482. #endif
  2483. ret = __btrfs_run_delayed_refs(trans, root, count);
  2484. if (ret < 0) {
  2485. btrfs_abort_transaction(trans, root, ret);
  2486. return ret;
  2487. }
  2488. if (run_all) {
  2489. if (!list_empty(&trans->new_bgs))
  2490. btrfs_create_pending_block_groups(trans, root);
  2491. spin_lock(&delayed_refs->lock);
  2492. node = rb_first(&delayed_refs->href_root);
  2493. if (!node) {
  2494. spin_unlock(&delayed_refs->lock);
  2495. goto out;
  2496. }
  2497. count = (unsigned long)-1;
  2498. while (node) {
  2499. head = rb_entry(node, struct btrfs_delayed_ref_head,
  2500. href_node);
  2501. if (btrfs_delayed_ref_is_head(&head->node)) {
  2502. struct btrfs_delayed_ref_node *ref;
  2503. ref = &head->node;
  2504. atomic_inc(&ref->refs);
  2505. spin_unlock(&delayed_refs->lock);
  2506. /*
  2507. * Mutex was contended, block until it's
  2508. * released and try again
  2509. */
  2510. mutex_lock(&head->mutex);
  2511. mutex_unlock(&head->mutex);
  2512. btrfs_put_delayed_ref(ref);
  2513. cond_resched();
  2514. goto again;
  2515. } else {
  2516. WARN_ON(1);
  2517. }
  2518. node = rb_next(node);
  2519. }
  2520. spin_unlock(&delayed_refs->lock);
  2521. cond_resched();
  2522. goto again;
  2523. }
  2524. out:
  2525. assert_qgroups_uptodate(trans);
  2526. return 0;
  2527. }
  2528. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2529. struct btrfs_root *root,
  2530. u64 bytenr, u64 num_bytes, u64 flags,
  2531. int level, int is_data)
  2532. {
  2533. struct btrfs_delayed_extent_op *extent_op;
  2534. int ret;
  2535. extent_op = btrfs_alloc_delayed_extent_op();
  2536. if (!extent_op)
  2537. return -ENOMEM;
  2538. extent_op->flags_to_set = flags;
  2539. extent_op->update_flags = 1;
  2540. extent_op->update_key = 0;
  2541. extent_op->is_data = is_data ? 1 : 0;
  2542. extent_op->level = level;
  2543. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2544. num_bytes, extent_op);
  2545. if (ret)
  2546. btrfs_free_delayed_extent_op(extent_op);
  2547. return ret;
  2548. }
  2549. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2550. struct btrfs_root *root,
  2551. struct btrfs_path *path,
  2552. u64 objectid, u64 offset, u64 bytenr)
  2553. {
  2554. struct btrfs_delayed_ref_head *head;
  2555. struct btrfs_delayed_ref_node *ref;
  2556. struct btrfs_delayed_data_ref *data_ref;
  2557. struct btrfs_delayed_ref_root *delayed_refs;
  2558. int ret = 0;
  2559. delayed_refs = &trans->transaction->delayed_refs;
  2560. spin_lock(&delayed_refs->lock);
  2561. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2562. if (!head) {
  2563. spin_unlock(&delayed_refs->lock);
  2564. return 0;
  2565. }
  2566. if (!mutex_trylock(&head->mutex)) {
  2567. atomic_inc(&head->node.refs);
  2568. spin_unlock(&delayed_refs->lock);
  2569. btrfs_release_path(path);
  2570. /*
  2571. * Mutex was contended, block until it's released and let
  2572. * caller try again
  2573. */
  2574. mutex_lock(&head->mutex);
  2575. mutex_unlock(&head->mutex);
  2576. btrfs_put_delayed_ref(&head->node);
  2577. return -EAGAIN;
  2578. }
  2579. spin_unlock(&delayed_refs->lock);
  2580. spin_lock(&head->lock);
  2581. list_for_each_entry(ref, &head->ref_list, list) {
  2582. /* If it's a shared ref we know a cross reference exists */
  2583. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
  2584. ret = 1;
  2585. break;
  2586. }
  2587. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2588. /*
  2589. * If our ref doesn't match the one we're currently looking at
  2590. * then we have a cross reference.
  2591. */
  2592. if (data_ref->root != root->root_key.objectid ||
  2593. data_ref->objectid != objectid ||
  2594. data_ref->offset != offset) {
  2595. ret = 1;
  2596. break;
  2597. }
  2598. }
  2599. spin_unlock(&head->lock);
  2600. mutex_unlock(&head->mutex);
  2601. return ret;
  2602. }
  2603. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2604. struct btrfs_root *root,
  2605. struct btrfs_path *path,
  2606. u64 objectid, u64 offset, u64 bytenr)
  2607. {
  2608. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2609. struct extent_buffer *leaf;
  2610. struct btrfs_extent_data_ref *ref;
  2611. struct btrfs_extent_inline_ref *iref;
  2612. struct btrfs_extent_item *ei;
  2613. struct btrfs_key key;
  2614. u32 item_size;
  2615. int ret;
  2616. key.objectid = bytenr;
  2617. key.offset = (u64)-1;
  2618. key.type = BTRFS_EXTENT_ITEM_KEY;
  2619. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2620. if (ret < 0)
  2621. goto out;
  2622. BUG_ON(ret == 0); /* Corruption */
  2623. ret = -ENOENT;
  2624. if (path->slots[0] == 0)
  2625. goto out;
  2626. path->slots[0]--;
  2627. leaf = path->nodes[0];
  2628. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2629. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2630. goto out;
  2631. ret = 1;
  2632. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2633. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2634. if (item_size < sizeof(*ei)) {
  2635. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2636. goto out;
  2637. }
  2638. #endif
  2639. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2640. if (item_size != sizeof(*ei) +
  2641. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2642. goto out;
  2643. if (btrfs_extent_generation(leaf, ei) <=
  2644. btrfs_root_last_snapshot(&root->root_item))
  2645. goto out;
  2646. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2647. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2648. BTRFS_EXTENT_DATA_REF_KEY)
  2649. goto out;
  2650. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2651. if (btrfs_extent_refs(leaf, ei) !=
  2652. btrfs_extent_data_ref_count(leaf, ref) ||
  2653. btrfs_extent_data_ref_root(leaf, ref) !=
  2654. root->root_key.objectid ||
  2655. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2656. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2657. goto out;
  2658. ret = 0;
  2659. out:
  2660. return ret;
  2661. }
  2662. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2663. struct btrfs_root *root,
  2664. u64 objectid, u64 offset, u64 bytenr)
  2665. {
  2666. struct btrfs_path *path;
  2667. int ret;
  2668. int ret2;
  2669. path = btrfs_alloc_path();
  2670. if (!path)
  2671. return -ENOENT;
  2672. do {
  2673. ret = check_committed_ref(trans, root, path, objectid,
  2674. offset, bytenr);
  2675. if (ret && ret != -ENOENT)
  2676. goto out;
  2677. ret2 = check_delayed_ref(trans, root, path, objectid,
  2678. offset, bytenr);
  2679. } while (ret2 == -EAGAIN);
  2680. if (ret2 && ret2 != -ENOENT) {
  2681. ret = ret2;
  2682. goto out;
  2683. }
  2684. if (ret != -ENOENT || ret2 != -ENOENT)
  2685. ret = 0;
  2686. out:
  2687. btrfs_free_path(path);
  2688. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2689. WARN_ON(ret > 0);
  2690. return ret;
  2691. }
  2692. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2693. struct btrfs_root *root,
  2694. struct extent_buffer *buf,
  2695. int full_backref, int inc)
  2696. {
  2697. u64 bytenr;
  2698. u64 num_bytes;
  2699. u64 parent;
  2700. u64 ref_root;
  2701. u32 nritems;
  2702. struct btrfs_key key;
  2703. struct btrfs_file_extent_item *fi;
  2704. int i;
  2705. int level;
  2706. int ret = 0;
  2707. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2708. u64, u64, u64, u64, u64, u64, int);
  2709. if (btrfs_test_is_dummy_root(root))
  2710. return 0;
  2711. ref_root = btrfs_header_owner(buf);
  2712. nritems = btrfs_header_nritems(buf);
  2713. level = btrfs_header_level(buf);
  2714. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
  2715. return 0;
  2716. if (inc)
  2717. process_func = btrfs_inc_extent_ref;
  2718. else
  2719. process_func = btrfs_free_extent;
  2720. if (full_backref)
  2721. parent = buf->start;
  2722. else
  2723. parent = 0;
  2724. for (i = 0; i < nritems; i++) {
  2725. if (level == 0) {
  2726. btrfs_item_key_to_cpu(buf, &key, i);
  2727. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2728. continue;
  2729. fi = btrfs_item_ptr(buf, i,
  2730. struct btrfs_file_extent_item);
  2731. if (btrfs_file_extent_type(buf, fi) ==
  2732. BTRFS_FILE_EXTENT_INLINE)
  2733. continue;
  2734. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2735. if (bytenr == 0)
  2736. continue;
  2737. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2738. key.offset -= btrfs_file_extent_offset(buf, fi);
  2739. ret = process_func(trans, root, bytenr, num_bytes,
  2740. parent, ref_root, key.objectid,
  2741. key.offset, 1);
  2742. if (ret)
  2743. goto fail;
  2744. } else {
  2745. bytenr = btrfs_node_blockptr(buf, i);
  2746. num_bytes = root->nodesize;
  2747. ret = process_func(trans, root, bytenr, num_bytes,
  2748. parent, ref_root, level - 1, 0,
  2749. 1);
  2750. if (ret)
  2751. goto fail;
  2752. }
  2753. }
  2754. return 0;
  2755. fail:
  2756. return ret;
  2757. }
  2758. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2759. struct extent_buffer *buf, int full_backref)
  2760. {
  2761. return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
  2762. }
  2763. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2764. struct extent_buffer *buf, int full_backref)
  2765. {
  2766. return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
  2767. }
  2768. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2769. struct btrfs_root *root,
  2770. struct btrfs_path *path,
  2771. struct btrfs_block_group_cache *cache)
  2772. {
  2773. int ret;
  2774. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2775. unsigned long bi;
  2776. struct extent_buffer *leaf;
  2777. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2778. if (ret) {
  2779. if (ret > 0)
  2780. ret = -ENOENT;
  2781. goto fail;
  2782. }
  2783. leaf = path->nodes[0];
  2784. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2785. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2786. btrfs_mark_buffer_dirty(leaf);
  2787. fail:
  2788. btrfs_release_path(path);
  2789. return ret;
  2790. }
  2791. static struct btrfs_block_group_cache *
  2792. next_block_group(struct btrfs_root *root,
  2793. struct btrfs_block_group_cache *cache)
  2794. {
  2795. struct rb_node *node;
  2796. spin_lock(&root->fs_info->block_group_cache_lock);
  2797. /* If our block group was removed, we need a full search. */
  2798. if (RB_EMPTY_NODE(&cache->cache_node)) {
  2799. const u64 next_bytenr = cache->key.objectid + cache->key.offset;
  2800. spin_unlock(&root->fs_info->block_group_cache_lock);
  2801. btrfs_put_block_group(cache);
  2802. cache = btrfs_lookup_first_block_group(root->fs_info,
  2803. next_bytenr);
  2804. return cache;
  2805. }
  2806. node = rb_next(&cache->cache_node);
  2807. btrfs_put_block_group(cache);
  2808. if (node) {
  2809. cache = rb_entry(node, struct btrfs_block_group_cache,
  2810. cache_node);
  2811. btrfs_get_block_group(cache);
  2812. } else
  2813. cache = NULL;
  2814. spin_unlock(&root->fs_info->block_group_cache_lock);
  2815. return cache;
  2816. }
  2817. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2818. struct btrfs_trans_handle *trans,
  2819. struct btrfs_path *path)
  2820. {
  2821. struct btrfs_root *root = block_group->fs_info->tree_root;
  2822. struct inode *inode = NULL;
  2823. u64 alloc_hint = 0;
  2824. int dcs = BTRFS_DC_ERROR;
  2825. u64 num_pages = 0;
  2826. int retries = 0;
  2827. int ret = 0;
  2828. /*
  2829. * If this block group is smaller than 100 megs don't bother caching the
  2830. * block group.
  2831. */
  2832. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2833. spin_lock(&block_group->lock);
  2834. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2835. spin_unlock(&block_group->lock);
  2836. return 0;
  2837. }
  2838. if (trans->aborted)
  2839. return 0;
  2840. again:
  2841. inode = lookup_free_space_inode(root, block_group, path);
  2842. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2843. ret = PTR_ERR(inode);
  2844. btrfs_release_path(path);
  2845. goto out;
  2846. }
  2847. if (IS_ERR(inode)) {
  2848. BUG_ON(retries);
  2849. retries++;
  2850. if (block_group->ro)
  2851. goto out_free;
  2852. ret = create_free_space_inode(root, trans, block_group, path);
  2853. if (ret)
  2854. goto out_free;
  2855. goto again;
  2856. }
  2857. /* We've already setup this transaction, go ahead and exit */
  2858. if (block_group->cache_generation == trans->transid &&
  2859. i_size_read(inode)) {
  2860. dcs = BTRFS_DC_SETUP;
  2861. goto out_put;
  2862. }
  2863. /*
  2864. * We want to set the generation to 0, that way if anything goes wrong
  2865. * from here on out we know not to trust this cache when we load up next
  2866. * time.
  2867. */
  2868. BTRFS_I(inode)->generation = 0;
  2869. ret = btrfs_update_inode(trans, root, inode);
  2870. if (ret) {
  2871. /*
  2872. * So theoretically we could recover from this, simply set the
  2873. * super cache generation to 0 so we know to invalidate the
  2874. * cache, but then we'd have to keep track of the block groups
  2875. * that fail this way so we know we _have_ to reset this cache
  2876. * before the next commit or risk reading stale cache. So to
  2877. * limit our exposure to horrible edge cases lets just abort the
  2878. * transaction, this only happens in really bad situations
  2879. * anyway.
  2880. */
  2881. btrfs_abort_transaction(trans, root, ret);
  2882. goto out_put;
  2883. }
  2884. WARN_ON(ret);
  2885. if (i_size_read(inode) > 0) {
  2886. ret = btrfs_check_trunc_cache_free_space(root,
  2887. &root->fs_info->global_block_rsv);
  2888. if (ret)
  2889. goto out_put;
  2890. ret = btrfs_truncate_free_space_cache(root, trans, NULL, inode);
  2891. if (ret)
  2892. goto out_put;
  2893. }
  2894. spin_lock(&block_group->lock);
  2895. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  2896. !btrfs_test_opt(root, SPACE_CACHE)) {
  2897. /*
  2898. * don't bother trying to write stuff out _if_
  2899. * a) we're not cached,
  2900. * b) we're with nospace_cache mount option.
  2901. */
  2902. dcs = BTRFS_DC_WRITTEN;
  2903. spin_unlock(&block_group->lock);
  2904. goto out_put;
  2905. }
  2906. spin_unlock(&block_group->lock);
  2907. /*
  2908. * Try to preallocate enough space based on how big the block group is.
  2909. * Keep in mind this has to include any pinned space which could end up
  2910. * taking up quite a bit since it's not folded into the other space
  2911. * cache.
  2912. */
  2913. num_pages = div_u64(block_group->key.offset, 256 * 1024 * 1024);
  2914. if (!num_pages)
  2915. num_pages = 1;
  2916. num_pages *= 16;
  2917. num_pages *= PAGE_CACHE_SIZE;
  2918. ret = btrfs_check_data_free_space(inode, num_pages, num_pages);
  2919. if (ret)
  2920. goto out_put;
  2921. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2922. num_pages, num_pages,
  2923. &alloc_hint);
  2924. if (!ret)
  2925. dcs = BTRFS_DC_SETUP;
  2926. btrfs_free_reserved_data_space(inode, num_pages);
  2927. out_put:
  2928. iput(inode);
  2929. out_free:
  2930. btrfs_release_path(path);
  2931. out:
  2932. spin_lock(&block_group->lock);
  2933. if (!ret && dcs == BTRFS_DC_SETUP)
  2934. block_group->cache_generation = trans->transid;
  2935. block_group->disk_cache_state = dcs;
  2936. spin_unlock(&block_group->lock);
  2937. return ret;
  2938. }
  2939. int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
  2940. struct btrfs_root *root)
  2941. {
  2942. struct btrfs_block_group_cache *cache, *tmp;
  2943. struct btrfs_transaction *cur_trans = trans->transaction;
  2944. struct btrfs_path *path;
  2945. if (list_empty(&cur_trans->dirty_bgs) ||
  2946. !btrfs_test_opt(root, SPACE_CACHE))
  2947. return 0;
  2948. path = btrfs_alloc_path();
  2949. if (!path)
  2950. return -ENOMEM;
  2951. /* Could add new block groups, use _safe just in case */
  2952. list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
  2953. dirty_list) {
  2954. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2955. cache_save_setup(cache, trans, path);
  2956. }
  2957. btrfs_free_path(path);
  2958. return 0;
  2959. }
  2960. /*
  2961. * transaction commit does final block group cache writeback during a
  2962. * critical section where nothing is allowed to change the FS. This is
  2963. * required in order for the cache to actually match the block group,
  2964. * but can introduce a lot of latency into the commit.
  2965. *
  2966. * So, btrfs_start_dirty_block_groups is here to kick off block group
  2967. * cache IO. There's a chance we'll have to redo some of it if the
  2968. * block group changes again during the commit, but it greatly reduces
  2969. * the commit latency by getting rid of the easy block groups while
  2970. * we're still allowing others to join the commit.
  2971. */
  2972. int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
  2973. struct btrfs_root *root)
  2974. {
  2975. struct btrfs_block_group_cache *cache;
  2976. struct btrfs_transaction *cur_trans = trans->transaction;
  2977. int ret = 0;
  2978. int should_put;
  2979. struct btrfs_path *path = NULL;
  2980. LIST_HEAD(dirty);
  2981. struct list_head *io = &cur_trans->io_bgs;
  2982. int num_started = 0;
  2983. int loops = 0;
  2984. spin_lock(&cur_trans->dirty_bgs_lock);
  2985. if (list_empty(&cur_trans->dirty_bgs)) {
  2986. spin_unlock(&cur_trans->dirty_bgs_lock);
  2987. return 0;
  2988. }
  2989. list_splice_init(&cur_trans->dirty_bgs, &dirty);
  2990. spin_unlock(&cur_trans->dirty_bgs_lock);
  2991. again:
  2992. /*
  2993. * make sure all the block groups on our dirty list actually
  2994. * exist
  2995. */
  2996. btrfs_create_pending_block_groups(trans, root);
  2997. if (!path) {
  2998. path = btrfs_alloc_path();
  2999. if (!path)
  3000. return -ENOMEM;
  3001. }
  3002. /*
  3003. * cache_write_mutex is here only to save us from balance or automatic
  3004. * removal of empty block groups deleting this block group while we are
  3005. * writing out the cache
  3006. */
  3007. mutex_lock(&trans->transaction->cache_write_mutex);
  3008. while (!list_empty(&dirty)) {
  3009. cache = list_first_entry(&dirty,
  3010. struct btrfs_block_group_cache,
  3011. dirty_list);
  3012. /*
  3013. * this can happen if something re-dirties a block
  3014. * group that is already under IO. Just wait for it to
  3015. * finish and then do it all again
  3016. */
  3017. if (!list_empty(&cache->io_list)) {
  3018. list_del_init(&cache->io_list);
  3019. btrfs_wait_cache_io(root, trans, cache,
  3020. &cache->io_ctl, path,
  3021. cache->key.objectid);
  3022. btrfs_put_block_group(cache);
  3023. }
  3024. /*
  3025. * btrfs_wait_cache_io uses the cache->dirty_list to decide
  3026. * if it should update the cache_state. Don't delete
  3027. * until after we wait.
  3028. *
  3029. * Since we're not running in the commit critical section
  3030. * we need the dirty_bgs_lock to protect from update_block_group
  3031. */
  3032. spin_lock(&cur_trans->dirty_bgs_lock);
  3033. list_del_init(&cache->dirty_list);
  3034. spin_unlock(&cur_trans->dirty_bgs_lock);
  3035. should_put = 1;
  3036. cache_save_setup(cache, trans, path);
  3037. if (cache->disk_cache_state == BTRFS_DC_SETUP) {
  3038. cache->io_ctl.inode = NULL;
  3039. ret = btrfs_write_out_cache(root, trans, cache, path);
  3040. if (ret == 0 && cache->io_ctl.inode) {
  3041. num_started++;
  3042. should_put = 0;
  3043. /*
  3044. * the cache_write_mutex is protecting
  3045. * the io_list
  3046. */
  3047. list_add_tail(&cache->io_list, io);
  3048. } else {
  3049. /*
  3050. * if we failed to write the cache, the
  3051. * generation will be bad and life goes on
  3052. */
  3053. ret = 0;
  3054. }
  3055. }
  3056. if (!ret) {
  3057. ret = write_one_cache_group(trans, root, path, cache);
  3058. /*
  3059. * Our block group might still be attached to the list
  3060. * of new block groups in the transaction handle of some
  3061. * other task (struct btrfs_trans_handle->new_bgs). This
  3062. * means its block group item isn't yet in the extent
  3063. * tree. If this happens ignore the error, as we will
  3064. * try again later in the critical section of the
  3065. * transaction commit.
  3066. */
  3067. if (ret == -ENOENT) {
  3068. ret = 0;
  3069. spin_lock(&cur_trans->dirty_bgs_lock);
  3070. if (list_empty(&cache->dirty_list)) {
  3071. list_add_tail(&cache->dirty_list,
  3072. &cur_trans->dirty_bgs);
  3073. btrfs_get_block_group(cache);
  3074. }
  3075. spin_unlock(&cur_trans->dirty_bgs_lock);
  3076. } else if (ret) {
  3077. btrfs_abort_transaction(trans, root, ret);
  3078. }
  3079. }
  3080. /* if its not on the io list, we need to put the block group */
  3081. if (should_put)
  3082. btrfs_put_block_group(cache);
  3083. if (ret)
  3084. break;
  3085. /*
  3086. * Avoid blocking other tasks for too long. It might even save
  3087. * us from writing caches for block groups that are going to be
  3088. * removed.
  3089. */
  3090. mutex_unlock(&trans->transaction->cache_write_mutex);
  3091. mutex_lock(&trans->transaction->cache_write_mutex);
  3092. }
  3093. mutex_unlock(&trans->transaction->cache_write_mutex);
  3094. /*
  3095. * go through delayed refs for all the stuff we've just kicked off
  3096. * and then loop back (just once)
  3097. */
  3098. ret = btrfs_run_delayed_refs(trans, root, 0);
  3099. if (!ret && loops == 0) {
  3100. loops++;
  3101. spin_lock(&cur_trans->dirty_bgs_lock);
  3102. list_splice_init(&cur_trans->dirty_bgs, &dirty);
  3103. /*
  3104. * dirty_bgs_lock protects us from concurrent block group
  3105. * deletes too (not just cache_write_mutex).
  3106. */
  3107. if (!list_empty(&dirty)) {
  3108. spin_unlock(&cur_trans->dirty_bgs_lock);
  3109. goto again;
  3110. }
  3111. spin_unlock(&cur_trans->dirty_bgs_lock);
  3112. }
  3113. btrfs_free_path(path);
  3114. return ret;
  3115. }
  3116. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  3117. struct btrfs_root *root)
  3118. {
  3119. struct btrfs_block_group_cache *cache;
  3120. struct btrfs_transaction *cur_trans = trans->transaction;
  3121. int ret = 0;
  3122. int should_put;
  3123. struct btrfs_path *path;
  3124. struct list_head *io = &cur_trans->io_bgs;
  3125. int num_started = 0;
  3126. path = btrfs_alloc_path();
  3127. if (!path)
  3128. return -ENOMEM;
  3129. /*
  3130. * We don't need the lock here since we are protected by the transaction
  3131. * commit. We want to do the cache_save_setup first and then run the
  3132. * delayed refs to make sure we have the best chance at doing this all
  3133. * in one shot.
  3134. */
  3135. while (!list_empty(&cur_trans->dirty_bgs)) {
  3136. cache = list_first_entry(&cur_trans->dirty_bgs,
  3137. struct btrfs_block_group_cache,
  3138. dirty_list);
  3139. /*
  3140. * this can happen if cache_save_setup re-dirties a block
  3141. * group that is already under IO. Just wait for it to
  3142. * finish and then do it all again
  3143. */
  3144. if (!list_empty(&cache->io_list)) {
  3145. list_del_init(&cache->io_list);
  3146. btrfs_wait_cache_io(root, trans, cache,
  3147. &cache->io_ctl, path,
  3148. cache->key.objectid);
  3149. btrfs_put_block_group(cache);
  3150. }
  3151. /*
  3152. * don't remove from the dirty list until after we've waited
  3153. * on any pending IO
  3154. */
  3155. list_del_init(&cache->dirty_list);
  3156. should_put = 1;
  3157. cache_save_setup(cache, trans, path);
  3158. if (!ret)
  3159. ret = btrfs_run_delayed_refs(trans, root, (unsigned long) -1);
  3160. if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
  3161. cache->io_ctl.inode = NULL;
  3162. ret = btrfs_write_out_cache(root, trans, cache, path);
  3163. if (ret == 0 && cache->io_ctl.inode) {
  3164. num_started++;
  3165. should_put = 0;
  3166. list_add_tail(&cache->io_list, io);
  3167. } else {
  3168. /*
  3169. * if we failed to write the cache, the
  3170. * generation will be bad and life goes on
  3171. */
  3172. ret = 0;
  3173. }
  3174. }
  3175. if (!ret) {
  3176. ret = write_one_cache_group(trans, root, path, cache);
  3177. if (ret)
  3178. btrfs_abort_transaction(trans, root, ret);
  3179. }
  3180. /* if its not on the io list, we need to put the block group */
  3181. if (should_put)
  3182. btrfs_put_block_group(cache);
  3183. }
  3184. while (!list_empty(io)) {
  3185. cache = list_first_entry(io, struct btrfs_block_group_cache,
  3186. io_list);
  3187. list_del_init(&cache->io_list);
  3188. btrfs_wait_cache_io(root, trans, cache,
  3189. &cache->io_ctl, path, cache->key.objectid);
  3190. btrfs_put_block_group(cache);
  3191. }
  3192. btrfs_free_path(path);
  3193. return ret;
  3194. }
  3195. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  3196. {
  3197. struct btrfs_block_group_cache *block_group;
  3198. int readonly = 0;
  3199. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  3200. if (!block_group || block_group->ro)
  3201. readonly = 1;
  3202. if (block_group)
  3203. btrfs_put_block_group(block_group);
  3204. return readonly;
  3205. }
  3206. static const char *alloc_name(u64 flags)
  3207. {
  3208. switch (flags) {
  3209. case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
  3210. return "mixed";
  3211. case BTRFS_BLOCK_GROUP_METADATA:
  3212. return "metadata";
  3213. case BTRFS_BLOCK_GROUP_DATA:
  3214. return "data";
  3215. case BTRFS_BLOCK_GROUP_SYSTEM:
  3216. return "system";
  3217. default:
  3218. WARN_ON(1);
  3219. return "invalid-combination";
  3220. };
  3221. }
  3222. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  3223. u64 total_bytes, u64 bytes_used,
  3224. struct btrfs_space_info **space_info)
  3225. {
  3226. struct btrfs_space_info *found;
  3227. int i;
  3228. int factor;
  3229. int ret;
  3230. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  3231. BTRFS_BLOCK_GROUP_RAID10))
  3232. factor = 2;
  3233. else
  3234. factor = 1;
  3235. found = __find_space_info(info, flags);
  3236. if (found) {
  3237. spin_lock(&found->lock);
  3238. found->total_bytes += total_bytes;
  3239. found->disk_total += total_bytes * factor;
  3240. found->bytes_used += bytes_used;
  3241. found->disk_used += bytes_used * factor;
  3242. if (total_bytes > 0)
  3243. found->full = 0;
  3244. spin_unlock(&found->lock);
  3245. *space_info = found;
  3246. return 0;
  3247. }
  3248. found = kzalloc(sizeof(*found), GFP_NOFS);
  3249. if (!found)
  3250. return -ENOMEM;
  3251. ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL);
  3252. if (ret) {
  3253. kfree(found);
  3254. return ret;
  3255. }
  3256. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  3257. INIT_LIST_HEAD(&found->block_groups[i]);
  3258. init_rwsem(&found->groups_sem);
  3259. spin_lock_init(&found->lock);
  3260. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  3261. found->total_bytes = total_bytes;
  3262. found->disk_total = total_bytes * factor;
  3263. found->bytes_used = bytes_used;
  3264. found->disk_used = bytes_used * factor;
  3265. found->bytes_pinned = 0;
  3266. found->bytes_reserved = 0;
  3267. found->bytes_readonly = 0;
  3268. found->bytes_may_use = 0;
  3269. if (total_bytes > 0)
  3270. found->full = 0;
  3271. else
  3272. found->full = 1;
  3273. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3274. found->chunk_alloc = 0;
  3275. found->flush = 0;
  3276. init_waitqueue_head(&found->wait);
  3277. INIT_LIST_HEAD(&found->ro_bgs);
  3278. ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
  3279. info->space_info_kobj, "%s",
  3280. alloc_name(found->flags));
  3281. if (ret) {
  3282. kfree(found);
  3283. return ret;
  3284. }
  3285. *space_info = found;
  3286. list_add_rcu(&found->list, &info->space_info);
  3287. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3288. info->data_sinfo = found;
  3289. return ret;
  3290. }
  3291. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  3292. {
  3293. u64 extra_flags = chunk_to_extended(flags) &
  3294. BTRFS_EXTENDED_PROFILE_MASK;
  3295. write_seqlock(&fs_info->profiles_lock);
  3296. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3297. fs_info->avail_data_alloc_bits |= extra_flags;
  3298. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3299. fs_info->avail_metadata_alloc_bits |= extra_flags;
  3300. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3301. fs_info->avail_system_alloc_bits |= extra_flags;
  3302. write_sequnlock(&fs_info->profiles_lock);
  3303. }
  3304. /*
  3305. * returns target flags in extended format or 0 if restripe for this
  3306. * chunk_type is not in progress
  3307. *
  3308. * should be called with either volume_mutex or balance_lock held
  3309. */
  3310. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  3311. {
  3312. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3313. u64 target = 0;
  3314. if (!bctl)
  3315. return 0;
  3316. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  3317. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3318. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  3319. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  3320. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3321. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  3322. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  3323. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  3324. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  3325. }
  3326. return target;
  3327. }
  3328. /*
  3329. * @flags: available profiles in extended format (see ctree.h)
  3330. *
  3331. * Returns reduced profile in chunk format. If profile changing is in
  3332. * progress (either running or paused) picks the target profile (if it's
  3333. * already available), otherwise falls back to plain reducing.
  3334. */
  3335. static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  3336. {
  3337. u64 num_devices = root->fs_info->fs_devices->rw_devices;
  3338. u64 target;
  3339. u64 tmp;
  3340. /*
  3341. * see if restripe for this chunk_type is in progress, if so
  3342. * try to reduce to the target profile
  3343. */
  3344. spin_lock(&root->fs_info->balance_lock);
  3345. target = get_restripe_target(root->fs_info, flags);
  3346. if (target) {
  3347. /* pick target profile only if it's already available */
  3348. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  3349. spin_unlock(&root->fs_info->balance_lock);
  3350. return extended_to_chunk(target);
  3351. }
  3352. }
  3353. spin_unlock(&root->fs_info->balance_lock);
  3354. /* First, mask out the RAID levels which aren't possible */
  3355. if (num_devices == 1)
  3356. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
  3357. BTRFS_BLOCK_GROUP_RAID5);
  3358. if (num_devices < 3)
  3359. flags &= ~BTRFS_BLOCK_GROUP_RAID6;
  3360. if (num_devices < 4)
  3361. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  3362. tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  3363. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
  3364. BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
  3365. flags &= ~tmp;
  3366. if (tmp & BTRFS_BLOCK_GROUP_RAID6)
  3367. tmp = BTRFS_BLOCK_GROUP_RAID6;
  3368. else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
  3369. tmp = BTRFS_BLOCK_GROUP_RAID5;
  3370. else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
  3371. tmp = BTRFS_BLOCK_GROUP_RAID10;
  3372. else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
  3373. tmp = BTRFS_BLOCK_GROUP_RAID1;
  3374. else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
  3375. tmp = BTRFS_BLOCK_GROUP_RAID0;
  3376. return extended_to_chunk(flags | tmp);
  3377. }
  3378. static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
  3379. {
  3380. unsigned seq;
  3381. u64 flags;
  3382. do {
  3383. flags = orig_flags;
  3384. seq = read_seqbegin(&root->fs_info->profiles_lock);
  3385. if (flags & BTRFS_BLOCK_GROUP_DATA)
  3386. flags |= root->fs_info->avail_data_alloc_bits;
  3387. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  3388. flags |= root->fs_info->avail_system_alloc_bits;
  3389. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  3390. flags |= root->fs_info->avail_metadata_alloc_bits;
  3391. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  3392. return btrfs_reduce_alloc_profile(root, flags);
  3393. }
  3394. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  3395. {
  3396. u64 flags;
  3397. u64 ret;
  3398. if (data)
  3399. flags = BTRFS_BLOCK_GROUP_DATA;
  3400. else if (root == root->fs_info->chunk_root)
  3401. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  3402. else
  3403. flags = BTRFS_BLOCK_GROUP_METADATA;
  3404. ret = get_alloc_profile(root, flags);
  3405. return ret;
  3406. }
  3407. /*
  3408. * This will check the space that the inode allocates from to make sure we have
  3409. * enough space for bytes.
  3410. */
  3411. int btrfs_check_data_free_space(struct inode *inode, u64 bytes, u64 write_bytes)
  3412. {
  3413. struct btrfs_space_info *data_sinfo;
  3414. struct btrfs_root *root = BTRFS_I(inode)->root;
  3415. struct btrfs_fs_info *fs_info = root->fs_info;
  3416. u64 used;
  3417. int ret = 0;
  3418. int need_commit = 2;
  3419. int have_pinned_space;
  3420. /* make sure bytes are sectorsize aligned */
  3421. bytes = ALIGN(bytes, root->sectorsize);
  3422. if (btrfs_is_free_space_inode(inode)) {
  3423. need_commit = 0;
  3424. ASSERT(current->journal_info);
  3425. }
  3426. data_sinfo = fs_info->data_sinfo;
  3427. if (!data_sinfo)
  3428. goto alloc;
  3429. again:
  3430. /* make sure we have enough space to handle the data first */
  3431. spin_lock(&data_sinfo->lock);
  3432. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  3433. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  3434. data_sinfo->bytes_may_use;
  3435. if (used + bytes > data_sinfo->total_bytes) {
  3436. struct btrfs_trans_handle *trans;
  3437. /*
  3438. * if we don't have enough free bytes in this space then we need
  3439. * to alloc a new chunk.
  3440. */
  3441. if (!data_sinfo->full) {
  3442. u64 alloc_target;
  3443. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3444. spin_unlock(&data_sinfo->lock);
  3445. alloc:
  3446. alloc_target = btrfs_get_alloc_profile(root, 1);
  3447. /*
  3448. * It is ugly that we don't call nolock join
  3449. * transaction for the free space inode case here.
  3450. * But it is safe because we only do the data space
  3451. * reservation for the free space cache in the
  3452. * transaction context, the common join transaction
  3453. * just increase the counter of the current transaction
  3454. * handler, doesn't try to acquire the trans_lock of
  3455. * the fs.
  3456. */
  3457. trans = btrfs_join_transaction(root);
  3458. if (IS_ERR(trans))
  3459. return PTR_ERR(trans);
  3460. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3461. alloc_target,
  3462. CHUNK_ALLOC_NO_FORCE);
  3463. btrfs_end_transaction(trans, root);
  3464. if (ret < 0) {
  3465. if (ret != -ENOSPC)
  3466. return ret;
  3467. else {
  3468. have_pinned_space = 1;
  3469. goto commit_trans;
  3470. }
  3471. }
  3472. if (!data_sinfo)
  3473. data_sinfo = fs_info->data_sinfo;
  3474. goto again;
  3475. }
  3476. /*
  3477. * If we don't have enough pinned space to deal with this
  3478. * allocation, and no removed chunk in current transaction,
  3479. * don't bother committing the transaction.
  3480. */
  3481. have_pinned_space = percpu_counter_compare(
  3482. &data_sinfo->total_bytes_pinned,
  3483. used + bytes - data_sinfo->total_bytes);
  3484. spin_unlock(&data_sinfo->lock);
  3485. /* commit the current transaction and try again */
  3486. commit_trans:
  3487. if (need_commit &&
  3488. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  3489. need_commit--;
  3490. if (need_commit > 0)
  3491. btrfs_wait_ordered_roots(fs_info, -1);
  3492. trans = btrfs_join_transaction(root);
  3493. if (IS_ERR(trans))
  3494. return PTR_ERR(trans);
  3495. if (have_pinned_space >= 0 ||
  3496. trans->transaction->have_free_bgs ||
  3497. need_commit > 0) {
  3498. ret = btrfs_commit_transaction(trans, root);
  3499. if (ret)
  3500. return ret;
  3501. /*
  3502. * make sure that all running delayed iput are
  3503. * done
  3504. */
  3505. down_write(&root->fs_info->delayed_iput_sem);
  3506. up_write(&root->fs_info->delayed_iput_sem);
  3507. goto again;
  3508. } else {
  3509. btrfs_end_transaction(trans, root);
  3510. }
  3511. }
  3512. trace_btrfs_space_reservation(root->fs_info,
  3513. "space_info:enospc",
  3514. data_sinfo->flags, bytes, 1);
  3515. return -ENOSPC;
  3516. }
  3517. ret = btrfs_qgroup_reserve(root, write_bytes);
  3518. if (ret)
  3519. goto out;
  3520. data_sinfo->bytes_may_use += bytes;
  3521. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3522. data_sinfo->flags, bytes, 1);
  3523. out:
  3524. spin_unlock(&data_sinfo->lock);
  3525. return ret;
  3526. }
  3527. /*
  3528. * Called if we need to clear a data reservation for this inode.
  3529. */
  3530. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  3531. {
  3532. struct btrfs_root *root = BTRFS_I(inode)->root;
  3533. struct btrfs_space_info *data_sinfo;
  3534. /* make sure bytes are sectorsize aligned */
  3535. bytes = ALIGN(bytes, root->sectorsize);
  3536. data_sinfo = root->fs_info->data_sinfo;
  3537. spin_lock(&data_sinfo->lock);
  3538. WARN_ON(data_sinfo->bytes_may_use < bytes);
  3539. data_sinfo->bytes_may_use -= bytes;
  3540. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3541. data_sinfo->flags, bytes, 0);
  3542. spin_unlock(&data_sinfo->lock);
  3543. }
  3544. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3545. {
  3546. struct list_head *head = &info->space_info;
  3547. struct btrfs_space_info *found;
  3548. rcu_read_lock();
  3549. list_for_each_entry_rcu(found, head, list) {
  3550. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3551. found->force_alloc = CHUNK_ALLOC_FORCE;
  3552. }
  3553. rcu_read_unlock();
  3554. }
  3555. static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
  3556. {
  3557. return (global->size << 1);
  3558. }
  3559. static int should_alloc_chunk(struct btrfs_root *root,
  3560. struct btrfs_space_info *sinfo, int force)
  3561. {
  3562. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3563. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  3564. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  3565. u64 thresh;
  3566. if (force == CHUNK_ALLOC_FORCE)
  3567. return 1;
  3568. /*
  3569. * We need to take into account the global rsv because for all intents
  3570. * and purposes it's used space. Don't worry about locking the
  3571. * global_rsv, it doesn't change except when the transaction commits.
  3572. */
  3573. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3574. num_allocated += calc_global_rsv_need_space(global_rsv);
  3575. /*
  3576. * in limited mode, we want to have some free space up to
  3577. * about 1% of the FS size.
  3578. */
  3579. if (force == CHUNK_ALLOC_LIMITED) {
  3580. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3581. thresh = max_t(u64, 64 * 1024 * 1024,
  3582. div_factor_fine(thresh, 1));
  3583. if (num_bytes - num_allocated < thresh)
  3584. return 1;
  3585. }
  3586. if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
  3587. return 0;
  3588. return 1;
  3589. }
  3590. static u64 get_profile_num_devs(struct btrfs_root *root, u64 type)
  3591. {
  3592. u64 num_dev;
  3593. if (type & (BTRFS_BLOCK_GROUP_RAID10 |
  3594. BTRFS_BLOCK_GROUP_RAID0 |
  3595. BTRFS_BLOCK_GROUP_RAID5 |
  3596. BTRFS_BLOCK_GROUP_RAID6))
  3597. num_dev = root->fs_info->fs_devices->rw_devices;
  3598. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  3599. num_dev = 2;
  3600. else
  3601. num_dev = 1; /* DUP or single */
  3602. return num_dev;
  3603. }
  3604. /*
  3605. * If @is_allocation is true, reserve space in the system space info necessary
  3606. * for allocating a chunk, otherwise if it's false, reserve space necessary for
  3607. * removing a chunk.
  3608. */
  3609. void check_system_chunk(struct btrfs_trans_handle *trans,
  3610. struct btrfs_root *root,
  3611. u64 type)
  3612. {
  3613. struct btrfs_space_info *info;
  3614. u64 left;
  3615. u64 thresh;
  3616. int ret = 0;
  3617. u64 num_devs;
  3618. /*
  3619. * Needed because we can end up allocating a system chunk and for an
  3620. * atomic and race free space reservation in the chunk block reserve.
  3621. */
  3622. ASSERT(mutex_is_locked(&root->fs_info->chunk_mutex));
  3623. info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3624. spin_lock(&info->lock);
  3625. left = info->total_bytes - info->bytes_used - info->bytes_pinned -
  3626. info->bytes_reserved - info->bytes_readonly -
  3627. info->bytes_may_use;
  3628. spin_unlock(&info->lock);
  3629. num_devs = get_profile_num_devs(root, type);
  3630. /* num_devs device items to update and 1 chunk item to add or remove */
  3631. thresh = btrfs_calc_trunc_metadata_size(root, num_devs) +
  3632. btrfs_calc_trans_metadata_size(root, 1);
  3633. if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  3634. btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
  3635. left, thresh, type);
  3636. dump_space_info(info, 0, 0);
  3637. }
  3638. if (left < thresh) {
  3639. u64 flags;
  3640. flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
  3641. /*
  3642. * Ignore failure to create system chunk. We might end up not
  3643. * needing it, as we might not need to COW all nodes/leafs from
  3644. * the paths we visit in the chunk tree (they were already COWed
  3645. * or created in the current transaction for example).
  3646. */
  3647. ret = btrfs_alloc_chunk(trans, root, flags);
  3648. }
  3649. if (!ret) {
  3650. ret = btrfs_block_rsv_add(root->fs_info->chunk_root,
  3651. &root->fs_info->chunk_block_rsv,
  3652. thresh, BTRFS_RESERVE_NO_FLUSH);
  3653. if (!ret)
  3654. trans->chunk_bytes_reserved += thresh;
  3655. }
  3656. }
  3657. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3658. struct btrfs_root *extent_root, u64 flags, int force)
  3659. {
  3660. struct btrfs_space_info *space_info;
  3661. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3662. int wait_for_alloc = 0;
  3663. int ret = 0;
  3664. /* Don't re-enter if we're already allocating a chunk */
  3665. if (trans->allocating_chunk)
  3666. return -ENOSPC;
  3667. space_info = __find_space_info(extent_root->fs_info, flags);
  3668. if (!space_info) {
  3669. ret = update_space_info(extent_root->fs_info, flags,
  3670. 0, 0, &space_info);
  3671. BUG_ON(ret); /* -ENOMEM */
  3672. }
  3673. BUG_ON(!space_info); /* Logic error */
  3674. again:
  3675. spin_lock(&space_info->lock);
  3676. if (force < space_info->force_alloc)
  3677. force = space_info->force_alloc;
  3678. if (space_info->full) {
  3679. if (should_alloc_chunk(extent_root, space_info, force))
  3680. ret = -ENOSPC;
  3681. else
  3682. ret = 0;
  3683. spin_unlock(&space_info->lock);
  3684. return ret;
  3685. }
  3686. if (!should_alloc_chunk(extent_root, space_info, force)) {
  3687. spin_unlock(&space_info->lock);
  3688. return 0;
  3689. } else if (space_info->chunk_alloc) {
  3690. wait_for_alloc = 1;
  3691. } else {
  3692. space_info->chunk_alloc = 1;
  3693. }
  3694. spin_unlock(&space_info->lock);
  3695. mutex_lock(&fs_info->chunk_mutex);
  3696. /*
  3697. * The chunk_mutex is held throughout the entirety of a chunk
  3698. * allocation, so once we've acquired the chunk_mutex we know that the
  3699. * other guy is done and we need to recheck and see if we should
  3700. * allocate.
  3701. */
  3702. if (wait_for_alloc) {
  3703. mutex_unlock(&fs_info->chunk_mutex);
  3704. wait_for_alloc = 0;
  3705. goto again;
  3706. }
  3707. trans->allocating_chunk = true;
  3708. /*
  3709. * If we have mixed data/metadata chunks we want to make sure we keep
  3710. * allocating mixed chunks instead of individual chunks.
  3711. */
  3712. if (btrfs_mixed_space_info(space_info))
  3713. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3714. /*
  3715. * if we're doing a data chunk, go ahead and make sure that
  3716. * we keep a reasonable number of metadata chunks allocated in the
  3717. * FS as well.
  3718. */
  3719. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3720. fs_info->data_chunk_allocations++;
  3721. if (!(fs_info->data_chunk_allocations %
  3722. fs_info->metadata_ratio))
  3723. force_metadata_allocation(fs_info);
  3724. }
  3725. /*
  3726. * Check if we have enough space in SYSTEM chunk because we may need
  3727. * to update devices.
  3728. */
  3729. check_system_chunk(trans, extent_root, flags);
  3730. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3731. trans->allocating_chunk = false;
  3732. spin_lock(&space_info->lock);
  3733. if (ret < 0 && ret != -ENOSPC)
  3734. goto out;
  3735. if (ret)
  3736. space_info->full = 1;
  3737. else
  3738. ret = 1;
  3739. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3740. out:
  3741. space_info->chunk_alloc = 0;
  3742. spin_unlock(&space_info->lock);
  3743. mutex_unlock(&fs_info->chunk_mutex);
  3744. /*
  3745. * When we allocate a new chunk we reserve space in the chunk block
  3746. * reserve to make sure we can COW nodes/leafs in the chunk tree or
  3747. * add new nodes/leafs to it if we end up needing to do it when
  3748. * inserting the chunk item and updating device items as part of the
  3749. * second phase of chunk allocation, performed by
  3750. * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
  3751. * large number of new block groups to create in our transaction
  3752. * handle's new_bgs list to avoid exhausting the chunk block reserve
  3753. * in extreme cases - like having a single transaction create many new
  3754. * block groups when starting to write out the free space caches of all
  3755. * the block groups that were made dirty during the lifetime of the
  3756. * transaction.
  3757. */
  3758. if (trans->chunk_bytes_reserved >= (2 * 1024 * 1024ull)) {
  3759. btrfs_create_pending_block_groups(trans, trans->root);
  3760. btrfs_trans_release_chunk_metadata(trans);
  3761. }
  3762. return ret;
  3763. }
  3764. static int can_overcommit(struct btrfs_root *root,
  3765. struct btrfs_space_info *space_info, u64 bytes,
  3766. enum btrfs_reserve_flush_enum flush)
  3767. {
  3768. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3769. u64 profile = btrfs_get_alloc_profile(root, 0);
  3770. u64 space_size;
  3771. u64 avail;
  3772. u64 used;
  3773. used = space_info->bytes_used + space_info->bytes_reserved +
  3774. space_info->bytes_pinned + space_info->bytes_readonly;
  3775. /*
  3776. * We only want to allow over committing if we have lots of actual space
  3777. * free, but if we don't have enough space to handle the global reserve
  3778. * space then we could end up having a real enospc problem when trying
  3779. * to allocate a chunk or some other such important allocation.
  3780. */
  3781. spin_lock(&global_rsv->lock);
  3782. space_size = calc_global_rsv_need_space(global_rsv);
  3783. spin_unlock(&global_rsv->lock);
  3784. if (used + space_size >= space_info->total_bytes)
  3785. return 0;
  3786. used += space_info->bytes_may_use;
  3787. spin_lock(&root->fs_info->free_chunk_lock);
  3788. avail = root->fs_info->free_chunk_space;
  3789. spin_unlock(&root->fs_info->free_chunk_lock);
  3790. /*
  3791. * If we have dup, raid1 or raid10 then only half of the free
  3792. * space is actually useable. For raid56, the space info used
  3793. * doesn't include the parity drive, so we don't have to
  3794. * change the math
  3795. */
  3796. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3797. BTRFS_BLOCK_GROUP_RAID1 |
  3798. BTRFS_BLOCK_GROUP_RAID10))
  3799. avail >>= 1;
  3800. /*
  3801. * If we aren't flushing all things, let us overcommit up to
  3802. * 1/2th of the space. If we can flush, don't let us overcommit
  3803. * too much, let it overcommit up to 1/8 of the space.
  3804. */
  3805. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  3806. avail >>= 3;
  3807. else
  3808. avail >>= 1;
  3809. if (used + bytes < space_info->total_bytes + avail)
  3810. return 1;
  3811. return 0;
  3812. }
  3813. static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
  3814. unsigned long nr_pages, int nr_items)
  3815. {
  3816. struct super_block *sb = root->fs_info->sb;
  3817. if (down_read_trylock(&sb->s_umount)) {
  3818. writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
  3819. up_read(&sb->s_umount);
  3820. } else {
  3821. /*
  3822. * We needn't worry the filesystem going from r/w to r/o though
  3823. * we don't acquire ->s_umount mutex, because the filesystem
  3824. * should guarantee the delalloc inodes list be empty after
  3825. * the filesystem is readonly(all dirty pages are written to
  3826. * the disk).
  3827. */
  3828. btrfs_start_delalloc_roots(root->fs_info, 0, nr_items);
  3829. if (!current->journal_info)
  3830. btrfs_wait_ordered_roots(root->fs_info, nr_items);
  3831. }
  3832. }
  3833. static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
  3834. {
  3835. u64 bytes;
  3836. int nr;
  3837. bytes = btrfs_calc_trans_metadata_size(root, 1);
  3838. nr = (int)div64_u64(to_reclaim, bytes);
  3839. if (!nr)
  3840. nr = 1;
  3841. return nr;
  3842. }
  3843. #define EXTENT_SIZE_PER_ITEM (256 * 1024)
  3844. /*
  3845. * shrink metadata reservation for delalloc
  3846. */
  3847. static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
  3848. bool wait_ordered)
  3849. {
  3850. struct btrfs_block_rsv *block_rsv;
  3851. struct btrfs_space_info *space_info;
  3852. struct btrfs_trans_handle *trans;
  3853. u64 delalloc_bytes;
  3854. u64 max_reclaim;
  3855. long time_left;
  3856. unsigned long nr_pages;
  3857. int loops;
  3858. int items;
  3859. enum btrfs_reserve_flush_enum flush;
  3860. /* Calc the number of the pages we need flush for space reservation */
  3861. items = calc_reclaim_items_nr(root, to_reclaim);
  3862. to_reclaim = items * EXTENT_SIZE_PER_ITEM;
  3863. trans = (struct btrfs_trans_handle *)current->journal_info;
  3864. block_rsv = &root->fs_info->delalloc_block_rsv;
  3865. space_info = block_rsv->space_info;
  3866. delalloc_bytes = percpu_counter_sum_positive(
  3867. &root->fs_info->delalloc_bytes);
  3868. if (delalloc_bytes == 0) {
  3869. if (trans)
  3870. return;
  3871. if (wait_ordered)
  3872. btrfs_wait_ordered_roots(root->fs_info, items);
  3873. return;
  3874. }
  3875. loops = 0;
  3876. while (delalloc_bytes && loops < 3) {
  3877. max_reclaim = min(delalloc_bytes, to_reclaim);
  3878. nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
  3879. btrfs_writeback_inodes_sb_nr(root, nr_pages, items);
  3880. /*
  3881. * We need to wait for the async pages to actually start before
  3882. * we do anything.
  3883. */
  3884. max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
  3885. if (!max_reclaim)
  3886. goto skip_async;
  3887. if (max_reclaim <= nr_pages)
  3888. max_reclaim = 0;
  3889. else
  3890. max_reclaim -= nr_pages;
  3891. wait_event(root->fs_info->async_submit_wait,
  3892. atomic_read(&root->fs_info->async_delalloc_pages) <=
  3893. (int)max_reclaim);
  3894. skip_async:
  3895. if (!trans)
  3896. flush = BTRFS_RESERVE_FLUSH_ALL;
  3897. else
  3898. flush = BTRFS_RESERVE_NO_FLUSH;
  3899. spin_lock(&space_info->lock);
  3900. if (can_overcommit(root, space_info, orig, flush)) {
  3901. spin_unlock(&space_info->lock);
  3902. break;
  3903. }
  3904. spin_unlock(&space_info->lock);
  3905. loops++;
  3906. if (wait_ordered && !trans) {
  3907. btrfs_wait_ordered_roots(root->fs_info, items);
  3908. } else {
  3909. time_left = schedule_timeout_killable(1);
  3910. if (time_left)
  3911. break;
  3912. }
  3913. delalloc_bytes = percpu_counter_sum_positive(
  3914. &root->fs_info->delalloc_bytes);
  3915. }
  3916. }
  3917. /**
  3918. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3919. * @root - the root we're allocating for
  3920. * @bytes - the number of bytes we want to reserve
  3921. * @force - force the commit
  3922. *
  3923. * This will check to make sure that committing the transaction will actually
  3924. * get us somewhere and then commit the transaction if it does. Otherwise it
  3925. * will return -ENOSPC.
  3926. */
  3927. static int may_commit_transaction(struct btrfs_root *root,
  3928. struct btrfs_space_info *space_info,
  3929. u64 bytes, int force)
  3930. {
  3931. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3932. struct btrfs_trans_handle *trans;
  3933. trans = (struct btrfs_trans_handle *)current->journal_info;
  3934. if (trans)
  3935. return -EAGAIN;
  3936. if (force)
  3937. goto commit;
  3938. /* See if there is enough pinned space to make this reservation */
  3939. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3940. bytes) >= 0)
  3941. goto commit;
  3942. /*
  3943. * See if there is some space in the delayed insertion reservation for
  3944. * this reservation.
  3945. */
  3946. if (space_info != delayed_rsv->space_info)
  3947. return -ENOSPC;
  3948. spin_lock(&delayed_rsv->lock);
  3949. if (percpu_counter_compare(&space_info->total_bytes_pinned,
  3950. bytes - delayed_rsv->size) >= 0) {
  3951. spin_unlock(&delayed_rsv->lock);
  3952. return -ENOSPC;
  3953. }
  3954. spin_unlock(&delayed_rsv->lock);
  3955. commit:
  3956. trans = btrfs_join_transaction(root);
  3957. if (IS_ERR(trans))
  3958. return -ENOSPC;
  3959. return btrfs_commit_transaction(trans, root);
  3960. }
  3961. enum flush_state {
  3962. FLUSH_DELAYED_ITEMS_NR = 1,
  3963. FLUSH_DELAYED_ITEMS = 2,
  3964. FLUSH_DELALLOC = 3,
  3965. FLUSH_DELALLOC_WAIT = 4,
  3966. ALLOC_CHUNK = 5,
  3967. COMMIT_TRANS = 6,
  3968. };
  3969. static int flush_space(struct btrfs_root *root,
  3970. struct btrfs_space_info *space_info, u64 num_bytes,
  3971. u64 orig_bytes, int state)
  3972. {
  3973. struct btrfs_trans_handle *trans;
  3974. int nr;
  3975. int ret = 0;
  3976. switch (state) {
  3977. case FLUSH_DELAYED_ITEMS_NR:
  3978. case FLUSH_DELAYED_ITEMS:
  3979. if (state == FLUSH_DELAYED_ITEMS_NR)
  3980. nr = calc_reclaim_items_nr(root, num_bytes) * 2;
  3981. else
  3982. nr = -1;
  3983. trans = btrfs_join_transaction(root);
  3984. if (IS_ERR(trans)) {
  3985. ret = PTR_ERR(trans);
  3986. break;
  3987. }
  3988. ret = btrfs_run_delayed_items_nr(trans, root, nr);
  3989. btrfs_end_transaction(trans, root);
  3990. break;
  3991. case FLUSH_DELALLOC:
  3992. case FLUSH_DELALLOC_WAIT:
  3993. shrink_delalloc(root, num_bytes * 2, orig_bytes,
  3994. state == FLUSH_DELALLOC_WAIT);
  3995. break;
  3996. case ALLOC_CHUNK:
  3997. trans = btrfs_join_transaction(root);
  3998. if (IS_ERR(trans)) {
  3999. ret = PTR_ERR(trans);
  4000. break;
  4001. }
  4002. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  4003. btrfs_get_alloc_profile(root, 0),
  4004. CHUNK_ALLOC_NO_FORCE);
  4005. btrfs_end_transaction(trans, root);
  4006. if (ret == -ENOSPC)
  4007. ret = 0;
  4008. break;
  4009. case COMMIT_TRANS:
  4010. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  4011. break;
  4012. default:
  4013. ret = -ENOSPC;
  4014. break;
  4015. }
  4016. return ret;
  4017. }
  4018. static inline u64
  4019. btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
  4020. struct btrfs_space_info *space_info)
  4021. {
  4022. u64 used;
  4023. u64 expected;
  4024. u64 to_reclaim;
  4025. to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024,
  4026. 16 * 1024 * 1024);
  4027. spin_lock(&space_info->lock);
  4028. if (can_overcommit(root, space_info, to_reclaim,
  4029. BTRFS_RESERVE_FLUSH_ALL)) {
  4030. to_reclaim = 0;
  4031. goto out;
  4032. }
  4033. used = space_info->bytes_used + space_info->bytes_reserved +
  4034. space_info->bytes_pinned + space_info->bytes_readonly +
  4035. space_info->bytes_may_use;
  4036. if (can_overcommit(root, space_info, 1024 * 1024,
  4037. BTRFS_RESERVE_FLUSH_ALL))
  4038. expected = div_factor_fine(space_info->total_bytes, 95);
  4039. else
  4040. expected = div_factor_fine(space_info->total_bytes, 90);
  4041. if (used > expected)
  4042. to_reclaim = used - expected;
  4043. else
  4044. to_reclaim = 0;
  4045. to_reclaim = min(to_reclaim, space_info->bytes_may_use +
  4046. space_info->bytes_reserved);
  4047. out:
  4048. spin_unlock(&space_info->lock);
  4049. return to_reclaim;
  4050. }
  4051. static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
  4052. struct btrfs_fs_info *fs_info, u64 used)
  4053. {
  4054. u64 thresh = div_factor_fine(space_info->total_bytes, 98);
  4055. /* If we're just plain full then async reclaim just slows us down. */
  4056. if (space_info->bytes_used >= thresh)
  4057. return 0;
  4058. return (used >= thresh && !btrfs_fs_closing(fs_info) &&
  4059. !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
  4060. }
  4061. static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info,
  4062. struct btrfs_fs_info *fs_info,
  4063. int flush_state)
  4064. {
  4065. u64 used;
  4066. spin_lock(&space_info->lock);
  4067. /*
  4068. * We run out of space and have not got any free space via flush_space,
  4069. * so don't bother doing async reclaim.
  4070. */
  4071. if (flush_state > COMMIT_TRANS && space_info->full) {
  4072. spin_unlock(&space_info->lock);
  4073. return 0;
  4074. }
  4075. used = space_info->bytes_used + space_info->bytes_reserved +
  4076. space_info->bytes_pinned + space_info->bytes_readonly +
  4077. space_info->bytes_may_use;
  4078. if (need_do_async_reclaim(space_info, fs_info, used)) {
  4079. spin_unlock(&space_info->lock);
  4080. return 1;
  4081. }
  4082. spin_unlock(&space_info->lock);
  4083. return 0;
  4084. }
  4085. static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
  4086. {
  4087. struct btrfs_fs_info *fs_info;
  4088. struct btrfs_space_info *space_info;
  4089. u64 to_reclaim;
  4090. int flush_state;
  4091. fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
  4092. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4093. to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
  4094. space_info);
  4095. if (!to_reclaim)
  4096. return;
  4097. flush_state = FLUSH_DELAYED_ITEMS_NR;
  4098. do {
  4099. flush_space(fs_info->fs_root, space_info, to_reclaim,
  4100. to_reclaim, flush_state);
  4101. flush_state++;
  4102. if (!btrfs_need_do_async_reclaim(space_info, fs_info,
  4103. flush_state))
  4104. return;
  4105. } while (flush_state < COMMIT_TRANS);
  4106. }
  4107. void btrfs_init_async_reclaim_work(struct work_struct *work)
  4108. {
  4109. INIT_WORK(work, btrfs_async_reclaim_metadata_space);
  4110. }
  4111. /**
  4112. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  4113. * @root - the root we're allocating for
  4114. * @block_rsv - the block_rsv we're allocating for
  4115. * @orig_bytes - the number of bytes we want
  4116. * @flush - whether or not we can flush to make our reservation
  4117. *
  4118. * This will reserve orgi_bytes number of bytes from the space info associated
  4119. * with the block_rsv. If there is not enough space it will make an attempt to
  4120. * flush out space to make room. It will do this by flushing delalloc if
  4121. * possible or committing the transaction. If flush is 0 then no attempts to
  4122. * regain reservations will be made and this will fail if there is not enough
  4123. * space already.
  4124. */
  4125. static int reserve_metadata_bytes(struct btrfs_root *root,
  4126. struct btrfs_block_rsv *block_rsv,
  4127. u64 orig_bytes,
  4128. enum btrfs_reserve_flush_enum flush)
  4129. {
  4130. struct btrfs_space_info *space_info = block_rsv->space_info;
  4131. u64 used;
  4132. u64 num_bytes = orig_bytes;
  4133. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  4134. int ret = 0;
  4135. bool flushing = false;
  4136. again:
  4137. ret = 0;
  4138. spin_lock(&space_info->lock);
  4139. /*
  4140. * We only want to wait if somebody other than us is flushing and we
  4141. * are actually allowed to flush all things.
  4142. */
  4143. while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
  4144. space_info->flush) {
  4145. spin_unlock(&space_info->lock);
  4146. /*
  4147. * If we have a trans handle we can't wait because the flusher
  4148. * may have to commit the transaction, which would mean we would
  4149. * deadlock since we are waiting for the flusher to finish, but
  4150. * hold the current transaction open.
  4151. */
  4152. if (current->journal_info)
  4153. return -EAGAIN;
  4154. ret = wait_event_killable(space_info->wait, !space_info->flush);
  4155. /* Must have been killed, return */
  4156. if (ret)
  4157. return -EINTR;
  4158. spin_lock(&space_info->lock);
  4159. }
  4160. ret = -ENOSPC;
  4161. used = space_info->bytes_used + space_info->bytes_reserved +
  4162. space_info->bytes_pinned + space_info->bytes_readonly +
  4163. space_info->bytes_may_use;
  4164. /*
  4165. * The idea here is that we've not already over-reserved the block group
  4166. * then we can go ahead and save our reservation first and then start
  4167. * flushing if we need to. Otherwise if we've already overcommitted
  4168. * lets start flushing stuff first and then come back and try to make
  4169. * our reservation.
  4170. */
  4171. if (used <= space_info->total_bytes) {
  4172. if (used + orig_bytes <= space_info->total_bytes) {
  4173. space_info->bytes_may_use += orig_bytes;
  4174. trace_btrfs_space_reservation(root->fs_info,
  4175. "space_info", space_info->flags, orig_bytes, 1);
  4176. ret = 0;
  4177. } else {
  4178. /*
  4179. * Ok set num_bytes to orig_bytes since we aren't
  4180. * overocmmitted, this way we only try and reclaim what
  4181. * we need.
  4182. */
  4183. num_bytes = orig_bytes;
  4184. }
  4185. } else {
  4186. /*
  4187. * Ok we're over committed, set num_bytes to the overcommitted
  4188. * amount plus the amount of bytes that we need for this
  4189. * reservation.
  4190. */
  4191. num_bytes = used - space_info->total_bytes +
  4192. (orig_bytes * 2);
  4193. }
  4194. if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
  4195. space_info->bytes_may_use += orig_bytes;
  4196. trace_btrfs_space_reservation(root->fs_info, "space_info",
  4197. space_info->flags, orig_bytes,
  4198. 1);
  4199. ret = 0;
  4200. }
  4201. /*
  4202. * Couldn't make our reservation, save our place so while we're trying
  4203. * to reclaim space we can actually use it instead of somebody else
  4204. * stealing it from us.
  4205. *
  4206. * We make the other tasks wait for the flush only when we can flush
  4207. * all things.
  4208. */
  4209. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  4210. flushing = true;
  4211. space_info->flush = 1;
  4212. } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
  4213. used += orig_bytes;
  4214. /*
  4215. * We will do the space reservation dance during log replay,
  4216. * which means we won't have fs_info->fs_root set, so don't do
  4217. * the async reclaim as we will panic.
  4218. */
  4219. if (!root->fs_info->log_root_recovering &&
  4220. need_do_async_reclaim(space_info, root->fs_info, used) &&
  4221. !work_busy(&root->fs_info->async_reclaim_work))
  4222. queue_work(system_unbound_wq,
  4223. &root->fs_info->async_reclaim_work);
  4224. }
  4225. spin_unlock(&space_info->lock);
  4226. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  4227. goto out;
  4228. ret = flush_space(root, space_info, num_bytes, orig_bytes,
  4229. flush_state);
  4230. flush_state++;
  4231. /*
  4232. * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
  4233. * would happen. So skip delalloc flush.
  4234. */
  4235. if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  4236. (flush_state == FLUSH_DELALLOC ||
  4237. flush_state == FLUSH_DELALLOC_WAIT))
  4238. flush_state = ALLOC_CHUNK;
  4239. if (!ret)
  4240. goto again;
  4241. else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  4242. flush_state < COMMIT_TRANS)
  4243. goto again;
  4244. else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  4245. flush_state <= COMMIT_TRANS)
  4246. goto again;
  4247. out:
  4248. if (ret == -ENOSPC &&
  4249. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  4250. struct btrfs_block_rsv *global_rsv =
  4251. &root->fs_info->global_block_rsv;
  4252. if (block_rsv != global_rsv &&
  4253. !block_rsv_use_bytes(global_rsv, orig_bytes))
  4254. ret = 0;
  4255. }
  4256. if (ret == -ENOSPC)
  4257. trace_btrfs_space_reservation(root->fs_info,
  4258. "space_info:enospc",
  4259. space_info->flags, orig_bytes, 1);
  4260. if (flushing) {
  4261. spin_lock(&space_info->lock);
  4262. space_info->flush = 0;
  4263. wake_up_all(&space_info->wait);
  4264. spin_unlock(&space_info->lock);
  4265. }
  4266. return ret;
  4267. }
  4268. static struct btrfs_block_rsv *get_block_rsv(
  4269. const struct btrfs_trans_handle *trans,
  4270. const struct btrfs_root *root)
  4271. {
  4272. struct btrfs_block_rsv *block_rsv = NULL;
  4273. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  4274. block_rsv = trans->block_rsv;
  4275. if (root == root->fs_info->csum_root && trans->adding_csums)
  4276. block_rsv = trans->block_rsv;
  4277. if (root == root->fs_info->uuid_root)
  4278. block_rsv = trans->block_rsv;
  4279. if (!block_rsv)
  4280. block_rsv = root->block_rsv;
  4281. if (!block_rsv)
  4282. block_rsv = &root->fs_info->empty_block_rsv;
  4283. return block_rsv;
  4284. }
  4285. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  4286. u64 num_bytes)
  4287. {
  4288. int ret = -ENOSPC;
  4289. spin_lock(&block_rsv->lock);
  4290. if (block_rsv->reserved >= num_bytes) {
  4291. block_rsv->reserved -= num_bytes;
  4292. if (block_rsv->reserved < block_rsv->size)
  4293. block_rsv->full = 0;
  4294. ret = 0;
  4295. }
  4296. spin_unlock(&block_rsv->lock);
  4297. return ret;
  4298. }
  4299. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  4300. u64 num_bytes, int update_size)
  4301. {
  4302. spin_lock(&block_rsv->lock);
  4303. block_rsv->reserved += num_bytes;
  4304. if (update_size)
  4305. block_rsv->size += num_bytes;
  4306. else if (block_rsv->reserved >= block_rsv->size)
  4307. block_rsv->full = 1;
  4308. spin_unlock(&block_rsv->lock);
  4309. }
  4310. int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
  4311. struct btrfs_block_rsv *dest, u64 num_bytes,
  4312. int min_factor)
  4313. {
  4314. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4315. u64 min_bytes;
  4316. if (global_rsv->space_info != dest->space_info)
  4317. return -ENOSPC;
  4318. spin_lock(&global_rsv->lock);
  4319. min_bytes = div_factor(global_rsv->size, min_factor);
  4320. if (global_rsv->reserved < min_bytes + num_bytes) {
  4321. spin_unlock(&global_rsv->lock);
  4322. return -ENOSPC;
  4323. }
  4324. global_rsv->reserved -= num_bytes;
  4325. if (global_rsv->reserved < global_rsv->size)
  4326. global_rsv->full = 0;
  4327. spin_unlock(&global_rsv->lock);
  4328. block_rsv_add_bytes(dest, num_bytes, 1);
  4329. return 0;
  4330. }
  4331. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  4332. struct btrfs_block_rsv *block_rsv,
  4333. struct btrfs_block_rsv *dest, u64 num_bytes)
  4334. {
  4335. struct btrfs_space_info *space_info = block_rsv->space_info;
  4336. spin_lock(&block_rsv->lock);
  4337. if (num_bytes == (u64)-1)
  4338. num_bytes = block_rsv->size;
  4339. block_rsv->size -= num_bytes;
  4340. if (block_rsv->reserved >= block_rsv->size) {
  4341. num_bytes = block_rsv->reserved - block_rsv->size;
  4342. block_rsv->reserved = block_rsv->size;
  4343. block_rsv->full = 1;
  4344. } else {
  4345. num_bytes = 0;
  4346. }
  4347. spin_unlock(&block_rsv->lock);
  4348. if (num_bytes > 0) {
  4349. if (dest) {
  4350. spin_lock(&dest->lock);
  4351. if (!dest->full) {
  4352. u64 bytes_to_add;
  4353. bytes_to_add = dest->size - dest->reserved;
  4354. bytes_to_add = min(num_bytes, bytes_to_add);
  4355. dest->reserved += bytes_to_add;
  4356. if (dest->reserved >= dest->size)
  4357. dest->full = 1;
  4358. num_bytes -= bytes_to_add;
  4359. }
  4360. spin_unlock(&dest->lock);
  4361. }
  4362. if (num_bytes) {
  4363. spin_lock(&space_info->lock);
  4364. space_info->bytes_may_use -= num_bytes;
  4365. trace_btrfs_space_reservation(fs_info, "space_info",
  4366. space_info->flags, num_bytes, 0);
  4367. spin_unlock(&space_info->lock);
  4368. }
  4369. }
  4370. }
  4371. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  4372. struct btrfs_block_rsv *dst, u64 num_bytes)
  4373. {
  4374. int ret;
  4375. ret = block_rsv_use_bytes(src, num_bytes);
  4376. if (ret)
  4377. return ret;
  4378. block_rsv_add_bytes(dst, num_bytes, 1);
  4379. return 0;
  4380. }
  4381. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  4382. {
  4383. memset(rsv, 0, sizeof(*rsv));
  4384. spin_lock_init(&rsv->lock);
  4385. rsv->type = type;
  4386. }
  4387. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
  4388. unsigned short type)
  4389. {
  4390. struct btrfs_block_rsv *block_rsv;
  4391. struct btrfs_fs_info *fs_info = root->fs_info;
  4392. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  4393. if (!block_rsv)
  4394. return NULL;
  4395. btrfs_init_block_rsv(block_rsv, type);
  4396. block_rsv->space_info = __find_space_info(fs_info,
  4397. BTRFS_BLOCK_GROUP_METADATA);
  4398. return block_rsv;
  4399. }
  4400. void btrfs_free_block_rsv(struct btrfs_root *root,
  4401. struct btrfs_block_rsv *rsv)
  4402. {
  4403. if (!rsv)
  4404. return;
  4405. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4406. kfree(rsv);
  4407. }
  4408. void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
  4409. {
  4410. kfree(rsv);
  4411. }
  4412. int btrfs_block_rsv_add(struct btrfs_root *root,
  4413. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  4414. enum btrfs_reserve_flush_enum flush)
  4415. {
  4416. int ret;
  4417. if (num_bytes == 0)
  4418. return 0;
  4419. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  4420. if (!ret) {
  4421. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  4422. return 0;
  4423. }
  4424. return ret;
  4425. }
  4426. int btrfs_block_rsv_check(struct btrfs_root *root,
  4427. struct btrfs_block_rsv *block_rsv, int min_factor)
  4428. {
  4429. u64 num_bytes = 0;
  4430. int ret = -ENOSPC;
  4431. if (!block_rsv)
  4432. return 0;
  4433. spin_lock(&block_rsv->lock);
  4434. num_bytes = div_factor(block_rsv->size, min_factor);
  4435. if (block_rsv->reserved >= num_bytes)
  4436. ret = 0;
  4437. spin_unlock(&block_rsv->lock);
  4438. return ret;
  4439. }
  4440. int btrfs_block_rsv_refill(struct btrfs_root *root,
  4441. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  4442. enum btrfs_reserve_flush_enum flush)
  4443. {
  4444. u64 num_bytes = 0;
  4445. int ret = -ENOSPC;
  4446. if (!block_rsv)
  4447. return 0;
  4448. spin_lock(&block_rsv->lock);
  4449. num_bytes = min_reserved;
  4450. if (block_rsv->reserved >= num_bytes)
  4451. ret = 0;
  4452. else
  4453. num_bytes -= block_rsv->reserved;
  4454. spin_unlock(&block_rsv->lock);
  4455. if (!ret)
  4456. return 0;
  4457. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  4458. if (!ret) {
  4459. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  4460. return 0;
  4461. }
  4462. return ret;
  4463. }
  4464. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  4465. struct btrfs_block_rsv *dst_rsv,
  4466. u64 num_bytes)
  4467. {
  4468. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4469. }
  4470. void btrfs_block_rsv_release(struct btrfs_root *root,
  4471. struct btrfs_block_rsv *block_rsv,
  4472. u64 num_bytes)
  4473. {
  4474. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4475. if (global_rsv == block_rsv ||
  4476. block_rsv->space_info != global_rsv->space_info)
  4477. global_rsv = NULL;
  4478. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  4479. num_bytes);
  4480. }
  4481. /*
  4482. * helper to calculate size of global block reservation.
  4483. * the desired value is sum of space used by extent tree,
  4484. * checksum tree and root tree
  4485. */
  4486. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  4487. {
  4488. struct btrfs_space_info *sinfo;
  4489. u64 num_bytes;
  4490. u64 meta_used;
  4491. u64 data_used;
  4492. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  4493. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  4494. spin_lock(&sinfo->lock);
  4495. data_used = sinfo->bytes_used;
  4496. spin_unlock(&sinfo->lock);
  4497. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4498. spin_lock(&sinfo->lock);
  4499. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  4500. data_used = 0;
  4501. meta_used = sinfo->bytes_used;
  4502. spin_unlock(&sinfo->lock);
  4503. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  4504. csum_size * 2;
  4505. num_bytes += div_u64(data_used + meta_used, 50);
  4506. if (num_bytes * 3 > meta_used)
  4507. num_bytes = div_u64(meta_used, 3);
  4508. return ALIGN(num_bytes, fs_info->extent_root->nodesize << 10);
  4509. }
  4510. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  4511. {
  4512. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  4513. struct btrfs_space_info *sinfo = block_rsv->space_info;
  4514. u64 num_bytes;
  4515. num_bytes = calc_global_metadata_size(fs_info);
  4516. spin_lock(&sinfo->lock);
  4517. spin_lock(&block_rsv->lock);
  4518. block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
  4519. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  4520. sinfo->bytes_reserved + sinfo->bytes_readonly +
  4521. sinfo->bytes_may_use;
  4522. if (sinfo->total_bytes > num_bytes) {
  4523. num_bytes = sinfo->total_bytes - num_bytes;
  4524. block_rsv->reserved += num_bytes;
  4525. sinfo->bytes_may_use += num_bytes;
  4526. trace_btrfs_space_reservation(fs_info, "space_info",
  4527. sinfo->flags, num_bytes, 1);
  4528. }
  4529. if (block_rsv->reserved >= block_rsv->size) {
  4530. num_bytes = block_rsv->reserved - block_rsv->size;
  4531. sinfo->bytes_may_use -= num_bytes;
  4532. trace_btrfs_space_reservation(fs_info, "space_info",
  4533. sinfo->flags, num_bytes, 0);
  4534. block_rsv->reserved = block_rsv->size;
  4535. block_rsv->full = 1;
  4536. }
  4537. spin_unlock(&block_rsv->lock);
  4538. spin_unlock(&sinfo->lock);
  4539. }
  4540. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  4541. {
  4542. struct btrfs_space_info *space_info;
  4543. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  4544. fs_info->chunk_block_rsv.space_info = space_info;
  4545. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  4546. fs_info->global_block_rsv.space_info = space_info;
  4547. fs_info->delalloc_block_rsv.space_info = space_info;
  4548. fs_info->trans_block_rsv.space_info = space_info;
  4549. fs_info->empty_block_rsv.space_info = space_info;
  4550. fs_info->delayed_block_rsv.space_info = space_info;
  4551. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  4552. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  4553. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  4554. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  4555. if (fs_info->quota_root)
  4556. fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
  4557. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  4558. update_global_block_rsv(fs_info);
  4559. }
  4560. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  4561. {
  4562. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  4563. (u64)-1);
  4564. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  4565. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  4566. WARN_ON(fs_info->trans_block_rsv.size > 0);
  4567. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  4568. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  4569. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  4570. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  4571. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  4572. }
  4573. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  4574. struct btrfs_root *root)
  4575. {
  4576. if (!trans->block_rsv)
  4577. return;
  4578. if (!trans->bytes_reserved)
  4579. return;
  4580. trace_btrfs_space_reservation(root->fs_info, "transaction",
  4581. trans->transid, trans->bytes_reserved, 0);
  4582. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  4583. trans->bytes_reserved = 0;
  4584. }
  4585. /*
  4586. * To be called after all the new block groups attached to the transaction
  4587. * handle have been created (btrfs_create_pending_block_groups()).
  4588. */
  4589. void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
  4590. {
  4591. struct btrfs_fs_info *fs_info = trans->root->fs_info;
  4592. if (!trans->chunk_bytes_reserved)
  4593. return;
  4594. WARN_ON_ONCE(!list_empty(&trans->new_bgs));
  4595. block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
  4596. trans->chunk_bytes_reserved);
  4597. trans->chunk_bytes_reserved = 0;
  4598. }
  4599. /* Can only return 0 or -ENOSPC */
  4600. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  4601. struct inode *inode)
  4602. {
  4603. struct btrfs_root *root = BTRFS_I(inode)->root;
  4604. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  4605. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  4606. /*
  4607. * We need to hold space in order to delete our orphan item once we've
  4608. * added it, so this takes the reservation so we can release it later
  4609. * when we are truly done with the orphan item.
  4610. */
  4611. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4612. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4613. btrfs_ino(inode), num_bytes, 1);
  4614. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  4615. }
  4616. void btrfs_orphan_release_metadata(struct inode *inode)
  4617. {
  4618. struct btrfs_root *root = BTRFS_I(inode)->root;
  4619. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  4620. trace_btrfs_space_reservation(root->fs_info, "orphan",
  4621. btrfs_ino(inode), num_bytes, 0);
  4622. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  4623. }
  4624. /*
  4625. * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
  4626. * root: the root of the parent directory
  4627. * rsv: block reservation
  4628. * items: the number of items that we need do reservation
  4629. * qgroup_reserved: used to return the reserved size in qgroup
  4630. *
  4631. * This function is used to reserve the space for snapshot/subvolume
  4632. * creation and deletion. Those operations are different with the
  4633. * common file/directory operations, they change two fs/file trees
  4634. * and root tree, the number of items that the qgroup reserves is
  4635. * different with the free space reservation. So we can not use
  4636. * the space reseravtion mechanism in start_transaction().
  4637. */
  4638. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  4639. struct btrfs_block_rsv *rsv,
  4640. int items,
  4641. u64 *qgroup_reserved,
  4642. bool use_global_rsv)
  4643. {
  4644. u64 num_bytes;
  4645. int ret;
  4646. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  4647. if (root->fs_info->quota_enabled) {
  4648. /* One for parent inode, two for dir entries */
  4649. num_bytes = 3 * root->nodesize;
  4650. ret = btrfs_qgroup_reserve(root, num_bytes);
  4651. if (ret)
  4652. return ret;
  4653. } else {
  4654. num_bytes = 0;
  4655. }
  4656. *qgroup_reserved = num_bytes;
  4657. num_bytes = btrfs_calc_trans_metadata_size(root, items);
  4658. rsv->space_info = __find_space_info(root->fs_info,
  4659. BTRFS_BLOCK_GROUP_METADATA);
  4660. ret = btrfs_block_rsv_add(root, rsv, num_bytes,
  4661. BTRFS_RESERVE_FLUSH_ALL);
  4662. if (ret == -ENOSPC && use_global_rsv)
  4663. ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
  4664. if (ret) {
  4665. if (*qgroup_reserved)
  4666. btrfs_qgroup_free(root, *qgroup_reserved);
  4667. }
  4668. return ret;
  4669. }
  4670. void btrfs_subvolume_release_metadata(struct btrfs_root *root,
  4671. struct btrfs_block_rsv *rsv,
  4672. u64 qgroup_reserved)
  4673. {
  4674. btrfs_block_rsv_release(root, rsv, (u64)-1);
  4675. }
  4676. /**
  4677. * drop_outstanding_extent - drop an outstanding extent
  4678. * @inode: the inode we're dropping the extent for
  4679. * @num_bytes: the number of bytes we're relaseing.
  4680. *
  4681. * This is called when we are freeing up an outstanding extent, either called
  4682. * after an error or after an extent is written. This will return the number of
  4683. * reserved extents that need to be freed. This must be called with
  4684. * BTRFS_I(inode)->lock held.
  4685. */
  4686. static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes)
  4687. {
  4688. unsigned drop_inode_space = 0;
  4689. unsigned dropped_extents = 0;
  4690. unsigned num_extents = 0;
  4691. num_extents = (unsigned)div64_u64(num_bytes +
  4692. BTRFS_MAX_EXTENT_SIZE - 1,
  4693. BTRFS_MAX_EXTENT_SIZE);
  4694. ASSERT(num_extents);
  4695. ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents);
  4696. BTRFS_I(inode)->outstanding_extents -= num_extents;
  4697. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  4698. test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4699. &BTRFS_I(inode)->runtime_flags))
  4700. drop_inode_space = 1;
  4701. /*
  4702. * If we have more or the same amount of outsanding extents than we have
  4703. * reserved then we need to leave the reserved extents count alone.
  4704. */
  4705. if (BTRFS_I(inode)->outstanding_extents >=
  4706. BTRFS_I(inode)->reserved_extents)
  4707. return drop_inode_space;
  4708. dropped_extents = BTRFS_I(inode)->reserved_extents -
  4709. BTRFS_I(inode)->outstanding_extents;
  4710. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  4711. return dropped_extents + drop_inode_space;
  4712. }
  4713. /**
  4714. * calc_csum_metadata_size - return the amount of metada space that must be
  4715. * reserved/free'd for the given bytes.
  4716. * @inode: the inode we're manipulating
  4717. * @num_bytes: the number of bytes in question
  4718. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  4719. *
  4720. * This adjusts the number of csum_bytes in the inode and then returns the
  4721. * correct amount of metadata that must either be reserved or freed. We
  4722. * calculate how many checksums we can fit into one leaf and then divide the
  4723. * number of bytes that will need to be checksumed by this value to figure out
  4724. * how many checksums will be required. If we are adding bytes then the number
  4725. * may go up and we will return the number of additional bytes that must be
  4726. * reserved. If it is going down we will return the number of bytes that must
  4727. * be freed.
  4728. *
  4729. * This must be called with BTRFS_I(inode)->lock held.
  4730. */
  4731. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  4732. int reserve)
  4733. {
  4734. struct btrfs_root *root = BTRFS_I(inode)->root;
  4735. u64 old_csums, num_csums;
  4736. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  4737. BTRFS_I(inode)->csum_bytes == 0)
  4738. return 0;
  4739. old_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes);
  4740. if (reserve)
  4741. BTRFS_I(inode)->csum_bytes += num_bytes;
  4742. else
  4743. BTRFS_I(inode)->csum_bytes -= num_bytes;
  4744. num_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes);
  4745. /* No change, no need to reserve more */
  4746. if (old_csums == num_csums)
  4747. return 0;
  4748. if (reserve)
  4749. return btrfs_calc_trans_metadata_size(root,
  4750. num_csums - old_csums);
  4751. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  4752. }
  4753. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  4754. {
  4755. struct btrfs_root *root = BTRFS_I(inode)->root;
  4756. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  4757. u64 to_reserve = 0;
  4758. u64 csum_bytes;
  4759. unsigned nr_extents = 0;
  4760. int extra_reserve = 0;
  4761. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  4762. int ret = 0;
  4763. bool delalloc_lock = true;
  4764. u64 to_free = 0;
  4765. unsigned dropped;
  4766. /* If we are a free space inode we need to not flush since we will be in
  4767. * the middle of a transaction commit. We also don't need the delalloc
  4768. * mutex since we won't race with anybody. We need this mostly to make
  4769. * lockdep shut its filthy mouth.
  4770. */
  4771. if (btrfs_is_free_space_inode(inode)) {
  4772. flush = BTRFS_RESERVE_NO_FLUSH;
  4773. delalloc_lock = false;
  4774. }
  4775. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4776. btrfs_transaction_in_commit(root->fs_info))
  4777. schedule_timeout(1);
  4778. if (delalloc_lock)
  4779. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4780. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4781. spin_lock(&BTRFS_I(inode)->lock);
  4782. nr_extents = (unsigned)div64_u64(num_bytes +
  4783. BTRFS_MAX_EXTENT_SIZE - 1,
  4784. BTRFS_MAX_EXTENT_SIZE);
  4785. BTRFS_I(inode)->outstanding_extents += nr_extents;
  4786. nr_extents = 0;
  4787. if (BTRFS_I(inode)->outstanding_extents >
  4788. BTRFS_I(inode)->reserved_extents)
  4789. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4790. BTRFS_I(inode)->reserved_extents;
  4791. /*
  4792. * Add an item to reserve for updating the inode when we complete the
  4793. * delalloc io.
  4794. */
  4795. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4796. &BTRFS_I(inode)->runtime_flags)) {
  4797. nr_extents++;
  4798. extra_reserve = 1;
  4799. }
  4800. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4801. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4802. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4803. spin_unlock(&BTRFS_I(inode)->lock);
  4804. if (root->fs_info->quota_enabled) {
  4805. ret = btrfs_qgroup_reserve(root, nr_extents * root->nodesize);
  4806. if (ret)
  4807. goto out_fail;
  4808. }
  4809. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4810. if (unlikely(ret)) {
  4811. if (root->fs_info->quota_enabled)
  4812. btrfs_qgroup_free(root, nr_extents * root->nodesize);
  4813. goto out_fail;
  4814. }
  4815. spin_lock(&BTRFS_I(inode)->lock);
  4816. if (extra_reserve) {
  4817. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4818. &BTRFS_I(inode)->runtime_flags);
  4819. nr_extents--;
  4820. }
  4821. BTRFS_I(inode)->reserved_extents += nr_extents;
  4822. spin_unlock(&BTRFS_I(inode)->lock);
  4823. if (delalloc_lock)
  4824. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4825. if (to_reserve)
  4826. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4827. btrfs_ino(inode), to_reserve, 1);
  4828. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4829. return 0;
  4830. out_fail:
  4831. spin_lock(&BTRFS_I(inode)->lock);
  4832. dropped = drop_outstanding_extent(inode, num_bytes);
  4833. /*
  4834. * If the inodes csum_bytes is the same as the original
  4835. * csum_bytes then we know we haven't raced with any free()ers
  4836. * so we can just reduce our inodes csum bytes and carry on.
  4837. */
  4838. if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
  4839. calc_csum_metadata_size(inode, num_bytes, 0);
  4840. } else {
  4841. u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
  4842. u64 bytes;
  4843. /*
  4844. * This is tricky, but first we need to figure out how much we
  4845. * free'd from any free-ers that occured during this
  4846. * reservation, so we reset ->csum_bytes to the csum_bytes
  4847. * before we dropped our lock, and then call the free for the
  4848. * number of bytes that were freed while we were trying our
  4849. * reservation.
  4850. */
  4851. bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
  4852. BTRFS_I(inode)->csum_bytes = csum_bytes;
  4853. to_free = calc_csum_metadata_size(inode, bytes, 0);
  4854. /*
  4855. * Now we need to see how much we would have freed had we not
  4856. * been making this reservation and our ->csum_bytes were not
  4857. * artificially inflated.
  4858. */
  4859. BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
  4860. bytes = csum_bytes - orig_csum_bytes;
  4861. bytes = calc_csum_metadata_size(inode, bytes, 0);
  4862. /*
  4863. * Now reset ->csum_bytes to what it should be. If bytes is
  4864. * more than to_free then we would have free'd more space had we
  4865. * not had an artificially high ->csum_bytes, so we need to free
  4866. * the remainder. If bytes is the same or less then we don't
  4867. * need to do anything, the other free-ers did the correct
  4868. * thing.
  4869. */
  4870. BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
  4871. if (bytes > to_free)
  4872. to_free = bytes - to_free;
  4873. else
  4874. to_free = 0;
  4875. }
  4876. spin_unlock(&BTRFS_I(inode)->lock);
  4877. if (dropped)
  4878. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4879. if (to_free) {
  4880. btrfs_block_rsv_release(root, block_rsv, to_free);
  4881. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4882. btrfs_ino(inode), to_free, 0);
  4883. }
  4884. if (delalloc_lock)
  4885. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4886. return ret;
  4887. }
  4888. /**
  4889. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4890. * @inode: the inode to release the reservation for
  4891. * @num_bytes: the number of bytes we're releasing
  4892. *
  4893. * This will release the metadata reservation for an inode. This can be called
  4894. * once we complete IO for a given set of bytes to release their metadata
  4895. * reservations.
  4896. */
  4897. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4898. {
  4899. struct btrfs_root *root = BTRFS_I(inode)->root;
  4900. u64 to_free = 0;
  4901. unsigned dropped;
  4902. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4903. spin_lock(&BTRFS_I(inode)->lock);
  4904. dropped = drop_outstanding_extent(inode, num_bytes);
  4905. if (num_bytes)
  4906. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4907. spin_unlock(&BTRFS_I(inode)->lock);
  4908. if (dropped > 0)
  4909. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4910. if (btrfs_test_is_dummy_root(root))
  4911. return;
  4912. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4913. btrfs_ino(inode), to_free, 0);
  4914. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4915. to_free);
  4916. }
  4917. /**
  4918. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4919. * @inode: inode we're writing to
  4920. * @num_bytes: the number of bytes we want to allocate
  4921. *
  4922. * This will do the following things
  4923. *
  4924. * o reserve space in the data space info for num_bytes
  4925. * o reserve space in the metadata space info based on number of outstanding
  4926. * extents and how much csums will be needed
  4927. * o add to the inodes ->delalloc_bytes
  4928. * o add it to the fs_info's delalloc inodes list.
  4929. *
  4930. * This will return 0 for success and -ENOSPC if there is no space left.
  4931. */
  4932. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4933. {
  4934. int ret;
  4935. ret = btrfs_check_data_free_space(inode, num_bytes, num_bytes);
  4936. if (ret)
  4937. return ret;
  4938. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4939. if (ret) {
  4940. btrfs_free_reserved_data_space(inode, num_bytes);
  4941. return ret;
  4942. }
  4943. return 0;
  4944. }
  4945. /**
  4946. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4947. * @inode: inode we're releasing space for
  4948. * @num_bytes: the number of bytes we want to free up
  4949. *
  4950. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4951. * called in the case that we don't need the metadata AND data reservations
  4952. * anymore. So if there is an error or we insert an inline extent.
  4953. *
  4954. * This function will release the metadata space that was not used and will
  4955. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4956. * list if there are no delalloc bytes left.
  4957. */
  4958. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4959. {
  4960. btrfs_delalloc_release_metadata(inode, num_bytes);
  4961. btrfs_free_reserved_data_space(inode, num_bytes);
  4962. }
  4963. static int update_block_group(struct btrfs_trans_handle *trans,
  4964. struct btrfs_root *root, u64 bytenr,
  4965. u64 num_bytes, int alloc)
  4966. {
  4967. struct btrfs_block_group_cache *cache = NULL;
  4968. struct btrfs_fs_info *info = root->fs_info;
  4969. u64 total = num_bytes;
  4970. u64 old_val;
  4971. u64 byte_in_group;
  4972. int factor;
  4973. /* block accounting for super block */
  4974. spin_lock(&info->delalloc_root_lock);
  4975. old_val = btrfs_super_bytes_used(info->super_copy);
  4976. if (alloc)
  4977. old_val += num_bytes;
  4978. else
  4979. old_val -= num_bytes;
  4980. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4981. spin_unlock(&info->delalloc_root_lock);
  4982. while (total) {
  4983. cache = btrfs_lookup_block_group(info, bytenr);
  4984. if (!cache)
  4985. return -ENOENT;
  4986. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4987. BTRFS_BLOCK_GROUP_RAID1 |
  4988. BTRFS_BLOCK_GROUP_RAID10))
  4989. factor = 2;
  4990. else
  4991. factor = 1;
  4992. /*
  4993. * If this block group has free space cache written out, we
  4994. * need to make sure to load it if we are removing space. This
  4995. * is because we need the unpinning stage to actually add the
  4996. * space back to the block group, otherwise we will leak space.
  4997. */
  4998. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4999. cache_block_group(cache, 1);
  5000. byte_in_group = bytenr - cache->key.objectid;
  5001. WARN_ON(byte_in_group > cache->key.offset);
  5002. spin_lock(&cache->space_info->lock);
  5003. spin_lock(&cache->lock);
  5004. if (btrfs_test_opt(root, SPACE_CACHE) &&
  5005. cache->disk_cache_state < BTRFS_DC_CLEAR)
  5006. cache->disk_cache_state = BTRFS_DC_CLEAR;
  5007. old_val = btrfs_block_group_used(&cache->item);
  5008. num_bytes = min(total, cache->key.offset - byte_in_group);
  5009. if (alloc) {
  5010. old_val += num_bytes;
  5011. btrfs_set_block_group_used(&cache->item, old_val);
  5012. cache->reserved -= num_bytes;
  5013. cache->space_info->bytes_reserved -= num_bytes;
  5014. cache->space_info->bytes_used += num_bytes;
  5015. cache->space_info->disk_used += num_bytes * factor;
  5016. spin_unlock(&cache->lock);
  5017. spin_unlock(&cache->space_info->lock);
  5018. } else {
  5019. old_val -= num_bytes;
  5020. btrfs_set_block_group_used(&cache->item, old_val);
  5021. cache->pinned += num_bytes;
  5022. cache->space_info->bytes_pinned += num_bytes;
  5023. cache->space_info->bytes_used -= num_bytes;
  5024. cache->space_info->disk_used -= num_bytes * factor;
  5025. spin_unlock(&cache->lock);
  5026. spin_unlock(&cache->space_info->lock);
  5027. set_extent_dirty(info->pinned_extents,
  5028. bytenr, bytenr + num_bytes - 1,
  5029. GFP_NOFS | __GFP_NOFAIL);
  5030. /*
  5031. * No longer have used bytes in this block group, queue
  5032. * it for deletion.
  5033. */
  5034. if (old_val == 0) {
  5035. spin_lock(&info->unused_bgs_lock);
  5036. if (list_empty(&cache->bg_list)) {
  5037. btrfs_get_block_group(cache);
  5038. list_add_tail(&cache->bg_list,
  5039. &info->unused_bgs);
  5040. }
  5041. spin_unlock(&info->unused_bgs_lock);
  5042. }
  5043. }
  5044. spin_lock(&trans->transaction->dirty_bgs_lock);
  5045. if (list_empty(&cache->dirty_list)) {
  5046. list_add_tail(&cache->dirty_list,
  5047. &trans->transaction->dirty_bgs);
  5048. trans->transaction->num_dirty_bgs++;
  5049. btrfs_get_block_group(cache);
  5050. }
  5051. spin_unlock(&trans->transaction->dirty_bgs_lock);
  5052. btrfs_put_block_group(cache);
  5053. total -= num_bytes;
  5054. bytenr += num_bytes;
  5055. }
  5056. return 0;
  5057. }
  5058. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  5059. {
  5060. struct btrfs_block_group_cache *cache;
  5061. u64 bytenr;
  5062. spin_lock(&root->fs_info->block_group_cache_lock);
  5063. bytenr = root->fs_info->first_logical_byte;
  5064. spin_unlock(&root->fs_info->block_group_cache_lock);
  5065. if (bytenr < (u64)-1)
  5066. return bytenr;
  5067. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  5068. if (!cache)
  5069. return 0;
  5070. bytenr = cache->key.objectid;
  5071. btrfs_put_block_group(cache);
  5072. return bytenr;
  5073. }
  5074. static int pin_down_extent(struct btrfs_root *root,
  5075. struct btrfs_block_group_cache *cache,
  5076. u64 bytenr, u64 num_bytes, int reserved)
  5077. {
  5078. spin_lock(&cache->space_info->lock);
  5079. spin_lock(&cache->lock);
  5080. cache->pinned += num_bytes;
  5081. cache->space_info->bytes_pinned += num_bytes;
  5082. if (reserved) {
  5083. cache->reserved -= num_bytes;
  5084. cache->space_info->bytes_reserved -= num_bytes;
  5085. }
  5086. spin_unlock(&cache->lock);
  5087. spin_unlock(&cache->space_info->lock);
  5088. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  5089. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  5090. if (reserved)
  5091. trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
  5092. return 0;
  5093. }
  5094. /*
  5095. * this function must be called within transaction
  5096. */
  5097. int btrfs_pin_extent(struct btrfs_root *root,
  5098. u64 bytenr, u64 num_bytes, int reserved)
  5099. {
  5100. struct btrfs_block_group_cache *cache;
  5101. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  5102. BUG_ON(!cache); /* Logic error */
  5103. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  5104. btrfs_put_block_group(cache);
  5105. return 0;
  5106. }
  5107. /*
  5108. * this function must be called within transaction
  5109. */
  5110. int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
  5111. u64 bytenr, u64 num_bytes)
  5112. {
  5113. struct btrfs_block_group_cache *cache;
  5114. int ret;
  5115. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  5116. if (!cache)
  5117. return -EINVAL;
  5118. /*
  5119. * pull in the free space cache (if any) so that our pin
  5120. * removes the free space from the cache. We have load_only set
  5121. * to one because the slow code to read in the free extents does check
  5122. * the pinned extents.
  5123. */
  5124. cache_block_group(cache, 1);
  5125. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  5126. /* remove us from the free space cache (if we're there at all) */
  5127. ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
  5128. btrfs_put_block_group(cache);
  5129. return ret;
  5130. }
  5131. static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
  5132. {
  5133. int ret;
  5134. struct btrfs_block_group_cache *block_group;
  5135. struct btrfs_caching_control *caching_ctl;
  5136. block_group = btrfs_lookup_block_group(root->fs_info, start);
  5137. if (!block_group)
  5138. return -EINVAL;
  5139. cache_block_group(block_group, 0);
  5140. caching_ctl = get_caching_control(block_group);
  5141. if (!caching_ctl) {
  5142. /* Logic error */
  5143. BUG_ON(!block_group_cache_done(block_group));
  5144. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  5145. } else {
  5146. mutex_lock(&caching_ctl->mutex);
  5147. if (start >= caching_ctl->progress) {
  5148. ret = add_excluded_extent(root, start, num_bytes);
  5149. } else if (start + num_bytes <= caching_ctl->progress) {
  5150. ret = btrfs_remove_free_space(block_group,
  5151. start, num_bytes);
  5152. } else {
  5153. num_bytes = caching_ctl->progress - start;
  5154. ret = btrfs_remove_free_space(block_group,
  5155. start, num_bytes);
  5156. if (ret)
  5157. goto out_lock;
  5158. num_bytes = (start + num_bytes) -
  5159. caching_ctl->progress;
  5160. start = caching_ctl->progress;
  5161. ret = add_excluded_extent(root, start, num_bytes);
  5162. }
  5163. out_lock:
  5164. mutex_unlock(&caching_ctl->mutex);
  5165. put_caching_control(caching_ctl);
  5166. }
  5167. btrfs_put_block_group(block_group);
  5168. return ret;
  5169. }
  5170. int btrfs_exclude_logged_extents(struct btrfs_root *log,
  5171. struct extent_buffer *eb)
  5172. {
  5173. struct btrfs_file_extent_item *item;
  5174. struct btrfs_key key;
  5175. int found_type;
  5176. int i;
  5177. if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
  5178. return 0;
  5179. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  5180. btrfs_item_key_to_cpu(eb, &key, i);
  5181. if (key.type != BTRFS_EXTENT_DATA_KEY)
  5182. continue;
  5183. item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  5184. found_type = btrfs_file_extent_type(eb, item);
  5185. if (found_type == BTRFS_FILE_EXTENT_INLINE)
  5186. continue;
  5187. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  5188. continue;
  5189. key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  5190. key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  5191. __exclude_logged_extent(log, key.objectid, key.offset);
  5192. }
  5193. return 0;
  5194. }
  5195. /**
  5196. * btrfs_update_reserved_bytes - update the block_group and space info counters
  5197. * @cache: The cache we are manipulating
  5198. * @num_bytes: The number of bytes in question
  5199. * @reserve: One of the reservation enums
  5200. * @delalloc: The blocks are allocated for the delalloc write
  5201. *
  5202. * This is called by the allocator when it reserves space, or by somebody who is
  5203. * freeing space that was never actually used on disk. For example if you
  5204. * reserve some space for a new leaf in transaction A and before transaction A
  5205. * commits you free that leaf, you call this with reserve set to 0 in order to
  5206. * clear the reservation.
  5207. *
  5208. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  5209. * ENOSPC accounting. For data we handle the reservation through clearing the
  5210. * delalloc bits in the io_tree. We have to do this since we could end up
  5211. * allocating less disk space for the amount of data we have reserved in the
  5212. * case of compression.
  5213. *
  5214. * If this is a reservation and the block group has become read only we cannot
  5215. * make the reservation and return -EAGAIN, otherwise this function always
  5216. * succeeds.
  5217. */
  5218. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  5219. u64 num_bytes, int reserve, int delalloc)
  5220. {
  5221. struct btrfs_space_info *space_info = cache->space_info;
  5222. int ret = 0;
  5223. spin_lock(&space_info->lock);
  5224. spin_lock(&cache->lock);
  5225. if (reserve != RESERVE_FREE) {
  5226. if (cache->ro) {
  5227. ret = -EAGAIN;
  5228. } else {
  5229. cache->reserved += num_bytes;
  5230. space_info->bytes_reserved += num_bytes;
  5231. if (reserve == RESERVE_ALLOC) {
  5232. trace_btrfs_space_reservation(cache->fs_info,
  5233. "space_info", space_info->flags,
  5234. num_bytes, 0);
  5235. space_info->bytes_may_use -= num_bytes;
  5236. }
  5237. if (delalloc)
  5238. cache->delalloc_bytes += num_bytes;
  5239. }
  5240. } else {
  5241. if (cache->ro)
  5242. space_info->bytes_readonly += num_bytes;
  5243. cache->reserved -= num_bytes;
  5244. space_info->bytes_reserved -= num_bytes;
  5245. if (delalloc)
  5246. cache->delalloc_bytes -= num_bytes;
  5247. }
  5248. spin_unlock(&cache->lock);
  5249. spin_unlock(&space_info->lock);
  5250. return ret;
  5251. }
  5252. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  5253. struct btrfs_root *root)
  5254. {
  5255. struct btrfs_fs_info *fs_info = root->fs_info;
  5256. struct btrfs_caching_control *next;
  5257. struct btrfs_caching_control *caching_ctl;
  5258. struct btrfs_block_group_cache *cache;
  5259. down_write(&fs_info->commit_root_sem);
  5260. list_for_each_entry_safe(caching_ctl, next,
  5261. &fs_info->caching_block_groups, list) {
  5262. cache = caching_ctl->block_group;
  5263. if (block_group_cache_done(cache)) {
  5264. cache->last_byte_to_unpin = (u64)-1;
  5265. list_del_init(&caching_ctl->list);
  5266. put_caching_control(caching_ctl);
  5267. } else {
  5268. cache->last_byte_to_unpin = caching_ctl->progress;
  5269. }
  5270. }
  5271. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  5272. fs_info->pinned_extents = &fs_info->freed_extents[1];
  5273. else
  5274. fs_info->pinned_extents = &fs_info->freed_extents[0];
  5275. up_write(&fs_info->commit_root_sem);
  5276. update_global_block_rsv(fs_info);
  5277. }
  5278. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end,
  5279. const bool return_free_space)
  5280. {
  5281. struct btrfs_fs_info *fs_info = root->fs_info;
  5282. struct btrfs_block_group_cache *cache = NULL;
  5283. struct btrfs_space_info *space_info;
  5284. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  5285. u64 len;
  5286. bool readonly;
  5287. while (start <= end) {
  5288. readonly = false;
  5289. if (!cache ||
  5290. start >= cache->key.objectid + cache->key.offset) {
  5291. if (cache)
  5292. btrfs_put_block_group(cache);
  5293. cache = btrfs_lookup_block_group(fs_info, start);
  5294. BUG_ON(!cache); /* Logic error */
  5295. }
  5296. len = cache->key.objectid + cache->key.offset - start;
  5297. len = min(len, end + 1 - start);
  5298. if (start < cache->last_byte_to_unpin) {
  5299. len = min(len, cache->last_byte_to_unpin - start);
  5300. if (return_free_space)
  5301. btrfs_add_free_space(cache, start, len);
  5302. }
  5303. start += len;
  5304. space_info = cache->space_info;
  5305. spin_lock(&space_info->lock);
  5306. spin_lock(&cache->lock);
  5307. cache->pinned -= len;
  5308. space_info->bytes_pinned -= len;
  5309. percpu_counter_add(&space_info->total_bytes_pinned, -len);
  5310. if (cache->ro) {
  5311. space_info->bytes_readonly += len;
  5312. readonly = true;
  5313. }
  5314. spin_unlock(&cache->lock);
  5315. if (!readonly && global_rsv->space_info == space_info) {
  5316. spin_lock(&global_rsv->lock);
  5317. if (!global_rsv->full) {
  5318. len = min(len, global_rsv->size -
  5319. global_rsv->reserved);
  5320. global_rsv->reserved += len;
  5321. space_info->bytes_may_use += len;
  5322. if (global_rsv->reserved >= global_rsv->size)
  5323. global_rsv->full = 1;
  5324. }
  5325. spin_unlock(&global_rsv->lock);
  5326. }
  5327. spin_unlock(&space_info->lock);
  5328. }
  5329. if (cache)
  5330. btrfs_put_block_group(cache);
  5331. return 0;
  5332. }
  5333. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  5334. struct btrfs_root *root)
  5335. {
  5336. struct btrfs_fs_info *fs_info = root->fs_info;
  5337. struct extent_io_tree *unpin;
  5338. u64 start;
  5339. u64 end;
  5340. int ret;
  5341. if (trans->aborted)
  5342. return 0;
  5343. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  5344. unpin = &fs_info->freed_extents[1];
  5345. else
  5346. unpin = &fs_info->freed_extents[0];
  5347. while (1) {
  5348. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  5349. ret = find_first_extent_bit(unpin, 0, &start, &end,
  5350. EXTENT_DIRTY, NULL);
  5351. if (ret) {
  5352. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  5353. break;
  5354. }
  5355. if (btrfs_test_opt(root, DISCARD))
  5356. ret = btrfs_discard_extent(root, start,
  5357. end + 1 - start, NULL);
  5358. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  5359. unpin_extent_range(root, start, end, true);
  5360. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  5361. cond_resched();
  5362. }
  5363. return 0;
  5364. }
  5365. static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
  5366. u64 owner, u64 root_objectid)
  5367. {
  5368. struct btrfs_space_info *space_info;
  5369. u64 flags;
  5370. if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5371. if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
  5372. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  5373. else
  5374. flags = BTRFS_BLOCK_GROUP_METADATA;
  5375. } else {
  5376. flags = BTRFS_BLOCK_GROUP_DATA;
  5377. }
  5378. space_info = __find_space_info(fs_info, flags);
  5379. BUG_ON(!space_info); /* Logic bug */
  5380. percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
  5381. }
  5382. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  5383. struct btrfs_root *root,
  5384. struct btrfs_delayed_ref_node *node, u64 parent,
  5385. u64 root_objectid, u64 owner_objectid,
  5386. u64 owner_offset, int refs_to_drop,
  5387. struct btrfs_delayed_extent_op *extent_op)
  5388. {
  5389. struct btrfs_key key;
  5390. struct btrfs_path *path;
  5391. struct btrfs_fs_info *info = root->fs_info;
  5392. struct btrfs_root *extent_root = info->extent_root;
  5393. struct extent_buffer *leaf;
  5394. struct btrfs_extent_item *ei;
  5395. struct btrfs_extent_inline_ref *iref;
  5396. int ret;
  5397. int is_data;
  5398. int extent_slot = 0;
  5399. int found_extent = 0;
  5400. int num_to_del = 1;
  5401. int no_quota = node->no_quota;
  5402. u32 item_size;
  5403. u64 refs;
  5404. u64 bytenr = node->bytenr;
  5405. u64 num_bytes = node->num_bytes;
  5406. int last_ref = 0;
  5407. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  5408. SKINNY_METADATA);
  5409. if (!info->quota_enabled || !is_fstree(root_objectid))
  5410. no_quota = 1;
  5411. path = btrfs_alloc_path();
  5412. if (!path)
  5413. return -ENOMEM;
  5414. path->reada = 1;
  5415. path->leave_spinning = 1;
  5416. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  5417. BUG_ON(!is_data && refs_to_drop != 1);
  5418. if (is_data)
  5419. skinny_metadata = 0;
  5420. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  5421. bytenr, num_bytes, parent,
  5422. root_objectid, owner_objectid,
  5423. owner_offset);
  5424. if (ret == 0) {
  5425. extent_slot = path->slots[0];
  5426. while (extent_slot >= 0) {
  5427. btrfs_item_key_to_cpu(path->nodes[0], &key,
  5428. extent_slot);
  5429. if (key.objectid != bytenr)
  5430. break;
  5431. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  5432. key.offset == num_bytes) {
  5433. found_extent = 1;
  5434. break;
  5435. }
  5436. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  5437. key.offset == owner_objectid) {
  5438. found_extent = 1;
  5439. break;
  5440. }
  5441. if (path->slots[0] - extent_slot > 5)
  5442. break;
  5443. extent_slot--;
  5444. }
  5445. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  5446. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  5447. if (found_extent && item_size < sizeof(*ei))
  5448. found_extent = 0;
  5449. #endif
  5450. if (!found_extent) {
  5451. BUG_ON(iref);
  5452. ret = remove_extent_backref(trans, extent_root, path,
  5453. NULL, refs_to_drop,
  5454. is_data, &last_ref);
  5455. if (ret) {
  5456. btrfs_abort_transaction(trans, extent_root, ret);
  5457. goto out;
  5458. }
  5459. btrfs_release_path(path);
  5460. path->leave_spinning = 1;
  5461. key.objectid = bytenr;
  5462. key.type = BTRFS_EXTENT_ITEM_KEY;
  5463. key.offset = num_bytes;
  5464. if (!is_data && skinny_metadata) {
  5465. key.type = BTRFS_METADATA_ITEM_KEY;
  5466. key.offset = owner_objectid;
  5467. }
  5468. ret = btrfs_search_slot(trans, extent_root,
  5469. &key, path, -1, 1);
  5470. if (ret > 0 && skinny_metadata && path->slots[0]) {
  5471. /*
  5472. * Couldn't find our skinny metadata item,
  5473. * see if we have ye olde extent item.
  5474. */
  5475. path->slots[0]--;
  5476. btrfs_item_key_to_cpu(path->nodes[0], &key,
  5477. path->slots[0]);
  5478. if (key.objectid == bytenr &&
  5479. key.type == BTRFS_EXTENT_ITEM_KEY &&
  5480. key.offset == num_bytes)
  5481. ret = 0;
  5482. }
  5483. if (ret > 0 && skinny_metadata) {
  5484. skinny_metadata = false;
  5485. key.objectid = bytenr;
  5486. key.type = BTRFS_EXTENT_ITEM_KEY;
  5487. key.offset = num_bytes;
  5488. btrfs_release_path(path);
  5489. ret = btrfs_search_slot(trans, extent_root,
  5490. &key, path, -1, 1);
  5491. }
  5492. if (ret) {
  5493. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5494. ret, bytenr);
  5495. if (ret > 0)
  5496. btrfs_print_leaf(extent_root,
  5497. path->nodes[0]);
  5498. }
  5499. if (ret < 0) {
  5500. btrfs_abort_transaction(trans, extent_root, ret);
  5501. goto out;
  5502. }
  5503. extent_slot = path->slots[0];
  5504. }
  5505. } else if (WARN_ON(ret == -ENOENT)) {
  5506. btrfs_print_leaf(extent_root, path->nodes[0]);
  5507. btrfs_err(info,
  5508. "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
  5509. bytenr, parent, root_objectid, owner_objectid,
  5510. owner_offset);
  5511. btrfs_abort_transaction(trans, extent_root, ret);
  5512. goto out;
  5513. } else {
  5514. btrfs_abort_transaction(trans, extent_root, ret);
  5515. goto out;
  5516. }
  5517. leaf = path->nodes[0];
  5518. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5519. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  5520. if (item_size < sizeof(*ei)) {
  5521. BUG_ON(found_extent || extent_slot != path->slots[0]);
  5522. ret = convert_extent_item_v0(trans, extent_root, path,
  5523. owner_objectid, 0);
  5524. if (ret < 0) {
  5525. btrfs_abort_transaction(trans, extent_root, ret);
  5526. goto out;
  5527. }
  5528. btrfs_release_path(path);
  5529. path->leave_spinning = 1;
  5530. key.objectid = bytenr;
  5531. key.type = BTRFS_EXTENT_ITEM_KEY;
  5532. key.offset = num_bytes;
  5533. ret = btrfs_search_slot(trans, extent_root, &key, path,
  5534. -1, 1);
  5535. if (ret) {
  5536. btrfs_err(info, "umm, got %d back from search, was looking for %llu",
  5537. ret, bytenr);
  5538. btrfs_print_leaf(extent_root, path->nodes[0]);
  5539. }
  5540. if (ret < 0) {
  5541. btrfs_abort_transaction(trans, extent_root, ret);
  5542. goto out;
  5543. }
  5544. extent_slot = path->slots[0];
  5545. leaf = path->nodes[0];
  5546. item_size = btrfs_item_size_nr(leaf, extent_slot);
  5547. }
  5548. #endif
  5549. BUG_ON(item_size < sizeof(*ei));
  5550. ei = btrfs_item_ptr(leaf, extent_slot,
  5551. struct btrfs_extent_item);
  5552. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
  5553. key.type == BTRFS_EXTENT_ITEM_KEY) {
  5554. struct btrfs_tree_block_info *bi;
  5555. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  5556. bi = (struct btrfs_tree_block_info *)(ei + 1);
  5557. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  5558. }
  5559. refs = btrfs_extent_refs(leaf, ei);
  5560. if (refs < refs_to_drop) {
  5561. btrfs_err(info, "trying to drop %d refs but we only have %Lu "
  5562. "for bytenr %Lu", refs_to_drop, refs, bytenr);
  5563. ret = -EINVAL;
  5564. btrfs_abort_transaction(trans, extent_root, ret);
  5565. goto out;
  5566. }
  5567. refs -= refs_to_drop;
  5568. if (refs > 0) {
  5569. if (extent_op)
  5570. __run_delayed_extent_op(extent_op, leaf, ei);
  5571. /*
  5572. * In the case of inline back ref, reference count will
  5573. * be updated by remove_extent_backref
  5574. */
  5575. if (iref) {
  5576. BUG_ON(!found_extent);
  5577. } else {
  5578. btrfs_set_extent_refs(leaf, ei, refs);
  5579. btrfs_mark_buffer_dirty(leaf);
  5580. }
  5581. if (found_extent) {
  5582. ret = remove_extent_backref(trans, extent_root, path,
  5583. iref, refs_to_drop,
  5584. is_data, &last_ref);
  5585. if (ret) {
  5586. btrfs_abort_transaction(trans, extent_root, ret);
  5587. goto out;
  5588. }
  5589. }
  5590. add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
  5591. root_objectid);
  5592. } else {
  5593. if (found_extent) {
  5594. BUG_ON(is_data && refs_to_drop !=
  5595. extent_data_ref_count(root, path, iref));
  5596. if (iref) {
  5597. BUG_ON(path->slots[0] != extent_slot);
  5598. } else {
  5599. BUG_ON(path->slots[0] != extent_slot + 1);
  5600. path->slots[0] = extent_slot;
  5601. num_to_del = 2;
  5602. }
  5603. }
  5604. last_ref = 1;
  5605. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  5606. num_to_del);
  5607. if (ret) {
  5608. btrfs_abort_transaction(trans, extent_root, ret);
  5609. goto out;
  5610. }
  5611. btrfs_release_path(path);
  5612. if (is_data) {
  5613. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  5614. if (ret) {
  5615. btrfs_abort_transaction(trans, extent_root, ret);
  5616. goto out;
  5617. }
  5618. }
  5619. ret = update_block_group(trans, root, bytenr, num_bytes, 0);
  5620. if (ret) {
  5621. btrfs_abort_transaction(trans, extent_root, ret);
  5622. goto out;
  5623. }
  5624. }
  5625. btrfs_release_path(path);
  5626. out:
  5627. btrfs_free_path(path);
  5628. return ret;
  5629. }
  5630. /*
  5631. * when we free an block, it is possible (and likely) that we free the last
  5632. * delayed ref for that extent as well. This searches the delayed ref tree for
  5633. * a given extent, and if there are no other delayed refs to be processed, it
  5634. * removes it from the tree.
  5635. */
  5636. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  5637. struct btrfs_root *root, u64 bytenr)
  5638. {
  5639. struct btrfs_delayed_ref_head *head;
  5640. struct btrfs_delayed_ref_root *delayed_refs;
  5641. int ret = 0;
  5642. delayed_refs = &trans->transaction->delayed_refs;
  5643. spin_lock(&delayed_refs->lock);
  5644. head = btrfs_find_delayed_ref_head(trans, bytenr);
  5645. if (!head)
  5646. goto out_delayed_unlock;
  5647. spin_lock(&head->lock);
  5648. if (!list_empty(&head->ref_list))
  5649. goto out;
  5650. if (head->extent_op) {
  5651. if (!head->must_insert_reserved)
  5652. goto out;
  5653. btrfs_free_delayed_extent_op(head->extent_op);
  5654. head->extent_op = NULL;
  5655. }
  5656. /*
  5657. * waiting for the lock here would deadlock. If someone else has it
  5658. * locked they are already in the process of dropping it anyway
  5659. */
  5660. if (!mutex_trylock(&head->mutex))
  5661. goto out;
  5662. /*
  5663. * at this point we have a head with no other entries. Go
  5664. * ahead and process it.
  5665. */
  5666. head->node.in_tree = 0;
  5667. rb_erase(&head->href_node, &delayed_refs->href_root);
  5668. atomic_dec(&delayed_refs->num_entries);
  5669. /*
  5670. * we don't take a ref on the node because we're removing it from the
  5671. * tree, so we just steal the ref the tree was holding.
  5672. */
  5673. delayed_refs->num_heads--;
  5674. if (head->processing == 0)
  5675. delayed_refs->num_heads_ready--;
  5676. head->processing = 0;
  5677. spin_unlock(&head->lock);
  5678. spin_unlock(&delayed_refs->lock);
  5679. BUG_ON(head->extent_op);
  5680. if (head->must_insert_reserved)
  5681. ret = 1;
  5682. mutex_unlock(&head->mutex);
  5683. btrfs_put_delayed_ref(&head->node);
  5684. return ret;
  5685. out:
  5686. spin_unlock(&head->lock);
  5687. out_delayed_unlock:
  5688. spin_unlock(&delayed_refs->lock);
  5689. return 0;
  5690. }
  5691. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  5692. struct btrfs_root *root,
  5693. struct extent_buffer *buf,
  5694. u64 parent, int last_ref)
  5695. {
  5696. int pin = 1;
  5697. int ret;
  5698. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5699. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5700. buf->start, buf->len,
  5701. parent, root->root_key.objectid,
  5702. btrfs_header_level(buf),
  5703. BTRFS_DROP_DELAYED_REF, NULL, 0);
  5704. BUG_ON(ret); /* -ENOMEM */
  5705. }
  5706. if (!last_ref)
  5707. return;
  5708. if (btrfs_header_generation(buf) == trans->transid) {
  5709. struct btrfs_block_group_cache *cache;
  5710. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  5711. ret = check_ref_cleanup(trans, root, buf->start);
  5712. if (!ret)
  5713. goto out;
  5714. }
  5715. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  5716. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  5717. pin_down_extent(root, cache, buf->start, buf->len, 1);
  5718. btrfs_put_block_group(cache);
  5719. goto out;
  5720. }
  5721. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  5722. btrfs_add_free_space(cache, buf->start, buf->len);
  5723. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0);
  5724. btrfs_put_block_group(cache);
  5725. trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
  5726. pin = 0;
  5727. }
  5728. out:
  5729. if (pin)
  5730. add_pinned_bytes(root->fs_info, buf->len,
  5731. btrfs_header_level(buf),
  5732. root->root_key.objectid);
  5733. /*
  5734. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  5735. * anymore.
  5736. */
  5737. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  5738. }
  5739. /* Can return -ENOMEM */
  5740. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  5741. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  5742. u64 owner, u64 offset, int no_quota)
  5743. {
  5744. int ret;
  5745. struct btrfs_fs_info *fs_info = root->fs_info;
  5746. if (btrfs_test_is_dummy_root(root))
  5747. return 0;
  5748. add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
  5749. /*
  5750. * tree log blocks never actually go into the extent allocation
  5751. * tree, just update pinning info and exit early.
  5752. */
  5753. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  5754. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  5755. /* unlocks the pinned mutex */
  5756. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  5757. ret = 0;
  5758. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  5759. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  5760. num_bytes,
  5761. parent, root_objectid, (int)owner,
  5762. BTRFS_DROP_DELAYED_REF, NULL, no_quota);
  5763. } else {
  5764. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  5765. num_bytes,
  5766. parent, root_objectid, owner,
  5767. offset, BTRFS_DROP_DELAYED_REF,
  5768. NULL, no_quota);
  5769. }
  5770. return ret;
  5771. }
  5772. /*
  5773. * when we wait for progress in the block group caching, its because
  5774. * our allocation attempt failed at least once. So, we must sleep
  5775. * and let some progress happen before we try again.
  5776. *
  5777. * This function will sleep at least once waiting for new free space to
  5778. * show up, and then it will check the block group free space numbers
  5779. * for our min num_bytes. Another option is to have it go ahead
  5780. * and look in the rbtree for a free extent of a given size, but this
  5781. * is a good start.
  5782. *
  5783. * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
  5784. * any of the information in this block group.
  5785. */
  5786. static noinline void
  5787. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  5788. u64 num_bytes)
  5789. {
  5790. struct btrfs_caching_control *caching_ctl;
  5791. caching_ctl = get_caching_control(cache);
  5792. if (!caching_ctl)
  5793. return;
  5794. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  5795. (cache->free_space_ctl->free_space >= num_bytes));
  5796. put_caching_control(caching_ctl);
  5797. }
  5798. static noinline int
  5799. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  5800. {
  5801. struct btrfs_caching_control *caching_ctl;
  5802. int ret = 0;
  5803. caching_ctl = get_caching_control(cache);
  5804. if (!caching_ctl)
  5805. return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
  5806. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  5807. if (cache->cached == BTRFS_CACHE_ERROR)
  5808. ret = -EIO;
  5809. put_caching_control(caching_ctl);
  5810. return ret;
  5811. }
  5812. int __get_raid_index(u64 flags)
  5813. {
  5814. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  5815. return BTRFS_RAID_RAID10;
  5816. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  5817. return BTRFS_RAID_RAID1;
  5818. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  5819. return BTRFS_RAID_DUP;
  5820. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5821. return BTRFS_RAID_RAID0;
  5822. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  5823. return BTRFS_RAID_RAID5;
  5824. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  5825. return BTRFS_RAID_RAID6;
  5826. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  5827. }
  5828. int get_block_group_index(struct btrfs_block_group_cache *cache)
  5829. {
  5830. return __get_raid_index(cache->flags);
  5831. }
  5832. static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
  5833. [BTRFS_RAID_RAID10] = "raid10",
  5834. [BTRFS_RAID_RAID1] = "raid1",
  5835. [BTRFS_RAID_DUP] = "dup",
  5836. [BTRFS_RAID_RAID0] = "raid0",
  5837. [BTRFS_RAID_SINGLE] = "single",
  5838. [BTRFS_RAID_RAID5] = "raid5",
  5839. [BTRFS_RAID_RAID6] = "raid6",
  5840. };
  5841. static const char *get_raid_name(enum btrfs_raid_types type)
  5842. {
  5843. if (type >= BTRFS_NR_RAID_TYPES)
  5844. return NULL;
  5845. return btrfs_raid_type_names[type];
  5846. }
  5847. enum btrfs_loop_type {
  5848. LOOP_CACHING_NOWAIT = 0,
  5849. LOOP_CACHING_WAIT = 1,
  5850. LOOP_ALLOC_CHUNK = 2,
  5851. LOOP_NO_EMPTY_SIZE = 3,
  5852. };
  5853. static inline void
  5854. btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
  5855. int delalloc)
  5856. {
  5857. if (delalloc)
  5858. down_read(&cache->data_rwsem);
  5859. }
  5860. static inline void
  5861. btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
  5862. int delalloc)
  5863. {
  5864. btrfs_get_block_group(cache);
  5865. if (delalloc)
  5866. down_read(&cache->data_rwsem);
  5867. }
  5868. static struct btrfs_block_group_cache *
  5869. btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
  5870. struct btrfs_free_cluster *cluster,
  5871. int delalloc)
  5872. {
  5873. struct btrfs_block_group_cache *used_bg;
  5874. bool locked = false;
  5875. again:
  5876. spin_lock(&cluster->refill_lock);
  5877. if (locked) {
  5878. if (used_bg == cluster->block_group)
  5879. return used_bg;
  5880. up_read(&used_bg->data_rwsem);
  5881. btrfs_put_block_group(used_bg);
  5882. }
  5883. used_bg = cluster->block_group;
  5884. if (!used_bg)
  5885. return NULL;
  5886. if (used_bg == block_group)
  5887. return used_bg;
  5888. btrfs_get_block_group(used_bg);
  5889. if (!delalloc)
  5890. return used_bg;
  5891. if (down_read_trylock(&used_bg->data_rwsem))
  5892. return used_bg;
  5893. spin_unlock(&cluster->refill_lock);
  5894. down_read(&used_bg->data_rwsem);
  5895. locked = true;
  5896. goto again;
  5897. }
  5898. static inline void
  5899. btrfs_release_block_group(struct btrfs_block_group_cache *cache,
  5900. int delalloc)
  5901. {
  5902. if (delalloc)
  5903. up_read(&cache->data_rwsem);
  5904. btrfs_put_block_group(cache);
  5905. }
  5906. /*
  5907. * walks the btree of allocated extents and find a hole of a given size.
  5908. * The key ins is changed to record the hole:
  5909. * ins->objectid == start position
  5910. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  5911. * ins->offset == the size of the hole.
  5912. * Any available blocks before search_start are skipped.
  5913. *
  5914. * If there is no suitable free space, we will record the max size of
  5915. * the free space extent currently.
  5916. */
  5917. static noinline int find_free_extent(struct btrfs_root *orig_root,
  5918. u64 num_bytes, u64 empty_size,
  5919. u64 hint_byte, struct btrfs_key *ins,
  5920. u64 flags, int delalloc)
  5921. {
  5922. int ret = 0;
  5923. struct btrfs_root *root = orig_root->fs_info->extent_root;
  5924. struct btrfs_free_cluster *last_ptr = NULL;
  5925. struct btrfs_block_group_cache *block_group = NULL;
  5926. u64 search_start = 0;
  5927. u64 max_extent_size = 0;
  5928. int empty_cluster = 2 * 1024 * 1024;
  5929. struct btrfs_space_info *space_info;
  5930. int loop = 0;
  5931. int index = __get_raid_index(flags);
  5932. int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
  5933. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  5934. bool failed_cluster_refill = false;
  5935. bool failed_alloc = false;
  5936. bool use_cluster = true;
  5937. bool have_caching_bg = false;
  5938. WARN_ON(num_bytes < root->sectorsize);
  5939. ins->type = BTRFS_EXTENT_ITEM_KEY;
  5940. ins->objectid = 0;
  5941. ins->offset = 0;
  5942. trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
  5943. space_info = __find_space_info(root->fs_info, flags);
  5944. if (!space_info) {
  5945. btrfs_err(root->fs_info, "No space info for %llu", flags);
  5946. return -ENOSPC;
  5947. }
  5948. /*
  5949. * If the space info is for both data and metadata it means we have a
  5950. * small filesystem and we can't use the clustering stuff.
  5951. */
  5952. if (btrfs_mixed_space_info(space_info))
  5953. use_cluster = false;
  5954. if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  5955. last_ptr = &root->fs_info->meta_alloc_cluster;
  5956. if (!btrfs_test_opt(root, SSD))
  5957. empty_cluster = 64 * 1024;
  5958. }
  5959. if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  5960. btrfs_test_opt(root, SSD)) {
  5961. last_ptr = &root->fs_info->data_alloc_cluster;
  5962. }
  5963. if (last_ptr) {
  5964. spin_lock(&last_ptr->lock);
  5965. if (last_ptr->block_group)
  5966. hint_byte = last_ptr->window_start;
  5967. spin_unlock(&last_ptr->lock);
  5968. }
  5969. search_start = max(search_start, first_logical_byte(root, 0));
  5970. search_start = max(search_start, hint_byte);
  5971. if (!last_ptr)
  5972. empty_cluster = 0;
  5973. if (search_start == hint_byte) {
  5974. block_group = btrfs_lookup_block_group(root->fs_info,
  5975. search_start);
  5976. /*
  5977. * we don't want to use the block group if it doesn't match our
  5978. * allocation bits, or if its not cached.
  5979. *
  5980. * However if we are re-searching with an ideal block group
  5981. * picked out then we don't care that the block group is cached.
  5982. */
  5983. if (block_group && block_group_bits(block_group, flags) &&
  5984. block_group->cached != BTRFS_CACHE_NO) {
  5985. down_read(&space_info->groups_sem);
  5986. if (list_empty(&block_group->list) ||
  5987. block_group->ro) {
  5988. /*
  5989. * someone is removing this block group,
  5990. * we can't jump into the have_block_group
  5991. * target because our list pointers are not
  5992. * valid
  5993. */
  5994. btrfs_put_block_group(block_group);
  5995. up_read(&space_info->groups_sem);
  5996. } else {
  5997. index = get_block_group_index(block_group);
  5998. btrfs_lock_block_group(block_group, delalloc);
  5999. goto have_block_group;
  6000. }
  6001. } else if (block_group) {
  6002. btrfs_put_block_group(block_group);
  6003. }
  6004. }
  6005. search:
  6006. have_caching_bg = false;
  6007. down_read(&space_info->groups_sem);
  6008. list_for_each_entry(block_group, &space_info->block_groups[index],
  6009. list) {
  6010. u64 offset;
  6011. int cached;
  6012. btrfs_grab_block_group(block_group, delalloc);
  6013. search_start = block_group->key.objectid;
  6014. /*
  6015. * this can happen if we end up cycling through all the
  6016. * raid types, but we want to make sure we only allocate
  6017. * for the proper type.
  6018. */
  6019. if (!block_group_bits(block_group, flags)) {
  6020. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  6021. BTRFS_BLOCK_GROUP_RAID1 |
  6022. BTRFS_BLOCK_GROUP_RAID5 |
  6023. BTRFS_BLOCK_GROUP_RAID6 |
  6024. BTRFS_BLOCK_GROUP_RAID10;
  6025. /*
  6026. * if they asked for extra copies and this block group
  6027. * doesn't provide them, bail. This does allow us to
  6028. * fill raid0 from raid1.
  6029. */
  6030. if ((flags & extra) && !(block_group->flags & extra))
  6031. goto loop;
  6032. }
  6033. have_block_group:
  6034. cached = block_group_cache_done(block_group);
  6035. if (unlikely(!cached)) {
  6036. ret = cache_block_group(block_group, 0);
  6037. BUG_ON(ret < 0);
  6038. ret = 0;
  6039. }
  6040. if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
  6041. goto loop;
  6042. if (unlikely(block_group->ro))
  6043. goto loop;
  6044. /*
  6045. * Ok we want to try and use the cluster allocator, so
  6046. * lets look there
  6047. */
  6048. if (last_ptr) {
  6049. struct btrfs_block_group_cache *used_block_group;
  6050. unsigned long aligned_cluster;
  6051. /*
  6052. * the refill lock keeps out other
  6053. * people trying to start a new cluster
  6054. */
  6055. used_block_group = btrfs_lock_cluster(block_group,
  6056. last_ptr,
  6057. delalloc);
  6058. if (!used_block_group)
  6059. goto refill_cluster;
  6060. if (used_block_group != block_group &&
  6061. (used_block_group->ro ||
  6062. !block_group_bits(used_block_group, flags)))
  6063. goto release_cluster;
  6064. offset = btrfs_alloc_from_cluster(used_block_group,
  6065. last_ptr,
  6066. num_bytes,
  6067. used_block_group->key.objectid,
  6068. &max_extent_size);
  6069. if (offset) {
  6070. /* we have a block, we're done */
  6071. spin_unlock(&last_ptr->refill_lock);
  6072. trace_btrfs_reserve_extent_cluster(root,
  6073. used_block_group,
  6074. search_start, num_bytes);
  6075. if (used_block_group != block_group) {
  6076. btrfs_release_block_group(block_group,
  6077. delalloc);
  6078. block_group = used_block_group;
  6079. }
  6080. goto checks;
  6081. }
  6082. WARN_ON(last_ptr->block_group != used_block_group);
  6083. release_cluster:
  6084. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  6085. * set up a new clusters, so lets just skip it
  6086. * and let the allocator find whatever block
  6087. * it can find. If we reach this point, we
  6088. * will have tried the cluster allocator
  6089. * plenty of times and not have found
  6090. * anything, so we are likely way too
  6091. * fragmented for the clustering stuff to find
  6092. * anything.
  6093. *
  6094. * However, if the cluster is taken from the
  6095. * current block group, release the cluster
  6096. * first, so that we stand a better chance of
  6097. * succeeding in the unclustered
  6098. * allocation. */
  6099. if (loop >= LOOP_NO_EMPTY_SIZE &&
  6100. used_block_group != block_group) {
  6101. spin_unlock(&last_ptr->refill_lock);
  6102. btrfs_release_block_group(used_block_group,
  6103. delalloc);
  6104. goto unclustered_alloc;
  6105. }
  6106. /*
  6107. * this cluster didn't work out, free it and
  6108. * start over
  6109. */
  6110. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  6111. if (used_block_group != block_group)
  6112. btrfs_release_block_group(used_block_group,
  6113. delalloc);
  6114. refill_cluster:
  6115. if (loop >= LOOP_NO_EMPTY_SIZE) {
  6116. spin_unlock(&last_ptr->refill_lock);
  6117. goto unclustered_alloc;
  6118. }
  6119. aligned_cluster = max_t(unsigned long,
  6120. empty_cluster + empty_size,
  6121. block_group->full_stripe_len);
  6122. /* allocate a cluster in this block group */
  6123. ret = btrfs_find_space_cluster(root, block_group,
  6124. last_ptr, search_start,
  6125. num_bytes,
  6126. aligned_cluster);
  6127. if (ret == 0) {
  6128. /*
  6129. * now pull our allocation out of this
  6130. * cluster
  6131. */
  6132. offset = btrfs_alloc_from_cluster(block_group,
  6133. last_ptr,
  6134. num_bytes,
  6135. search_start,
  6136. &max_extent_size);
  6137. if (offset) {
  6138. /* we found one, proceed */
  6139. spin_unlock(&last_ptr->refill_lock);
  6140. trace_btrfs_reserve_extent_cluster(root,
  6141. block_group, search_start,
  6142. num_bytes);
  6143. goto checks;
  6144. }
  6145. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  6146. && !failed_cluster_refill) {
  6147. spin_unlock(&last_ptr->refill_lock);
  6148. failed_cluster_refill = true;
  6149. wait_block_group_cache_progress(block_group,
  6150. num_bytes + empty_cluster + empty_size);
  6151. goto have_block_group;
  6152. }
  6153. /*
  6154. * at this point we either didn't find a cluster
  6155. * or we weren't able to allocate a block from our
  6156. * cluster. Free the cluster we've been trying
  6157. * to use, and go to the next block group
  6158. */
  6159. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  6160. spin_unlock(&last_ptr->refill_lock);
  6161. goto loop;
  6162. }
  6163. unclustered_alloc:
  6164. spin_lock(&block_group->free_space_ctl->tree_lock);
  6165. if (cached &&
  6166. block_group->free_space_ctl->free_space <
  6167. num_bytes + empty_cluster + empty_size) {
  6168. if (block_group->free_space_ctl->free_space >
  6169. max_extent_size)
  6170. max_extent_size =
  6171. block_group->free_space_ctl->free_space;
  6172. spin_unlock(&block_group->free_space_ctl->tree_lock);
  6173. goto loop;
  6174. }
  6175. spin_unlock(&block_group->free_space_ctl->tree_lock);
  6176. offset = btrfs_find_space_for_alloc(block_group, search_start,
  6177. num_bytes, empty_size,
  6178. &max_extent_size);
  6179. /*
  6180. * If we didn't find a chunk, and we haven't failed on this
  6181. * block group before, and this block group is in the middle of
  6182. * caching and we are ok with waiting, then go ahead and wait
  6183. * for progress to be made, and set failed_alloc to true.
  6184. *
  6185. * If failed_alloc is true then we've already waited on this
  6186. * block group once and should move on to the next block group.
  6187. */
  6188. if (!offset && !failed_alloc && !cached &&
  6189. loop > LOOP_CACHING_NOWAIT) {
  6190. wait_block_group_cache_progress(block_group,
  6191. num_bytes + empty_size);
  6192. failed_alloc = true;
  6193. goto have_block_group;
  6194. } else if (!offset) {
  6195. if (!cached)
  6196. have_caching_bg = true;
  6197. goto loop;
  6198. }
  6199. checks:
  6200. search_start = ALIGN(offset, root->stripesize);
  6201. /* move on to the next group */
  6202. if (search_start + num_bytes >
  6203. block_group->key.objectid + block_group->key.offset) {
  6204. btrfs_add_free_space(block_group, offset, num_bytes);
  6205. goto loop;
  6206. }
  6207. if (offset < search_start)
  6208. btrfs_add_free_space(block_group, offset,
  6209. search_start - offset);
  6210. BUG_ON(offset > search_start);
  6211. ret = btrfs_update_reserved_bytes(block_group, num_bytes,
  6212. alloc_type, delalloc);
  6213. if (ret == -EAGAIN) {
  6214. btrfs_add_free_space(block_group, offset, num_bytes);
  6215. goto loop;
  6216. }
  6217. /* we are all good, lets return */
  6218. ins->objectid = search_start;
  6219. ins->offset = num_bytes;
  6220. trace_btrfs_reserve_extent(orig_root, block_group,
  6221. search_start, num_bytes);
  6222. btrfs_release_block_group(block_group, delalloc);
  6223. break;
  6224. loop:
  6225. failed_cluster_refill = false;
  6226. failed_alloc = false;
  6227. BUG_ON(index != get_block_group_index(block_group));
  6228. btrfs_release_block_group(block_group, delalloc);
  6229. }
  6230. up_read(&space_info->groups_sem);
  6231. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  6232. goto search;
  6233. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  6234. goto search;
  6235. /*
  6236. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  6237. * caching kthreads as we move along
  6238. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  6239. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  6240. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  6241. * again
  6242. */
  6243. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  6244. index = 0;
  6245. loop++;
  6246. if (loop == LOOP_ALLOC_CHUNK) {
  6247. struct btrfs_trans_handle *trans;
  6248. int exist = 0;
  6249. trans = current->journal_info;
  6250. if (trans)
  6251. exist = 1;
  6252. else
  6253. trans = btrfs_join_transaction(root);
  6254. if (IS_ERR(trans)) {
  6255. ret = PTR_ERR(trans);
  6256. goto out;
  6257. }
  6258. ret = do_chunk_alloc(trans, root, flags,
  6259. CHUNK_ALLOC_FORCE);
  6260. /*
  6261. * Do not bail out on ENOSPC since we
  6262. * can do more things.
  6263. */
  6264. if (ret < 0 && ret != -ENOSPC)
  6265. btrfs_abort_transaction(trans,
  6266. root, ret);
  6267. else
  6268. ret = 0;
  6269. if (!exist)
  6270. btrfs_end_transaction(trans, root);
  6271. if (ret)
  6272. goto out;
  6273. }
  6274. if (loop == LOOP_NO_EMPTY_SIZE) {
  6275. empty_size = 0;
  6276. empty_cluster = 0;
  6277. }
  6278. goto search;
  6279. } else if (!ins->objectid) {
  6280. ret = -ENOSPC;
  6281. } else if (ins->objectid) {
  6282. ret = 0;
  6283. }
  6284. out:
  6285. if (ret == -ENOSPC)
  6286. ins->offset = max_extent_size;
  6287. return ret;
  6288. }
  6289. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  6290. int dump_block_groups)
  6291. {
  6292. struct btrfs_block_group_cache *cache;
  6293. int index = 0;
  6294. spin_lock(&info->lock);
  6295. printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
  6296. info->flags,
  6297. info->total_bytes - info->bytes_used - info->bytes_pinned -
  6298. info->bytes_reserved - info->bytes_readonly,
  6299. (info->full) ? "" : "not ");
  6300. printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
  6301. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  6302. info->total_bytes, info->bytes_used, info->bytes_pinned,
  6303. info->bytes_reserved, info->bytes_may_use,
  6304. info->bytes_readonly);
  6305. spin_unlock(&info->lock);
  6306. if (!dump_block_groups)
  6307. return;
  6308. down_read(&info->groups_sem);
  6309. again:
  6310. list_for_each_entry(cache, &info->block_groups[index], list) {
  6311. spin_lock(&cache->lock);
  6312. printk(KERN_INFO "BTRFS: "
  6313. "block group %llu has %llu bytes, "
  6314. "%llu used %llu pinned %llu reserved %s\n",
  6315. cache->key.objectid, cache->key.offset,
  6316. btrfs_block_group_used(&cache->item), cache->pinned,
  6317. cache->reserved, cache->ro ? "[readonly]" : "");
  6318. btrfs_dump_free_space(cache, bytes);
  6319. spin_unlock(&cache->lock);
  6320. }
  6321. if (++index < BTRFS_NR_RAID_TYPES)
  6322. goto again;
  6323. up_read(&info->groups_sem);
  6324. }
  6325. int btrfs_reserve_extent(struct btrfs_root *root,
  6326. u64 num_bytes, u64 min_alloc_size,
  6327. u64 empty_size, u64 hint_byte,
  6328. struct btrfs_key *ins, int is_data, int delalloc)
  6329. {
  6330. bool final_tried = false;
  6331. u64 flags;
  6332. int ret;
  6333. flags = btrfs_get_alloc_profile(root, is_data);
  6334. again:
  6335. WARN_ON(num_bytes < root->sectorsize);
  6336. ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
  6337. flags, delalloc);
  6338. if (ret == -ENOSPC) {
  6339. if (!final_tried && ins->offset) {
  6340. num_bytes = min(num_bytes >> 1, ins->offset);
  6341. num_bytes = round_down(num_bytes, root->sectorsize);
  6342. num_bytes = max(num_bytes, min_alloc_size);
  6343. if (num_bytes == min_alloc_size)
  6344. final_tried = true;
  6345. goto again;
  6346. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  6347. struct btrfs_space_info *sinfo;
  6348. sinfo = __find_space_info(root->fs_info, flags);
  6349. btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
  6350. flags, num_bytes);
  6351. if (sinfo)
  6352. dump_space_info(sinfo, num_bytes, 1);
  6353. }
  6354. }
  6355. return ret;
  6356. }
  6357. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  6358. u64 start, u64 len,
  6359. int pin, int delalloc)
  6360. {
  6361. struct btrfs_block_group_cache *cache;
  6362. int ret = 0;
  6363. cache = btrfs_lookup_block_group(root->fs_info, start);
  6364. if (!cache) {
  6365. btrfs_err(root->fs_info, "Unable to find block group for %llu",
  6366. start);
  6367. return -ENOSPC;
  6368. }
  6369. if (pin)
  6370. pin_down_extent(root, cache, start, len, 1);
  6371. else {
  6372. if (btrfs_test_opt(root, DISCARD))
  6373. ret = btrfs_discard_extent(root, start, len, NULL);
  6374. btrfs_add_free_space(cache, start, len);
  6375. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc);
  6376. }
  6377. btrfs_put_block_group(cache);
  6378. trace_btrfs_reserved_extent_free(root, start, len);
  6379. return ret;
  6380. }
  6381. int btrfs_free_reserved_extent(struct btrfs_root *root,
  6382. u64 start, u64 len, int delalloc)
  6383. {
  6384. return __btrfs_free_reserved_extent(root, start, len, 0, delalloc);
  6385. }
  6386. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  6387. u64 start, u64 len)
  6388. {
  6389. return __btrfs_free_reserved_extent(root, start, len, 1, 0);
  6390. }
  6391. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  6392. struct btrfs_root *root,
  6393. u64 parent, u64 root_objectid,
  6394. u64 flags, u64 owner, u64 offset,
  6395. struct btrfs_key *ins, int ref_mod)
  6396. {
  6397. int ret;
  6398. struct btrfs_fs_info *fs_info = root->fs_info;
  6399. struct btrfs_extent_item *extent_item;
  6400. struct btrfs_extent_inline_ref *iref;
  6401. struct btrfs_path *path;
  6402. struct extent_buffer *leaf;
  6403. int type;
  6404. u32 size;
  6405. if (parent > 0)
  6406. type = BTRFS_SHARED_DATA_REF_KEY;
  6407. else
  6408. type = BTRFS_EXTENT_DATA_REF_KEY;
  6409. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  6410. path = btrfs_alloc_path();
  6411. if (!path)
  6412. return -ENOMEM;
  6413. path->leave_spinning = 1;
  6414. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  6415. ins, size);
  6416. if (ret) {
  6417. btrfs_free_path(path);
  6418. return ret;
  6419. }
  6420. leaf = path->nodes[0];
  6421. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  6422. struct btrfs_extent_item);
  6423. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  6424. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  6425. btrfs_set_extent_flags(leaf, extent_item,
  6426. flags | BTRFS_EXTENT_FLAG_DATA);
  6427. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  6428. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  6429. if (parent > 0) {
  6430. struct btrfs_shared_data_ref *ref;
  6431. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  6432. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  6433. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  6434. } else {
  6435. struct btrfs_extent_data_ref *ref;
  6436. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  6437. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  6438. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  6439. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  6440. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  6441. }
  6442. btrfs_mark_buffer_dirty(path->nodes[0]);
  6443. btrfs_free_path(path);
  6444. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  6445. if (ret) { /* -ENOENT, logic error */
  6446. btrfs_err(fs_info, "update block group failed for %llu %llu",
  6447. ins->objectid, ins->offset);
  6448. BUG();
  6449. }
  6450. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  6451. return ret;
  6452. }
  6453. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  6454. struct btrfs_root *root,
  6455. u64 parent, u64 root_objectid,
  6456. u64 flags, struct btrfs_disk_key *key,
  6457. int level, struct btrfs_key *ins,
  6458. int no_quota)
  6459. {
  6460. int ret;
  6461. struct btrfs_fs_info *fs_info = root->fs_info;
  6462. struct btrfs_extent_item *extent_item;
  6463. struct btrfs_tree_block_info *block_info;
  6464. struct btrfs_extent_inline_ref *iref;
  6465. struct btrfs_path *path;
  6466. struct extent_buffer *leaf;
  6467. u32 size = sizeof(*extent_item) + sizeof(*iref);
  6468. u64 num_bytes = ins->offset;
  6469. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6470. SKINNY_METADATA);
  6471. if (!skinny_metadata)
  6472. size += sizeof(*block_info);
  6473. path = btrfs_alloc_path();
  6474. if (!path) {
  6475. btrfs_free_and_pin_reserved_extent(root, ins->objectid,
  6476. root->nodesize);
  6477. return -ENOMEM;
  6478. }
  6479. path->leave_spinning = 1;
  6480. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  6481. ins, size);
  6482. if (ret) {
  6483. btrfs_free_path(path);
  6484. btrfs_free_and_pin_reserved_extent(root, ins->objectid,
  6485. root->nodesize);
  6486. return ret;
  6487. }
  6488. leaf = path->nodes[0];
  6489. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  6490. struct btrfs_extent_item);
  6491. btrfs_set_extent_refs(leaf, extent_item, 1);
  6492. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  6493. btrfs_set_extent_flags(leaf, extent_item,
  6494. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  6495. if (skinny_metadata) {
  6496. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  6497. num_bytes = root->nodesize;
  6498. } else {
  6499. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  6500. btrfs_set_tree_block_key(leaf, block_info, key);
  6501. btrfs_set_tree_block_level(leaf, block_info, level);
  6502. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  6503. }
  6504. if (parent > 0) {
  6505. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  6506. btrfs_set_extent_inline_ref_type(leaf, iref,
  6507. BTRFS_SHARED_BLOCK_REF_KEY);
  6508. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  6509. } else {
  6510. btrfs_set_extent_inline_ref_type(leaf, iref,
  6511. BTRFS_TREE_BLOCK_REF_KEY);
  6512. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  6513. }
  6514. btrfs_mark_buffer_dirty(leaf);
  6515. btrfs_free_path(path);
  6516. ret = update_block_group(trans, root, ins->objectid, root->nodesize,
  6517. 1);
  6518. if (ret) { /* -ENOENT, logic error */
  6519. btrfs_err(fs_info, "update block group failed for %llu %llu",
  6520. ins->objectid, ins->offset);
  6521. BUG();
  6522. }
  6523. trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->nodesize);
  6524. return ret;
  6525. }
  6526. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  6527. struct btrfs_root *root,
  6528. u64 root_objectid, u64 owner,
  6529. u64 offset, struct btrfs_key *ins)
  6530. {
  6531. int ret;
  6532. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  6533. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  6534. ins->offset, 0,
  6535. root_objectid, owner, offset,
  6536. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  6537. return ret;
  6538. }
  6539. /*
  6540. * this is used by the tree logging recovery code. It records that
  6541. * an extent has been allocated and makes sure to clear the free
  6542. * space cache bits as well
  6543. */
  6544. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  6545. struct btrfs_root *root,
  6546. u64 root_objectid, u64 owner, u64 offset,
  6547. struct btrfs_key *ins)
  6548. {
  6549. int ret;
  6550. struct btrfs_block_group_cache *block_group;
  6551. /*
  6552. * Mixed block groups will exclude before processing the log so we only
  6553. * need to do the exlude dance if this fs isn't mixed.
  6554. */
  6555. if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
  6556. ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
  6557. if (ret)
  6558. return ret;
  6559. }
  6560. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  6561. if (!block_group)
  6562. return -EINVAL;
  6563. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  6564. RESERVE_ALLOC_NO_ACCOUNT, 0);
  6565. BUG_ON(ret); /* logic error */
  6566. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  6567. 0, owner, offset, ins, 1);
  6568. btrfs_put_block_group(block_group);
  6569. return ret;
  6570. }
  6571. static struct extent_buffer *
  6572. btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  6573. u64 bytenr, int level)
  6574. {
  6575. struct extent_buffer *buf;
  6576. buf = btrfs_find_create_tree_block(root, bytenr);
  6577. if (!buf)
  6578. return ERR_PTR(-ENOMEM);
  6579. btrfs_set_header_generation(buf, trans->transid);
  6580. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  6581. btrfs_tree_lock(buf);
  6582. clean_tree_block(trans, root->fs_info, buf);
  6583. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  6584. btrfs_set_lock_blocking(buf);
  6585. btrfs_set_buffer_uptodate(buf);
  6586. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  6587. buf->log_index = root->log_transid % 2;
  6588. /*
  6589. * we allow two log transactions at a time, use different
  6590. * EXENT bit to differentiate dirty pages.
  6591. */
  6592. if (buf->log_index == 0)
  6593. set_extent_dirty(&root->dirty_log_pages, buf->start,
  6594. buf->start + buf->len - 1, GFP_NOFS);
  6595. else
  6596. set_extent_new(&root->dirty_log_pages, buf->start,
  6597. buf->start + buf->len - 1, GFP_NOFS);
  6598. } else {
  6599. buf->log_index = -1;
  6600. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  6601. buf->start + buf->len - 1, GFP_NOFS);
  6602. }
  6603. trans->blocks_used++;
  6604. /* this returns a buffer locked for blocking */
  6605. return buf;
  6606. }
  6607. static struct btrfs_block_rsv *
  6608. use_block_rsv(struct btrfs_trans_handle *trans,
  6609. struct btrfs_root *root, u32 blocksize)
  6610. {
  6611. struct btrfs_block_rsv *block_rsv;
  6612. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  6613. int ret;
  6614. bool global_updated = false;
  6615. block_rsv = get_block_rsv(trans, root);
  6616. if (unlikely(block_rsv->size == 0))
  6617. goto try_reserve;
  6618. again:
  6619. ret = block_rsv_use_bytes(block_rsv, blocksize);
  6620. if (!ret)
  6621. return block_rsv;
  6622. if (block_rsv->failfast)
  6623. return ERR_PTR(ret);
  6624. if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
  6625. global_updated = true;
  6626. update_global_block_rsv(root->fs_info);
  6627. goto again;
  6628. }
  6629. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  6630. static DEFINE_RATELIMIT_STATE(_rs,
  6631. DEFAULT_RATELIMIT_INTERVAL * 10,
  6632. /*DEFAULT_RATELIMIT_BURST*/ 1);
  6633. if (__ratelimit(&_rs))
  6634. WARN(1, KERN_DEBUG
  6635. "BTRFS: block rsv returned %d\n", ret);
  6636. }
  6637. try_reserve:
  6638. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  6639. BTRFS_RESERVE_NO_FLUSH);
  6640. if (!ret)
  6641. return block_rsv;
  6642. /*
  6643. * If we couldn't reserve metadata bytes try and use some from
  6644. * the global reserve if its space type is the same as the global
  6645. * reservation.
  6646. */
  6647. if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
  6648. block_rsv->space_info == global_rsv->space_info) {
  6649. ret = block_rsv_use_bytes(global_rsv, blocksize);
  6650. if (!ret)
  6651. return global_rsv;
  6652. }
  6653. return ERR_PTR(ret);
  6654. }
  6655. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  6656. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  6657. {
  6658. block_rsv_add_bytes(block_rsv, blocksize, 0);
  6659. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  6660. }
  6661. /*
  6662. * finds a free extent and does all the dirty work required for allocation
  6663. * returns the key for the extent through ins, and a tree buffer for
  6664. * the first block of the extent through buf.
  6665. *
  6666. * returns the tree buffer or an ERR_PTR on error.
  6667. */
  6668. struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
  6669. struct btrfs_root *root,
  6670. u64 parent, u64 root_objectid,
  6671. struct btrfs_disk_key *key, int level,
  6672. u64 hint, u64 empty_size)
  6673. {
  6674. struct btrfs_key ins;
  6675. struct btrfs_block_rsv *block_rsv;
  6676. struct extent_buffer *buf;
  6677. struct btrfs_delayed_extent_op *extent_op;
  6678. u64 flags = 0;
  6679. int ret;
  6680. u32 blocksize = root->nodesize;
  6681. bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
  6682. SKINNY_METADATA);
  6683. if (btrfs_test_is_dummy_root(root)) {
  6684. buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
  6685. level);
  6686. if (!IS_ERR(buf))
  6687. root->alloc_bytenr += blocksize;
  6688. return buf;
  6689. }
  6690. block_rsv = use_block_rsv(trans, root, blocksize);
  6691. if (IS_ERR(block_rsv))
  6692. return ERR_CAST(block_rsv);
  6693. ret = btrfs_reserve_extent(root, blocksize, blocksize,
  6694. empty_size, hint, &ins, 0, 0);
  6695. if (ret)
  6696. goto out_unuse;
  6697. buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
  6698. if (IS_ERR(buf)) {
  6699. ret = PTR_ERR(buf);
  6700. goto out_free_reserved;
  6701. }
  6702. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  6703. if (parent == 0)
  6704. parent = ins.objectid;
  6705. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6706. } else
  6707. BUG_ON(parent > 0);
  6708. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  6709. extent_op = btrfs_alloc_delayed_extent_op();
  6710. if (!extent_op) {
  6711. ret = -ENOMEM;
  6712. goto out_free_buf;
  6713. }
  6714. if (key)
  6715. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  6716. else
  6717. memset(&extent_op->key, 0, sizeof(extent_op->key));
  6718. extent_op->flags_to_set = flags;
  6719. if (skinny_metadata)
  6720. extent_op->update_key = 0;
  6721. else
  6722. extent_op->update_key = 1;
  6723. extent_op->update_flags = 1;
  6724. extent_op->is_data = 0;
  6725. extent_op->level = level;
  6726. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  6727. ins.objectid, ins.offset,
  6728. parent, root_objectid, level,
  6729. BTRFS_ADD_DELAYED_EXTENT,
  6730. extent_op, 0);
  6731. if (ret)
  6732. goto out_free_delayed;
  6733. }
  6734. return buf;
  6735. out_free_delayed:
  6736. btrfs_free_delayed_extent_op(extent_op);
  6737. out_free_buf:
  6738. free_extent_buffer(buf);
  6739. out_free_reserved:
  6740. btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 0);
  6741. out_unuse:
  6742. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  6743. return ERR_PTR(ret);
  6744. }
  6745. struct walk_control {
  6746. u64 refs[BTRFS_MAX_LEVEL];
  6747. u64 flags[BTRFS_MAX_LEVEL];
  6748. struct btrfs_key update_progress;
  6749. int stage;
  6750. int level;
  6751. int shared_level;
  6752. int update_ref;
  6753. int keep_locks;
  6754. int reada_slot;
  6755. int reada_count;
  6756. int for_reloc;
  6757. };
  6758. #define DROP_REFERENCE 1
  6759. #define UPDATE_BACKREF 2
  6760. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  6761. struct btrfs_root *root,
  6762. struct walk_control *wc,
  6763. struct btrfs_path *path)
  6764. {
  6765. u64 bytenr;
  6766. u64 generation;
  6767. u64 refs;
  6768. u64 flags;
  6769. u32 nritems;
  6770. u32 blocksize;
  6771. struct btrfs_key key;
  6772. struct extent_buffer *eb;
  6773. int ret;
  6774. int slot;
  6775. int nread = 0;
  6776. if (path->slots[wc->level] < wc->reada_slot) {
  6777. wc->reada_count = wc->reada_count * 2 / 3;
  6778. wc->reada_count = max(wc->reada_count, 2);
  6779. } else {
  6780. wc->reada_count = wc->reada_count * 3 / 2;
  6781. wc->reada_count = min_t(int, wc->reada_count,
  6782. BTRFS_NODEPTRS_PER_BLOCK(root));
  6783. }
  6784. eb = path->nodes[wc->level];
  6785. nritems = btrfs_header_nritems(eb);
  6786. blocksize = root->nodesize;
  6787. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  6788. if (nread >= wc->reada_count)
  6789. break;
  6790. cond_resched();
  6791. bytenr = btrfs_node_blockptr(eb, slot);
  6792. generation = btrfs_node_ptr_generation(eb, slot);
  6793. if (slot == path->slots[wc->level])
  6794. goto reada;
  6795. if (wc->stage == UPDATE_BACKREF &&
  6796. generation <= root->root_key.offset)
  6797. continue;
  6798. /* We don't lock the tree block, it's OK to be racy here */
  6799. ret = btrfs_lookup_extent_info(trans, root, bytenr,
  6800. wc->level - 1, 1, &refs,
  6801. &flags);
  6802. /* We don't care about errors in readahead. */
  6803. if (ret < 0)
  6804. continue;
  6805. BUG_ON(refs == 0);
  6806. if (wc->stage == DROP_REFERENCE) {
  6807. if (refs == 1)
  6808. goto reada;
  6809. if (wc->level == 1 &&
  6810. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6811. continue;
  6812. if (!wc->update_ref ||
  6813. generation <= root->root_key.offset)
  6814. continue;
  6815. btrfs_node_key_to_cpu(eb, &key, slot);
  6816. ret = btrfs_comp_cpu_keys(&key,
  6817. &wc->update_progress);
  6818. if (ret < 0)
  6819. continue;
  6820. } else {
  6821. if (wc->level == 1 &&
  6822. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6823. continue;
  6824. }
  6825. reada:
  6826. readahead_tree_block(root, bytenr);
  6827. nread++;
  6828. }
  6829. wc->reada_slot = slot;
  6830. }
  6831. /*
  6832. * TODO: Modify related function to add related node/leaf to dirty_extent_root,
  6833. * for later qgroup accounting.
  6834. *
  6835. * Current, this function does nothing.
  6836. */
  6837. static int account_leaf_items(struct btrfs_trans_handle *trans,
  6838. struct btrfs_root *root,
  6839. struct extent_buffer *eb)
  6840. {
  6841. int nr = btrfs_header_nritems(eb);
  6842. int i, extent_type;
  6843. struct btrfs_key key;
  6844. struct btrfs_file_extent_item *fi;
  6845. u64 bytenr, num_bytes;
  6846. for (i = 0; i < nr; i++) {
  6847. btrfs_item_key_to_cpu(eb, &key, i);
  6848. if (key.type != BTRFS_EXTENT_DATA_KEY)
  6849. continue;
  6850. fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  6851. /* filter out non qgroup-accountable extents */
  6852. extent_type = btrfs_file_extent_type(eb, fi);
  6853. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  6854. continue;
  6855. bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
  6856. if (!bytenr)
  6857. continue;
  6858. num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
  6859. }
  6860. return 0;
  6861. }
  6862. /*
  6863. * Walk up the tree from the bottom, freeing leaves and any interior
  6864. * nodes which have had all slots visited. If a node (leaf or
  6865. * interior) is freed, the node above it will have it's slot
  6866. * incremented. The root node will never be freed.
  6867. *
  6868. * At the end of this function, we should have a path which has all
  6869. * slots incremented to the next position for a search. If we need to
  6870. * read a new node it will be NULL and the node above it will have the
  6871. * correct slot selected for a later read.
  6872. *
  6873. * If we increment the root nodes slot counter past the number of
  6874. * elements, 1 is returned to signal completion of the search.
  6875. */
  6876. static int adjust_slots_upwards(struct btrfs_root *root,
  6877. struct btrfs_path *path, int root_level)
  6878. {
  6879. int level = 0;
  6880. int nr, slot;
  6881. struct extent_buffer *eb;
  6882. if (root_level == 0)
  6883. return 1;
  6884. while (level <= root_level) {
  6885. eb = path->nodes[level];
  6886. nr = btrfs_header_nritems(eb);
  6887. path->slots[level]++;
  6888. slot = path->slots[level];
  6889. if (slot >= nr || level == 0) {
  6890. /*
  6891. * Don't free the root - we will detect this
  6892. * condition after our loop and return a
  6893. * positive value for caller to stop walking the tree.
  6894. */
  6895. if (level != root_level) {
  6896. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6897. path->locks[level] = 0;
  6898. free_extent_buffer(eb);
  6899. path->nodes[level] = NULL;
  6900. path->slots[level] = 0;
  6901. }
  6902. } else {
  6903. /*
  6904. * We have a valid slot to walk back down
  6905. * from. Stop here so caller can process these
  6906. * new nodes.
  6907. */
  6908. break;
  6909. }
  6910. level++;
  6911. }
  6912. eb = path->nodes[root_level];
  6913. if (path->slots[root_level] >= btrfs_header_nritems(eb))
  6914. return 1;
  6915. return 0;
  6916. }
  6917. /*
  6918. * root_eb is the subtree root and is locked before this function is called.
  6919. * TODO: Modify this function to mark all (including complete shared node)
  6920. * to dirty_extent_root to allow it get accounted in qgroup.
  6921. */
  6922. static int account_shared_subtree(struct btrfs_trans_handle *trans,
  6923. struct btrfs_root *root,
  6924. struct extent_buffer *root_eb,
  6925. u64 root_gen,
  6926. int root_level)
  6927. {
  6928. int ret = 0;
  6929. int level;
  6930. struct extent_buffer *eb = root_eb;
  6931. struct btrfs_path *path = NULL;
  6932. BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
  6933. BUG_ON(root_eb == NULL);
  6934. if (!root->fs_info->quota_enabled)
  6935. return 0;
  6936. if (!extent_buffer_uptodate(root_eb)) {
  6937. ret = btrfs_read_buffer(root_eb, root_gen);
  6938. if (ret)
  6939. goto out;
  6940. }
  6941. if (root_level == 0) {
  6942. ret = account_leaf_items(trans, root, root_eb);
  6943. goto out;
  6944. }
  6945. path = btrfs_alloc_path();
  6946. if (!path)
  6947. return -ENOMEM;
  6948. /*
  6949. * Walk down the tree. Missing extent blocks are filled in as
  6950. * we go. Metadata is accounted every time we read a new
  6951. * extent block.
  6952. *
  6953. * When we reach a leaf, we account for file extent items in it,
  6954. * walk back up the tree (adjusting slot pointers as we go)
  6955. * and restart the search process.
  6956. */
  6957. extent_buffer_get(root_eb); /* For path */
  6958. path->nodes[root_level] = root_eb;
  6959. path->slots[root_level] = 0;
  6960. path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
  6961. walk_down:
  6962. level = root_level;
  6963. while (level >= 0) {
  6964. if (path->nodes[level] == NULL) {
  6965. int parent_slot;
  6966. u64 child_gen;
  6967. u64 child_bytenr;
  6968. /* We need to get child blockptr/gen from
  6969. * parent before we can read it. */
  6970. eb = path->nodes[level + 1];
  6971. parent_slot = path->slots[level + 1];
  6972. child_bytenr = btrfs_node_blockptr(eb, parent_slot);
  6973. child_gen = btrfs_node_ptr_generation(eb, parent_slot);
  6974. eb = read_tree_block(root, child_bytenr, child_gen);
  6975. if (IS_ERR(eb)) {
  6976. ret = PTR_ERR(eb);
  6977. goto out;
  6978. } else if (!extent_buffer_uptodate(eb)) {
  6979. free_extent_buffer(eb);
  6980. ret = -EIO;
  6981. goto out;
  6982. }
  6983. path->nodes[level] = eb;
  6984. path->slots[level] = 0;
  6985. btrfs_tree_read_lock(eb);
  6986. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  6987. path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
  6988. }
  6989. if (level == 0) {
  6990. ret = account_leaf_items(trans, root, path->nodes[level]);
  6991. if (ret)
  6992. goto out;
  6993. /* Nonzero return here means we completed our search */
  6994. ret = adjust_slots_upwards(root, path, root_level);
  6995. if (ret)
  6996. break;
  6997. /* Restart search with new slots */
  6998. goto walk_down;
  6999. }
  7000. level--;
  7001. }
  7002. ret = 0;
  7003. out:
  7004. btrfs_free_path(path);
  7005. return ret;
  7006. }
  7007. /*
  7008. * helper to process tree block while walking down the tree.
  7009. *
  7010. * when wc->stage == UPDATE_BACKREF, this function updates
  7011. * back refs for pointers in the block.
  7012. *
  7013. * NOTE: return value 1 means we should stop walking down.
  7014. */
  7015. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  7016. struct btrfs_root *root,
  7017. struct btrfs_path *path,
  7018. struct walk_control *wc, int lookup_info)
  7019. {
  7020. int level = wc->level;
  7021. struct extent_buffer *eb = path->nodes[level];
  7022. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  7023. int ret;
  7024. if (wc->stage == UPDATE_BACKREF &&
  7025. btrfs_header_owner(eb) != root->root_key.objectid)
  7026. return 1;
  7027. /*
  7028. * when reference count of tree block is 1, it won't increase
  7029. * again. once full backref flag is set, we never clear it.
  7030. */
  7031. if (lookup_info &&
  7032. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  7033. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  7034. BUG_ON(!path->locks[level]);
  7035. ret = btrfs_lookup_extent_info(trans, root,
  7036. eb->start, level, 1,
  7037. &wc->refs[level],
  7038. &wc->flags[level]);
  7039. BUG_ON(ret == -ENOMEM);
  7040. if (ret)
  7041. return ret;
  7042. BUG_ON(wc->refs[level] == 0);
  7043. }
  7044. if (wc->stage == DROP_REFERENCE) {
  7045. if (wc->refs[level] > 1)
  7046. return 1;
  7047. if (path->locks[level] && !wc->keep_locks) {
  7048. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7049. path->locks[level] = 0;
  7050. }
  7051. return 0;
  7052. }
  7053. /* wc->stage == UPDATE_BACKREF */
  7054. if (!(wc->flags[level] & flag)) {
  7055. BUG_ON(!path->locks[level]);
  7056. ret = btrfs_inc_ref(trans, root, eb, 1);
  7057. BUG_ON(ret); /* -ENOMEM */
  7058. ret = btrfs_dec_ref(trans, root, eb, 0);
  7059. BUG_ON(ret); /* -ENOMEM */
  7060. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  7061. eb->len, flag,
  7062. btrfs_header_level(eb), 0);
  7063. BUG_ON(ret); /* -ENOMEM */
  7064. wc->flags[level] |= flag;
  7065. }
  7066. /*
  7067. * the block is shared by multiple trees, so it's not good to
  7068. * keep the tree lock
  7069. */
  7070. if (path->locks[level] && level > 0) {
  7071. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7072. path->locks[level] = 0;
  7073. }
  7074. return 0;
  7075. }
  7076. /*
  7077. * helper to process tree block pointer.
  7078. *
  7079. * when wc->stage == DROP_REFERENCE, this function checks
  7080. * reference count of the block pointed to. if the block
  7081. * is shared and we need update back refs for the subtree
  7082. * rooted at the block, this function changes wc->stage to
  7083. * UPDATE_BACKREF. if the block is shared and there is no
  7084. * need to update back, this function drops the reference
  7085. * to the block.
  7086. *
  7087. * NOTE: return value 1 means we should stop walking down.
  7088. */
  7089. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  7090. struct btrfs_root *root,
  7091. struct btrfs_path *path,
  7092. struct walk_control *wc, int *lookup_info)
  7093. {
  7094. u64 bytenr;
  7095. u64 generation;
  7096. u64 parent;
  7097. u32 blocksize;
  7098. struct btrfs_key key;
  7099. struct extent_buffer *next;
  7100. int level = wc->level;
  7101. int reada = 0;
  7102. int ret = 0;
  7103. bool need_account = false;
  7104. generation = btrfs_node_ptr_generation(path->nodes[level],
  7105. path->slots[level]);
  7106. /*
  7107. * if the lower level block was created before the snapshot
  7108. * was created, we know there is no need to update back refs
  7109. * for the subtree
  7110. */
  7111. if (wc->stage == UPDATE_BACKREF &&
  7112. generation <= root->root_key.offset) {
  7113. *lookup_info = 1;
  7114. return 1;
  7115. }
  7116. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  7117. blocksize = root->nodesize;
  7118. next = btrfs_find_tree_block(root->fs_info, bytenr);
  7119. if (!next) {
  7120. next = btrfs_find_create_tree_block(root, bytenr);
  7121. if (!next)
  7122. return -ENOMEM;
  7123. btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
  7124. level - 1);
  7125. reada = 1;
  7126. }
  7127. btrfs_tree_lock(next);
  7128. btrfs_set_lock_blocking(next);
  7129. ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
  7130. &wc->refs[level - 1],
  7131. &wc->flags[level - 1]);
  7132. if (ret < 0) {
  7133. btrfs_tree_unlock(next);
  7134. return ret;
  7135. }
  7136. if (unlikely(wc->refs[level - 1] == 0)) {
  7137. btrfs_err(root->fs_info, "Missing references.");
  7138. BUG();
  7139. }
  7140. *lookup_info = 0;
  7141. if (wc->stage == DROP_REFERENCE) {
  7142. if (wc->refs[level - 1] > 1) {
  7143. need_account = true;
  7144. if (level == 1 &&
  7145. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  7146. goto skip;
  7147. if (!wc->update_ref ||
  7148. generation <= root->root_key.offset)
  7149. goto skip;
  7150. btrfs_node_key_to_cpu(path->nodes[level], &key,
  7151. path->slots[level]);
  7152. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  7153. if (ret < 0)
  7154. goto skip;
  7155. wc->stage = UPDATE_BACKREF;
  7156. wc->shared_level = level - 1;
  7157. }
  7158. } else {
  7159. if (level == 1 &&
  7160. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  7161. goto skip;
  7162. }
  7163. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  7164. btrfs_tree_unlock(next);
  7165. free_extent_buffer(next);
  7166. next = NULL;
  7167. *lookup_info = 1;
  7168. }
  7169. if (!next) {
  7170. if (reada && level == 1)
  7171. reada_walk_down(trans, root, wc, path);
  7172. next = read_tree_block(root, bytenr, generation);
  7173. if (IS_ERR(next)) {
  7174. return PTR_ERR(next);
  7175. } else if (!extent_buffer_uptodate(next)) {
  7176. free_extent_buffer(next);
  7177. return -EIO;
  7178. }
  7179. btrfs_tree_lock(next);
  7180. btrfs_set_lock_blocking(next);
  7181. }
  7182. level--;
  7183. BUG_ON(level != btrfs_header_level(next));
  7184. path->nodes[level] = next;
  7185. path->slots[level] = 0;
  7186. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7187. wc->level = level;
  7188. if (wc->level == 1)
  7189. wc->reada_slot = 0;
  7190. return 0;
  7191. skip:
  7192. wc->refs[level - 1] = 0;
  7193. wc->flags[level - 1] = 0;
  7194. if (wc->stage == DROP_REFERENCE) {
  7195. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  7196. parent = path->nodes[level]->start;
  7197. } else {
  7198. BUG_ON(root->root_key.objectid !=
  7199. btrfs_header_owner(path->nodes[level]));
  7200. parent = 0;
  7201. }
  7202. if (need_account) {
  7203. ret = account_shared_subtree(trans, root, next,
  7204. generation, level - 1);
  7205. if (ret) {
  7206. printk_ratelimited(KERN_ERR "BTRFS: %s Error "
  7207. "%d accounting shared subtree. Quota "
  7208. "is out of sync, rescan required.\n",
  7209. root->fs_info->sb->s_id, ret);
  7210. }
  7211. }
  7212. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  7213. root->root_key.objectid, level - 1, 0, 0);
  7214. BUG_ON(ret); /* -ENOMEM */
  7215. }
  7216. btrfs_tree_unlock(next);
  7217. free_extent_buffer(next);
  7218. *lookup_info = 1;
  7219. return 1;
  7220. }
  7221. /*
  7222. * helper to process tree block while walking up the tree.
  7223. *
  7224. * when wc->stage == DROP_REFERENCE, this function drops
  7225. * reference count on the block.
  7226. *
  7227. * when wc->stage == UPDATE_BACKREF, this function changes
  7228. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  7229. * to UPDATE_BACKREF previously while processing the block.
  7230. *
  7231. * NOTE: return value 1 means we should stop walking up.
  7232. */
  7233. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  7234. struct btrfs_root *root,
  7235. struct btrfs_path *path,
  7236. struct walk_control *wc)
  7237. {
  7238. int ret;
  7239. int level = wc->level;
  7240. struct extent_buffer *eb = path->nodes[level];
  7241. u64 parent = 0;
  7242. if (wc->stage == UPDATE_BACKREF) {
  7243. BUG_ON(wc->shared_level < level);
  7244. if (level < wc->shared_level)
  7245. goto out;
  7246. ret = find_next_key(path, level + 1, &wc->update_progress);
  7247. if (ret > 0)
  7248. wc->update_ref = 0;
  7249. wc->stage = DROP_REFERENCE;
  7250. wc->shared_level = -1;
  7251. path->slots[level] = 0;
  7252. /*
  7253. * check reference count again if the block isn't locked.
  7254. * we should start walking down the tree again if reference
  7255. * count is one.
  7256. */
  7257. if (!path->locks[level]) {
  7258. BUG_ON(level == 0);
  7259. btrfs_tree_lock(eb);
  7260. btrfs_set_lock_blocking(eb);
  7261. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7262. ret = btrfs_lookup_extent_info(trans, root,
  7263. eb->start, level, 1,
  7264. &wc->refs[level],
  7265. &wc->flags[level]);
  7266. if (ret < 0) {
  7267. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7268. path->locks[level] = 0;
  7269. return ret;
  7270. }
  7271. BUG_ON(wc->refs[level] == 0);
  7272. if (wc->refs[level] == 1) {
  7273. btrfs_tree_unlock_rw(eb, path->locks[level]);
  7274. path->locks[level] = 0;
  7275. return 1;
  7276. }
  7277. }
  7278. }
  7279. /* wc->stage == DROP_REFERENCE */
  7280. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  7281. if (wc->refs[level] == 1) {
  7282. if (level == 0) {
  7283. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  7284. ret = btrfs_dec_ref(trans, root, eb, 1);
  7285. else
  7286. ret = btrfs_dec_ref(trans, root, eb, 0);
  7287. BUG_ON(ret); /* -ENOMEM */
  7288. ret = account_leaf_items(trans, root, eb);
  7289. if (ret) {
  7290. printk_ratelimited(KERN_ERR "BTRFS: %s Error "
  7291. "%d accounting leaf items. Quota "
  7292. "is out of sync, rescan required.\n",
  7293. root->fs_info->sb->s_id, ret);
  7294. }
  7295. }
  7296. /* make block locked assertion in clean_tree_block happy */
  7297. if (!path->locks[level] &&
  7298. btrfs_header_generation(eb) == trans->transid) {
  7299. btrfs_tree_lock(eb);
  7300. btrfs_set_lock_blocking(eb);
  7301. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7302. }
  7303. clean_tree_block(trans, root->fs_info, eb);
  7304. }
  7305. if (eb == root->node) {
  7306. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  7307. parent = eb->start;
  7308. else
  7309. BUG_ON(root->root_key.objectid !=
  7310. btrfs_header_owner(eb));
  7311. } else {
  7312. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  7313. parent = path->nodes[level + 1]->start;
  7314. else
  7315. BUG_ON(root->root_key.objectid !=
  7316. btrfs_header_owner(path->nodes[level + 1]));
  7317. }
  7318. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  7319. out:
  7320. wc->refs[level] = 0;
  7321. wc->flags[level] = 0;
  7322. return 0;
  7323. }
  7324. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  7325. struct btrfs_root *root,
  7326. struct btrfs_path *path,
  7327. struct walk_control *wc)
  7328. {
  7329. int level = wc->level;
  7330. int lookup_info = 1;
  7331. int ret;
  7332. while (level >= 0) {
  7333. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  7334. if (ret > 0)
  7335. break;
  7336. if (level == 0)
  7337. break;
  7338. if (path->slots[level] >=
  7339. btrfs_header_nritems(path->nodes[level]))
  7340. break;
  7341. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  7342. if (ret > 0) {
  7343. path->slots[level]++;
  7344. continue;
  7345. } else if (ret < 0)
  7346. return ret;
  7347. level = wc->level;
  7348. }
  7349. return 0;
  7350. }
  7351. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  7352. struct btrfs_root *root,
  7353. struct btrfs_path *path,
  7354. struct walk_control *wc, int max_level)
  7355. {
  7356. int level = wc->level;
  7357. int ret;
  7358. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  7359. while (level < max_level && path->nodes[level]) {
  7360. wc->level = level;
  7361. if (path->slots[level] + 1 <
  7362. btrfs_header_nritems(path->nodes[level])) {
  7363. path->slots[level]++;
  7364. return 0;
  7365. } else {
  7366. ret = walk_up_proc(trans, root, path, wc);
  7367. if (ret > 0)
  7368. return 0;
  7369. if (path->locks[level]) {
  7370. btrfs_tree_unlock_rw(path->nodes[level],
  7371. path->locks[level]);
  7372. path->locks[level] = 0;
  7373. }
  7374. free_extent_buffer(path->nodes[level]);
  7375. path->nodes[level] = NULL;
  7376. level++;
  7377. }
  7378. }
  7379. return 1;
  7380. }
  7381. /*
  7382. * drop a subvolume tree.
  7383. *
  7384. * this function traverses the tree freeing any blocks that only
  7385. * referenced by the tree.
  7386. *
  7387. * when a shared tree block is found. this function decreases its
  7388. * reference count by one. if update_ref is true, this function
  7389. * also make sure backrefs for the shared block and all lower level
  7390. * blocks are properly updated.
  7391. *
  7392. * If called with for_reloc == 0, may exit early with -EAGAIN
  7393. */
  7394. int btrfs_drop_snapshot(struct btrfs_root *root,
  7395. struct btrfs_block_rsv *block_rsv, int update_ref,
  7396. int for_reloc)
  7397. {
  7398. struct btrfs_path *path;
  7399. struct btrfs_trans_handle *trans;
  7400. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7401. struct btrfs_root_item *root_item = &root->root_item;
  7402. struct walk_control *wc;
  7403. struct btrfs_key key;
  7404. int err = 0;
  7405. int ret;
  7406. int level;
  7407. bool root_dropped = false;
  7408. btrfs_debug(root->fs_info, "Drop subvolume %llu", root->objectid);
  7409. path = btrfs_alloc_path();
  7410. if (!path) {
  7411. err = -ENOMEM;
  7412. goto out;
  7413. }
  7414. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  7415. if (!wc) {
  7416. btrfs_free_path(path);
  7417. err = -ENOMEM;
  7418. goto out;
  7419. }
  7420. trans = btrfs_start_transaction(tree_root, 0);
  7421. if (IS_ERR(trans)) {
  7422. err = PTR_ERR(trans);
  7423. goto out_free;
  7424. }
  7425. if (block_rsv)
  7426. trans->block_rsv = block_rsv;
  7427. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  7428. level = btrfs_header_level(root->node);
  7429. path->nodes[level] = btrfs_lock_root_node(root);
  7430. btrfs_set_lock_blocking(path->nodes[level]);
  7431. path->slots[level] = 0;
  7432. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7433. memset(&wc->update_progress, 0,
  7434. sizeof(wc->update_progress));
  7435. } else {
  7436. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  7437. memcpy(&wc->update_progress, &key,
  7438. sizeof(wc->update_progress));
  7439. level = root_item->drop_level;
  7440. BUG_ON(level == 0);
  7441. path->lowest_level = level;
  7442. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  7443. path->lowest_level = 0;
  7444. if (ret < 0) {
  7445. err = ret;
  7446. goto out_end_trans;
  7447. }
  7448. WARN_ON(ret > 0);
  7449. /*
  7450. * unlock our path, this is safe because only this
  7451. * function is allowed to delete this snapshot
  7452. */
  7453. btrfs_unlock_up_safe(path, 0);
  7454. level = btrfs_header_level(root->node);
  7455. while (1) {
  7456. btrfs_tree_lock(path->nodes[level]);
  7457. btrfs_set_lock_blocking(path->nodes[level]);
  7458. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7459. ret = btrfs_lookup_extent_info(trans, root,
  7460. path->nodes[level]->start,
  7461. level, 1, &wc->refs[level],
  7462. &wc->flags[level]);
  7463. if (ret < 0) {
  7464. err = ret;
  7465. goto out_end_trans;
  7466. }
  7467. BUG_ON(wc->refs[level] == 0);
  7468. if (level == root_item->drop_level)
  7469. break;
  7470. btrfs_tree_unlock(path->nodes[level]);
  7471. path->locks[level] = 0;
  7472. WARN_ON(wc->refs[level] != 1);
  7473. level--;
  7474. }
  7475. }
  7476. wc->level = level;
  7477. wc->shared_level = -1;
  7478. wc->stage = DROP_REFERENCE;
  7479. wc->update_ref = update_ref;
  7480. wc->keep_locks = 0;
  7481. wc->for_reloc = for_reloc;
  7482. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  7483. while (1) {
  7484. ret = walk_down_tree(trans, root, path, wc);
  7485. if (ret < 0) {
  7486. err = ret;
  7487. break;
  7488. }
  7489. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  7490. if (ret < 0) {
  7491. err = ret;
  7492. break;
  7493. }
  7494. if (ret > 0) {
  7495. BUG_ON(wc->stage != DROP_REFERENCE);
  7496. break;
  7497. }
  7498. if (wc->stage == DROP_REFERENCE) {
  7499. level = wc->level;
  7500. btrfs_node_key(path->nodes[level],
  7501. &root_item->drop_progress,
  7502. path->slots[level]);
  7503. root_item->drop_level = level;
  7504. }
  7505. BUG_ON(wc->level == 0);
  7506. if (btrfs_should_end_transaction(trans, tree_root) ||
  7507. (!for_reloc && btrfs_need_cleaner_sleep(root))) {
  7508. ret = btrfs_update_root(trans, tree_root,
  7509. &root->root_key,
  7510. root_item);
  7511. if (ret) {
  7512. btrfs_abort_transaction(trans, tree_root, ret);
  7513. err = ret;
  7514. goto out_end_trans;
  7515. }
  7516. btrfs_end_transaction_throttle(trans, tree_root);
  7517. if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
  7518. pr_debug("BTRFS: drop snapshot early exit\n");
  7519. err = -EAGAIN;
  7520. goto out_free;
  7521. }
  7522. trans = btrfs_start_transaction(tree_root, 0);
  7523. if (IS_ERR(trans)) {
  7524. err = PTR_ERR(trans);
  7525. goto out_free;
  7526. }
  7527. if (block_rsv)
  7528. trans->block_rsv = block_rsv;
  7529. }
  7530. }
  7531. btrfs_release_path(path);
  7532. if (err)
  7533. goto out_end_trans;
  7534. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  7535. if (ret) {
  7536. btrfs_abort_transaction(trans, tree_root, ret);
  7537. goto out_end_trans;
  7538. }
  7539. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  7540. ret = btrfs_find_root(tree_root, &root->root_key, path,
  7541. NULL, NULL);
  7542. if (ret < 0) {
  7543. btrfs_abort_transaction(trans, tree_root, ret);
  7544. err = ret;
  7545. goto out_end_trans;
  7546. } else if (ret > 0) {
  7547. /* if we fail to delete the orphan item this time
  7548. * around, it'll get picked up the next time.
  7549. *
  7550. * The most common failure here is just -ENOENT.
  7551. */
  7552. btrfs_del_orphan_item(trans, tree_root,
  7553. root->root_key.objectid);
  7554. }
  7555. }
  7556. if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
  7557. btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
  7558. } else {
  7559. free_extent_buffer(root->node);
  7560. free_extent_buffer(root->commit_root);
  7561. btrfs_put_fs_root(root);
  7562. }
  7563. root_dropped = true;
  7564. out_end_trans:
  7565. btrfs_end_transaction_throttle(trans, tree_root);
  7566. out_free:
  7567. kfree(wc);
  7568. btrfs_free_path(path);
  7569. out:
  7570. /*
  7571. * So if we need to stop dropping the snapshot for whatever reason we
  7572. * need to make sure to add it back to the dead root list so that we
  7573. * keep trying to do the work later. This also cleans up roots if we
  7574. * don't have it in the radix (like when we recover after a power fail
  7575. * or unmount) so we don't leak memory.
  7576. */
  7577. if (!for_reloc && root_dropped == false)
  7578. btrfs_add_dead_root(root);
  7579. if (err && err != -EAGAIN)
  7580. btrfs_std_error(root->fs_info, err);
  7581. return err;
  7582. }
  7583. /*
  7584. * drop subtree rooted at tree block 'node'.
  7585. *
  7586. * NOTE: this function will unlock and release tree block 'node'
  7587. * only used by relocation code
  7588. */
  7589. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  7590. struct btrfs_root *root,
  7591. struct extent_buffer *node,
  7592. struct extent_buffer *parent)
  7593. {
  7594. struct btrfs_path *path;
  7595. struct walk_control *wc;
  7596. int level;
  7597. int parent_level;
  7598. int ret = 0;
  7599. int wret;
  7600. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  7601. path = btrfs_alloc_path();
  7602. if (!path)
  7603. return -ENOMEM;
  7604. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  7605. if (!wc) {
  7606. btrfs_free_path(path);
  7607. return -ENOMEM;
  7608. }
  7609. btrfs_assert_tree_locked(parent);
  7610. parent_level = btrfs_header_level(parent);
  7611. extent_buffer_get(parent);
  7612. path->nodes[parent_level] = parent;
  7613. path->slots[parent_level] = btrfs_header_nritems(parent);
  7614. btrfs_assert_tree_locked(node);
  7615. level = btrfs_header_level(node);
  7616. path->nodes[level] = node;
  7617. path->slots[level] = 0;
  7618. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  7619. wc->refs[parent_level] = 1;
  7620. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  7621. wc->level = level;
  7622. wc->shared_level = -1;
  7623. wc->stage = DROP_REFERENCE;
  7624. wc->update_ref = 0;
  7625. wc->keep_locks = 1;
  7626. wc->for_reloc = 1;
  7627. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  7628. while (1) {
  7629. wret = walk_down_tree(trans, root, path, wc);
  7630. if (wret < 0) {
  7631. ret = wret;
  7632. break;
  7633. }
  7634. wret = walk_up_tree(trans, root, path, wc, parent_level);
  7635. if (wret < 0)
  7636. ret = wret;
  7637. if (wret != 0)
  7638. break;
  7639. }
  7640. kfree(wc);
  7641. btrfs_free_path(path);
  7642. return ret;
  7643. }
  7644. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  7645. {
  7646. u64 num_devices;
  7647. u64 stripped;
  7648. /*
  7649. * if restripe for this chunk_type is on pick target profile and
  7650. * return, otherwise do the usual balance
  7651. */
  7652. stripped = get_restripe_target(root->fs_info, flags);
  7653. if (stripped)
  7654. return extended_to_chunk(stripped);
  7655. num_devices = root->fs_info->fs_devices->rw_devices;
  7656. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  7657. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  7658. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  7659. if (num_devices == 1) {
  7660. stripped |= BTRFS_BLOCK_GROUP_DUP;
  7661. stripped = flags & ~stripped;
  7662. /* turn raid0 into single device chunks */
  7663. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  7664. return stripped;
  7665. /* turn mirroring into duplication */
  7666. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7667. BTRFS_BLOCK_GROUP_RAID10))
  7668. return stripped | BTRFS_BLOCK_GROUP_DUP;
  7669. } else {
  7670. /* they already had raid on here, just return */
  7671. if (flags & stripped)
  7672. return flags;
  7673. stripped |= BTRFS_BLOCK_GROUP_DUP;
  7674. stripped = flags & ~stripped;
  7675. /* switch duplicated blocks with raid1 */
  7676. if (flags & BTRFS_BLOCK_GROUP_DUP)
  7677. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  7678. /* this is drive concat, leave it alone */
  7679. }
  7680. return flags;
  7681. }
  7682. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  7683. {
  7684. struct btrfs_space_info *sinfo = cache->space_info;
  7685. u64 num_bytes;
  7686. u64 min_allocable_bytes;
  7687. int ret = -ENOSPC;
  7688. /*
  7689. * We need some metadata space and system metadata space for
  7690. * allocating chunks in some corner cases until we force to set
  7691. * it to be readonly.
  7692. */
  7693. if ((sinfo->flags &
  7694. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  7695. !force)
  7696. min_allocable_bytes = 1 * 1024 * 1024;
  7697. else
  7698. min_allocable_bytes = 0;
  7699. spin_lock(&sinfo->lock);
  7700. spin_lock(&cache->lock);
  7701. if (cache->ro) {
  7702. ret = 0;
  7703. goto out;
  7704. }
  7705. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7706. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7707. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  7708. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  7709. min_allocable_bytes <= sinfo->total_bytes) {
  7710. sinfo->bytes_readonly += num_bytes;
  7711. cache->ro = 1;
  7712. list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
  7713. ret = 0;
  7714. }
  7715. out:
  7716. spin_unlock(&cache->lock);
  7717. spin_unlock(&sinfo->lock);
  7718. return ret;
  7719. }
  7720. int btrfs_set_block_group_ro(struct btrfs_root *root,
  7721. struct btrfs_block_group_cache *cache)
  7722. {
  7723. struct btrfs_trans_handle *trans;
  7724. u64 alloc_flags;
  7725. int ret;
  7726. BUG_ON(cache->ro);
  7727. again:
  7728. trans = btrfs_join_transaction(root);
  7729. if (IS_ERR(trans))
  7730. return PTR_ERR(trans);
  7731. /*
  7732. * we're not allowed to set block groups readonly after the dirty
  7733. * block groups cache has started writing. If it already started,
  7734. * back off and let this transaction commit
  7735. */
  7736. mutex_lock(&root->fs_info->ro_block_group_mutex);
  7737. if (trans->transaction->dirty_bg_run) {
  7738. u64 transid = trans->transid;
  7739. mutex_unlock(&root->fs_info->ro_block_group_mutex);
  7740. btrfs_end_transaction(trans, root);
  7741. ret = btrfs_wait_for_commit(root, transid);
  7742. if (ret)
  7743. return ret;
  7744. goto again;
  7745. }
  7746. /*
  7747. * if we are changing raid levels, try to allocate a corresponding
  7748. * block group with the new raid level.
  7749. */
  7750. alloc_flags = update_block_group_flags(root, cache->flags);
  7751. if (alloc_flags != cache->flags) {
  7752. ret = do_chunk_alloc(trans, root, alloc_flags,
  7753. CHUNK_ALLOC_FORCE);
  7754. /*
  7755. * ENOSPC is allowed here, we may have enough space
  7756. * already allocated at the new raid level to
  7757. * carry on
  7758. */
  7759. if (ret == -ENOSPC)
  7760. ret = 0;
  7761. if (ret < 0)
  7762. goto out;
  7763. }
  7764. ret = set_block_group_ro(cache, 0);
  7765. if (!ret)
  7766. goto out;
  7767. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  7768. ret = do_chunk_alloc(trans, root, alloc_flags,
  7769. CHUNK_ALLOC_FORCE);
  7770. if (ret < 0)
  7771. goto out;
  7772. ret = set_block_group_ro(cache, 0);
  7773. out:
  7774. if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
  7775. alloc_flags = update_block_group_flags(root, cache->flags);
  7776. lock_chunks(root->fs_info->chunk_root);
  7777. check_system_chunk(trans, root, alloc_flags);
  7778. unlock_chunks(root->fs_info->chunk_root);
  7779. }
  7780. mutex_unlock(&root->fs_info->ro_block_group_mutex);
  7781. btrfs_end_transaction(trans, root);
  7782. return ret;
  7783. }
  7784. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  7785. struct btrfs_root *root, u64 type)
  7786. {
  7787. u64 alloc_flags = get_alloc_profile(root, type);
  7788. return do_chunk_alloc(trans, root, alloc_flags,
  7789. CHUNK_ALLOC_FORCE);
  7790. }
  7791. /*
  7792. * helper to account the unused space of all the readonly block group in the
  7793. * space_info. takes mirrors into account.
  7794. */
  7795. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  7796. {
  7797. struct btrfs_block_group_cache *block_group;
  7798. u64 free_bytes = 0;
  7799. int factor;
  7800. /* It's df, we don't care if it's racey */
  7801. if (list_empty(&sinfo->ro_bgs))
  7802. return 0;
  7803. spin_lock(&sinfo->lock);
  7804. list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
  7805. spin_lock(&block_group->lock);
  7806. if (!block_group->ro) {
  7807. spin_unlock(&block_group->lock);
  7808. continue;
  7809. }
  7810. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7811. BTRFS_BLOCK_GROUP_RAID10 |
  7812. BTRFS_BLOCK_GROUP_DUP))
  7813. factor = 2;
  7814. else
  7815. factor = 1;
  7816. free_bytes += (block_group->key.offset -
  7817. btrfs_block_group_used(&block_group->item)) *
  7818. factor;
  7819. spin_unlock(&block_group->lock);
  7820. }
  7821. spin_unlock(&sinfo->lock);
  7822. return free_bytes;
  7823. }
  7824. void btrfs_set_block_group_rw(struct btrfs_root *root,
  7825. struct btrfs_block_group_cache *cache)
  7826. {
  7827. struct btrfs_space_info *sinfo = cache->space_info;
  7828. u64 num_bytes;
  7829. BUG_ON(!cache->ro);
  7830. spin_lock(&sinfo->lock);
  7831. spin_lock(&cache->lock);
  7832. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7833. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7834. sinfo->bytes_readonly -= num_bytes;
  7835. cache->ro = 0;
  7836. list_del_init(&cache->ro_list);
  7837. spin_unlock(&cache->lock);
  7838. spin_unlock(&sinfo->lock);
  7839. }
  7840. /*
  7841. * checks to see if its even possible to relocate this block group.
  7842. *
  7843. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  7844. * ok to go ahead and try.
  7845. */
  7846. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  7847. {
  7848. struct btrfs_block_group_cache *block_group;
  7849. struct btrfs_space_info *space_info;
  7850. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  7851. struct btrfs_device *device;
  7852. struct btrfs_trans_handle *trans;
  7853. u64 min_free;
  7854. u64 dev_min = 1;
  7855. u64 dev_nr = 0;
  7856. u64 target;
  7857. int index;
  7858. int full = 0;
  7859. int ret = 0;
  7860. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  7861. /* odd, couldn't find the block group, leave it alone */
  7862. if (!block_group)
  7863. return -1;
  7864. min_free = btrfs_block_group_used(&block_group->item);
  7865. /* no bytes used, we're good */
  7866. if (!min_free)
  7867. goto out;
  7868. space_info = block_group->space_info;
  7869. spin_lock(&space_info->lock);
  7870. full = space_info->full;
  7871. /*
  7872. * if this is the last block group we have in this space, we can't
  7873. * relocate it unless we're able to allocate a new chunk below.
  7874. *
  7875. * Otherwise, we need to make sure we have room in the space to handle
  7876. * all of the extents from this block group. If we can, we're good
  7877. */
  7878. if ((space_info->total_bytes != block_group->key.offset) &&
  7879. (space_info->bytes_used + space_info->bytes_reserved +
  7880. space_info->bytes_pinned + space_info->bytes_readonly +
  7881. min_free < space_info->total_bytes)) {
  7882. spin_unlock(&space_info->lock);
  7883. goto out;
  7884. }
  7885. spin_unlock(&space_info->lock);
  7886. /*
  7887. * ok we don't have enough space, but maybe we have free space on our
  7888. * devices to allocate new chunks for relocation, so loop through our
  7889. * alloc devices and guess if we have enough space. if this block
  7890. * group is going to be restriped, run checks against the target
  7891. * profile instead of the current one.
  7892. */
  7893. ret = -1;
  7894. /*
  7895. * index:
  7896. * 0: raid10
  7897. * 1: raid1
  7898. * 2: dup
  7899. * 3: raid0
  7900. * 4: single
  7901. */
  7902. target = get_restripe_target(root->fs_info, block_group->flags);
  7903. if (target) {
  7904. index = __get_raid_index(extended_to_chunk(target));
  7905. } else {
  7906. /*
  7907. * this is just a balance, so if we were marked as full
  7908. * we know there is no space for a new chunk
  7909. */
  7910. if (full)
  7911. goto out;
  7912. index = get_block_group_index(block_group);
  7913. }
  7914. if (index == BTRFS_RAID_RAID10) {
  7915. dev_min = 4;
  7916. /* Divide by 2 */
  7917. min_free >>= 1;
  7918. } else if (index == BTRFS_RAID_RAID1) {
  7919. dev_min = 2;
  7920. } else if (index == BTRFS_RAID_DUP) {
  7921. /* Multiply by 2 */
  7922. min_free <<= 1;
  7923. } else if (index == BTRFS_RAID_RAID0) {
  7924. dev_min = fs_devices->rw_devices;
  7925. min_free = div64_u64(min_free, dev_min);
  7926. }
  7927. /* We need to do this so that we can look at pending chunks */
  7928. trans = btrfs_join_transaction(root);
  7929. if (IS_ERR(trans)) {
  7930. ret = PTR_ERR(trans);
  7931. goto out;
  7932. }
  7933. mutex_lock(&root->fs_info->chunk_mutex);
  7934. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7935. u64 dev_offset;
  7936. /*
  7937. * check to make sure we can actually find a chunk with enough
  7938. * space to fit our block group in.
  7939. */
  7940. if (device->total_bytes > device->bytes_used + min_free &&
  7941. !device->is_tgtdev_for_dev_replace) {
  7942. ret = find_free_dev_extent(trans, device, min_free,
  7943. &dev_offset, NULL);
  7944. if (!ret)
  7945. dev_nr++;
  7946. if (dev_nr >= dev_min)
  7947. break;
  7948. ret = -1;
  7949. }
  7950. }
  7951. mutex_unlock(&root->fs_info->chunk_mutex);
  7952. btrfs_end_transaction(trans, root);
  7953. out:
  7954. btrfs_put_block_group(block_group);
  7955. return ret;
  7956. }
  7957. static int find_first_block_group(struct btrfs_root *root,
  7958. struct btrfs_path *path, struct btrfs_key *key)
  7959. {
  7960. int ret = 0;
  7961. struct btrfs_key found_key;
  7962. struct extent_buffer *leaf;
  7963. int slot;
  7964. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7965. if (ret < 0)
  7966. goto out;
  7967. while (1) {
  7968. slot = path->slots[0];
  7969. leaf = path->nodes[0];
  7970. if (slot >= btrfs_header_nritems(leaf)) {
  7971. ret = btrfs_next_leaf(root, path);
  7972. if (ret == 0)
  7973. continue;
  7974. if (ret < 0)
  7975. goto out;
  7976. break;
  7977. }
  7978. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  7979. if (found_key.objectid >= key->objectid &&
  7980. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  7981. ret = 0;
  7982. goto out;
  7983. }
  7984. path->slots[0]++;
  7985. }
  7986. out:
  7987. return ret;
  7988. }
  7989. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  7990. {
  7991. struct btrfs_block_group_cache *block_group;
  7992. u64 last = 0;
  7993. while (1) {
  7994. struct inode *inode;
  7995. block_group = btrfs_lookup_first_block_group(info, last);
  7996. while (block_group) {
  7997. spin_lock(&block_group->lock);
  7998. if (block_group->iref)
  7999. break;
  8000. spin_unlock(&block_group->lock);
  8001. block_group = next_block_group(info->tree_root,
  8002. block_group);
  8003. }
  8004. if (!block_group) {
  8005. if (last == 0)
  8006. break;
  8007. last = 0;
  8008. continue;
  8009. }
  8010. inode = block_group->inode;
  8011. block_group->iref = 0;
  8012. block_group->inode = NULL;
  8013. spin_unlock(&block_group->lock);
  8014. iput(inode);
  8015. last = block_group->key.objectid + block_group->key.offset;
  8016. btrfs_put_block_group(block_group);
  8017. }
  8018. }
  8019. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  8020. {
  8021. struct btrfs_block_group_cache *block_group;
  8022. struct btrfs_space_info *space_info;
  8023. struct btrfs_caching_control *caching_ctl;
  8024. struct rb_node *n;
  8025. down_write(&info->commit_root_sem);
  8026. while (!list_empty(&info->caching_block_groups)) {
  8027. caching_ctl = list_entry(info->caching_block_groups.next,
  8028. struct btrfs_caching_control, list);
  8029. list_del(&caching_ctl->list);
  8030. put_caching_control(caching_ctl);
  8031. }
  8032. up_write(&info->commit_root_sem);
  8033. spin_lock(&info->unused_bgs_lock);
  8034. while (!list_empty(&info->unused_bgs)) {
  8035. block_group = list_first_entry(&info->unused_bgs,
  8036. struct btrfs_block_group_cache,
  8037. bg_list);
  8038. list_del_init(&block_group->bg_list);
  8039. btrfs_put_block_group(block_group);
  8040. }
  8041. spin_unlock(&info->unused_bgs_lock);
  8042. spin_lock(&info->block_group_cache_lock);
  8043. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  8044. block_group = rb_entry(n, struct btrfs_block_group_cache,
  8045. cache_node);
  8046. rb_erase(&block_group->cache_node,
  8047. &info->block_group_cache_tree);
  8048. RB_CLEAR_NODE(&block_group->cache_node);
  8049. spin_unlock(&info->block_group_cache_lock);
  8050. down_write(&block_group->space_info->groups_sem);
  8051. list_del(&block_group->list);
  8052. up_write(&block_group->space_info->groups_sem);
  8053. if (block_group->cached == BTRFS_CACHE_STARTED)
  8054. wait_block_group_cache_done(block_group);
  8055. /*
  8056. * We haven't cached this block group, which means we could
  8057. * possibly have excluded extents on this block group.
  8058. */
  8059. if (block_group->cached == BTRFS_CACHE_NO ||
  8060. block_group->cached == BTRFS_CACHE_ERROR)
  8061. free_excluded_extents(info->extent_root, block_group);
  8062. btrfs_remove_free_space_cache(block_group);
  8063. btrfs_put_block_group(block_group);
  8064. spin_lock(&info->block_group_cache_lock);
  8065. }
  8066. spin_unlock(&info->block_group_cache_lock);
  8067. /* now that all the block groups are freed, go through and
  8068. * free all the space_info structs. This is only called during
  8069. * the final stages of unmount, and so we know nobody is
  8070. * using them. We call synchronize_rcu() once before we start,
  8071. * just to be on the safe side.
  8072. */
  8073. synchronize_rcu();
  8074. release_global_block_rsv(info);
  8075. while (!list_empty(&info->space_info)) {
  8076. int i;
  8077. space_info = list_entry(info->space_info.next,
  8078. struct btrfs_space_info,
  8079. list);
  8080. if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
  8081. if (WARN_ON(space_info->bytes_pinned > 0 ||
  8082. space_info->bytes_reserved > 0 ||
  8083. space_info->bytes_may_use > 0)) {
  8084. dump_space_info(space_info, 0, 0);
  8085. }
  8086. }
  8087. list_del(&space_info->list);
  8088. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  8089. struct kobject *kobj;
  8090. kobj = space_info->block_group_kobjs[i];
  8091. space_info->block_group_kobjs[i] = NULL;
  8092. if (kobj) {
  8093. kobject_del(kobj);
  8094. kobject_put(kobj);
  8095. }
  8096. }
  8097. kobject_del(&space_info->kobj);
  8098. kobject_put(&space_info->kobj);
  8099. }
  8100. return 0;
  8101. }
  8102. static void __link_block_group(struct btrfs_space_info *space_info,
  8103. struct btrfs_block_group_cache *cache)
  8104. {
  8105. int index = get_block_group_index(cache);
  8106. bool first = false;
  8107. down_write(&space_info->groups_sem);
  8108. if (list_empty(&space_info->block_groups[index]))
  8109. first = true;
  8110. list_add_tail(&cache->list, &space_info->block_groups[index]);
  8111. up_write(&space_info->groups_sem);
  8112. if (first) {
  8113. struct raid_kobject *rkobj;
  8114. int ret;
  8115. rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
  8116. if (!rkobj)
  8117. goto out_err;
  8118. rkobj->raid_type = index;
  8119. kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
  8120. ret = kobject_add(&rkobj->kobj, &space_info->kobj,
  8121. "%s", get_raid_name(index));
  8122. if (ret) {
  8123. kobject_put(&rkobj->kobj);
  8124. goto out_err;
  8125. }
  8126. space_info->block_group_kobjs[index] = &rkobj->kobj;
  8127. }
  8128. return;
  8129. out_err:
  8130. pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
  8131. }
  8132. static struct btrfs_block_group_cache *
  8133. btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
  8134. {
  8135. struct btrfs_block_group_cache *cache;
  8136. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  8137. if (!cache)
  8138. return NULL;
  8139. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  8140. GFP_NOFS);
  8141. if (!cache->free_space_ctl) {
  8142. kfree(cache);
  8143. return NULL;
  8144. }
  8145. cache->key.objectid = start;
  8146. cache->key.offset = size;
  8147. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  8148. cache->sectorsize = root->sectorsize;
  8149. cache->fs_info = root->fs_info;
  8150. cache->full_stripe_len = btrfs_full_stripe_len(root,
  8151. &root->fs_info->mapping_tree,
  8152. start);
  8153. atomic_set(&cache->count, 1);
  8154. spin_lock_init(&cache->lock);
  8155. init_rwsem(&cache->data_rwsem);
  8156. INIT_LIST_HEAD(&cache->list);
  8157. INIT_LIST_HEAD(&cache->cluster_list);
  8158. INIT_LIST_HEAD(&cache->bg_list);
  8159. INIT_LIST_HEAD(&cache->ro_list);
  8160. INIT_LIST_HEAD(&cache->dirty_list);
  8161. INIT_LIST_HEAD(&cache->io_list);
  8162. btrfs_init_free_space_ctl(cache);
  8163. atomic_set(&cache->trimming, 0);
  8164. return cache;
  8165. }
  8166. int btrfs_read_block_groups(struct btrfs_root *root)
  8167. {
  8168. struct btrfs_path *path;
  8169. int ret;
  8170. struct btrfs_block_group_cache *cache;
  8171. struct btrfs_fs_info *info = root->fs_info;
  8172. struct btrfs_space_info *space_info;
  8173. struct btrfs_key key;
  8174. struct btrfs_key found_key;
  8175. struct extent_buffer *leaf;
  8176. int need_clear = 0;
  8177. u64 cache_gen;
  8178. root = info->extent_root;
  8179. key.objectid = 0;
  8180. key.offset = 0;
  8181. key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  8182. path = btrfs_alloc_path();
  8183. if (!path)
  8184. return -ENOMEM;
  8185. path->reada = 1;
  8186. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  8187. if (btrfs_test_opt(root, SPACE_CACHE) &&
  8188. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  8189. need_clear = 1;
  8190. if (btrfs_test_opt(root, CLEAR_CACHE))
  8191. need_clear = 1;
  8192. while (1) {
  8193. ret = find_first_block_group(root, path, &key);
  8194. if (ret > 0)
  8195. break;
  8196. if (ret != 0)
  8197. goto error;
  8198. leaf = path->nodes[0];
  8199. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  8200. cache = btrfs_create_block_group_cache(root, found_key.objectid,
  8201. found_key.offset);
  8202. if (!cache) {
  8203. ret = -ENOMEM;
  8204. goto error;
  8205. }
  8206. if (need_clear) {
  8207. /*
  8208. * When we mount with old space cache, we need to
  8209. * set BTRFS_DC_CLEAR and set dirty flag.
  8210. *
  8211. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  8212. * truncate the old free space cache inode and
  8213. * setup a new one.
  8214. * b) Setting 'dirty flag' makes sure that we flush
  8215. * the new space cache info onto disk.
  8216. */
  8217. if (btrfs_test_opt(root, SPACE_CACHE))
  8218. cache->disk_cache_state = BTRFS_DC_CLEAR;
  8219. }
  8220. read_extent_buffer(leaf, &cache->item,
  8221. btrfs_item_ptr_offset(leaf, path->slots[0]),
  8222. sizeof(cache->item));
  8223. cache->flags = btrfs_block_group_flags(&cache->item);
  8224. key.objectid = found_key.objectid + found_key.offset;
  8225. btrfs_release_path(path);
  8226. /*
  8227. * We need to exclude the super stripes now so that the space
  8228. * info has super bytes accounted for, otherwise we'll think
  8229. * we have more space than we actually do.
  8230. */
  8231. ret = exclude_super_stripes(root, cache);
  8232. if (ret) {
  8233. /*
  8234. * We may have excluded something, so call this just in
  8235. * case.
  8236. */
  8237. free_excluded_extents(root, cache);
  8238. btrfs_put_block_group(cache);
  8239. goto error;
  8240. }
  8241. /*
  8242. * check for two cases, either we are full, and therefore
  8243. * don't need to bother with the caching work since we won't
  8244. * find any space, or we are empty, and we can just add all
  8245. * the space in and be done with it. This saves us _alot_ of
  8246. * time, particularly in the full case.
  8247. */
  8248. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  8249. cache->last_byte_to_unpin = (u64)-1;
  8250. cache->cached = BTRFS_CACHE_FINISHED;
  8251. free_excluded_extents(root, cache);
  8252. } else if (btrfs_block_group_used(&cache->item) == 0) {
  8253. cache->last_byte_to_unpin = (u64)-1;
  8254. cache->cached = BTRFS_CACHE_FINISHED;
  8255. add_new_free_space(cache, root->fs_info,
  8256. found_key.objectid,
  8257. found_key.objectid +
  8258. found_key.offset);
  8259. free_excluded_extents(root, cache);
  8260. }
  8261. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  8262. if (ret) {
  8263. btrfs_remove_free_space_cache(cache);
  8264. btrfs_put_block_group(cache);
  8265. goto error;
  8266. }
  8267. ret = update_space_info(info, cache->flags, found_key.offset,
  8268. btrfs_block_group_used(&cache->item),
  8269. &space_info);
  8270. if (ret) {
  8271. btrfs_remove_free_space_cache(cache);
  8272. spin_lock(&info->block_group_cache_lock);
  8273. rb_erase(&cache->cache_node,
  8274. &info->block_group_cache_tree);
  8275. RB_CLEAR_NODE(&cache->cache_node);
  8276. spin_unlock(&info->block_group_cache_lock);
  8277. btrfs_put_block_group(cache);
  8278. goto error;
  8279. }
  8280. cache->space_info = space_info;
  8281. spin_lock(&cache->space_info->lock);
  8282. cache->space_info->bytes_readonly += cache->bytes_super;
  8283. spin_unlock(&cache->space_info->lock);
  8284. __link_block_group(space_info, cache);
  8285. set_avail_alloc_bits(root->fs_info, cache->flags);
  8286. if (btrfs_chunk_readonly(root, cache->key.objectid)) {
  8287. set_block_group_ro(cache, 1);
  8288. } else if (btrfs_block_group_used(&cache->item) == 0) {
  8289. spin_lock(&info->unused_bgs_lock);
  8290. /* Should always be true but just in case. */
  8291. if (list_empty(&cache->bg_list)) {
  8292. btrfs_get_block_group(cache);
  8293. list_add_tail(&cache->bg_list,
  8294. &info->unused_bgs);
  8295. }
  8296. spin_unlock(&info->unused_bgs_lock);
  8297. }
  8298. }
  8299. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  8300. if (!(get_alloc_profile(root, space_info->flags) &
  8301. (BTRFS_BLOCK_GROUP_RAID10 |
  8302. BTRFS_BLOCK_GROUP_RAID1 |
  8303. BTRFS_BLOCK_GROUP_RAID5 |
  8304. BTRFS_BLOCK_GROUP_RAID6 |
  8305. BTRFS_BLOCK_GROUP_DUP)))
  8306. continue;
  8307. /*
  8308. * avoid allocating from un-mirrored block group if there are
  8309. * mirrored block groups.
  8310. */
  8311. list_for_each_entry(cache,
  8312. &space_info->block_groups[BTRFS_RAID_RAID0],
  8313. list)
  8314. set_block_group_ro(cache, 1);
  8315. list_for_each_entry(cache,
  8316. &space_info->block_groups[BTRFS_RAID_SINGLE],
  8317. list)
  8318. set_block_group_ro(cache, 1);
  8319. }
  8320. init_global_block_rsv(info);
  8321. ret = 0;
  8322. error:
  8323. btrfs_free_path(path);
  8324. return ret;
  8325. }
  8326. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  8327. struct btrfs_root *root)
  8328. {
  8329. struct btrfs_block_group_cache *block_group, *tmp;
  8330. struct btrfs_root *extent_root = root->fs_info->extent_root;
  8331. struct btrfs_block_group_item item;
  8332. struct btrfs_key key;
  8333. int ret = 0;
  8334. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
  8335. if (ret)
  8336. goto next;
  8337. spin_lock(&block_group->lock);
  8338. memcpy(&item, &block_group->item, sizeof(item));
  8339. memcpy(&key, &block_group->key, sizeof(key));
  8340. spin_unlock(&block_group->lock);
  8341. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  8342. sizeof(item));
  8343. if (ret)
  8344. btrfs_abort_transaction(trans, extent_root, ret);
  8345. ret = btrfs_finish_chunk_alloc(trans, extent_root,
  8346. key.objectid, key.offset);
  8347. if (ret)
  8348. btrfs_abort_transaction(trans, extent_root, ret);
  8349. next:
  8350. list_del_init(&block_group->bg_list);
  8351. }
  8352. }
  8353. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  8354. struct btrfs_root *root, u64 bytes_used,
  8355. u64 type, u64 chunk_objectid, u64 chunk_offset,
  8356. u64 size)
  8357. {
  8358. int ret;
  8359. struct btrfs_root *extent_root;
  8360. struct btrfs_block_group_cache *cache;
  8361. extent_root = root->fs_info->extent_root;
  8362. btrfs_set_log_full_commit(root->fs_info, trans);
  8363. cache = btrfs_create_block_group_cache(root, chunk_offset, size);
  8364. if (!cache)
  8365. return -ENOMEM;
  8366. btrfs_set_block_group_used(&cache->item, bytes_used);
  8367. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  8368. btrfs_set_block_group_flags(&cache->item, type);
  8369. cache->flags = type;
  8370. cache->last_byte_to_unpin = (u64)-1;
  8371. cache->cached = BTRFS_CACHE_FINISHED;
  8372. ret = exclude_super_stripes(root, cache);
  8373. if (ret) {
  8374. /*
  8375. * We may have excluded something, so call this just in
  8376. * case.
  8377. */
  8378. free_excluded_extents(root, cache);
  8379. btrfs_put_block_group(cache);
  8380. return ret;
  8381. }
  8382. add_new_free_space(cache, root->fs_info, chunk_offset,
  8383. chunk_offset + size);
  8384. free_excluded_extents(root, cache);
  8385. /*
  8386. * Call to ensure the corresponding space_info object is created and
  8387. * assigned to our block group, but don't update its counters just yet.
  8388. * We want our bg to be added to the rbtree with its ->space_info set.
  8389. */
  8390. ret = update_space_info(root->fs_info, cache->flags, 0, 0,
  8391. &cache->space_info);
  8392. if (ret) {
  8393. btrfs_remove_free_space_cache(cache);
  8394. btrfs_put_block_group(cache);
  8395. return ret;
  8396. }
  8397. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  8398. if (ret) {
  8399. btrfs_remove_free_space_cache(cache);
  8400. btrfs_put_block_group(cache);
  8401. return ret;
  8402. }
  8403. /*
  8404. * Now that our block group has its ->space_info set and is inserted in
  8405. * the rbtree, update the space info's counters.
  8406. */
  8407. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  8408. &cache->space_info);
  8409. if (ret) {
  8410. btrfs_remove_free_space_cache(cache);
  8411. spin_lock(&root->fs_info->block_group_cache_lock);
  8412. rb_erase(&cache->cache_node,
  8413. &root->fs_info->block_group_cache_tree);
  8414. RB_CLEAR_NODE(&cache->cache_node);
  8415. spin_unlock(&root->fs_info->block_group_cache_lock);
  8416. btrfs_put_block_group(cache);
  8417. return ret;
  8418. }
  8419. update_global_block_rsv(root->fs_info);
  8420. spin_lock(&cache->space_info->lock);
  8421. cache->space_info->bytes_readonly += cache->bytes_super;
  8422. spin_unlock(&cache->space_info->lock);
  8423. __link_block_group(cache->space_info, cache);
  8424. list_add_tail(&cache->bg_list, &trans->new_bgs);
  8425. set_avail_alloc_bits(extent_root->fs_info, type);
  8426. return 0;
  8427. }
  8428. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  8429. {
  8430. u64 extra_flags = chunk_to_extended(flags) &
  8431. BTRFS_EXTENDED_PROFILE_MASK;
  8432. write_seqlock(&fs_info->profiles_lock);
  8433. if (flags & BTRFS_BLOCK_GROUP_DATA)
  8434. fs_info->avail_data_alloc_bits &= ~extra_flags;
  8435. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  8436. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  8437. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  8438. fs_info->avail_system_alloc_bits &= ~extra_flags;
  8439. write_sequnlock(&fs_info->profiles_lock);
  8440. }
  8441. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  8442. struct btrfs_root *root, u64 group_start,
  8443. struct extent_map *em)
  8444. {
  8445. struct btrfs_path *path;
  8446. struct btrfs_block_group_cache *block_group;
  8447. struct btrfs_free_cluster *cluster;
  8448. struct btrfs_root *tree_root = root->fs_info->tree_root;
  8449. struct btrfs_key key;
  8450. struct inode *inode;
  8451. struct kobject *kobj = NULL;
  8452. int ret;
  8453. int index;
  8454. int factor;
  8455. struct btrfs_caching_control *caching_ctl = NULL;
  8456. bool remove_em;
  8457. root = root->fs_info->extent_root;
  8458. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  8459. BUG_ON(!block_group);
  8460. BUG_ON(!block_group->ro);
  8461. /*
  8462. * Free the reserved super bytes from this block group before
  8463. * remove it.
  8464. */
  8465. free_excluded_extents(root, block_group);
  8466. memcpy(&key, &block_group->key, sizeof(key));
  8467. index = get_block_group_index(block_group);
  8468. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  8469. BTRFS_BLOCK_GROUP_RAID1 |
  8470. BTRFS_BLOCK_GROUP_RAID10))
  8471. factor = 2;
  8472. else
  8473. factor = 1;
  8474. /* make sure this block group isn't part of an allocation cluster */
  8475. cluster = &root->fs_info->data_alloc_cluster;
  8476. spin_lock(&cluster->refill_lock);
  8477. btrfs_return_cluster_to_free_space(block_group, cluster);
  8478. spin_unlock(&cluster->refill_lock);
  8479. /*
  8480. * make sure this block group isn't part of a metadata
  8481. * allocation cluster
  8482. */
  8483. cluster = &root->fs_info->meta_alloc_cluster;
  8484. spin_lock(&cluster->refill_lock);
  8485. btrfs_return_cluster_to_free_space(block_group, cluster);
  8486. spin_unlock(&cluster->refill_lock);
  8487. path = btrfs_alloc_path();
  8488. if (!path) {
  8489. ret = -ENOMEM;
  8490. goto out;
  8491. }
  8492. /*
  8493. * get the inode first so any iput calls done for the io_list
  8494. * aren't the final iput (no unlinks allowed now)
  8495. */
  8496. inode = lookup_free_space_inode(tree_root, block_group, path);
  8497. mutex_lock(&trans->transaction->cache_write_mutex);
  8498. /*
  8499. * make sure our free spache cache IO is done before remove the
  8500. * free space inode
  8501. */
  8502. spin_lock(&trans->transaction->dirty_bgs_lock);
  8503. if (!list_empty(&block_group->io_list)) {
  8504. list_del_init(&block_group->io_list);
  8505. WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
  8506. spin_unlock(&trans->transaction->dirty_bgs_lock);
  8507. btrfs_wait_cache_io(root, trans, block_group,
  8508. &block_group->io_ctl, path,
  8509. block_group->key.objectid);
  8510. btrfs_put_block_group(block_group);
  8511. spin_lock(&trans->transaction->dirty_bgs_lock);
  8512. }
  8513. if (!list_empty(&block_group->dirty_list)) {
  8514. list_del_init(&block_group->dirty_list);
  8515. btrfs_put_block_group(block_group);
  8516. }
  8517. spin_unlock(&trans->transaction->dirty_bgs_lock);
  8518. mutex_unlock(&trans->transaction->cache_write_mutex);
  8519. if (!IS_ERR(inode)) {
  8520. ret = btrfs_orphan_add(trans, inode);
  8521. if (ret) {
  8522. btrfs_add_delayed_iput(inode);
  8523. goto out;
  8524. }
  8525. clear_nlink(inode);
  8526. /* One for the block groups ref */
  8527. spin_lock(&block_group->lock);
  8528. if (block_group->iref) {
  8529. block_group->iref = 0;
  8530. block_group->inode = NULL;
  8531. spin_unlock(&block_group->lock);
  8532. iput(inode);
  8533. } else {
  8534. spin_unlock(&block_group->lock);
  8535. }
  8536. /* One for our lookup ref */
  8537. btrfs_add_delayed_iput(inode);
  8538. }
  8539. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  8540. key.offset = block_group->key.objectid;
  8541. key.type = 0;
  8542. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  8543. if (ret < 0)
  8544. goto out;
  8545. if (ret > 0)
  8546. btrfs_release_path(path);
  8547. if (ret == 0) {
  8548. ret = btrfs_del_item(trans, tree_root, path);
  8549. if (ret)
  8550. goto out;
  8551. btrfs_release_path(path);
  8552. }
  8553. spin_lock(&root->fs_info->block_group_cache_lock);
  8554. rb_erase(&block_group->cache_node,
  8555. &root->fs_info->block_group_cache_tree);
  8556. RB_CLEAR_NODE(&block_group->cache_node);
  8557. if (root->fs_info->first_logical_byte == block_group->key.objectid)
  8558. root->fs_info->first_logical_byte = (u64)-1;
  8559. spin_unlock(&root->fs_info->block_group_cache_lock);
  8560. down_write(&block_group->space_info->groups_sem);
  8561. /*
  8562. * we must use list_del_init so people can check to see if they
  8563. * are still on the list after taking the semaphore
  8564. */
  8565. list_del_init(&block_group->list);
  8566. if (list_empty(&block_group->space_info->block_groups[index])) {
  8567. kobj = block_group->space_info->block_group_kobjs[index];
  8568. block_group->space_info->block_group_kobjs[index] = NULL;
  8569. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  8570. }
  8571. up_write(&block_group->space_info->groups_sem);
  8572. if (kobj) {
  8573. kobject_del(kobj);
  8574. kobject_put(kobj);
  8575. }
  8576. if (block_group->has_caching_ctl)
  8577. caching_ctl = get_caching_control(block_group);
  8578. if (block_group->cached == BTRFS_CACHE_STARTED)
  8579. wait_block_group_cache_done(block_group);
  8580. if (block_group->has_caching_ctl) {
  8581. down_write(&root->fs_info->commit_root_sem);
  8582. if (!caching_ctl) {
  8583. struct btrfs_caching_control *ctl;
  8584. list_for_each_entry(ctl,
  8585. &root->fs_info->caching_block_groups, list)
  8586. if (ctl->block_group == block_group) {
  8587. caching_ctl = ctl;
  8588. atomic_inc(&caching_ctl->count);
  8589. break;
  8590. }
  8591. }
  8592. if (caching_ctl)
  8593. list_del_init(&caching_ctl->list);
  8594. up_write(&root->fs_info->commit_root_sem);
  8595. if (caching_ctl) {
  8596. /* Once for the caching bgs list and once for us. */
  8597. put_caching_control(caching_ctl);
  8598. put_caching_control(caching_ctl);
  8599. }
  8600. }
  8601. spin_lock(&trans->transaction->dirty_bgs_lock);
  8602. if (!list_empty(&block_group->dirty_list)) {
  8603. WARN_ON(1);
  8604. }
  8605. if (!list_empty(&block_group->io_list)) {
  8606. WARN_ON(1);
  8607. }
  8608. spin_unlock(&trans->transaction->dirty_bgs_lock);
  8609. btrfs_remove_free_space_cache(block_group);
  8610. spin_lock(&block_group->space_info->lock);
  8611. list_del_init(&block_group->ro_list);
  8612. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  8613. WARN_ON(block_group->space_info->total_bytes
  8614. < block_group->key.offset);
  8615. WARN_ON(block_group->space_info->bytes_readonly
  8616. < block_group->key.offset);
  8617. WARN_ON(block_group->space_info->disk_total
  8618. < block_group->key.offset * factor);
  8619. }
  8620. block_group->space_info->total_bytes -= block_group->key.offset;
  8621. block_group->space_info->bytes_readonly -= block_group->key.offset;
  8622. block_group->space_info->disk_total -= block_group->key.offset * factor;
  8623. spin_unlock(&block_group->space_info->lock);
  8624. memcpy(&key, &block_group->key, sizeof(key));
  8625. lock_chunks(root);
  8626. if (!list_empty(&em->list)) {
  8627. /* We're in the transaction->pending_chunks list. */
  8628. free_extent_map(em);
  8629. }
  8630. spin_lock(&block_group->lock);
  8631. block_group->removed = 1;
  8632. /*
  8633. * At this point trimming can't start on this block group, because we
  8634. * removed the block group from the tree fs_info->block_group_cache_tree
  8635. * so no one can't find it anymore and even if someone already got this
  8636. * block group before we removed it from the rbtree, they have already
  8637. * incremented block_group->trimming - if they didn't, they won't find
  8638. * any free space entries because we already removed them all when we
  8639. * called btrfs_remove_free_space_cache().
  8640. *
  8641. * And we must not remove the extent map from the fs_info->mapping_tree
  8642. * to prevent the same logical address range and physical device space
  8643. * ranges from being reused for a new block group. This is because our
  8644. * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
  8645. * completely transactionless, so while it is trimming a range the
  8646. * currently running transaction might finish and a new one start,
  8647. * allowing for new block groups to be created that can reuse the same
  8648. * physical device locations unless we take this special care.
  8649. */
  8650. remove_em = (atomic_read(&block_group->trimming) == 0);
  8651. /*
  8652. * Make sure a trimmer task always sees the em in the pinned_chunks list
  8653. * if it sees block_group->removed == 1 (needs to lock block_group->lock
  8654. * before checking block_group->removed).
  8655. */
  8656. if (!remove_em) {
  8657. /*
  8658. * Our em might be in trans->transaction->pending_chunks which
  8659. * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
  8660. * and so is the fs_info->pinned_chunks list.
  8661. *
  8662. * So at this point we must be holding the chunk_mutex to avoid
  8663. * any races with chunk allocation (more specifically at
  8664. * volumes.c:contains_pending_extent()), to ensure it always
  8665. * sees the em, either in the pending_chunks list or in the
  8666. * pinned_chunks list.
  8667. */
  8668. list_move_tail(&em->list, &root->fs_info->pinned_chunks);
  8669. }
  8670. spin_unlock(&block_group->lock);
  8671. if (remove_em) {
  8672. struct extent_map_tree *em_tree;
  8673. em_tree = &root->fs_info->mapping_tree.map_tree;
  8674. write_lock(&em_tree->lock);
  8675. /*
  8676. * The em might be in the pending_chunks list, so make sure the
  8677. * chunk mutex is locked, since remove_extent_mapping() will
  8678. * delete us from that list.
  8679. */
  8680. remove_extent_mapping(em_tree, em);
  8681. write_unlock(&em_tree->lock);
  8682. /* once for the tree */
  8683. free_extent_map(em);
  8684. }
  8685. unlock_chunks(root);
  8686. btrfs_put_block_group(block_group);
  8687. btrfs_put_block_group(block_group);
  8688. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  8689. if (ret > 0)
  8690. ret = -EIO;
  8691. if (ret < 0)
  8692. goto out;
  8693. ret = btrfs_del_item(trans, root, path);
  8694. out:
  8695. btrfs_free_path(path);
  8696. return ret;
  8697. }
  8698. /*
  8699. * Process the unused_bgs list and remove any that don't have any allocated
  8700. * space inside of them.
  8701. */
  8702. void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
  8703. {
  8704. struct btrfs_block_group_cache *block_group;
  8705. struct btrfs_space_info *space_info;
  8706. struct btrfs_root *root = fs_info->extent_root;
  8707. struct btrfs_trans_handle *trans;
  8708. int ret = 0;
  8709. if (!fs_info->open)
  8710. return;
  8711. spin_lock(&fs_info->unused_bgs_lock);
  8712. while (!list_empty(&fs_info->unused_bgs)) {
  8713. u64 start, end;
  8714. block_group = list_first_entry(&fs_info->unused_bgs,
  8715. struct btrfs_block_group_cache,
  8716. bg_list);
  8717. space_info = block_group->space_info;
  8718. list_del_init(&block_group->bg_list);
  8719. if (ret || btrfs_mixed_space_info(space_info)) {
  8720. btrfs_put_block_group(block_group);
  8721. continue;
  8722. }
  8723. spin_unlock(&fs_info->unused_bgs_lock);
  8724. mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
  8725. /* Don't want to race with allocators so take the groups_sem */
  8726. down_write(&space_info->groups_sem);
  8727. spin_lock(&block_group->lock);
  8728. if (block_group->reserved ||
  8729. btrfs_block_group_used(&block_group->item) ||
  8730. block_group->ro) {
  8731. /*
  8732. * We want to bail if we made new allocations or have
  8733. * outstanding allocations in this block group. We do
  8734. * the ro check in case balance is currently acting on
  8735. * this block group.
  8736. */
  8737. spin_unlock(&block_group->lock);
  8738. up_write(&space_info->groups_sem);
  8739. goto next;
  8740. }
  8741. spin_unlock(&block_group->lock);
  8742. /* We don't want to force the issue, only flip if it's ok. */
  8743. ret = set_block_group_ro(block_group, 0);
  8744. up_write(&space_info->groups_sem);
  8745. if (ret < 0) {
  8746. ret = 0;
  8747. goto next;
  8748. }
  8749. /*
  8750. * Want to do this before we do anything else so we can recover
  8751. * properly if we fail to join the transaction.
  8752. */
  8753. /* 1 for btrfs_orphan_reserve_metadata() */
  8754. trans = btrfs_start_transaction(root, 1);
  8755. if (IS_ERR(trans)) {
  8756. btrfs_set_block_group_rw(root, block_group);
  8757. ret = PTR_ERR(trans);
  8758. goto next;
  8759. }
  8760. /*
  8761. * We could have pending pinned extents for this block group,
  8762. * just delete them, we don't care about them anymore.
  8763. */
  8764. start = block_group->key.objectid;
  8765. end = start + block_group->key.offset - 1;
  8766. /*
  8767. * Hold the unused_bg_unpin_mutex lock to avoid racing with
  8768. * btrfs_finish_extent_commit(). If we are at transaction N,
  8769. * another task might be running finish_extent_commit() for the
  8770. * previous transaction N - 1, and have seen a range belonging
  8771. * to the block group in freed_extents[] before we were able to
  8772. * clear the whole block group range from freed_extents[]. This
  8773. * means that task can lookup for the block group after we
  8774. * unpinned it from freed_extents[] and removed it, leading to
  8775. * a BUG_ON() at btrfs_unpin_extent_range().
  8776. */
  8777. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  8778. ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
  8779. EXTENT_DIRTY, GFP_NOFS);
  8780. if (ret) {
  8781. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  8782. btrfs_set_block_group_rw(root, block_group);
  8783. goto end_trans;
  8784. }
  8785. ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
  8786. EXTENT_DIRTY, GFP_NOFS);
  8787. if (ret) {
  8788. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  8789. btrfs_set_block_group_rw(root, block_group);
  8790. goto end_trans;
  8791. }
  8792. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  8793. /* Reset pinned so btrfs_put_block_group doesn't complain */
  8794. spin_lock(&space_info->lock);
  8795. spin_lock(&block_group->lock);
  8796. space_info->bytes_pinned -= block_group->pinned;
  8797. space_info->bytes_readonly += block_group->pinned;
  8798. percpu_counter_add(&space_info->total_bytes_pinned,
  8799. -block_group->pinned);
  8800. block_group->pinned = 0;
  8801. spin_unlock(&block_group->lock);
  8802. spin_unlock(&space_info->lock);
  8803. /*
  8804. * Btrfs_remove_chunk will abort the transaction if things go
  8805. * horribly wrong.
  8806. */
  8807. ret = btrfs_remove_chunk(trans, root,
  8808. block_group->key.objectid);
  8809. end_trans:
  8810. btrfs_end_transaction(trans, root);
  8811. next:
  8812. mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
  8813. btrfs_put_block_group(block_group);
  8814. spin_lock(&fs_info->unused_bgs_lock);
  8815. }
  8816. spin_unlock(&fs_info->unused_bgs_lock);
  8817. }
  8818. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  8819. {
  8820. struct btrfs_space_info *space_info;
  8821. struct btrfs_super_block *disk_super;
  8822. u64 features;
  8823. u64 flags;
  8824. int mixed = 0;
  8825. int ret;
  8826. disk_super = fs_info->super_copy;
  8827. if (!btrfs_super_root(disk_super))
  8828. return 1;
  8829. features = btrfs_super_incompat_flags(disk_super);
  8830. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  8831. mixed = 1;
  8832. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  8833. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  8834. if (ret)
  8835. goto out;
  8836. if (mixed) {
  8837. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  8838. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  8839. } else {
  8840. flags = BTRFS_BLOCK_GROUP_METADATA;
  8841. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  8842. if (ret)
  8843. goto out;
  8844. flags = BTRFS_BLOCK_GROUP_DATA;
  8845. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  8846. }
  8847. out:
  8848. return ret;
  8849. }
  8850. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  8851. {
  8852. return unpin_extent_range(root, start, end, false);
  8853. }
  8854. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  8855. {
  8856. struct btrfs_fs_info *fs_info = root->fs_info;
  8857. struct btrfs_block_group_cache *cache = NULL;
  8858. u64 group_trimmed;
  8859. u64 start;
  8860. u64 end;
  8861. u64 trimmed = 0;
  8862. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  8863. int ret = 0;
  8864. /*
  8865. * try to trim all FS space, our block group may start from non-zero.
  8866. */
  8867. if (range->len == total_bytes)
  8868. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  8869. else
  8870. cache = btrfs_lookup_block_group(fs_info, range->start);
  8871. while (cache) {
  8872. if (cache->key.objectid >= (range->start + range->len)) {
  8873. btrfs_put_block_group(cache);
  8874. break;
  8875. }
  8876. start = max(range->start, cache->key.objectid);
  8877. end = min(range->start + range->len,
  8878. cache->key.objectid + cache->key.offset);
  8879. if (end - start >= range->minlen) {
  8880. if (!block_group_cache_done(cache)) {
  8881. ret = cache_block_group(cache, 0);
  8882. if (ret) {
  8883. btrfs_put_block_group(cache);
  8884. break;
  8885. }
  8886. ret = wait_block_group_cache_done(cache);
  8887. if (ret) {
  8888. btrfs_put_block_group(cache);
  8889. break;
  8890. }
  8891. }
  8892. ret = btrfs_trim_block_group(cache,
  8893. &group_trimmed,
  8894. start,
  8895. end,
  8896. range->minlen);
  8897. trimmed += group_trimmed;
  8898. if (ret) {
  8899. btrfs_put_block_group(cache);
  8900. break;
  8901. }
  8902. }
  8903. cache = next_block_group(fs_info->tree_root, cache);
  8904. }
  8905. range->len = trimmed;
  8906. return ret;
  8907. }
  8908. /*
  8909. * btrfs_{start,end}_write_no_snapshoting() are similar to
  8910. * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
  8911. * data into the page cache through nocow before the subvolume is snapshoted,
  8912. * but flush the data into disk after the snapshot creation, or to prevent
  8913. * operations while snapshoting is ongoing and that cause the snapshot to be
  8914. * inconsistent (writes followed by expanding truncates for example).
  8915. */
  8916. void btrfs_end_write_no_snapshoting(struct btrfs_root *root)
  8917. {
  8918. percpu_counter_dec(&root->subv_writers->counter);
  8919. /*
  8920. * Make sure counter is updated before we wake up
  8921. * waiters.
  8922. */
  8923. smp_mb();
  8924. if (waitqueue_active(&root->subv_writers->wait))
  8925. wake_up(&root->subv_writers->wait);
  8926. }
  8927. int btrfs_start_write_no_snapshoting(struct btrfs_root *root)
  8928. {
  8929. if (atomic_read(&root->will_be_snapshoted))
  8930. return 0;
  8931. percpu_counter_inc(&root->subv_writers->counter);
  8932. /*
  8933. * Make sure counter is updated before we check for snapshot creation.
  8934. */
  8935. smp_mb();
  8936. if (atomic_read(&root->will_be_snapshoted)) {
  8937. btrfs_end_write_no_snapshoting(root);
  8938. return 0;
  8939. }
  8940. return 1;
  8941. }