extent-tree.c 213 KB

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