extent-tree.c 214 KB

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