extent-tree.c 257 KB

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