extent-tree.c 219 KB

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