extent-tree.c 136 KB

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