extent-tree.c 154 KB

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