extent-tree.c 217 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185
  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, 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. list_del_init(&locked_ref->cluster);
  2069. mutex_unlock(&locked_ref->mutex);
  2070. printk(KERN_DEBUG "btrfs: run_delayed_extent_op returned %d\n", ret);
  2071. spin_lock(&delayed_refs->lock);
  2072. return ret;
  2073. }
  2074. goto next;
  2075. }
  2076. list_del_init(&locked_ref->cluster);
  2077. locked_ref = NULL;
  2078. }
  2079. ref->in_tree = 0;
  2080. rb_erase(&ref->rb_node, &delayed_refs->root);
  2081. delayed_refs->num_entries--;
  2082. if (locked_ref) {
  2083. /*
  2084. * when we play the delayed ref, also correct the
  2085. * ref_mod on head
  2086. */
  2087. switch (ref->action) {
  2088. case BTRFS_ADD_DELAYED_REF:
  2089. case BTRFS_ADD_DELAYED_EXTENT:
  2090. locked_ref->node.ref_mod -= ref->ref_mod;
  2091. break;
  2092. case BTRFS_DROP_DELAYED_REF:
  2093. locked_ref->node.ref_mod += ref->ref_mod;
  2094. break;
  2095. default:
  2096. WARN_ON(1);
  2097. }
  2098. }
  2099. spin_unlock(&delayed_refs->lock);
  2100. ret = run_one_delayed_ref(trans, root, ref, extent_op,
  2101. must_insert_reserved);
  2102. btrfs_put_delayed_ref(ref);
  2103. kfree(extent_op);
  2104. count++;
  2105. if (ret) {
  2106. if (locked_ref) {
  2107. list_del_init(&locked_ref->cluster);
  2108. mutex_unlock(&locked_ref->mutex);
  2109. }
  2110. printk(KERN_DEBUG "btrfs: run_one_delayed_ref returned %d\n", ret);
  2111. spin_lock(&delayed_refs->lock);
  2112. return ret;
  2113. }
  2114. next:
  2115. cond_resched();
  2116. spin_lock(&delayed_refs->lock);
  2117. }
  2118. return count;
  2119. }
  2120. #ifdef SCRAMBLE_DELAYED_REFS
  2121. /*
  2122. * Normally delayed refs get processed in ascending bytenr order. This
  2123. * correlates in most cases to the order added. To expose dependencies on this
  2124. * order, we start to process the tree in the middle instead of the beginning
  2125. */
  2126. static u64 find_middle(struct rb_root *root)
  2127. {
  2128. struct rb_node *n = root->rb_node;
  2129. struct btrfs_delayed_ref_node *entry;
  2130. int alt = 1;
  2131. u64 middle;
  2132. u64 first = 0, last = 0;
  2133. n = rb_first(root);
  2134. if (n) {
  2135. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2136. first = entry->bytenr;
  2137. }
  2138. n = rb_last(root);
  2139. if (n) {
  2140. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2141. last = entry->bytenr;
  2142. }
  2143. n = root->rb_node;
  2144. while (n) {
  2145. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  2146. WARN_ON(!entry->in_tree);
  2147. middle = entry->bytenr;
  2148. if (alt)
  2149. n = n->rb_left;
  2150. else
  2151. n = n->rb_right;
  2152. alt = 1 - alt;
  2153. }
  2154. return middle;
  2155. }
  2156. #endif
  2157. int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
  2158. struct btrfs_fs_info *fs_info)
  2159. {
  2160. struct qgroup_update *qgroup_update;
  2161. int ret = 0;
  2162. if (list_empty(&trans->qgroup_ref_list) !=
  2163. !trans->delayed_ref_elem.seq) {
  2164. /* list without seq or seq without list */
  2165. printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
  2166. list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2167. trans->delayed_ref_elem.seq);
  2168. BUG();
  2169. }
  2170. if (!trans->delayed_ref_elem.seq)
  2171. return 0;
  2172. while (!list_empty(&trans->qgroup_ref_list)) {
  2173. qgroup_update = list_first_entry(&trans->qgroup_ref_list,
  2174. struct qgroup_update, list);
  2175. list_del(&qgroup_update->list);
  2176. if (!ret)
  2177. ret = btrfs_qgroup_account_ref(
  2178. trans, fs_info, qgroup_update->node,
  2179. qgroup_update->extent_op);
  2180. kfree(qgroup_update);
  2181. }
  2182. btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  2183. return ret;
  2184. }
  2185. /*
  2186. * this starts processing the delayed reference count updates and
  2187. * extent insertions we have queued up so far. count can be
  2188. * 0, which means to process everything in the tree at the start
  2189. * of the run (but not newly added entries), or it can be some target
  2190. * number you'd like to process.
  2191. *
  2192. * Returns 0 on success or if called with an aborted transaction
  2193. * Returns <0 on error and aborts the transaction
  2194. */
  2195. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2196. struct btrfs_root *root, unsigned long count)
  2197. {
  2198. struct rb_node *node;
  2199. struct btrfs_delayed_ref_root *delayed_refs;
  2200. struct btrfs_delayed_ref_node *ref;
  2201. struct list_head cluster;
  2202. int ret;
  2203. u64 delayed_start;
  2204. int run_all = count == (unsigned long)-1;
  2205. int run_most = 0;
  2206. int loops;
  2207. /* We'll clean this up in btrfs_cleanup_transaction */
  2208. if (trans->aborted)
  2209. return 0;
  2210. if (root == root->fs_info->extent_root)
  2211. root = root->fs_info->tree_root;
  2212. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  2213. delayed_refs = &trans->transaction->delayed_refs;
  2214. INIT_LIST_HEAD(&cluster);
  2215. again:
  2216. loops = 0;
  2217. spin_lock(&delayed_refs->lock);
  2218. #ifdef SCRAMBLE_DELAYED_REFS
  2219. delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
  2220. #endif
  2221. if (count == 0) {
  2222. count = delayed_refs->num_entries * 2;
  2223. run_most = 1;
  2224. }
  2225. while (1) {
  2226. if (!(run_all || run_most) &&
  2227. delayed_refs->num_heads_ready < 64)
  2228. break;
  2229. /*
  2230. * go find something we can process in the rbtree. We start at
  2231. * the beginning of the tree, and then build a cluster
  2232. * of refs to process starting at the first one we are able to
  2233. * lock
  2234. */
  2235. delayed_start = delayed_refs->run_delayed_start;
  2236. ret = btrfs_find_ref_cluster(trans, &cluster,
  2237. delayed_refs->run_delayed_start);
  2238. if (ret)
  2239. break;
  2240. ret = run_clustered_refs(trans, root, &cluster);
  2241. if (ret < 0) {
  2242. spin_unlock(&delayed_refs->lock);
  2243. btrfs_abort_transaction(trans, root, ret);
  2244. return ret;
  2245. }
  2246. count -= min_t(unsigned long, ret, count);
  2247. if (count == 0)
  2248. break;
  2249. if (delayed_start >= delayed_refs->run_delayed_start) {
  2250. if (loops == 0) {
  2251. /*
  2252. * btrfs_find_ref_cluster looped. let's do one
  2253. * more cycle. if we don't run any delayed ref
  2254. * during that cycle (because we can't because
  2255. * all of them are blocked), bail out.
  2256. */
  2257. loops = 1;
  2258. } else {
  2259. /*
  2260. * no runnable refs left, stop trying
  2261. */
  2262. BUG_ON(run_all);
  2263. break;
  2264. }
  2265. }
  2266. if (ret) {
  2267. /* refs were run, let's reset staleness detection */
  2268. loops = 0;
  2269. }
  2270. }
  2271. if (run_all) {
  2272. if (!list_empty(&trans->new_bgs)) {
  2273. spin_unlock(&delayed_refs->lock);
  2274. btrfs_create_pending_block_groups(trans, root);
  2275. spin_lock(&delayed_refs->lock);
  2276. }
  2277. node = rb_first(&delayed_refs->root);
  2278. if (!node)
  2279. goto out;
  2280. count = (unsigned long)-1;
  2281. while (node) {
  2282. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  2283. rb_node);
  2284. if (btrfs_delayed_ref_is_head(ref)) {
  2285. struct btrfs_delayed_ref_head *head;
  2286. head = btrfs_delayed_node_to_head(ref);
  2287. atomic_inc(&ref->refs);
  2288. spin_unlock(&delayed_refs->lock);
  2289. /*
  2290. * Mutex was contended, block until it's
  2291. * released and try again
  2292. */
  2293. mutex_lock(&head->mutex);
  2294. mutex_unlock(&head->mutex);
  2295. btrfs_put_delayed_ref(ref);
  2296. cond_resched();
  2297. goto again;
  2298. }
  2299. node = rb_next(node);
  2300. }
  2301. spin_unlock(&delayed_refs->lock);
  2302. schedule_timeout(1);
  2303. goto again;
  2304. }
  2305. out:
  2306. spin_unlock(&delayed_refs->lock);
  2307. assert_qgroups_uptodate(trans);
  2308. return 0;
  2309. }
  2310. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2311. struct btrfs_root *root,
  2312. u64 bytenr, u64 num_bytes, u64 flags,
  2313. int is_data)
  2314. {
  2315. struct btrfs_delayed_extent_op *extent_op;
  2316. int ret;
  2317. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2318. if (!extent_op)
  2319. return -ENOMEM;
  2320. extent_op->flags_to_set = flags;
  2321. extent_op->update_flags = 1;
  2322. extent_op->update_key = 0;
  2323. extent_op->is_data = is_data ? 1 : 0;
  2324. ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
  2325. num_bytes, extent_op);
  2326. if (ret)
  2327. kfree(extent_op);
  2328. return ret;
  2329. }
  2330. static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
  2331. struct btrfs_root *root,
  2332. struct btrfs_path *path,
  2333. u64 objectid, u64 offset, u64 bytenr)
  2334. {
  2335. struct btrfs_delayed_ref_head *head;
  2336. struct btrfs_delayed_ref_node *ref;
  2337. struct btrfs_delayed_data_ref *data_ref;
  2338. struct btrfs_delayed_ref_root *delayed_refs;
  2339. struct rb_node *node;
  2340. int ret = 0;
  2341. ret = -ENOENT;
  2342. delayed_refs = &trans->transaction->delayed_refs;
  2343. spin_lock(&delayed_refs->lock);
  2344. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2345. if (!head)
  2346. goto out;
  2347. if (!mutex_trylock(&head->mutex)) {
  2348. atomic_inc(&head->node.refs);
  2349. spin_unlock(&delayed_refs->lock);
  2350. btrfs_release_path(path);
  2351. /*
  2352. * Mutex was contended, block until it's released and let
  2353. * caller try again
  2354. */
  2355. mutex_lock(&head->mutex);
  2356. mutex_unlock(&head->mutex);
  2357. btrfs_put_delayed_ref(&head->node);
  2358. return -EAGAIN;
  2359. }
  2360. node = rb_prev(&head->node.rb_node);
  2361. if (!node)
  2362. goto out_unlock;
  2363. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2364. if (ref->bytenr != bytenr)
  2365. goto out_unlock;
  2366. ret = 1;
  2367. if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
  2368. goto out_unlock;
  2369. data_ref = btrfs_delayed_node_to_data_ref(ref);
  2370. node = rb_prev(node);
  2371. if (node) {
  2372. int seq = ref->seq;
  2373. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2374. if (ref->bytenr == bytenr && ref->seq == seq)
  2375. goto out_unlock;
  2376. }
  2377. if (data_ref->root != root->root_key.objectid ||
  2378. data_ref->objectid != objectid || data_ref->offset != offset)
  2379. goto out_unlock;
  2380. ret = 0;
  2381. out_unlock:
  2382. mutex_unlock(&head->mutex);
  2383. out:
  2384. spin_unlock(&delayed_refs->lock);
  2385. return ret;
  2386. }
  2387. static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
  2388. struct btrfs_root *root,
  2389. struct btrfs_path *path,
  2390. u64 objectid, u64 offset, u64 bytenr)
  2391. {
  2392. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2393. struct extent_buffer *leaf;
  2394. struct btrfs_extent_data_ref *ref;
  2395. struct btrfs_extent_inline_ref *iref;
  2396. struct btrfs_extent_item *ei;
  2397. struct btrfs_key key;
  2398. u32 item_size;
  2399. int ret;
  2400. key.objectid = bytenr;
  2401. key.offset = (u64)-1;
  2402. key.type = BTRFS_EXTENT_ITEM_KEY;
  2403. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2404. if (ret < 0)
  2405. goto out;
  2406. BUG_ON(ret == 0); /* Corruption */
  2407. ret = -ENOENT;
  2408. if (path->slots[0] == 0)
  2409. goto out;
  2410. path->slots[0]--;
  2411. leaf = path->nodes[0];
  2412. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2413. if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
  2414. goto out;
  2415. ret = 1;
  2416. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2417. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2418. if (item_size < sizeof(*ei)) {
  2419. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2420. goto out;
  2421. }
  2422. #endif
  2423. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  2424. if (item_size != sizeof(*ei) +
  2425. btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
  2426. goto out;
  2427. if (btrfs_extent_generation(leaf, ei) <=
  2428. btrfs_root_last_snapshot(&root->root_item))
  2429. goto out;
  2430. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2431. if (btrfs_extent_inline_ref_type(leaf, iref) !=
  2432. BTRFS_EXTENT_DATA_REF_KEY)
  2433. goto out;
  2434. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  2435. if (btrfs_extent_refs(leaf, ei) !=
  2436. btrfs_extent_data_ref_count(leaf, ref) ||
  2437. btrfs_extent_data_ref_root(leaf, ref) !=
  2438. root->root_key.objectid ||
  2439. btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
  2440. btrfs_extent_data_ref_offset(leaf, ref) != offset)
  2441. goto out;
  2442. ret = 0;
  2443. out:
  2444. return ret;
  2445. }
  2446. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  2447. struct btrfs_root *root,
  2448. u64 objectid, u64 offset, u64 bytenr)
  2449. {
  2450. struct btrfs_path *path;
  2451. int ret;
  2452. int ret2;
  2453. path = btrfs_alloc_path();
  2454. if (!path)
  2455. return -ENOENT;
  2456. do {
  2457. ret = check_committed_ref(trans, root, path, objectid,
  2458. offset, bytenr);
  2459. if (ret && ret != -ENOENT)
  2460. goto out;
  2461. ret2 = check_delayed_ref(trans, root, path, objectid,
  2462. offset, bytenr);
  2463. } while (ret2 == -EAGAIN);
  2464. if (ret2 && ret2 != -ENOENT) {
  2465. ret = ret2;
  2466. goto out;
  2467. }
  2468. if (ret != -ENOENT || ret2 != -ENOENT)
  2469. ret = 0;
  2470. out:
  2471. btrfs_free_path(path);
  2472. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  2473. WARN_ON(ret > 0);
  2474. return ret;
  2475. }
  2476. static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
  2477. struct btrfs_root *root,
  2478. struct extent_buffer *buf,
  2479. int full_backref, int inc, int for_cow)
  2480. {
  2481. u64 bytenr;
  2482. u64 num_bytes;
  2483. u64 parent;
  2484. u64 ref_root;
  2485. u32 nritems;
  2486. struct btrfs_key key;
  2487. struct btrfs_file_extent_item *fi;
  2488. int i;
  2489. int level;
  2490. int ret = 0;
  2491. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  2492. u64, u64, u64, u64, u64, u64, int);
  2493. ref_root = btrfs_header_owner(buf);
  2494. nritems = btrfs_header_nritems(buf);
  2495. level = btrfs_header_level(buf);
  2496. if (!root->ref_cows && level == 0)
  2497. return 0;
  2498. if (inc)
  2499. process_func = btrfs_inc_extent_ref;
  2500. else
  2501. process_func = btrfs_free_extent;
  2502. if (full_backref)
  2503. parent = buf->start;
  2504. else
  2505. parent = 0;
  2506. for (i = 0; i < nritems; i++) {
  2507. if (level == 0) {
  2508. btrfs_item_key_to_cpu(buf, &key, i);
  2509. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2510. continue;
  2511. fi = btrfs_item_ptr(buf, i,
  2512. struct btrfs_file_extent_item);
  2513. if (btrfs_file_extent_type(buf, fi) ==
  2514. BTRFS_FILE_EXTENT_INLINE)
  2515. continue;
  2516. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  2517. if (bytenr == 0)
  2518. continue;
  2519. num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
  2520. key.offset -= btrfs_file_extent_offset(buf, fi);
  2521. ret = process_func(trans, root, bytenr, num_bytes,
  2522. parent, ref_root, key.objectid,
  2523. key.offset, for_cow);
  2524. if (ret)
  2525. goto fail;
  2526. } else {
  2527. bytenr = btrfs_node_blockptr(buf, i);
  2528. num_bytes = btrfs_level_size(root, level - 1);
  2529. ret = process_func(trans, root, bytenr, num_bytes,
  2530. parent, ref_root, level - 1, 0,
  2531. for_cow);
  2532. if (ret)
  2533. goto fail;
  2534. }
  2535. }
  2536. return 0;
  2537. fail:
  2538. return ret;
  2539. }
  2540. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2541. struct extent_buffer *buf, int full_backref, int for_cow)
  2542. {
  2543. return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
  2544. }
  2545. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2546. struct extent_buffer *buf, int full_backref, int for_cow)
  2547. {
  2548. return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
  2549. }
  2550. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  2551. struct btrfs_root *root,
  2552. struct btrfs_path *path,
  2553. struct btrfs_block_group_cache *cache)
  2554. {
  2555. int ret;
  2556. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2557. unsigned long bi;
  2558. struct extent_buffer *leaf;
  2559. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  2560. if (ret < 0)
  2561. goto fail;
  2562. BUG_ON(ret); /* Corruption */
  2563. leaf = path->nodes[0];
  2564. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2565. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  2566. btrfs_mark_buffer_dirty(leaf);
  2567. btrfs_release_path(path);
  2568. fail:
  2569. if (ret) {
  2570. btrfs_abort_transaction(trans, root, ret);
  2571. return ret;
  2572. }
  2573. return 0;
  2574. }
  2575. static struct btrfs_block_group_cache *
  2576. next_block_group(struct btrfs_root *root,
  2577. struct btrfs_block_group_cache *cache)
  2578. {
  2579. struct rb_node *node;
  2580. spin_lock(&root->fs_info->block_group_cache_lock);
  2581. node = rb_next(&cache->cache_node);
  2582. btrfs_put_block_group(cache);
  2583. if (node) {
  2584. cache = rb_entry(node, struct btrfs_block_group_cache,
  2585. cache_node);
  2586. btrfs_get_block_group(cache);
  2587. } else
  2588. cache = NULL;
  2589. spin_unlock(&root->fs_info->block_group_cache_lock);
  2590. return cache;
  2591. }
  2592. static int cache_save_setup(struct btrfs_block_group_cache *block_group,
  2593. struct btrfs_trans_handle *trans,
  2594. struct btrfs_path *path)
  2595. {
  2596. struct btrfs_root *root = block_group->fs_info->tree_root;
  2597. struct inode *inode = NULL;
  2598. u64 alloc_hint = 0;
  2599. int dcs = BTRFS_DC_ERROR;
  2600. int num_pages = 0;
  2601. int retries = 0;
  2602. int ret = 0;
  2603. /*
  2604. * If this block group is smaller than 100 megs don't bother caching the
  2605. * block group.
  2606. */
  2607. if (block_group->key.offset < (100 * 1024 * 1024)) {
  2608. spin_lock(&block_group->lock);
  2609. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  2610. spin_unlock(&block_group->lock);
  2611. return 0;
  2612. }
  2613. again:
  2614. inode = lookup_free_space_inode(root, block_group, path);
  2615. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  2616. ret = PTR_ERR(inode);
  2617. btrfs_release_path(path);
  2618. goto out;
  2619. }
  2620. if (IS_ERR(inode)) {
  2621. BUG_ON(retries);
  2622. retries++;
  2623. if (block_group->ro)
  2624. goto out_free;
  2625. ret = create_free_space_inode(root, trans, block_group, path);
  2626. if (ret)
  2627. goto out_free;
  2628. goto again;
  2629. }
  2630. /* We've already setup this transaction, go ahead and exit */
  2631. if (block_group->cache_generation == trans->transid &&
  2632. i_size_read(inode)) {
  2633. dcs = BTRFS_DC_SETUP;
  2634. goto out_put;
  2635. }
  2636. /*
  2637. * We want to set the generation to 0, that way if anything goes wrong
  2638. * from here on out we know not to trust this cache when we load up next
  2639. * time.
  2640. */
  2641. BTRFS_I(inode)->generation = 0;
  2642. ret = btrfs_update_inode(trans, root, inode);
  2643. WARN_ON(ret);
  2644. if (i_size_read(inode) > 0) {
  2645. ret = btrfs_truncate_free_space_cache(root, trans, path,
  2646. inode);
  2647. if (ret)
  2648. goto out_put;
  2649. }
  2650. spin_lock(&block_group->lock);
  2651. if (block_group->cached != BTRFS_CACHE_FINISHED ||
  2652. !btrfs_test_opt(root, SPACE_CACHE)) {
  2653. /*
  2654. * don't bother trying to write stuff out _if_
  2655. * a) we're not cached,
  2656. * b) we're with nospace_cache mount option.
  2657. */
  2658. dcs = BTRFS_DC_WRITTEN;
  2659. spin_unlock(&block_group->lock);
  2660. goto out_put;
  2661. }
  2662. spin_unlock(&block_group->lock);
  2663. /*
  2664. * Try to preallocate enough space based on how big the block group is.
  2665. * Keep in mind this has to include any pinned space which could end up
  2666. * taking up quite a bit since it's not folded into the other space
  2667. * cache.
  2668. */
  2669. num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
  2670. if (!num_pages)
  2671. num_pages = 1;
  2672. num_pages *= 16;
  2673. num_pages *= PAGE_CACHE_SIZE;
  2674. ret = btrfs_check_data_free_space(inode, num_pages);
  2675. if (ret)
  2676. goto out_put;
  2677. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
  2678. num_pages, num_pages,
  2679. &alloc_hint);
  2680. if (!ret)
  2681. dcs = BTRFS_DC_SETUP;
  2682. btrfs_free_reserved_data_space(inode, num_pages);
  2683. out_put:
  2684. iput(inode);
  2685. out_free:
  2686. btrfs_release_path(path);
  2687. out:
  2688. spin_lock(&block_group->lock);
  2689. if (!ret && dcs == BTRFS_DC_SETUP)
  2690. block_group->cache_generation = trans->transid;
  2691. block_group->disk_cache_state = dcs;
  2692. spin_unlock(&block_group->lock);
  2693. return ret;
  2694. }
  2695. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2696. struct btrfs_root *root)
  2697. {
  2698. struct btrfs_block_group_cache *cache;
  2699. int err = 0;
  2700. struct btrfs_path *path;
  2701. u64 last = 0;
  2702. path = btrfs_alloc_path();
  2703. if (!path)
  2704. return -ENOMEM;
  2705. again:
  2706. while (1) {
  2707. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2708. while (cache) {
  2709. if (cache->disk_cache_state == BTRFS_DC_CLEAR)
  2710. break;
  2711. cache = next_block_group(root, cache);
  2712. }
  2713. if (!cache) {
  2714. if (last == 0)
  2715. break;
  2716. last = 0;
  2717. continue;
  2718. }
  2719. err = cache_save_setup(cache, trans, path);
  2720. last = cache->key.objectid + cache->key.offset;
  2721. btrfs_put_block_group(cache);
  2722. }
  2723. while (1) {
  2724. if (last == 0) {
  2725. err = btrfs_run_delayed_refs(trans, root,
  2726. (unsigned long)-1);
  2727. if (err) /* File system offline */
  2728. goto out;
  2729. }
  2730. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2731. while (cache) {
  2732. if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
  2733. btrfs_put_block_group(cache);
  2734. goto again;
  2735. }
  2736. if (cache->dirty)
  2737. break;
  2738. cache = next_block_group(root, cache);
  2739. }
  2740. if (!cache) {
  2741. if (last == 0)
  2742. break;
  2743. last = 0;
  2744. continue;
  2745. }
  2746. if (cache->disk_cache_state == BTRFS_DC_SETUP)
  2747. cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
  2748. cache->dirty = 0;
  2749. last = cache->key.objectid + cache->key.offset;
  2750. err = write_one_cache_group(trans, root, path, cache);
  2751. if (err) /* File system offline */
  2752. goto out;
  2753. btrfs_put_block_group(cache);
  2754. }
  2755. while (1) {
  2756. /*
  2757. * I don't think this is needed since we're just marking our
  2758. * preallocated extent as written, but just in case it can't
  2759. * hurt.
  2760. */
  2761. if (last == 0) {
  2762. err = btrfs_run_delayed_refs(trans, root,
  2763. (unsigned long)-1);
  2764. if (err) /* File system offline */
  2765. goto out;
  2766. }
  2767. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  2768. while (cache) {
  2769. /*
  2770. * Really this shouldn't happen, but it could if we
  2771. * couldn't write the entire preallocated extent and
  2772. * splitting the extent resulted in a new block.
  2773. */
  2774. if (cache->dirty) {
  2775. btrfs_put_block_group(cache);
  2776. goto again;
  2777. }
  2778. if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2779. break;
  2780. cache = next_block_group(root, cache);
  2781. }
  2782. if (!cache) {
  2783. if (last == 0)
  2784. break;
  2785. last = 0;
  2786. continue;
  2787. }
  2788. err = btrfs_write_out_cache(root, trans, cache, path);
  2789. /*
  2790. * If we didn't have an error then the cache state is still
  2791. * NEED_WRITE, so we can set it to WRITTEN.
  2792. */
  2793. if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
  2794. cache->disk_cache_state = BTRFS_DC_WRITTEN;
  2795. last = cache->key.objectid + cache->key.offset;
  2796. btrfs_put_block_group(cache);
  2797. }
  2798. out:
  2799. btrfs_free_path(path);
  2800. return err;
  2801. }
  2802. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2803. {
  2804. struct btrfs_block_group_cache *block_group;
  2805. int readonly = 0;
  2806. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2807. if (!block_group || block_group->ro)
  2808. readonly = 1;
  2809. if (block_group)
  2810. btrfs_put_block_group(block_group);
  2811. return readonly;
  2812. }
  2813. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  2814. u64 total_bytes, u64 bytes_used,
  2815. struct btrfs_space_info **space_info)
  2816. {
  2817. struct btrfs_space_info *found;
  2818. int i;
  2819. int factor;
  2820. if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2821. BTRFS_BLOCK_GROUP_RAID10))
  2822. factor = 2;
  2823. else
  2824. factor = 1;
  2825. found = __find_space_info(info, flags);
  2826. if (found) {
  2827. spin_lock(&found->lock);
  2828. found->total_bytes += total_bytes;
  2829. found->disk_total += total_bytes * factor;
  2830. found->bytes_used += bytes_used;
  2831. found->disk_used += bytes_used * factor;
  2832. found->full = 0;
  2833. spin_unlock(&found->lock);
  2834. *space_info = found;
  2835. return 0;
  2836. }
  2837. found = kzalloc(sizeof(*found), GFP_NOFS);
  2838. if (!found)
  2839. return -ENOMEM;
  2840. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  2841. INIT_LIST_HEAD(&found->block_groups[i]);
  2842. init_rwsem(&found->groups_sem);
  2843. spin_lock_init(&found->lock);
  2844. found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  2845. found->total_bytes = total_bytes;
  2846. found->disk_total = total_bytes * factor;
  2847. found->bytes_used = bytes_used;
  2848. found->disk_used = bytes_used * factor;
  2849. found->bytes_pinned = 0;
  2850. found->bytes_reserved = 0;
  2851. found->bytes_readonly = 0;
  2852. found->bytes_may_use = 0;
  2853. found->full = 0;
  2854. found->force_alloc = CHUNK_ALLOC_NO_FORCE;
  2855. found->chunk_alloc = 0;
  2856. found->flush = 0;
  2857. init_waitqueue_head(&found->wait);
  2858. *space_info = found;
  2859. list_add_rcu(&found->list, &info->space_info);
  2860. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2861. info->data_sinfo = found;
  2862. return 0;
  2863. }
  2864. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  2865. {
  2866. u64 extra_flags = chunk_to_extended(flags) &
  2867. BTRFS_EXTENDED_PROFILE_MASK;
  2868. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2869. fs_info->avail_data_alloc_bits |= extra_flags;
  2870. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2871. fs_info->avail_metadata_alloc_bits |= extra_flags;
  2872. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2873. fs_info->avail_system_alloc_bits |= extra_flags;
  2874. }
  2875. /*
  2876. * returns target flags in extended format or 0 if restripe for this
  2877. * chunk_type is not in progress
  2878. *
  2879. * should be called with either volume_mutex or balance_lock held
  2880. */
  2881. static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
  2882. {
  2883. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2884. u64 target = 0;
  2885. if (!bctl)
  2886. return 0;
  2887. if (flags & BTRFS_BLOCK_GROUP_DATA &&
  2888. bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2889. target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
  2890. } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
  2891. bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2892. target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
  2893. } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
  2894. bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2895. target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
  2896. }
  2897. return target;
  2898. }
  2899. /*
  2900. * @flags: available profiles in extended format (see ctree.h)
  2901. *
  2902. * Returns reduced profile in chunk format. If profile changing is in
  2903. * progress (either running or paused) picks the target profile (if it's
  2904. * already available), otherwise falls back to plain reducing.
  2905. */
  2906. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  2907. {
  2908. /*
  2909. * we add in the count of missing devices because we want
  2910. * to make sure that any RAID levels on a degraded FS
  2911. * continue to be honored.
  2912. */
  2913. u64 num_devices = root->fs_info->fs_devices->rw_devices +
  2914. root->fs_info->fs_devices->missing_devices;
  2915. u64 target;
  2916. /*
  2917. * see if restripe for this chunk_type is in progress, if so
  2918. * try to reduce to the target profile
  2919. */
  2920. spin_lock(&root->fs_info->balance_lock);
  2921. target = get_restripe_target(root->fs_info, flags);
  2922. if (target) {
  2923. /* pick target profile only if it's already available */
  2924. if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
  2925. spin_unlock(&root->fs_info->balance_lock);
  2926. return extended_to_chunk(target);
  2927. }
  2928. }
  2929. spin_unlock(&root->fs_info->balance_lock);
  2930. if (num_devices == 1)
  2931. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  2932. if (num_devices < 4)
  2933. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  2934. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  2935. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  2936. BTRFS_BLOCK_GROUP_RAID10))) {
  2937. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  2938. }
  2939. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  2940. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  2941. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  2942. }
  2943. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  2944. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  2945. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  2946. (flags & BTRFS_BLOCK_GROUP_DUP))) {
  2947. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  2948. }
  2949. return extended_to_chunk(flags);
  2950. }
  2951. static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
  2952. {
  2953. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2954. flags |= root->fs_info->avail_data_alloc_bits;
  2955. else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2956. flags |= root->fs_info->avail_system_alloc_bits;
  2957. else if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2958. flags |= root->fs_info->avail_metadata_alloc_bits;
  2959. return btrfs_reduce_alloc_profile(root, flags);
  2960. }
  2961. u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
  2962. {
  2963. u64 flags;
  2964. if (data)
  2965. flags = BTRFS_BLOCK_GROUP_DATA;
  2966. else if (root == root->fs_info->chunk_root)
  2967. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  2968. else
  2969. flags = BTRFS_BLOCK_GROUP_METADATA;
  2970. return get_alloc_profile(root, flags);
  2971. }
  2972. /*
  2973. * This will check the space that the inode allocates from to make sure we have
  2974. * enough space for bytes.
  2975. */
  2976. int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
  2977. {
  2978. struct btrfs_space_info *data_sinfo;
  2979. struct btrfs_root *root = BTRFS_I(inode)->root;
  2980. struct btrfs_fs_info *fs_info = root->fs_info;
  2981. u64 used;
  2982. int ret = 0, committed = 0, alloc_chunk = 1;
  2983. /* make sure bytes are sectorsize aligned */
  2984. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2985. if (root == root->fs_info->tree_root ||
  2986. BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
  2987. alloc_chunk = 0;
  2988. committed = 1;
  2989. }
  2990. data_sinfo = fs_info->data_sinfo;
  2991. if (!data_sinfo)
  2992. goto alloc;
  2993. again:
  2994. /* make sure we have enough space to handle the data first */
  2995. spin_lock(&data_sinfo->lock);
  2996. used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
  2997. data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
  2998. data_sinfo->bytes_may_use;
  2999. if (used + bytes > data_sinfo->total_bytes) {
  3000. struct btrfs_trans_handle *trans;
  3001. /*
  3002. * if we don't have enough free bytes in this space then we need
  3003. * to alloc a new chunk.
  3004. */
  3005. if (!data_sinfo->full && alloc_chunk) {
  3006. u64 alloc_target;
  3007. data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
  3008. spin_unlock(&data_sinfo->lock);
  3009. alloc:
  3010. alloc_target = btrfs_get_alloc_profile(root, 1);
  3011. trans = btrfs_join_transaction(root);
  3012. if (IS_ERR(trans))
  3013. return PTR_ERR(trans);
  3014. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3015. alloc_target,
  3016. CHUNK_ALLOC_NO_FORCE);
  3017. btrfs_end_transaction(trans, root);
  3018. if (ret < 0) {
  3019. if (ret != -ENOSPC)
  3020. return ret;
  3021. else
  3022. goto commit_trans;
  3023. }
  3024. if (!data_sinfo)
  3025. data_sinfo = fs_info->data_sinfo;
  3026. goto again;
  3027. }
  3028. /*
  3029. * If we have less pinned bytes than we want to allocate then
  3030. * don't bother committing the transaction, it won't help us.
  3031. */
  3032. if (data_sinfo->bytes_pinned < bytes)
  3033. committed = 1;
  3034. spin_unlock(&data_sinfo->lock);
  3035. /* commit the current transaction and try again */
  3036. commit_trans:
  3037. if (!committed &&
  3038. !atomic_read(&root->fs_info->open_ioctl_trans)) {
  3039. committed = 1;
  3040. trans = btrfs_join_transaction(root);
  3041. if (IS_ERR(trans))
  3042. return PTR_ERR(trans);
  3043. ret = btrfs_commit_transaction(trans, root);
  3044. if (ret)
  3045. return ret;
  3046. goto again;
  3047. }
  3048. return -ENOSPC;
  3049. }
  3050. data_sinfo->bytes_may_use += bytes;
  3051. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3052. data_sinfo->flags, bytes, 1);
  3053. spin_unlock(&data_sinfo->lock);
  3054. return 0;
  3055. }
  3056. /*
  3057. * Called if we need to clear a data reservation for this inode.
  3058. */
  3059. void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
  3060. {
  3061. struct btrfs_root *root = BTRFS_I(inode)->root;
  3062. struct btrfs_space_info *data_sinfo;
  3063. /* make sure bytes are sectorsize aligned */
  3064. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  3065. data_sinfo = root->fs_info->data_sinfo;
  3066. spin_lock(&data_sinfo->lock);
  3067. data_sinfo->bytes_may_use -= bytes;
  3068. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3069. data_sinfo->flags, bytes, 0);
  3070. spin_unlock(&data_sinfo->lock);
  3071. }
  3072. static void force_metadata_allocation(struct btrfs_fs_info *info)
  3073. {
  3074. struct list_head *head = &info->space_info;
  3075. struct btrfs_space_info *found;
  3076. rcu_read_lock();
  3077. list_for_each_entry_rcu(found, head, list) {
  3078. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  3079. found->force_alloc = CHUNK_ALLOC_FORCE;
  3080. }
  3081. rcu_read_unlock();
  3082. }
  3083. static int should_alloc_chunk(struct btrfs_root *root,
  3084. struct btrfs_space_info *sinfo, int force)
  3085. {
  3086. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3087. u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
  3088. u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
  3089. u64 thresh;
  3090. if (force == CHUNK_ALLOC_FORCE)
  3091. return 1;
  3092. /*
  3093. * We need to take into account the global rsv because for all intents
  3094. * and purposes it's used space. Don't worry about locking the
  3095. * global_rsv, it doesn't change except when the transaction commits.
  3096. */
  3097. if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
  3098. num_allocated += global_rsv->size;
  3099. /*
  3100. * in limited mode, we want to have some free space up to
  3101. * about 1% of the FS size.
  3102. */
  3103. if (force == CHUNK_ALLOC_LIMITED) {
  3104. thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
  3105. thresh = max_t(u64, 64 * 1024 * 1024,
  3106. div_factor_fine(thresh, 1));
  3107. if (num_bytes - num_allocated < thresh)
  3108. return 1;
  3109. }
  3110. if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
  3111. return 0;
  3112. return 1;
  3113. }
  3114. static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
  3115. {
  3116. u64 num_dev;
  3117. if (type & BTRFS_BLOCK_GROUP_RAID10 ||
  3118. type & BTRFS_BLOCK_GROUP_RAID0)
  3119. num_dev = root->fs_info->fs_devices->rw_devices;
  3120. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  3121. num_dev = 2;
  3122. else
  3123. num_dev = 1; /* DUP or single */
  3124. /* metadata for updaing devices and chunk tree */
  3125. return btrfs_calc_trans_metadata_size(root, num_dev + 1);
  3126. }
  3127. static void check_system_chunk(struct btrfs_trans_handle *trans,
  3128. struct btrfs_root *root, u64 type)
  3129. {
  3130. struct btrfs_space_info *info;
  3131. u64 left;
  3132. u64 thresh;
  3133. info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3134. spin_lock(&info->lock);
  3135. left = info->total_bytes - info->bytes_used - info->bytes_pinned -
  3136. info->bytes_reserved - info->bytes_readonly;
  3137. spin_unlock(&info->lock);
  3138. thresh = get_system_chunk_thresh(root, type);
  3139. if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  3140. printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
  3141. left, thresh, type);
  3142. dump_space_info(info, 0, 0);
  3143. }
  3144. if (left < thresh) {
  3145. u64 flags;
  3146. flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
  3147. btrfs_alloc_chunk(trans, root, flags);
  3148. }
  3149. }
  3150. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  3151. struct btrfs_root *extent_root, u64 flags, int force)
  3152. {
  3153. struct btrfs_space_info *space_info;
  3154. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3155. int wait_for_alloc = 0;
  3156. int ret = 0;
  3157. space_info = __find_space_info(extent_root->fs_info, flags);
  3158. if (!space_info) {
  3159. ret = update_space_info(extent_root->fs_info, flags,
  3160. 0, 0, &space_info);
  3161. BUG_ON(ret); /* -ENOMEM */
  3162. }
  3163. BUG_ON(!space_info); /* Logic error */
  3164. again:
  3165. spin_lock(&space_info->lock);
  3166. if (force < space_info->force_alloc)
  3167. force = space_info->force_alloc;
  3168. if (space_info->full) {
  3169. spin_unlock(&space_info->lock);
  3170. return 0;
  3171. }
  3172. if (!should_alloc_chunk(extent_root, space_info, force)) {
  3173. spin_unlock(&space_info->lock);
  3174. return 0;
  3175. } else if (space_info->chunk_alloc) {
  3176. wait_for_alloc = 1;
  3177. } else {
  3178. space_info->chunk_alloc = 1;
  3179. }
  3180. spin_unlock(&space_info->lock);
  3181. mutex_lock(&fs_info->chunk_mutex);
  3182. /*
  3183. * The chunk_mutex is held throughout the entirety of a chunk
  3184. * allocation, so once we've acquired the chunk_mutex we know that the
  3185. * other guy is done and we need to recheck and see if we should
  3186. * allocate.
  3187. */
  3188. if (wait_for_alloc) {
  3189. mutex_unlock(&fs_info->chunk_mutex);
  3190. wait_for_alloc = 0;
  3191. goto again;
  3192. }
  3193. /*
  3194. * If we have mixed data/metadata chunks we want to make sure we keep
  3195. * allocating mixed chunks instead of individual chunks.
  3196. */
  3197. if (btrfs_mixed_space_info(space_info))
  3198. flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
  3199. /*
  3200. * if we're doing a data chunk, go ahead and make sure that
  3201. * we keep a reasonable number of metadata chunks allocated in the
  3202. * FS as well.
  3203. */
  3204. if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
  3205. fs_info->data_chunk_allocations++;
  3206. if (!(fs_info->data_chunk_allocations %
  3207. fs_info->metadata_ratio))
  3208. force_metadata_allocation(fs_info);
  3209. }
  3210. /*
  3211. * Check if we have enough space in SYSTEM chunk because we may need
  3212. * to update devices.
  3213. */
  3214. check_system_chunk(trans, extent_root, flags);
  3215. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  3216. if (ret < 0 && ret != -ENOSPC)
  3217. goto out;
  3218. spin_lock(&space_info->lock);
  3219. if (ret)
  3220. space_info->full = 1;
  3221. else
  3222. ret = 1;
  3223. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  3224. space_info->chunk_alloc = 0;
  3225. spin_unlock(&space_info->lock);
  3226. out:
  3227. mutex_unlock(&fs_info->chunk_mutex);
  3228. return ret;
  3229. }
  3230. static int can_overcommit(struct btrfs_root *root,
  3231. struct btrfs_space_info *space_info, u64 bytes,
  3232. enum btrfs_reserve_flush_enum flush)
  3233. {
  3234. u64 profile = btrfs_get_alloc_profile(root, 0);
  3235. u64 avail;
  3236. u64 used;
  3237. used = space_info->bytes_used + space_info->bytes_reserved +
  3238. space_info->bytes_pinned + space_info->bytes_readonly +
  3239. space_info->bytes_may_use;
  3240. spin_lock(&root->fs_info->free_chunk_lock);
  3241. avail = root->fs_info->free_chunk_space;
  3242. spin_unlock(&root->fs_info->free_chunk_lock);
  3243. /*
  3244. * If we have dup, raid1 or raid10 then only half of the free
  3245. * space is actually useable.
  3246. */
  3247. if (profile & (BTRFS_BLOCK_GROUP_DUP |
  3248. BTRFS_BLOCK_GROUP_RAID1 |
  3249. BTRFS_BLOCK_GROUP_RAID10))
  3250. avail >>= 1;
  3251. /*
  3252. * If we aren't flushing all things, let us overcommit up to
  3253. * 1/2th of the space. If we can flush, don't let us overcommit
  3254. * too much, let it overcommit up to 1/8 of the space.
  3255. */
  3256. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  3257. avail >>= 3;
  3258. else
  3259. avail >>= 1;
  3260. if (used + bytes < space_info->total_bytes + avail)
  3261. return 1;
  3262. return 0;
  3263. }
  3264. static int writeback_inodes_sb_nr_if_idle_safe(struct super_block *sb,
  3265. unsigned long nr_pages,
  3266. enum wb_reason reason)
  3267. {
  3268. if (!writeback_in_progress(sb->s_bdi) &&
  3269. down_read_trylock(&sb->s_umount)) {
  3270. writeback_inodes_sb_nr(sb, nr_pages, reason);
  3271. up_read(&sb->s_umount);
  3272. return 1;
  3273. }
  3274. return 0;
  3275. }
  3276. /*
  3277. * shrink metadata reservation for delalloc
  3278. */
  3279. static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
  3280. bool wait_ordered)
  3281. {
  3282. struct btrfs_block_rsv *block_rsv;
  3283. struct btrfs_space_info *space_info;
  3284. struct btrfs_trans_handle *trans;
  3285. u64 delalloc_bytes;
  3286. u64 max_reclaim;
  3287. long time_left;
  3288. unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
  3289. int loops = 0;
  3290. enum btrfs_reserve_flush_enum flush;
  3291. trans = (struct btrfs_trans_handle *)current->journal_info;
  3292. block_rsv = &root->fs_info->delalloc_block_rsv;
  3293. space_info = block_rsv->space_info;
  3294. smp_mb();
  3295. delalloc_bytes = root->fs_info->delalloc_bytes;
  3296. if (delalloc_bytes == 0) {
  3297. if (trans)
  3298. return;
  3299. btrfs_wait_ordered_extents(root, 0);
  3300. return;
  3301. }
  3302. while (delalloc_bytes && loops < 3) {
  3303. max_reclaim = min(delalloc_bytes, to_reclaim);
  3304. nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
  3305. writeback_inodes_sb_nr_if_idle_safe(root->fs_info->sb,
  3306. nr_pages,
  3307. WB_REASON_FS_FREE_SPACE);
  3308. /*
  3309. * We need to wait for the async pages to actually start before
  3310. * we do anything.
  3311. */
  3312. wait_event(root->fs_info->async_submit_wait,
  3313. !atomic_read(&root->fs_info->async_delalloc_pages));
  3314. if (!trans)
  3315. flush = BTRFS_RESERVE_FLUSH_ALL;
  3316. else
  3317. flush = BTRFS_RESERVE_NO_FLUSH;
  3318. spin_lock(&space_info->lock);
  3319. if (can_overcommit(root, space_info, orig, flush)) {
  3320. spin_unlock(&space_info->lock);
  3321. break;
  3322. }
  3323. spin_unlock(&space_info->lock);
  3324. loops++;
  3325. if (wait_ordered && !trans) {
  3326. btrfs_wait_ordered_extents(root, 0);
  3327. } else {
  3328. time_left = schedule_timeout_killable(1);
  3329. if (time_left)
  3330. break;
  3331. }
  3332. smp_mb();
  3333. delalloc_bytes = root->fs_info->delalloc_bytes;
  3334. }
  3335. }
  3336. /**
  3337. * maybe_commit_transaction - possibly commit the transaction if its ok to
  3338. * @root - the root we're allocating for
  3339. * @bytes - the number of bytes we want to reserve
  3340. * @force - force the commit
  3341. *
  3342. * This will check to make sure that committing the transaction will actually
  3343. * get us somewhere and then commit the transaction if it does. Otherwise it
  3344. * will return -ENOSPC.
  3345. */
  3346. static int may_commit_transaction(struct btrfs_root *root,
  3347. struct btrfs_space_info *space_info,
  3348. u64 bytes, int force)
  3349. {
  3350. struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
  3351. struct btrfs_trans_handle *trans;
  3352. trans = (struct btrfs_trans_handle *)current->journal_info;
  3353. if (trans)
  3354. return -EAGAIN;
  3355. if (force)
  3356. goto commit;
  3357. /* See if there is enough pinned space to make this reservation */
  3358. spin_lock(&space_info->lock);
  3359. if (space_info->bytes_pinned >= bytes) {
  3360. spin_unlock(&space_info->lock);
  3361. goto commit;
  3362. }
  3363. spin_unlock(&space_info->lock);
  3364. /*
  3365. * See if there is some space in the delayed insertion reservation for
  3366. * this reservation.
  3367. */
  3368. if (space_info != delayed_rsv->space_info)
  3369. return -ENOSPC;
  3370. spin_lock(&space_info->lock);
  3371. spin_lock(&delayed_rsv->lock);
  3372. if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
  3373. spin_unlock(&delayed_rsv->lock);
  3374. spin_unlock(&space_info->lock);
  3375. return -ENOSPC;
  3376. }
  3377. spin_unlock(&delayed_rsv->lock);
  3378. spin_unlock(&space_info->lock);
  3379. commit:
  3380. trans = btrfs_join_transaction(root);
  3381. if (IS_ERR(trans))
  3382. return -ENOSPC;
  3383. return btrfs_commit_transaction(trans, root);
  3384. }
  3385. enum flush_state {
  3386. FLUSH_DELAYED_ITEMS_NR = 1,
  3387. FLUSH_DELAYED_ITEMS = 2,
  3388. FLUSH_DELALLOC = 3,
  3389. FLUSH_DELALLOC_WAIT = 4,
  3390. ALLOC_CHUNK = 5,
  3391. COMMIT_TRANS = 6,
  3392. };
  3393. static int flush_space(struct btrfs_root *root,
  3394. struct btrfs_space_info *space_info, u64 num_bytes,
  3395. u64 orig_bytes, int state)
  3396. {
  3397. struct btrfs_trans_handle *trans;
  3398. int nr;
  3399. int ret = 0;
  3400. switch (state) {
  3401. case FLUSH_DELAYED_ITEMS_NR:
  3402. case FLUSH_DELAYED_ITEMS:
  3403. if (state == FLUSH_DELAYED_ITEMS_NR) {
  3404. u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
  3405. nr = (int)div64_u64(num_bytes, bytes);
  3406. if (!nr)
  3407. nr = 1;
  3408. nr *= 2;
  3409. } else {
  3410. nr = -1;
  3411. }
  3412. trans = btrfs_join_transaction(root);
  3413. if (IS_ERR(trans)) {
  3414. ret = PTR_ERR(trans);
  3415. break;
  3416. }
  3417. ret = btrfs_run_delayed_items_nr(trans, root, nr);
  3418. btrfs_end_transaction(trans, root);
  3419. break;
  3420. case FLUSH_DELALLOC:
  3421. case FLUSH_DELALLOC_WAIT:
  3422. shrink_delalloc(root, num_bytes, orig_bytes,
  3423. state == FLUSH_DELALLOC_WAIT);
  3424. break;
  3425. case ALLOC_CHUNK:
  3426. trans = btrfs_join_transaction(root);
  3427. if (IS_ERR(trans)) {
  3428. ret = PTR_ERR(trans);
  3429. break;
  3430. }
  3431. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3432. btrfs_get_alloc_profile(root, 0),
  3433. CHUNK_ALLOC_NO_FORCE);
  3434. btrfs_end_transaction(trans, root);
  3435. if (ret == -ENOSPC)
  3436. ret = 0;
  3437. break;
  3438. case COMMIT_TRANS:
  3439. ret = may_commit_transaction(root, space_info, orig_bytes, 0);
  3440. break;
  3441. default:
  3442. ret = -ENOSPC;
  3443. break;
  3444. }
  3445. return ret;
  3446. }
  3447. /**
  3448. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  3449. * @root - the root we're allocating for
  3450. * @block_rsv - the block_rsv we're allocating for
  3451. * @orig_bytes - the number of bytes we want
  3452. * @flush - whether or not we can flush to make our reservation
  3453. *
  3454. * This will reserve orgi_bytes number of bytes from the space info associated
  3455. * with the block_rsv. If there is not enough space it will make an attempt to
  3456. * flush out space to make room. It will do this by flushing delalloc if
  3457. * possible or committing the transaction. If flush is 0 then no attempts to
  3458. * regain reservations will be made and this will fail if there is not enough
  3459. * space already.
  3460. */
  3461. static int reserve_metadata_bytes(struct btrfs_root *root,
  3462. struct btrfs_block_rsv *block_rsv,
  3463. u64 orig_bytes,
  3464. enum btrfs_reserve_flush_enum flush)
  3465. {
  3466. struct btrfs_space_info *space_info = block_rsv->space_info;
  3467. u64 used;
  3468. u64 num_bytes = orig_bytes;
  3469. int flush_state = FLUSH_DELAYED_ITEMS_NR;
  3470. int ret = 0;
  3471. bool flushing = false;
  3472. again:
  3473. ret = 0;
  3474. spin_lock(&space_info->lock);
  3475. /*
  3476. * We only want to wait if somebody other than us is flushing and we
  3477. * are actually allowed to flush all things.
  3478. */
  3479. while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
  3480. space_info->flush) {
  3481. spin_unlock(&space_info->lock);
  3482. /*
  3483. * If we have a trans handle we can't wait because the flusher
  3484. * may have to commit the transaction, which would mean we would
  3485. * deadlock since we are waiting for the flusher to finish, but
  3486. * hold the current transaction open.
  3487. */
  3488. if (current->journal_info)
  3489. return -EAGAIN;
  3490. ret = wait_event_killable(space_info->wait, !space_info->flush);
  3491. /* Must have been killed, return */
  3492. if (ret)
  3493. return -EINTR;
  3494. spin_lock(&space_info->lock);
  3495. }
  3496. ret = -ENOSPC;
  3497. used = space_info->bytes_used + space_info->bytes_reserved +
  3498. space_info->bytes_pinned + space_info->bytes_readonly +
  3499. space_info->bytes_may_use;
  3500. /*
  3501. * The idea here is that we've not already over-reserved the block group
  3502. * then we can go ahead and save our reservation first and then start
  3503. * flushing if we need to. Otherwise if we've already overcommitted
  3504. * lets start flushing stuff first and then come back and try to make
  3505. * our reservation.
  3506. */
  3507. if (used <= space_info->total_bytes) {
  3508. if (used + orig_bytes <= space_info->total_bytes) {
  3509. space_info->bytes_may_use += orig_bytes;
  3510. trace_btrfs_space_reservation(root->fs_info,
  3511. "space_info", space_info->flags, orig_bytes, 1);
  3512. ret = 0;
  3513. } else {
  3514. /*
  3515. * Ok set num_bytes to orig_bytes since we aren't
  3516. * overocmmitted, this way we only try and reclaim what
  3517. * we need.
  3518. */
  3519. num_bytes = orig_bytes;
  3520. }
  3521. } else {
  3522. /*
  3523. * Ok we're over committed, set num_bytes to the overcommitted
  3524. * amount plus the amount of bytes that we need for this
  3525. * reservation.
  3526. */
  3527. num_bytes = used - space_info->total_bytes +
  3528. (orig_bytes * 2);
  3529. }
  3530. if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
  3531. space_info->bytes_may_use += orig_bytes;
  3532. trace_btrfs_space_reservation(root->fs_info, "space_info",
  3533. space_info->flags, orig_bytes,
  3534. 1);
  3535. ret = 0;
  3536. }
  3537. /*
  3538. * Couldn't make our reservation, save our place so while we're trying
  3539. * to reclaim space we can actually use it instead of somebody else
  3540. * stealing it from us.
  3541. *
  3542. * We make the other tasks wait for the flush only when we can flush
  3543. * all things.
  3544. */
  3545. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  3546. flushing = true;
  3547. space_info->flush = 1;
  3548. }
  3549. spin_unlock(&space_info->lock);
  3550. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  3551. goto out;
  3552. ret = flush_space(root, space_info, num_bytes, orig_bytes,
  3553. flush_state);
  3554. flush_state++;
  3555. /*
  3556. * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
  3557. * would happen. So skip delalloc flush.
  3558. */
  3559. if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3560. (flush_state == FLUSH_DELALLOC ||
  3561. flush_state == FLUSH_DELALLOC_WAIT))
  3562. flush_state = ALLOC_CHUNK;
  3563. if (!ret)
  3564. goto again;
  3565. else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
  3566. flush_state < COMMIT_TRANS)
  3567. goto again;
  3568. else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
  3569. flush_state <= COMMIT_TRANS)
  3570. goto again;
  3571. out:
  3572. if (flushing) {
  3573. spin_lock(&space_info->lock);
  3574. space_info->flush = 0;
  3575. wake_up_all(&space_info->wait);
  3576. spin_unlock(&space_info->lock);
  3577. }
  3578. return ret;
  3579. }
  3580. static struct btrfs_block_rsv *get_block_rsv(
  3581. const struct btrfs_trans_handle *trans,
  3582. const struct btrfs_root *root)
  3583. {
  3584. struct btrfs_block_rsv *block_rsv = NULL;
  3585. if (root->ref_cows)
  3586. block_rsv = trans->block_rsv;
  3587. if (root == root->fs_info->csum_root && trans->adding_csums)
  3588. block_rsv = trans->block_rsv;
  3589. if (!block_rsv)
  3590. block_rsv = root->block_rsv;
  3591. if (!block_rsv)
  3592. block_rsv = &root->fs_info->empty_block_rsv;
  3593. return block_rsv;
  3594. }
  3595. static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
  3596. u64 num_bytes)
  3597. {
  3598. int ret = -ENOSPC;
  3599. spin_lock(&block_rsv->lock);
  3600. if (block_rsv->reserved >= num_bytes) {
  3601. block_rsv->reserved -= num_bytes;
  3602. if (block_rsv->reserved < block_rsv->size)
  3603. block_rsv->full = 0;
  3604. ret = 0;
  3605. }
  3606. spin_unlock(&block_rsv->lock);
  3607. return ret;
  3608. }
  3609. static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
  3610. u64 num_bytes, int update_size)
  3611. {
  3612. spin_lock(&block_rsv->lock);
  3613. block_rsv->reserved += num_bytes;
  3614. if (update_size)
  3615. block_rsv->size += num_bytes;
  3616. else if (block_rsv->reserved >= block_rsv->size)
  3617. block_rsv->full = 1;
  3618. spin_unlock(&block_rsv->lock);
  3619. }
  3620. static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
  3621. struct btrfs_block_rsv *block_rsv,
  3622. struct btrfs_block_rsv *dest, u64 num_bytes)
  3623. {
  3624. struct btrfs_space_info *space_info = block_rsv->space_info;
  3625. spin_lock(&block_rsv->lock);
  3626. if (num_bytes == (u64)-1)
  3627. num_bytes = block_rsv->size;
  3628. block_rsv->size -= num_bytes;
  3629. if (block_rsv->reserved >= block_rsv->size) {
  3630. num_bytes = block_rsv->reserved - block_rsv->size;
  3631. block_rsv->reserved = block_rsv->size;
  3632. block_rsv->full = 1;
  3633. } else {
  3634. num_bytes = 0;
  3635. }
  3636. spin_unlock(&block_rsv->lock);
  3637. if (num_bytes > 0) {
  3638. if (dest) {
  3639. spin_lock(&dest->lock);
  3640. if (!dest->full) {
  3641. u64 bytes_to_add;
  3642. bytes_to_add = dest->size - dest->reserved;
  3643. bytes_to_add = min(num_bytes, bytes_to_add);
  3644. dest->reserved += bytes_to_add;
  3645. if (dest->reserved >= dest->size)
  3646. dest->full = 1;
  3647. num_bytes -= bytes_to_add;
  3648. }
  3649. spin_unlock(&dest->lock);
  3650. }
  3651. if (num_bytes) {
  3652. spin_lock(&space_info->lock);
  3653. space_info->bytes_may_use -= num_bytes;
  3654. trace_btrfs_space_reservation(fs_info, "space_info",
  3655. space_info->flags, num_bytes, 0);
  3656. space_info->reservation_progress++;
  3657. spin_unlock(&space_info->lock);
  3658. }
  3659. }
  3660. }
  3661. static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
  3662. struct btrfs_block_rsv *dst, u64 num_bytes)
  3663. {
  3664. int ret;
  3665. ret = block_rsv_use_bytes(src, num_bytes);
  3666. if (ret)
  3667. return ret;
  3668. block_rsv_add_bytes(dst, num_bytes, 1);
  3669. return 0;
  3670. }
  3671. void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
  3672. {
  3673. memset(rsv, 0, sizeof(*rsv));
  3674. spin_lock_init(&rsv->lock);
  3675. rsv->type = type;
  3676. }
  3677. struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
  3678. unsigned short type)
  3679. {
  3680. struct btrfs_block_rsv *block_rsv;
  3681. struct btrfs_fs_info *fs_info = root->fs_info;
  3682. block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
  3683. if (!block_rsv)
  3684. return NULL;
  3685. btrfs_init_block_rsv(block_rsv, type);
  3686. block_rsv->space_info = __find_space_info(fs_info,
  3687. BTRFS_BLOCK_GROUP_METADATA);
  3688. return block_rsv;
  3689. }
  3690. void btrfs_free_block_rsv(struct btrfs_root *root,
  3691. struct btrfs_block_rsv *rsv)
  3692. {
  3693. if (!rsv)
  3694. return;
  3695. btrfs_block_rsv_release(root, rsv, (u64)-1);
  3696. kfree(rsv);
  3697. }
  3698. int btrfs_block_rsv_add(struct btrfs_root *root,
  3699. struct btrfs_block_rsv *block_rsv, u64 num_bytes,
  3700. enum btrfs_reserve_flush_enum flush)
  3701. {
  3702. int ret;
  3703. if (num_bytes == 0)
  3704. return 0;
  3705. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3706. if (!ret) {
  3707. block_rsv_add_bytes(block_rsv, num_bytes, 1);
  3708. return 0;
  3709. }
  3710. return ret;
  3711. }
  3712. int btrfs_block_rsv_check(struct btrfs_root *root,
  3713. struct btrfs_block_rsv *block_rsv, int min_factor)
  3714. {
  3715. u64 num_bytes = 0;
  3716. int ret = -ENOSPC;
  3717. if (!block_rsv)
  3718. return 0;
  3719. spin_lock(&block_rsv->lock);
  3720. num_bytes = div_factor(block_rsv->size, min_factor);
  3721. if (block_rsv->reserved >= num_bytes)
  3722. ret = 0;
  3723. spin_unlock(&block_rsv->lock);
  3724. return ret;
  3725. }
  3726. int btrfs_block_rsv_refill(struct btrfs_root *root,
  3727. struct btrfs_block_rsv *block_rsv, u64 min_reserved,
  3728. enum btrfs_reserve_flush_enum flush)
  3729. {
  3730. u64 num_bytes = 0;
  3731. int ret = -ENOSPC;
  3732. if (!block_rsv)
  3733. return 0;
  3734. spin_lock(&block_rsv->lock);
  3735. num_bytes = min_reserved;
  3736. if (block_rsv->reserved >= num_bytes)
  3737. ret = 0;
  3738. else
  3739. num_bytes -= block_rsv->reserved;
  3740. spin_unlock(&block_rsv->lock);
  3741. if (!ret)
  3742. return 0;
  3743. ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
  3744. if (!ret) {
  3745. block_rsv_add_bytes(block_rsv, num_bytes, 0);
  3746. return 0;
  3747. }
  3748. return ret;
  3749. }
  3750. int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
  3751. struct btrfs_block_rsv *dst_rsv,
  3752. u64 num_bytes)
  3753. {
  3754. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3755. }
  3756. void btrfs_block_rsv_release(struct btrfs_root *root,
  3757. struct btrfs_block_rsv *block_rsv,
  3758. u64 num_bytes)
  3759. {
  3760. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  3761. if (global_rsv->full || global_rsv == block_rsv ||
  3762. block_rsv->space_info != global_rsv->space_info)
  3763. global_rsv = NULL;
  3764. block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
  3765. num_bytes);
  3766. }
  3767. /*
  3768. * helper to calculate size of global block reservation.
  3769. * the desired value is sum of space used by extent tree,
  3770. * checksum tree and root tree
  3771. */
  3772. static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
  3773. {
  3774. struct btrfs_space_info *sinfo;
  3775. u64 num_bytes;
  3776. u64 meta_used;
  3777. u64 data_used;
  3778. int csum_size = btrfs_super_csum_size(fs_info->super_copy);
  3779. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
  3780. spin_lock(&sinfo->lock);
  3781. data_used = sinfo->bytes_used;
  3782. spin_unlock(&sinfo->lock);
  3783. sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  3784. spin_lock(&sinfo->lock);
  3785. if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
  3786. data_used = 0;
  3787. meta_used = sinfo->bytes_used;
  3788. spin_unlock(&sinfo->lock);
  3789. num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
  3790. csum_size * 2;
  3791. num_bytes += div64_u64(data_used + meta_used, 50);
  3792. if (num_bytes * 3 > meta_used)
  3793. num_bytes = div64_u64(meta_used, 3);
  3794. return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
  3795. }
  3796. static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
  3797. {
  3798. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  3799. struct btrfs_space_info *sinfo = block_rsv->space_info;
  3800. u64 num_bytes;
  3801. num_bytes = calc_global_metadata_size(fs_info);
  3802. spin_lock(&sinfo->lock);
  3803. spin_lock(&block_rsv->lock);
  3804. block_rsv->size = num_bytes;
  3805. num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
  3806. sinfo->bytes_reserved + sinfo->bytes_readonly +
  3807. sinfo->bytes_may_use;
  3808. if (sinfo->total_bytes > num_bytes) {
  3809. num_bytes = sinfo->total_bytes - num_bytes;
  3810. block_rsv->reserved += num_bytes;
  3811. sinfo->bytes_may_use += num_bytes;
  3812. trace_btrfs_space_reservation(fs_info, "space_info",
  3813. sinfo->flags, num_bytes, 1);
  3814. }
  3815. if (block_rsv->reserved >= block_rsv->size) {
  3816. num_bytes = block_rsv->reserved - block_rsv->size;
  3817. sinfo->bytes_may_use -= num_bytes;
  3818. trace_btrfs_space_reservation(fs_info, "space_info",
  3819. sinfo->flags, num_bytes, 0);
  3820. sinfo->reservation_progress++;
  3821. block_rsv->reserved = block_rsv->size;
  3822. block_rsv->full = 1;
  3823. }
  3824. spin_unlock(&block_rsv->lock);
  3825. spin_unlock(&sinfo->lock);
  3826. }
  3827. static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
  3828. {
  3829. struct btrfs_space_info *space_info;
  3830. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  3831. fs_info->chunk_block_rsv.space_info = space_info;
  3832. space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  3833. fs_info->global_block_rsv.space_info = space_info;
  3834. fs_info->delalloc_block_rsv.space_info = space_info;
  3835. fs_info->trans_block_rsv.space_info = space_info;
  3836. fs_info->empty_block_rsv.space_info = space_info;
  3837. fs_info->delayed_block_rsv.space_info = space_info;
  3838. fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
  3839. fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
  3840. fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
  3841. fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
  3842. fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
  3843. update_global_block_rsv(fs_info);
  3844. }
  3845. static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
  3846. {
  3847. block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
  3848. (u64)-1);
  3849. WARN_ON(fs_info->delalloc_block_rsv.size > 0);
  3850. WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
  3851. WARN_ON(fs_info->trans_block_rsv.size > 0);
  3852. WARN_ON(fs_info->trans_block_rsv.reserved > 0);
  3853. WARN_ON(fs_info->chunk_block_rsv.size > 0);
  3854. WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
  3855. WARN_ON(fs_info->delayed_block_rsv.size > 0);
  3856. WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
  3857. }
  3858. void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
  3859. struct btrfs_root *root)
  3860. {
  3861. if (!trans->block_rsv)
  3862. return;
  3863. if (!trans->bytes_reserved)
  3864. return;
  3865. trace_btrfs_space_reservation(root->fs_info, "transaction",
  3866. trans->transid, trans->bytes_reserved, 0);
  3867. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  3868. trans->bytes_reserved = 0;
  3869. }
  3870. /* Can only return 0 or -ENOSPC */
  3871. int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
  3872. struct inode *inode)
  3873. {
  3874. struct btrfs_root *root = BTRFS_I(inode)->root;
  3875. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  3876. struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
  3877. /*
  3878. * We need to hold space in order to delete our orphan item once we've
  3879. * added it, so this takes the reservation so we can release it later
  3880. * when we are truly done with the orphan item.
  3881. */
  3882. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  3883. trace_btrfs_space_reservation(root->fs_info, "orphan",
  3884. btrfs_ino(inode), num_bytes, 1);
  3885. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3886. }
  3887. void btrfs_orphan_release_metadata(struct inode *inode)
  3888. {
  3889. struct btrfs_root *root = BTRFS_I(inode)->root;
  3890. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  3891. trace_btrfs_space_reservation(root->fs_info, "orphan",
  3892. btrfs_ino(inode), num_bytes, 0);
  3893. btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
  3894. }
  3895. int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
  3896. struct btrfs_pending_snapshot *pending)
  3897. {
  3898. struct btrfs_root *root = pending->root;
  3899. struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
  3900. struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
  3901. /*
  3902. * two for root back/forward refs, two for directory entries,
  3903. * one for root of the snapshot and one for parent inode.
  3904. */
  3905. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 6);
  3906. dst_rsv->space_info = src_rsv->space_info;
  3907. return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
  3908. }
  3909. /**
  3910. * drop_outstanding_extent - drop an outstanding extent
  3911. * @inode: the inode we're dropping the extent for
  3912. *
  3913. * This is called when we are freeing up an outstanding extent, either called
  3914. * after an error or after an extent is written. This will return the number of
  3915. * reserved extents that need to be freed. This must be called with
  3916. * BTRFS_I(inode)->lock held.
  3917. */
  3918. static unsigned drop_outstanding_extent(struct inode *inode)
  3919. {
  3920. unsigned drop_inode_space = 0;
  3921. unsigned dropped_extents = 0;
  3922. BUG_ON(!BTRFS_I(inode)->outstanding_extents);
  3923. BTRFS_I(inode)->outstanding_extents--;
  3924. if (BTRFS_I(inode)->outstanding_extents == 0 &&
  3925. test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  3926. &BTRFS_I(inode)->runtime_flags))
  3927. drop_inode_space = 1;
  3928. /*
  3929. * If we have more or the same amount of outsanding extents than we have
  3930. * reserved then we need to leave the reserved extents count alone.
  3931. */
  3932. if (BTRFS_I(inode)->outstanding_extents >=
  3933. BTRFS_I(inode)->reserved_extents)
  3934. return drop_inode_space;
  3935. dropped_extents = BTRFS_I(inode)->reserved_extents -
  3936. BTRFS_I(inode)->outstanding_extents;
  3937. BTRFS_I(inode)->reserved_extents -= dropped_extents;
  3938. return dropped_extents + drop_inode_space;
  3939. }
  3940. /**
  3941. * calc_csum_metadata_size - return the amount of metada space that must be
  3942. * reserved/free'd for the given bytes.
  3943. * @inode: the inode we're manipulating
  3944. * @num_bytes: the number of bytes in question
  3945. * @reserve: 1 if we are reserving space, 0 if we are freeing space
  3946. *
  3947. * This adjusts the number of csum_bytes in the inode and then returns the
  3948. * correct amount of metadata that must either be reserved or freed. We
  3949. * calculate how many checksums we can fit into one leaf and then divide the
  3950. * number of bytes that will need to be checksumed by this value to figure out
  3951. * how many checksums will be required. If we are adding bytes then the number
  3952. * may go up and we will return the number of additional bytes that must be
  3953. * reserved. If it is going down we will return the number of bytes that must
  3954. * be freed.
  3955. *
  3956. * This must be called with BTRFS_I(inode)->lock held.
  3957. */
  3958. static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
  3959. int reserve)
  3960. {
  3961. struct btrfs_root *root = BTRFS_I(inode)->root;
  3962. u64 csum_size;
  3963. int num_csums_per_leaf;
  3964. int num_csums;
  3965. int old_csums;
  3966. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
  3967. BTRFS_I(inode)->csum_bytes == 0)
  3968. return 0;
  3969. old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  3970. if (reserve)
  3971. BTRFS_I(inode)->csum_bytes += num_bytes;
  3972. else
  3973. BTRFS_I(inode)->csum_bytes -= num_bytes;
  3974. csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
  3975. num_csums_per_leaf = (int)div64_u64(csum_size,
  3976. sizeof(struct btrfs_csum_item) +
  3977. sizeof(struct btrfs_disk_key));
  3978. num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
  3979. num_csums = num_csums + num_csums_per_leaf - 1;
  3980. num_csums = num_csums / num_csums_per_leaf;
  3981. old_csums = old_csums + num_csums_per_leaf - 1;
  3982. old_csums = old_csums / num_csums_per_leaf;
  3983. /* No change, no need to reserve more */
  3984. if (old_csums == num_csums)
  3985. return 0;
  3986. if (reserve)
  3987. return btrfs_calc_trans_metadata_size(root,
  3988. num_csums - old_csums);
  3989. return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
  3990. }
  3991. int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
  3992. {
  3993. struct btrfs_root *root = BTRFS_I(inode)->root;
  3994. struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
  3995. u64 to_reserve = 0;
  3996. u64 csum_bytes;
  3997. unsigned nr_extents = 0;
  3998. int extra_reserve = 0;
  3999. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  4000. int ret;
  4001. bool delalloc_lock = true;
  4002. /* If we are a free space inode we need to not flush since we will be in
  4003. * the middle of a transaction commit. We also don't need the delalloc
  4004. * mutex since we won't race with anybody. We need this mostly to make
  4005. * lockdep shut its filthy mouth.
  4006. */
  4007. if (btrfs_is_free_space_inode(inode)) {
  4008. flush = BTRFS_RESERVE_NO_FLUSH;
  4009. delalloc_lock = false;
  4010. }
  4011. if (flush != BTRFS_RESERVE_NO_FLUSH &&
  4012. btrfs_transaction_in_commit(root->fs_info))
  4013. schedule_timeout(1);
  4014. if (delalloc_lock)
  4015. mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
  4016. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4017. spin_lock(&BTRFS_I(inode)->lock);
  4018. BTRFS_I(inode)->outstanding_extents++;
  4019. if (BTRFS_I(inode)->outstanding_extents >
  4020. BTRFS_I(inode)->reserved_extents)
  4021. nr_extents = BTRFS_I(inode)->outstanding_extents -
  4022. BTRFS_I(inode)->reserved_extents;
  4023. /*
  4024. * Add an item to reserve for updating the inode when we complete the
  4025. * delalloc io.
  4026. */
  4027. if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4028. &BTRFS_I(inode)->runtime_flags)) {
  4029. nr_extents++;
  4030. extra_reserve = 1;
  4031. }
  4032. to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
  4033. to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
  4034. csum_bytes = BTRFS_I(inode)->csum_bytes;
  4035. spin_unlock(&BTRFS_I(inode)->lock);
  4036. if (root->fs_info->quota_enabled) {
  4037. ret = btrfs_qgroup_reserve(root, num_bytes +
  4038. nr_extents * root->leafsize);
  4039. if (ret) {
  4040. spin_lock(&BTRFS_I(inode)->lock);
  4041. calc_csum_metadata_size(inode, num_bytes, 0);
  4042. spin_unlock(&BTRFS_I(inode)->lock);
  4043. if (delalloc_lock)
  4044. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4045. return ret;
  4046. }
  4047. }
  4048. ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
  4049. if (ret) {
  4050. u64 to_free = 0;
  4051. unsigned dropped;
  4052. spin_lock(&BTRFS_I(inode)->lock);
  4053. dropped = drop_outstanding_extent(inode);
  4054. /*
  4055. * If the inodes csum_bytes is the same as the original
  4056. * csum_bytes then we know we haven't raced with any free()ers
  4057. * so we can just reduce our inodes csum bytes and carry on.
  4058. * Otherwise we have to do the normal free thing to account for
  4059. * the case that the free side didn't free up its reserve
  4060. * because of this outstanding reservation.
  4061. */
  4062. if (BTRFS_I(inode)->csum_bytes == csum_bytes)
  4063. calc_csum_metadata_size(inode, num_bytes, 0);
  4064. else
  4065. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4066. spin_unlock(&BTRFS_I(inode)->lock);
  4067. if (dropped)
  4068. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4069. if (to_free) {
  4070. btrfs_block_rsv_release(root, block_rsv, to_free);
  4071. trace_btrfs_space_reservation(root->fs_info,
  4072. "delalloc",
  4073. btrfs_ino(inode),
  4074. to_free, 0);
  4075. }
  4076. if (root->fs_info->quota_enabled) {
  4077. btrfs_qgroup_free(root, num_bytes +
  4078. nr_extents * root->leafsize);
  4079. }
  4080. if (delalloc_lock)
  4081. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4082. return ret;
  4083. }
  4084. spin_lock(&BTRFS_I(inode)->lock);
  4085. if (extra_reserve) {
  4086. set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  4087. &BTRFS_I(inode)->runtime_flags);
  4088. nr_extents--;
  4089. }
  4090. BTRFS_I(inode)->reserved_extents += nr_extents;
  4091. spin_unlock(&BTRFS_I(inode)->lock);
  4092. if (delalloc_lock)
  4093. mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
  4094. if (to_reserve)
  4095. trace_btrfs_space_reservation(root->fs_info,"delalloc",
  4096. btrfs_ino(inode), to_reserve, 1);
  4097. block_rsv_add_bytes(block_rsv, to_reserve, 1);
  4098. return 0;
  4099. }
  4100. /**
  4101. * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
  4102. * @inode: the inode to release the reservation for
  4103. * @num_bytes: the number of bytes we're releasing
  4104. *
  4105. * This will release the metadata reservation for an inode. This can be called
  4106. * once we complete IO for a given set of bytes to release their metadata
  4107. * reservations.
  4108. */
  4109. void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
  4110. {
  4111. struct btrfs_root *root = BTRFS_I(inode)->root;
  4112. u64 to_free = 0;
  4113. unsigned dropped;
  4114. num_bytes = ALIGN(num_bytes, root->sectorsize);
  4115. spin_lock(&BTRFS_I(inode)->lock);
  4116. dropped = drop_outstanding_extent(inode);
  4117. to_free = calc_csum_metadata_size(inode, num_bytes, 0);
  4118. spin_unlock(&BTRFS_I(inode)->lock);
  4119. if (dropped > 0)
  4120. to_free += btrfs_calc_trans_metadata_size(root, dropped);
  4121. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  4122. btrfs_ino(inode), to_free, 0);
  4123. if (root->fs_info->quota_enabled) {
  4124. btrfs_qgroup_free(root, num_bytes +
  4125. dropped * root->leafsize);
  4126. }
  4127. btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
  4128. to_free);
  4129. }
  4130. /**
  4131. * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
  4132. * @inode: inode we're writing to
  4133. * @num_bytes: the number of bytes we want to allocate
  4134. *
  4135. * This will do the following things
  4136. *
  4137. * o reserve space in the data space info for num_bytes
  4138. * o reserve space in the metadata space info based on number of outstanding
  4139. * extents and how much csums will be needed
  4140. * o add to the inodes ->delalloc_bytes
  4141. * o add it to the fs_info's delalloc inodes list.
  4142. *
  4143. * This will return 0 for success and -ENOSPC if there is no space left.
  4144. */
  4145. int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
  4146. {
  4147. int ret;
  4148. ret = btrfs_check_data_free_space(inode, num_bytes);
  4149. if (ret)
  4150. return ret;
  4151. ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
  4152. if (ret) {
  4153. btrfs_free_reserved_data_space(inode, num_bytes);
  4154. return ret;
  4155. }
  4156. return 0;
  4157. }
  4158. /**
  4159. * btrfs_delalloc_release_space - release data and metadata space for delalloc
  4160. * @inode: inode we're releasing space for
  4161. * @num_bytes: the number of bytes we want to free up
  4162. *
  4163. * This must be matched with a call to btrfs_delalloc_reserve_space. This is
  4164. * called in the case that we don't need the metadata AND data reservations
  4165. * anymore. So if there is an error or we insert an inline extent.
  4166. *
  4167. * This function will release the metadata space that was not used and will
  4168. * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
  4169. * list if there are no delalloc bytes left.
  4170. */
  4171. void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
  4172. {
  4173. btrfs_delalloc_release_metadata(inode, num_bytes);
  4174. btrfs_free_reserved_data_space(inode, num_bytes);
  4175. }
  4176. static int update_block_group(struct btrfs_trans_handle *trans,
  4177. struct btrfs_root *root,
  4178. u64 bytenr, u64 num_bytes, int alloc)
  4179. {
  4180. struct btrfs_block_group_cache *cache = NULL;
  4181. struct btrfs_fs_info *info = root->fs_info;
  4182. u64 total = num_bytes;
  4183. u64 old_val;
  4184. u64 byte_in_group;
  4185. int factor;
  4186. /* block accounting for super block */
  4187. spin_lock(&info->delalloc_lock);
  4188. old_val = btrfs_super_bytes_used(info->super_copy);
  4189. if (alloc)
  4190. old_val += num_bytes;
  4191. else
  4192. old_val -= num_bytes;
  4193. btrfs_set_super_bytes_used(info->super_copy, old_val);
  4194. spin_unlock(&info->delalloc_lock);
  4195. while (total) {
  4196. cache = btrfs_lookup_block_group(info, bytenr);
  4197. if (!cache)
  4198. return -ENOENT;
  4199. if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
  4200. BTRFS_BLOCK_GROUP_RAID1 |
  4201. BTRFS_BLOCK_GROUP_RAID10))
  4202. factor = 2;
  4203. else
  4204. factor = 1;
  4205. /*
  4206. * If this block group has free space cache written out, we
  4207. * need to make sure to load it if we are removing space. This
  4208. * is because we need the unpinning stage to actually add the
  4209. * space back to the block group, otherwise we will leak space.
  4210. */
  4211. if (!alloc && cache->cached == BTRFS_CACHE_NO)
  4212. cache_block_group(cache, trans, NULL, 1);
  4213. byte_in_group = bytenr - cache->key.objectid;
  4214. WARN_ON(byte_in_group > cache->key.offset);
  4215. spin_lock(&cache->space_info->lock);
  4216. spin_lock(&cache->lock);
  4217. if (btrfs_test_opt(root, SPACE_CACHE) &&
  4218. cache->disk_cache_state < BTRFS_DC_CLEAR)
  4219. cache->disk_cache_state = BTRFS_DC_CLEAR;
  4220. cache->dirty = 1;
  4221. old_val = btrfs_block_group_used(&cache->item);
  4222. num_bytes = min(total, cache->key.offset - byte_in_group);
  4223. if (alloc) {
  4224. old_val += num_bytes;
  4225. btrfs_set_block_group_used(&cache->item, old_val);
  4226. cache->reserved -= num_bytes;
  4227. cache->space_info->bytes_reserved -= num_bytes;
  4228. cache->space_info->bytes_used += num_bytes;
  4229. cache->space_info->disk_used += num_bytes * factor;
  4230. spin_unlock(&cache->lock);
  4231. spin_unlock(&cache->space_info->lock);
  4232. } else {
  4233. old_val -= num_bytes;
  4234. btrfs_set_block_group_used(&cache->item, old_val);
  4235. cache->pinned += num_bytes;
  4236. cache->space_info->bytes_pinned += num_bytes;
  4237. cache->space_info->bytes_used -= num_bytes;
  4238. cache->space_info->disk_used -= num_bytes * factor;
  4239. spin_unlock(&cache->lock);
  4240. spin_unlock(&cache->space_info->lock);
  4241. set_extent_dirty(info->pinned_extents,
  4242. bytenr, bytenr + num_bytes - 1,
  4243. GFP_NOFS | __GFP_NOFAIL);
  4244. }
  4245. btrfs_put_block_group(cache);
  4246. total -= num_bytes;
  4247. bytenr += num_bytes;
  4248. }
  4249. return 0;
  4250. }
  4251. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  4252. {
  4253. struct btrfs_block_group_cache *cache;
  4254. u64 bytenr;
  4255. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  4256. if (!cache)
  4257. return 0;
  4258. bytenr = cache->key.objectid;
  4259. btrfs_put_block_group(cache);
  4260. return bytenr;
  4261. }
  4262. static int pin_down_extent(struct btrfs_root *root,
  4263. struct btrfs_block_group_cache *cache,
  4264. u64 bytenr, u64 num_bytes, int reserved)
  4265. {
  4266. spin_lock(&cache->space_info->lock);
  4267. spin_lock(&cache->lock);
  4268. cache->pinned += num_bytes;
  4269. cache->space_info->bytes_pinned += num_bytes;
  4270. if (reserved) {
  4271. cache->reserved -= num_bytes;
  4272. cache->space_info->bytes_reserved -= num_bytes;
  4273. }
  4274. spin_unlock(&cache->lock);
  4275. spin_unlock(&cache->space_info->lock);
  4276. set_extent_dirty(root->fs_info->pinned_extents, bytenr,
  4277. bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
  4278. return 0;
  4279. }
  4280. /*
  4281. * this function must be called within transaction
  4282. */
  4283. int btrfs_pin_extent(struct btrfs_root *root,
  4284. u64 bytenr, u64 num_bytes, int reserved)
  4285. {
  4286. struct btrfs_block_group_cache *cache;
  4287. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4288. BUG_ON(!cache); /* Logic error */
  4289. pin_down_extent(root, cache, bytenr, num_bytes, reserved);
  4290. btrfs_put_block_group(cache);
  4291. return 0;
  4292. }
  4293. /*
  4294. * this function must be called within transaction
  4295. */
  4296. int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
  4297. struct btrfs_root *root,
  4298. u64 bytenr, u64 num_bytes)
  4299. {
  4300. struct btrfs_block_group_cache *cache;
  4301. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  4302. BUG_ON(!cache); /* Logic error */
  4303. /*
  4304. * pull in the free space cache (if any) so that our pin
  4305. * removes the free space from the cache. We have load_only set
  4306. * to one because the slow code to read in the free extents does check
  4307. * the pinned extents.
  4308. */
  4309. cache_block_group(cache, trans, root, 1);
  4310. pin_down_extent(root, cache, bytenr, num_bytes, 0);
  4311. /* remove us from the free space cache (if we're there at all) */
  4312. btrfs_remove_free_space(cache, bytenr, num_bytes);
  4313. btrfs_put_block_group(cache);
  4314. return 0;
  4315. }
  4316. /**
  4317. * btrfs_update_reserved_bytes - update the block_group and space info counters
  4318. * @cache: The cache we are manipulating
  4319. * @num_bytes: The number of bytes in question
  4320. * @reserve: One of the reservation enums
  4321. *
  4322. * This is called by the allocator when it reserves space, or by somebody who is
  4323. * freeing space that was never actually used on disk. For example if you
  4324. * reserve some space for a new leaf in transaction A and before transaction A
  4325. * commits you free that leaf, you call this with reserve set to 0 in order to
  4326. * clear the reservation.
  4327. *
  4328. * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
  4329. * ENOSPC accounting. For data we handle the reservation through clearing the
  4330. * delalloc bits in the io_tree. We have to do this since we could end up
  4331. * allocating less disk space for the amount of data we have reserved in the
  4332. * case of compression.
  4333. *
  4334. * If this is a reservation and the block group has become read only we cannot
  4335. * make the reservation and return -EAGAIN, otherwise this function always
  4336. * succeeds.
  4337. */
  4338. static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
  4339. u64 num_bytes, int reserve)
  4340. {
  4341. struct btrfs_space_info *space_info = cache->space_info;
  4342. int ret = 0;
  4343. spin_lock(&space_info->lock);
  4344. spin_lock(&cache->lock);
  4345. if (reserve != RESERVE_FREE) {
  4346. if (cache->ro) {
  4347. ret = -EAGAIN;
  4348. } else {
  4349. cache->reserved += num_bytes;
  4350. space_info->bytes_reserved += num_bytes;
  4351. if (reserve == RESERVE_ALLOC) {
  4352. trace_btrfs_space_reservation(cache->fs_info,
  4353. "space_info", space_info->flags,
  4354. num_bytes, 0);
  4355. space_info->bytes_may_use -= num_bytes;
  4356. }
  4357. }
  4358. } else {
  4359. if (cache->ro)
  4360. space_info->bytes_readonly += num_bytes;
  4361. cache->reserved -= num_bytes;
  4362. space_info->bytes_reserved -= num_bytes;
  4363. space_info->reservation_progress++;
  4364. }
  4365. spin_unlock(&cache->lock);
  4366. spin_unlock(&space_info->lock);
  4367. return ret;
  4368. }
  4369. void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
  4370. struct btrfs_root *root)
  4371. {
  4372. struct btrfs_fs_info *fs_info = root->fs_info;
  4373. struct btrfs_caching_control *next;
  4374. struct btrfs_caching_control *caching_ctl;
  4375. struct btrfs_block_group_cache *cache;
  4376. down_write(&fs_info->extent_commit_sem);
  4377. list_for_each_entry_safe(caching_ctl, next,
  4378. &fs_info->caching_block_groups, list) {
  4379. cache = caching_ctl->block_group;
  4380. if (block_group_cache_done(cache)) {
  4381. cache->last_byte_to_unpin = (u64)-1;
  4382. list_del_init(&caching_ctl->list);
  4383. put_caching_control(caching_ctl);
  4384. } else {
  4385. cache->last_byte_to_unpin = caching_ctl->progress;
  4386. }
  4387. }
  4388. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4389. fs_info->pinned_extents = &fs_info->freed_extents[1];
  4390. else
  4391. fs_info->pinned_extents = &fs_info->freed_extents[0];
  4392. up_write(&fs_info->extent_commit_sem);
  4393. update_global_block_rsv(fs_info);
  4394. }
  4395. static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  4396. {
  4397. struct btrfs_fs_info *fs_info = root->fs_info;
  4398. struct btrfs_block_group_cache *cache = NULL;
  4399. struct btrfs_space_info *space_info;
  4400. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4401. u64 len;
  4402. bool readonly;
  4403. while (start <= end) {
  4404. readonly = false;
  4405. if (!cache ||
  4406. start >= cache->key.objectid + cache->key.offset) {
  4407. if (cache)
  4408. btrfs_put_block_group(cache);
  4409. cache = btrfs_lookup_block_group(fs_info, start);
  4410. BUG_ON(!cache); /* Logic error */
  4411. }
  4412. len = cache->key.objectid + cache->key.offset - start;
  4413. len = min(len, end + 1 - start);
  4414. if (start < cache->last_byte_to_unpin) {
  4415. len = min(len, cache->last_byte_to_unpin - start);
  4416. btrfs_add_free_space(cache, start, len);
  4417. }
  4418. start += len;
  4419. space_info = cache->space_info;
  4420. spin_lock(&space_info->lock);
  4421. spin_lock(&cache->lock);
  4422. cache->pinned -= len;
  4423. space_info->bytes_pinned -= len;
  4424. if (cache->ro) {
  4425. space_info->bytes_readonly += len;
  4426. readonly = true;
  4427. }
  4428. spin_unlock(&cache->lock);
  4429. if (!readonly && global_rsv->space_info == space_info) {
  4430. spin_lock(&global_rsv->lock);
  4431. if (!global_rsv->full) {
  4432. len = min(len, global_rsv->size -
  4433. global_rsv->reserved);
  4434. global_rsv->reserved += len;
  4435. space_info->bytes_may_use += len;
  4436. if (global_rsv->reserved >= global_rsv->size)
  4437. global_rsv->full = 1;
  4438. }
  4439. spin_unlock(&global_rsv->lock);
  4440. }
  4441. spin_unlock(&space_info->lock);
  4442. }
  4443. if (cache)
  4444. btrfs_put_block_group(cache);
  4445. return 0;
  4446. }
  4447. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  4448. struct btrfs_root *root)
  4449. {
  4450. struct btrfs_fs_info *fs_info = root->fs_info;
  4451. struct extent_io_tree *unpin;
  4452. u64 start;
  4453. u64 end;
  4454. int ret;
  4455. if (trans->aborted)
  4456. return 0;
  4457. if (fs_info->pinned_extents == &fs_info->freed_extents[0])
  4458. unpin = &fs_info->freed_extents[1];
  4459. else
  4460. unpin = &fs_info->freed_extents[0];
  4461. while (1) {
  4462. ret = find_first_extent_bit(unpin, 0, &start, &end,
  4463. EXTENT_DIRTY, NULL);
  4464. if (ret)
  4465. break;
  4466. if (btrfs_test_opt(root, DISCARD))
  4467. ret = btrfs_discard_extent(root, start,
  4468. end + 1 - start, NULL);
  4469. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  4470. unpin_extent_range(root, start, end);
  4471. cond_resched();
  4472. }
  4473. return 0;
  4474. }
  4475. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  4476. struct btrfs_root *root,
  4477. u64 bytenr, u64 num_bytes, u64 parent,
  4478. u64 root_objectid, u64 owner_objectid,
  4479. u64 owner_offset, int refs_to_drop,
  4480. struct btrfs_delayed_extent_op *extent_op)
  4481. {
  4482. struct btrfs_key key;
  4483. struct btrfs_path *path;
  4484. struct btrfs_fs_info *info = root->fs_info;
  4485. struct btrfs_root *extent_root = info->extent_root;
  4486. struct extent_buffer *leaf;
  4487. struct btrfs_extent_item *ei;
  4488. struct btrfs_extent_inline_ref *iref;
  4489. int ret;
  4490. int is_data;
  4491. int extent_slot = 0;
  4492. int found_extent = 0;
  4493. int num_to_del = 1;
  4494. u32 item_size;
  4495. u64 refs;
  4496. path = btrfs_alloc_path();
  4497. if (!path)
  4498. return -ENOMEM;
  4499. path->reada = 1;
  4500. path->leave_spinning = 1;
  4501. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  4502. BUG_ON(!is_data && refs_to_drop != 1);
  4503. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  4504. bytenr, num_bytes, parent,
  4505. root_objectid, owner_objectid,
  4506. owner_offset);
  4507. if (ret == 0) {
  4508. extent_slot = path->slots[0];
  4509. while (extent_slot >= 0) {
  4510. btrfs_item_key_to_cpu(path->nodes[0], &key,
  4511. extent_slot);
  4512. if (key.objectid != bytenr)
  4513. break;
  4514. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  4515. key.offset == num_bytes) {
  4516. found_extent = 1;
  4517. break;
  4518. }
  4519. if (path->slots[0] - extent_slot > 5)
  4520. break;
  4521. extent_slot--;
  4522. }
  4523. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4524. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  4525. if (found_extent && item_size < sizeof(*ei))
  4526. found_extent = 0;
  4527. #endif
  4528. if (!found_extent) {
  4529. BUG_ON(iref);
  4530. ret = remove_extent_backref(trans, extent_root, path,
  4531. NULL, refs_to_drop,
  4532. is_data);
  4533. if (ret) {
  4534. btrfs_abort_transaction(trans, extent_root, ret);
  4535. goto out;
  4536. }
  4537. btrfs_release_path(path);
  4538. path->leave_spinning = 1;
  4539. key.objectid = bytenr;
  4540. key.type = BTRFS_EXTENT_ITEM_KEY;
  4541. key.offset = num_bytes;
  4542. ret = btrfs_search_slot(trans, extent_root,
  4543. &key, path, -1, 1);
  4544. if (ret) {
  4545. printk(KERN_ERR "umm, got %d back from search"
  4546. ", was looking for %llu\n", ret,
  4547. (unsigned long long)bytenr);
  4548. if (ret > 0)
  4549. btrfs_print_leaf(extent_root,
  4550. path->nodes[0]);
  4551. }
  4552. if (ret < 0) {
  4553. btrfs_abort_transaction(trans, extent_root, ret);
  4554. goto out;
  4555. }
  4556. extent_slot = path->slots[0];
  4557. }
  4558. } else if (ret == -ENOENT) {
  4559. btrfs_print_leaf(extent_root, path->nodes[0]);
  4560. WARN_ON(1);
  4561. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  4562. "parent %llu root %llu owner %llu offset %llu\n",
  4563. (unsigned long long)bytenr,
  4564. (unsigned long long)parent,
  4565. (unsigned long long)root_objectid,
  4566. (unsigned long long)owner_objectid,
  4567. (unsigned long long)owner_offset);
  4568. } else {
  4569. btrfs_abort_transaction(trans, extent_root, ret);
  4570. goto out;
  4571. }
  4572. leaf = path->nodes[0];
  4573. item_size = btrfs_item_size_nr(leaf, extent_slot);
  4574. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  4575. if (item_size < sizeof(*ei)) {
  4576. BUG_ON(found_extent || extent_slot != path->slots[0]);
  4577. ret = convert_extent_item_v0(trans, extent_root, path,
  4578. owner_objectid, 0);
  4579. if (ret < 0) {
  4580. btrfs_abort_transaction(trans, extent_root, ret);
  4581. goto out;
  4582. }
  4583. btrfs_release_path(path);
  4584. path->leave_spinning = 1;
  4585. key.objectid = bytenr;
  4586. key.type = BTRFS_EXTENT_ITEM_KEY;
  4587. key.offset = num_bytes;
  4588. ret = btrfs_search_slot(trans, extent_root, &key, path,
  4589. -1, 1);
  4590. if (ret) {
  4591. printk(KERN_ERR "umm, got %d back from search"
  4592. ", was looking for %llu\n", ret,
  4593. (unsigned long long)bytenr);
  4594. btrfs_print_leaf(extent_root, path->nodes[0]);
  4595. }
  4596. if (ret < 0) {
  4597. btrfs_abort_transaction(trans, extent_root, ret);
  4598. goto out;
  4599. }
  4600. extent_slot = path->slots[0];
  4601. leaf = path->nodes[0];
  4602. item_size = btrfs_item_size_nr(leaf, extent_slot);
  4603. }
  4604. #endif
  4605. BUG_ON(item_size < sizeof(*ei));
  4606. ei = btrfs_item_ptr(leaf, extent_slot,
  4607. struct btrfs_extent_item);
  4608. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4609. struct btrfs_tree_block_info *bi;
  4610. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  4611. bi = (struct btrfs_tree_block_info *)(ei + 1);
  4612. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  4613. }
  4614. refs = btrfs_extent_refs(leaf, ei);
  4615. BUG_ON(refs < refs_to_drop);
  4616. refs -= refs_to_drop;
  4617. if (refs > 0) {
  4618. if (extent_op)
  4619. __run_delayed_extent_op(extent_op, leaf, ei);
  4620. /*
  4621. * In the case of inline back ref, reference count will
  4622. * be updated by remove_extent_backref
  4623. */
  4624. if (iref) {
  4625. BUG_ON(!found_extent);
  4626. } else {
  4627. btrfs_set_extent_refs(leaf, ei, refs);
  4628. btrfs_mark_buffer_dirty(leaf);
  4629. }
  4630. if (found_extent) {
  4631. ret = remove_extent_backref(trans, extent_root, path,
  4632. iref, refs_to_drop,
  4633. is_data);
  4634. if (ret) {
  4635. btrfs_abort_transaction(trans, extent_root, ret);
  4636. goto out;
  4637. }
  4638. }
  4639. } else {
  4640. if (found_extent) {
  4641. BUG_ON(is_data && refs_to_drop !=
  4642. extent_data_ref_count(root, path, iref));
  4643. if (iref) {
  4644. BUG_ON(path->slots[0] != extent_slot);
  4645. } else {
  4646. BUG_ON(path->slots[0] != extent_slot + 1);
  4647. path->slots[0] = extent_slot;
  4648. num_to_del = 2;
  4649. }
  4650. }
  4651. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  4652. num_to_del);
  4653. if (ret) {
  4654. btrfs_abort_transaction(trans, extent_root, ret);
  4655. goto out;
  4656. }
  4657. btrfs_release_path(path);
  4658. if (is_data) {
  4659. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  4660. if (ret) {
  4661. btrfs_abort_transaction(trans, extent_root, ret);
  4662. goto out;
  4663. }
  4664. }
  4665. ret = update_block_group(trans, root, bytenr, num_bytes, 0);
  4666. if (ret) {
  4667. btrfs_abort_transaction(trans, extent_root, ret);
  4668. goto out;
  4669. }
  4670. }
  4671. out:
  4672. btrfs_free_path(path);
  4673. return ret;
  4674. }
  4675. /*
  4676. * when we free an block, it is possible (and likely) that we free the last
  4677. * delayed ref for that extent as well. This searches the delayed ref tree for
  4678. * a given extent, and if there are no other delayed refs to be processed, it
  4679. * removes it from the tree.
  4680. */
  4681. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  4682. struct btrfs_root *root, u64 bytenr)
  4683. {
  4684. struct btrfs_delayed_ref_head *head;
  4685. struct btrfs_delayed_ref_root *delayed_refs;
  4686. struct btrfs_delayed_ref_node *ref;
  4687. struct rb_node *node;
  4688. int ret = 0;
  4689. delayed_refs = &trans->transaction->delayed_refs;
  4690. spin_lock(&delayed_refs->lock);
  4691. head = btrfs_find_delayed_ref_head(trans, bytenr);
  4692. if (!head)
  4693. goto out;
  4694. node = rb_prev(&head->node.rb_node);
  4695. if (!node)
  4696. goto out;
  4697. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  4698. /* there are still entries for this ref, we can't drop it */
  4699. if (ref->bytenr == bytenr)
  4700. goto out;
  4701. if (head->extent_op) {
  4702. if (!head->must_insert_reserved)
  4703. goto out;
  4704. kfree(head->extent_op);
  4705. head->extent_op = NULL;
  4706. }
  4707. /*
  4708. * waiting for the lock here would deadlock. If someone else has it
  4709. * locked they are already in the process of dropping it anyway
  4710. */
  4711. if (!mutex_trylock(&head->mutex))
  4712. goto out;
  4713. /*
  4714. * at this point we have a head with no other entries. Go
  4715. * ahead and process it.
  4716. */
  4717. head->node.in_tree = 0;
  4718. rb_erase(&head->node.rb_node, &delayed_refs->root);
  4719. delayed_refs->num_entries--;
  4720. /*
  4721. * we don't take a ref on the node because we're removing it from the
  4722. * tree, so we just steal the ref the tree was holding.
  4723. */
  4724. delayed_refs->num_heads--;
  4725. if (list_empty(&head->cluster))
  4726. delayed_refs->num_heads_ready--;
  4727. list_del_init(&head->cluster);
  4728. spin_unlock(&delayed_refs->lock);
  4729. BUG_ON(head->extent_op);
  4730. if (head->must_insert_reserved)
  4731. ret = 1;
  4732. mutex_unlock(&head->mutex);
  4733. btrfs_put_delayed_ref(&head->node);
  4734. return ret;
  4735. out:
  4736. spin_unlock(&delayed_refs->lock);
  4737. return 0;
  4738. }
  4739. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  4740. struct btrfs_root *root,
  4741. struct extent_buffer *buf,
  4742. u64 parent, int last_ref)
  4743. {
  4744. struct btrfs_block_group_cache *cache = NULL;
  4745. int ret;
  4746. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4747. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  4748. buf->start, buf->len,
  4749. parent, root->root_key.objectid,
  4750. btrfs_header_level(buf),
  4751. BTRFS_DROP_DELAYED_REF, NULL, 0);
  4752. BUG_ON(ret); /* -ENOMEM */
  4753. }
  4754. if (!last_ref)
  4755. return;
  4756. cache = btrfs_lookup_block_group(root->fs_info, buf->start);
  4757. if (btrfs_header_generation(buf) == trans->transid) {
  4758. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4759. ret = check_ref_cleanup(trans, root, buf->start);
  4760. if (!ret)
  4761. goto out;
  4762. }
  4763. if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  4764. pin_down_extent(root, cache, buf->start, buf->len, 1);
  4765. goto out;
  4766. }
  4767. WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
  4768. btrfs_add_free_space(cache, buf->start, buf->len);
  4769. btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
  4770. }
  4771. out:
  4772. /*
  4773. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  4774. * anymore.
  4775. */
  4776. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  4777. btrfs_put_block_group(cache);
  4778. }
  4779. /* Can return -ENOMEM */
  4780. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4781. u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
  4782. u64 owner, u64 offset, int for_cow)
  4783. {
  4784. int ret;
  4785. struct btrfs_fs_info *fs_info = root->fs_info;
  4786. /*
  4787. * tree log blocks never actually go into the extent allocation
  4788. * tree, just update pinning info and exit early.
  4789. */
  4790. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  4791. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  4792. /* unlocks the pinned mutex */
  4793. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  4794. ret = 0;
  4795. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4796. ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
  4797. num_bytes,
  4798. parent, root_objectid, (int)owner,
  4799. BTRFS_DROP_DELAYED_REF, NULL, for_cow);
  4800. } else {
  4801. ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
  4802. num_bytes,
  4803. parent, root_objectid, owner,
  4804. offset, BTRFS_DROP_DELAYED_REF,
  4805. NULL, for_cow);
  4806. }
  4807. return ret;
  4808. }
  4809. static u64 stripe_align(struct btrfs_root *root, u64 val)
  4810. {
  4811. u64 mask = ((u64)root->stripesize - 1);
  4812. u64 ret = (val + mask) & ~mask;
  4813. return ret;
  4814. }
  4815. /*
  4816. * when we wait for progress in the block group caching, its because
  4817. * our allocation attempt failed at least once. So, we must sleep
  4818. * and let some progress happen before we try again.
  4819. *
  4820. * This function will sleep at least once waiting for new free space to
  4821. * show up, and then it will check the block group free space numbers
  4822. * for our min num_bytes. Another option is to have it go ahead
  4823. * and look in the rbtree for a free extent of a given size, but this
  4824. * is a good start.
  4825. */
  4826. static noinline int
  4827. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  4828. u64 num_bytes)
  4829. {
  4830. struct btrfs_caching_control *caching_ctl;
  4831. DEFINE_WAIT(wait);
  4832. caching_ctl = get_caching_control(cache);
  4833. if (!caching_ctl)
  4834. return 0;
  4835. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  4836. (cache->free_space_ctl->free_space >= num_bytes));
  4837. put_caching_control(caching_ctl);
  4838. return 0;
  4839. }
  4840. static noinline int
  4841. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  4842. {
  4843. struct btrfs_caching_control *caching_ctl;
  4844. DEFINE_WAIT(wait);
  4845. caching_ctl = get_caching_control(cache);
  4846. if (!caching_ctl)
  4847. return 0;
  4848. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  4849. put_caching_control(caching_ctl);
  4850. return 0;
  4851. }
  4852. int __get_raid_index(u64 flags)
  4853. {
  4854. int index;
  4855. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  4856. index = 0;
  4857. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  4858. index = 1;
  4859. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  4860. index = 2;
  4861. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  4862. index = 3;
  4863. else
  4864. index = 4;
  4865. return index;
  4866. }
  4867. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  4868. {
  4869. return __get_raid_index(cache->flags);
  4870. }
  4871. enum btrfs_loop_type {
  4872. LOOP_CACHING_NOWAIT = 0,
  4873. LOOP_CACHING_WAIT = 1,
  4874. LOOP_ALLOC_CHUNK = 2,
  4875. LOOP_NO_EMPTY_SIZE = 3,
  4876. };
  4877. /*
  4878. * walks the btree of allocated extents and find a hole of a given size.
  4879. * The key ins is changed to record the hole:
  4880. * ins->objectid == block start
  4881. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  4882. * ins->offset == number of blocks
  4883. * Any available blocks before search_start are skipped.
  4884. */
  4885. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  4886. struct btrfs_root *orig_root,
  4887. u64 num_bytes, u64 empty_size,
  4888. u64 hint_byte, struct btrfs_key *ins,
  4889. u64 data)
  4890. {
  4891. int ret = 0;
  4892. struct btrfs_root *root = orig_root->fs_info->extent_root;
  4893. struct btrfs_free_cluster *last_ptr = NULL;
  4894. struct btrfs_block_group_cache *block_group = NULL;
  4895. struct btrfs_block_group_cache *used_block_group;
  4896. u64 search_start = 0;
  4897. int empty_cluster = 2 * 1024 * 1024;
  4898. struct btrfs_space_info *space_info;
  4899. int loop = 0;
  4900. int index = __get_raid_index(data);
  4901. int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
  4902. RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
  4903. bool found_uncached_bg = false;
  4904. bool failed_cluster_refill = false;
  4905. bool failed_alloc = false;
  4906. bool use_cluster = true;
  4907. bool have_caching_bg = false;
  4908. WARN_ON(num_bytes < root->sectorsize);
  4909. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  4910. ins->objectid = 0;
  4911. ins->offset = 0;
  4912. trace_find_free_extent(orig_root, num_bytes, empty_size, data);
  4913. space_info = __find_space_info(root->fs_info, data);
  4914. if (!space_info) {
  4915. printk(KERN_ERR "No space info for %llu\n", data);
  4916. return -ENOSPC;
  4917. }
  4918. /*
  4919. * If the space info is for both data and metadata it means we have a
  4920. * small filesystem and we can't use the clustering stuff.
  4921. */
  4922. if (btrfs_mixed_space_info(space_info))
  4923. use_cluster = false;
  4924. if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  4925. last_ptr = &root->fs_info->meta_alloc_cluster;
  4926. if (!btrfs_test_opt(root, SSD))
  4927. empty_cluster = 64 * 1024;
  4928. }
  4929. if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  4930. btrfs_test_opt(root, SSD)) {
  4931. last_ptr = &root->fs_info->data_alloc_cluster;
  4932. }
  4933. if (last_ptr) {
  4934. spin_lock(&last_ptr->lock);
  4935. if (last_ptr->block_group)
  4936. hint_byte = last_ptr->window_start;
  4937. spin_unlock(&last_ptr->lock);
  4938. }
  4939. search_start = max(search_start, first_logical_byte(root, 0));
  4940. search_start = max(search_start, hint_byte);
  4941. if (!last_ptr)
  4942. empty_cluster = 0;
  4943. if (search_start == hint_byte) {
  4944. block_group = btrfs_lookup_block_group(root->fs_info,
  4945. search_start);
  4946. used_block_group = block_group;
  4947. /*
  4948. * we don't want to use the block group if it doesn't match our
  4949. * allocation bits, or if its not cached.
  4950. *
  4951. * However if we are re-searching with an ideal block group
  4952. * picked out then we don't care that the block group is cached.
  4953. */
  4954. if (block_group && block_group_bits(block_group, data) &&
  4955. block_group->cached != BTRFS_CACHE_NO) {
  4956. down_read(&space_info->groups_sem);
  4957. if (list_empty(&block_group->list) ||
  4958. block_group->ro) {
  4959. /*
  4960. * someone is removing this block group,
  4961. * we can't jump into the have_block_group
  4962. * target because our list pointers are not
  4963. * valid
  4964. */
  4965. btrfs_put_block_group(block_group);
  4966. up_read(&space_info->groups_sem);
  4967. } else {
  4968. index = get_block_group_index(block_group);
  4969. goto have_block_group;
  4970. }
  4971. } else if (block_group) {
  4972. btrfs_put_block_group(block_group);
  4973. }
  4974. }
  4975. search:
  4976. have_caching_bg = false;
  4977. down_read(&space_info->groups_sem);
  4978. list_for_each_entry(block_group, &space_info->block_groups[index],
  4979. list) {
  4980. u64 offset;
  4981. int cached;
  4982. used_block_group = block_group;
  4983. btrfs_get_block_group(block_group);
  4984. search_start = block_group->key.objectid;
  4985. /*
  4986. * this can happen if we end up cycling through all the
  4987. * raid types, but we want to make sure we only allocate
  4988. * for the proper type.
  4989. */
  4990. if (!block_group_bits(block_group, data)) {
  4991. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  4992. BTRFS_BLOCK_GROUP_RAID1 |
  4993. BTRFS_BLOCK_GROUP_RAID10;
  4994. /*
  4995. * if they asked for extra copies and this block group
  4996. * doesn't provide them, bail. This does allow us to
  4997. * fill raid0 from raid1.
  4998. */
  4999. if ((data & extra) && !(block_group->flags & extra))
  5000. goto loop;
  5001. }
  5002. have_block_group:
  5003. cached = block_group_cache_done(block_group);
  5004. if (unlikely(!cached)) {
  5005. found_uncached_bg = true;
  5006. ret = cache_block_group(block_group, trans,
  5007. orig_root, 0);
  5008. BUG_ON(ret < 0);
  5009. ret = 0;
  5010. }
  5011. if (unlikely(block_group->ro))
  5012. goto loop;
  5013. /*
  5014. * Ok we want to try and use the cluster allocator, so
  5015. * lets look there
  5016. */
  5017. if (last_ptr) {
  5018. /*
  5019. * the refill lock keeps out other
  5020. * people trying to start a new cluster
  5021. */
  5022. spin_lock(&last_ptr->refill_lock);
  5023. used_block_group = last_ptr->block_group;
  5024. if (used_block_group != block_group &&
  5025. (!used_block_group ||
  5026. used_block_group->ro ||
  5027. !block_group_bits(used_block_group, data))) {
  5028. used_block_group = block_group;
  5029. goto refill_cluster;
  5030. }
  5031. if (used_block_group != block_group)
  5032. btrfs_get_block_group(used_block_group);
  5033. offset = btrfs_alloc_from_cluster(used_block_group,
  5034. last_ptr, num_bytes, used_block_group->key.objectid);
  5035. if (offset) {
  5036. /* we have a block, we're done */
  5037. spin_unlock(&last_ptr->refill_lock);
  5038. trace_btrfs_reserve_extent_cluster(root,
  5039. block_group, search_start, num_bytes);
  5040. goto checks;
  5041. }
  5042. WARN_ON(last_ptr->block_group != used_block_group);
  5043. if (used_block_group != block_group) {
  5044. btrfs_put_block_group(used_block_group);
  5045. used_block_group = block_group;
  5046. }
  5047. refill_cluster:
  5048. BUG_ON(used_block_group != block_group);
  5049. /* If we are on LOOP_NO_EMPTY_SIZE, we can't
  5050. * set up a new clusters, so lets just skip it
  5051. * and let the allocator find whatever block
  5052. * it can find. If we reach this point, we
  5053. * will have tried the cluster allocator
  5054. * plenty of times and not have found
  5055. * anything, so we are likely way too
  5056. * fragmented for the clustering stuff to find
  5057. * anything.
  5058. *
  5059. * However, if the cluster is taken from the
  5060. * current block group, release the cluster
  5061. * first, so that we stand a better chance of
  5062. * succeeding in the unclustered
  5063. * allocation. */
  5064. if (loop >= LOOP_NO_EMPTY_SIZE &&
  5065. last_ptr->block_group != block_group) {
  5066. spin_unlock(&last_ptr->refill_lock);
  5067. goto unclustered_alloc;
  5068. }
  5069. /*
  5070. * this cluster didn't work out, free it and
  5071. * start over
  5072. */
  5073. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5074. if (loop >= LOOP_NO_EMPTY_SIZE) {
  5075. spin_unlock(&last_ptr->refill_lock);
  5076. goto unclustered_alloc;
  5077. }
  5078. /* allocate a cluster in this block group */
  5079. ret = btrfs_find_space_cluster(trans, root,
  5080. block_group, last_ptr,
  5081. search_start, num_bytes,
  5082. empty_cluster + empty_size);
  5083. if (ret == 0) {
  5084. /*
  5085. * now pull our allocation out of this
  5086. * cluster
  5087. */
  5088. offset = btrfs_alloc_from_cluster(block_group,
  5089. last_ptr, num_bytes,
  5090. search_start);
  5091. if (offset) {
  5092. /* we found one, proceed */
  5093. spin_unlock(&last_ptr->refill_lock);
  5094. trace_btrfs_reserve_extent_cluster(root,
  5095. block_group, search_start,
  5096. num_bytes);
  5097. goto checks;
  5098. }
  5099. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  5100. && !failed_cluster_refill) {
  5101. spin_unlock(&last_ptr->refill_lock);
  5102. failed_cluster_refill = true;
  5103. wait_block_group_cache_progress(block_group,
  5104. num_bytes + empty_cluster + empty_size);
  5105. goto have_block_group;
  5106. }
  5107. /*
  5108. * at this point we either didn't find a cluster
  5109. * or we weren't able to allocate a block from our
  5110. * cluster. Free the cluster we've been trying
  5111. * to use, and go to the next block group
  5112. */
  5113. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  5114. spin_unlock(&last_ptr->refill_lock);
  5115. goto loop;
  5116. }
  5117. unclustered_alloc:
  5118. spin_lock(&block_group->free_space_ctl->tree_lock);
  5119. if (cached &&
  5120. block_group->free_space_ctl->free_space <
  5121. num_bytes + empty_cluster + empty_size) {
  5122. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5123. goto loop;
  5124. }
  5125. spin_unlock(&block_group->free_space_ctl->tree_lock);
  5126. offset = btrfs_find_space_for_alloc(block_group, search_start,
  5127. num_bytes, empty_size);
  5128. /*
  5129. * If we didn't find a chunk, and we haven't failed on this
  5130. * block group before, and this block group is in the middle of
  5131. * caching and we are ok with waiting, then go ahead and wait
  5132. * for progress to be made, and set failed_alloc to true.
  5133. *
  5134. * If failed_alloc is true then we've already waited on this
  5135. * block group once and should move on to the next block group.
  5136. */
  5137. if (!offset && !failed_alloc && !cached &&
  5138. loop > LOOP_CACHING_NOWAIT) {
  5139. wait_block_group_cache_progress(block_group,
  5140. num_bytes + empty_size);
  5141. failed_alloc = true;
  5142. goto have_block_group;
  5143. } else if (!offset) {
  5144. if (!cached)
  5145. have_caching_bg = true;
  5146. goto loop;
  5147. }
  5148. checks:
  5149. search_start = stripe_align(root, offset);
  5150. /* move on to the next group */
  5151. if (search_start + num_bytes >
  5152. used_block_group->key.objectid + used_block_group->key.offset) {
  5153. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5154. goto loop;
  5155. }
  5156. if (offset < search_start)
  5157. btrfs_add_free_space(used_block_group, offset,
  5158. search_start - offset);
  5159. BUG_ON(offset > search_start);
  5160. ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
  5161. alloc_type);
  5162. if (ret == -EAGAIN) {
  5163. btrfs_add_free_space(used_block_group, offset, num_bytes);
  5164. goto loop;
  5165. }
  5166. /* we are all good, lets return */
  5167. ins->objectid = search_start;
  5168. ins->offset = num_bytes;
  5169. trace_btrfs_reserve_extent(orig_root, block_group,
  5170. search_start, num_bytes);
  5171. if (used_block_group != block_group)
  5172. btrfs_put_block_group(used_block_group);
  5173. btrfs_put_block_group(block_group);
  5174. break;
  5175. loop:
  5176. failed_cluster_refill = false;
  5177. failed_alloc = false;
  5178. BUG_ON(index != get_block_group_index(block_group));
  5179. if (used_block_group != block_group)
  5180. btrfs_put_block_group(used_block_group);
  5181. btrfs_put_block_group(block_group);
  5182. }
  5183. up_read(&space_info->groups_sem);
  5184. if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
  5185. goto search;
  5186. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  5187. goto search;
  5188. /*
  5189. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  5190. * caching kthreads as we move along
  5191. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  5192. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  5193. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  5194. * again
  5195. */
  5196. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
  5197. index = 0;
  5198. loop++;
  5199. if (loop == LOOP_ALLOC_CHUNK) {
  5200. ret = do_chunk_alloc(trans, root, data,
  5201. CHUNK_ALLOC_FORCE);
  5202. /*
  5203. * Do not bail out on ENOSPC since we
  5204. * can do more things.
  5205. */
  5206. if (ret < 0 && ret != -ENOSPC) {
  5207. btrfs_abort_transaction(trans,
  5208. root, ret);
  5209. goto out;
  5210. }
  5211. }
  5212. if (loop == LOOP_NO_EMPTY_SIZE) {
  5213. empty_size = 0;
  5214. empty_cluster = 0;
  5215. }
  5216. goto search;
  5217. } else if (!ins->objectid) {
  5218. ret = -ENOSPC;
  5219. } else if (ins->objectid) {
  5220. ret = 0;
  5221. }
  5222. out:
  5223. return ret;
  5224. }
  5225. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  5226. int dump_block_groups)
  5227. {
  5228. struct btrfs_block_group_cache *cache;
  5229. int index = 0;
  5230. spin_lock(&info->lock);
  5231. printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
  5232. (unsigned long long)info->flags,
  5233. (unsigned long long)(info->total_bytes - info->bytes_used -
  5234. info->bytes_pinned - info->bytes_reserved -
  5235. info->bytes_readonly),
  5236. (info->full) ? "" : "not ");
  5237. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  5238. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  5239. (unsigned long long)info->total_bytes,
  5240. (unsigned long long)info->bytes_used,
  5241. (unsigned long long)info->bytes_pinned,
  5242. (unsigned long long)info->bytes_reserved,
  5243. (unsigned long long)info->bytes_may_use,
  5244. (unsigned long long)info->bytes_readonly);
  5245. spin_unlock(&info->lock);
  5246. if (!dump_block_groups)
  5247. return;
  5248. down_read(&info->groups_sem);
  5249. again:
  5250. list_for_each_entry(cache, &info->block_groups[index], list) {
  5251. spin_lock(&cache->lock);
  5252. printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
  5253. (unsigned long long)cache->key.objectid,
  5254. (unsigned long long)cache->key.offset,
  5255. (unsigned long long)btrfs_block_group_used(&cache->item),
  5256. (unsigned long long)cache->pinned,
  5257. (unsigned long long)cache->reserved,
  5258. cache->ro ? "[readonly]" : "");
  5259. btrfs_dump_free_space(cache, bytes);
  5260. spin_unlock(&cache->lock);
  5261. }
  5262. if (++index < BTRFS_NR_RAID_TYPES)
  5263. goto again;
  5264. up_read(&info->groups_sem);
  5265. }
  5266. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  5267. struct btrfs_root *root,
  5268. u64 num_bytes, u64 min_alloc_size,
  5269. u64 empty_size, u64 hint_byte,
  5270. struct btrfs_key *ins, u64 data)
  5271. {
  5272. bool final_tried = false;
  5273. int ret;
  5274. data = btrfs_get_alloc_profile(root, data);
  5275. again:
  5276. WARN_ON(num_bytes < root->sectorsize);
  5277. ret = find_free_extent(trans, root, num_bytes, empty_size,
  5278. hint_byte, ins, data);
  5279. if (ret == -ENOSPC) {
  5280. if (!final_tried) {
  5281. num_bytes = num_bytes >> 1;
  5282. num_bytes = num_bytes & ~(root->sectorsize - 1);
  5283. num_bytes = max(num_bytes, min_alloc_size);
  5284. if (num_bytes == min_alloc_size)
  5285. final_tried = true;
  5286. goto again;
  5287. } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
  5288. struct btrfs_space_info *sinfo;
  5289. sinfo = __find_space_info(root->fs_info, data);
  5290. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  5291. "wanted %llu\n", (unsigned long long)data,
  5292. (unsigned long long)num_bytes);
  5293. if (sinfo)
  5294. dump_space_info(sinfo, num_bytes, 1);
  5295. }
  5296. }
  5297. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  5298. return ret;
  5299. }
  5300. static int __btrfs_free_reserved_extent(struct btrfs_root *root,
  5301. u64 start, u64 len, int pin)
  5302. {
  5303. struct btrfs_block_group_cache *cache;
  5304. int ret = 0;
  5305. cache = btrfs_lookup_block_group(root->fs_info, start);
  5306. if (!cache) {
  5307. printk(KERN_ERR "Unable to find block group for %llu\n",
  5308. (unsigned long long)start);
  5309. return -ENOSPC;
  5310. }
  5311. if (btrfs_test_opt(root, DISCARD))
  5312. ret = btrfs_discard_extent(root, start, len, NULL);
  5313. if (pin)
  5314. pin_down_extent(root, cache, start, len, 1);
  5315. else {
  5316. btrfs_add_free_space(cache, start, len);
  5317. btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
  5318. }
  5319. btrfs_put_block_group(cache);
  5320. trace_btrfs_reserved_extent_free(root, start, len);
  5321. return ret;
  5322. }
  5323. int btrfs_free_reserved_extent(struct btrfs_root *root,
  5324. u64 start, u64 len)
  5325. {
  5326. return __btrfs_free_reserved_extent(root, start, len, 0);
  5327. }
  5328. int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
  5329. u64 start, u64 len)
  5330. {
  5331. return __btrfs_free_reserved_extent(root, start, len, 1);
  5332. }
  5333. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5334. struct btrfs_root *root,
  5335. u64 parent, u64 root_objectid,
  5336. u64 flags, u64 owner, u64 offset,
  5337. struct btrfs_key *ins, int ref_mod)
  5338. {
  5339. int ret;
  5340. struct btrfs_fs_info *fs_info = root->fs_info;
  5341. struct btrfs_extent_item *extent_item;
  5342. struct btrfs_extent_inline_ref *iref;
  5343. struct btrfs_path *path;
  5344. struct extent_buffer *leaf;
  5345. int type;
  5346. u32 size;
  5347. if (parent > 0)
  5348. type = BTRFS_SHARED_DATA_REF_KEY;
  5349. else
  5350. type = BTRFS_EXTENT_DATA_REF_KEY;
  5351. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  5352. path = btrfs_alloc_path();
  5353. if (!path)
  5354. return -ENOMEM;
  5355. path->leave_spinning = 1;
  5356. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5357. ins, size);
  5358. if (ret) {
  5359. btrfs_free_path(path);
  5360. return ret;
  5361. }
  5362. leaf = path->nodes[0];
  5363. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5364. struct btrfs_extent_item);
  5365. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  5366. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5367. btrfs_set_extent_flags(leaf, extent_item,
  5368. flags | BTRFS_EXTENT_FLAG_DATA);
  5369. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  5370. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  5371. if (parent > 0) {
  5372. struct btrfs_shared_data_ref *ref;
  5373. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  5374. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5375. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  5376. } else {
  5377. struct btrfs_extent_data_ref *ref;
  5378. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  5379. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  5380. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  5381. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  5382. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  5383. }
  5384. btrfs_mark_buffer_dirty(path->nodes[0]);
  5385. btrfs_free_path(path);
  5386. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  5387. if (ret) { /* -ENOENT, logic error */
  5388. printk(KERN_ERR "btrfs update block group failed for %llu "
  5389. "%llu\n", (unsigned long long)ins->objectid,
  5390. (unsigned long long)ins->offset);
  5391. BUG();
  5392. }
  5393. return ret;
  5394. }
  5395. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  5396. struct btrfs_root *root,
  5397. u64 parent, u64 root_objectid,
  5398. u64 flags, struct btrfs_disk_key *key,
  5399. int level, struct btrfs_key *ins)
  5400. {
  5401. int ret;
  5402. struct btrfs_fs_info *fs_info = root->fs_info;
  5403. struct btrfs_extent_item *extent_item;
  5404. struct btrfs_tree_block_info *block_info;
  5405. struct btrfs_extent_inline_ref *iref;
  5406. struct btrfs_path *path;
  5407. struct extent_buffer *leaf;
  5408. u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
  5409. path = btrfs_alloc_path();
  5410. if (!path)
  5411. return -ENOMEM;
  5412. path->leave_spinning = 1;
  5413. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  5414. ins, size);
  5415. if (ret) {
  5416. btrfs_free_path(path);
  5417. return ret;
  5418. }
  5419. leaf = path->nodes[0];
  5420. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  5421. struct btrfs_extent_item);
  5422. btrfs_set_extent_refs(leaf, extent_item, 1);
  5423. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  5424. btrfs_set_extent_flags(leaf, extent_item,
  5425. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  5426. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  5427. btrfs_set_tree_block_key(leaf, block_info, key);
  5428. btrfs_set_tree_block_level(leaf, block_info, level);
  5429. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  5430. if (parent > 0) {
  5431. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  5432. btrfs_set_extent_inline_ref_type(leaf, iref,
  5433. BTRFS_SHARED_BLOCK_REF_KEY);
  5434. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  5435. } else {
  5436. btrfs_set_extent_inline_ref_type(leaf, iref,
  5437. BTRFS_TREE_BLOCK_REF_KEY);
  5438. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  5439. }
  5440. btrfs_mark_buffer_dirty(leaf);
  5441. btrfs_free_path(path);
  5442. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  5443. if (ret) { /* -ENOENT, logic error */
  5444. printk(KERN_ERR "btrfs update block group failed for %llu "
  5445. "%llu\n", (unsigned long long)ins->objectid,
  5446. (unsigned long long)ins->offset);
  5447. BUG();
  5448. }
  5449. return ret;
  5450. }
  5451. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  5452. struct btrfs_root *root,
  5453. u64 root_objectid, u64 owner,
  5454. u64 offset, struct btrfs_key *ins)
  5455. {
  5456. int ret;
  5457. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  5458. ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
  5459. ins->offset, 0,
  5460. root_objectid, owner, offset,
  5461. BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
  5462. return ret;
  5463. }
  5464. /*
  5465. * this is used by the tree logging recovery code. It records that
  5466. * an extent has been allocated and makes sure to clear the free
  5467. * space cache bits as well
  5468. */
  5469. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  5470. struct btrfs_root *root,
  5471. u64 root_objectid, u64 owner, u64 offset,
  5472. struct btrfs_key *ins)
  5473. {
  5474. int ret;
  5475. struct btrfs_block_group_cache *block_group;
  5476. struct btrfs_caching_control *caching_ctl;
  5477. u64 start = ins->objectid;
  5478. u64 num_bytes = ins->offset;
  5479. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  5480. cache_block_group(block_group, trans, NULL, 0);
  5481. caching_ctl = get_caching_control(block_group);
  5482. if (!caching_ctl) {
  5483. BUG_ON(!block_group_cache_done(block_group));
  5484. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  5485. BUG_ON(ret); /* -ENOMEM */
  5486. } else {
  5487. mutex_lock(&caching_ctl->mutex);
  5488. if (start >= caching_ctl->progress) {
  5489. ret = add_excluded_extent(root, start, num_bytes);
  5490. BUG_ON(ret); /* -ENOMEM */
  5491. } else if (start + num_bytes <= caching_ctl->progress) {
  5492. ret = btrfs_remove_free_space(block_group,
  5493. start, num_bytes);
  5494. BUG_ON(ret); /* -ENOMEM */
  5495. } else {
  5496. num_bytes = caching_ctl->progress - start;
  5497. ret = btrfs_remove_free_space(block_group,
  5498. start, num_bytes);
  5499. BUG_ON(ret); /* -ENOMEM */
  5500. start = caching_ctl->progress;
  5501. num_bytes = ins->objectid + ins->offset -
  5502. caching_ctl->progress;
  5503. ret = add_excluded_extent(root, start, num_bytes);
  5504. BUG_ON(ret); /* -ENOMEM */
  5505. }
  5506. mutex_unlock(&caching_ctl->mutex);
  5507. put_caching_control(caching_ctl);
  5508. }
  5509. ret = btrfs_update_reserved_bytes(block_group, ins->offset,
  5510. RESERVE_ALLOC_NO_ACCOUNT);
  5511. BUG_ON(ret); /* logic error */
  5512. btrfs_put_block_group(block_group);
  5513. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  5514. 0, owner, offset, ins, 1);
  5515. return ret;
  5516. }
  5517. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  5518. struct btrfs_root *root,
  5519. u64 bytenr, u32 blocksize,
  5520. int level)
  5521. {
  5522. struct extent_buffer *buf;
  5523. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5524. if (!buf)
  5525. return ERR_PTR(-ENOMEM);
  5526. btrfs_set_header_generation(buf, trans->transid);
  5527. btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
  5528. btrfs_tree_lock(buf);
  5529. clean_tree_block(trans, root, buf);
  5530. clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
  5531. btrfs_set_lock_blocking(buf);
  5532. btrfs_set_buffer_uptodate(buf);
  5533. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  5534. /*
  5535. * we allow two log transactions at a time, use different
  5536. * EXENT bit to differentiate dirty pages.
  5537. */
  5538. if (root->log_transid % 2 == 0)
  5539. set_extent_dirty(&root->dirty_log_pages, buf->start,
  5540. buf->start + buf->len - 1, GFP_NOFS);
  5541. else
  5542. set_extent_new(&root->dirty_log_pages, buf->start,
  5543. buf->start + buf->len - 1, GFP_NOFS);
  5544. } else {
  5545. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  5546. buf->start + buf->len - 1, GFP_NOFS);
  5547. }
  5548. trans->blocks_used++;
  5549. /* this returns a buffer locked for blocking */
  5550. return buf;
  5551. }
  5552. static struct btrfs_block_rsv *
  5553. use_block_rsv(struct btrfs_trans_handle *trans,
  5554. struct btrfs_root *root, u32 blocksize)
  5555. {
  5556. struct btrfs_block_rsv *block_rsv;
  5557. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  5558. int ret;
  5559. block_rsv = get_block_rsv(trans, root);
  5560. if (block_rsv->size == 0) {
  5561. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  5562. BTRFS_RESERVE_NO_FLUSH);
  5563. /*
  5564. * If we couldn't reserve metadata bytes try and use some from
  5565. * the global reserve.
  5566. */
  5567. if (ret && block_rsv != global_rsv) {
  5568. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5569. if (!ret)
  5570. return global_rsv;
  5571. return ERR_PTR(ret);
  5572. } else if (ret) {
  5573. return ERR_PTR(ret);
  5574. }
  5575. return block_rsv;
  5576. }
  5577. ret = block_rsv_use_bytes(block_rsv, blocksize);
  5578. if (!ret)
  5579. return block_rsv;
  5580. if (ret && !block_rsv->failfast) {
  5581. static DEFINE_RATELIMIT_STATE(_rs,
  5582. DEFAULT_RATELIMIT_INTERVAL,
  5583. /*DEFAULT_RATELIMIT_BURST*/ 2);
  5584. if (__ratelimit(&_rs))
  5585. WARN(1, KERN_DEBUG "btrfs: block rsv returned %d\n",
  5586. ret);
  5587. ret = reserve_metadata_bytes(root, block_rsv, blocksize,
  5588. BTRFS_RESERVE_NO_FLUSH);
  5589. if (!ret) {
  5590. return block_rsv;
  5591. } else if (ret && block_rsv != global_rsv) {
  5592. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5593. if (!ret)
  5594. return global_rsv;
  5595. }
  5596. }
  5597. return ERR_PTR(-ENOSPC);
  5598. }
  5599. static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
  5600. struct btrfs_block_rsv *block_rsv, u32 blocksize)
  5601. {
  5602. block_rsv_add_bytes(block_rsv, blocksize, 0);
  5603. block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
  5604. }
  5605. /*
  5606. * finds a free extent and does all the dirty work required for allocation
  5607. * returns the key for the extent through ins, and a tree buffer for
  5608. * the first block of the extent through buf.
  5609. *
  5610. * returns the tree buffer or NULL.
  5611. */
  5612. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  5613. struct btrfs_root *root, u32 blocksize,
  5614. u64 parent, u64 root_objectid,
  5615. struct btrfs_disk_key *key, int level,
  5616. u64 hint, u64 empty_size)
  5617. {
  5618. struct btrfs_key ins;
  5619. struct btrfs_block_rsv *block_rsv;
  5620. struct extent_buffer *buf;
  5621. u64 flags = 0;
  5622. int ret;
  5623. block_rsv = use_block_rsv(trans, root, blocksize);
  5624. if (IS_ERR(block_rsv))
  5625. return ERR_CAST(block_rsv);
  5626. ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
  5627. empty_size, hint, &ins, 0);
  5628. if (ret) {
  5629. unuse_block_rsv(root->fs_info, block_rsv, blocksize);
  5630. return ERR_PTR(ret);
  5631. }
  5632. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  5633. blocksize, level);
  5634. BUG_ON(IS_ERR(buf)); /* -ENOMEM */
  5635. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  5636. if (parent == 0)
  5637. parent = ins.objectid;
  5638. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5639. } else
  5640. BUG_ON(parent > 0);
  5641. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  5642. struct btrfs_delayed_extent_op *extent_op;
  5643. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  5644. BUG_ON(!extent_op); /* -ENOMEM */
  5645. if (key)
  5646. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  5647. else
  5648. memset(&extent_op->key, 0, sizeof(extent_op->key));
  5649. extent_op->flags_to_set = flags;
  5650. extent_op->update_key = 1;
  5651. extent_op->update_flags = 1;
  5652. extent_op->is_data = 0;
  5653. ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
  5654. ins.objectid,
  5655. ins.offset, parent, root_objectid,
  5656. level, BTRFS_ADD_DELAYED_EXTENT,
  5657. extent_op, 0);
  5658. BUG_ON(ret); /* -ENOMEM */
  5659. }
  5660. return buf;
  5661. }
  5662. struct walk_control {
  5663. u64 refs[BTRFS_MAX_LEVEL];
  5664. u64 flags[BTRFS_MAX_LEVEL];
  5665. struct btrfs_key update_progress;
  5666. int stage;
  5667. int level;
  5668. int shared_level;
  5669. int update_ref;
  5670. int keep_locks;
  5671. int reada_slot;
  5672. int reada_count;
  5673. int for_reloc;
  5674. };
  5675. #define DROP_REFERENCE 1
  5676. #define UPDATE_BACKREF 2
  5677. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  5678. struct btrfs_root *root,
  5679. struct walk_control *wc,
  5680. struct btrfs_path *path)
  5681. {
  5682. u64 bytenr;
  5683. u64 generation;
  5684. u64 refs;
  5685. u64 flags;
  5686. u32 nritems;
  5687. u32 blocksize;
  5688. struct btrfs_key key;
  5689. struct extent_buffer *eb;
  5690. int ret;
  5691. int slot;
  5692. int nread = 0;
  5693. if (path->slots[wc->level] < wc->reada_slot) {
  5694. wc->reada_count = wc->reada_count * 2 / 3;
  5695. wc->reada_count = max(wc->reada_count, 2);
  5696. } else {
  5697. wc->reada_count = wc->reada_count * 3 / 2;
  5698. wc->reada_count = min_t(int, wc->reada_count,
  5699. BTRFS_NODEPTRS_PER_BLOCK(root));
  5700. }
  5701. eb = path->nodes[wc->level];
  5702. nritems = btrfs_header_nritems(eb);
  5703. blocksize = btrfs_level_size(root, wc->level - 1);
  5704. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  5705. if (nread >= wc->reada_count)
  5706. break;
  5707. cond_resched();
  5708. bytenr = btrfs_node_blockptr(eb, slot);
  5709. generation = btrfs_node_ptr_generation(eb, slot);
  5710. if (slot == path->slots[wc->level])
  5711. goto reada;
  5712. if (wc->stage == UPDATE_BACKREF &&
  5713. generation <= root->root_key.offset)
  5714. continue;
  5715. /* We don't lock the tree block, it's OK to be racy here */
  5716. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5717. &refs, &flags);
  5718. /* We don't care about errors in readahead. */
  5719. if (ret < 0)
  5720. continue;
  5721. BUG_ON(refs == 0);
  5722. if (wc->stage == DROP_REFERENCE) {
  5723. if (refs == 1)
  5724. goto reada;
  5725. if (wc->level == 1 &&
  5726. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5727. continue;
  5728. if (!wc->update_ref ||
  5729. generation <= root->root_key.offset)
  5730. continue;
  5731. btrfs_node_key_to_cpu(eb, &key, slot);
  5732. ret = btrfs_comp_cpu_keys(&key,
  5733. &wc->update_progress);
  5734. if (ret < 0)
  5735. continue;
  5736. } else {
  5737. if (wc->level == 1 &&
  5738. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5739. continue;
  5740. }
  5741. reada:
  5742. ret = readahead_tree_block(root, bytenr, blocksize,
  5743. generation);
  5744. if (ret)
  5745. break;
  5746. nread++;
  5747. }
  5748. wc->reada_slot = slot;
  5749. }
  5750. /*
  5751. * hepler to process tree block while walking down the tree.
  5752. *
  5753. * when wc->stage == UPDATE_BACKREF, this function updates
  5754. * back refs for pointers in the block.
  5755. *
  5756. * NOTE: return value 1 means we should stop walking down.
  5757. */
  5758. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  5759. struct btrfs_root *root,
  5760. struct btrfs_path *path,
  5761. struct walk_control *wc, int lookup_info)
  5762. {
  5763. int level = wc->level;
  5764. struct extent_buffer *eb = path->nodes[level];
  5765. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5766. int ret;
  5767. if (wc->stage == UPDATE_BACKREF &&
  5768. btrfs_header_owner(eb) != root->root_key.objectid)
  5769. return 1;
  5770. /*
  5771. * when reference count of tree block is 1, it won't increase
  5772. * again. once full backref flag is set, we never clear it.
  5773. */
  5774. if (lookup_info &&
  5775. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  5776. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  5777. BUG_ON(!path->locks[level]);
  5778. ret = btrfs_lookup_extent_info(trans, root,
  5779. eb->start, eb->len,
  5780. &wc->refs[level],
  5781. &wc->flags[level]);
  5782. BUG_ON(ret == -ENOMEM);
  5783. if (ret)
  5784. return ret;
  5785. BUG_ON(wc->refs[level] == 0);
  5786. }
  5787. if (wc->stage == DROP_REFERENCE) {
  5788. if (wc->refs[level] > 1)
  5789. return 1;
  5790. if (path->locks[level] && !wc->keep_locks) {
  5791. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5792. path->locks[level] = 0;
  5793. }
  5794. return 0;
  5795. }
  5796. /* wc->stage == UPDATE_BACKREF */
  5797. if (!(wc->flags[level] & flag)) {
  5798. BUG_ON(!path->locks[level]);
  5799. ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
  5800. BUG_ON(ret); /* -ENOMEM */
  5801. ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
  5802. BUG_ON(ret); /* -ENOMEM */
  5803. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  5804. eb->len, flag, 0);
  5805. BUG_ON(ret); /* -ENOMEM */
  5806. wc->flags[level] |= flag;
  5807. }
  5808. /*
  5809. * the block is shared by multiple trees, so it's not good to
  5810. * keep the tree lock
  5811. */
  5812. if (path->locks[level] && level > 0) {
  5813. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5814. path->locks[level] = 0;
  5815. }
  5816. return 0;
  5817. }
  5818. /*
  5819. * hepler to process tree block pointer.
  5820. *
  5821. * when wc->stage == DROP_REFERENCE, this function checks
  5822. * reference count of the block pointed to. if the block
  5823. * is shared and we need update back refs for the subtree
  5824. * rooted at the block, this function changes wc->stage to
  5825. * UPDATE_BACKREF. if the block is shared and there is no
  5826. * need to update back, this function drops the reference
  5827. * to the block.
  5828. *
  5829. * NOTE: return value 1 means we should stop walking down.
  5830. */
  5831. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  5832. struct btrfs_root *root,
  5833. struct btrfs_path *path,
  5834. struct walk_control *wc, int *lookup_info)
  5835. {
  5836. u64 bytenr;
  5837. u64 generation;
  5838. u64 parent;
  5839. u32 blocksize;
  5840. struct btrfs_key key;
  5841. struct extent_buffer *next;
  5842. int level = wc->level;
  5843. int reada = 0;
  5844. int ret = 0;
  5845. generation = btrfs_node_ptr_generation(path->nodes[level],
  5846. path->slots[level]);
  5847. /*
  5848. * if the lower level block was created before the snapshot
  5849. * was created, we know there is no need to update back refs
  5850. * for the subtree
  5851. */
  5852. if (wc->stage == UPDATE_BACKREF &&
  5853. generation <= root->root_key.offset) {
  5854. *lookup_info = 1;
  5855. return 1;
  5856. }
  5857. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  5858. blocksize = btrfs_level_size(root, level - 1);
  5859. next = btrfs_find_tree_block(root, bytenr, blocksize);
  5860. if (!next) {
  5861. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5862. if (!next)
  5863. return -ENOMEM;
  5864. reada = 1;
  5865. }
  5866. btrfs_tree_lock(next);
  5867. btrfs_set_lock_blocking(next);
  5868. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5869. &wc->refs[level - 1],
  5870. &wc->flags[level - 1]);
  5871. if (ret < 0) {
  5872. btrfs_tree_unlock(next);
  5873. return ret;
  5874. }
  5875. BUG_ON(wc->refs[level - 1] == 0);
  5876. *lookup_info = 0;
  5877. if (wc->stage == DROP_REFERENCE) {
  5878. if (wc->refs[level - 1] > 1) {
  5879. if (level == 1 &&
  5880. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5881. goto skip;
  5882. if (!wc->update_ref ||
  5883. generation <= root->root_key.offset)
  5884. goto skip;
  5885. btrfs_node_key_to_cpu(path->nodes[level], &key,
  5886. path->slots[level]);
  5887. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  5888. if (ret < 0)
  5889. goto skip;
  5890. wc->stage = UPDATE_BACKREF;
  5891. wc->shared_level = level - 1;
  5892. }
  5893. } else {
  5894. if (level == 1 &&
  5895. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5896. goto skip;
  5897. }
  5898. if (!btrfs_buffer_uptodate(next, generation, 0)) {
  5899. btrfs_tree_unlock(next);
  5900. free_extent_buffer(next);
  5901. next = NULL;
  5902. *lookup_info = 1;
  5903. }
  5904. if (!next) {
  5905. if (reada && level == 1)
  5906. reada_walk_down(trans, root, wc, path);
  5907. next = read_tree_block(root, bytenr, blocksize, generation);
  5908. if (!next)
  5909. return -EIO;
  5910. btrfs_tree_lock(next);
  5911. btrfs_set_lock_blocking(next);
  5912. }
  5913. level--;
  5914. BUG_ON(level != btrfs_header_level(next));
  5915. path->nodes[level] = next;
  5916. path->slots[level] = 0;
  5917. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5918. wc->level = level;
  5919. if (wc->level == 1)
  5920. wc->reada_slot = 0;
  5921. return 0;
  5922. skip:
  5923. wc->refs[level - 1] = 0;
  5924. wc->flags[level - 1] = 0;
  5925. if (wc->stage == DROP_REFERENCE) {
  5926. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  5927. parent = path->nodes[level]->start;
  5928. } else {
  5929. BUG_ON(root->root_key.objectid !=
  5930. btrfs_header_owner(path->nodes[level]));
  5931. parent = 0;
  5932. }
  5933. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  5934. root->root_key.objectid, level - 1, 0, 0);
  5935. BUG_ON(ret); /* -ENOMEM */
  5936. }
  5937. btrfs_tree_unlock(next);
  5938. free_extent_buffer(next);
  5939. *lookup_info = 1;
  5940. return 1;
  5941. }
  5942. /*
  5943. * hepler to process tree block while walking up the tree.
  5944. *
  5945. * when wc->stage == DROP_REFERENCE, this function drops
  5946. * reference count on the block.
  5947. *
  5948. * when wc->stage == UPDATE_BACKREF, this function changes
  5949. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  5950. * to UPDATE_BACKREF previously while processing the block.
  5951. *
  5952. * NOTE: return value 1 means we should stop walking up.
  5953. */
  5954. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  5955. struct btrfs_root *root,
  5956. struct btrfs_path *path,
  5957. struct walk_control *wc)
  5958. {
  5959. int ret;
  5960. int level = wc->level;
  5961. struct extent_buffer *eb = path->nodes[level];
  5962. u64 parent = 0;
  5963. if (wc->stage == UPDATE_BACKREF) {
  5964. BUG_ON(wc->shared_level < level);
  5965. if (level < wc->shared_level)
  5966. goto out;
  5967. ret = find_next_key(path, level + 1, &wc->update_progress);
  5968. if (ret > 0)
  5969. wc->update_ref = 0;
  5970. wc->stage = DROP_REFERENCE;
  5971. wc->shared_level = -1;
  5972. path->slots[level] = 0;
  5973. /*
  5974. * check reference count again if the block isn't locked.
  5975. * we should start walking down the tree again if reference
  5976. * count is one.
  5977. */
  5978. if (!path->locks[level]) {
  5979. BUG_ON(level == 0);
  5980. btrfs_tree_lock(eb);
  5981. btrfs_set_lock_blocking(eb);
  5982. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  5983. ret = btrfs_lookup_extent_info(trans, root,
  5984. eb->start, eb->len,
  5985. &wc->refs[level],
  5986. &wc->flags[level]);
  5987. if (ret < 0) {
  5988. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5989. path->locks[level] = 0;
  5990. return ret;
  5991. }
  5992. BUG_ON(wc->refs[level] == 0);
  5993. if (wc->refs[level] == 1) {
  5994. btrfs_tree_unlock_rw(eb, path->locks[level]);
  5995. path->locks[level] = 0;
  5996. return 1;
  5997. }
  5998. }
  5999. }
  6000. /* wc->stage == DROP_REFERENCE */
  6001. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  6002. if (wc->refs[level] == 1) {
  6003. if (level == 0) {
  6004. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6005. ret = btrfs_dec_ref(trans, root, eb, 1,
  6006. wc->for_reloc);
  6007. else
  6008. ret = btrfs_dec_ref(trans, root, eb, 0,
  6009. wc->for_reloc);
  6010. BUG_ON(ret); /* -ENOMEM */
  6011. }
  6012. /* make block locked assertion in clean_tree_block happy */
  6013. if (!path->locks[level] &&
  6014. btrfs_header_generation(eb) == trans->transid) {
  6015. btrfs_tree_lock(eb);
  6016. btrfs_set_lock_blocking(eb);
  6017. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6018. }
  6019. clean_tree_block(trans, root, eb);
  6020. }
  6021. if (eb == root->node) {
  6022. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6023. parent = eb->start;
  6024. else
  6025. BUG_ON(root->root_key.objectid !=
  6026. btrfs_header_owner(eb));
  6027. } else {
  6028. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  6029. parent = path->nodes[level + 1]->start;
  6030. else
  6031. BUG_ON(root->root_key.objectid !=
  6032. btrfs_header_owner(path->nodes[level + 1]));
  6033. }
  6034. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  6035. out:
  6036. wc->refs[level] = 0;
  6037. wc->flags[level] = 0;
  6038. return 0;
  6039. }
  6040. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  6041. struct btrfs_root *root,
  6042. struct btrfs_path *path,
  6043. struct walk_control *wc)
  6044. {
  6045. int level = wc->level;
  6046. int lookup_info = 1;
  6047. int ret;
  6048. while (level >= 0) {
  6049. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  6050. if (ret > 0)
  6051. break;
  6052. if (level == 0)
  6053. break;
  6054. if (path->slots[level] >=
  6055. btrfs_header_nritems(path->nodes[level]))
  6056. break;
  6057. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  6058. if (ret > 0) {
  6059. path->slots[level]++;
  6060. continue;
  6061. } else if (ret < 0)
  6062. return ret;
  6063. level = wc->level;
  6064. }
  6065. return 0;
  6066. }
  6067. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  6068. struct btrfs_root *root,
  6069. struct btrfs_path *path,
  6070. struct walk_control *wc, int max_level)
  6071. {
  6072. int level = wc->level;
  6073. int ret;
  6074. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  6075. while (level < max_level && path->nodes[level]) {
  6076. wc->level = level;
  6077. if (path->slots[level] + 1 <
  6078. btrfs_header_nritems(path->nodes[level])) {
  6079. path->slots[level]++;
  6080. return 0;
  6081. } else {
  6082. ret = walk_up_proc(trans, root, path, wc);
  6083. if (ret > 0)
  6084. return 0;
  6085. if (path->locks[level]) {
  6086. btrfs_tree_unlock_rw(path->nodes[level],
  6087. path->locks[level]);
  6088. path->locks[level] = 0;
  6089. }
  6090. free_extent_buffer(path->nodes[level]);
  6091. path->nodes[level] = NULL;
  6092. level++;
  6093. }
  6094. }
  6095. return 1;
  6096. }
  6097. /*
  6098. * drop a subvolume tree.
  6099. *
  6100. * this function traverses the tree freeing any blocks that only
  6101. * referenced by the tree.
  6102. *
  6103. * when a shared tree block is found. this function decreases its
  6104. * reference count by one. if update_ref is true, this function
  6105. * also make sure backrefs for the shared block and all lower level
  6106. * blocks are properly updated.
  6107. */
  6108. int btrfs_drop_snapshot(struct btrfs_root *root,
  6109. struct btrfs_block_rsv *block_rsv, int update_ref,
  6110. int for_reloc)
  6111. {
  6112. struct btrfs_path *path;
  6113. struct btrfs_trans_handle *trans;
  6114. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6115. struct btrfs_root_item *root_item = &root->root_item;
  6116. struct walk_control *wc;
  6117. struct btrfs_key key;
  6118. int err = 0;
  6119. int ret;
  6120. int level;
  6121. path = btrfs_alloc_path();
  6122. if (!path) {
  6123. err = -ENOMEM;
  6124. goto out;
  6125. }
  6126. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6127. if (!wc) {
  6128. btrfs_free_path(path);
  6129. err = -ENOMEM;
  6130. goto out;
  6131. }
  6132. trans = btrfs_start_transaction(tree_root, 0);
  6133. if (IS_ERR(trans)) {
  6134. err = PTR_ERR(trans);
  6135. goto out_free;
  6136. }
  6137. if (block_rsv)
  6138. trans->block_rsv = block_rsv;
  6139. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  6140. level = btrfs_header_level(root->node);
  6141. path->nodes[level] = btrfs_lock_root_node(root);
  6142. btrfs_set_lock_blocking(path->nodes[level]);
  6143. path->slots[level] = 0;
  6144. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6145. memset(&wc->update_progress, 0,
  6146. sizeof(wc->update_progress));
  6147. } else {
  6148. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  6149. memcpy(&wc->update_progress, &key,
  6150. sizeof(wc->update_progress));
  6151. level = root_item->drop_level;
  6152. BUG_ON(level == 0);
  6153. path->lowest_level = level;
  6154. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6155. path->lowest_level = 0;
  6156. if (ret < 0) {
  6157. err = ret;
  6158. goto out_end_trans;
  6159. }
  6160. WARN_ON(ret > 0);
  6161. /*
  6162. * unlock our path, this is safe because only this
  6163. * function is allowed to delete this snapshot
  6164. */
  6165. btrfs_unlock_up_safe(path, 0);
  6166. level = btrfs_header_level(root->node);
  6167. while (1) {
  6168. btrfs_tree_lock(path->nodes[level]);
  6169. btrfs_set_lock_blocking(path->nodes[level]);
  6170. ret = btrfs_lookup_extent_info(trans, root,
  6171. path->nodes[level]->start,
  6172. path->nodes[level]->len,
  6173. &wc->refs[level],
  6174. &wc->flags[level]);
  6175. if (ret < 0) {
  6176. err = ret;
  6177. goto out_end_trans;
  6178. }
  6179. BUG_ON(wc->refs[level] == 0);
  6180. if (level == root_item->drop_level)
  6181. break;
  6182. btrfs_tree_unlock(path->nodes[level]);
  6183. WARN_ON(wc->refs[level] != 1);
  6184. level--;
  6185. }
  6186. }
  6187. wc->level = level;
  6188. wc->shared_level = -1;
  6189. wc->stage = DROP_REFERENCE;
  6190. wc->update_ref = update_ref;
  6191. wc->keep_locks = 0;
  6192. wc->for_reloc = for_reloc;
  6193. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6194. while (1) {
  6195. ret = walk_down_tree(trans, root, path, wc);
  6196. if (ret < 0) {
  6197. err = ret;
  6198. break;
  6199. }
  6200. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  6201. if (ret < 0) {
  6202. err = ret;
  6203. break;
  6204. }
  6205. if (ret > 0) {
  6206. BUG_ON(wc->stage != DROP_REFERENCE);
  6207. break;
  6208. }
  6209. if (wc->stage == DROP_REFERENCE) {
  6210. level = wc->level;
  6211. btrfs_node_key(path->nodes[level],
  6212. &root_item->drop_progress,
  6213. path->slots[level]);
  6214. root_item->drop_level = level;
  6215. }
  6216. BUG_ON(wc->level == 0);
  6217. if (btrfs_should_end_transaction(trans, tree_root)) {
  6218. ret = btrfs_update_root(trans, tree_root,
  6219. &root->root_key,
  6220. root_item);
  6221. if (ret) {
  6222. btrfs_abort_transaction(trans, tree_root, ret);
  6223. err = ret;
  6224. goto out_end_trans;
  6225. }
  6226. btrfs_end_transaction_throttle(trans, tree_root);
  6227. trans = btrfs_start_transaction(tree_root, 0);
  6228. if (IS_ERR(trans)) {
  6229. err = PTR_ERR(trans);
  6230. goto out_free;
  6231. }
  6232. if (block_rsv)
  6233. trans->block_rsv = block_rsv;
  6234. }
  6235. }
  6236. btrfs_release_path(path);
  6237. if (err)
  6238. goto out_end_trans;
  6239. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  6240. if (ret) {
  6241. btrfs_abort_transaction(trans, tree_root, ret);
  6242. goto out_end_trans;
  6243. }
  6244. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  6245. ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
  6246. NULL, NULL);
  6247. if (ret < 0) {
  6248. btrfs_abort_transaction(trans, tree_root, ret);
  6249. err = ret;
  6250. goto out_end_trans;
  6251. } else if (ret > 0) {
  6252. /* if we fail to delete the orphan item this time
  6253. * around, it'll get picked up the next time.
  6254. *
  6255. * The most common failure here is just -ENOENT.
  6256. */
  6257. btrfs_del_orphan_item(trans, tree_root,
  6258. root->root_key.objectid);
  6259. }
  6260. }
  6261. if (root->in_radix) {
  6262. btrfs_free_fs_root(tree_root->fs_info, root);
  6263. } else {
  6264. free_extent_buffer(root->node);
  6265. free_extent_buffer(root->commit_root);
  6266. kfree(root);
  6267. }
  6268. out_end_trans:
  6269. btrfs_end_transaction_throttle(trans, tree_root);
  6270. out_free:
  6271. kfree(wc);
  6272. btrfs_free_path(path);
  6273. out:
  6274. if (err)
  6275. btrfs_std_error(root->fs_info, err);
  6276. return err;
  6277. }
  6278. /*
  6279. * drop subtree rooted at tree block 'node'.
  6280. *
  6281. * NOTE: this function will unlock and release tree block 'node'
  6282. * only used by relocation code
  6283. */
  6284. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  6285. struct btrfs_root *root,
  6286. struct extent_buffer *node,
  6287. struct extent_buffer *parent)
  6288. {
  6289. struct btrfs_path *path;
  6290. struct walk_control *wc;
  6291. int level;
  6292. int parent_level;
  6293. int ret = 0;
  6294. int wret;
  6295. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6296. path = btrfs_alloc_path();
  6297. if (!path)
  6298. return -ENOMEM;
  6299. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  6300. if (!wc) {
  6301. btrfs_free_path(path);
  6302. return -ENOMEM;
  6303. }
  6304. btrfs_assert_tree_locked(parent);
  6305. parent_level = btrfs_header_level(parent);
  6306. extent_buffer_get(parent);
  6307. path->nodes[parent_level] = parent;
  6308. path->slots[parent_level] = btrfs_header_nritems(parent);
  6309. btrfs_assert_tree_locked(node);
  6310. level = btrfs_header_level(node);
  6311. path->nodes[level] = node;
  6312. path->slots[level] = 0;
  6313. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  6314. wc->refs[parent_level] = 1;
  6315. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  6316. wc->level = level;
  6317. wc->shared_level = -1;
  6318. wc->stage = DROP_REFERENCE;
  6319. wc->update_ref = 0;
  6320. wc->keep_locks = 1;
  6321. wc->for_reloc = 1;
  6322. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  6323. while (1) {
  6324. wret = walk_down_tree(trans, root, path, wc);
  6325. if (wret < 0) {
  6326. ret = wret;
  6327. break;
  6328. }
  6329. wret = walk_up_tree(trans, root, path, wc, parent_level);
  6330. if (wret < 0)
  6331. ret = wret;
  6332. if (wret != 0)
  6333. break;
  6334. }
  6335. kfree(wc);
  6336. btrfs_free_path(path);
  6337. return ret;
  6338. }
  6339. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  6340. {
  6341. u64 num_devices;
  6342. u64 stripped;
  6343. /*
  6344. * if restripe for this chunk_type is on pick target profile and
  6345. * return, otherwise do the usual balance
  6346. */
  6347. stripped = get_restripe_target(root->fs_info, flags);
  6348. if (stripped)
  6349. return extended_to_chunk(stripped);
  6350. /*
  6351. * we add in the count of missing devices because we want
  6352. * to make sure that any RAID levels on a degraded FS
  6353. * continue to be honored.
  6354. */
  6355. num_devices = root->fs_info->fs_devices->rw_devices +
  6356. root->fs_info->fs_devices->missing_devices;
  6357. stripped = BTRFS_BLOCK_GROUP_RAID0 |
  6358. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  6359. if (num_devices == 1) {
  6360. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6361. stripped = flags & ~stripped;
  6362. /* turn raid0 into single device chunks */
  6363. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  6364. return stripped;
  6365. /* turn mirroring into duplication */
  6366. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6367. BTRFS_BLOCK_GROUP_RAID10))
  6368. return stripped | BTRFS_BLOCK_GROUP_DUP;
  6369. } else {
  6370. /* they already had raid on here, just return */
  6371. if (flags & stripped)
  6372. return flags;
  6373. stripped |= BTRFS_BLOCK_GROUP_DUP;
  6374. stripped = flags & ~stripped;
  6375. /* switch duplicated blocks with raid1 */
  6376. if (flags & BTRFS_BLOCK_GROUP_DUP)
  6377. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  6378. /* this is drive concat, leave it alone */
  6379. }
  6380. return flags;
  6381. }
  6382. static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
  6383. {
  6384. struct btrfs_space_info *sinfo = cache->space_info;
  6385. u64 num_bytes;
  6386. u64 min_allocable_bytes;
  6387. int ret = -ENOSPC;
  6388. /*
  6389. * We need some metadata space and system metadata space for
  6390. * allocating chunks in some corner cases until we force to set
  6391. * it to be readonly.
  6392. */
  6393. if ((sinfo->flags &
  6394. (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
  6395. !force)
  6396. min_allocable_bytes = 1 * 1024 * 1024;
  6397. else
  6398. min_allocable_bytes = 0;
  6399. spin_lock(&sinfo->lock);
  6400. spin_lock(&cache->lock);
  6401. if (cache->ro) {
  6402. ret = 0;
  6403. goto out;
  6404. }
  6405. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6406. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6407. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  6408. sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
  6409. min_allocable_bytes <= sinfo->total_bytes) {
  6410. sinfo->bytes_readonly += num_bytes;
  6411. cache->ro = 1;
  6412. ret = 0;
  6413. }
  6414. out:
  6415. spin_unlock(&cache->lock);
  6416. spin_unlock(&sinfo->lock);
  6417. return ret;
  6418. }
  6419. int btrfs_set_block_group_ro(struct btrfs_root *root,
  6420. struct btrfs_block_group_cache *cache)
  6421. {
  6422. struct btrfs_trans_handle *trans;
  6423. u64 alloc_flags;
  6424. int ret;
  6425. BUG_ON(cache->ro);
  6426. trans = btrfs_join_transaction(root);
  6427. if (IS_ERR(trans))
  6428. return PTR_ERR(trans);
  6429. alloc_flags = update_block_group_flags(root, cache->flags);
  6430. if (alloc_flags != cache->flags) {
  6431. ret = do_chunk_alloc(trans, root, alloc_flags,
  6432. CHUNK_ALLOC_FORCE);
  6433. if (ret < 0)
  6434. goto out;
  6435. }
  6436. ret = set_block_group_ro(cache, 0);
  6437. if (!ret)
  6438. goto out;
  6439. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  6440. ret = do_chunk_alloc(trans, root, alloc_flags,
  6441. CHUNK_ALLOC_FORCE);
  6442. if (ret < 0)
  6443. goto out;
  6444. ret = set_block_group_ro(cache, 0);
  6445. out:
  6446. btrfs_end_transaction(trans, root);
  6447. return ret;
  6448. }
  6449. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  6450. struct btrfs_root *root, u64 type)
  6451. {
  6452. u64 alloc_flags = get_alloc_profile(root, type);
  6453. return do_chunk_alloc(trans, root, alloc_flags,
  6454. CHUNK_ALLOC_FORCE);
  6455. }
  6456. /*
  6457. * helper to account the unused space of all the readonly block group in the
  6458. * list. takes mirrors into account.
  6459. */
  6460. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  6461. {
  6462. struct btrfs_block_group_cache *block_group;
  6463. u64 free_bytes = 0;
  6464. int factor;
  6465. list_for_each_entry(block_group, groups_list, list) {
  6466. spin_lock(&block_group->lock);
  6467. if (!block_group->ro) {
  6468. spin_unlock(&block_group->lock);
  6469. continue;
  6470. }
  6471. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  6472. BTRFS_BLOCK_GROUP_RAID10 |
  6473. BTRFS_BLOCK_GROUP_DUP))
  6474. factor = 2;
  6475. else
  6476. factor = 1;
  6477. free_bytes += (block_group->key.offset -
  6478. btrfs_block_group_used(&block_group->item)) *
  6479. factor;
  6480. spin_unlock(&block_group->lock);
  6481. }
  6482. return free_bytes;
  6483. }
  6484. /*
  6485. * helper to account the unused space of all the readonly block group in the
  6486. * space_info. takes mirrors into account.
  6487. */
  6488. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  6489. {
  6490. int i;
  6491. u64 free_bytes = 0;
  6492. spin_lock(&sinfo->lock);
  6493. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  6494. if (!list_empty(&sinfo->block_groups[i]))
  6495. free_bytes += __btrfs_get_ro_block_group_free_space(
  6496. &sinfo->block_groups[i]);
  6497. spin_unlock(&sinfo->lock);
  6498. return free_bytes;
  6499. }
  6500. void btrfs_set_block_group_rw(struct btrfs_root *root,
  6501. struct btrfs_block_group_cache *cache)
  6502. {
  6503. struct btrfs_space_info *sinfo = cache->space_info;
  6504. u64 num_bytes;
  6505. BUG_ON(!cache->ro);
  6506. spin_lock(&sinfo->lock);
  6507. spin_lock(&cache->lock);
  6508. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  6509. cache->bytes_super - btrfs_block_group_used(&cache->item);
  6510. sinfo->bytes_readonly -= num_bytes;
  6511. cache->ro = 0;
  6512. spin_unlock(&cache->lock);
  6513. spin_unlock(&sinfo->lock);
  6514. }
  6515. /*
  6516. * checks to see if its even possible to relocate this block group.
  6517. *
  6518. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  6519. * ok to go ahead and try.
  6520. */
  6521. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  6522. {
  6523. struct btrfs_block_group_cache *block_group;
  6524. struct btrfs_space_info *space_info;
  6525. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  6526. struct btrfs_device *device;
  6527. u64 min_free;
  6528. u64 dev_min = 1;
  6529. u64 dev_nr = 0;
  6530. u64 target;
  6531. int index;
  6532. int full = 0;
  6533. int ret = 0;
  6534. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  6535. /* odd, couldn't find the block group, leave it alone */
  6536. if (!block_group)
  6537. return -1;
  6538. min_free = btrfs_block_group_used(&block_group->item);
  6539. /* no bytes used, we're good */
  6540. if (!min_free)
  6541. goto out;
  6542. space_info = block_group->space_info;
  6543. spin_lock(&space_info->lock);
  6544. full = space_info->full;
  6545. /*
  6546. * if this is the last block group we have in this space, we can't
  6547. * relocate it unless we're able to allocate a new chunk below.
  6548. *
  6549. * Otherwise, we need to make sure we have room in the space to handle
  6550. * all of the extents from this block group. If we can, we're good
  6551. */
  6552. if ((space_info->total_bytes != block_group->key.offset) &&
  6553. (space_info->bytes_used + space_info->bytes_reserved +
  6554. space_info->bytes_pinned + space_info->bytes_readonly +
  6555. min_free < space_info->total_bytes)) {
  6556. spin_unlock(&space_info->lock);
  6557. goto out;
  6558. }
  6559. spin_unlock(&space_info->lock);
  6560. /*
  6561. * ok we don't have enough space, but maybe we have free space on our
  6562. * devices to allocate new chunks for relocation, so loop through our
  6563. * alloc devices and guess if we have enough space. if this block
  6564. * group is going to be restriped, run checks against the target
  6565. * profile instead of the current one.
  6566. */
  6567. ret = -1;
  6568. /*
  6569. * index:
  6570. * 0: raid10
  6571. * 1: raid1
  6572. * 2: dup
  6573. * 3: raid0
  6574. * 4: single
  6575. */
  6576. target = get_restripe_target(root->fs_info, block_group->flags);
  6577. if (target) {
  6578. index = __get_raid_index(extended_to_chunk(target));
  6579. } else {
  6580. /*
  6581. * this is just a balance, so if we were marked as full
  6582. * we know there is no space for a new chunk
  6583. */
  6584. if (full)
  6585. goto out;
  6586. index = get_block_group_index(block_group);
  6587. }
  6588. if (index == 0) {
  6589. dev_min = 4;
  6590. /* Divide by 2 */
  6591. min_free >>= 1;
  6592. } else if (index == 1) {
  6593. dev_min = 2;
  6594. } else if (index == 2) {
  6595. /* Multiply by 2 */
  6596. min_free <<= 1;
  6597. } else if (index == 3) {
  6598. dev_min = fs_devices->rw_devices;
  6599. do_div(min_free, dev_min);
  6600. }
  6601. mutex_lock(&root->fs_info->chunk_mutex);
  6602. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  6603. u64 dev_offset;
  6604. /*
  6605. * check to make sure we can actually find a chunk with enough
  6606. * space to fit our block group in.
  6607. */
  6608. if (device->total_bytes > device->bytes_used + min_free &&
  6609. !device->is_tgtdev_for_dev_replace) {
  6610. ret = find_free_dev_extent(device, min_free,
  6611. &dev_offset, NULL);
  6612. if (!ret)
  6613. dev_nr++;
  6614. if (dev_nr >= dev_min)
  6615. break;
  6616. ret = -1;
  6617. }
  6618. }
  6619. mutex_unlock(&root->fs_info->chunk_mutex);
  6620. out:
  6621. btrfs_put_block_group(block_group);
  6622. return ret;
  6623. }
  6624. static int find_first_block_group(struct btrfs_root *root,
  6625. struct btrfs_path *path, struct btrfs_key *key)
  6626. {
  6627. int ret = 0;
  6628. struct btrfs_key found_key;
  6629. struct extent_buffer *leaf;
  6630. int slot;
  6631. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  6632. if (ret < 0)
  6633. goto out;
  6634. while (1) {
  6635. slot = path->slots[0];
  6636. leaf = path->nodes[0];
  6637. if (slot >= btrfs_header_nritems(leaf)) {
  6638. ret = btrfs_next_leaf(root, path);
  6639. if (ret == 0)
  6640. continue;
  6641. if (ret < 0)
  6642. goto out;
  6643. break;
  6644. }
  6645. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6646. if (found_key.objectid >= key->objectid &&
  6647. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  6648. ret = 0;
  6649. goto out;
  6650. }
  6651. path->slots[0]++;
  6652. }
  6653. out:
  6654. return ret;
  6655. }
  6656. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  6657. {
  6658. struct btrfs_block_group_cache *block_group;
  6659. u64 last = 0;
  6660. while (1) {
  6661. struct inode *inode;
  6662. block_group = btrfs_lookup_first_block_group(info, last);
  6663. while (block_group) {
  6664. spin_lock(&block_group->lock);
  6665. if (block_group->iref)
  6666. break;
  6667. spin_unlock(&block_group->lock);
  6668. block_group = next_block_group(info->tree_root,
  6669. block_group);
  6670. }
  6671. if (!block_group) {
  6672. if (last == 0)
  6673. break;
  6674. last = 0;
  6675. continue;
  6676. }
  6677. inode = block_group->inode;
  6678. block_group->iref = 0;
  6679. block_group->inode = NULL;
  6680. spin_unlock(&block_group->lock);
  6681. iput(inode);
  6682. last = block_group->key.objectid + block_group->key.offset;
  6683. btrfs_put_block_group(block_group);
  6684. }
  6685. }
  6686. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  6687. {
  6688. struct btrfs_block_group_cache *block_group;
  6689. struct btrfs_space_info *space_info;
  6690. struct btrfs_caching_control *caching_ctl;
  6691. struct rb_node *n;
  6692. down_write(&info->extent_commit_sem);
  6693. while (!list_empty(&info->caching_block_groups)) {
  6694. caching_ctl = list_entry(info->caching_block_groups.next,
  6695. struct btrfs_caching_control, list);
  6696. list_del(&caching_ctl->list);
  6697. put_caching_control(caching_ctl);
  6698. }
  6699. up_write(&info->extent_commit_sem);
  6700. spin_lock(&info->block_group_cache_lock);
  6701. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  6702. block_group = rb_entry(n, struct btrfs_block_group_cache,
  6703. cache_node);
  6704. rb_erase(&block_group->cache_node,
  6705. &info->block_group_cache_tree);
  6706. spin_unlock(&info->block_group_cache_lock);
  6707. down_write(&block_group->space_info->groups_sem);
  6708. list_del(&block_group->list);
  6709. up_write(&block_group->space_info->groups_sem);
  6710. if (block_group->cached == BTRFS_CACHE_STARTED)
  6711. wait_block_group_cache_done(block_group);
  6712. /*
  6713. * We haven't cached this block group, which means we could
  6714. * possibly have excluded extents on this block group.
  6715. */
  6716. if (block_group->cached == BTRFS_CACHE_NO)
  6717. free_excluded_extents(info->extent_root, block_group);
  6718. btrfs_remove_free_space_cache(block_group);
  6719. btrfs_put_block_group(block_group);
  6720. spin_lock(&info->block_group_cache_lock);
  6721. }
  6722. spin_unlock(&info->block_group_cache_lock);
  6723. /* now that all the block groups are freed, go through and
  6724. * free all the space_info structs. This is only called during
  6725. * the final stages of unmount, and so we know nobody is
  6726. * using them. We call synchronize_rcu() once before we start,
  6727. * just to be on the safe side.
  6728. */
  6729. synchronize_rcu();
  6730. release_global_block_rsv(info);
  6731. while(!list_empty(&info->space_info)) {
  6732. space_info = list_entry(info->space_info.next,
  6733. struct btrfs_space_info,
  6734. list);
  6735. if (space_info->bytes_pinned > 0 ||
  6736. space_info->bytes_reserved > 0 ||
  6737. space_info->bytes_may_use > 0) {
  6738. WARN_ON(1);
  6739. dump_space_info(space_info, 0, 0);
  6740. }
  6741. list_del(&space_info->list);
  6742. kfree(space_info);
  6743. }
  6744. return 0;
  6745. }
  6746. static void __link_block_group(struct btrfs_space_info *space_info,
  6747. struct btrfs_block_group_cache *cache)
  6748. {
  6749. int index = get_block_group_index(cache);
  6750. down_write(&space_info->groups_sem);
  6751. list_add_tail(&cache->list, &space_info->block_groups[index]);
  6752. up_write(&space_info->groups_sem);
  6753. }
  6754. int btrfs_read_block_groups(struct btrfs_root *root)
  6755. {
  6756. struct btrfs_path *path;
  6757. int ret;
  6758. struct btrfs_block_group_cache *cache;
  6759. struct btrfs_fs_info *info = root->fs_info;
  6760. struct btrfs_space_info *space_info;
  6761. struct btrfs_key key;
  6762. struct btrfs_key found_key;
  6763. struct extent_buffer *leaf;
  6764. int need_clear = 0;
  6765. u64 cache_gen;
  6766. root = info->extent_root;
  6767. key.objectid = 0;
  6768. key.offset = 0;
  6769. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  6770. path = btrfs_alloc_path();
  6771. if (!path)
  6772. return -ENOMEM;
  6773. path->reada = 1;
  6774. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  6775. if (btrfs_test_opt(root, SPACE_CACHE) &&
  6776. btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
  6777. need_clear = 1;
  6778. if (btrfs_test_opt(root, CLEAR_CACHE))
  6779. need_clear = 1;
  6780. while (1) {
  6781. ret = find_first_block_group(root, path, &key);
  6782. if (ret > 0)
  6783. break;
  6784. if (ret != 0)
  6785. goto error;
  6786. leaf = path->nodes[0];
  6787. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6788. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6789. if (!cache) {
  6790. ret = -ENOMEM;
  6791. goto error;
  6792. }
  6793. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  6794. GFP_NOFS);
  6795. if (!cache->free_space_ctl) {
  6796. kfree(cache);
  6797. ret = -ENOMEM;
  6798. goto error;
  6799. }
  6800. atomic_set(&cache->count, 1);
  6801. spin_lock_init(&cache->lock);
  6802. cache->fs_info = info;
  6803. INIT_LIST_HEAD(&cache->list);
  6804. INIT_LIST_HEAD(&cache->cluster_list);
  6805. if (need_clear) {
  6806. /*
  6807. * When we mount with old space cache, we need to
  6808. * set BTRFS_DC_CLEAR and set dirty flag.
  6809. *
  6810. * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
  6811. * truncate the old free space cache inode and
  6812. * setup a new one.
  6813. * b) Setting 'dirty flag' makes sure that we flush
  6814. * the new space cache info onto disk.
  6815. */
  6816. cache->disk_cache_state = BTRFS_DC_CLEAR;
  6817. if (btrfs_test_opt(root, SPACE_CACHE))
  6818. cache->dirty = 1;
  6819. }
  6820. read_extent_buffer(leaf, &cache->item,
  6821. btrfs_item_ptr_offset(leaf, path->slots[0]),
  6822. sizeof(cache->item));
  6823. memcpy(&cache->key, &found_key, sizeof(found_key));
  6824. key.objectid = found_key.objectid + found_key.offset;
  6825. btrfs_release_path(path);
  6826. cache->flags = btrfs_block_group_flags(&cache->item);
  6827. cache->sectorsize = root->sectorsize;
  6828. btrfs_init_free_space_ctl(cache);
  6829. /*
  6830. * We need to exclude the super stripes now so that the space
  6831. * info has super bytes accounted for, otherwise we'll think
  6832. * we have more space than we actually do.
  6833. */
  6834. exclude_super_stripes(root, cache);
  6835. /*
  6836. * check for two cases, either we are full, and therefore
  6837. * don't need to bother with the caching work since we won't
  6838. * find any space, or we are empty, and we can just add all
  6839. * the space in and be done with it. This saves us _alot_ of
  6840. * time, particularly in the full case.
  6841. */
  6842. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  6843. cache->last_byte_to_unpin = (u64)-1;
  6844. cache->cached = BTRFS_CACHE_FINISHED;
  6845. free_excluded_extents(root, cache);
  6846. } else if (btrfs_block_group_used(&cache->item) == 0) {
  6847. cache->last_byte_to_unpin = (u64)-1;
  6848. cache->cached = BTRFS_CACHE_FINISHED;
  6849. add_new_free_space(cache, root->fs_info,
  6850. found_key.objectid,
  6851. found_key.objectid +
  6852. found_key.offset);
  6853. free_excluded_extents(root, cache);
  6854. }
  6855. ret = update_space_info(info, cache->flags, found_key.offset,
  6856. btrfs_block_group_used(&cache->item),
  6857. &space_info);
  6858. BUG_ON(ret); /* -ENOMEM */
  6859. cache->space_info = space_info;
  6860. spin_lock(&cache->space_info->lock);
  6861. cache->space_info->bytes_readonly += cache->bytes_super;
  6862. spin_unlock(&cache->space_info->lock);
  6863. __link_block_group(space_info, cache);
  6864. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6865. BUG_ON(ret); /* Logic error */
  6866. set_avail_alloc_bits(root->fs_info, cache->flags);
  6867. if (btrfs_chunk_readonly(root, cache->key.objectid))
  6868. set_block_group_ro(cache, 1);
  6869. }
  6870. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  6871. if (!(get_alloc_profile(root, space_info->flags) &
  6872. (BTRFS_BLOCK_GROUP_RAID10 |
  6873. BTRFS_BLOCK_GROUP_RAID1 |
  6874. BTRFS_BLOCK_GROUP_DUP)))
  6875. continue;
  6876. /*
  6877. * avoid allocating from un-mirrored block group if there are
  6878. * mirrored block groups.
  6879. */
  6880. list_for_each_entry(cache, &space_info->block_groups[3], list)
  6881. set_block_group_ro(cache, 1);
  6882. list_for_each_entry(cache, &space_info->block_groups[4], list)
  6883. set_block_group_ro(cache, 1);
  6884. }
  6885. init_global_block_rsv(info);
  6886. ret = 0;
  6887. error:
  6888. btrfs_free_path(path);
  6889. return ret;
  6890. }
  6891. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
  6892. struct btrfs_root *root)
  6893. {
  6894. struct btrfs_block_group_cache *block_group, *tmp;
  6895. struct btrfs_root *extent_root = root->fs_info->extent_root;
  6896. struct btrfs_block_group_item item;
  6897. struct btrfs_key key;
  6898. int ret = 0;
  6899. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
  6900. new_bg_list) {
  6901. list_del_init(&block_group->new_bg_list);
  6902. if (ret)
  6903. continue;
  6904. spin_lock(&block_group->lock);
  6905. memcpy(&item, &block_group->item, sizeof(item));
  6906. memcpy(&key, &block_group->key, sizeof(key));
  6907. spin_unlock(&block_group->lock);
  6908. ret = btrfs_insert_item(trans, extent_root, &key, &item,
  6909. sizeof(item));
  6910. if (ret)
  6911. btrfs_abort_transaction(trans, extent_root, ret);
  6912. }
  6913. }
  6914. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  6915. struct btrfs_root *root, u64 bytes_used,
  6916. u64 type, u64 chunk_objectid, u64 chunk_offset,
  6917. u64 size)
  6918. {
  6919. int ret;
  6920. struct btrfs_root *extent_root;
  6921. struct btrfs_block_group_cache *cache;
  6922. extent_root = root->fs_info->extent_root;
  6923. root->fs_info->last_trans_log_full_commit = trans->transid;
  6924. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6925. if (!cache)
  6926. return -ENOMEM;
  6927. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  6928. GFP_NOFS);
  6929. if (!cache->free_space_ctl) {
  6930. kfree(cache);
  6931. return -ENOMEM;
  6932. }
  6933. cache->key.objectid = chunk_offset;
  6934. cache->key.offset = size;
  6935. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  6936. cache->sectorsize = root->sectorsize;
  6937. cache->fs_info = root->fs_info;
  6938. atomic_set(&cache->count, 1);
  6939. spin_lock_init(&cache->lock);
  6940. INIT_LIST_HEAD(&cache->list);
  6941. INIT_LIST_HEAD(&cache->cluster_list);
  6942. INIT_LIST_HEAD(&cache->new_bg_list);
  6943. btrfs_init_free_space_ctl(cache);
  6944. btrfs_set_block_group_used(&cache->item, bytes_used);
  6945. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  6946. cache->flags = type;
  6947. btrfs_set_block_group_flags(&cache->item, type);
  6948. cache->last_byte_to_unpin = (u64)-1;
  6949. cache->cached = BTRFS_CACHE_FINISHED;
  6950. exclude_super_stripes(root, cache);
  6951. add_new_free_space(cache, root->fs_info, chunk_offset,
  6952. chunk_offset + size);
  6953. free_excluded_extents(root, cache);
  6954. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  6955. &cache->space_info);
  6956. BUG_ON(ret); /* -ENOMEM */
  6957. update_global_block_rsv(root->fs_info);
  6958. spin_lock(&cache->space_info->lock);
  6959. cache->space_info->bytes_readonly += cache->bytes_super;
  6960. spin_unlock(&cache->space_info->lock);
  6961. __link_block_group(cache->space_info, cache);
  6962. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6963. BUG_ON(ret); /* Logic error */
  6964. list_add_tail(&cache->new_bg_list, &trans->new_bgs);
  6965. set_avail_alloc_bits(extent_root->fs_info, type);
  6966. return 0;
  6967. }
  6968. static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  6969. {
  6970. u64 extra_flags = chunk_to_extended(flags) &
  6971. BTRFS_EXTENDED_PROFILE_MASK;
  6972. if (flags & BTRFS_BLOCK_GROUP_DATA)
  6973. fs_info->avail_data_alloc_bits &= ~extra_flags;
  6974. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  6975. fs_info->avail_metadata_alloc_bits &= ~extra_flags;
  6976. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  6977. fs_info->avail_system_alloc_bits &= ~extra_flags;
  6978. }
  6979. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  6980. struct btrfs_root *root, u64 group_start)
  6981. {
  6982. struct btrfs_path *path;
  6983. struct btrfs_block_group_cache *block_group;
  6984. struct btrfs_free_cluster *cluster;
  6985. struct btrfs_root *tree_root = root->fs_info->tree_root;
  6986. struct btrfs_key key;
  6987. struct inode *inode;
  6988. int ret;
  6989. int index;
  6990. int factor;
  6991. root = root->fs_info->extent_root;
  6992. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  6993. BUG_ON(!block_group);
  6994. BUG_ON(!block_group->ro);
  6995. /*
  6996. * Free the reserved super bytes from this block group before
  6997. * remove it.
  6998. */
  6999. free_excluded_extents(root, block_group);
  7000. memcpy(&key, &block_group->key, sizeof(key));
  7001. index = get_block_group_index(block_group);
  7002. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7003. BTRFS_BLOCK_GROUP_RAID1 |
  7004. BTRFS_BLOCK_GROUP_RAID10))
  7005. factor = 2;
  7006. else
  7007. factor = 1;
  7008. /* make sure this block group isn't part of an allocation cluster */
  7009. cluster = &root->fs_info->data_alloc_cluster;
  7010. spin_lock(&cluster->refill_lock);
  7011. btrfs_return_cluster_to_free_space(block_group, cluster);
  7012. spin_unlock(&cluster->refill_lock);
  7013. /*
  7014. * make sure this block group isn't part of a metadata
  7015. * allocation cluster
  7016. */
  7017. cluster = &root->fs_info->meta_alloc_cluster;
  7018. spin_lock(&cluster->refill_lock);
  7019. btrfs_return_cluster_to_free_space(block_group, cluster);
  7020. spin_unlock(&cluster->refill_lock);
  7021. path = btrfs_alloc_path();
  7022. if (!path) {
  7023. ret = -ENOMEM;
  7024. goto out;
  7025. }
  7026. inode = lookup_free_space_inode(tree_root, block_group, path);
  7027. if (!IS_ERR(inode)) {
  7028. ret = btrfs_orphan_add(trans, inode);
  7029. if (ret) {
  7030. btrfs_add_delayed_iput(inode);
  7031. goto out;
  7032. }
  7033. clear_nlink(inode);
  7034. /* One for the block groups ref */
  7035. spin_lock(&block_group->lock);
  7036. if (block_group->iref) {
  7037. block_group->iref = 0;
  7038. block_group->inode = NULL;
  7039. spin_unlock(&block_group->lock);
  7040. iput(inode);
  7041. } else {
  7042. spin_unlock(&block_group->lock);
  7043. }
  7044. /* One for our lookup ref */
  7045. btrfs_add_delayed_iput(inode);
  7046. }
  7047. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7048. key.offset = block_group->key.objectid;
  7049. key.type = 0;
  7050. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7051. if (ret < 0)
  7052. goto out;
  7053. if (ret > 0)
  7054. btrfs_release_path(path);
  7055. if (ret == 0) {
  7056. ret = btrfs_del_item(trans, tree_root, path);
  7057. if (ret)
  7058. goto out;
  7059. btrfs_release_path(path);
  7060. }
  7061. spin_lock(&root->fs_info->block_group_cache_lock);
  7062. rb_erase(&block_group->cache_node,
  7063. &root->fs_info->block_group_cache_tree);
  7064. spin_unlock(&root->fs_info->block_group_cache_lock);
  7065. down_write(&block_group->space_info->groups_sem);
  7066. /*
  7067. * we must use list_del_init so people can check to see if they
  7068. * are still on the list after taking the semaphore
  7069. */
  7070. list_del_init(&block_group->list);
  7071. if (list_empty(&block_group->space_info->block_groups[index]))
  7072. clear_avail_alloc_bits(root->fs_info, block_group->flags);
  7073. up_write(&block_group->space_info->groups_sem);
  7074. if (block_group->cached == BTRFS_CACHE_STARTED)
  7075. wait_block_group_cache_done(block_group);
  7076. btrfs_remove_free_space_cache(block_group);
  7077. spin_lock(&block_group->space_info->lock);
  7078. block_group->space_info->total_bytes -= block_group->key.offset;
  7079. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7080. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7081. spin_unlock(&block_group->space_info->lock);
  7082. memcpy(&key, &block_group->key, sizeof(key));
  7083. btrfs_clear_space_info_full(root->fs_info);
  7084. btrfs_put_block_group(block_group);
  7085. btrfs_put_block_group(block_group);
  7086. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7087. if (ret > 0)
  7088. ret = -EIO;
  7089. if (ret < 0)
  7090. goto out;
  7091. ret = btrfs_del_item(trans, root, path);
  7092. out:
  7093. btrfs_free_path(path);
  7094. return ret;
  7095. }
  7096. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  7097. {
  7098. struct btrfs_space_info *space_info;
  7099. struct btrfs_super_block *disk_super;
  7100. u64 features;
  7101. u64 flags;
  7102. int mixed = 0;
  7103. int ret;
  7104. disk_super = fs_info->super_copy;
  7105. if (!btrfs_super_root(disk_super))
  7106. return 1;
  7107. features = btrfs_super_incompat_flags(disk_super);
  7108. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  7109. mixed = 1;
  7110. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  7111. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7112. if (ret)
  7113. goto out;
  7114. if (mixed) {
  7115. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  7116. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7117. } else {
  7118. flags = BTRFS_BLOCK_GROUP_METADATA;
  7119. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7120. if (ret)
  7121. goto out;
  7122. flags = BTRFS_BLOCK_GROUP_DATA;
  7123. ret = update_space_info(fs_info, flags, 0, 0, &space_info);
  7124. }
  7125. out:
  7126. return ret;
  7127. }
  7128. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  7129. {
  7130. return unpin_extent_range(root, start, end);
  7131. }
  7132. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  7133. u64 num_bytes, u64 *actual_bytes)
  7134. {
  7135. return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
  7136. }
  7137. int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
  7138. {
  7139. struct btrfs_fs_info *fs_info = root->fs_info;
  7140. struct btrfs_block_group_cache *cache = NULL;
  7141. u64 group_trimmed;
  7142. u64 start;
  7143. u64 end;
  7144. u64 trimmed = 0;
  7145. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  7146. int ret = 0;
  7147. /*
  7148. * try to trim all FS space, our block group may start from non-zero.
  7149. */
  7150. if (range->len == total_bytes)
  7151. cache = btrfs_lookup_first_block_group(fs_info, range->start);
  7152. else
  7153. cache = btrfs_lookup_block_group(fs_info, range->start);
  7154. while (cache) {
  7155. if (cache->key.objectid >= (range->start + range->len)) {
  7156. btrfs_put_block_group(cache);
  7157. break;
  7158. }
  7159. start = max(range->start, cache->key.objectid);
  7160. end = min(range->start + range->len,
  7161. cache->key.objectid + cache->key.offset);
  7162. if (end - start >= range->minlen) {
  7163. if (!block_group_cache_done(cache)) {
  7164. ret = cache_block_group(cache, NULL, root, 0);
  7165. if (!ret)
  7166. wait_block_group_cache_done(cache);
  7167. }
  7168. ret = btrfs_trim_block_group(cache,
  7169. &group_trimmed,
  7170. start,
  7171. end,
  7172. range->minlen);
  7173. trimmed += group_trimmed;
  7174. if (ret) {
  7175. btrfs_put_block_group(cache);
  7176. break;
  7177. }
  7178. }
  7179. cache = next_block_group(fs_info->tree_root, cache);
  7180. }
  7181. range->len = trimmed;
  7182. return ret;
  7183. }