extent-tree.c 192 KB

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