extent-tree.c 163 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292
  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 "compat.h"
  24. #include "hash.h"
  25. #include "crc32c.h"
  26. #include "ctree.h"
  27. #include "disk-io.h"
  28. #include "print-tree.h"
  29. #include "transaction.h"
  30. #include "volumes.h"
  31. #include "locking.h"
  32. #include "ref-cache.h"
  33. #define PENDING_EXTENT_INSERT 0
  34. #define PENDING_EXTENT_DELETE 1
  35. #define PENDING_BACKREF_UPDATE 2
  36. struct pending_extent_op {
  37. int type;
  38. u64 bytenr;
  39. u64 num_bytes;
  40. u64 parent;
  41. u64 orig_parent;
  42. u64 generation;
  43. u64 orig_generation;
  44. int level;
  45. struct list_head list;
  46. int del;
  47. };
  48. static int finish_current_insert(struct btrfs_trans_handle *trans,
  49. struct btrfs_root *extent_root, int all);
  50. static int del_pending_extents(struct btrfs_trans_handle *trans,
  51. struct btrfs_root *extent_root, int all);
  52. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  53. struct btrfs_root *root,
  54. u64 bytenr, u64 num_bytes, int is_data);
  55. static int update_block_group(struct btrfs_trans_handle *trans,
  56. struct btrfs_root *root,
  57. u64 bytenr, u64 num_bytes, int alloc,
  58. int mark_free);
  59. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  60. {
  61. return (cache->flags & bits) == bits;
  62. }
  63. /*
  64. * this adds the block group to the fs_info rb tree for the block group
  65. * cache
  66. */
  67. static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
  68. struct btrfs_block_group_cache *block_group)
  69. {
  70. struct rb_node **p;
  71. struct rb_node *parent = NULL;
  72. struct btrfs_block_group_cache *cache;
  73. spin_lock(&info->block_group_cache_lock);
  74. p = &info->block_group_cache_tree.rb_node;
  75. while (*p) {
  76. parent = *p;
  77. cache = rb_entry(parent, struct btrfs_block_group_cache,
  78. cache_node);
  79. if (block_group->key.objectid < cache->key.objectid) {
  80. p = &(*p)->rb_left;
  81. } else if (block_group->key.objectid > cache->key.objectid) {
  82. p = &(*p)->rb_right;
  83. } else {
  84. spin_unlock(&info->block_group_cache_lock);
  85. return -EEXIST;
  86. }
  87. }
  88. rb_link_node(&block_group->cache_node, parent, p);
  89. rb_insert_color(&block_group->cache_node,
  90. &info->block_group_cache_tree);
  91. spin_unlock(&info->block_group_cache_lock);
  92. return 0;
  93. }
  94. /*
  95. * This will return the block group at or after bytenr if contains is 0, else
  96. * it will return the block group that contains the bytenr
  97. */
  98. static struct btrfs_block_group_cache *
  99. block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
  100. int contains)
  101. {
  102. struct btrfs_block_group_cache *cache, *ret = NULL;
  103. struct rb_node *n;
  104. u64 end, start;
  105. spin_lock(&info->block_group_cache_lock);
  106. n = info->block_group_cache_tree.rb_node;
  107. while (n) {
  108. cache = rb_entry(n, struct btrfs_block_group_cache,
  109. cache_node);
  110. end = cache->key.objectid + cache->key.offset - 1;
  111. start = cache->key.objectid;
  112. if (bytenr < start) {
  113. if (!contains && (!ret || start < ret->key.objectid))
  114. ret = cache;
  115. n = n->rb_left;
  116. } else if (bytenr > start) {
  117. if (contains && bytenr <= end) {
  118. ret = cache;
  119. break;
  120. }
  121. n = n->rb_right;
  122. } else {
  123. ret = cache;
  124. break;
  125. }
  126. }
  127. if (ret)
  128. atomic_inc(&ret->count);
  129. spin_unlock(&info->block_group_cache_lock);
  130. return ret;
  131. }
  132. /*
  133. * this is only called by cache_block_group, since we could have freed extents
  134. * we need to check the pinned_extents for any extents that can't be used yet
  135. * since their free space will be released as soon as the transaction commits.
  136. */
  137. static int add_new_free_space(struct btrfs_block_group_cache *block_group,
  138. struct btrfs_fs_info *info, u64 start, u64 end)
  139. {
  140. u64 extent_start, extent_end, size;
  141. int ret;
  142. mutex_lock(&info->pinned_mutex);
  143. while (start < end) {
  144. ret = find_first_extent_bit(&info->pinned_extents, start,
  145. &extent_start, &extent_end,
  146. EXTENT_DIRTY);
  147. if (ret)
  148. break;
  149. if (extent_start == start) {
  150. start = extent_end + 1;
  151. } else if (extent_start > start && extent_start < end) {
  152. size = extent_start - start;
  153. ret = btrfs_add_free_space(block_group, start,
  154. size);
  155. BUG_ON(ret);
  156. start = extent_end + 1;
  157. } else {
  158. break;
  159. }
  160. }
  161. if (start < end) {
  162. size = end - start;
  163. ret = btrfs_add_free_space(block_group, start, size);
  164. BUG_ON(ret);
  165. }
  166. mutex_unlock(&info->pinned_mutex);
  167. return 0;
  168. }
  169. static int remove_sb_from_cache(struct btrfs_root *root,
  170. struct btrfs_block_group_cache *cache)
  171. {
  172. u64 bytenr;
  173. u64 *logical;
  174. int stripe_len;
  175. int i, nr, ret;
  176. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  177. bytenr = btrfs_sb_offset(i);
  178. ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
  179. cache->key.objectid, bytenr, 0,
  180. &logical, &nr, &stripe_len);
  181. BUG_ON(ret);
  182. while (nr--) {
  183. btrfs_remove_free_space(cache, logical[nr],
  184. stripe_len);
  185. }
  186. kfree(logical);
  187. }
  188. return 0;
  189. }
  190. static int cache_block_group(struct btrfs_root *root,
  191. struct btrfs_block_group_cache *block_group)
  192. {
  193. struct btrfs_path *path;
  194. int ret = 0;
  195. struct btrfs_key key;
  196. struct extent_buffer *leaf;
  197. int slot;
  198. u64 last;
  199. if (!block_group)
  200. return 0;
  201. root = root->fs_info->extent_root;
  202. if (block_group->cached)
  203. return 0;
  204. path = btrfs_alloc_path();
  205. if (!path)
  206. return -ENOMEM;
  207. path->reada = 2;
  208. /*
  209. * we get into deadlocks with paths held by callers of this function.
  210. * since the alloc_mutex is protecting things right now, just
  211. * skip the locking here
  212. */
  213. path->skip_locking = 1;
  214. last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
  215. key.objectid = last;
  216. key.offset = 0;
  217. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  218. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  219. if (ret < 0)
  220. goto err;
  221. while (1) {
  222. leaf = path->nodes[0];
  223. slot = path->slots[0];
  224. if (slot >= btrfs_header_nritems(leaf)) {
  225. ret = btrfs_next_leaf(root, path);
  226. if (ret < 0)
  227. goto err;
  228. if (ret == 0)
  229. continue;
  230. else
  231. break;
  232. }
  233. btrfs_item_key_to_cpu(leaf, &key, slot);
  234. if (key.objectid < block_group->key.objectid)
  235. goto next;
  236. if (key.objectid >= block_group->key.objectid +
  237. block_group->key.offset)
  238. break;
  239. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  240. add_new_free_space(block_group, root->fs_info, last,
  241. key.objectid);
  242. last = key.objectid + key.offset;
  243. }
  244. next:
  245. path->slots[0]++;
  246. }
  247. add_new_free_space(block_group, root->fs_info, last,
  248. block_group->key.objectid +
  249. block_group->key.offset);
  250. remove_sb_from_cache(root, block_group);
  251. block_group->cached = 1;
  252. ret = 0;
  253. err:
  254. btrfs_free_path(path);
  255. return ret;
  256. }
  257. /*
  258. * return the block group that starts at or after bytenr
  259. */
  260. static struct btrfs_block_group_cache *
  261. btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
  262. {
  263. struct btrfs_block_group_cache *cache;
  264. cache = block_group_cache_tree_search(info, bytenr, 0);
  265. return cache;
  266. }
  267. /*
  268. * return the block group that contains teh given bytenr
  269. */
  270. struct btrfs_block_group_cache *btrfs_lookup_block_group(
  271. struct btrfs_fs_info *info,
  272. u64 bytenr)
  273. {
  274. struct btrfs_block_group_cache *cache;
  275. cache = block_group_cache_tree_search(info, bytenr, 1);
  276. return cache;
  277. }
  278. static inline void put_block_group(struct btrfs_block_group_cache *cache)
  279. {
  280. if (atomic_dec_and_test(&cache->count))
  281. kfree(cache);
  282. }
  283. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  284. u64 flags)
  285. {
  286. struct list_head *head = &info->space_info;
  287. struct btrfs_space_info *found;
  288. list_for_each_entry(found, head, list) {
  289. if (found->flags == flags)
  290. return found;
  291. }
  292. return NULL;
  293. }
  294. static u64 div_factor(u64 num, int factor)
  295. {
  296. if (factor == 10)
  297. return num;
  298. num *= factor;
  299. do_div(num, 10);
  300. return num;
  301. }
  302. u64 btrfs_find_block_group(struct btrfs_root *root,
  303. u64 search_start, u64 search_hint, int owner)
  304. {
  305. struct btrfs_block_group_cache *cache;
  306. u64 used;
  307. u64 last = max(search_hint, search_start);
  308. u64 group_start = 0;
  309. int full_search = 0;
  310. int factor = 9;
  311. int wrapped = 0;
  312. again:
  313. while (1) {
  314. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  315. if (!cache)
  316. break;
  317. spin_lock(&cache->lock);
  318. last = cache->key.objectid + cache->key.offset;
  319. used = btrfs_block_group_used(&cache->item);
  320. if ((full_search || !cache->ro) &&
  321. block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
  322. if (used + cache->pinned + cache->reserved <
  323. div_factor(cache->key.offset, factor)) {
  324. group_start = cache->key.objectid;
  325. spin_unlock(&cache->lock);
  326. put_block_group(cache);
  327. goto found;
  328. }
  329. }
  330. spin_unlock(&cache->lock);
  331. put_block_group(cache);
  332. cond_resched();
  333. }
  334. if (!wrapped) {
  335. last = search_start;
  336. wrapped = 1;
  337. goto again;
  338. }
  339. if (!full_search && factor < 10) {
  340. last = search_start;
  341. full_search = 1;
  342. factor = 10;
  343. goto again;
  344. }
  345. found:
  346. return group_start;
  347. }
  348. /* simple helper to search for an existing extent at a given offset */
  349. int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
  350. {
  351. int ret;
  352. struct btrfs_key key;
  353. struct btrfs_path *path;
  354. path = btrfs_alloc_path();
  355. BUG_ON(!path);
  356. key.objectid = start;
  357. key.offset = len;
  358. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  359. ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
  360. 0, 0);
  361. btrfs_free_path(path);
  362. return ret;
  363. }
  364. /*
  365. * Back reference rules. Back refs have three main goals:
  366. *
  367. * 1) differentiate between all holders of references to an extent so that
  368. * when a reference is dropped we can make sure it was a valid reference
  369. * before freeing the extent.
  370. *
  371. * 2) Provide enough information to quickly find the holders of an extent
  372. * if we notice a given block is corrupted or bad.
  373. *
  374. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  375. * maintenance. This is actually the same as #2, but with a slightly
  376. * different use case.
  377. *
  378. * File extents can be referenced by:
  379. *
  380. * - multiple snapshots, subvolumes, or different generations in one subvol
  381. * - different files inside a single subvolume
  382. * - different offsets inside a file (bookend extents in file.c)
  383. *
  384. * The extent ref structure has fields for:
  385. *
  386. * - Objectid of the subvolume root
  387. * - Generation number of the tree holding the reference
  388. * - objectid of the file holding the reference
  389. * - number of references holding by parent node (alway 1 for tree blocks)
  390. *
  391. * Btree leaf may hold multiple references to a file extent. In most cases,
  392. * these references are from same file and the corresponding offsets inside
  393. * the file are close together.
  394. *
  395. * When a file extent is allocated the fields are filled in:
  396. * (root_key.objectid, trans->transid, inode objectid, 1)
  397. *
  398. * When a leaf is cow'd new references are added for every file extent found
  399. * in the leaf. It looks similar to the create case, but trans->transid will
  400. * be different when the block is cow'd.
  401. *
  402. * (root_key.objectid, trans->transid, inode objectid,
  403. * number of references in the leaf)
  404. *
  405. * When a file extent is removed either during snapshot deletion or
  406. * file truncation, we find the corresponding back reference and check
  407. * the following fields:
  408. *
  409. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  410. * inode objectid)
  411. *
  412. * Btree extents can be referenced by:
  413. *
  414. * - Different subvolumes
  415. * - Different generations of the same subvolume
  416. *
  417. * When a tree block is created, back references are inserted:
  418. *
  419. * (root->root_key.objectid, trans->transid, level, 1)
  420. *
  421. * When a tree block is cow'd, new back references are added for all the
  422. * blocks it points to. If the tree block isn't in reference counted root,
  423. * the old back references are removed. These new back references are of
  424. * the form (trans->transid will have increased since creation):
  425. *
  426. * (root->root_key.objectid, trans->transid, level, 1)
  427. *
  428. * When a backref is in deleting, the following fields are checked:
  429. *
  430. * if backref was for a tree root:
  431. * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
  432. * else
  433. * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
  434. *
  435. * Back Reference Key composing:
  436. *
  437. * The key objectid corresponds to the first byte in the extent, the key
  438. * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
  439. * byte of parent extent. If a extent is tree root, the key offset is set
  440. * to the key objectid.
  441. */
  442. static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
  443. struct btrfs_root *root,
  444. struct btrfs_path *path,
  445. u64 bytenr, u64 parent,
  446. u64 ref_root, u64 ref_generation,
  447. u64 owner_objectid, int del)
  448. {
  449. struct btrfs_key key;
  450. struct btrfs_extent_ref *ref;
  451. struct extent_buffer *leaf;
  452. u64 ref_objectid;
  453. int ret;
  454. key.objectid = bytenr;
  455. key.type = BTRFS_EXTENT_REF_KEY;
  456. key.offset = parent;
  457. ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
  458. if (ret < 0)
  459. goto out;
  460. if (ret > 0) {
  461. ret = -ENOENT;
  462. goto out;
  463. }
  464. leaf = path->nodes[0];
  465. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  466. ref_objectid = btrfs_ref_objectid(leaf, ref);
  467. if (btrfs_ref_root(leaf, ref) != ref_root ||
  468. btrfs_ref_generation(leaf, ref) != ref_generation ||
  469. (ref_objectid != owner_objectid &&
  470. ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
  471. ret = -EIO;
  472. WARN_ON(1);
  473. goto out;
  474. }
  475. ret = 0;
  476. out:
  477. return ret;
  478. }
  479. /*
  480. * updates all the backrefs that are pending on update_list for the
  481. * extent_root
  482. */
  483. static noinline int update_backrefs(struct btrfs_trans_handle *trans,
  484. struct btrfs_root *extent_root,
  485. struct btrfs_path *path,
  486. struct list_head *update_list)
  487. {
  488. struct btrfs_key key;
  489. struct btrfs_extent_ref *ref;
  490. struct btrfs_fs_info *info = extent_root->fs_info;
  491. struct pending_extent_op *op;
  492. struct extent_buffer *leaf;
  493. int ret = 0;
  494. struct list_head *cur = update_list->next;
  495. u64 ref_objectid;
  496. u64 ref_root = extent_root->root_key.objectid;
  497. op = list_entry(cur, struct pending_extent_op, list);
  498. search:
  499. key.objectid = op->bytenr;
  500. key.type = BTRFS_EXTENT_REF_KEY;
  501. key.offset = op->orig_parent;
  502. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
  503. BUG_ON(ret);
  504. leaf = path->nodes[0];
  505. loop:
  506. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  507. ref_objectid = btrfs_ref_objectid(leaf, ref);
  508. if (btrfs_ref_root(leaf, ref) != ref_root ||
  509. btrfs_ref_generation(leaf, ref) != op->orig_generation ||
  510. (ref_objectid != op->level &&
  511. ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
  512. printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, "
  513. "root %llu, owner %u\n",
  514. (unsigned long long)op->bytenr,
  515. (unsigned long long)op->orig_parent,
  516. (unsigned long long)ref_root, op->level);
  517. btrfs_print_leaf(extent_root, leaf);
  518. BUG();
  519. }
  520. key.objectid = op->bytenr;
  521. key.offset = op->parent;
  522. key.type = BTRFS_EXTENT_REF_KEY;
  523. ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
  524. BUG_ON(ret);
  525. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  526. btrfs_set_ref_generation(leaf, ref, op->generation);
  527. cur = cur->next;
  528. list_del_init(&op->list);
  529. unlock_extent(&info->extent_ins, op->bytenr,
  530. op->bytenr + op->num_bytes - 1, GFP_NOFS);
  531. kfree(op);
  532. if (cur == update_list) {
  533. btrfs_mark_buffer_dirty(path->nodes[0]);
  534. btrfs_release_path(extent_root, path);
  535. goto out;
  536. }
  537. op = list_entry(cur, struct pending_extent_op, list);
  538. path->slots[0]++;
  539. while (path->slots[0] < btrfs_header_nritems(leaf)) {
  540. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  541. if (key.objectid == op->bytenr &&
  542. key.type == BTRFS_EXTENT_REF_KEY)
  543. goto loop;
  544. path->slots[0]++;
  545. }
  546. btrfs_mark_buffer_dirty(path->nodes[0]);
  547. btrfs_release_path(extent_root, path);
  548. goto search;
  549. out:
  550. return 0;
  551. }
  552. static noinline int insert_extents(struct btrfs_trans_handle *trans,
  553. struct btrfs_root *extent_root,
  554. struct btrfs_path *path,
  555. struct list_head *insert_list, int nr)
  556. {
  557. struct btrfs_key *keys;
  558. u32 *data_size;
  559. struct pending_extent_op *op;
  560. struct extent_buffer *leaf;
  561. struct list_head *cur = insert_list->next;
  562. struct btrfs_fs_info *info = extent_root->fs_info;
  563. u64 ref_root = extent_root->root_key.objectid;
  564. int i = 0, last = 0, ret;
  565. int total = nr * 2;
  566. if (!nr)
  567. return 0;
  568. keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
  569. if (!keys)
  570. return -ENOMEM;
  571. data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
  572. if (!data_size) {
  573. kfree(keys);
  574. return -ENOMEM;
  575. }
  576. list_for_each_entry(op, insert_list, list) {
  577. keys[i].objectid = op->bytenr;
  578. keys[i].offset = op->num_bytes;
  579. keys[i].type = BTRFS_EXTENT_ITEM_KEY;
  580. data_size[i] = sizeof(struct btrfs_extent_item);
  581. i++;
  582. keys[i].objectid = op->bytenr;
  583. keys[i].offset = op->parent;
  584. keys[i].type = BTRFS_EXTENT_REF_KEY;
  585. data_size[i] = sizeof(struct btrfs_extent_ref);
  586. i++;
  587. }
  588. op = list_entry(cur, struct pending_extent_op, list);
  589. i = 0;
  590. while (i < total) {
  591. int c;
  592. ret = btrfs_insert_some_items(trans, extent_root, path,
  593. keys+i, data_size+i, total-i);
  594. BUG_ON(ret < 0);
  595. if (last && ret > 1)
  596. BUG();
  597. leaf = path->nodes[0];
  598. for (c = 0; c < ret; c++) {
  599. int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
  600. /*
  601. * if the first item we inserted was a backref, then
  602. * the EXTENT_ITEM will be the odd c's, else it will
  603. * be the even c's
  604. */
  605. if ((ref_first && (c % 2)) ||
  606. (!ref_first && !(c % 2))) {
  607. struct btrfs_extent_item *itm;
  608. itm = btrfs_item_ptr(leaf, path->slots[0] + c,
  609. struct btrfs_extent_item);
  610. btrfs_set_extent_refs(path->nodes[0], itm, 1);
  611. op->del++;
  612. } else {
  613. struct btrfs_extent_ref *ref;
  614. ref = btrfs_item_ptr(leaf, path->slots[0] + c,
  615. struct btrfs_extent_ref);
  616. btrfs_set_ref_root(leaf, ref, ref_root);
  617. btrfs_set_ref_generation(leaf, ref,
  618. op->generation);
  619. btrfs_set_ref_objectid(leaf, ref, op->level);
  620. btrfs_set_ref_num_refs(leaf, ref, 1);
  621. op->del++;
  622. }
  623. /*
  624. * using del to see when its ok to free up the
  625. * pending_extent_op. In the case where we insert the
  626. * last item on the list in order to help do batching
  627. * we need to not free the extent op until we actually
  628. * insert the extent_item
  629. */
  630. if (op->del == 2) {
  631. unlock_extent(&info->extent_ins, op->bytenr,
  632. op->bytenr + op->num_bytes - 1,
  633. GFP_NOFS);
  634. cur = cur->next;
  635. list_del_init(&op->list);
  636. kfree(op);
  637. if (cur != insert_list)
  638. op = list_entry(cur,
  639. struct pending_extent_op,
  640. list);
  641. }
  642. }
  643. btrfs_mark_buffer_dirty(leaf);
  644. btrfs_release_path(extent_root, path);
  645. /*
  646. * Ok backref's and items usually go right next to eachother,
  647. * but if we could only insert 1 item that means that we
  648. * inserted on the end of a leaf, and we have no idea what may
  649. * be on the next leaf so we just play it safe. In order to
  650. * try and help this case we insert the last thing on our
  651. * insert list so hopefully it will end up being the last
  652. * thing on the leaf and everything else will be before it,
  653. * which will let us insert a whole bunch of items at the same
  654. * time.
  655. */
  656. if (ret == 1 && !last && (i + ret < total)) {
  657. /*
  658. * last: where we will pick up the next time around
  659. * i: our current key to insert, will be total - 1
  660. * cur: the current op we are screwing with
  661. * op: duh
  662. */
  663. last = i + ret;
  664. i = total - 1;
  665. cur = insert_list->prev;
  666. op = list_entry(cur, struct pending_extent_op, list);
  667. } else if (last) {
  668. /*
  669. * ok we successfully inserted the last item on the
  670. * list, lets reset everything
  671. *
  672. * i: our current key to insert, so where we left off
  673. * last time
  674. * last: done with this
  675. * cur: the op we are messing with
  676. * op: duh
  677. * total: since we inserted the last key, we need to
  678. * decrement total so we dont overflow
  679. */
  680. i = last;
  681. last = 0;
  682. total--;
  683. if (i < total) {
  684. cur = insert_list->next;
  685. op = list_entry(cur, struct pending_extent_op,
  686. list);
  687. }
  688. } else {
  689. i += ret;
  690. }
  691. cond_resched();
  692. }
  693. ret = 0;
  694. kfree(keys);
  695. kfree(data_size);
  696. return ret;
  697. }
  698. static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
  699. struct btrfs_root *root,
  700. struct btrfs_path *path,
  701. u64 bytenr, u64 parent,
  702. u64 ref_root, u64 ref_generation,
  703. u64 owner_objectid)
  704. {
  705. struct btrfs_key key;
  706. struct extent_buffer *leaf;
  707. struct btrfs_extent_ref *ref;
  708. u32 num_refs;
  709. int ret;
  710. key.objectid = bytenr;
  711. key.type = BTRFS_EXTENT_REF_KEY;
  712. key.offset = parent;
  713. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
  714. if (ret == 0) {
  715. leaf = path->nodes[0];
  716. ref = btrfs_item_ptr(leaf, path->slots[0],
  717. struct btrfs_extent_ref);
  718. btrfs_set_ref_root(leaf, ref, ref_root);
  719. btrfs_set_ref_generation(leaf, ref, ref_generation);
  720. btrfs_set_ref_objectid(leaf, ref, owner_objectid);
  721. btrfs_set_ref_num_refs(leaf, ref, 1);
  722. } else if (ret == -EEXIST) {
  723. u64 existing_owner;
  724. BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
  725. leaf = path->nodes[0];
  726. ref = btrfs_item_ptr(leaf, path->slots[0],
  727. struct btrfs_extent_ref);
  728. if (btrfs_ref_root(leaf, ref) != ref_root ||
  729. btrfs_ref_generation(leaf, ref) != ref_generation) {
  730. ret = -EIO;
  731. WARN_ON(1);
  732. goto out;
  733. }
  734. num_refs = btrfs_ref_num_refs(leaf, ref);
  735. BUG_ON(num_refs == 0);
  736. btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
  737. existing_owner = btrfs_ref_objectid(leaf, ref);
  738. if (existing_owner != owner_objectid &&
  739. existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
  740. btrfs_set_ref_objectid(leaf, ref,
  741. BTRFS_MULTIPLE_OBJECTIDS);
  742. }
  743. ret = 0;
  744. } else {
  745. goto out;
  746. }
  747. btrfs_mark_buffer_dirty(path->nodes[0]);
  748. out:
  749. btrfs_release_path(root, path);
  750. return ret;
  751. }
  752. static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
  753. struct btrfs_root *root,
  754. struct btrfs_path *path)
  755. {
  756. struct extent_buffer *leaf;
  757. struct btrfs_extent_ref *ref;
  758. u32 num_refs;
  759. int ret = 0;
  760. leaf = path->nodes[0];
  761. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  762. num_refs = btrfs_ref_num_refs(leaf, ref);
  763. BUG_ON(num_refs == 0);
  764. num_refs -= 1;
  765. if (num_refs == 0) {
  766. ret = btrfs_del_item(trans, root, path);
  767. } else {
  768. btrfs_set_ref_num_refs(leaf, ref, num_refs);
  769. btrfs_mark_buffer_dirty(leaf);
  770. }
  771. btrfs_release_path(root, path);
  772. return ret;
  773. }
  774. #ifdef BIO_RW_DISCARD
  775. static void btrfs_issue_discard(struct block_device *bdev,
  776. u64 start, u64 len)
  777. {
  778. blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
  779. }
  780. #endif
  781. static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
  782. u64 num_bytes)
  783. {
  784. #ifdef BIO_RW_DISCARD
  785. int ret;
  786. u64 map_length = num_bytes;
  787. struct btrfs_multi_bio *multi = NULL;
  788. /* Tell the block device(s) that the sectors can be discarded */
  789. ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
  790. bytenr, &map_length, &multi, 0);
  791. if (!ret) {
  792. struct btrfs_bio_stripe *stripe = multi->stripes;
  793. int i;
  794. if (map_length > num_bytes)
  795. map_length = num_bytes;
  796. for (i = 0; i < multi->num_stripes; i++, stripe++) {
  797. btrfs_issue_discard(stripe->dev->bdev,
  798. stripe->physical,
  799. map_length);
  800. }
  801. kfree(multi);
  802. }
  803. return ret;
  804. #else
  805. return 0;
  806. #endif
  807. }
  808. static noinline int free_extents(struct btrfs_trans_handle *trans,
  809. struct btrfs_root *extent_root,
  810. struct list_head *del_list)
  811. {
  812. struct btrfs_fs_info *info = extent_root->fs_info;
  813. struct btrfs_path *path;
  814. struct btrfs_key key, found_key;
  815. struct extent_buffer *leaf;
  816. struct list_head *cur;
  817. struct pending_extent_op *op;
  818. struct btrfs_extent_item *ei;
  819. int ret, num_to_del, extent_slot = 0, found_extent = 0;
  820. u32 refs;
  821. u64 bytes_freed = 0;
  822. path = btrfs_alloc_path();
  823. if (!path)
  824. return -ENOMEM;
  825. path->reada = 1;
  826. search:
  827. /* search for the backref for the current ref we want to delete */
  828. cur = del_list->next;
  829. op = list_entry(cur, struct pending_extent_op, list);
  830. ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
  831. op->orig_parent,
  832. extent_root->root_key.objectid,
  833. op->orig_generation, op->level, 1);
  834. if (ret) {
  835. printk(KERN_ERR "btrfs unable to find backref byte nr %llu "
  836. "root %llu gen %llu owner %u\n",
  837. (unsigned long long)op->bytenr,
  838. (unsigned long long)extent_root->root_key.objectid,
  839. (unsigned long long)op->orig_generation, op->level);
  840. btrfs_print_leaf(extent_root, path->nodes[0]);
  841. WARN_ON(1);
  842. goto out;
  843. }
  844. extent_slot = path->slots[0];
  845. num_to_del = 1;
  846. found_extent = 0;
  847. /*
  848. * if we aren't the first item on the leaf we can move back one and see
  849. * if our ref is right next to our extent item
  850. */
  851. if (likely(extent_slot)) {
  852. extent_slot--;
  853. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  854. extent_slot);
  855. if (found_key.objectid == op->bytenr &&
  856. found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  857. found_key.offset == op->num_bytes) {
  858. num_to_del++;
  859. found_extent = 1;
  860. }
  861. }
  862. /*
  863. * if we didn't find the extent we need to delete the backref and then
  864. * search for the extent item key so we can update its ref count
  865. */
  866. if (!found_extent) {
  867. key.objectid = op->bytenr;
  868. key.type = BTRFS_EXTENT_ITEM_KEY;
  869. key.offset = op->num_bytes;
  870. ret = remove_extent_backref(trans, extent_root, path);
  871. BUG_ON(ret);
  872. btrfs_release_path(extent_root, path);
  873. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  874. BUG_ON(ret);
  875. extent_slot = path->slots[0];
  876. }
  877. /* this is where we update the ref count for the extent */
  878. leaf = path->nodes[0];
  879. ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
  880. refs = btrfs_extent_refs(leaf, ei);
  881. BUG_ON(refs == 0);
  882. refs--;
  883. btrfs_set_extent_refs(leaf, ei, refs);
  884. btrfs_mark_buffer_dirty(leaf);
  885. /*
  886. * This extent needs deleting. The reason cur_slot is extent_slot +
  887. * num_to_del is because extent_slot points to the slot where the extent
  888. * is, and if the backref was not right next to the extent we will be
  889. * deleting at least 1 item, and will want to start searching at the
  890. * slot directly next to extent_slot. However if we did find the
  891. * backref next to the extent item them we will be deleting at least 2
  892. * items and will want to start searching directly after the ref slot
  893. */
  894. if (!refs) {
  895. struct list_head *pos, *n, *end;
  896. int cur_slot = extent_slot+num_to_del;
  897. u64 super_used;
  898. u64 root_used;
  899. path->slots[0] = extent_slot;
  900. bytes_freed = op->num_bytes;
  901. mutex_lock(&info->pinned_mutex);
  902. ret = pin_down_bytes(trans, extent_root, op->bytenr,
  903. op->num_bytes, op->level >=
  904. BTRFS_FIRST_FREE_OBJECTID);
  905. mutex_unlock(&info->pinned_mutex);
  906. BUG_ON(ret < 0);
  907. op->del = ret;
  908. /*
  909. * we need to see if we can delete multiple things at once, so
  910. * start looping through the list of extents we are wanting to
  911. * delete and see if their extent/backref's are right next to
  912. * eachother and the extents only have 1 ref
  913. */
  914. for (pos = cur->next; pos != del_list; pos = pos->next) {
  915. struct pending_extent_op *tmp;
  916. tmp = list_entry(pos, struct pending_extent_op, list);
  917. /* we only want to delete extent+ref at this stage */
  918. if (cur_slot >= btrfs_header_nritems(leaf) - 1)
  919. break;
  920. btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
  921. if (found_key.objectid != tmp->bytenr ||
  922. found_key.type != BTRFS_EXTENT_ITEM_KEY ||
  923. found_key.offset != tmp->num_bytes)
  924. break;
  925. /* check to make sure this extent only has one ref */
  926. ei = btrfs_item_ptr(leaf, cur_slot,
  927. struct btrfs_extent_item);
  928. if (btrfs_extent_refs(leaf, ei) != 1)
  929. break;
  930. btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
  931. if (found_key.objectid != tmp->bytenr ||
  932. found_key.type != BTRFS_EXTENT_REF_KEY ||
  933. found_key.offset != tmp->orig_parent)
  934. break;
  935. /*
  936. * the ref is right next to the extent, we can set the
  937. * ref count to 0 since we will delete them both now
  938. */
  939. btrfs_set_extent_refs(leaf, ei, 0);
  940. /* pin down the bytes for this extent */
  941. mutex_lock(&info->pinned_mutex);
  942. ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
  943. tmp->num_bytes, tmp->level >=
  944. BTRFS_FIRST_FREE_OBJECTID);
  945. mutex_unlock(&info->pinned_mutex);
  946. BUG_ON(ret < 0);
  947. /*
  948. * use the del field to tell if we need to go ahead and
  949. * free up the extent when we delete the item or not.
  950. */
  951. tmp->del = ret;
  952. bytes_freed += tmp->num_bytes;
  953. num_to_del += 2;
  954. cur_slot += 2;
  955. }
  956. end = pos;
  957. /* update the free space counters */
  958. spin_lock(&info->delalloc_lock);
  959. super_used = btrfs_super_bytes_used(&info->super_copy);
  960. btrfs_set_super_bytes_used(&info->super_copy,
  961. super_used - bytes_freed);
  962. root_used = btrfs_root_used(&extent_root->root_item);
  963. btrfs_set_root_used(&extent_root->root_item,
  964. root_used - bytes_freed);
  965. spin_unlock(&info->delalloc_lock);
  966. /* delete the items */
  967. ret = btrfs_del_items(trans, extent_root, path,
  968. path->slots[0], num_to_del);
  969. BUG_ON(ret);
  970. /*
  971. * loop through the extents we deleted and do the cleanup work
  972. * on them
  973. */
  974. for (pos = cur, n = pos->next; pos != end;
  975. pos = n, n = pos->next) {
  976. struct pending_extent_op *tmp;
  977. tmp = list_entry(pos, struct pending_extent_op, list);
  978. /*
  979. * remember tmp->del tells us wether or not we pinned
  980. * down the extent
  981. */
  982. ret = update_block_group(trans, extent_root,
  983. tmp->bytenr, tmp->num_bytes, 0,
  984. tmp->del);
  985. BUG_ON(ret);
  986. list_del_init(&tmp->list);
  987. unlock_extent(&info->extent_ins, tmp->bytenr,
  988. tmp->bytenr + tmp->num_bytes - 1,
  989. GFP_NOFS);
  990. kfree(tmp);
  991. }
  992. } else if (refs && found_extent) {
  993. /*
  994. * the ref and extent were right next to eachother, but the
  995. * extent still has a ref, so just free the backref and keep
  996. * going
  997. */
  998. ret = remove_extent_backref(trans, extent_root, path);
  999. BUG_ON(ret);
  1000. list_del_init(&op->list);
  1001. unlock_extent(&info->extent_ins, op->bytenr,
  1002. op->bytenr + op->num_bytes - 1, GFP_NOFS);
  1003. kfree(op);
  1004. } else {
  1005. /*
  1006. * the extent has multiple refs and the backref we were looking
  1007. * for was not right next to it, so just unlock and go next,
  1008. * we're good to go
  1009. */
  1010. list_del_init(&op->list);
  1011. unlock_extent(&info->extent_ins, op->bytenr,
  1012. op->bytenr + op->num_bytes - 1, GFP_NOFS);
  1013. kfree(op);
  1014. }
  1015. btrfs_release_path(extent_root, path);
  1016. if (!list_empty(del_list))
  1017. goto search;
  1018. out:
  1019. btrfs_free_path(path);
  1020. return ret;
  1021. }
  1022. static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  1023. struct btrfs_root *root, u64 bytenr,
  1024. u64 orig_parent, u64 parent,
  1025. u64 orig_root, u64 ref_root,
  1026. u64 orig_generation, u64 ref_generation,
  1027. u64 owner_objectid)
  1028. {
  1029. int ret;
  1030. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1031. struct btrfs_path *path;
  1032. if (root == root->fs_info->extent_root) {
  1033. struct pending_extent_op *extent_op;
  1034. u64 num_bytes;
  1035. BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
  1036. num_bytes = btrfs_level_size(root, (int)owner_objectid);
  1037. mutex_lock(&root->fs_info->extent_ins_mutex);
  1038. if (test_range_bit(&root->fs_info->extent_ins, bytenr,
  1039. bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
  1040. u64 priv;
  1041. ret = get_state_private(&root->fs_info->extent_ins,
  1042. bytenr, &priv);
  1043. BUG_ON(ret);
  1044. extent_op = (struct pending_extent_op *)
  1045. (unsigned long)priv;
  1046. BUG_ON(extent_op->parent != orig_parent);
  1047. BUG_ON(extent_op->generation != orig_generation);
  1048. extent_op->parent = parent;
  1049. extent_op->generation = ref_generation;
  1050. } else {
  1051. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  1052. BUG_ON(!extent_op);
  1053. extent_op->type = PENDING_BACKREF_UPDATE;
  1054. extent_op->bytenr = bytenr;
  1055. extent_op->num_bytes = num_bytes;
  1056. extent_op->parent = parent;
  1057. extent_op->orig_parent = orig_parent;
  1058. extent_op->generation = ref_generation;
  1059. extent_op->orig_generation = orig_generation;
  1060. extent_op->level = (int)owner_objectid;
  1061. INIT_LIST_HEAD(&extent_op->list);
  1062. extent_op->del = 0;
  1063. set_extent_bits(&root->fs_info->extent_ins,
  1064. bytenr, bytenr + num_bytes - 1,
  1065. EXTENT_WRITEBACK, GFP_NOFS);
  1066. set_state_private(&root->fs_info->extent_ins,
  1067. bytenr, (unsigned long)extent_op);
  1068. }
  1069. mutex_unlock(&root->fs_info->extent_ins_mutex);
  1070. return 0;
  1071. }
  1072. path = btrfs_alloc_path();
  1073. if (!path)
  1074. return -ENOMEM;
  1075. ret = lookup_extent_backref(trans, extent_root, path,
  1076. bytenr, orig_parent, orig_root,
  1077. orig_generation, owner_objectid, 1);
  1078. if (ret)
  1079. goto out;
  1080. ret = remove_extent_backref(trans, extent_root, path);
  1081. if (ret)
  1082. goto out;
  1083. ret = insert_extent_backref(trans, extent_root, path, bytenr,
  1084. parent, ref_root, ref_generation,
  1085. owner_objectid);
  1086. BUG_ON(ret);
  1087. finish_current_insert(trans, extent_root, 0);
  1088. del_pending_extents(trans, extent_root, 0);
  1089. out:
  1090. btrfs_free_path(path);
  1091. return ret;
  1092. }
  1093. int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  1094. struct btrfs_root *root, u64 bytenr,
  1095. u64 orig_parent, u64 parent,
  1096. u64 ref_root, u64 ref_generation,
  1097. u64 owner_objectid)
  1098. {
  1099. int ret;
  1100. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  1101. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  1102. return 0;
  1103. ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
  1104. parent, ref_root, ref_root,
  1105. ref_generation, ref_generation,
  1106. owner_objectid);
  1107. return ret;
  1108. }
  1109. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1110. struct btrfs_root *root, u64 bytenr,
  1111. u64 orig_parent, u64 parent,
  1112. u64 orig_root, u64 ref_root,
  1113. u64 orig_generation, u64 ref_generation,
  1114. u64 owner_objectid)
  1115. {
  1116. struct btrfs_path *path;
  1117. int ret;
  1118. struct btrfs_key key;
  1119. struct extent_buffer *l;
  1120. struct btrfs_extent_item *item;
  1121. u32 refs;
  1122. path = btrfs_alloc_path();
  1123. if (!path)
  1124. return -ENOMEM;
  1125. path->reada = 1;
  1126. key.objectid = bytenr;
  1127. key.type = BTRFS_EXTENT_ITEM_KEY;
  1128. key.offset = (u64)-1;
  1129. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  1130. 0, 1);
  1131. if (ret < 0)
  1132. return ret;
  1133. BUG_ON(ret == 0 || path->slots[0] == 0);
  1134. path->slots[0]--;
  1135. l = path->nodes[0];
  1136. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1137. if (key.objectid != bytenr) {
  1138. btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
  1139. printk(KERN_ERR "btrfs wanted %llu found %llu\n",
  1140. (unsigned long long)bytenr,
  1141. (unsigned long long)key.objectid);
  1142. BUG();
  1143. }
  1144. BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
  1145. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  1146. refs = btrfs_extent_refs(l, item);
  1147. btrfs_set_extent_refs(l, item, refs + 1);
  1148. btrfs_mark_buffer_dirty(path->nodes[0]);
  1149. btrfs_release_path(root->fs_info->extent_root, path);
  1150. path->reada = 1;
  1151. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  1152. path, bytenr, parent,
  1153. ref_root, ref_generation,
  1154. owner_objectid);
  1155. BUG_ON(ret);
  1156. finish_current_insert(trans, root->fs_info->extent_root, 0);
  1157. del_pending_extents(trans, root->fs_info->extent_root, 0);
  1158. btrfs_free_path(path);
  1159. return 0;
  1160. }
  1161. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1162. struct btrfs_root *root,
  1163. u64 bytenr, u64 num_bytes, u64 parent,
  1164. u64 ref_root, u64 ref_generation,
  1165. u64 owner_objectid)
  1166. {
  1167. int ret;
  1168. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  1169. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  1170. return 0;
  1171. ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
  1172. 0, ref_root, 0, ref_generation,
  1173. owner_objectid);
  1174. return ret;
  1175. }
  1176. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  1177. struct btrfs_root *root)
  1178. {
  1179. u64 start;
  1180. u64 end;
  1181. int ret;
  1182. while(1) {
  1183. finish_current_insert(trans, root->fs_info->extent_root, 1);
  1184. del_pending_extents(trans, root->fs_info->extent_root, 1);
  1185. /* is there more work to do? */
  1186. ret = find_first_extent_bit(&root->fs_info->pending_del,
  1187. 0, &start, &end, EXTENT_WRITEBACK);
  1188. if (!ret)
  1189. continue;
  1190. ret = find_first_extent_bit(&root->fs_info->extent_ins,
  1191. 0, &start, &end, EXTENT_WRITEBACK);
  1192. if (!ret)
  1193. continue;
  1194. break;
  1195. }
  1196. return 0;
  1197. }
  1198. int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
  1199. struct btrfs_root *root, u64 bytenr,
  1200. u64 num_bytes, u32 *refs)
  1201. {
  1202. struct btrfs_path *path;
  1203. int ret;
  1204. struct btrfs_key key;
  1205. struct extent_buffer *l;
  1206. struct btrfs_extent_item *item;
  1207. WARN_ON(num_bytes < root->sectorsize);
  1208. path = btrfs_alloc_path();
  1209. path->reada = 1;
  1210. key.objectid = bytenr;
  1211. key.offset = num_bytes;
  1212. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1213. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  1214. 0, 0);
  1215. if (ret < 0)
  1216. goto out;
  1217. if (ret != 0) {
  1218. btrfs_print_leaf(root, path->nodes[0]);
  1219. printk(KERN_INFO "btrfs failed to find block number %llu\n",
  1220. (unsigned long long)bytenr);
  1221. BUG();
  1222. }
  1223. l = path->nodes[0];
  1224. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  1225. *refs = btrfs_extent_refs(l, item);
  1226. out:
  1227. btrfs_free_path(path);
  1228. return 0;
  1229. }
  1230. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  1231. struct btrfs_root *root, u64 objectid, u64 bytenr)
  1232. {
  1233. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1234. struct btrfs_path *path;
  1235. struct extent_buffer *leaf;
  1236. struct btrfs_extent_ref *ref_item;
  1237. struct btrfs_key key;
  1238. struct btrfs_key found_key;
  1239. u64 ref_root;
  1240. u64 last_snapshot;
  1241. u32 nritems;
  1242. int ret;
  1243. key.objectid = bytenr;
  1244. key.offset = (u64)-1;
  1245. key.type = BTRFS_EXTENT_ITEM_KEY;
  1246. path = btrfs_alloc_path();
  1247. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  1248. if (ret < 0)
  1249. goto out;
  1250. BUG_ON(ret == 0);
  1251. ret = -ENOENT;
  1252. if (path->slots[0] == 0)
  1253. goto out;
  1254. path->slots[0]--;
  1255. leaf = path->nodes[0];
  1256. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1257. if (found_key.objectid != bytenr ||
  1258. found_key.type != BTRFS_EXTENT_ITEM_KEY)
  1259. goto out;
  1260. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1261. while (1) {
  1262. leaf = path->nodes[0];
  1263. nritems = btrfs_header_nritems(leaf);
  1264. if (path->slots[0] >= nritems) {
  1265. ret = btrfs_next_leaf(extent_root, path);
  1266. if (ret < 0)
  1267. goto out;
  1268. if (ret == 0)
  1269. continue;
  1270. break;
  1271. }
  1272. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1273. if (found_key.objectid != bytenr)
  1274. break;
  1275. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  1276. path->slots[0]++;
  1277. continue;
  1278. }
  1279. ref_item = btrfs_item_ptr(leaf, path->slots[0],
  1280. struct btrfs_extent_ref);
  1281. ref_root = btrfs_ref_root(leaf, ref_item);
  1282. if ((ref_root != root->root_key.objectid &&
  1283. ref_root != BTRFS_TREE_LOG_OBJECTID) ||
  1284. objectid != btrfs_ref_objectid(leaf, ref_item)) {
  1285. ret = 1;
  1286. goto out;
  1287. }
  1288. if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
  1289. ret = 1;
  1290. goto out;
  1291. }
  1292. path->slots[0]++;
  1293. }
  1294. ret = 0;
  1295. out:
  1296. btrfs_free_path(path);
  1297. return ret;
  1298. }
  1299. int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1300. struct extent_buffer *buf, u32 nr_extents)
  1301. {
  1302. struct btrfs_key key;
  1303. struct btrfs_file_extent_item *fi;
  1304. u64 root_gen;
  1305. u32 nritems;
  1306. int i;
  1307. int level;
  1308. int ret = 0;
  1309. int shared = 0;
  1310. if (!root->ref_cows)
  1311. return 0;
  1312. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  1313. shared = 0;
  1314. root_gen = root->root_key.offset;
  1315. } else {
  1316. shared = 1;
  1317. root_gen = trans->transid - 1;
  1318. }
  1319. level = btrfs_header_level(buf);
  1320. nritems = btrfs_header_nritems(buf);
  1321. if (level == 0) {
  1322. struct btrfs_leaf_ref *ref;
  1323. struct btrfs_extent_info *info;
  1324. ref = btrfs_alloc_leaf_ref(root, nr_extents);
  1325. if (!ref) {
  1326. ret = -ENOMEM;
  1327. goto out;
  1328. }
  1329. ref->root_gen = root_gen;
  1330. ref->bytenr = buf->start;
  1331. ref->owner = btrfs_header_owner(buf);
  1332. ref->generation = btrfs_header_generation(buf);
  1333. ref->nritems = nr_extents;
  1334. info = ref->extents;
  1335. for (i = 0; nr_extents > 0 && i < nritems; i++) {
  1336. u64 disk_bytenr;
  1337. btrfs_item_key_to_cpu(buf, &key, i);
  1338. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1339. continue;
  1340. fi = btrfs_item_ptr(buf, i,
  1341. struct btrfs_file_extent_item);
  1342. if (btrfs_file_extent_type(buf, fi) ==
  1343. BTRFS_FILE_EXTENT_INLINE)
  1344. continue;
  1345. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1346. if (disk_bytenr == 0)
  1347. continue;
  1348. info->bytenr = disk_bytenr;
  1349. info->num_bytes =
  1350. btrfs_file_extent_disk_num_bytes(buf, fi);
  1351. info->objectid = key.objectid;
  1352. info->offset = key.offset;
  1353. info++;
  1354. }
  1355. ret = btrfs_add_leaf_ref(root, ref, shared);
  1356. if (ret == -EEXIST && shared) {
  1357. struct btrfs_leaf_ref *old;
  1358. old = btrfs_lookup_leaf_ref(root, ref->bytenr);
  1359. BUG_ON(!old);
  1360. btrfs_remove_leaf_ref(root, old);
  1361. btrfs_free_leaf_ref(root, old);
  1362. ret = btrfs_add_leaf_ref(root, ref, shared);
  1363. }
  1364. WARN_ON(ret);
  1365. btrfs_free_leaf_ref(root, ref);
  1366. }
  1367. out:
  1368. return ret;
  1369. }
  1370. /* when a block goes through cow, we update the reference counts of
  1371. * everything that block points to. The internal pointers of the block
  1372. * can be in just about any order, and it is likely to have clusters of
  1373. * things that are close together and clusters of things that are not.
  1374. *
  1375. * To help reduce the seeks that come with updating all of these reference
  1376. * counts, sort them by byte number before actual updates are done.
  1377. *
  1378. * struct refsort is used to match byte number to slot in the btree block.
  1379. * we sort based on the byte number and then use the slot to actually
  1380. * find the item.
  1381. *
  1382. * struct refsort is smaller than strcut btrfs_item and smaller than
  1383. * struct btrfs_key_ptr. Since we're currently limited to the page size
  1384. * for a btree block, there's no way for a kmalloc of refsorts for a
  1385. * single node to be bigger than a page.
  1386. */
  1387. struct refsort {
  1388. u64 bytenr;
  1389. u32 slot;
  1390. };
  1391. /*
  1392. * for passing into sort()
  1393. */
  1394. static int refsort_cmp(const void *a_void, const void *b_void)
  1395. {
  1396. const struct refsort *a = a_void;
  1397. const struct refsort *b = b_void;
  1398. if (a->bytenr < b->bytenr)
  1399. return -1;
  1400. if (a->bytenr > b->bytenr)
  1401. return 1;
  1402. return 0;
  1403. }
  1404. noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
  1405. struct btrfs_root *root,
  1406. struct extent_buffer *orig_buf,
  1407. struct extent_buffer *buf, u32 *nr_extents)
  1408. {
  1409. u64 bytenr;
  1410. u64 ref_root;
  1411. u64 orig_root;
  1412. u64 ref_generation;
  1413. u64 orig_generation;
  1414. struct refsort *sorted;
  1415. u32 nritems;
  1416. u32 nr_file_extents = 0;
  1417. struct btrfs_key key;
  1418. struct btrfs_file_extent_item *fi;
  1419. int i;
  1420. int level;
  1421. int ret = 0;
  1422. int faili = 0;
  1423. int refi = 0;
  1424. int slot;
  1425. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  1426. u64, u64, u64, u64, u64, u64, u64, u64);
  1427. ref_root = btrfs_header_owner(buf);
  1428. ref_generation = btrfs_header_generation(buf);
  1429. orig_root = btrfs_header_owner(orig_buf);
  1430. orig_generation = btrfs_header_generation(orig_buf);
  1431. nritems = btrfs_header_nritems(buf);
  1432. level = btrfs_header_level(buf);
  1433. sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
  1434. BUG_ON(!sorted);
  1435. if (root->ref_cows) {
  1436. process_func = __btrfs_inc_extent_ref;
  1437. } else {
  1438. if (level == 0 &&
  1439. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1440. goto out;
  1441. if (level != 0 &&
  1442. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1443. goto out;
  1444. process_func = __btrfs_update_extent_ref;
  1445. }
  1446. /*
  1447. * we make two passes through the items. In the first pass we
  1448. * only record the byte number and slot. Then we sort based on
  1449. * byte number and do the actual work based on the sorted results
  1450. */
  1451. for (i = 0; i < nritems; i++) {
  1452. cond_resched();
  1453. if (level == 0) {
  1454. btrfs_item_key_to_cpu(buf, &key, i);
  1455. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1456. continue;
  1457. fi = btrfs_item_ptr(buf, i,
  1458. struct btrfs_file_extent_item);
  1459. if (btrfs_file_extent_type(buf, fi) ==
  1460. BTRFS_FILE_EXTENT_INLINE)
  1461. continue;
  1462. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1463. if (bytenr == 0)
  1464. continue;
  1465. nr_file_extents++;
  1466. sorted[refi].bytenr = bytenr;
  1467. sorted[refi].slot = i;
  1468. refi++;
  1469. } else {
  1470. bytenr = btrfs_node_blockptr(buf, i);
  1471. sorted[refi].bytenr = bytenr;
  1472. sorted[refi].slot = i;
  1473. refi++;
  1474. }
  1475. }
  1476. /*
  1477. * if refi == 0, we didn't actually put anything into the sorted
  1478. * array and we're done
  1479. */
  1480. if (refi == 0)
  1481. goto out;
  1482. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  1483. for (i = 0; i < refi; i++) {
  1484. cond_resched();
  1485. slot = sorted[i].slot;
  1486. bytenr = sorted[i].bytenr;
  1487. if (level == 0) {
  1488. btrfs_item_key_to_cpu(buf, &key, slot);
  1489. ret = process_func(trans, root, bytenr,
  1490. orig_buf->start, buf->start,
  1491. orig_root, ref_root,
  1492. orig_generation, ref_generation,
  1493. key.objectid);
  1494. if (ret) {
  1495. faili = slot;
  1496. WARN_ON(1);
  1497. goto fail;
  1498. }
  1499. } else {
  1500. ret = process_func(trans, root, bytenr,
  1501. orig_buf->start, buf->start,
  1502. orig_root, ref_root,
  1503. orig_generation, ref_generation,
  1504. level - 1);
  1505. if (ret) {
  1506. faili = slot;
  1507. WARN_ON(1);
  1508. goto fail;
  1509. }
  1510. }
  1511. }
  1512. out:
  1513. kfree(sorted);
  1514. if (nr_extents) {
  1515. if (level == 0)
  1516. *nr_extents = nr_file_extents;
  1517. else
  1518. *nr_extents = nritems;
  1519. }
  1520. return 0;
  1521. fail:
  1522. kfree(sorted);
  1523. WARN_ON(1);
  1524. return ret;
  1525. }
  1526. int btrfs_update_ref(struct btrfs_trans_handle *trans,
  1527. struct btrfs_root *root, struct extent_buffer *orig_buf,
  1528. struct extent_buffer *buf, int start_slot, int nr)
  1529. {
  1530. u64 bytenr;
  1531. u64 ref_root;
  1532. u64 orig_root;
  1533. u64 ref_generation;
  1534. u64 orig_generation;
  1535. struct btrfs_key key;
  1536. struct btrfs_file_extent_item *fi;
  1537. int i;
  1538. int ret;
  1539. int slot;
  1540. int level;
  1541. BUG_ON(start_slot < 0);
  1542. BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
  1543. ref_root = btrfs_header_owner(buf);
  1544. ref_generation = btrfs_header_generation(buf);
  1545. orig_root = btrfs_header_owner(orig_buf);
  1546. orig_generation = btrfs_header_generation(orig_buf);
  1547. level = btrfs_header_level(buf);
  1548. if (!root->ref_cows) {
  1549. if (level == 0 &&
  1550. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1551. return 0;
  1552. if (level != 0 &&
  1553. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1554. return 0;
  1555. }
  1556. for (i = 0, slot = start_slot; i < nr; i++, slot++) {
  1557. cond_resched();
  1558. if (level == 0) {
  1559. btrfs_item_key_to_cpu(buf, &key, slot);
  1560. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1561. continue;
  1562. fi = btrfs_item_ptr(buf, slot,
  1563. struct btrfs_file_extent_item);
  1564. if (btrfs_file_extent_type(buf, fi) ==
  1565. BTRFS_FILE_EXTENT_INLINE)
  1566. continue;
  1567. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1568. if (bytenr == 0)
  1569. continue;
  1570. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1571. orig_buf->start, buf->start,
  1572. orig_root, ref_root,
  1573. orig_generation, ref_generation,
  1574. key.objectid);
  1575. if (ret)
  1576. goto fail;
  1577. } else {
  1578. bytenr = btrfs_node_blockptr(buf, slot);
  1579. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1580. orig_buf->start, buf->start,
  1581. orig_root, ref_root,
  1582. orig_generation, ref_generation,
  1583. level - 1);
  1584. if (ret)
  1585. goto fail;
  1586. }
  1587. }
  1588. return 0;
  1589. fail:
  1590. WARN_ON(1);
  1591. return -1;
  1592. }
  1593. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  1594. struct btrfs_root *root,
  1595. struct btrfs_path *path,
  1596. struct btrfs_block_group_cache *cache)
  1597. {
  1598. int ret;
  1599. int pending_ret;
  1600. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1601. unsigned long bi;
  1602. struct extent_buffer *leaf;
  1603. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  1604. if (ret < 0)
  1605. goto fail;
  1606. BUG_ON(ret);
  1607. leaf = path->nodes[0];
  1608. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1609. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  1610. btrfs_mark_buffer_dirty(leaf);
  1611. btrfs_release_path(extent_root, path);
  1612. fail:
  1613. finish_current_insert(trans, extent_root, 0);
  1614. pending_ret = del_pending_extents(trans, extent_root, 0);
  1615. if (ret)
  1616. return ret;
  1617. if (pending_ret)
  1618. return pending_ret;
  1619. return 0;
  1620. }
  1621. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  1622. struct btrfs_root *root)
  1623. {
  1624. struct btrfs_block_group_cache *cache, *entry;
  1625. struct rb_node *n;
  1626. int err = 0;
  1627. int werr = 0;
  1628. struct btrfs_path *path;
  1629. u64 last = 0;
  1630. path = btrfs_alloc_path();
  1631. if (!path)
  1632. return -ENOMEM;
  1633. while (1) {
  1634. cache = NULL;
  1635. spin_lock(&root->fs_info->block_group_cache_lock);
  1636. for (n = rb_first(&root->fs_info->block_group_cache_tree);
  1637. n; n = rb_next(n)) {
  1638. entry = rb_entry(n, struct btrfs_block_group_cache,
  1639. cache_node);
  1640. if (entry->dirty) {
  1641. cache = entry;
  1642. break;
  1643. }
  1644. }
  1645. spin_unlock(&root->fs_info->block_group_cache_lock);
  1646. if (!cache)
  1647. break;
  1648. cache->dirty = 0;
  1649. last += cache->key.offset;
  1650. err = write_one_cache_group(trans, root,
  1651. path, cache);
  1652. /*
  1653. * if we fail to write the cache group, we want
  1654. * to keep it marked dirty in hopes that a later
  1655. * write will work
  1656. */
  1657. if (err) {
  1658. werr = err;
  1659. continue;
  1660. }
  1661. }
  1662. btrfs_free_path(path);
  1663. return werr;
  1664. }
  1665. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  1666. {
  1667. struct btrfs_block_group_cache *block_group;
  1668. int readonly = 0;
  1669. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  1670. if (!block_group || block_group->ro)
  1671. readonly = 1;
  1672. if (block_group)
  1673. put_block_group(block_group);
  1674. return readonly;
  1675. }
  1676. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  1677. u64 total_bytes, u64 bytes_used,
  1678. struct btrfs_space_info **space_info)
  1679. {
  1680. struct btrfs_space_info *found;
  1681. found = __find_space_info(info, flags);
  1682. if (found) {
  1683. spin_lock(&found->lock);
  1684. found->total_bytes += total_bytes;
  1685. found->bytes_used += bytes_used;
  1686. found->full = 0;
  1687. spin_unlock(&found->lock);
  1688. *space_info = found;
  1689. return 0;
  1690. }
  1691. found = kzalloc(sizeof(*found), GFP_NOFS);
  1692. if (!found)
  1693. return -ENOMEM;
  1694. list_add(&found->list, &info->space_info);
  1695. INIT_LIST_HEAD(&found->block_groups);
  1696. init_rwsem(&found->groups_sem);
  1697. spin_lock_init(&found->lock);
  1698. found->flags = flags;
  1699. found->total_bytes = total_bytes;
  1700. found->bytes_used = bytes_used;
  1701. found->bytes_pinned = 0;
  1702. found->bytes_reserved = 0;
  1703. found->bytes_readonly = 0;
  1704. found->full = 0;
  1705. found->force_alloc = 0;
  1706. *space_info = found;
  1707. return 0;
  1708. }
  1709. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  1710. {
  1711. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  1712. BTRFS_BLOCK_GROUP_RAID1 |
  1713. BTRFS_BLOCK_GROUP_RAID10 |
  1714. BTRFS_BLOCK_GROUP_DUP);
  1715. if (extra_flags) {
  1716. if (flags & BTRFS_BLOCK_GROUP_DATA)
  1717. fs_info->avail_data_alloc_bits |= extra_flags;
  1718. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  1719. fs_info->avail_metadata_alloc_bits |= extra_flags;
  1720. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  1721. fs_info->avail_system_alloc_bits |= extra_flags;
  1722. }
  1723. }
  1724. static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
  1725. {
  1726. spin_lock(&cache->space_info->lock);
  1727. spin_lock(&cache->lock);
  1728. if (!cache->ro) {
  1729. cache->space_info->bytes_readonly += cache->key.offset -
  1730. btrfs_block_group_used(&cache->item);
  1731. cache->ro = 1;
  1732. }
  1733. spin_unlock(&cache->lock);
  1734. spin_unlock(&cache->space_info->lock);
  1735. }
  1736. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  1737. {
  1738. u64 num_devices = root->fs_info->fs_devices->rw_devices;
  1739. if (num_devices == 1)
  1740. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  1741. if (num_devices < 4)
  1742. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  1743. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  1744. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  1745. BTRFS_BLOCK_GROUP_RAID10))) {
  1746. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  1747. }
  1748. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  1749. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  1750. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  1751. }
  1752. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  1753. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  1754. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  1755. (flags & BTRFS_BLOCK_GROUP_DUP)))
  1756. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  1757. return flags;
  1758. }
  1759. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  1760. struct btrfs_root *extent_root, u64 alloc_bytes,
  1761. u64 flags, int force)
  1762. {
  1763. struct btrfs_space_info *space_info;
  1764. u64 thresh;
  1765. int ret = 0;
  1766. mutex_lock(&extent_root->fs_info->chunk_mutex);
  1767. flags = btrfs_reduce_alloc_profile(extent_root, flags);
  1768. space_info = __find_space_info(extent_root->fs_info, flags);
  1769. if (!space_info) {
  1770. ret = update_space_info(extent_root->fs_info, flags,
  1771. 0, 0, &space_info);
  1772. BUG_ON(ret);
  1773. }
  1774. BUG_ON(!space_info);
  1775. spin_lock(&space_info->lock);
  1776. if (space_info->force_alloc) {
  1777. force = 1;
  1778. space_info->force_alloc = 0;
  1779. }
  1780. if (space_info->full) {
  1781. spin_unlock(&space_info->lock);
  1782. goto out;
  1783. }
  1784. thresh = space_info->total_bytes - space_info->bytes_readonly;
  1785. thresh = div_factor(thresh, 6);
  1786. if (!force &&
  1787. (space_info->bytes_used + space_info->bytes_pinned +
  1788. space_info->bytes_reserved + alloc_bytes) < thresh) {
  1789. spin_unlock(&space_info->lock);
  1790. goto out;
  1791. }
  1792. spin_unlock(&space_info->lock);
  1793. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  1794. if (ret)
  1795. space_info->full = 1;
  1796. out:
  1797. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  1798. return ret;
  1799. }
  1800. static int update_block_group(struct btrfs_trans_handle *trans,
  1801. struct btrfs_root *root,
  1802. u64 bytenr, u64 num_bytes, int alloc,
  1803. int mark_free)
  1804. {
  1805. struct btrfs_block_group_cache *cache;
  1806. struct btrfs_fs_info *info = root->fs_info;
  1807. u64 total = num_bytes;
  1808. u64 old_val;
  1809. u64 byte_in_group;
  1810. while (total) {
  1811. cache = btrfs_lookup_block_group(info, bytenr);
  1812. if (!cache)
  1813. return -1;
  1814. byte_in_group = bytenr - cache->key.objectid;
  1815. WARN_ON(byte_in_group > cache->key.offset);
  1816. spin_lock(&cache->space_info->lock);
  1817. spin_lock(&cache->lock);
  1818. cache->dirty = 1;
  1819. old_val = btrfs_block_group_used(&cache->item);
  1820. num_bytes = min(total, cache->key.offset - byte_in_group);
  1821. if (alloc) {
  1822. old_val += num_bytes;
  1823. cache->space_info->bytes_used += num_bytes;
  1824. if (cache->ro)
  1825. cache->space_info->bytes_readonly -= num_bytes;
  1826. btrfs_set_block_group_used(&cache->item, old_val);
  1827. spin_unlock(&cache->lock);
  1828. spin_unlock(&cache->space_info->lock);
  1829. } else {
  1830. old_val -= num_bytes;
  1831. cache->space_info->bytes_used -= num_bytes;
  1832. if (cache->ro)
  1833. cache->space_info->bytes_readonly += num_bytes;
  1834. btrfs_set_block_group_used(&cache->item, old_val);
  1835. spin_unlock(&cache->lock);
  1836. spin_unlock(&cache->space_info->lock);
  1837. if (mark_free) {
  1838. int ret;
  1839. ret = btrfs_discard_extent(root, bytenr,
  1840. num_bytes);
  1841. WARN_ON(ret);
  1842. ret = btrfs_add_free_space(cache, bytenr,
  1843. num_bytes);
  1844. WARN_ON(ret);
  1845. }
  1846. }
  1847. put_block_group(cache);
  1848. total -= num_bytes;
  1849. bytenr += num_bytes;
  1850. }
  1851. return 0;
  1852. }
  1853. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  1854. {
  1855. struct btrfs_block_group_cache *cache;
  1856. u64 bytenr;
  1857. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  1858. if (!cache)
  1859. return 0;
  1860. bytenr = cache->key.objectid;
  1861. put_block_group(cache);
  1862. return bytenr;
  1863. }
  1864. int btrfs_update_pinned_extents(struct btrfs_root *root,
  1865. u64 bytenr, u64 num, int pin)
  1866. {
  1867. u64 len;
  1868. struct btrfs_block_group_cache *cache;
  1869. struct btrfs_fs_info *fs_info = root->fs_info;
  1870. WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
  1871. if (pin) {
  1872. set_extent_dirty(&fs_info->pinned_extents,
  1873. bytenr, bytenr + num - 1, GFP_NOFS);
  1874. } else {
  1875. clear_extent_dirty(&fs_info->pinned_extents,
  1876. bytenr, bytenr + num - 1, GFP_NOFS);
  1877. }
  1878. while (num > 0) {
  1879. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1880. BUG_ON(!cache);
  1881. len = min(num, cache->key.offset -
  1882. (bytenr - cache->key.objectid));
  1883. if (pin) {
  1884. spin_lock(&cache->space_info->lock);
  1885. spin_lock(&cache->lock);
  1886. cache->pinned += len;
  1887. cache->space_info->bytes_pinned += len;
  1888. spin_unlock(&cache->lock);
  1889. spin_unlock(&cache->space_info->lock);
  1890. fs_info->total_pinned += len;
  1891. } else {
  1892. spin_lock(&cache->space_info->lock);
  1893. spin_lock(&cache->lock);
  1894. cache->pinned -= len;
  1895. cache->space_info->bytes_pinned -= len;
  1896. spin_unlock(&cache->lock);
  1897. spin_unlock(&cache->space_info->lock);
  1898. fs_info->total_pinned -= len;
  1899. if (cache->cached)
  1900. btrfs_add_free_space(cache, bytenr, len);
  1901. }
  1902. put_block_group(cache);
  1903. bytenr += len;
  1904. num -= len;
  1905. }
  1906. return 0;
  1907. }
  1908. static int update_reserved_extents(struct btrfs_root *root,
  1909. u64 bytenr, u64 num, int reserve)
  1910. {
  1911. u64 len;
  1912. struct btrfs_block_group_cache *cache;
  1913. struct btrfs_fs_info *fs_info = root->fs_info;
  1914. while (num > 0) {
  1915. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1916. BUG_ON(!cache);
  1917. len = min(num, cache->key.offset -
  1918. (bytenr - cache->key.objectid));
  1919. spin_lock(&cache->space_info->lock);
  1920. spin_lock(&cache->lock);
  1921. if (reserve) {
  1922. cache->reserved += len;
  1923. cache->space_info->bytes_reserved += len;
  1924. } else {
  1925. cache->reserved -= len;
  1926. cache->space_info->bytes_reserved -= len;
  1927. }
  1928. spin_unlock(&cache->lock);
  1929. spin_unlock(&cache->space_info->lock);
  1930. put_block_group(cache);
  1931. bytenr += len;
  1932. num -= len;
  1933. }
  1934. return 0;
  1935. }
  1936. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  1937. {
  1938. u64 last = 0;
  1939. u64 start;
  1940. u64 end;
  1941. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  1942. int ret;
  1943. mutex_lock(&root->fs_info->pinned_mutex);
  1944. while (1) {
  1945. ret = find_first_extent_bit(pinned_extents, last,
  1946. &start, &end, EXTENT_DIRTY);
  1947. if (ret)
  1948. break;
  1949. set_extent_dirty(copy, start, end, GFP_NOFS);
  1950. last = end + 1;
  1951. }
  1952. mutex_unlock(&root->fs_info->pinned_mutex);
  1953. return 0;
  1954. }
  1955. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  1956. struct btrfs_root *root,
  1957. struct extent_io_tree *unpin)
  1958. {
  1959. u64 start;
  1960. u64 end;
  1961. int ret;
  1962. mutex_lock(&root->fs_info->pinned_mutex);
  1963. while (1) {
  1964. ret = find_first_extent_bit(unpin, 0, &start, &end,
  1965. EXTENT_DIRTY);
  1966. if (ret)
  1967. break;
  1968. ret = btrfs_discard_extent(root, start, end + 1 - start);
  1969. btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
  1970. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  1971. if (need_resched()) {
  1972. mutex_unlock(&root->fs_info->pinned_mutex);
  1973. cond_resched();
  1974. mutex_lock(&root->fs_info->pinned_mutex);
  1975. }
  1976. }
  1977. mutex_unlock(&root->fs_info->pinned_mutex);
  1978. return ret;
  1979. }
  1980. static int finish_current_insert(struct btrfs_trans_handle *trans,
  1981. struct btrfs_root *extent_root, int all)
  1982. {
  1983. u64 start;
  1984. u64 end;
  1985. u64 priv;
  1986. u64 search = 0;
  1987. struct btrfs_fs_info *info = extent_root->fs_info;
  1988. struct btrfs_path *path;
  1989. struct pending_extent_op *extent_op, *tmp;
  1990. struct list_head insert_list, update_list;
  1991. int ret;
  1992. int num_inserts = 0, max_inserts, restart = 0;
  1993. path = btrfs_alloc_path();
  1994. INIT_LIST_HEAD(&insert_list);
  1995. INIT_LIST_HEAD(&update_list);
  1996. max_inserts = extent_root->leafsize /
  1997. (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
  1998. sizeof(struct btrfs_extent_ref) +
  1999. sizeof(struct btrfs_extent_item));
  2000. again:
  2001. mutex_lock(&info->extent_ins_mutex);
  2002. while (1) {
  2003. ret = find_first_extent_bit(&info->extent_ins, search, &start,
  2004. &end, EXTENT_WRITEBACK);
  2005. if (ret) {
  2006. if (restart && !num_inserts &&
  2007. list_empty(&update_list)) {
  2008. restart = 0;
  2009. search = 0;
  2010. continue;
  2011. }
  2012. break;
  2013. }
  2014. ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
  2015. if (!ret) {
  2016. if (all)
  2017. restart = 1;
  2018. search = end + 1;
  2019. if (need_resched()) {
  2020. mutex_unlock(&info->extent_ins_mutex);
  2021. cond_resched();
  2022. mutex_lock(&info->extent_ins_mutex);
  2023. }
  2024. continue;
  2025. }
  2026. ret = get_state_private(&info->extent_ins, start, &priv);
  2027. BUG_ON(ret);
  2028. extent_op = (struct pending_extent_op *)(unsigned long) priv;
  2029. if (extent_op->type == PENDING_EXTENT_INSERT) {
  2030. num_inserts++;
  2031. list_add_tail(&extent_op->list, &insert_list);
  2032. search = end + 1;
  2033. if (num_inserts == max_inserts) {
  2034. restart = 1;
  2035. break;
  2036. }
  2037. } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
  2038. list_add_tail(&extent_op->list, &update_list);
  2039. search = end + 1;
  2040. } else {
  2041. BUG();
  2042. }
  2043. }
  2044. /*
  2045. * process the update list, clear the writeback bit for it, and if
  2046. * somebody marked this thing for deletion then just unlock it and be
  2047. * done, the free_extents will handle it
  2048. */
  2049. list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
  2050. clear_extent_bits(&info->extent_ins, extent_op->bytenr,
  2051. extent_op->bytenr + extent_op->num_bytes - 1,
  2052. EXTENT_WRITEBACK, GFP_NOFS);
  2053. if (extent_op->del) {
  2054. list_del_init(&extent_op->list);
  2055. unlock_extent(&info->extent_ins, extent_op->bytenr,
  2056. extent_op->bytenr + extent_op->num_bytes
  2057. - 1, GFP_NOFS);
  2058. kfree(extent_op);
  2059. }
  2060. }
  2061. mutex_unlock(&info->extent_ins_mutex);
  2062. /*
  2063. * still have things left on the update list, go ahead an update
  2064. * everything
  2065. */
  2066. if (!list_empty(&update_list)) {
  2067. ret = update_backrefs(trans, extent_root, path, &update_list);
  2068. BUG_ON(ret);
  2069. /* we may have COW'ed new blocks, so lets start over */
  2070. if (all)
  2071. restart = 1;
  2072. }
  2073. /*
  2074. * if no inserts need to be done, but we skipped some extents and we
  2075. * need to make sure everything is cleaned then reset everything and
  2076. * go back to the beginning
  2077. */
  2078. if (!num_inserts && restart) {
  2079. search = 0;
  2080. restart = 0;
  2081. INIT_LIST_HEAD(&update_list);
  2082. INIT_LIST_HEAD(&insert_list);
  2083. goto again;
  2084. } else if (!num_inserts) {
  2085. goto out;
  2086. }
  2087. /*
  2088. * process the insert extents list. Again if we are deleting this
  2089. * extent, then just unlock it, pin down the bytes if need be, and be
  2090. * done with it. Saves us from having to actually insert the extent
  2091. * into the tree and then subsequently come along and delete it
  2092. */
  2093. mutex_lock(&info->extent_ins_mutex);
  2094. list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
  2095. clear_extent_bits(&info->extent_ins, extent_op->bytenr,
  2096. extent_op->bytenr + extent_op->num_bytes - 1,
  2097. EXTENT_WRITEBACK, GFP_NOFS);
  2098. if (extent_op->del) {
  2099. u64 used;
  2100. list_del_init(&extent_op->list);
  2101. unlock_extent(&info->extent_ins, extent_op->bytenr,
  2102. extent_op->bytenr + extent_op->num_bytes
  2103. - 1, GFP_NOFS);
  2104. mutex_lock(&extent_root->fs_info->pinned_mutex);
  2105. ret = pin_down_bytes(trans, extent_root,
  2106. extent_op->bytenr,
  2107. extent_op->num_bytes, 0);
  2108. mutex_unlock(&extent_root->fs_info->pinned_mutex);
  2109. spin_lock(&info->delalloc_lock);
  2110. used = btrfs_super_bytes_used(&info->super_copy);
  2111. btrfs_set_super_bytes_used(&info->super_copy,
  2112. used - extent_op->num_bytes);
  2113. used = btrfs_root_used(&extent_root->root_item);
  2114. btrfs_set_root_used(&extent_root->root_item,
  2115. used - extent_op->num_bytes);
  2116. spin_unlock(&info->delalloc_lock);
  2117. ret = update_block_group(trans, extent_root,
  2118. extent_op->bytenr,
  2119. extent_op->num_bytes,
  2120. 0, ret > 0);
  2121. BUG_ON(ret);
  2122. kfree(extent_op);
  2123. num_inserts--;
  2124. }
  2125. }
  2126. mutex_unlock(&info->extent_ins_mutex);
  2127. ret = insert_extents(trans, extent_root, path, &insert_list,
  2128. num_inserts);
  2129. BUG_ON(ret);
  2130. /*
  2131. * if restart is set for whatever reason we need to go back and start
  2132. * searching through the pending list again.
  2133. *
  2134. * We just inserted some extents, which could have resulted in new
  2135. * blocks being allocated, which would result in new blocks needing
  2136. * updates, so if all is set we _must_ restart to get the updated
  2137. * blocks.
  2138. */
  2139. if (restart || all) {
  2140. INIT_LIST_HEAD(&insert_list);
  2141. INIT_LIST_HEAD(&update_list);
  2142. search = 0;
  2143. restart = 0;
  2144. num_inserts = 0;
  2145. goto again;
  2146. }
  2147. out:
  2148. btrfs_free_path(path);
  2149. return 0;
  2150. }
  2151. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  2152. struct btrfs_root *root,
  2153. u64 bytenr, u64 num_bytes, int is_data)
  2154. {
  2155. int err = 0;
  2156. struct extent_buffer *buf;
  2157. if (is_data)
  2158. goto pinit;
  2159. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  2160. if (!buf)
  2161. goto pinit;
  2162. /* we can reuse a block if it hasn't been written
  2163. * and it is from this transaction. We can't
  2164. * reuse anything from the tree log root because
  2165. * it has tiny sub-transactions.
  2166. */
  2167. if (btrfs_buffer_uptodate(buf, 0) &&
  2168. btrfs_try_tree_lock(buf)) {
  2169. u64 header_owner = btrfs_header_owner(buf);
  2170. u64 header_transid = btrfs_header_generation(buf);
  2171. if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
  2172. header_owner != BTRFS_TREE_RELOC_OBJECTID &&
  2173. header_transid == trans->transid &&
  2174. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  2175. clean_tree_block(NULL, root, buf);
  2176. btrfs_tree_unlock(buf);
  2177. free_extent_buffer(buf);
  2178. return 1;
  2179. }
  2180. btrfs_tree_unlock(buf);
  2181. }
  2182. free_extent_buffer(buf);
  2183. pinit:
  2184. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  2185. BUG_ON(err < 0);
  2186. return 0;
  2187. }
  2188. /*
  2189. * remove an extent from the root, returns 0 on success
  2190. */
  2191. static int __free_extent(struct btrfs_trans_handle *trans,
  2192. struct btrfs_root *root,
  2193. u64 bytenr, u64 num_bytes, u64 parent,
  2194. u64 root_objectid, u64 ref_generation,
  2195. u64 owner_objectid, int pin, int mark_free)
  2196. {
  2197. struct btrfs_path *path;
  2198. struct btrfs_key key;
  2199. struct btrfs_fs_info *info = root->fs_info;
  2200. struct btrfs_root *extent_root = info->extent_root;
  2201. struct extent_buffer *leaf;
  2202. int ret;
  2203. int extent_slot = 0;
  2204. int found_extent = 0;
  2205. int num_to_del = 1;
  2206. struct btrfs_extent_item *ei;
  2207. u32 refs;
  2208. key.objectid = bytenr;
  2209. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  2210. key.offset = num_bytes;
  2211. path = btrfs_alloc_path();
  2212. if (!path)
  2213. return -ENOMEM;
  2214. path->reada = 1;
  2215. ret = lookup_extent_backref(trans, extent_root, path,
  2216. bytenr, parent, root_objectid,
  2217. ref_generation, owner_objectid, 1);
  2218. if (ret == 0) {
  2219. struct btrfs_key found_key;
  2220. extent_slot = path->slots[0];
  2221. while (extent_slot > 0) {
  2222. extent_slot--;
  2223. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2224. extent_slot);
  2225. if (found_key.objectid != bytenr)
  2226. break;
  2227. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  2228. found_key.offset == num_bytes) {
  2229. found_extent = 1;
  2230. break;
  2231. }
  2232. if (path->slots[0] - extent_slot > 5)
  2233. break;
  2234. }
  2235. if (!found_extent) {
  2236. ret = remove_extent_backref(trans, extent_root, path);
  2237. BUG_ON(ret);
  2238. btrfs_release_path(extent_root, path);
  2239. ret = btrfs_search_slot(trans, extent_root,
  2240. &key, path, -1, 1);
  2241. if (ret) {
  2242. printk(KERN_ERR "umm, got %d back from search"
  2243. ", was looking for %llu\n", ret,
  2244. (unsigned long long)bytenr);
  2245. btrfs_print_leaf(extent_root, path->nodes[0]);
  2246. }
  2247. BUG_ON(ret);
  2248. extent_slot = path->slots[0];
  2249. }
  2250. } else {
  2251. btrfs_print_leaf(extent_root, path->nodes[0]);
  2252. WARN_ON(1);
  2253. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  2254. "root %llu gen %llu owner %llu\n",
  2255. (unsigned long long)bytenr,
  2256. (unsigned long long)root_objectid,
  2257. (unsigned long long)ref_generation,
  2258. (unsigned long long)owner_objectid);
  2259. }
  2260. leaf = path->nodes[0];
  2261. ei = btrfs_item_ptr(leaf, extent_slot,
  2262. struct btrfs_extent_item);
  2263. refs = btrfs_extent_refs(leaf, ei);
  2264. BUG_ON(refs == 0);
  2265. refs -= 1;
  2266. btrfs_set_extent_refs(leaf, ei, refs);
  2267. btrfs_mark_buffer_dirty(leaf);
  2268. if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
  2269. struct btrfs_extent_ref *ref;
  2270. ref = btrfs_item_ptr(leaf, path->slots[0],
  2271. struct btrfs_extent_ref);
  2272. BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
  2273. /* if the back ref and the extent are next to each other
  2274. * they get deleted below in one shot
  2275. */
  2276. path->slots[0] = extent_slot;
  2277. num_to_del = 2;
  2278. } else if (found_extent) {
  2279. /* otherwise delete the extent back ref */
  2280. ret = remove_extent_backref(trans, extent_root, path);
  2281. BUG_ON(ret);
  2282. /* if refs are 0, we need to setup the path for deletion */
  2283. if (refs == 0) {
  2284. btrfs_release_path(extent_root, path);
  2285. ret = btrfs_search_slot(trans, extent_root, &key, path,
  2286. -1, 1);
  2287. BUG_ON(ret);
  2288. }
  2289. }
  2290. if (refs == 0) {
  2291. u64 super_used;
  2292. u64 root_used;
  2293. if (pin) {
  2294. mutex_lock(&root->fs_info->pinned_mutex);
  2295. ret = pin_down_bytes(trans, root, bytenr, num_bytes,
  2296. owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
  2297. mutex_unlock(&root->fs_info->pinned_mutex);
  2298. if (ret > 0)
  2299. mark_free = 1;
  2300. BUG_ON(ret < 0);
  2301. }
  2302. /* block accounting for super block */
  2303. spin_lock(&info->delalloc_lock);
  2304. super_used = btrfs_super_bytes_used(&info->super_copy);
  2305. btrfs_set_super_bytes_used(&info->super_copy,
  2306. super_used - num_bytes);
  2307. /* block accounting for root item */
  2308. root_used = btrfs_root_used(&root->root_item);
  2309. btrfs_set_root_used(&root->root_item,
  2310. root_used - num_bytes);
  2311. spin_unlock(&info->delalloc_lock);
  2312. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  2313. num_to_del);
  2314. BUG_ON(ret);
  2315. btrfs_release_path(extent_root, path);
  2316. if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  2317. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  2318. BUG_ON(ret);
  2319. }
  2320. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  2321. mark_free);
  2322. BUG_ON(ret);
  2323. }
  2324. btrfs_free_path(path);
  2325. finish_current_insert(trans, extent_root, 0);
  2326. return ret;
  2327. }
  2328. /*
  2329. * find all the blocks marked as pending in the radix tree and remove
  2330. * them from the extent map
  2331. */
  2332. static int del_pending_extents(struct btrfs_trans_handle *trans,
  2333. struct btrfs_root *extent_root, int all)
  2334. {
  2335. int ret;
  2336. int err = 0;
  2337. u64 start;
  2338. u64 end;
  2339. u64 priv;
  2340. u64 search = 0;
  2341. int nr = 0, skipped = 0;
  2342. struct extent_io_tree *pending_del;
  2343. struct extent_io_tree *extent_ins;
  2344. struct pending_extent_op *extent_op;
  2345. struct btrfs_fs_info *info = extent_root->fs_info;
  2346. struct list_head delete_list;
  2347. INIT_LIST_HEAD(&delete_list);
  2348. extent_ins = &extent_root->fs_info->extent_ins;
  2349. pending_del = &extent_root->fs_info->pending_del;
  2350. again:
  2351. mutex_lock(&info->extent_ins_mutex);
  2352. while (1) {
  2353. ret = find_first_extent_bit(pending_del, search, &start, &end,
  2354. EXTENT_WRITEBACK);
  2355. if (ret) {
  2356. if (all && skipped && !nr) {
  2357. search = 0;
  2358. skipped = 0;
  2359. continue;
  2360. }
  2361. mutex_unlock(&info->extent_ins_mutex);
  2362. break;
  2363. }
  2364. ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
  2365. if (!ret) {
  2366. search = end+1;
  2367. skipped = 1;
  2368. if (need_resched()) {
  2369. mutex_unlock(&info->extent_ins_mutex);
  2370. cond_resched();
  2371. mutex_lock(&info->extent_ins_mutex);
  2372. }
  2373. continue;
  2374. }
  2375. BUG_ON(ret < 0);
  2376. ret = get_state_private(pending_del, start, &priv);
  2377. BUG_ON(ret);
  2378. extent_op = (struct pending_extent_op *)(unsigned long)priv;
  2379. clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
  2380. GFP_NOFS);
  2381. if (!test_range_bit(extent_ins, start, end,
  2382. EXTENT_WRITEBACK, 0)) {
  2383. list_add_tail(&extent_op->list, &delete_list);
  2384. nr++;
  2385. } else {
  2386. kfree(extent_op);
  2387. ret = get_state_private(&info->extent_ins, start,
  2388. &priv);
  2389. BUG_ON(ret);
  2390. extent_op = (struct pending_extent_op *)
  2391. (unsigned long)priv;
  2392. clear_extent_bits(&info->extent_ins, start, end,
  2393. EXTENT_WRITEBACK, GFP_NOFS);
  2394. if (extent_op->type == PENDING_BACKREF_UPDATE) {
  2395. list_add_tail(&extent_op->list, &delete_list);
  2396. search = end + 1;
  2397. nr++;
  2398. continue;
  2399. }
  2400. mutex_lock(&extent_root->fs_info->pinned_mutex);
  2401. ret = pin_down_bytes(trans, extent_root, start,
  2402. end + 1 - start, 0);
  2403. mutex_unlock(&extent_root->fs_info->pinned_mutex);
  2404. ret = update_block_group(trans, extent_root, start,
  2405. end + 1 - start, 0, ret > 0);
  2406. unlock_extent(extent_ins, start, end, GFP_NOFS);
  2407. BUG_ON(ret);
  2408. kfree(extent_op);
  2409. }
  2410. if (ret)
  2411. err = ret;
  2412. search = end + 1;
  2413. if (need_resched()) {
  2414. mutex_unlock(&info->extent_ins_mutex);
  2415. cond_resched();
  2416. mutex_lock(&info->extent_ins_mutex);
  2417. }
  2418. }
  2419. if (nr) {
  2420. ret = free_extents(trans, extent_root, &delete_list);
  2421. BUG_ON(ret);
  2422. }
  2423. if (all && skipped) {
  2424. INIT_LIST_HEAD(&delete_list);
  2425. search = 0;
  2426. nr = 0;
  2427. goto again;
  2428. }
  2429. if (!err)
  2430. finish_current_insert(trans, extent_root, 0);
  2431. return err;
  2432. }
  2433. /*
  2434. * remove an extent from the root, returns 0 on success
  2435. */
  2436. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  2437. struct btrfs_root *root,
  2438. u64 bytenr, u64 num_bytes, u64 parent,
  2439. u64 root_objectid, u64 ref_generation,
  2440. u64 owner_objectid, int pin)
  2441. {
  2442. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2443. int pending_ret;
  2444. int ret;
  2445. WARN_ON(num_bytes < root->sectorsize);
  2446. if (root == extent_root) {
  2447. struct pending_extent_op *extent_op = NULL;
  2448. mutex_lock(&root->fs_info->extent_ins_mutex);
  2449. if (test_range_bit(&root->fs_info->extent_ins, bytenr,
  2450. bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
  2451. u64 priv;
  2452. ret = get_state_private(&root->fs_info->extent_ins,
  2453. bytenr, &priv);
  2454. BUG_ON(ret);
  2455. extent_op = (struct pending_extent_op *)
  2456. (unsigned long)priv;
  2457. extent_op->del = 1;
  2458. if (extent_op->type == PENDING_EXTENT_INSERT) {
  2459. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2460. return 0;
  2461. }
  2462. }
  2463. if (extent_op) {
  2464. ref_generation = extent_op->orig_generation;
  2465. parent = extent_op->orig_parent;
  2466. }
  2467. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2468. BUG_ON(!extent_op);
  2469. extent_op->type = PENDING_EXTENT_DELETE;
  2470. extent_op->bytenr = bytenr;
  2471. extent_op->num_bytes = num_bytes;
  2472. extent_op->parent = parent;
  2473. extent_op->orig_parent = parent;
  2474. extent_op->generation = ref_generation;
  2475. extent_op->orig_generation = ref_generation;
  2476. extent_op->level = (int)owner_objectid;
  2477. INIT_LIST_HEAD(&extent_op->list);
  2478. extent_op->del = 0;
  2479. set_extent_bits(&root->fs_info->pending_del,
  2480. bytenr, bytenr + num_bytes - 1,
  2481. EXTENT_WRITEBACK, GFP_NOFS);
  2482. set_state_private(&root->fs_info->pending_del,
  2483. bytenr, (unsigned long)extent_op);
  2484. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2485. return 0;
  2486. }
  2487. /* if metadata always pin */
  2488. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  2489. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  2490. mutex_lock(&root->fs_info->pinned_mutex);
  2491. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  2492. mutex_unlock(&root->fs_info->pinned_mutex);
  2493. update_reserved_extents(root, bytenr, num_bytes, 0);
  2494. return 0;
  2495. }
  2496. pin = 1;
  2497. }
  2498. /* if data pin when any transaction has committed this */
  2499. if (ref_generation != trans->transid)
  2500. pin = 1;
  2501. ret = __free_extent(trans, root, bytenr, num_bytes, parent,
  2502. root_objectid, ref_generation,
  2503. owner_objectid, pin, pin == 0);
  2504. finish_current_insert(trans, root->fs_info->extent_root, 0);
  2505. pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
  2506. return ret ? ret : pending_ret;
  2507. }
  2508. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  2509. struct btrfs_root *root,
  2510. u64 bytenr, u64 num_bytes, u64 parent,
  2511. u64 root_objectid, u64 ref_generation,
  2512. u64 owner_objectid, int pin)
  2513. {
  2514. int ret;
  2515. ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
  2516. root_objectid, ref_generation,
  2517. owner_objectid, pin);
  2518. return ret;
  2519. }
  2520. static u64 stripe_align(struct btrfs_root *root, u64 val)
  2521. {
  2522. u64 mask = ((u64)root->stripesize - 1);
  2523. u64 ret = (val + mask) & ~mask;
  2524. return ret;
  2525. }
  2526. /*
  2527. * walks the btree of allocated extents and find a hole of a given size.
  2528. * The key ins is changed to record the hole:
  2529. * ins->objectid == block start
  2530. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  2531. * ins->offset == number of blocks
  2532. * Any available blocks before search_start are skipped.
  2533. */
  2534. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  2535. struct btrfs_root *orig_root,
  2536. u64 num_bytes, u64 empty_size,
  2537. u64 search_start, u64 search_end,
  2538. u64 hint_byte, struct btrfs_key *ins,
  2539. u64 exclude_start, u64 exclude_nr,
  2540. int data)
  2541. {
  2542. int ret = 0;
  2543. struct btrfs_root *root = orig_root->fs_info->extent_root;
  2544. u64 total_needed = num_bytes;
  2545. u64 *last_ptr = NULL;
  2546. u64 last_wanted = 0;
  2547. struct btrfs_block_group_cache *block_group = NULL;
  2548. int chunk_alloc_done = 0;
  2549. int empty_cluster = 2 * 1024 * 1024;
  2550. int allowed_chunk_alloc = 0;
  2551. struct list_head *head = NULL, *cur = NULL;
  2552. int loop = 0;
  2553. int extra_loop = 0;
  2554. struct btrfs_space_info *space_info;
  2555. WARN_ON(num_bytes < root->sectorsize);
  2556. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  2557. ins->objectid = 0;
  2558. ins->offset = 0;
  2559. if (orig_root->ref_cows || empty_size)
  2560. allowed_chunk_alloc = 1;
  2561. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  2562. last_ptr = &root->fs_info->last_alloc;
  2563. if (!btrfs_test_opt(root, SSD))
  2564. empty_cluster = 64 * 1024;
  2565. }
  2566. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
  2567. last_ptr = &root->fs_info->last_data_alloc;
  2568. if (last_ptr) {
  2569. if (*last_ptr) {
  2570. hint_byte = *last_ptr;
  2571. last_wanted = *last_ptr;
  2572. } else
  2573. empty_size += empty_cluster;
  2574. } else {
  2575. empty_cluster = 0;
  2576. }
  2577. search_start = max(search_start, first_logical_byte(root, 0));
  2578. search_start = max(search_start, hint_byte);
  2579. if (last_wanted && search_start != last_wanted) {
  2580. last_wanted = 0;
  2581. empty_size += empty_cluster;
  2582. }
  2583. total_needed += empty_size;
  2584. block_group = btrfs_lookup_block_group(root->fs_info, search_start);
  2585. if (!block_group)
  2586. block_group = btrfs_lookup_first_block_group(root->fs_info,
  2587. search_start);
  2588. space_info = __find_space_info(root->fs_info, data);
  2589. down_read(&space_info->groups_sem);
  2590. while (1) {
  2591. struct btrfs_free_space *free_space;
  2592. /*
  2593. * the only way this happens if our hint points to a block
  2594. * group thats not of the proper type, while looping this
  2595. * should never happen
  2596. */
  2597. if (empty_size)
  2598. extra_loop = 1;
  2599. if (!block_group)
  2600. goto new_group_no_lock;
  2601. if (unlikely(!block_group->cached)) {
  2602. mutex_lock(&block_group->cache_mutex);
  2603. ret = cache_block_group(root, block_group);
  2604. mutex_unlock(&block_group->cache_mutex);
  2605. if (ret)
  2606. break;
  2607. }
  2608. mutex_lock(&block_group->alloc_mutex);
  2609. if (unlikely(!block_group_bits(block_group, data)))
  2610. goto new_group;
  2611. if (unlikely(block_group->ro))
  2612. goto new_group;
  2613. free_space = btrfs_find_free_space(block_group, search_start,
  2614. total_needed);
  2615. if (free_space) {
  2616. u64 start = block_group->key.objectid;
  2617. u64 end = block_group->key.objectid +
  2618. block_group->key.offset;
  2619. search_start = stripe_align(root, free_space->offset);
  2620. /* move on to the next group */
  2621. if (search_start + num_bytes >= search_end)
  2622. goto new_group;
  2623. /* move on to the next group */
  2624. if (search_start + num_bytes > end)
  2625. goto new_group;
  2626. if (last_wanted && search_start != last_wanted) {
  2627. total_needed += empty_cluster;
  2628. empty_size += empty_cluster;
  2629. last_wanted = 0;
  2630. /*
  2631. * if search_start is still in this block group
  2632. * then we just re-search this block group
  2633. */
  2634. if (search_start >= start &&
  2635. search_start < end) {
  2636. mutex_unlock(&block_group->alloc_mutex);
  2637. continue;
  2638. }
  2639. /* else we go to the next block group */
  2640. goto new_group;
  2641. }
  2642. if (exclude_nr > 0 &&
  2643. (search_start + num_bytes > exclude_start &&
  2644. search_start < exclude_start + exclude_nr)) {
  2645. search_start = exclude_start + exclude_nr;
  2646. /*
  2647. * if search_start is still in this block group
  2648. * then we just re-search this block group
  2649. */
  2650. if (search_start >= start &&
  2651. search_start < end) {
  2652. mutex_unlock(&block_group->alloc_mutex);
  2653. last_wanted = 0;
  2654. continue;
  2655. }
  2656. /* else we go to the next block group */
  2657. goto new_group;
  2658. }
  2659. ins->objectid = search_start;
  2660. ins->offset = num_bytes;
  2661. btrfs_remove_free_space_lock(block_group, search_start,
  2662. num_bytes);
  2663. /* we are all good, lets return */
  2664. mutex_unlock(&block_group->alloc_mutex);
  2665. break;
  2666. }
  2667. new_group:
  2668. mutex_unlock(&block_group->alloc_mutex);
  2669. put_block_group(block_group);
  2670. block_group = NULL;
  2671. new_group_no_lock:
  2672. /* don't try to compare new allocations against the
  2673. * last allocation any more
  2674. */
  2675. last_wanted = 0;
  2676. /*
  2677. * Here's how this works.
  2678. * loop == 0: we were searching a block group via a hint
  2679. * and didn't find anything, so we start at
  2680. * the head of the block groups and keep searching
  2681. * loop == 1: we're searching through all of the block groups
  2682. * if we hit the head again we have searched
  2683. * all of the block groups for this space and we
  2684. * need to try and allocate, if we cant error out.
  2685. * loop == 2: we allocated more space and are looping through
  2686. * all of the block groups again.
  2687. */
  2688. if (loop == 0) {
  2689. head = &space_info->block_groups;
  2690. cur = head->next;
  2691. loop++;
  2692. } else if (loop == 1 && cur == head) {
  2693. int keep_going;
  2694. /* at this point we give up on the empty_size
  2695. * allocations and just try to allocate the min
  2696. * space.
  2697. *
  2698. * The extra_loop field was set if an empty_size
  2699. * allocation was attempted above, and if this
  2700. * is try we need to try the loop again without
  2701. * the additional empty_size.
  2702. */
  2703. total_needed -= empty_size;
  2704. empty_size = 0;
  2705. keep_going = extra_loop;
  2706. loop++;
  2707. if (allowed_chunk_alloc && !chunk_alloc_done) {
  2708. up_read(&space_info->groups_sem);
  2709. ret = do_chunk_alloc(trans, root, num_bytes +
  2710. 2 * 1024 * 1024, data, 1);
  2711. down_read(&space_info->groups_sem);
  2712. if (ret < 0)
  2713. goto loop_check;
  2714. head = &space_info->block_groups;
  2715. /*
  2716. * we've allocated a new chunk, keep
  2717. * trying
  2718. */
  2719. keep_going = 1;
  2720. chunk_alloc_done = 1;
  2721. } else if (!allowed_chunk_alloc) {
  2722. space_info->force_alloc = 1;
  2723. }
  2724. loop_check:
  2725. if (keep_going) {
  2726. cur = head->next;
  2727. extra_loop = 0;
  2728. } else {
  2729. break;
  2730. }
  2731. } else if (cur == head) {
  2732. break;
  2733. }
  2734. block_group = list_entry(cur, struct btrfs_block_group_cache,
  2735. list);
  2736. atomic_inc(&block_group->count);
  2737. search_start = block_group->key.objectid;
  2738. cur = cur->next;
  2739. }
  2740. /* we found what we needed */
  2741. if (ins->objectid) {
  2742. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  2743. trans->block_group = block_group->key.objectid;
  2744. if (last_ptr)
  2745. *last_ptr = ins->objectid + ins->offset;
  2746. ret = 0;
  2747. } else if (!ret) {
  2748. printk(KERN_ERR "btrfs searching for %llu bytes, "
  2749. "num_bytes %llu, loop %d, allowed_alloc %d\n",
  2750. (unsigned long long)total_needed,
  2751. (unsigned long long)num_bytes,
  2752. loop, allowed_chunk_alloc);
  2753. ret = -ENOSPC;
  2754. }
  2755. if (block_group)
  2756. put_block_group(block_group);
  2757. up_read(&space_info->groups_sem);
  2758. return ret;
  2759. }
  2760. static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
  2761. {
  2762. struct btrfs_block_group_cache *cache;
  2763. printk(KERN_INFO "space_info has %llu free, is %sfull\n",
  2764. (unsigned long long)(info->total_bytes - info->bytes_used -
  2765. info->bytes_pinned - info->bytes_reserved),
  2766. (info->full) ? "" : "not ");
  2767. down_read(&info->groups_sem);
  2768. list_for_each_entry(cache, &info->block_groups, list) {
  2769. spin_lock(&cache->lock);
  2770. printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
  2771. "%llu pinned %llu reserved\n",
  2772. (unsigned long long)cache->key.objectid,
  2773. (unsigned long long)cache->key.offset,
  2774. (unsigned long long)btrfs_block_group_used(&cache->item),
  2775. (unsigned long long)cache->pinned,
  2776. (unsigned long long)cache->reserved);
  2777. btrfs_dump_free_space(cache, bytes);
  2778. spin_unlock(&cache->lock);
  2779. }
  2780. up_read(&info->groups_sem);
  2781. }
  2782. static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2783. struct btrfs_root *root,
  2784. u64 num_bytes, u64 min_alloc_size,
  2785. u64 empty_size, u64 hint_byte,
  2786. u64 search_end, struct btrfs_key *ins,
  2787. u64 data)
  2788. {
  2789. int ret;
  2790. u64 search_start = 0;
  2791. u64 alloc_profile;
  2792. struct btrfs_fs_info *info = root->fs_info;
  2793. if (data) {
  2794. alloc_profile = info->avail_data_alloc_bits &
  2795. info->data_alloc_profile;
  2796. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  2797. } else if (root == root->fs_info->chunk_root) {
  2798. alloc_profile = info->avail_system_alloc_bits &
  2799. info->system_alloc_profile;
  2800. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  2801. } else {
  2802. alloc_profile = info->avail_metadata_alloc_bits &
  2803. info->metadata_alloc_profile;
  2804. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  2805. }
  2806. again:
  2807. data = btrfs_reduce_alloc_profile(root, data);
  2808. /*
  2809. * the only place that sets empty_size is btrfs_realloc_node, which
  2810. * is not called recursively on allocations
  2811. */
  2812. if (empty_size || root->ref_cows) {
  2813. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  2814. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2815. 2 * 1024 * 1024,
  2816. BTRFS_BLOCK_GROUP_METADATA |
  2817. (info->metadata_alloc_profile &
  2818. info->avail_metadata_alloc_bits), 0);
  2819. }
  2820. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2821. num_bytes + 2 * 1024 * 1024, data, 0);
  2822. }
  2823. WARN_ON(num_bytes < root->sectorsize);
  2824. ret = find_free_extent(trans, root, num_bytes, empty_size,
  2825. search_start, search_end, hint_byte, ins,
  2826. trans->alloc_exclude_start,
  2827. trans->alloc_exclude_nr, data);
  2828. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  2829. num_bytes = num_bytes >> 1;
  2830. num_bytes = num_bytes & ~(root->sectorsize - 1);
  2831. num_bytes = max(num_bytes, min_alloc_size);
  2832. do_chunk_alloc(trans, root->fs_info->extent_root,
  2833. num_bytes, data, 1);
  2834. goto again;
  2835. }
  2836. if (ret) {
  2837. struct btrfs_space_info *sinfo;
  2838. sinfo = __find_space_info(root->fs_info, data);
  2839. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  2840. "wanted %llu\n", (unsigned long long)data,
  2841. (unsigned long long)num_bytes);
  2842. dump_space_info(sinfo, num_bytes);
  2843. BUG();
  2844. }
  2845. return ret;
  2846. }
  2847. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  2848. {
  2849. struct btrfs_block_group_cache *cache;
  2850. int ret = 0;
  2851. cache = btrfs_lookup_block_group(root->fs_info, start);
  2852. if (!cache) {
  2853. printk(KERN_ERR "Unable to find block group for %llu\n",
  2854. (unsigned long long)start);
  2855. return -ENOSPC;
  2856. }
  2857. ret = btrfs_discard_extent(root, start, len);
  2858. btrfs_add_free_space(cache, start, len);
  2859. put_block_group(cache);
  2860. update_reserved_extents(root, start, len, 0);
  2861. return ret;
  2862. }
  2863. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2864. struct btrfs_root *root,
  2865. u64 num_bytes, u64 min_alloc_size,
  2866. u64 empty_size, u64 hint_byte,
  2867. u64 search_end, struct btrfs_key *ins,
  2868. u64 data)
  2869. {
  2870. int ret;
  2871. ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
  2872. empty_size, hint_byte, search_end, ins,
  2873. data);
  2874. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2875. return ret;
  2876. }
  2877. static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2878. struct btrfs_root *root, u64 parent,
  2879. u64 root_objectid, u64 ref_generation,
  2880. u64 owner, struct btrfs_key *ins)
  2881. {
  2882. int ret;
  2883. int pending_ret;
  2884. u64 super_used;
  2885. u64 root_used;
  2886. u64 num_bytes = ins->offset;
  2887. u32 sizes[2];
  2888. struct btrfs_fs_info *info = root->fs_info;
  2889. struct btrfs_root *extent_root = info->extent_root;
  2890. struct btrfs_extent_item *extent_item;
  2891. struct btrfs_extent_ref *ref;
  2892. struct btrfs_path *path;
  2893. struct btrfs_key keys[2];
  2894. if (parent == 0)
  2895. parent = ins->objectid;
  2896. /* block accounting for super block */
  2897. spin_lock(&info->delalloc_lock);
  2898. super_used = btrfs_super_bytes_used(&info->super_copy);
  2899. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  2900. /* block accounting for root item */
  2901. root_used = btrfs_root_used(&root->root_item);
  2902. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  2903. spin_unlock(&info->delalloc_lock);
  2904. if (root == extent_root) {
  2905. struct pending_extent_op *extent_op;
  2906. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2907. BUG_ON(!extent_op);
  2908. extent_op->type = PENDING_EXTENT_INSERT;
  2909. extent_op->bytenr = ins->objectid;
  2910. extent_op->num_bytes = ins->offset;
  2911. extent_op->parent = parent;
  2912. extent_op->orig_parent = 0;
  2913. extent_op->generation = ref_generation;
  2914. extent_op->orig_generation = 0;
  2915. extent_op->level = (int)owner;
  2916. INIT_LIST_HEAD(&extent_op->list);
  2917. extent_op->del = 0;
  2918. mutex_lock(&root->fs_info->extent_ins_mutex);
  2919. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  2920. ins->objectid + ins->offset - 1,
  2921. EXTENT_WRITEBACK, GFP_NOFS);
  2922. set_state_private(&root->fs_info->extent_ins,
  2923. ins->objectid, (unsigned long)extent_op);
  2924. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2925. goto update_block;
  2926. }
  2927. memcpy(&keys[0], ins, sizeof(*ins));
  2928. keys[1].objectid = ins->objectid;
  2929. keys[1].type = BTRFS_EXTENT_REF_KEY;
  2930. keys[1].offset = parent;
  2931. sizes[0] = sizeof(*extent_item);
  2932. sizes[1] = sizeof(*ref);
  2933. path = btrfs_alloc_path();
  2934. BUG_ON(!path);
  2935. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  2936. sizes, 2);
  2937. BUG_ON(ret);
  2938. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2939. struct btrfs_extent_item);
  2940. btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
  2941. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  2942. struct btrfs_extent_ref);
  2943. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  2944. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  2945. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  2946. btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
  2947. btrfs_mark_buffer_dirty(path->nodes[0]);
  2948. trans->alloc_exclude_start = 0;
  2949. trans->alloc_exclude_nr = 0;
  2950. btrfs_free_path(path);
  2951. finish_current_insert(trans, extent_root, 0);
  2952. pending_ret = del_pending_extents(trans, extent_root, 0);
  2953. if (ret)
  2954. goto out;
  2955. if (pending_ret) {
  2956. ret = pending_ret;
  2957. goto out;
  2958. }
  2959. update_block:
  2960. ret = update_block_group(trans, root, ins->objectid,
  2961. ins->offset, 1, 0);
  2962. if (ret) {
  2963. printk(KERN_ERR "btrfs update block group failed for %llu "
  2964. "%llu\n", (unsigned long long)ins->objectid,
  2965. (unsigned long long)ins->offset);
  2966. BUG();
  2967. }
  2968. out:
  2969. return ret;
  2970. }
  2971. int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2972. struct btrfs_root *root, u64 parent,
  2973. u64 root_objectid, u64 ref_generation,
  2974. u64 owner, struct btrfs_key *ins)
  2975. {
  2976. int ret;
  2977. if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
  2978. return 0;
  2979. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  2980. ref_generation, owner, ins);
  2981. update_reserved_extents(root, ins->objectid, ins->offset, 0);
  2982. return ret;
  2983. }
  2984. /*
  2985. * this is used by the tree logging recovery code. It records that
  2986. * an extent has been allocated and makes sure to clear the free
  2987. * space cache bits as well
  2988. */
  2989. int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
  2990. struct btrfs_root *root, u64 parent,
  2991. u64 root_objectid, u64 ref_generation,
  2992. u64 owner, struct btrfs_key *ins)
  2993. {
  2994. int ret;
  2995. struct btrfs_block_group_cache *block_group;
  2996. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  2997. mutex_lock(&block_group->cache_mutex);
  2998. cache_block_group(root, block_group);
  2999. mutex_unlock(&block_group->cache_mutex);
  3000. ret = btrfs_remove_free_space(block_group, ins->objectid,
  3001. ins->offset);
  3002. BUG_ON(ret);
  3003. put_block_group(block_group);
  3004. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  3005. ref_generation, owner, ins);
  3006. return ret;
  3007. }
  3008. /*
  3009. * finds a free extent and does all the dirty work required for allocation
  3010. * returns the key for the extent through ins, and a tree buffer for
  3011. * the first block of the extent through buf.
  3012. *
  3013. * returns 0 if everything worked, non-zero otherwise.
  3014. */
  3015. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  3016. struct btrfs_root *root,
  3017. u64 num_bytes, u64 parent, u64 min_alloc_size,
  3018. u64 root_objectid, u64 ref_generation,
  3019. u64 owner_objectid, u64 empty_size, u64 hint_byte,
  3020. u64 search_end, struct btrfs_key *ins, u64 data)
  3021. {
  3022. int ret;
  3023. ret = __btrfs_reserve_extent(trans, root, num_bytes,
  3024. min_alloc_size, empty_size, hint_byte,
  3025. search_end, ins, data);
  3026. BUG_ON(ret);
  3027. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  3028. ret = __btrfs_alloc_reserved_extent(trans, root, parent,
  3029. root_objectid, ref_generation,
  3030. owner_objectid, ins);
  3031. BUG_ON(ret);
  3032. } else {
  3033. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  3034. }
  3035. return ret;
  3036. }
  3037. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  3038. struct btrfs_root *root,
  3039. u64 bytenr, u32 blocksize)
  3040. {
  3041. struct extent_buffer *buf;
  3042. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  3043. if (!buf)
  3044. return ERR_PTR(-ENOMEM);
  3045. btrfs_set_header_generation(buf, trans->transid);
  3046. btrfs_tree_lock(buf);
  3047. clean_tree_block(trans, root, buf);
  3048. btrfs_set_lock_blocking(buf);
  3049. btrfs_set_buffer_uptodate(buf);
  3050. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  3051. set_extent_dirty(&root->dirty_log_pages, buf->start,
  3052. buf->start + buf->len - 1, GFP_NOFS);
  3053. } else {
  3054. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  3055. buf->start + buf->len - 1, GFP_NOFS);
  3056. }
  3057. trans->blocks_used++;
  3058. /* this returns a buffer locked for blocking */
  3059. return buf;
  3060. }
  3061. /*
  3062. * helper function to allocate a block for a given tree
  3063. * returns the tree buffer or NULL.
  3064. */
  3065. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  3066. struct btrfs_root *root,
  3067. u32 blocksize, u64 parent,
  3068. u64 root_objectid,
  3069. u64 ref_generation,
  3070. int level,
  3071. u64 hint,
  3072. u64 empty_size)
  3073. {
  3074. struct btrfs_key ins;
  3075. int ret;
  3076. struct extent_buffer *buf;
  3077. ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
  3078. root_objectid, ref_generation, level,
  3079. empty_size, hint, (u64)-1, &ins, 0);
  3080. if (ret) {
  3081. BUG_ON(ret > 0);
  3082. return ERR_PTR(ret);
  3083. }
  3084. buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
  3085. return buf;
  3086. }
  3087. int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
  3088. struct btrfs_root *root, struct extent_buffer *leaf)
  3089. {
  3090. u64 leaf_owner;
  3091. u64 leaf_generation;
  3092. struct refsort *sorted;
  3093. struct btrfs_key key;
  3094. struct btrfs_file_extent_item *fi;
  3095. int i;
  3096. int nritems;
  3097. int ret;
  3098. int refi = 0;
  3099. int slot;
  3100. BUG_ON(!btrfs_is_leaf(leaf));
  3101. nritems = btrfs_header_nritems(leaf);
  3102. leaf_owner = btrfs_header_owner(leaf);
  3103. leaf_generation = btrfs_header_generation(leaf);
  3104. sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
  3105. /* we do this loop twice. The first time we build a list
  3106. * of the extents we have a reference on, then we sort the list
  3107. * by bytenr. The second time around we actually do the
  3108. * extent freeing.
  3109. */
  3110. for (i = 0; i < nritems; i++) {
  3111. u64 disk_bytenr;
  3112. cond_resched();
  3113. btrfs_item_key_to_cpu(leaf, &key, i);
  3114. /* only extents have references, skip everything else */
  3115. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  3116. continue;
  3117. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  3118. /* inline extents live in the btree, they don't have refs */
  3119. if (btrfs_file_extent_type(leaf, fi) ==
  3120. BTRFS_FILE_EXTENT_INLINE)
  3121. continue;
  3122. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  3123. /* holes don't have refs */
  3124. if (disk_bytenr == 0)
  3125. continue;
  3126. sorted[refi].bytenr = disk_bytenr;
  3127. sorted[refi].slot = i;
  3128. refi++;
  3129. }
  3130. if (refi == 0)
  3131. goto out;
  3132. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  3133. for (i = 0; i < refi; i++) {
  3134. u64 disk_bytenr;
  3135. disk_bytenr = sorted[i].bytenr;
  3136. slot = sorted[i].slot;
  3137. cond_resched();
  3138. btrfs_item_key_to_cpu(leaf, &key, slot);
  3139. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  3140. continue;
  3141. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  3142. ret = __btrfs_free_extent(trans, root, disk_bytenr,
  3143. btrfs_file_extent_disk_num_bytes(leaf, fi),
  3144. leaf->start, leaf_owner, leaf_generation,
  3145. key.objectid, 0);
  3146. BUG_ON(ret);
  3147. atomic_inc(&root->fs_info->throttle_gen);
  3148. wake_up(&root->fs_info->transaction_throttle);
  3149. cond_resched();
  3150. }
  3151. out:
  3152. kfree(sorted);
  3153. return 0;
  3154. }
  3155. static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
  3156. struct btrfs_root *root,
  3157. struct btrfs_leaf_ref *ref)
  3158. {
  3159. int i;
  3160. int ret;
  3161. struct btrfs_extent_info *info;
  3162. struct refsort *sorted;
  3163. if (ref->nritems == 0)
  3164. return 0;
  3165. sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
  3166. for (i = 0; i < ref->nritems; i++) {
  3167. sorted[i].bytenr = ref->extents[i].bytenr;
  3168. sorted[i].slot = i;
  3169. }
  3170. sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
  3171. /*
  3172. * the items in the ref were sorted when the ref was inserted
  3173. * into the ref cache, so this is already in order
  3174. */
  3175. for (i = 0; i < ref->nritems; i++) {
  3176. info = ref->extents + sorted[i].slot;
  3177. ret = __btrfs_free_extent(trans, root, info->bytenr,
  3178. info->num_bytes, ref->bytenr,
  3179. ref->owner, ref->generation,
  3180. info->objectid, 0);
  3181. atomic_inc(&root->fs_info->throttle_gen);
  3182. wake_up(&root->fs_info->transaction_throttle);
  3183. cond_resched();
  3184. BUG_ON(ret);
  3185. info++;
  3186. }
  3187. kfree(sorted);
  3188. return 0;
  3189. }
  3190. static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start,
  3191. u64 len, u32 *refs)
  3192. {
  3193. int ret;
  3194. ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
  3195. BUG_ON(ret);
  3196. #if 0 /* some debugging code in case we see problems here */
  3197. /* if the refs count is one, it won't get increased again. But
  3198. * if the ref count is > 1, someone may be decreasing it at
  3199. * the same time we are.
  3200. */
  3201. if (*refs != 1) {
  3202. struct extent_buffer *eb = NULL;
  3203. eb = btrfs_find_create_tree_block(root, start, len);
  3204. if (eb)
  3205. btrfs_tree_lock(eb);
  3206. mutex_lock(&root->fs_info->alloc_mutex);
  3207. ret = lookup_extent_ref(NULL, root, start, len, refs);
  3208. BUG_ON(ret);
  3209. mutex_unlock(&root->fs_info->alloc_mutex);
  3210. if (eb) {
  3211. btrfs_tree_unlock(eb);
  3212. free_extent_buffer(eb);
  3213. }
  3214. if (*refs == 1) {
  3215. printk(KERN_ERR "btrfs block %llu went down to one "
  3216. "during drop_snap\n", (unsigned long long)start);
  3217. }
  3218. }
  3219. #endif
  3220. cond_resched();
  3221. return ret;
  3222. }
  3223. /*
  3224. * this is used while deleting old snapshots, and it drops the refs
  3225. * on a whole subtree starting from a level 1 node.
  3226. *
  3227. * The idea is to sort all the leaf pointers, and then drop the
  3228. * ref on all the leaves in order. Most of the time the leaves
  3229. * will have ref cache entries, so no leaf IOs will be required to
  3230. * find the extents they have references on.
  3231. *
  3232. * For each leaf, any references it has are also dropped in order
  3233. *
  3234. * This ends up dropping the references in something close to optimal
  3235. * order for reading and modifying the extent allocation tree.
  3236. */
  3237. static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
  3238. struct btrfs_root *root,
  3239. struct btrfs_path *path)
  3240. {
  3241. u64 bytenr;
  3242. u64 root_owner;
  3243. u64 root_gen;
  3244. struct extent_buffer *eb = path->nodes[1];
  3245. struct extent_buffer *leaf;
  3246. struct btrfs_leaf_ref *ref;
  3247. struct refsort *sorted = NULL;
  3248. int nritems = btrfs_header_nritems(eb);
  3249. int ret;
  3250. int i;
  3251. int refi = 0;
  3252. int slot = path->slots[1];
  3253. u32 blocksize = btrfs_level_size(root, 0);
  3254. u32 refs;
  3255. if (nritems == 0)
  3256. goto out;
  3257. root_owner = btrfs_header_owner(eb);
  3258. root_gen = btrfs_header_generation(eb);
  3259. sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
  3260. /*
  3261. * step one, sort all the leaf pointers so we don't scribble
  3262. * randomly into the extent allocation tree
  3263. */
  3264. for (i = slot; i < nritems; i++) {
  3265. sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
  3266. sorted[refi].slot = i;
  3267. refi++;
  3268. }
  3269. /*
  3270. * nritems won't be zero, but if we're picking up drop_snapshot
  3271. * after a crash, slot might be > 0, so double check things
  3272. * just in case.
  3273. */
  3274. if (refi == 0)
  3275. goto out;
  3276. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  3277. /*
  3278. * the first loop frees everything the leaves point to
  3279. */
  3280. for (i = 0; i < refi; i++) {
  3281. u64 ptr_gen;
  3282. bytenr = sorted[i].bytenr;
  3283. /*
  3284. * check the reference count on this leaf. If it is > 1
  3285. * we just decrement it below and don't update any
  3286. * of the refs the leaf points to.
  3287. */
  3288. ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
  3289. BUG_ON(ret);
  3290. if (refs != 1)
  3291. continue;
  3292. ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
  3293. /*
  3294. * the leaf only had one reference, which means the
  3295. * only thing pointing to this leaf is the snapshot
  3296. * we're deleting. It isn't possible for the reference
  3297. * count to increase again later
  3298. *
  3299. * The reference cache is checked for the leaf,
  3300. * and if found we'll be able to drop any refs held by
  3301. * the leaf without needing to read it in.
  3302. */
  3303. ref = btrfs_lookup_leaf_ref(root, bytenr);
  3304. if (ref && ref->generation != ptr_gen) {
  3305. btrfs_free_leaf_ref(root, ref);
  3306. ref = NULL;
  3307. }
  3308. if (ref) {
  3309. ret = cache_drop_leaf_ref(trans, root, ref);
  3310. BUG_ON(ret);
  3311. btrfs_remove_leaf_ref(root, ref);
  3312. btrfs_free_leaf_ref(root, ref);
  3313. } else {
  3314. /*
  3315. * the leaf wasn't in the reference cache, so
  3316. * we have to read it.
  3317. */
  3318. leaf = read_tree_block(root, bytenr, blocksize,
  3319. ptr_gen);
  3320. ret = btrfs_drop_leaf_ref(trans, root, leaf);
  3321. BUG_ON(ret);
  3322. free_extent_buffer(leaf);
  3323. }
  3324. atomic_inc(&root->fs_info->throttle_gen);
  3325. wake_up(&root->fs_info->transaction_throttle);
  3326. cond_resched();
  3327. }
  3328. /*
  3329. * run through the loop again to free the refs on the leaves.
  3330. * This is faster than doing it in the loop above because
  3331. * the leaves are likely to be clustered together. We end up
  3332. * working in nice chunks on the extent allocation tree.
  3333. */
  3334. for (i = 0; i < refi; i++) {
  3335. bytenr = sorted[i].bytenr;
  3336. ret = __btrfs_free_extent(trans, root, bytenr,
  3337. blocksize, eb->start,
  3338. root_owner, root_gen, 0, 1);
  3339. BUG_ON(ret);
  3340. atomic_inc(&root->fs_info->throttle_gen);
  3341. wake_up(&root->fs_info->transaction_throttle);
  3342. cond_resched();
  3343. }
  3344. out:
  3345. kfree(sorted);
  3346. /*
  3347. * update the path to show we've processed the entire level 1
  3348. * node. This will get saved into the root's drop_snapshot_progress
  3349. * field so these drops are not repeated again if this transaction
  3350. * commits.
  3351. */
  3352. path->slots[1] = nritems;
  3353. return 0;
  3354. }
  3355. /*
  3356. * helper function for drop_snapshot, this walks down the tree dropping ref
  3357. * counts as it goes.
  3358. */
  3359. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  3360. struct btrfs_root *root,
  3361. struct btrfs_path *path, int *level)
  3362. {
  3363. u64 root_owner;
  3364. u64 root_gen;
  3365. u64 bytenr;
  3366. u64 ptr_gen;
  3367. struct extent_buffer *next;
  3368. struct extent_buffer *cur;
  3369. struct extent_buffer *parent;
  3370. u32 blocksize;
  3371. int ret;
  3372. u32 refs;
  3373. WARN_ON(*level < 0);
  3374. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3375. ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
  3376. path->nodes[*level]->len, &refs);
  3377. BUG_ON(ret);
  3378. if (refs > 1)
  3379. goto out;
  3380. /*
  3381. * walk down to the last node level and free all the leaves
  3382. */
  3383. while (*level >= 0) {
  3384. WARN_ON(*level < 0);
  3385. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3386. cur = path->nodes[*level];
  3387. if (btrfs_header_level(cur) != *level)
  3388. WARN_ON(1);
  3389. if (path->slots[*level] >=
  3390. btrfs_header_nritems(cur))
  3391. break;
  3392. /* the new code goes down to level 1 and does all the
  3393. * leaves pointed to that node in bulk. So, this check
  3394. * for level 0 will always be false.
  3395. *
  3396. * But, the disk format allows the drop_snapshot_progress
  3397. * field in the root to leave things in a state where
  3398. * a leaf will need cleaning up here. If someone crashes
  3399. * with the old code and then boots with the new code,
  3400. * we might find a leaf here.
  3401. */
  3402. if (*level == 0) {
  3403. ret = btrfs_drop_leaf_ref(trans, root, cur);
  3404. BUG_ON(ret);
  3405. break;
  3406. }
  3407. /*
  3408. * once we get to level one, process the whole node
  3409. * at once, including everything below it.
  3410. */
  3411. if (*level == 1) {
  3412. ret = drop_level_one_refs(trans, root, path);
  3413. BUG_ON(ret);
  3414. break;
  3415. }
  3416. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  3417. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  3418. blocksize = btrfs_level_size(root, *level - 1);
  3419. ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
  3420. BUG_ON(ret);
  3421. /*
  3422. * if there is more than one reference, we don't need
  3423. * to read that node to drop any references it has. We
  3424. * just drop the ref we hold on that node and move on to the
  3425. * next slot in this level.
  3426. */
  3427. if (refs != 1) {
  3428. parent = path->nodes[*level];
  3429. root_owner = btrfs_header_owner(parent);
  3430. root_gen = btrfs_header_generation(parent);
  3431. path->slots[*level]++;
  3432. ret = __btrfs_free_extent(trans, root, bytenr,
  3433. blocksize, parent->start,
  3434. root_owner, root_gen,
  3435. *level - 1, 1);
  3436. BUG_ON(ret);
  3437. atomic_inc(&root->fs_info->throttle_gen);
  3438. wake_up(&root->fs_info->transaction_throttle);
  3439. cond_resched();
  3440. continue;
  3441. }
  3442. /*
  3443. * we need to keep freeing things in the next level down.
  3444. * read the block and loop around to process it
  3445. */
  3446. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  3447. WARN_ON(*level <= 0);
  3448. if (path->nodes[*level-1])
  3449. free_extent_buffer(path->nodes[*level-1]);
  3450. path->nodes[*level-1] = next;
  3451. *level = btrfs_header_level(next);
  3452. path->slots[*level] = 0;
  3453. cond_resched();
  3454. }
  3455. out:
  3456. WARN_ON(*level < 0);
  3457. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3458. if (path->nodes[*level] == root->node) {
  3459. parent = path->nodes[*level];
  3460. bytenr = path->nodes[*level]->start;
  3461. } else {
  3462. parent = path->nodes[*level + 1];
  3463. bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
  3464. }
  3465. blocksize = btrfs_level_size(root, *level);
  3466. root_owner = btrfs_header_owner(parent);
  3467. root_gen = btrfs_header_generation(parent);
  3468. /*
  3469. * cleanup and free the reference on the last node
  3470. * we processed
  3471. */
  3472. ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
  3473. parent->start, root_owner, root_gen,
  3474. *level, 1);
  3475. free_extent_buffer(path->nodes[*level]);
  3476. path->nodes[*level] = NULL;
  3477. *level += 1;
  3478. BUG_ON(ret);
  3479. cond_resched();
  3480. return 0;
  3481. }
  3482. /*
  3483. * helper function for drop_subtree, this function is similar to
  3484. * walk_down_tree. The main difference is that it checks reference
  3485. * counts while tree blocks are locked.
  3486. */
  3487. static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
  3488. struct btrfs_root *root,
  3489. struct btrfs_path *path, int *level)
  3490. {
  3491. struct extent_buffer *next;
  3492. struct extent_buffer *cur;
  3493. struct extent_buffer *parent;
  3494. u64 bytenr;
  3495. u64 ptr_gen;
  3496. u32 blocksize;
  3497. u32 refs;
  3498. int ret;
  3499. cur = path->nodes[*level];
  3500. ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
  3501. &refs);
  3502. BUG_ON(ret);
  3503. if (refs > 1)
  3504. goto out;
  3505. while (*level >= 0) {
  3506. cur = path->nodes[*level];
  3507. if (*level == 0) {
  3508. ret = btrfs_drop_leaf_ref(trans, root, cur);
  3509. BUG_ON(ret);
  3510. clean_tree_block(trans, root, cur);
  3511. break;
  3512. }
  3513. if (path->slots[*level] >= btrfs_header_nritems(cur)) {
  3514. clean_tree_block(trans, root, cur);
  3515. break;
  3516. }
  3517. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  3518. blocksize = btrfs_level_size(root, *level - 1);
  3519. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  3520. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  3521. btrfs_tree_lock(next);
  3522. btrfs_set_lock_blocking(next);
  3523. ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
  3524. &refs);
  3525. BUG_ON(ret);
  3526. if (refs > 1) {
  3527. parent = path->nodes[*level];
  3528. ret = btrfs_free_extent(trans, root, bytenr,
  3529. blocksize, parent->start,
  3530. btrfs_header_owner(parent),
  3531. btrfs_header_generation(parent),
  3532. *level - 1, 1);
  3533. BUG_ON(ret);
  3534. path->slots[*level]++;
  3535. btrfs_tree_unlock(next);
  3536. free_extent_buffer(next);
  3537. continue;
  3538. }
  3539. *level = btrfs_header_level(next);
  3540. path->nodes[*level] = next;
  3541. path->slots[*level] = 0;
  3542. path->locks[*level] = 1;
  3543. cond_resched();
  3544. }
  3545. out:
  3546. parent = path->nodes[*level + 1];
  3547. bytenr = path->nodes[*level]->start;
  3548. blocksize = path->nodes[*level]->len;
  3549. ret = btrfs_free_extent(trans, root, bytenr, blocksize,
  3550. parent->start, btrfs_header_owner(parent),
  3551. btrfs_header_generation(parent), *level, 1);
  3552. BUG_ON(ret);
  3553. if (path->locks[*level]) {
  3554. btrfs_tree_unlock(path->nodes[*level]);
  3555. path->locks[*level] = 0;
  3556. }
  3557. free_extent_buffer(path->nodes[*level]);
  3558. path->nodes[*level] = NULL;
  3559. *level += 1;
  3560. cond_resched();
  3561. return 0;
  3562. }
  3563. /*
  3564. * helper for dropping snapshots. This walks back up the tree in the path
  3565. * to find the first node higher up where we haven't yet gone through
  3566. * all the slots
  3567. */
  3568. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  3569. struct btrfs_root *root,
  3570. struct btrfs_path *path,
  3571. int *level, int max_level)
  3572. {
  3573. u64 root_owner;
  3574. u64 root_gen;
  3575. struct btrfs_root_item *root_item = &root->root_item;
  3576. int i;
  3577. int slot;
  3578. int ret;
  3579. for (i = *level; i < max_level && path->nodes[i]; i++) {
  3580. slot = path->slots[i];
  3581. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  3582. struct extent_buffer *node;
  3583. struct btrfs_disk_key disk_key;
  3584. /*
  3585. * there is more work to do in this level.
  3586. * Update the drop_progress marker to reflect
  3587. * the work we've done so far, and then bump
  3588. * the slot number
  3589. */
  3590. node = path->nodes[i];
  3591. path->slots[i]++;
  3592. *level = i;
  3593. WARN_ON(*level == 0);
  3594. btrfs_node_key(node, &disk_key, path->slots[i]);
  3595. memcpy(&root_item->drop_progress,
  3596. &disk_key, sizeof(disk_key));
  3597. root_item->drop_level = i;
  3598. return 0;
  3599. } else {
  3600. struct extent_buffer *parent;
  3601. /*
  3602. * this whole node is done, free our reference
  3603. * on it and go up one level
  3604. */
  3605. if (path->nodes[*level] == root->node)
  3606. parent = path->nodes[*level];
  3607. else
  3608. parent = path->nodes[*level + 1];
  3609. root_owner = btrfs_header_owner(parent);
  3610. root_gen = btrfs_header_generation(parent);
  3611. clean_tree_block(trans, root, path->nodes[*level]);
  3612. ret = btrfs_free_extent(trans, root,
  3613. path->nodes[*level]->start,
  3614. path->nodes[*level]->len,
  3615. parent->start, root_owner,
  3616. root_gen, *level, 1);
  3617. BUG_ON(ret);
  3618. if (path->locks[*level]) {
  3619. btrfs_tree_unlock(path->nodes[*level]);
  3620. path->locks[*level] = 0;
  3621. }
  3622. free_extent_buffer(path->nodes[*level]);
  3623. path->nodes[*level] = NULL;
  3624. *level = i + 1;
  3625. }
  3626. }
  3627. return 1;
  3628. }
  3629. /*
  3630. * drop the reference count on the tree rooted at 'snap'. This traverses
  3631. * the tree freeing any blocks that have a ref count of zero after being
  3632. * decremented.
  3633. */
  3634. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  3635. *root)
  3636. {
  3637. int ret = 0;
  3638. int wret;
  3639. int level;
  3640. struct btrfs_path *path;
  3641. int i;
  3642. int orig_level;
  3643. struct btrfs_root_item *root_item = &root->root_item;
  3644. WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
  3645. path = btrfs_alloc_path();
  3646. BUG_ON(!path);
  3647. level = btrfs_header_level(root->node);
  3648. orig_level = level;
  3649. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  3650. path->nodes[level] = root->node;
  3651. extent_buffer_get(root->node);
  3652. path->slots[level] = 0;
  3653. } else {
  3654. struct btrfs_key key;
  3655. struct btrfs_disk_key found_key;
  3656. struct extent_buffer *node;
  3657. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  3658. level = root_item->drop_level;
  3659. path->lowest_level = level;
  3660. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3661. if (wret < 0) {
  3662. ret = wret;
  3663. goto out;
  3664. }
  3665. node = path->nodes[level];
  3666. btrfs_node_key(node, &found_key, path->slots[level]);
  3667. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  3668. sizeof(found_key)));
  3669. /*
  3670. * unlock our path, this is safe because only this
  3671. * function is allowed to delete this snapshot
  3672. */
  3673. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  3674. if (path->nodes[i] && path->locks[i]) {
  3675. path->locks[i] = 0;
  3676. btrfs_tree_unlock(path->nodes[i]);
  3677. }
  3678. }
  3679. }
  3680. while (1) {
  3681. wret = walk_down_tree(trans, root, path, &level);
  3682. if (wret > 0)
  3683. break;
  3684. if (wret < 0)
  3685. ret = wret;
  3686. wret = walk_up_tree(trans, root, path, &level,
  3687. BTRFS_MAX_LEVEL);
  3688. if (wret > 0)
  3689. break;
  3690. if (wret < 0)
  3691. ret = wret;
  3692. if (trans->transaction->in_commit) {
  3693. ret = -EAGAIN;
  3694. break;
  3695. }
  3696. atomic_inc(&root->fs_info->throttle_gen);
  3697. wake_up(&root->fs_info->transaction_throttle);
  3698. }
  3699. for (i = 0; i <= orig_level; i++) {
  3700. if (path->nodes[i]) {
  3701. free_extent_buffer(path->nodes[i]);
  3702. path->nodes[i] = NULL;
  3703. }
  3704. }
  3705. out:
  3706. btrfs_free_path(path);
  3707. return ret;
  3708. }
  3709. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  3710. struct btrfs_root *root,
  3711. struct extent_buffer *node,
  3712. struct extent_buffer *parent)
  3713. {
  3714. struct btrfs_path *path;
  3715. int level;
  3716. int parent_level;
  3717. int ret = 0;
  3718. int wret;
  3719. path = btrfs_alloc_path();
  3720. BUG_ON(!path);
  3721. BUG_ON(!btrfs_tree_locked(parent));
  3722. parent_level = btrfs_header_level(parent);
  3723. extent_buffer_get(parent);
  3724. path->nodes[parent_level] = parent;
  3725. path->slots[parent_level] = btrfs_header_nritems(parent);
  3726. BUG_ON(!btrfs_tree_locked(node));
  3727. level = btrfs_header_level(node);
  3728. extent_buffer_get(node);
  3729. path->nodes[level] = node;
  3730. path->slots[level] = 0;
  3731. while (1) {
  3732. wret = walk_down_subtree(trans, root, path, &level);
  3733. if (wret < 0)
  3734. ret = wret;
  3735. if (wret != 0)
  3736. break;
  3737. wret = walk_up_tree(trans, root, path, &level, parent_level);
  3738. if (wret < 0)
  3739. ret = wret;
  3740. if (wret != 0)
  3741. break;
  3742. }
  3743. btrfs_free_path(path);
  3744. return ret;
  3745. }
  3746. static unsigned long calc_ra(unsigned long start, unsigned long last,
  3747. unsigned long nr)
  3748. {
  3749. return min(last, start + nr - 1);
  3750. }
  3751. static noinline int relocate_inode_pages(struct inode *inode, u64 start,
  3752. u64 len)
  3753. {
  3754. u64 page_start;
  3755. u64 page_end;
  3756. unsigned long first_index;
  3757. unsigned long last_index;
  3758. unsigned long i;
  3759. struct page *page;
  3760. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3761. struct file_ra_state *ra;
  3762. struct btrfs_ordered_extent *ordered;
  3763. unsigned int total_read = 0;
  3764. unsigned int total_dirty = 0;
  3765. int ret = 0;
  3766. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  3767. mutex_lock(&inode->i_mutex);
  3768. first_index = start >> PAGE_CACHE_SHIFT;
  3769. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  3770. /* make sure the dirty trick played by the caller work */
  3771. ret = invalidate_inode_pages2_range(inode->i_mapping,
  3772. first_index, last_index);
  3773. if (ret)
  3774. goto out_unlock;
  3775. file_ra_state_init(ra, inode->i_mapping);
  3776. for (i = first_index ; i <= last_index; i++) {
  3777. if (total_read % ra->ra_pages == 0) {
  3778. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  3779. calc_ra(i, last_index, ra->ra_pages));
  3780. }
  3781. total_read++;
  3782. again:
  3783. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  3784. BUG_ON(1);
  3785. page = grab_cache_page(inode->i_mapping, i);
  3786. if (!page) {
  3787. ret = -ENOMEM;
  3788. goto out_unlock;
  3789. }
  3790. if (!PageUptodate(page)) {
  3791. btrfs_readpage(NULL, page);
  3792. lock_page(page);
  3793. if (!PageUptodate(page)) {
  3794. unlock_page(page);
  3795. page_cache_release(page);
  3796. ret = -EIO;
  3797. goto out_unlock;
  3798. }
  3799. }
  3800. wait_on_page_writeback(page);
  3801. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  3802. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3803. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3804. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3805. if (ordered) {
  3806. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3807. unlock_page(page);
  3808. page_cache_release(page);
  3809. btrfs_start_ordered_extent(inode, ordered, 1);
  3810. btrfs_put_ordered_extent(ordered);
  3811. goto again;
  3812. }
  3813. set_page_extent_mapped(page);
  3814. if (i == first_index)
  3815. set_extent_bits(io_tree, page_start, page_end,
  3816. EXTENT_BOUNDARY, GFP_NOFS);
  3817. btrfs_set_extent_delalloc(inode, page_start, page_end);
  3818. set_page_dirty(page);
  3819. total_dirty++;
  3820. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3821. unlock_page(page);
  3822. page_cache_release(page);
  3823. }
  3824. out_unlock:
  3825. kfree(ra);
  3826. mutex_unlock(&inode->i_mutex);
  3827. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  3828. return ret;
  3829. }
  3830. static noinline int relocate_data_extent(struct inode *reloc_inode,
  3831. struct btrfs_key *extent_key,
  3832. u64 offset)
  3833. {
  3834. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  3835. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  3836. struct extent_map *em;
  3837. u64 start = extent_key->objectid - offset;
  3838. u64 end = start + extent_key->offset - 1;
  3839. em = alloc_extent_map(GFP_NOFS);
  3840. BUG_ON(!em || IS_ERR(em));
  3841. em->start = start;
  3842. em->len = extent_key->offset;
  3843. em->block_len = extent_key->offset;
  3844. em->block_start = extent_key->objectid;
  3845. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3846. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  3847. /* setup extent map to cheat btrfs_readpage */
  3848. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  3849. while (1) {
  3850. int ret;
  3851. spin_lock(&em_tree->lock);
  3852. ret = add_extent_mapping(em_tree, em);
  3853. spin_unlock(&em_tree->lock);
  3854. if (ret != -EEXIST) {
  3855. free_extent_map(em);
  3856. break;
  3857. }
  3858. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  3859. }
  3860. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  3861. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  3862. }
  3863. struct btrfs_ref_path {
  3864. u64 extent_start;
  3865. u64 nodes[BTRFS_MAX_LEVEL];
  3866. u64 root_objectid;
  3867. u64 root_generation;
  3868. u64 owner_objectid;
  3869. u32 num_refs;
  3870. int lowest_level;
  3871. int current_level;
  3872. int shared_level;
  3873. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  3874. u64 new_nodes[BTRFS_MAX_LEVEL];
  3875. };
  3876. struct disk_extent {
  3877. u64 ram_bytes;
  3878. u64 disk_bytenr;
  3879. u64 disk_num_bytes;
  3880. u64 offset;
  3881. u64 num_bytes;
  3882. u8 compression;
  3883. u8 encryption;
  3884. u16 other_encoding;
  3885. };
  3886. static int is_cowonly_root(u64 root_objectid)
  3887. {
  3888. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  3889. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  3890. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  3891. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  3892. root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  3893. root_objectid == BTRFS_CSUM_TREE_OBJECTID)
  3894. return 1;
  3895. return 0;
  3896. }
  3897. static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
  3898. struct btrfs_root *extent_root,
  3899. struct btrfs_ref_path *ref_path,
  3900. int first_time)
  3901. {
  3902. struct extent_buffer *leaf;
  3903. struct btrfs_path *path;
  3904. struct btrfs_extent_ref *ref;
  3905. struct btrfs_key key;
  3906. struct btrfs_key found_key;
  3907. u64 bytenr;
  3908. u32 nritems;
  3909. int level;
  3910. int ret = 1;
  3911. path = btrfs_alloc_path();
  3912. if (!path)
  3913. return -ENOMEM;
  3914. if (first_time) {
  3915. ref_path->lowest_level = -1;
  3916. ref_path->current_level = -1;
  3917. ref_path->shared_level = -1;
  3918. goto walk_up;
  3919. }
  3920. walk_down:
  3921. level = ref_path->current_level - 1;
  3922. while (level >= -1) {
  3923. u64 parent;
  3924. if (level < ref_path->lowest_level)
  3925. break;
  3926. if (level >= 0)
  3927. bytenr = ref_path->nodes[level];
  3928. else
  3929. bytenr = ref_path->extent_start;
  3930. BUG_ON(bytenr == 0);
  3931. parent = ref_path->nodes[level + 1];
  3932. ref_path->nodes[level + 1] = 0;
  3933. ref_path->current_level = level;
  3934. BUG_ON(parent == 0);
  3935. key.objectid = bytenr;
  3936. key.offset = parent + 1;
  3937. key.type = BTRFS_EXTENT_REF_KEY;
  3938. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3939. if (ret < 0)
  3940. goto out;
  3941. BUG_ON(ret == 0);
  3942. leaf = path->nodes[0];
  3943. nritems = btrfs_header_nritems(leaf);
  3944. if (path->slots[0] >= nritems) {
  3945. ret = btrfs_next_leaf(extent_root, path);
  3946. if (ret < 0)
  3947. goto out;
  3948. if (ret > 0)
  3949. goto next;
  3950. leaf = path->nodes[0];
  3951. }
  3952. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3953. if (found_key.objectid == bytenr &&
  3954. found_key.type == BTRFS_EXTENT_REF_KEY) {
  3955. if (level < ref_path->shared_level)
  3956. ref_path->shared_level = level;
  3957. goto found;
  3958. }
  3959. next:
  3960. level--;
  3961. btrfs_release_path(extent_root, path);
  3962. cond_resched();
  3963. }
  3964. /* reached lowest level */
  3965. ret = 1;
  3966. goto out;
  3967. walk_up:
  3968. level = ref_path->current_level;
  3969. while (level < BTRFS_MAX_LEVEL - 1) {
  3970. u64 ref_objectid;
  3971. if (level >= 0)
  3972. bytenr = ref_path->nodes[level];
  3973. else
  3974. bytenr = ref_path->extent_start;
  3975. BUG_ON(bytenr == 0);
  3976. key.objectid = bytenr;
  3977. key.offset = 0;
  3978. key.type = BTRFS_EXTENT_REF_KEY;
  3979. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3980. if (ret < 0)
  3981. goto out;
  3982. leaf = path->nodes[0];
  3983. nritems = btrfs_header_nritems(leaf);
  3984. if (path->slots[0] >= nritems) {
  3985. ret = btrfs_next_leaf(extent_root, path);
  3986. if (ret < 0)
  3987. goto out;
  3988. if (ret > 0) {
  3989. /* the extent was freed by someone */
  3990. if (ref_path->lowest_level == level)
  3991. goto out;
  3992. btrfs_release_path(extent_root, path);
  3993. goto walk_down;
  3994. }
  3995. leaf = path->nodes[0];
  3996. }
  3997. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3998. if (found_key.objectid != bytenr ||
  3999. found_key.type != BTRFS_EXTENT_REF_KEY) {
  4000. /* the extent was freed by someone */
  4001. if (ref_path->lowest_level == level) {
  4002. ret = 1;
  4003. goto out;
  4004. }
  4005. btrfs_release_path(extent_root, path);
  4006. goto walk_down;
  4007. }
  4008. found:
  4009. ref = btrfs_item_ptr(leaf, path->slots[0],
  4010. struct btrfs_extent_ref);
  4011. ref_objectid = btrfs_ref_objectid(leaf, ref);
  4012. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4013. if (first_time) {
  4014. level = (int)ref_objectid;
  4015. BUG_ON(level >= BTRFS_MAX_LEVEL);
  4016. ref_path->lowest_level = level;
  4017. ref_path->current_level = level;
  4018. ref_path->nodes[level] = bytenr;
  4019. } else {
  4020. WARN_ON(ref_objectid != level);
  4021. }
  4022. } else {
  4023. WARN_ON(level != -1);
  4024. }
  4025. first_time = 0;
  4026. if (ref_path->lowest_level == level) {
  4027. ref_path->owner_objectid = ref_objectid;
  4028. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  4029. }
  4030. /*
  4031. * the block is tree root or the block isn't in reference
  4032. * counted tree.
  4033. */
  4034. if (found_key.objectid == found_key.offset ||
  4035. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  4036. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  4037. ref_path->root_generation =
  4038. btrfs_ref_generation(leaf, ref);
  4039. if (level < 0) {
  4040. /* special reference from the tree log */
  4041. ref_path->nodes[0] = found_key.offset;
  4042. ref_path->current_level = 0;
  4043. }
  4044. ret = 0;
  4045. goto out;
  4046. }
  4047. level++;
  4048. BUG_ON(ref_path->nodes[level] != 0);
  4049. ref_path->nodes[level] = found_key.offset;
  4050. ref_path->current_level = level;
  4051. /*
  4052. * the reference was created in the running transaction,
  4053. * no need to continue walking up.
  4054. */
  4055. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  4056. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  4057. ref_path->root_generation =
  4058. btrfs_ref_generation(leaf, ref);
  4059. ret = 0;
  4060. goto out;
  4061. }
  4062. btrfs_release_path(extent_root, path);
  4063. cond_resched();
  4064. }
  4065. /* reached max tree level, but no tree root found. */
  4066. BUG();
  4067. out:
  4068. btrfs_free_path(path);
  4069. return ret;
  4070. }
  4071. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  4072. struct btrfs_root *extent_root,
  4073. struct btrfs_ref_path *ref_path,
  4074. u64 extent_start)
  4075. {
  4076. memset(ref_path, 0, sizeof(*ref_path));
  4077. ref_path->extent_start = extent_start;
  4078. return __next_ref_path(trans, extent_root, ref_path, 1);
  4079. }
  4080. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  4081. struct btrfs_root *extent_root,
  4082. struct btrfs_ref_path *ref_path)
  4083. {
  4084. return __next_ref_path(trans, extent_root, ref_path, 0);
  4085. }
  4086. static noinline int get_new_locations(struct inode *reloc_inode,
  4087. struct btrfs_key *extent_key,
  4088. u64 offset, int no_fragment,
  4089. struct disk_extent **extents,
  4090. int *nr_extents)
  4091. {
  4092. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  4093. struct btrfs_path *path;
  4094. struct btrfs_file_extent_item *fi;
  4095. struct extent_buffer *leaf;
  4096. struct disk_extent *exts = *extents;
  4097. struct btrfs_key found_key;
  4098. u64 cur_pos;
  4099. u64 last_byte;
  4100. u32 nritems;
  4101. int nr = 0;
  4102. int max = *nr_extents;
  4103. int ret;
  4104. WARN_ON(!no_fragment && *extents);
  4105. if (!exts) {
  4106. max = 1;
  4107. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  4108. if (!exts)
  4109. return -ENOMEM;
  4110. }
  4111. path = btrfs_alloc_path();
  4112. BUG_ON(!path);
  4113. cur_pos = extent_key->objectid - offset;
  4114. last_byte = extent_key->objectid + extent_key->offset;
  4115. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  4116. cur_pos, 0);
  4117. if (ret < 0)
  4118. goto out;
  4119. if (ret > 0) {
  4120. ret = -ENOENT;
  4121. goto out;
  4122. }
  4123. while (1) {
  4124. leaf = path->nodes[0];
  4125. nritems = btrfs_header_nritems(leaf);
  4126. if (path->slots[0] >= nritems) {
  4127. ret = btrfs_next_leaf(root, path);
  4128. if (ret < 0)
  4129. goto out;
  4130. if (ret > 0)
  4131. break;
  4132. leaf = path->nodes[0];
  4133. }
  4134. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4135. if (found_key.offset != cur_pos ||
  4136. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  4137. found_key.objectid != reloc_inode->i_ino)
  4138. break;
  4139. fi = btrfs_item_ptr(leaf, path->slots[0],
  4140. struct btrfs_file_extent_item);
  4141. if (btrfs_file_extent_type(leaf, fi) !=
  4142. BTRFS_FILE_EXTENT_REG ||
  4143. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  4144. break;
  4145. if (nr == max) {
  4146. struct disk_extent *old = exts;
  4147. max *= 2;
  4148. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  4149. memcpy(exts, old, sizeof(*exts) * nr);
  4150. if (old != *extents)
  4151. kfree(old);
  4152. }
  4153. exts[nr].disk_bytenr =
  4154. btrfs_file_extent_disk_bytenr(leaf, fi);
  4155. exts[nr].disk_num_bytes =
  4156. btrfs_file_extent_disk_num_bytes(leaf, fi);
  4157. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  4158. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4159. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  4160. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  4161. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  4162. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  4163. fi);
  4164. BUG_ON(exts[nr].offset > 0);
  4165. BUG_ON(exts[nr].compression || exts[nr].encryption);
  4166. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  4167. cur_pos += exts[nr].num_bytes;
  4168. nr++;
  4169. if (cur_pos + offset >= last_byte)
  4170. break;
  4171. if (no_fragment) {
  4172. ret = 1;
  4173. goto out;
  4174. }
  4175. path->slots[0]++;
  4176. }
  4177. BUG_ON(cur_pos + offset > last_byte);
  4178. if (cur_pos + offset < last_byte) {
  4179. ret = -ENOENT;
  4180. goto out;
  4181. }
  4182. ret = 0;
  4183. out:
  4184. btrfs_free_path(path);
  4185. if (ret) {
  4186. if (exts != *extents)
  4187. kfree(exts);
  4188. } else {
  4189. *extents = exts;
  4190. *nr_extents = nr;
  4191. }
  4192. return ret;
  4193. }
  4194. static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
  4195. struct btrfs_root *root,
  4196. struct btrfs_path *path,
  4197. struct btrfs_key *extent_key,
  4198. struct btrfs_key *leaf_key,
  4199. struct btrfs_ref_path *ref_path,
  4200. struct disk_extent *new_extents,
  4201. int nr_extents)
  4202. {
  4203. struct extent_buffer *leaf;
  4204. struct btrfs_file_extent_item *fi;
  4205. struct inode *inode = NULL;
  4206. struct btrfs_key key;
  4207. u64 lock_start = 0;
  4208. u64 lock_end = 0;
  4209. u64 num_bytes;
  4210. u64 ext_offset;
  4211. u64 search_end = (u64)-1;
  4212. u32 nritems;
  4213. int nr_scaned = 0;
  4214. int extent_locked = 0;
  4215. int extent_type;
  4216. int ret;
  4217. memcpy(&key, leaf_key, sizeof(key));
  4218. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  4219. if (key.objectid < ref_path->owner_objectid ||
  4220. (key.objectid == ref_path->owner_objectid &&
  4221. key.type < BTRFS_EXTENT_DATA_KEY)) {
  4222. key.objectid = ref_path->owner_objectid;
  4223. key.type = BTRFS_EXTENT_DATA_KEY;
  4224. key.offset = 0;
  4225. }
  4226. }
  4227. while (1) {
  4228. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  4229. if (ret < 0)
  4230. goto out;
  4231. leaf = path->nodes[0];
  4232. nritems = btrfs_header_nritems(leaf);
  4233. next:
  4234. if (extent_locked && ret > 0) {
  4235. /*
  4236. * the file extent item was modified by someone
  4237. * before the extent got locked.
  4238. */
  4239. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4240. lock_end, GFP_NOFS);
  4241. extent_locked = 0;
  4242. }
  4243. if (path->slots[0] >= nritems) {
  4244. if (++nr_scaned > 2)
  4245. break;
  4246. BUG_ON(extent_locked);
  4247. ret = btrfs_next_leaf(root, path);
  4248. if (ret < 0)
  4249. goto out;
  4250. if (ret > 0)
  4251. break;
  4252. leaf = path->nodes[0];
  4253. nritems = btrfs_header_nritems(leaf);
  4254. }
  4255. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4256. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  4257. if ((key.objectid > ref_path->owner_objectid) ||
  4258. (key.objectid == ref_path->owner_objectid &&
  4259. key.type > BTRFS_EXTENT_DATA_KEY) ||
  4260. key.offset >= search_end)
  4261. break;
  4262. }
  4263. if (inode && key.objectid != inode->i_ino) {
  4264. BUG_ON(extent_locked);
  4265. btrfs_release_path(root, path);
  4266. mutex_unlock(&inode->i_mutex);
  4267. iput(inode);
  4268. inode = NULL;
  4269. continue;
  4270. }
  4271. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  4272. path->slots[0]++;
  4273. ret = 1;
  4274. goto next;
  4275. }
  4276. fi = btrfs_item_ptr(leaf, path->slots[0],
  4277. struct btrfs_file_extent_item);
  4278. extent_type = btrfs_file_extent_type(leaf, fi);
  4279. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  4280. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  4281. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  4282. extent_key->objectid)) {
  4283. path->slots[0]++;
  4284. ret = 1;
  4285. goto next;
  4286. }
  4287. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4288. ext_offset = btrfs_file_extent_offset(leaf, fi);
  4289. if (search_end == (u64)-1) {
  4290. search_end = key.offset - ext_offset +
  4291. btrfs_file_extent_ram_bytes(leaf, fi);
  4292. }
  4293. if (!extent_locked) {
  4294. lock_start = key.offset;
  4295. lock_end = lock_start + num_bytes - 1;
  4296. } else {
  4297. if (lock_start > key.offset ||
  4298. lock_end + 1 < key.offset + num_bytes) {
  4299. unlock_extent(&BTRFS_I(inode)->io_tree,
  4300. lock_start, lock_end, GFP_NOFS);
  4301. extent_locked = 0;
  4302. }
  4303. }
  4304. if (!inode) {
  4305. btrfs_release_path(root, path);
  4306. inode = btrfs_iget_locked(root->fs_info->sb,
  4307. key.objectid, root);
  4308. if (inode->i_state & I_NEW) {
  4309. BTRFS_I(inode)->root = root;
  4310. BTRFS_I(inode)->location.objectid =
  4311. key.objectid;
  4312. BTRFS_I(inode)->location.type =
  4313. BTRFS_INODE_ITEM_KEY;
  4314. BTRFS_I(inode)->location.offset = 0;
  4315. btrfs_read_locked_inode(inode);
  4316. unlock_new_inode(inode);
  4317. }
  4318. /*
  4319. * some code call btrfs_commit_transaction while
  4320. * holding the i_mutex, so we can't use mutex_lock
  4321. * here.
  4322. */
  4323. if (is_bad_inode(inode) ||
  4324. !mutex_trylock(&inode->i_mutex)) {
  4325. iput(inode);
  4326. inode = NULL;
  4327. key.offset = (u64)-1;
  4328. goto skip;
  4329. }
  4330. }
  4331. if (!extent_locked) {
  4332. struct btrfs_ordered_extent *ordered;
  4333. btrfs_release_path(root, path);
  4334. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4335. lock_end, GFP_NOFS);
  4336. ordered = btrfs_lookup_first_ordered_extent(inode,
  4337. lock_end);
  4338. if (ordered &&
  4339. ordered->file_offset <= lock_end &&
  4340. ordered->file_offset + ordered->len > lock_start) {
  4341. unlock_extent(&BTRFS_I(inode)->io_tree,
  4342. lock_start, lock_end, GFP_NOFS);
  4343. btrfs_start_ordered_extent(inode, ordered, 1);
  4344. btrfs_put_ordered_extent(ordered);
  4345. key.offset += num_bytes;
  4346. goto skip;
  4347. }
  4348. if (ordered)
  4349. btrfs_put_ordered_extent(ordered);
  4350. extent_locked = 1;
  4351. continue;
  4352. }
  4353. if (nr_extents == 1) {
  4354. /* update extent pointer in place */
  4355. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4356. new_extents[0].disk_bytenr);
  4357. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4358. new_extents[0].disk_num_bytes);
  4359. btrfs_mark_buffer_dirty(leaf);
  4360. btrfs_drop_extent_cache(inode, key.offset,
  4361. key.offset + num_bytes - 1, 0);
  4362. ret = btrfs_inc_extent_ref(trans, root,
  4363. new_extents[0].disk_bytenr,
  4364. new_extents[0].disk_num_bytes,
  4365. leaf->start,
  4366. root->root_key.objectid,
  4367. trans->transid,
  4368. key.objectid);
  4369. BUG_ON(ret);
  4370. ret = btrfs_free_extent(trans, root,
  4371. extent_key->objectid,
  4372. extent_key->offset,
  4373. leaf->start,
  4374. btrfs_header_owner(leaf),
  4375. btrfs_header_generation(leaf),
  4376. key.objectid, 0);
  4377. BUG_ON(ret);
  4378. btrfs_release_path(root, path);
  4379. key.offset += num_bytes;
  4380. } else {
  4381. BUG_ON(1);
  4382. #if 0
  4383. u64 alloc_hint;
  4384. u64 extent_len;
  4385. int i;
  4386. /*
  4387. * drop old extent pointer at first, then insert the
  4388. * new pointers one bye one
  4389. */
  4390. btrfs_release_path(root, path);
  4391. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  4392. key.offset + num_bytes,
  4393. key.offset, &alloc_hint);
  4394. BUG_ON(ret);
  4395. for (i = 0; i < nr_extents; i++) {
  4396. if (ext_offset >= new_extents[i].num_bytes) {
  4397. ext_offset -= new_extents[i].num_bytes;
  4398. continue;
  4399. }
  4400. extent_len = min(new_extents[i].num_bytes -
  4401. ext_offset, num_bytes);
  4402. ret = btrfs_insert_empty_item(trans, root,
  4403. path, &key,
  4404. sizeof(*fi));
  4405. BUG_ON(ret);
  4406. leaf = path->nodes[0];
  4407. fi = btrfs_item_ptr(leaf, path->slots[0],
  4408. struct btrfs_file_extent_item);
  4409. btrfs_set_file_extent_generation(leaf, fi,
  4410. trans->transid);
  4411. btrfs_set_file_extent_type(leaf, fi,
  4412. BTRFS_FILE_EXTENT_REG);
  4413. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4414. new_extents[i].disk_bytenr);
  4415. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4416. new_extents[i].disk_num_bytes);
  4417. btrfs_set_file_extent_ram_bytes(leaf, fi,
  4418. new_extents[i].ram_bytes);
  4419. btrfs_set_file_extent_compression(leaf, fi,
  4420. new_extents[i].compression);
  4421. btrfs_set_file_extent_encryption(leaf, fi,
  4422. new_extents[i].encryption);
  4423. btrfs_set_file_extent_other_encoding(leaf, fi,
  4424. new_extents[i].other_encoding);
  4425. btrfs_set_file_extent_num_bytes(leaf, fi,
  4426. extent_len);
  4427. ext_offset += new_extents[i].offset;
  4428. btrfs_set_file_extent_offset(leaf, fi,
  4429. ext_offset);
  4430. btrfs_mark_buffer_dirty(leaf);
  4431. btrfs_drop_extent_cache(inode, key.offset,
  4432. key.offset + extent_len - 1, 0);
  4433. ret = btrfs_inc_extent_ref(trans, root,
  4434. new_extents[i].disk_bytenr,
  4435. new_extents[i].disk_num_bytes,
  4436. leaf->start,
  4437. root->root_key.objectid,
  4438. trans->transid, key.objectid);
  4439. BUG_ON(ret);
  4440. btrfs_release_path(root, path);
  4441. inode_add_bytes(inode, extent_len);
  4442. ext_offset = 0;
  4443. num_bytes -= extent_len;
  4444. key.offset += extent_len;
  4445. if (num_bytes == 0)
  4446. break;
  4447. }
  4448. BUG_ON(i >= nr_extents);
  4449. #endif
  4450. }
  4451. if (extent_locked) {
  4452. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4453. lock_end, GFP_NOFS);
  4454. extent_locked = 0;
  4455. }
  4456. skip:
  4457. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  4458. key.offset >= search_end)
  4459. break;
  4460. cond_resched();
  4461. }
  4462. ret = 0;
  4463. out:
  4464. btrfs_release_path(root, path);
  4465. if (inode) {
  4466. mutex_unlock(&inode->i_mutex);
  4467. if (extent_locked) {
  4468. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4469. lock_end, GFP_NOFS);
  4470. }
  4471. iput(inode);
  4472. }
  4473. return ret;
  4474. }
  4475. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  4476. struct btrfs_root *root,
  4477. struct extent_buffer *buf, u64 orig_start)
  4478. {
  4479. int level;
  4480. int ret;
  4481. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  4482. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  4483. level = btrfs_header_level(buf);
  4484. if (level == 0) {
  4485. struct btrfs_leaf_ref *ref;
  4486. struct btrfs_leaf_ref *orig_ref;
  4487. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  4488. if (!orig_ref)
  4489. return -ENOENT;
  4490. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  4491. if (!ref) {
  4492. btrfs_free_leaf_ref(root, orig_ref);
  4493. return -ENOMEM;
  4494. }
  4495. ref->nritems = orig_ref->nritems;
  4496. memcpy(ref->extents, orig_ref->extents,
  4497. sizeof(ref->extents[0]) * ref->nritems);
  4498. btrfs_free_leaf_ref(root, orig_ref);
  4499. ref->root_gen = trans->transid;
  4500. ref->bytenr = buf->start;
  4501. ref->owner = btrfs_header_owner(buf);
  4502. ref->generation = btrfs_header_generation(buf);
  4503. ret = btrfs_add_leaf_ref(root, ref, 0);
  4504. WARN_ON(ret);
  4505. btrfs_free_leaf_ref(root, ref);
  4506. }
  4507. return 0;
  4508. }
  4509. static noinline int invalidate_extent_cache(struct btrfs_root *root,
  4510. struct extent_buffer *leaf,
  4511. struct btrfs_block_group_cache *group,
  4512. struct btrfs_root *target_root)
  4513. {
  4514. struct btrfs_key key;
  4515. struct inode *inode = NULL;
  4516. struct btrfs_file_extent_item *fi;
  4517. u64 num_bytes;
  4518. u64 skip_objectid = 0;
  4519. u32 nritems;
  4520. u32 i;
  4521. nritems = btrfs_header_nritems(leaf);
  4522. for (i = 0; i < nritems; i++) {
  4523. btrfs_item_key_to_cpu(leaf, &key, i);
  4524. if (key.objectid == skip_objectid ||
  4525. key.type != BTRFS_EXTENT_DATA_KEY)
  4526. continue;
  4527. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  4528. if (btrfs_file_extent_type(leaf, fi) ==
  4529. BTRFS_FILE_EXTENT_INLINE)
  4530. continue;
  4531. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  4532. continue;
  4533. if (!inode || inode->i_ino != key.objectid) {
  4534. iput(inode);
  4535. inode = btrfs_ilookup(target_root->fs_info->sb,
  4536. key.objectid, target_root, 1);
  4537. }
  4538. if (!inode) {
  4539. skip_objectid = key.objectid;
  4540. continue;
  4541. }
  4542. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4543. lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  4544. key.offset + num_bytes - 1, GFP_NOFS);
  4545. btrfs_drop_extent_cache(inode, key.offset,
  4546. key.offset + num_bytes - 1, 1);
  4547. unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  4548. key.offset + num_bytes - 1, GFP_NOFS);
  4549. cond_resched();
  4550. }
  4551. iput(inode);
  4552. return 0;
  4553. }
  4554. static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  4555. struct btrfs_root *root,
  4556. struct extent_buffer *leaf,
  4557. struct btrfs_block_group_cache *group,
  4558. struct inode *reloc_inode)
  4559. {
  4560. struct btrfs_key key;
  4561. struct btrfs_key extent_key;
  4562. struct btrfs_file_extent_item *fi;
  4563. struct btrfs_leaf_ref *ref;
  4564. struct disk_extent *new_extent;
  4565. u64 bytenr;
  4566. u64 num_bytes;
  4567. u32 nritems;
  4568. u32 i;
  4569. int ext_index;
  4570. int nr_extent;
  4571. int ret;
  4572. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  4573. BUG_ON(!new_extent);
  4574. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  4575. BUG_ON(!ref);
  4576. ext_index = -1;
  4577. nritems = btrfs_header_nritems(leaf);
  4578. for (i = 0; i < nritems; i++) {
  4579. btrfs_item_key_to_cpu(leaf, &key, i);
  4580. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  4581. continue;
  4582. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  4583. if (btrfs_file_extent_type(leaf, fi) ==
  4584. BTRFS_FILE_EXTENT_INLINE)
  4585. continue;
  4586. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  4587. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  4588. if (bytenr == 0)
  4589. continue;
  4590. ext_index++;
  4591. if (bytenr >= group->key.objectid + group->key.offset ||
  4592. bytenr + num_bytes <= group->key.objectid)
  4593. continue;
  4594. extent_key.objectid = bytenr;
  4595. extent_key.offset = num_bytes;
  4596. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  4597. nr_extent = 1;
  4598. ret = get_new_locations(reloc_inode, &extent_key,
  4599. group->key.objectid, 1,
  4600. &new_extent, &nr_extent);
  4601. if (ret > 0)
  4602. continue;
  4603. BUG_ON(ret < 0);
  4604. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  4605. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  4606. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  4607. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  4608. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4609. new_extent->disk_bytenr);
  4610. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4611. new_extent->disk_num_bytes);
  4612. btrfs_mark_buffer_dirty(leaf);
  4613. ret = btrfs_inc_extent_ref(trans, root,
  4614. new_extent->disk_bytenr,
  4615. new_extent->disk_num_bytes,
  4616. leaf->start,
  4617. root->root_key.objectid,
  4618. trans->transid, key.objectid);
  4619. BUG_ON(ret);
  4620. ret = btrfs_free_extent(trans, root,
  4621. bytenr, num_bytes, leaf->start,
  4622. btrfs_header_owner(leaf),
  4623. btrfs_header_generation(leaf),
  4624. key.objectid, 0);
  4625. BUG_ON(ret);
  4626. cond_resched();
  4627. }
  4628. kfree(new_extent);
  4629. BUG_ON(ext_index + 1 != ref->nritems);
  4630. btrfs_free_leaf_ref(root, ref);
  4631. return 0;
  4632. }
  4633. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  4634. struct btrfs_root *root)
  4635. {
  4636. struct btrfs_root *reloc_root;
  4637. int ret;
  4638. if (root->reloc_root) {
  4639. reloc_root = root->reloc_root;
  4640. root->reloc_root = NULL;
  4641. list_add(&reloc_root->dead_list,
  4642. &root->fs_info->dead_reloc_roots);
  4643. btrfs_set_root_bytenr(&reloc_root->root_item,
  4644. reloc_root->node->start);
  4645. btrfs_set_root_level(&root->root_item,
  4646. btrfs_header_level(reloc_root->node));
  4647. memset(&reloc_root->root_item.drop_progress, 0,
  4648. sizeof(struct btrfs_disk_key));
  4649. reloc_root->root_item.drop_level = 0;
  4650. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  4651. &reloc_root->root_key,
  4652. &reloc_root->root_item);
  4653. BUG_ON(ret);
  4654. }
  4655. return 0;
  4656. }
  4657. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  4658. {
  4659. struct btrfs_trans_handle *trans;
  4660. struct btrfs_root *reloc_root;
  4661. struct btrfs_root *prev_root = NULL;
  4662. struct list_head dead_roots;
  4663. int ret;
  4664. unsigned long nr;
  4665. INIT_LIST_HEAD(&dead_roots);
  4666. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  4667. while (!list_empty(&dead_roots)) {
  4668. reloc_root = list_entry(dead_roots.prev,
  4669. struct btrfs_root, dead_list);
  4670. list_del_init(&reloc_root->dead_list);
  4671. BUG_ON(reloc_root->commit_root != NULL);
  4672. while (1) {
  4673. trans = btrfs_join_transaction(root, 1);
  4674. BUG_ON(!trans);
  4675. mutex_lock(&root->fs_info->drop_mutex);
  4676. ret = btrfs_drop_snapshot(trans, reloc_root);
  4677. if (ret != -EAGAIN)
  4678. break;
  4679. mutex_unlock(&root->fs_info->drop_mutex);
  4680. nr = trans->blocks_used;
  4681. ret = btrfs_end_transaction(trans, root);
  4682. BUG_ON(ret);
  4683. btrfs_btree_balance_dirty(root, nr);
  4684. }
  4685. free_extent_buffer(reloc_root->node);
  4686. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  4687. &reloc_root->root_key);
  4688. BUG_ON(ret);
  4689. mutex_unlock(&root->fs_info->drop_mutex);
  4690. nr = trans->blocks_used;
  4691. ret = btrfs_end_transaction(trans, root);
  4692. BUG_ON(ret);
  4693. btrfs_btree_balance_dirty(root, nr);
  4694. kfree(prev_root);
  4695. prev_root = reloc_root;
  4696. }
  4697. if (prev_root) {
  4698. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  4699. kfree(prev_root);
  4700. }
  4701. return 0;
  4702. }
  4703. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  4704. {
  4705. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  4706. return 0;
  4707. }
  4708. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  4709. {
  4710. struct btrfs_root *reloc_root;
  4711. struct btrfs_trans_handle *trans;
  4712. struct btrfs_key location;
  4713. int found;
  4714. int ret;
  4715. mutex_lock(&root->fs_info->tree_reloc_mutex);
  4716. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  4717. BUG_ON(ret);
  4718. found = !list_empty(&root->fs_info->dead_reloc_roots);
  4719. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  4720. if (found) {
  4721. trans = btrfs_start_transaction(root, 1);
  4722. BUG_ON(!trans);
  4723. ret = btrfs_commit_transaction(trans, root);
  4724. BUG_ON(ret);
  4725. }
  4726. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  4727. location.offset = (u64)-1;
  4728. location.type = BTRFS_ROOT_ITEM_KEY;
  4729. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  4730. BUG_ON(!reloc_root);
  4731. btrfs_orphan_cleanup(reloc_root);
  4732. return 0;
  4733. }
  4734. static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
  4735. struct btrfs_root *root)
  4736. {
  4737. struct btrfs_root *reloc_root;
  4738. struct extent_buffer *eb;
  4739. struct btrfs_root_item *root_item;
  4740. struct btrfs_key root_key;
  4741. int ret;
  4742. BUG_ON(!root->ref_cows);
  4743. if (root->reloc_root)
  4744. return 0;
  4745. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  4746. BUG_ON(!root_item);
  4747. ret = btrfs_copy_root(trans, root, root->commit_root,
  4748. &eb, BTRFS_TREE_RELOC_OBJECTID);
  4749. BUG_ON(ret);
  4750. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  4751. root_key.offset = root->root_key.objectid;
  4752. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4753. memcpy(root_item, &root->root_item, sizeof(root_item));
  4754. btrfs_set_root_refs(root_item, 0);
  4755. btrfs_set_root_bytenr(root_item, eb->start);
  4756. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  4757. btrfs_set_root_generation(root_item, trans->transid);
  4758. btrfs_tree_unlock(eb);
  4759. free_extent_buffer(eb);
  4760. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  4761. &root_key, root_item);
  4762. BUG_ON(ret);
  4763. kfree(root_item);
  4764. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  4765. &root_key);
  4766. BUG_ON(!reloc_root);
  4767. reloc_root->last_trans = trans->transid;
  4768. reloc_root->commit_root = NULL;
  4769. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  4770. root->reloc_root = reloc_root;
  4771. return 0;
  4772. }
  4773. /*
  4774. * Core function of space balance.
  4775. *
  4776. * The idea is using reloc trees to relocate tree blocks in reference
  4777. * counted roots. There is one reloc tree for each subvol, and all
  4778. * reloc trees share same root key objectid. Reloc trees are snapshots
  4779. * of the latest committed roots of subvols (root->commit_root).
  4780. *
  4781. * To relocate a tree block referenced by a subvol, there are two steps.
  4782. * COW the block through subvol's reloc tree, then update block pointer
  4783. * in the subvol to point to the new block. Since all reloc trees share
  4784. * same root key objectid, doing special handing for tree blocks owned
  4785. * by them is easy. Once a tree block has been COWed in one reloc tree,
  4786. * we can use the resulting new block directly when the same block is
  4787. * required to COW again through other reloc trees. By this way, relocated
  4788. * tree blocks are shared between reloc trees, so they are also shared
  4789. * between subvols.
  4790. */
  4791. static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
  4792. struct btrfs_root *root,
  4793. struct btrfs_path *path,
  4794. struct btrfs_key *first_key,
  4795. struct btrfs_ref_path *ref_path,
  4796. struct btrfs_block_group_cache *group,
  4797. struct inode *reloc_inode)
  4798. {
  4799. struct btrfs_root *reloc_root;
  4800. struct extent_buffer *eb = NULL;
  4801. struct btrfs_key *keys;
  4802. u64 *nodes;
  4803. int level;
  4804. int shared_level;
  4805. int lowest_level = 0;
  4806. int ret;
  4807. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  4808. lowest_level = ref_path->owner_objectid;
  4809. if (!root->ref_cows) {
  4810. path->lowest_level = lowest_level;
  4811. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  4812. BUG_ON(ret < 0);
  4813. path->lowest_level = 0;
  4814. btrfs_release_path(root, path);
  4815. return 0;
  4816. }
  4817. mutex_lock(&root->fs_info->tree_reloc_mutex);
  4818. ret = init_reloc_tree(trans, root);
  4819. BUG_ON(ret);
  4820. reloc_root = root->reloc_root;
  4821. shared_level = ref_path->shared_level;
  4822. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  4823. keys = ref_path->node_keys;
  4824. nodes = ref_path->new_nodes;
  4825. memset(&keys[shared_level + 1], 0,
  4826. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  4827. memset(&nodes[shared_level + 1], 0,
  4828. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  4829. if (nodes[lowest_level] == 0) {
  4830. path->lowest_level = lowest_level;
  4831. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  4832. 0, 1);
  4833. BUG_ON(ret);
  4834. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  4835. eb = path->nodes[level];
  4836. if (!eb || eb == reloc_root->node)
  4837. break;
  4838. nodes[level] = eb->start;
  4839. if (level == 0)
  4840. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  4841. else
  4842. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  4843. }
  4844. if (nodes[0] &&
  4845. ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4846. eb = path->nodes[0];
  4847. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  4848. group, reloc_inode);
  4849. BUG_ON(ret);
  4850. }
  4851. btrfs_release_path(reloc_root, path);
  4852. } else {
  4853. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  4854. lowest_level);
  4855. BUG_ON(ret);
  4856. }
  4857. /*
  4858. * replace tree blocks in the fs tree with tree blocks in
  4859. * the reloc tree.
  4860. */
  4861. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  4862. BUG_ON(ret < 0);
  4863. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4864. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  4865. 0, 0);
  4866. BUG_ON(ret);
  4867. extent_buffer_get(path->nodes[0]);
  4868. eb = path->nodes[0];
  4869. btrfs_release_path(reloc_root, path);
  4870. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  4871. BUG_ON(ret);
  4872. free_extent_buffer(eb);
  4873. }
  4874. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  4875. path->lowest_level = 0;
  4876. return 0;
  4877. }
  4878. static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
  4879. struct btrfs_root *root,
  4880. struct btrfs_path *path,
  4881. struct btrfs_key *first_key,
  4882. struct btrfs_ref_path *ref_path)
  4883. {
  4884. int ret;
  4885. ret = relocate_one_path(trans, root, path, first_key,
  4886. ref_path, NULL, NULL);
  4887. BUG_ON(ret);
  4888. if (root == root->fs_info->extent_root)
  4889. btrfs_extent_post_op(trans, root);
  4890. return 0;
  4891. }
  4892. static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
  4893. struct btrfs_root *extent_root,
  4894. struct btrfs_path *path,
  4895. struct btrfs_key *extent_key)
  4896. {
  4897. int ret;
  4898. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  4899. if (ret)
  4900. goto out;
  4901. ret = btrfs_del_item(trans, extent_root, path);
  4902. out:
  4903. btrfs_release_path(extent_root, path);
  4904. return ret;
  4905. }
  4906. static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
  4907. struct btrfs_ref_path *ref_path)
  4908. {
  4909. struct btrfs_key root_key;
  4910. root_key.objectid = ref_path->root_objectid;
  4911. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4912. if (is_cowonly_root(ref_path->root_objectid))
  4913. root_key.offset = 0;
  4914. else
  4915. root_key.offset = (u64)-1;
  4916. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  4917. }
  4918. static noinline int relocate_one_extent(struct btrfs_root *extent_root,
  4919. struct btrfs_path *path,
  4920. struct btrfs_key *extent_key,
  4921. struct btrfs_block_group_cache *group,
  4922. struct inode *reloc_inode, int pass)
  4923. {
  4924. struct btrfs_trans_handle *trans;
  4925. struct btrfs_root *found_root;
  4926. struct btrfs_ref_path *ref_path = NULL;
  4927. struct disk_extent *new_extents = NULL;
  4928. int nr_extents = 0;
  4929. int loops;
  4930. int ret;
  4931. int level;
  4932. struct btrfs_key first_key;
  4933. u64 prev_block = 0;
  4934. trans = btrfs_start_transaction(extent_root, 1);
  4935. BUG_ON(!trans);
  4936. if (extent_key->objectid == 0) {
  4937. ret = del_extent_zero(trans, extent_root, path, extent_key);
  4938. goto out;
  4939. }
  4940. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  4941. if (!ref_path) {
  4942. ret = -ENOMEM;
  4943. goto out;
  4944. }
  4945. for (loops = 0; ; loops++) {
  4946. if (loops == 0) {
  4947. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  4948. extent_key->objectid);
  4949. } else {
  4950. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  4951. }
  4952. if (ret < 0)
  4953. goto out;
  4954. if (ret > 0)
  4955. break;
  4956. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  4957. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  4958. continue;
  4959. found_root = read_ref_root(extent_root->fs_info, ref_path);
  4960. BUG_ON(!found_root);
  4961. /*
  4962. * for reference counted tree, only process reference paths
  4963. * rooted at the latest committed root.
  4964. */
  4965. if (found_root->ref_cows &&
  4966. ref_path->root_generation != found_root->root_key.offset)
  4967. continue;
  4968. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4969. if (pass == 0) {
  4970. /*
  4971. * copy data extents to new locations
  4972. */
  4973. u64 group_start = group->key.objectid;
  4974. ret = relocate_data_extent(reloc_inode,
  4975. extent_key,
  4976. group_start);
  4977. if (ret < 0)
  4978. goto out;
  4979. break;
  4980. }
  4981. level = 0;
  4982. } else {
  4983. level = ref_path->owner_objectid;
  4984. }
  4985. if (prev_block != ref_path->nodes[level]) {
  4986. struct extent_buffer *eb;
  4987. u64 block_start = ref_path->nodes[level];
  4988. u64 block_size = btrfs_level_size(found_root, level);
  4989. eb = read_tree_block(found_root, block_start,
  4990. block_size, 0);
  4991. btrfs_tree_lock(eb);
  4992. BUG_ON(level != btrfs_header_level(eb));
  4993. if (level == 0)
  4994. btrfs_item_key_to_cpu(eb, &first_key, 0);
  4995. else
  4996. btrfs_node_key_to_cpu(eb, &first_key, 0);
  4997. btrfs_tree_unlock(eb);
  4998. free_extent_buffer(eb);
  4999. prev_block = block_start;
  5000. }
  5001. btrfs_record_root_in_trans(found_root);
  5002. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  5003. /*
  5004. * try to update data extent references while
  5005. * keeping metadata shared between snapshots.
  5006. */
  5007. if (pass == 1) {
  5008. ret = relocate_one_path(trans, found_root,
  5009. path, &first_key, ref_path,
  5010. group, reloc_inode);
  5011. if (ret < 0)
  5012. goto out;
  5013. continue;
  5014. }
  5015. /*
  5016. * use fallback method to process the remaining
  5017. * references.
  5018. */
  5019. if (!new_extents) {
  5020. u64 group_start = group->key.objectid;
  5021. new_extents = kmalloc(sizeof(*new_extents),
  5022. GFP_NOFS);
  5023. nr_extents = 1;
  5024. ret = get_new_locations(reloc_inode,
  5025. extent_key,
  5026. group_start, 1,
  5027. &new_extents,
  5028. &nr_extents);
  5029. if (ret)
  5030. goto out;
  5031. }
  5032. ret = replace_one_extent(trans, found_root,
  5033. path, extent_key,
  5034. &first_key, ref_path,
  5035. new_extents, nr_extents);
  5036. } else {
  5037. ret = relocate_tree_block(trans, found_root, path,
  5038. &first_key, ref_path);
  5039. }
  5040. if (ret < 0)
  5041. goto out;
  5042. }
  5043. ret = 0;
  5044. out:
  5045. btrfs_end_transaction(trans, extent_root);
  5046. kfree(new_extents);
  5047. kfree(ref_path);
  5048. return ret;
  5049. }
  5050. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  5051. {
  5052. u64 num_devices;
  5053. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  5054. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  5055. num_devices = root->fs_info->fs_devices->rw_devices;
  5056. if (num_devices == 1) {
  5057. stripped |= BTRFS_BLOCK_GROUP_DUP;
  5058. stripped = flags & ~stripped;
  5059. /* turn raid0 into single device chunks */
  5060. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5061. return stripped;
  5062. /* turn mirroring into duplication */
  5063. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  5064. BTRFS_BLOCK_GROUP_RAID10))
  5065. return stripped | BTRFS_BLOCK_GROUP_DUP;
  5066. return flags;
  5067. } else {
  5068. /* they already had raid on here, just return */
  5069. if (flags & stripped)
  5070. return flags;
  5071. stripped |= BTRFS_BLOCK_GROUP_DUP;
  5072. stripped = flags & ~stripped;
  5073. /* switch duplicated blocks with raid1 */
  5074. if (flags & BTRFS_BLOCK_GROUP_DUP)
  5075. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  5076. /* turn single device chunks into raid0 */
  5077. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  5078. }
  5079. return flags;
  5080. }
  5081. static int __alloc_chunk_for_shrink(struct btrfs_root *root,
  5082. struct btrfs_block_group_cache *shrink_block_group,
  5083. int force)
  5084. {
  5085. struct btrfs_trans_handle *trans;
  5086. u64 new_alloc_flags;
  5087. u64 calc;
  5088. spin_lock(&shrink_block_group->lock);
  5089. if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
  5090. spin_unlock(&shrink_block_group->lock);
  5091. trans = btrfs_start_transaction(root, 1);
  5092. spin_lock(&shrink_block_group->lock);
  5093. new_alloc_flags = update_block_group_flags(root,
  5094. shrink_block_group->flags);
  5095. if (new_alloc_flags != shrink_block_group->flags) {
  5096. calc =
  5097. btrfs_block_group_used(&shrink_block_group->item);
  5098. } else {
  5099. calc = shrink_block_group->key.offset;
  5100. }
  5101. spin_unlock(&shrink_block_group->lock);
  5102. do_chunk_alloc(trans, root->fs_info->extent_root,
  5103. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  5104. btrfs_end_transaction(trans, root);
  5105. } else
  5106. spin_unlock(&shrink_block_group->lock);
  5107. return 0;
  5108. }
  5109. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  5110. struct btrfs_root *root,
  5111. u64 objectid, u64 size)
  5112. {
  5113. struct btrfs_path *path;
  5114. struct btrfs_inode_item *item;
  5115. struct extent_buffer *leaf;
  5116. int ret;
  5117. path = btrfs_alloc_path();
  5118. if (!path)
  5119. return -ENOMEM;
  5120. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  5121. if (ret)
  5122. goto out;
  5123. leaf = path->nodes[0];
  5124. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  5125. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  5126. btrfs_set_inode_generation(leaf, item, 1);
  5127. btrfs_set_inode_size(leaf, item, size);
  5128. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  5129. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
  5130. btrfs_mark_buffer_dirty(leaf);
  5131. btrfs_release_path(root, path);
  5132. out:
  5133. btrfs_free_path(path);
  5134. return ret;
  5135. }
  5136. static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
  5137. struct btrfs_block_group_cache *group)
  5138. {
  5139. struct inode *inode = NULL;
  5140. struct btrfs_trans_handle *trans;
  5141. struct btrfs_root *root;
  5142. struct btrfs_key root_key;
  5143. u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
  5144. int err = 0;
  5145. root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  5146. root_key.type = BTRFS_ROOT_ITEM_KEY;
  5147. root_key.offset = (u64)-1;
  5148. root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  5149. if (IS_ERR(root))
  5150. return ERR_CAST(root);
  5151. trans = btrfs_start_transaction(root, 1);
  5152. BUG_ON(!trans);
  5153. err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
  5154. if (err)
  5155. goto out;
  5156. err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
  5157. BUG_ON(err);
  5158. err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
  5159. group->key.offset, 0, group->key.offset,
  5160. 0, 0, 0);
  5161. BUG_ON(err);
  5162. inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
  5163. if (inode->i_state & I_NEW) {
  5164. BTRFS_I(inode)->root = root;
  5165. BTRFS_I(inode)->location.objectid = objectid;
  5166. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  5167. BTRFS_I(inode)->location.offset = 0;
  5168. btrfs_read_locked_inode(inode);
  5169. unlock_new_inode(inode);
  5170. BUG_ON(is_bad_inode(inode));
  5171. } else {
  5172. BUG_ON(1);
  5173. }
  5174. BTRFS_I(inode)->index_cnt = group->key.objectid;
  5175. err = btrfs_orphan_add(trans, inode);
  5176. out:
  5177. btrfs_end_transaction(trans, root);
  5178. if (err) {
  5179. if (inode)
  5180. iput(inode);
  5181. inode = ERR_PTR(err);
  5182. }
  5183. return inode;
  5184. }
  5185. int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
  5186. {
  5187. struct btrfs_ordered_sum *sums;
  5188. struct btrfs_sector_sum *sector_sum;
  5189. struct btrfs_ordered_extent *ordered;
  5190. struct btrfs_root *root = BTRFS_I(inode)->root;
  5191. struct list_head list;
  5192. size_t offset;
  5193. int ret;
  5194. u64 disk_bytenr;
  5195. INIT_LIST_HEAD(&list);
  5196. ordered = btrfs_lookup_ordered_extent(inode, file_pos);
  5197. BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
  5198. disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
  5199. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
  5200. disk_bytenr + len - 1, &list);
  5201. while (!list_empty(&list)) {
  5202. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  5203. list_del_init(&sums->list);
  5204. sector_sum = sums->sums;
  5205. sums->bytenr = ordered->start;
  5206. offset = 0;
  5207. while (offset < sums->len) {
  5208. sector_sum->bytenr += ordered->start - disk_bytenr;
  5209. sector_sum++;
  5210. offset += root->sectorsize;
  5211. }
  5212. btrfs_add_ordered_sum(inode, ordered, sums);
  5213. }
  5214. btrfs_put_ordered_extent(ordered);
  5215. return 0;
  5216. }
  5217. int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
  5218. {
  5219. struct btrfs_trans_handle *trans;
  5220. struct btrfs_path *path;
  5221. struct btrfs_fs_info *info = root->fs_info;
  5222. struct extent_buffer *leaf;
  5223. struct inode *reloc_inode;
  5224. struct btrfs_block_group_cache *block_group;
  5225. struct btrfs_key key;
  5226. u64 skipped;
  5227. u64 cur_byte;
  5228. u64 total_found;
  5229. u32 nritems;
  5230. int ret;
  5231. int progress;
  5232. int pass = 0;
  5233. root = root->fs_info->extent_root;
  5234. block_group = btrfs_lookup_block_group(info, group_start);
  5235. BUG_ON(!block_group);
  5236. printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
  5237. (unsigned long long)block_group->key.objectid,
  5238. (unsigned long long)block_group->flags);
  5239. path = btrfs_alloc_path();
  5240. BUG_ON(!path);
  5241. reloc_inode = create_reloc_inode(info, block_group);
  5242. BUG_ON(IS_ERR(reloc_inode));
  5243. __alloc_chunk_for_shrink(root, block_group, 1);
  5244. set_block_group_readonly(block_group);
  5245. btrfs_start_delalloc_inodes(info->tree_root);
  5246. btrfs_wait_ordered_extents(info->tree_root, 0);
  5247. again:
  5248. skipped = 0;
  5249. total_found = 0;
  5250. progress = 0;
  5251. key.objectid = block_group->key.objectid;
  5252. key.offset = 0;
  5253. key.type = 0;
  5254. cur_byte = key.objectid;
  5255. trans = btrfs_start_transaction(info->tree_root, 1);
  5256. btrfs_commit_transaction(trans, info->tree_root);
  5257. mutex_lock(&root->fs_info->cleaner_mutex);
  5258. btrfs_clean_old_snapshots(info->tree_root);
  5259. btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
  5260. mutex_unlock(&root->fs_info->cleaner_mutex);
  5261. while (1) {
  5262. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5263. if (ret < 0)
  5264. goto out;
  5265. next:
  5266. leaf = path->nodes[0];
  5267. nritems = btrfs_header_nritems(leaf);
  5268. if (path->slots[0] >= nritems) {
  5269. ret = btrfs_next_leaf(root, path);
  5270. if (ret < 0)
  5271. goto out;
  5272. if (ret == 1) {
  5273. ret = 0;
  5274. break;
  5275. }
  5276. leaf = path->nodes[0];
  5277. nritems = btrfs_header_nritems(leaf);
  5278. }
  5279. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  5280. if (key.objectid >= block_group->key.objectid +
  5281. block_group->key.offset)
  5282. break;
  5283. if (progress && need_resched()) {
  5284. btrfs_release_path(root, path);
  5285. cond_resched();
  5286. progress = 0;
  5287. continue;
  5288. }
  5289. progress = 1;
  5290. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
  5291. key.objectid + key.offset <= cur_byte) {
  5292. path->slots[0]++;
  5293. goto next;
  5294. }
  5295. total_found++;
  5296. cur_byte = key.objectid + key.offset;
  5297. btrfs_release_path(root, path);
  5298. __alloc_chunk_for_shrink(root, block_group, 0);
  5299. ret = relocate_one_extent(root, path, &key, block_group,
  5300. reloc_inode, pass);
  5301. BUG_ON(ret < 0);
  5302. if (ret > 0)
  5303. skipped++;
  5304. key.objectid = cur_byte;
  5305. key.type = 0;
  5306. key.offset = 0;
  5307. }
  5308. btrfs_release_path(root, path);
  5309. if (pass == 0) {
  5310. btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
  5311. invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
  5312. }
  5313. if (total_found > 0) {
  5314. printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
  5315. (unsigned long long)total_found, pass);
  5316. pass++;
  5317. if (total_found == skipped && pass > 2) {
  5318. iput(reloc_inode);
  5319. reloc_inode = create_reloc_inode(info, block_group);
  5320. pass = 0;
  5321. }
  5322. goto again;
  5323. }
  5324. /* delete reloc_inode */
  5325. iput(reloc_inode);
  5326. /* unpin extents in this range */
  5327. trans = btrfs_start_transaction(info->tree_root, 1);
  5328. btrfs_commit_transaction(trans, info->tree_root);
  5329. spin_lock(&block_group->lock);
  5330. WARN_ON(block_group->pinned > 0);
  5331. WARN_ON(block_group->reserved > 0);
  5332. WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
  5333. spin_unlock(&block_group->lock);
  5334. put_block_group(block_group);
  5335. ret = 0;
  5336. out:
  5337. btrfs_free_path(path);
  5338. return ret;
  5339. }
  5340. static int find_first_block_group(struct btrfs_root *root,
  5341. struct btrfs_path *path, struct btrfs_key *key)
  5342. {
  5343. int ret = 0;
  5344. struct btrfs_key found_key;
  5345. struct extent_buffer *leaf;
  5346. int slot;
  5347. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  5348. if (ret < 0)
  5349. goto out;
  5350. while (1) {
  5351. slot = path->slots[0];
  5352. leaf = path->nodes[0];
  5353. if (slot >= btrfs_header_nritems(leaf)) {
  5354. ret = btrfs_next_leaf(root, path);
  5355. if (ret == 0)
  5356. continue;
  5357. if (ret < 0)
  5358. goto out;
  5359. break;
  5360. }
  5361. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5362. if (found_key.objectid >= key->objectid &&
  5363. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  5364. ret = 0;
  5365. goto out;
  5366. }
  5367. path->slots[0]++;
  5368. }
  5369. ret = -ENOENT;
  5370. out:
  5371. return ret;
  5372. }
  5373. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  5374. {
  5375. struct btrfs_block_group_cache *block_group;
  5376. struct rb_node *n;
  5377. spin_lock(&info->block_group_cache_lock);
  5378. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  5379. block_group = rb_entry(n, struct btrfs_block_group_cache,
  5380. cache_node);
  5381. rb_erase(&block_group->cache_node,
  5382. &info->block_group_cache_tree);
  5383. spin_unlock(&info->block_group_cache_lock);
  5384. btrfs_remove_free_space_cache(block_group);
  5385. down_write(&block_group->space_info->groups_sem);
  5386. list_del(&block_group->list);
  5387. up_write(&block_group->space_info->groups_sem);
  5388. WARN_ON(atomic_read(&block_group->count) != 1);
  5389. kfree(block_group);
  5390. spin_lock(&info->block_group_cache_lock);
  5391. }
  5392. spin_unlock(&info->block_group_cache_lock);
  5393. return 0;
  5394. }
  5395. int btrfs_read_block_groups(struct btrfs_root *root)
  5396. {
  5397. struct btrfs_path *path;
  5398. int ret;
  5399. struct btrfs_block_group_cache *cache;
  5400. struct btrfs_fs_info *info = root->fs_info;
  5401. struct btrfs_space_info *space_info;
  5402. struct btrfs_key key;
  5403. struct btrfs_key found_key;
  5404. struct extent_buffer *leaf;
  5405. root = info->extent_root;
  5406. key.objectid = 0;
  5407. key.offset = 0;
  5408. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  5409. path = btrfs_alloc_path();
  5410. if (!path)
  5411. return -ENOMEM;
  5412. while (1) {
  5413. ret = find_first_block_group(root, path, &key);
  5414. if (ret > 0) {
  5415. ret = 0;
  5416. goto error;
  5417. }
  5418. if (ret != 0)
  5419. goto error;
  5420. leaf = path->nodes[0];
  5421. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5422. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  5423. if (!cache) {
  5424. ret = -ENOMEM;
  5425. break;
  5426. }
  5427. atomic_set(&cache->count, 1);
  5428. spin_lock_init(&cache->lock);
  5429. mutex_init(&cache->alloc_mutex);
  5430. mutex_init(&cache->cache_mutex);
  5431. INIT_LIST_HEAD(&cache->list);
  5432. read_extent_buffer(leaf, &cache->item,
  5433. btrfs_item_ptr_offset(leaf, path->slots[0]),
  5434. sizeof(cache->item));
  5435. memcpy(&cache->key, &found_key, sizeof(found_key));
  5436. key.objectid = found_key.objectid + found_key.offset;
  5437. btrfs_release_path(root, path);
  5438. cache->flags = btrfs_block_group_flags(&cache->item);
  5439. ret = update_space_info(info, cache->flags, found_key.offset,
  5440. btrfs_block_group_used(&cache->item),
  5441. &space_info);
  5442. BUG_ON(ret);
  5443. cache->space_info = space_info;
  5444. down_write(&space_info->groups_sem);
  5445. list_add_tail(&cache->list, &space_info->block_groups);
  5446. up_write(&space_info->groups_sem);
  5447. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  5448. BUG_ON(ret);
  5449. set_avail_alloc_bits(root->fs_info, cache->flags);
  5450. if (btrfs_chunk_readonly(root, cache->key.objectid))
  5451. set_block_group_readonly(cache);
  5452. }
  5453. ret = 0;
  5454. error:
  5455. btrfs_free_path(path);
  5456. return ret;
  5457. }
  5458. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  5459. struct btrfs_root *root, u64 bytes_used,
  5460. u64 type, u64 chunk_objectid, u64 chunk_offset,
  5461. u64 size)
  5462. {
  5463. int ret;
  5464. struct btrfs_root *extent_root;
  5465. struct btrfs_block_group_cache *cache;
  5466. extent_root = root->fs_info->extent_root;
  5467. root->fs_info->last_trans_new_blockgroup = trans->transid;
  5468. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  5469. if (!cache)
  5470. return -ENOMEM;
  5471. cache->key.objectid = chunk_offset;
  5472. cache->key.offset = size;
  5473. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  5474. atomic_set(&cache->count, 1);
  5475. spin_lock_init(&cache->lock);
  5476. mutex_init(&cache->alloc_mutex);
  5477. mutex_init(&cache->cache_mutex);
  5478. INIT_LIST_HEAD(&cache->list);
  5479. btrfs_set_block_group_used(&cache->item, bytes_used);
  5480. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  5481. cache->flags = type;
  5482. btrfs_set_block_group_flags(&cache->item, type);
  5483. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  5484. &cache->space_info);
  5485. BUG_ON(ret);
  5486. down_write(&cache->space_info->groups_sem);
  5487. list_add_tail(&cache->list, &cache->space_info->block_groups);
  5488. up_write(&cache->space_info->groups_sem);
  5489. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  5490. BUG_ON(ret);
  5491. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  5492. sizeof(cache->item));
  5493. BUG_ON(ret);
  5494. finish_current_insert(trans, extent_root, 0);
  5495. ret = del_pending_extents(trans, extent_root, 0);
  5496. BUG_ON(ret);
  5497. set_avail_alloc_bits(extent_root->fs_info, type);
  5498. return 0;
  5499. }
  5500. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  5501. struct btrfs_root *root, u64 group_start)
  5502. {
  5503. struct btrfs_path *path;
  5504. struct btrfs_block_group_cache *block_group;
  5505. struct btrfs_key key;
  5506. int ret;
  5507. root = root->fs_info->extent_root;
  5508. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  5509. BUG_ON(!block_group);
  5510. BUG_ON(!block_group->ro);
  5511. memcpy(&key, &block_group->key, sizeof(key));
  5512. path = btrfs_alloc_path();
  5513. BUG_ON(!path);
  5514. spin_lock(&root->fs_info->block_group_cache_lock);
  5515. rb_erase(&block_group->cache_node,
  5516. &root->fs_info->block_group_cache_tree);
  5517. spin_unlock(&root->fs_info->block_group_cache_lock);
  5518. btrfs_remove_free_space_cache(block_group);
  5519. down_write(&block_group->space_info->groups_sem);
  5520. list_del(&block_group->list);
  5521. up_write(&block_group->space_info->groups_sem);
  5522. spin_lock(&block_group->space_info->lock);
  5523. block_group->space_info->total_bytes -= block_group->key.offset;
  5524. block_group->space_info->bytes_readonly -= block_group->key.offset;
  5525. spin_unlock(&block_group->space_info->lock);
  5526. block_group->space_info->full = 0;
  5527. put_block_group(block_group);
  5528. put_block_group(block_group);
  5529. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  5530. if (ret > 0)
  5531. ret = -EIO;
  5532. if (ret < 0)
  5533. goto out;
  5534. ret = btrfs_del_item(trans, root, path);
  5535. out:
  5536. btrfs_free_path(path);
  5537. return ret;
  5538. }