extent-tree.c 223 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392
  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. u64 to_free = 0;
  4162. unsigned dropped;
  4163. /* If we are a free space inode we need to not flush since we will be in
  4164. * the middle of a transaction commit. We also don't need the delalloc
  4165. * mutex since we won't race with anybody. We need this mostly to make
  4166. * lockdep shut its filthy mouth.
  4167. */
  4168. if (btrfs_is_free_space_inode(inode)) {
  4169. flush = BTRFS_RESERVE_NO_FLUSH;
  4170. delalloc_lock = false;
  4171. }
  4172. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4173. btrfs_transaction_in_commit(root->fs_info))
  4174. schedule_timeout(1);
  4175. if (delalloc_lock)
  4176. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4177. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4178. spin_lock(&BTRFS_I(inode)->lock);
  4179. BTRFS_I(inode)->outstanding_extents++;
  4180. if (BTRFS_I(inode)->outstanding_extents >
  4181. BTRFS_I(inode)->reserved_extents)
  4182. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4183. BTRFS_I(inode)->reserved_extents;
  4184. /*
  4185. * Add an item to reserve for updating the inode when we complete the
  4186. * delalloc io.
  4187. */
  4188. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4189. &BTRFS_I(inode)->runtime_flags)) {
  4190. nr_extents++;
  4191. extra_reserve = 1;
  4192. }
  4193. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4194. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4195. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4196. spin_unlock(&BTRFS_I(inode)->lock);
  4197. if (root->fs_info->quota_enabled) {
  4198. ret = btrfs_qgroup_reserve(root, num_bytes +
  4199. nr_extents * root->leafsize);
  4200. if (ret)
  4201. goto out_fail;
  4202. }
  4203. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4204. if (unlikely(ret)) {
  4205. if (root->fs_info->quota_enabled)
  4206. btrfs_qgroup_free(root, num_bytes +
  4207. nr_extents * root->leafsize);
  4208. goto out_fail;
  4209. }
  4210. spin_lock(&BTRFS_I(inode)->lock);
  4211. if (extra_reserve) {
  4212. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4213. &BTRFS_I(inode)->runtime_flags);
  4214. nr_extents--;
  4215. }
  4216. BTRFS_I(inode)->reserved_extents += nr_extents;
  4217. spin_unlock(&BTRFS_I(inode)->lock);
  4218. if (delalloc_lock)
  4219. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4220. if (to_reserve)
  4221. trace_btrfs_space_reservation(root->fs_info,"delalloc",
  4222. btrfs_ino(inode), to_reserve, 1);
  4223. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4224. return 0;
  4225. out_fail:
  4226. spin_lock(&BTRFS_I(inode)->lock);
  4227. dropped = drop_outstanding_extent(inode);
  4228. /*
  4229. * If the inodes csum_bytes is the same as the original
  4230. * csum_bytes then we know we haven't raced with any free()ers
  4231. * so we can just reduce our inodes csum bytes and carry on.
  4232. * Otherwise we have to do the normal free thing to account for
  4233. * the case that the free side didn't free up its reserve
  4234. * because of this outstanding reservation.
  4235. */
  4236. if (BTRFS_I(inode)->csum_bytes == csum_bytes)
  4237. calc_csum_metadata_size(inode, num_bytes, 0);
  4238. else
  4239. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4240. spin_unlock(&BTRFS_I(inode)->lock);
  4241. if (dropped)
  4242. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4243. if (to_free) {
  4244. btrfs_block_rsv_release(root, block_rsv, to_free);
  4245. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4246. btrfs_ino(inode), to_free, 0);
  4247. }
  4248. if (delalloc_lock)
  4249. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4250. return ret;
  4251. }
  4252. /**
  4253. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4254. * @inode: the inode to release the reservation for
  4255. * @num_bytes: the number of bytes we're releasing
  4256. *
  4257. * This will release the metadata reservation for an inode. This can be called
  4258. * once we complete IO for a given set of bytes to release their metadata
  4259. * reservations.
  4260. */
  4261. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4262. {
  4263. struct btrfs_root *root = BTRFS_I(inode)->root;
  4264. u64 to_free = 0;
  4265. unsigned dropped;
  4266. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4267. spin_lock(&BTRFS_I(inode)->lock);
  4268. dropped = drop_outstanding_extent(inode);
  4269. if (num_bytes)
  4270. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4271. spin_unlock(&BTRFS_I(inode)->lock);
  4272. if (dropped > 0)
  4273. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4274. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4275. btrfs_ino(inode), to_free, 0);
  4276. if (root->fs_info->quota_enabled) {
  4277. btrfs_qgroup_free(root, num_bytes +
  4278. dropped * root->leafsize);
  4279. }
  4280. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4281. to_free);
  4282. }
  4283. /**
  4284. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4285. * @inode: inode we're writing to
  4286. * @num_bytes: the number of bytes we want to allocate
  4287. *
  4288. * This will do the following things
  4289. *
  4290. * o reserve space in the data space info for num_bytes
  4291. * o reserve space in the metadata space info based on number of outstanding
  4292. * extents and how much csums will be needed
  4293. * o add to the inodes ->delalloc_bytes
  4294. * o add it to the fs_info's delalloc inodes list.
  4295. *
  4296. * This will return 0 for success and -ENOSPC if there is no space left.
  4297. */
  4298. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4299. {
  4300. int ret;
  4301. ret = btrfs_check_data_free_space(inode, num_bytes);
  4302. if (ret)
  4303. return ret;
  4304. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4305. if (ret) {
  4306. btrfs_free_reserved_data_space(inode, num_bytes);
  4307. return ret;
  4308. }
  4309. return 0;
  4310. }
  4311. /**
  4312. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4313. * @inode: inode we're releasing space for
  4314. * @num_bytes: the number of bytes we want to free up
  4315. *
  4316. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4317. * called in the case that we don't need the metadata AND data reservations
  4318. * anymore. So if there is an error or we insert an inline extent.
  4319. *
  4320. * This function will release the metadata space that was not used and will
  4321. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4322. * list if there are no delalloc bytes left.
  4323. */
  4324. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4325. {
  4326. btrfs_delalloc_release_metadata(inode, num_bytes);
  4327. btrfs_free_reserved_data_space(inode, num_bytes);
  4328. }
  4329. static int update_block_group(struct btrfs_root *root,
  4330. u64 bytenr, u64 num_bytes, int alloc)
  4331. {
  4332. struct btrfs_block_group_cache *cache = NULL;
  4333. struct btrfs_fs_info *info = root->fs_info;
  4334. u64 total = num_bytes;
  4335. u64 old_val;
  4336. u64 byte_in_group;
  4337. int factor;
  4338. /* block accounting for super block */
  4339. spin_lock(&info->delalloc_lock);
  4340. old_val = btrfs_super_bytes_used(info->super_copy);
  4341. if (alloc)
  4342. old_val += num_bytes;
  4343. else
  4344. old_val -= num_bytes;
  4345. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4346. spin_unlock(&info->delalloc_lock);
  4347. while (total) {
  4348. cache = btrfs_lookup_block_group(info, bytenr);
  4349. if (!cache)
  4350. return -ENOENT;
  4351. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4352. BTRFS_BLOCK_GROUP_RAID1 |
  4353. BTRFS_BLOCK_GROUP_RAID10))
  4354. factor = 2;
  4355. else
  4356. factor = 1;
  4357. /*
  4358. * If this block group has free space cache written out, we
  4359. * need to make sure to load it if we are removing space. This
  4360. * is because we need the unpinning stage to actually add the
  4361. * space back to the block group, otherwise we will leak space.
  4362. */
  4363. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4364. cache_block_group(cache, 1);
  4365. byte_in_group = bytenr - cache->key.objectid;
  4366. WARN_ON(byte_in_group > cache->key.offset);
  4367. spin_lock(&cache->space_info->lock);
  4368. spin_lock(&cache->lock);
  4369. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4370. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4371. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4372. cache->dirty = 1;
  4373. old_val = btrfs_block_group_used(&cache->item);
  4374. num_bytes = min(total, cache->key.offset - byte_in_group);
  4375. if (alloc) {
  4376. old_val += num_bytes;
  4377. btrfs_set_block_group_used(&cache->item, old_val);
  4378. cache->reserved -= num_bytes;
  4379. cache->space_info->bytes_reserved -= num_bytes;
  4380. cache->space_info->bytes_used += num_bytes;
  4381. cache->space_info->disk_used += num_bytes * factor;
  4382. spin_unlock(&cache->lock);
  4383. spin_unlock(&cache->space_info->lock);
  4384. } else {
  4385. old_val -= num_bytes;
  4386. btrfs_set_block_group_used(&cache->item, old_val);
  4387. cache->pinned += num_bytes;
  4388. cache->space_info->bytes_pinned += num_bytes;
  4389. cache->space_info->bytes_used -= num_bytes;
  4390. cache->space_info->disk_used -= num_bytes * factor;
  4391. spin_unlock(&cache->lock);
  4392. spin_unlock(&cache->space_info->lock);
  4393. set_extent_dirty(info->pinned_extents,
  4394. bytenr, bytenr + num_bytes - 1,
  4395. GFP_NOFS | __GFP_NOFAIL);
  4396. }
  4397. btrfs_put_block_group(cache);
  4398. total -= num_bytes;
  4399. bytenr += num_bytes;
  4400. }
  4401. return 0;
  4402. }
  4403. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4404. {
  4405. struct btrfs_block_group_cache *cache;
  4406. u64 bytenr;
  4407. spin_lock(&root->fs_info->block_group_cache_lock);
  4408. bytenr = root->fs_info->first_logical_byte;
  4409. spin_unlock(&root->fs_info->block_group_cache_lock);
  4410. if (bytenr < (u64)-1)
  4411. return bytenr;
  4412. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4413. if (!cache)
  4414. return 0;
  4415. bytenr = cache->key.objectid;
  4416. btrfs_put_block_group(cache);
  4417. return bytenr;
  4418. }
  4419. static int pin_down_extent(struct btrfs_root *root,
  4420. struct btrfs_block_group_cache *cache,
  4421. u64 bytenr, u64 num_bytes, int reserved)
  4422. {
  4423. spin_lock(&cache->space_info->lock);
  4424. spin_lock(&cache->lock);
  4425. cache->pinned += num_bytes;
  4426. cache->space_info->bytes_pinned += num_bytes;
  4427. if (reserved) {
  4428. cache->reserved -= num_bytes;
  4429. cache->space_info->bytes_reserved -= num_bytes;
  4430. }
  4431. spin_unlock(&cache->lock);
  4432. spin_unlock(&cache->space_info->lock);
  4433. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4434. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4435. return 0;
  4436. }
  4437. /*
  4438. * this function must be called within transaction
  4439. */
  4440. int btrfs_pin_extent(struct btrfs_root *root,
  4441. u64 bytenr, u64 num_bytes, int reserved)
  4442. {
  4443. struct btrfs_block_group_cache *cache;
  4444. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4445. BUG_ON(!cache); /* Logic error */
  4446. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4447. btrfs_put_block_group(cache);
  4448. return 0;
  4449. }
  4450. /*
  4451. * this function must be called within transaction
  4452. */
  4453. int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
  4454. u64 bytenr, u64 num_bytes)
  4455. {
  4456. struct btrfs_block_group_cache *cache;
  4457. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4458. BUG_ON(!cache); /* Logic error */
  4459. /*
  4460. * pull in the free space cache (if any) so that our pin
  4461. * removes the free space from the cache. We have load_only set
  4462. * to one because the slow code to read in the free extents does check
  4463. * the pinned extents.
  4464. */
  4465. cache_block_group(cache, 1);
  4466. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4467. /* remove us from the free space cache (if we're there at all) */
  4468. btrfs_remove_free_space(cache, bytenr, num_bytes);
  4469. btrfs_put_block_group(cache);
  4470. return 0;
  4471. }
  4472. /**
  4473. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4474. * @cache: The cache we are manipulating
  4475. * @num_bytes: The number of bytes in question
  4476. * @reserve: One of the reservation enums
  4477. *
  4478. * This is called by the allocator when it reserves space, or by somebody who is
  4479. * freeing space that was never actually used on disk. For example if you
  4480. * reserve some space for a new leaf in transaction A and before transaction A
  4481. * commits you free that leaf, you call this with reserve set to 0 in order to
  4482. * clear the reservation.
  4483. *
  4484. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4485. * ENOSPC accounting. For data we handle the reservation through clearing the
  4486. * delalloc bits in the io_tree. We have to do this since we could end up
  4487. * allocating less disk space for the amount of data we have reserved in the
  4488. * case of compression.
  4489. *
  4490. * If this is a reservation and the block group has become read only we cannot
  4491. * make the reservation and return -EAGAIN, otherwise this function always
  4492. * succeeds.
  4493. */
  4494. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4495. u64 num_bytes, int reserve)
  4496. {
  4497. struct btrfs_space_info *space_info = cache->space_info;
  4498. int ret = 0;
  4499. spin_lock(&space_info->lock);
  4500. spin_lock(&cache->lock);
  4501. if (reserve != RESERVE_FREE) {
  4502. if (cache->ro) {
  4503. ret = -EAGAIN;
  4504. } else {
  4505. cache->reserved += num_bytes;
  4506. space_info->bytes_reserved += num_bytes;
  4507. if (reserve == RESERVE_ALLOC) {
  4508. trace_btrfs_space_reservation(cache->fs_info,
  4509. "space_info", space_info->flags,
  4510. num_bytes, 0);
  4511. space_info->bytes_may_use -= num_bytes;
  4512. }
  4513. }
  4514. } else {
  4515. if (cache->ro)
  4516. space_info->bytes_readonly += num_bytes;
  4517. cache->reserved -= num_bytes;
  4518. space_info->bytes_reserved -= num_bytes;
  4519. space_info->reservation_progress++;
  4520. }
  4521. spin_unlock(&cache->lock);
  4522. spin_unlock(&space_info->lock);
  4523. return ret;
  4524. }
  4525. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4526. struct btrfs_root *root)
  4527. {
  4528. struct btrfs_fs_info *fs_info = root->fs_info;
  4529. struct btrfs_caching_control *next;
  4530. struct btrfs_caching_control *caching_ctl;
  4531. struct btrfs_block_group_cache *cache;
  4532. down_write(&fs_info->extent_commit_sem);
  4533. list_for_each_entry_safe(caching_ctl, next,
  4534. &fs_info->caching_block_groups, list) {
  4535. cache = caching_ctl->block_group;
  4536. if (block_group_cache_done(cache)) {
  4537. cache->last_byte_to_unpin = (u64)-1;
  4538. list_del_init(&caching_ctl->list);
  4539. put_caching_control(caching_ctl);
  4540. } else {
  4541. cache->last_byte_to_unpin = caching_ctl->progress;
  4542. }
  4543. }
  4544. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4545. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4546. else
  4547. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4548. up_write(&fs_info->extent_commit_sem);
  4549. update_global_block_rsv(fs_info);
  4550. }
  4551. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4552. {
  4553. struct btrfs_fs_info *fs_info = root->fs_info;
  4554. struct btrfs_block_group_cache *cache = NULL;
  4555. struct btrfs_space_info *space_info;
  4556. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4557. u64 len;
  4558. bool readonly;
  4559. while (start <= end) {
  4560. readonly = false;
  4561. if (!cache ||
  4562. start >= cache->key.objectid + cache->key.offset) {
  4563. if (cache)
  4564. btrfs_put_block_group(cache);
  4565. cache = btrfs_lookup_block_group(fs_info, start);
  4566. BUG_ON(!cache); /* Logic error */
  4567. }
  4568. len = cache->key.objectid + cache->key.offset - start;
  4569. len = min(len, end + 1 - start);
  4570. if (start < cache->last_byte_to_unpin) {
  4571. len = min(len, cache->last_byte_to_unpin - start);
  4572. btrfs_add_free_space(cache, start, len);
  4573. }
  4574. start += len;
  4575. space_info = cache->space_info;
  4576. spin_lock(&space_info->lock);
  4577. spin_lock(&cache->lock);
  4578. cache->pinned -= len;
  4579. space_info->bytes_pinned -= len;
  4580. if (cache->ro) {
  4581. space_info->bytes_readonly += len;
  4582. readonly = true;
  4583. }
  4584. spin_unlock(&cache->lock);
  4585. if (!readonly && global_rsv->space_info == space_info) {
  4586. spin_lock(&global_rsv->lock);
  4587. if (!global_rsv->full) {
  4588. len = min(len, global_rsv->size -
  4589. global_rsv->reserved);
  4590. global_rsv->reserved += len;
  4591. space_info->bytes_may_use += len;
  4592. if (global_rsv->reserved >= global_rsv->size)
  4593. global_rsv->full = 1;
  4594. }
  4595. spin_unlock(&global_rsv->lock);
  4596. }
  4597. spin_unlock(&space_info->lock);
  4598. }
  4599. if (cache)
  4600. btrfs_put_block_group(cache);
  4601. return 0;
  4602. }
  4603. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4604. struct btrfs_root *root)
  4605. {
  4606. struct btrfs_fs_info *fs_info = root->fs_info;
  4607. struct extent_io_tree *unpin;
  4608. u64 start;
  4609. u64 end;
  4610. int ret;
  4611. if (trans->aborted)
  4612. return 0;
  4613. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4614. unpin = &fs_info->freed_extents[1];
  4615. else
  4616. unpin = &fs_info->freed_extents[0];
  4617. while (1) {
  4618. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4619. EXTENT_DIRTY, NULL);
  4620. if (ret)
  4621. break;
  4622. if (btrfs_test_opt(root, DISCARD))
  4623. ret = btrfs_discard_extent(root, start,
  4624. end + 1 - start, NULL);
  4625. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4626. unpin_extent_range(root, start, end);
  4627. cond_resched();
  4628. }
  4629. return 0;
  4630. }
  4631. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4632. struct btrfs_root *root,
  4633. u64 bytenr, u64 num_bytes, u64 parent,
  4634. u64 root_objectid, u64 owner_objectid,
  4635. u64 owner_offset, int refs_to_drop,
  4636. struct btrfs_delayed_extent_op *extent_op)
  4637. {
  4638. struct btrfs_key key;
  4639. struct btrfs_path *path;
  4640. struct btrfs_fs_info *info = root->fs_info;
  4641. struct btrfs_root *extent_root = info->extent_root;
  4642. struct extent_buffer *leaf;
  4643. struct btrfs_extent_item *ei;
  4644. struct btrfs_extent_inline_ref *iref;
  4645. int ret;
  4646. int is_data;
  4647. int extent_slot = 0;
  4648. int found_extent = 0;
  4649. int num_to_del = 1;
  4650. u32 item_size;
  4651. u64 refs;
  4652. path = btrfs_alloc_path();
  4653. if (!path)
  4654. return -ENOMEM;
  4655. path->reada = 1;
  4656. path->leave_spinning = 1;
  4657. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4658. BUG_ON(!is_data && refs_to_drop != 1);
  4659. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4660. bytenr, num_bytes, parent,
  4661. root_objectid, owner_objectid,
  4662. owner_offset);
  4663. if (ret == 0) {
  4664. extent_slot = path->slots[0];
  4665. while (extent_slot >= 0) {
  4666. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4667. extent_slot);
  4668. if (key.objectid != bytenr)
  4669. break;
  4670. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4671. key.offset == num_bytes) {
  4672. found_extent = 1;
  4673. break;
  4674. }
  4675. if (path->slots[0] - extent_slot > 5)
  4676. break;
  4677. extent_slot--;
  4678. }
  4679. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4680. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4681. if (found_extent && item_size < sizeof(*ei))
  4682. found_extent = 0;
  4683. #endif
  4684. if (!found_extent) {
  4685. BUG_ON(iref);
  4686. ret = remove_extent_backref(trans, extent_root, path,
  4687. NULL, refs_to_drop,
  4688. is_data);
  4689. if (ret) {
  4690. btrfs_abort_transaction(trans, extent_root, ret);
  4691. goto out;
  4692. }
  4693. btrfs_release_path(path);
  4694. path->leave_spinning = 1;
  4695. key.objectid = bytenr;
  4696. key.type = BTRFS_EXTENT_ITEM_KEY;
  4697. key.offset = num_bytes;
  4698. ret = btrfs_search_slot(trans, extent_root,
  4699. &key, path, -1, 1);
  4700. if (ret) {
  4701. printk(KERN_ERR "umm, got %d back from search"
  4702. ", was looking for %llu\n", ret,
  4703. (unsigned long long)bytenr);
  4704. if (ret > 0)
  4705. btrfs_print_leaf(extent_root,
  4706. path->nodes[0]);
  4707. }
  4708. if (ret < 0) {
  4709. btrfs_abort_transaction(trans, extent_root, ret);
  4710. goto out;
  4711. }
  4712. extent_slot = path->slots[0];
  4713. }
  4714. } else if (ret == -ENOENT) {
  4715. btrfs_print_leaf(extent_root, path->nodes[0]);
  4716. WARN_ON(1);
  4717. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  4718. "parent %llu root %llu owner %llu offset %llu\n",
  4719. (unsigned long long)bytenr,
  4720. (unsigned long long)parent,
  4721. (unsigned long long)root_objectid,
  4722. (unsigned long long)owner_objectid,
  4723. (unsigned long long)owner_offset);
  4724. } else {
  4725. btrfs_abort_transaction(trans, extent_root, ret);
  4726. goto out;
  4727. }
  4728. leaf = path->nodes[0];
  4729. item_size = btrfs_item_size_nr(leaf, extent_slot);
  4730. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4731. if (item_size < sizeof(*ei)) {
  4732. BUG_ON(found_extent || extent_slot != path->slots[0]);
  4733. ret = convert_extent_item_v0(trans, extent_root, path,
  4734. owner_objectid, 0);
  4735. if (ret < 0) {
  4736. btrfs_abort_transaction(trans, extent_root, ret);
  4737. goto out;
  4738. }
  4739. btrfs_release_path(path);
  4740. path->leave_spinning = 1;
  4741. key.objectid = bytenr;
  4742. key.type = BTRFS_EXTENT_ITEM_KEY;
  4743. key.offset = num_bytes;
  4744. ret = btrfs_search_slot(trans, extent_root, &key, path,
  4745. -1, 1);
  4746. if (ret) {
  4747. printk(KERN_ERR "umm, got %d back from search"
  4748. ", was looking for %llu\n", ret,
  4749. (unsigned long long)bytenr);
  4750. btrfs_print_leaf(extent_root, path->nodes[0]);
  4751. }
  4752. if (ret < 0) {
  4753. btrfs_abort_transaction(trans, extent_root, ret);
  4754. goto out;
  4755. }
  4756. extent_slot = path->slots[0];
  4757. leaf = path->nodes[0];
  4758. item_size = btrfs_item_size_nr(leaf, extent_slot);
  4759. }
  4760. #endif
  4761. BUG_ON(item_size < sizeof(*ei));
  4762. ei = btrfs_item_ptr(leaf, extent_slot,
  4763. struct btrfs_extent_item);
  4764. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4765. struct btrfs_tree_block_info *bi;
  4766. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  4767. bi = (struct btrfs_tree_block_info *)(ei + 1);
  4768. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  4769. }
  4770. refs = btrfs_extent_refs(leaf, ei);
  4771. BUG_ON(refs < refs_to_drop);
  4772. refs -= refs_to_drop;
  4773. if (refs > 0) {
  4774. if (extent_op)
  4775. __run_delayed_extent_op(extent_op, leaf, ei);
  4776. /*
  4777. * In the case of inline back ref, reference count will
  4778. * be updated by remove_extent_backref
  4779. */
  4780. if (iref) {
  4781. BUG_ON(!found_extent);
  4782. } else {
  4783. btrfs_set_extent_refs(leaf, ei, refs);
  4784. btrfs_mark_buffer_dirty(leaf);
  4785. }
  4786. if (found_extent) {
  4787. ret = remove_extent_backref(trans, extent_root, path,
  4788. iref, refs_to_drop,
  4789. is_data);
  4790. if (ret) {
  4791. btrfs_abort_transaction(trans, extent_root, ret);
  4792. goto out;
  4793. }
  4794. }
  4795. } else {
  4796. if (found_extent) {
  4797. BUG_ON(is_data && refs_to_drop !=
  4798. extent_data_ref_count(root, path, iref));
  4799. if (iref) {
  4800. BUG_ON(path->slots[0] != extent_slot);
  4801. } else {
  4802. BUG_ON(path->slots[0] != extent_slot + 1);
  4803. path->slots[0] = extent_slot;
  4804. num_to_del = 2;
  4805. }
  4806. }
  4807. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  4808. num_to_del);
  4809. if (ret) {
  4810. btrfs_abort_transaction(trans, extent_root, ret);
  4811. goto out;
  4812. }
  4813. btrfs_release_path(path);
  4814. if (is_data) {
  4815. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  4816. if (ret) {
  4817. btrfs_abort_transaction(trans, extent_root, ret);
  4818. goto out;
  4819. }
  4820. }
  4821. ret = update_block_group(root, bytenr, num_bytes, 0);
  4822. if (ret) {
  4823. btrfs_abort_transaction(trans, extent_root, ret);
  4824. goto out;
  4825. }
  4826. }
  4827. out:
  4828. btrfs_free_path(path);
  4829. return ret;
  4830. }
  4831. /*
  4832. * when we free an block, it is possible (and likely) that we free the last
  4833. * delayed ref for that extent as well. This searches the delayed ref tree for
  4834. * a given extent, and if there are no other delayed refs to be processed, it
  4835. * removes it from the tree.
  4836. */
  4837. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  4838. struct btrfs_root *root, u64 bytenr)
  4839. {
  4840. struct btrfs_delayed_ref_head *head;
  4841. struct btrfs_delayed_ref_root *delayed_refs;
  4842. struct btrfs_delayed_ref_node *ref;
  4843. struct rb_node *node;
  4844. int ret = 0;
  4845. delayed_refs = &trans->transaction->delayed_refs;
  4846. spin_lock(&delayed_refs->lock);
  4847. head = btrfs_find_delayed_ref_head(trans, bytenr);
  4848. if (!head)
  4849. goto out;
  4850. node = rb_prev(&head->node.rb_node);
  4851. if (!node)
  4852. goto out;
  4853. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  4854. /* there are still entries for this ref, we can't drop it */
  4855. if (ref->bytenr == bytenr)
  4856. goto out;
  4857. if (head->extent_op) {
  4858. if (!head->must_insert_reserved)
  4859. goto out;
  4860. btrfs_free_delayed_extent_op(head->extent_op);
  4861. head->extent_op = NULL;
  4862. }
  4863. /*
  4864. * waiting for the lock here would deadlock. If someone else has it
  4865. * locked they are already in the process of dropping it anyway
  4866. */
  4867. if (!mutex_trylock(&head->mutex))
  4868. goto out;
  4869. /*
  4870. * at this point we have a head with no other entries. Go
  4871. * ahead and process it.
  4872. */
  4873. head->node.in_tree = 0;
  4874. rb_erase(&head->node.rb_node, &delayed_refs->root);
  4875. delayed_refs->num_entries--;
  4876. /*
  4877. * we don't take a ref on the node because we're removing it from the
  4878. * tree, so we just steal the ref the tree was holding.
  4879. */
  4880. delayed_refs->num_heads--;
  4881. if (list_empty(&head->cluster))
  4882. delayed_refs->num_heads_ready--;
  4883. list_del_init(&head->cluster);
  4884. spin_unlock(&delayed_refs->lock);
  4885. BUG_ON(head->extent_op);
  4886. if (head->must_insert_reserved)
  4887. ret = 1;
  4888. mutex_unlock(&head->mutex);
  4889. btrfs_put_delayed_ref(&head->node);
  4890. return ret;
  4891. out:
  4892. spin_unlock(&delayed_refs->lock);
  4893. return 0;
  4894. }
  4895. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  4896. struct btrfs_root *root,
  4897. struct extent_buffer *buf,
  4898. u64 parent, int last_ref)
  4899. {
  4900. struct btrfs_block_group_cache *cache = NULL;
  4901. int ret;
  4902. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4903. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  4904. buf->start, buf->len,
  4905. parent, root->root_key.objectid,
  4906. btrfs_header_level(buf),
  4907. BTRFS_DROP_DELAYED_REF, NULL, 0);
  4908. BUG_ON(ret); /* -ENOMEM */
  4909. }
  4910. if (!last_ref)
  4911. return;
  4912. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  4913. if (btrfs_header_generation(buf) == trans->transid) {
  4914. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4915. ret = check_ref_cleanup(trans, root, buf->start);
  4916. if (!ret)
  4917. goto out;
  4918. }
  4919. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  4920. pin_down_extent(root, cache, buf->start, buf->len, 1);
  4921. goto out;
  4922. }
  4923. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  4924. btrfs_add_free_space(cache, buf->start, buf->len);
  4925. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  4926. }
  4927. out:
  4928. /*
  4929. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  4930. * anymore.
  4931. */
  4932. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  4933. btrfs_put_block_group(cache);
  4934. }
  4935. /* Can return -ENOMEM */
  4936. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4937. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  4938. u64 owner, u64 offset, int for_cow)
  4939. {
  4940. int ret;
  4941. struct btrfs_fs_info *fs_info = root->fs_info;
  4942. /*
  4943. * tree log blocks never actually go into the extent allocation
  4944. * tree, just update pinning info and exit early.
  4945. */
  4946. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  4947. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  4948. /* unlocks the pinned mutex */
  4949. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  4950. ret = 0;
  4951. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4952. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  4953. num_bytes,
  4954. parent, root_objectid, (int)owner,
  4955. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  4956. } else {
  4957. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  4958. num_bytes,
  4959. parent, root_objectid, owner,
  4960. offset, BTRFS_DROP_DELAYED_REF,
  4961. NULL, for_cow);
  4962. }
  4963. return ret;
  4964. }
  4965. static u64 stripe_align(struct btrfs_root *root,
  4966. struct btrfs_block_group_cache *cache,
  4967. u64 val, u64 num_bytes)
  4968. {
  4969. u64 ret = ALIGN(val, root->stripesize);
  4970. return ret;
  4971. }
  4972. /*
  4973. * when we wait for progress in the block group caching, its because
  4974. * our allocation attempt failed at least once. So, we must sleep
  4975. * and let some progress happen before we try again.
  4976. *
  4977. * This function will sleep at least once waiting for new free space to
  4978. * show up, and then it will check the block group free space numbers
  4979. * for our min num_bytes. Another option is to have it go ahead
  4980. * and look in the rbtree for a free extent of a given size, but this
  4981. * is a good start.
  4982. */
  4983. static noinline int
  4984. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  4985. u64 num_bytes)
  4986. {
  4987. struct btrfs_caching_control *caching_ctl;
  4988. caching_ctl = get_caching_control(cache);
  4989. if (!caching_ctl)
  4990. return 0;
  4991. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  4992. (cache->free_space_ctl->free_space >= num_bytes));
  4993. put_caching_control(caching_ctl);
  4994. return 0;
  4995. }
  4996. static noinline int
  4997. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  4998. {
  4999. struct btrfs_caching_control *caching_ctl;
  5000. caching_ctl = get_caching_control(cache);
  5001. if (!caching_ctl)
  5002. return 0;
  5003. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  5004. put_caching_control(caching_ctl);
  5005. return 0;
  5006. }
  5007. int __get_raid_index(u64 flags)
  5008. {
  5009. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  5010. return BTRFS_RAID_RAID10;
  5011. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  5012. return BTRFS_RAID_RAID1;
  5013. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  5014. return BTRFS_RAID_DUP;
  5015. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5016. return BTRFS_RAID_RAID0;
  5017. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  5018. return BTRFS_RAID_RAID5;
  5019. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  5020. return BTRFS_RAID_RAID6;
  5021. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  5022. }
  5023. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  5024. {
  5025. return __get_raid_index(cache->flags);
  5026. }
  5027. enum btrfs_loop_type {
  5028. LOOP_CACHING_NOWAIT = 0,
  5029. LOOP_CACHING_WAIT = 1,
  5030. LOOP_ALLOC_CHUNK = 2,
  5031. LOOP_NO_EMPTY_SIZE = 3,
  5032. };
  5033. /*
  5034. * walks the btree of allocated extents and find a hole of a given size.
  5035. * The key ins is changed to record the hole:
  5036. * ins->objectid == block start
  5037. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  5038. * ins->offset == number of blocks
  5039. * Any available blocks before search_start are skipped.
  5040. */
  5041. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  5042. struct btrfs_root *orig_root,
  5043. u64 num_bytes, u64 empty_size,
  5044. u64 hint_byte, struct btrfs_key *ins,
  5045. u64 data)
  5046. {
  5047. int ret = 0;
  5048. struct btrfs_root *root = orig_root->fs_info->extent_root;
  5049. struct btrfs_free_cluster *last_ptr = NULL;
  5050. struct btrfs_block_group_cache *block_group = NULL;
  5051. struct btrfs_block_group_cache *used_block_group;
  5052. u64 search_start = 0;
  5053. int empty_cluster = 2 * 1024 * 1024;
  5054. struct btrfs_space_info *space_info;
  5055. int loop = 0;
  5056. int index = __get_raid_index(data);
  5057. int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
  5058. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  5059. bool found_uncached_bg = false;
  5060. bool failed_cluster_refill = false;
  5061. bool failed_alloc = false;
  5062. bool use_cluster = true;
  5063. bool have_caching_bg = false;
  5064. WARN_ON(num_bytes < root->sectorsize);
  5065. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  5066. ins->objectid = 0;
  5067. ins->offset = 0;
  5068. trace_find_free_extent(orig_root, num_bytes, empty_size, data);
  5069. space_info = __find_space_info(root->fs_info, data);
  5070. if (!space_info) {
  5071. printk(KERN_ERR "No space info for %llu\n", data);
  5072. return -ENOSPC;
  5073. }
  5074. /*
  5075. * If the space info is for both data and metadata it means we have a
  5076. * small filesystem and we can't use the clustering stuff.
  5077. */
  5078. if (btrfs_mixed_space_info(space_info))
  5079. use_cluster = false;
  5080. if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  5081. last_ptr = &root->fs_info->meta_alloc_cluster;
  5082. if (!btrfs_test_opt(root, SSD))
  5083. empty_cluster = 64 * 1024;
  5084. }
  5085. if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  5086. btrfs_test_opt(root, SSD)) {
  5087. last_ptr = &root->fs_info->data_alloc_cluster;
  5088. }
  5089. if (last_ptr) {
  5090. spin_lock(&last_ptr->lock);
  5091. if (last_ptr->block_group)
  5092. hint_byte = last_ptr->window_start;
  5093. spin_unlock(&last_ptr->lock);
  5094. }
  5095. search_start = max(search_start, first_logical_byte(root, 0));
  5096. search_start = max(search_start, hint_byte);
  5097. if (!last_ptr)
  5098. empty_cluster = 0;
  5099. if (search_start == hint_byte) {
  5100. block_group = btrfs_lookup_block_group(root->fs_info,
  5101. search_start);
  5102. used_block_group = block_group;
  5103. /*
  5104. * we don't want to use the block group if it doesn't match our
  5105. * allocation bits, or if its not cached.
  5106. *
  5107. * However if we are re-searching with an ideal block group
  5108. * picked out then we don't care that the block group is cached.
  5109. */
  5110. if (block_group && block_group_bits(block_group, data) &&
  5111. block_group->cached != BTRFS_CACHE_NO) {
  5112. down_read(&space_info->groups_sem);
  5113. if (list_empty(&block_group->list) ||
  5114. block_group->ro) {
  5115. /*
  5116. * someone is removing this block group,
  5117. * we can't jump into the have_block_group
  5118. * target because our list pointers are not
  5119. * valid
  5120. */
  5121. btrfs_put_block_group(block_group);
  5122. up_read(&space_info->groups_sem);
  5123. } else {
  5124. index = get_block_group_index(block_group);
  5125. goto have_block_group;
  5126. }
  5127. } else if (block_group) {
  5128. btrfs_put_block_group(block_group);
  5129. }
  5130. }
  5131. search:
  5132. have_caching_bg = false;
  5133. down_read(&space_info->groups_sem);
  5134. list_for_each_entry(block_group, &space_info->block_groups[index],
  5135. list) {
  5136. u64 offset;
  5137. int cached;
  5138. used_block_group = block_group;
  5139. btrfs_get_block_group(block_group);
  5140. search_start = block_group->key.objectid;
  5141. /*
  5142. * this can happen if we end up cycling through all the
  5143. * raid types, but we want to make sure we only allocate
  5144. * for the proper type.
  5145. */
  5146. if (!block_group_bits(block_group, data)) {
  5147. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  5148. BTRFS_BLOCK_GROUP_RAID1 |
  5149. BTRFS_BLOCK_GROUP_RAID5 |
  5150. BTRFS_BLOCK_GROUP_RAID6 |
  5151. BTRFS_BLOCK_GROUP_RAID10;
  5152. /*
  5153. * if they asked for extra copies and this block group
  5154. * doesn't provide them, bail. This does allow us to
  5155. * fill raid0 from raid1.
  5156. */
  5157. if ((data & extra) && !(block_group->flags & extra))
  5158. goto loop;
  5159. }
  5160. have_block_group:
  5161. cached = block_group_cache_done(block_group);
  5162. if (unlikely(!cached)) {
  5163. found_uncached_bg = true;
  5164. ret = cache_block_group(block_group, 0);
  5165. BUG_ON(ret < 0);
  5166. ret = 0;
  5167. }
  5168. if (unlikely(block_group->ro))
  5169. goto loop;
  5170. /*
  5171. * Ok we want to try and use the cluster allocator, so
  5172. * lets look there
  5173. */
  5174. if (last_ptr) {
  5175. unsigned long aligned_cluster;
  5176. /*
  5177. * the refill lock keeps out other
  5178. * people trying to start a new cluster
  5179. */
  5180. spin_lock(&last_ptr->refill_lock);
  5181. used_block_group = last_ptr->block_group;
  5182. if (used_block_group != block_group &&
  5183. (!used_block_group ||
  5184. used_block_group->ro ||
  5185. !block_group_bits(used_block_group, data))) {
  5186. used_block_group = block_group;
  5187. goto refill_cluster;
  5188. }
  5189. if (used_block_group != block_group)
  5190. btrfs_get_block_group(used_block_group);
  5191. offset = btrfs_alloc_from_cluster(used_block_group,
  5192. last_ptr, num_bytes, used_block_group->key.objectid);
  5193. if (offset) {
  5194. /* we have a block, we're done */
  5195. spin_unlock(&last_ptr->refill_lock);
  5196. trace_btrfs_reserve_extent_cluster(root,
  5197. block_group, search_start, num_bytes);
  5198. goto checks;
  5199. }
  5200. WARN_ON(last_ptr->block_group != used_block_group);
  5201. if (used_block_group != block_group) {
  5202. btrfs_put_block_group(used_block_group);
  5203. used_block_group = block_group;
  5204. }
  5205. refill_cluster:
  5206. BUG_ON(used_block_group != block_group);
  5207. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  5208. * set up a new clusters, so lets just skip it
  5209. * and let the allocator find whatever block
  5210. * it can find. If we reach this point, we
  5211. * will have tried the cluster allocator
  5212. * plenty of times and not have found
  5213. * anything, so we are likely way too
  5214. * fragmented for the clustering stuff to find
  5215. * anything.
  5216. *
  5217. * However, if the cluster is taken from the
  5218. * current block group, release the cluster
  5219. * first, so that we stand a better chance of
  5220. * succeeding in the unclustered
  5221. * allocation. */
  5222. if (loop >= LOOP_NO_EMPTY_SIZE &&
  5223. last_ptr->block_group != block_group) {
  5224. spin_unlock(&last_ptr->refill_lock);
  5225. goto unclustered_alloc;
  5226. }
  5227. /*
  5228. * this cluster didn't work out, free it and
  5229. * start over
  5230. */
  5231. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5232. if (loop >= LOOP_NO_EMPTY_SIZE) {
  5233. spin_unlock(&last_ptr->refill_lock);
  5234. goto unclustered_alloc;
  5235. }
  5236. aligned_cluster = max_t(unsigned long,
  5237. empty_cluster + empty_size,
  5238. block_group->full_stripe_len);
  5239. /* allocate a cluster in this block group */
  5240. ret = btrfs_find_space_cluster(trans, root,
  5241. block_group, last_ptr,
  5242. search_start, num_bytes,
  5243. aligned_cluster);
  5244. if (ret == 0) {
  5245. /*
  5246. * now pull our allocation out of this
  5247. * cluster
  5248. */
  5249. offset = btrfs_alloc_from_cluster(block_group,
  5250. last_ptr, num_bytes,
  5251. search_start);
  5252. if (offset) {
  5253. /* we found one, proceed */
  5254. spin_unlock(&last_ptr->refill_lock);
  5255. trace_btrfs_reserve_extent_cluster(root,
  5256. block_group, search_start,
  5257. num_bytes);
  5258. goto checks;
  5259. }
  5260. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  5261. && !failed_cluster_refill) {
  5262. spin_unlock(&last_ptr->refill_lock);
  5263. failed_cluster_refill = true;
  5264. wait_block_group_cache_progress(block_group,
  5265. num_bytes + empty_cluster + empty_size);
  5266. goto have_block_group;
  5267. }
  5268. /*
  5269. * at this point we either didn't find a cluster
  5270. * or we weren't able to allocate a block from our
  5271. * cluster. Free the cluster we've been trying
  5272. * to use, and go to the next block group
  5273. */
  5274. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5275. spin_unlock(&last_ptr->refill_lock);
  5276. goto loop;
  5277. }
  5278. unclustered_alloc:
  5279. spin_lock(&block_group->free_space_ctl->tree_lock);
  5280. if (cached &&
  5281. block_group->free_space_ctl->free_space <
  5282. num_bytes + empty_cluster + empty_size) {
  5283. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5284. goto loop;
  5285. }
  5286. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5287. offset = btrfs_find_space_for_alloc(block_group, search_start,
  5288. num_bytes, empty_size);
  5289. /*
  5290. * If we didn't find a chunk, and we haven't failed on this
  5291. * block group before, and this block group is in the middle of
  5292. * caching and we are ok with waiting, then go ahead and wait
  5293. * for progress to be made, and set failed_alloc to true.
  5294. *
  5295. * If failed_alloc is true then we've already waited on this
  5296. * block group once and should move on to the next block group.
  5297. */
  5298. if (!offset && !failed_alloc && !cached &&
  5299. loop > LOOP_CACHING_NOWAIT) {
  5300. wait_block_group_cache_progress(block_group,
  5301. num_bytes + empty_size);
  5302. failed_alloc = true;
  5303. goto have_block_group;
  5304. } else if (!offset) {
  5305. if (!cached)
  5306. have_caching_bg = true;
  5307. goto loop;
  5308. }
  5309. checks:
  5310. search_start = stripe_align(root, used_block_group,
  5311. offset, num_bytes);
  5312. /* move on to the next group */
  5313. if (search_start + num_bytes >
  5314. used_block_group->key.objectid + used_block_group->key.offset) {
  5315. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5316. goto loop;
  5317. }
  5318. if (offset < search_start)
  5319. btrfs_add_free_space(used_block_group, offset,
  5320. search_start - offset);
  5321. BUG_ON(offset > search_start);
  5322. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  5323. alloc_type);
  5324. if (ret == -EAGAIN) {
  5325. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5326. goto loop;
  5327. }
  5328. /* we are all good, lets return */
  5329. ins->objectid = search_start;
  5330. ins->offset = num_bytes;
  5331. trace_btrfs_reserve_extent(orig_root, block_group,
  5332. search_start, num_bytes);
  5333. if (used_block_group != block_group)
  5334. btrfs_put_block_group(used_block_group);
  5335. btrfs_put_block_group(block_group);
  5336. break;
  5337. loop:
  5338. failed_cluster_refill = false;
  5339. failed_alloc = false;
  5340. BUG_ON(index != get_block_group_index(block_group));
  5341. if (used_block_group != block_group)
  5342. btrfs_put_block_group(used_block_group);
  5343. btrfs_put_block_group(block_group);
  5344. }
  5345. up_read(&space_info->groups_sem);
  5346. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  5347. goto search;
  5348. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  5349. goto search;
  5350. /*
  5351. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  5352. * caching kthreads as we move along
  5353. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  5354. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  5355. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  5356. * again
  5357. */
  5358. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  5359. index = 0;
  5360. loop++;
  5361. if (loop == LOOP_ALLOC_CHUNK) {
  5362. ret = do_chunk_alloc(trans, root, data,
  5363. CHUNK_ALLOC_FORCE);
  5364. /*
  5365. * Do not bail out on ENOSPC since we
  5366. * can do more things.
  5367. */
  5368. if (ret < 0 && ret != -ENOSPC) {
  5369. btrfs_abort_transaction(trans,
  5370. root, ret);
  5371. goto out;
  5372. }
  5373. }
  5374. if (loop == LOOP_NO_EMPTY_SIZE) {
  5375. empty_size = 0;
  5376. empty_cluster = 0;
  5377. }
  5378. goto search;
  5379. } else if (!ins->objectid) {
  5380. ret = -ENOSPC;
  5381. } else if (ins->objectid) {
  5382. ret = 0;
  5383. }
  5384. out:
  5385. return ret;
  5386. }
  5387. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5388. int dump_block_groups)
  5389. {
  5390. struct btrfs_block_group_cache *cache;
  5391. int index = 0;
  5392. spin_lock(&info->lock);
  5393. printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
  5394. (unsigned long long)info->flags,
  5395. (unsigned long long)(info->total_bytes - info->bytes_used -
  5396. info->bytes_pinned - info->bytes_reserved -
  5397. info->bytes_readonly),
  5398. (info->full) ? "" : "not ");
  5399. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  5400. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5401. (unsigned long long)info->total_bytes,
  5402. (unsigned long long)info->bytes_used,
  5403. (unsigned long long)info->bytes_pinned,
  5404. (unsigned long long)info->bytes_reserved,
  5405. (unsigned long long)info->bytes_may_use,
  5406. (unsigned long long)info->bytes_readonly);
  5407. spin_unlock(&info->lock);
  5408. if (!dump_block_groups)
  5409. return;
  5410. down_read(&info->groups_sem);
  5411. again:
  5412. list_for_each_entry(cache, &info->block_groups[index], list) {
  5413. spin_lock(&cache->lock);
  5414. printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
  5415. (unsigned long long)cache->key.objectid,
  5416. (unsigned long long)cache->key.offset,
  5417. (unsigned long long)btrfs_block_group_used(&cache->item),
  5418. (unsigned long long)cache->pinned,
  5419. (unsigned long long)cache->reserved,
  5420. cache->ro ? "[readonly]" : "");
  5421. btrfs_dump_free_space(cache, bytes);
  5422. spin_unlock(&cache->lock);
  5423. }
  5424. if (++index < BTRFS_NR_RAID_TYPES)
  5425. goto again;
  5426. up_read(&info->groups_sem);
  5427. }
  5428. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  5429. struct btrfs_root *root,
  5430. u64 num_bytes, u64 min_alloc_size,
  5431. u64 empty_size, u64 hint_byte,
  5432. struct btrfs_key *ins, u64 data)
  5433. {
  5434. bool final_tried = false;
  5435. int ret;
  5436. data = btrfs_get_alloc_profile(root, data);
  5437. again:
  5438. WARN_ON(num_bytes < root->sectorsize);
  5439. ret = find_free_extent(trans, root, num_bytes, empty_size,
  5440. hint_byte, ins, data);
  5441. if (ret == -ENOSPC) {
  5442. if (!final_tried) {
  5443. num_bytes = num_bytes >> 1;
  5444. num_bytes = round_down(num_bytes, root->sectorsize);
  5445. num_bytes = max(num_bytes, min_alloc_size);
  5446. if (num_bytes == min_alloc_size)
  5447. final_tried = true;
  5448. goto again;
  5449. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5450. struct btrfs_space_info *sinfo;
  5451. sinfo = __find_space_info(root->fs_info, data);
  5452. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  5453. "wanted %llu\n", (unsigned long long)data,
  5454. (unsigned long long)num_bytes);
  5455. if (sinfo)
  5456. dump_space_info(sinfo, num_bytes, 1);
  5457. }
  5458. }
  5459. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  5460. return ret;
  5461. }
  5462. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  5463. u64 start, u64 len, int pin)
  5464. {
  5465. struct btrfs_block_group_cache *cache;
  5466. int ret = 0;
  5467. cache = btrfs_lookup_block_group(root->fs_info, start);
  5468. if (!cache) {
  5469. printk(KERN_ERR "Unable to find block group for %llu\n",
  5470. (unsigned long long)start);
  5471. return -ENOSPC;
  5472. }
  5473. if (btrfs_test_opt(root, DISCARD))
  5474. ret = btrfs_discard_extent(root, start, len, NULL);
  5475. if (pin)
  5476. pin_down_extent(root, cache, start, len, 1);
  5477. else {
  5478. btrfs_add_free_space(cache, start, len);
  5479. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
  5480. }
  5481. btrfs_put_block_group(cache);
  5482. trace_btrfs_reserved_extent_free(root, start, len);
  5483. return ret;
  5484. }
  5485. int btrfs_free_reserved_extent(struct btrfs_root *root,
  5486. u64 start, u64 len)
  5487. {
  5488. return __btrfs_free_reserved_extent(root, start, len, 0);
  5489. }
  5490. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  5491. u64 start, u64 len)
  5492. {
  5493. return __btrfs_free_reserved_extent(root, start, len, 1);
  5494. }
  5495. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5496. struct btrfs_root *root,
  5497. u64 parent, u64 root_objectid,
  5498. u64 flags, u64 owner, u64 offset,
  5499. struct btrfs_key *ins, int ref_mod)
  5500. {
  5501. int ret;
  5502. struct btrfs_fs_info *fs_info = root->fs_info;
  5503. struct btrfs_extent_item *extent_item;
  5504. struct btrfs_extent_inline_ref *iref;
  5505. struct btrfs_path *path;
  5506. struct extent_buffer *leaf;
  5507. int type;
  5508. u32 size;
  5509. if (parent > 0)
  5510. type = BTRFS_SHARED_DATA_REF_KEY;
  5511. else
  5512. type = BTRFS_EXTENT_DATA_REF_KEY;
  5513. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  5514. path = btrfs_alloc_path();
  5515. if (!path)
  5516. return -ENOMEM;
  5517. path->leave_spinning = 1;
  5518. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5519. ins, size);
  5520. if (ret) {
  5521. btrfs_free_path(path);
  5522. return ret;
  5523. }
  5524. leaf = path->nodes[0];
  5525. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5526. struct btrfs_extent_item);
  5527. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  5528. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5529. btrfs_set_extent_flags(leaf, extent_item,
  5530. flags | BTRFS_EXTENT_FLAG_DATA);
  5531. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5532. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  5533. if (parent > 0) {
  5534. struct btrfs_shared_data_ref *ref;
  5535. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  5536. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5537. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  5538. } else {
  5539. struct btrfs_extent_data_ref *ref;
  5540. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  5541. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  5542. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  5543. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  5544. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  5545. }
  5546. btrfs_mark_buffer_dirty(path->nodes[0]);
  5547. btrfs_free_path(path);
  5548. ret = update_block_group(root, ins->objectid, ins->offset, 1);
  5549. if (ret) { /* -ENOENT, logic error */
  5550. printk(KERN_ERR "btrfs update block group failed for %llu "
  5551. "%llu\n", (unsigned long long)ins->objectid,
  5552. (unsigned long long)ins->offset);
  5553. BUG();
  5554. }
  5555. return ret;
  5556. }
  5557. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  5558. struct btrfs_root *root,
  5559. u64 parent, u64 root_objectid,
  5560. u64 flags, struct btrfs_disk_key *key,
  5561. int level, struct btrfs_key *ins)
  5562. {
  5563. int ret;
  5564. struct btrfs_fs_info *fs_info = root->fs_info;
  5565. struct btrfs_extent_item *extent_item;
  5566. struct btrfs_tree_block_info *block_info;
  5567. struct btrfs_extent_inline_ref *iref;
  5568. struct btrfs_path *path;
  5569. struct extent_buffer *leaf;
  5570. u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
  5571. path = btrfs_alloc_path();
  5572. if (!path)
  5573. return -ENOMEM;
  5574. path->leave_spinning = 1;
  5575. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5576. ins, size);
  5577. if (ret) {
  5578. btrfs_free_path(path);
  5579. return ret;
  5580. }
  5581. leaf = path->nodes[0];
  5582. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5583. struct btrfs_extent_item);
  5584. btrfs_set_extent_refs(leaf, extent_item, 1);
  5585. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5586. btrfs_set_extent_flags(leaf, extent_item,
  5587. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  5588. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  5589. btrfs_set_tree_block_key(leaf, block_info, key);
  5590. btrfs_set_tree_block_level(leaf, block_info, level);
  5591. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  5592. if (parent > 0) {
  5593. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  5594. btrfs_set_extent_inline_ref_type(leaf, iref,
  5595. BTRFS_SHARED_BLOCK_REF_KEY);
  5596. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5597. } else {
  5598. btrfs_set_extent_inline_ref_type(leaf, iref,
  5599. BTRFS_TREE_BLOCK_REF_KEY);
  5600. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  5601. }
  5602. btrfs_mark_buffer_dirty(leaf);
  5603. btrfs_free_path(path);
  5604. ret = update_block_group(root, ins->objectid, ins->offset, 1);
  5605. if (ret) { /* -ENOENT, logic error */
  5606. printk(KERN_ERR "btrfs update block group failed for %llu "
  5607. "%llu\n", (unsigned long long)ins->objectid,
  5608. (unsigned long long)ins->offset);
  5609. BUG();
  5610. }
  5611. return ret;
  5612. }
  5613. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5614. struct btrfs_root *root,
  5615. u64 root_objectid, u64 owner,
  5616. u64 offset, struct btrfs_key *ins)
  5617. {
  5618. int ret;
  5619. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  5620. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  5621. ins->offset, 0,
  5622. root_objectid, owner, offset,
  5623. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  5624. return ret;
  5625. }
  5626. /*
  5627. * this is used by the tree logging recovery code. It records that
  5628. * an extent has been allocated and makes sure to clear the free
  5629. * space cache bits as well
  5630. */
  5631. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  5632. struct btrfs_root *root,
  5633. u64 root_objectid, u64 owner, u64 offset,
  5634. struct btrfs_key *ins)
  5635. {
  5636. int ret;
  5637. struct btrfs_block_group_cache *block_group;
  5638. struct btrfs_caching_control *caching_ctl;
  5639. u64 start = ins->objectid;
  5640. u64 num_bytes = ins->offset;
  5641. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  5642. cache_block_group(block_group, 0);
  5643. caching_ctl = get_caching_control(block_group);
  5644. if (!caching_ctl) {
  5645. BUG_ON(!block_group_cache_done(block_group));
  5646. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  5647. BUG_ON(ret); /* -ENOMEM */
  5648. } else {
  5649. mutex_lock(&caching_ctl->mutex);
  5650. if (start >= caching_ctl->progress) {
  5651. ret = add_excluded_extent(root, start, num_bytes);
  5652. BUG_ON(ret); /* -ENOMEM */
  5653. } else if (start + num_bytes <= caching_ctl->progress) {
  5654. ret = btrfs_remove_free_space(block_group,
  5655. start, num_bytes);
  5656. BUG_ON(ret); /* -ENOMEM */
  5657. } else {
  5658. num_bytes = caching_ctl->progress - start;
  5659. ret = btrfs_remove_free_space(block_group,
  5660. start, num_bytes);
  5661. BUG_ON(ret); /* -ENOMEM */
  5662. start = caching_ctl->progress;
  5663. num_bytes = ins->objectid + ins->offset -
  5664. caching_ctl->progress;
  5665. ret = add_excluded_extent(root, start, num_bytes);
  5666. BUG_ON(ret); /* -ENOMEM */
  5667. }
  5668. mutex_unlock(&caching_ctl->mutex);
  5669. put_caching_control(caching_ctl);
  5670. }
  5671. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  5672. RESERVE_ALLOC_NO_ACCOUNT);
  5673. BUG_ON(ret); /* logic error */
  5674. btrfs_put_block_group(block_group);
  5675. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  5676. 0, owner, offset, ins, 1);
  5677. return ret;
  5678. }
  5679. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  5680. struct btrfs_root *root,
  5681. u64 bytenr, u32 blocksize,
  5682. int level)
  5683. {
  5684. struct extent_buffer *buf;
  5685. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5686. if (!buf)
  5687. return ERR_PTR(-ENOMEM);
  5688. btrfs_set_header_generation(buf, trans->transid);
  5689. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  5690. btrfs_tree_lock(buf);
  5691. clean_tree_block(trans, root, buf);
  5692. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  5693. btrfs_set_lock_blocking(buf);
  5694. btrfs_set_buffer_uptodate(buf);
  5695. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  5696. /*
  5697. * we allow two log transactions at a time, use different
  5698. * EXENT bit to differentiate dirty pages.
  5699. */
  5700. if (root->log_transid % 2 == 0)
  5701. set_extent_dirty(&root->dirty_log_pages, buf->start,
  5702. buf->start + buf->len - 1, GFP_NOFS);
  5703. else
  5704. set_extent_new(&root->dirty_log_pages, buf->start,
  5705. buf->start + buf->len - 1, GFP_NOFS);
  5706. } else {
  5707. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  5708. buf->start + buf->len - 1, GFP_NOFS);
  5709. }
  5710. trans->blocks_used++;
  5711. /* this returns a buffer locked for blocking */
  5712. return buf;
  5713. }
  5714. static struct btrfs_block_rsv *
  5715. use_block_rsv(struct btrfs_trans_handle *trans,
  5716. struct btrfs_root *root, u32 blocksize)
  5717. {
  5718. struct btrfs_block_rsv *block_rsv;
  5719. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  5720. int ret;
  5721. block_rsv = get_block_rsv(trans, root);
  5722. if (block_rsv->size == 0) {
  5723. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  5724. BTRFS_RESERVE_NO_FLUSH);
  5725. /*
  5726. * If we couldn't reserve metadata bytes try and use some from
  5727. * the global reserve.
  5728. */
  5729. if (ret && block_rsv != global_rsv) {
  5730. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5731. if (!ret)
  5732. return global_rsv;
  5733. return ERR_PTR(ret);
  5734. } else if (ret) {
  5735. return ERR_PTR(ret);
  5736. }
  5737. return block_rsv;
  5738. }
  5739. ret = block_rsv_use_bytes(block_rsv, blocksize);
  5740. if (!ret)
  5741. return block_rsv;
  5742. if (ret && !block_rsv->failfast) {
  5743. if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5744. static DEFINE_RATELIMIT_STATE(_rs,
  5745. DEFAULT_RATELIMIT_INTERVAL * 10,
  5746. /*DEFAULT_RATELIMIT_BURST*/ 1);
  5747. if (__ratelimit(&_rs))
  5748. WARN(1, KERN_DEBUG
  5749. "btrfs: block rsv returned %d\n", ret);
  5750. }
  5751. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  5752. BTRFS_RESERVE_NO_FLUSH);
  5753. if (!ret) {
  5754. return block_rsv;
  5755. } else if (ret && block_rsv != global_rsv) {
  5756. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5757. if (!ret)
  5758. return global_rsv;
  5759. }
  5760. }
  5761. return ERR_PTR(-ENOSPC);
  5762. }
  5763. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  5764. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  5765. {
  5766. block_rsv_add_bytes(block_rsv, blocksize, 0);
  5767. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  5768. }
  5769. /*
  5770. * finds a free extent and does all the dirty work required for allocation
  5771. * returns the key for the extent through ins, and a tree buffer for
  5772. * the first block of the extent through buf.
  5773. *
  5774. * returns the tree buffer or NULL.
  5775. */
  5776. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  5777. struct btrfs_root *root, u32 blocksize,
  5778. u64 parent, u64 root_objectid,
  5779. struct btrfs_disk_key *key, int level,
  5780. u64 hint, u64 empty_size)
  5781. {
  5782. struct btrfs_key ins;
  5783. struct btrfs_block_rsv *block_rsv;
  5784. struct extent_buffer *buf;
  5785. u64 flags = 0;
  5786. int ret;
  5787. block_rsv = use_block_rsv(trans, root, blocksize);
  5788. if (IS_ERR(block_rsv))
  5789. return ERR_CAST(block_rsv);
  5790. ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
  5791. empty_size, hint, &ins, 0);
  5792. if (ret) {
  5793. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  5794. return ERR_PTR(ret);
  5795. }
  5796. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  5797. blocksize, level);
  5798. BUG_ON(IS_ERR(buf)); /* -ENOMEM */
  5799. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  5800. if (parent == 0)
  5801. parent = ins.objectid;
  5802. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5803. } else
  5804. BUG_ON(parent > 0);
  5805. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  5806. struct btrfs_delayed_extent_op *extent_op;
  5807. extent_op = btrfs_alloc_delayed_extent_op();
  5808. BUG_ON(!extent_op); /* -ENOMEM */
  5809. if (key)
  5810. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  5811. else
  5812. memset(&extent_op->key, 0, sizeof(extent_op->key));
  5813. extent_op->flags_to_set = flags;
  5814. extent_op->update_key = 1;
  5815. extent_op->update_flags = 1;
  5816. extent_op->is_data = 0;
  5817. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5818. ins.objectid,
  5819. ins.offset, parent, root_objectid,
  5820. level, BTRFS_ADD_DELAYED_EXTENT,
  5821. extent_op, 0);
  5822. BUG_ON(ret); /* -ENOMEM */
  5823. }
  5824. return buf;
  5825. }
  5826. struct walk_control {
  5827. u64 refs[BTRFS_MAX_LEVEL];
  5828. u64 flags[BTRFS_MAX_LEVEL];
  5829. struct btrfs_key update_progress;
  5830. int stage;
  5831. int level;
  5832. int shared_level;
  5833. int update_ref;
  5834. int keep_locks;
  5835. int reada_slot;
  5836. int reada_count;
  5837. int for_reloc;
  5838. };
  5839. #define DROP_REFERENCE 1
  5840. #define UPDATE_BACKREF 2
  5841. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  5842. struct btrfs_root *root,
  5843. struct walk_control *wc,
  5844. struct btrfs_path *path)
  5845. {
  5846. u64 bytenr;
  5847. u64 generation;
  5848. u64 refs;
  5849. u64 flags;
  5850. u32 nritems;
  5851. u32 blocksize;
  5852. struct btrfs_key key;
  5853. struct extent_buffer *eb;
  5854. int ret;
  5855. int slot;
  5856. int nread = 0;
  5857. if (path->slots[wc->level] < wc->reada_slot) {
  5858. wc->reada_count = wc->reada_count * 2 / 3;
  5859. wc->reada_count = max(wc->reada_count, 2);
  5860. } else {
  5861. wc->reada_count = wc->reada_count * 3 / 2;
  5862. wc->reada_count = min_t(int, wc->reada_count,
  5863. BTRFS_NODEPTRS_PER_BLOCK(root));
  5864. }
  5865. eb = path->nodes[wc->level];
  5866. nritems = btrfs_header_nritems(eb);
  5867. blocksize = btrfs_level_size(root, wc->level - 1);
  5868. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  5869. if (nread >= wc->reada_count)
  5870. break;
  5871. cond_resched();
  5872. bytenr = btrfs_node_blockptr(eb, slot);
  5873. generation = btrfs_node_ptr_generation(eb, slot);
  5874. if (slot == path->slots[wc->level])
  5875. goto reada;
  5876. if (wc->stage == UPDATE_BACKREF &&
  5877. generation <= root->root_key.offset)
  5878. continue;
  5879. /* We don't lock the tree block, it's OK to be racy here */
  5880. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5881. &refs, &flags);
  5882. /* We don't care about errors in readahead. */
  5883. if (ret < 0)
  5884. continue;
  5885. BUG_ON(refs == 0);
  5886. if (wc->stage == DROP_REFERENCE) {
  5887. if (refs == 1)
  5888. goto reada;
  5889. if (wc->level == 1 &&
  5890. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5891. continue;
  5892. if (!wc->update_ref ||
  5893. generation <= root->root_key.offset)
  5894. continue;
  5895. btrfs_node_key_to_cpu(eb, &key, slot);
  5896. ret = btrfs_comp_cpu_keys(&key,
  5897. &wc->update_progress);
  5898. if (ret < 0)
  5899. continue;
  5900. } else {
  5901. if (wc->level == 1 &&
  5902. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5903. continue;
  5904. }
  5905. reada:
  5906. ret = readahead_tree_block(root, bytenr, blocksize,
  5907. generation);
  5908. if (ret)
  5909. break;
  5910. nread++;
  5911. }
  5912. wc->reada_slot = slot;
  5913. }
  5914. /*
  5915. * hepler to process tree block while walking down the tree.
  5916. *
  5917. * when wc->stage == UPDATE_BACKREF, this function updates
  5918. * back refs for pointers in the block.
  5919. *
  5920. * NOTE: return value 1 means we should stop walking down.
  5921. */
  5922. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  5923. struct btrfs_root *root,
  5924. struct btrfs_path *path,
  5925. struct walk_control *wc, int lookup_info)
  5926. {
  5927. int level = wc->level;
  5928. struct extent_buffer *eb = path->nodes[level];
  5929. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5930. int ret;
  5931. if (wc->stage == UPDATE_BACKREF &&
  5932. btrfs_header_owner(eb) != root->root_key.objectid)
  5933. return 1;
  5934. /*
  5935. * when reference count of tree block is 1, it won't increase
  5936. * again. once full backref flag is set, we never clear it.
  5937. */
  5938. if (lookup_info &&
  5939. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  5940. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  5941. BUG_ON(!path->locks[level]);
  5942. ret = btrfs_lookup_extent_info(trans, root,
  5943. eb->start, eb->len,
  5944. &wc->refs[level],
  5945. &wc->flags[level]);
  5946. BUG_ON(ret == -ENOMEM);
  5947. if (ret)
  5948. return ret;
  5949. BUG_ON(wc->refs[level] == 0);
  5950. }
  5951. if (wc->stage == DROP_REFERENCE) {
  5952. if (wc->refs[level] > 1)
  5953. return 1;
  5954. if (path->locks[level] && !wc->keep_locks) {
  5955. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5956. path->locks[level] = 0;
  5957. }
  5958. return 0;
  5959. }
  5960. /* wc->stage == UPDATE_BACKREF */
  5961. if (!(wc->flags[level] & flag)) {
  5962. BUG_ON(!path->locks[level]);
  5963. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  5964. BUG_ON(ret); /* -ENOMEM */
  5965. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  5966. BUG_ON(ret); /* -ENOMEM */
  5967. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  5968. eb->len, flag, 0);
  5969. BUG_ON(ret); /* -ENOMEM */
  5970. wc->flags[level] |= flag;
  5971. }
  5972. /*
  5973. * the block is shared by multiple trees, so it's not good to
  5974. * keep the tree lock
  5975. */
  5976. if (path->locks[level] && level > 0) {
  5977. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5978. path->locks[level] = 0;
  5979. }
  5980. return 0;
  5981. }
  5982. /*
  5983. * hepler to process tree block pointer.
  5984. *
  5985. * when wc->stage == DROP_REFERENCE, this function checks
  5986. * reference count of the block pointed to. if the block
  5987. * is shared and we need update back refs for the subtree
  5988. * rooted at the block, this function changes wc->stage to
  5989. * UPDATE_BACKREF. if the block is shared and there is no
  5990. * need to update back, this function drops the reference
  5991. * to the block.
  5992. *
  5993. * NOTE: return value 1 means we should stop walking down.
  5994. */
  5995. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  5996. struct btrfs_root *root,
  5997. struct btrfs_path *path,
  5998. struct walk_control *wc, int *lookup_info)
  5999. {
  6000. u64 bytenr;
  6001. u64 generation;
  6002. u64 parent;
  6003. u32 blocksize;
  6004. struct btrfs_key key;
  6005. struct extent_buffer *next;
  6006. int level = wc->level;
  6007. int reada = 0;
  6008. int ret = 0;
  6009. generation = btrfs_node_ptr_generation(path->nodes[level],
  6010. path->slots[level]);
  6011. /*
  6012. * if the lower level block was created before the snapshot
  6013. * was created, we know there is no need to update back refs
  6014. * for the subtree
  6015. */
  6016. if (wc->stage == UPDATE_BACKREF &&
  6017. generation <= root->root_key.offset) {
  6018. *lookup_info = 1;
  6019. return 1;
  6020. }
  6021. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  6022. blocksize = btrfs_level_size(root, level - 1);
  6023. next = btrfs_find_tree_block(root, bytenr, blocksize);
  6024. if (!next) {
  6025. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  6026. if (!next)
  6027. return -ENOMEM;
  6028. reada = 1;
  6029. }
  6030. btrfs_tree_lock(next);
  6031. btrfs_set_lock_blocking(next);
  6032. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  6033. &wc->refs[level - 1],
  6034. &wc->flags[level - 1]);
  6035. if (ret < 0) {
  6036. btrfs_tree_unlock(next);
  6037. return ret;
  6038. }
  6039. BUG_ON(wc->refs[level - 1] == 0);
  6040. *lookup_info = 0;
  6041. if (wc->stage == DROP_REFERENCE) {
  6042. if (wc->refs[level - 1] > 1) {
  6043. if (level == 1 &&
  6044. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6045. goto skip;
  6046. if (!wc->update_ref ||
  6047. generation <= root->root_key.offset)
  6048. goto skip;
  6049. btrfs_node_key_to_cpu(path->nodes[level], &key,
  6050. path->slots[level]);
  6051. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  6052. if (ret < 0)
  6053. goto skip;
  6054. wc->stage = UPDATE_BACKREF;
  6055. wc->shared_level = level - 1;
  6056. }
  6057. } else {
  6058. if (level == 1 &&
  6059. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  6060. goto skip;
  6061. }
  6062. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  6063. btrfs_tree_unlock(next);
  6064. free_extent_buffer(next);
  6065. next = NULL;
  6066. *lookup_info = 1;
  6067. }
  6068. if (!next) {
  6069. if (reada && level == 1)
  6070. reada_walk_down(trans, root, wc, path);
  6071. next = read_tree_block(root, bytenr, blocksize, generation);
  6072. if (!next)
  6073. return -EIO;
  6074. btrfs_tree_lock(next);
  6075. btrfs_set_lock_blocking(next);
  6076. }
  6077. level--;
  6078. BUG_ON(level != btrfs_header_level(next));
  6079. path->nodes[level] = next;
  6080. path->slots[level] = 0;
  6081. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6082. wc->level = level;
  6083. if (wc->level == 1)
  6084. wc->reada_slot = 0;
  6085. return 0;
  6086. skip:
  6087. wc->refs[level - 1] = 0;
  6088. wc->flags[level - 1] = 0;
  6089. if (wc->stage == DROP_REFERENCE) {
  6090. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  6091. parent = path->nodes[level]->start;
  6092. } else {
  6093. BUG_ON(root->root_key.objectid !=
  6094. btrfs_header_owner(path->nodes[level]));
  6095. parent = 0;
  6096. }
  6097. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  6098. root->root_key.objectid, level - 1, 0, 0);
  6099. BUG_ON(ret); /* -ENOMEM */
  6100. }
  6101. btrfs_tree_unlock(next);
  6102. free_extent_buffer(next);
  6103. *lookup_info = 1;
  6104. return 1;
  6105. }
  6106. /*
  6107. * hepler to process tree block while walking up the tree.
  6108. *
  6109. * when wc->stage == DROP_REFERENCE, this function drops
  6110. * reference count on the block.
  6111. *
  6112. * when wc->stage == UPDATE_BACKREF, this function changes
  6113. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  6114. * to UPDATE_BACKREF previously while processing the block.
  6115. *
  6116. * NOTE: return value 1 means we should stop walking up.
  6117. */
  6118. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  6119. struct btrfs_root *root,
  6120. struct btrfs_path *path,
  6121. struct walk_control *wc)
  6122. {
  6123. int ret;
  6124. int level = wc->level;
  6125. struct extent_buffer *eb = path->nodes[level];
  6126. u64 parent = 0;
  6127. if (wc->stage == UPDATE_BACKREF) {
  6128. BUG_ON(wc->shared_level < level);
  6129. if (level < wc->shared_level)
  6130. goto out;
  6131. ret = find_next_key(path, level + 1, &wc->update_progress);
  6132. if (ret > 0)
  6133. wc->update_ref = 0;
  6134. wc->stage = DROP_REFERENCE;
  6135. wc->shared_level = -1;
  6136. path->slots[level] = 0;
  6137. /*
  6138. * check reference count again if the block isn't locked.
  6139. * we should start walking down the tree again if reference
  6140. * count is one.
  6141. */
  6142. if (!path->locks[level]) {
  6143. BUG_ON(level == 0);
  6144. btrfs_tree_lock(eb);
  6145. btrfs_set_lock_blocking(eb);
  6146. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6147. ret = btrfs_lookup_extent_info(trans, root,
  6148. eb->start, eb->len,
  6149. &wc->refs[level],
  6150. &wc->flags[level]);
  6151. if (ret < 0) {
  6152. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6153. path->locks[level] = 0;
  6154. return ret;
  6155. }
  6156. BUG_ON(wc->refs[level] == 0);
  6157. if (wc->refs[level] == 1) {
  6158. btrfs_tree_unlock_rw(eb, path->locks[level]);
  6159. path->locks[level] = 0;
  6160. return 1;
  6161. }
  6162. }
  6163. }
  6164. /* wc->stage == DROP_REFERENCE */
  6165. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  6166. if (wc->refs[level] == 1) {
  6167. if (level == 0) {
  6168. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6169. ret = btrfs_dec_ref(trans, root, eb, 1,
  6170. wc->for_reloc);
  6171. else
  6172. ret = btrfs_dec_ref(trans, root, eb, 0,
  6173. wc->for_reloc);
  6174. BUG_ON(ret); /* -ENOMEM */
  6175. }
  6176. /* make block locked assertion in clean_tree_block happy */
  6177. if (!path->locks[level] &&
  6178. btrfs_header_generation(eb) == trans->transid) {
  6179. btrfs_tree_lock(eb);
  6180. btrfs_set_lock_blocking(eb);
  6181. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6182. }
  6183. clean_tree_block(trans, root, eb);
  6184. }
  6185. if (eb == root->node) {
  6186. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6187. parent = eb->start;
  6188. else
  6189. BUG_ON(root->root_key.objectid !=
  6190. btrfs_header_owner(eb));
  6191. } else {
  6192. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6193. parent = path->nodes[level + 1]->start;
  6194. else
  6195. BUG_ON(root->root_key.objectid !=
  6196. btrfs_header_owner(path->nodes[level + 1]));
  6197. }
  6198. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  6199. out:
  6200. wc->refs[level] = 0;
  6201. wc->flags[level] = 0;
  6202. return 0;
  6203. }
  6204. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  6205. struct btrfs_root *root,
  6206. struct btrfs_path *path,
  6207. struct walk_control *wc)
  6208. {
  6209. int level = wc->level;
  6210. int lookup_info = 1;
  6211. int ret;
  6212. while (level >= 0) {
  6213. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  6214. if (ret > 0)
  6215. break;
  6216. if (level == 0)
  6217. break;
  6218. if (path->slots[level] >=
  6219. btrfs_header_nritems(path->nodes[level]))
  6220. break;
  6221. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  6222. if (ret > 0) {
  6223. path->slots[level]++;
  6224. continue;
  6225. } else if (ret < 0)
  6226. return ret;
  6227. level = wc->level;
  6228. }
  6229. return 0;
  6230. }
  6231. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  6232. struct btrfs_root *root,
  6233. struct btrfs_path *path,
  6234. struct walk_control *wc, int max_level)
  6235. {
  6236. int level = wc->level;
  6237. int ret;
  6238. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  6239. while (level < max_level && path->nodes[level]) {
  6240. wc->level = level;
  6241. if (path->slots[level] + 1 <
  6242. btrfs_header_nritems(path->nodes[level])) {
  6243. path->slots[level]++;
  6244. return 0;
  6245. } else {
  6246. ret = walk_up_proc(trans, root, path, wc);
  6247. if (ret > 0)
  6248. return 0;
  6249. if (path->locks[level]) {
  6250. btrfs_tree_unlock_rw(path->nodes[level],
  6251. path->locks[level]);
  6252. path->locks[level] = 0;
  6253. }
  6254. free_extent_buffer(path->nodes[level]);
  6255. path->nodes[level] = NULL;
  6256. level++;
  6257. }
  6258. }
  6259. return 1;
  6260. }
  6261. /*
  6262. * drop a subvolume tree.
  6263. *
  6264. * this function traverses the tree freeing any blocks that only
  6265. * referenced by the tree.
  6266. *
  6267. * when a shared tree block is found. this function decreases its
  6268. * reference count by one. if update_ref is true, this function
  6269. * also make sure backrefs for the shared block and all lower level
  6270. * blocks are properly updated.
  6271. */
  6272. int btrfs_drop_snapshot(struct btrfs_root *root,
  6273. struct btrfs_block_rsv *block_rsv, int update_ref,
  6274. int for_reloc)
  6275. {
  6276. struct btrfs_path *path;
  6277. struct btrfs_trans_handle *trans;
  6278. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6279. struct btrfs_root_item *root_item = &root->root_item;
  6280. struct walk_control *wc;
  6281. struct btrfs_key key;
  6282. int err = 0;
  6283. int ret;
  6284. int level;
  6285. path = btrfs_alloc_path();
  6286. if (!path) {
  6287. err = -ENOMEM;
  6288. goto out;
  6289. }
  6290. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6291. if (!wc) {
  6292. btrfs_free_path(path);
  6293. err = -ENOMEM;
  6294. goto out;
  6295. }
  6296. trans = btrfs_start_transaction(tree_root, 0);
  6297. if (IS_ERR(trans)) {
  6298. err = PTR_ERR(trans);
  6299. goto out_free;
  6300. }
  6301. if (block_rsv)
  6302. trans->block_rsv = block_rsv;
  6303. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  6304. level = btrfs_header_level(root->node);
  6305. path->nodes[level] = btrfs_lock_root_node(root);
  6306. btrfs_set_lock_blocking(path->nodes[level]);
  6307. path->slots[level] = 0;
  6308. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6309. memset(&wc->update_progress, 0,
  6310. sizeof(wc->update_progress));
  6311. } else {
  6312. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  6313. memcpy(&wc->update_progress, &key,
  6314. sizeof(wc->update_progress));
  6315. level = root_item->drop_level;
  6316. BUG_ON(level == 0);
  6317. path->lowest_level = level;
  6318. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6319. path->lowest_level = 0;
  6320. if (ret < 0) {
  6321. err = ret;
  6322. goto out_end_trans;
  6323. }
  6324. WARN_ON(ret > 0);
  6325. /*
  6326. * unlock our path, this is safe because only this
  6327. * function is allowed to delete this snapshot
  6328. */
  6329. btrfs_unlock_up_safe(path, 0);
  6330. level = btrfs_header_level(root->node);
  6331. while (1) {
  6332. btrfs_tree_lock(path->nodes[level]);
  6333. btrfs_set_lock_blocking(path->nodes[level]);
  6334. ret = btrfs_lookup_extent_info(trans, root,
  6335. path->nodes[level]->start,
  6336. path->nodes[level]->len,
  6337. &wc->refs[level],
  6338. &wc->flags[level]);
  6339. if (ret < 0) {
  6340. err = ret;
  6341. goto out_end_trans;
  6342. }
  6343. BUG_ON(wc->refs[level] == 0);
  6344. if (level == root_item->drop_level)
  6345. break;
  6346. btrfs_tree_unlock(path->nodes[level]);
  6347. WARN_ON(wc->refs[level] != 1);
  6348. level--;
  6349. }
  6350. }
  6351. wc->level = level;
  6352. wc->shared_level = -1;
  6353. wc->stage = DROP_REFERENCE;
  6354. wc->update_ref = update_ref;
  6355. wc->keep_locks = 0;
  6356. wc->for_reloc = for_reloc;
  6357. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6358. while (1) {
  6359. ret = walk_down_tree(trans, root, path, wc);
  6360. if (ret < 0) {
  6361. err = ret;
  6362. break;
  6363. }
  6364. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  6365. if (ret < 0) {
  6366. err = ret;
  6367. break;
  6368. }
  6369. if (ret > 0) {
  6370. BUG_ON(wc->stage != DROP_REFERENCE);
  6371. break;
  6372. }
  6373. if (wc->stage == DROP_REFERENCE) {
  6374. level = wc->level;
  6375. btrfs_node_key(path->nodes[level],
  6376. &root_item->drop_progress,
  6377. path->slots[level]);
  6378. root_item->drop_level = level;
  6379. }
  6380. BUG_ON(wc->level == 0);
  6381. if (btrfs_should_end_transaction(trans, tree_root)) {
  6382. ret = btrfs_update_root(trans, tree_root,
  6383. &root->root_key,
  6384. root_item);
  6385. if (ret) {
  6386. btrfs_abort_transaction(trans, tree_root, ret);
  6387. err = ret;
  6388. goto out_end_trans;
  6389. }
  6390. btrfs_end_transaction_throttle(trans, tree_root);
  6391. trans = btrfs_start_transaction(tree_root, 0);
  6392. if (IS_ERR(trans)) {
  6393. err = PTR_ERR(trans);
  6394. goto out_free;
  6395. }
  6396. if (block_rsv)
  6397. trans->block_rsv = block_rsv;
  6398. }
  6399. }
  6400. btrfs_release_path(path);
  6401. if (err)
  6402. goto out_end_trans;
  6403. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  6404. if (ret) {
  6405. btrfs_abort_transaction(trans, tree_root, ret);
  6406. goto out_end_trans;
  6407. }
  6408. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6409. ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
  6410. NULL, NULL);
  6411. if (ret < 0) {
  6412. btrfs_abort_transaction(trans, tree_root, ret);
  6413. err = ret;
  6414. goto out_end_trans;
  6415. } else if (ret > 0) {
  6416. /* if we fail to delete the orphan item this time
  6417. * around, it'll get picked up the next time.
  6418. *
  6419. * The most common failure here is just -ENOENT.
  6420. */
  6421. btrfs_del_orphan_item(trans, tree_root,
  6422. root->root_key.objectid);
  6423. }
  6424. }
  6425. if (root->in_radix) {
  6426. btrfs_free_fs_root(tree_root->fs_info, root);
  6427. } else {
  6428. free_extent_buffer(root->node);
  6429. free_extent_buffer(root->commit_root);
  6430. kfree(root);
  6431. }
  6432. out_end_trans:
  6433. btrfs_end_transaction_throttle(trans, tree_root);
  6434. out_free:
  6435. kfree(wc);
  6436. btrfs_free_path(path);
  6437. out:
  6438. if (err)
  6439. btrfs_std_error(root->fs_info, err);
  6440. return err;
  6441. }
  6442. /*
  6443. * drop subtree rooted at tree block 'node'.
  6444. *
  6445. * NOTE: this function will unlock and release tree block 'node'
  6446. * only used by relocation code
  6447. */
  6448. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6449. struct btrfs_root *root,
  6450. struct extent_buffer *node,
  6451. struct extent_buffer *parent)
  6452. {
  6453. struct btrfs_path *path;
  6454. struct walk_control *wc;
  6455. int level;
  6456. int parent_level;
  6457. int ret = 0;
  6458. int wret;
  6459. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6460. path = btrfs_alloc_path();
  6461. if (!path)
  6462. return -ENOMEM;
  6463. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6464. if (!wc) {
  6465. btrfs_free_path(path);
  6466. return -ENOMEM;
  6467. }
  6468. btrfs_assert_tree_locked(parent);
  6469. parent_level = btrfs_header_level(parent);
  6470. extent_buffer_get(parent);
  6471. path->nodes[parent_level] = parent;
  6472. path->slots[parent_level] = btrfs_header_nritems(parent);
  6473. btrfs_assert_tree_locked(node);
  6474. level = btrfs_header_level(node);
  6475. path->nodes[level] = node;
  6476. path->slots[level] = 0;
  6477. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6478. wc->refs[parent_level] = 1;
  6479. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6480. wc->level = level;
  6481. wc->shared_level = -1;
  6482. wc->stage = DROP_REFERENCE;
  6483. wc->update_ref = 0;
  6484. wc->keep_locks = 1;
  6485. wc->for_reloc = 1;
  6486. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6487. while (1) {
  6488. wret = walk_down_tree(trans, root, path, wc);
  6489. if (wret < 0) {
  6490. ret = wret;
  6491. break;
  6492. }
  6493. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6494. if (wret < 0)
  6495. ret = wret;
  6496. if (wret != 0)
  6497. break;
  6498. }
  6499. kfree(wc);
  6500. btrfs_free_path(path);
  6501. return ret;
  6502. }
  6503. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6504. {
  6505. u64 num_devices;
  6506. u64 stripped;
  6507. /*
  6508. * if restripe for this chunk_type is on pick target profile and
  6509. * return, otherwise do the usual balance
  6510. */
  6511. stripped = get_restripe_target(root->fs_info, flags);
  6512. if (stripped)
  6513. return extended_to_chunk(stripped);
  6514. /*
  6515. * we add in the count of missing devices because we want
  6516. * to make sure that any RAID levels on a degraded FS
  6517. * continue to be honored.
  6518. */
  6519. num_devices = root->fs_info->fs_devices->rw_devices +
  6520. root->fs_info->fs_devices->missing_devices;
  6521. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6522. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  6523. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6524. if (num_devices == 1) {
  6525. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6526. stripped = flags & ~stripped;
  6527. /* turn raid0 into single device chunks */
  6528. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6529. return stripped;
  6530. /* turn mirroring into duplication */
  6531. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6532. BTRFS_BLOCK_GROUP_RAID10))
  6533. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6534. } else {
  6535. /* they already had raid on here, just return */
  6536. if (flags & stripped)
  6537. return flags;
  6538. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6539. stripped = flags & ~stripped;
  6540. /* switch duplicated blocks with raid1 */
  6541. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6542. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6543. /* this is drive concat, leave it alone */
  6544. }
  6545. return flags;
  6546. }
  6547. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6548. {
  6549. struct btrfs_space_info *sinfo = cache->space_info;
  6550. u64 num_bytes;
  6551. u64 min_allocable_bytes;
  6552. int ret = -ENOSPC;
  6553. /*
  6554. * We need some metadata space and system metadata space for
  6555. * allocating chunks in some corner cases until we force to set
  6556. * it to be readonly.
  6557. */
  6558. if ((sinfo->flags &
  6559. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6560. !force)
  6561. min_allocable_bytes = 1 * 1024 * 1024;
  6562. else
  6563. min_allocable_bytes = 0;
  6564. spin_lock(&sinfo->lock);
  6565. spin_lock(&cache->lock);
  6566. if (cache->ro) {
  6567. ret = 0;
  6568. goto out;
  6569. }
  6570. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6571. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6572. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6573. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6574. min_allocable_bytes <= sinfo->total_bytes) {
  6575. sinfo->bytes_readonly += num_bytes;
  6576. cache->ro = 1;
  6577. ret = 0;
  6578. }
  6579. out:
  6580. spin_unlock(&cache->lock);
  6581. spin_unlock(&sinfo->lock);
  6582. return ret;
  6583. }
  6584. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6585. struct btrfs_block_group_cache *cache)
  6586. {
  6587. struct btrfs_trans_handle *trans;
  6588. u64 alloc_flags;
  6589. int ret;
  6590. BUG_ON(cache->ro);
  6591. trans = btrfs_join_transaction(root);
  6592. if (IS_ERR(trans))
  6593. return PTR_ERR(trans);
  6594. alloc_flags = update_block_group_flags(root, cache->flags);
  6595. if (alloc_flags != cache->flags) {
  6596. ret = do_chunk_alloc(trans, root, alloc_flags,
  6597. CHUNK_ALLOC_FORCE);
  6598. if (ret < 0)
  6599. goto out;
  6600. }
  6601. ret = set_block_group_ro(cache, 0);
  6602. if (!ret)
  6603. goto out;
  6604. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6605. ret = do_chunk_alloc(trans, root, alloc_flags,
  6606. CHUNK_ALLOC_FORCE);
  6607. if (ret < 0)
  6608. goto out;
  6609. ret = set_block_group_ro(cache, 0);
  6610. out:
  6611. btrfs_end_transaction(trans, root);
  6612. return ret;
  6613. }
  6614. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6615. struct btrfs_root *root, u64 type)
  6616. {
  6617. u64 alloc_flags = get_alloc_profile(root, type);
  6618. return do_chunk_alloc(trans, root, alloc_flags,
  6619. CHUNK_ALLOC_FORCE);
  6620. }
  6621. /*
  6622. * helper to account the unused space of all the readonly block group in the
  6623. * list. takes mirrors into account.
  6624. */
  6625. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  6626. {
  6627. struct btrfs_block_group_cache *block_group;
  6628. u64 free_bytes = 0;
  6629. int factor;
  6630. list_for_each_entry(block_group, groups_list, list) {
  6631. spin_lock(&block_group->lock);
  6632. if (!block_group->ro) {
  6633. spin_unlock(&block_group->lock);
  6634. continue;
  6635. }
  6636. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6637. BTRFS_BLOCK_GROUP_RAID10 |
  6638. BTRFS_BLOCK_GROUP_DUP))
  6639. factor = 2;
  6640. else
  6641. factor = 1;
  6642. free_bytes += (block_group->key.offset -
  6643. btrfs_block_group_used(&block_group->item)) *
  6644. factor;
  6645. spin_unlock(&block_group->lock);
  6646. }
  6647. return free_bytes;
  6648. }
  6649. /*
  6650. * helper to account the unused space of all the readonly block group in the
  6651. * space_info. takes mirrors into account.
  6652. */
  6653. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  6654. {
  6655. int i;
  6656. u64 free_bytes = 0;
  6657. spin_lock(&sinfo->lock);
  6658. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  6659. if (!list_empty(&sinfo->block_groups[i]))
  6660. free_bytes += __btrfs_get_ro_block_group_free_space(
  6661. &sinfo->block_groups[i]);
  6662. spin_unlock(&sinfo->lock);
  6663. return free_bytes;
  6664. }
  6665. void btrfs_set_block_group_rw(struct btrfs_root *root,
  6666. struct btrfs_block_group_cache *cache)
  6667. {
  6668. struct btrfs_space_info *sinfo = cache->space_info;
  6669. u64 num_bytes;
  6670. BUG_ON(!cache->ro);
  6671. spin_lock(&sinfo->lock);
  6672. spin_lock(&cache->lock);
  6673. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6674. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6675. sinfo->bytes_readonly -= num_bytes;
  6676. cache->ro = 0;
  6677. spin_unlock(&cache->lock);
  6678. spin_unlock(&sinfo->lock);
  6679. }
  6680. /*
  6681. * checks to see if its even possible to relocate this block group.
  6682. *
  6683. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  6684. * ok to go ahead and try.
  6685. */
  6686. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  6687. {
  6688. struct btrfs_block_group_cache *block_group;
  6689. struct btrfs_space_info *space_info;
  6690. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  6691. struct btrfs_device *device;
  6692. u64 min_free;
  6693. u64 dev_min = 1;
  6694. u64 dev_nr = 0;
  6695. u64 target;
  6696. int index;
  6697. int full = 0;
  6698. int ret = 0;
  6699. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  6700. /* odd, couldn't find the block group, leave it alone */
  6701. if (!block_group)
  6702. return -1;
  6703. min_free = btrfs_block_group_used(&block_group->item);
  6704. /* no bytes used, we're good */
  6705. if (!min_free)
  6706. goto out;
  6707. space_info = block_group->space_info;
  6708. spin_lock(&space_info->lock);
  6709. full = space_info->full;
  6710. /*
  6711. * if this is the last block group we have in this space, we can't
  6712. * relocate it unless we're able to allocate a new chunk below.
  6713. *
  6714. * Otherwise, we need to make sure we have room in the space to handle
  6715. * all of the extents from this block group. If we can, we're good
  6716. */
  6717. if ((space_info->total_bytes != block_group->key.offset) &&
  6718. (space_info->bytes_used + space_info->bytes_reserved +
  6719. space_info->bytes_pinned + space_info->bytes_readonly +
  6720. min_free < space_info->total_bytes)) {
  6721. spin_unlock(&space_info->lock);
  6722. goto out;
  6723. }
  6724. spin_unlock(&space_info->lock);
  6725. /*
  6726. * ok we don't have enough space, but maybe we have free space on our
  6727. * devices to allocate new chunks for relocation, so loop through our
  6728. * alloc devices and guess if we have enough space. if this block
  6729. * group is going to be restriped, run checks against the target
  6730. * profile instead of the current one.
  6731. */
  6732. ret = -1;
  6733. /*
  6734. * index:
  6735. * 0: raid10
  6736. * 1: raid1
  6737. * 2: dup
  6738. * 3: raid0
  6739. * 4: single
  6740. */
  6741. target = get_restripe_target(root->fs_info, block_group->flags);
  6742. if (target) {
  6743. index = __get_raid_index(extended_to_chunk(target));
  6744. } else {
  6745. /*
  6746. * this is just a balance, so if we were marked as full
  6747. * we know there is no space for a new chunk
  6748. */
  6749. if (full)
  6750. goto out;
  6751. index = get_block_group_index(block_group);
  6752. }
  6753. if (index == BTRFS_RAID_RAID10) {
  6754. dev_min = 4;
  6755. /* Divide by 2 */
  6756. min_free >>= 1;
  6757. } else if (index == BTRFS_RAID_RAID1) {
  6758. dev_min = 2;
  6759. } else if (index == BTRFS_RAID_DUP) {
  6760. /* Multiply by 2 */
  6761. min_free <<= 1;
  6762. } else if (index == BTRFS_RAID_RAID0) {
  6763. dev_min = fs_devices->rw_devices;
  6764. do_div(min_free, dev_min);
  6765. }
  6766. mutex_lock(&root->fs_info->chunk_mutex);
  6767. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  6768. u64 dev_offset;
  6769. /*
  6770. * check to make sure we can actually find a chunk with enough
  6771. * space to fit our block group in.
  6772. */
  6773. if (device->total_bytes > device->bytes_used + min_free &&
  6774. !device->is_tgtdev_for_dev_replace) {
  6775. ret = find_free_dev_extent(device, min_free,
  6776. &dev_offset, NULL);
  6777. if (!ret)
  6778. dev_nr++;
  6779. if (dev_nr >= dev_min)
  6780. break;
  6781. ret = -1;
  6782. }
  6783. }
  6784. mutex_unlock(&root->fs_info->chunk_mutex);
  6785. out:
  6786. btrfs_put_block_group(block_group);
  6787. return ret;
  6788. }
  6789. static int find_first_block_group(struct btrfs_root *root,
  6790. struct btrfs_path *path, struct btrfs_key *key)
  6791. {
  6792. int ret = 0;
  6793. struct btrfs_key found_key;
  6794. struct extent_buffer *leaf;
  6795. int slot;
  6796. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  6797. if (ret < 0)
  6798. goto out;
  6799. while (1) {
  6800. slot = path->slots[0];
  6801. leaf = path->nodes[0];
  6802. if (slot >= btrfs_header_nritems(leaf)) {
  6803. ret = btrfs_next_leaf(root, path);
  6804. if (ret == 0)
  6805. continue;
  6806. if (ret < 0)
  6807. goto out;
  6808. break;
  6809. }
  6810. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6811. if (found_key.objectid >= key->objectid &&
  6812. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  6813. ret = 0;
  6814. goto out;
  6815. }
  6816. path->slots[0]++;
  6817. }
  6818. out:
  6819. return ret;
  6820. }
  6821. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  6822. {
  6823. struct btrfs_block_group_cache *block_group;
  6824. u64 last = 0;
  6825. while (1) {
  6826. struct inode *inode;
  6827. block_group = btrfs_lookup_first_block_group(info, last);
  6828. while (block_group) {
  6829. spin_lock(&block_group->lock);
  6830. if (block_group->iref)
  6831. break;
  6832. spin_unlock(&block_group->lock);
  6833. block_group = next_block_group(info->tree_root,
  6834. block_group);
  6835. }
  6836. if (!block_group) {
  6837. if (last == 0)
  6838. break;
  6839. last = 0;
  6840. continue;
  6841. }
  6842. inode = block_group->inode;
  6843. block_group->iref = 0;
  6844. block_group->inode = NULL;
  6845. spin_unlock(&block_group->lock);
  6846. iput(inode);
  6847. last = block_group->key.objectid + block_group->key.offset;
  6848. btrfs_put_block_group(block_group);
  6849. }
  6850. }
  6851. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  6852. {
  6853. struct btrfs_block_group_cache *block_group;
  6854. struct btrfs_space_info *space_info;
  6855. struct btrfs_caching_control *caching_ctl;
  6856. struct rb_node *n;
  6857. down_write(&info->extent_commit_sem);
  6858. while (!list_empty(&info->caching_block_groups)) {
  6859. caching_ctl = list_entry(info->caching_block_groups.next,
  6860. struct btrfs_caching_control, list);
  6861. list_del(&caching_ctl->list);
  6862. put_caching_control(caching_ctl);
  6863. }
  6864. up_write(&info->extent_commit_sem);
  6865. spin_lock(&info->block_group_cache_lock);
  6866. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  6867. block_group = rb_entry(n, struct btrfs_block_group_cache,
  6868. cache_node);
  6869. rb_erase(&block_group->cache_node,
  6870. &info->block_group_cache_tree);
  6871. spin_unlock(&info->block_group_cache_lock);
  6872. down_write(&block_group->space_info->groups_sem);
  6873. list_del(&block_group->list);
  6874. up_write(&block_group->space_info->groups_sem);
  6875. if (block_group->cached == BTRFS_CACHE_STARTED)
  6876. wait_block_group_cache_done(block_group);
  6877. /*
  6878. * We haven't cached this block group, which means we could
  6879. * possibly have excluded extents on this block group.
  6880. */
  6881. if (block_group->cached == BTRFS_CACHE_NO)
  6882. free_excluded_extents(info->extent_root, block_group);
  6883. btrfs_remove_free_space_cache(block_group);
  6884. btrfs_put_block_group(block_group);
  6885. spin_lock(&info->block_group_cache_lock);
  6886. }
  6887. spin_unlock(&info->block_group_cache_lock);
  6888. /* now that all the block groups are freed, go through and
  6889. * free all the space_info structs. This is only called during
  6890. * the final stages of unmount, and so we know nobody is
  6891. * using them. We call synchronize_rcu() once before we start,
  6892. * just to be on the safe side.
  6893. */
  6894. synchronize_rcu();
  6895. release_global_block_rsv(info);
  6896. while(!list_empty(&info->space_info)) {
  6897. space_info = list_entry(info->space_info.next,
  6898. struct btrfs_space_info,
  6899. list);
  6900. if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
  6901. if (space_info->bytes_pinned > 0 ||
  6902. space_info->bytes_reserved > 0 ||
  6903. space_info->bytes_may_use > 0) {
  6904. WARN_ON(1);
  6905. dump_space_info(space_info, 0, 0);
  6906. }
  6907. }
  6908. list_del(&space_info->list);
  6909. kfree(space_info);
  6910. }
  6911. return 0;
  6912. }
  6913. static void __link_block_group(struct btrfs_space_info *space_info,
  6914. struct btrfs_block_group_cache *cache)
  6915. {
  6916. int index = get_block_group_index(cache);
  6917. down_write(&space_info->groups_sem);
  6918. list_add_tail(&cache->list, &space_info->block_groups[index]);
  6919. up_write(&space_info->groups_sem);
  6920. }
  6921. int btrfs_read_block_groups(struct btrfs_root *root)
  6922. {
  6923. struct btrfs_path *path;
  6924. int ret;
  6925. struct btrfs_block_group_cache *cache;
  6926. struct btrfs_fs_info *info = root->fs_info;
  6927. struct btrfs_space_info *space_info;
  6928. struct btrfs_key key;
  6929. struct btrfs_key found_key;
  6930. struct extent_buffer *leaf;
  6931. int need_clear = 0;
  6932. u64 cache_gen;
  6933. root = info->extent_root;
  6934. key.objectid = 0;
  6935. key.offset = 0;
  6936. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  6937. path = btrfs_alloc_path();
  6938. if (!path)
  6939. return -ENOMEM;
  6940. path->reada = 1;
  6941. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  6942. if (btrfs_test_opt(root, SPACE_CACHE) &&
  6943. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  6944. need_clear = 1;
  6945. if (btrfs_test_opt(root, CLEAR_CACHE))
  6946. need_clear = 1;
  6947. while (1) {
  6948. ret = find_first_block_group(root, path, &key);
  6949. if (ret > 0)
  6950. break;
  6951. if (ret != 0)
  6952. goto error;
  6953. leaf = path->nodes[0];
  6954. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6955. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6956. if (!cache) {
  6957. ret = -ENOMEM;
  6958. goto error;
  6959. }
  6960. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  6961. GFP_NOFS);
  6962. if (!cache->free_space_ctl) {
  6963. kfree(cache);
  6964. ret = -ENOMEM;
  6965. goto error;
  6966. }
  6967. atomic_set(&cache->count, 1);
  6968. spin_lock_init(&cache->lock);
  6969. cache->fs_info = info;
  6970. INIT_LIST_HEAD(&cache->list);
  6971. INIT_LIST_HEAD(&cache->cluster_list);
  6972. if (need_clear) {
  6973. /*
  6974. * When we mount with old space cache, we need to
  6975. * set BTRFS_DC_CLEAR and set dirty flag.
  6976. *
  6977. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  6978. * truncate the old free space cache inode and
  6979. * setup a new one.
  6980. * b) Setting 'dirty flag' makes sure that we flush
  6981. * the new space cache info onto disk.
  6982. */
  6983. cache->disk_cache_state = BTRFS_DC_CLEAR;
  6984. if (btrfs_test_opt(root, SPACE_CACHE))
  6985. cache->dirty = 1;
  6986. }
  6987. read_extent_buffer(leaf, &cache->item,
  6988. btrfs_item_ptr_offset(leaf, path->slots[0]),
  6989. sizeof(cache->item));
  6990. memcpy(&cache->key, &found_key, sizeof(found_key));
  6991. key.objectid = found_key.objectid + found_key.offset;
  6992. btrfs_release_path(path);
  6993. cache->flags = btrfs_block_group_flags(&cache->item);
  6994. cache->sectorsize = root->sectorsize;
  6995. cache->full_stripe_len = btrfs_full_stripe_len(root,
  6996. &root->fs_info->mapping_tree,
  6997. found_key.objectid);
  6998. btrfs_init_free_space_ctl(cache);
  6999. /*
  7000. * We need to exclude the super stripes now so that the space
  7001. * info has super bytes accounted for, otherwise we'll think
  7002. * we have more space than we actually do.
  7003. */
  7004. exclude_super_stripes(root, cache);
  7005. /*
  7006. * check for two cases, either we are full, and therefore
  7007. * don't need to bother with the caching work since we won't
  7008. * find any space, or we are empty, and we can just add all
  7009. * the space in and be done with it. This saves us _alot_ of
  7010. * time, particularly in the full case.
  7011. */
  7012. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  7013. cache->last_byte_to_unpin = (u64)-1;
  7014. cache->cached = BTRFS_CACHE_FINISHED;
  7015. free_excluded_extents(root, cache);
  7016. } else if (btrfs_block_group_used(&cache->item) == 0) {
  7017. cache->last_byte_to_unpin = (u64)-1;
  7018. cache->cached = BTRFS_CACHE_FINISHED;
  7019. add_new_free_space(cache, root->fs_info,
  7020. found_key.objectid,
  7021. found_key.objectid +
  7022. found_key.offset);
  7023. free_excluded_extents(root, cache);
  7024. }
  7025. ret = update_space_info(info, cache->flags, found_key.offset,
  7026. btrfs_block_group_used(&cache->item),
  7027. &space_info);
  7028. BUG_ON(ret); /* -ENOMEM */
  7029. cache->space_info = space_info;
  7030. spin_lock(&cache->space_info->lock);
  7031. cache->space_info->bytes_readonly += cache->bytes_super;
  7032. spin_unlock(&cache->space_info->lock);
  7033. __link_block_group(space_info, cache);
  7034. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7035. BUG_ON(ret); /* Logic error */
  7036. set_avail_alloc_bits(root->fs_info, cache->flags);
  7037. if (btrfs_chunk_readonly(root, cache->key.objectid))
  7038. set_block_group_ro(cache, 1);
  7039. }
  7040. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  7041. if (!(get_alloc_profile(root, space_info->flags) &
  7042. (BTRFS_BLOCK_GROUP_RAID10 |
  7043. BTRFS_BLOCK_GROUP_RAID1 |
  7044. BTRFS_BLOCK_GROUP_RAID5 |
  7045. BTRFS_BLOCK_GROUP_RAID6 |
  7046. BTRFS_BLOCK_GROUP_DUP)))
  7047. continue;
  7048. /*
  7049. * avoid allocating from un-mirrored block group if there are
  7050. * mirrored block groups.
  7051. */
  7052. list_for_each_entry(cache, &space_info->block_groups[3], list)
  7053. set_block_group_ro(cache, 1);
  7054. list_for_each_entry(cache, &space_info->block_groups[4], list)
  7055. set_block_group_ro(cache, 1);
  7056. }
  7057. init_global_block_rsv(info);
  7058. ret = 0;
  7059. error:
  7060. btrfs_free_path(path);
  7061. return ret;
  7062. }
  7063. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  7064. struct btrfs_root *root)
  7065. {
  7066. struct btrfs_block_group_cache *block_group, *tmp;
  7067. struct btrfs_root *extent_root = root->fs_info->extent_root;
  7068. struct btrfs_block_group_item item;
  7069. struct btrfs_key key;
  7070. int ret = 0;
  7071. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
  7072. new_bg_list) {
  7073. list_del_init(&block_group->new_bg_list);
  7074. if (ret)
  7075. continue;
  7076. spin_lock(&block_group->lock);
  7077. memcpy(&item, &block_group->item, sizeof(item));
  7078. memcpy(&key, &block_group->key, sizeof(key));
  7079. spin_unlock(&block_group->lock);
  7080. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  7081. sizeof(item));
  7082. if (ret)
  7083. btrfs_abort_transaction(trans, extent_root, ret);
  7084. }
  7085. }
  7086. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  7087. struct btrfs_root *root, u64 bytes_used,
  7088. u64 type, u64 chunk_objectid, u64 chunk_offset,
  7089. u64 size)
  7090. {
  7091. int ret;
  7092. struct btrfs_root *extent_root;
  7093. struct btrfs_block_group_cache *cache;
  7094. extent_root = root->fs_info->extent_root;
  7095. root->fs_info->last_trans_log_full_commit = trans->transid;
  7096. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7097. if (!cache)
  7098. return -ENOMEM;
  7099. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  7100. GFP_NOFS);
  7101. if (!cache->free_space_ctl) {
  7102. kfree(cache);
  7103. return -ENOMEM;
  7104. }
  7105. cache->key.objectid = chunk_offset;
  7106. cache->key.offset = size;
  7107. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  7108. cache->sectorsize = root->sectorsize;
  7109. cache->fs_info = root->fs_info;
  7110. cache->full_stripe_len = btrfs_full_stripe_len(root,
  7111. &root->fs_info->mapping_tree,
  7112. chunk_offset);
  7113. atomic_set(&cache->count, 1);
  7114. spin_lock_init(&cache->lock);
  7115. INIT_LIST_HEAD(&cache->list);
  7116. INIT_LIST_HEAD(&cache->cluster_list);
  7117. INIT_LIST_HEAD(&cache->new_bg_list);
  7118. btrfs_init_free_space_ctl(cache);
  7119. btrfs_set_block_group_used(&cache->item, bytes_used);
  7120. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  7121. cache->flags = type;
  7122. btrfs_set_block_group_flags(&cache->item, type);
  7123. cache->last_byte_to_unpin = (u64)-1;
  7124. cache->cached = BTRFS_CACHE_FINISHED;
  7125. exclude_super_stripes(root, cache);
  7126. add_new_free_space(cache, root->fs_info, chunk_offset,
  7127. chunk_offset + size);
  7128. free_excluded_extents(root, cache);
  7129. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  7130. &cache->space_info);
  7131. BUG_ON(ret); /* -ENOMEM */
  7132. update_global_block_rsv(root->fs_info);
  7133. spin_lock(&cache->space_info->lock);
  7134. cache->space_info->bytes_readonly += cache->bytes_super;
  7135. spin_unlock(&cache->space_info->lock);
  7136. __link_block_group(cache->space_info, cache);
  7137. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7138. BUG_ON(ret); /* Logic error */
  7139. list_add_tail(&cache->new_bg_list, &trans->new_bgs);
  7140. set_avail_alloc_bits(extent_root->fs_info, type);
  7141. return 0;
  7142. }
  7143. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  7144. {
  7145. u64 extra_flags = chunk_to_extended(flags) &
  7146. BTRFS_EXTENDED_PROFILE_MASK;
  7147. write_seqlock(&fs_info->profiles_lock);
  7148. if (flags & BTRFS_BLOCK_GROUP_DATA)
  7149. fs_info->avail_data_alloc_bits &= ~extra_flags;
  7150. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  7151. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  7152. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  7153. fs_info->avail_system_alloc_bits &= ~extra_flags;
  7154. write_sequnlock(&fs_info->profiles_lock);
  7155. }
  7156. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  7157. struct btrfs_root *root, u64 group_start)
  7158. {
  7159. struct btrfs_path *path;
  7160. struct btrfs_block_group_cache *block_group;
  7161. struct btrfs_free_cluster *cluster;
  7162. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7163. struct btrfs_key key;
  7164. struct inode *inode;
  7165. int ret;
  7166. int index;
  7167. int factor;
  7168. root = root->fs_info->extent_root;
  7169. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  7170. BUG_ON(!block_group);
  7171. BUG_ON(!block_group->ro);
  7172. /*
  7173. * Free the reserved super bytes from this block group before
  7174. * remove it.
  7175. */
  7176. free_excluded_extents(root, block_group);
  7177. memcpy(&key, &block_group->key, sizeof(key));
  7178. index = get_block_group_index(block_group);
  7179. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7180. BTRFS_BLOCK_GROUP_RAID1 |
  7181. BTRFS_BLOCK_GROUP_RAID10))
  7182. factor = 2;
  7183. else
  7184. factor = 1;
  7185. /* make sure this block group isn't part of an allocation cluster */
  7186. cluster = &root->fs_info->data_alloc_cluster;
  7187. spin_lock(&cluster->refill_lock);
  7188. btrfs_return_cluster_to_free_space(block_group, cluster);
  7189. spin_unlock(&cluster->refill_lock);
  7190. /*
  7191. * make sure this block group isn't part of a metadata
  7192. * allocation cluster
  7193. */
  7194. cluster = &root->fs_info->meta_alloc_cluster;
  7195. spin_lock(&cluster->refill_lock);
  7196. btrfs_return_cluster_to_free_space(block_group, cluster);
  7197. spin_unlock(&cluster->refill_lock);
  7198. path = btrfs_alloc_path();
  7199. if (!path) {
  7200. ret = -ENOMEM;
  7201. goto out;
  7202. }
  7203. inode = lookup_free_space_inode(tree_root, block_group, path);
  7204. if (!IS_ERR(inode)) {
  7205. ret = btrfs_orphan_add(trans, inode);
  7206. if (ret) {
  7207. btrfs_add_delayed_iput(inode);
  7208. goto out;
  7209. }
  7210. clear_nlink(inode);
  7211. /* One for the block groups ref */
  7212. spin_lock(&block_group->lock);
  7213. if (block_group->iref) {
  7214. block_group->iref = 0;
  7215. block_group->inode = NULL;
  7216. spin_unlock(&block_group->lock);
  7217. iput(inode);
  7218. } else {
  7219. spin_unlock(&block_group->lock);
  7220. }
  7221. /* One for our lookup ref */
  7222. btrfs_add_delayed_iput(inode);
  7223. }
  7224. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7225. key.offset = block_group->key.objectid;
  7226. key.type = 0;
  7227. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7228. if (ret < 0)
  7229. goto out;
  7230. if (ret > 0)
  7231. btrfs_release_path(path);
  7232. if (ret == 0) {
  7233. ret = btrfs_del_item(trans, tree_root, path);
  7234. if (ret)
  7235. goto out;
  7236. btrfs_release_path(path);
  7237. }
  7238. spin_lock(&root->fs_info->block_group_cache_lock);
  7239. rb_erase(&block_group->cache_node,
  7240. &root->fs_info->block_group_cache_tree);
  7241. if (root->fs_info->first_logical_byte == block_group->key.objectid)
  7242. root->fs_info->first_logical_byte = (u64)-1;
  7243. spin_unlock(&root->fs_info->block_group_cache_lock);
  7244. down_write(&block_group->space_info->groups_sem);
  7245. /*
  7246. * we must use list_del_init so people can check to see if they
  7247. * are still on the list after taking the semaphore
  7248. */
  7249. list_del_init(&block_group->list);
  7250. if (list_empty(&block_group->space_info->block_groups[index]))
  7251. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  7252. up_write(&block_group->space_info->groups_sem);
  7253. if (block_group->cached == BTRFS_CACHE_STARTED)
  7254. wait_block_group_cache_done(block_group);
  7255. btrfs_remove_free_space_cache(block_group);
  7256. spin_lock(&block_group->space_info->lock);
  7257. block_group->space_info->total_bytes -= block_group->key.offset;
  7258. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7259. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7260. spin_unlock(&block_group->space_info->lock);
  7261. memcpy(&key, &block_group->key, sizeof(key));
  7262. btrfs_clear_space_info_full(root->fs_info);
  7263. btrfs_put_block_group(block_group);
  7264. btrfs_put_block_group(block_group);
  7265. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7266. if (ret > 0)
  7267. ret = -EIO;
  7268. if (ret < 0)
  7269. goto out;
  7270. ret = btrfs_del_item(trans, root, path);
  7271. out:
  7272. btrfs_free_path(path);
  7273. return ret;
  7274. }
  7275. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  7276. {
  7277. struct btrfs_space_info *space_info;
  7278. struct btrfs_super_block *disk_super;
  7279. u64 features;
  7280. u64 flags;
  7281. int mixed = 0;
  7282. int ret;
  7283. disk_super = fs_info->super_copy;
  7284. if (!btrfs_super_root(disk_super))
  7285. return 1;
  7286. features = btrfs_super_incompat_flags(disk_super);
  7287. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  7288. mixed = 1;
  7289. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  7290. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7291. if (ret)
  7292. goto out;
  7293. if (mixed) {
  7294. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  7295. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7296. } else {
  7297. flags = BTRFS_BLOCK_GROUP_METADATA;
  7298. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7299. if (ret)
  7300. goto out;
  7301. flags = BTRFS_BLOCK_GROUP_DATA;
  7302. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7303. }
  7304. out:
  7305. return ret;
  7306. }
  7307. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  7308. {
  7309. return unpin_extent_range(root, start, end);
  7310. }
  7311. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  7312. u64 num_bytes, u64 *actual_bytes)
  7313. {
  7314. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  7315. }
  7316. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  7317. {
  7318. struct btrfs_fs_info *fs_info = root->fs_info;
  7319. struct btrfs_block_group_cache *cache = NULL;
  7320. u64 group_trimmed;
  7321. u64 start;
  7322. u64 end;
  7323. u64 trimmed = 0;
  7324. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  7325. int ret = 0;
  7326. /*
  7327. * try to trim all FS space, our block group may start from non-zero.
  7328. */
  7329. if (range->len == total_bytes)
  7330. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  7331. else
  7332. cache = btrfs_lookup_block_group(fs_info, range->start);
  7333. while (cache) {
  7334. if (cache->key.objectid >= (range->start + range->len)) {
  7335. btrfs_put_block_group(cache);
  7336. break;
  7337. }
  7338. start = max(range->start, cache->key.objectid);
  7339. end = min(range->start + range->len,
  7340. cache->key.objectid + cache->key.offset);
  7341. if (end - start >= range->minlen) {
  7342. if (!block_group_cache_done(cache)) {
  7343. ret = cache_block_group(cache, 0);
  7344. if (!ret)
  7345. wait_block_group_cache_done(cache);
  7346. }
  7347. ret = btrfs_trim_block_group(cache,
  7348. &group_trimmed,
  7349. start,
  7350. end,
  7351. range->minlen);
  7352. trimmed += group_trimmed;
  7353. if (ret) {
  7354. btrfs_put_block_group(cache);
  7355. break;
  7356. }
  7357. }
  7358. cache = next_block_group(fs_info->tree_root, cache);
  7359. }
  7360. range->len = trimmed;
  7361. return ret;
  7362. }