extent-tree.c 229 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773
  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_NOFS);
  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_NOFS, 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. /*
  4187. * Deleting the buffer, clear the corrupt flag since it doesn't matter
  4188. * anymore.
  4189. */
  4190. clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
  4191. btrfs_put_block_group(cache);
  4192. }
  4193. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  4194. struct btrfs_root *root,
  4195. u64 bytenr, u64 num_bytes, u64 parent,
  4196. u64 root_objectid, u64 owner, u64 offset)
  4197. {
  4198. int ret;
  4199. /*
  4200. * tree log blocks never actually go into the extent allocation
  4201. * tree, just update pinning info and exit early.
  4202. */
  4203. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  4204. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  4205. /* unlocks the pinned mutex */
  4206. btrfs_pin_extent(root, bytenr, num_bytes, 1);
  4207. ret = 0;
  4208. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  4209. ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
  4210. parent, root_objectid, (int)owner,
  4211. BTRFS_DROP_DELAYED_REF, NULL);
  4212. BUG_ON(ret);
  4213. } else {
  4214. ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
  4215. parent, root_objectid, owner,
  4216. offset, BTRFS_DROP_DELAYED_REF, NULL);
  4217. BUG_ON(ret);
  4218. }
  4219. return ret;
  4220. }
  4221. static u64 stripe_align(struct btrfs_root *root, u64 val)
  4222. {
  4223. u64 mask = ((u64)root->stripesize - 1);
  4224. u64 ret = (val + mask) & ~mask;
  4225. return ret;
  4226. }
  4227. /*
  4228. * when we wait for progress in the block group caching, its because
  4229. * our allocation attempt failed at least once. So, we must sleep
  4230. * and let some progress happen before we try again.
  4231. *
  4232. * This function will sleep at least once waiting for new free space to
  4233. * show up, and then it will check the block group free space numbers
  4234. * for our min num_bytes. Another option is to have it go ahead
  4235. * and look in the rbtree for a free extent of a given size, but this
  4236. * is a good start.
  4237. */
  4238. static noinline int
  4239. wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
  4240. u64 num_bytes)
  4241. {
  4242. struct btrfs_caching_control *caching_ctl;
  4243. DEFINE_WAIT(wait);
  4244. caching_ctl = get_caching_control(cache);
  4245. if (!caching_ctl)
  4246. return 0;
  4247. wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
  4248. (cache->free_space >= num_bytes));
  4249. put_caching_control(caching_ctl);
  4250. return 0;
  4251. }
  4252. static noinline int
  4253. wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
  4254. {
  4255. struct btrfs_caching_control *caching_ctl;
  4256. DEFINE_WAIT(wait);
  4257. caching_ctl = get_caching_control(cache);
  4258. if (!caching_ctl)
  4259. return 0;
  4260. wait_event(caching_ctl->wait, block_group_cache_done(cache));
  4261. put_caching_control(caching_ctl);
  4262. return 0;
  4263. }
  4264. static int get_block_group_index(struct btrfs_block_group_cache *cache)
  4265. {
  4266. int index;
  4267. if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
  4268. index = 0;
  4269. else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
  4270. index = 1;
  4271. else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
  4272. index = 2;
  4273. else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
  4274. index = 3;
  4275. else
  4276. index = 4;
  4277. return index;
  4278. }
  4279. enum btrfs_loop_type {
  4280. LOOP_FIND_IDEAL = 0,
  4281. LOOP_CACHING_NOWAIT = 1,
  4282. LOOP_CACHING_WAIT = 2,
  4283. LOOP_ALLOC_CHUNK = 3,
  4284. LOOP_NO_EMPTY_SIZE = 4,
  4285. };
  4286. /*
  4287. * walks the btree of allocated extents and find a hole of a given size.
  4288. * The key ins is changed to record the hole:
  4289. * ins->objectid == block start
  4290. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  4291. * ins->offset == number of blocks
  4292. * Any available blocks before search_start are skipped.
  4293. */
  4294. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  4295. struct btrfs_root *orig_root,
  4296. u64 num_bytes, u64 empty_size,
  4297. u64 search_start, u64 search_end,
  4298. u64 hint_byte, struct btrfs_key *ins,
  4299. int data)
  4300. {
  4301. int ret = 0;
  4302. struct btrfs_root *root = orig_root->fs_info->extent_root;
  4303. struct btrfs_free_cluster *last_ptr = NULL;
  4304. struct btrfs_block_group_cache *block_group = NULL;
  4305. int empty_cluster = 2 * 1024 * 1024;
  4306. int allowed_chunk_alloc = 0;
  4307. int done_chunk_alloc = 0;
  4308. struct btrfs_space_info *space_info;
  4309. int last_ptr_loop = 0;
  4310. int loop = 0;
  4311. int index = 0;
  4312. bool found_uncached_bg = false;
  4313. bool failed_cluster_refill = false;
  4314. bool failed_alloc = false;
  4315. bool use_cluster = true;
  4316. u64 ideal_cache_percent = 0;
  4317. u64 ideal_cache_offset = 0;
  4318. WARN_ON(num_bytes < root->sectorsize);
  4319. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  4320. ins->objectid = 0;
  4321. ins->offset = 0;
  4322. space_info = __find_space_info(root->fs_info, data);
  4323. if (!space_info) {
  4324. printk(KERN_ERR "No space info for %d\n", data);
  4325. return -ENOSPC;
  4326. }
  4327. /*
  4328. * If the space info is for both data and metadata it means we have a
  4329. * small filesystem and we can't use the clustering stuff.
  4330. */
  4331. if (btrfs_mixed_space_info(space_info))
  4332. use_cluster = false;
  4333. if (orig_root->ref_cows || empty_size)
  4334. allowed_chunk_alloc = 1;
  4335. if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
  4336. last_ptr = &root->fs_info->meta_alloc_cluster;
  4337. if (!btrfs_test_opt(root, SSD))
  4338. empty_cluster = 64 * 1024;
  4339. }
  4340. if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
  4341. btrfs_test_opt(root, SSD)) {
  4342. last_ptr = &root->fs_info->data_alloc_cluster;
  4343. }
  4344. if (last_ptr) {
  4345. spin_lock(&last_ptr->lock);
  4346. if (last_ptr->block_group)
  4347. hint_byte = last_ptr->window_start;
  4348. spin_unlock(&last_ptr->lock);
  4349. }
  4350. search_start = max(search_start, first_logical_byte(root, 0));
  4351. search_start = max(search_start, hint_byte);
  4352. if (!last_ptr)
  4353. empty_cluster = 0;
  4354. if (search_start == hint_byte) {
  4355. ideal_cache:
  4356. block_group = btrfs_lookup_block_group(root->fs_info,
  4357. search_start);
  4358. /*
  4359. * we don't want to use the block group if it doesn't match our
  4360. * allocation bits, or if its not cached.
  4361. *
  4362. * However if we are re-searching with an ideal block group
  4363. * picked out then we don't care that the block group is cached.
  4364. */
  4365. if (block_group && block_group_bits(block_group, data) &&
  4366. (block_group->cached != BTRFS_CACHE_NO ||
  4367. search_start == ideal_cache_offset)) {
  4368. down_read(&space_info->groups_sem);
  4369. if (list_empty(&block_group->list) ||
  4370. block_group->ro) {
  4371. /*
  4372. * someone is removing this block group,
  4373. * we can't jump into the have_block_group
  4374. * target because our list pointers are not
  4375. * valid
  4376. */
  4377. btrfs_put_block_group(block_group);
  4378. up_read(&space_info->groups_sem);
  4379. } else {
  4380. index = get_block_group_index(block_group);
  4381. goto have_block_group;
  4382. }
  4383. } else if (block_group) {
  4384. btrfs_put_block_group(block_group);
  4385. }
  4386. }
  4387. search:
  4388. down_read(&space_info->groups_sem);
  4389. list_for_each_entry(block_group, &space_info->block_groups[index],
  4390. list) {
  4391. u64 offset;
  4392. int cached;
  4393. btrfs_get_block_group(block_group);
  4394. search_start = block_group->key.objectid;
  4395. /*
  4396. * this can happen if we end up cycling through all the
  4397. * raid types, but we want to make sure we only allocate
  4398. * for the proper type.
  4399. */
  4400. if (!block_group_bits(block_group, data)) {
  4401. u64 extra = BTRFS_BLOCK_GROUP_DUP |
  4402. BTRFS_BLOCK_GROUP_RAID1 |
  4403. BTRFS_BLOCK_GROUP_RAID10;
  4404. /*
  4405. * if they asked for extra copies and this block group
  4406. * doesn't provide them, bail. This does allow us to
  4407. * fill raid0 from raid1.
  4408. */
  4409. if ((data & extra) && !(block_group->flags & extra))
  4410. goto loop;
  4411. }
  4412. have_block_group:
  4413. if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
  4414. u64 free_percent;
  4415. ret = cache_block_group(block_group, trans,
  4416. orig_root, 1);
  4417. if (block_group->cached == BTRFS_CACHE_FINISHED)
  4418. goto have_block_group;
  4419. free_percent = btrfs_block_group_used(&block_group->item);
  4420. free_percent *= 100;
  4421. free_percent = div64_u64(free_percent,
  4422. block_group->key.offset);
  4423. free_percent = 100 - free_percent;
  4424. if (free_percent > ideal_cache_percent &&
  4425. likely(!block_group->ro)) {
  4426. ideal_cache_offset = block_group->key.objectid;
  4427. ideal_cache_percent = free_percent;
  4428. }
  4429. /*
  4430. * We only want to start kthread caching if we are at
  4431. * the point where we will wait for caching to make
  4432. * progress, or if our ideal search is over and we've
  4433. * found somebody to start caching.
  4434. */
  4435. if (loop > LOOP_CACHING_NOWAIT ||
  4436. (loop > LOOP_FIND_IDEAL &&
  4437. atomic_read(&space_info->caching_threads) < 2)) {
  4438. ret = cache_block_group(block_group, trans,
  4439. orig_root, 0);
  4440. BUG_ON(ret);
  4441. }
  4442. found_uncached_bg = true;
  4443. /*
  4444. * If loop is set for cached only, try the next block
  4445. * group.
  4446. */
  4447. if (loop == LOOP_FIND_IDEAL)
  4448. goto loop;
  4449. }
  4450. cached = block_group_cache_done(block_group);
  4451. if (unlikely(!cached))
  4452. found_uncached_bg = true;
  4453. if (unlikely(block_group->ro))
  4454. goto loop;
  4455. /*
  4456. * Ok we want to try and use the cluster allocator, so lets look
  4457. * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
  4458. * have tried the cluster allocator plenty of times at this
  4459. * point and not have found anything, so we are likely way too
  4460. * fragmented for the clustering stuff to find anything, so lets
  4461. * just skip it and let the allocator find whatever block it can
  4462. * find
  4463. */
  4464. if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
  4465. /*
  4466. * the refill lock keeps out other
  4467. * people trying to start a new cluster
  4468. */
  4469. spin_lock(&last_ptr->refill_lock);
  4470. if (last_ptr->block_group &&
  4471. (last_ptr->block_group->ro ||
  4472. !block_group_bits(last_ptr->block_group, data))) {
  4473. offset = 0;
  4474. goto refill_cluster;
  4475. }
  4476. offset = btrfs_alloc_from_cluster(block_group, last_ptr,
  4477. num_bytes, search_start);
  4478. if (offset) {
  4479. /* we have a block, we're done */
  4480. spin_unlock(&last_ptr->refill_lock);
  4481. goto checks;
  4482. }
  4483. spin_lock(&last_ptr->lock);
  4484. /*
  4485. * whoops, this cluster doesn't actually point to
  4486. * this block group. Get a ref on the block
  4487. * group is does point to and try again
  4488. */
  4489. if (!last_ptr_loop && last_ptr->block_group &&
  4490. last_ptr->block_group != block_group) {
  4491. btrfs_put_block_group(block_group);
  4492. block_group = last_ptr->block_group;
  4493. btrfs_get_block_group(block_group);
  4494. spin_unlock(&last_ptr->lock);
  4495. spin_unlock(&last_ptr->refill_lock);
  4496. last_ptr_loop = 1;
  4497. search_start = block_group->key.objectid;
  4498. /*
  4499. * we know this block group is properly
  4500. * in the list because
  4501. * btrfs_remove_block_group, drops the
  4502. * cluster before it removes the block
  4503. * group from the list
  4504. */
  4505. goto have_block_group;
  4506. }
  4507. spin_unlock(&last_ptr->lock);
  4508. refill_cluster:
  4509. /*
  4510. * this cluster didn't work out, free it and
  4511. * start over
  4512. */
  4513. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  4514. last_ptr_loop = 0;
  4515. /* allocate a cluster in this block group */
  4516. ret = btrfs_find_space_cluster(trans, root,
  4517. block_group, last_ptr,
  4518. offset, num_bytes,
  4519. empty_cluster + empty_size);
  4520. if (ret == 0) {
  4521. /*
  4522. * now pull our allocation out of this
  4523. * cluster
  4524. */
  4525. offset = btrfs_alloc_from_cluster(block_group,
  4526. last_ptr, num_bytes,
  4527. search_start);
  4528. if (offset) {
  4529. /* we found one, proceed */
  4530. spin_unlock(&last_ptr->refill_lock);
  4531. goto checks;
  4532. }
  4533. } else if (!cached && loop > LOOP_CACHING_NOWAIT
  4534. && !failed_cluster_refill) {
  4535. spin_unlock(&last_ptr->refill_lock);
  4536. failed_cluster_refill = true;
  4537. wait_block_group_cache_progress(block_group,
  4538. num_bytes + empty_cluster + empty_size);
  4539. goto have_block_group;
  4540. }
  4541. /*
  4542. * at this point we either didn't find a cluster
  4543. * or we weren't able to allocate a block from our
  4544. * cluster. Free the cluster we've been trying
  4545. * to use, and go to the next block group
  4546. */
  4547. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  4548. spin_unlock(&last_ptr->refill_lock);
  4549. goto loop;
  4550. }
  4551. offset = btrfs_find_space_for_alloc(block_group, search_start,
  4552. num_bytes, empty_size);
  4553. /*
  4554. * If we didn't find a chunk, and we haven't failed on this
  4555. * block group before, and this block group is in the middle of
  4556. * caching and we are ok with waiting, then go ahead and wait
  4557. * for progress to be made, and set failed_alloc to true.
  4558. *
  4559. * If failed_alloc is true then we've already waited on this
  4560. * block group once and should move on to the next block group.
  4561. */
  4562. if (!offset && !failed_alloc && !cached &&
  4563. loop > LOOP_CACHING_NOWAIT) {
  4564. wait_block_group_cache_progress(block_group,
  4565. num_bytes + empty_size);
  4566. failed_alloc = true;
  4567. goto have_block_group;
  4568. } else if (!offset) {
  4569. goto loop;
  4570. }
  4571. checks:
  4572. search_start = stripe_align(root, offset);
  4573. /* move on to the next group */
  4574. if (search_start + num_bytes >= search_end) {
  4575. btrfs_add_free_space(block_group, offset, num_bytes);
  4576. goto loop;
  4577. }
  4578. /* move on to the next group */
  4579. if (search_start + num_bytes >
  4580. block_group->key.objectid + block_group->key.offset) {
  4581. btrfs_add_free_space(block_group, offset, num_bytes);
  4582. goto loop;
  4583. }
  4584. ins->objectid = search_start;
  4585. ins->offset = num_bytes;
  4586. if (offset < search_start)
  4587. btrfs_add_free_space(block_group, offset,
  4588. search_start - offset);
  4589. BUG_ON(offset > search_start);
  4590. ret = update_reserved_bytes(block_group, num_bytes, 1,
  4591. (data & BTRFS_BLOCK_GROUP_DATA));
  4592. if (ret == -EAGAIN) {
  4593. btrfs_add_free_space(block_group, offset, num_bytes);
  4594. goto loop;
  4595. }
  4596. /* we are all good, lets return */
  4597. ins->objectid = search_start;
  4598. ins->offset = num_bytes;
  4599. if (offset < search_start)
  4600. btrfs_add_free_space(block_group, offset,
  4601. search_start - offset);
  4602. BUG_ON(offset > search_start);
  4603. break;
  4604. loop:
  4605. failed_cluster_refill = false;
  4606. failed_alloc = false;
  4607. BUG_ON(index != get_block_group_index(block_group));
  4608. btrfs_put_block_group(block_group);
  4609. }
  4610. up_read(&space_info->groups_sem);
  4611. if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
  4612. goto search;
  4613. /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
  4614. * for them to make caching progress. Also
  4615. * determine the best possible bg to cache
  4616. * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
  4617. * caching kthreads as we move along
  4618. * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
  4619. * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
  4620. * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
  4621. * again
  4622. */
  4623. if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
  4624. (found_uncached_bg || empty_size || empty_cluster ||
  4625. allowed_chunk_alloc)) {
  4626. index = 0;
  4627. if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
  4628. found_uncached_bg = false;
  4629. loop++;
  4630. if (!ideal_cache_percent &&
  4631. atomic_read(&space_info->caching_threads))
  4632. goto search;
  4633. /*
  4634. * 1 of the following 2 things have happened so far
  4635. *
  4636. * 1) We found an ideal block group for caching that
  4637. * is mostly full and will cache quickly, so we might
  4638. * as well wait for it.
  4639. *
  4640. * 2) We searched for cached only and we didn't find
  4641. * anything, and we didn't start any caching kthreads
  4642. * either, so chances are we will loop through and
  4643. * start a couple caching kthreads, and then come back
  4644. * around and just wait for them. This will be slower
  4645. * because we will have 2 caching kthreads reading at
  4646. * the same time when we could have just started one
  4647. * and waited for it to get far enough to give us an
  4648. * allocation, so go ahead and go to the wait caching
  4649. * loop.
  4650. */
  4651. loop = LOOP_CACHING_WAIT;
  4652. search_start = ideal_cache_offset;
  4653. ideal_cache_percent = 0;
  4654. goto ideal_cache;
  4655. } else if (loop == LOOP_FIND_IDEAL) {
  4656. /*
  4657. * Didn't find a uncached bg, wait on anything we find
  4658. * next.
  4659. */
  4660. loop = LOOP_CACHING_WAIT;
  4661. goto search;
  4662. }
  4663. if (loop < LOOP_CACHING_WAIT) {
  4664. loop++;
  4665. goto search;
  4666. }
  4667. if (loop == LOOP_ALLOC_CHUNK) {
  4668. empty_size = 0;
  4669. empty_cluster = 0;
  4670. }
  4671. if (allowed_chunk_alloc) {
  4672. ret = do_chunk_alloc(trans, root, num_bytes +
  4673. 2 * 1024 * 1024, data, 1);
  4674. allowed_chunk_alloc = 0;
  4675. done_chunk_alloc = 1;
  4676. } else if (!done_chunk_alloc) {
  4677. space_info->force_alloc = 1;
  4678. }
  4679. if (loop < LOOP_NO_EMPTY_SIZE) {
  4680. loop++;
  4681. goto search;
  4682. }
  4683. ret = -ENOSPC;
  4684. } else if (!ins->objectid) {
  4685. ret = -ENOSPC;
  4686. }
  4687. /* we found what we needed */
  4688. if (ins->objectid) {
  4689. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  4690. trans->block_group = block_group->key.objectid;
  4691. btrfs_put_block_group(block_group);
  4692. ret = 0;
  4693. }
  4694. return ret;
  4695. }
  4696. static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
  4697. int dump_block_groups)
  4698. {
  4699. struct btrfs_block_group_cache *cache;
  4700. int index = 0;
  4701. spin_lock(&info->lock);
  4702. printk(KERN_INFO "space_info has %llu free, is %sfull\n",
  4703. (unsigned long long)(info->total_bytes - info->bytes_used -
  4704. info->bytes_pinned - info->bytes_reserved -
  4705. info->bytes_readonly),
  4706. (info->full) ? "" : "not ");
  4707. printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
  4708. "reserved=%llu, may_use=%llu, readonly=%llu\n",
  4709. (unsigned long long)info->total_bytes,
  4710. (unsigned long long)info->bytes_used,
  4711. (unsigned long long)info->bytes_pinned,
  4712. (unsigned long long)info->bytes_reserved,
  4713. (unsigned long long)info->bytes_may_use,
  4714. (unsigned long long)info->bytes_readonly);
  4715. spin_unlock(&info->lock);
  4716. if (!dump_block_groups)
  4717. return;
  4718. down_read(&info->groups_sem);
  4719. again:
  4720. list_for_each_entry(cache, &info->block_groups[index], list) {
  4721. spin_lock(&cache->lock);
  4722. printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
  4723. "%llu pinned %llu reserved\n",
  4724. (unsigned long long)cache->key.objectid,
  4725. (unsigned long long)cache->key.offset,
  4726. (unsigned long long)btrfs_block_group_used(&cache->item),
  4727. (unsigned long long)cache->pinned,
  4728. (unsigned long long)cache->reserved);
  4729. btrfs_dump_free_space(cache, bytes);
  4730. spin_unlock(&cache->lock);
  4731. }
  4732. if (++index < BTRFS_NR_RAID_TYPES)
  4733. goto again;
  4734. up_read(&info->groups_sem);
  4735. }
  4736. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  4737. struct btrfs_root *root,
  4738. u64 num_bytes, u64 min_alloc_size,
  4739. u64 empty_size, u64 hint_byte,
  4740. u64 search_end, struct btrfs_key *ins,
  4741. u64 data)
  4742. {
  4743. int ret;
  4744. u64 search_start = 0;
  4745. data = btrfs_get_alloc_profile(root, data);
  4746. again:
  4747. /*
  4748. * the only place that sets empty_size is btrfs_realloc_node, which
  4749. * is not called recursively on allocations
  4750. */
  4751. if (empty_size || root->ref_cows)
  4752. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  4753. num_bytes + 2 * 1024 * 1024, data, 0);
  4754. WARN_ON(num_bytes < root->sectorsize);
  4755. ret = find_free_extent(trans, root, num_bytes, empty_size,
  4756. search_start, search_end, hint_byte,
  4757. ins, data);
  4758. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  4759. num_bytes = num_bytes >> 1;
  4760. num_bytes = num_bytes & ~(root->sectorsize - 1);
  4761. num_bytes = max(num_bytes, min_alloc_size);
  4762. do_chunk_alloc(trans, root->fs_info->extent_root,
  4763. num_bytes, data, 1);
  4764. goto again;
  4765. }
  4766. if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
  4767. struct btrfs_space_info *sinfo;
  4768. sinfo = __find_space_info(root->fs_info, data);
  4769. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  4770. "wanted %llu\n", (unsigned long long)data,
  4771. (unsigned long long)num_bytes);
  4772. dump_space_info(sinfo, num_bytes, 1);
  4773. }
  4774. trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
  4775. return ret;
  4776. }
  4777. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  4778. {
  4779. struct btrfs_block_group_cache *cache;
  4780. int ret = 0;
  4781. cache = btrfs_lookup_block_group(root->fs_info, start);
  4782. if (!cache) {
  4783. printk(KERN_ERR "Unable to find block group for %llu\n",
  4784. (unsigned long long)start);
  4785. return -ENOSPC;
  4786. }
  4787. ret = btrfs_discard_extent(root, start, len);
  4788. btrfs_add_free_space(cache, start, len);
  4789. update_reserved_bytes(cache, len, 0, 1);
  4790. btrfs_put_block_group(cache);
  4791. trace_btrfs_reserved_extent_free(root, start, len);
  4792. return ret;
  4793. }
  4794. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  4795. struct btrfs_root *root,
  4796. u64 parent, u64 root_objectid,
  4797. u64 flags, u64 owner, u64 offset,
  4798. struct btrfs_key *ins, int ref_mod)
  4799. {
  4800. int ret;
  4801. struct btrfs_fs_info *fs_info = root->fs_info;
  4802. struct btrfs_extent_item *extent_item;
  4803. struct btrfs_extent_inline_ref *iref;
  4804. struct btrfs_path *path;
  4805. struct extent_buffer *leaf;
  4806. int type;
  4807. u32 size;
  4808. if (parent > 0)
  4809. type = BTRFS_SHARED_DATA_REF_KEY;
  4810. else
  4811. type = BTRFS_EXTENT_DATA_REF_KEY;
  4812. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  4813. path = btrfs_alloc_path();
  4814. if (!path)
  4815. return -ENOMEM;
  4816. path->leave_spinning = 1;
  4817. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  4818. ins, size);
  4819. BUG_ON(ret);
  4820. leaf = path->nodes[0];
  4821. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  4822. struct btrfs_extent_item);
  4823. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  4824. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  4825. btrfs_set_extent_flags(leaf, extent_item,
  4826. flags | BTRFS_EXTENT_FLAG_DATA);
  4827. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  4828. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  4829. if (parent > 0) {
  4830. struct btrfs_shared_data_ref *ref;
  4831. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  4832. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  4833. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  4834. } else {
  4835. struct btrfs_extent_data_ref *ref;
  4836. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  4837. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  4838. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  4839. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  4840. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  4841. }
  4842. btrfs_mark_buffer_dirty(path->nodes[0]);
  4843. btrfs_free_path(path);
  4844. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  4845. if (ret) {
  4846. printk(KERN_ERR "btrfs update block group failed for %llu "
  4847. "%llu\n", (unsigned long long)ins->objectid,
  4848. (unsigned long long)ins->offset);
  4849. BUG();
  4850. }
  4851. return ret;
  4852. }
  4853. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  4854. struct btrfs_root *root,
  4855. u64 parent, u64 root_objectid,
  4856. u64 flags, struct btrfs_disk_key *key,
  4857. int level, struct btrfs_key *ins)
  4858. {
  4859. int ret;
  4860. struct btrfs_fs_info *fs_info = root->fs_info;
  4861. struct btrfs_extent_item *extent_item;
  4862. struct btrfs_tree_block_info *block_info;
  4863. struct btrfs_extent_inline_ref *iref;
  4864. struct btrfs_path *path;
  4865. struct extent_buffer *leaf;
  4866. u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
  4867. path = btrfs_alloc_path();
  4868. BUG_ON(!path);
  4869. path->leave_spinning = 1;
  4870. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  4871. ins, size);
  4872. BUG_ON(ret);
  4873. leaf = path->nodes[0];
  4874. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  4875. struct btrfs_extent_item);
  4876. btrfs_set_extent_refs(leaf, extent_item, 1);
  4877. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  4878. btrfs_set_extent_flags(leaf, extent_item,
  4879. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  4880. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  4881. btrfs_set_tree_block_key(leaf, block_info, key);
  4882. btrfs_set_tree_block_level(leaf, block_info, level);
  4883. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  4884. if (parent > 0) {
  4885. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  4886. btrfs_set_extent_inline_ref_type(leaf, iref,
  4887. BTRFS_SHARED_BLOCK_REF_KEY);
  4888. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  4889. } else {
  4890. btrfs_set_extent_inline_ref_type(leaf, iref,
  4891. BTRFS_TREE_BLOCK_REF_KEY);
  4892. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  4893. }
  4894. btrfs_mark_buffer_dirty(leaf);
  4895. btrfs_free_path(path);
  4896. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  4897. if (ret) {
  4898. printk(KERN_ERR "btrfs update block group failed for %llu "
  4899. "%llu\n", (unsigned long long)ins->objectid,
  4900. (unsigned long long)ins->offset);
  4901. BUG();
  4902. }
  4903. return ret;
  4904. }
  4905. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  4906. struct btrfs_root *root,
  4907. u64 root_objectid, u64 owner,
  4908. u64 offset, struct btrfs_key *ins)
  4909. {
  4910. int ret;
  4911. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  4912. ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
  4913. 0, root_objectid, owner, offset,
  4914. BTRFS_ADD_DELAYED_EXTENT, NULL);
  4915. return ret;
  4916. }
  4917. /*
  4918. * this is used by the tree logging recovery code. It records that
  4919. * an extent has been allocated and makes sure to clear the free
  4920. * space cache bits as well
  4921. */
  4922. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  4923. struct btrfs_root *root,
  4924. u64 root_objectid, u64 owner, u64 offset,
  4925. struct btrfs_key *ins)
  4926. {
  4927. int ret;
  4928. struct btrfs_block_group_cache *block_group;
  4929. struct btrfs_caching_control *caching_ctl;
  4930. u64 start = ins->objectid;
  4931. u64 num_bytes = ins->offset;
  4932. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  4933. cache_block_group(block_group, trans, NULL, 0);
  4934. caching_ctl = get_caching_control(block_group);
  4935. if (!caching_ctl) {
  4936. BUG_ON(!block_group_cache_done(block_group));
  4937. ret = btrfs_remove_free_space(block_group, start, num_bytes);
  4938. BUG_ON(ret);
  4939. } else {
  4940. mutex_lock(&caching_ctl->mutex);
  4941. if (start >= caching_ctl->progress) {
  4942. ret = add_excluded_extent(root, start, num_bytes);
  4943. BUG_ON(ret);
  4944. } else if (start + num_bytes <= caching_ctl->progress) {
  4945. ret = btrfs_remove_free_space(block_group,
  4946. start, num_bytes);
  4947. BUG_ON(ret);
  4948. } else {
  4949. num_bytes = caching_ctl->progress - start;
  4950. ret = btrfs_remove_free_space(block_group,
  4951. start, num_bytes);
  4952. BUG_ON(ret);
  4953. start = caching_ctl->progress;
  4954. num_bytes = ins->objectid + ins->offset -
  4955. caching_ctl->progress;
  4956. ret = add_excluded_extent(root, start, num_bytes);
  4957. BUG_ON(ret);
  4958. }
  4959. mutex_unlock(&caching_ctl->mutex);
  4960. put_caching_control(caching_ctl);
  4961. }
  4962. ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
  4963. BUG_ON(ret);
  4964. btrfs_put_block_group(block_group);
  4965. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  4966. 0, owner, offset, ins, 1);
  4967. return ret;
  4968. }
  4969. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  4970. struct btrfs_root *root,
  4971. u64 bytenr, u32 blocksize,
  4972. int level)
  4973. {
  4974. struct extent_buffer *buf;
  4975. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  4976. if (!buf)
  4977. return ERR_PTR(-ENOMEM);
  4978. btrfs_set_header_generation(buf, trans->transid);
  4979. btrfs_set_buffer_lockdep_class(buf, level);
  4980. btrfs_tree_lock(buf);
  4981. clean_tree_block(trans, root, buf);
  4982. btrfs_set_lock_blocking(buf);
  4983. btrfs_set_buffer_uptodate(buf);
  4984. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  4985. /*
  4986. * we allow two log transactions at a time, use different
  4987. * EXENT bit to differentiate dirty pages.
  4988. */
  4989. if (root->log_transid % 2 == 0)
  4990. set_extent_dirty(&root->dirty_log_pages, buf->start,
  4991. buf->start + buf->len - 1, GFP_NOFS);
  4992. else
  4993. set_extent_new(&root->dirty_log_pages, buf->start,
  4994. buf->start + buf->len - 1, GFP_NOFS);
  4995. } else {
  4996. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  4997. buf->start + buf->len - 1, GFP_NOFS);
  4998. }
  4999. trans->blocks_used++;
  5000. /* this returns a buffer locked for blocking */
  5001. return buf;
  5002. }
  5003. static struct btrfs_block_rsv *
  5004. use_block_rsv(struct btrfs_trans_handle *trans,
  5005. struct btrfs_root *root, u32 blocksize)
  5006. {
  5007. struct btrfs_block_rsv *block_rsv;
  5008. struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
  5009. int ret;
  5010. block_rsv = get_block_rsv(trans, root);
  5011. if (block_rsv->size == 0) {
  5012. ret = reserve_metadata_bytes(trans, root, block_rsv,
  5013. blocksize, 0);
  5014. /*
  5015. * If we couldn't reserve metadata bytes try and use some from
  5016. * the global reserve.
  5017. */
  5018. if (ret && block_rsv != global_rsv) {
  5019. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5020. if (!ret)
  5021. return global_rsv;
  5022. return ERR_PTR(ret);
  5023. } else if (ret) {
  5024. return ERR_PTR(ret);
  5025. }
  5026. return block_rsv;
  5027. }
  5028. ret = block_rsv_use_bytes(block_rsv, blocksize);
  5029. if (!ret)
  5030. return block_rsv;
  5031. if (ret) {
  5032. WARN_ON(1);
  5033. ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
  5034. 0);
  5035. if (!ret) {
  5036. spin_lock(&block_rsv->lock);
  5037. block_rsv->size += blocksize;
  5038. spin_unlock(&block_rsv->lock);
  5039. return block_rsv;
  5040. } else if (ret && block_rsv != global_rsv) {
  5041. ret = block_rsv_use_bytes(global_rsv, blocksize);
  5042. if (!ret)
  5043. return global_rsv;
  5044. }
  5045. }
  5046. return ERR_PTR(-ENOSPC);
  5047. }
  5048. static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
  5049. {
  5050. block_rsv_add_bytes(block_rsv, blocksize, 0);
  5051. block_rsv_release_bytes(block_rsv, NULL, 0);
  5052. }
  5053. /*
  5054. * finds a free extent and does all the dirty work required for allocation
  5055. * returns the key for the extent through ins, and a tree buffer for
  5056. * the first block of the extent through buf.
  5057. *
  5058. * returns the tree buffer or NULL.
  5059. */
  5060. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  5061. struct btrfs_root *root, u32 blocksize,
  5062. u64 parent, u64 root_objectid,
  5063. struct btrfs_disk_key *key, int level,
  5064. u64 hint, u64 empty_size)
  5065. {
  5066. struct btrfs_key ins;
  5067. struct btrfs_block_rsv *block_rsv;
  5068. struct extent_buffer *buf;
  5069. u64 flags = 0;
  5070. int ret;
  5071. block_rsv = use_block_rsv(trans, root, blocksize);
  5072. if (IS_ERR(block_rsv))
  5073. return ERR_CAST(block_rsv);
  5074. ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
  5075. empty_size, hint, (u64)-1, &ins, 0);
  5076. if (ret) {
  5077. unuse_block_rsv(block_rsv, blocksize);
  5078. return ERR_PTR(ret);
  5079. }
  5080. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  5081. blocksize, level);
  5082. BUG_ON(IS_ERR(buf));
  5083. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  5084. if (parent == 0)
  5085. parent = ins.objectid;
  5086. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5087. } else
  5088. BUG_ON(parent > 0);
  5089. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  5090. struct btrfs_delayed_extent_op *extent_op;
  5091. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  5092. BUG_ON(!extent_op);
  5093. if (key)
  5094. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  5095. else
  5096. memset(&extent_op->key, 0, sizeof(extent_op->key));
  5097. extent_op->flags_to_set = flags;
  5098. extent_op->update_key = 1;
  5099. extent_op->update_flags = 1;
  5100. extent_op->is_data = 0;
  5101. ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
  5102. ins.offset, parent, root_objectid,
  5103. level, BTRFS_ADD_DELAYED_EXTENT,
  5104. extent_op);
  5105. BUG_ON(ret);
  5106. }
  5107. return buf;
  5108. }
  5109. struct walk_control {
  5110. u64 refs[BTRFS_MAX_LEVEL];
  5111. u64 flags[BTRFS_MAX_LEVEL];
  5112. struct btrfs_key update_progress;
  5113. int stage;
  5114. int level;
  5115. int shared_level;
  5116. int update_ref;
  5117. int keep_locks;
  5118. int reada_slot;
  5119. int reada_count;
  5120. };
  5121. #define DROP_REFERENCE 1
  5122. #define UPDATE_BACKREF 2
  5123. static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
  5124. struct btrfs_root *root,
  5125. struct walk_control *wc,
  5126. struct btrfs_path *path)
  5127. {
  5128. u64 bytenr;
  5129. u64 generation;
  5130. u64 refs;
  5131. u64 flags;
  5132. u32 nritems;
  5133. u32 blocksize;
  5134. struct btrfs_key key;
  5135. struct extent_buffer *eb;
  5136. int ret;
  5137. int slot;
  5138. int nread = 0;
  5139. if (path->slots[wc->level] < wc->reada_slot) {
  5140. wc->reada_count = wc->reada_count * 2 / 3;
  5141. wc->reada_count = max(wc->reada_count, 2);
  5142. } else {
  5143. wc->reada_count = wc->reada_count * 3 / 2;
  5144. wc->reada_count = min_t(int, wc->reada_count,
  5145. BTRFS_NODEPTRS_PER_BLOCK(root));
  5146. }
  5147. eb = path->nodes[wc->level];
  5148. nritems = btrfs_header_nritems(eb);
  5149. blocksize = btrfs_level_size(root, wc->level - 1);
  5150. for (slot = path->slots[wc->level]; slot < nritems; slot++) {
  5151. if (nread >= wc->reada_count)
  5152. break;
  5153. cond_resched();
  5154. bytenr = btrfs_node_blockptr(eb, slot);
  5155. generation = btrfs_node_ptr_generation(eb, slot);
  5156. if (slot == path->slots[wc->level])
  5157. goto reada;
  5158. if (wc->stage == UPDATE_BACKREF &&
  5159. generation <= root->root_key.offset)
  5160. continue;
  5161. /* We don't lock the tree block, it's OK to be racy here */
  5162. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5163. &refs, &flags);
  5164. BUG_ON(ret);
  5165. BUG_ON(refs == 0);
  5166. if (wc->stage == DROP_REFERENCE) {
  5167. if (refs == 1)
  5168. goto reada;
  5169. if (wc->level == 1 &&
  5170. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5171. continue;
  5172. if (!wc->update_ref ||
  5173. generation <= root->root_key.offset)
  5174. continue;
  5175. btrfs_node_key_to_cpu(eb, &key, slot);
  5176. ret = btrfs_comp_cpu_keys(&key,
  5177. &wc->update_progress);
  5178. if (ret < 0)
  5179. continue;
  5180. } else {
  5181. if (wc->level == 1 &&
  5182. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5183. continue;
  5184. }
  5185. reada:
  5186. ret = readahead_tree_block(root, bytenr, blocksize,
  5187. generation);
  5188. if (ret)
  5189. break;
  5190. nread++;
  5191. }
  5192. wc->reada_slot = slot;
  5193. }
  5194. /*
  5195. * hepler to process tree block while walking down the tree.
  5196. *
  5197. * when wc->stage == UPDATE_BACKREF, this function updates
  5198. * back refs for pointers in the block.
  5199. *
  5200. * NOTE: return value 1 means we should stop walking down.
  5201. */
  5202. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  5203. struct btrfs_root *root,
  5204. struct btrfs_path *path,
  5205. struct walk_control *wc, int lookup_info)
  5206. {
  5207. int level = wc->level;
  5208. struct extent_buffer *eb = path->nodes[level];
  5209. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5210. int ret;
  5211. if (wc->stage == UPDATE_BACKREF &&
  5212. btrfs_header_owner(eb) != root->root_key.objectid)
  5213. return 1;
  5214. /*
  5215. * when reference count of tree block is 1, it won't increase
  5216. * again. once full backref flag is set, we never clear it.
  5217. */
  5218. if (lookup_info &&
  5219. ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  5220. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
  5221. BUG_ON(!path->locks[level]);
  5222. ret = btrfs_lookup_extent_info(trans, root,
  5223. eb->start, eb->len,
  5224. &wc->refs[level],
  5225. &wc->flags[level]);
  5226. BUG_ON(ret);
  5227. BUG_ON(wc->refs[level] == 0);
  5228. }
  5229. if (wc->stage == DROP_REFERENCE) {
  5230. if (wc->refs[level] > 1)
  5231. return 1;
  5232. if (path->locks[level] && !wc->keep_locks) {
  5233. btrfs_tree_unlock(eb);
  5234. path->locks[level] = 0;
  5235. }
  5236. return 0;
  5237. }
  5238. /* wc->stage == UPDATE_BACKREF */
  5239. if (!(wc->flags[level] & flag)) {
  5240. BUG_ON(!path->locks[level]);
  5241. ret = btrfs_inc_ref(trans, root, eb, 1);
  5242. BUG_ON(ret);
  5243. ret = btrfs_dec_ref(trans, root, eb, 0);
  5244. BUG_ON(ret);
  5245. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  5246. eb->len, flag, 0);
  5247. BUG_ON(ret);
  5248. wc->flags[level] |= flag;
  5249. }
  5250. /*
  5251. * the block is shared by multiple trees, so it's not good to
  5252. * keep the tree lock
  5253. */
  5254. if (path->locks[level] && level > 0) {
  5255. btrfs_tree_unlock(eb);
  5256. path->locks[level] = 0;
  5257. }
  5258. return 0;
  5259. }
  5260. /*
  5261. * hepler to process tree block pointer.
  5262. *
  5263. * when wc->stage == DROP_REFERENCE, this function checks
  5264. * reference count of the block pointed to. if the block
  5265. * is shared and we need update back refs for the subtree
  5266. * rooted at the block, this function changes wc->stage to
  5267. * UPDATE_BACKREF. if the block is shared and there is no
  5268. * need to update back, this function drops the reference
  5269. * to the block.
  5270. *
  5271. * NOTE: return value 1 means we should stop walking down.
  5272. */
  5273. static noinline int do_walk_down(struct btrfs_trans_handle *trans,
  5274. struct btrfs_root *root,
  5275. struct btrfs_path *path,
  5276. struct walk_control *wc, int *lookup_info)
  5277. {
  5278. u64 bytenr;
  5279. u64 generation;
  5280. u64 parent;
  5281. u32 blocksize;
  5282. struct btrfs_key key;
  5283. struct extent_buffer *next;
  5284. int level = wc->level;
  5285. int reada = 0;
  5286. int ret = 0;
  5287. generation = btrfs_node_ptr_generation(path->nodes[level],
  5288. path->slots[level]);
  5289. /*
  5290. * if the lower level block was created before the snapshot
  5291. * was created, we know there is no need to update back refs
  5292. * for the subtree
  5293. */
  5294. if (wc->stage == UPDATE_BACKREF &&
  5295. generation <= root->root_key.offset) {
  5296. *lookup_info = 1;
  5297. return 1;
  5298. }
  5299. bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
  5300. blocksize = btrfs_level_size(root, level - 1);
  5301. next = btrfs_find_tree_block(root, bytenr, blocksize);
  5302. if (!next) {
  5303. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  5304. if (!next)
  5305. return -ENOMEM;
  5306. reada = 1;
  5307. }
  5308. btrfs_tree_lock(next);
  5309. btrfs_set_lock_blocking(next);
  5310. ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
  5311. &wc->refs[level - 1],
  5312. &wc->flags[level - 1]);
  5313. BUG_ON(ret);
  5314. BUG_ON(wc->refs[level - 1] == 0);
  5315. *lookup_info = 0;
  5316. if (wc->stage == DROP_REFERENCE) {
  5317. if (wc->refs[level - 1] > 1) {
  5318. if (level == 1 &&
  5319. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5320. goto skip;
  5321. if (!wc->update_ref ||
  5322. generation <= root->root_key.offset)
  5323. goto skip;
  5324. btrfs_node_key_to_cpu(path->nodes[level], &key,
  5325. path->slots[level]);
  5326. ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
  5327. if (ret < 0)
  5328. goto skip;
  5329. wc->stage = UPDATE_BACKREF;
  5330. wc->shared_level = level - 1;
  5331. }
  5332. } else {
  5333. if (level == 1 &&
  5334. (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  5335. goto skip;
  5336. }
  5337. if (!btrfs_buffer_uptodate(next, generation)) {
  5338. btrfs_tree_unlock(next);
  5339. free_extent_buffer(next);
  5340. next = NULL;
  5341. *lookup_info = 1;
  5342. }
  5343. if (!next) {
  5344. if (reada && level == 1)
  5345. reada_walk_down(trans, root, wc, path);
  5346. next = read_tree_block(root, bytenr, blocksize, generation);
  5347. if (!next)
  5348. return -EIO;
  5349. btrfs_tree_lock(next);
  5350. btrfs_set_lock_blocking(next);
  5351. }
  5352. level--;
  5353. BUG_ON(level != btrfs_header_level(next));
  5354. path->nodes[level] = next;
  5355. path->slots[level] = 0;
  5356. path->locks[level] = 1;
  5357. wc->level = level;
  5358. if (wc->level == 1)
  5359. wc->reada_slot = 0;
  5360. return 0;
  5361. skip:
  5362. wc->refs[level - 1] = 0;
  5363. wc->flags[level - 1] = 0;
  5364. if (wc->stage == DROP_REFERENCE) {
  5365. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  5366. parent = path->nodes[level]->start;
  5367. } else {
  5368. BUG_ON(root->root_key.objectid !=
  5369. btrfs_header_owner(path->nodes[level]));
  5370. parent = 0;
  5371. }
  5372. ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
  5373. root->root_key.objectid, level - 1, 0);
  5374. BUG_ON(ret);
  5375. }
  5376. btrfs_tree_unlock(next);
  5377. free_extent_buffer(next);
  5378. *lookup_info = 1;
  5379. return 1;
  5380. }
  5381. /*
  5382. * hepler to process tree block while walking up the tree.
  5383. *
  5384. * when wc->stage == DROP_REFERENCE, this function drops
  5385. * reference count on the block.
  5386. *
  5387. * when wc->stage == UPDATE_BACKREF, this function changes
  5388. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  5389. * to UPDATE_BACKREF previously while processing the block.
  5390. *
  5391. * NOTE: return value 1 means we should stop walking up.
  5392. */
  5393. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  5394. struct btrfs_root *root,
  5395. struct btrfs_path *path,
  5396. struct walk_control *wc)
  5397. {
  5398. int ret;
  5399. int level = wc->level;
  5400. struct extent_buffer *eb = path->nodes[level];
  5401. u64 parent = 0;
  5402. if (wc->stage == UPDATE_BACKREF) {
  5403. BUG_ON(wc->shared_level < level);
  5404. if (level < wc->shared_level)
  5405. goto out;
  5406. ret = find_next_key(path, level + 1, &wc->update_progress);
  5407. if (ret > 0)
  5408. wc->update_ref = 0;
  5409. wc->stage = DROP_REFERENCE;
  5410. wc->shared_level = -1;
  5411. path->slots[level] = 0;
  5412. /*
  5413. * check reference count again if the block isn't locked.
  5414. * we should start walking down the tree again if reference
  5415. * count is one.
  5416. */
  5417. if (!path->locks[level]) {
  5418. BUG_ON(level == 0);
  5419. btrfs_tree_lock(eb);
  5420. btrfs_set_lock_blocking(eb);
  5421. path->locks[level] = 1;
  5422. ret = btrfs_lookup_extent_info(trans, root,
  5423. eb->start, eb->len,
  5424. &wc->refs[level],
  5425. &wc->flags[level]);
  5426. BUG_ON(ret);
  5427. BUG_ON(wc->refs[level] == 0);
  5428. if (wc->refs[level] == 1) {
  5429. btrfs_tree_unlock(eb);
  5430. path->locks[level] = 0;
  5431. return 1;
  5432. }
  5433. }
  5434. }
  5435. /* wc->stage == DROP_REFERENCE */
  5436. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  5437. if (wc->refs[level] == 1) {
  5438. if (level == 0) {
  5439. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5440. ret = btrfs_dec_ref(trans, root, eb, 1);
  5441. else
  5442. ret = btrfs_dec_ref(trans, root, eb, 0);
  5443. BUG_ON(ret);
  5444. }
  5445. /* make block locked assertion in clean_tree_block happy */
  5446. if (!path->locks[level] &&
  5447. btrfs_header_generation(eb) == trans->transid) {
  5448. btrfs_tree_lock(eb);
  5449. btrfs_set_lock_blocking(eb);
  5450. path->locks[level] = 1;
  5451. }
  5452. clean_tree_block(trans, root, eb);
  5453. }
  5454. if (eb == root->node) {
  5455. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5456. parent = eb->start;
  5457. else
  5458. BUG_ON(root->root_key.objectid !=
  5459. btrfs_header_owner(eb));
  5460. } else {
  5461. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  5462. parent = path->nodes[level + 1]->start;
  5463. else
  5464. BUG_ON(root->root_key.objectid !=
  5465. btrfs_header_owner(path->nodes[level + 1]));
  5466. }
  5467. btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  5468. out:
  5469. wc->refs[level] = 0;
  5470. wc->flags[level] = 0;
  5471. return 0;
  5472. }
  5473. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  5474. struct btrfs_root *root,
  5475. struct btrfs_path *path,
  5476. struct walk_control *wc)
  5477. {
  5478. int level = wc->level;
  5479. int lookup_info = 1;
  5480. int ret;
  5481. while (level >= 0) {
  5482. ret = walk_down_proc(trans, root, path, wc, lookup_info);
  5483. if (ret > 0)
  5484. break;
  5485. if (level == 0)
  5486. break;
  5487. if (path->slots[level] >=
  5488. btrfs_header_nritems(path->nodes[level]))
  5489. break;
  5490. ret = do_walk_down(trans, root, path, wc, &lookup_info);
  5491. if (ret > 0) {
  5492. path->slots[level]++;
  5493. continue;
  5494. } else if (ret < 0)
  5495. return ret;
  5496. level = wc->level;
  5497. }
  5498. return 0;
  5499. }
  5500. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  5501. struct btrfs_root *root,
  5502. struct btrfs_path *path,
  5503. struct walk_control *wc, int max_level)
  5504. {
  5505. int level = wc->level;
  5506. int ret;
  5507. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  5508. while (level < max_level && path->nodes[level]) {
  5509. wc->level = level;
  5510. if (path->slots[level] + 1 <
  5511. btrfs_header_nritems(path->nodes[level])) {
  5512. path->slots[level]++;
  5513. return 0;
  5514. } else {
  5515. ret = walk_up_proc(trans, root, path, wc);
  5516. if (ret > 0)
  5517. return 0;
  5518. if (path->locks[level]) {
  5519. btrfs_tree_unlock(path->nodes[level]);
  5520. path->locks[level] = 0;
  5521. }
  5522. free_extent_buffer(path->nodes[level]);
  5523. path->nodes[level] = NULL;
  5524. level++;
  5525. }
  5526. }
  5527. return 1;
  5528. }
  5529. /*
  5530. * drop a subvolume tree.
  5531. *
  5532. * this function traverses the tree freeing any blocks that only
  5533. * referenced by the tree.
  5534. *
  5535. * when a shared tree block is found. this function decreases its
  5536. * reference count by one. if update_ref is true, this function
  5537. * also make sure backrefs for the shared block and all lower level
  5538. * blocks are properly updated.
  5539. */
  5540. int btrfs_drop_snapshot(struct btrfs_root *root,
  5541. struct btrfs_block_rsv *block_rsv, int update_ref)
  5542. {
  5543. struct btrfs_path *path;
  5544. struct btrfs_trans_handle *trans;
  5545. struct btrfs_root *tree_root = root->fs_info->tree_root;
  5546. struct btrfs_root_item *root_item = &root->root_item;
  5547. struct walk_control *wc;
  5548. struct btrfs_key key;
  5549. int err = 0;
  5550. int ret;
  5551. int level;
  5552. path = btrfs_alloc_path();
  5553. BUG_ON(!path);
  5554. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  5555. BUG_ON(!wc);
  5556. trans = btrfs_start_transaction(tree_root, 0);
  5557. BUG_ON(IS_ERR(trans));
  5558. if (block_rsv)
  5559. trans->block_rsv = block_rsv;
  5560. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  5561. level = btrfs_header_level(root->node);
  5562. path->nodes[level] = btrfs_lock_root_node(root);
  5563. btrfs_set_lock_blocking(path->nodes[level]);
  5564. path->slots[level] = 0;
  5565. path->locks[level] = 1;
  5566. memset(&wc->update_progress, 0,
  5567. sizeof(wc->update_progress));
  5568. } else {
  5569. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  5570. memcpy(&wc->update_progress, &key,
  5571. sizeof(wc->update_progress));
  5572. level = root_item->drop_level;
  5573. BUG_ON(level == 0);
  5574. path->lowest_level = level;
  5575. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5576. path->lowest_level = 0;
  5577. if (ret < 0) {
  5578. err = ret;
  5579. goto out;
  5580. }
  5581. WARN_ON(ret > 0);
  5582. /*
  5583. * unlock our path, this is safe because only this
  5584. * function is allowed to delete this snapshot
  5585. */
  5586. btrfs_unlock_up_safe(path, 0);
  5587. level = btrfs_header_level(root->node);
  5588. while (1) {
  5589. btrfs_tree_lock(path->nodes[level]);
  5590. btrfs_set_lock_blocking(path->nodes[level]);
  5591. ret = btrfs_lookup_extent_info(trans, root,
  5592. path->nodes[level]->start,
  5593. path->nodes[level]->len,
  5594. &wc->refs[level],
  5595. &wc->flags[level]);
  5596. BUG_ON(ret);
  5597. BUG_ON(wc->refs[level] == 0);
  5598. if (level == root_item->drop_level)
  5599. break;
  5600. btrfs_tree_unlock(path->nodes[level]);
  5601. WARN_ON(wc->refs[level] != 1);
  5602. level--;
  5603. }
  5604. }
  5605. wc->level = level;
  5606. wc->shared_level = -1;
  5607. wc->stage = DROP_REFERENCE;
  5608. wc->update_ref = update_ref;
  5609. wc->keep_locks = 0;
  5610. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  5611. while (1) {
  5612. ret = walk_down_tree(trans, root, path, wc);
  5613. if (ret < 0) {
  5614. err = ret;
  5615. break;
  5616. }
  5617. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  5618. if (ret < 0) {
  5619. err = ret;
  5620. break;
  5621. }
  5622. if (ret > 0) {
  5623. BUG_ON(wc->stage != DROP_REFERENCE);
  5624. break;
  5625. }
  5626. if (wc->stage == DROP_REFERENCE) {
  5627. level = wc->level;
  5628. btrfs_node_key(path->nodes[level],
  5629. &root_item->drop_progress,
  5630. path->slots[level]);
  5631. root_item->drop_level = level;
  5632. }
  5633. BUG_ON(wc->level == 0);
  5634. if (btrfs_should_end_transaction(trans, tree_root)) {
  5635. ret = btrfs_update_root(trans, tree_root,
  5636. &root->root_key,
  5637. root_item);
  5638. BUG_ON(ret);
  5639. btrfs_end_transaction_throttle(trans, tree_root);
  5640. trans = btrfs_start_transaction(tree_root, 0);
  5641. BUG_ON(IS_ERR(trans));
  5642. if (block_rsv)
  5643. trans->block_rsv = block_rsv;
  5644. }
  5645. }
  5646. btrfs_release_path(root, path);
  5647. BUG_ON(err);
  5648. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  5649. BUG_ON(ret);
  5650. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  5651. ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
  5652. NULL, NULL);
  5653. BUG_ON(ret < 0);
  5654. if (ret > 0) {
  5655. /* if we fail to delete the orphan item this time
  5656. * around, it'll get picked up the next time.
  5657. *
  5658. * The most common failure here is just -ENOENT.
  5659. */
  5660. btrfs_del_orphan_item(trans, tree_root,
  5661. root->root_key.objectid);
  5662. }
  5663. }
  5664. if (root->in_radix) {
  5665. btrfs_free_fs_root(tree_root->fs_info, root);
  5666. } else {
  5667. free_extent_buffer(root->node);
  5668. free_extent_buffer(root->commit_root);
  5669. kfree(root);
  5670. }
  5671. out:
  5672. btrfs_end_transaction_throttle(trans, tree_root);
  5673. kfree(wc);
  5674. btrfs_free_path(path);
  5675. return err;
  5676. }
  5677. /*
  5678. * drop subtree rooted at tree block 'node'.
  5679. *
  5680. * NOTE: this function will unlock and release tree block 'node'
  5681. */
  5682. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  5683. struct btrfs_root *root,
  5684. struct extent_buffer *node,
  5685. struct extent_buffer *parent)
  5686. {
  5687. struct btrfs_path *path;
  5688. struct walk_control *wc;
  5689. int level;
  5690. int parent_level;
  5691. int ret = 0;
  5692. int wret;
  5693. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  5694. path = btrfs_alloc_path();
  5695. if (!path)
  5696. return -ENOMEM;
  5697. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  5698. if (!wc) {
  5699. btrfs_free_path(path);
  5700. return -ENOMEM;
  5701. }
  5702. btrfs_assert_tree_locked(parent);
  5703. parent_level = btrfs_header_level(parent);
  5704. extent_buffer_get(parent);
  5705. path->nodes[parent_level] = parent;
  5706. path->slots[parent_level] = btrfs_header_nritems(parent);
  5707. btrfs_assert_tree_locked(node);
  5708. level = btrfs_header_level(node);
  5709. path->nodes[level] = node;
  5710. path->slots[level] = 0;
  5711. path->locks[level] = 1;
  5712. wc->refs[parent_level] = 1;
  5713. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  5714. wc->level = level;
  5715. wc->shared_level = -1;
  5716. wc->stage = DROP_REFERENCE;
  5717. wc->update_ref = 0;
  5718. wc->keep_locks = 1;
  5719. wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
  5720. while (1) {
  5721. wret = walk_down_tree(trans, root, path, wc);
  5722. if (wret < 0) {
  5723. ret = wret;
  5724. break;
  5725. }
  5726. wret = walk_up_tree(trans, root, path, wc, parent_level);
  5727. if (wret < 0)
  5728. ret = wret;
  5729. if (wret != 0)
  5730. break;
  5731. }
  5732. kfree(wc);
  5733. btrfs_free_path(path);
  5734. return ret;
  5735. }
  5736. #if 0
  5737. static unsigned long calc_ra(unsigned long start, unsigned long last,
  5738. unsigned long nr)
  5739. {
  5740. return min(last, start + nr - 1);
  5741. }
  5742. static noinline int relocate_inode_pages(struct inode *inode, u64 start,
  5743. u64 len)
  5744. {
  5745. u64 page_start;
  5746. u64 page_end;
  5747. unsigned long first_index;
  5748. unsigned long last_index;
  5749. unsigned long i;
  5750. struct page *page;
  5751. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5752. struct file_ra_state *ra;
  5753. struct btrfs_ordered_extent *ordered;
  5754. unsigned int total_read = 0;
  5755. unsigned int total_dirty = 0;
  5756. int ret = 0;
  5757. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  5758. if (!ra)
  5759. return -ENOMEM;
  5760. mutex_lock(&inode->i_mutex);
  5761. first_index = start >> PAGE_CACHE_SHIFT;
  5762. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  5763. /* make sure the dirty trick played by the caller work */
  5764. ret = invalidate_inode_pages2_range(inode->i_mapping,
  5765. first_index, last_index);
  5766. if (ret)
  5767. goto out_unlock;
  5768. file_ra_state_init(ra, inode->i_mapping);
  5769. for (i = first_index ; i <= last_index; i++) {
  5770. if (total_read % ra->ra_pages == 0) {
  5771. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  5772. calc_ra(i, last_index, ra->ra_pages));
  5773. }
  5774. total_read++;
  5775. again:
  5776. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  5777. BUG_ON(1);
  5778. page = grab_cache_page(inode->i_mapping, i);
  5779. if (!page) {
  5780. ret = -ENOMEM;
  5781. goto out_unlock;
  5782. }
  5783. if (!PageUptodate(page)) {
  5784. btrfs_readpage(NULL, page);
  5785. lock_page(page);
  5786. if (!PageUptodate(page)) {
  5787. unlock_page(page);
  5788. page_cache_release(page);
  5789. ret = -EIO;
  5790. goto out_unlock;
  5791. }
  5792. }
  5793. wait_on_page_writeback(page);
  5794. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  5795. page_end = page_start + PAGE_CACHE_SIZE - 1;
  5796. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  5797. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  5798. if (ordered) {
  5799. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  5800. unlock_page(page);
  5801. page_cache_release(page);
  5802. btrfs_start_ordered_extent(inode, ordered, 1);
  5803. btrfs_put_ordered_extent(ordered);
  5804. goto again;
  5805. }
  5806. set_page_extent_mapped(page);
  5807. if (i == first_index)
  5808. set_extent_bits(io_tree, page_start, page_end,
  5809. EXTENT_BOUNDARY, GFP_NOFS);
  5810. btrfs_set_extent_delalloc(inode, page_start, page_end);
  5811. set_page_dirty(page);
  5812. total_dirty++;
  5813. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  5814. unlock_page(page);
  5815. page_cache_release(page);
  5816. }
  5817. out_unlock:
  5818. kfree(ra);
  5819. mutex_unlock(&inode->i_mutex);
  5820. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  5821. return ret;
  5822. }
  5823. static noinline int relocate_data_extent(struct inode *reloc_inode,
  5824. struct btrfs_key *extent_key,
  5825. u64 offset)
  5826. {
  5827. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  5828. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  5829. struct extent_map *em;
  5830. u64 start = extent_key->objectid - offset;
  5831. u64 end = start + extent_key->offset - 1;
  5832. em = alloc_extent_map(GFP_NOFS);
  5833. BUG_ON(!em);
  5834. em->start = start;
  5835. em->len = extent_key->offset;
  5836. em->block_len = extent_key->offset;
  5837. em->block_start = extent_key->objectid;
  5838. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5839. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  5840. /* setup extent map to cheat btrfs_readpage */
  5841. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  5842. while (1) {
  5843. int ret;
  5844. write_lock(&em_tree->lock);
  5845. ret = add_extent_mapping(em_tree, em);
  5846. write_unlock(&em_tree->lock);
  5847. if (ret != -EEXIST) {
  5848. free_extent_map(em);
  5849. break;
  5850. }
  5851. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  5852. }
  5853. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  5854. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  5855. }
  5856. struct btrfs_ref_path {
  5857. u64 extent_start;
  5858. u64 nodes[BTRFS_MAX_LEVEL];
  5859. u64 root_objectid;
  5860. u64 root_generation;
  5861. u64 owner_objectid;
  5862. u32 num_refs;
  5863. int lowest_level;
  5864. int current_level;
  5865. int shared_level;
  5866. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  5867. u64 new_nodes[BTRFS_MAX_LEVEL];
  5868. };
  5869. struct disk_extent {
  5870. u64 ram_bytes;
  5871. u64 disk_bytenr;
  5872. u64 disk_num_bytes;
  5873. u64 offset;
  5874. u64 num_bytes;
  5875. u8 compression;
  5876. u8 encryption;
  5877. u16 other_encoding;
  5878. };
  5879. static int is_cowonly_root(u64 root_objectid)
  5880. {
  5881. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  5882. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  5883. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  5884. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  5885. root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  5886. root_objectid == BTRFS_CSUM_TREE_OBJECTID)
  5887. return 1;
  5888. return 0;
  5889. }
  5890. static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
  5891. struct btrfs_root *extent_root,
  5892. struct btrfs_ref_path *ref_path,
  5893. int first_time)
  5894. {
  5895. struct extent_buffer *leaf;
  5896. struct btrfs_path *path;
  5897. struct btrfs_extent_ref *ref;
  5898. struct btrfs_key key;
  5899. struct btrfs_key found_key;
  5900. u64 bytenr;
  5901. u32 nritems;
  5902. int level;
  5903. int ret = 1;
  5904. path = btrfs_alloc_path();
  5905. if (!path)
  5906. return -ENOMEM;
  5907. if (first_time) {
  5908. ref_path->lowest_level = -1;
  5909. ref_path->current_level = -1;
  5910. ref_path->shared_level = -1;
  5911. goto walk_up;
  5912. }
  5913. walk_down:
  5914. level = ref_path->current_level - 1;
  5915. while (level >= -1) {
  5916. u64 parent;
  5917. if (level < ref_path->lowest_level)
  5918. break;
  5919. if (level >= 0)
  5920. bytenr = ref_path->nodes[level];
  5921. else
  5922. bytenr = ref_path->extent_start;
  5923. BUG_ON(bytenr == 0);
  5924. parent = ref_path->nodes[level + 1];
  5925. ref_path->nodes[level + 1] = 0;
  5926. ref_path->current_level = level;
  5927. BUG_ON(parent == 0);
  5928. key.objectid = bytenr;
  5929. key.offset = parent + 1;
  5930. key.type = BTRFS_EXTENT_REF_KEY;
  5931. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  5932. if (ret < 0)
  5933. goto out;
  5934. BUG_ON(ret == 0);
  5935. leaf = path->nodes[0];
  5936. nritems = btrfs_header_nritems(leaf);
  5937. if (path->slots[0] >= nritems) {
  5938. ret = btrfs_next_leaf(extent_root, path);
  5939. if (ret < 0)
  5940. goto out;
  5941. if (ret > 0)
  5942. goto next;
  5943. leaf = path->nodes[0];
  5944. }
  5945. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5946. if (found_key.objectid == bytenr &&
  5947. found_key.type == BTRFS_EXTENT_REF_KEY) {
  5948. if (level < ref_path->shared_level)
  5949. ref_path->shared_level = level;
  5950. goto found;
  5951. }
  5952. next:
  5953. level--;
  5954. btrfs_release_path(extent_root, path);
  5955. cond_resched();
  5956. }
  5957. /* reached lowest level */
  5958. ret = 1;
  5959. goto out;
  5960. walk_up:
  5961. level = ref_path->current_level;
  5962. while (level < BTRFS_MAX_LEVEL - 1) {
  5963. u64 ref_objectid;
  5964. if (level >= 0)
  5965. bytenr = ref_path->nodes[level];
  5966. else
  5967. bytenr = ref_path->extent_start;
  5968. BUG_ON(bytenr == 0);
  5969. key.objectid = bytenr;
  5970. key.offset = 0;
  5971. key.type = BTRFS_EXTENT_REF_KEY;
  5972. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  5973. if (ret < 0)
  5974. goto out;
  5975. leaf = path->nodes[0];
  5976. nritems = btrfs_header_nritems(leaf);
  5977. if (path->slots[0] >= nritems) {
  5978. ret = btrfs_next_leaf(extent_root, path);
  5979. if (ret < 0)
  5980. goto out;
  5981. if (ret > 0) {
  5982. /* the extent was freed by someone */
  5983. if (ref_path->lowest_level == level)
  5984. goto out;
  5985. btrfs_release_path(extent_root, path);
  5986. goto walk_down;
  5987. }
  5988. leaf = path->nodes[0];
  5989. }
  5990. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5991. if (found_key.objectid != bytenr ||
  5992. found_key.type != BTRFS_EXTENT_REF_KEY) {
  5993. /* the extent was freed by someone */
  5994. if (ref_path->lowest_level == level) {
  5995. ret = 1;
  5996. goto out;
  5997. }
  5998. btrfs_release_path(extent_root, path);
  5999. goto walk_down;
  6000. }
  6001. found:
  6002. ref = btrfs_item_ptr(leaf, path->slots[0],
  6003. struct btrfs_extent_ref);
  6004. ref_objectid = btrfs_ref_objectid(leaf, ref);
  6005. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  6006. if (first_time) {
  6007. level = (int)ref_objectid;
  6008. BUG_ON(level >= BTRFS_MAX_LEVEL);
  6009. ref_path->lowest_level = level;
  6010. ref_path->current_level = level;
  6011. ref_path->nodes[level] = bytenr;
  6012. } else {
  6013. WARN_ON(ref_objectid != level);
  6014. }
  6015. } else {
  6016. WARN_ON(level != -1);
  6017. }
  6018. first_time = 0;
  6019. if (ref_path->lowest_level == level) {
  6020. ref_path->owner_objectid = ref_objectid;
  6021. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  6022. }
  6023. /*
  6024. * the block is tree root or the block isn't in reference
  6025. * counted tree.
  6026. */
  6027. if (found_key.objectid == found_key.offset ||
  6028. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  6029. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  6030. ref_path->root_generation =
  6031. btrfs_ref_generation(leaf, ref);
  6032. if (level < 0) {
  6033. /* special reference from the tree log */
  6034. ref_path->nodes[0] = found_key.offset;
  6035. ref_path->current_level = 0;
  6036. }
  6037. ret = 0;
  6038. goto out;
  6039. }
  6040. level++;
  6041. BUG_ON(ref_path->nodes[level] != 0);
  6042. ref_path->nodes[level] = found_key.offset;
  6043. ref_path->current_level = level;
  6044. /*
  6045. * the reference was created in the running transaction,
  6046. * no need to continue walking up.
  6047. */
  6048. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  6049. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  6050. ref_path->root_generation =
  6051. btrfs_ref_generation(leaf, ref);
  6052. ret = 0;
  6053. goto out;
  6054. }
  6055. btrfs_release_path(extent_root, path);
  6056. cond_resched();
  6057. }
  6058. /* reached max tree level, but no tree root found. */
  6059. BUG();
  6060. out:
  6061. btrfs_free_path(path);
  6062. return ret;
  6063. }
  6064. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  6065. struct btrfs_root *extent_root,
  6066. struct btrfs_ref_path *ref_path,
  6067. u64 extent_start)
  6068. {
  6069. memset(ref_path, 0, sizeof(*ref_path));
  6070. ref_path->extent_start = extent_start;
  6071. return __next_ref_path(trans, extent_root, ref_path, 1);
  6072. }
  6073. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  6074. struct btrfs_root *extent_root,
  6075. struct btrfs_ref_path *ref_path)
  6076. {
  6077. return __next_ref_path(trans, extent_root, ref_path, 0);
  6078. }
  6079. static noinline int get_new_locations(struct inode *reloc_inode,
  6080. struct btrfs_key *extent_key,
  6081. u64 offset, int no_fragment,
  6082. struct disk_extent **extents,
  6083. int *nr_extents)
  6084. {
  6085. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  6086. struct btrfs_path *path;
  6087. struct btrfs_file_extent_item *fi;
  6088. struct extent_buffer *leaf;
  6089. struct disk_extent *exts = *extents;
  6090. struct btrfs_key found_key;
  6091. u64 cur_pos;
  6092. u64 last_byte;
  6093. u32 nritems;
  6094. int nr = 0;
  6095. int max = *nr_extents;
  6096. int ret;
  6097. WARN_ON(!no_fragment && *extents);
  6098. if (!exts) {
  6099. max = 1;
  6100. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  6101. if (!exts)
  6102. return -ENOMEM;
  6103. }
  6104. path = btrfs_alloc_path();
  6105. if (!path) {
  6106. if (exts != *extents)
  6107. kfree(exts);
  6108. return -ENOMEM;
  6109. }
  6110. cur_pos = extent_key->objectid - offset;
  6111. last_byte = extent_key->objectid + extent_key->offset;
  6112. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  6113. cur_pos, 0);
  6114. if (ret < 0)
  6115. goto out;
  6116. if (ret > 0) {
  6117. ret = -ENOENT;
  6118. goto out;
  6119. }
  6120. while (1) {
  6121. leaf = path->nodes[0];
  6122. nritems = btrfs_header_nritems(leaf);
  6123. if (path->slots[0] >= nritems) {
  6124. ret = btrfs_next_leaf(root, path);
  6125. if (ret < 0)
  6126. goto out;
  6127. if (ret > 0)
  6128. break;
  6129. leaf = path->nodes[0];
  6130. }
  6131. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6132. if (found_key.offset != cur_pos ||
  6133. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  6134. found_key.objectid != reloc_inode->i_ino)
  6135. break;
  6136. fi = btrfs_item_ptr(leaf, path->slots[0],
  6137. struct btrfs_file_extent_item);
  6138. if (btrfs_file_extent_type(leaf, fi) !=
  6139. BTRFS_FILE_EXTENT_REG ||
  6140. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  6141. break;
  6142. if (nr == max) {
  6143. struct disk_extent *old = exts;
  6144. max *= 2;
  6145. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  6146. memcpy(exts, old, sizeof(*exts) * nr);
  6147. if (old != *extents)
  6148. kfree(old);
  6149. }
  6150. exts[nr].disk_bytenr =
  6151. btrfs_file_extent_disk_bytenr(leaf, fi);
  6152. exts[nr].disk_num_bytes =
  6153. btrfs_file_extent_disk_num_bytes(leaf, fi);
  6154. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  6155. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  6156. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  6157. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  6158. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  6159. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  6160. fi);
  6161. BUG_ON(exts[nr].offset > 0);
  6162. BUG_ON(exts[nr].compression || exts[nr].encryption);
  6163. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  6164. cur_pos += exts[nr].num_bytes;
  6165. nr++;
  6166. if (cur_pos + offset >= last_byte)
  6167. break;
  6168. if (no_fragment) {
  6169. ret = 1;
  6170. goto out;
  6171. }
  6172. path->slots[0]++;
  6173. }
  6174. BUG_ON(cur_pos + offset > last_byte);
  6175. if (cur_pos + offset < last_byte) {
  6176. ret = -ENOENT;
  6177. goto out;
  6178. }
  6179. ret = 0;
  6180. out:
  6181. btrfs_free_path(path);
  6182. if (ret) {
  6183. if (exts != *extents)
  6184. kfree(exts);
  6185. } else {
  6186. *extents = exts;
  6187. *nr_extents = nr;
  6188. }
  6189. return ret;
  6190. }
  6191. static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
  6192. struct btrfs_root *root,
  6193. struct btrfs_path *path,
  6194. struct btrfs_key *extent_key,
  6195. struct btrfs_key *leaf_key,
  6196. struct btrfs_ref_path *ref_path,
  6197. struct disk_extent *new_extents,
  6198. int nr_extents)
  6199. {
  6200. struct extent_buffer *leaf;
  6201. struct btrfs_file_extent_item *fi;
  6202. struct inode *inode = NULL;
  6203. struct btrfs_key key;
  6204. u64 lock_start = 0;
  6205. u64 lock_end = 0;
  6206. u64 num_bytes;
  6207. u64 ext_offset;
  6208. u64 search_end = (u64)-1;
  6209. u32 nritems;
  6210. int nr_scaned = 0;
  6211. int extent_locked = 0;
  6212. int extent_type;
  6213. int ret;
  6214. memcpy(&key, leaf_key, sizeof(key));
  6215. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  6216. if (key.objectid < ref_path->owner_objectid ||
  6217. (key.objectid == ref_path->owner_objectid &&
  6218. key.type < BTRFS_EXTENT_DATA_KEY)) {
  6219. key.objectid = ref_path->owner_objectid;
  6220. key.type = BTRFS_EXTENT_DATA_KEY;
  6221. key.offset = 0;
  6222. }
  6223. }
  6224. while (1) {
  6225. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  6226. if (ret < 0)
  6227. goto out;
  6228. leaf = path->nodes[0];
  6229. nritems = btrfs_header_nritems(leaf);
  6230. next:
  6231. if (extent_locked && ret > 0) {
  6232. /*
  6233. * the file extent item was modified by someone
  6234. * before the extent got locked.
  6235. */
  6236. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  6237. lock_end, GFP_NOFS);
  6238. extent_locked = 0;
  6239. }
  6240. if (path->slots[0] >= nritems) {
  6241. if (++nr_scaned > 2)
  6242. break;
  6243. BUG_ON(extent_locked);
  6244. ret = btrfs_next_leaf(root, path);
  6245. if (ret < 0)
  6246. goto out;
  6247. if (ret > 0)
  6248. break;
  6249. leaf = path->nodes[0];
  6250. nritems = btrfs_header_nritems(leaf);
  6251. }
  6252. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  6253. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  6254. if ((key.objectid > ref_path->owner_objectid) ||
  6255. (key.objectid == ref_path->owner_objectid &&
  6256. key.type > BTRFS_EXTENT_DATA_KEY) ||
  6257. key.offset >= search_end)
  6258. break;
  6259. }
  6260. if (inode && key.objectid != inode->i_ino) {
  6261. BUG_ON(extent_locked);
  6262. btrfs_release_path(root, path);
  6263. mutex_unlock(&inode->i_mutex);
  6264. iput(inode);
  6265. inode = NULL;
  6266. continue;
  6267. }
  6268. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  6269. path->slots[0]++;
  6270. ret = 1;
  6271. goto next;
  6272. }
  6273. fi = btrfs_item_ptr(leaf, path->slots[0],
  6274. struct btrfs_file_extent_item);
  6275. extent_type = btrfs_file_extent_type(leaf, fi);
  6276. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  6277. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  6278. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  6279. extent_key->objectid)) {
  6280. path->slots[0]++;
  6281. ret = 1;
  6282. goto next;
  6283. }
  6284. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  6285. ext_offset = btrfs_file_extent_offset(leaf, fi);
  6286. if (search_end == (u64)-1) {
  6287. search_end = key.offset - ext_offset +
  6288. btrfs_file_extent_ram_bytes(leaf, fi);
  6289. }
  6290. if (!extent_locked) {
  6291. lock_start = key.offset;
  6292. lock_end = lock_start + num_bytes - 1;
  6293. } else {
  6294. if (lock_start > key.offset ||
  6295. lock_end + 1 < key.offset + num_bytes) {
  6296. unlock_extent(&BTRFS_I(inode)->io_tree,
  6297. lock_start, lock_end, GFP_NOFS);
  6298. extent_locked = 0;
  6299. }
  6300. }
  6301. if (!inode) {
  6302. btrfs_release_path(root, path);
  6303. inode = btrfs_iget_locked(root->fs_info->sb,
  6304. key.objectid, root);
  6305. if (inode->i_state & I_NEW) {
  6306. BTRFS_I(inode)->root = root;
  6307. BTRFS_I(inode)->location.objectid =
  6308. key.objectid;
  6309. BTRFS_I(inode)->location.type =
  6310. BTRFS_INODE_ITEM_KEY;
  6311. BTRFS_I(inode)->location.offset = 0;
  6312. btrfs_read_locked_inode(inode);
  6313. unlock_new_inode(inode);
  6314. }
  6315. /*
  6316. * some code call btrfs_commit_transaction while
  6317. * holding the i_mutex, so we can't use mutex_lock
  6318. * here.
  6319. */
  6320. if (is_bad_inode(inode) ||
  6321. !mutex_trylock(&inode->i_mutex)) {
  6322. iput(inode);
  6323. inode = NULL;
  6324. key.offset = (u64)-1;
  6325. goto skip;
  6326. }
  6327. }
  6328. if (!extent_locked) {
  6329. struct btrfs_ordered_extent *ordered;
  6330. btrfs_release_path(root, path);
  6331. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  6332. lock_end, GFP_NOFS);
  6333. ordered = btrfs_lookup_first_ordered_extent(inode,
  6334. lock_end);
  6335. if (ordered &&
  6336. ordered->file_offset <= lock_end &&
  6337. ordered->file_offset + ordered->len > lock_start) {
  6338. unlock_extent(&BTRFS_I(inode)->io_tree,
  6339. lock_start, lock_end, GFP_NOFS);
  6340. btrfs_start_ordered_extent(inode, ordered, 1);
  6341. btrfs_put_ordered_extent(ordered);
  6342. key.offset += num_bytes;
  6343. goto skip;
  6344. }
  6345. if (ordered)
  6346. btrfs_put_ordered_extent(ordered);
  6347. extent_locked = 1;
  6348. continue;
  6349. }
  6350. if (nr_extents == 1) {
  6351. /* update extent pointer in place */
  6352. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  6353. new_extents[0].disk_bytenr);
  6354. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  6355. new_extents[0].disk_num_bytes);
  6356. btrfs_mark_buffer_dirty(leaf);
  6357. btrfs_drop_extent_cache(inode, key.offset,
  6358. key.offset + num_bytes - 1, 0);
  6359. ret = btrfs_inc_extent_ref(trans, root,
  6360. new_extents[0].disk_bytenr,
  6361. new_extents[0].disk_num_bytes,
  6362. leaf->start,
  6363. root->root_key.objectid,
  6364. trans->transid,
  6365. key.objectid);
  6366. BUG_ON(ret);
  6367. ret = btrfs_free_extent(trans, root,
  6368. extent_key->objectid,
  6369. extent_key->offset,
  6370. leaf->start,
  6371. btrfs_header_owner(leaf),
  6372. btrfs_header_generation(leaf),
  6373. key.objectid, 0);
  6374. BUG_ON(ret);
  6375. btrfs_release_path(root, path);
  6376. key.offset += num_bytes;
  6377. } else {
  6378. BUG_ON(1);
  6379. #if 0
  6380. u64 alloc_hint;
  6381. u64 extent_len;
  6382. int i;
  6383. /*
  6384. * drop old extent pointer at first, then insert the
  6385. * new pointers one bye one
  6386. */
  6387. btrfs_release_path(root, path);
  6388. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  6389. key.offset + num_bytes,
  6390. key.offset, &alloc_hint);
  6391. BUG_ON(ret);
  6392. for (i = 0; i < nr_extents; i++) {
  6393. if (ext_offset >= new_extents[i].num_bytes) {
  6394. ext_offset -= new_extents[i].num_bytes;
  6395. continue;
  6396. }
  6397. extent_len = min(new_extents[i].num_bytes -
  6398. ext_offset, num_bytes);
  6399. ret = btrfs_insert_empty_item(trans, root,
  6400. path, &key,
  6401. sizeof(*fi));
  6402. BUG_ON(ret);
  6403. leaf = path->nodes[0];
  6404. fi = btrfs_item_ptr(leaf, path->slots[0],
  6405. struct btrfs_file_extent_item);
  6406. btrfs_set_file_extent_generation(leaf, fi,
  6407. trans->transid);
  6408. btrfs_set_file_extent_type(leaf, fi,
  6409. BTRFS_FILE_EXTENT_REG);
  6410. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  6411. new_extents[i].disk_bytenr);
  6412. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  6413. new_extents[i].disk_num_bytes);
  6414. btrfs_set_file_extent_ram_bytes(leaf, fi,
  6415. new_extents[i].ram_bytes);
  6416. btrfs_set_file_extent_compression(leaf, fi,
  6417. new_extents[i].compression);
  6418. btrfs_set_file_extent_encryption(leaf, fi,
  6419. new_extents[i].encryption);
  6420. btrfs_set_file_extent_other_encoding(leaf, fi,
  6421. new_extents[i].other_encoding);
  6422. btrfs_set_file_extent_num_bytes(leaf, fi,
  6423. extent_len);
  6424. ext_offset += new_extents[i].offset;
  6425. btrfs_set_file_extent_offset(leaf, fi,
  6426. ext_offset);
  6427. btrfs_mark_buffer_dirty(leaf);
  6428. btrfs_drop_extent_cache(inode, key.offset,
  6429. key.offset + extent_len - 1, 0);
  6430. ret = btrfs_inc_extent_ref(trans, root,
  6431. new_extents[i].disk_bytenr,
  6432. new_extents[i].disk_num_bytes,
  6433. leaf->start,
  6434. root->root_key.objectid,
  6435. trans->transid, key.objectid);
  6436. BUG_ON(ret);
  6437. btrfs_release_path(root, path);
  6438. inode_add_bytes(inode, extent_len);
  6439. ext_offset = 0;
  6440. num_bytes -= extent_len;
  6441. key.offset += extent_len;
  6442. if (num_bytes == 0)
  6443. break;
  6444. }
  6445. BUG_ON(i >= nr_extents);
  6446. #endif
  6447. }
  6448. if (extent_locked) {
  6449. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  6450. lock_end, GFP_NOFS);
  6451. extent_locked = 0;
  6452. }
  6453. skip:
  6454. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  6455. key.offset >= search_end)
  6456. break;
  6457. cond_resched();
  6458. }
  6459. ret = 0;
  6460. out:
  6461. btrfs_release_path(root, path);
  6462. if (inode) {
  6463. mutex_unlock(&inode->i_mutex);
  6464. if (extent_locked) {
  6465. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  6466. lock_end, GFP_NOFS);
  6467. }
  6468. iput(inode);
  6469. }
  6470. return ret;
  6471. }
  6472. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  6473. struct btrfs_root *root,
  6474. struct extent_buffer *buf, u64 orig_start)
  6475. {
  6476. int level;
  6477. int ret;
  6478. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  6479. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  6480. level = btrfs_header_level(buf);
  6481. if (level == 0) {
  6482. struct btrfs_leaf_ref *ref;
  6483. struct btrfs_leaf_ref *orig_ref;
  6484. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  6485. if (!orig_ref)
  6486. return -ENOENT;
  6487. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  6488. if (!ref) {
  6489. btrfs_free_leaf_ref(root, orig_ref);
  6490. return -ENOMEM;
  6491. }
  6492. ref->nritems = orig_ref->nritems;
  6493. memcpy(ref->extents, orig_ref->extents,
  6494. sizeof(ref->extents[0]) * ref->nritems);
  6495. btrfs_free_leaf_ref(root, orig_ref);
  6496. ref->root_gen = trans->transid;
  6497. ref->bytenr = buf->start;
  6498. ref->owner = btrfs_header_owner(buf);
  6499. ref->generation = btrfs_header_generation(buf);
  6500. ret = btrfs_add_leaf_ref(root, ref, 0);
  6501. WARN_ON(ret);
  6502. btrfs_free_leaf_ref(root, ref);
  6503. }
  6504. return 0;
  6505. }
  6506. static noinline int invalidate_extent_cache(struct btrfs_root *root,
  6507. struct extent_buffer *leaf,
  6508. struct btrfs_block_group_cache *group,
  6509. struct btrfs_root *target_root)
  6510. {
  6511. struct btrfs_key key;
  6512. struct inode *inode = NULL;
  6513. struct btrfs_file_extent_item *fi;
  6514. struct extent_state *cached_state = NULL;
  6515. u64 num_bytes;
  6516. u64 skip_objectid = 0;
  6517. u32 nritems;
  6518. u32 i;
  6519. nritems = btrfs_header_nritems(leaf);
  6520. for (i = 0; i < nritems; i++) {
  6521. btrfs_item_key_to_cpu(leaf, &key, i);
  6522. if (key.objectid == skip_objectid ||
  6523. key.type != BTRFS_EXTENT_DATA_KEY)
  6524. continue;
  6525. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  6526. if (btrfs_file_extent_type(leaf, fi) ==
  6527. BTRFS_FILE_EXTENT_INLINE)
  6528. continue;
  6529. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  6530. continue;
  6531. if (!inode || inode->i_ino != key.objectid) {
  6532. iput(inode);
  6533. inode = btrfs_ilookup(target_root->fs_info->sb,
  6534. key.objectid, target_root, 1);
  6535. }
  6536. if (!inode) {
  6537. skip_objectid = key.objectid;
  6538. continue;
  6539. }
  6540. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  6541. lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
  6542. key.offset + num_bytes - 1, 0, &cached_state,
  6543. GFP_NOFS);
  6544. btrfs_drop_extent_cache(inode, key.offset,
  6545. key.offset + num_bytes - 1, 1);
  6546. unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
  6547. key.offset + num_bytes - 1, &cached_state,
  6548. GFP_NOFS);
  6549. cond_resched();
  6550. }
  6551. iput(inode);
  6552. return 0;
  6553. }
  6554. static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  6555. struct btrfs_root *root,
  6556. struct extent_buffer *leaf,
  6557. struct btrfs_block_group_cache *group,
  6558. struct inode *reloc_inode)
  6559. {
  6560. struct btrfs_key key;
  6561. struct btrfs_key extent_key;
  6562. struct btrfs_file_extent_item *fi;
  6563. struct btrfs_leaf_ref *ref;
  6564. struct disk_extent *new_extent;
  6565. u64 bytenr;
  6566. u64 num_bytes;
  6567. u32 nritems;
  6568. u32 i;
  6569. int ext_index;
  6570. int nr_extent;
  6571. int ret;
  6572. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  6573. if (!new_extent)
  6574. return -ENOMEM;
  6575. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  6576. BUG_ON(!ref);
  6577. ext_index = -1;
  6578. nritems = btrfs_header_nritems(leaf);
  6579. for (i = 0; i < nritems; i++) {
  6580. btrfs_item_key_to_cpu(leaf, &key, i);
  6581. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  6582. continue;
  6583. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  6584. if (btrfs_file_extent_type(leaf, fi) ==
  6585. BTRFS_FILE_EXTENT_INLINE)
  6586. continue;
  6587. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  6588. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  6589. if (bytenr == 0)
  6590. continue;
  6591. ext_index++;
  6592. if (bytenr >= group->key.objectid + group->key.offset ||
  6593. bytenr + num_bytes <= group->key.objectid)
  6594. continue;
  6595. extent_key.objectid = bytenr;
  6596. extent_key.offset = num_bytes;
  6597. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  6598. nr_extent = 1;
  6599. ret = get_new_locations(reloc_inode, &extent_key,
  6600. group->key.objectid, 1,
  6601. &new_extent, &nr_extent);
  6602. if (ret > 0)
  6603. continue;
  6604. BUG_ON(ret < 0);
  6605. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  6606. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  6607. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  6608. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  6609. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  6610. new_extent->disk_bytenr);
  6611. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  6612. new_extent->disk_num_bytes);
  6613. btrfs_mark_buffer_dirty(leaf);
  6614. ret = btrfs_inc_extent_ref(trans, root,
  6615. new_extent->disk_bytenr,
  6616. new_extent->disk_num_bytes,
  6617. leaf->start,
  6618. root->root_key.objectid,
  6619. trans->transid, key.objectid);
  6620. BUG_ON(ret);
  6621. ret = btrfs_free_extent(trans, root,
  6622. bytenr, num_bytes, leaf->start,
  6623. btrfs_header_owner(leaf),
  6624. btrfs_header_generation(leaf),
  6625. key.objectid, 0);
  6626. BUG_ON(ret);
  6627. cond_resched();
  6628. }
  6629. kfree(new_extent);
  6630. BUG_ON(ext_index + 1 != ref->nritems);
  6631. btrfs_free_leaf_ref(root, ref);
  6632. return 0;
  6633. }
  6634. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  6635. struct btrfs_root *root)
  6636. {
  6637. struct btrfs_root *reloc_root;
  6638. int ret;
  6639. if (root->reloc_root) {
  6640. reloc_root = root->reloc_root;
  6641. root->reloc_root = NULL;
  6642. list_add(&reloc_root->dead_list,
  6643. &root->fs_info->dead_reloc_roots);
  6644. btrfs_set_root_bytenr(&reloc_root->root_item,
  6645. reloc_root->node->start);
  6646. btrfs_set_root_level(&root->root_item,
  6647. btrfs_header_level(reloc_root->node));
  6648. memset(&reloc_root->root_item.drop_progress, 0,
  6649. sizeof(struct btrfs_disk_key));
  6650. reloc_root->root_item.drop_level = 0;
  6651. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  6652. &reloc_root->root_key,
  6653. &reloc_root->root_item);
  6654. BUG_ON(ret);
  6655. }
  6656. return 0;
  6657. }
  6658. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  6659. {
  6660. struct btrfs_trans_handle *trans;
  6661. struct btrfs_root *reloc_root;
  6662. struct btrfs_root *prev_root = NULL;
  6663. struct list_head dead_roots;
  6664. int ret;
  6665. unsigned long nr;
  6666. INIT_LIST_HEAD(&dead_roots);
  6667. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  6668. while (!list_empty(&dead_roots)) {
  6669. reloc_root = list_entry(dead_roots.prev,
  6670. struct btrfs_root, dead_list);
  6671. list_del_init(&reloc_root->dead_list);
  6672. BUG_ON(reloc_root->commit_root != NULL);
  6673. while (1) {
  6674. trans = btrfs_join_transaction(root, 1);
  6675. BUG_ON(IS_ERR(trans));
  6676. mutex_lock(&root->fs_info->drop_mutex);
  6677. ret = btrfs_drop_snapshot(trans, reloc_root);
  6678. if (ret != -EAGAIN)
  6679. break;
  6680. mutex_unlock(&root->fs_info->drop_mutex);
  6681. nr = trans->blocks_used;
  6682. ret = btrfs_end_transaction(trans, root);
  6683. BUG_ON(ret);
  6684. btrfs_btree_balance_dirty(root, nr);
  6685. }
  6686. free_extent_buffer(reloc_root->node);
  6687. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  6688. &reloc_root->root_key);
  6689. BUG_ON(ret);
  6690. mutex_unlock(&root->fs_info->drop_mutex);
  6691. nr = trans->blocks_used;
  6692. ret = btrfs_end_transaction(trans, root);
  6693. BUG_ON(ret);
  6694. btrfs_btree_balance_dirty(root, nr);
  6695. kfree(prev_root);
  6696. prev_root = reloc_root;
  6697. }
  6698. if (prev_root) {
  6699. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  6700. kfree(prev_root);
  6701. }
  6702. return 0;
  6703. }
  6704. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  6705. {
  6706. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  6707. return 0;
  6708. }
  6709. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  6710. {
  6711. struct btrfs_root *reloc_root;
  6712. struct btrfs_trans_handle *trans;
  6713. struct btrfs_key location;
  6714. int found;
  6715. int ret;
  6716. mutex_lock(&root->fs_info->tree_reloc_mutex);
  6717. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  6718. BUG_ON(ret);
  6719. found = !list_empty(&root->fs_info->dead_reloc_roots);
  6720. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  6721. if (found) {
  6722. trans = btrfs_start_transaction(root, 1);
  6723. BUG_ON(IS_ERR(trans));
  6724. ret = btrfs_commit_transaction(trans, root);
  6725. BUG_ON(ret);
  6726. }
  6727. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  6728. location.offset = (u64)-1;
  6729. location.type = BTRFS_ROOT_ITEM_KEY;
  6730. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  6731. BUG_ON(!reloc_root);
  6732. ret = btrfs_orphan_cleanup(reloc_root);
  6733. BUG_ON(ret);
  6734. return 0;
  6735. }
  6736. static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
  6737. struct btrfs_root *root)
  6738. {
  6739. struct btrfs_root *reloc_root;
  6740. struct extent_buffer *eb;
  6741. struct btrfs_root_item *root_item;
  6742. struct btrfs_key root_key;
  6743. int ret;
  6744. BUG_ON(!root->ref_cows);
  6745. if (root->reloc_root)
  6746. return 0;
  6747. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  6748. if (!root_item)
  6749. return -ENOMEM;
  6750. ret = btrfs_copy_root(trans, root, root->commit_root,
  6751. &eb, BTRFS_TREE_RELOC_OBJECTID);
  6752. BUG_ON(ret);
  6753. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  6754. root_key.offset = root->root_key.objectid;
  6755. root_key.type = BTRFS_ROOT_ITEM_KEY;
  6756. memcpy(root_item, &root->root_item, sizeof(root_item));
  6757. btrfs_set_root_refs(root_item, 0);
  6758. btrfs_set_root_bytenr(root_item, eb->start);
  6759. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  6760. btrfs_set_root_generation(root_item, trans->transid);
  6761. btrfs_tree_unlock(eb);
  6762. free_extent_buffer(eb);
  6763. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  6764. &root_key, root_item);
  6765. BUG_ON(ret);
  6766. kfree(root_item);
  6767. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  6768. &root_key);
  6769. BUG_ON(IS_ERR(reloc_root));
  6770. reloc_root->last_trans = trans->transid;
  6771. reloc_root->commit_root = NULL;
  6772. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  6773. root->reloc_root = reloc_root;
  6774. return 0;
  6775. }
  6776. /*
  6777. * Core function of space balance.
  6778. *
  6779. * The idea is using reloc trees to relocate tree blocks in reference
  6780. * counted roots. There is one reloc tree for each subvol, and all
  6781. * reloc trees share same root key objectid. Reloc trees are snapshots
  6782. * of the latest committed roots of subvols (root->commit_root).
  6783. *
  6784. * To relocate a tree block referenced by a subvol, there are two steps.
  6785. * COW the block through subvol's reloc tree, then update block pointer
  6786. * in the subvol to point to the new block. Since all reloc trees share
  6787. * same root key objectid, doing special handing for tree blocks owned
  6788. * by them is easy. Once a tree block has been COWed in one reloc tree,
  6789. * we can use the resulting new block directly when the same block is
  6790. * required to COW again through other reloc trees. By this way, relocated
  6791. * tree blocks are shared between reloc trees, so they are also shared
  6792. * between subvols.
  6793. */
  6794. static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
  6795. struct btrfs_root *root,
  6796. struct btrfs_path *path,
  6797. struct btrfs_key *first_key,
  6798. struct btrfs_ref_path *ref_path,
  6799. struct btrfs_block_group_cache *group,
  6800. struct inode *reloc_inode)
  6801. {
  6802. struct btrfs_root *reloc_root;
  6803. struct extent_buffer *eb = NULL;
  6804. struct btrfs_key *keys;
  6805. u64 *nodes;
  6806. int level;
  6807. int shared_level;
  6808. int lowest_level = 0;
  6809. int ret;
  6810. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  6811. lowest_level = ref_path->owner_objectid;
  6812. if (!root->ref_cows) {
  6813. path->lowest_level = lowest_level;
  6814. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  6815. BUG_ON(ret < 0);
  6816. path->lowest_level = 0;
  6817. btrfs_release_path(root, path);
  6818. return 0;
  6819. }
  6820. mutex_lock(&root->fs_info->tree_reloc_mutex);
  6821. ret = init_reloc_tree(trans, root);
  6822. BUG_ON(ret);
  6823. reloc_root = root->reloc_root;
  6824. shared_level = ref_path->shared_level;
  6825. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  6826. keys = ref_path->node_keys;
  6827. nodes = ref_path->new_nodes;
  6828. memset(&keys[shared_level + 1], 0,
  6829. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  6830. memset(&nodes[shared_level + 1], 0,
  6831. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  6832. if (nodes[lowest_level] == 0) {
  6833. path->lowest_level = lowest_level;
  6834. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  6835. 0, 1);
  6836. BUG_ON(ret);
  6837. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  6838. eb = path->nodes[level];
  6839. if (!eb || eb == reloc_root->node)
  6840. break;
  6841. nodes[level] = eb->start;
  6842. if (level == 0)
  6843. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  6844. else
  6845. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  6846. }
  6847. if (nodes[0] &&
  6848. ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6849. eb = path->nodes[0];
  6850. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  6851. group, reloc_inode);
  6852. BUG_ON(ret);
  6853. }
  6854. btrfs_release_path(reloc_root, path);
  6855. } else {
  6856. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  6857. lowest_level);
  6858. BUG_ON(ret);
  6859. }
  6860. /*
  6861. * replace tree blocks in the fs tree with tree blocks in
  6862. * the reloc tree.
  6863. */
  6864. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  6865. BUG_ON(ret < 0);
  6866. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6867. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  6868. 0, 0);
  6869. BUG_ON(ret);
  6870. extent_buffer_get(path->nodes[0]);
  6871. eb = path->nodes[0];
  6872. btrfs_release_path(reloc_root, path);
  6873. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  6874. BUG_ON(ret);
  6875. free_extent_buffer(eb);
  6876. }
  6877. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  6878. path->lowest_level = 0;
  6879. return 0;
  6880. }
  6881. static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
  6882. struct btrfs_root *root,
  6883. struct btrfs_path *path,
  6884. struct btrfs_key *first_key,
  6885. struct btrfs_ref_path *ref_path)
  6886. {
  6887. int ret;
  6888. ret = relocate_one_path(trans, root, path, first_key,
  6889. ref_path, NULL, NULL);
  6890. BUG_ON(ret);
  6891. return 0;
  6892. }
  6893. static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
  6894. struct btrfs_root *extent_root,
  6895. struct btrfs_path *path,
  6896. struct btrfs_key *extent_key)
  6897. {
  6898. int ret;
  6899. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  6900. if (ret)
  6901. goto out;
  6902. ret = btrfs_del_item(trans, extent_root, path);
  6903. out:
  6904. btrfs_release_path(extent_root, path);
  6905. return ret;
  6906. }
  6907. static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
  6908. struct btrfs_ref_path *ref_path)
  6909. {
  6910. struct btrfs_key root_key;
  6911. root_key.objectid = ref_path->root_objectid;
  6912. root_key.type = BTRFS_ROOT_ITEM_KEY;
  6913. if (is_cowonly_root(ref_path->root_objectid))
  6914. root_key.offset = 0;
  6915. else
  6916. root_key.offset = (u64)-1;
  6917. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  6918. }
  6919. static noinline int relocate_one_extent(struct btrfs_root *extent_root,
  6920. struct btrfs_path *path,
  6921. struct btrfs_key *extent_key,
  6922. struct btrfs_block_group_cache *group,
  6923. struct inode *reloc_inode, int pass)
  6924. {
  6925. struct btrfs_trans_handle *trans;
  6926. struct btrfs_root *found_root;
  6927. struct btrfs_ref_path *ref_path = NULL;
  6928. struct disk_extent *new_extents = NULL;
  6929. int nr_extents = 0;
  6930. int loops;
  6931. int ret;
  6932. int level;
  6933. struct btrfs_key first_key;
  6934. u64 prev_block = 0;
  6935. trans = btrfs_start_transaction(extent_root, 1);
  6936. BUG_ON(IS_ERR(trans));
  6937. if (extent_key->objectid == 0) {
  6938. ret = del_extent_zero(trans, extent_root, path, extent_key);
  6939. goto out;
  6940. }
  6941. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  6942. if (!ref_path) {
  6943. ret = -ENOMEM;
  6944. goto out;
  6945. }
  6946. for (loops = 0; ; loops++) {
  6947. if (loops == 0) {
  6948. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  6949. extent_key->objectid);
  6950. } else {
  6951. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  6952. }
  6953. if (ret < 0)
  6954. goto out;
  6955. if (ret > 0)
  6956. break;
  6957. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  6958. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  6959. continue;
  6960. found_root = read_ref_root(extent_root->fs_info, ref_path);
  6961. BUG_ON(!found_root);
  6962. /*
  6963. * for reference counted tree, only process reference paths
  6964. * rooted at the latest committed root.
  6965. */
  6966. if (found_root->ref_cows &&
  6967. ref_path->root_generation != found_root->root_key.offset)
  6968. continue;
  6969. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  6970. if (pass == 0) {
  6971. /*
  6972. * copy data extents to new locations
  6973. */
  6974. u64 group_start = group->key.objectid;
  6975. ret = relocate_data_extent(reloc_inode,
  6976. extent_key,
  6977. group_start);
  6978. if (ret < 0)
  6979. goto out;
  6980. break;
  6981. }
  6982. level = 0;
  6983. } else {
  6984. level = ref_path->owner_objectid;
  6985. }
  6986. if (prev_block != ref_path->nodes[level]) {
  6987. struct extent_buffer *eb;
  6988. u64 block_start = ref_path->nodes[level];
  6989. u64 block_size = btrfs_level_size(found_root, level);
  6990. eb = read_tree_block(found_root, block_start,
  6991. block_size, 0);
  6992. if (!eb) {
  6993. ret = -EIO;
  6994. goto out;
  6995. }
  6996. btrfs_tree_lock(eb);
  6997. BUG_ON(level != btrfs_header_level(eb));
  6998. if (level == 0)
  6999. btrfs_item_key_to_cpu(eb, &first_key, 0);
  7000. else
  7001. btrfs_node_key_to_cpu(eb, &first_key, 0);
  7002. btrfs_tree_unlock(eb);
  7003. free_extent_buffer(eb);
  7004. prev_block = block_start;
  7005. }
  7006. mutex_lock(&extent_root->fs_info->trans_mutex);
  7007. btrfs_record_root_in_trans(found_root);
  7008. mutex_unlock(&extent_root->fs_info->trans_mutex);
  7009. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  7010. /*
  7011. * try to update data extent references while
  7012. * keeping metadata shared between snapshots.
  7013. */
  7014. if (pass == 1) {
  7015. ret = relocate_one_path(trans, found_root,
  7016. path, &first_key, ref_path,
  7017. group, reloc_inode);
  7018. if (ret < 0)
  7019. goto out;
  7020. continue;
  7021. }
  7022. /*
  7023. * use fallback method to process the remaining
  7024. * references.
  7025. */
  7026. if (!new_extents) {
  7027. u64 group_start = group->key.objectid;
  7028. new_extents = kmalloc(sizeof(*new_extents),
  7029. GFP_NOFS);
  7030. nr_extents = 1;
  7031. ret = get_new_locations(reloc_inode,
  7032. extent_key,
  7033. group_start, 1,
  7034. &new_extents,
  7035. &nr_extents);
  7036. if (ret)
  7037. goto out;
  7038. }
  7039. ret = replace_one_extent(trans, found_root,
  7040. path, extent_key,
  7041. &first_key, ref_path,
  7042. new_extents, nr_extents);
  7043. } else {
  7044. ret = relocate_tree_block(trans, found_root, path,
  7045. &first_key, ref_path);
  7046. }
  7047. if (ret < 0)
  7048. goto out;
  7049. }
  7050. ret = 0;
  7051. out:
  7052. btrfs_end_transaction(trans, extent_root);
  7053. kfree(new_extents);
  7054. kfree(ref_path);
  7055. return ret;
  7056. }
  7057. #endif
  7058. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  7059. {
  7060. u64 num_devices;
  7061. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  7062. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  7063. /*
  7064. * we add in the count of missing devices because we want
  7065. * to make sure that any RAID levels on a degraded FS
  7066. * continue to be honored.
  7067. */
  7068. num_devices = root->fs_info->fs_devices->rw_devices +
  7069. root->fs_info->fs_devices->missing_devices;
  7070. if (num_devices == 1) {
  7071. stripped |= BTRFS_BLOCK_GROUP_DUP;
  7072. stripped = flags & ~stripped;
  7073. /* turn raid0 into single device chunks */
  7074. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  7075. return stripped;
  7076. /* turn mirroring into duplication */
  7077. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7078. BTRFS_BLOCK_GROUP_RAID10))
  7079. return stripped | BTRFS_BLOCK_GROUP_DUP;
  7080. return flags;
  7081. } else {
  7082. /* they already had raid on here, just return */
  7083. if (flags & stripped)
  7084. return flags;
  7085. stripped |= BTRFS_BLOCK_GROUP_DUP;
  7086. stripped = flags & ~stripped;
  7087. /* switch duplicated blocks with raid1 */
  7088. if (flags & BTRFS_BLOCK_GROUP_DUP)
  7089. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  7090. /* turn single device chunks into raid0 */
  7091. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  7092. }
  7093. return flags;
  7094. }
  7095. static int set_block_group_ro(struct btrfs_block_group_cache *cache)
  7096. {
  7097. struct btrfs_space_info *sinfo = cache->space_info;
  7098. u64 num_bytes;
  7099. int ret = -ENOSPC;
  7100. if (cache->ro)
  7101. return 0;
  7102. spin_lock(&sinfo->lock);
  7103. spin_lock(&cache->lock);
  7104. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7105. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7106. if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
  7107. sinfo->bytes_may_use + sinfo->bytes_readonly +
  7108. cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
  7109. sinfo->bytes_readonly += num_bytes;
  7110. sinfo->bytes_reserved += cache->reserved_pinned;
  7111. cache->reserved_pinned = 0;
  7112. cache->ro = 1;
  7113. ret = 0;
  7114. }
  7115. spin_unlock(&cache->lock);
  7116. spin_unlock(&sinfo->lock);
  7117. return ret;
  7118. }
  7119. int btrfs_set_block_group_ro(struct btrfs_root *root,
  7120. struct btrfs_block_group_cache *cache)
  7121. {
  7122. struct btrfs_trans_handle *trans;
  7123. u64 alloc_flags;
  7124. int ret;
  7125. BUG_ON(cache->ro);
  7126. trans = btrfs_join_transaction(root, 1);
  7127. BUG_ON(IS_ERR(trans));
  7128. alloc_flags = update_block_group_flags(root, cache->flags);
  7129. if (alloc_flags != cache->flags)
  7130. do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
  7131. ret = set_block_group_ro(cache);
  7132. if (!ret)
  7133. goto out;
  7134. alloc_flags = get_alloc_profile(root, cache->space_info->flags);
  7135. ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
  7136. if (ret < 0)
  7137. goto out;
  7138. ret = set_block_group_ro(cache);
  7139. out:
  7140. btrfs_end_transaction(trans, root);
  7141. return ret;
  7142. }
  7143. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
  7144. struct btrfs_root *root, u64 type)
  7145. {
  7146. u64 alloc_flags = get_alloc_profile(root, type);
  7147. return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
  7148. }
  7149. /*
  7150. * helper to account the unused space of all the readonly block group in the
  7151. * list. takes mirrors into account.
  7152. */
  7153. static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
  7154. {
  7155. struct btrfs_block_group_cache *block_group;
  7156. u64 free_bytes = 0;
  7157. int factor;
  7158. list_for_each_entry(block_group, groups_list, list) {
  7159. spin_lock(&block_group->lock);
  7160. if (!block_group->ro) {
  7161. spin_unlock(&block_group->lock);
  7162. continue;
  7163. }
  7164. if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
  7165. BTRFS_BLOCK_GROUP_RAID10 |
  7166. BTRFS_BLOCK_GROUP_DUP))
  7167. factor = 2;
  7168. else
  7169. factor = 1;
  7170. free_bytes += (block_group->key.offset -
  7171. btrfs_block_group_used(&block_group->item)) *
  7172. factor;
  7173. spin_unlock(&block_group->lock);
  7174. }
  7175. return free_bytes;
  7176. }
  7177. /*
  7178. * helper to account the unused space of all the readonly block group in the
  7179. * space_info. takes mirrors into account.
  7180. */
  7181. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
  7182. {
  7183. int i;
  7184. u64 free_bytes = 0;
  7185. spin_lock(&sinfo->lock);
  7186. for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  7187. if (!list_empty(&sinfo->block_groups[i]))
  7188. free_bytes += __btrfs_get_ro_block_group_free_space(
  7189. &sinfo->block_groups[i]);
  7190. spin_unlock(&sinfo->lock);
  7191. return free_bytes;
  7192. }
  7193. int btrfs_set_block_group_rw(struct btrfs_root *root,
  7194. struct btrfs_block_group_cache *cache)
  7195. {
  7196. struct btrfs_space_info *sinfo = cache->space_info;
  7197. u64 num_bytes;
  7198. BUG_ON(!cache->ro);
  7199. spin_lock(&sinfo->lock);
  7200. spin_lock(&cache->lock);
  7201. num_bytes = cache->key.offset - cache->reserved - cache->pinned -
  7202. cache->bytes_super - btrfs_block_group_used(&cache->item);
  7203. sinfo->bytes_readonly -= num_bytes;
  7204. cache->ro = 0;
  7205. spin_unlock(&cache->lock);
  7206. spin_unlock(&sinfo->lock);
  7207. return 0;
  7208. }
  7209. /*
  7210. * checks to see if its even possible to relocate this block group.
  7211. *
  7212. * @return - -1 if it's not a good idea to relocate this block group, 0 if its
  7213. * ok to go ahead and try.
  7214. */
  7215. int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
  7216. {
  7217. struct btrfs_block_group_cache *block_group;
  7218. struct btrfs_space_info *space_info;
  7219. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  7220. struct btrfs_device *device;
  7221. int full = 0;
  7222. int ret = 0;
  7223. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  7224. /* odd, couldn't find the block group, leave it alone */
  7225. if (!block_group)
  7226. return -1;
  7227. /* no bytes used, we're good */
  7228. if (!btrfs_block_group_used(&block_group->item))
  7229. goto out;
  7230. space_info = block_group->space_info;
  7231. spin_lock(&space_info->lock);
  7232. full = space_info->full;
  7233. /*
  7234. * if this is the last block group we have in this space, we can't
  7235. * relocate it unless we're able to allocate a new chunk below.
  7236. *
  7237. * Otherwise, we need to make sure we have room in the space to handle
  7238. * all of the extents from this block group. If we can, we're good
  7239. */
  7240. if ((space_info->total_bytes != block_group->key.offset) &&
  7241. (space_info->bytes_used + space_info->bytes_reserved +
  7242. space_info->bytes_pinned + space_info->bytes_readonly +
  7243. btrfs_block_group_used(&block_group->item) <
  7244. space_info->total_bytes)) {
  7245. spin_unlock(&space_info->lock);
  7246. goto out;
  7247. }
  7248. spin_unlock(&space_info->lock);
  7249. /*
  7250. * ok we don't have enough space, but maybe we have free space on our
  7251. * devices to allocate new chunks for relocation, so loop through our
  7252. * alloc devices and guess if we have enough space. However, if we
  7253. * were marked as full, then we know there aren't enough chunks, and we
  7254. * can just return.
  7255. */
  7256. ret = -1;
  7257. if (full)
  7258. goto out;
  7259. mutex_lock(&root->fs_info->chunk_mutex);
  7260. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  7261. u64 min_free = btrfs_block_group_used(&block_group->item);
  7262. u64 dev_offset;
  7263. /*
  7264. * check to make sure we can actually find a chunk with enough
  7265. * space to fit our block group in.
  7266. */
  7267. if (device->total_bytes > device->bytes_used + min_free) {
  7268. ret = find_free_dev_extent(NULL, device, min_free,
  7269. &dev_offset, NULL);
  7270. if (!ret)
  7271. break;
  7272. ret = -1;
  7273. }
  7274. }
  7275. mutex_unlock(&root->fs_info->chunk_mutex);
  7276. out:
  7277. btrfs_put_block_group(block_group);
  7278. return ret;
  7279. }
  7280. static int find_first_block_group(struct btrfs_root *root,
  7281. struct btrfs_path *path, struct btrfs_key *key)
  7282. {
  7283. int ret = 0;
  7284. struct btrfs_key found_key;
  7285. struct extent_buffer *leaf;
  7286. int slot;
  7287. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  7288. if (ret < 0)
  7289. goto out;
  7290. while (1) {
  7291. slot = path->slots[0];
  7292. leaf = path->nodes[0];
  7293. if (slot >= btrfs_header_nritems(leaf)) {
  7294. ret = btrfs_next_leaf(root, path);
  7295. if (ret == 0)
  7296. continue;
  7297. if (ret < 0)
  7298. goto out;
  7299. break;
  7300. }
  7301. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  7302. if (found_key.objectid >= key->objectid &&
  7303. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  7304. ret = 0;
  7305. goto out;
  7306. }
  7307. path->slots[0]++;
  7308. }
  7309. out:
  7310. return ret;
  7311. }
  7312. void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
  7313. {
  7314. struct btrfs_block_group_cache *block_group;
  7315. u64 last = 0;
  7316. while (1) {
  7317. struct inode *inode;
  7318. block_group = btrfs_lookup_first_block_group(info, last);
  7319. while (block_group) {
  7320. spin_lock(&block_group->lock);
  7321. if (block_group->iref)
  7322. break;
  7323. spin_unlock(&block_group->lock);
  7324. block_group = next_block_group(info->tree_root,
  7325. block_group);
  7326. }
  7327. if (!block_group) {
  7328. if (last == 0)
  7329. break;
  7330. last = 0;
  7331. continue;
  7332. }
  7333. inode = block_group->inode;
  7334. block_group->iref = 0;
  7335. block_group->inode = NULL;
  7336. spin_unlock(&block_group->lock);
  7337. iput(inode);
  7338. last = block_group->key.objectid + block_group->key.offset;
  7339. btrfs_put_block_group(block_group);
  7340. }
  7341. }
  7342. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  7343. {
  7344. struct btrfs_block_group_cache *block_group;
  7345. struct btrfs_space_info *space_info;
  7346. struct btrfs_caching_control *caching_ctl;
  7347. struct rb_node *n;
  7348. down_write(&info->extent_commit_sem);
  7349. while (!list_empty(&info->caching_block_groups)) {
  7350. caching_ctl = list_entry(info->caching_block_groups.next,
  7351. struct btrfs_caching_control, list);
  7352. list_del(&caching_ctl->list);
  7353. put_caching_control(caching_ctl);
  7354. }
  7355. up_write(&info->extent_commit_sem);
  7356. spin_lock(&info->block_group_cache_lock);
  7357. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  7358. block_group = rb_entry(n, struct btrfs_block_group_cache,
  7359. cache_node);
  7360. rb_erase(&block_group->cache_node,
  7361. &info->block_group_cache_tree);
  7362. spin_unlock(&info->block_group_cache_lock);
  7363. down_write(&block_group->space_info->groups_sem);
  7364. list_del(&block_group->list);
  7365. up_write(&block_group->space_info->groups_sem);
  7366. if (block_group->cached == BTRFS_CACHE_STARTED)
  7367. wait_block_group_cache_done(block_group);
  7368. /*
  7369. * We haven't cached this block group, which means we could
  7370. * possibly have excluded extents on this block group.
  7371. */
  7372. if (block_group->cached == BTRFS_CACHE_NO)
  7373. free_excluded_extents(info->extent_root, block_group);
  7374. btrfs_remove_free_space_cache(block_group);
  7375. btrfs_put_block_group(block_group);
  7376. spin_lock(&info->block_group_cache_lock);
  7377. }
  7378. spin_unlock(&info->block_group_cache_lock);
  7379. /* now that all the block groups are freed, go through and
  7380. * free all the space_info structs. This is only called during
  7381. * the final stages of unmount, and so we know nobody is
  7382. * using them. We call synchronize_rcu() once before we start,
  7383. * just to be on the safe side.
  7384. */
  7385. synchronize_rcu();
  7386. release_global_block_rsv(info);
  7387. while(!list_empty(&info->space_info)) {
  7388. space_info = list_entry(info->space_info.next,
  7389. struct btrfs_space_info,
  7390. list);
  7391. if (space_info->bytes_pinned > 0 ||
  7392. space_info->bytes_reserved > 0) {
  7393. WARN_ON(1);
  7394. dump_space_info(space_info, 0, 0);
  7395. }
  7396. list_del(&space_info->list);
  7397. kfree(space_info);
  7398. }
  7399. return 0;
  7400. }
  7401. static void __link_block_group(struct btrfs_space_info *space_info,
  7402. struct btrfs_block_group_cache *cache)
  7403. {
  7404. int index = get_block_group_index(cache);
  7405. down_write(&space_info->groups_sem);
  7406. list_add_tail(&cache->list, &space_info->block_groups[index]);
  7407. up_write(&space_info->groups_sem);
  7408. }
  7409. int btrfs_read_block_groups(struct btrfs_root *root)
  7410. {
  7411. struct btrfs_path *path;
  7412. int ret;
  7413. struct btrfs_block_group_cache *cache;
  7414. struct btrfs_fs_info *info = root->fs_info;
  7415. struct btrfs_space_info *space_info;
  7416. struct btrfs_key key;
  7417. struct btrfs_key found_key;
  7418. struct extent_buffer *leaf;
  7419. int need_clear = 0;
  7420. u64 cache_gen;
  7421. root = info->extent_root;
  7422. key.objectid = 0;
  7423. key.offset = 0;
  7424. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  7425. path = btrfs_alloc_path();
  7426. if (!path)
  7427. return -ENOMEM;
  7428. cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
  7429. if (cache_gen != 0 &&
  7430. btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
  7431. need_clear = 1;
  7432. if (btrfs_test_opt(root, CLEAR_CACHE))
  7433. need_clear = 1;
  7434. if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
  7435. printk(KERN_INFO "btrfs: disk space caching is enabled\n");
  7436. while (1) {
  7437. ret = find_first_block_group(root, path, &key);
  7438. if (ret > 0)
  7439. break;
  7440. if (ret != 0)
  7441. goto error;
  7442. leaf = path->nodes[0];
  7443. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  7444. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7445. if (!cache) {
  7446. ret = -ENOMEM;
  7447. goto error;
  7448. }
  7449. atomic_set(&cache->count, 1);
  7450. spin_lock_init(&cache->lock);
  7451. spin_lock_init(&cache->tree_lock);
  7452. cache->fs_info = info;
  7453. INIT_LIST_HEAD(&cache->list);
  7454. INIT_LIST_HEAD(&cache->cluster_list);
  7455. if (need_clear)
  7456. cache->disk_cache_state = BTRFS_DC_CLEAR;
  7457. /*
  7458. * we only want to have 32k of ram per block group for keeping
  7459. * track of free space, and if we pass 1/2 of that we want to
  7460. * start converting things over to using bitmaps
  7461. */
  7462. cache->extents_thresh = ((1024 * 32) / 2) /
  7463. sizeof(struct btrfs_free_space);
  7464. read_extent_buffer(leaf, &cache->item,
  7465. btrfs_item_ptr_offset(leaf, path->slots[0]),
  7466. sizeof(cache->item));
  7467. memcpy(&cache->key, &found_key, sizeof(found_key));
  7468. key.objectid = found_key.objectid + found_key.offset;
  7469. btrfs_release_path(root, path);
  7470. cache->flags = btrfs_block_group_flags(&cache->item);
  7471. cache->sectorsize = root->sectorsize;
  7472. /*
  7473. * We need to exclude the super stripes now so that the space
  7474. * info has super bytes accounted for, otherwise we'll think
  7475. * we have more space than we actually do.
  7476. */
  7477. exclude_super_stripes(root, cache);
  7478. /*
  7479. * check for two cases, either we are full, and therefore
  7480. * don't need to bother with the caching work since we won't
  7481. * find any space, or we are empty, and we can just add all
  7482. * the space in and be done with it. This saves us _alot_ of
  7483. * time, particularly in the full case.
  7484. */
  7485. if (found_key.offset == btrfs_block_group_used(&cache->item)) {
  7486. cache->last_byte_to_unpin = (u64)-1;
  7487. cache->cached = BTRFS_CACHE_FINISHED;
  7488. free_excluded_extents(root, cache);
  7489. } else if (btrfs_block_group_used(&cache->item) == 0) {
  7490. cache->last_byte_to_unpin = (u64)-1;
  7491. cache->cached = BTRFS_CACHE_FINISHED;
  7492. add_new_free_space(cache, root->fs_info,
  7493. found_key.objectid,
  7494. found_key.objectid +
  7495. found_key.offset);
  7496. free_excluded_extents(root, cache);
  7497. }
  7498. ret = update_space_info(info, cache->flags, found_key.offset,
  7499. btrfs_block_group_used(&cache->item),
  7500. &space_info);
  7501. BUG_ON(ret);
  7502. cache->space_info = space_info;
  7503. spin_lock(&cache->space_info->lock);
  7504. cache->space_info->bytes_readonly += cache->bytes_super;
  7505. spin_unlock(&cache->space_info->lock);
  7506. __link_block_group(space_info, cache);
  7507. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7508. BUG_ON(ret);
  7509. set_avail_alloc_bits(root->fs_info, cache->flags);
  7510. if (btrfs_chunk_readonly(root, cache->key.objectid))
  7511. set_block_group_ro(cache);
  7512. }
  7513. list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
  7514. if (!(get_alloc_profile(root, space_info->flags) &
  7515. (BTRFS_BLOCK_GROUP_RAID10 |
  7516. BTRFS_BLOCK_GROUP_RAID1 |
  7517. BTRFS_BLOCK_GROUP_DUP)))
  7518. continue;
  7519. /*
  7520. * avoid allocating from un-mirrored block group if there are
  7521. * mirrored block groups.
  7522. */
  7523. list_for_each_entry(cache, &space_info->block_groups[3], list)
  7524. set_block_group_ro(cache);
  7525. list_for_each_entry(cache, &space_info->block_groups[4], list)
  7526. set_block_group_ro(cache);
  7527. }
  7528. init_global_block_rsv(info);
  7529. ret = 0;
  7530. error:
  7531. btrfs_free_path(path);
  7532. return ret;
  7533. }
  7534. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  7535. struct btrfs_root *root, u64 bytes_used,
  7536. u64 type, u64 chunk_objectid, u64 chunk_offset,
  7537. u64 size)
  7538. {
  7539. int ret;
  7540. struct btrfs_root *extent_root;
  7541. struct btrfs_block_group_cache *cache;
  7542. extent_root = root->fs_info->extent_root;
  7543. root->fs_info->last_trans_log_full_commit = trans->transid;
  7544. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  7545. if (!cache)
  7546. return -ENOMEM;
  7547. cache->key.objectid = chunk_offset;
  7548. cache->key.offset = size;
  7549. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  7550. cache->sectorsize = root->sectorsize;
  7551. cache->fs_info = root->fs_info;
  7552. /*
  7553. * we only want to have 32k of ram per block group for keeping track
  7554. * of free space, and if we pass 1/2 of that we want to start
  7555. * converting things over to using bitmaps
  7556. */
  7557. cache->extents_thresh = ((1024 * 32) / 2) /
  7558. sizeof(struct btrfs_free_space);
  7559. atomic_set(&cache->count, 1);
  7560. spin_lock_init(&cache->lock);
  7561. spin_lock_init(&cache->tree_lock);
  7562. INIT_LIST_HEAD(&cache->list);
  7563. INIT_LIST_HEAD(&cache->cluster_list);
  7564. btrfs_set_block_group_used(&cache->item, bytes_used);
  7565. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  7566. cache->flags = type;
  7567. btrfs_set_block_group_flags(&cache->item, type);
  7568. cache->last_byte_to_unpin = (u64)-1;
  7569. cache->cached = BTRFS_CACHE_FINISHED;
  7570. exclude_super_stripes(root, cache);
  7571. add_new_free_space(cache, root->fs_info, chunk_offset,
  7572. chunk_offset + size);
  7573. free_excluded_extents(root, cache);
  7574. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  7575. &cache->space_info);
  7576. BUG_ON(ret);
  7577. spin_lock(&cache->space_info->lock);
  7578. cache->space_info->bytes_readonly += cache->bytes_super;
  7579. spin_unlock(&cache->space_info->lock);
  7580. __link_block_group(cache->space_info, cache);
  7581. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  7582. BUG_ON(ret);
  7583. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  7584. sizeof(cache->item));
  7585. BUG_ON(ret);
  7586. set_avail_alloc_bits(extent_root->fs_info, type);
  7587. return 0;
  7588. }
  7589. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  7590. struct btrfs_root *root, u64 group_start)
  7591. {
  7592. struct btrfs_path *path;
  7593. struct btrfs_block_group_cache *block_group;
  7594. struct btrfs_free_cluster *cluster;
  7595. struct btrfs_root *tree_root = root->fs_info->tree_root;
  7596. struct btrfs_key key;
  7597. struct inode *inode;
  7598. int ret;
  7599. int factor;
  7600. root = root->fs_info->extent_root;
  7601. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  7602. BUG_ON(!block_group);
  7603. BUG_ON(!block_group->ro);
  7604. memcpy(&key, &block_group->key, sizeof(key));
  7605. if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
  7606. BTRFS_BLOCK_GROUP_RAID1 |
  7607. BTRFS_BLOCK_GROUP_RAID10))
  7608. factor = 2;
  7609. else
  7610. factor = 1;
  7611. /* make sure this block group isn't part of an allocation cluster */
  7612. cluster = &root->fs_info->data_alloc_cluster;
  7613. spin_lock(&cluster->refill_lock);
  7614. btrfs_return_cluster_to_free_space(block_group, cluster);
  7615. spin_unlock(&cluster->refill_lock);
  7616. /*
  7617. * make sure this block group isn't part of a metadata
  7618. * allocation cluster
  7619. */
  7620. cluster = &root->fs_info->meta_alloc_cluster;
  7621. spin_lock(&cluster->refill_lock);
  7622. btrfs_return_cluster_to_free_space(block_group, cluster);
  7623. spin_unlock(&cluster->refill_lock);
  7624. path = btrfs_alloc_path();
  7625. BUG_ON(!path);
  7626. inode = lookup_free_space_inode(root, block_group, path);
  7627. if (!IS_ERR(inode)) {
  7628. btrfs_orphan_add(trans, inode);
  7629. clear_nlink(inode);
  7630. /* One for the block groups ref */
  7631. spin_lock(&block_group->lock);
  7632. if (block_group->iref) {
  7633. block_group->iref = 0;
  7634. block_group->inode = NULL;
  7635. spin_unlock(&block_group->lock);
  7636. iput(inode);
  7637. } else {
  7638. spin_unlock(&block_group->lock);
  7639. }
  7640. /* One for our lookup ref */
  7641. iput(inode);
  7642. }
  7643. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  7644. key.offset = block_group->key.objectid;
  7645. key.type = 0;
  7646. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  7647. if (ret < 0)
  7648. goto out;
  7649. if (ret > 0)
  7650. btrfs_release_path(tree_root, path);
  7651. if (ret == 0) {
  7652. ret = btrfs_del_item(trans, tree_root, path);
  7653. if (ret)
  7654. goto out;
  7655. btrfs_release_path(tree_root, path);
  7656. }
  7657. spin_lock(&root->fs_info->block_group_cache_lock);
  7658. rb_erase(&block_group->cache_node,
  7659. &root->fs_info->block_group_cache_tree);
  7660. spin_unlock(&root->fs_info->block_group_cache_lock);
  7661. down_write(&block_group->space_info->groups_sem);
  7662. /*
  7663. * we must use list_del_init so people can check to see if they
  7664. * are still on the list after taking the semaphore
  7665. */
  7666. list_del_init(&block_group->list);
  7667. up_write(&block_group->space_info->groups_sem);
  7668. if (block_group->cached == BTRFS_CACHE_STARTED)
  7669. wait_block_group_cache_done(block_group);
  7670. btrfs_remove_free_space_cache(block_group);
  7671. spin_lock(&block_group->space_info->lock);
  7672. block_group->space_info->total_bytes -= block_group->key.offset;
  7673. block_group->space_info->bytes_readonly -= block_group->key.offset;
  7674. block_group->space_info->disk_total -= block_group->key.offset * factor;
  7675. spin_unlock(&block_group->space_info->lock);
  7676. memcpy(&key, &block_group->key, sizeof(key));
  7677. btrfs_clear_space_info_full(root->fs_info);
  7678. btrfs_put_block_group(block_group);
  7679. btrfs_put_block_group(block_group);
  7680. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  7681. if (ret > 0)
  7682. ret = -EIO;
  7683. if (ret < 0)
  7684. goto out;
  7685. ret = btrfs_del_item(trans, root, path);
  7686. out:
  7687. btrfs_free_path(path);
  7688. return ret;
  7689. }
  7690. int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  7691. {
  7692. return unpin_extent_range(root, start, end);
  7693. }
  7694. int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
  7695. u64 num_bytes)
  7696. {
  7697. return btrfs_discard_extent(root, bytenr, num_bytes);
  7698. }