extent-tree.c 192 KB

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