extent-tree.c 135 KB

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