extent-tree.c 195 KB

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