extent-tree.c 181 KB

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