extent-tree.c 194 KB

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