extent-tree.c 201 KB

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