extent-tree.c 231 KB

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