extent-tree.c 201 KB

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