extent-tree.c 216 KB

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