extent-tree.c 223 KB

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