extent-tree.c 186 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180
  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. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  2140. struct btrfs_root *root)
  2141. {
  2142. struct btrfs_block_group_cache *cache, *entry;
  2143. struct rb_node *n;
  2144. int err = 0;
  2145. int werr = 0;
  2146. struct btrfs_path *path;
  2147. u64 last = 0;
  2148. path = btrfs_alloc_path();
  2149. if (!path)
  2150. return -ENOMEM;
  2151. while (1) {
  2152. cache = NULL;
  2153. spin_lock(&root->fs_info->block_group_cache_lock);
  2154. for (n = rb_first(&root->fs_info->block_group_cache_tree);
  2155. n; n = rb_next(n)) {
  2156. entry = rb_entry(n, struct btrfs_block_group_cache,
  2157. cache_node);
  2158. if (entry->dirty) {
  2159. cache = entry;
  2160. break;
  2161. }
  2162. }
  2163. spin_unlock(&root->fs_info->block_group_cache_lock);
  2164. if (!cache)
  2165. break;
  2166. cache->dirty = 0;
  2167. last += cache->key.offset;
  2168. err = write_one_cache_group(trans, root,
  2169. path, cache);
  2170. /*
  2171. * if we fail to write the cache group, we want
  2172. * to keep it marked dirty in hopes that a later
  2173. * write will work
  2174. */
  2175. if (err) {
  2176. werr = err;
  2177. continue;
  2178. }
  2179. }
  2180. btrfs_free_path(path);
  2181. return werr;
  2182. }
  2183. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  2184. {
  2185. struct btrfs_block_group_cache *block_group;
  2186. int readonly = 0;
  2187. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  2188. if (!block_group || block_group->ro)
  2189. readonly = 1;
  2190. if (block_group)
  2191. btrfs_put_block_group(block_group);
  2192. return readonly;
  2193. }
  2194. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  2195. u64 total_bytes, u64 bytes_used,
  2196. struct btrfs_space_info **space_info)
  2197. {
  2198. struct btrfs_space_info *found;
  2199. found = __find_space_info(info, flags);
  2200. if (found) {
  2201. spin_lock(&found->lock);
  2202. found->total_bytes += total_bytes;
  2203. found->bytes_used += bytes_used;
  2204. found->full = 0;
  2205. spin_unlock(&found->lock);
  2206. *space_info = found;
  2207. return 0;
  2208. }
  2209. found = kzalloc(sizeof(*found), GFP_NOFS);
  2210. if (!found)
  2211. return -ENOMEM;
  2212. INIT_LIST_HEAD(&found->block_groups);
  2213. init_rwsem(&found->groups_sem);
  2214. spin_lock_init(&found->lock);
  2215. found->flags = flags;
  2216. found->total_bytes = total_bytes;
  2217. found->bytes_used = bytes_used;
  2218. found->bytes_pinned = 0;
  2219. found->bytes_reserved = 0;
  2220. found->bytes_readonly = 0;
  2221. found->bytes_delalloc = 0;
  2222. found->full = 0;
  2223. found->force_alloc = 0;
  2224. *space_info = found;
  2225. list_add_rcu(&found->list, &info->space_info);
  2226. return 0;
  2227. }
  2228. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  2229. {
  2230. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  2231. BTRFS_BLOCK_GROUP_RAID1 |
  2232. BTRFS_BLOCK_GROUP_RAID10 |
  2233. BTRFS_BLOCK_GROUP_DUP);
  2234. if (extra_flags) {
  2235. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2236. fs_info->avail_data_alloc_bits |= extra_flags;
  2237. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  2238. fs_info->avail_metadata_alloc_bits |= extra_flags;
  2239. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  2240. fs_info->avail_system_alloc_bits |= extra_flags;
  2241. }
  2242. }
  2243. static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
  2244. {
  2245. spin_lock(&cache->space_info->lock);
  2246. spin_lock(&cache->lock);
  2247. if (!cache->ro) {
  2248. cache->space_info->bytes_readonly += cache->key.offset -
  2249. btrfs_block_group_used(&cache->item);
  2250. cache->ro = 1;
  2251. }
  2252. spin_unlock(&cache->lock);
  2253. spin_unlock(&cache->space_info->lock);
  2254. }
  2255. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  2256. {
  2257. u64 num_devices = root->fs_info->fs_devices->rw_devices;
  2258. if (num_devices == 1)
  2259. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  2260. if (num_devices < 4)
  2261. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  2262. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  2263. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  2264. BTRFS_BLOCK_GROUP_RAID10))) {
  2265. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  2266. }
  2267. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  2268. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  2269. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  2270. }
  2271. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  2272. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  2273. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  2274. (flags & BTRFS_BLOCK_GROUP_DUP)))
  2275. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  2276. return flags;
  2277. }
  2278. static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
  2279. {
  2280. struct btrfs_fs_info *info = root->fs_info;
  2281. u64 alloc_profile;
  2282. if (data) {
  2283. alloc_profile = info->avail_data_alloc_bits &
  2284. info->data_alloc_profile;
  2285. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  2286. } else if (root == root->fs_info->chunk_root) {
  2287. alloc_profile = info->avail_system_alloc_bits &
  2288. info->system_alloc_profile;
  2289. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  2290. } else {
  2291. alloc_profile = info->avail_metadata_alloc_bits &
  2292. info->metadata_alloc_profile;
  2293. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  2294. }
  2295. return btrfs_reduce_alloc_profile(root, data);
  2296. }
  2297. void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
  2298. {
  2299. u64 alloc_target;
  2300. alloc_target = btrfs_get_alloc_profile(root, 1);
  2301. BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
  2302. alloc_target);
  2303. }
  2304. /*
  2305. * for now this just makes sure we have at least 5% of our metadata space free
  2306. * for use.
  2307. */
  2308. int btrfs_check_metadata_free_space(struct btrfs_root *root)
  2309. {
  2310. struct btrfs_fs_info *info = root->fs_info;
  2311. struct btrfs_space_info *meta_sinfo;
  2312. u64 alloc_target, thresh;
  2313. int committed = 0, ret;
  2314. /* get the space info for where the metadata will live */
  2315. alloc_target = btrfs_get_alloc_profile(root, 0);
  2316. meta_sinfo = __find_space_info(info, alloc_target);
  2317. again:
  2318. spin_lock(&meta_sinfo->lock);
  2319. if (!meta_sinfo->full)
  2320. thresh = meta_sinfo->total_bytes * 80;
  2321. else
  2322. thresh = meta_sinfo->total_bytes * 95;
  2323. do_div(thresh, 100);
  2324. if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
  2325. meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
  2326. struct btrfs_trans_handle *trans;
  2327. if (!meta_sinfo->full) {
  2328. meta_sinfo->force_alloc = 1;
  2329. spin_unlock(&meta_sinfo->lock);
  2330. trans = btrfs_start_transaction(root, 1);
  2331. if (!trans)
  2332. return -ENOMEM;
  2333. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2334. 2 * 1024 * 1024, alloc_target, 0);
  2335. btrfs_end_transaction(trans, root);
  2336. goto again;
  2337. }
  2338. spin_unlock(&meta_sinfo->lock);
  2339. if (!committed) {
  2340. committed = 1;
  2341. trans = btrfs_join_transaction(root, 1);
  2342. if (!trans)
  2343. return -ENOMEM;
  2344. ret = btrfs_commit_transaction(trans, root);
  2345. if (ret)
  2346. return ret;
  2347. goto again;
  2348. }
  2349. return -ENOSPC;
  2350. }
  2351. spin_unlock(&meta_sinfo->lock);
  2352. return 0;
  2353. }
  2354. /*
  2355. * This will check the space that the inode allocates from to make sure we have
  2356. * enough space for bytes.
  2357. */
  2358. int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
  2359. u64 bytes)
  2360. {
  2361. struct btrfs_space_info *data_sinfo;
  2362. int ret = 0, committed = 0;
  2363. /* make sure bytes are sectorsize aligned */
  2364. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2365. data_sinfo = BTRFS_I(inode)->space_info;
  2366. again:
  2367. /* make sure we have enough space to handle the data first */
  2368. spin_lock(&data_sinfo->lock);
  2369. if (data_sinfo->total_bytes - data_sinfo->bytes_used -
  2370. data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
  2371. data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
  2372. data_sinfo->bytes_may_use < bytes) {
  2373. struct btrfs_trans_handle *trans;
  2374. /*
  2375. * if we don't have enough free bytes in this space then we need
  2376. * to alloc a new chunk.
  2377. */
  2378. if (!data_sinfo->full) {
  2379. u64 alloc_target;
  2380. data_sinfo->force_alloc = 1;
  2381. spin_unlock(&data_sinfo->lock);
  2382. alloc_target = btrfs_get_alloc_profile(root, 1);
  2383. trans = btrfs_start_transaction(root, 1);
  2384. if (!trans)
  2385. return -ENOMEM;
  2386. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2387. bytes + 2 * 1024 * 1024,
  2388. alloc_target, 0);
  2389. btrfs_end_transaction(trans, root);
  2390. if (ret)
  2391. return ret;
  2392. goto again;
  2393. }
  2394. spin_unlock(&data_sinfo->lock);
  2395. /* commit the current transaction and try again */
  2396. if (!committed) {
  2397. committed = 1;
  2398. trans = btrfs_join_transaction(root, 1);
  2399. if (!trans)
  2400. return -ENOMEM;
  2401. ret = btrfs_commit_transaction(trans, root);
  2402. if (ret)
  2403. return ret;
  2404. goto again;
  2405. }
  2406. printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
  2407. ", %llu bytes_used, %llu bytes_reserved, "
  2408. "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
  2409. "%llu total\n", (unsigned long long)bytes,
  2410. (unsigned long long)data_sinfo->bytes_delalloc,
  2411. (unsigned long long)data_sinfo->bytes_used,
  2412. (unsigned long long)data_sinfo->bytes_reserved,
  2413. (unsigned long long)data_sinfo->bytes_pinned,
  2414. (unsigned long long)data_sinfo->bytes_readonly,
  2415. (unsigned long long)data_sinfo->bytes_may_use,
  2416. (unsigned long long)data_sinfo->total_bytes);
  2417. return -ENOSPC;
  2418. }
  2419. data_sinfo->bytes_may_use += bytes;
  2420. BTRFS_I(inode)->reserved_bytes += bytes;
  2421. spin_unlock(&data_sinfo->lock);
  2422. return btrfs_check_metadata_free_space(root);
  2423. }
  2424. /*
  2425. * if there was an error for whatever reason after calling
  2426. * btrfs_check_data_free_space, call this so we can cleanup the counters.
  2427. */
  2428. void btrfs_free_reserved_data_space(struct btrfs_root *root,
  2429. struct inode *inode, u64 bytes)
  2430. {
  2431. struct btrfs_space_info *data_sinfo;
  2432. /* make sure bytes are sectorsize aligned */
  2433. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  2434. data_sinfo = BTRFS_I(inode)->space_info;
  2435. spin_lock(&data_sinfo->lock);
  2436. data_sinfo->bytes_may_use -= bytes;
  2437. BTRFS_I(inode)->reserved_bytes -= bytes;
  2438. spin_unlock(&data_sinfo->lock);
  2439. }
  2440. /* called when we are adding a delalloc extent to the inode's io_tree */
  2441. void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
  2442. u64 bytes)
  2443. {
  2444. struct btrfs_space_info *data_sinfo;
  2445. /* get the space info for where this inode will be storing its data */
  2446. data_sinfo = BTRFS_I(inode)->space_info;
  2447. /* make sure we have enough space to handle the data first */
  2448. spin_lock(&data_sinfo->lock);
  2449. data_sinfo->bytes_delalloc += bytes;
  2450. /*
  2451. * we are adding a delalloc extent without calling
  2452. * btrfs_check_data_free_space first. This happens on a weird
  2453. * writepage condition, but shouldn't hurt our accounting
  2454. */
  2455. if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
  2456. data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
  2457. BTRFS_I(inode)->reserved_bytes = 0;
  2458. } else {
  2459. data_sinfo->bytes_may_use -= bytes;
  2460. BTRFS_I(inode)->reserved_bytes -= bytes;
  2461. }
  2462. spin_unlock(&data_sinfo->lock);
  2463. }
  2464. /* called when we are clearing an delalloc extent from the inode's io_tree */
  2465. void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
  2466. u64 bytes)
  2467. {
  2468. struct btrfs_space_info *info;
  2469. info = BTRFS_I(inode)->space_info;
  2470. spin_lock(&info->lock);
  2471. info->bytes_delalloc -= bytes;
  2472. spin_unlock(&info->lock);
  2473. }
  2474. static void force_metadata_allocation(struct btrfs_fs_info *info)
  2475. {
  2476. struct list_head *head = &info->space_info;
  2477. struct btrfs_space_info *found;
  2478. rcu_read_lock();
  2479. list_for_each_entry_rcu(found, head, list) {
  2480. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  2481. found->force_alloc = 1;
  2482. }
  2483. rcu_read_unlock();
  2484. }
  2485. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  2486. struct btrfs_root *extent_root, u64 alloc_bytes,
  2487. u64 flags, int force)
  2488. {
  2489. struct btrfs_space_info *space_info;
  2490. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  2491. u64 thresh;
  2492. int ret = 0;
  2493. mutex_lock(&fs_info->chunk_mutex);
  2494. flags = btrfs_reduce_alloc_profile(extent_root, flags);
  2495. space_info = __find_space_info(extent_root->fs_info, flags);
  2496. if (!space_info) {
  2497. ret = update_space_info(extent_root->fs_info, flags,
  2498. 0, 0, &space_info);
  2499. BUG_ON(ret);
  2500. }
  2501. BUG_ON(!space_info);
  2502. spin_lock(&space_info->lock);
  2503. if (space_info->force_alloc) {
  2504. force = 1;
  2505. space_info->force_alloc = 0;
  2506. }
  2507. if (space_info->full) {
  2508. spin_unlock(&space_info->lock);
  2509. goto out;
  2510. }
  2511. thresh = space_info->total_bytes - space_info->bytes_readonly;
  2512. thresh = div_factor(thresh, 6);
  2513. if (!force &&
  2514. (space_info->bytes_used + space_info->bytes_pinned +
  2515. space_info->bytes_reserved + alloc_bytes) < thresh) {
  2516. spin_unlock(&space_info->lock);
  2517. goto out;
  2518. }
  2519. spin_unlock(&space_info->lock);
  2520. /*
  2521. * if we're doing a data chunk, go ahead and make sure that
  2522. * we keep a reasonable number of metadata chunks allocated in the
  2523. * FS as well.
  2524. */
  2525. if (flags & BTRFS_BLOCK_GROUP_DATA) {
  2526. fs_info->data_chunk_allocations++;
  2527. if (!(fs_info->data_chunk_allocations %
  2528. fs_info->metadata_ratio))
  2529. force_metadata_allocation(fs_info);
  2530. }
  2531. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  2532. if (ret)
  2533. space_info->full = 1;
  2534. out:
  2535. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  2536. return ret;
  2537. }
  2538. static int update_block_group(struct btrfs_trans_handle *trans,
  2539. struct btrfs_root *root,
  2540. u64 bytenr, u64 num_bytes, int alloc,
  2541. int mark_free)
  2542. {
  2543. struct btrfs_block_group_cache *cache;
  2544. struct btrfs_fs_info *info = root->fs_info;
  2545. u64 total = num_bytes;
  2546. u64 old_val;
  2547. u64 byte_in_group;
  2548. /* block accounting for super block */
  2549. spin_lock(&info->delalloc_lock);
  2550. old_val = btrfs_super_bytes_used(&info->super_copy);
  2551. if (alloc)
  2552. old_val += num_bytes;
  2553. else
  2554. old_val -= num_bytes;
  2555. btrfs_set_super_bytes_used(&info->super_copy, old_val);
  2556. /* block accounting for root item */
  2557. old_val = btrfs_root_used(&root->root_item);
  2558. if (alloc)
  2559. old_val += num_bytes;
  2560. else
  2561. old_val -= num_bytes;
  2562. btrfs_set_root_used(&root->root_item, old_val);
  2563. spin_unlock(&info->delalloc_lock);
  2564. while (total) {
  2565. cache = btrfs_lookup_block_group(info, bytenr);
  2566. if (!cache)
  2567. return -1;
  2568. byte_in_group = bytenr - cache->key.objectid;
  2569. WARN_ON(byte_in_group > cache->key.offset);
  2570. spin_lock(&cache->space_info->lock);
  2571. spin_lock(&cache->lock);
  2572. cache->dirty = 1;
  2573. old_val = btrfs_block_group_used(&cache->item);
  2574. num_bytes = min(total, cache->key.offset - byte_in_group);
  2575. if (alloc) {
  2576. old_val += num_bytes;
  2577. cache->space_info->bytes_used += num_bytes;
  2578. if (cache->ro)
  2579. cache->space_info->bytes_readonly -= num_bytes;
  2580. btrfs_set_block_group_used(&cache->item, old_val);
  2581. spin_unlock(&cache->lock);
  2582. spin_unlock(&cache->space_info->lock);
  2583. } else {
  2584. old_val -= num_bytes;
  2585. cache->space_info->bytes_used -= num_bytes;
  2586. if (cache->ro)
  2587. cache->space_info->bytes_readonly += num_bytes;
  2588. btrfs_set_block_group_used(&cache->item, old_val);
  2589. spin_unlock(&cache->lock);
  2590. spin_unlock(&cache->space_info->lock);
  2591. if (mark_free) {
  2592. int ret;
  2593. ret = btrfs_discard_extent(root, bytenr,
  2594. num_bytes);
  2595. WARN_ON(ret);
  2596. ret = btrfs_add_free_space(cache, bytenr,
  2597. num_bytes);
  2598. WARN_ON(ret);
  2599. }
  2600. }
  2601. btrfs_put_block_group(cache);
  2602. total -= num_bytes;
  2603. bytenr += num_bytes;
  2604. }
  2605. return 0;
  2606. }
  2607. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  2608. {
  2609. struct btrfs_block_group_cache *cache;
  2610. u64 bytenr;
  2611. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  2612. if (!cache)
  2613. return 0;
  2614. bytenr = cache->key.objectid;
  2615. btrfs_put_block_group(cache);
  2616. return bytenr;
  2617. }
  2618. int btrfs_update_pinned_extents(struct btrfs_root *root,
  2619. u64 bytenr, u64 num, int pin)
  2620. {
  2621. u64 len;
  2622. struct btrfs_block_group_cache *cache;
  2623. struct btrfs_fs_info *fs_info = root->fs_info;
  2624. if (pin) {
  2625. set_extent_dirty(&fs_info->pinned_extents,
  2626. bytenr, bytenr + num - 1, GFP_NOFS);
  2627. } else {
  2628. clear_extent_dirty(&fs_info->pinned_extents,
  2629. bytenr, bytenr + num - 1, GFP_NOFS);
  2630. }
  2631. while (num > 0) {
  2632. cache = btrfs_lookup_block_group(fs_info, bytenr);
  2633. BUG_ON(!cache);
  2634. len = min(num, cache->key.offset -
  2635. (bytenr - cache->key.objectid));
  2636. if (pin) {
  2637. spin_lock(&cache->space_info->lock);
  2638. spin_lock(&cache->lock);
  2639. cache->pinned += len;
  2640. cache->space_info->bytes_pinned += len;
  2641. spin_unlock(&cache->lock);
  2642. spin_unlock(&cache->space_info->lock);
  2643. fs_info->total_pinned += len;
  2644. } else {
  2645. spin_lock(&cache->space_info->lock);
  2646. spin_lock(&cache->lock);
  2647. cache->pinned -= len;
  2648. cache->space_info->bytes_pinned -= len;
  2649. spin_unlock(&cache->lock);
  2650. spin_unlock(&cache->space_info->lock);
  2651. fs_info->total_pinned -= len;
  2652. if (cache->cached)
  2653. btrfs_add_free_space(cache, bytenr, len);
  2654. }
  2655. btrfs_put_block_group(cache);
  2656. bytenr += len;
  2657. num -= len;
  2658. }
  2659. return 0;
  2660. }
  2661. static int update_reserved_extents(struct btrfs_root *root,
  2662. u64 bytenr, u64 num, int reserve)
  2663. {
  2664. u64 len;
  2665. struct btrfs_block_group_cache *cache;
  2666. struct btrfs_fs_info *fs_info = root->fs_info;
  2667. while (num > 0) {
  2668. cache = btrfs_lookup_block_group(fs_info, bytenr);
  2669. BUG_ON(!cache);
  2670. len = min(num, cache->key.offset -
  2671. (bytenr - cache->key.objectid));
  2672. spin_lock(&cache->space_info->lock);
  2673. spin_lock(&cache->lock);
  2674. if (reserve) {
  2675. cache->reserved += len;
  2676. cache->space_info->bytes_reserved += len;
  2677. } else {
  2678. cache->reserved -= len;
  2679. cache->space_info->bytes_reserved -= len;
  2680. }
  2681. spin_unlock(&cache->lock);
  2682. spin_unlock(&cache->space_info->lock);
  2683. btrfs_put_block_group(cache);
  2684. bytenr += len;
  2685. num -= len;
  2686. }
  2687. return 0;
  2688. }
  2689. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  2690. {
  2691. u64 last = 0;
  2692. u64 start;
  2693. u64 end;
  2694. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  2695. int ret;
  2696. while (1) {
  2697. ret = find_first_extent_bit(pinned_extents, last,
  2698. &start, &end, EXTENT_DIRTY);
  2699. if (ret)
  2700. break;
  2701. set_extent_dirty(copy, start, end, GFP_NOFS);
  2702. last = end + 1;
  2703. }
  2704. return 0;
  2705. }
  2706. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  2707. struct btrfs_root *root,
  2708. struct extent_io_tree *unpin)
  2709. {
  2710. u64 start;
  2711. u64 end;
  2712. int ret;
  2713. while (1) {
  2714. ret = find_first_extent_bit(unpin, 0, &start, &end,
  2715. EXTENT_DIRTY);
  2716. if (ret)
  2717. break;
  2718. ret = btrfs_discard_extent(root, start, end + 1 - start);
  2719. /* unlocks the pinned mutex */
  2720. btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
  2721. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  2722. cond_resched();
  2723. }
  2724. return ret;
  2725. }
  2726. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  2727. struct btrfs_root *root,
  2728. struct btrfs_path *path,
  2729. u64 bytenr, u64 num_bytes, int is_data,
  2730. struct extent_buffer **must_clean)
  2731. {
  2732. int err = 0;
  2733. struct extent_buffer *buf;
  2734. if (is_data)
  2735. goto pinit;
  2736. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  2737. if (!buf)
  2738. goto pinit;
  2739. /* we can reuse a block if it hasn't been written
  2740. * and it is from this transaction. We can't
  2741. * reuse anything from the tree log root because
  2742. * it has tiny sub-transactions.
  2743. */
  2744. if (btrfs_buffer_uptodate(buf, 0) &&
  2745. btrfs_try_tree_lock(buf)) {
  2746. u64 header_owner = btrfs_header_owner(buf);
  2747. u64 header_transid = btrfs_header_generation(buf);
  2748. if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
  2749. header_transid == trans->transid &&
  2750. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  2751. *must_clean = buf;
  2752. return 1;
  2753. }
  2754. btrfs_tree_unlock(buf);
  2755. }
  2756. free_extent_buffer(buf);
  2757. pinit:
  2758. btrfs_set_path_blocking(path);
  2759. /* unlocks the pinned mutex */
  2760. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  2761. BUG_ON(err < 0);
  2762. return 0;
  2763. }
  2764. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  2765. struct btrfs_root *root,
  2766. u64 bytenr, u64 num_bytes, u64 parent,
  2767. u64 root_objectid, u64 owner_objectid,
  2768. u64 owner_offset, int refs_to_drop,
  2769. struct btrfs_delayed_extent_op *extent_op)
  2770. {
  2771. struct btrfs_key key;
  2772. struct btrfs_path *path;
  2773. struct btrfs_fs_info *info = root->fs_info;
  2774. struct btrfs_root *extent_root = info->extent_root;
  2775. struct extent_buffer *leaf;
  2776. struct btrfs_extent_item *ei;
  2777. struct btrfs_extent_inline_ref *iref;
  2778. int ret;
  2779. int is_data;
  2780. int extent_slot = 0;
  2781. int found_extent = 0;
  2782. int num_to_del = 1;
  2783. u32 item_size;
  2784. u64 refs;
  2785. path = btrfs_alloc_path();
  2786. if (!path)
  2787. return -ENOMEM;
  2788. path->reada = 1;
  2789. path->leave_spinning = 1;
  2790. is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
  2791. BUG_ON(!is_data && refs_to_drop != 1);
  2792. ret = lookup_extent_backref(trans, extent_root, path, &iref,
  2793. bytenr, num_bytes, parent,
  2794. root_objectid, owner_objectid,
  2795. owner_offset);
  2796. if (ret == 0) {
  2797. extent_slot = path->slots[0];
  2798. while (extent_slot >= 0) {
  2799. btrfs_item_key_to_cpu(path->nodes[0], &key,
  2800. extent_slot);
  2801. if (key.objectid != bytenr)
  2802. break;
  2803. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  2804. key.offset == num_bytes) {
  2805. found_extent = 1;
  2806. break;
  2807. }
  2808. if (path->slots[0] - extent_slot > 5)
  2809. break;
  2810. extent_slot--;
  2811. }
  2812. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2813. item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
  2814. if (found_extent && item_size < sizeof(*ei))
  2815. found_extent = 0;
  2816. #endif
  2817. if (!found_extent) {
  2818. BUG_ON(iref);
  2819. ret = remove_extent_backref(trans, extent_root, path,
  2820. NULL, refs_to_drop,
  2821. is_data);
  2822. BUG_ON(ret);
  2823. btrfs_release_path(extent_root, path);
  2824. path->leave_spinning = 1;
  2825. key.objectid = bytenr;
  2826. key.type = BTRFS_EXTENT_ITEM_KEY;
  2827. key.offset = num_bytes;
  2828. ret = btrfs_search_slot(trans, extent_root,
  2829. &key, path, -1, 1);
  2830. if (ret) {
  2831. printk(KERN_ERR "umm, got %d back from search"
  2832. ", was looking for %llu\n", ret,
  2833. (unsigned long long)bytenr);
  2834. btrfs_print_leaf(extent_root, path->nodes[0]);
  2835. }
  2836. BUG_ON(ret);
  2837. extent_slot = path->slots[0];
  2838. }
  2839. } else {
  2840. btrfs_print_leaf(extent_root, path->nodes[0]);
  2841. WARN_ON(1);
  2842. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  2843. "parent %llu root %llu owner %llu offset %llu\n",
  2844. (unsigned long long)bytenr,
  2845. (unsigned long long)parent,
  2846. (unsigned long long)root_objectid,
  2847. (unsigned long long)owner_objectid,
  2848. (unsigned long long)owner_offset);
  2849. }
  2850. leaf = path->nodes[0];
  2851. item_size = btrfs_item_size_nr(leaf, extent_slot);
  2852. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2853. if (item_size < sizeof(*ei)) {
  2854. BUG_ON(found_extent || extent_slot != path->slots[0]);
  2855. ret = convert_extent_item_v0(trans, extent_root, path,
  2856. owner_objectid, 0);
  2857. BUG_ON(ret < 0);
  2858. btrfs_release_path(extent_root, path);
  2859. path->leave_spinning = 1;
  2860. key.objectid = bytenr;
  2861. key.type = BTRFS_EXTENT_ITEM_KEY;
  2862. key.offset = num_bytes;
  2863. ret = btrfs_search_slot(trans, extent_root, &key, path,
  2864. -1, 1);
  2865. if (ret) {
  2866. printk(KERN_ERR "umm, got %d back from search"
  2867. ", was looking for %llu\n", ret,
  2868. (unsigned long long)bytenr);
  2869. btrfs_print_leaf(extent_root, path->nodes[0]);
  2870. }
  2871. BUG_ON(ret);
  2872. extent_slot = path->slots[0];
  2873. leaf = path->nodes[0];
  2874. item_size = btrfs_item_size_nr(leaf, extent_slot);
  2875. }
  2876. #endif
  2877. BUG_ON(item_size < sizeof(*ei));
  2878. ei = btrfs_item_ptr(leaf, extent_slot,
  2879. struct btrfs_extent_item);
  2880. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  2881. struct btrfs_tree_block_info *bi;
  2882. BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
  2883. bi = (struct btrfs_tree_block_info *)(ei + 1);
  2884. WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
  2885. }
  2886. refs = btrfs_extent_refs(leaf, ei);
  2887. BUG_ON(refs < refs_to_drop);
  2888. refs -= refs_to_drop;
  2889. if (refs > 0) {
  2890. if (extent_op)
  2891. __run_delayed_extent_op(extent_op, leaf, ei);
  2892. /*
  2893. * In the case of inline back ref, reference count will
  2894. * be updated by remove_extent_backref
  2895. */
  2896. if (iref) {
  2897. BUG_ON(!found_extent);
  2898. } else {
  2899. btrfs_set_extent_refs(leaf, ei, refs);
  2900. btrfs_mark_buffer_dirty(leaf);
  2901. }
  2902. if (found_extent) {
  2903. ret = remove_extent_backref(trans, extent_root, path,
  2904. iref, refs_to_drop,
  2905. is_data);
  2906. BUG_ON(ret);
  2907. }
  2908. } else {
  2909. int mark_free = 0;
  2910. struct extent_buffer *must_clean = NULL;
  2911. if (found_extent) {
  2912. BUG_ON(is_data && refs_to_drop !=
  2913. extent_data_ref_count(root, path, iref));
  2914. if (iref) {
  2915. BUG_ON(path->slots[0] != extent_slot);
  2916. } else {
  2917. BUG_ON(path->slots[0] != extent_slot + 1);
  2918. path->slots[0] = extent_slot;
  2919. num_to_del = 2;
  2920. }
  2921. }
  2922. ret = pin_down_bytes(trans, root, path, bytenr,
  2923. num_bytes, is_data, &must_clean);
  2924. if (ret > 0)
  2925. mark_free = 1;
  2926. BUG_ON(ret < 0);
  2927. /*
  2928. * it is going to be very rare for someone to be waiting
  2929. * on the block we're freeing. del_items might need to
  2930. * schedule, so rather than get fancy, just force it
  2931. * to blocking here
  2932. */
  2933. if (must_clean)
  2934. btrfs_set_lock_blocking(must_clean);
  2935. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  2936. num_to_del);
  2937. BUG_ON(ret);
  2938. btrfs_release_path(extent_root, path);
  2939. if (must_clean) {
  2940. clean_tree_block(NULL, root, must_clean);
  2941. btrfs_tree_unlock(must_clean);
  2942. free_extent_buffer(must_clean);
  2943. }
  2944. if (is_data) {
  2945. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  2946. BUG_ON(ret);
  2947. } else {
  2948. invalidate_mapping_pages(info->btree_inode->i_mapping,
  2949. bytenr >> PAGE_CACHE_SHIFT,
  2950. (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
  2951. }
  2952. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  2953. mark_free);
  2954. BUG_ON(ret);
  2955. }
  2956. btrfs_free_path(path);
  2957. return ret;
  2958. }
  2959. /*
  2960. * when we free an extent, it is possible (and likely) that we free the last
  2961. * delayed ref for that extent as well. This searches the delayed ref tree for
  2962. * a given extent, and if there are no other delayed refs to be processed, it
  2963. * removes it from the tree.
  2964. */
  2965. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  2966. struct btrfs_root *root, u64 bytenr)
  2967. {
  2968. struct btrfs_delayed_ref_head *head;
  2969. struct btrfs_delayed_ref_root *delayed_refs;
  2970. struct btrfs_delayed_ref_node *ref;
  2971. struct rb_node *node;
  2972. int ret;
  2973. delayed_refs = &trans->transaction->delayed_refs;
  2974. spin_lock(&delayed_refs->lock);
  2975. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2976. if (!head)
  2977. goto out;
  2978. node = rb_prev(&head->node.rb_node);
  2979. if (!node)
  2980. goto out;
  2981. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2982. /* there are still entries for this ref, we can't drop it */
  2983. if (ref->bytenr == bytenr)
  2984. goto out;
  2985. if (head->extent_op) {
  2986. if (!head->must_insert_reserved)
  2987. goto out;
  2988. kfree(head->extent_op);
  2989. head->extent_op = NULL;
  2990. }
  2991. /*
  2992. * waiting for the lock here would deadlock. If someone else has it
  2993. * locked they are already in the process of dropping it anyway
  2994. */
  2995. if (!mutex_trylock(&head->mutex))
  2996. goto out;
  2997. /*
  2998. * at this point we have a head with no other entries. Go
  2999. * ahead and process it.
  3000. */
  3001. head->node.in_tree = 0;
  3002. rb_erase(&head->node.rb_node, &delayed_refs->root);
  3003. delayed_refs->num_entries--;
  3004. /*
  3005. * we don't take a ref on the node because we're removing it from the
  3006. * tree, so we just steal the ref the tree was holding.
  3007. */
  3008. delayed_refs->num_heads--;
  3009. if (list_empty(&head->cluster))
  3010. delayed_refs->num_heads_ready--;
  3011. list_del_init(&head->cluster);
  3012. spin_unlock(&delayed_refs->lock);
  3013. ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
  3014. &head->node, head->extent_op,
  3015. head->must_insert_reserved);
  3016. BUG_ON(ret);
  3017. btrfs_put_delayed_ref(&head->node);
  3018. return 0;
  3019. out:
  3020. spin_unlock(&delayed_refs->lock);
  3021. return 0;
  3022. }
  3023. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  3024. struct btrfs_root *root,
  3025. u64 bytenr, u64 num_bytes, u64 parent,
  3026. u64 root_objectid, u64 owner, u64 offset)
  3027. {
  3028. int ret;
  3029. /*
  3030. * tree log blocks never actually go into the extent allocation
  3031. * tree, just update pinning info and exit early.
  3032. */
  3033. if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
  3034. WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
  3035. /* unlocks the pinned mutex */
  3036. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  3037. update_reserved_extents(root, bytenr, num_bytes, 0);
  3038. ret = 0;
  3039. } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
  3040. ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
  3041. parent, root_objectid, (int)owner,
  3042. BTRFS_DROP_DELAYED_REF, NULL);
  3043. BUG_ON(ret);
  3044. ret = check_ref_cleanup(trans, root, bytenr);
  3045. BUG_ON(ret);
  3046. } else {
  3047. ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
  3048. parent, root_objectid, owner,
  3049. offset, BTRFS_DROP_DELAYED_REF, NULL);
  3050. BUG_ON(ret);
  3051. }
  3052. return ret;
  3053. }
  3054. static u64 stripe_align(struct btrfs_root *root, u64 val)
  3055. {
  3056. u64 mask = ((u64)root->stripesize - 1);
  3057. u64 ret = (val + mask) & ~mask;
  3058. return ret;
  3059. }
  3060. /*
  3061. * walks the btree of allocated extents and find a hole of a given size.
  3062. * The key ins is changed to record the hole:
  3063. * ins->objectid == block start
  3064. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  3065. * ins->offset == number of blocks
  3066. * Any available blocks before search_start are skipped.
  3067. */
  3068. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  3069. struct btrfs_root *orig_root,
  3070. u64 num_bytes, u64 empty_size,
  3071. u64 search_start, u64 search_end,
  3072. u64 hint_byte, struct btrfs_key *ins,
  3073. u64 exclude_start, u64 exclude_nr,
  3074. int data)
  3075. {
  3076. int ret = 0;
  3077. struct btrfs_root *root = orig_root->fs_info->extent_root;
  3078. struct btrfs_free_cluster *last_ptr = NULL;
  3079. struct btrfs_block_group_cache *block_group = NULL;
  3080. int empty_cluster = 2 * 1024 * 1024;
  3081. int allowed_chunk_alloc = 0;
  3082. struct btrfs_space_info *space_info;
  3083. int last_ptr_loop = 0;
  3084. int loop = 0;
  3085. WARN_ON(num_bytes < root->sectorsize);
  3086. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  3087. ins->objectid = 0;
  3088. ins->offset = 0;
  3089. space_info = __find_space_info(root->fs_info, data);
  3090. if (orig_root->ref_cows || empty_size)
  3091. allowed_chunk_alloc = 1;
  3092. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  3093. last_ptr = &root->fs_info->meta_alloc_cluster;
  3094. if (!btrfs_test_opt(root, SSD))
  3095. empty_cluster = 64 * 1024;
  3096. }
  3097. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
  3098. last_ptr = &root->fs_info->data_alloc_cluster;
  3099. }
  3100. if (last_ptr) {
  3101. spin_lock(&last_ptr->lock);
  3102. if (last_ptr->block_group)
  3103. hint_byte = last_ptr->window_start;
  3104. spin_unlock(&last_ptr->lock);
  3105. }
  3106. search_start = max(search_start, first_logical_byte(root, 0));
  3107. search_start = max(search_start, hint_byte);
  3108. if (!last_ptr) {
  3109. empty_cluster = 0;
  3110. loop = 1;
  3111. }
  3112. if (search_start == hint_byte) {
  3113. block_group = btrfs_lookup_block_group(root->fs_info,
  3114. search_start);
  3115. if (block_group && block_group_bits(block_group, data)) {
  3116. down_read(&space_info->groups_sem);
  3117. if (list_empty(&block_group->list) ||
  3118. block_group->ro) {
  3119. /*
  3120. * someone is removing this block group,
  3121. * we can't jump into the have_block_group
  3122. * target because our list pointers are not
  3123. * valid
  3124. */
  3125. btrfs_put_block_group(block_group);
  3126. up_read(&space_info->groups_sem);
  3127. } else
  3128. goto have_block_group;
  3129. } else if (block_group) {
  3130. btrfs_put_block_group(block_group);
  3131. }
  3132. }
  3133. search:
  3134. down_read(&space_info->groups_sem);
  3135. list_for_each_entry(block_group, &space_info->block_groups, list) {
  3136. u64 offset;
  3137. atomic_inc(&block_group->count);
  3138. search_start = block_group->key.objectid;
  3139. have_block_group:
  3140. if (unlikely(!block_group->cached)) {
  3141. mutex_lock(&block_group->cache_mutex);
  3142. ret = cache_block_group(root, block_group);
  3143. mutex_unlock(&block_group->cache_mutex);
  3144. if (ret) {
  3145. btrfs_put_block_group(block_group);
  3146. break;
  3147. }
  3148. }
  3149. if (unlikely(block_group->ro))
  3150. goto loop;
  3151. if (last_ptr) {
  3152. /*
  3153. * the refill lock keeps out other
  3154. * people trying to start a new cluster
  3155. */
  3156. spin_lock(&last_ptr->refill_lock);
  3157. if (last_ptr->block_group &&
  3158. (last_ptr->block_group->ro ||
  3159. !block_group_bits(last_ptr->block_group, data))) {
  3160. offset = 0;
  3161. goto refill_cluster;
  3162. }
  3163. offset = btrfs_alloc_from_cluster(block_group, last_ptr,
  3164. num_bytes, search_start);
  3165. if (offset) {
  3166. /* we have a block, we're done */
  3167. spin_unlock(&last_ptr->refill_lock);
  3168. goto checks;
  3169. }
  3170. spin_lock(&last_ptr->lock);
  3171. /*
  3172. * whoops, this cluster doesn't actually point to
  3173. * this block group. Get a ref on the block
  3174. * group is does point to and try again
  3175. */
  3176. if (!last_ptr_loop && last_ptr->block_group &&
  3177. last_ptr->block_group != block_group) {
  3178. btrfs_put_block_group(block_group);
  3179. block_group = last_ptr->block_group;
  3180. atomic_inc(&block_group->count);
  3181. spin_unlock(&last_ptr->lock);
  3182. spin_unlock(&last_ptr->refill_lock);
  3183. last_ptr_loop = 1;
  3184. search_start = block_group->key.objectid;
  3185. /*
  3186. * we know this block group is properly
  3187. * in the list because
  3188. * btrfs_remove_block_group, drops the
  3189. * cluster before it removes the block
  3190. * group from the list
  3191. */
  3192. goto have_block_group;
  3193. }
  3194. spin_unlock(&last_ptr->lock);
  3195. refill_cluster:
  3196. /*
  3197. * this cluster didn't work out, free it and
  3198. * start over
  3199. */
  3200. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  3201. last_ptr_loop = 0;
  3202. /* allocate a cluster in this block group */
  3203. ret = btrfs_find_space_cluster(trans, root,
  3204. block_group, last_ptr,
  3205. offset, num_bytes,
  3206. empty_cluster + empty_size);
  3207. if (ret == 0) {
  3208. /*
  3209. * now pull our allocation out of this
  3210. * cluster
  3211. */
  3212. offset = btrfs_alloc_from_cluster(block_group,
  3213. last_ptr, num_bytes,
  3214. search_start);
  3215. if (offset) {
  3216. /* we found one, proceed */
  3217. spin_unlock(&last_ptr->refill_lock);
  3218. goto checks;
  3219. }
  3220. }
  3221. /*
  3222. * at this point we either didn't find a cluster
  3223. * or we weren't able to allocate a block from our
  3224. * cluster. Free the cluster we've been trying
  3225. * to use, and go to the next block group
  3226. */
  3227. if (loop < 2) {
  3228. btrfs_return_cluster_to_free_space(NULL,
  3229. last_ptr);
  3230. spin_unlock(&last_ptr->refill_lock);
  3231. goto loop;
  3232. }
  3233. spin_unlock(&last_ptr->refill_lock);
  3234. }
  3235. offset = btrfs_find_space_for_alloc(block_group, search_start,
  3236. num_bytes, empty_size);
  3237. if (!offset)
  3238. goto loop;
  3239. checks:
  3240. search_start = stripe_align(root, offset);
  3241. /* move on to the next group */
  3242. if (search_start + num_bytes >= search_end) {
  3243. btrfs_add_free_space(block_group, offset, num_bytes);
  3244. goto loop;
  3245. }
  3246. /* move on to the next group */
  3247. if (search_start + num_bytes >
  3248. block_group->key.objectid + block_group->key.offset) {
  3249. btrfs_add_free_space(block_group, offset, num_bytes);
  3250. goto loop;
  3251. }
  3252. if (exclude_nr > 0 &&
  3253. (search_start + num_bytes > exclude_start &&
  3254. search_start < exclude_start + exclude_nr)) {
  3255. search_start = exclude_start + exclude_nr;
  3256. btrfs_add_free_space(block_group, offset, num_bytes);
  3257. /*
  3258. * if search_start is still in this block group
  3259. * then we just re-search this block group
  3260. */
  3261. if (search_start >= block_group->key.objectid &&
  3262. search_start < (block_group->key.objectid +
  3263. block_group->key.offset))
  3264. goto have_block_group;
  3265. goto loop;
  3266. }
  3267. ins->objectid = search_start;
  3268. ins->offset = num_bytes;
  3269. if (offset < search_start)
  3270. btrfs_add_free_space(block_group, offset,
  3271. search_start - offset);
  3272. BUG_ON(offset > search_start);
  3273. /* we are all good, lets return */
  3274. break;
  3275. loop:
  3276. btrfs_put_block_group(block_group);
  3277. }
  3278. up_read(&space_info->groups_sem);
  3279. /* loop == 0, try to find a clustered alloc in every block group
  3280. * loop == 1, try again after forcing a chunk allocation
  3281. * loop == 2, set empty_size and empty_cluster to 0 and try again
  3282. */
  3283. if (!ins->objectid && loop < 3 &&
  3284. (empty_size || empty_cluster || allowed_chunk_alloc)) {
  3285. if (loop >= 2) {
  3286. empty_size = 0;
  3287. empty_cluster = 0;
  3288. }
  3289. if (allowed_chunk_alloc) {
  3290. ret = do_chunk_alloc(trans, root, num_bytes +
  3291. 2 * 1024 * 1024, data, 1);
  3292. allowed_chunk_alloc = 0;
  3293. } else {
  3294. space_info->force_alloc = 1;
  3295. }
  3296. if (loop < 3) {
  3297. loop++;
  3298. goto search;
  3299. }
  3300. ret = -ENOSPC;
  3301. } else if (!ins->objectid) {
  3302. ret = -ENOSPC;
  3303. }
  3304. /* we found what we needed */
  3305. if (ins->objectid) {
  3306. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  3307. trans->block_group = block_group->key.objectid;
  3308. btrfs_put_block_group(block_group);
  3309. ret = 0;
  3310. }
  3311. return ret;
  3312. }
  3313. static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
  3314. {
  3315. struct btrfs_block_group_cache *cache;
  3316. printk(KERN_INFO "space_info has %llu free, is %sfull\n",
  3317. (unsigned long long)(info->total_bytes - info->bytes_used -
  3318. info->bytes_pinned - info->bytes_reserved),
  3319. (info->full) ? "" : "not ");
  3320. printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
  3321. " may_use=%llu, used=%llu\n",
  3322. (unsigned long long)info->total_bytes,
  3323. (unsigned long long)info->bytes_pinned,
  3324. (unsigned long long)info->bytes_delalloc,
  3325. (unsigned long long)info->bytes_may_use,
  3326. (unsigned long long)info->bytes_used);
  3327. down_read(&info->groups_sem);
  3328. list_for_each_entry(cache, &info->block_groups, list) {
  3329. spin_lock(&cache->lock);
  3330. printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
  3331. "%llu pinned %llu reserved\n",
  3332. (unsigned long long)cache->key.objectid,
  3333. (unsigned long long)cache->key.offset,
  3334. (unsigned long long)btrfs_block_group_used(&cache->item),
  3335. (unsigned long long)cache->pinned,
  3336. (unsigned long long)cache->reserved);
  3337. btrfs_dump_free_space(cache, bytes);
  3338. spin_unlock(&cache->lock);
  3339. }
  3340. up_read(&info->groups_sem);
  3341. }
  3342. static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  3343. struct btrfs_root *root,
  3344. u64 num_bytes, u64 min_alloc_size,
  3345. u64 empty_size, u64 hint_byte,
  3346. u64 search_end, struct btrfs_key *ins,
  3347. u64 data)
  3348. {
  3349. int ret;
  3350. u64 search_start = 0;
  3351. struct btrfs_fs_info *info = root->fs_info;
  3352. data = btrfs_get_alloc_profile(root, data);
  3353. again:
  3354. /*
  3355. * the only place that sets empty_size is btrfs_realloc_node, which
  3356. * is not called recursively on allocations
  3357. */
  3358. if (empty_size || root->ref_cows) {
  3359. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  3360. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3361. 2 * 1024 * 1024,
  3362. BTRFS_BLOCK_GROUP_METADATA |
  3363. (info->metadata_alloc_profile &
  3364. info->avail_metadata_alloc_bits), 0);
  3365. }
  3366. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  3367. num_bytes + 2 * 1024 * 1024, data, 0);
  3368. }
  3369. WARN_ON(num_bytes < root->sectorsize);
  3370. ret = find_free_extent(trans, root, num_bytes, empty_size,
  3371. search_start, search_end, hint_byte, ins,
  3372. trans->alloc_exclude_start,
  3373. trans->alloc_exclude_nr, data);
  3374. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  3375. num_bytes = num_bytes >> 1;
  3376. num_bytes = num_bytes & ~(root->sectorsize - 1);
  3377. num_bytes = max(num_bytes, min_alloc_size);
  3378. do_chunk_alloc(trans, root->fs_info->extent_root,
  3379. num_bytes, data, 1);
  3380. goto again;
  3381. }
  3382. if (ret) {
  3383. struct btrfs_space_info *sinfo;
  3384. sinfo = __find_space_info(root->fs_info, data);
  3385. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  3386. "wanted %llu\n", (unsigned long long)data,
  3387. (unsigned long long)num_bytes);
  3388. dump_space_info(sinfo, num_bytes);
  3389. BUG();
  3390. }
  3391. return ret;
  3392. }
  3393. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  3394. {
  3395. struct btrfs_block_group_cache *cache;
  3396. int ret = 0;
  3397. cache = btrfs_lookup_block_group(root->fs_info, start);
  3398. if (!cache) {
  3399. printk(KERN_ERR "Unable to find block group for %llu\n",
  3400. (unsigned long long)start);
  3401. return -ENOSPC;
  3402. }
  3403. ret = btrfs_discard_extent(root, start, len);
  3404. btrfs_add_free_space(cache, start, len);
  3405. btrfs_put_block_group(cache);
  3406. update_reserved_extents(root, start, len, 0);
  3407. return ret;
  3408. }
  3409. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  3410. struct btrfs_root *root,
  3411. u64 num_bytes, u64 min_alloc_size,
  3412. u64 empty_size, u64 hint_byte,
  3413. u64 search_end, struct btrfs_key *ins,
  3414. u64 data)
  3415. {
  3416. int ret;
  3417. ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
  3418. empty_size, hint_byte, search_end, ins,
  3419. data);
  3420. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  3421. return ret;
  3422. }
  3423. static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  3424. struct btrfs_root *root,
  3425. u64 parent, u64 root_objectid,
  3426. u64 flags, u64 owner, u64 offset,
  3427. struct btrfs_key *ins, int ref_mod)
  3428. {
  3429. int ret;
  3430. struct btrfs_fs_info *fs_info = root->fs_info;
  3431. struct btrfs_extent_item *extent_item;
  3432. struct btrfs_extent_inline_ref *iref;
  3433. struct btrfs_path *path;
  3434. struct extent_buffer *leaf;
  3435. int type;
  3436. u32 size;
  3437. if (parent > 0)
  3438. type = BTRFS_SHARED_DATA_REF_KEY;
  3439. else
  3440. type = BTRFS_EXTENT_DATA_REF_KEY;
  3441. size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
  3442. path = btrfs_alloc_path();
  3443. BUG_ON(!path);
  3444. path->leave_spinning = 1;
  3445. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  3446. ins, size);
  3447. BUG_ON(ret);
  3448. leaf = path->nodes[0];
  3449. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  3450. struct btrfs_extent_item);
  3451. btrfs_set_extent_refs(leaf, extent_item, ref_mod);
  3452. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  3453. btrfs_set_extent_flags(leaf, extent_item,
  3454. flags | BTRFS_EXTENT_FLAG_DATA);
  3455. iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
  3456. btrfs_set_extent_inline_ref_type(leaf, iref, type);
  3457. if (parent > 0) {
  3458. struct btrfs_shared_data_ref *ref;
  3459. ref = (struct btrfs_shared_data_ref *)(iref + 1);
  3460. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  3461. btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
  3462. } else {
  3463. struct btrfs_extent_data_ref *ref;
  3464. ref = (struct btrfs_extent_data_ref *)(&iref->offset);
  3465. btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
  3466. btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
  3467. btrfs_set_extent_data_ref_offset(leaf, ref, offset);
  3468. btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
  3469. }
  3470. btrfs_mark_buffer_dirty(path->nodes[0]);
  3471. btrfs_free_path(path);
  3472. ret = update_block_group(trans, root, ins->objectid, ins->offset,
  3473. 1, 0);
  3474. if (ret) {
  3475. printk(KERN_ERR "btrfs update block group failed for %llu "
  3476. "%llu\n", (unsigned long long)ins->objectid,
  3477. (unsigned long long)ins->offset);
  3478. BUG();
  3479. }
  3480. return ret;
  3481. }
  3482. static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
  3483. struct btrfs_root *root,
  3484. u64 parent, u64 root_objectid,
  3485. u64 flags, struct btrfs_disk_key *key,
  3486. int level, struct btrfs_key *ins)
  3487. {
  3488. int ret;
  3489. struct btrfs_fs_info *fs_info = root->fs_info;
  3490. struct btrfs_extent_item *extent_item;
  3491. struct btrfs_tree_block_info *block_info;
  3492. struct btrfs_extent_inline_ref *iref;
  3493. struct btrfs_path *path;
  3494. struct extent_buffer *leaf;
  3495. u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
  3496. path = btrfs_alloc_path();
  3497. BUG_ON(!path);
  3498. path->leave_spinning = 1;
  3499. ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
  3500. ins, size);
  3501. BUG_ON(ret);
  3502. leaf = path->nodes[0];
  3503. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  3504. struct btrfs_extent_item);
  3505. btrfs_set_extent_refs(leaf, extent_item, 1);
  3506. btrfs_set_extent_generation(leaf, extent_item, trans->transid);
  3507. btrfs_set_extent_flags(leaf, extent_item,
  3508. flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
  3509. block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
  3510. btrfs_set_tree_block_key(leaf, block_info, key);
  3511. btrfs_set_tree_block_level(leaf, block_info, level);
  3512. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  3513. if (parent > 0) {
  3514. BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  3515. btrfs_set_extent_inline_ref_type(leaf, iref,
  3516. BTRFS_SHARED_BLOCK_REF_KEY);
  3517. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  3518. } else {
  3519. btrfs_set_extent_inline_ref_type(leaf, iref,
  3520. BTRFS_TREE_BLOCK_REF_KEY);
  3521. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  3522. }
  3523. btrfs_mark_buffer_dirty(leaf);
  3524. btrfs_free_path(path);
  3525. ret = update_block_group(trans, root, ins->objectid, ins->offset,
  3526. 1, 0);
  3527. if (ret) {
  3528. printk(KERN_ERR "btrfs update block group failed for %llu "
  3529. "%llu\n", (unsigned long long)ins->objectid,
  3530. (unsigned long long)ins->offset);
  3531. BUG();
  3532. }
  3533. return ret;
  3534. }
  3535. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  3536. struct btrfs_root *root,
  3537. u64 root_objectid, u64 owner,
  3538. u64 offset, struct btrfs_key *ins)
  3539. {
  3540. int ret;
  3541. BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
  3542. ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
  3543. 0, root_objectid, owner, offset,
  3544. BTRFS_ADD_DELAYED_EXTENT, NULL);
  3545. return ret;
  3546. }
  3547. /*
  3548. * this is used by the tree logging recovery code. It records that
  3549. * an extent has been allocated and makes sure to clear the free
  3550. * space cache bits as well
  3551. */
  3552. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  3553. struct btrfs_root *root,
  3554. u64 root_objectid, u64 owner, u64 offset,
  3555. struct btrfs_key *ins)
  3556. {
  3557. int ret;
  3558. struct btrfs_block_group_cache *block_group;
  3559. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  3560. mutex_lock(&block_group->cache_mutex);
  3561. cache_block_group(root, block_group);
  3562. mutex_unlock(&block_group->cache_mutex);
  3563. ret = btrfs_remove_free_space(block_group, ins->objectid,
  3564. ins->offset);
  3565. BUG_ON(ret);
  3566. btrfs_put_block_group(block_group);
  3567. ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
  3568. 0, owner, offset, ins, 1);
  3569. return ret;
  3570. }
  3571. /*
  3572. * finds a free extent and does all the dirty work required for allocation
  3573. * returns the key for the extent through ins, and a tree buffer for
  3574. * the first block of the extent through buf.
  3575. *
  3576. * returns 0 if everything worked, non-zero otherwise.
  3577. */
  3578. static int alloc_tree_block(struct btrfs_trans_handle *trans,
  3579. struct btrfs_root *root,
  3580. u64 num_bytes, u64 parent, u64 root_objectid,
  3581. struct btrfs_disk_key *key, int level,
  3582. u64 empty_size, u64 hint_byte, u64 search_end,
  3583. struct btrfs_key *ins)
  3584. {
  3585. int ret;
  3586. u64 flags = 0;
  3587. ret = __btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
  3588. empty_size, hint_byte, search_end,
  3589. ins, 0);
  3590. BUG_ON(ret);
  3591. if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
  3592. if (parent == 0)
  3593. parent = ins->objectid;
  3594. flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  3595. } else
  3596. BUG_ON(parent > 0);
  3597. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  3598. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  3599. struct btrfs_delayed_extent_op *extent_op;
  3600. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  3601. BUG_ON(!extent_op);
  3602. if (key)
  3603. memcpy(&extent_op->key, key, sizeof(extent_op->key));
  3604. else
  3605. memset(&extent_op->key, 0, sizeof(extent_op->key));
  3606. extent_op->flags_to_set = flags;
  3607. extent_op->update_key = 1;
  3608. extent_op->update_flags = 1;
  3609. extent_op->is_data = 0;
  3610. ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
  3611. ins->offset, parent, root_objectid,
  3612. level, BTRFS_ADD_DELAYED_EXTENT,
  3613. extent_op);
  3614. BUG_ON(ret);
  3615. }
  3616. return ret;
  3617. }
  3618. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  3619. struct btrfs_root *root,
  3620. u64 bytenr, u32 blocksize,
  3621. int level)
  3622. {
  3623. struct extent_buffer *buf;
  3624. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  3625. if (!buf)
  3626. return ERR_PTR(-ENOMEM);
  3627. btrfs_set_header_generation(buf, trans->transid);
  3628. btrfs_set_buffer_lockdep_class(buf, level);
  3629. btrfs_tree_lock(buf);
  3630. clean_tree_block(trans, root, buf);
  3631. btrfs_set_lock_blocking(buf);
  3632. btrfs_set_buffer_uptodate(buf);
  3633. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  3634. set_extent_dirty(&root->dirty_log_pages, buf->start,
  3635. buf->start + buf->len - 1, GFP_NOFS);
  3636. } else {
  3637. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  3638. buf->start + buf->len - 1, GFP_NOFS);
  3639. }
  3640. trans->blocks_used++;
  3641. /* this returns a buffer locked for blocking */
  3642. return buf;
  3643. }
  3644. /*
  3645. * helper function to allocate a block for a given tree
  3646. * returns the tree buffer or NULL.
  3647. */
  3648. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  3649. struct btrfs_root *root, u32 blocksize,
  3650. u64 parent, u64 root_objectid,
  3651. struct btrfs_disk_key *key, int level,
  3652. u64 hint, u64 empty_size)
  3653. {
  3654. struct btrfs_key ins;
  3655. int ret;
  3656. struct extent_buffer *buf;
  3657. ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
  3658. key, level, empty_size, hint, (u64)-1, &ins);
  3659. if (ret) {
  3660. BUG_ON(ret > 0);
  3661. return ERR_PTR(ret);
  3662. }
  3663. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  3664. blocksize, level);
  3665. return buf;
  3666. }
  3667. #if 0
  3668. int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
  3669. struct btrfs_root *root, struct extent_buffer *leaf)
  3670. {
  3671. u64 disk_bytenr;
  3672. u64 num_bytes;
  3673. struct btrfs_key key;
  3674. struct btrfs_file_extent_item *fi;
  3675. u32 nritems;
  3676. int i;
  3677. int ret;
  3678. BUG_ON(!btrfs_is_leaf(leaf));
  3679. nritems = btrfs_header_nritems(leaf);
  3680. for (i = 0; i < nritems; i++) {
  3681. cond_resched();
  3682. btrfs_item_key_to_cpu(leaf, &key, i);
  3683. /* only extents have references, skip everything else */
  3684. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  3685. continue;
  3686. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  3687. /* inline extents live in the btree, they don't have refs */
  3688. if (btrfs_file_extent_type(leaf, fi) ==
  3689. BTRFS_FILE_EXTENT_INLINE)
  3690. continue;
  3691. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  3692. /* holes don't have refs */
  3693. if (disk_bytenr == 0)
  3694. continue;
  3695. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  3696. ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes,
  3697. leaf->start, 0, key.objectid, 0);
  3698. BUG_ON(ret);
  3699. }
  3700. return 0;
  3701. }
  3702. static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
  3703. struct btrfs_root *root,
  3704. struct btrfs_leaf_ref *ref)
  3705. {
  3706. int i;
  3707. int ret;
  3708. struct btrfs_extent_info *info;
  3709. struct refsort *sorted;
  3710. if (ref->nritems == 0)
  3711. return 0;
  3712. sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
  3713. for (i = 0; i < ref->nritems; i++) {
  3714. sorted[i].bytenr = ref->extents[i].bytenr;
  3715. sorted[i].slot = i;
  3716. }
  3717. sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
  3718. /*
  3719. * the items in the ref were sorted when the ref was inserted
  3720. * into the ref cache, so this is already in order
  3721. */
  3722. for (i = 0; i < ref->nritems; i++) {
  3723. info = ref->extents + sorted[i].slot;
  3724. ret = btrfs_free_extent(trans, root, info->bytenr,
  3725. info->num_bytes, ref->bytenr,
  3726. ref->owner, ref->generation,
  3727. info->objectid, 0);
  3728. atomic_inc(&root->fs_info->throttle_gen);
  3729. wake_up(&root->fs_info->transaction_throttle);
  3730. cond_resched();
  3731. BUG_ON(ret);
  3732. info++;
  3733. }
  3734. kfree(sorted);
  3735. return 0;
  3736. }
  3737. static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
  3738. struct btrfs_root *root, u64 start,
  3739. u64 len, u32 *refs)
  3740. {
  3741. int ret;
  3742. ret = btrfs_lookup_extent_refs(trans, root, start, len, refs);
  3743. BUG_ON(ret);
  3744. #if 0 /* some debugging code in case we see problems here */
  3745. /* if the refs count is one, it won't get increased again. But
  3746. * if the ref count is > 1, someone may be decreasing it at
  3747. * the same time we are.
  3748. */
  3749. if (*refs != 1) {
  3750. struct extent_buffer *eb = NULL;
  3751. eb = btrfs_find_create_tree_block(root, start, len);
  3752. if (eb)
  3753. btrfs_tree_lock(eb);
  3754. mutex_lock(&root->fs_info->alloc_mutex);
  3755. ret = lookup_extent_ref(NULL, root, start, len, refs);
  3756. BUG_ON(ret);
  3757. mutex_unlock(&root->fs_info->alloc_mutex);
  3758. if (eb) {
  3759. btrfs_tree_unlock(eb);
  3760. free_extent_buffer(eb);
  3761. }
  3762. if (*refs == 1) {
  3763. printk(KERN_ERR "btrfs block %llu went down to one "
  3764. "during drop_snap\n", (unsigned long long)start);
  3765. }
  3766. }
  3767. #endif
  3768. cond_resched();
  3769. return ret;
  3770. }
  3771. /*
  3772. * this is used while deleting old snapshots, and it drops the refs
  3773. * on a whole subtree starting from a level 1 node.
  3774. *
  3775. * The idea is to sort all the leaf pointers, and then drop the
  3776. * ref on all the leaves in order. Most of the time the leaves
  3777. * will have ref cache entries, so no leaf IOs will be required to
  3778. * find the extents they have references on.
  3779. *
  3780. * For each leaf, any references it has are also dropped in order
  3781. *
  3782. * This ends up dropping the references in something close to optimal
  3783. * order for reading and modifying the extent allocation tree.
  3784. */
  3785. static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
  3786. struct btrfs_root *root,
  3787. struct btrfs_path *path)
  3788. {
  3789. u64 bytenr;
  3790. u64 root_owner;
  3791. u64 root_gen;
  3792. struct extent_buffer *eb = path->nodes[1];
  3793. struct extent_buffer *leaf;
  3794. struct btrfs_leaf_ref *ref;
  3795. struct refsort *sorted = NULL;
  3796. int nritems = btrfs_header_nritems(eb);
  3797. int ret;
  3798. int i;
  3799. int refi = 0;
  3800. int slot = path->slots[1];
  3801. u32 blocksize = btrfs_level_size(root, 0);
  3802. u32 refs;
  3803. if (nritems == 0)
  3804. goto out;
  3805. root_owner = btrfs_header_owner(eb);
  3806. root_gen = btrfs_header_generation(eb);
  3807. sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
  3808. /*
  3809. * step one, sort all the leaf pointers so we don't scribble
  3810. * randomly into the extent allocation tree
  3811. */
  3812. for (i = slot; i < nritems; i++) {
  3813. sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
  3814. sorted[refi].slot = i;
  3815. refi++;
  3816. }
  3817. /*
  3818. * nritems won't be zero, but if we're picking up drop_snapshot
  3819. * after a crash, slot might be > 0, so double check things
  3820. * just in case.
  3821. */
  3822. if (refi == 0)
  3823. goto out;
  3824. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  3825. /*
  3826. * the first loop frees everything the leaves point to
  3827. */
  3828. for (i = 0; i < refi; i++) {
  3829. u64 ptr_gen;
  3830. bytenr = sorted[i].bytenr;
  3831. /*
  3832. * check the reference count on this leaf. If it is > 1
  3833. * we just decrement it below and don't update any
  3834. * of the refs the leaf points to.
  3835. */
  3836. ret = drop_snap_lookup_refcount(trans, root, bytenr,
  3837. blocksize, &refs);
  3838. BUG_ON(ret);
  3839. if (refs != 1)
  3840. continue;
  3841. ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
  3842. /*
  3843. * the leaf only had one reference, which means the
  3844. * only thing pointing to this leaf is the snapshot
  3845. * we're deleting. It isn't possible for the reference
  3846. * count to increase again later
  3847. *
  3848. * The reference cache is checked for the leaf,
  3849. * and if found we'll be able to drop any refs held by
  3850. * the leaf without needing to read it in.
  3851. */
  3852. ref = btrfs_lookup_leaf_ref(root, bytenr);
  3853. if (ref && ref->generation != ptr_gen) {
  3854. btrfs_free_leaf_ref(root, ref);
  3855. ref = NULL;
  3856. }
  3857. if (ref) {
  3858. ret = cache_drop_leaf_ref(trans, root, ref);
  3859. BUG_ON(ret);
  3860. btrfs_remove_leaf_ref(root, ref);
  3861. btrfs_free_leaf_ref(root, ref);
  3862. } else {
  3863. /*
  3864. * the leaf wasn't in the reference cache, so
  3865. * we have to read it.
  3866. */
  3867. leaf = read_tree_block(root, bytenr, blocksize,
  3868. ptr_gen);
  3869. ret = btrfs_drop_leaf_ref(trans, root, leaf);
  3870. BUG_ON(ret);
  3871. free_extent_buffer(leaf);
  3872. }
  3873. atomic_inc(&root->fs_info->throttle_gen);
  3874. wake_up(&root->fs_info->transaction_throttle);
  3875. cond_resched();
  3876. }
  3877. /*
  3878. * run through the loop again to free the refs on the leaves.
  3879. * This is faster than doing it in the loop above because
  3880. * the leaves are likely to be clustered together. We end up
  3881. * working in nice chunks on the extent allocation tree.
  3882. */
  3883. for (i = 0; i < refi; i++) {
  3884. bytenr = sorted[i].bytenr;
  3885. ret = btrfs_free_extent(trans, root, bytenr,
  3886. blocksize, eb->start,
  3887. root_owner, root_gen, 0, 1);
  3888. BUG_ON(ret);
  3889. atomic_inc(&root->fs_info->throttle_gen);
  3890. wake_up(&root->fs_info->transaction_throttle);
  3891. cond_resched();
  3892. }
  3893. out:
  3894. kfree(sorted);
  3895. /*
  3896. * update the path to show we've processed the entire level 1
  3897. * node. This will get saved into the root's drop_snapshot_progress
  3898. * field so these drops are not repeated again if this transaction
  3899. * commits.
  3900. */
  3901. path->slots[1] = nritems;
  3902. return 0;
  3903. }
  3904. /*
  3905. * helper function for drop_snapshot, this walks down the tree dropping ref
  3906. * counts as it goes.
  3907. */
  3908. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  3909. struct btrfs_root *root,
  3910. struct btrfs_path *path, int *level)
  3911. {
  3912. u64 root_owner;
  3913. u64 root_gen;
  3914. u64 bytenr;
  3915. u64 ptr_gen;
  3916. struct extent_buffer *next;
  3917. struct extent_buffer *cur;
  3918. struct extent_buffer *parent;
  3919. u32 blocksize;
  3920. int ret;
  3921. u32 refs;
  3922. WARN_ON(*level < 0);
  3923. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3924. ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
  3925. path->nodes[*level]->len, &refs);
  3926. BUG_ON(ret);
  3927. if (refs > 1)
  3928. goto out;
  3929. /*
  3930. * walk down to the last node level and free all the leaves
  3931. */
  3932. while (*level >= 0) {
  3933. WARN_ON(*level < 0);
  3934. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3935. cur = path->nodes[*level];
  3936. if (btrfs_header_level(cur) != *level)
  3937. WARN_ON(1);
  3938. if (path->slots[*level] >=
  3939. btrfs_header_nritems(cur))
  3940. break;
  3941. /* the new code goes down to level 1 and does all the
  3942. * leaves pointed to that node in bulk. So, this check
  3943. * for level 0 will always be false.
  3944. *
  3945. * But, the disk format allows the drop_snapshot_progress
  3946. * field in the root to leave things in a state where
  3947. * a leaf will need cleaning up here. If someone crashes
  3948. * with the old code and then boots with the new code,
  3949. * we might find a leaf here.
  3950. */
  3951. if (*level == 0) {
  3952. ret = btrfs_drop_leaf_ref(trans, root, cur);
  3953. BUG_ON(ret);
  3954. break;
  3955. }
  3956. /*
  3957. * once we get to level one, process the whole node
  3958. * at once, including everything below it.
  3959. */
  3960. if (*level == 1) {
  3961. ret = drop_level_one_refs(trans, root, path);
  3962. BUG_ON(ret);
  3963. break;
  3964. }
  3965. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  3966. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  3967. blocksize = btrfs_level_size(root, *level - 1);
  3968. ret = drop_snap_lookup_refcount(trans, root, bytenr,
  3969. blocksize, &refs);
  3970. BUG_ON(ret);
  3971. /*
  3972. * if there is more than one reference, we don't need
  3973. * to read that node to drop any references it has. We
  3974. * just drop the ref we hold on that node and move on to the
  3975. * next slot in this level.
  3976. */
  3977. if (refs != 1) {
  3978. parent = path->nodes[*level];
  3979. root_owner = btrfs_header_owner(parent);
  3980. root_gen = btrfs_header_generation(parent);
  3981. path->slots[*level]++;
  3982. ret = btrfs_free_extent(trans, root, bytenr,
  3983. blocksize, parent->start,
  3984. root_owner, root_gen,
  3985. *level - 1, 1);
  3986. BUG_ON(ret);
  3987. atomic_inc(&root->fs_info->throttle_gen);
  3988. wake_up(&root->fs_info->transaction_throttle);
  3989. cond_resched();
  3990. continue;
  3991. }
  3992. /*
  3993. * we need to keep freeing things in the next level down.
  3994. * read the block and loop around to process it
  3995. */
  3996. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  3997. WARN_ON(*level <= 0);
  3998. if (path->nodes[*level-1])
  3999. free_extent_buffer(path->nodes[*level-1]);
  4000. path->nodes[*level-1] = next;
  4001. *level = btrfs_header_level(next);
  4002. path->slots[*level] = 0;
  4003. cond_resched();
  4004. }
  4005. out:
  4006. WARN_ON(*level < 0);
  4007. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  4008. if (path->nodes[*level] == root->node) {
  4009. parent = path->nodes[*level];
  4010. bytenr = path->nodes[*level]->start;
  4011. } else {
  4012. parent = path->nodes[*level + 1];
  4013. bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
  4014. }
  4015. blocksize = btrfs_level_size(root, *level);
  4016. root_owner = btrfs_header_owner(parent);
  4017. root_gen = btrfs_header_generation(parent);
  4018. /*
  4019. * cleanup and free the reference on the last node
  4020. * we processed
  4021. */
  4022. ret = btrfs_free_extent(trans, root, bytenr, blocksize,
  4023. parent->start, root_owner, root_gen,
  4024. *level, 1);
  4025. free_extent_buffer(path->nodes[*level]);
  4026. path->nodes[*level] = NULL;
  4027. *level += 1;
  4028. BUG_ON(ret);
  4029. cond_resched();
  4030. return 0;
  4031. }
  4032. #endif
  4033. struct walk_control {
  4034. u64 refs[BTRFS_MAX_LEVEL];
  4035. u64 flags[BTRFS_MAX_LEVEL];
  4036. struct btrfs_key update_progress;
  4037. int stage;
  4038. int level;
  4039. int shared_level;
  4040. int update_ref;
  4041. int keep_locks;
  4042. };
  4043. #define DROP_REFERENCE 1
  4044. #define UPDATE_BACKREF 2
  4045. /*
  4046. * hepler to process tree block while walking down the tree.
  4047. *
  4048. * when wc->stage == DROP_REFERENCE, this function checks
  4049. * reference count of the block. if the block is shared and
  4050. * we need update back refs for the subtree rooted at the
  4051. * block, this function changes wc->stage to UPDATE_BACKREF
  4052. *
  4053. * when wc->stage == UPDATE_BACKREF, this function updates
  4054. * back refs for pointers in the block.
  4055. *
  4056. * NOTE: return value 1 means we should stop walking down.
  4057. */
  4058. static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
  4059. struct btrfs_root *root,
  4060. struct btrfs_path *path,
  4061. struct walk_control *wc)
  4062. {
  4063. int level = wc->level;
  4064. struct extent_buffer *eb = path->nodes[level];
  4065. struct btrfs_key key;
  4066. u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  4067. int ret;
  4068. if (wc->stage == UPDATE_BACKREF &&
  4069. btrfs_header_owner(eb) != root->root_key.objectid)
  4070. return 1;
  4071. /*
  4072. * when reference count of tree block is 1, it won't increase
  4073. * again. once full backref flag is set, we never clear it.
  4074. */
  4075. if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
  4076. (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
  4077. BUG_ON(!path->locks[level]);
  4078. ret = btrfs_lookup_extent_info(trans, root,
  4079. eb->start, eb->len,
  4080. &wc->refs[level],
  4081. &wc->flags[level]);
  4082. BUG_ON(ret);
  4083. BUG_ON(wc->refs[level] == 0);
  4084. }
  4085. if (wc->stage == DROP_REFERENCE &&
  4086. wc->update_ref && wc->refs[level] > 1) {
  4087. BUG_ON(eb == root->node);
  4088. BUG_ON(path->slots[level] > 0);
  4089. if (level == 0)
  4090. btrfs_item_key_to_cpu(eb, &key, path->slots[level]);
  4091. else
  4092. btrfs_node_key_to_cpu(eb, &key, path->slots[level]);
  4093. if (btrfs_header_owner(eb) == root->root_key.objectid &&
  4094. btrfs_comp_cpu_keys(&key, &wc->update_progress) >= 0) {
  4095. wc->stage = UPDATE_BACKREF;
  4096. wc->shared_level = level;
  4097. }
  4098. }
  4099. if (wc->stage == DROP_REFERENCE) {
  4100. if (wc->refs[level] > 1)
  4101. return 1;
  4102. if (path->locks[level] && !wc->keep_locks) {
  4103. btrfs_tree_unlock(eb);
  4104. path->locks[level] = 0;
  4105. }
  4106. return 0;
  4107. }
  4108. /* wc->stage == UPDATE_BACKREF */
  4109. if (!(wc->flags[level] & flag)) {
  4110. BUG_ON(!path->locks[level]);
  4111. ret = btrfs_inc_ref(trans, root, eb, 1);
  4112. BUG_ON(ret);
  4113. ret = btrfs_dec_ref(trans, root, eb, 0);
  4114. BUG_ON(ret);
  4115. ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
  4116. eb->len, flag, 0);
  4117. BUG_ON(ret);
  4118. wc->flags[level] |= flag;
  4119. }
  4120. /*
  4121. * the block is shared by multiple trees, so it's not good to
  4122. * keep the tree lock
  4123. */
  4124. if (path->locks[level] && level > 0) {
  4125. btrfs_tree_unlock(eb);
  4126. path->locks[level] = 0;
  4127. }
  4128. return 0;
  4129. }
  4130. /*
  4131. * hepler to process tree block while walking up the tree.
  4132. *
  4133. * when wc->stage == DROP_REFERENCE, this function drops
  4134. * reference count on the block.
  4135. *
  4136. * when wc->stage == UPDATE_BACKREF, this function changes
  4137. * wc->stage back to DROP_REFERENCE if we changed wc->stage
  4138. * to UPDATE_BACKREF previously while processing the block.
  4139. *
  4140. * NOTE: return value 1 means we should stop walking up.
  4141. */
  4142. static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
  4143. struct btrfs_root *root,
  4144. struct btrfs_path *path,
  4145. struct walk_control *wc)
  4146. {
  4147. int ret = 0;
  4148. int level = wc->level;
  4149. struct extent_buffer *eb = path->nodes[level];
  4150. u64 parent = 0;
  4151. if (wc->stage == UPDATE_BACKREF) {
  4152. BUG_ON(wc->shared_level < level);
  4153. if (level < wc->shared_level)
  4154. goto out;
  4155. BUG_ON(wc->refs[level] <= 1);
  4156. ret = find_next_key(path, level + 1, &wc->update_progress);
  4157. if (ret > 0)
  4158. wc->update_ref = 0;
  4159. wc->stage = DROP_REFERENCE;
  4160. wc->shared_level = -1;
  4161. path->slots[level] = 0;
  4162. /*
  4163. * check reference count again if the block isn't locked.
  4164. * we should start walking down the tree again if reference
  4165. * count is one.
  4166. */
  4167. if (!path->locks[level]) {
  4168. BUG_ON(level == 0);
  4169. btrfs_tree_lock(eb);
  4170. btrfs_set_lock_blocking(eb);
  4171. path->locks[level] = 1;
  4172. ret = btrfs_lookup_extent_info(trans, root,
  4173. eb->start, eb->len,
  4174. &wc->refs[level],
  4175. &wc->flags[level]);
  4176. BUG_ON(ret);
  4177. BUG_ON(wc->refs[level] == 0);
  4178. if (wc->refs[level] == 1) {
  4179. btrfs_tree_unlock(eb);
  4180. path->locks[level] = 0;
  4181. return 1;
  4182. }
  4183. } else {
  4184. BUG_ON(level != 0);
  4185. }
  4186. }
  4187. /* wc->stage == DROP_REFERENCE */
  4188. BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
  4189. if (wc->refs[level] == 1) {
  4190. if (level == 0) {
  4191. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  4192. ret = btrfs_dec_ref(trans, root, eb, 1);
  4193. else
  4194. ret = btrfs_dec_ref(trans, root, eb, 0);
  4195. BUG_ON(ret);
  4196. }
  4197. /* make block locked assertion in clean_tree_block happy */
  4198. if (!path->locks[level] &&
  4199. btrfs_header_generation(eb) == trans->transid) {
  4200. btrfs_tree_lock(eb);
  4201. btrfs_set_lock_blocking(eb);
  4202. path->locks[level] = 1;
  4203. }
  4204. clean_tree_block(trans, root, eb);
  4205. }
  4206. if (eb == root->node) {
  4207. if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  4208. parent = eb->start;
  4209. else
  4210. BUG_ON(root->root_key.objectid !=
  4211. btrfs_header_owner(eb));
  4212. } else {
  4213. if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  4214. parent = path->nodes[level + 1]->start;
  4215. else
  4216. BUG_ON(root->root_key.objectid !=
  4217. btrfs_header_owner(path->nodes[level + 1]));
  4218. }
  4219. ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
  4220. root->root_key.objectid, level, 0);
  4221. BUG_ON(ret);
  4222. out:
  4223. wc->refs[level] = 0;
  4224. wc->flags[level] = 0;
  4225. return ret;
  4226. }
  4227. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  4228. struct btrfs_root *root,
  4229. struct btrfs_path *path,
  4230. struct walk_control *wc)
  4231. {
  4232. struct extent_buffer *next;
  4233. struct extent_buffer *cur;
  4234. u64 bytenr;
  4235. u64 ptr_gen;
  4236. u32 blocksize;
  4237. int level = wc->level;
  4238. int ret;
  4239. while (level >= 0) {
  4240. cur = path->nodes[level];
  4241. BUG_ON(path->slots[level] >= btrfs_header_nritems(cur));
  4242. ret = walk_down_proc(trans, root, path, wc);
  4243. if (ret > 0)
  4244. break;
  4245. if (level == 0)
  4246. break;
  4247. bytenr = btrfs_node_blockptr(cur, path->slots[level]);
  4248. blocksize = btrfs_level_size(root, level - 1);
  4249. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[level]);
  4250. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  4251. btrfs_tree_lock(next);
  4252. btrfs_set_lock_blocking(next);
  4253. level--;
  4254. BUG_ON(level != btrfs_header_level(next));
  4255. path->nodes[level] = next;
  4256. path->slots[level] = 0;
  4257. path->locks[level] = 1;
  4258. wc->level = level;
  4259. }
  4260. return 0;
  4261. }
  4262. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  4263. struct btrfs_root *root,
  4264. struct btrfs_path *path,
  4265. struct walk_control *wc, int max_level)
  4266. {
  4267. int level = wc->level;
  4268. int ret;
  4269. path->slots[level] = btrfs_header_nritems(path->nodes[level]);
  4270. while (level < max_level && path->nodes[level]) {
  4271. wc->level = level;
  4272. if (path->slots[level] + 1 <
  4273. btrfs_header_nritems(path->nodes[level])) {
  4274. path->slots[level]++;
  4275. return 0;
  4276. } else {
  4277. ret = walk_up_proc(trans, root, path, wc);
  4278. if (ret > 0)
  4279. return 0;
  4280. if (path->locks[level]) {
  4281. btrfs_tree_unlock(path->nodes[level]);
  4282. path->locks[level] = 0;
  4283. }
  4284. free_extent_buffer(path->nodes[level]);
  4285. path->nodes[level] = NULL;
  4286. level++;
  4287. }
  4288. }
  4289. return 1;
  4290. }
  4291. /*
  4292. * drop a subvolume tree.
  4293. *
  4294. * this function traverses the tree freeing any blocks that only
  4295. * referenced by the tree.
  4296. *
  4297. * when a shared tree block is found. this function decreases its
  4298. * reference count by one. if update_ref is true, this function
  4299. * also make sure backrefs for the shared block and all lower level
  4300. * blocks are properly updated.
  4301. */
  4302. int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
  4303. {
  4304. struct btrfs_path *path;
  4305. struct btrfs_trans_handle *trans;
  4306. struct btrfs_root *tree_root = root->fs_info->tree_root;
  4307. struct btrfs_root_item *root_item = &root->root_item;
  4308. struct walk_control *wc;
  4309. struct btrfs_key key;
  4310. int err = 0;
  4311. int ret;
  4312. int level;
  4313. path = btrfs_alloc_path();
  4314. BUG_ON(!path);
  4315. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  4316. BUG_ON(!wc);
  4317. trans = btrfs_start_transaction(tree_root, 1);
  4318. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  4319. level = btrfs_header_level(root->node);
  4320. path->nodes[level] = btrfs_lock_root_node(root);
  4321. btrfs_set_lock_blocking(path->nodes[level]);
  4322. path->slots[level] = 0;
  4323. path->locks[level] = 1;
  4324. memset(&wc->update_progress, 0,
  4325. sizeof(wc->update_progress));
  4326. } else {
  4327. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  4328. memcpy(&wc->update_progress, &key,
  4329. sizeof(wc->update_progress));
  4330. level = root_item->drop_level;
  4331. BUG_ON(level == 0);
  4332. path->lowest_level = level;
  4333. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4334. path->lowest_level = 0;
  4335. if (ret < 0) {
  4336. err = ret;
  4337. goto out;
  4338. }
  4339. btrfs_node_key_to_cpu(path->nodes[level], &key,
  4340. path->slots[level]);
  4341. WARN_ON(memcmp(&key, &wc->update_progress, sizeof(key)));
  4342. /*
  4343. * unlock our path, this is safe because only this
  4344. * function is allowed to delete this snapshot
  4345. */
  4346. btrfs_unlock_up_safe(path, 0);
  4347. level = btrfs_header_level(root->node);
  4348. while (1) {
  4349. btrfs_tree_lock(path->nodes[level]);
  4350. btrfs_set_lock_blocking(path->nodes[level]);
  4351. ret = btrfs_lookup_extent_info(trans, root,
  4352. path->nodes[level]->start,
  4353. path->nodes[level]->len,
  4354. &wc->refs[level],
  4355. &wc->flags[level]);
  4356. BUG_ON(ret);
  4357. BUG_ON(wc->refs[level] == 0);
  4358. if (level == root_item->drop_level)
  4359. break;
  4360. btrfs_tree_unlock(path->nodes[level]);
  4361. WARN_ON(wc->refs[level] != 1);
  4362. level--;
  4363. }
  4364. }
  4365. wc->level = level;
  4366. wc->shared_level = -1;
  4367. wc->stage = DROP_REFERENCE;
  4368. wc->update_ref = update_ref;
  4369. wc->keep_locks = 0;
  4370. while (1) {
  4371. ret = walk_down_tree(trans, root, path, wc);
  4372. if (ret < 0) {
  4373. err = ret;
  4374. break;
  4375. }
  4376. ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
  4377. if (ret < 0) {
  4378. err = ret;
  4379. break;
  4380. }
  4381. if (ret > 0) {
  4382. BUG_ON(wc->stage != DROP_REFERENCE);
  4383. break;
  4384. }
  4385. if (wc->stage == DROP_REFERENCE) {
  4386. level = wc->level;
  4387. btrfs_node_key(path->nodes[level],
  4388. &root_item->drop_progress,
  4389. path->slots[level]);
  4390. root_item->drop_level = level;
  4391. }
  4392. BUG_ON(wc->level == 0);
  4393. if (trans->transaction->in_commit ||
  4394. trans->transaction->delayed_refs.flushing) {
  4395. ret = btrfs_update_root(trans, tree_root,
  4396. &root->root_key,
  4397. root_item);
  4398. BUG_ON(ret);
  4399. btrfs_end_transaction(trans, tree_root);
  4400. trans = btrfs_start_transaction(tree_root, 1);
  4401. } else {
  4402. unsigned long update;
  4403. update = trans->delayed_ref_updates;
  4404. trans->delayed_ref_updates = 0;
  4405. if (update)
  4406. btrfs_run_delayed_refs(trans, tree_root,
  4407. update);
  4408. }
  4409. }
  4410. btrfs_release_path(root, path);
  4411. BUG_ON(err);
  4412. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  4413. BUG_ON(ret);
  4414. free_extent_buffer(root->node);
  4415. free_extent_buffer(root->commit_root);
  4416. kfree(root);
  4417. out:
  4418. btrfs_end_transaction(trans, tree_root);
  4419. kfree(wc);
  4420. btrfs_free_path(path);
  4421. return err;
  4422. }
  4423. /*
  4424. * drop subtree rooted at tree block 'node'.
  4425. *
  4426. * NOTE: this function will unlock and release tree block 'node'
  4427. */
  4428. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  4429. struct btrfs_root *root,
  4430. struct extent_buffer *node,
  4431. struct extent_buffer *parent)
  4432. {
  4433. struct btrfs_path *path;
  4434. struct walk_control *wc;
  4435. int level;
  4436. int parent_level;
  4437. int ret = 0;
  4438. int wret;
  4439. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  4440. path = btrfs_alloc_path();
  4441. BUG_ON(!path);
  4442. wc = kzalloc(sizeof(*wc), GFP_NOFS);
  4443. BUG_ON(!wc);
  4444. btrfs_assert_tree_locked(parent);
  4445. parent_level = btrfs_header_level(parent);
  4446. extent_buffer_get(parent);
  4447. path->nodes[parent_level] = parent;
  4448. path->slots[parent_level] = btrfs_header_nritems(parent);
  4449. btrfs_assert_tree_locked(node);
  4450. level = btrfs_header_level(node);
  4451. path->nodes[level] = node;
  4452. path->slots[level] = 0;
  4453. path->locks[level] = 1;
  4454. wc->refs[parent_level] = 1;
  4455. wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  4456. wc->level = level;
  4457. wc->shared_level = -1;
  4458. wc->stage = DROP_REFERENCE;
  4459. wc->update_ref = 0;
  4460. wc->keep_locks = 1;
  4461. while (1) {
  4462. wret = walk_down_tree(trans, root, path, wc);
  4463. if (wret < 0) {
  4464. ret = wret;
  4465. break;
  4466. }
  4467. wret = walk_up_tree(trans, root, path, wc, parent_level);
  4468. if (wret < 0)
  4469. ret = wret;
  4470. if (wret != 0)
  4471. break;
  4472. }
  4473. kfree(wc);
  4474. btrfs_free_path(path);
  4475. return ret;
  4476. }
  4477. #if 0
  4478. static unsigned long calc_ra(unsigned long start, unsigned long last,
  4479. unsigned long nr)
  4480. {
  4481. return min(last, start + nr - 1);
  4482. }
  4483. static noinline int relocate_inode_pages(struct inode *inode, u64 start,
  4484. u64 len)
  4485. {
  4486. u64 page_start;
  4487. u64 page_end;
  4488. unsigned long first_index;
  4489. unsigned long last_index;
  4490. unsigned long i;
  4491. struct page *page;
  4492. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4493. struct file_ra_state *ra;
  4494. struct btrfs_ordered_extent *ordered;
  4495. unsigned int total_read = 0;
  4496. unsigned int total_dirty = 0;
  4497. int ret = 0;
  4498. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  4499. mutex_lock(&inode->i_mutex);
  4500. first_index = start >> PAGE_CACHE_SHIFT;
  4501. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  4502. /* make sure the dirty trick played by the caller work */
  4503. ret = invalidate_inode_pages2_range(inode->i_mapping,
  4504. first_index, last_index);
  4505. if (ret)
  4506. goto out_unlock;
  4507. file_ra_state_init(ra, inode->i_mapping);
  4508. for (i = first_index ; i <= last_index; i++) {
  4509. if (total_read % ra->ra_pages == 0) {
  4510. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  4511. calc_ra(i, last_index, ra->ra_pages));
  4512. }
  4513. total_read++;
  4514. again:
  4515. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  4516. BUG_ON(1);
  4517. page = grab_cache_page(inode->i_mapping, i);
  4518. if (!page) {
  4519. ret = -ENOMEM;
  4520. goto out_unlock;
  4521. }
  4522. if (!PageUptodate(page)) {
  4523. btrfs_readpage(NULL, page);
  4524. lock_page(page);
  4525. if (!PageUptodate(page)) {
  4526. unlock_page(page);
  4527. page_cache_release(page);
  4528. ret = -EIO;
  4529. goto out_unlock;
  4530. }
  4531. }
  4532. wait_on_page_writeback(page);
  4533. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  4534. page_end = page_start + PAGE_CACHE_SIZE - 1;
  4535. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4536. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  4537. if (ordered) {
  4538. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4539. unlock_page(page);
  4540. page_cache_release(page);
  4541. btrfs_start_ordered_extent(inode, ordered, 1);
  4542. btrfs_put_ordered_extent(ordered);
  4543. goto again;
  4544. }
  4545. set_page_extent_mapped(page);
  4546. if (i == first_index)
  4547. set_extent_bits(io_tree, page_start, page_end,
  4548. EXTENT_BOUNDARY, GFP_NOFS);
  4549. btrfs_set_extent_delalloc(inode, page_start, page_end);
  4550. set_page_dirty(page);
  4551. total_dirty++;
  4552. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4553. unlock_page(page);
  4554. page_cache_release(page);
  4555. }
  4556. out_unlock:
  4557. kfree(ra);
  4558. mutex_unlock(&inode->i_mutex);
  4559. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  4560. return ret;
  4561. }
  4562. static noinline int relocate_data_extent(struct inode *reloc_inode,
  4563. struct btrfs_key *extent_key,
  4564. u64 offset)
  4565. {
  4566. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  4567. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  4568. struct extent_map *em;
  4569. u64 start = extent_key->objectid - offset;
  4570. u64 end = start + extent_key->offset - 1;
  4571. em = alloc_extent_map(GFP_NOFS);
  4572. BUG_ON(!em || IS_ERR(em));
  4573. em->start = start;
  4574. em->len = extent_key->offset;
  4575. em->block_len = extent_key->offset;
  4576. em->block_start = extent_key->objectid;
  4577. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4578. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  4579. /* setup extent map to cheat btrfs_readpage */
  4580. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  4581. while (1) {
  4582. int ret;
  4583. spin_lock(&em_tree->lock);
  4584. ret = add_extent_mapping(em_tree, em);
  4585. spin_unlock(&em_tree->lock);
  4586. if (ret != -EEXIST) {
  4587. free_extent_map(em);
  4588. break;
  4589. }
  4590. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  4591. }
  4592. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  4593. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  4594. }
  4595. struct btrfs_ref_path {
  4596. u64 extent_start;
  4597. u64 nodes[BTRFS_MAX_LEVEL];
  4598. u64 root_objectid;
  4599. u64 root_generation;
  4600. u64 owner_objectid;
  4601. u32 num_refs;
  4602. int lowest_level;
  4603. int current_level;
  4604. int shared_level;
  4605. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  4606. u64 new_nodes[BTRFS_MAX_LEVEL];
  4607. };
  4608. struct disk_extent {
  4609. u64 ram_bytes;
  4610. u64 disk_bytenr;
  4611. u64 disk_num_bytes;
  4612. u64 offset;
  4613. u64 num_bytes;
  4614. u8 compression;
  4615. u8 encryption;
  4616. u16 other_encoding;
  4617. };
  4618. static int is_cowonly_root(u64 root_objectid)
  4619. {
  4620. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  4621. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  4622. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  4623. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  4624. root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  4625. root_objectid == BTRFS_CSUM_TREE_OBJECTID)
  4626. return 1;
  4627. return 0;
  4628. }
  4629. static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
  4630. struct btrfs_root *extent_root,
  4631. struct btrfs_ref_path *ref_path,
  4632. int first_time)
  4633. {
  4634. struct extent_buffer *leaf;
  4635. struct btrfs_path *path;
  4636. struct btrfs_extent_ref *ref;
  4637. struct btrfs_key key;
  4638. struct btrfs_key found_key;
  4639. u64 bytenr;
  4640. u32 nritems;
  4641. int level;
  4642. int ret = 1;
  4643. path = btrfs_alloc_path();
  4644. if (!path)
  4645. return -ENOMEM;
  4646. if (first_time) {
  4647. ref_path->lowest_level = -1;
  4648. ref_path->current_level = -1;
  4649. ref_path->shared_level = -1;
  4650. goto walk_up;
  4651. }
  4652. walk_down:
  4653. level = ref_path->current_level - 1;
  4654. while (level >= -1) {
  4655. u64 parent;
  4656. if (level < ref_path->lowest_level)
  4657. break;
  4658. if (level >= 0)
  4659. bytenr = ref_path->nodes[level];
  4660. else
  4661. bytenr = ref_path->extent_start;
  4662. BUG_ON(bytenr == 0);
  4663. parent = ref_path->nodes[level + 1];
  4664. ref_path->nodes[level + 1] = 0;
  4665. ref_path->current_level = level;
  4666. BUG_ON(parent == 0);
  4667. key.objectid = bytenr;
  4668. key.offset = parent + 1;
  4669. key.type = BTRFS_EXTENT_REF_KEY;
  4670. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  4671. if (ret < 0)
  4672. goto out;
  4673. BUG_ON(ret == 0);
  4674. leaf = path->nodes[0];
  4675. nritems = btrfs_header_nritems(leaf);
  4676. if (path->slots[0] >= nritems) {
  4677. ret = btrfs_next_leaf(extent_root, path);
  4678. if (ret < 0)
  4679. goto out;
  4680. if (ret > 0)
  4681. goto next;
  4682. leaf = path->nodes[0];
  4683. }
  4684. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4685. if (found_key.objectid == bytenr &&
  4686. found_key.type == BTRFS_EXTENT_REF_KEY) {
  4687. if (level < ref_path->shared_level)
  4688. ref_path->shared_level = level;
  4689. goto found;
  4690. }
  4691. next:
  4692. level--;
  4693. btrfs_release_path(extent_root, path);
  4694. cond_resched();
  4695. }
  4696. /* reached lowest level */
  4697. ret = 1;
  4698. goto out;
  4699. walk_up:
  4700. level = ref_path->current_level;
  4701. while (level < BTRFS_MAX_LEVEL - 1) {
  4702. u64 ref_objectid;
  4703. if (level >= 0)
  4704. bytenr = ref_path->nodes[level];
  4705. else
  4706. bytenr = ref_path->extent_start;
  4707. BUG_ON(bytenr == 0);
  4708. key.objectid = bytenr;
  4709. key.offset = 0;
  4710. key.type = BTRFS_EXTENT_REF_KEY;
  4711. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  4712. if (ret < 0)
  4713. goto out;
  4714. leaf = path->nodes[0];
  4715. nritems = btrfs_header_nritems(leaf);
  4716. if (path->slots[0] >= nritems) {
  4717. ret = btrfs_next_leaf(extent_root, path);
  4718. if (ret < 0)
  4719. goto out;
  4720. if (ret > 0) {
  4721. /* the extent was freed by someone */
  4722. if (ref_path->lowest_level == level)
  4723. goto out;
  4724. btrfs_release_path(extent_root, path);
  4725. goto walk_down;
  4726. }
  4727. leaf = path->nodes[0];
  4728. }
  4729. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4730. if (found_key.objectid != bytenr ||
  4731. found_key.type != BTRFS_EXTENT_REF_KEY) {
  4732. /* the extent was freed by someone */
  4733. if (ref_path->lowest_level == level) {
  4734. ret = 1;
  4735. goto out;
  4736. }
  4737. btrfs_release_path(extent_root, path);
  4738. goto walk_down;
  4739. }
  4740. found:
  4741. ref = btrfs_item_ptr(leaf, path->slots[0],
  4742. struct btrfs_extent_ref);
  4743. ref_objectid = btrfs_ref_objectid(leaf, ref);
  4744. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4745. if (first_time) {
  4746. level = (int)ref_objectid;
  4747. BUG_ON(level >= BTRFS_MAX_LEVEL);
  4748. ref_path->lowest_level = level;
  4749. ref_path->current_level = level;
  4750. ref_path->nodes[level] = bytenr;
  4751. } else {
  4752. WARN_ON(ref_objectid != level);
  4753. }
  4754. } else {
  4755. WARN_ON(level != -1);
  4756. }
  4757. first_time = 0;
  4758. if (ref_path->lowest_level == level) {
  4759. ref_path->owner_objectid = ref_objectid;
  4760. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  4761. }
  4762. /*
  4763. * the block is tree root or the block isn't in reference
  4764. * counted tree.
  4765. */
  4766. if (found_key.objectid == found_key.offset ||
  4767. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  4768. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  4769. ref_path->root_generation =
  4770. btrfs_ref_generation(leaf, ref);
  4771. if (level < 0) {
  4772. /* special reference from the tree log */
  4773. ref_path->nodes[0] = found_key.offset;
  4774. ref_path->current_level = 0;
  4775. }
  4776. ret = 0;
  4777. goto out;
  4778. }
  4779. level++;
  4780. BUG_ON(ref_path->nodes[level] != 0);
  4781. ref_path->nodes[level] = found_key.offset;
  4782. ref_path->current_level = level;
  4783. /*
  4784. * the reference was created in the running transaction,
  4785. * no need to continue walking up.
  4786. */
  4787. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  4788. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  4789. ref_path->root_generation =
  4790. btrfs_ref_generation(leaf, ref);
  4791. ret = 0;
  4792. goto out;
  4793. }
  4794. btrfs_release_path(extent_root, path);
  4795. cond_resched();
  4796. }
  4797. /* reached max tree level, but no tree root found. */
  4798. BUG();
  4799. out:
  4800. btrfs_free_path(path);
  4801. return ret;
  4802. }
  4803. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  4804. struct btrfs_root *extent_root,
  4805. struct btrfs_ref_path *ref_path,
  4806. u64 extent_start)
  4807. {
  4808. memset(ref_path, 0, sizeof(*ref_path));
  4809. ref_path->extent_start = extent_start;
  4810. return __next_ref_path(trans, extent_root, ref_path, 1);
  4811. }
  4812. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  4813. struct btrfs_root *extent_root,
  4814. struct btrfs_ref_path *ref_path)
  4815. {
  4816. return __next_ref_path(trans, extent_root, ref_path, 0);
  4817. }
  4818. static noinline int get_new_locations(struct inode *reloc_inode,
  4819. struct btrfs_key *extent_key,
  4820. u64 offset, int no_fragment,
  4821. struct disk_extent **extents,
  4822. int *nr_extents)
  4823. {
  4824. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  4825. struct btrfs_path *path;
  4826. struct btrfs_file_extent_item *fi;
  4827. struct extent_buffer *leaf;
  4828. struct disk_extent *exts = *extents;
  4829. struct btrfs_key found_key;
  4830. u64 cur_pos;
  4831. u64 last_byte;
  4832. u32 nritems;
  4833. int nr = 0;
  4834. int max = *nr_extents;
  4835. int ret;
  4836. WARN_ON(!no_fragment && *extents);
  4837. if (!exts) {
  4838. max = 1;
  4839. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  4840. if (!exts)
  4841. return -ENOMEM;
  4842. }
  4843. path = btrfs_alloc_path();
  4844. BUG_ON(!path);
  4845. cur_pos = extent_key->objectid - offset;
  4846. last_byte = extent_key->objectid + extent_key->offset;
  4847. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  4848. cur_pos, 0);
  4849. if (ret < 0)
  4850. goto out;
  4851. if (ret > 0) {
  4852. ret = -ENOENT;
  4853. goto out;
  4854. }
  4855. while (1) {
  4856. leaf = path->nodes[0];
  4857. nritems = btrfs_header_nritems(leaf);
  4858. if (path->slots[0] >= nritems) {
  4859. ret = btrfs_next_leaf(root, path);
  4860. if (ret < 0)
  4861. goto out;
  4862. if (ret > 0)
  4863. break;
  4864. leaf = path->nodes[0];
  4865. }
  4866. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4867. if (found_key.offset != cur_pos ||
  4868. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  4869. found_key.objectid != reloc_inode->i_ino)
  4870. break;
  4871. fi = btrfs_item_ptr(leaf, path->slots[0],
  4872. struct btrfs_file_extent_item);
  4873. if (btrfs_file_extent_type(leaf, fi) !=
  4874. BTRFS_FILE_EXTENT_REG ||
  4875. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  4876. break;
  4877. if (nr == max) {
  4878. struct disk_extent *old = exts;
  4879. max *= 2;
  4880. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  4881. memcpy(exts, old, sizeof(*exts) * nr);
  4882. if (old != *extents)
  4883. kfree(old);
  4884. }
  4885. exts[nr].disk_bytenr =
  4886. btrfs_file_extent_disk_bytenr(leaf, fi);
  4887. exts[nr].disk_num_bytes =
  4888. btrfs_file_extent_disk_num_bytes(leaf, fi);
  4889. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  4890. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4891. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  4892. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  4893. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  4894. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  4895. fi);
  4896. BUG_ON(exts[nr].offset > 0);
  4897. BUG_ON(exts[nr].compression || exts[nr].encryption);
  4898. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  4899. cur_pos += exts[nr].num_bytes;
  4900. nr++;
  4901. if (cur_pos + offset >= last_byte)
  4902. break;
  4903. if (no_fragment) {
  4904. ret = 1;
  4905. goto out;
  4906. }
  4907. path->slots[0]++;
  4908. }
  4909. BUG_ON(cur_pos + offset > last_byte);
  4910. if (cur_pos + offset < last_byte) {
  4911. ret = -ENOENT;
  4912. goto out;
  4913. }
  4914. ret = 0;
  4915. out:
  4916. btrfs_free_path(path);
  4917. if (ret) {
  4918. if (exts != *extents)
  4919. kfree(exts);
  4920. } else {
  4921. *extents = exts;
  4922. *nr_extents = nr;
  4923. }
  4924. return ret;
  4925. }
  4926. static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
  4927. struct btrfs_root *root,
  4928. struct btrfs_path *path,
  4929. struct btrfs_key *extent_key,
  4930. struct btrfs_key *leaf_key,
  4931. struct btrfs_ref_path *ref_path,
  4932. struct disk_extent *new_extents,
  4933. int nr_extents)
  4934. {
  4935. struct extent_buffer *leaf;
  4936. struct btrfs_file_extent_item *fi;
  4937. struct inode *inode = NULL;
  4938. struct btrfs_key key;
  4939. u64 lock_start = 0;
  4940. u64 lock_end = 0;
  4941. u64 num_bytes;
  4942. u64 ext_offset;
  4943. u64 search_end = (u64)-1;
  4944. u32 nritems;
  4945. int nr_scaned = 0;
  4946. int extent_locked = 0;
  4947. int extent_type;
  4948. int ret;
  4949. memcpy(&key, leaf_key, sizeof(key));
  4950. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  4951. if (key.objectid < ref_path->owner_objectid ||
  4952. (key.objectid == ref_path->owner_objectid &&
  4953. key.type < BTRFS_EXTENT_DATA_KEY)) {
  4954. key.objectid = ref_path->owner_objectid;
  4955. key.type = BTRFS_EXTENT_DATA_KEY;
  4956. key.offset = 0;
  4957. }
  4958. }
  4959. while (1) {
  4960. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  4961. if (ret < 0)
  4962. goto out;
  4963. leaf = path->nodes[0];
  4964. nritems = btrfs_header_nritems(leaf);
  4965. next:
  4966. if (extent_locked && ret > 0) {
  4967. /*
  4968. * the file extent item was modified by someone
  4969. * before the extent got locked.
  4970. */
  4971. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4972. lock_end, GFP_NOFS);
  4973. extent_locked = 0;
  4974. }
  4975. if (path->slots[0] >= nritems) {
  4976. if (++nr_scaned > 2)
  4977. break;
  4978. BUG_ON(extent_locked);
  4979. ret = btrfs_next_leaf(root, path);
  4980. if (ret < 0)
  4981. goto out;
  4982. if (ret > 0)
  4983. break;
  4984. leaf = path->nodes[0];
  4985. nritems = btrfs_header_nritems(leaf);
  4986. }
  4987. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4988. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  4989. if ((key.objectid > ref_path->owner_objectid) ||
  4990. (key.objectid == ref_path->owner_objectid &&
  4991. key.type > BTRFS_EXTENT_DATA_KEY) ||
  4992. key.offset >= search_end)
  4993. break;
  4994. }
  4995. if (inode && key.objectid != inode->i_ino) {
  4996. BUG_ON(extent_locked);
  4997. btrfs_release_path(root, path);
  4998. mutex_unlock(&inode->i_mutex);
  4999. iput(inode);
  5000. inode = NULL;
  5001. continue;
  5002. }
  5003. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  5004. path->slots[0]++;
  5005. ret = 1;
  5006. goto next;
  5007. }
  5008. fi = btrfs_item_ptr(leaf, path->slots[0],
  5009. struct btrfs_file_extent_item);
  5010. extent_type = btrfs_file_extent_type(leaf, fi);
  5011. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  5012. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  5013. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  5014. extent_key->objectid)) {
  5015. path->slots[0]++;
  5016. ret = 1;
  5017. goto next;
  5018. }
  5019. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  5020. ext_offset = btrfs_file_extent_offset(leaf, fi);
  5021. if (search_end == (u64)-1) {
  5022. search_end = key.offset - ext_offset +
  5023. btrfs_file_extent_ram_bytes(leaf, fi);
  5024. }
  5025. if (!extent_locked) {
  5026. lock_start = key.offset;
  5027. lock_end = lock_start + num_bytes - 1;
  5028. } else {
  5029. if (lock_start > key.offset ||
  5030. lock_end + 1 < key.offset + num_bytes) {
  5031. unlock_extent(&BTRFS_I(inode)->io_tree,
  5032. lock_start, lock_end, GFP_NOFS);
  5033. extent_locked = 0;
  5034. }
  5035. }
  5036. if (!inode) {
  5037. btrfs_release_path(root, path);
  5038. inode = btrfs_iget_locked(root->fs_info->sb,
  5039. key.objectid, root);
  5040. if (inode->i_state & I_NEW) {
  5041. BTRFS_I(inode)->root = root;
  5042. BTRFS_I(inode)->location.objectid =
  5043. key.objectid;
  5044. BTRFS_I(inode)->location.type =
  5045. BTRFS_INODE_ITEM_KEY;
  5046. BTRFS_I(inode)->location.offset = 0;
  5047. btrfs_read_locked_inode(inode);
  5048. unlock_new_inode(inode);
  5049. }
  5050. /*
  5051. * some code call btrfs_commit_transaction while
  5052. * holding the i_mutex, so we can't use mutex_lock
  5053. * here.
  5054. */
  5055. if (is_bad_inode(inode) ||
  5056. !mutex_trylock(&inode->i_mutex)) {
  5057. iput(inode);
  5058. inode = NULL;
  5059. key.offset = (u64)-1;
  5060. goto skip;
  5061. }
  5062. }
  5063. if (!extent_locked) {
  5064. struct btrfs_ordered_extent *ordered;
  5065. btrfs_release_path(root, path);
  5066. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  5067. lock_end, GFP_NOFS);
  5068. ordered = btrfs_lookup_first_ordered_extent(inode,
  5069. lock_end);
  5070. if (ordered &&
  5071. ordered->file_offset <= lock_end &&
  5072. ordered->file_offset + ordered->len > lock_start) {
  5073. unlock_extent(&BTRFS_I(inode)->io_tree,
  5074. lock_start, lock_end, GFP_NOFS);
  5075. btrfs_start_ordered_extent(inode, ordered, 1);
  5076. btrfs_put_ordered_extent(ordered);
  5077. key.offset += num_bytes;
  5078. goto skip;
  5079. }
  5080. if (ordered)
  5081. btrfs_put_ordered_extent(ordered);
  5082. extent_locked = 1;
  5083. continue;
  5084. }
  5085. if (nr_extents == 1) {
  5086. /* update extent pointer in place */
  5087. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  5088. new_extents[0].disk_bytenr);
  5089. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  5090. new_extents[0].disk_num_bytes);
  5091. btrfs_mark_buffer_dirty(leaf);
  5092. btrfs_drop_extent_cache(inode, key.offset,
  5093. key.offset + num_bytes - 1, 0);
  5094. ret = btrfs_inc_extent_ref(trans, root,
  5095. new_extents[0].disk_bytenr,
  5096. new_extents[0].disk_num_bytes,
  5097. leaf->start,
  5098. root->root_key.objectid,
  5099. trans->transid,
  5100. key.objectid);
  5101. BUG_ON(ret);
  5102. ret = btrfs_free_extent(trans, root,
  5103. extent_key->objectid,
  5104. extent_key->offset,
  5105. leaf->start,
  5106. btrfs_header_owner(leaf),
  5107. btrfs_header_generation(leaf),
  5108. key.objectid, 0);
  5109. BUG_ON(ret);
  5110. btrfs_release_path(root, path);
  5111. key.offset += num_bytes;
  5112. } else {
  5113. BUG_ON(1);
  5114. #if 0
  5115. u64 alloc_hint;
  5116. u64 extent_len;
  5117. int i;
  5118. /*
  5119. * drop old extent pointer at first, then insert the
  5120. * new pointers one bye one
  5121. */
  5122. btrfs_release_path(root, path);
  5123. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  5124. key.offset + num_bytes,
  5125. key.offset, &alloc_hint);
  5126. BUG_ON(ret);
  5127. for (i = 0; i < nr_extents; i++) {
  5128. if (ext_offset >= new_extents[i].num_bytes) {
  5129. ext_offset -= new_extents[i].num_bytes;
  5130. continue;
  5131. }
  5132. extent_len = min(new_extents[i].num_bytes -
  5133. ext_offset, num_bytes);
  5134. ret = btrfs_insert_empty_item(trans, root,
  5135. path, &key,
  5136. sizeof(*fi));
  5137. BUG_ON(ret);
  5138. leaf = path->nodes[0];
  5139. fi = btrfs_item_ptr(leaf, path->slots[0],
  5140. struct btrfs_file_extent_item);
  5141. btrfs_set_file_extent_generation(leaf, fi,
  5142. trans->transid);
  5143. btrfs_set_file_extent_type(leaf, fi,
  5144. BTRFS_FILE_EXTENT_REG);
  5145. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  5146. new_extents[i].disk_bytenr);
  5147. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  5148. new_extents[i].disk_num_bytes);
  5149. btrfs_set_file_extent_ram_bytes(leaf, fi,
  5150. new_extents[i].ram_bytes);
  5151. btrfs_set_file_extent_compression(leaf, fi,
  5152. new_extents[i].compression);
  5153. btrfs_set_file_extent_encryption(leaf, fi,
  5154. new_extents[i].encryption);
  5155. btrfs_set_file_extent_other_encoding(leaf, fi,
  5156. new_extents[i].other_encoding);
  5157. btrfs_set_file_extent_num_bytes(leaf, fi,
  5158. extent_len);
  5159. ext_offset += new_extents[i].offset;
  5160. btrfs_set_file_extent_offset(leaf, fi,
  5161. ext_offset);
  5162. btrfs_mark_buffer_dirty(leaf);
  5163. btrfs_drop_extent_cache(inode, key.offset,
  5164. key.offset + extent_len - 1, 0);
  5165. ret = btrfs_inc_extent_ref(trans, root,
  5166. new_extents[i].disk_bytenr,
  5167. new_extents[i].disk_num_bytes,
  5168. leaf->start,
  5169. root->root_key.objectid,
  5170. trans->transid, key.objectid);
  5171. BUG_ON(ret);
  5172. btrfs_release_path(root, path);
  5173. inode_add_bytes(inode, extent_len);
  5174. ext_offset = 0;
  5175. num_bytes -= extent_len;
  5176. key.offset += extent_len;
  5177. if (num_bytes == 0)
  5178. break;
  5179. }
  5180. BUG_ON(i >= nr_extents);
  5181. #endif
  5182. }
  5183. if (extent_locked) {
  5184. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  5185. lock_end, GFP_NOFS);
  5186. extent_locked = 0;
  5187. }
  5188. skip:
  5189. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  5190. key.offset >= search_end)
  5191. break;
  5192. cond_resched();
  5193. }
  5194. ret = 0;
  5195. out:
  5196. btrfs_release_path(root, path);
  5197. if (inode) {
  5198. mutex_unlock(&inode->i_mutex);
  5199. if (extent_locked) {
  5200. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  5201. lock_end, GFP_NOFS);
  5202. }
  5203. iput(inode);
  5204. }
  5205. return ret;
  5206. }
  5207. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  5208. struct btrfs_root *root,
  5209. struct extent_buffer *buf, u64 orig_start)
  5210. {
  5211. int level;
  5212. int ret;
  5213. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  5214. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  5215. level = btrfs_header_level(buf);
  5216. if (level == 0) {
  5217. struct btrfs_leaf_ref *ref;
  5218. struct btrfs_leaf_ref *orig_ref;
  5219. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  5220. if (!orig_ref)
  5221. return -ENOENT;
  5222. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  5223. if (!ref) {
  5224. btrfs_free_leaf_ref(root, orig_ref);
  5225. return -ENOMEM;
  5226. }
  5227. ref->nritems = orig_ref->nritems;
  5228. memcpy(ref->extents, orig_ref->extents,
  5229. sizeof(ref->extents[0]) * ref->nritems);
  5230. btrfs_free_leaf_ref(root, orig_ref);
  5231. ref->root_gen = trans->transid;
  5232. ref->bytenr = buf->start;
  5233. ref->owner = btrfs_header_owner(buf);
  5234. ref->generation = btrfs_header_generation(buf);
  5235. ret = btrfs_add_leaf_ref(root, ref, 0);
  5236. WARN_ON(ret);
  5237. btrfs_free_leaf_ref(root, ref);
  5238. }
  5239. return 0;
  5240. }
  5241. static noinline int invalidate_extent_cache(struct btrfs_root *root,
  5242. struct extent_buffer *leaf,
  5243. struct btrfs_block_group_cache *group,
  5244. struct btrfs_root *target_root)
  5245. {
  5246. struct btrfs_key key;
  5247. struct inode *inode = NULL;
  5248. struct btrfs_file_extent_item *fi;
  5249. u64 num_bytes;
  5250. u64 skip_objectid = 0;
  5251. u32 nritems;
  5252. u32 i;
  5253. nritems = btrfs_header_nritems(leaf);
  5254. for (i = 0; i < nritems; i++) {
  5255. btrfs_item_key_to_cpu(leaf, &key, i);
  5256. if (key.objectid == skip_objectid ||
  5257. key.type != BTRFS_EXTENT_DATA_KEY)
  5258. continue;
  5259. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  5260. if (btrfs_file_extent_type(leaf, fi) ==
  5261. BTRFS_FILE_EXTENT_INLINE)
  5262. continue;
  5263. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  5264. continue;
  5265. if (!inode || inode->i_ino != key.objectid) {
  5266. iput(inode);
  5267. inode = btrfs_ilookup(target_root->fs_info->sb,
  5268. key.objectid, target_root, 1);
  5269. }
  5270. if (!inode) {
  5271. skip_objectid = key.objectid;
  5272. continue;
  5273. }
  5274. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  5275. lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  5276. key.offset + num_bytes - 1, GFP_NOFS);
  5277. btrfs_drop_extent_cache(inode, key.offset,
  5278. key.offset + num_bytes - 1, 1);
  5279. unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  5280. key.offset + num_bytes - 1, GFP_NOFS);
  5281. cond_resched();
  5282. }
  5283. iput(inode);
  5284. return 0;
  5285. }
  5286. static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  5287. struct btrfs_root *root,
  5288. struct extent_buffer *leaf,
  5289. struct btrfs_block_group_cache *group,
  5290. struct inode *reloc_inode)
  5291. {
  5292. struct btrfs_key key;
  5293. struct btrfs_key extent_key;
  5294. struct btrfs_file_extent_item *fi;
  5295. struct btrfs_leaf_ref *ref;
  5296. struct disk_extent *new_extent;
  5297. u64 bytenr;
  5298. u64 num_bytes;
  5299. u32 nritems;
  5300. u32 i;
  5301. int ext_index;
  5302. int nr_extent;
  5303. int ret;
  5304. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  5305. BUG_ON(!new_extent);
  5306. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  5307. BUG_ON(!ref);
  5308. ext_index = -1;
  5309. nritems = btrfs_header_nritems(leaf);
  5310. for (i = 0; i < nritems; i++) {
  5311. btrfs_item_key_to_cpu(leaf, &key, i);
  5312. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  5313. continue;
  5314. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  5315. if (btrfs_file_extent_type(leaf, fi) ==
  5316. BTRFS_FILE_EXTENT_INLINE)
  5317. continue;
  5318. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  5319. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  5320. if (bytenr == 0)
  5321. continue;
  5322. ext_index++;
  5323. if (bytenr >= group->key.objectid + group->key.offset ||
  5324. bytenr + num_bytes <= group->key.objectid)
  5325. continue;
  5326. extent_key.objectid = bytenr;
  5327. extent_key.offset = num_bytes;
  5328. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  5329. nr_extent = 1;
  5330. ret = get_new_locations(reloc_inode, &extent_key,
  5331. group->key.objectid, 1,
  5332. &new_extent, &nr_extent);
  5333. if (ret > 0)
  5334. continue;
  5335. BUG_ON(ret < 0);
  5336. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  5337. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  5338. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  5339. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  5340. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  5341. new_extent->disk_bytenr);
  5342. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  5343. new_extent->disk_num_bytes);
  5344. btrfs_mark_buffer_dirty(leaf);
  5345. ret = btrfs_inc_extent_ref(trans, root,
  5346. new_extent->disk_bytenr,
  5347. new_extent->disk_num_bytes,
  5348. leaf->start,
  5349. root->root_key.objectid,
  5350. trans->transid, key.objectid);
  5351. BUG_ON(ret);
  5352. ret = btrfs_free_extent(trans, root,
  5353. bytenr, num_bytes, leaf->start,
  5354. btrfs_header_owner(leaf),
  5355. btrfs_header_generation(leaf),
  5356. key.objectid, 0);
  5357. BUG_ON(ret);
  5358. cond_resched();
  5359. }
  5360. kfree(new_extent);
  5361. BUG_ON(ext_index + 1 != ref->nritems);
  5362. btrfs_free_leaf_ref(root, ref);
  5363. return 0;
  5364. }
  5365. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  5366. struct btrfs_root *root)
  5367. {
  5368. struct btrfs_root *reloc_root;
  5369. int ret;
  5370. if (root->reloc_root) {
  5371. reloc_root = root->reloc_root;
  5372. root->reloc_root = NULL;
  5373. list_add(&reloc_root->dead_list,
  5374. &root->fs_info->dead_reloc_roots);
  5375. btrfs_set_root_bytenr(&reloc_root->root_item,
  5376. reloc_root->node->start);
  5377. btrfs_set_root_level(&root->root_item,
  5378. btrfs_header_level(reloc_root->node));
  5379. memset(&reloc_root->root_item.drop_progress, 0,
  5380. sizeof(struct btrfs_disk_key));
  5381. reloc_root->root_item.drop_level = 0;
  5382. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  5383. &reloc_root->root_key,
  5384. &reloc_root->root_item);
  5385. BUG_ON(ret);
  5386. }
  5387. return 0;
  5388. }
  5389. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  5390. {
  5391. struct btrfs_trans_handle *trans;
  5392. struct btrfs_root *reloc_root;
  5393. struct btrfs_root *prev_root = NULL;
  5394. struct list_head dead_roots;
  5395. int ret;
  5396. unsigned long nr;
  5397. INIT_LIST_HEAD(&dead_roots);
  5398. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  5399. while (!list_empty(&dead_roots)) {
  5400. reloc_root = list_entry(dead_roots.prev,
  5401. struct btrfs_root, dead_list);
  5402. list_del_init(&reloc_root->dead_list);
  5403. BUG_ON(reloc_root->commit_root != NULL);
  5404. while (1) {
  5405. trans = btrfs_join_transaction(root, 1);
  5406. BUG_ON(!trans);
  5407. mutex_lock(&root->fs_info->drop_mutex);
  5408. ret = btrfs_drop_snapshot(trans, reloc_root);
  5409. if (ret != -EAGAIN)
  5410. break;
  5411. mutex_unlock(&root->fs_info->drop_mutex);
  5412. nr = trans->blocks_used;
  5413. ret = btrfs_end_transaction(trans, root);
  5414. BUG_ON(ret);
  5415. btrfs_btree_balance_dirty(root, nr);
  5416. }
  5417. free_extent_buffer(reloc_root->node);
  5418. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  5419. &reloc_root->root_key);
  5420. BUG_ON(ret);
  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. kfree(prev_root);
  5427. prev_root = reloc_root;
  5428. }
  5429. if (prev_root) {
  5430. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  5431. kfree(prev_root);
  5432. }
  5433. return 0;
  5434. }
  5435. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  5436. {
  5437. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  5438. return 0;
  5439. }
  5440. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  5441. {
  5442. struct btrfs_root *reloc_root;
  5443. struct btrfs_trans_handle *trans;
  5444. struct btrfs_key location;
  5445. int found;
  5446. int ret;
  5447. mutex_lock(&root->fs_info->tree_reloc_mutex);
  5448. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  5449. BUG_ON(ret);
  5450. found = !list_empty(&root->fs_info->dead_reloc_roots);
  5451. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  5452. if (found) {
  5453. trans = btrfs_start_transaction(root, 1);
  5454. BUG_ON(!trans);
  5455. ret = btrfs_commit_transaction(trans, root);
  5456. BUG_ON(ret);
  5457. }
  5458. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  5459. location.offset = (u64)-1;
  5460. location.type = BTRFS_ROOT_ITEM_KEY;
  5461. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  5462. BUG_ON(!reloc_root);
  5463. btrfs_orphan_cleanup(reloc_root);
  5464. return 0;
  5465. }
  5466. static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
  5467. struct btrfs_root *root)
  5468. {
  5469. struct btrfs_root *reloc_root;
  5470. struct extent_buffer *eb;
  5471. struct btrfs_root_item *root_item;
  5472. struct btrfs_key root_key;
  5473. int ret;
  5474. BUG_ON(!root->ref_cows);
  5475. if (root->reloc_root)
  5476. return 0;
  5477. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  5478. BUG_ON(!root_item);
  5479. ret = btrfs_copy_root(trans, root, root->commit_root,
  5480. &eb, BTRFS_TREE_RELOC_OBJECTID);
  5481. BUG_ON(ret);
  5482. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  5483. root_key.offset = root->root_key.objectid;
  5484. root_key.type = BTRFS_ROOT_ITEM_KEY;
  5485. memcpy(root_item, &root->root_item, sizeof(root_item));
  5486. btrfs_set_root_refs(root_item, 0);
  5487. btrfs_set_root_bytenr(root_item, eb->start);
  5488. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  5489. btrfs_set_root_generation(root_item, trans->transid);
  5490. btrfs_tree_unlock(eb);
  5491. free_extent_buffer(eb);
  5492. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  5493. &root_key, root_item);
  5494. BUG_ON(ret);
  5495. kfree(root_item);
  5496. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  5497. &root_key);
  5498. BUG_ON(!reloc_root);
  5499. reloc_root->last_trans = trans->transid;
  5500. reloc_root->commit_root = NULL;
  5501. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  5502. root->reloc_root = reloc_root;
  5503. return 0;
  5504. }
  5505. /*
  5506. * Core function of space balance.
  5507. *
  5508. * The idea is using reloc trees to relocate tree blocks in reference
  5509. * counted roots. There is one reloc tree for each subvol, and all
  5510. * reloc trees share same root key objectid. Reloc trees are snapshots
  5511. * of the latest committed roots of subvols (root->commit_root).
  5512. *
  5513. * To relocate a tree block referenced by a subvol, there are two steps.
  5514. * COW the block through subvol's reloc tree, then update block pointer
  5515. * in the subvol to point to the new block. Since all reloc trees share
  5516. * same root key objectid, doing special handing for tree blocks owned
  5517. * by them is easy. Once a tree block has been COWed in one reloc tree,
  5518. * we can use the resulting new block directly when the same block is
  5519. * required to COW again through other reloc trees. By this way, relocated
  5520. * tree blocks are shared between reloc trees, so they are also shared
  5521. * between subvols.
  5522. */
  5523. static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
  5524. struct btrfs_root *root,
  5525. struct btrfs_path *path,
  5526. struct btrfs_key *first_key,
  5527. struct btrfs_ref_path *ref_path,
  5528. struct btrfs_block_group_cache *group,
  5529. struct inode *reloc_inode)
  5530. {
  5531. struct btrfs_root *reloc_root;
  5532. struct extent_buffer *eb = NULL;
  5533. struct btrfs_key *keys;
  5534. u64 *nodes;
  5535. int level;
  5536. int shared_level;
  5537. int lowest_level = 0;
  5538. int ret;
  5539. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  5540. lowest_level = ref_path->owner_objectid;
  5541. if (!root->ref_cows) {
  5542. path->lowest_level = lowest_level;
  5543. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  5544. BUG_ON(ret < 0);
  5545. path->lowest_level = 0;
  5546. btrfs_release_path(root, path);
  5547. return 0;
  5548. }
  5549. mutex_lock(&root->fs_info->tree_reloc_mutex);
  5550. ret = init_reloc_tree(trans, root);
  5551. BUG_ON(ret);
  5552. reloc_root = root->reloc_root;
  5553. shared_level = ref_path->shared_level;
  5554. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  5555. keys = ref_path->node_keys;
  5556. nodes = ref_path->new_nodes;
  5557. memset(&keys[shared_level + 1], 0,
  5558. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  5559. memset(&nodes[shared_level + 1], 0,
  5560. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  5561. if (nodes[lowest_level] == 0) {
  5562. path->lowest_level = lowest_level;
  5563. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  5564. 0, 1);
  5565. BUG_ON(ret);
  5566. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  5567. eb = path->nodes[level];
  5568. if (!eb || eb == reloc_root->node)
  5569. break;
  5570. nodes[level] = eb->start;
  5571. if (level == 0)
  5572. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  5573. else
  5574. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  5575. }
  5576. if (nodes[0] &&
  5577. ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  5578. eb = path->nodes[0];
  5579. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  5580. group, reloc_inode);
  5581. BUG_ON(ret);
  5582. }
  5583. btrfs_release_path(reloc_root, path);
  5584. } else {
  5585. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  5586. lowest_level);
  5587. BUG_ON(ret);
  5588. }
  5589. /*
  5590. * replace tree blocks in the fs tree with tree blocks in
  5591. * the reloc tree.
  5592. */
  5593. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  5594. BUG_ON(ret < 0);
  5595. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  5596. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  5597. 0, 0);
  5598. BUG_ON(ret);
  5599. extent_buffer_get(path->nodes[0]);
  5600. eb = path->nodes[0];
  5601. btrfs_release_path(reloc_root, path);
  5602. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  5603. BUG_ON(ret);
  5604. free_extent_buffer(eb);
  5605. }
  5606. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  5607. path->lowest_level = 0;
  5608. return 0;
  5609. }
  5610. static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
  5611. struct btrfs_root *root,
  5612. struct btrfs_path *path,
  5613. struct btrfs_key *first_key,
  5614. struct btrfs_ref_path *ref_path)
  5615. {
  5616. int ret;
  5617. ret = relocate_one_path(trans, root, path, first_key,
  5618. ref_path, NULL, NULL);
  5619. BUG_ON(ret);
  5620. return 0;
  5621. }
  5622. static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
  5623. struct btrfs_root *extent_root,
  5624. struct btrfs_path *path,
  5625. struct btrfs_key *extent_key)
  5626. {
  5627. int ret;
  5628. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  5629. if (ret)
  5630. goto out;
  5631. ret = btrfs_del_item(trans, extent_root, path);
  5632. out:
  5633. btrfs_release_path(extent_root, path);
  5634. return ret;
  5635. }
  5636. static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
  5637. struct btrfs_ref_path *ref_path)
  5638. {
  5639. struct btrfs_key root_key;
  5640. root_key.objectid = ref_path->root_objectid;
  5641. root_key.type = BTRFS_ROOT_ITEM_KEY;
  5642. if (is_cowonly_root(ref_path->root_objectid))
  5643. root_key.offset = 0;
  5644. else
  5645. root_key.offset = (u64)-1;
  5646. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  5647. }
  5648. static noinline int relocate_one_extent(struct btrfs_root *extent_root,
  5649. struct btrfs_path *path,
  5650. struct btrfs_key *extent_key,
  5651. struct btrfs_block_group_cache *group,
  5652. struct inode *reloc_inode, int pass)
  5653. {
  5654. struct btrfs_trans_handle *trans;
  5655. struct btrfs_root *found_root;
  5656. struct btrfs_ref_path *ref_path = NULL;
  5657. struct disk_extent *new_extents = NULL;
  5658. int nr_extents = 0;
  5659. int loops;
  5660. int ret;
  5661. int level;
  5662. struct btrfs_key first_key;
  5663. u64 prev_block = 0;
  5664. trans = btrfs_start_transaction(extent_root, 1);
  5665. BUG_ON(!trans);
  5666. if (extent_key->objectid == 0) {
  5667. ret = del_extent_zero(trans, extent_root, path, extent_key);
  5668. goto out;
  5669. }
  5670. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  5671. if (!ref_path) {
  5672. ret = -ENOMEM;
  5673. goto out;
  5674. }
  5675. for (loops = 0; ; loops++) {
  5676. if (loops == 0) {
  5677. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  5678. extent_key->objectid);
  5679. } else {
  5680. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  5681. }
  5682. if (ret < 0)
  5683. goto out;
  5684. if (ret > 0)
  5685. break;
  5686. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  5687. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  5688. continue;
  5689. found_root = read_ref_root(extent_root->fs_info, ref_path);
  5690. BUG_ON(!found_root);
  5691. /*
  5692. * for reference counted tree, only process reference paths
  5693. * rooted at the latest committed root.
  5694. */
  5695. if (found_root->ref_cows &&
  5696. ref_path->root_generation != found_root->root_key.offset)
  5697. continue;
  5698. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  5699. if (pass == 0) {
  5700. /*
  5701. * copy data extents to new locations
  5702. */
  5703. u64 group_start = group->key.objectid;
  5704. ret = relocate_data_extent(reloc_inode,
  5705. extent_key,
  5706. group_start);
  5707. if (ret < 0)
  5708. goto out;
  5709. break;
  5710. }
  5711. level = 0;
  5712. } else {
  5713. level = ref_path->owner_objectid;
  5714. }
  5715. if (prev_block != ref_path->nodes[level]) {
  5716. struct extent_buffer *eb;
  5717. u64 block_start = ref_path->nodes[level];
  5718. u64 block_size = btrfs_level_size(found_root, level);
  5719. eb = read_tree_block(found_root, block_start,
  5720. block_size, 0);
  5721. btrfs_tree_lock(eb);
  5722. BUG_ON(level != btrfs_header_level(eb));
  5723. if (level == 0)
  5724. btrfs_item_key_to_cpu(eb, &first_key, 0);
  5725. else
  5726. btrfs_node_key_to_cpu(eb, &first_key, 0);
  5727. btrfs_tree_unlock(eb);
  5728. free_extent_buffer(eb);
  5729. prev_block = block_start;
  5730. }
  5731. mutex_lock(&extent_root->fs_info->trans_mutex);
  5732. btrfs_record_root_in_trans(found_root);
  5733. mutex_unlock(&extent_root->fs_info->trans_mutex);
  5734. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  5735. /*
  5736. * try to update data extent references while
  5737. * keeping metadata shared between snapshots.
  5738. */
  5739. if (pass == 1) {
  5740. ret = relocate_one_path(trans, found_root,
  5741. path, &first_key, ref_path,
  5742. group, reloc_inode);
  5743. if (ret < 0)
  5744. goto out;
  5745. continue;
  5746. }
  5747. /*
  5748. * use fallback method to process the remaining
  5749. * references.
  5750. */
  5751. if (!new_extents) {
  5752. u64 group_start = group->key.objectid;
  5753. new_extents = kmalloc(sizeof(*new_extents),
  5754. GFP_NOFS);
  5755. nr_extents = 1;
  5756. ret = get_new_locations(reloc_inode,
  5757. extent_key,
  5758. group_start, 1,
  5759. &new_extents,
  5760. &nr_extents);
  5761. if (ret)
  5762. goto out;
  5763. }
  5764. ret = replace_one_extent(trans, found_root,
  5765. path, extent_key,
  5766. &first_key, ref_path,
  5767. new_extents, nr_extents);
  5768. } else {
  5769. ret = relocate_tree_block(trans, found_root, path,
  5770. &first_key, ref_path);
  5771. }
  5772. if (ret < 0)
  5773. goto out;
  5774. }
  5775. ret = 0;
  5776. out:
  5777. btrfs_end_transaction(trans, extent_root);
  5778. kfree(new_extents);
  5779. kfree(ref_path);
  5780. return ret;
  5781. }
  5782. #endif
  5783. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  5784. {
  5785. u64 num_devices;
  5786. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  5787. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  5788. num_devices = root->fs_info->fs_devices->rw_devices;
  5789. if (num_devices == 1) {
  5790. stripped |= BTRFS_BLOCK_GROUP_DUP;
  5791. stripped = flags & ~stripped;
  5792. /* turn raid0 into single device chunks */
  5793. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  5794. return stripped;
  5795. /* turn mirroring into duplication */
  5796. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  5797. BTRFS_BLOCK_GROUP_RAID10))
  5798. return stripped | BTRFS_BLOCK_GROUP_DUP;
  5799. return flags;
  5800. } else {
  5801. /* they already had raid on here, just return */
  5802. if (flags & stripped)
  5803. return flags;
  5804. stripped |= BTRFS_BLOCK_GROUP_DUP;
  5805. stripped = flags & ~stripped;
  5806. /* switch duplicated blocks with raid1 */
  5807. if (flags & BTRFS_BLOCK_GROUP_DUP)
  5808. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  5809. /* turn single device chunks into raid0 */
  5810. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  5811. }
  5812. return flags;
  5813. }
  5814. static int __alloc_chunk_for_shrink(struct btrfs_root *root,
  5815. struct btrfs_block_group_cache *shrink_block_group,
  5816. int force)
  5817. {
  5818. struct btrfs_trans_handle *trans;
  5819. u64 new_alloc_flags;
  5820. u64 calc;
  5821. spin_lock(&shrink_block_group->lock);
  5822. if (btrfs_block_group_used(&shrink_block_group->item) +
  5823. shrink_block_group->reserved > 0) {
  5824. spin_unlock(&shrink_block_group->lock);
  5825. trans = btrfs_start_transaction(root, 1);
  5826. spin_lock(&shrink_block_group->lock);
  5827. new_alloc_flags = update_block_group_flags(root,
  5828. shrink_block_group->flags);
  5829. if (new_alloc_flags != shrink_block_group->flags) {
  5830. calc =
  5831. btrfs_block_group_used(&shrink_block_group->item);
  5832. } else {
  5833. calc = shrink_block_group->key.offset;
  5834. }
  5835. spin_unlock(&shrink_block_group->lock);
  5836. do_chunk_alloc(trans, root->fs_info->extent_root,
  5837. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  5838. btrfs_end_transaction(trans, root);
  5839. } else
  5840. spin_unlock(&shrink_block_group->lock);
  5841. return 0;
  5842. }
  5843. int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
  5844. struct btrfs_block_group_cache *group)
  5845. {
  5846. __alloc_chunk_for_shrink(root, group, 1);
  5847. set_block_group_readonly(group);
  5848. return 0;
  5849. }
  5850. #if 0
  5851. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  5852. struct btrfs_root *root,
  5853. u64 objectid, u64 size)
  5854. {
  5855. struct btrfs_path *path;
  5856. struct btrfs_inode_item *item;
  5857. struct extent_buffer *leaf;
  5858. int ret;
  5859. path = btrfs_alloc_path();
  5860. if (!path)
  5861. return -ENOMEM;
  5862. path->leave_spinning = 1;
  5863. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  5864. if (ret)
  5865. goto out;
  5866. leaf = path->nodes[0];
  5867. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  5868. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  5869. btrfs_set_inode_generation(leaf, item, 1);
  5870. btrfs_set_inode_size(leaf, item, size);
  5871. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  5872. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
  5873. btrfs_mark_buffer_dirty(leaf);
  5874. btrfs_release_path(root, path);
  5875. out:
  5876. btrfs_free_path(path);
  5877. return ret;
  5878. }
  5879. static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
  5880. struct btrfs_block_group_cache *group)
  5881. {
  5882. struct inode *inode = NULL;
  5883. struct btrfs_trans_handle *trans;
  5884. struct btrfs_root *root;
  5885. struct btrfs_key root_key;
  5886. u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
  5887. int err = 0;
  5888. root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  5889. root_key.type = BTRFS_ROOT_ITEM_KEY;
  5890. root_key.offset = (u64)-1;
  5891. root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  5892. if (IS_ERR(root))
  5893. return ERR_CAST(root);
  5894. trans = btrfs_start_transaction(root, 1);
  5895. BUG_ON(!trans);
  5896. err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
  5897. if (err)
  5898. goto out;
  5899. err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
  5900. BUG_ON(err);
  5901. err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
  5902. group->key.offset, 0, group->key.offset,
  5903. 0, 0, 0);
  5904. BUG_ON(err);
  5905. inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
  5906. if (inode->i_state & I_NEW) {
  5907. BTRFS_I(inode)->root = root;
  5908. BTRFS_I(inode)->location.objectid = objectid;
  5909. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  5910. BTRFS_I(inode)->location.offset = 0;
  5911. btrfs_read_locked_inode(inode);
  5912. unlock_new_inode(inode);
  5913. BUG_ON(is_bad_inode(inode));
  5914. } else {
  5915. BUG_ON(1);
  5916. }
  5917. BTRFS_I(inode)->index_cnt = group->key.objectid;
  5918. err = btrfs_orphan_add(trans, inode);
  5919. out:
  5920. btrfs_end_transaction(trans, root);
  5921. if (err) {
  5922. if (inode)
  5923. iput(inode);
  5924. inode = ERR_PTR(err);
  5925. }
  5926. return inode;
  5927. }
  5928. int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
  5929. {
  5930. struct btrfs_ordered_sum *sums;
  5931. struct btrfs_sector_sum *sector_sum;
  5932. struct btrfs_ordered_extent *ordered;
  5933. struct btrfs_root *root = BTRFS_I(inode)->root;
  5934. struct list_head list;
  5935. size_t offset;
  5936. int ret;
  5937. u64 disk_bytenr;
  5938. INIT_LIST_HEAD(&list);
  5939. ordered = btrfs_lookup_ordered_extent(inode, file_pos);
  5940. BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
  5941. disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
  5942. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
  5943. disk_bytenr + len - 1, &list);
  5944. while (!list_empty(&list)) {
  5945. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  5946. list_del_init(&sums->list);
  5947. sector_sum = sums->sums;
  5948. sums->bytenr = ordered->start;
  5949. offset = 0;
  5950. while (offset < sums->len) {
  5951. sector_sum->bytenr += ordered->start - disk_bytenr;
  5952. sector_sum++;
  5953. offset += root->sectorsize;
  5954. }
  5955. btrfs_add_ordered_sum(inode, ordered, sums);
  5956. }
  5957. btrfs_put_ordered_extent(ordered);
  5958. return 0;
  5959. }
  5960. int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
  5961. {
  5962. struct btrfs_trans_handle *trans;
  5963. struct btrfs_path *path;
  5964. struct btrfs_fs_info *info = root->fs_info;
  5965. struct extent_buffer *leaf;
  5966. struct inode *reloc_inode;
  5967. struct btrfs_block_group_cache *block_group;
  5968. struct btrfs_key key;
  5969. u64 skipped;
  5970. u64 cur_byte;
  5971. u64 total_found;
  5972. u32 nritems;
  5973. int ret;
  5974. int progress;
  5975. int pass = 0;
  5976. root = root->fs_info->extent_root;
  5977. block_group = btrfs_lookup_block_group(info, group_start);
  5978. BUG_ON(!block_group);
  5979. printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
  5980. (unsigned long long)block_group->key.objectid,
  5981. (unsigned long long)block_group->flags);
  5982. path = btrfs_alloc_path();
  5983. BUG_ON(!path);
  5984. reloc_inode = create_reloc_inode(info, block_group);
  5985. BUG_ON(IS_ERR(reloc_inode));
  5986. __alloc_chunk_for_shrink(root, block_group, 1);
  5987. set_block_group_readonly(block_group);
  5988. btrfs_start_delalloc_inodes(info->tree_root);
  5989. btrfs_wait_ordered_extents(info->tree_root, 0);
  5990. again:
  5991. skipped = 0;
  5992. total_found = 0;
  5993. progress = 0;
  5994. key.objectid = block_group->key.objectid;
  5995. key.offset = 0;
  5996. key.type = 0;
  5997. cur_byte = key.objectid;
  5998. trans = btrfs_start_transaction(info->tree_root, 1);
  5999. btrfs_commit_transaction(trans, info->tree_root);
  6000. mutex_lock(&root->fs_info->cleaner_mutex);
  6001. btrfs_clean_old_snapshots(info->tree_root);
  6002. btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
  6003. mutex_unlock(&root->fs_info->cleaner_mutex);
  6004. trans = btrfs_start_transaction(info->tree_root, 1);
  6005. btrfs_commit_transaction(trans, info->tree_root);
  6006. while (1) {
  6007. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6008. if (ret < 0)
  6009. goto out;
  6010. next:
  6011. leaf = path->nodes[0];
  6012. nritems = btrfs_header_nritems(leaf);
  6013. if (path->slots[0] >= nritems) {
  6014. ret = btrfs_next_leaf(root, path);
  6015. if (ret < 0)
  6016. goto out;
  6017. if (ret == 1) {
  6018. ret = 0;
  6019. break;
  6020. }
  6021. leaf = path->nodes[0];
  6022. nritems = btrfs_header_nritems(leaf);
  6023. }
  6024. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  6025. if (key.objectid >= block_group->key.objectid +
  6026. block_group->key.offset)
  6027. break;
  6028. if (progress && need_resched()) {
  6029. btrfs_release_path(root, path);
  6030. cond_resched();
  6031. progress = 0;
  6032. continue;
  6033. }
  6034. progress = 1;
  6035. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
  6036. key.objectid + key.offset <= cur_byte) {
  6037. path->slots[0]++;
  6038. goto next;
  6039. }
  6040. total_found++;
  6041. cur_byte = key.objectid + key.offset;
  6042. btrfs_release_path(root, path);
  6043. __alloc_chunk_for_shrink(root, block_group, 0);
  6044. ret = relocate_one_extent(root, path, &key, block_group,
  6045. reloc_inode, pass);
  6046. BUG_ON(ret < 0);
  6047. if (ret > 0)
  6048. skipped++;
  6049. key.objectid = cur_byte;
  6050. key.type = 0;
  6051. key.offset = 0;
  6052. }
  6053. btrfs_release_path(root, path);
  6054. if (pass == 0) {
  6055. btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
  6056. invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
  6057. }
  6058. if (total_found > 0) {
  6059. printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
  6060. (unsigned long long)total_found, pass);
  6061. pass++;
  6062. if (total_found == skipped && pass > 2) {
  6063. iput(reloc_inode);
  6064. reloc_inode = create_reloc_inode(info, block_group);
  6065. pass = 0;
  6066. }
  6067. goto again;
  6068. }
  6069. /* delete reloc_inode */
  6070. iput(reloc_inode);
  6071. /* unpin extents in this range */
  6072. trans = btrfs_start_transaction(info->tree_root, 1);
  6073. btrfs_commit_transaction(trans, info->tree_root);
  6074. spin_lock(&block_group->lock);
  6075. WARN_ON(block_group->pinned > 0);
  6076. WARN_ON(block_group->reserved > 0);
  6077. WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
  6078. spin_unlock(&block_group->lock);
  6079. btrfs_put_block_group(block_group);
  6080. ret = 0;
  6081. out:
  6082. btrfs_free_path(path);
  6083. return ret;
  6084. }
  6085. #endif
  6086. static int find_first_block_group(struct btrfs_root *root,
  6087. struct btrfs_path *path, struct btrfs_key *key)
  6088. {
  6089. int ret = 0;
  6090. struct btrfs_key found_key;
  6091. struct extent_buffer *leaf;
  6092. int slot;
  6093. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  6094. if (ret < 0)
  6095. goto out;
  6096. while (1) {
  6097. slot = path->slots[0];
  6098. leaf = path->nodes[0];
  6099. if (slot >= btrfs_header_nritems(leaf)) {
  6100. ret = btrfs_next_leaf(root, path);
  6101. if (ret == 0)
  6102. continue;
  6103. if (ret < 0)
  6104. goto out;
  6105. break;
  6106. }
  6107. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6108. if (found_key.objectid >= key->objectid &&
  6109. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  6110. ret = 0;
  6111. goto out;
  6112. }
  6113. path->slots[0]++;
  6114. }
  6115. ret = -ENOENT;
  6116. out:
  6117. return ret;
  6118. }
  6119. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  6120. {
  6121. struct btrfs_block_group_cache *block_group;
  6122. struct btrfs_space_info *space_info;
  6123. struct rb_node *n;
  6124. spin_lock(&info->block_group_cache_lock);
  6125. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  6126. block_group = rb_entry(n, struct btrfs_block_group_cache,
  6127. cache_node);
  6128. rb_erase(&block_group->cache_node,
  6129. &info->block_group_cache_tree);
  6130. spin_unlock(&info->block_group_cache_lock);
  6131. btrfs_remove_free_space_cache(block_group);
  6132. down_write(&block_group->space_info->groups_sem);
  6133. list_del(&block_group->list);
  6134. up_write(&block_group->space_info->groups_sem);
  6135. WARN_ON(atomic_read(&block_group->count) != 1);
  6136. kfree(block_group);
  6137. spin_lock(&info->block_group_cache_lock);
  6138. }
  6139. spin_unlock(&info->block_group_cache_lock);
  6140. /* now that all the block groups are freed, go through and
  6141. * free all the space_info structs. This is only called during
  6142. * the final stages of unmount, and so we know nobody is
  6143. * using them. We call synchronize_rcu() once before we start,
  6144. * just to be on the safe side.
  6145. */
  6146. synchronize_rcu();
  6147. while(!list_empty(&info->space_info)) {
  6148. space_info = list_entry(info->space_info.next,
  6149. struct btrfs_space_info,
  6150. list);
  6151. list_del(&space_info->list);
  6152. kfree(space_info);
  6153. }
  6154. return 0;
  6155. }
  6156. int btrfs_read_block_groups(struct btrfs_root *root)
  6157. {
  6158. struct btrfs_path *path;
  6159. int ret;
  6160. struct btrfs_block_group_cache *cache;
  6161. struct btrfs_fs_info *info = root->fs_info;
  6162. struct btrfs_space_info *space_info;
  6163. struct btrfs_key key;
  6164. struct btrfs_key found_key;
  6165. struct extent_buffer *leaf;
  6166. root = info->extent_root;
  6167. key.objectid = 0;
  6168. key.offset = 0;
  6169. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  6170. path = btrfs_alloc_path();
  6171. if (!path)
  6172. return -ENOMEM;
  6173. while (1) {
  6174. ret = find_first_block_group(root, path, &key);
  6175. if (ret > 0) {
  6176. ret = 0;
  6177. goto error;
  6178. }
  6179. if (ret != 0)
  6180. goto error;
  6181. leaf = path->nodes[0];
  6182. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6183. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6184. if (!cache) {
  6185. ret = -ENOMEM;
  6186. break;
  6187. }
  6188. atomic_set(&cache->count, 1);
  6189. spin_lock_init(&cache->lock);
  6190. spin_lock_init(&cache->tree_lock);
  6191. mutex_init(&cache->cache_mutex);
  6192. INIT_LIST_HEAD(&cache->list);
  6193. INIT_LIST_HEAD(&cache->cluster_list);
  6194. read_extent_buffer(leaf, &cache->item,
  6195. btrfs_item_ptr_offset(leaf, path->slots[0]),
  6196. sizeof(cache->item));
  6197. memcpy(&cache->key, &found_key, sizeof(found_key));
  6198. key.objectid = found_key.objectid + found_key.offset;
  6199. btrfs_release_path(root, path);
  6200. cache->flags = btrfs_block_group_flags(&cache->item);
  6201. ret = update_space_info(info, cache->flags, found_key.offset,
  6202. btrfs_block_group_used(&cache->item),
  6203. &space_info);
  6204. BUG_ON(ret);
  6205. cache->space_info = space_info;
  6206. down_write(&space_info->groups_sem);
  6207. list_add_tail(&cache->list, &space_info->block_groups);
  6208. up_write(&space_info->groups_sem);
  6209. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6210. BUG_ON(ret);
  6211. set_avail_alloc_bits(root->fs_info, cache->flags);
  6212. if (btrfs_chunk_readonly(root, cache->key.objectid))
  6213. set_block_group_readonly(cache);
  6214. }
  6215. ret = 0;
  6216. error:
  6217. btrfs_free_path(path);
  6218. return ret;
  6219. }
  6220. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  6221. struct btrfs_root *root, u64 bytes_used,
  6222. u64 type, u64 chunk_objectid, u64 chunk_offset,
  6223. u64 size)
  6224. {
  6225. int ret;
  6226. struct btrfs_root *extent_root;
  6227. struct btrfs_block_group_cache *cache;
  6228. extent_root = root->fs_info->extent_root;
  6229. root->fs_info->last_trans_log_full_commit = trans->transid;
  6230. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  6231. if (!cache)
  6232. return -ENOMEM;
  6233. cache->key.objectid = chunk_offset;
  6234. cache->key.offset = size;
  6235. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  6236. atomic_set(&cache->count, 1);
  6237. spin_lock_init(&cache->lock);
  6238. spin_lock_init(&cache->tree_lock);
  6239. mutex_init(&cache->cache_mutex);
  6240. INIT_LIST_HEAD(&cache->list);
  6241. INIT_LIST_HEAD(&cache->cluster_list);
  6242. btrfs_set_block_group_used(&cache->item, bytes_used);
  6243. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  6244. cache->flags = type;
  6245. btrfs_set_block_group_flags(&cache->item, type);
  6246. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  6247. &cache->space_info);
  6248. BUG_ON(ret);
  6249. down_write(&cache->space_info->groups_sem);
  6250. list_add_tail(&cache->list, &cache->space_info->block_groups);
  6251. up_write(&cache->space_info->groups_sem);
  6252. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  6253. BUG_ON(ret);
  6254. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  6255. sizeof(cache->item));
  6256. BUG_ON(ret);
  6257. set_avail_alloc_bits(extent_root->fs_info, type);
  6258. return 0;
  6259. }
  6260. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  6261. struct btrfs_root *root, u64 group_start)
  6262. {
  6263. struct btrfs_path *path;
  6264. struct btrfs_block_group_cache *block_group;
  6265. struct btrfs_free_cluster *cluster;
  6266. struct btrfs_key key;
  6267. int ret;
  6268. root = root->fs_info->extent_root;
  6269. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  6270. BUG_ON(!block_group);
  6271. BUG_ON(!block_group->ro);
  6272. memcpy(&key, &block_group->key, sizeof(key));
  6273. /* make sure this block group isn't part of an allocation cluster */
  6274. cluster = &root->fs_info->data_alloc_cluster;
  6275. spin_lock(&cluster->refill_lock);
  6276. btrfs_return_cluster_to_free_space(block_group, cluster);
  6277. spin_unlock(&cluster->refill_lock);
  6278. /*
  6279. * make sure this block group isn't part of a metadata
  6280. * allocation cluster
  6281. */
  6282. cluster = &root->fs_info->meta_alloc_cluster;
  6283. spin_lock(&cluster->refill_lock);
  6284. btrfs_return_cluster_to_free_space(block_group, cluster);
  6285. spin_unlock(&cluster->refill_lock);
  6286. path = btrfs_alloc_path();
  6287. BUG_ON(!path);
  6288. spin_lock(&root->fs_info->block_group_cache_lock);
  6289. rb_erase(&block_group->cache_node,
  6290. &root->fs_info->block_group_cache_tree);
  6291. spin_unlock(&root->fs_info->block_group_cache_lock);
  6292. btrfs_remove_free_space_cache(block_group);
  6293. down_write(&block_group->space_info->groups_sem);
  6294. /*
  6295. * we must use list_del_init so people can check to see if they
  6296. * are still on the list after taking the semaphore
  6297. */
  6298. list_del_init(&block_group->list);
  6299. up_write(&block_group->space_info->groups_sem);
  6300. spin_lock(&block_group->space_info->lock);
  6301. block_group->space_info->total_bytes -= block_group->key.offset;
  6302. block_group->space_info->bytes_readonly -= block_group->key.offset;
  6303. spin_unlock(&block_group->space_info->lock);
  6304. block_group->space_info->full = 0;
  6305. btrfs_put_block_group(block_group);
  6306. btrfs_put_block_group(block_group);
  6307. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  6308. if (ret > 0)
  6309. ret = -EIO;
  6310. if (ret < 0)
  6311. goto out;
  6312. ret = btrfs_del_item(trans, root, path);
  6313. out:
  6314. btrfs_free_path(path);
  6315. return ret;
  6316. }