extent-tree.c 236 KB

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