extent-tree.c 195 KB

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