extent-tree.c 193 KB

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