extent-tree.c 192 KB

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