extent-tree.c 229 KB

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