extent-tree.c 228 KB

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