extent-tree.c 136 KB

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