extent-tree.c 153 KB

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