extent-tree.c 211 KB

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