extent-tree.c 136 KB

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