extent-tree.c 227 KB

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