extent-tree.c 197 KB

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