extent-tree.c 154 KB

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