extent-tree.c 187 KB

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