extent-tree.c 237 KB

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