extent-tree.c 216 KB

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