extent-tree.c 228 KB

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