extent-tree.c 155 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013
  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 "compat.h"
  25. #include "hash.h"
  26. #include "crc32c.h"
  27. #include "ctree.h"
  28. #include "disk-io.h"
  29. #include "print-tree.h"
  30. #include "transaction.h"
  31. #include "volumes.h"
  32. #include "locking.h"
  33. #include "ref-cache.h"
  34. #include "free-space-cache.h"
  35. #define PENDING_EXTENT_INSERT 0
  36. #define PENDING_EXTENT_DELETE 1
  37. #define PENDING_BACKREF_UPDATE 2
  38. struct pending_extent_op {
  39. int type;
  40. u64 bytenr;
  41. u64 num_bytes;
  42. u64 parent;
  43. u64 orig_parent;
  44. u64 generation;
  45. u64 orig_generation;
  46. int level;
  47. struct list_head list;
  48. int del;
  49. };
  50. static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  51. struct btrfs_root *root, u64 parent,
  52. u64 root_objectid, u64 ref_generation,
  53. u64 owner, struct btrfs_key *ins,
  54. int ref_mod);
  55. static int update_reserved_extents(struct btrfs_root *root,
  56. u64 bytenr, u64 num, int reserve);
  57. static int update_block_group(struct btrfs_trans_handle *trans,
  58. struct btrfs_root *root,
  59. u64 bytenr, u64 num_bytes, int alloc,
  60. int mark_free);
  61. static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  62. struct btrfs_root *root,
  63. u64 bytenr, u64 num_bytes, u64 parent,
  64. u64 root_objectid, u64 ref_generation,
  65. u64 owner_objectid, int pin,
  66. int ref_to_drop);
  67. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  68. struct btrfs_root *extent_root, u64 alloc_bytes,
  69. u64 flags, int force);
  70. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  71. {
  72. return (cache->flags & bits) == bits;
  73. }
  74. /*
  75. * this adds the block group to the fs_info rb tree for the block group
  76. * cache
  77. */
  78. static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
  79. struct btrfs_block_group_cache *block_group)
  80. {
  81. struct rb_node **p;
  82. struct rb_node *parent = NULL;
  83. struct btrfs_block_group_cache *cache;
  84. spin_lock(&info->block_group_cache_lock);
  85. p = &info->block_group_cache_tree.rb_node;
  86. while (*p) {
  87. parent = *p;
  88. cache = rb_entry(parent, struct btrfs_block_group_cache,
  89. cache_node);
  90. if (block_group->key.objectid < cache->key.objectid) {
  91. p = &(*p)->rb_left;
  92. } else if (block_group->key.objectid > cache->key.objectid) {
  93. p = &(*p)->rb_right;
  94. } else {
  95. spin_unlock(&info->block_group_cache_lock);
  96. return -EEXIST;
  97. }
  98. }
  99. rb_link_node(&block_group->cache_node, parent, p);
  100. rb_insert_color(&block_group->cache_node,
  101. &info->block_group_cache_tree);
  102. spin_unlock(&info->block_group_cache_lock);
  103. return 0;
  104. }
  105. /*
  106. * This will return the block group at or after bytenr if contains is 0, else
  107. * it will return the block group that contains the bytenr
  108. */
  109. static struct btrfs_block_group_cache *
  110. block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
  111. int contains)
  112. {
  113. struct btrfs_block_group_cache *cache, *ret = NULL;
  114. struct rb_node *n;
  115. u64 end, start;
  116. spin_lock(&info->block_group_cache_lock);
  117. n = info->block_group_cache_tree.rb_node;
  118. while (n) {
  119. cache = rb_entry(n, struct btrfs_block_group_cache,
  120. cache_node);
  121. end = cache->key.objectid + cache->key.offset - 1;
  122. start = cache->key.objectid;
  123. if (bytenr < start) {
  124. if (!contains && (!ret || start < ret->key.objectid))
  125. ret = cache;
  126. n = n->rb_left;
  127. } else if (bytenr > start) {
  128. if (contains && bytenr <= end) {
  129. ret = cache;
  130. break;
  131. }
  132. n = n->rb_right;
  133. } else {
  134. ret = cache;
  135. break;
  136. }
  137. }
  138. if (ret)
  139. atomic_inc(&ret->count);
  140. spin_unlock(&info->block_group_cache_lock);
  141. return ret;
  142. }
  143. /*
  144. * this is only called by cache_block_group, since we could have freed extents
  145. * we need to check the pinned_extents for any extents that can't be used yet
  146. * since their free space will be released as soon as the transaction commits.
  147. */
  148. static int add_new_free_space(struct btrfs_block_group_cache *block_group,
  149. struct btrfs_fs_info *info, u64 start, u64 end)
  150. {
  151. u64 extent_start, extent_end, size;
  152. int ret;
  153. while (start < end) {
  154. ret = find_first_extent_bit(&info->pinned_extents, start,
  155. &extent_start, &extent_end,
  156. EXTENT_DIRTY);
  157. if (ret)
  158. break;
  159. if (extent_start == start) {
  160. start = extent_end + 1;
  161. } else if (extent_start > start && extent_start < end) {
  162. size = extent_start - start;
  163. ret = btrfs_add_free_space(block_group, start,
  164. size);
  165. BUG_ON(ret);
  166. start = extent_end + 1;
  167. } else {
  168. break;
  169. }
  170. }
  171. if (start < end) {
  172. size = end - start;
  173. ret = btrfs_add_free_space(block_group, start, size);
  174. BUG_ON(ret);
  175. }
  176. return 0;
  177. }
  178. static int remove_sb_from_cache(struct btrfs_root *root,
  179. struct btrfs_block_group_cache *cache)
  180. {
  181. u64 bytenr;
  182. u64 *logical;
  183. int stripe_len;
  184. int i, nr, ret;
  185. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  186. bytenr = btrfs_sb_offset(i);
  187. ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
  188. cache->key.objectid, bytenr, 0,
  189. &logical, &nr, &stripe_len);
  190. BUG_ON(ret);
  191. while (nr--) {
  192. btrfs_remove_free_space(cache, logical[nr],
  193. stripe_len);
  194. }
  195. kfree(logical);
  196. }
  197. return 0;
  198. }
  199. static int cache_block_group(struct btrfs_root *root,
  200. struct btrfs_block_group_cache *block_group)
  201. {
  202. struct btrfs_path *path;
  203. int ret = 0;
  204. struct btrfs_key key;
  205. struct extent_buffer *leaf;
  206. int slot;
  207. u64 last;
  208. if (!block_group)
  209. return 0;
  210. root = root->fs_info->extent_root;
  211. if (block_group->cached)
  212. return 0;
  213. path = btrfs_alloc_path();
  214. if (!path)
  215. return -ENOMEM;
  216. path->reada = 2;
  217. /*
  218. * we get into deadlocks with paths held by callers of this function.
  219. * since the alloc_mutex is protecting things right now, just
  220. * skip the locking here
  221. */
  222. path->skip_locking = 1;
  223. last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
  224. key.objectid = last;
  225. key.offset = 0;
  226. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  227. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  228. if (ret < 0)
  229. goto err;
  230. while (1) {
  231. leaf = path->nodes[0];
  232. slot = path->slots[0];
  233. if (slot >= btrfs_header_nritems(leaf)) {
  234. ret = btrfs_next_leaf(root, path);
  235. if (ret < 0)
  236. goto err;
  237. if (ret == 0)
  238. continue;
  239. else
  240. break;
  241. }
  242. btrfs_item_key_to_cpu(leaf, &key, slot);
  243. if (key.objectid < block_group->key.objectid)
  244. goto next;
  245. if (key.objectid >= block_group->key.objectid +
  246. block_group->key.offset)
  247. break;
  248. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  249. add_new_free_space(block_group, root->fs_info, last,
  250. key.objectid);
  251. last = key.objectid + key.offset;
  252. }
  253. next:
  254. path->slots[0]++;
  255. }
  256. add_new_free_space(block_group, root->fs_info, last,
  257. block_group->key.objectid +
  258. block_group->key.offset);
  259. block_group->cached = 1;
  260. remove_sb_from_cache(root, block_group);
  261. ret = 0;
  262. err:
  263. btrfs_free_path(path);
  264. return ret;
  265. }
  266. /*
  267. * return the block group that starts at or after bytenr
  268. */
  269. static struct btrfs_block_group_cache *
  270. btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
  271. {
  272. struct btrfs_block_group_cache *cache;
  273. cache = block_group_cache_tree_search(info, bytenr, 0);
  274. return cache;
  275. }
  276. /*
  277. * return the block group that contains the given bytenr
  278. */
  279. struct btrfs_block_group_cache *btrfs_lookup_block_group(
  280. struct btrfs_fs_info *info,
  281. u64 bytenr)
  282. {
  283. struct btrfs_block_group_cache *cache;
  284. cache = block_group_cache_tree_search(info, bytenr, 1);
  285. return cache;
  286. }
  287. void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
  288. {
  289. if (atomic_dec_and_test(&cache->count))
  290. kfree(cache);
  291. }
  292. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  293. u64 flags)
  294. {
  295. struct list_head *head = &info->space_info;
  296. struct btrfs_space_info *found;
  297. rcu_read_lock();
  298. list_for_each_entry_rcu(found, head, list) {
  299. if (found->flags == flags) {
  300. rcu_read_unlock();
  301. return found;
  302. }
  303. }
  304. rcu_read_unlock();
  305. return NULL;
  306. }
  307. /*
  308. * after adding space to the filesystem, we need to clear the full flags
  309. * on all the space infos.
  310. */
  311. void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
  312. {
  313. struct list_head *head = &info->space_info;
  314. struct btrfs_space_info *found;
  315. rcu_read_lock();
  316. list_for_each_entry_rcu(found, head, list)
  317. found->full = 0;
  318. rcu_read_unlock();
  319. }
  320. static u64 div_factor(u64 num, int factor)
  321. {
  322. if (factor == 10)
  323. return num;
  324. num *= factor;
  325. do_div(num, 10);
  326. return num;
  327. }
  328. u64 btrfs_find_block_group(struct btrfs_root *root,
  329. u64 search_start, u64 search_hint, int owner)
  330. {
  331. struct btrfs_block_group_cache *cache;
  332. u64 used;
  333. u64 last = max(search_hint, search_start);
  334. u64 group_start = 0;
  335. int full_search = 0;
  336. int factor = 9;
  337. int wrapped = 0;
  338. again:
  339. while (1) {
  340. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  341. if (!cache)
  342. break;
  343. spin_lock(&cache->lock);
  344. last = cache->key.objectid + cache->key.offset;
  345. used = btrfs_block_group_used(&cache->item);
  346. if ((full_search || !cache->ro) &&
  347. block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
  348. if (used + cache->pinned + cache->reserved <
  349. div_factor(cache->key.offset, factor)) {
  350. group_start = cache->key.objectid;
  351. spin_unlock(&cache->lock);
  352. btrfs_put_block_group(cache);
  353. goto found;
  354. }
  355. }
  356. spin_unlock(&cache->lock);
  357. btrfs_put_block_group(cache);
  358. cond_resched();
  359. }
  360. if (!wrapped) {
  361. last = search_start;
  362. wrapped = 1;
  363. goto again;
  364. }
  365. if (!full_search && factor < 10) {
  366. last = search_start;
  367. full_search = 1;
  368. factor = 10;
  369. goto again;
  370. }
  371. found:
  372. return group_start;
  373. }
  374. /* simple helper to search for an existing extent at a given offset */
  375. int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
  376. {
  377. int ret;
  378. struct btrfs_key key;
  379. struct btrfs_path *path;
  380. path = btrfs_alloc_path();
  381. BUG_ON(!path);
  382. key.objectid = start;
  383. key.offset = len;
  384. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  385. ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
  386. 0, 0);
  387. btrfs_free_path(path);
  388. return ret;
  389. }
  390. /*
  391. * Back reference rules. Back refs have three main goals:
  392. *
  393. * 1) differentiate between all holders of references to an extent so that
  394. * when a reference is dropped we can make sure it was a valid reference
  395. * before freeing the extent.
  396. *
  397. * 2) Provide enough information to quickly find the holders of an extent
  398. * if we notice a given block is corrupted or bad.
  399. *
  400. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  401. * maintenance. This is actually the same as #2, but with a slightly
  402. * different use case.
  403. *
  404. * File extents can be referenced by:
  405. *
  406. * - multiple snapshots, subvolumes, or different generations in one subvol
  407. * - different files inside a single subvolume
  408. * - different offsets inside a file (bookend extents in file.c)
  409. *
  410. * The extent ref structure has fields for:
  411. *
  412. * - Objectid of the subvolume root
  413. * - Generation number of the tree holding the reference
  414. * - objectid of the file holding the reference
  415. * - number of references holding by parent node (alway 1 for tree blocks)
  416. *
  417. * Btree leaf may hold multiple references to a file extent. In most cases,
  418. * these references are from same file and the corresponding offsets inside
  419. * the file are close together.
  420. *
  421. * When a file extent is allocated the fields are filled in:
  422. * (root_key.objectid, trans->transid, inode objectid, 1)
  423. *
  424. * When a leaf is cow'd new references are added for every file extent found
  425. * in the leaf. It looks similar to the create case, but trans->transid will
  426. * be different when the block is cow'd.
  427. *
  428. * (root_key.objectid, trans->transid, inode objectid,
  429. * number of references in the leaf)
  430. *
  431. * When a file extent is removed either during snapshot deletion or
  432. * file truncation, we find the corresponding back reference and check
  433. * the following fields:
  434. *
  435. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  436. * inode objectid)
  437. *
  438. * Btree extents can be referenced by:
  439. *
  440. * - Different subvolumes
  441. * - Different generations of the same subvolume
  442. *
  443. * When a tree block is created, back references are inserted:
  444. *
  445. * (root->root_key.objectid, trans->transid, level, 1)
  446. *
  447. * When a tree block is cow'd, new back references are added for all the
  448. * blocks it points to. If the tree block isn't in reference counted root,
  449. * the old back references are removed. These new back references are of
  450. * the form (trans->transid will have increased since creation):
  451. *
  452. * (root->root_key.objectid, trans->transid, level, 1)
  453. *
  454. * When a backref is in deleting, the following fields are checked:
  455. *
  456. * if backref was for a tree root:
  457. * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
  458. * else
  459. * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
  460. *
  461. * Back Reference Key composing:
  462. *
  463. * The key objectid corresponds to the first byte in the extent, the key
  464. * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
  465. * byte of parent extent. If a extent is tree root, the key offset is set
  466. * to the key objectid.
  467. */
  468. static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
  469. struct btrfs_root *root,
  470. struct btrfs_path *path,
  471. u64 bytenr, u64 parent,
  472. u64 ref_root, u64 ref_generation,
  473. u64 owner_objectid, int del)
  474. {
  475. struct btrfs_key key;
  476. struct btrfs_extent_ref *ref;
  477. struct extent_buffer *leaf;
  478. u64 ref_objectid;
  479. int ret;
  480. key.objectid = bytenr;
  481. key.type = BTRFS_EXTENT_REF_KEY;
  482. key.offset = parent;
  483. ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
  484. if (ret < 0)
  485. goto out;
  486. if (ret > 0) {
  487. ret = -ENOENT;
  488. goto out;
  489. }
  490. leaf = path->nodes[0];
  491. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  492. ref_objectid = btrfs_ref_objectid(leaf, ref);
  493. if (btrfs_ref_root(leaf, ref) != ref_root ||
  494. btrfs_ref_generation(leaf, ref) != ref_generation ||
  495. (ref_objectid != owner_objectid &&
  496. ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
  497. ret = -EIO;
  498. WARN_ON(1);
  499. goto out;
  500. }
  501. ret = 0;
  502. out:
  503. return ret;
  504. }
  505. static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
  506. struct btrfs_root *root,
  507. struct btrfs_path *path,
  508. u64 bytenr, u64 parent,
  509. u64 ref_root, u64 ref_generation,
  510. u64 owner_objectid,
  511. int refs_to_add)
  512. {
  513. struct btrfs_key key;
  514. struct extent_buffer *leaf;
  515. struct btrfs_extent_ref *ref;
  516. u32 num_refs;
  517. int ret;
  518. key.objectid = bytenr;
  519. key.type = BTRFS_EXTENT_REF_KEY;
  520. key.offset = parent;
  521. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
  522. if (ret == 0) {
  523. leaf = path->nodes[0];
  524. ref = btrfs_item_ptr(leaf, path->slots[0],
  525. struct btrfs_extent_ref);
  526. btrfs_set_ref_root(leaf, ref, ref_root);
  527. btrfs_set_ref_generation(leaf, ref, ref_generation);
  528. btrfs_set_ref_objectid(leaf, ref, owner_objectid);
  529. btrfs_set_ref_num_refs(leaf, ref, refs_to_add);
  530. } else if (ret == -EEXIST) {
  531. u64 existing_owner;
  532. BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
  533. leaf = path->nodes[0];
  534. ref = btrfs_item_ptr(leaf, path->slots[0],
  535. struct btrfs_extent_ref);
  536. if (btrfs_ref_root(leaf, ref) != ref_root ||
  537. btrfs_ref_generation(leaf, ref) != ref_generation) {
  538. ret = -EIO;
  539. WARN_ON(1);
  540. goto out;
  541. }
  542. num_refs = btrfs_ref_num_refs(leaf, ref);
  543. BUG_ON(num_refs == 0);
  544. btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add);
  545. existing_owner = btrfs_ref_objectid(leaf, ref);
  546. if (existing_owner != owner_objectid &&
  547. existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
  548. btrfs_set_ref_objectid(leaf, ref,
  549. BTRFS_MULTIPLE_OBJECTIDS);
  550. }
  551. ret = 0;
  552. } else {
  553. goto out;
  554. }
  555. btrfs_unlock_up_safe(path, 1);
  556. btrfs_mark_buffer_dirty(path->nodes[0]);
  557. out:
  558. btrfs_release_path(root, path);
  559. return ret;
  560. }
  561. static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
  562. struct btrfs_root *root,
  563. struct btrfs_path *path,
  564. int refs_to_drop)
  565. {
  566. struct extent_buffer *leaf;
  567. struct btrfs_extent_ref *ref;
  568. u32 num_refs;
  569. int ret = 0;
  570. leaf = path->nodes[0];
  571. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  572. num_refs = btrfs_ref_num_refs(leaf, ref);
  573. BUG_ON(num_refs < refs_to_drop);
  574. num_refs -= refs_to_drop;
  575. if (num_refs == 0) {
  576. ret = btrfs_del_item(trans, root, path);
  577. } else {
  578. btrfs_set_ref_num_refs(leaf, ref, num_refs);
  579. btrfs_mark_buffer_dirty(leaf);
  580. }
  581. btrfs_release_path(root, path);
  582. return ret;
  583. }
  584. #ifdef BIO_RW_DISCARD
  585. static void btrfs_issue_discard(struct block_device *bdev,
  586. u64 start, u64 len)
  587. {
  588. blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
  589. }
  590. #endif
  591. static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
  592. u64 num_bytes)
  593. {
  594. #ifdef BIO_RW_DISCARD
  595. int ret;
  596. u64 map_length = num_bytes;
  597. struct btrfs_multi_bio *multi = NULL;
  598. /* Tell the block device(s) that the sectors can be discarded */
  599. ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
  600. bytenr, &map_length, &multi, 0);
  601. if (!ret) {
  602. struct btrfs_bio_stripe *stripe = multi->stripes;
  603. int i;
  604. if (map_length > num_bytes)
  605. map_length = num_bytes;
  606. for (i = 0; i < multi->num_stripes; i++, stripe++) {
  607. btrfs_issue_discard(stripe->dev->bdev,
  608. stripe->physical,
  609. map_length);
  610. }
  611. kfree(multi);
  612. }
  613. return ret;
  614. #else
  615. return 0;
  616. #endif
  617. }
  618. static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  619. struct btrfs_root *root, u64 bytenr,
  620. u64 num_bytes,
  621. u64 orig_parent, u64 parent,
  622. u64 orig_root, u64 ref_root,
  623. u64 orig_generation, u64 ref_generation,
  624. u64 owner_objectid)
  625. {
  626. int ret;
  627. int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID;
  628. ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes,
  629. orig_parent, parent, orig_root,
  630. ref_root, orig_generation,
  631. ref_generation, owner_objectid, pin);
  632. BUG_ON(ret);
  633. return ret;
  634. }
  635. int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  636. struct btrfs_root *root, u64 bytenr,
  637. u64 num_bytes, u64 orig_parent, u64 parent,
  638. u64 ref_root, u64 ref_generation,
  639. u64 owner_objectid)
  640. {
  641. int ret;
  642. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  643. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  644. return 0;
  645. ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes,
  646. orig_parent, parent, ref_root,
  647. ref_root, ref_generation,
  648. ref_generation, owner_objectid);
  649. return ret;
  650. }
  651. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  652. struct btrfs_root *root, u64 bytenr,
  653. u64 num_bytes,
  654. u64 orig_parent, u64 parent,
  655. u64 orig_root, u64 ref_root,
  656. u64 orig_generation, u64 ref_generation,
  657. u64 owner_objectid)
  658. {
  659. int ret;
  660. ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root,
  661. ref_generation, owner_objectid,
  662. BTRFS_ADD_DELAYED_REF, 0);
  663. BUG_ON(ret);
  664. return ret;
  665. }
  666. static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans,
  667. struct btrfs_root *root, u64 bytenr,
  668. u64 num_bytes, u64 parent, u64 ref_root,
  669. u64 ref_generation, u64 owner_objectid,
  670. int refs_to_add)
  671. {
  672. struct btrfs_path *path;
  673. int ret;
  674. struct btrfs_key key;
  675. struct extent_buffer *l;
  676. struct btrfs_extent_item *item;
  677. u32 refs;
  678. path = btrfs_alloc_path();
  679. if (!path)
  680. return -ENOMEM;
  681. path->reada = 1;
  682. path->leave_spinning = 1;
  683. key.objectid = bytenr;
  684. key.type = BTRFS_EXTENT_ITEM_KEY;
  685. key.offset = num_bytes;
  686. /* first find the extent item and update its reference count */
  687. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
  688. path, 0, 1);
  689. if (ret < 0) {
  690. btrfs_set_path_blocking(path);
  691. return ret;
  692. }
  693. if (ret > 0) {
  694. WARN_ON(1);
  695. btrfs_free_path(path);
  696. return -EIO;
  697. }
  698. l = path->nodes[0];
  699. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  700. if (key.objectid != bytenr) {
  701. btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
  702. printk(KERN_ERR "btrfs wanted %llu found %llu\n",
  703. (unsigned long long)bytenr,
  704. (unsigned long long)key.objectid);
  705. BUG();
  706. }
  707. BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
  708. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  709. refs = btrfs_extent_refs(l, item);
  710. btrfs_set_extent_refs(l, item, refs + refs_to_add);
  711. btrfs_unlock_up_safe(path, 1);
  712. btrfs_mark_buffer_dirty(path->nodes[0]);
  713. btrfs_release_path(root->fs_info->extent_root, path);
  714. path->reada = 1;
  715. path->leave_spinning = 1;
  716. /* now insert the actual backref */
  717. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  718. path, bytenr, parent,
  719. ref_root, ref_generation,
  720. owner_objectid, refs_to_add);
  721. BUG_ON(ret);
  722. btrfs_free_path(path);
  723. return 0;
  724. }
  725. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  726. struct btrfs_root *root,
  727. u64 bytenr, u64 num_bytes, u64 parent,
  728. u64 ref_root, u64 ref_generation,
  729. u64 owner_objectid)
  730. {
  731. int ret;
  732. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  733. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  734. return 0;
  735. ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent,
  736. 0, ref_root, 0, ref_generation,
  737. owner_objectid);
  738. return ret;
  739. }
  740. static int drop_delayed_ref(struct btrfs_trans_handle *trans,
  741. struct btrfs_root *root,
  742. struct btrfs_delayed_ref_node *node)
  743. {
  744. int ret = 0;
  745. struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node);
  746. BUG_ON(node->ref_mod == 0);
  747. ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes,
  748. node->parent, ref->root, ref->generation,
  749. ref->owner_objectid, ref->pin, node->ref_mod);
  750. return ret;
  751. }
  752. /* helper function to actually process a single delayed ref entry */
  753. static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans,
  754. struct btrfs_root *root,
  755. struct btrfs_delayed_ref_node *node,
  756. int insert_reserved)
  757. {
  758. int ret;
  759. struct btrfs_delayed_ref *ref;
  760. if (node->parent == (u64)-1) {
  761. struct btrfs_delayed_ref_head *head;
  762. /*
  763. * we've hit the end of the chain and we were supposed
  764. * to insert this extent into the tree. But, it got
  765. * deleted before we ever needed to insert it, so all
  766. * we have to do is clean up the accounting
  767. */
  768. if (insert_reserved) {
  769. update_reserved_extents(root, node->bytenr,
  770. node->num_bytes, 0);
  771. }
  772. head = btrfs_delayed_node_to_head(node);
  773. mutex_unlock(&head->mutex);
  774. return 0;
  775. }
  776. ref = btrfs_delayed_node_to_ref(node);
  777. if (ref->action == BTRFS_ADD_DELAYED_REF) {
  778. if (insert_reserved) {
  779. struct btrfs_key ins;
  780. ins.objectid = node->bytenr;
  781. ins.offset = node->num_bytes;
  782. ins.type = BTRFS_EXTENT_ITEM_KEY;
  783. /* record the full extent allocation */
  784. ret = __btrfs_alloc_reserved_extent(trans, root,
  785. node->parent, ref->root,
  786. ref->generation, ref->owner_objectid,
  787. &ins, node->ref_mod);
  788. update_reserved_extents(root, node->bytenr,
  789. node->num_bytes, 0);
  790. } else {
  791. /* just add one backref */
  792. ret = add_extent_ref(trans, root, node->bytenr,
  793. node->num_bytes,
  794. node->parent, ref->root, ref->generation,
  795. ref->owner_objectid, node->ref_mod);
  796. }
  797. BUG_ON(ret);
  798. } else if (ref->action == BTRFS_DROP_DELAYED_REF) {
  799. WARN_ON(insert_reserved);
  800. ret = drop_delayed_ref(trans, root, node);
  801. }
  802. return 0;
  803. }
  804. static noinline struct btrfs_delayed_ref_node *
  805. select_delayed_ref(struct btrfs_delayed_ref_head *head)
  806. {
  807. struct rb_node *node;
  808. struct btrfs_delayed_ref_node *ref;
  809. int action = BTRFS_ADD_DELAYED_REF;
  810. again:
  811. /*
  812. * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
  813. * this prevents ref count from going down to zero when
  814. * there still are pending delayed ref.
  815. */
  816. node = rb_prev(&head->node.rb_node);
  817. while (1) {
  818. if (!node)
  819. break;
  820. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  821. rb_node);
  822. if (ref->bytenr != head->node.bytenr)
  823. break;
  824. if (btrfs_delayed_node_to_ref(ref)->action == action)
  825. return ref;
  826. node = rb_prev(node);
  827. }
  828. if (action == BTRFS_ADD_DELAYED_REF) {
  829. action = BTRFS_DROP_DELAYED_REF;
  830. goto again;
  831. }
  832. return NULL;
  833. }
  834. static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
  835. struct btrfs_root *root,
  836. struct list_head *cluster)
  837. {
  838. struct btrfs_delayed_ref_root *delayed_refs;
  839. struct btrfs_delayed_ref_node *ref;
  840. struct btrfs_delayed_ref_head *locked_ref = NULL;
  841. int ret;
  842. int count = 0;
  843. int must_insert_reserved = 0;
  844. delayed_refs = &trans->transaction->delayed_refs;
  845. while (1) {
  846. if (!locked_ref) {
  847. /* pick a new head ref from the cluster list */
  848. if (list_empty(cluster))
  849. break;
  850. locked_ref = list_entry(cluster->next,
  851. struct btrfs_delayed_ref_head, cluster);
  852. /* grab the lock that says we are going to process
  853. * all the refs for this head */
  854. ret = btrfs_delayed_ref_lock(trans, locked_ref);
  855. /*
  856. * we may have dropped the spin lock to get the head
  857. * mutex lock, and that might have given someone else
  858. * time to free the head. If that's true, it has been
  859. * removed from our list and we can move on.
  860. */
  861. if (ret == -EAGAIN) {
  862. locked_ref = NULL;
  863. count++;
  864. continue;
  865. }
  866. }
  867. /*
  868. * record the must insert reserved flag before we
  869. * drop the spin lock.
  870. */
  871. must_insert_reserved = locked_ref->must_insert_reserved;
  872. locked_ref->must_insert_reserved = 0;
  873. /*
  874. * locked_ref is the head node, so we have to go one
  875. * node back for any delayed ref updates
  876. */
  877. ref = select_delayed_ref(locked_ref);
  878. if (!ref) {
  879. /* All delayed refs have been processed, Go ahead
  880. * and send the head node to run_one_delayed_ref,
  881. * so that any accounting fixes can happen
  882. */
  883. ref = &locked_ref->node;
  884. list_del_init(&locked_ref->cluster);
  885. locked_ref = NULL;
  886. }
  887. ref->in_tree = 0;
  888. rb_erase(&ref->rb_node, &delayed_refs->root);
  889. delayed_refs->num_entries--;
  890. spin_unlock(&delayed_refs->lock);
  891. ret = run_one_delayed_ref(trans, root, ref,
  892. must_insert_reserved);
  893. BUG_ON(ret);
  894. btrfs_put_delayed_ref(ref);
  895. count++;
  896. cond_resched();
  897. spin_lock(&delayed_refs->lock);
  898. }
  899. return count;
  900. }
  901. /*
  902. * this starts processing the delayed reference count updates and
  903. * extent insertions we have queued up so far. count can be
  904. * 0, which means to process everything in the tree at the start
  905. * of the run (but not newly added entries), or it can be some target
  906. * number you'd like to process.
  907. */
  908. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  909. struct btrfs_root *root, unsigned long count)
  910. {
  911. struct rb_node *node;
  912. struct btrfs_delayed_ref_root *delayed_refs;
  913. struct btrfs_delayed_ref_node *ref;
  914. struct list_head cluster;
  915. int ret;
  916. int run_all = count == (unsigned long)-1;
  917. int run_most = 0;
  918. if (root == root->fs_info->extent_root)
  919. root = root->fs_info->tree_root;
  920. delayed_refs = &trans->transaction->delayed_refs;
  921. INIT_LIST_HEAD(&cluster);
  922. again:
  923. spin_lock(&delayed_refs->lock);
  924. if (count == 0) {
  925. count = delayed_refs->num_entries * 2;
  926. run_most = 1;
  927. }
  928. while (1) {
  929. if (!(run_all || run_most) &&
  930. delayed_refs->num_heads_ready < 64)
  931. break;
  932. /*
  933. * go find something we can process in the rbtree. We start at
  934. * the beginning of the tree, and then build a cluster
  935. * of refs to process starting at the first one we are able to
  936. * lock
  937. */
  938. ret = btrfs_find_ref_cluster(trans, &cluster,
  939. delayed_refs->run_delayed_start);
  940. if (ret)
  941. break;
  942. ret = run_clustered_refs(trans, root, &cluster);
  943. BUG_ON(ret < 0);
  944. count -= min_t(unsigned long, ret, count);
  945. if (count == 0)
  946. break;
  947. }
  948. if (run_all) {
  949. node = rb_first(&delayed_refs->root);
  950. if (!node)
  951. goto out;
  952. count = (unsigned long)-1;
  953. while (node) {
  954. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  955. rb_node);
  956. if (btrfs_delayed_ref_is_head(ref)) {
  957. struct btrfs_delayed_ref_head *head;
  958. head = btrfs_delayed_node_to_head(ref);
  959. atomic_inc(&ref->refs);
  960. spin_unlock(&delayed_refs->lock);
  961. mutex_lock(&head->mutex);
  962. mutex_unlock(&head->mutex);
  963. btrfs_put_delayed_ref(ref);
  964. cond_resched();
  965. goto again;
  966. }
  967. node = rb_next(node);
  968. }
  969. spin_unlock(&delayed_refs->lock);
  970. schedule_timeout(1);
  971. goto again;
  972. }
  973. out:
  974. spin_unlock(&delayed_refs->lock);
  975. return 0;
  976. }
  977. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  978. struct btrfs_root *root, u64 objectid, u64 bytenr)
  979. {
  980. struct btrfs_root *extent_root = root->fs_info->extent_root;
  981. struct btrfs_path *path;
  982. struct extent_buffer *leaf;
  983. struct btrfs_extent_ref *ref_item;
  984. struct btrfs_key key;
  985. struct btrfs_key found_key;
  986. u64 ref_root;
  987. u64 last_snapshot;
  988. u32 nritems;
  989. int ret;
  990. key.objectid = bytenr;
  991. key.offset = (u64)-1;
  992. key.type = BTRFS_EXTENT_ITEM_KEY;
  993. path = btrfs_alloc_path();
  994. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  995. if (ret < 0)
  996. goto out;
  997. BUG_ON(ret == 0);
  998. ret = -ENOENT;
  999. if (path->slots[0] == 0)
  1000. goto out;
  1001. path->slots[0]--;
  1002. leaf = path->nodes[0];
  1003. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1004. if (found_key.objectid != bytenr ||
  1005. found_key.type != BTRFS_EXTENT_ITEM_KEY)
  1006. goto out;
  1007. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1008. while (1) {
  1009. leaf = path->nodes[0];
  1010. nritems = btrfs_header_nritems(leaf);
  1011. if (path->slots[0] >= nritems) {
  1012. ret = btrfs_next_leaf(extent_root, path);
  1013. if (ret < 0)
  1014. goto out;
  1015. if (ret == 0)
  1016. continue;
  1017. break;
  1018. }
  1019. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1020. if (found_key.objectid != bytenr)
  1021. break;
  1022. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  1023. path->slots[0]++;
  1024. continue;
  1025. }
  1026. ref_item = btrfs_item_ptr(leaf, path->slots[0],
  1027. struct btrfs_extent_ref);
  1028. ref_root = btrfs_ref_root(leaf, ref_item);
  1029. if ((ref_root != root->root_key.objectid &&
  1030. ref_root != BTRFS_TREE_LOG_OBJECTID) ||
  1031. objectid != btrfs_ref_objectid(leaf, ref_item)) {
  1032. ret = 1;
  1033. goto out;
  1034. }
  1035. if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
  1036. ret = 1;
  1037. goto out;
  1038. }
  1039. path->slots[0]++;
  1040. }
  1041. ret = 0;
  1042. out:
  1043. btrfs_free_path(path);
  1044. return ret;
  1045. }
  1046. int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1047. struct extent_buffer *buf, u32 nr_extents)
  1048. {
  1049. struct btrfs_key key;
  1050. struct btrfs_file_extent_item *fi;
  1051. u64 root_gen;
  1052. u32 nritems;
  1053. int i;
  1054. int level;
  1055. int ret = 0;
  1056. int shared = 0;
  1057. if (!root->ref_cows)
  1058. return 0;
  1059. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  1060. shared = 0;
  1061. root_gen = root->root_key.offset;
  1062. } else {
  1063. shared = 1;
  1064. root_gen = trans->transid - 1;
  1065. }
  1066. level = btrfs_header_level(buf);
  1067. nritems = btrfs_header_nritems(buf);
  1068. if (level == 0) {
  1069. struct btrfs_leaf_ref *ref;
  1070. struct btrfs_extent_info *info;
  1071. ref = btrfs_alloc_leaf_ref(root, nr_extents);
  1072. if (!ref) {
  1073. ret = -ENOMEM;
  1074. goto out;
  1075. }
  1076. ref->root_gen = root_gen;
  1077. ref->bytenr = buf->start;
  1078. ref->owner = btrfs_header_owner(buf);
  1079. ref->generation = btrfs_header_generation(buf);
  1080. ref->nritems = nr_extents;
  1081. info = ref->extents;
  1082. for (i = 0; nr_extents > 0 && i < nritems; i++) {
  1083. u64 disk_bytenr;
  1084. btrfs_item_key_to_cpu(buf, &key, i);
  1085. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1086. continue;
  1087. fi = btrfs_item_ptr(buf, i,
  1088. struct btrfs_file_extent_item);
  1089. if (btrfs_file_extent_type(buf, fi) ==
  1090. BTRFS_FILE_EXTENT_INLINE)
  1091. continue;
  1092. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1093. if (disk_bytenr == 0)
  1094. continue;
  1095. info->bytenr = disk_bytenr;
  1096. info->num_bytes =
  1097. btrfs_file_extent_disk_num_bytes(buf, fi);
  1098. info->objectid = key.objectid;
  1099. info->offset = key.offset;
  1100. info++;
  1101. }
  1102. ret = btrfs_add_leaf_ref(root, ref, shared);
  1103. if (ret == -EEXIST && shared) {
  1104. struct btrfs_leaf_ref *old;
  1105. old = btrfs_lookup_leaf_ref(root, ref->bytenr);
  1106. BUG_ON(!old);
  1107. btrfs_remove_leaf_ref(root, old);
  1108. btrfs_free_leaf_ref(root, old);
  1109. ret = btrfs_add_leaf_ref(root, ref, shared);
  1110. }
  1111. WARN_ON(ret);
  1112. btrfs_free_leaf_ref(root, ref);
  1113. }
  1114. out:
  1115. return ret;
  1116. }
  1117. /* when a block goes through cow, we update the reference counts of
  1118. * everything that block points to. The internal pointers of the block
  1119. * can be in just about any order, and it is likely to have clusters of
  1120. * things that are close together and clusters of things that are not.
  1121. *
  1122. * To help reduce the seeks that come with updating all of these reference
  1123. * counts, sort them by byte number before actual updates are done.
  1124. *
  1125. * struct refsort is used to match byte number to slot in the btree block.
  1126. * we sort based on the byte number and then use the slot to actually
  1127. * find the item.
  1128. *
  1129. * struct refsort is smaller than strcut btrfs_item and smaller than
  1130. * struct btrfs_key_ptr. Since we're currently limited to the page size
  1131. * for a btree block, there's no way for a kmalloc of refsorts for a
  1132. * single node to be bigger than a page.
  1133. */
  1134. struct refsort {
  1135. u64 bytenr;
  1136. u32 slot;
  1137. };
  1138. /*
  1139. * for passing into sort()
  1140. */
  1141. static int refsort_cmp(const void *a_void, const void *b_void)
  1142. {
  1143. const struct refsort *a = a_void;
  1144. const struct refsort *b = b_void;
  1145. if (a->bytenr < b->bytenr)
  1146. return -1;
  1147. if (a->bytenr > b->bytenr)
  1148. return 1;
  1149. return 0;
  1150. }
  1151. noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
  1152. struct btrfs_root *root,
  1153. struct extent_buffer *orig_buf,
  1154. struct extent_buffer *buf, u32 *nr_extents)
  1155. {
  1156. u64 bytenr;
  1157. u64 ref_root;
  1158. u64 orig_root;
  1159. u64 ref_generation;
  1160. u64 orig_generation;
  1161. struct refsort *sorted;
  1162. u32 nritems;
  1163. u32 nr_file_extents = 0;
  1164. struct btrfs_key key;
  1165. struct btrfs_file_extent_item *fi;
  1166. int i;
  1167. int level;
  1168. int ret = 0;
  1169. int faili = 0;
  1170. int refi = 0;
  1171. int slot;
  1172. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  1173. u64, u64, u64, u64, u64, u64, u64, u64, u64);
  1174. ref_root = btrfs_header_owner(buf);
  1175. ref_generation = btrfs_header_generation(buf);
  1176. orig_root = btrfs_header_owner(orig_buf);
  1177. orig_generation = btrfs_header_generation(orig_buf);
  1178. nritems = btrfs_header_nritems(buf);
  1179. level = btrfs_header_level(buf);
  1180. sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
  1181. BUG_ON(!sorted);
  1182. if (root->ref_cows) {
  1183. process_func = __btrfs_inc_extent_ref;
  1184. } else {
  1185. if (level == 0 &&
  1186. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1187. goto out;
  1188. if (level != 0 &&
  1189. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1190. goto out;
  1191. process_func = __btrfs_update_extent_ref;
  1192. }
  1193. /*
  1194. * we make two passes through the items. In the first pass we
  1195. * only record the byte number and slot. Then we sort based on
  1196. * byte number and do the actual work based on the sorted results
  1197. */
  1198. for (i = 0; i < nritems; i++) {
  1199. cond_resched();
  1200. if (level == 0) {
  1201. btrfs_item_key_to_cpu(buf, &key, i);
  1202. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1203. continue;
  1204. fi = btrfs_item_ptr(buf, i,
  1205. struct btrfs_file_extent_item);
  1206. if (btrfs_file_extent_type(buf, fi) ==
  1207. BTRFS_FILE_EXTENT_INLINE)
  1208. continue;
  1209. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1210. if (bytenr == 0)
  1211. continue;
  1212. nr_file_extents++;
  1213. sorted[refi].bytenr = bytenr;
  1214. sorted[refi].slot = i;
  1215. refi++;
  1216. } else {
  1217. bytenr = btrfs_node_blockptr(buf, i);
  1218. sorted[refi].bytenr = bytenr;
  1219. sorted[refi].slot = i;
  1220. refi++;
  1221. }
  1222. }
  1223. /*
  1224. * if refi == 0, we didn't actually put anything into the sorted
  1225. * array and we're done
  1226. */
  1227. if (refi == 0)
  1228. goto out;
  1229. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  1230. for (i = 0; i < refi; i++) {
  1231. cond_resched();
  1232. slot = sorted[i].slot;
  1233. bytenr = sorted[i].bytenr;
  1234. if (level == 0) {
  1235. btrfs_item_key_to_cpu(buf, &key, slot);
  1236. fi = btrfs_item_ptr(buf, slot,
  1237. struct btrfs_file_extent_item);
  1238. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1239. if (bytenr == 0)
  1240. continue;
  1241. ret = process_func(trans, root, bytenr,
  1242. btrfs_file_extent_disk_num_bytes(buf, fi),
  1243. orig_buf->start, buf->start,
  1244. orig_root, ref_root,
  1245. orig_generation, ref_generation,
  1246. key.objectid);
  1247. if (ret) {
  1248. faili = slot;
  1249. WARN_ON(1);
  1250. goto fail;
  1251. }
  1252. } else {
  1253. ret = process_func(trans, root, bytenr, buf->len,
  1254. orig_buf->start, buf->start,
  1255. orig_root, ref_root,
  1256. orig_generation, ref_generation,
  1257. level - 1);
  1258. if (ret) {
  1259. faili = slot;
  1260. WARN_ON(1);
  1261. goto fail;
  1262. }
  1263. }
  1264. }
  1265. out:
  1266. kfree(sorted);
  1267. if (nr_extents) {
  1268. if (level == 0)
  1269. *nr_extents = nr_file_extents;
  1270. else
  1271. *nr_extents = nritems;
  1272. }
  1273. return 0;
  1274. fail:
  1275. kfree(sorted);
  1276. WARN_ON(1);
  1277. return ret;
  1278. }
  1279. int btrfs_update_ref(struct btrfs_trans_handle *trans,
  1280. struct btrfs_root *root, struct extent_buffer *orig_buf,
  1281. struct extent_buffer *buf, int start_slot, int nr)
  1282. {
  1283. u64 bytenr;
  1284. u64 ref_root;
  1285. u64 orig_root;
  1286. u64 ref_generation;
  1287. u64 orig_generation;
  1288. struct btrfs_key key;
  1289. struct btrfs_file_extent_item *fi;
  1290. int i;
  1291. int ret;
  1292. int slot;
  1293. int level;
  1294. BUG_ON(start_slot < 0);
  1295. BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
  1296. ref_root = btrfs_header_owner(buf);
  1297. ref_generation = btrfs_header_generation(buf);
  1298. orig_root = btrfs_header_owner(orig_buf);
  1299. orig_generation = btrfs_header_generation(orig_buf);
  1300. level = btrfs_header_level(buf);
  1301. if (!root->ref_cows) {
  1302. if (level == 0 &&
  1303. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1304. return 0;
  1305. if (level != 0 &&
  1306. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1307. return 0;
  1308. }
  1309. for (i = 0, slot = start_slot; i < nr; i++, slot++) {
  1310. cond_resched();
  1311. if (level == 0) {
  1312. btrfs_item_key_to_cpu(buf, &key, slot);
  1313. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1314. continue;
  1315. fi = btrfs_item_ptr(buf, slot,
  1316. struct btrfs_file_extent_item);
  1317. if (btrfs_file_extent_type(buf, fi) ==
  1318. BTRFS_FILE_EXTENT_INLINE)
  1319. continue;
  1320. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1321. if (bytenr == 0)
  1322. continue;
  1323. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1324. btrfs_file_extent_disk_num_bytes(buf, fi),
  1325. orig_buf->start, buf->start,
  1326. orig_root, ref_root, orig_generation,
  1327. ref_generation, key.objectid);
  1328. if (ret)
  1329. goto fail;
  1330. } else {
  1331. bytenr = btrfs_node_blockptr(buf, slot);
  1332. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1333. buf->len, orig_buf->start,
  1334. buf->start, orig_root, ref_root,
  1335. orig_generation, ref_generation,
  1336. level - 1);
  1337. if (ret)
  1338. goto fail;
  1339. }
  1340. }
  1341. return 0;
  1342. fail:
  1343. WARN_ON(1);
  1344. return -1;
  1345. }
  1346. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  1347. struct btrfs_root *root,
  1348. struct btrfs_path *path,
  1349. struct btrfs_block_group_cache *cache)
  1350. {
  1351. int ret;
  1352. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1353. unsigned long bi;
  1354. struct extent_buffer *leaf;
  1355. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  1356. if (ret < 0)
  1357. goto fail;
  1358. BUG_ON(ret);
  1359. leaf = path->nodes[0];
  1360. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1361. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  1362. btrfs_mark_buffer_dirty(leaf);
  1363. btrfs_release_path(extent_root, path);
  1364. fail:
  1365. if (ret)
  1366. return ret;
  1367. return 0;
  1368. }
  1369. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  1370. struct btrfs_root *root)
  1371. {
  1372. struct btrfs_block_group_cache *cache, *entry;
  1373. struct rb_node *n;
  1374. int err = 0;
  1375. int werr = 0;
  1376. struct btrfs_path *path;
  1377. u64 last = 0;
  1378. path = btrfs_alloc_path();
  1379. if (!path)
  1380. return -ENOMEM;
  1381. while (1) {
  1382. cache = NULL;
  1383. spin_lock(&root->fs_info->block_group_cache_lock);
  1384. for (n = rb_first(&root->fs_info->block_group_cache_tree);
  1385. n; n = rb_next(n)) {
  1386. entry = rb_entry(n, struct btrfs_block_group_cache,
  1387. cache_node);
  1388. if (entry->dirty) {
  1389. cache = entry;
  1390. break;
  1391. }
  1392. }
  1393. spin_unlock(&root->fs_info->block_group_cache_lock);
  1394. if (!cache)
  1395. break;
  1396. cache->dirty = 0;
  1397. last += cache->key.offset;
  1398. err = write_one_cache_group(trans, root,
  1399. path, cache);
  1400. /*
  1401. * if we fail to write the cache group, we want
  1402. * to keep it marked dirty in hopes that a later
  1403. * write will work
  1404. */
  1405. if (err) {
  1406. werr = err;
  1407. continue;
  1408. }
  1409. }
  1410. btrfs_free_path(path);
  1411. return werr;
  1412. }
  1413. int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
  1414. {
  1415. struct btrfs_block_group_cache *block_group;
  1416. int readonly = 0;
  1417. block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
  1418. if (!block_group || block_group->ro)
  1419. readonly = 1;
  1420. if (block_group)
  1421. btrfs_put_block_group(block_group);
  1422. return readonly;
  1423. }
  1424. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  1425. u64 total_bytes, u64 bytes_used,
  1426. struct btrfs_space_info **space_info)
  1427. {
  1428. struct btrfs_space_info *found;
  1429. found = __find_space_info(info, flags);
  1430. if (found) {
  1431. spin_lock(&found->lock);
  1432. found->total_bytes += total_bytes;
  1433. found->bytes_used += bytes_used;
  1434. found->full = 0;
  1435. spin_unlock(&found->lock);
  1436. *space_info = found;
  1437. return 0;
  1438. }
  1439. found = kzalloc(sizeof(*found), GFP_NOFS);
  1440. if (!found)
  1441. return -ENOMEM;
  1442. INIT_LIST_HEAD(&found->block_groups);
  1443. init_rwsem(&found->groups_sem);
  1444. spin_lock_init(&found->lock);
  1445. found->flags = flags;
  1446. found->total_bytes = total_bytes;
  1447. found->bytes_used = bytes_used;
  1448. found->bytes_pinned = 0;
  1449. found->bytes_reserved = 0;
  1450. found->bytes_readonly = 0;
  1451. found->bytes_delalloc = 0;
  1452. found->full = 0;
  1453. found->force_alloc = 0;
  1454. *space_info = found;
  1455. list_add_rcu(&found->list, &info->space_info);
  1456. return 0;
  1457. }
  1458. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  1459. {
  1460. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  1461. BTRFS_BLOCK_GROUP_RAID1 |
  1462. BTRFS_BLOCK_GROUP_RAID10 |
  1463. BTRFS_BLOCK_GROUP_DUP);
  1464. if (extra_flags) {
  1465. if (flags & BTRFS_BLOCK_GROUP_DATA)
  1466. fs_info->avail_data_alloc_bits |= extra_flags;
  1467. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  1468. fs_info->avail_metadata_alloc_bits |= extra_flags;
  1469. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  1470. fs_info->avail_system_alloc_bits |= extra_flags;
  1471. }
  1472. }
  1473. static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
  1474. {
  1475. spin_lock(&cache->space_info->lock);
  1476. spin_lock(&cache->lock);
  1477. if (!cache->ro) {
  1478. cache->space_info->bytes_readonly += cache->key.offset -
  1479. btrfs_block_group_used(&cache->item);
  1480. cache->ro = 1;
  1481. }
  1482. spin_unlock(&cache->lock);
  1483. spin_unlock(&cache->space_info->lock);
  1484. }
  1485. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  1486. {
  1487. u64 num_devices = root->fs_info->fs_devices->rw_devices;
  1488. if (num_devices == 1)
  1489. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  1490. if (num_devices < 4)
  1491. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  1492. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  1493. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  1494. BTRFS_BLOCK_GROUP_RAID10))) {
  1495. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  1496. }
  1497. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  1498. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  1499. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  1500. }
  1501. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  1502. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  1503. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  1504. (flags & BTRFS_BLOCK_GROUP_DUP)))
  1505. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  1506. return flags;
  1507. }
  1508. static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
  1509. {
  1510. struct btrfs_fs_info *info = root->fs_info;
  1511. u64 alloc_profile;
  1512. if (data) {
  1513. alloc_profile = info->avail_data_alloc_bits &
  1514. info->data_alloc_profile;
  1515. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  1516. } else if (root == root->fs_info->chunk_root) {
  1517. alloc_profile = info->avail_system_alloc_bits &
  1518. info->system_alloc_profile;
  1519. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  1520. } else {
  1521. alloc_profile = info->avail_metadata_alloc_bits &
  1522. info->metadata_alloc_profile;
  1523. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  1524. }
  1525. return btrfs_reduce_alloc_profile(root, data);
  1526. }
  1527. void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
  1528. {
  1529. u64 alloc_target;
  1530. alloc_target = btrfs_get_alloc_profile(root, 1);
  1531. BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
  1532. alloc_target);
  1533. }
  1534. /*
  1535. * for now this just makes sure we have at least 5% of our metadata space free
  1536. * for use.
  1537. */
  1538. int btrfs_check_metadata_free_space(struct btrfs_root *root)
  1539. {
  1540. struct btrfs_fs_info *info = root->fs_info;
  1541. struct btrfs_space_info *meta_sinfo;
  1542. u64 alloc_target, thresh;
  1543. int committed = 0, ret;
  1544. /* get the space info for where the metadata will live */
  1545. alloc_target = btrfs_get_alloc_profile(root, 0);
  1546. meta_sinfo = __find_space_info(info, alloc_target);
  1547. again:
  1548. spin_lock(&meta_sinfo->lock);
  1549. if (!meta_sinfo->full)
  1550. thresh = meta_sinfo->total_bytes * 80;
  1551. else
  1552. thresh = meta_sinfo->total_bytes * 95;
  1553. do_div(thresh, 100);
  1554. if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
  1555. meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
  1556. struct btrfs_trans_handle *trans;
  1557. if (!meta_sinfo->full) {
  1558. meta_sinfo->force_alloc = 1;
  1559. spin_unlock(&meta_sinfo->lock);
  1560. trans = btrfs_start_transaction(root, 1);
  1561. if (!trans)
  1562. return -ENOMEM;
  1563. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  1564. 2 * 1024 * 1024, alloc_target, 0);
  1565. btrfs_end_transaction(trans, root);
  1566. goto again;
  1567. }
  1568. spin_unlock(&meta_sinfo->lock);
  1569. if (!committed) {
  1570. committed = 1;
  1571. trans = btrfs_join_transaction(root, 1);
  1572. if (!trans)
  1573. return -ENOMEM;
  1574. ret = btrfs_commit_transaction(trans, root);
  1575. if (ret)
  1576. return ret;
  1577. goto again;
  1578. }
  1579. return -ENOSPC;
  1580. }
  1581. spin_unlock(&meta_sinfo->lock);
  1582. return 0;
  1583. }
  1584. /*
  1585. * This will check the space that the inode allocates from to make sure we have
  1586. * enough space for bytes.
  1587. */
  1588. int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
  1589. u64 bytes)
  1590. {
  1591. struct btrfs_space_info *data_sinfo;
  1592. int ret = 0, committed = 0;
  1593. /* make sure bytes are sectorsize aligned */
  1594. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  1595. data_sinfo = BTRFS_I(inode)->space_info;
  1596. again:
  1597. /* make sure we have enough space to handle the data first */
  1598. spin_lock(&data_sinfo->lock);
  1599. if (data_sinfo->total_bytes - data_sinfo->bytes_used -
  1600. data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
  1601. data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
  1602. data_sinfo->bytes_may_use < bytes) {
  1603. struct btrfs_trans_handle *trans;
  1604. /*
  1605. * if we don't have enough free bytes in this space then we need
  1606. * to alloc a new chunk.
  1607. */
  1608. if (!data_sinfo->full) {
  1609. u64 alloc_target;
  1610. data_sinfo->force_alloc = 1;
  1611. spin_unlock(&data_sinfo->lock);
  1612. alloc_target = btrfs_get_alloc_profile(root, 1);
  1613. trans = btrfs_start_transaction(root, 1);
  1614. if (!trans)
  1615. return -ENOMEM;
  1616. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  1617. bytes + 2 * 1024 * 1024,
  1618. alloc_target, 0);
  1619. btrfs_end_transaction(trans, root);
  1620. if (ret)
  1621. return ret;
  1622. goto again;
  1623. }
  1624. spin_unlock(&data_sinfo->lock);
  1625. /* commit the current transaction and try again */
  1626. if (!committed) {
  1627. committed = 1;
  1628. trans = btrfs_join_transaction(root, 1);
  1629. if (!trans)
  1630. return -ENOMEM;
  1631. ret = btrfs_commit_transaction(trans, root);
  1632. if (ret)
  1633. return ret;
  1634. goto again;
  1635. }
  1636. printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
  1637. ", %llu bytes_used, %llu bytes_reserved, "
  1638. "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
  1639. "%llu total\n", (unsigned long long)bytes,
  1640. (unsigned long long)data_sinfo->bytes_delalloc,
  1641. (unsigned long long)data_sinfo->bytes_used,
  1642. (unsigned long long)data_sinfo->bytes_reserved,
  1643. (unsigned long long)data_sinfo->bytes_pinned,
  1644. (unsigned long long)data_sinfo->bytes_readonly,
  1645. (unsigned long long)data_sinfo->bytes_may_use,
  1646. (unsigned long long)data_sinfo->total_bytes);
  1647. return -ENOSPC;
  1648. }
  1649. data_sinfo->bytes_may_use += bytes;
  1650. BTRFS_I(inode)->reserved_bytes += bytes;
  1651. spin_unlock(&data_sinfo->lock);
  1652. return btrfs_check_metadata_free_space(root);
  1653. }
  1654. /*
  1655. * if there was an error for whatever reason after calling
  1656. * btrfs_check_data_free_space, call this so we can cleanup the counters.
  1657. */
  1658. void btrfs_free_reserved_data_space(struct btrfs_root *root,
  1659. struct inode *inode, u64 bytes)
  1660. {
  1661. struct btrfs_space_info *data_sinfo;
  1662. /* make sure bytes are sectorsize aligned */
  1663. bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  1664. data_sinfo = BTRFS_I(inode)->space_info;
  1665. spin_lock(&data_sinfo->lock);
  1666. data_sinfo->bytes_may_use -= bytes;
  1667. BTRFS_I(inode)->reserved_bytes -= bytes;
  1668. spin_unlock(&data_sinfo->lock);
  1669. }
  1670. /* called when we are adding a delalloc extent to the inode's io_tree */
  1671. void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
  1672. u64 bytes)
  1673. {
  1674. struct btrfs_space_info *data_sinfo;
  1675. /* get the space info for where this inode will be storing its data */
  1676. data_sinfo = BTRFS_I(inode)->space_info;
  1677. /* make sure we have enough space to handle the data first */
  1678. spin_lock(&data_sinfo->lock);
  1679. data_sinfo->bytes_delalloc += bytes;
  1680. /*
  1681. * we are adding a delalloc extent without calling
  1682. * btrfs_check_data_free_space first. This happens on a weird
  1683. * writepage condition, but shouldn't hurt our accounting
  1684. */
  1685. if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
  1686. data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
  1687. BTRFS_I(inode)->reserved_bytes = 0;
  1688. } else {
  1689. data_sinfo->bytes_may_use -= bytes;
  1690. BTRFS_I(inode)->reserved_bytes -= bytes;
  1691. }
  1692. spin_unlock(&data_sinfo->lock);
  1693. }
  1694. /* called when we are clearing an delalloc extent from the inode's io_tree */
  1695. void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
  1696. u64 bytes)
  1697. {
  1698. struct btrfs_space_info *info;
  1699. info = BTRFS_I(inode)->space_info;
  1700. spin_lock(&info->lock);
  1701. info->bytes_delalloc -= bytes;
  1702. spin_unlock(&info->lock);
  1703. }
  1704. static void force_metadata_allocation(struct btrfs_fs_info *info)
  1705. {
  1706. struct list_head *head = &info->space_info;
  1707. struct btrfs_space_info *found;
  1708. rcu_read_lock();
  1709. list_for_each_entry_rcu(found, head, list) {
  1710. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  1711. found->force_alloc = 1;
  1712. }
  1713. rcu_read_unlock();
  1714. }
  1715. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  1716. struct btrfs_root *extent_root, u64 alloc_bytes,
  1717. u64 flags, int force)
  1718. {
  1719. struct btrfs_space_info *space_info;
  1720. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  1721. u64 thresh;
  1722. int ret = 0;
  1723. mutex_lock(&fs_info->chunk_mutex);
  1724. flags = btrfs_reduce_alloc_profile(extent_root, flags);
  1725. space_info = __find_space_info(extent_root->fs_info, flags);
  1726. if (!space_info) {
  1727. ret = update_space_info(extent_root->fs_info, flags,
  1728. 0, 0, &space_info);
  1729. BUG_ON(ret);
  1730. }
  1731. BUG_ON(!space_info);
  1732. spin_lock(&space_info->lock);
  1733. if (space_info->force_alloc) {
  1734. force = 1;
  1735. space_info->force_alloc = 0;
  1736. }
  1737. if (space_info->full) {
  1738. spin_unlock(&space_info->lock);
  1739. goto out;
  1740. }
  1741. thresh = space_info->total_bytes - space_info->bytes_readonly;
  1742. thresh = div_factor(thresh, 6);
  1743. if (!force &&
  1744. (space_info->bytes_used + space_info->bytes_pinned +
  1745. space_info->bytes_reserved + alloc_bytes) < thresh) {
  1746. spin_unlock(&space_info->lock);
  1747. goto out;
  1748. }
  1749. spin_unlock(&space_info->lock);
  1750. /*
  1751. * if we're doing a data chunk, go ahead and make sure that
  1752. * we keep a reasonable number of metadata chunks allocated in the
  1753. * FS as well.
  1754. */
  1755. if (flags & BTRFS_BLOCK_GROUP_DATA) {
  1756. fs_info->data_chunk_allocations++;
  1757. if (!(fs_info->data_chunk_allocations %
  1758. fs_info->metadata_ratio))
  1759. force_metadata_allocation(fs_info);
  1760. }
  1761. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  1762. if (ret)
  1763. space_info->full = 1;
  1764. out:
  1765. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  1766. return ret;
  1767. }
  1768. static int update_block_group(struct btrfs_trans_handle *trans,
  1769. struct btrfs_root *root,
  1770. u64 bytenr, u64 num_bytes, int alloc,
  1771. int mark_free)
  1772. {
  1773. struct btrfs_block_group_cache *cache;
  1774. struct btrfs_fs_info *info = root->fs_info;
  1775. u64 total = num_bytes;
  1776. u64 old_val;
  1777. u64 byte_in_group;
  1778. while (total) {
  1779. cache = btrfs_lookup_block_group(info, bytenr);
  1780. if (!cache)
  1781. return -1;
  1782. byte_in_group = bytenr - cache->key.objectid;
  1783. WARN_ON(byte_in_group > cache->key.offset);
  1784. spin_lock(&cache->space_info->lock);
  1785. spin_lock(&cache->lock);
  1786. cache->dirty = 1;
  1787. old_val = btrfs_block_group_used(&cache->item);
  1788. num_bytes = min(total, cache->key.offset - byte_in_group);
  1789. if (alloc) {
  1790. old_val += num_bytes;
  1791. cache->space_info->bytes_used += num_bytes;
  1792. if (cache->ro)
  1793. cache->space_info->bytes_readonly -= num_bytes;
  1794. btrfs_set_block_group_used(&cache->item, old_val);
  1795. spin_unlock(&cache->lock);
  1796. spin_unlock(&cache->space_info->lock);
  1797. } else {
  1798. old_val -= num_bytes;
  1799. cache->space_info->bytes_used -= num_bytes;
  1800. if (cache->ro)
  1801. cache->space_info->bytes_readonly += num_bytes;
  1802. btrfs_set_block_group_used(&cache->item, old_val);
  1803. spin_unlock(&cache->lock);
  1804. spin_unlock(&cache->space_info->lock);
  1805. if (mark_free) {
  1806. int ret;
  1807. ret = btrfs_discard_extent(root, bytenr,
  1808. num_bytes);
  1809. WARN_ON(ret);
  1810. ret = btrfs_add_free_space(cache, bytenr,
  1811. num_bytes);
  1812. WARN_ON(ret);
  1813. }
  1814. }
  1815. btrfs_put_block_group(cache);
  1816. total -= num_bytes;
  1817. bytenr += num_bytes;
  1818. }
  1819. return 0;
  1820. }
  1821. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  1822. {
  1823. struct btrfs_block_group_cache *cache;
  1824. u64 bytenr;
  1825. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  1826. if (!cache)
  1827. return 0;
  1828. bytenr = cache->key.objectid;
  1829. btrfs_put_block_group(cache);
  1830. return bytenr;
  1831. }
  1832. int btrfs_update_pinned_extents(struct btrfs_root *root,
  1833. u64 bytenr, u64 num, int pin)
  1834. {
  1835. u64 len;
  1836. struct btrfs_block_group_cache *cache;
  1837. struct btrfs_fs_info *fs_info = root->fs_info;
  1838. if (pin) {
  1839. set_extent_dirty(&fs_info->pinned_extents,
  1840. bytenr, bytenr + num - 1, GFP_NOFS);
  1841. } else {
  1842. clear_extent_dirty(&fs_info->pinned_extents,
  1843. bytenr, bytenr + num - 1, GFP_NOFS);
  1844. }
  1845. while (num > 0) {
  1846. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1847. BUG_ON(!cache);
  1848. len = min(num, cache->key.offset -
  1849. (bytenr - cache->key.objectid));
  1850. if (pin) {
  1851. spin_lock(&cache->space_info->lock);
  1852. spin_lock(&cache->lock);
  1853. cache->pinned += len;
  1854. cache->space_info->bytes_pinned += len;
  1855. spin_unlock(&cache->lock);
  1856. spin_unlock(&cache->space_info->lock);
  1857. fs_info->total_pinned += len;
  1858. } else {
  1859. spin_lock(&cache->space_info->lock);
  1860. spin_lock(&cache->lock);
  1861. cache->pinned -= len;
  1862. cache->space_info->bytes_pinned -= len;
  1863. spin_unlock(&cache->lock);
  1864. spin_unlock(&cache->space_info->lock);
  1865. fs_info->total_pinned -= len;
  1866. if (cache->cached)
  1867. btrfs_add_free_space(cache, bytenr, len);
  1868. }
  1869. btrfs_put_block_group(cache);
  1870. bytenr += len;
  1871. num -= len;
  1872. }
  1873. return 0;
  1874. }
  1875. static int update_reserved_extents(struct btrfs_root *root,
  1876. u64 bytenr, u64 num, int reserve)
  1877. {
  1878. u64 len;
  1879. struct btrfs_block_group_cache *cache;
  1880. struct btrfs_fs_info *fs_info = root->fs_info;
  1881. while (num > 0) {
  1882. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1883. BUG_ON(!cache);
  1884. len = min(num, cache->key.offset -
  1885. (bytenr - cache->key.objectid));
  1886. spin_lock(&cache->space_info->lock);
  1887. spin_lock(&cache->lock);
  1888. if (reserve) {
  1889. cache->reserved += len;
  1890. cache->space_info->bytes_reserved += len;
  1891. } else {
  1892. cache->reserved -= len;
  1893. cache->space_info->bytes_reserved -= len;
  1894. }
  1895. spin_unlock(&cache->lock);
  1896. spin_unlock(&cache->space_info->lock);
  1897. btrfs_put_block_group(cache);
  1898. bytenr += len;
  1899. num -= len;
  1900. }
  1901. return 0;
  1902. }
  1903. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  1904. {
  1905. u64 last = 0;
  1906. u64 start;
  1907. u64 end;
  1908. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  1909. int ret;
  1910. while (1) {
  1911. ret = find_first_extent_bit(pinned_extents, last,
  1912. &start, &end, EXTENT_DIRTY);
  1913. if (ret)
  1914. break;
  1915. set_extent_dirty(copy, start, end, GFP_NOFS);
  1916. last = end + 1;
  1917. }
  1918. return 0;
  1919. }
  1920. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  1921. struct btrfs_root *root,
  1922. struct extent_io_tree *unpin)
  1923. {
  1924. u64 start;
  1925. u64 end;
  1926. int ret;
  1927. while (1) {
  1928. ret = find_first_extent_bit(unpin, 0, &start, &end,
  1929. EXTENT_DIRTY);
  1930. if (ret)
  1931. break;
  1932. ret = btrfs_discard_extent(root, start, end + 1 - start);
  1933. /* unlocks the pinned mutex */
  1934. btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
  1935. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  1936. cond_resched();
  1937. }
  1938. return ret;
  1939. }
  1940. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  1941. struct btrfs_root *root,
  1942. struct btrfs_path *path,
  1943. u64 bytenr, u64 num_bytes, int is_data,
  1944. struct extent_buffer **must_clean)
  1945. {
  1946. int err = 0;
  1947. struct extent_buffer *buf;
  1948. if (is_data)
  1949. goto pinit;
  1950. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  1951. if (!buf)
  1952. goto pinit;
  1953. /* we can reuse a block if it hasn't been written
  1954. * and it is from this transaction. We can't
  1955. * reuse anything from the tree log root because
  1956. * it has tiny sub-transactions.
  1957. */
  1958. if (btrfs_buffer_uptodate(buf, 0) &&
  1959. btrfs_try_tree_lock(buf)) {
  1960. u64 header_owner = btrfs_header_owner(buf);
  1961. u64 header_transid = btrfs_header_generation(buf);
  1962. if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
  1963. header_owner != BTRFS_TREE_RELOC_OBJECTID &&
  1964. header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1965. header_transid == trans->transid &&
  1966. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  1967. *must_clean = buf;
  1968. return 1;
  1969. }
  1970. btrfs_tree_unlock(buf);
  1971. }
  1972. free_extent_buffer(buf);
  1973. pinit:
  1974. btrfs_set_path_blocking(path);
  1975. /* unlocks the pinned mutex */
  1976. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  1977. BUG_ON(err < 0);
  1978. return 0;
  1979. }
  1980. /*
  1981. * remove an extent from the root, returns 0 on success
  1982. */
  1983. static int __free_extent(struct btrfs_trans_handle *trans,
  1984. struct btrfs_root *root,
  1985. u64 bytenr, u64 num_bytes, u64 parent,
  1986. u64 root_objectid, u64 ref_generation,
  1987. u64 owner_objectid, int pin, int mark_free,
  1988. int refs_to_drop)
  1989. {
  1990. struct btrfs_path *path;
  1991. struct btrfs_key key;
  1992. struct btrfs_fs_info *info = root->fs_info;
  1993. struct btrfs_root *extent_root = info->extent_root;
  1994. struct extent_buffer *leaf;
  1995. int ret;
  1996. int extent_slot = 0;
  1997. int found_extent = 0;
  1998. int num_to_del = 1;
  1999. struct btrfs_extent_item *ei;
  2000. u32 refs;
  2001. key.objectid = bytenr;
  2002. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  2003. key.offset = num_bytes;
  2004. path = btrfs_alloc_path();
  2005. if (!path)
  2006. return -ENOMEM;
  2007. path->reada = 1;
  2008. path->leave_spinning = 1;
  2009. ret = lookup_extent_backref(trans, extent_root, path,
  2010. bytenr, parent, root_objectid,
  2011. ref_generation, owner_objectid, 1);
  2012. if (ret == 0) {
  2013. struct btrfs_key found_key;
  2014. extent_slot = path->slots[0];
  2015. while (extent_slot > 0) {
  2016. extent_slot--;
  2017. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2018. extent_slot);
  2019. if (found_key.objectid != bytenr)
  2020. break;
  2021. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  2022. found_key.offset == num_bytes) {
  2023. found_extent = 1;
  2024. break;
  2025. }
  2026. if (path->slots[0] - extent_slot > 5)
  2027. break;
  2028. }
  2029. if (!found_extent) {
  2030. ret = remove_extent_backref(trans, extent_root, path,
  2031. refs_to_drop);
  2032. BUG_ON(ret);
  2033. btrfs_release_path(extent_root, path);
  2034. path->leave_spinning = 1;
  2035. ret = btrfs_search_slot(trans, extent_root,
  2036. &key, path, -1, 1);
  2037. if (ret) {
  2038. printk(KERN_ERR "umm, got %d back from search"
  2039. ", was looking for %llu\n", ret,
  2040. (unsigned long long)bytenr);
  2041. btrfs_print_leaf(extent_root, path->nodes[0]);
  2042. }
  2043. BUG_ON(ret);
  2044. extent_slot = path->slots[0];
  2045. }
  2046. } else {
  2047. btrfs_print_leaf(extent_root, path->nodes[0]);
  2048. WARN_ON(1);
  2049. printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
  2050. "parent %llu root %llu gen %llu owner %llu\n",
  2051. (unsigned long long)bytenr,
  2052. (unsigned long long)parent,
  2053. (unsigned long long)root_objectid,
  2054. (unsigned long long)ref_generation,
  2055. (unsigned long long)owner_objectid);
  2056. }
  2057. leaf = path->nodes[0];
  2058. ei = btrfs_item_ptr(leaf, extent_slot,
  2059. struct btrfs_extent_item);
  2060. refs = btrfs_extent_refs(leaf, ei);
  2061. /*
  2062. * we're not allowed to delete the extent item if there
  2063. * are other delayed ref updates pending
  2064. */
  2065. BUG_ON(refs < refs_to_drop);
  2066. refs -= refs_to_drop;
  2067. btrfs_set_extent_refs(leaf, ei, refs);
  2068. btrfs_mark_buffer_dirty(leaf);
  2069. if (refs == 0 && found_extent &&
  2070. path->slots[0] == extent_slot + 1) {
  2071. struct btrfs_extent_ref *ref;
  2072. ref = btrfs_item_ptr(leaf, path->slots[0],
  2073. struct btrfs_extent_ref);
  2074. BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
  2075. /* if the back ref and the extent are next to each other
  2076. * they get deleted below in one shot
  2077. */
  2078. path->slots[0] = extent_slot;
  2079. num_to_del = 2;
  2080. } else if (found_extent) {
  2081. /* otherwise delete the extent back ref */
  2082. ret = remove_extent_backref(trans, extent_root, path,
  2083. refs_to_drop);
  2084. BUG_ON(ret);
  2085. /* if refs are 0, we need to setup the path for deletion */
  2086. if (refs == 0) {
  2087. btrfs_release_path(extent_root, path);
  2088. path->leave_spinning = 1;
  2089. ret = btrfs_search_slot(trans, extent_root, &key, path,
  2090. -1, 1);
  2091. BUG_ON(ret);
  2092. }
  2093. }
  2094. if (refs == 0) {
  2095. u64 super_used;
  2096. u64 root_used;
  2097. struct extent_buffer *must_clean = NULL;
  2098. if (pin) {
  2099. ret = pin_down_bytes(trans, root, path,
  2100. bytenr, num_bytes,
  2101. owner_objectid >= BTRFS_FIRST_FREE_OBJECTID,
  2102. &must_clean);
  2103. if (ret > 0)
  2104. mark_free = 1;
  2105. BUG_ON(ret < 0);
  2106. }
  2107. /* block accounting for super block */
  2108. spin_lock(&info->delalloc_lock);
  2109. super_used = btrfs_super_bytes_used(&info->super_copy);
  2110. btrfs_set_super_bytes_used(&info->super_copy,
  2111. super_used - num_bytes);
  2112. /* block accounting for root item */
  2113. root_used = btrfs_root_used(&root->root_item);
  2114. btrfs_set_root_used(&root->root_item,
  2115. root_used - num_bytes);
  2116. spin_unlock(&info->delalloc_lock);
  2117. /*
  2118. * it is going to be very rare for someone to be waiting
  2119. * on the block we're freeing. del_items might need to
  2120. * schedule, so rather than get fancy, just force it
  2121. * to blocking here
  2122. */
  2123. if (must_clean)
  2124. btrfs_set_lock_blocking(must_clean);
  2125. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  2126. num_to_del);
  2127. BUG_ON(ret);
  2128. btrfs_release_path(extent_root, path);
  2129. if (must_clean) {
  2130. clean_tree_block(NULL, root, must_clean);
  2131. btrfs_tree_unlock(must_clean);
  2132. free_extent_buffer(must_clean);
  2133. }
  2134. if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  2135. ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
  2136. BUG_ON(ret);
  2137. } else {
  2138. invalidate_mapping_pages(info->btree_inode->i_mapping,
  2139. bytenr >> PAGE_CACHE_SHIFT,
  2140. (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
  2141. }
  2142. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  2143. mark_free);
  2144. BUG_ON(ret);
  2145. }
  2146. btrfs_free_path(path);
  2147. return ret;
  2148. }
  2149. /*
  2150. * remove an extent from the root, returns 0 on success
  2151. */
  2152. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  2153. struct btrfs_root *root,
  2154. u64 bytenr, u64 num_bytes, u64 parent,
  2155. u64 root_objectid, u64 ref_generation,
  2156. u64 owner_objectid, int pin,
  2157. int refs_to_drop)
  2158. {
  2159. WARN_ON(num_bytes < root->sectorsize);
  2160. /*
  2161. * if metadata always pin
  2162. * if data pin when any transaction has committed this
  2163. */
  2164. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
  2165. ref_generation != trans->transid)
  2166. pin = 1;
  2167. if (ref_generation != trans->transid)
  2168. pin = 1;
  2169. return __free_extent(trans, root, bytenr, num_bytes, parent,
  2170. root_objectid, ref_generation,
  2171. owner_objectid, pin, pin == 0, refs_to_drop);
  2172. }
  2173. /*
  2174. * when we free an extent, it is possible (and likely) that we free the last
  2175. * delayed ref for that extent as well. This searches the delayed ref tree for
  2176. * a given extent, and if there are no other delayed refs to be processed, it
  2177. * removes it from the tree.
  2178. */
  2179. static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
  2180. struct btrfs_root *root, u64 bytenr)
  2181. {
  2182. struct btrfs_delayed_ref_head *head;
  2183. struct btrfs_delayed_ref_root *delayed_refs;
  2184. struct btrfs_delayed_ref_node *ref;
  2185. struct rb_node *node;
  2186. int ret;
  2187. delayed_refs = &trans->transaction->delayed_refs;
  2188. spin_lock(&delayed_refs->lock);
  2189. head = btrfs_find_delayed_ref_head(trans, bytenr);
  2190. if (!head)
  2191. goto out;
  2192. node = rb_prev(&head->node.rb_node);
  2193. if (!node)
  2194. goto out;
  2195. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  2196. /* there are still entries for this ref, we can't drop it */
  2197. if (ref->bytenr == bytenr)
  2198. goto out;
  2199. /*
  2200. * waiting for the lock here would deadlock. If someone else has it
  2201. * locked they are already in the process of dropping it anyway
  2202. */
  2203. if (!mutex_trylock(&head->mutex))
  2204. goto out;
  2205. /*
  2206. * at this point we have a head with no other entries. Go
  2207. * ahead and process it.
  2208. */
  2209. head->node.in_tree = 0;
  2210. rb_erase(&head->node.rb_node, &delayed_refs->root);
  2211. delayed_refs->num_entries--;
  2212. /*
  2213. * we don't take a ref on the node because we're removing it from the
  2214. * tree, so we just steal the ref the tree was holding.
  2215. */
  2216. delayed_refs->num_heads--;
  2217. if (list_empty(&head->cluster))
  2218. delayed_refs->num_heads_ready--;
  2219. list_del_init(&head->cluster);
  2220. spin_unlock(&delayed_refs->lock);
  2221. ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
  2222. &head->node, head->must_insert_reserved);
  2223. BUG_ON(ret);
  2224. btrfs_put_delayed_ref(&head->node);
  2225. return 0;
  2226. out:
  2227. spin_unlock(&delayed_refs->lock);
  2228. return 0;
  2229. }
  2230. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  2231. struct btrfs_root *root,
  2232. u64 bytenr, u64 num_bytes, u64 parent,
  2233. u64 root_objectid, u64 ref_generation,
  2234. u64 owner_objectid, int pin)
  2235. {
  2236. int ret;
  2237. /*
  2238. * tree log blocks never actually go into the extent allocation
  2239. * tree, just update pinning info and exit early.
  2240. *
  2241. * data extents referenced by the tree log do need to have
  2242. * their reference counts bumped.
  2243. */
  2244. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
  2245. owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  2246. /* unlocks the pinned mutex */
  2247. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  2248. update_reserved_extents(root, bytenr, num_bytes, 0);
  2249. ret = 0;
  2250. } else {
  2251. ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
  2252. root_objectid, ref_generation,
  2253. owner_objectid,
  2254. BTRFS_DROP_DELAYED_REF, 1);
  2255. BUG_ON(ret);
  2256. ret = check_ref_cleanup(trans, root, bytenr);
  2257. BUG_ON(ret);
  2258. }
  2259. return ret;
  2260. }
  2261. static u64 stripe_align(struct btrfs_root *root, u64 val)
  2262. {
  2263. u64 mask = ((u64)root->stripesize - 1);
  2264. u64 ret = (val + mask) & ~mask;
  2265. return ret;
  2266. }
  2267. /*
  2268. * walks the btree of allocated extents and find a hole of a given size.
  2269. * The key ins is changed to record the hole:
  2270. * ins->objectid == block start
  2271. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  2272. * ins->offset == number of blocks
  2273. * Any available blocks before search_start are skipped.
  2274. */
  2275. static noinline int find_free_extent(struct btrfs_trans_handle *trans,
  2276. struct btrfs_root *orig_root,
  2277. u64 num_bytes, u64 empty_size,
  2278. u64 search_start, u64 search_end,
  2279. u64 hint_byte, struct btrfs_key *ins,
  2280. u64 exclude_start, u64 exclude_nr,
  2281. int data)
  2282. {
  2283. int ret = 0;
  2284. struct btrfs_root *root = orig_root->fs_info->extent_root;
  2285. struct btrfs_free_cluster *last_ptr = NULL;
  2286. struct btrfs_block_group_cache *block_group = NULL;
  2287. int empty_cluster = 2 * 1024 * 1024;
  2288. int allowed_chunk_alloc = 0;
  2289. struct btrfs_space_info *space_info;
  2290. int last_ptr_loop = 0;
  2291. int loop = 0;
  2292. WARN_ON(num_bytes < root->sectorsize);
  2293. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  2294. ins->objectid = 0;
  2295. ins->offset = 0;
  2296. space_info = __find_space_info(root->fs_info, data);
  2297. if (orig_root->ref_cows || empty_size)
  2298. allowed_chunk_alloc = 1;
  2299. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  2300. last_ptr = &root->fs_info->meta_alloc_cluster;
  2301. if (!btrfs_test_opt(root, SSD))
  2302. empty_cluster = 64 * 1024;
  2303. }
  2304. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
  2305. last_ptr = &root->fs_info->data_alloc_cluster;
  2306. }
  2307. if (last_ptr) {
  2308. spin_lock(&last_ptr->lock);
  2309. if (last_ptr->block_group)
  2310. hint_byte = last_ptr->window_start;
  2311. spin_unlock(&last_ptr->lock);
  2312. }
  2313. search_start = max(search_start, first_logical_byte(root, 0));
  2314. search_start = max(search_start, hint_byte);
  2315. if (!last_ptr) {
  2316. empty_cluster = 0;
  2317. loop = 1;
  2318. }
  2319. if (search_start == hint_byte) {
  2320. block_group = btrfs_lookup_block_group(root->fs_info,
  2321. search_start);
  2322. if (block_group && block_group_bits(block_group, data)) {
  2323. down_read(&space_info->groups_sem);
  2324. goto have_block_group;
  2325. } else if (block_group) {
  2326. btrfs_put_block_group(block_group);
  2327. }
  2328. }
  2329. search:
  2330. down_read(&space_info->groups_sem);
  2331. list_for_each_entry(block_group, &space_info->block_groups, list) {
  2332. u64 offset;
  2333. atomic_inc(&block_group->count);
  2334. search_start = block_group->key.objectid;
  2335. have_block_group:
  2336. if (unlikely(!block_group->cached)) {
  2337. mutex_lock(&block_group->cache_mutex);
  2338. ret = cache_block_group(root, block_group);
  2339. mutex_unlock(&block_group->cache_mutex);
  2340. if (ret) {
  2341. btrfs_put_block_group(block_group);
  2342. break;
  2343. }
  2344. }
  2345. if (unlikely(block_group->ro))
  2346. goto loop;
  2347. if (last_ptr) {
  2348. /*
  2349. * the refill lock keeps out other
  2350. * people trying to start a new cluster
  2351. */
  2352. spin_lock(&last_ptr->refill_lock);
  2353. offset = btrfs_alloc_from_cluster(block_group, last_ptr,
  2354. num_bytes, search_start);
  2355. if (offset) {
  2356. /* we have a block, we're done */
  2357. spin_unlock(&last_ptr->refill_lock);
  2358. goto checks;
  2359. }
  2360. spin_lock(&last_ptr->lock);
  2361. /*
  2362. * whoops, this cluster doesn't actually point to
  2363. * this block group. Get a ref on the block
  2364. * group is does point to and try again
  2365. */
  2366. if (!last_ptr_loop && last_ptr->block_group &&
  2367. last_ptr->block_group != block_group) {
  2368. btrfs_put_block_group(block_group);
  2369. block_group = last_ptr->block_group;
  2370. atomic_inc(&block_group->count);
  2371. spin_unlock(&last_ptr->lock);
  2372. spin_unlock(&last_ptr->refill_lock);
  2373. last_ptr_loop = 1;
  2374. search_start = block_group->key.objectid;
  2375. goto have_block_group;
  2376. }
  2377. spin_unlock(&last_ptr->lock);
  2378. /*
  2379. * this cluster didn't work out, free it and
  2380. * start over
  2381. */
  2382. btrfs_return_cluster_to_free_space(NULL, last_ptr);
  2383. last_ptr_loop = 0;
  2384. /* allocate a cluster in this block group */
  2385. ret = btrfs_find_space_cluster(trans,
  2386. block_group, last_ptr,
  2387. offset, num_bytes,
  2388. empty_cluster + empty_size);
  2389. if (ret == 0) {
  2390. /*
  2391. * now pull our allocation out of this
  2392. * cluster
  2393. */
  2394. offset = btrfs_alloc_from_cluster(block_group,
  2395. last_ptr, num_bytes,
  2396. search_start);
  2397. if (offset) {
  2398. /* we found one, proceed */
  2399. spin_unlock(&last_ptr->refill_lock);
  2400. goto checks;
  2401. }
  2402. }
  2403. /*
  2404. * at this point we either didn't find a cluster
  2405. * or we weren't able to allocate a block from our
  2406. * cluster. Free the cluster we've been trying
  2407. * to use, and go to the next block group
  2408. */
  2409. if (loop < 2) {
  2410. btrfs_return_cluster_to_free_space(NULL,
  2411. last_ptr);
  2412. spin_unlock(&last_ptr->refill_lock);
  2413. goto loop;
  2414. }
  2415. spin_unlock(&last_ptr->refill_lock);
  2416. }
  2417. offset = btrfs_find_space_for_alloc(block_group, search_start,
  2418. num_bytes, empty_size);
  2419. if (!offset)
  2420. goto loop;
  2421. checks:
  2422. search_start = stripe_align(root, offset);
  2423. /* move on to the next group */
  2424. if (search_start + num_bytes >= search_end) {
  2425. btrfs_add_free_space(block_group, offset, num_bytes);
  2426. goto loop;
  2427. }
  2428. /* move on to the next group */
  2429. if (search_start + num_bytes >
  2430. block_group->key.objectid + block_group->key.offset) {
  2431. btrfs_add_free_space(block_group, offset, num_bytes);
  2432. goto loop;
  2433. }
  2434. if (exclude_nr > 0 &&
  2435. (search_start + num_bytes > exclude_start &&
  2436. search_start < exclude_start + exclude_nr)) {
  2437. search_start = exclude_start + exclude_nr;
  2438. btrfs_add_free_space(block_group, offset, num_bytes);
  2439. /*
  2440. * if search_start is still in this block group
  2441. * then we just re-search this block group
  2442. */
  2443. if (search_start >= block_group->key.objectid &&
  2444. search_start < (block_group->key.objectid +
  2445. block_group->key.offset))
  2446. goto have_block_group;
  2447. goto loop;
  2448. }
  2449. ins->objectid = search_start;
  2450. ins->offset = num_bytes;
  2451. if (offset < search_start)
  2452. btrfs_add_free_space(block_group, offset,
  2453. search_start - offset);
  2454. BUG_ON(offset > search_start);
  2455. /* we are all good, lets return */
  2456. break;
  2457. loop:
  2458. btrfs_put_block_group(block_group);
  2459. }
  2460. up_read(&space_info->groups_sem);
  2461. /* loop == 0, try to find a clustered alloc in every block group
  2462. * loop == 1, try again after forcing a chunk allocation
  2463. * loop == 2, set empty_size and empty_cluster to 0 and try again
  2464. */
  2465. if (!ins->objectid && loop < 3 &&
  2466. (empty_size || empty_cluster || allowed_chunk_alloc)) {
  2467. if (loop >= 2) {
  2468. empty_size = 0;
  2469. empty_cluster = 0;
  2470. }
  2471. if (allowed_chunk_alloc) {
  2472. ret = do_chunk_alloc(trans, root, num_bytes +
  2473. 2 * 1024 * 1024, data, 1);
  2474. allowed_chunk_alloc = 0;
  2475. } else {
  2476. space_info->force_alloc = 1;
  2477. }
  2478. if (loop < 3) {
  2479. loop++;
  2480. goto search;
  2481. }
  2482. ret = -ENOSPC;
  2483. } else if (!ins->objectid) {
  2484. ret = -ENOSPC;
  2485. }
  2486. /* we found what we needed */
  2487. if (ins->objectid) {
  2488. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  2489. trans->block_group = block_group->key.objectid;
  2490. btrfs_put_block_group(block_group);
  2491. ret = 0;
  2492. }
  2493. return ret;
  2494. }
  2495. static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
  2496. {
  2497. struct btrfs_block_group_cache *cache;
  2498. printk(KERN_INFO "space_info has %llu free, is %sfull\n",
  2499. (unsigned long long)(info->total_bytes - info->bytes_used -
  2500. info->bytes_pinned - info->bytes_reserved),
  2501. (info->full) ? "" : "not ");
  2502. printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
  2503. " may_use=%llu, used=%llu\n",
  2504. (unsigned long long)info->total_bytes,
  2505. (unsigned long long)info->bytes_pinned,
  2506. (unsigned long long)info->bytes_delalloc,
  2507. (unsigned long long)info->bytes_may_use,
  2508. (unsigned long long)info->bytes_used);
  2509. down_read(&info->groups_sem);
  2510. list_for_each_entry(cache, &info->block_groups, list) {
  2511. spin_lock(&cache->lock);
  2512. printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
  2513. "%llu pinned %llu reserved\n",
  2514. (unsigned long long)cache->key.objectid,
  2515. (unsigned long long)cache->key.offset,
  2516. (unsigned long long)btrfs_block_group_used(&cache->item),
  2517. (unsigned long long)cache->pinned,
  2518. (unsigned long long)cache->reserved);
  2519. btrfs_dump_free_space(cache, bytes);
  2520. spin_unlock(&cache->lock);
  2521. }
  2522. up_read(&info->groups_sem);
  2523. }
  2524. static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2525. struct btrfs_root *root,
  2526. u64 num_bytes, u64 min_alloc_size,
  2527. u64 empty_size, u64 hint_byte,
  2528. u64 search_end, struct btrfs_key *ins,
  2529. u64 data)
  2530. {
  2531. int ret;
  2532. u64 search_start = 0;
  2533. struct btrfs_fs_info *info = root->fs_info;
  2534. data = btrfs_get_alloc_profile(root, data);
  2535. again:
  2536. /*
  2537. * the only place that sets empty_size is btrfs_realloc_node, which
  2538. * is not called recursively on allocations
  2539. */
  2540. if (empty_size || root->ref_cows) {
  2541. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  2542. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2543. 2 * 1024 * 1024,
  2544. BTRFS_BLOCK_GROUP_METADATA |
  2545. (info->metadata_alloc_profile &
  2546. info->avail_metadata_alloc_bits), 0);
  2547. }
  2548. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2549. num_bytes + 2 * 1024 * 1024, data, 0);
  2550. }
  2551. WARN_ON(num_bytes < root->sectorsize);
  2552. ret = find_free_extent(trans, root, num_bytes, empty_size,
  2553. search_start, search_end, hint_byte, ins,
  2554. trans->alloc_exclude_start,
  2555. trans->alloc_exclude_nr, data);
  2556. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  2557. num_bytes = num_bytes >> 1;
  2558. num_bytes = num_bytes & ~(root->sectorsize - 1);
  2559. num_bytes = max(num_bytes, min_alloc_size);
  2560. do_chunk_alloc(trans, root->fs_info->extent_root,
  2561. num_bytes, data, 1);
  2562. goto again;
  2563. }
  2564. if (ret) {
  2565. struct btrfs_space_info *sinfo;
  2566. sinfo = __find_space_info(root->fs_info, data);
  2567. printk(KERN_ERR "btrfs allocation failed flags %llu, "
  2568. "wanted %llu\n", (unsigned long long)data,
  2569. (unsigned long long)num_bytes);
  2570. dump_space_info(sinfo, num_bytes);
  2571. BUG();
  2572. }
  2573. return ret;
  2574. }
  2575. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  2576. {
  2577. struct btrfs_block_group_cache *cache;
  2578. int ret = 0;
  2579. cache = btrfs_lookup_block_group(root->fs_info, start);
  2580. if (!cache) {
  2581. printk(KERN_ERR "Unable to find block group for %llu\n",
  2582. (unsigned long long)start);
  2583. return -ENOSPC;
  2584. }
  2585. ret = btrfs_discard_extent(root, start, len);
  2586. btrfs_add_free_space(cache, start, len);
  2587. btrfs_put_block_group(cache);
  2588. update_reserved_extents(root, start, len, 0);
  2589. return ret;
  2590. }
  2591. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2592. struct btrfs_root *root,
  2593. u64 num_bytes, u64 min_alloc_size,
  2594. u64 empty_size, u64 hint_byte,
  2595. u64 search_end, struct btrfs_key *ins,
  2596. u64 data)
  2597. {
  2598. int ret;
  2599. ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
  2600. empty_size, hint_byte, search_end, ins,
  2601. data);
  2602. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2603. return ret;
  2604. }
  2605. static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2606. struct btrfs_root *root, u64 parent,
  2607. u64 root_objectid, u64 ref_generation,
  2608. u64 owner, struct btrfs_key *ins,
  2609. int ref_mod)
  2610. {
  2611. int ret;
  2612. u64 super_used;
  2613. u64 root_used;
  2614. u64 num_bytes = ins->offset;
  2615. u32 sizes[2];
  2616. struct btrfs_fs_info *info = root->fs_info;
  2617. struct btrfs_root *extent_root = info->extent_root;
  2618. struct btrfs_extent_item *extent_item;
  2619. struct btrfs_extent_ref *ref;
  2620. struct btrfs_path *path;
  2621. struct btrfs_key keys[2];
  2622. if (parent == 0)
  2623. parent = ins->objectid;
  2624. /* block accounting for super block */
  2625. spin_lock(&info->delalloc_lock);
  2626. super_used = btrfs_super_bytes_used(&info->super_copy);
  2627. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  2628. /* block accounting for root item */
  2629. root_used = btrfs_root_used(&root->root_item);
  2630. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  2631. spin_unlock(&info->delalloc_lock);
  2632. memcpy(&keys[0], ins, sizeof(*ins));
  2633. keys[1].objectid = ins->objectid;
  2634. keys[1].type = BTRFS_EXTENT_REF_KEY;
  2635. keys[1].offset = parent;
  2636. sizes[0] = sizeof(*extent_item);
  2637. sizes[1] = sizeof(*ref);
  2638. path = btrfs_alloc_path();
  2639. BUG_ON(!path);
  2640. path->leave_spinning = 1;
  2641. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  2642. sizes, 2);
  2643. BUG_ON(ret);
  2644. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2645. struct btrfs_extent_item);
  2646. btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
  2647. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  2648. struct btrfs_extent_ref);
  2649. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  2650. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  2651. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  2652. btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
  2653. btrfs_mark_buffer_dirty(path->nodes[0]);
  2654. trans->alloc_exclude_start = 0;
  2655. trans->alloc_exclude_nr = 0;
  2656. btrfs_free_path(path);
  2657. if (ret)
  2658. goto out;
  2659. ret = update_block_group(trans, root, ins->objectid,
  2660. ins->offset, 1, 0);
  2661. if (ret) {
  2662. printk(KERN_ERR "btrfs update block group failed for %llu "
  2663. "%llu\n", (unsigned long long)ins->objectid,
  2664. (unsigned long long)ins->offset);
  2665. BUG();
  2666. }
  2667. out:
  2668. return ret;
  2669. }
  2670. int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2671. struct btrfs_root *root, u64 parent,
  2672. u64 root_objectid, u64 ref_generation,
  2673. u64 owner, struct btrfs_key *ins)
  2674. {
  2675. int ret;
  2676. if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
  2677. return 0;
  2678. ret = btrfs_add_delayed_ref(trans, ins->objectid,
  2679. ins->offset, parent, root_objectid,
  2680. ref_generation, owner,
  2681. BTRFS_ADD_DELAYED_EXTENT, 0);
  2682. BUG_ON(ret);
  2683. return ret;
  2684. }
  2685. /*
  2686. * this is used by the tree logging recovery code. It records that
  2687. * an extent has been allocated and makes sure to clear the free
  2688. * space cache bits as well
  2689. */
  2690. int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
  2691. struct btrfs_root *root, u64 parent,
  2692. u64 root_objectid, u64 ref_generation,
  2693. u64 owner, struct btrfs_key *ins)
  2694. {
  2695. int ret;
  2696. struct btrfs_block_group_cache *block_group;
  2697. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  2698. mutex_lock(&block_group->cache_mutex);
  2699. cache_block_group(root, block_group);
  2700. mutex_unlock(&block_group->cache_mutex);
  2701. ret = btrfs_remove_free_space(block_group, ins->objectid,
  2702. ins->offset);
  2703. BUG_ON(ret);
  2704. btrfs_put_block_group(block_group);
  2705. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  2706. ref_generation, owner, ins, 1);
  2707. return ret;
  2708. }
  2709. /*
  2710. * finds a free extent and does all the dirty work required for allocation
  2711. * returns the key for the extent through ins, and a tree buffer for
  2712. * the first block of the extent through buf.
  2713. *
  2714. * returns 0 if everything worked, non-zero otherwise.
  2715. */
  2716. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  2717. struct btrfs_root *root,
  2718. u64 num_bytes, u64 parent, u64 min_alloc_size,
  2719. u64 root_objectid, u64 ref_generation,
  2720. u64 owner_objectid, u64 empty_size, u64 hint_byte,
  2721. u64 search_end, struct btrfs_key *ins, u64 data)
  2722. {
  2723. int ret;
  2724. ret = __btrfs_reserve_extent(trans, root, num_bytes,
  2725. min_alloc_size, empty_size, hint_byte,
  2726. search_end, ins, data);
  2727. BUG_ON(ret);
  2728. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  2729. ret = btrfs_add_delayed_ref(trans, ins->objectid,
  2730. ins->offset, parent, root_objectid,
  2731. ref_generation, owner_objectid,
  2732. BTRFS_ADD_DELAYED_EXTENT, 0);
  2733. BUG_ON(ret);
  2734. }
  2735. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2736. return ret;
  2737. }
  2738. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  2739. struct btrfs_root *root,
  2740. u64 bytenr, u32 blocksize,
  2741. int level)
  2742. {
  2743. struct extent_buffer *buf;
  2744. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  2745. if (!buf)
  2746. return ERR_PTR(-ENOMEM);
  2747. btrfs_set_header_generation(buf, trans->transid);
  2748. btrfs_set_buffer_lockdep_class(buf, level);
  2749. btrfs_tree_lock(buf);
  2750. clean_tree_block(trans, root, buf);
  2751. btrfs_set_lock_blocking(buf);
  2752. btrfs_set_buffer_uptodate(buf);
  2753. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  2754. set_extent_dirty(&root->dirty_log_pages, buf->start,
  2755. buf->start + buf->len - 1, GFP_NOFS);
  2756. } else {
  2757. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  2758. buf->start + buf->len - 1, GFP_NOFS);
  2759. }
  2760. trans->blocks_used++;
  2761. /* this returns a buffer locked for blocking */
  2762. return buf;
  2763. }
  2764. /*
  2765. * helper function to allocate a block for a given tree
  2766. * returns the tree buffer or NULL.
  2767. */
  2768. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  2769. struct btrfs_root *root,
  2770. u32 blocksize, u64 parent,
  2771. u64 root_objectid,
  2772. u64 ref_generation,
  2773. int level,
  2774. u64 hint,
  2775. u64 empty_size)
  2776. {
  2777. struct btrfs_key ins;
  2778. int ret;
  2779. struct extent_buffer *buf;
  2780. ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
  2781. root_objectid, ref_generation, level,
  2782. empty_size, hint, (u64)-1, &ins, 0);
  2783. if (ret) {
  2784. BUG_ON(ret > 0);
  2785. return ERR_PTR(ret);
  2786. }
  2787. buf = btrfs_init_new_buffer(trans, root, ins.objectid,
  2788. blocksize, level);
  2789. return buf;
  2790. }
  2791. int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
  2792. struct btrfs_root *root, struct extent_buffer *leaf)
  2793. {
  2794. u64 leaf_owner;
  2795. u64 leaf_generation;
  2796. struct refsort *sorted;
  2797. struct btrfs_key key;
  2798. struct btrfs_file_extent_item *fi;
  2799. int i;
  2800. int nritems;
  2801. int ret;
  2802. int refi = 0;
  2803. int slot;
  2804. BUG_ON(!btrfs_is_leaf(leaf));
  2805. nritems = btrfs_header_nritems(leaf);
  2806. leaf_owner = btrfs_header_owner(leaf);
  2807. leaf_generation = btrfs_header_generation(leaf);
  2808. sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
  2809. /* we do this loop twice. The first time we build a list
  2810. * of the extents we have a reference on, then we sort the list
  2811. * by bytenr. The second time around we actually do the
  2812. * extent freeing.
  2813. */
  2814. for (i = 0; i < nritems; i++) {
  2815. u64 disk_bytenr;
  2816. cond_resched();
  2817. btrfs_item_key_to_cpu(leaf, &key, i);
  2818. /* only extents have references, skip everything else */
  2819. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2820. continue;
  2821. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  2822. /* inline extents live in the btree, they don't have refs */
  2823. if (btrfs_file_extent_type(leaf, fi) ==
  2824. BTRFS_FILE_EXTENT_INLINE)
  2825. continue;
  2826. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  2827. /* holes don't have refs */
  2828. if (disk_bytenr == 0)
  2829. continue;
  2830. sorted[refi].bytenr = disk_bytenr;
  2831. sorted[refi].slot = i;
  2832. refi++;
  2833. }
  2834. if (refi == 0)
  2835. goto out;
  2836. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  2837. for (i = 0; i < refi; i++) {
  2838. u64 disk_bytenr;
  2839. disk_bytenr = sorted[i].bytenr;
  2840. slot = sorted[i].slot;
  2841. cond_resched();
  2842. btrfs_item_key_to_cpu(leaf, &key, slot);
  2843. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2844. continue;
  2845. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  2846. ret = btrfs_free_extent(trans, root, disk_bytenr,
  2847. btrfs_file_extent_disk_num_bytes(leaf, fi),
  2848. leaf->start, leaf_owner, leaf_generation,
  2849. key.objectid, 0);
  2850. BUG_ON(ret);
  2851. atomic_inc(&root->fs_info->throttle_gen);
  2852. wake_up(&root->fs_info->transaction_throttle);
  2853. cond_resched();
  2854. }
  2855. out:
  2856. kfree(sorted);
  2857. return 0;
  2858. }
  2859. static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
  2860. struct btrfs_root *root,
  2861. struct btrfs_leaf_ref *ref)
  2862. {
  2863. int i;
  2864. int ret;
  2865. struct btrfs_extent_info *info;
  2866. struct refsort *sorted;
  2867. if (ref->nritems == 0)
  2868. return 0;
  2869. sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
  2870. for (i = 0; i < ref->nritems; i++) {
  2871. sorted[i].bytenr = ref->extents[i].bytenr;
  2872. sorted[i].slot = i;
  2873. }
  2874. sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
  2875. /*
  2876. * the items in the ref were sorted when the ref was inserted
  2877. * into the ref cache, so this is already in order
  2878. */
  2879. for (i = 0; i < ref->nritems; i++) {
  2880. info = ref->extents + sorted[i].slot;
  2881. ret = btrfs_free_extent(trans, root, info->bytenr,
  2882. info->num_bytes, ref->bytenr,
  2883. ref->owner, ref->generation,
  2884. info->objectid, 0);
  2885. atomic_inc(&root->fs_info->throttle_gen);
  2886. wake_up(&root->fs_info->transaction_throttle);
  2887. cond_resched();
  2888. BUG_ON(ret);
  2889. info++;
  2890. }
  2891. kfree(sorted);
  2892. return 0;
  2893. }
  2894. static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
  2895. struct btrfs_root *root, u64 start,
  2896. u64 len, u32 *refs)
  2897. {
  2898. int ret;
  2899. ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
  2900. BUG_ON(ret);
  2901. #if 0 /* some debugging code in case we see problems here */
  2902. /* if the refs count is one, it won't get increased again. But
  2903. * if the ref count is > 1, someone may be decreasing it at
  2904. * the same time we are.
  2905. */
  2906. if (*refs != 1) {
  2907. struct extent_buffer *eb = NULL;
  2908. eb = btrfs_find_create_tree_block(root, start, len);
  2909. if (eb)
  2910. btrfs_tree_lock(eb);
  2911. mutex_lock(&root->fs_info->alloc_mutex);
  2912. ret = lookup_extent_ref(NULL, root, start, len, refs);
  2913. BUG_ON(ret);
  2914. mutex_unlock(&root->fs_info->alloc_mutex);
  2915. if (eb) {
  2916. btrfs_tree_unlock(eb);
  2917. free_extent_buffer(eb);
  2918. }
  2919. if (*refs == 1) {
  2920. printk(KERN_ERR "btrfs block %llu went down to one "
  2921. "during drop_snap\n", (unsigned long long)start);
  2922. }
  2923. }
  2924. #endif
  2925. cond_resched();
  2926. return ret;
  2927. }
  2928. /*
  2929. * this is used while deleting old snapshots, and it drops the refs
  2930. * on a whole subtree starting from a level 1 node.
  2931. *
  2932. * The idea is to sort all the leaf pointers, and then drop the
  2933. * ref on all the leaves in order. Most of the time the leaves
  2934. * will have ref cache entries, so no leaf IOs will be required to
  2935. * find the extents they have references on.
  2936. *
  2937. * For each leaf, any references it has are also dropped in order
  2938. *
  2939. * This ends up dropping the references in something close to optimal
  2940. * order for reading and modifying the extent allocation tree.
  2941. */
  2942. static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
  2943. struct btrfs_root *root,
  2944. struct btrfs_path *path)
  2945. {
  2946. u64 bytenr;
  2947. u64 root_owner;
  2948. u64 root_gen;
  2949. struct extent_buffer *eb = path->nodes[1];
  2950. struct extent_buffer *leaf;
  2951. struct btrfs_leaf_ref *ref;
  2952. struct refsort *sorted = NULL;
  2953. int nritems = btrfs_header_nritems(eb);
  2954. int ret;
  2955. int i;
  2956. int refi = 0;
  2957. int slot = path->slots[1];
  2958. u32 blocksize = btrfs_level_size(root, 0);
  2959. u32 refs;
  2960. if (nritems == 0)
  2961. goto out;
  2962. root_owner = btrfs_header_owner(eb);
  2963. root_gen = btrfs_header_generation(eb);
  2964. sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
  2965. /*
  2966. * step one, sort all the leaf pointers so we don't scribble
  2967. * randomly into the extent allocation tree
  2968. */
  2969. for (i = slot; i < nritems; i++) {
  2970. sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
  2971. sorted[refi].slot = i;
  2972. refi++;
  2973. }
  2974. /*
  2975. * nritems won't be zero, but if we're picking up drop_snapshot
  2976. * after a crash, slot might be > 0, so double check things
  2977. * just in case.
  2978. */
  2979. if (refi == 0)
  2980. goto out;
  2981. sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
  2982. /*
  2983. * the first loop frees everything the leaves point to
  2984. */
  2985. for (i = 0; i < refi; i++) {
  2986. u64 ptr_gen;
  2987. bytenr = sorted[i].bytenr;
  2988. /*
  2989. * check the reference count on this leaf. If it is > 1
  2990. * we just decrement it below and don't update any
  2991. * of the refs the leaf points to.
  2992. */
  2993. ret = drop_snap_lookup_refcount(trans, root, bytenr,
  2994. blocksize, &refs);
  2995. BUG_ON(ret);
  2996. if (refs != 1)
  2997. continue;
  2998. ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
  2999. /*
  3000. * the leaf only had one reference, which means the
  3001. * only thing pointing to this leaf is the snapshot
  3002. * we're deleting. It isn't possible for the reference
  3003. * count to increase again later
  3004. *
  3005. * The reference cache is checked for the leaf,
  3006. * and if found we'll be able to drop any refs held by
  3007. * the leaf without needing to read it in.
  3008. */
  3009. ref = btrfs_lookup_leaf_ref(root, bytenr);
  3010. if (ref && ref->generation != ptr_gen) {
  3011. btrfs_free_leaf_ref(root, ref);
  3012. ref = NULL;
  3013. }
  3014. if (ref) {
  3015. ret = cache_drop_leaf_ref(trans, root, ref);
  3016. BUG_ON(ret);
  3017. btrfs_remove_leaf_ref(root, ref);
  3018. btrfs_free_leaf_ref(root, ref);
  3019. } else {
  3020. /*
  3021. * the leaf wasn't in the reference cache, so
  3022. * we have to read it.
  3023. */
  3024. leaf = read_tree_block(root, bytenr, blocksize,
  3025. ptr_gen);
  3026. ret = btrfs_drop_leaf_ref(trans, root, leaf);
  3027. BUG_ON(ret);
  3028. free_extent_buffer(leaf);
  3029. }
  3030. atomic_inc(&root->fs_info->throttle_gen);
  3031. wake_up(&root->fs_info->transaction_throttle);
  3032. cond_resched();
  3033. }
  3034. /*
  3035. * run through the loop again to free the refs on the leaves.
  3036. * This is faster than doing it in the loop above because
  3037. * the leaves are likely to be clustered together. We end up
  3038. * working in nice chunks on the extent allocation tree.
  3039. */
  3040. for (i = 0; i < refi; i++) {
  3041. bytenr = sorted[i].bytenr;
  3042. ret = btrfs_free_extent(trans, root, bytenr,
  3043. blocksize, eb->start,
  3044. root_owner, root_gen, 0, 1);
  3045. BUG_ON(ret);
  3046. atomic_inc(&root->fs_info->throttle_gen);
  3047. wake_up(&root->fs_info->transaction_throttle);
  3048. cond_resched();
  3049. }
  3050. out:
  3051. kfree(sorted);
  3052. /*
  3053. * update the path to show we've processed the entire level 1
  3054. * node. This will get saved into the root's drop_snapshot_progress
  3055. * field so these drops are not repeated again if this transaction
  3056. * commits.
  3057. */
  3058. path->slots[1] = nritems;
  3059. return 0;
  3060. }
  3061. /*
  3062. * helper function for drop_snapshot, this walks down the tree dropping ref
  3063. * counts as it goes.
  3064. */
  3065. static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
  3066. struct btrfs_root *root,
  3067. struct btrfs_path *path, int *level)
  3068. {
  3069. u64 root_owner;
  3070. u64 root_gen;
  3071. u64 bytenr;
  3072. u64 ptr_gen;
  3073. struct extent_buffer *next;
  3074. struct extent_buffer *cur;
  3075. struct extent_buffer *parent;
  3076. u32 blocksize;
  3077. int ret;
  3078. u32 refs;
  3079. WARN_ON(*level < 0);
  3080. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3081. ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
  3082. path->nodes[*level]->len, &refs);
  3083. BUG_ON(ret);
  3084. if (refs > 1)
  3085. goto out;
  3086. /*
  3087. * walk down to the last node level and free all the leaves
  3088. */
  3089. while (*level >= 0) {
  3090. WARN_ON(*level < 0);
  3091. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3092. cur = path->nodes[*level];
  3093. if (btrfs_header_level(cur) != *level)
  3094. WARN_ON(1);
  3095. if (path->slots[*level] >=
  3096. btrfs_header_nritems(cur))
  3097. break;
  3098. /* the new code goes down to level 1 and does all the
  3099. * leaves pointed to that node in bulk. So, this check
  3100. * for level 0 will always be false.
  3101. *
  3102. * But, the disk format allows the drop_snapshot_progress
  3103. * field in the root to leave things in a state where
  3104. * a leaf will need cleaning up here. If someone crashes
  3105. * with the old code and then boots with the new code,
  3106. * we might find a leaf here.
  3107. */
  3108. if (*level == 0) {
  3109. ret = btrfs_drop_leaf_ref(trans, root, cur);
  3110. BUG_ON(ret);
  3111. break;
  3112. }
  3113. /*
  3114. * once we get to level one, process the whole node
  3115. * at once, including everything below it.
  3116. */
  3117. if (*level == 1) {
  3118. ret = drop_level_one_refs(trans, root, path);
  3119. BUG_ON(ret);
  3120. break;
  3121. }
  3122. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  3123. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  3124. blocksize = btrfs_level_size(root, *level - 1);
  3125. ret = drop_snap_lookup_refcount(trans, root, bytenr,
  3126. blocksize, &refs);
  3127. BUG_ON(ret);
  3128. /*
  3129. * if there is more than one reference, we don't need
  3130. * to read that node to drop any references it has. We
  3131. * just drop the ref we hold on that node and move on to the
  3132. * next slot in this level.
  3133. */
  3134. if (refs != 1) {
  3135. parent = path->nodes[*level];
  3136. root_owner = btrfs_header_owner(parent);
  3137. root_gen = btrfs_header_generation(parent);
  3138. path->slots[*level]++;
  3139. ret = btrfs_free_extent(trans, root, bytenr,
  3140. blocksize, parent->start,
  3141. root_owner, root_gen,
  3142. *level - 1, 1);
  3143. BUG_ON(ret);
  3144. atomic_inc(&root->fs_info->throttle_gen);
  3145. wake_up(&root->fs_info->transaction_throttle);
  3146. cond_resched();
  3147. continue;
  3148. }
  3149. /*
  3150. * we need to keep freeing things in the next level down.
  3151. * read the block and loop around to process it
  3152. */
  3153. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  3154. WARN_ON(*level <= 0);
  3155. if (path->nodes[*level-1])
  3156. free_extent_buffer(path->nodes[*level-1]);
  3157. path->nodes[*level-1] = next;
  3158. *level = btrfs_header_level(next);
  3159. path->slots[*level] = 0;
  3160. cond_resched();
  3161. }
  3162. out:
  3163. WARN_ON(*level < 0);
  3164. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3165. if (path->nodes[*level] == root->node) {
  3166. parent = path->nodes[*level];
  3167. bytenr = path->nodes[*level]->start;
  3168. } else {
  3169. parent = path->nodes[*level + 1];
  3170. bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
  3171. }
  3172. blocksize = btrfs_level_size(root, *level);
  3173. root_owner = btrfs_header_owner(parent);
  3174. root_gen = btrfs_header_generation(parent);
  3175. /*
  3176. * cleanup and free the reference on the last node
  3177. * we processed
  3178. */
  3179. ret = btrfs_free_extent(trans, root, bytenr, blocksize,
  3180. parent->start, root_owner, root_gen,
  3181. *level, 1);
  3182. free_extent_buffer(path->nodes[*level]);
  3183. path->nodes[*level] = NULL;
  3184. *level += 1;
  3185. BUG_ON(ret);
  3186. cond_resched();
  3187. return 0;
  3188. }
  3189. /*
  3190. * helper function for drop_subtree, this function is similar to
  3191. * walk_down_tree. The main difference is that it checks reference
  3192. * counts while tree blocks are locked.
  3193. */
  3194. static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
  3195. struct btrfs_root *root,
  3196. struct btrfs_path *path, int *level)
  3197. {
  3198. struct extent_buffer *next;
  3199. struct extent_buffer *cur;
  3200. struct extent_buffer *parent;
  3201. u64 bytenr;
  3202. u64 ptr_gen;
  3203. u32 blocksize;
  3204. u32 refs;
  3205. int ret;
  3206. cur = path->nodes[*level];
  3207. ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
  3208. &refs);
  3209. BUG_ON(ret);
  3210. if (refs > 1)
  3211. goto out;
  3212. while (*level >= 0) {
  3213. cur = path->nodes[*level];
  3214. if (*level == 0) {
  3215. ret = btrfs_drop_leaf_ref(trans, root, cur);
  3216. BUG_ON(ret);
  3217. clean_tree_block(trans, root, cur);
  3218. break;
  3219. }
  3220. if (path->slots[*level] >= btrfs_header_nritems(cur)) {
  3221. clean_tree_block(trans, root, cur);
  3222. break;
  3223. }
  3224. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  3225. blocksize = btrfs_level_size(root, *level - 1);
  3226. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  3227. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  3228. btrfs_tree_lock(next);
  3229. btrfs_set_lock_blocking(next);
  3230. ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
  3231. &refs);
  3232. BUG_ON(ret);
  3233. if (refs > 1) {
  3234. parent = path->nodes[*level];
  3235. ret = btrfs_free_extent(trans, root, bytenr,
  3236. blocksize, parent->start,
  3237. btrfs_header_owner(parent),
  3238. btrfs_header_generation(parent),
  3239. *level - 1, 1);
  3240. BUG_ON(ret);
  3241. path->slots[*level]++;
  3242. btrfs_tree_unlock(next);
  3243. free_extent_buffer(next);
  3244. continue;
  3245. }
  3246. *level = btrfs_header_level(next);
  3247. path->nodes[*level] = next;
  3248. path->slots[*level] = 0;
  3249. path->locks[*level] = 1;
  3250. cond_resched();
  3251. }
  3252. out:
  3253. parent = path->nodes[*level + 1];
  3254. bytenr = path->nodes[*level]->start;
  3255. blocksize = path->nodes[*level]->len;
  3256. ret = btrfs_free_extent(trans, root, bytenr, blocksize,
  3257. parent->start, btrfs_header_owner(parent),
  3258. btrfs_header_generation(parent), *level, 1);
  3259. BUG_ON(ret);
  3260. if (path->locks[*level]) {
  3261. btrfs_tree_unlock(path->nodes[*level]);
  3262. path->locks[*level] = 0;
  3263. }
  3264. free_extent_buffer(path->nodes[*level]);
  3265. path->nodes[*level] = NULL;
  3266. *level += 1;
  3267. cond_resched();
  3268. return 0;
  3269. }
  3270. /*
  3271. * helper for dropping snapshots. This walks back up the tree in the path
  3272. * to find the first node higher up where we haven't yet gone through
  3273. * all the slots
  3274. */
  3275. static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
  3276. struct btrfs_root *root,
  3277. struct btrfs_path *path,
  3278. int *level, int max_level)
  3279. {
  3280. u64 root_owner;
  3281. u64 root_gen;
  3282. struct btrfs_root_item *root_item = &root->root_item;
  3283. int i;
  3284. int slot;
  3285. int ret;
  3286. for (i = *level; i < max_level && path->nodes[i]; i++) {
  3287. slot = path->slots[i];
  3288. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  3289. struct extent_buffer *node;
  3290. struct btrfs_disk_key disk_key;
  3291. /*
  3292. * there is more work to do in this level.
  3293. * Update the drop_progress marker to reflect
  3294. * the work we've done so far, and then bump
  3295. * the slot number
  3296. */
  3297. node = path->nodes[i];
  3298. path->slots[i]++;
  3299. *level = i;
  3300. WARN_ON(*level == 0);
  3301. btrfs_node_key(node, &disk_key, path->slots[i]);
  3302. memcpy(&root_item->drop_progress,
  3303. &disk_key, sizeof(disk_key));
  3304. root_item->drop_level = i;
  3305. return 0;
  3306. } else {
  3307. struct extent_buffer *parent;
  3308. /*
  3309. * this whole node is done, free our reference
  3310. * on it and go up one level
  3311. */
  3312. if (path->nodes[*level] == root->node)
  3313. parent = path->nodes[*level];
  3314. else
  3315. parent = path->nodes[*level + 1];
  3316. root_owner = btrfs_header_owner(parent);
  3317. root_gen = btrfs_header_generation(parent);
  3318. clean_tree_block(trans, root, path->nodes[*level]);
  3319. ret = btrfs_free_extent(trans, root,
  3320. path->nodes[*level]->start,
  3321. path->nodes[*level]->len,
  3322. parent->start, root_owner,
  3323. root_gen, *level, 1);
  3324. BUG_ON(ret);
  3325. if (path->locks[*level]) {
  3326. btrfs_tree_unlock(path->nodes[*level]);
  3327. path->locks[*level] = 0;
  3328. }
  3329. free_extent_buffer(path->nodes[*level]);
  3330. path->nodes[*level] = NULL;
  3331. *level = i + 1;
  3332. }
  3333. }
  3334. return 1;
  3335. }
  3336. /*
  3337. * drop the reference count on the tree rooted at 'snap'. This traverses
  3338. * the tree freeing any blocks that have a ref count of zero after being
  3339. * decremented.
  3340. */
  3341. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  3342. *root)
  3343. {
  3344. int ret = 0;
  3345. int wret;
  3346. int level;
  3347. struct btrfs_path *path;
  3348. int i;
  3349. int orig_level;
  3350. int update_count;
  3351. struct btrfs_root_item *root_item = &root->root_item;
  3352. WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
  3353. path = btrfs_alloc_path();
  3354. BUG_ON(!path);
  3355. level = btrfs_header_level(root->node);
  3356. orig_level = level;
  3357. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  3358. path->nodes[level] = root->node;
  3359. extent_buffer_get(root->node);
  3360. path->slots[level] = 0;
  3361. } else {
  3362. struct btrfs_key key;
  3363. struct btrfs_disk_key found_key;
  3364. struct extent_buffer *node;
  3365. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  3366. level = root_item->drop_level;
  3367. path->lowest_level = level;
  3368. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3369. if (wret < 0) {
  3370. ret = wret;
  3371. goto out;
  3372. }
  3373. node = path->nodes[level];
  3374. btrfs_node_key(node, &found_key, path->slots[level]);
  3375. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  3376. sizeof(found_key)));
  3377. /*
  3378. * unlock our path, this is safe because only this
  3379. * function is allowed to delete this snapshot
  3380. */
  3381. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  3382. if (path->nodes[i] && path->locks[i]) {
  3383. path->locks[i] = 0;
  3384. btrfs_tree_unlock(path->nodes[i]);
  3385. }
  3386. }
  3387. }
  3388. while (1) {
  3389. unsigned long update;
  3390. wret = walk_down_tree(trans, root, path, &level);
  3391. if (wret > 0)
  3392. break;
  3393. if (wret < 0)
  3394. ret = wret;
  3395. wret = walk_up_tree(trans, root, path, &level,
  3396. BTRFS_MAX_LEVEL);
  3397. if (wret > 0)
  3398. break;
  3399. if (wret < 0)
  3400. ret = wret;
  3401. if (trans->transaction->in_commit ||
  3402. trans->transaction->delayed_refs.flushing) {
  3403. ret = -EAGAIN;
  3404. break;
  3405. }
  3406. atomic_inc(&root->fs_info->throttle_gen);
  3407. wake_up(&root->fs_info->transaction_throttle);
  3408. for (update_count = 0; update_count < 16; update_count++) {
  3409. update = trans->delayed_ref_updates;
  3410. trans->delayed_ref_updates = 0;
  3411. if (update)
  3412. btrfs_run_delayed_refs(trans, root, update);
  3413. else
  3414. break;
  3415. }
  3416. }
  3417. for (i = 0; i <= orig_level; i++) {
  3418. if (path->nodes[i]) {
  3419. free_extent_buffer(path->nodes[i]);
  3420. path->nodes[i] = NULL;
  3421. }
  3422. }
  3423. out:
  3424. btrfs_free_path(path);
  3425. return ret;
  3426. }
  3427. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  3428. struct btrfs_root *root,
  3429. struct extent_buffer *node,
  3430. struct extent_buffer *parent)
  3431. {
  3432. struct btrfs_path *path;
  3433. int level;
  3434. int parent_level;
  3435. int ret = 0;
  3436. int wret;
  3437. path = btrfs_alloc_path();
  3438. BUG_ON(!path);
  3439. btrfs_assert_tree_locked(parent);
  3440. parent_level = btrfs_header_level(parent);
  3441. extent_buffer_get(parent);
  3442. path->nodes[parent_level] = parent;
  3443. path->slots[parent_level] = btrfs_header_nritems(parent);
  3444. btrfs_assert_tree_locked(node);
  3445. level = btrfs_header_level(node);
  3446. extent_buffer_get(node);
  3447. path->nodes[level] = node;
  3448. path->slots[level] = 0;
  3449. while (1) {
  3450. wret = walk_down_subtree(trans, root, path, &level);
  3451. if (wret < 0)
  3452. ret = wret;
  3453. if (wret != 0)
  3454. break;
  3455. wret = walk_up_tree(trans, root, path, &level, parent_level);
  3456. if (wret < 0)
  3457. ret = wret;
  3458. if (wret != 0)
  3459. break;
  3460. }
  3461. btrfs_free_path(path);
  3462. return ret;
  3463. }
  3464. static unsigned long calc_ra(unsigned long start, unsigned long last,
  3465. unsigned long nr)
  3466. {
  3467. return min(last, start + nr - 1);
  3468. }
  3469. static noinline int relocate_inode_pages(struct inode *inode, u64 start,
  3470. u64 len)
  3471. {
  3472. u64 page_start;
  3473. u64 page_end;
  3474. unsigned long first_index;
  3475. unsigned long last_index;
  3476. unsigned long i;
  3477. struct page *page;
  3478. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3479. struct file_ra_state *ra;
  3480. struct btrfs_ordered_extent *ordered;
  3481. unsigned int total_read = 0;
  3482. unsigned int total_dirty = 0;
  3483. int ret = 0;
  3484. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  3485. mutex_lock(&inode->i_mutex);
  3486. first_index = start >> PAGE_CACHE_SHIFT;
  3487. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  3488. /* make sure the dirty trick played by the caller work */
  3489. ret = invalidate_inode_pages2_range(inode->i_mapping,
  3490. first_index, last_index);
  3491. if (ret)
  3492. goto out_unlock;
  3493. file_ra_state_init(ra, inode->i_mapping);
  3494. for (i = first_index ; i <= last_index; i++) {
  3495. if (total_read % ra->ra_pages == 0) {
  3496. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  3497. calc_ra(i, last_index, ra->ra_pages));
  3498. }
  3499. total_read++;
  3500. again:
  3501. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  3502. BUG_ON(1);
  3503. page = grab_cache_page(inode->i_mapping, i);
  3504. if (!page) {
  3505. ret = -ENOMEM;
  3506. goto out_unlock;
  3507. }
  3508. if (!PageUptodate(page)) {
  3509. btrfs_readpage(NULL, page);
  3510. lock_page(page);
  3511. if (!PageUptodate(page)) {
  3512. unlock_page(page);
  3513. page_cache_release(page);
  3514. ret = -EIO;
  3515. goto out_unlock;
  3516. }
  3517. }
  3518. wait_on_page_writeback(page);
  3519. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  3520. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3521. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3522. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3523. if (ordered) {
  3524. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3525. unlock_page(page);
  3526. page_cache_release(page);
  3527. btrfs_start_ordered_extent(inode, ordered, 1);
  3528. btrfs_put_ordered_extent(ordered);
  3529. goto again;
  3530. }
  3531. set_page_extent_mapped(page);
  3532. if (i == first_index)
  3533. set_extent_bits(io_tree, page_start, page_end,
  3534. EXTENT_BOUNDARY, GFP_NOFS);
  3535. btrfs_set_extent_delalloc(inode, page_start, page_end);
  3536. set_page_dirty(page);
  3537. total_dirty++;
  3538. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3539. unlock_page(page);
  3540. page_cache_release(page);
  3541. }
  3542. out_unlock:
  3543. kfree(ra);
  3544. mutex_unlock(&inode->i_mutex);
  3545. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  3546. return ret;
  3547. }
  3548. static noinline int relocate_data_extent(struct inode *reloc_inode,
  3549. struct btrfs_key *extent_key,
  3550. u64 offset)
  3551. {
  3552. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  3553. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  3554. struct extent_map *em;
  3555. u64 start = extent_key->objectid - offset;
  3556. u64 end = start + extent_key->offset - 1;
  3557. em = alloc_extent_map(GFP_NOFS);
  3558. BUG_ON(!em || IS_ERR(em));
  3559. em->start = start;
  3560. em->len = extent_key->offset;
  3561. em->block_len = extent_key->offset;
  3562. em->block_start = extent_key->objectid;
  3563. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3564. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  3565. /* setup extent map to cheat btrfs_readpage */
  3566. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  3567. while (1) {
  3568. int ret;
  3569. spin_lock(&em_tree->lock);
  3570. ret = add_extent_mapping(em_tree, em);
  3571. spin_unlock(&em_tree->lock);
  3572. if (ret != -EEXIST) {
  3573. free_extent_map(em);
  3574. break;
  3575. }
  3576. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  3577. }
  3578. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  3579. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  3580. }
  3581. struct btrfs_ref_path {
  3582. u64 extent_start;
  3583. u64 nodes[BTRFS_MAX_LEVEL];
  3584. u64 root_objectid;
  3585. u64 root_generation;
  3586. u64 owner_objectid;
  3587. u32 num_refs;
  3588. int lowest_level;
  3589. int current_level;
  3590. int shared_level;
  3591. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  3592. u64 new_nodes[BTRFS_MAX_LEVEL];
  3593. };
  3594. struct disk_extent {
  3595. u64 ram_bytes;
  3596. u64 disk_bytenr;
  3597. u64 disk_num_bytes;
  3598. u64 offset;
  3599. u64 num_bytes;
  3600. u8 compression;
  3601. u8 encryption;
  3602. u16 other_encoding;
  3603. };
  3604. static int is_cowonly_root(u64 root_objectid)
  3605. {
  3606. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  3607. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  3608. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  3609. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  3610. root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  3611. root_objectid == BTRFS_CSUM_TREE_OBJECTID)
  3612. return 1;
  3613. return 0;
  3614. }
  3615. static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
  3616. struct btrfs_root *extent_root,
  3617. struct btrfs_ref_path *ref_path,
  3618. int first_time)
  3619. {
  3620. struct extent_buffer *leaf;
  3621. struct btrfs_path *path;
  3622. struct btrfs_extent_ref *ref;
  3623. struct btrfs_key key;
  3624. struct btrfs_key found_key;
  3625. u64 bytenr;
  3626. u32 nritems;
  3627. int level;
  3628. int ret = 1;
  3629. path = btrfs_alloc_path();
  3630. if (!path)
  3631. return -ENOMEM;
  3632. if (first_time) {
  3633. ref_path->lowest_level = -1;
  3634. ref_path->current_level = -1;
  3635. ref_path->shared_level = -1;
  3636. goto walk_up;
  3637. }
  3638. walk_down:
  3639. level = ref_path->current_level - 1;
  3640. while (level >= -1) {
  3641. u64 parent;
  3642. if (level < ref_path->lowest_level)
  3643. break;
  3644. if (level >= 0)
  3645. bytenr = ref_path->nodes[level];
  3646. else
  3647. bytenr = ref_path->extent_start;
  3648. BUG_ON(bytenr == 0);
  3649. parent = ref_path->nodes[level + 1];
  3650. ref_path->nodes[level + 1] = 0;
  3651. ref_path->current_level = level;
  3652. BUG_ON(parent == 0);
  3653. key.objectid = bytenr;
  3654. key.offset = parent + 1;
  3655. key.type = BTRFS_EXTENT_REF_KEY;
  3656. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3657. if (ret < 0)
  3658. goto out;
  3659. BUG_ON(ret == 0);
  3660. leaf = path->nodes[0];
  3661. nritems = btrfs_header_nritems(leaf);
  3662. if (path->slots[0] >= nritems) {
  3663. ret = btrfs_next_leaf(extent_root, path);
  3664. if (ret < 0)
  3665. goto out;
  3666. if (ret > 0)
  3667. goto next;
  3668. leaf = path->nodes[0];
  3669. }
  3670. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3671. if (found_key.objectid == bytenr &&
  3672. found_key.type == BTRFS_EXTENT_REF_KEY) {
  3673. if (level < ref_path->shared_level)
  3674. ref_path->shared_level = level;
  3675. goto found;
  3676. }
  3677. next:
  3678. level--;
  3679. btrfs_release_path(extent_root, path);
  3680. cond_resched();
  3681. }
  3682. /* reached lowest level */
  3683. ret = 1;
  3684. goto out;
  3685. walk_up:
  3686. level = ref_path->current_level;
  3687. while (level < BTRFS_MAX_LEVEL - 1) {
  3688. u64 ref_objectid;
  3689. if (level >= 0)
  3690. bytenr = ref_path->nodes[level];
  3691. else
  3692. bytenr = ref_path->extent_start;
  3693. BUG_ON(bytenr == 0);
  3694. key.objectid = bytenr;
  3695. key.offset = 0;
  3696. key.type = BTRFS_EXTENT_REF_KEY;
  3697. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3698. if (ret < 0)
  3699. goto out;
  3700. leaf = path->nodes[0];
  3701. nritems = btrfs_header_nritems(leaf);
  3702. if (path->slots[0] >= nritems) {
  3703. ret = btrfs_next_leaf(extent_root, path);
  3704. if (ret < 0)
  3705. goto out;
  3706. if (ret > 0) {
  3707. /* the extent was freed by someone */
  3708. if (ref_path->lowest_level == level)
  3709. goto out;
  3710. btrfs_release_path(extent_root, path);
  3711. goto walk_down;
  3712. }
  3713. leaf = path->nodes[0];
  3714. }
  3715. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3716. if (found_key.objectid != bytenr ||
  3717. found_key.type != BTRFS_EXTENT_REF_KEY) {
  3718. /* the extent was freed by someone */
  3719. if (ref_path->lowest_level == level) {
  3720. ret = 1;
  3721. goto out;
  3722. }
  3723. btrfs_release_path(extent_root, path);
  3724. goto walk_down;
  3725. }
  3726. found:
  3727. ref = btrfs_item_ptr(leaf, path->slots[0],
  3728. struct btrfs_extent_ref);
  3729. ref_objectid = btrfs_ref_objectid(leaf, ref);
  3730. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  3731. if (first_time) {
  3732. level = (int)ref_objectid;
  3733. BUG_ON(level >= BTRFS_MAX_LEVEL);
  3734. ref_path->lowest_level = level;
  3735. ref_path->current_level = level;
  3736. ref_path->nodes[level] = bytenr;
  3737. } else {
  3738. WARN_ON(ref_objectid != level);
  3739. }
  3740. } else {
  3741. WARN_ON(level != -1);
  3742. }
  3743. first_time = 0;
  3744. if (ref_path->lowest_level == level) {
  3745. ref_path->owner_objectid = ref_objectid;
  3746. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  3747. }
  3748. /*
  3749. * the block is tree root or the block isn't in reference
  3750. * counted tree.
  3751. */
  3752. if (found_key.objectid == found_key.offset ||
  3753. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  3754. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  3755. ref_path->root_generation =
  3756. btrfs_ref_generation(leaf, ref);
  3757. if (level < 0) {
  3758. /* special reference from the tree log */
  3759. ref_path->nodes[0] = found_key.offset;
  3760. ref_path->current_level = 0;
  3761. }
  3762. ret = 0;
  3763. goto out;
  3764. }
  3765. level++;
  3766. BUG_ON(ref_path->nodes[level] != 0);
  3767. ref_path->nodes[level] = found_key.offset;
  3768. ref_path->current_level = level;
  3769. /*
  3770. * the reference was created in the running transaction,
  3771. * no need to continue walking up.
  3772. */
  3773. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  3774. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  3775. ref_path->root_generation =
  3776. btrfs_ref_generation(leaf, ref);
  3777. ret = 0;
  3778. goto out;
  3779. }
  3780. btrfs_release_path(extent_root, path);
  3781. cond_resched();
  3782. }
  3783. /* reached max tree level, but no tree root found. */
  3784. BUG();
  3785. out:
  3786. btrfs_free_path(path);
  3787. return ret;
  3788. }
  3789. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  3790. struct btrfs_root *extent_root,
  3791. struct btrfs_ref_path *ref_path,
  3792. u64 extent_start)
  3793. {
  3794. memset(ref_path, 0, sizeof(*ref_path));
  3795. ref_path->extent_start = extent_start;
  3796. return __next_ref_path(trans, extent_root, ref_path, 1);
  3797. }
  3798. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  3799. struct btrfs_root *extent_root,
  3800. struct btrfs_ref_path *ref_path)
  3801. {
  3802. return __next_ref_path(trans, extent_root, ref_path, 0);
  3803. }
  3804. static noinline int get_new_locations(struct inode *reloc_inode,
  3805. struct btrfs_key *extent_key,
  3806. u64 offset, int no_fragment,
  3807. struct disk_extent **extents,
  3808. int *nr_extents)
  3809. {
  3810. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  3811. struct btrfs_path *path;
  3812. struct btrfs_file_extent_item *fi;
  3813. struct extent_buffer *leaf;
  3814. struct disk_extent *exts = *extents;
  3815. struct btrfs_key found_key;
  3816. u64 cur_pos;
  3817. u64 last_byte;
  3818. u32 nritems;
  3819. int nr = 0;
  3820. int max = *nr_extents;
  3821. int ret;
  3822. WARN_ON(!no_fragment && *extents);
  3823. if (!exts) {
  3824. max = 1;
  3825. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  3826. if (!exts)
  3827. return -ENOMEM;
  3828. }
  3829. path = btrfs_alloc_path();
  3830. BUG_ON(!path);
  3831. cur_pos = extent_key->objectid - offset;
  3832. last_byte = extent_key->objectid + extent_key->offset;
  3833. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  3834. cur_pos, 0);
  3835. if (ret < 0)
  3836. goto out;
  3837. if (ret > 0) {
  3838. ret = -ENOENT;
  3839. goto out;
  3840. }
  3841. while (1) {
  3842. leaf = path->nodes[0];
  3843. nritems = btrfs_header_nritems(leaf);
  3844. if (path->slots[0] >= nritems) {
  3845. ret = btrfs_next_leaf(root, path);
  3846. if (ret < 0)
  3847. goto out;
  3848. if (ret > 0)
  3849. break;
  3850. leaf = path->nodes[0];
  3851. }
  3852. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3853. if (found_key.offset != cur_pos ||
  3854. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  3855. found_key.objectid != reloc_inode->i_ino)
  3856. break;
  3857. fi = btrfs_item_ptr(leaf, path->slots[0],
  3858. struct btrfs_file_extent_item);
  3859. if (btrfs_file_extent_type(leaf, fi) !=
  3860. BTRFS_FILE_EXTENT_REG ||
  3861. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  3862. break;
  3863. if (nr == max) {
  3864. struct disk_extent *old = exts;
  3865. max *= 2;
  3866. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  3867. memcpy(exts, old, sizeof(*exts) * nr);
  3868. if (old != *extents)
  3869. kfree(old);
  3870. }
  3871. exts[nr].disk_bytenr =
  3872. btrfs_file_extent_disk_bytenr(leaf, fi);
  3873. exts[nr].disk_num_bytes =
  3874. btrfs_file_extent_disk_num_bytes(leaf, fi);
  3875. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  3876. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  3877. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  3878. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  3879. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  3880. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  3881. fi);
  3882. BUG_ON(exts[nr].offset > 0);
  3883. BUG_ON(exts[nr].compression || exts[nr].encryption);
  3884. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  3885. cur_pos += exts[nr].num_bytes;
  3886. nr++;
  3887. if (cur_pos + offset >= last_byte)
  3888. break;
  3889. if (no_fragment) {
  3890. ret = 1;
  3891. goto out;
  3892. }
  3893. path->slots[0]++;
  3894. }
  3895. BUG_ON(cur_pos + offset > last_byte);
  3896. if (cur_pos + offset < last_byte) {
  3897. ret = -ENOENT;
  3898. goto out;
  3899. }
  3900. ret = 0;
  3901. out:
  3902. btrfs_free_path(path);
  3903. if (ret) {
  3904. if (exts != *extents)
  3905. kfree(exts);
  3906. } else {
  3907. *extents = exts;
  3908. *nr_extents = nr;
  3909. }
  3910. return ret;
  3911. }
  3912. static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
  3913. struct btrfs_root *root,
  3914. struct btrfs_path *path,
  3915. struct btrfs_key *extent_key,
  3916. struct btrfs_key *leaf_key,
  3917. struct btrfs_ref_path *ref_path,
  3918. struct disk_extent *new_extents,
  3919. int nr_extents)
  3920. {
  3921. struct extent_buffer *leaf;
  3922. struct btrfs_file_extent_item *fi;
  3923. struct inode *inode = NULL;
  3924. struct btrfs_key key;
  3925. u64 lock_start = 0;
  3926. u64 lock_end = 0;
  3927. u64 num_bytes;
  3928. u64 ext_offset;
  3929. u64 search_end = (u64)-1;
  3930. u32 nritems;
  3931. int nr_scaned = 0;
  3932. int extent_locked = 0;
  3933. int extent_type;
  3934. int ret;
  3935. memcpy(&key, leaf_key, sizeof(key));
  3936. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  3937. if (key.objectid < ref_path->owner_objectid ||
  3938. (key.objectid == ref_path->owner_objectid &&
  3939. key.type < BTRFS_EXTENT_DATA_KEY)) {
  3940. key.objectid = ref_path->owner_objectid;
  3941. key.type = BTRFS_EXTENT_DATA_KEY;
  3942. key.offset = 0;
  3943. }
  3944. }
  3945. while (1) {
  3946. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3947. if (ret < 0)
  3948. goto out;
  3949. leaf = path->nodes[0];
  3950. nritems = btrfs_header_nritems(leaf);
  3951. next:
  3952. if (extent_locked && ret > 0) {
  3953. /*
  3954. * the file extent item was modified by someone
  3955. * before the extent got locked.
  3956. */
  3957. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3958. lock_end, GFP_NOFS);
  3959. extent_locked = 0;
  3960. }
  3961. if (path->slots[0] >= nritems) {
  3962. if (++nr_scaned > 2)
  3963. break;
  3964. BUG_ON(extent_locked);
  3965. ret = btrfs_next_leaf(root, path);
  3966. if (ret < 0)
  3967. goto out;
  3968. if (ret > 0)
  3969. break;
  3970. leaf = path->nodes[0];
  3971. nritems = btrfs_header_nritems(leaf);
  3972. }
  3973. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3974. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  3975. if ((key.objectid > ref_path->owner_objectid) ||
  3976. (key.objectid == ref_path->owner_objectid &&
  3977. key.type > BTRFS_EXTENT_DATA_KEY) ||
  3978. key.offset >= search_end)
  3979. break;
  3980. }
  3981. if (inode && key.objectid != inode->i_ino) {
  3982. BUG_ON(extent_locked);
  3983. btrfs_release_path(root, path);
  3984. mutex_unlock(&inode->i_mutex);
  3985. iput(inode);
  3986. inode = NULL;
  3987. continue;
  3988. }
  3989. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  3990. path->slots[0]++;
  3991. ret = 1;
  3992. goto next;
  3993. }
  3994. fi = btrfs_item_ptr(leaf, path->slots[0],
  3995. struct btrfs_file_extent_item);
  3996. extent_type = btrfs_file_extent_type(leaf, fi);
  3997. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  3998. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  3999. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  4000. extent_key->objectid)) {
  4001. path->slots[0]++;
  4002. ret = 1;
  4003. goto next;
  4004. }
  4005. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4006. ext_offset = btrfs_file_extent_offset(leaf, fi);
  4007. if (search_end == (u64)-1) {
  4008. search_end = key.offset - ext_offset +
  4009. btrfs_file_extent_ram_bytes(leaf, fi);
  4010. }
  4011. if (!extent_locked) {
  4012. lock_start = key.offset;
  4013. lock_end = lock_start + num_bytes - 1;
  4014. } else {
  4015. if (lock_start > key.offset ||
  4016. lock_end + 1 < key.offset + num_bytes) {
  4017. unlock_extent(&BTRFS_I(inode)->io_tree,
  4018. lock_start, lock_end, GFP_NOFS);
  4019. extent_locked = 0;
  4020. }
  4021. }
  4022. if (!inode) {
  4023. btrfs_release_path(root, path);
  4024. inode = btrfs_iget_locked(root->fs_info->sb,
  4025. key.objectid, root);
  4026. if (inode->i_state & I_NEW) {
  4027. BTRFS_I(inode)->root = root;
  4028. BTRFS_I(inode)->location.objectid =
  4029. key.objectid;
  4030. BTRFS_I(inode)->location.type =
  4031. BTRFS_INODE_ITEM_KEY;
  4032. BTRFS_I(inode)->location.offset = 0;
  4033. btrfs_read_locked_inode(inode);
  4034. unlock_new_inode(inode);
  4035. }
  4036. /*
  4037. * some code call btrfs_commit_transaction while
  4038. * holding the i_mutex, so we can't use mutex_lock
  4039. * here.
  4040. */
  4041. if (is_bad_inode(inode) ||
  4042. !mutex_trylock(&inode->i_mutex)) {
  4043. iput(inode);
  4044. inode = NULL;
  4045. key.offset = (u64)-1;
  4046. goto skip;
  4047. }
  4048. }
  4049. if (!extent_locked) {
  4050. struct btrfs_ordered_extent *ordered;
  4051. btrfs_release_path(root, path);
  4052. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4053. lock_end, GFP_NOFS);
  4054. ordered = btrfs_lookup_first_ordered_extent(inode,
  4055. lock_end);
  4056. if (ordered &&
  4057. ordered->file_offset <= lock_end &&
  4058. ordered->file_offset + ordered->len > lock_start) {
  4059. unlock_extent(&BTRFS_I(inode)->io_tree,
  4060. lock_start, lock_end, GFP_NOFS);
  4061. btrfs_start_ordered_extent(inode, ordered, 1);
  4062. btrfs_put_ordered_extent(ordered);
  4063. key.offset += num_bytes;
  4064. goto skip;
  4065. }
  4066. if (ordered)
  4067. btrfs_put_ordered_extent(ordered);
  4068. extent_locked = 1;
  4069. continue;
  4070. }
  4071. if (nr_extents == 1) {
  4072. /* update extent pointer in place */
  4073. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4074. new_extents[0].disk_bytenr);
  4075. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4076. new_extents[0].disk_num_bytes);
  4077. btrfs_mark_buffer_dirty(leaf);
  4078. btrfs_drop_extent_cache(inode, key.offset,
  4079. key.offset + num_bytes - 1, 0);
  4080. ret = btrfs_inc_extent_ref(trans, root,
  4081. new_extents[0].disk_bytenr,
  4082. new_extents[0].disk_num_bytes,
  4083. leaf->start,
  4084. root->root_key.objectid,
  4085. trans->transid,
  4086. key.objectid);
  4087. BUG_ON(ret);
  4088. ret = btrfs_free_extent(trans, root,
  4089. extent_key->objectid,
  4090. extent_key->offset,
  4091. leaf->start,
  4092. btrfs_header_owner(leaf),
  4093. btrfs_header_generation(leaf),
  4094. key.objectid, 0);
  4095. BUG_ON(ret);
  4096. btrfs_release_path(root, path);
  4097. key.offset += num_bytes;
  4098. } else {
  4099. BUG_ON(1);
  4100. #if 0
  4101. u64 alloc_hint;
  4102. u64 extent_len;
  4103. int i;
  4104. /*
  4105. * drop old extent pointer at first, then insert the
  4106. * new pointers one bye one
  4107. */
  4108. btrfs_release_path(root, path);
  4109. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  4110. key.offset + num_bytes,
  4111. key.offset, &alloc_hint);
  4112. BUG_ON(ret);
  4113. for (i = 0; i < nr_extents; i++) {
  4114. if (ext_offset >= new_extents[i].num_bytes) {
  4115. ext_offset -= new_extents[i].num_bytes;
  4116. continue;
  4117. }
  4118. extent_len = min(new_extents[i].num_bytes -
  4119. ext_offset, num_bytes);
  4120. ret = btrfs_insert_empty_item(trans, root,
  4121. path, &key,
  4122. sizeof(*fi));
  4123. BUG_ON(ret);
  4124. leaf = path->nodes[0];
  4125. fi = btrfs_item_ptr(leaf, path->slots[0],
  4126. struct btrfs_file_extent_item);
  4127. btrfs_set_file_extent_generation(leaf, fi,
  4128. trans->transid);
  4129. btrfs_set_file_extent_type(leaf, fi,
  4130. BTRFS_FILE_EXTENT_REG);
  4131. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4132. new_extents[i].disk_bytenr);
  4133. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4134. new_extents[i].disk_num_bytes);
  4135. btrfs_set_file_extent_ram_bytes(leaf, fi,
  4136. new_extents[i].ram_bytes);
  4137. btrfs_set_file_extent_compression(leaf, fi,
  4138. new_extents[i].compression);
  4139. btrfs_set_file_extent_encryption(leaf, fi,
  4140. new_extents[i].encryption);
  4141. btrfs_set_file_extent_other_encoding(leaf, fi,
  4142. new_extents[i].other_encoding);
  4143. btrfs_set_file_extent_num_bytes(leaf, fi,
  4144. extent_len);
  4145. ext_offset += new_extents[i].offset;
  4146. btrfs_set_file_extent_offset(leaf, fi,
  4147. ext_offset);
  4148. btrfs_mark_buffer_dirty(leaf);
  4149. btrfs_drop_extent_cache(inode, key.offset,
  4150. key.offset + extent_len - 1, 0);
  4151. ret = btrfs_inc_extent_ref(trans, root,
  4152. new_extents[i].disk_bytenr,
  4153. new_extents[i].disk_num_bytes,
  4154. leaf->start,
  4155. root->root_key.objectid,
  4156. trans->transid, key.objectid);
  4157. BUG_ON(ret);
  4158. btrfs_release_path(root, path);
  4159. inode_add_bytes(inode, extent_len);
  4160. ext_offset = 0;
  4161. num_bytes -= extent_len;
  4162. key.offset += extent_len;
  4163. if (num_bytes == 0)
  4164. break;
  4165. }
  4166. BUG_ON(i >= nr_extents);
  4167. #endif
  4168. }
  4169. if (extent_locked) {
  4170. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4171. lock_end, GFP_NOFS);
  4172. extent_locked = 0;
  4173. }
  4174. skip:
  4175. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  4176. key.offset >= search_end)
  4177. break;
  4178. cond_resched();
  4179. }
  4180. ret = 0;
  4181. out:
  4182. btrfs_release_path(root, path);
  4183. if (inode) {
  4184. mutex_unlock(&inode->i_mutex);
  4185. if (extent_locked) {
  4186. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4187. lock_end, GFP_NOFS);
  4188. }
  4189. iput(inode);
  4190. }
  4191. return ret;
  4192. }
  4193. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  4194. struct btrfs_root *root,
  4195. struct extent_buffer *buf, u64 orig_start)
  4196. {
  4197. int level;
  4198. int ret;
  4199. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  4200. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  4201. level = btrfs_header_level(buf);
  4202. if (level == 0) {
  4203. struct btrfs_leaf_ref *ref;
  4204. struct btrfs_leaf_ref *orig_ref;
  4205. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  4206. if (!orig_ref)
  4207. return -ENOENT;
  4208. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  4209. if (!ref) {
  4210. btrfs_free_leaf_ref(root, orig_ref);
  4211. return -ENOMEM;
  4212. }
  4213. ref->nritems = orig_ref->nritems;
  4214. memcpy(ref->extents, orig_ref->extents,
  4215. sizeof(ref->extents[0]) * ref->nritems);
  4216. btrfs_free_leaf_ref(root, orig_ref);
  4217. ref->root_gen = trans->transid;
  4218. ref->bytenr = buf->start;
  4219. ref->owner = btrfs_header_owner(buf);
  4220. ref->generation = btrfs_header_generation(buf);
  4221. ret = btrfs_add_leaf_ref(root, ref, 0);
  4222. WARN_ON(ret);
  4223. btrfs_free_leaf_ref(root, ref);
  4224. }
  4225. return 0;
  4226. }
  4227. static noinline int invalidate_extent_cache(struct btrfs_root *root,
  4228. struct extent_buffer *leaf,
  4229. struct btrfs_block_group_cache *group,
  4230. struct btrfs_root *target_root)
  4231. {
  4232. struct btrfs_key key;
  4233. struct inode *inode = NULL;
  4234. struct btrfs_file_extent_item *fi;
  4235. u64 num_bytes;
  4236. u64 skip_objectid = 0;
  4237. u32 nritems;
  4238. u32 i;
  4239. nritems = btrfs_header_nritems(leaf);
  4240. for (i = 0; i < nritems; i++) {
  4241. btrfs_item_key_to_cpu(leaf, &key, i);
  4242. if (key.objectid == skip_objectid ||
  4243. key.type != BTRFS_EXTENT_DATA_KEY)
  4244. continue;
  4245. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  4246. if (btrfs_file_extent_type(leaf, fi) ==
  4247. BTRFS_FILE_EXTENT_INLINE)
  4248. continue;
  4249. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  4250. continue;
  4251. if (!inode || inode->i_ino != key.objectid) {
  4252. iput(inode);
  4253. inode = btrfs_ilookup(target_root->fs_info->sb,
  4254. key.objectid, target_root, 1);
  4255. }
  4256. if (!inode) {
  4257. skip_objectid = key.objectid;
  4258. continue;
  4259. }
  4260. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4261. lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  4262. key.offset + num_bytes - 1, GFP_NOFS);
  4263. btrfs_drop_extent_cache(inode, key.offset,
  4264. key.offset + num_bytes - 1, 1);
  4265. unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  4266. key.offset + num_bytes - 1, GFP_NOFS);
  4267. cond_resched();
  4268. }
  4269. iput(inode);
  4270. return 0;
  4271. }
  4272. static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  4273. struct btrfs_root *root,
  4274. struct extent_buffer *leaf,
  4275. struct btrfs_block_group_cache *group,
  4276. struct inode *reloc_inode)
  4277. {
  4278. struct btrfs_key key;
  4279. struct btrfs_key extent_key;
  4280. struct btrfs_file_extent_item *fi;
  4281. struct btrfs_leaf_ref *ref;
  4282. struct disk_extent *new_extent;
  4283. u64 bytenr;
  4284. u64 num_bytes;
  4285. u32 nritems;
  4286. u32 i;
  4287. int ext_index;
  4288. int nr_extent;
  4289. int ret;
  4290. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  4291. BUG_ON(!new_extent);
  4292. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  4293. BUG_ON(!ref);
  4294. ext_index = -1;
  4295. nritems = btrfs_header_nritems(leaf);
  4296. for (i = 0; i < nritems; i++) {
  4297. btrfs_item_key_to_cpu(leaf, &key, i);
  4298. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  4299. continue;
  4300. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  4301. if (btrfs_file_extent_type(leaf, fi) ==
  4302. BTRFS_FILE_EXTENT_INLINE)
  4303. continue;
  4304. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  4305. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  4306. if (bytenr == 0)
  4307. continue;
  4308. ext_index++;
  4309. if (bytenr >= group->key.objectid + group->key.offset ||
  4310. bytenr + num_bytes <= group->key.objectid)
  4311. continue;
  4312. extent_key.objectid = bytenr;
  4313. extent_key.offset = num_bytes;
  4314. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  4315. nr_extent = 1;
  4316. ret = get_new_locations(reloc_inode, &extent_key,
  4317. group->key.objectid, 1,
  4318. &new_extent, &nr_extent);
  4319. if (ret > 0)
  4320. continue;
  4321. BUG_ON(ret < 0);
  4322. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  4323. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  4324. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  4325. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  4326. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4327. new_extent->disk_bytenr);
  4328. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4329. new_extent->disk_num_bytes);
  4330. btrfs_mark_buffer_dirty(leaf);
  4331. ret = btrfs_inc_extent_ref(trans, root,
  4332. new_extent->disk_bytenr,
  4333. new_extent->disk_num_bytes,
  4334. leaf->start,
  4335. root->root_key.objectid,
  4336. trans->transid, key.objectid);
  4337. BUG_ON(ret);
  4338. ret = btrfs_free_extent(trans, root,
  4339. bytenr, num_bytes, leaf->start,
  4340. btrfs_header_owner(leaf),
  4341. btrfs_header_generation(leaf),
  4342. key.objectid, 0);
  4343. BUG_ON(ret);
  4344. cond_resched();
  4345. }
  4346. kfree(new_extent);
  4347. BUG_ON(ext_index + 1 != ref->nritems);
  4348. btrfs_free_leaf_ref(root, ref);
  4349. return 0;
  4350. }
  4351. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  4352. struct btrfs_root *root)
  4353. {
  4354. struct btrfs_root *reloc_root;
  4355. int ret;
  4356. if (root->reloc_root) {
  4357. reloc_root = root->reloc_root;
  4358. root->reloc_root = NULL;
  4359. list_add(&reloc_root->dead_list,
  4360. &root->fs_info->dead_reloc_roots);
  4361. btrfs_set_root_bytenr(&reloc_root->root_item,
  4362. reloc_root->node->start);
  4363. btrfs_set_root_level(&root->root_item,
  4364. btrfs_header_level(reloc_root->node));
  4365. memset(&reloc_root->root_item.drop_progress, 0,
  4366. sizeof(struct btrfs_disk_key));
  4367. reloc_root->root_item.drop_level = 0;
  4368. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  4369. &reloc_root->root_key,
  4370. &reloc_root->root_item);
  4371. BUG_ON(ret);
  4372. }
  4373. return 0;
  4374. }
  4375. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  4376. {
  4377. struct btrfs_trans_handle *trans;
  4378. struct btrfs_root *reloc_root;
  4379. struct btrfs_root *prev_root = NULL;
  4380. struct list_head dead_roots;
  4381. int ret;
  4382. unsigned long nr;
  4383. INIT_LIST_HEAD(&dead_roots);
  4384. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  4385. while (!list_empty(&dead_roots)) {
  4386. reloc_root = list_entry(dead_roots.prev,
  4387. struct btrfs_root, dead_list);
  4388. list_del_init(&reloc_root->dead_list);
  4389. BUG_ON(reloc_root->commit_root != NULL);
  4390. while (1) {
  4391. trans = btrfs_join_transaction(root, 1);
  4392. BUG_ON(!trans);
  4393. mutex_lock(&root->fs_info->drop_mutex);
  4394. ret = btrfs_drop_snapshot(trans, reloc_root);
  4395. if (ret != -EAGAIN)
  4396. break;
  4397. mutex_unlock(&root->fs_info->drop_mutex);
  4398. nr = trans->blocks_used;
  4399. ret = btrfs_end_transaction(trans, root);
  4400. BUG_ON(ret);
  4401. btrfs_btree_balance_dirty(root, nr);
  4402. }
  4403. free_extent_buffer(reloc_root->node);
  4404. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  4405. &reloc_root->root_key);
  4406. BUG_ON(ret);
  4407. mutex_unlock(&root->fs_info->drop_mutex);
  4408. nr = trans->blocks_used;
  4409. ret = btrfs_end_transaction(trans, root);
  4410. BUG_ON(ret);
  4411. btrfs_btree_balance_dirty(root, nr);
  4412. kfree(prev_root);
  4413. prev_root = reloc_root;
  4414. }
  4415. if (prev_root) {
  4416. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  4417. kfree(prev_root);
  4418. }
  4419. return 0;
  4420. }
  4421. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  4422. {
  4423. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  4424. return 0;
  4425. }
  4426. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  4427. {
  4428. struct btrfs_root *reloc_root;
  4429. struct btrfs_trans_handle *trans;
  4430. struct btrfs_key location;
  4431. int found;
  4432. int ret;
  4433. mutex_lock(&root->fs_info->tree_reloc_mutex);
  4434. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  4435. BUG_ON(ret);
  4436. found = !list_empty(&root->fs_info->dead_reloc_roots);
  4437. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  4438. if (found) {
  4439. trans = btrfs_start_transaction(root, 1);
  4440. BUG_ON(!trans);
  4441. ret = btrfs_commit_transaction(trans, root);
  4442. BUG_ON(ret);
  4443. }
  4444. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  4445. location.offset = (u64)-1;
  4446. location.type = BTRFS_ROOT_ITEM_KEY;
  4447. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  4448. BUG_ON(!reloc_root);
  4449. btrfs_orphan_cleanup(reloc_root);
  4450. return 0;
  4451. }
  4452. static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
  4453. struct btrfs_root *root)
  4454. {
  4455. struct btrfs_root *reloc_root;
  4456. struct extent_buffer *eb;
  4457. struct btrfs_root_item *root_item;
  4458. struct btrfs_key root_key;
  4459. int ret;
  4460. BUG_ON(!root->ref_cows);
  4461. if (root->reloc_root)
  4462. return 0;
  4463. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  4464. BUG_ON(!root_item);
  4465. ret = btrfs_copy_root(trans, root, root->commit_root,
  4466. &eb, BTRFS_TREE_RELOC_OBJECTID);
  4467. BUG_ON(ret);
  4468. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  4469. root_key.offset = root->root_key.objectid;
  4470. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4471. memcpy(root_item, &root->root_item, sizeof(root_item));
  4472. btrfs_set_root_refs(root_item, 0);
  4473. btrfs_set_root_bytenr(root_item, eb->start);
  4474. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  4475. btrfs_set_root_generation(root_item, trans->transid);
  4476. btrfs_tree_unlock(eb);
  4477. free_extent_buffer(eb);
  4478. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  4479. &root_key, root_item);
  4480. BUG_ON(ret);
  4481. kfree(root_item);
  4482. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  4483. &root_key);
  4484. BUG_ON(!reloc_root);
  4485. reloc_root->last_trans = trans->transid;
  4486. reloc_root->commit_root = NULL;
  4487. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  4488. root->reloc_root = reloc_root;
  4489. return 0;
  4490. }
  4491. /*
  4492. * Core function of space balance.
  4493. *
  4494. * The idea is using reloc trees to relocate tree blocks in reference
  4495. * counted roots. There is one reloc tree for each subvol, and all
  4496. * reloc trees share same root key objectid. Reloc trees are snapshots
  4497. * of the latest committed roots of subvols (root->commit_root).
  4498. *
  4499. * To relocate a tree block referenced by a subvol, there are two steps.
  4500. * COW the block through subvol's reloc tree, then update block pointer
  4501. * in the subvol to point to the new block. Since all reloc trees share
  4502. * same root key objectid, doing special handing for tree blocks owned
  4503. * by them is easy. Once a tree block has been COWed in one reloc tree,
  4504. * we can use the resulting new block directly when the same block is
  4505. * required to COW again through other reloc trees. By this way, relocated
  4506. * tree blocks are shared between reloc trees, so they are also shared
  4507. * between subvols.
  4508. */
  4509. static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
  4510. struct btrfs_root *root,
  4511. struct btrfs_path *path,
  4512. struct btrfs_key *first_key,
  4513. struct btrfs_ref_path *ref_path,
  4514. struct btrfs_block_group_cache *group,
  4515. struct inode *reloc_inode)
  4516. {
  4517. struct btrfs_root *reloc_root;
  4518. struct extent_buffer *eb = NULL;
  4519. struct btrfs_key *keys;
  4520. u64 *nodes;
  4521. int level;
  4522. int shared_level;
  4523. int lowest_level = 0;
  4524. int ret;
  4525. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  4526. lowest_level = ref_path->owner_objectid;
  4527. if (!root->ref_cows) {
  4528. path->lowest_level = lowest_level;
  4529. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  4530. BUG_ON(ret < 0);
  4531. path->lowest_level = 0;
  4532. btrfs_release_path(root, path);
  4533. return 0;
  4534. }
  4535. mutex_lock(&root->fs_info->tree_reloc_mutex);
  4536. ret = init_reloc_tree(trans, root);
  4537. BUG_ON(ret);
  4538. reloc_root = root->reloc_root;
  4539. shared_level = ref_path->shared_level;
  4540. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  4541. keys = ref_path->node_keys;
  4542. nodes = ref_path->new_nodes;
  4543. memset(&keys[shared_level + 1], 0,
  4544. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  4545. memset(&nodes[shared_level + 1], 0,
  4546. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  4547. if (nodes[lowest_level] == 0) {
  4548. path->lowest_level = lowest_level;
  4549. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  4550. 0, 1);
  4551. BUG_ON(ret);
  4552. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  4553. eb = path->nodes[level];
  4554. if (!eb || eb == reloc_root->node)
  4555. break;
  4556. nodes[level] = eb->start;
  4557. if (level == 0)
  4558. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  4559. else
  4560. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  4561. }
  4562. if (nodes[0] &&
  4563. ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4564. eb = path->nodes[0];
  4565. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  4566. group, reloc_inode);
  4567. BUG_ON(ret);
  4568. }
  4569. btrfs_release_path(reloc_root, path);
  4570. } else {
  4571. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  4572. lowest_level);
  4573. BUG_ON(ret);
  4574. }
  4575. /*
  4576. * replace tree blocks in the fs tree with tree blocks in
  4577. * the reloc tree.
  4578. */
  4579. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  4580. BUG_ON(ret < 0);
  4581. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4582. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  4583. 0, 0);
  4584. BUG_ON(ret);
  4585. extent_buffer_get(path->nodes[0]);
  4586. eb = path->nodes[0];
  4587. btrfs_release_path(reloc_root, path);
  4588. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  4589. BUG_ON(ret);
  4590. free_extent_buffer(eb);
  4591. }
  4592. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  4593. path->lowest_level = 0;
  4594. return 0;
  4595. }
  4596. static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
  4597. struct btrfs_root *root,
  4598. struct btrfs_path *path,
  4599. struct btrfs_key *first_key,
  4600. struct btrfs_ref_path *ref_path)
  4601. {
  4602. int ret;
  4603. ret = relocate_one_path(trans, root, path, first_key,
  4604. ref_path, NULL, NULL);
  4605. BUG_ON(ret);
  4606. return 0;
  4607. }
  4608. static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
  4609. struct btrfs_root *extent_root,
  4610. struct btrfs_path *path,
  4611. struct btrfs_key *extent_key)
  4612. {
  4613. int ret;
  4614. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  4615. if (ret)
  4616. goto out;
  4617. ret = btrfs_del_item(trans, extent_root, path);
  4618. out:
  4619. btrfs_release_path(extent_root, path);
  4620. return ret;
  4621. }
  4622. static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
  4623. struct btrfs_ref_path *ref_path)
  4624. {
  4625. struct btrfs_key root_key;
  4626. root_key.objectid = ref_path->root_objectid;
  4627. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4628. if (is_cowonly_root(ref_path->root_objectid))
  4629. root_key.offset = 0;
  4630. else
  4631. root_key.offset = (u64)-1;
  4632. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  4633. }
  4634. static noinline int relocate_one_extent(struct btrfs_root *extent_root,
  4635. struct btrfs_path *path,
  4636. struct btrfs_key *extent_key,
  4637. struct btrfs_block_group_cache *group,
  4638. struct inode *reloc_inode, int pass)
  4639. {
  4640. struct btrfs_trans_handle *trans;
  4641. struct btrfs_root *found_root;
  4642. struct btrfs_ref_path *ref_path = NULL;
  4643. struct disk_extent *new_extents = NULL;
  4644. int nr_extents = 0;
  4645. int loops;
  4646. int ret;
  4647. int level;
  4648. struct btrfs_key first_key;
  4649. u64 prev_block = 0;
  4650. trans = btrfs_start_transaction(extent_root, 1);
  4651. BUG_ON(!trans);
  4652. if (extent_key->objectid == 0) {
  4653. ret = del_extent_zero(trans, extent_root, path, extent_key);
  4654. goto out;
  4655. }
  4656. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  4657. if (!ref_path) {
  4658. ret = -ENOMEM;
  4659. goto out;
  4660. }
  4661. for (loops = 0; ; loops++) {
  4662. if (loops == 0) {
  4663. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  4664. extent_key->objectid);
  4665. } else {
  4666. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  4667. }
  4668. if (ret < 0)
  4669. goto out;
  4670. if (ret > 0)
  4671. break;
  4672. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  4673. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  4674. continue;
  4675. found_root = read_ref_root(extent_root->fs_info, ref_path);
  4676. BUG_ON(!found_root);
  4677. /*
  4678. * for reference counted tree, only process reference paths
  4679. * rooted at the latest committed root.
  4680. */
  4681. if (found_root->ref_cows &&
  4682. ref_path->root_generation != found_root->root_key.offset)
  4683. continue;
  4684. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4685. if (pass == 0) {
  4686. /*
  4687. * copy data extents to new locations
  4688. */
  4689. u64 group_start = group->key.objectid;
  4690. ret = relocate_data_extent(reloc_inode,
  4691. extent_key,
  4692. group_start);
  4693. if (ret < 0)
  4694. goto out;
  4695. break;
  4696. }
  4697. level = 0;
  4698. } else {
  4699. level = ref_path->owner_objectid;
  4700. }
  4701. if (prev_block != ref_path->nodes[level]) {
  4702. struct extent_buffer *eb;
  4703. u64 block_start = ref_path->nodes[level];
  4704. u64 block_size = btrfs_level_size(found_root, level);
  4705. eb = read_tree_block(found_root, block_start,
  4706. block_size, 0);
  4707. btrfs_tree_lock(eb);
  4708. BUG_ON(level != btrfs_header_level(eb));
  4709. if (level == 0)
  4710. btrfs_item_key_to_cpu(eb, &first_key, 0);
  4711. else
  4712. btrfs_node_key_to_cpu(eb, &first_key, 0);
  4713. btrfs_tree_unlock(eb);
  4714. free_extent_buffer(eb);
  4715. prev_block = block_start;
  4716. }
  4717. mutex_lock(&extent_root->fs_info->trans_mutex);
  4718. btrfs_record_root_in_trans(found_root);
  4719. mutex_unlock(&extent_root->fs_info->trans_mutex);
  4720. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4721. /*
  4722. * try to update data extent references while
  4723. * keeping metadata shared between snapshots.
  4724. */
  4725. if (pass == 1) {
  4726. ret = relocate_one_path(trans, found_root,
  4727. path, &first_key, ref_path,
  4728. group, reloc_inode);
  4729. if (ret < 0)
  4730. goto out;
  4731. continue;
  4732. }
  4733. /*
  4734. * use fallback method to process the remaining
  4735. * references.
  4736. */
  4737. if (!new_extents) {
  4738. u64 group_start = group->key.objectid;
  4739. new_extents = kmalloc(sizeof(*new_extents),
  4740. GFP_NOFS);
  4741. nr_extents = 1;
  4742. ret = get_new_locations(reloc_inode,
  4743. extent_key,
  4744. group_start, 1,
  4745. &new_extents,
  4746. &nr_extents);
  4747. if (ret)
  4748. goto out;
  4749. }
  4750. ret = replace_one_extent(trans, found_root,
  4751. path, extent_key,
  4752. &first_key, ref_path,
  4753. new_extents, nr_extents);
  4754. } else {
  4755. ret = relocate_tree_block(trans, found_root, path,
  4756. &first_key, ref_path);
  4757. }
  4758. if (ret < 0)
  4759. goto out;
  4760. }
  4761. ret = 0;
  4762. out:
  4763. btrfs_end_transaction(trans, extent_root);
  4764. kfree(new_extents);
  4765. kfree(ref_path);
  4766. return ret;
  4767. }
  4768. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  4769. {
  4770. u64 num_devices;
  4771. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  4772. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  4773. num_devices = root->fs_info->fs_devices->rw_devices;
  4774. if (num_devices == 1) {
  4775. stripped |= BTRFS_BLOCK_GROUP_DUP;
  4776. stripped = flags & ~stripped;
  4777. /* turn raid0 into single device chunks */
  4778. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  4779. return stripped;
  4780. /* turn mirroring into duplication */
  4781. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  4782. BTRFS_BLOCK_GROUP_RAID10))
  4783. return stripped | BTRFS_BLOCK_GROUP_DUP;
  4784. return flags;
  4785. } else {
  4786. /* they already had raid on here, just return */
  4787. if (flags & stripped)
  4788. return flags;
  4789. stripped |= BTRFS_BLOCK_GROUP_DUP;
  4790. stripped = flags & ~stripped;
  4791. /* switch duplicated blocks with raid1 */
  4792. if (flags & BTRFS_BLOCK_GROUP_DUP)
  4793. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  4794. /* turn single device chunks into raid0 */
  4795. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  4796. }
  4797. return flags;
  4798. }
  4799. static int __alloc_chunk_for_shrink(struct btrfs_root *root,
  4800. struct btrfs_block_group_cache *shrink_block_group,
  4801. int force)
  4802. {
  4803. struct btrfs_trans_handle *trans;
  4804. u64 new_alloc_flags;
  4805. u64 calc;
  4806. spin_lock(&shrink_block_group->lock);
  4807. if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
  4808. spin_unlock(&shrink_block_group->lock);
  4809. trans = btrfs_start_transaction(root, 1);
  4810. spin_lock(&shrink_block_group->lock);
  4811. new_alloc_flags = update_block_group_flags(root,
  4812. shrink_block_group->flags);
  4813. if (new_alloc_flags != shrink_block_group->flags) {
  4814. calc =
  4815. btrfs_block_group_used(&shrink_block_group->item);
  4816. } else {
  4817. calc = shrink_block_group->key.offset;
  4818. }
  4819. spin_unlock(&shrink_block_group->lock);
  4820. do_chunk_alloc(trans, root->fs_info->extent_root,
  4821. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  4822. btrfs_end_transaction(trans, root);
  4823. } else
  4824. spin_unlock(&shrink_block_group->lock);
  4825. return 0;
  4826. }
  4827. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  4828. struct btrfs_root *root,
  4829. u64 objectid, u64 size)
  4830. {
  4831. struct btrfs_path *path;
  4832. struct btrfs_inode_item *item;
  4833. struct extent_buffer *leaf;
  4834. int ret;
  4835. path = btrfs_alloc_path();
  4836. if (!path)
  4837. return -ENOMEM;
  4838. path->leave_spinning = 1;
  4839. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  4840. if (ret)
  4841. goto out;
  4842. leaf = path->nodes[0];
  4843. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  4844. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  4845. btrfs_set_inode_generation(leaf, item, 1);
  4846. btrfs_set_inode_size(leaf, item, size);
  4847. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  4848. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
  4849. btrfs_mark_buffer_dirty(leaf);
  4850. btrfs_release_path(root, path);
  4851. out:
  4852. btrfs_free_path(path);
  4853. return ret;
  4854. }
  4855. static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
  4856. struct btrfs_block_group_cache *group)
  4857. {
  4858. struct inode *inode = NULL;
  4859. struct btrfs_trans_handle *trans;
  4860. struct btrfs_root *root;
  4861. struct btrfs_key root_key;
  4862. u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
  4863. int err = 0;
  4864. root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  4865. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4866. root_key.offset = (u64)-1;
  4867. root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  4868. if (IS_ERR(root))
  4869. return ERR_CAST(root);
  4870. trans = btrfs_start_transaction(root, 1);
  4871. BUG_ON(!trans);
  4872. err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
  4873. if (err)
  4874. goto out;
  4875. err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
  4876. BUG_ON(err);
  4877. err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
  4878. group->key.offset, 0, group->key.offset,
  4879. 0, 0, 0);
  4880. BUG_ON(err);
  4881. inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
  4882. if (inode->i_state & I_NEW) {
  4883. BTRFS_I(inode)->root = root;
  4884. BTRFS_I(inode)->location.objectid = objectid;
  4885. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  4886. BTRFS_I(inode)->location.offset = 0;
  4887. btrfs_read_locked_inode(inode);
  4888. unlock_new_inode(inode);
  4889. BUG_ON(is_bad_inode(inode));
  4890. } else {
  4891. BUG_ON(1);
  4892. }
  4893. BTRFS_I(inode)->index_cnt = group->key.objectid;
  4894. err = btrfs_orphan_add(trans, inode);
  4895. out:
  4896. btrfs_end_transaction(trans, root);
  4897. if (err) {
  4898. if (inode)
  4899. iput(inode);
  4900. inode = ERR_PTR(err);
  4901. }
  4902. return inode;
  4903. }
  4904. int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
  4905. {
  4906. struct btrfs_ordered_sum *sums;
  4907. struct btrfs_sector_sum *sector_sum;
  4908. struct btrfs_ordered_extent *ordered;
  4909. struct btrfs_root *root = BTRFS_I(inode)->root;
  4910. struct list_head list;
  4911. size_t offset;
  4912. int ret;
  4913. u64 disk_bytenr;
  4914. INIT_LIST_HEAD(&list);
  4915. ordered = btrfs_lookup_ordered_extent(inode, file_pos);
  4916. BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
  4917. disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
  4918. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
  4919. disk_bytenr + len - 1, &list);
  4920. while (!list_empty(&list)) {
  4921. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  4922. list_del_init(&sums->list);
  4923. sector_sum = sums->sums;
  4924. sums->bytenr = ordered->start;
  4925. offset = 0;
  4926. while (offset < sums->len) {
  4927. sector_sum->bytenr += ordered->start - disk_bytenr;
  4928. sector_sum++;
  4929. offset += root->sectorsize;
  4930. }
  4931. btrfs_add_ordered_sum(inode, ordered, sums);
  4932. }
  4933. btrfs_put_ordered_extent(ordered);
  4934. return 0;
  4935. }
  4936. int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
  4937. {
  4938. struct btrfs_trans_handle *trans;
  4939. struct btrfs_path *path;
  4940. struct btrfs_fs_info *info = root->fs_info;
  4941. struct extent_buffer *leaf;
  4942. struct inode *reloc_inode;
  4943. struct btrfs_block_group_cache *block_group;
  4944. struct btrfs_key key;
  4945. u64 skipped;
  4946. u64 cur_byte;
  4947. u64 total_found;
  4948. u32 nritems;
  4949. int ret;
  4950. int progress;
  4951. int pass = 0;
  4952. root = root->fs_info->extent_root;
  4953. block_group = btrfs_lookup_block_group(info, group_start);
  4954. BUG_ON(!block_group);
  4955. printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
  4956. (unsigned long long)block_group->key.objectid,
  4957. (unsigned long long)block_group->flags);
  4958. path = btrfs_alloc_path();
  4959. BUG_ON(!path);
  4960. reloc_inode = create_reloc_inode(info, block_group);
  4961. BUG_ON(IS_ERR(reloc_inode));
  4962. __alloc_chunk_for_shrink(root, block_group, 1);
  4963. set_block_group_readonly(block_group);
  4964. btrfs_start_delalloc_inodes(info->tree_root);
  4965. btrfs_wait_ordered_extents(info->tree_root, 0);
  4966. again:
  4967. skipped = 0;
  4968. total_found = 0;
  4969. progress = 0;
  4970. key.objectid = block_group->key.objectid;
  4971. key.offset = 0;
  4972. key.type = 0;
  4973. cur_byte = key.objectid;
  4974. trans = btrfs_start_transaction(info->tree_root, 1);
  4975. btrfs_commit_transaction(trans, info->tree_root);
  4976. mutex_lock(&root->fs_info->cleaner_mutex);
  4977. btrfs_clean_old_snapshots(info->tree_root);
  4978. btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
  4979. mutex_unlock(&root->fs_info->cleaner_mutex);
  4980. trans = btrfs_start_transaction(info->tree_root, 1);
  4981. btrfs_commit_transaction(trans, info->tree_root);
  4982. while (1) {
  4983. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4984. if (ret < 0)
  4985. goto out;
  4986. next:
  4987. leaf = path->nodes[0];
  4988. nritems = btrfs_header_nritems(leaf);
  4989. if (path->slots[0] >= nritems) {
  4990. ret = btrfs_next_leaf(root, path);
  4991. if (ret < 0)
  4992. goto out;
  4993. if (ret == 1) {
  4994. ret = 0;
  4995. break;
  4996. }
  4997. leaf = path->nodes[0];
  4998. nritems = btrfs_header_nritems(leaf);
  4999. }
  5000. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  5001. if (key.objectid >= block_group->key.objectid +
  5002. block_group->key.offset)
  5003. break;
  5004. if (progress && need_resched()) {
  5005. btrfs_release_path(root, path);
  5006. cond_resched();
  5007. progress = 0;
  5008. continue;
  5009. }
  5010. progress = 1;
  5011. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
  5012. key.objectid + key.offset <= cur_byte) {
  5013. path->slots[0]++;
  5014. goto next;
  5015. }
  5016. total_found++;
  5017. cur_byte = key.objectid + key.offset;
  5018. btrfs_release_path(root, path);
  5019. __alloc_chunk_for_shrink(root, block_group, 0);
  5020. ret = relocate_one_extent(root, path, &key, block_group,
  5021. reloc_inode, pass);
  5022. BUG_ON(ret < 0);
  5023. if (ret > 0)
  5024. skipped++;
  5025. key.objectid = cur_byte;
  5026. key.type = 0;
  5027. key.offset = 0;
  5028. }
  5029. btrfs_release_path(root, path);
  5030. if (pass == 0) {
  5031. btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
  5032. invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
  5033. }
  5034. if (total_found > 0) {
  5035. printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
  5036. (unsigned long long)total_found, pass);
  5037. pass++;
  5038. if (total_found == skipped && pass > 2) {
  5039. iput(reloc_inode);
  5040. reloc_inode = create_reloc_inode(info, block_group);
  5041. pass = 0;
  5042. }
  5043. goto again;
  5044. }
  5045. /* delete reloc_inode */
  5046. iput(reloc_inode);
  5047. /* unpin extents in this range */
  5048. trans = btrfs_start_transaction(info->tree_root, 1);
  5049. btrfs_commit_transaction(trans, info->tree_root);
  5050. spin_lock(&block_group->lock);
  5051. WARN_ON(block_group->pinned > 0);
  5052. WARN_ON(block_group->reserved > 0);
  5053. WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
  5054. spin_unlock(&block_group->lock);
  5055. btrfs_put_block_group(block_group);
  5056. ret = 0;
  5057. out:
  5058. btrfs_free_path(path);
  5059. return ret;
  5060. }
  5061. static int find_first_block_group(struct btrfs_root *root,
  5062. struct btrfs_path *path, struct btrfs_key *key)
  5063. {
  5064. int ret = 0;
  5065. struct btrfs_key found_key;
  5066. struct extent_buffer *leaf;
  5067. int slot;
  5068. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  5069. if (ret < 0)
  5070. goto out;
  5071. while (1) {
  5072. slot = path->slots[0];
  5073. leaf = path->nodes[0];
  5074. if (slot >= btrfs_header_nritems(leaf)) {
  5075. ret = btrfs_next_leaf(root, path);
  5076. if (ret == 0)
  5077. continue;
  5078. if (ret < 0)
  5079. goto out;
  5080. break;
  5081. }
  5082. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5083. if (found_key.objectid >= key->objectid &&
  5084. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  5085. ret = 0;
  5086. goto out;
  5087. }
  5088. path->slots[0]++;
  5089. }
  5090. ret = -ENOENT;
  5091. out:
  5092. return ret;
  5093. }
  5094. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  5095. {
  5096. struct btrfs_block_group_cache *block_group;
  5097. struct btrfs_space_info *space_info;
  5098. struct rb_node *n;
  5099. spin_lock(&info->block_group_cache_lock);
  5100. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  5101. block_group = rb_entry(n, struct btrfs_block_group_cache,
  5102. cache_node);
  5103. rb_erase(&block_group->cache_node,
  5104. &info->block_group_cache_tree);
  5105. spin_unlock(&info->block_group_cache_lock);
  5106. btrfs_remove_free_space_cache(block_group);
  5107. down_write(&block_group->space_info->groups_sem);
  5108. list_del(&block_group->list);
  5109. up_write(&block_group->space_info->groups_sem);
  5110. WARN_ON(atomic_read(&block_group->count) != 1);
  5111. kfree(block_group);
  5112. spin_lock(&info->block_group_cache_lock);
  5113. }
  5114. spin_unlock(&info->block_group_cache_lock);
  5115. /* now that all the block groups are freed, go through and
  5116. * free all the space_info structs. This is only called during
  5117. * the final stages of unmount, and so we know nobody is
  5118. * using them. We call synchronize_rcu() once before we start,
  5119. * just to be on the safe side.
  5120. */
  5121. synchronize_rcu();
  5122. while(!list_empty(&info->space_info)) {
  5123. space_info = list_entry(info->space_info.next,
  5124. struct btrfs_space_info,
  5125. list);
  5126. list_del(&space_info->list);
  5127. kfree(space_info);
  5128. }
  5129. return 0;
  5130. }
  5131. int btrfs_read_block_groups(struct btrfs_root *root)
  5132. {
  5133. struct btrfs_path *path;
  5134. int ret;
  5135. struct btrfs_block_group_cache *cache;
  5136. struct btrfs_fs_info *info = root->fs_info;
  5137. struct btrfs_space_info *space_info;
  5138. struct btrfs_key key;
  5139. struct btrfs_key found_key;
  5140. struct extent_buffer *leaf;
  5141. root = info->extent_root;
  5142. key.objectid = 0;
  5143. key.offset = 0;
  5144. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  5145. path = btrfs_alloc_path();
  5146. if (!path)
  5147. return -ENOMEM;
  5148. while (1) {
  5149. ret = find_first_block_group(root, path, &key);
  5150. if (ret > 0) {
  5151. ret = 0;
  5152. goto error;
  5153. }
  5154. if (ret != 0)
  5155. goto error;
  5156. leaf = path->nodes[0];
  5157. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5158. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  5159. if (!cache) {
  5160. ret = -ENOMEM;
  5161. break;
  5162. }
  5163. atomic_set(&cache->count, 1);
  5164. spin_lock_init(&cache->lock);
  5165. spin_lock_init(&cache->tree_lock);
  5166. mutex_init(&cache->cache_mutex);
  5167. INIT_LIST_HEAD(&cache->list);
  5168. INIT_LIST_HEAD(&cache->cluster_list);
  5169. read_extent_buffer(leaf, &cache->item,
  5170. btrfs_item_ptr_offset(leaf, path->slots[0]),
  5171. sizeof(cache->item));
  5172. memcpy(&cache->key, &found_key, sizeof(found_key));
  5173. key.objectid = found_key.objectid + found_key.offset;
  5174. btrfs_release_path(root, path);
  5175. cache->flags = btrfs_block_group_flags(&cache->item);
  5176. ret = update_space_info(info, cache->flags, found_key.offset,
  5177. btrfs_block_group_used(&cache->item),
  5178. &space_info);
  5179. BUG_ON(ret);
  5180. cache->space_info = space_info;
  5181. down_write(&space_info->groups_sem);
  5182. list_add_tail(&cache->list, &space_info->block_groups);
  5183. up_write(&space_info->groups_sem);
  5184. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  5185. BUG_ON(ret);
  5186. set_avail_alloc_bits(root->fs_info, cache->flags);
  5187. if (btrfs_chunk_readonly(root, cache->key.objectid))
  5188. set_block_group_readonly(cache);
  5189. }
  5190. ret = 0;
  5191. error:
  5192. btrfs_free_path(path);
  5193. return ret;
  5194. }
  5195. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  5196. struct btrfs_root *root, u64 bytes_used,
  5197. u64 type, u64 chunk_objectid, u64 chunk_offset,
  5198. u64 size)
  5199. {
  5200. int ret;
  5201. struct btrfs_root *extent_root;
  5202. struct btrfs_block_group_cache *cache;
  5203. extent_root = root->fs_info->extent_root;
  5204. root->fs_info->last_trans_log_full_commit = trans->transid;
  5205. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  5206. if (!cache)
  5207. return -ENOMEM;
  5208. cache->key.objectid = chunk_offset;
  5209. cache->key.offset = size;
  5210. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  5211. atomic_set(&cache->count, 1);
  5212. spin_lock_init(&cache->lock);
  5213. spin_lock_init(&cache->tree_lock);
  5214. mutex_init(&cache->cache_mutex);
  5215. INIT_LIST_HEAD(&cache->list);
  5216. INIT_LIST_HEAD(&cache->cluster_list);
  5217. btrfs_set_block_group_used(&cache->item, bytes_used);
  5218. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  5219. cache->flags = type;
  5220. btrfs_set_block_group_flags(&cache->item, type);
  5221. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  5222. &cache->space_info);
  5223. BUG_ON(ret);
  5224. down_write(&cache->space_info->groups_sem);
  5225. list_add_tail(&cache->list, &cache->space_info->block_groups);
  5226. up_write(&cache->space_info->groups_sem);
  5227. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  5228. BUG_ON(ret);
  5229. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  5230. sizeof(cache->item));
  5231. BUG_ON(ret);
  5232. set_avail_alloc_bits(extent_root->fs_info, type);
  5233. return 0;
  5234. }
  5235. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  5236. struct btrfs_root *root, u64 group_start)
  5237. {
  5238. struct btrfs_path *path;
  5239. struct btrfs_block_group_cache *block_group;
  5240. struct btrfs_key key;
  5241. int ret;
  5242. root = root->fs_info->extent_root;
  5243. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  5244. BUG_ON(!block_group);
  5245. BUG_ON(!block_group->ro);
  5246. memcpy(&key, &block_group->key, sizeof(key));
  5247. path = btrfs_alloc_path();
  5248. BUG_ON(!path);
  5249. spin_lock(&root->fs_info->block_group_cache_lock);
  5250. rb_erase(&block_group->cache_node,
  5251. &root->fs_info->block_group_cache_tree);
  5252. spin_unlock(&root->fs_info->block_group_cache_lock);
  5253. btrfs_remove_free_space_cache(block_group);
  5254. down_write(&block_group->space_info->groups_sem);
  5255. list_del(&block_group->list);
  5256. up_write(&block_group->space_info->groups_sem);
  5257. spin_lock(&block_group->space_info->lock);
  5258. block_group->space_info->total_bytes -= block_group->key.offset;
  5259. block_group->space_info->bytes_readonly -= block_group->key.offset;
  5260. spin_unlock(&block_group->space_info->lock);
  5261. block_group->space_info->full = 0;
  5262. btrfs_put_block_group(block_group);
  5263. btrfs_put_block_group(block_group);
  5264. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  5265. if (ret > 0)
  5266. ret = -EIO;
  5267. if (ret < 0)
  5268. goto out;
  5269. ret = btrfs_del_item(trans, root, path);
  5270. out:
  5271. btrfs_free_path(path);
  5272. return ret;
  5273. }