extent-tree.c 222 KB

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