extent-tree.c 215 KB

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