extent-tree.c 229 KB

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