extent-tree.c 224 KB

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