extent-tree.c 218 KB

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