extent-tree.c 196 KB

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