extent-tree.c 233 KB

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