extent-tree.c 209 KB

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