extent-tree.c 218 KB

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