extent-tree.c 215 KB

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