extent-tree.c 155 KB

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