relocation.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819
  1. /*
  2. * Copyright (C) 2009 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 <linux/rbtree.h>
  23. #include <linux/slab.h>
  24. #include "ctree.h"
  25. #include "disk-io.h"
  26. #include "transaction.h"
  27. #include "volumes.h"
  28. #include "locking.h"
  29. #include "btrfs_inode.h"
  30. #include "async-thread.h"
  31. #include "free-space-cache.h"
  32. #include "inode-map.h"
  33. #include "qgroup.h"
  34. /*
  35. * backref_node, mapping_node and tree_block start with this
  36. */
  37. struct tree_entry {
  38. struct rb_node rb_node;
  39. u64 bytenr;
  40. };
  41. /*
  42. * present a tree block in the backref cache
  43. */
  44. struct backref_node {
  45. struct rb_node rb_node;
  46. u64 bytenr;
  47. u64 new_bytenr;
  48. /* objectid of tree block owner, can be not uptodate */
  49. u64 owner;
  50. /* link to pending, changed or detached list */
  51. struct list_head list;
  52. /* list of upper level blocks reference this block */
  53. struct list_head upper;
  54. /* list of child blocks in the cache */
  55. struct list_head lower;
  56. /* NULL if this node is not tree root */
  57. struct btrfs_root *root;
  58. /* extent buffer got by COW the block */
  59. struct extent_buffer *eb;
  60. /* level of tree block */
  61. unsigned int level:8;
  62. /* is the block in non-reference counted tree */
  63. unsigned int cowonly:1;
  64. /* 1 if no child node in the cache */
  65. unsigned int lowest:1;
  66. /* is the extent buffer locked */
  67. unsigned int locked:1;
  68. /* has the block been processed */
  69. unsigned int processed:1;
  70. /* have backrefs of this block been checked */
  71. unsigned int checked:1;
  72. /*
  73. * 1 if corresponding block has been cowed but some upper
  74. * level block pointers may not point to the new location
  75. */
  76. unsigned int pending:1;
  77. /*
  78. * 1 if the backref node isn't connected to any other
  79. * backref node.
  80. */
  81. unsigned int detached:1;
  82. };
  83. /*
  84. * present a block pointer in the backref cache
  85. */
  86. struct backref_edge {
  87. struct list_head list[2];
  88. struct backref_node *node[2];
  89. };
  90. #define LOWER 0
  91. #define UPPER 1
  92. #define RELOCATION_RESERVED_NODES 256
  93. struct backref_cache {
  94. /* red black tree of all backref nodes in the cache */
  95. struct rb_root rb_root;
  96. /* for passing backref nodes to btrfs_reloc_cow_block */
  97. struct backref_node *path[BTRFS_MAX_LEVEL];
  98. /*
  99. * list of blocks that have been cowed but some block
  100. * pointers in upper level blocks may not reflect the
  101. * new location
  102. */
  103. struct list_head pending[BTRFS_MAX_LEVEL];
  104. /* list of backref nodes with no child node */
  105. struct list_head leaves;
  106. /* list of blocks that have been cowed in current transaction */
  107. struct list_head changed;
  108. /* list of detached backref node. */
  109. struct list_head detached;
  110. u64 last_trans;
  111. int nr_nodes;
  112. int nr_edges;
  113. };
  114. /*
  115. * map address of tree root to tree
  116. */
  117. struct mapping_node {
  118. struct rb_node rb_node;
  119. u64 bytenr;
  120. void *data;
  121. };
  122. struct mapping_tree {
  123. struct rb_root rb_root;
  124. spinlock_t lock;
  125. };
  126. /*
  127. * present a tree block to process
  128. */
  129. struct tree_block {
  130. struct rb_node rb_node;
  131. u64 bytenr;
  132. struct btrfs_key key;
  133. unsigned int level:8;
  134. unsigned int key_ready:1;
  135. };
  136. #define MAX_EXTENTS 128
  137. struct file_extent_cluster {
  138. u64 start;
  139. u64 end;
  140. u64 boundary[MAX_EXTENTS];
  141. unsigned int nr;
  142. };
  143. struct reloc_control {
  144. /* block group to relocate */
  145. struct btrfs_block_group_cache *block_group;
  146. /* extent tree */
  147. struct btrfs_root *extent_root;
  148. /* inode for moving data */
  149. struct inode *data_inode;
  150. struct btrfs_block_rsv *block_rsv;
  151. struct backref_cache backref_cache;
  152. struct file_extent_cluster cluster;
  153. /* tree blocks have been processed */
  154. struct extent_io_tree processed_blocks;
  155. /* map start of tree root to corresponding reloc tree */
  156. struct mapping_tree reloc_root_tree;
  157. /* list of reloc trees */
  158. struct list_head reloc_roots;
  159. /* size of metadata reservation for merging reloc trees */
  160. u64 merging_rsv_size;
  161. /* size of relocated tree nodes */
  162. u64 nodes_relocated;
  163. /* reserved size for block group relocation*/
  164. u64 reserved_bytes;
  165. u64 search_start;
  166. u64 extents_found;
  167. unsigned int stage:8;
  168. unsigned int create_reloc_tree:1;
  169. unsigned int merge_reloc_tree:1;
  170. unsigned int found_file_extent:1;
  171. };
  172. /* stages of data relocation */
  173. #define MOVE_DATA_EXTENTS 0
  174. #define UPDATE_DATA_PTRS 1
  175. static void remove_backref_node(struct backref_cache *cache,
  176. struct backref_node *node);
  177. static void __mark_block_processed(struct reloc_control *rc,
  178. struct backref_node *node);
  179. static void mapping_tree_init(struct mapping_tree *tree)
  180. {
  181. tree->rb_root = RB_ROOT;
  182. spin_lock_init(&tree->lock);
  183. }
  184. static void backref_cache_init(struct backref_cache *cache)
  185. {
  186. int i;
  187. cache->rb_root = RB_ROOT;
  188. for (i = 0; i < BTRFS_MAX_LEVEL; i++)
  189. INIT_LIST_HEAD(&cache->pending[i]);
  190. INIT_LIST_HEAD(&cache->changed);
  191. INIT_LIST_HEAD(&cache->detached);
  192. INIT_LIST_HEAD(&cache->leaves);
  193. }
  194. static void backref_cache_cleanup(struct backref_cache *cache)
  195. {
  196. struct backref_node *node;
  197. int i;
  198. while (!list_empty(&cache->detached)) {
  199. node = list_entry(cache->detached.next,
  200. struct backref_node, list);
  201. remove_backref_node(cache, node);
  202. }
  203. while (!list_empty(&cache->leaves)) {
  204. node = list_entry(cache->leaves.next,
  205. struct backref_node, lower);
  206. remove_backref_node(cache, node);
  207. }
  208. cache->last_trans = 0;
  209. for (i = 0; i < BTRFS_MAX_LEVEL; i++)
  210. ASSERT(list_empty(&cache->pending[i]));
  211. ASSERT(list_empty(&cache->changed));
  212. ASSERT(list_empty(&cache->detached));
  213. ASSERT(RB_EMPTY_ROOT(&cache->rb_root));
  214. ASSERT(!cache->nr_nodes);
  215. ASSERT(!cache->nr_edges);
  216. }
  217. static struct backref_node *alloc_backref_node(struct backref_cache *cache)
  218. {
  219. struct backref_node *node;
  220. node = kzalloc(sizeof(*node), GFP_NOFS);
  221. if (node) {
  222. INIT_LIST_HEAD(&node->list);
  223. INIT_LIST_HEAD(&node->upper);
  224. INIT_LIST_HEAD(&node->lower);
  225. RB_CLEAR_NODE(&node->rb_node);
  226. cache->nr_nodes++;
  227. }
  228. return node;
  229. }
  230. static void free_backref_node(struct backref_cache *cache,
  231. struct backref_node *node)
  232. {
  233. if (node) {
  234. cache->nr_nodes--;
  235. kfree(node);
  236. }
  237. }
  238. static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
  239. {
  240. struct backref_edge *edge;
  241. edge = kzalloc(sizeof(*edge), GFP_NOFS);
  242. if (edge)
  243. cache->nr_edges++;
  244. return edge;
  245. }
  246. static void free_backref_edge(struct backref_cache *cache,
  247. struct backref_edge *edge)
  248. {
  249. if (edge) {
  250. cache->nr_edges--;
  251. kfree(edge);
  252. }
  253. }
  254. static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
  255. struct rb_node *node)
  256. {
  257. struct rb_node **p = &root->rb_node;
  258. struct rb_node *parent = NULL;
  259. struct tree_entry *entry;
  260. while (*p) {
  261. parent = *p;
  262. entry = rb_entry(parent, struct tree_entry, rb_node);
  263. if (bytenr < entry->bytenr)
  264. p = &(*p)->rb_left;
  265. else if (bytenr > entry->bytenr)
  266. p = &(*p)->rb_right;
  267. else
  268. return parent;
  269. }
  270. rb_link_node(node, parent, p);
  271. rb_insert_color(node, root);
  272. return NULL;
  273. }
  274. static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
  275. {
  276. struct rb_node *n = root->rb_node;
  277. struct tree_entry *entry;
  278. while (n) {
  279. entry = rb_entry(n, struct tree_entry, rb_node);
  280. if (bytenr < entry->bytenr)
  281. n = n->rb_left;
  282. else if (bytenr > entry->bytenr)
  283. n = n->rb_right;
  284. else
  285. return n;
  286. }
  287. return NULL;
  288. }
  289. static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
  290. {
  291. struct btrfs_fs_info *fs_info = NULL;
  292. struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
  293. rb_node);
  294. if (bnode->root)
  295. fs_info = bnode->root->fs_info;
  296. btrfs_panic(fs_info, errno,
  297. "Inconsistency in backref cache found at offset %llu",
  298. bytenr);
  299. }
  300. /*
  301. * walk up backref nodes until reach node presents tree root
  302. */
  303. static struct backref_node *walk_up_backref(struct backref_node *node,
  304. struct backref_edge *edges[],
  305. int *index)
  306. {
  307. struct backref_edge *edge;
  308. int idx = *index;
  309. while (!list_empty(&node->upper)) {
  310. edge = list_entry(node->upper.next,
  311. struct backref_edge, list[LOWER]);
  312. edges[idx++] = edge;
  313. node = edge->node[UPPER];
  314. }
  315. BUG_ON(node->detached);
  316. *index = idx;
  317. return node;
  318. }
  319. /*
  320. * walk down backref nodes to find start of next reference path
  321. */
  322. static struct backref_node *walk_down_backref(struct backref_edge *edges[],
  323. int *index)
  324. {
  325. struct backref_edge *edge;
  326. struct backref_node *lower;
  327. int idx = *index;
  328. while (idx > 0) {
  329. edge = edges[idx - 1];
  330. lower = edge->node[LOWER];
  331. if (list_is_last(&edge->list[LOWER], &lower->upper)) {
  332. idx--;
  333. continue;
  334. }
  335. edge = list_entry(edge->list[LOWER].next,
  336. struct backref_edge, list[LOWER]);
  337. edges[idx - 1] = edge;
  338. *index = idx;
  339. return edge->node[UPPER];
  340. }
  341. *index = 0;
  342. return NULL;
  343. }
  344. static void unlock_node_buffer(struct backref_node *node)
  345. {
  346. if (node->locked) {
  347. btrfs_tree_unlock(node->eb);
  348. node->locked = 0;
  349. }
  350. }
  351. static void drop_node_buffer(struct backref_node *node)
  352. {
  353. if (node->eb) {
  354. unlock_node_buffer(node);
  355. free_extent_buffer(node->eb);
  356. node->eb = NULL;
  357. }
  358. }
  359. static void drop_backref_node(struct backref_cache *tree,
  360. struct backref_node *node)
  361. {
  362. BUG_ON(!list_empty(&node->upper));
  363. drop_node_buffer(node);
  364. list_del(&node->list);
  365. list_del(&node->lower);
  366. if (!RB_EMPTY_NODE(&node->rb_node))
  367. rb_erase(&node->rb_node, &tree->rb_root);
  368. free_backref_node(tree, node);
  369. }
  370. /*
  371. * remove a backref node from the backref cache
  372. */
  373. static void remove_backref_node(struct backref_cache *cache,
  374. struct backref_node *node)
  375. {
  376. struct backref_node *upper;
  377. struct backref_edge *edge;
  378. if (!node)
  379. return;
  380. BUG_ON(!node->lowest && !node->detached);
  381. while (!list_empty(&node->upper)) {
  382. edge = list_entry(node->upper.next, struct backref_edge,
  383. list[LOWER]);
  384. upper = edge->node[UPPER];
  385. list_del(&edge->list[LOWER]);
  386. list_del(&edge->list[UPPER]);
  387. free_backref_edge(cache, edge);
  388. if (RB_EMPTY_NODE(&upper->rb_node)) {
  389. BUG_ON(!list_empty(&node->upper));
  390. drop_backref_node(cache, node);
  391. node = upper;
  392. node->lowest = 1;
  393. continue;
  394. }
  395. /*
  396. * add the node to leaf node list if no other
  397. * child block cached.
  398. */
  399. if (list_empty(&upper->lower)) {
  400. list_add_tail(&upper->lower, &cache->leaves);
  401. upper->lowest = 1;
  402. }
  403. }
  404. drop_backref_node(cache, node);
  405. }
  406. static void update_backref_node(struct backref_cache *cache,
  407. struct backref_node *node, u64 bytenr)
  408. {
  409. struct rb_node *rb_node;
  410. rb_erase(&node->rb_node, &cache->rb_root);
  411. node->bytenr = bytenr;
  412. rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
  413. if (rb_node)
  414. backref_tree_panic(rb_node, -EEXIST, bytenr);
  415. }
  416. /*
  417. * update backref cache after a transaction commit
  418. */
  419. static int update_backref_cache(struct btrfs_trans_handle *trans,
  420. struct backref_cache *cache)
  421. {
  422. struct backref_node *node;
  423. int level = 0;
  424. if (cache->last_trans == 0) {
  425. cache->last_trans = trans->transid;
  426. return 0;
  427. }
  428. if (cache->last_trans == trans->transid)
  429. return 0;
  430. /*
  431. * detached nodes are used to avoid unnecessary backref
  432. * lookup. transaction commit changes the extent tree.
  433. * so the detached nodes are no longer useful.
  434. */
  435. while (!list_empty(&cache->detached)) {
  436. node = list_entry(cache->detached.next,
  437. struct backref_node, list);
  438. remove_backref_node(cache, node);
  439. }
  440. while (!list_empty(&cache->changed)) {
  441. node = list_entry(cache->changed.next,
  442. struct backref_node, list);
  443. list_del_init(&node->list);
  444. BUG_ON(node->pending);
  445. update_backref_node(cache, node, node->new_bytenr);
  446. }
  447. /*
  448. * some nodes can be left in the pending list if there were
  449. * errors during processing the pending nodes.
  450. */
  451. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  452. list_for_each_entry(node, &cache->pending[level], list) {
  453. BUG_ON(!node->pending);
  454. if (node->bytenr == node->new_bytenr)
  455. continue;
  456. update_backref_node(cache, node, node->new_bytenr);
  457. }
  458. }
  459. cache->last_trans = 0;
  460. return 1;
  461. }
  462. static int should_ignore_root(struct btrfs_root *root)
  463. {
  464. struct btrfs_root *reloc_root;
  465. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  466. return 0;
  467. reloc_root = root->reloc_root;
  468. if (!reloc_root)
  469. return 0;
  470. if (btrfs_root_last_snapshot(&reloc_root->root_item) ==
  471. root->fs_info->running_transaction->transid - 1)
  472. return 0;
  473. /*
  474. * if there is reloc tree and it was created in previous
  475. * transaction backref lookup can find the reloc tree,
  476. * so backref node for the fs tree root is useless for
  477. * relocation.
  478. */
  479. return 1;
  480. }
  481. /*
  482. * find reloc tree by address of tree root
  483. */
  484. static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
  485. u64 bytenr)
  486. {
  487. struct rb_node *rb_node;
  488. struct mapping_node *node;
  489. struct btrfs_root *root = NULL;
  490. spin_lock(&rc->reloc_root_tree.lock);
  491. rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
  492. if (rb_node) {
  493. node = rb_entry(rb_node, struct mapping_node, rb_node);
  494. root = (struct btrfs_root *)node->data;
  495. }
  496. spin_unlock(&rc->reloc_root_tree.lock);
  497. return root;
  498. }
  499. static int is_cowonly_root(u64 root_objectid)
  500. {
  501. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  502. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  503. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  504. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  505. root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  506. root_objectid == BTRFS_CSUM_TREE_OBJECTID ||
  507. root_objectid == BTRFS_UUID_TREE_OBJECTID ||
  508. root_objectid == BTRFS_QUOTA_TREE_OBJECTID ||
  509. root_objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
  510. return 1;
  511. return 0;
  512. }
  513. static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
  514. u64 root_objectid)
  515. {
  516. struct btrfs_key key;
  517. key.objectid = root_objectid;
  518. key.type = BTRFS_ROOT_ITEM_KEY;
  519. if (is_cowonly_root(root_objectid))
  520. key.offset = 0;
  521. else
  522. key.offset = (u64)-1;
  523. return btrfs_get_fs_root(fs_info, &key, false);
  524. }
  525. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  526. static noinline_for_stack
  527. struct btrfs_root *find_tree_root(struct reloc_control *rc,
  528. struct extent_buffer *leaf,
  529. struct btrfs_extent_ref_v0 *ref0)
  530. {
  531. struct btrfs_root *root;
  532. u64 root_objectid = btrfs_ref_root_v0(leaf, ref0);
  533. u64 generation = btrfs_ref_generation_v0(leaf, ref0);
  534. BUG_ON(root_objectid == BTRFS_TREE_RELOC_OBJECTID);
  535. root = read_fs_root(rc->extent_root->fs_info, root_objectid);
  536. BUG_ON(IS_ERR(root));
  537. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  538. generation != btrfs_root_generation(&root->root_item))
  539. return NULL;
  540. return root;
  541. }
  542. #endif
  543. static noinline_for_stack
  544. int find_inline_backref(struct extent_buffer *leaf, int slot,
  545. unsigned long *ptr, unsigned long *end)
  546. {
  547. struct btrfs_key key;
  548. struct btrfs_extent_item *ei;
  549. struct btrfs_tree_block_info *bi;
  550. u32 item_size;
  551. btrfs_item_key_to_cpu(leaf, &key, slot);
  552. item_size = btrfs_item_size_nr(leaf, slot);
  553. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  554. if (item_size < sizeof(*ei)) {
  555. WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  556. return 1;
  557. }
  558. #endif
  559. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  560. WARN_ON(!(btrfs_extent_flags(leaf, ei) &
  561. BTRFS_EXTENT_FLAG_TREE_BLOCK));
  562. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  563. item_size <= sizeof(*ei) + sizeof(*bi)) {
  564. WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
  565. return 1;
  566. }
  567. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  568. item_size <= sizeof(*ei)) {
  569. WARN_ON(item_size < sizeof(*ei));
  570. return 1;
  571. }
  572. if (key.type == BTRFS_EXTENT_ITEM_KEY) {
  573. bi = (struct btrfs_tree_block_info *)(ei + 1);
  574. *ptr = (unsigned long)(bi + 1);
  575. } else {
  576. *ptr = (unsigned long)(ei + 1);
  577. }
  578. *end = (unsigned long)ei + item_size;
  579. return 0;
  580. }
  581. /*
  582. * build backref tree for a given tree block. root of the backref tree
  583. * corresponds the tree block, leaves of the backref tree correspond
  584. * roots of b-trees that reference the tree block.
  585. *
  586. * the basic idea of this function is check backrefs of a given block
  587. * to find upper level blocks that reference the block, and then check
  588. * backrefs of these upper level blocks recursively. the recursion stop
  589. * when tree root is reached or backrefs for the block is cached.
  590. *
  591. * NOTE: if we find backrefs for a block are cached, we know backrefs
  592. * for all upper level blocks that directly/indirectly reference the
  593. * block are also cached.
  594. */
  595. static noinline_for_stack
  596. struct backref_node *build_backref_tree(struct reloc_control *rc,
  597. struct btrfs_key *node_key,
  598. int level, u64 bytenr)
  599. {
  600. struct backref_cache *cache = &rc->backref_cache;
  601. struct btrfs_path *path1;
  602. struct btrfs_path *path2;
  603. struct extent_buffer *eb;
  604. struct btrfs_root *root;
  605. struct backref_node *cur;
  606. struct backref_node *upper;
  607. struct backref_node *lower;
  608. struct backref_node *node = NULL;
  609. struct backref_node *exist = NULL;
  610. struct backref_edge *edge;
  611. struct rb_node *rb_node;
  612. struct btrfs_key key;
  613. unsigned long end;
  614. unsigned long ptr;
  615. LIST_HEAD(list);
  616. LIST_HEAD(useless);
  617. int cowonly;
  618. int ret;
  619. int err = 0;
  620. bool need_check = true;
  621. path1 = btrfs_alloc_path();
  622. path2 = btrfs_alloc_path();
  623. if (!path1 || !path2) {
  624. err = -ENOMEM;
  625. goto out;
  626. }
  627. path1->reada = READA_FORWARD;
  628. path2->reada = READA_FORWARD;
  629. node = alloc_backref_node(cache);
  630. if (!node) {
  631. err = -ENOMEM;
  632. goto out;
  633. }
  634. node->bytenr = bytenr;
  635. node->level = level;
  636. node->lowest = 1;
  637. cur = node;
  638. again:
  639. end = 0;
  640. ptr = 0;
  641. key.objectid = cur->bytenr;
  642. key.type = BTRFS_METADATA_ITEM_KEY;
  643. key.offset = (u64)-1;
  644. path1->search_commit_root = 1;
  645. path1->skip_locking = 1;
  646. ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
  647. 0, 0);
  648. if (ret < 0) {
  649. err = ret;
  650. goto out;
  651. }
  652. ASSERT(ret);
  653. ASSERT(path1->slots[0]);
  654. path1->slots[0]--;
  655. WARN_ON(cur->checked);
  656. if (!list_empty(&cur->upper)) {
  657. /*
  658. * the backref was added previously when processing
  659. * backref of type BTRFS_TREE_BLOCK_REF_KEY
  660. */
  661. ASSERT(list_is_singular(&cur->upper));
  662. edge = list_entry(cur->upper.next, struct backref_edge,
  663. list[LOWER]);
  664. ASSERT(list_empty(&edge->list[UPPER]));
  665. exist = edge->node[UPPER];
  666. /*
  667. * add the upper level block to pending list if we need
  668. * check its backrefs
  669. */
  670. if (!exist->checked)
  671. list_add_tail(&edge->list[UPPER], &list);
  672. } else {
  673. exist = NULL;
  674. }
  675. while (1) {
  676. cond_resched();
  677. eb = path1->nodes[0];
  678. if (ptr >= end) {
  679. if (path1->slots[0] >= btrfs_header_nritems(eb)) {
  680. ret = btrfs_next_leaf(rc->extent_root, path1);
  681. if (ret < 0) {
  682. err = ret;
  683. goto out;
  684. }
  685. if (ret > 0)
  686. break;
  687. eb = path1->nodes[0];
  688. }
  689. btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
  690. if (key.objectid != cur->bytenr) {
  691. WARN_ON(exist);
  692. break;
  693. }
  694. if (key.type == BTRFS_EXTENT_ITEM_KEY ||
  695. key.type == BTRFS_METADATA_ITEM_KEY) {
  696. ret = find_inline_backref(eb, path1->slots[0],
  697. &ptr, &end);
  698. if (ret)
  699. goto next;
  700. }
  701. }
  702. if (ptr < end) {
  703. /* update key for inline back ref */
  704. struct btrfs_extent_inline_ref *iref;
  705. iref = (struct btrfs_extent_inline_ref *)ptr;
  706. key.type = btrfs_extent_inline_ref_type(eb, iref);
  707. key.offset = btrfs_extent_inline_ref_offset(eb, iref);
  708. WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
  709. key.type != BTRFS_SHARED_BLOCK_REF_KEY);
  710. }
  711. if (exist &&
  712. ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
  713. exist->owner == key.offset) ||
  714. (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
  715. exist->bytenr == key.offset))) {
  716. exist = NULL;
  717. goto next;
  718. }
  719. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  720. if (key.type == BTRFS_SHARED_BLOCK_REF_KEY ||
  721. key.type == BTRFS_EXTENT_REF_V0_KEY) {
  722. if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
  723. struct btrfs_extent_ref_v0 *ref0;
  724. ref0 = btrfs_item_ptr(eb, path1->slots[0],
  725. struct btrfs_extent_ref_v0);
  726. if (key.objectid == key.offset) {
  727. root = find_tree_root(rc, eb, ref0);
  728. if (root && !should_ignore_root(root))
  729. cur->root = root;
  730. else
  731. list_add(&cur->list, &useless);
  732. break;
  733. }
  734. if (is_cowonly_root(btrfs_ref_root_v0(eb,
  735. ref0)))
  736. cur->cowonly = 1;
  737. }
  738. #else
  739. ASSERT(key.type != BTRFS_EXTENT_REF_V0_KEY);
  740. if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
  741. #endif
  742. if (key.objectid == key.offset) {
  743. /*
  744. * only root blocks of reloc trees use
  745. * backref of this type.
  746. */
  747. root = find_reloc_root(rc, cur->bytenr);
  748. ASSERT(root);
  749. cur->root = root;
  750. break;
  751. }
  752. edge = alloc_backref_edge(cache);
  753. if (!edge) {
  754. err = -ENOMEM;
  755. goto out;
  756. }
  757. rb_node = tree_search(&cache->rb_root, key.offset);
  758. if (!rb_node) {
  759. upper = alloc_backref_node(cache);
  760. if (!upper) {
  761. free_backref_edge(cache, edge);
  762. err = -ENOMEM;
  763. goto out;
  764. }
  765. upper->bytenr = key.offset;
  766. upper->level = cur->level + 1;
  767. /*
  768. * backrefs for the upper level block isn't
  769. * cached, add the block to pending list
  770. */
  771. list_add_tail(&edge->list[UPPER], &list);
  772. } else {
  773. upper = rb_entry(rb_node, struct backref_node,
  774. rb_node);
  775. ASSERT(upper->checked);
  776. INIT_LIST_HEAD(&edge->list[UPPER]);
  777. }
  778. list_add_tail(&edge->list[LOWER], &cur->upper);
  779. edge->node[LOWER] = cur;
  780. edge->node[UPPER] = upper;
  781. goto next;
  782. } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
  783. goto next;
  784. }
  785. /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
  786. root = read_fs_root(rc->extent_root->fs_info, key.offset);
  787. if (IS_ERR(root)) {
  788. err = PTR_ERR(root);
  789. goto out;
  790. }
  791. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  792. cur->cowonly = 1;
  793. if (btrfs_root_level(&root->root_item) == cur->level) {
  794. /* tree root */
  795. ASSERT(btrfs_root_bytenr(&root->root_item) ==
  796. cur->bytenr);
  797. if (should_ignore_root(root))
  798. list_add(&cur->list, &useless);
  799. else
  800. cur->root = root;
  801. break;
  802. }
  803. level = cur->level + 1;
  804. /*
  805. * searching the tree to find upper level blocks
  806. * reference the block.
  807. */
  808. path2->search_commit_root = 1;
  809. path2->skip_locking = 1;
  810. path2->lowest_level = level;
  811. ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
  812. path2->lowest_level = 0;
  813. if (ret < 0) {
  814. err = ret;
  815. goto out;
  816. }
  817. if (ret > 0 && path2->slots[level] > 0)
  818. path2->slots[level]--;
  819. eb = path2->nodes[level];
  820. if (btrfs_node_blockptr(eb, path2->slots[level]) !=
  821. cur->bytenr) {
  822. btrfs_err(root->fs_info,
  823. "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)",
  824. cur->bytenr, level - 1, root->objectid,
  825. node_key->objectid, node_key->type,
  826. node_key->offset);
  827. err = -ENOENT;
  828. goto out;
  829. }
  830. lower = cur;
  831. need_check = true;
  832. for (; level < BTRFS_MAX_LEVEL; level++) {
  833. if (!path2->nodes[level]) {
  834. ASSERT(btrfs_root_bytenr(&root->root_item) ==
  835. lower->bytenr);
  836. if (should_ignore_root(root))
  837. list_add(&lower->list, &useless);
  838. else
  839. lower->root = root;
  840. break;
  841. }
  842. edge = alloc_backref_edge(cache);
  843. if (!edge) {
  844. err = -ENOMEM;
  845. goto out;
  846. }
  847. eb = path2->nodes[level];
  848. rb_node = tree_search(&cache->rb_root, eb->start);
  849. if (!rb_node) {
  850. upper = alloc_backref_node(cache);
  851. if (!upper) {
  852. free_backref_edge(cache, edge);
  853. err = -ENOMEM;
  854. goto out;
  855. }
  856. upper->bytenr = eb->start;
  857. upper->owner = btrfs_header_owner(eb);
  858. upper->level = lower->level + 1;
  859. if (!test_bit(BTRFS_ROOT_REF_COWS,
  860. &root->state))
  861. upper->cowonly = 1;
  862. /*
  863. * if we know the block isn't shared
  864. * we can void checking its backrefs.
  865. */
  866. if (btrfs_block_can_be_shared(root, eb))
  867. upper->checked = 0;
  868. else
  869. upper->checked = 1;
  870. /*
  871. * add the block to pending list if we
  872. * need check its backrefs, we only do this once
  873. * while walking up a tree as we will catch
  874. * anything else later on.
  875. */
  876. if (!upper->checked && need_check) {
  877. need_check = false;
  878. list_add_tail(&edge->list[UPPER],
  879. &list);
  880. } else {
  881. if (upper->checked)
  882. need_check = true;
  883. INIT_LIST_HEAD(&edge->list[UPPER]);
  884. }
  885. } else {
  886. upper = rb_entry(rb_node, struct backref_node,
  887. rb_node);
  888. ASSERT(upper->checked);
  889. INIT_LIST_HEAD(&edge->list[UPPER]);
  890. if (!upper->owner)
  891. upper->owner = btrfs_header_owner(eb);
  892. }
  893. list_add_tail(&edge->list[LOWER], &lower->upper);
  894. edge->node[LOWER] = lower;
  895. edge->node[UPPER] = upper;
  896. if (rb_node)
  897. break;
  898. lower = upper;
  899. upper = NULL;
  900. }
  901. btrfs_release_path(path2);
  902. next:
  903. if (ptr < end) {
  904. ptr += btrfs_extent_inline_ref_size(key.type);
  905. if (ptr >= end) {
  906. WARN_ON(ptr > end);
  907. ptr = 0;
  908. end = 0;
  909. }
  910. }
  911. if (ptr >= end)
  912. path1->slots[0]++;
  913. }
  914. btrfs_release_path(path1);
  915. cur->checked = 1;
  916. WARN_ON(exist);
  917. /* the pending list isn't empty, take the first block to process */
  918. if (!list_empty(&list)) {
  919. edge = list_entry(list.next, struct backref_edge, list[UPPER]);
  920. list_del_init(&edge->list[UPPER]);
  921. cur = edge->node[UPPER];
  922. goto again;
  923. }
  924. /*
  925. * everything goes well, connect backref nodes and insert backref nodes
  926. * into the cache.
  927. */
  928. ASSERT(node->checked);
  929. cowonly = node->cowonly;
  930. if (!cowonly) {
  931. rb_node = tree_insert(&cache->rb_root, node->bytenr,
  932. &node->rb_node);
  933. if (rb_node)
  934. backref_tree_panic(rb_node, -EEXIST, node->bytenr);
  935. list_add_tail(&node->lower, &cache->leaves);
  936. }
  937. list_for_each_entry(edge, &node->upper, list[LOWER])
  938. list_add_tail(&edge->list[UPPER], &list);
  939. while (!list_empty(&list)) {
  940. edge = list_entry(list.next, struct backref_edge, list[UPPER]);
  941. list_del_init(&edge->list[UPPER]);
  942. upper = edge->node[UPPER];
  943. if (upper->detached) {
  944. list_del(&edge->list[LOWER]);
  945. lower = edge->node[LOWER];
  946. free_backref_edge(cache, edge);
  947. if (list_empty(&lower->upper))
  948. list_add(&lower->list, &useless);
  949. continue;
  950. }
  951. if (!RB_EMPTY_NODE(&upper->rb_node)) {
  952. if (upper->lowest) {
  953. list_del_init(&upper->lower);
  954. upper->lowest = 0;
  955. }
  956. list_add_tail(&edge->list[UPPER], &upper->lower);
  957. continue;
  958. }
  959. if (!upper->checked) {
  960. /*
  961. * Still want to blow up for developers since this is a
  962. * logic bug.
  963. */
  964. ASSERT(0);
  965. err = -EINVAL;
  966. goto out;
  967. }
  968. if (cowonly != upper->cowonly) {
  969. ASSERT(0);
  970. err = -EINVAL;
  971. goto out;
  972. }
  973. if (!cowonly) {
  974. rb_node = tree_insert(&cache->rb_root, upper->bytenr,
  975. &upper->rb_node);
  976. if (rb_node)
  977. backref_tree_panic(rb_node, -EEXIST,
  978. upper->bytenr);
  979. }
  980. list_add_tail(&edge->list[UPPER], &upper->lower);
  981. list_for_each_entry(edge, &upper->upper, list[LOWER])
  982. list_add_tail(&edge->list[UPPER], &list);
  983. }
  984. /*
  985. * process useless backref nodes. backref nodes for tree leaves
  986. * are deleted from the cache. backref nodes for upper level
  987. * tree blocks are left in the cache to avoid unnecessary backref
  988. * lookup.
  989. */
  990. while (!list_empty(&useless)) {
  991. upper = list_entry(useless.next, struct backref_node, list);
  992. list_del_init(&upper->list);
  993. ASSERT(list_empty(&upper->upper));
  994. if (upper == node)
  995. node = NULL;
  996. if (upper->lowest) {
  997. list_del_init(&upper->lower);
  998. upper->lowest = 0;
  999. }
  1000. while (!list_empty(&upper->lower)) {
  1001. edge = list_entry(upper->lower.next,
  1002. struct backref_edge, list[UPPER]);
  1003. list_del(&edge->list[UPPER]);
  1004. list_del(&edge->list[LOWER]);
  1005. lower = edge->node[LOWER];
  1006. free_backref_edge(cache, edge);
  1007. if (list_empty(&lower->upper))
  1008. list_add(&lower->list, &useless);
  1009. }
  1010. __mark_block_processed(rc, upper);
  1011. if (upper->level > 0) {
  1012. list_add(&upper->list, &cache->detached);
  1013. upper->detached = 1;
  1014. } else {
  1015. rb_erase(&upper->rb_node, &cache->rb_root);
  1016. free_backref_node(cache, upper);
  1017. }
  1018. }
  1019. out:
  1020. btrfs_free_path(path1);
  1021. btrfs_free_path(path2);
  1022. if (err) {
  1023. while (!list_empty(&useless)) {
  1024. lower = list_entry(useless.next,
  1025. struct backref_node, list);
  1026. list_del_init(&lower->list);
  1027. }
  1028. while (!list_empty(&list)) {
  1029. edge = list_first_entry(&list, struct backref_edge,
  1030. list[UPPER]);
  1031. list_del(&edge->list[UPPER]);
  1032. list_del(&edge->list[LOWER]);
  1033. lower = edge->node[LOWER];
  1034. upper = edge->node[UPPER];
  1035. free_backref_edge(cache, edge);
  1036. /*
  1037. * Lower is no longer linked to any upper backref nodes
  1038. * and isn't in the cache, we can free it ourselves.
  1039. */
  1040. if (list_empty(&lower->upper) &&
  1041. RB_EMPTY_NODE(&lower->rb_node))
  1042. list_add(&lower->list, &useless);
  1043. if (!RB_EMPTY_NODE(&upper->rb_node))
  1044. continue;
  1045. /* Add this guy's upper edges to the list to process */
  1046. list_for_each_entry(edge, &upper->upper, list[LOWER])
  1047. list_add_tail(&edge->list[UPPER], &list);
  1048. if (list_empty(&upper->upper))
  1049. list_add(&upper->list, &useless);
  1050. }
  1051. while (!list_empty(&useless)) {
  1052. lower = list_entry(useless.next,
  1053. struct backref_node, list);
  1054. list_del_init(&lower->list);
  1055. if (lower == node)
  1056. node = NULL;
  1057. free_backref_node(cache, lower);
  1058. }
  1059. free_backref_node(cache, node);
  1060. return ERR_PTR(err);
  1061. }
  1062. ASSERT(!node || !node->detached);
  1063. return node;
  1064. }
  1065. /*
  1066. * helper to add backref node for the newly created snapshot.
  1067. * the backref node is created by cloning backref node that
  1068. * corresponds to root of source tree
  1069. */
  1070. static int clone_backref_node(struct btrfs_trans_handle *trans,
  1071. struct reloc_control *rc,
  1072. struct btrfs_root *src,
  1073. struct btrfs_root *dest)
  1074. {
  1075. struct btrfs_root *reloc_root = src->reloc_root;
  1076. struct backref_cache *cache = &rc->backref_cache;
  1077. struct backref_node *node = NULL;
  1078. struct backref_node *new_node;
  1079. struct backref_edge *edge;
  1080. struct backref_edge *new_edge;
  1081. struct rb_node *rb_node;
  1082. if (cache->last_trans > 0)
  1083. update_backref_cache(trans, cache);
  1084. rb_node = tree_search(&cache->rb_root, src->commit_root->start);
  1085. if (rb_node) {
  1086. node = rb_entry(rb_node, struct backref_node, rb_node);
  1087. if (node->detached)
  1088. node = NULL;
  1089. else
  1090. BUG_ON(node->new_bytenr != reloc_root->node->start);
  1091. }
  1092. if (!node) {
  1093. rb_node = tree_search(&cache->rb_root,
  1094. reloc_root->commit_root->start);
  1095. if (rb_node) {
  1096. node = rb_entry(rb_node, struct backref_node,
  1097. rb_node);
  1098. BUG_ON(node->detached);
  1099. }
  1100. }
  1101. if (!node)
  1102. return 0;
  1103. new_node = alloc_backref_node(cache);
  1104. if (!new_node)
  1105. return -ENOMEM;
  1106. new_node->bytenr = dest->node->start;
  1107. new_node->level = node->level;
  1108. new_node->lowest = node->lowest;
  1109. new_node->checked = 1;
  1110. new_node->root = dest;
  1111. if (!node->lowest) {
  1112. list_for_each_entry(edge, &node->lower, list[UPPER]) {
  1113. new_edge = alloc_backref_edge(cache);
  1114. if (!new_edge)
  1115. goto fail;
  1116. new_edge->node[UPPER] = new_node;
  1117. new_edge->node[LOWER] = edge->node[LOWER];
  1118. list_add_tail(&new_edge->list[UPPER],
  1119. &new_node->lower);
  1120. }
  1121. } else {
  1122. list_add_tail(&new_node->lower, &cache->leaves);
  1123. }
  1124. rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
  1125. &new_node->rb_node);
  1126. if (rb_node)
  1127. backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
  1128. if (!new_node->lowest) {
  1129. list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
  1130. list_add_tail(&new_edge->list[LOWER],
  1131. &new_edge->node[LOWER]->upper);
  1132. }
  1133. }
  1134. return 0;
  1135. fail:
  1136. while (!list_empty(&new_node->lower)) {
  1137. new_edge = list_entry(new_node->lower.next,
  1138. struct backref_edge, list[UPPER]);
  1139. list_del(&new_edge->list[UPPER]);
  1140. free_backref_edge(cache, new_edge);
  1141. }
  1142. free_backref_node(cache, new_node);
  1143. return -ENOMEM;
  1144. }
  1145. /*
  1146. * helper to add 'address of tree root -> reloc tree' mapping
  1147. */
  1148. static int __must_check __add_reloc_root(struct btrfs_root *root)
  1149. {
  1150. struct rb_node *rb_node;
  1151. struct mapping_node *node;
  1152. struct reloc_control *rc = root->fs_info->reloc_ctl;
  1153. node = kmalloc(sizeof(*node), GFP_NOFS);
  1154. if (!node)
  1155. return -ENOMEM;
  1156. node->bytenr = root->node->start;
  1157. node->data = root;
  1158. spin_lock(&rc->reloc_root_tree.lock);
  1159. rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
  1160. node->bytenr, &node->rb_node);
  1161. spin_unlock(&rc->reloc_root_tree.lock);
  1162. if (rb_node) {
  1163. btrfs_panic(root->fs_info, -EEXIST,
  1164. "Duplicate root found for start=%llu while inserting into relocation tree",
  1165. node->bytenr);
  1166. kfree(node);
  1167. return -EEXIST;
  1168. }
  1169. list_add_tail(&root->root_list, &rc->reloc_roots);
  1170. return 0;
  1171. }
  1172. /*
  1173. * helper to delete the 'address of tree root -> reloc tree'
  1174. * mapping
  1175. */
  1176. static void __del_reloc_root(struct btrfs_root *root)
  1177. {
  1178. struct rb_node *rb_node;
  1179. struct mapping_node *node = NULL;
  1180. struct reloc_control *rc = root->fs_info->reloc_ctl;
  1181. if (rc && root->node) {
  1182. spin_lock(&rc->reloc_root_tree.lock);
  1183. rb_node = tree_search(&rc->reloc_root_tree.rb_root,
  1184. root->node->start);
  1185. if (rb_node) {
  1186. node = rb_entry(rb_node, struct mapping_node, rb_node);
  1187. rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
  1188. }
  1189. spin_unlock(&rc->reloc_root_tree.lock);
  1190. if (!node)
  1191. return;
  1192. BUG_ON((struct btrfs_root *)node->data != root);
  1193. }
  1194. spin_lock(&root->fs_info->trans_lock);
  1195. list_del_init(&root->root_list);
  1196. spin_unlock(&root->fs_info->trans_lock);
  1197. kfree(node);
  1198. }
  1199. /*
  1200. * helper to update the 'address of tree root -> reloc tree'
  1201. * mapping
  1202. */
  1203. static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
  1204. {
  1205. struct rb_node *rb_node;
  1206. struct mapping_node *node = NULL;
  1207. struct reloc_control *rc = root->fs_info->reloc_ctl;
  1208. spin_lock(&rc->reloc_root_tree.lock);
  1209. rb_node = tree_search(&rc->reloc_root_tree.rb_root,
  1210. root->node->start);
  1211. if (rb_node) {
  1212. node = rb_entry(rb_node, struct mapping_node, rb_node);
  1213. rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
  1214. }
  1215. spin_unlock(&rc->reloc_root_tree.lock);
  1216. if (!node)
  1217. return 0;
  1218. BUG_ON((struct btrfs_root *)node->data != root);
  1219. spin_lock(&rc->reloc_root_tree.lock);
  1220. node->bytenr = new_bytenr;
  1221. rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
  1222. node->bytenr, &node->rb_node);
  1223. spin_unlock(&rc->reloc_root_tree.lock);
  1224. if (rb_node)
  1225. backref_tree_panic(rb_node, -EEXIST, node->bytenr);
  1226. return 0;
  1227. }
  1228. static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
  1229. struct btrfs_root *root, u64 objectid)
  1230. {
  1231. struct btrfs_root *reloc_root;
  1232. struct extent_buffer *eb;
  1233. struct btrfs_root_item *root_item;
  1234. struct btrfs_key root_key;
  1235. u64 last_snap = 0;
  1236. int ret;
  1237. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  1238. BUG_ON(!root_item);
  1239. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  1240. root_key.type = BTRFS_ROOT_ITEM_KEY;
  1241. root_key.offset = objectid;
  1242. if (root->root_key.objectid == objectid) {
  1243. u64 commit_root_gen;
  1244. /* called by btrfs_init_reloc_root */
  1245. ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
  1246. BTRFS_TREE_RELOC_OBJECTID);
  1247. BUG_ON(ret);
  1248. last_snap = btrfs_root_last_snapshot(&root->root_item);
  1249. /*
  1250. * Set the last_snapshot field to the generation of the commit
  1251. * root - like this ctree.c:btrfs_block_can_be_shared() behaves
  1252. * correctly (returns true) when the relocation root is created
  1253. * either inside the critical section of a transaction commit
  1254. * (through transaction.c:qgroup_account_snapshot()) and when
  1255. * it's created before the transaction commit is started.
  1256. */
  1257. commit_root_gen = btrfs_header_generation(root->commit_root);
  1258. btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
  1259. } else {
  1260. /*
  1261. * called by btrfs_reloc_post_snapshot_hook.
  1262. * the source tree is a reloc tree, all tree blocks
  1263. * modified after it was created have RELOC flag
  1264. * set in their headers. so it's OK to not update
  1265. * the 'last_snapshot'.
  1266. */
  1267. ret = btrfs_copy_root(trans, root, root->node, &eb,
  1268. BTRFS_TREE_RELOC_OBJECTID);
  1269. BUG_ON(ret);
  1270. }
  1271. memcpy(root_item, &root->root_item, sizeof(*root_item));
  1272. btrfs_set_root_bytenr(root_item, eb->start);
  1273. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  1274. btrfs_set_root_generation(root_item, trans->transid);
  1275. if (root->root_key.objectid == objectid) {
  1276. btrfs_set_root_refs(root_item, 0);
  1277. memset(&root_item->drop_progress, 0,
  1278. sizeof(struct btrfs_disk_key));
  1279. root_item->drop_level = 0;
  1280. /*
  1281. * abuse rtransid, it is safe because it is impossible to
  1282. * receive data into a relocation tree.
  1283. */
  1284. btrfs_set_root_rtransid(root_item, last_snap);
  1285. btrfs_set_root_otransid(root_item, trans->transid);
  1286. }
  1287. btrfs_tree_unlock(eb);
  1288. free_extent_buffer(eb);
  1289. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  1290. &root_key, root_item);
  1291. BUG_ON(ret);
  1292. kfree(root_item);
  1293. reloc_root = btrfs_read_fs_root(root->fs_info->tree_root, &root_key);
  1294. BUG_ON(IS_ERR(reloc_root));
  1295. reloc_root->last_trans = trans->transid;
  1296. return reloc_root;
  1297. }
  1298. /*
  1299. * create reloc tree for a given fs tree. reloc tree is just a
  1300. * snapshot of the fs tree with special root objectid.
  1301. */
  1302. int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
  1303. struct btrfs_root *root)
  1304. {
  1305. struct btrfs_root *reloc_root;
  1306. struct reloc_control *rc = root->fs_info->reloc_ctl;
  1307. struct btrfs_block_rsv *rsv;
  1308. int clear_rsv = 0;
  1309. int ret;
  1310. if (root->reloc_root) {
  1311. reloc_root = root->reloc_root;
  1312. reloc_root->last_trans = trans->transid;
  1313. return 0;
  1314. }
  1315. if (!rc || !rc->create_reloc_tree ||
  1316. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  1317. return 0;
  1318. if (!trans->reloc_reserved) {
  1319. rsv = trans->block_rsv;
  1320. trans->block_rsv = rc->block_rsv;
  1321. clear_rsv = 1;
  1322. }
  1323. reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
  1324. if (clear_rsv)
  1325. trans->block_rsv = rsv;
  1326. ret = __add_reloc_root(reloc_root);
  1327. BUG_ON(ret < 0);
  1328. root->reloc_root = reloc_root;
  1329. return 0;
  1330. }
  1331. /*
  1332. * update root item of reloc tree
  1333. */
  1334. int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
  1335. struct btrfs_root *root)
  1336. {
  1337. struct btrfs_root *reloc_root;
  1338. struct btrfs_root_item *root_item;
  1339. int ret;
  1340. if (!root->reloc_root)
  1341. goto out;
  1342. reloc_root = root->reloc_root;
  1343. root_item = &reloc_root->root_item;
  1344. if (root->fs_info->reloc_ctl->merge_reloc_tree &&
  1345. btrfs_root_refs(root_item) == 0) {
  1346. root->reloc_root = NULL;
  1347. __del_reloc_root(reloc_root);
  1348. }
  1349. if (reloc_root->commit_root != reloc_root->node) {
  1350. btrfs_set_root_node(root_item, reloc_root->node);
  1351. free_extent_buffer(reloc_root->commit_root);
  1352. reloc_root->commit_root = btrfs_root_node(reloc_root);
  1353. }
  1354. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1355. &reloc_root->root_key, root_item);
  1356. BUG_ON(ret);
  1357. out:
  1358. return 0;
  1359. }
  1360. /*
  1361. * helper to find first cached inode with inode number >= objectid
  1362. * in a subvolume
  1363. */
  1364. static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
  1365. {
  1366. struct rb_node *node;
  1367. struct rb_node *prev;
  1368. struct btrfs_inode *entry;
  1369. struct inode *inode;
  1370. spin_lock(&root->inode_lock);
  1371. again:
  1372. node = root->inode_tree.rb_node;
  1373. prev = NULL;
  1374. while (node) {
  1375. prev = node;
  1376. entry = rb_entry(node, struct btrfs_inode, rb_node);
  1377. if (objectid < btrfs_ino(&entry->vfs_inode))
  1378. node = node->rb_left;
  1379. else if (objectid > btrfs_ino(&entry->vfs_inode))
  1380. node = node->rb_right;
  1381. else
  1382. break;
  1383. }
  1384. if (!node) {
  1385. while (prev) {
  1386. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  1387. if (objectid <= btrfs_ino(&entry->vfs_inode)) {
  1388. node = prev;
  1389. break;
  1390. }
  1391. prev = rb_next(prev);
  1392. }
  1393. }
  1394. while (node) {
  1395. entry = rb_entry(node, struct btrfs_inode, rb_node);
  1396. inode = igrab(&entry->vfs_inode);
  1397. if (inode) {
  1398. spin_unlock(&root->inode_lock);
  1399. return inode;
  1400. }
  1401. objectid = btrfs_ino(&entry->vfs_inode) + 1;
  1402. if (cond_resched_lock(&root->inode_lock))
  1403. goto again;
  1404. node = rb_next(node);
  1405. }
  1406. spin_unlock(&root->inode_lock);
  1407. return NULL;
  1408. }
  1409. static int in_block_group(u64 bytenr,
  1410. struct btrfs_block_group_cache *block_group)
  1411. {
  1412. if (bytenr >= block_group->key.objectid &&
  1413. bytenr < block_group->key.objectid + block_group->key.offset)
  1414. return 1;
  1415. return 0;
  1416. }
  1417. /*
  1418. * get new location of data
  1419. */
  1420. static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
  1421. u64 bytenr, u64 num_bytes)
  1422. {
  1423. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  1424. struct btrfs_path *path;
  1425. struct btrfs_file_extent_item *fi;
  1426. struct extent_buffer *leaf;
  1427. int ret;
  1428. path = btrfs_alloc_path();
  1429. if (!path)
  1430. return -ENOMEM;
  1431. bytenr -= BTRFS_I(reloc_inode)->index_cnt;
  1432. ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(reloc_inode),
  1433. bytenr, 0);
  1434. if (ret < 0)
  1435. goto out;
  1436. if (ret > 0) {
  1437. ret = -ENOENT;
  1438. goto out;
  1439. }
  1440. leaf = path->nodes[0];
  1441. fi = btrfs_item_ptr(leaf, path->slots[0],
  1442. struct btrfs_file_extent_item);
  1443. BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
  1444. btrfs_file_extent_compression(leaf, fi) ||
  1445. btrfs_file_extent_encryption(leaf, fi) ||
  1446. btrfs_file_extent_other_encoding(leaf, fi));
  1447. if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
  1448. ret = -EINVAL;
  1449. goto out;
  1450. }
  1451. *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1452. ret = 0;
  1453. out:
  1454. btrfs_free_path(path);
  1455. return ret;
  1456. }
  1457. /*
  1458. * update file extent items in the tree leaf to point to
  1459. * the new locations.
  1460. */
  1461. static noinline_for_stack
  1462. int replace_file_extents(struct btrfs_trans_handle *trans,
  1463. struct reloc_control *rc,
  1464. struct btrfs_root *root,
  1465. struct extent_buffer *leaf)
  1466. {
  1467. struct btrfs_key key;
  1468. struct btrfs_file_extent_item *fi;
  1469. struct inode *inode = NULL;
  1470. u64 parent;
  1471. u64 bytenr;
  1472. u64 new_bytenr = 0;
  1473. u64 num_bytes;
  1474. u64 end;
  1475. u32 nritems;
  1476. u32 i;
  1477. int ret = 0;
  1478. int first = 1;
  1479. int dirty = 0;
  1480. if (rc->stage != UPDATE_DATA_PTRS)
  1481. return 0;
  1482. /* reloc trees always use full backref */
  1483. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  1484. parent = leaf->start;
  1485. else
  1486. parent = 0;
  1487. nritems = btrfs_header_nritems(leaf);
  1488. for (i = 0; i < nritems; i++) {
  1489. cond_resched();
  1490. btrfs_item_key_to_cpu(leaf, &key, i);
  1491. if (key.type != BTRFS_EXTENT_DATA_KEY)
  1492. continue;
  1493. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  1494. if (btrfs_file_extent_type(leaf, fi) ==
  1495. BTRFS_FILE_EXTENT_INLINE)
  1496. continue;
  1497. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1498. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  1499. if (bytenr == 0)
  1500. continue;
  1501. if (!in_block_group(bytenr, rc->block_group))
  1502. continue;
  1503. /*
  1504. * if we are modifying block in fs tree, wait for readpage
  1505. * to complete and drop the extent cache
  1506. */
  1507. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  1508. if (first) {
  1509. inode = find_next_inode(root, key.objectid);
  1510. first = 0;
  1511. } else if (inode && btrfs_ino(inode) < key.objectid) {
  1512. btrfs_add_delayed_iput(inode);
  1513. inode = find_next_inode(root, key.objectid);
  1514. }
  1515. if (inode && btrfs_ino(inode) == key.objectid) {
  1516. end = key.offset +
  1517. btrfs_file_extent_num_bytes(leaf, fi);
  1518. WARN_ON(!IS_ALIGNED(key.offset,
  1519. root->sectorsize));
  1520. WARN_ON(!IS_ALIGNED(end, root->sectorsize));
  1521. end--;
  1522. ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
  1523. key.offset, end);
  1524. if (!ret)
  1525. continue;
  1526. btrfs_drop_extent_cache(inode, key.offset, end,
  1527. 1);
  1528. unlock_extent(&BTRFS_I(inode)->io_tree,
  1529. key.offset, end);
  1530. }
  1531. }
  1532. ret = get_new_location(rc->data_inode, &new_bytenr,
  1533. bytenr, num_bytes);
  1534. if (ret) {
  1535. /*
  1536. * Don't have to abort since we've not changed anything
  1537. * in the file extent yet.
  1538. */
  1539. break;
  1540. }
  1541. btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
  1542. dirty = 1;
  1543. key.offset -= btrfs_file_extent_offset(leaf, fi);
  1544. ret = btrfs_inc_extent_ref(trans, root, new_bytenr,
  1545. num_bytes, parent,
  1546. btrfs_header_owner(leaf),
  1547. key.objectid, key.offset);
  1548. if (ret) {
  1549. btrfs_abort_transaction(trans, ret);
  1550. break;
  1551. }
  1552. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  1553. parent, btrfs_header_owner(leaf),
  1554. key.objectid, key.offset);
  1555. if (ret) {
  1556. btrfs_abort_transaction(trans, ret);
  1557. break;
  1558. }
  1559. }
  1560. if (dirty)
  1561. btrfs_mark_buffer_dirty(leaf);
  1562. if (inode)
  1563. btrfs_add_delayed_iput(inode);
  1564. return ret;
  1565. }
  1566. static noinline_for_stack
  1567. int memcmp_node_keys(struct extent_buffer *eb, int slot,
  1568. struct btrfs_path *path, int level)
  1569. {
  1570. struct btrfs_disk_key key1;
  1571. struct btrfs_disk_key key2;
  1572. btrfs_node_key(eb, &key1, slot);
  1573. btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
  1574. return memcmp(&key1, &key2, sizeof(key1));
  1575. }
  1576. /*
  1577. * try to replace tree blocks in fs tree with the new blocks
  1578. * in reloc tree. tree blocks haven't been modified since the
  1579. * reloc tree was create can be replaced.
  1580. *
  1581. * if a block was replaced, level of the block + 1 is returned.
  1582. * if no block got replaced, 0 is returned. if there are other
  1583. * errors, a negative error number is returned.
  1584. */
  1585. static noinline_for_stack
  1586. int replace_path(struct btrfs_trans_handle *trans,
  1587. struct btrfs_root *dest, struct btrfs_root *src,
  1588. struct btrfs_path *path, struct btrfs_key *next_key,
  1589. int lowest_level, int max_level)
  1590. {
  1591. struct extent_buffer *eb;
  1592. struct extent_buffer *parent;
  1593. struct btrfs_key key;
  1594. u64 old_bytenr;
  1595. u64 new_bytenr;
  1596. u64 old_ptr_gen;
  1597. u64 new_ptr_gen;
  1598. u64 last_snapshot;
  1599. u32 blocksize;
  1600. int cow = 0;
  1601. int level;
  1602. int ret;
  1603. int slot;
  1604. BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  1605. BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
  1606. last_snapshot = btrfs_root_last_snapshot(&src->root_item);
  1607. again:
  1608. slot = path->slots[lowest_level];
  1609. btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
  1610. eb = btrfs_lock_root_node(dest);
  1611. btrfs_set_lock_blocking(eb);
  1612. level = btrfs_header_level(eb);
  1613. if (level < lowest_level) {
  1614. btrfs_tree_unlock(eb);
  1615. free_extent_buffer(eb);
  1616. return 0;
  1617. }
  1618. if (cow) {
  1619. ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
  1620. BUG_ON(ret);
  1621. }
  1622. btrfs_set_lock_blocking(eb);
  1623. if (next_key) {
  1624. next_key->objectid = (u64)-1;
  1625. next_key->type = (u8)-1;
  1626. next_key->offset = (u64)-1;
  1627. }
  1628. parent = eb;
  1629. while (1) {
  1630. level = btrfs_header_level(parent);
  1631. BUG_ON(level < lowest_level);
  1632. ret = btrfs_bin_search(parent, &key, level, &slot);
  1633. if (ret && slot > 0)
  1634. slot--;
  1635. if (next_key && slot + 1 < btrfs_header_nritems(parent))
  1636. btrfs_node_key_to_cpu(parent, next_key, slot + 1);
  1637. old_bytenr = btrfs_node_blockptr(parent, slot);
  1638. blocksize = dest->nodesize;
  1639. old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
  1640. if (level <= max_level) {
  1641. eb = path->nodes[level];
  1642. new_bytenr = btrfs_node_blockptr(eb,
  1643. path->slots[level]);
  1644. new_ptr_gen = btrfs_node_ptr_generation(eb,
  1645. path->slots[level]);
  1646. } else {
  1647. new_bytenr = 0;
  1648. new_ptr_gen = 0;
  1649. }
  1650. if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
  1651. ret = level;
  1652. break;
  1653. }
  1654. if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
  1655. memcmp_node_keys(parent, slot, path, level)) {
  1656. if (level <= lowest_level) {
  1657. ret = 0;
  1658. break;
  1659. }
  1660. eb = read_tree_block(dest, old_bytenr, old_ptr_gen);
  1661. if (IS_ERR(eb)) {
  1662. ret = PTR_ERR(eb);
  1663. break;
  1664. } else if (!extent_buffer_uptodate(eb)) {
  1665. ret = -EIO;
  1666. free_extent_buffer(eb);
  1667. break;
  1668. }
  1669. btrfs_tree_lock(eb);
  1670. if (cow) {
  1671. ret = btrfs_cow_block(trans, dest, eb, parent,
  1672. slot, &eb);
  1673. BUG_ON(ret);
  1674. }
  1675. btrfs_set_lock_blocking(eb);
  1676. btrfs_tree_unlock(parent);
  1677. free_extent_buffer(parent);
  1678. parent = eb;
  1679. continue;
  1680. }
  1681. if (!cow) {
  1682. btrfs_tree_unlock(parent);
  1683. free_extent_buffer(parent);
  1684. cow = 1;
  1685. goto again;
  1686. }
  1687. btrfs_node_key_to_cpu(path->nodes[level], &key,
  1688. path->slots[level]);
  1689. btrfs_release_path(path);
  1690. path->lowest_level = level;
  1691. ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
  1692. path->lowest_level = 0;
  1693. BUG_ON(ret);
  1694. /*
  1695. * swap blocks in fs tree and reloc tree.
  1696. */
  1697. btrfs_set_node_blockptr(parent, slot, new_bytenr);
  1698. btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
  1699. btrfs_mark_buffer_dirty(parent);
  1700. btrfs_set_node_blockptr(path->nodes[level],
  1701. path->slots[level], old_bytenr);
  1702. btrfs_set_node_ptr_generation(path->nodes[level],
  1703. path->slots[level], old_ptr_gen);
  1704. btrfs_mark_buffer_dirty(path->nodes[level]);
  1705. ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize,
  1706. path->nodes[level]->start,
  1707. src->root_key.objectid, level - 1, 0);
  1708. BUG_ON(ret);
  1709. ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize,
  1710. 0, dest->root_key.objectid, level - 1,
  1711. 0);
  1712. BUG_ON(ret);
  1713. ret = btrfs_free_extent(trans, src, new_bytenr, blocksize,
  1714. path->nodes[level]->start,
  1715. src->root_key.objectid, level - 1, 0);
  1716. BUG_ON(ret);
  1717. ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize,
  1718. 0, dest->root_key.objectid, level - 1,
  1719. 0);
  1720. BUG_ON(ret);
  1721. btrfs_unlock_up_safe(path, 0);
  1722. ret = level;
  1723. break;
  1724. }
  1725. btrfs_tree_unlock(parent);
  1726. free_extent_buffer(parent);
  1727. return ret;
  1728. }
  1729. /*
  1730. * helper to find next relocated block in reloc tree
  1731. */
  1732. static noinline_for_stack
  1733. int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
  1734. int *level)
  1735. {
  1736. struct extent_buffer *eb;
  1737. int i;
  1738. u64 last_snapshot;
  1739. u32 nritems;
  1740. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1741. for (i = 0; i < *level; i++) {
  1742. free_extent_buffer(path->nodes[i]);
  1743. path->nodes[i] = NULL;
  1744. }
  1745. for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
  1746. eb = path->nodes[i];
  1747. nritems = btrfs_header_nritems(eb);
  1748. while (path->slots[i] + 1 < nritems) {
  1749. path->slots[i]++;
  1750. if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
  1751. last_snapshot)
  1752. continue;
  1753. *level = i;
  1754. return 0;
  1755. }
  1756. free_extent_buffer(path->nodes[i]);
  1757. path->nodes[i] = NULL;
  1758. }
  1759. return 1;
  1760. }
  1761. /*
  1762. * walk down reloc tree to find relocated block of lowest level
  1763. */
  1764. static noinline_for_stack
  1765. int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
  1766. int *level)
  1767. {
  1768. struct extent_buffer *eb = NULL;
  1769. int i;
  1770. u64 bytenr;
  1771. u64 ptr_gen = 0;
  1772. u64 last_snapshot;
  1773. u32 nritems;
  1774. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1775. for (i = *level; i > 0; i--) {
  1776. eb = path->nodes[i];
  1777. nritems = btrfs_header_nritems(eb);
  1778. while (path->slots[i] < nritems) {
  1779. ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
  1780. if (ptr_gen > last_snapshot)
  1781. break;
  1782. path->slots[i]++;
  1783. }
  1784. if (path->slots[i] >= nritems) {
  1785. if (i == *level)
  1786. break;
  1787. *level = i + 1;
  1788. return 0;
  1789. }
  1790. if (i == 1) {
  1791. *level = i;
  1792. return 0;
  1793. }
  1794. bytenr = btrfs_node_blockptr(eb, path->slots[i]);
  1795. eb = read_tree_block(root, bytenr, ptr_gen);
  1796. if (IS_ERR(eb)) {
  1797. return PTR_ERR(eb);
  1798. } else if (!extent_buffer_uptodate(eb)) {
  1799. free_extent_buffer(eb);
  1800. return -EIO;
  1801. }
  1802. BUG_ON(btrfs_header_level(eb) != i - 1);
  1803. path->nodes[i - 1] = eb;
  1804. path->slots[i - 1] = 0;
  1805. }
  1806. return 1;
  1807. }
  1808. /*
  1809. * invalidate extent cache for file extents whose key in range of
  1810. * [min_key, max_key)
  1811. */
  1812. static int invalidate_extent_cache(struct btrfs_root *root,
  1813. struct btrfs_key *min_key,
  1814. struct btrfs_key *max_key)
  1815. {
  1816. struct inode *inode = NULL;
  1817. u64 objectid;
  1818. u64 start, end;
  1819. u64 ino;
  1820. objectid = min_key->objectid;
  1821. while (1) {
  1822. cond_resched();
  1823. iput(inode);
  1824. if (objectid > max_key->objectid)
  1825. break;
  1826. inode = find_next_inode(root, objectid);
  1827. if (!inode)
  1828. break;
  1829. ino = btrfs_ino(inode);
  1830. if (ino > max_key->objectid) {
  1831. iput(inode);
  1832. break;
  1833. }
  1834. objectid = ino + 1;
  1835. if (!S_ISREG(inode->i_mode))
  1836. continue;
  1837. if (unlikely(min_key->objectid == ino)) {
  1838. if (min_key->type > BTRFS_EXTENT_DATA_KEY)
  1839. continue;
  1840. if (min_key->type < BTRFS_EXTENT_DATA_KEY)
  1841. start = 0;
  1842. else {
  1843. start = min_key->offset;
  1844. WARN_ON(!IS_ALIGNED(start, root->sectorsize));
  1845. }
  1846. } else {
  1847. start = 0;
  1848. }
  1849. if (unlikely(max_key->objectid == ino)) {
  1850. if (max_key->type < BTRFS_EXTENT_DATA_KEY)
  1851. continue;
  1852. if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
  1853. end = (u64)-1;
  1854. } else {
  1855. if (max_key->offset == 0)
  1856. continue;
  1857. end = max_key->offset;
  1858. WARN_ON(!IS_ALIGNED(end, root->sectorsize));
  1859. end--;
  1860. }
  1861. } else {
  1862. end = (u64)-1;
  1863. }
  1864. /* the lock_extent waits for readpage to complete */
  1865. lock_extent(&BTRFS_I(inode)->io_tree, start, end);
  1866. btrfs_drop_extent_cache(inode, start, end, 1);
  1867. unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
  1868. }
  1869. return 0;
  1870. }
  1871. static int find_next_key(struct btrfs_path *path, int level,
  1872. struct btrfs_key *key)
  1873. {
  1874. while (level < BTRFS_MAX_LEVEL) {
  1875. if (!path->nodes[level])
  1876. break;
  1877. if (path->slots[level] + 1 <
  1878. btrfs_header_nritems(path->nodes[level])) {
  1879. btrfs_node_key_to_cpu(path->nodes[level], key,
  1880. path->slots[level] + 1);
  1881. return 0;
  1882. }
  1883. level++;
  1884. }
  1885. return 1;
  1886. }
  1887. /*
  1888. * merge the relocated tree blocks in reloc tree with corresponding
  1889. * fs tree.
  1890. */
  1891. static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
  1892. struct btrfs_root *root)
  1893. {
  1894. LIST_HEAD(inode_list);
  1895. struct btrfs_key key;
  1896. struct btrfs_key next_key;
  1897. struct btrfs_trans_handle *trans = NULL;
  1898. struct btrfs_root *reloc_root;
  1899. struct btrfs_root_item *root_item;
  1900. struct btrfs_path *path;
  1901. struct extent_buffer *leaf;
  1902. int level;
  1903. int max_level;
  1904. int replaced = 0;
  1905. int ret;
  1906. int err = 0;
  1907. u32 min_reserved;
  1908. path = btrfs_alloc_path();
  1909. if (!path)
  1910. return -ENOMEM;
  1911. path->reada = READA_FORWARD;
  1912. reloc_root = root->reloc_root;
  1913. root_item = &reloc_root->root_item;
  1914. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  1915. level = btrfs_root_level(root_item);
  1916. extent_buffer_get(reloc_root->node);
  1917. path->nodes[level] = reloc_root->node;
  1918. path->slots[level] = 0;
  1919. } else {
  1920. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  1921. level = root_item->drop_level;
  1922. BUG_ON(level == 0);
  1923. path->lowest_level = level;
  1924. ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
  1925. path->lowest_level = 0;
  1926. if (ret < 0) {
  1927. btrfs_free_path(path);
  1928. return ret;
  1929. }
  1930. btrfs_node_key_to_cpu(path->nodes[level], &next_key,
  1931. path->slots[level]);
  1932. WARN_ON(memcmp(&key, &next_key, sizeof(key)));
  1933. btrfs_unlock_up_safe(path, 0);
  1934. }
  1935. min_reserved = root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
  1936. memset(&next_key, 0, sizeof(next_key));
  1937. while (1) {
  1938. ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
  1939. BTRFS_RESERVE_FLUSH_ALL);
  1940. if (ret) {
  1941. err = ret;
  1942. goto out;
  1943. }
  1944. trans = btrfs_start_transaction(root, 0);
  1945. if (IS_ERR(trans)) {
  1946. err = PTR_ERR(trans);
  1947. trans = NULL;
  1948. goto out;
  1949. }
  1950. trans->block_rsv = rc->block_rsv;
  1951. replaced = 0;
  1952. max_level = level;
  1953. ret = walk_down_reloc_tree(reloc_root, path, &level);
  1954. if (ret < 0) {
  1955. err = ret;
  1956. goto out;
  1957. }
  1958. if (ret > 0)
  1959. break;
  1960. if (!find_next_key(path, level, &key) &&
  1961. btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
  1962. ret = 0;
  1963. } else {
  1964. ret = replace_path(trans, root, reloc_root, path,
  1965. &next_key, level, max_level);
  1966. }
  1967. if (ret < 0) {
  1968. err = ret;
  1969. goto out;
  1970. }
  1971. if (ret > 0) {
  1972. level = ret;
  1973. btrfs_node_key_to_cpu(path->nodes[level], &key,
  1974. path->slots[level]);
  1975. replaced = 1;
  1976. }
  1977. ret = walk_up_reloc_tree(reloc_root, path, &level);
  1978. if (ret > 0)
  1979. break;
  1980. BUG_ON(level == 0);
  1981. /*
  1982. * save the merging progress in the drop_progress.
  1983. * this is OK since root refs == 1 in this case.
  1984. */
  1985. btrfs_node_key(path->nodes[level], &root_item->drop_progress,
  1986. path->slots[level]);
  1987. root_item->drop_level = level;
  1988. btrfs_end_transaction_throttle(trans, root);
  1989. trans = NULL;
  1990. btrfs_btree_balance_dirty(root);
  1991. if (replaced && rc->stage == UPDATE_DATA_PTRS)
  1992. invalidate_extent_cache(root, &key, &next_key);
  1993. }
  1994. /*
  1995. * handle the case only one block in the fs tree need to be
  1996. * relocated and the block is tree root.
  1997. */
  1998. leaf = btrfs_lock_root_node(root);
  1999. ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
  2000. btrfs_tree_unlock(leaf);
  2001. free_extent_buffer(leaf);
  2002. if (ret < 0)
  2003. err = ret;
  2004. out:
  2005. btrfs_free_path(path);
  2006. if (err == 0) {
  2007. memset(&root_item->drop_progress, 0,
  2008. sizeof(root_item->drop_progress));
  2009. root_item->drop_level = 0;
  2010. btrfs_set_root_refs(root_item, 0);
  2011. btrfs_update_reloc_root(trans, root);
  2012. }
  2013. if (trans)
  2014. btrfs_end_transaction_throttle(trans, root);
  2015. btrfs_btree_balance_dirty(root);
  2016. if (replaced && rc->stage == UPDATE_DATA_PTRS)
  2017. invalidate_extent_cache(root, &key, &next_key);
  2018. return err;
  2019. }
  2020. static noinline_for_stack
  2021. int prepare_to_merge(struct reloc_control *rc, int err)
  2022. {
  2023. struct btrfs_root *root = rc->extent_root;
  2024. struct btrfs_root *reloc_root;
  2025. struct btrfs_trans_handle *trans;
  2026. LIST_HEAD(reloc_roots);
  2027. u64 num_bytes = 0;
  2028. int ret;
  2029. mutex_lock(&root->fs_info->reloc_mutex);
  2030. rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
  2031. rc->merging_rsv_size += rc->nodes_relocated * 2;
  2032. mutex_unlock(&root->fs_info->reloc_mutex);
  2033. again:
  2034. if (!err) {
  2035. num_bytes = rc->merging_rsv_size;
  2036. ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
  2037. BTRFS_RESERVE_FLUSH_ALL);
  2038. if (ret)
  2039. err = ret;
  2040. }
  2041. trans = btrfs_join_transaction(rc->extent_root);
  2042. if (IS_ERR(trans)) {
  2043. if (!err)
  2044. btrfs_block_rsv_release(rc->extent_root,
  2045. rc->block_rsv, num_bytes);
  2046. return PTR_ERR(trans);
  2047. }
  2048. if (!err) {
  2049. if (num_bytes != rc->merging_rsv_size) {
  2050. btrfs_end_transaction(trans, rc->extent_root);
  2051. btrfs_block_rsv_release(rc->extent_root,
  2052. rc->block_rsv, num_bytes);
  2053. goto again;
  2054. }
  2055. }
  2056. rc->merge_reloc_tree = 1;
  2057. while (!list_empty(&rc->reloc_roots)) {
  2058. reloc_root = list_entry(rc->reloc_roots.next,
  2059. struct btrfs_root, root_list);
  2060. list_del_init(&reloc_root->root_list);
  2061. root = read_fs_root(reloc_root->fs_info,
  2062. reloc_root->root_key.offset);
  2063. BUG_ON(IS_ERR(root));
  2064. BUG_ON(root->reloc_root != reloc_root);
  2065. /*
  2066. * set reference count to 1, so btrfs_recover_relocation
  2067. * knows it should resumes merging
  2068. */
  2069. if (!err)
  2070. btrfs_set_root_refs(&reloc_root->root_item, 1);
  2071. btrfs_update_reloc_root(trans, root);
  2072. list_add(&reloc_root->root_list, &reloc_roots);
  2073. }
  2074. list_splice(&reloc_roots, &rc->reloc_roots);
  2075. if (!err)
  2076. btrfs_commit_transaction(trans, rc->extent_root);
  2077. else
  2078. btrfs_end_transaction(trans, rc->extent_root);
  2079. return err;
  2080. }
  2081. static noinline_for_stack
  2082. void free_reloc_roots(struct list_head *list)
  2083. {
  2084. struct btrfs_root *reloc_root;
  2085. while (!list_empty(list)) {
  2086. reloc_root = list_entry(list->next, struct btrfs_root,
  2087. root_list);
  2088. __del_reloc_root(reloc_root);
  2089. free_extent_buffer(reloc_root->node);
  2090. free_extent_buffer(reloc_root->commit_root);
  2091. reloc_root->node = NULL;
  2092. reloc_root->commit_root = NULL;
  2093. }
  2094. }
  2095. static noinline_for_stack
  2096. void merge_reloc_roots(struct reloc_control *rc)
  2097. {
  2098. struct btrfs_root *root;
  2099. struct btrfs_root *reloc_root;
  2100. u64 last_snap;
  2101. u64 otransid;
  2102. u64 objectid;
  2103. LIST_HEAD(reloc_roots);
  2104. int found = 0;
  2105. int ret = 0;
  2106. again:
  2107. root = rc->extent_root;
  2108. /*
  2109. * this serializes us with btrfs_record_root_in_transaction,
  2110. * we have to make sure nobody is in the middle of
  2111. * adding their roots to the list while we are
  2112. * doing this splice
  2113. */
  2114. mutex_lock(&root->fs_info->reloc_mutex);
  2115. list_splice_init(&rc->reloc_roots, &reloc_roots);
  2116. mutex_unlock(&root->fs_info->reloc_mutex);
  2117. while (!list_empty(&reloc_roots)) {
  2118. found = 1;
  2119. reloc_root = list_entry(reloc_roots.next,
  2120. struct btrfs_root, root_list);
  2121. if (btrfs_root_refs(&reloc_root->root_item) > 0) {
  2122. root = read_fs_root(reloc_root->fs_info,
  2123. reloc_root->root_key.offset);
  2124. BUG_ON(IS_ERR(root));
  2125. BUG_ON(root->reloc_root != reloc_root);
  2126. ret = merge_reloc_root(rc, root);
  2127. if (ret) {
  2128. if (list_empty(&reloc_root->root_list))
  2129. list_add_tail(&reloc_root->root_list,
  2130. &reloc_roots);
  2131. goto out;
  2132. }
  2133. } else {
  2134. list_del_init(&reloc_root->root_list);
  2135. }
  2136. /*
  2137. * we keep the old last snapshot transid in rtranid when we
  2138. * created the relocation tree.
  2139. */
  2140. last_snap = btrfs_root_rtransid(&reloc_root->root_item);
  2141. otransid = btrfs_root_otransid(&reloc_root->root_item);
  2142. objectid = reloc_root->root_key.offset;
  2143. ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1);
  2144. if (ret < 0) {
  2145. if (list_empty(&reloc_root->root_list))
  2146. list_add_tail(&reloc_root->root_list,
  2147. &reloc_roots);
  2148. goto out;
  2149. }
  2150. }
  2151. if (found) {
  2152. found = 0;
  2153. goto again;
  2154. }
  2155. out:
  2156. if (ret) {
  2157. btrfs_handle_fs_error(root->fs_info, ret, NULL);
  2158. if (!list_empty(&reloc_roots))
  2159. free_reloc_roots(&reloc_roots);
  2160. /* new reloc root may be added */
  2161. mutex_lock(&root->fs_info->reloc_mutex);
  2162. list_splice_init(&rc->reloc_roots, &reloc_roots);
  2163. mutex_unlock(&root->fs_info->reloc_mutex);
  2164. if (!list_empty(&reloc_roots))
  2165. free_reloc_roots(&reloc_roots);
  2166. }
  2167. BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
  2168. }
  2169. static void free_block_list(struct rb_root *blocks)
  2170. {
  2171. struct tree_block *block;
  2172. struct rb_node *rb_node;
  2173. while ((rb_node = rb_first(blocks))) {
  2174. block = rb_entry(rb_node, struct tree_block, rb_node);
  2175. rb_erase(rb_node, blocks);
  2176. kfree(block);
  2177. }
  2178. }
  2179. static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
  2180. struct btrfs_root *reloc_root)
  2181. {
  2182. struct btrfs_root *root;
  2183. if (reloc_root->last_trans == trans->transid)
  2184. return 0;
  2185. root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
  2186. BUG_ON(IS_ERR(root));
  2187. BUG_ON(root->reloc_root != reloc_root);
  2188. return btrfs_record_root_in_trans(trans, root);
  2189. }
  2190. static noinline_for_stack
  2191. struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
  2192. struct reloc_control *rc,
  2193. struct backref_node *node,
  2194. struct backref_edge *edges[])
  2195. {
  2196. struct backref_node *next;
  2197. struct btrfs_root *root;
  2198. int index = 0;
  2199. next = node;
  2200. while (1) {
  2201. cond_resched();
  2202. next = walk_up_backref(next, edges, &index);
  2203. root = next->root;
  2204. BUG_ON(!root);
  2205. BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state));
  2206. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
  2207. record_reloc_root_in_trans(trans, root);
  2208. break;
  2209. }
  2210. btrfs_record_root_in_trans(trans, root);
  2211. root = root->reloc_root;
  2212. if (next->new_bytenr != root->node->start) {
  2213. BUG_ON(next->new_bytenr);
  2214. BUG_ON(!list_empty(&next->list));
  2215. next->new_bytenr = root->node->start;
  2216. next->root = root;
  2217. list_add_tail(&next->list,
  2218. &rc->backref_cache.changed);
  2219. __mark_block_processed(rc, next);
  2220. break;
  2221. }
  2222. WARN_ON(1);
  2223. root = NULL;
  2224. next = walk_down_backref(edges, &index);
  2225. if (!next || next->level <= node->level)
  2226. break;
  2227. }
  2228. if (!root)
  2229. return NULL;
  2230. next = node;
  2231. /* setup backref node path for btrfs_reloc_cow_block */
  2232. while (1) {
  2233. rc->backref_cache.path[next->level] = next;
  2234. if (--index < 0)
  2235. break;
  2236. next = edges[index]->node[UPPER];
  2237. }
  2238. return root;
  2239. }
  2240. /*
  2241. * select a tree root for relocation. return NULL if the block
  2242. * is reference counted. we should use do_relocation() in this
  2243. * case. return a tree root pointer if the block isn't reference
  2244. * counted. return -ENOENT if the block is root of reloc tree.
  2245. */
  2246. static noinline_for_stack
  2247. struct btrfs_root *select_one_root(struct backref_node *node)
  2248. {
  2249. struct backref_node *next;
  2250. struct btrfs_root *root;
  2251. struct btrfs_root *fs_root = NULL;
  2252. struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  2253. int index = 0;
  2254. next = node;
  2255. while (1) {
  2256. cond_resched();
  2257. next = walk_up_backref(next, edges, &index);
  2258. root = next->root;
  2259. BUG_ON(!root);
  2260. /* no other choice for non-references counted tree */
  2261. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  2262. return root;
  2263. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
  2264. fs_root = root;
  2265. if (next != node)
  2266. return NULL;
  2267. next = walk_down_backref(edges, &index);
  2268. if (!next || next->level <= node->level)
  2269. break;
  2270. }
  2271. if (!fs_root)
  2272. return ERR_PTR(-ENOENT);
  2273. return fs_root;
  2274. }
  2275. static noinline_for_stack
  2276. u64 calcu_metadata_size(struct reloc_control *rc,
  2277. struct backref_node *node, int reserve)
  2278. {
  2279. struct backref_node *next = node;
  2280. struct backref_edge *edge;
  2281. struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  2282. u64 num_bytes = 0;
  2283. int index = 0;
  2284. BUG_ON(reserve && node->processed);
  2285. while (next) {
  2286. cond_resched();
  2287. while (1) {
  2288. if (next->processed && (reserve || next != node))
  2289. break;
  2290. num_bytes += rc->extent_root->nodesize;
  2291. if (list_empty(&next->upper))
  2292. break;
  2293. edge = list_entry(next->upper.next,
  2294. struct backref_edge, list[LOWER]);
  2295. edges[index++] = edge;
  2296. next = edge->node[UPPER];
  2297. }
  2298. next = walk_down_backref(edges, &index);
  2299. }
  2300. return num_bytes;
  2301. }
  2302. static int reserve_metadata_space(struct btrfs_trans_handle *trans,
  2303. struct reloc_control *rc,
  2304. struct backref_node *node)
  2305. {
  2306. struct btrfs_root *root = rc->extent_root;
  2307. u64 num_bytes;
  2308. int ret;
  2309. u64 tmp;
  2310. num_bytes = calcu_metadata_size(rc, node, 1) * 2;
  2311. trans->block_rsv = rc->block_rsv;
  2312. rc->reserved_bytes += num_bytes;
  2313. /*
  2314. * We are under a transaction here so we can only do limited flushing.
  2315. * If we get an enospc just kick back -EAGAIN so we know to drop the
  2316. * transaction and try to refill when we can flush all the things.
  2317. */
  2318. ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
  2319. BTRFS_RESERVE_FLUSH_LIMIT);
  2320. if (ret) {
  2321. tmp = rc->extent_root->nodesize * RELOCATION_RESERVED_NODES;
  2322. while (tmp <= rc->reserved_bytes)
  2323. tmp <<= 1;
  2324. /*
  2325. * only one thread can access block_rsv at this point,
  2326. * so we don't need hold lock to protect block_rsv.
  2327. * we expand more reservation size here to allow enough
  2328. * space for relocation and we will return eailer in
  2329. * enospc case.
  2330. */
  2331. rc->block_rsv->size = tmp + rc->extent_root->nodesize *
  2332. RELOCATION_RESERVED_NODES;
  2333. return -EAGAIN;
  2334. }
  2335. return 0;
  2336. }
  2337. /*
  2338. * relocate a block tree, and then update pointers in upper level
  2339. * blocks that reference the block to point to the new location.
  2340. *
  2341. * if called by link_to_upper, the block has already been relocated.
  2342. * in that case this function just updates pointers.
  2343. */
  2344. static int do_relocation(struct btrfs_trans_handle *trans,
  2345. struct reloc_control *rc,
  2346. struct backref_node *node,
  2347. struct btrfs_key *key,
  2348. struct btrfs_path *path, int lowest)
  2349. {
  2350. struct backref_node *upper;
  2351. struct backref_edge *edge;
  2352. struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  2353. struct btrfs_root *root;
  2354. struct extent_buffer *eb;
  2355. u32 blocksize;
  2356. u64 bytenr;
  2357. u64 generation;
  2358. int slot;
  2359. int ret;
  2360. int err = 0;
  2361. BUG_ON(lowest && node->eb);
  2362. path->lowest_level = node->level + 1;
  2363. rc->backref_cache.path[node->level] = node;
  2364. list_for_each_entry(edge, &node->upper, list[LOWER]) {
  2365. cond_resched();
  2366. upper = edge->node[UPPER];
  2367. root = select_reloc_root(trans, rc, upper, edges);
  2368. BUG_ON(!root);
  2369. if (upper->eb && !upper->locked) {
  2370. if (!lowest) {
  2371. ret = btrfs_bin_search(upper->eb, key,
  2372. upper->level, &slot);
  2373. BUG_ON(ret);
  2374. bytenr = btrfs_node_blockptr(upper->eb, slot);
  2375. if (node->eb->start == bytenr)
  2376. goto next;
  2377. }
  2378. drop_node_buffer(upper);
  2379. }
  2380. if (!upper->eb) {
  2381. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  2382. if (ret) {
  2383. if (ret < 0)
  2384. err = ret;
  2385. else
  2386. err = -ENOENT;
  2387. btrfs_release_path(path);
  2388. break;
  2389. }
  2390. if (!upper->eb) {
  2391. upper->eb = path->nodes[upper->level];
  2392. path->nodes[upper->level] = NULL;
  2393. } else {
  2394. BUG_ON(upper->eb != path->nodes[upper->level]);
  2395. }
  2396. upper->locked = 1;
  2397. path->locks[upper->level] = 0;
  2398. slot = path->slots[upper->level];
  2399. btrfs_release_path(path);
  2400. } else {
  2401. ret = btrfs_bin_search(upper->eb, key, upper->level,
  2402. &slot);
  2403. BUG_ON(ret);
  2404. }
  2405. bytenr = btrfs_node_blockptr(upper->eb, slot);
  2406. if (lowest) {
  2407. if (bytenr != node->bytenr) {
  2408. btrfs_err(root->fs_info,
  2409. "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
  2410. bytenr, node->bytenr, slot,
  2411. upper->eb->start);
  2412. err = -EIO;
  2413. goto next;
  2414. }
  2415. } else {
  2416. if (node->eb->start == bytenr)
  2417. goto next;
  2418. }
  2419. blocksize = root->nodesize;
  2420. generation = btrfs_node_ptr_generation(upper->eb, slot);
  2421. eb = read_tree_block(root, bytenr, generation);
  2422. if (IS_ERR(eb)) {
  2423. err = PTR_ERR(eb);
  2424. goto next;
  2425. } else if (!extent_buffer_uptodate(eb)) {
  2426. free_extent_buffer(eb);
  2427. err = -EIO;
  2428. goto next;
  2429. }
  2430. btrfs_tree_lock(eb);
  2431. btrfs_set_lock_blocking(eb);
  2432. if (!node->eb) {
  2433. ret = btrfs_cow_block(trans, root, eb, upper->eb,
  2434. slot, &eb);
  2435. btrfs_tree_unlock(eb);
  2436. free_extent_buffer(eb);
  2437. if (ret < 0) {
  2438. err = ret;
  2439. goto next;
  2440. }
  2441. BUG_ON(node->eb != eb);
  2442. } else {
  2443. btrfs_set_node_blockptr(upper->eb, slot,
  2444. node->eb->start);
  2445. btrfs_set_node_ptr_generation(upper->eb, slot,
  2446. trans->transid);
  2447. btrfs_mark_buffer_dirty(upper->eb);
  2448. ret = btrfs_inc_extent_ref(trans, root,
  2449. node->eb->start, blocksize,
  2450. upper->eb->start,
  2451. btrfs_header_owner(upper->eb),
  2452. node->level, 0);
  2453. BUG_ON(ret);
  2454. ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
  2455. BUG_ON(ret);
  2456. }
  2457. next:
  2458. if (!upper->pending)
  2459. drop_node_buffer(upper);
  2460. else
  2461. unlock_node_buffer(upper);
  2462. if (err)
  2463. break;
  2464. }
  2465. if (!err && node->pending) {
  2466. drop_node_buffer(node);
  2467. list_move_tail(&node->list, &rc->backref_cache.changed);
  2468. node->pending = 0;
  2469. }
  2470. path->lowest_level = 0;
  2471. BUG_ON(err == -ENOSPC);
  2472. return err;
  2473. }
  2474. static int link_to_upper(struct btrfs_trans_handle *trans,
  2475. struct reloc_control *rc,
  2476. struct backref_node *node,
  2477. struct btrfs_path *path)
  2478. {
  2479. struct btrfs_key key;
  2480. btrfs_node_key_to_cpu(node->eb, &key, 0);
  2481. return do_relocation(trans, rc, node, &key, path, 0);
  2482. }
  2483. static int finish_pending_nodes(struct btrfs_trans_handle *trans,
  2484. struct reloc_control *rc,
  2485. struct btrfs_path *path, int err)
  2486. {
  2487. LIST_HEAD(list);
  2488. struct backref_cache *cache = &rc->backref_cache;
  2489. struct backref_node *node;
  2490. int level;
  2491. int ret;
  2492. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  2493. while (!list_empty(&cache->pending[level])) {
  2494. node = list_entry(cache->pending[level].next,
  2495. struct backref_node, list);
  2496. list_move_tail(&node->list, &list);
  2497. BUG_ON(!node->pending);
  2498. if (!err) {
  2499. ret = link_to_upper(trans, rc, node, path);
  2500. if (ret < 0)
  2501. err = ret;
  2502. }
  2503. }
  2504. list_splice_init(&list, &cache->pending[level]);
  2505. }
  2506. return err;
  2507. }
  2508. static void mark_block_processed(struct reloc_control *rc,
  2509. u64 bytenr, u32 blocksize)
  2510. {
  2511. set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
  2512. EXTENT_DIRTY);
  2513. }
  2514. static void __mark_block_processed(struct reloc_control *rc,
  2515. struct backref_node *node)
  2516. {
  2517. u32 blocksize;
  2518. if (node->level == 0 ||
  2519. in_block_group(node->bytenr, rc->block_group)) {
  2520. blocksize = rc->extent_root->nodesize;
  2521. mark_block_processed(rc, node->bytenr, blocksize);
  2522. }
  2523. node->processed = 1;
  2524. }
  2525. /*
  2526. * mark a block and all blocks directly/indirectly reference the block
  2527. * as processed.
  2528. */
  2529. static void update_processed_blocks(struct reloc_control *rc,
  2530. struct backref_node *node)
  2531. {
  2532. struct backref_node *next = node;
  2533. struct backref_edge *edge;
  2534. struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  2535. int index = 0;
  2536. while (next) {
  2537. cond_resched();
  2538. while (1) {
  2539. if (next->processed)
  2540. break;
  2541. __mark_block_processed(rc, next);
  2542. if (list_empty(&next->upper))
  2543. break;
  2544. edge = list_entry(next->upper.next,
  2545. struct backref_edge, list[LOWER]);
  2546. edges[index++] = edge;
  2547. next = edge->node[UPPER];
  2548. }
  2549. next = walk_down_backref(edges, &index);
  2550. }
  2551. }
  2552. static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
  2553. {
  2554. u32 blocksize = rc->extent_root->nodesize;
  2555. if (test_range_bit(&rc->processed_blocks, bytenr,
  2556. bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
  2557. return 1;
  2558. return 0;
  2559. }
  2560. static int get_tree_block_key(struct reloc_control *rc,
  2561. struct tree_block *block)
  2562. {
  2563. struct extent_buffer *eb;
  2564. BUG_ON(block->key_ready);
  2565. eb = read_tree_block(rc->extent_root, block->bytenr,
  2566. block->key.offset);
  2567. if (IS_ERR(eb)) {
  2568. return PTR_ERR(eb);
  2569. } else if (!extent_buffer_uptodate(eb)) {
  2570. free_extent_buffer(eb);
  2571. return -EIO;
  2572. }
  2573. WARN_ON(btrfs_header_level(eb) != block->level);
  2574. if (block->level == 0)
  2575. btrfs_item_key_to_cpu(eb, &block->key, 0);
  2576. else
  2577. btrfs_node_key_to_cpu(eb, &block->key, 0);
  2578. free_extent_buffer(eb);
  2579. block->key_ready = 1;
  2580. return 0;
  2581. }
  2582. /*
  2583. * helper function to relocate a tree block
  2584. */
  2585. static int relocate_tree_block(struct btrfs_trans_handle *trans,
  2586. struct reloc_control *rc,
  2587. struct backref_node *node,
  2588. struct btrfs_key *key,
  2589. struct btrfs_path *path)
  2590. {
  2591. struct btrfs_root *root;
  2592. int ret = 0;
  2593. if (!node)
  2594. return 0;
  2595. BUG_ON(node->processed);
  2596. root = select_one_root(node);
  2597. if (root == ERR_PTR(-ENOENT)) {
  2598. update_processed_blocks(rc, node);
  2599. goto out;
  2600. }
  2601. if (!root || test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
  2602. ret = reserve_metadata_space(trans, rc, node);
  2603. if (ret)
  2604. goto out;
  2605. }
  2606. if (root) {
  2607. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
  2608. BUG_ON(node->new_bytenr);
  2609. BUG_ON(!list_empty(&node->list));
  2610. btrfs_record_root_in_trans(trans, root);
  2611. root = root->reloc_root;
  2612. node->new_bytenr = root->node->start;
  2613. node->root = root;
  2614. list_add_tail(&node->list, &rc->backref_cache.changed);
  2615. } else {
  2616. path->lowest_level = node->level;
  2617. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  2618. btrfs_release_path(path);
  2619. if (ret > 0)
  2620. ret = 0;
  2621. }
  2622. if (!ret)
  2623. update_processed_blocks(rc, node);
  2624. } else {
  2625. ret = do_relocation(trans, rc, node, key, path, 1);
  2626. }
  2627. out:
  2628. if (ret || node->level == 0 || node->cowonly)
  2629. remove_backref_node(&rc->backref_cache, node);
  2630. return ret;
  2631. }
  2632. /*
  2633. * relocate a list of blocks
  2634. */
  2635. static noinline_for_stack
  2636. int relocate_tree_blocks(struct btrfs_trans_handle *trans,
  2637. struct reloc_control *rc, struct rb_root *blocks)
  2638. {
  2639. struct backref_node *node;
  2640. struct btrfs_path *path;
  2641. struct tree_block *block;
  2642. struct rb_node *rb_node;
  2643. int ret;
  2644. int err = 0;
  2645. path = btrfs_alloc_path();
  2646. if (!path) {
  2647. err = -ENOMEM;
  2648. goto out_free_blocks;
  2649. }
  2650. rb_node = rb_first(blocks);
  2651. while (rb_node) {
  2652. block = rb_entry(rb_node, struct tree_block, rb_node);
  2653. if (!block->key_ready)
  2654. readahead_tree_block(rc->extent_root, block->bytenr);
  2655. rb_node = rb_next(rb_node);
  2656. }
  2657. rb_node = rb_first(blocks);
  2658. while (rb_node) {
  2659. block = rb_entry(rb_node, struct tree_block, rb_node);
  2660. if (!block->key_ready) {
  2661. err = get_tree_block_key(rc, block);
  2662. if (err)
  2663. goto out_free_path;
  2664. }
  2665. rb_node = rb_next(rb_node);
  2666. }
  2667. rb_node = rb_first(blocks);
  2668. while (rb_node) {
  2669. block = rb_entry(rb_node, struct tree_block, rb_node);
  2670. node = build_backref_tree(rc, &block->key,
  2671. block->level, block->bytenr);
  2672. if (IS_ERR(node)) {
  2673. err = PTR_ERR(node);
  2674. goto out;
  2675. }
  2676. ret = relocate_tree_block(trans, rc, node, &block->key,
  2677. path);
  2678. if (ret < 0) {
  2679. if (ret != -EAGAIN || rb_node == rb_first(blocks))
  2680. err = ret;
  2681. goto out;
  2682. }
  2683. rb_node = rb_next(rb_node);
  2684. }
  2685. out:
  2686. err = finish_pending_nodes(trans, rc, path, err);
  2687. out_free_path:
  2688. btrfs_free_path(path);
  2689. out_free_blocks:
  2690. free_block_list(blocks);
  2691. return err;
  2692. }
  2693. static noinline_for_stack
  2694. int prealloc_file_extent_cluster(struct inode *inode,
  2695. struct file_extent_cluster *cluster)
  2696. {
  2697. u64 alloc_hint = 0;
  2698. u64 start;
  2699. u64 end;
  2700. u64 offset = BTRFS_I(inode)->index_cnt;
  2701. u64 num_bytes;
  2702. int nr = 0;
  2703. int ret = 0;
  2704. u64 prealloc_start = cluster->start - offset;
  2705. u64 prealloc_end = cluster->end - offset;
  2706. u64 cur_offset;
  2707. BUG_ON(cluster->start != cluster->boundary[0]);
  2708. inode_lock(inode);
  2709. ret = btrfs_check_data_free_space(inode, prealloc_start,
  2710. prealloc_end + 1 - prealloc_start);
  2711. if (ret)
  2712. goto out;
  2713. cur_offset = prealloc_start;
  2714. while (nr < cluster->nr) {
  2715. start = cluster->boundary[nr] - offset;
  2716. if (nr + 1 < cluster->nr)
  2717. end = cluster->boundary[nr + 1] - 1 - offset;
  2718. else
  2719. end = cluster->end - offset;
  2720. lock_extent(&BTRFS_I(inode)->io_tree, start, end);
  2721. num_bytes = end + 1 - start;
  2722. if (cur_offset < start)
  2723. btrfs_free_reserved_data_space(inode, cur_offset,
  2724. start - cur_offset);
  2725. ret = btrfs_prealloc_file_range(inode, 0, start,
  2726. num_bytes, num_bytes,
  2727. end + 1, &alloc_hint);
  2728. cur_offset = end + 1;
  2729. unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
  2730. if (ret)
  2731. break;
  2732. nr++;
  2733. }
  2734. if (cur_offset < prealloc_end)
  2735. btrfs_free_reserved_data_space(inode, cur_offset,
  2736. prealloc_end + 1 - cur_offset);
  2737. out:
  2738. inode_unlock(inode);
  2739. return ret;
  2740. }
  2741. static noinline_for_stack
  2742. int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
  2743. u64 block_start)
  2744. {
  2745. struct btrfs_root *root = BTRFS_I(inode)->root;
  2746. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2747. struct extent_map *em;
  2748. int ret = 0;
  2749. em = alloc_extent_map();
  2750. if (!em)
  2751. return -ENOMEM;
  2752. em->start = start;
  2753. em->len = end + 1 - start;
  2754. em->block_len = em->len;
  2755. em->block_start = block_start;
  2756. em->bdev = root->fs_info->fs_devices->latest_bdev;
  2757. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  2758. lock_extent(&BTRFS_I(inode)->io_tree, start, end);
  2759. while (1) {
  2760. write_lock(&em_tree->lock);
  2761. ret = add_extent_mapping(em_tree, em, 0);
  2762. write_unlock(&em_tree->lock);
  2763. if (ret != -EEXIST) {
  2764. free_extent_map(em);
  2765. break;
  2766. }
  2767. btrfs_drop_extent_cache(inode, start, end, 0);
  2768. }
  2769. unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
  2770. return ret;
  2771. }
  2772. static int relocate_file_extent_cluster(struct inode *inode,
  2773. struct file_extent_cluster *cluster)
  2774. {
  2775. u64 page_start;
  2776. u64 page_end;
  2777. u64 offset = BTRFS_I(inode)->index_cnt;
  2778. unsigned long index;
  2779. unsigned long last_index;
  2780. struct page *page;
  2781. struct file_ra_state *ra;
  2782. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  2783. int nr = 0;
  2784. int ret = 0;
  2785. if (!cluster->nr)
  2786. return 0;
  2787. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  2788. if (!ra)
  2789. return -ENOMEM;
  2790. ret = prealloc_file_extent_cluster(inode, cluster);
  2791. if (ret)
  2792. goto out;
  2793. file_ra_state_init(ra, inode->i_mapping);
  2794. ret = setup_extent_mapping(inode, cluster->start - offset,
  2795. cluster->end - offset, cluster->start);
  2796. if (ret)
  2797. goto out;
  2798. index = (cluster->start - offset) >> PAGE_SHIFT;
  2799. last_index = (cluster->end - offset) >> PAGE_SHIFT;
  2800. while (index <= last_index) {
  2801. ret = btrfs_delalloc_reserve_metadata(inode, PAGE_SIZE);
  2802. if (ret)
  2803. goto out;
  2804. page = find_lock_page(inode->i_mapping, index);
  2805. if (!page) {
  2806. page_cache_sync_readahead(inode->i_mapping,
  2807. ra, NULL, index,
  2808. last_index + 1 - index);
  2809. page = find_or_create_page(inode->i_mapping, index,
  2810. mask);
  2811. if (!page) {
  2812. btrfs_delalloc_release_metadata(inode,
  2813. PAGE_SIZE);
  2814. ret = -ENOMEM;
  2815. goto out;
  2816. }
  2817. }
  2818. if (PageReadahead(page)) {
  2819. page_cache_async_readahead(inode->i_mapping,
  2820. ra, NULL, page, index,
  2821. last_index + 1 - index);
  2822. }
  2823. if (!PageUptodate(page)) {
  2824. btrfs_readpage(NULL, page);
  2825. lock_page(page);
  2826. if (!PageUptodate(page)) {
  2827. unlock_page(page);
  2828. put_page(page);
  2829. btrfs_delalloc_release_metadata(inode,
  2830. PAGE_SIZE);
  2831. ret = -EIO;
  2832. goto out;
  2833. }
  2834. }
  2835. page_start = page_offset(page);
  2836. page_end = page_start + PAGE_SIZE - 1;
  2837. lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
  2838. set_page_extent_mapped(page);
  2839. if (nr < cluster->nr &&
  2840. page_start + offset == cluster->boundary[nr]) {
  2841. set_extent_bits(&BTRFS_I(inode)->io_tree,
  2842. page_start, page_end,
  2843. EXTENT_BOUNDARY);
  2844. nr++;
  2845. }
  2846. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL, 0);
  2847. set_page_dirty(page);
  2848. unlock_extent(&BTRFS_I(inode)->io_tree,
  2849. page_start, page_end);
  2850. unlock_page(page);
  2851. put_page(page);
  2852. index++;
  2853. balance_dirty_pages_ratelimited(inode->i_mapping);
  2854. btrfs_throttle(BTRFS_I(inode)->root);
  2855. }
  2856. WARN_ON(nr != cluster->nr);
  2857. out:
  2858. kfree(ra);
  2859. return ret;
  2860. }
  2861. static noinline_for_stack
  2862. int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
  2863. struct file_extent_cluster *cluster)
  2864. {
  2865. int ret;
  2866. if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
  2867. ret = relocate_file_extent_cluster(inode, cluster);
  2868. if (ret)
  2869. return ret;
  2870. cluster->nr = 0;
  2871. }
  2872. if (!cluster->nr)
  2873. cluster->start = extent_key->objectid;
  2874. else
  2875. BUG_ON(cluster->nr >= MAX_EXTENTS);
  2876. cluster->end = extent_key->objectid + extent_key->offset - 1;
  2877. cluster->boundary[cluster->nr] = extent_key->objectid;
  2878. cluster->nr++;
  2879. if (cluster->nr >= MAX_EXTENTS) {
  2880. ret = relocate_file_extent_cluster(inode, cluster);
  2881. if (ret)
  2882. return ret;
  2883. cluster->nr = 0;
  2884. }
  2885. return 0;
  2886. }
  2887. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2888. static int get_ref_objectid_v0(struct reloc_control *rc,
  2889. struct btrfs_path *path,
  2890. struct btrfs_key *extent_key,
  2891. u64 *ref_objectid, int *path_change)
  2892. {
  2893. struct btrfs_key key;
  2894. struct extent_buffer *leaf;
  2895. struct btrfs_extent_ref_v0 *ref0;
  2896. int ret;
  2897. int slot;
  2898. leaf = path->nodes[0];
  2899. slot = path->slots[0];
  2900. while (1) {
  2901. if (slot >= btrfs_header_nritems(leaf)) {
  2902. ret = btrfs_next_leaf(rc->extent_root, path);
  2903. if (ret < 0)
  2904. return ret;
  2905. BUG_ON(ret > 0);
  2906. leaf = path->nodes[0];
  2907. slot = path->slots[0];
  2908. if (path_change)
  2909. *path_change = 1;
  2910. }
  2911. btrfs_item_key_to_cpu(leaf, &key, slot);
  2912. if (key.objectid != extent_key->objectid)
  2913. return -ENOENT;
  2914. if (key.type != BTRFS_EXTENT_REF_V0_KEY) {
  2915. slot++;
  2916. continue;
  2917. }
  2918. ref0 = btrfs_item_ptr(leaf, slot,
  2919. struct btrfs_extent_ref_v0);
  2920. *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0);
  2921. break;
  2922. }
  2923. return 0;
  2924. }
  2925. #endif
  2926. /*
  2927. * helper to add a tree block to the list.
  2928. * the major work is getting the generation and level of the block
  2929. */
  2930. static int add_tree_block(struct reloc_control *rc,
  2931. struct btrfs_key *extent_key,
  2932. struct btrfs_path *path,
  2933. struct rb_root *blocks)
  2934. {
  2935. struct extent_buffer *eb;
  2936. struct btrfs_extent_item *ei;
  2937. struct btrfs_tree_block_info *bi;
  2938. struct tree_block *block;
  2939. struct rb_node *rb_node;
  2940. u32 item_size;
  2941. int level = -1;
  2942. u64 generation;
  2943. eb = path->nodes[0];
  2944. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  2945. if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
  2946. item_size >= sizeof(*ei) + sizeof(*bi)) {
  2947. ei = btrfs_item_ptr(eb, path->slots[0],
  2948. struct btrfs_extent_item);
  2949. if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
  2950. bi = (struct btrfs_tree_block_info *)(ei + 1);
  2951. level = btrfs_tree_block_level(eb, bi);
  2952. } else {
  2953. level = (int)extent_key->offset;
  2954. }
  2955. generation = btrfs_extent_generation(eb, ei);
  2956. } else {
  2957. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  2958. u64 ref_owner;
  2959. int ret;
  2960. BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0));
  2961. ret = get_ref_objectid_v0(rc, path, extent_key,
  2962. &ref_owner, NULL);
  2963. if (ret < 0)
  2964. return ret;
  2965. BUG_ON(ref_owner >= BTRFS_MAX_LEVEL);
  2966. level = (int)ref_owner;
  2967. /* FIXME: get real generation */
  2968. generation = 0;
  2969. #else
  2970. BUG();
  2971. #endif
  2972. }
  2973. btrfs_release_path(path);
  2974. BUG_ON(level == -1);
  2975. block = kmalloc(sizeof(*block), GFP_NOFS);
  2976. if (!block)
  2977. return -ENOMEM;
  2978. block->bytenr = extent_key->objectid;
  2979. block->key.objectid = rc->extent_root->nodesize;
  2980. block->key.offset = generation;
  2981. block->level = level;
  2982. block->key_ready = 0;
  2983. rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
  2984. if (rb_node)
  2985. backref_tree_panic(rb_node, -EEXIST, block->bytenr);
  2986. return 0;
  2987. }
  2988. /*
  2989. * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
  2990. */
  2991. static int __add_tree_block(struct reloc_control *rc,
  2992. u64 bytenr, u32 blocksize,
  2993. struct rb_root *blocks)
  2994. {
  2995. struct btrfs_path *path;
  2996. struct btrfs_key key;
  2997. int ret;
  2998. bool skinny = btrfs_fs_incompat(rc->extent_root->fs_info,
  2999. SKINNY_METADATA);
  3000. if (tree_block_processed(bytenr, rc))
  3001. return 0;
  3002. if (tree_search(blocks, bytenr))
  3003. return 0;
  3004. path = btrfs_alloc_path();
  3005. if (!path)
  3006. return -ENOMEM;
  3007. again:
  3008. key.objectid = bytenr;
  3009. if (skinny) {
  3010. key.type = BTRFS_METADATA_ITEM_KEY;
  3011. key.offset = (u64)-1;
  3012. } else {
  3013. key.type = BTRFS_EXTENT_ITEM_KEY;
  3014. key.offset = blocksize;
  3015. }
  3016. path->search_commit_root = 1;
  3017. path->skip_locking = 1;
  3018. ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
  3019. if (ret < 0)
  3020. goto out;
  3021. if (ret > 0 && skinny) {
  3022. if (path->slots[0]) {
  3023. path->slots[0]--;
  3024. btrfs_item_key_to_cpu(path->nodes[0], &key,
  3025. path->slots[0]);
  3026. if (key.objectid == bytenr &&
  3027. (key.type == BTRFS_METADATA_ITEM_KEY ||
  3028. (key.type == BTRFS_EXTENT_ITEM_KEY &&
  3029. key.offset == blocksize)))
  3030. ret = 0;
  3031. }
  3032. if (ret) {
  3033. skinny = false;
  3034. btrfs_release_path(path);
  3035. goto again;
  3036. }
  3037. }
  3038. BUG_ON(ret);
  3039. ret = add_tree_block(rc, &key, path, blocks);
  3040. out:
  3041. btrfs_free_path(path);
  3042. return ret;
  3043. }
  3044. /*
  3045. * helper to check if the block use full backrefs for pointers in it
  3046. */
  3047. static int block_use_full_backref(struct reloc_control *rc,
  3048. struct extent_buffer *eb)
  3049. {
  3050. u64 flags;
  3051. int ret;
  3052. if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
  3053. btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
  3054. return 1;
  3055. ret = btrfs_lookup_extent_info(NULL, rc->extent_root,
  3056. eb->start, btrfs_header_level(eb), 1,
  3057. NULL, &flags);
  3058. BUG_ON(ret);
  3059. if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
  3060. ret = 1;
  3061. else
  3062. ret = 0;
  3063. return ret;
  3064. }
  3065. static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
  3066. struct btrfs_block_group_cache *block_group,
  3067. struct inode *inode,
  3068. u64 ino)
  3069. {
  3070. struct btrfs_key key;
  3071. struct btrfs_root *root = fs_info->tree_root;
  3072. struct btrfs_trans_handle *trans;
  3073. int ret = 0;
  3074. if (inode)
  3075. goto truncate;
  3076. key.objectid = ino;
  3077. key.type = BTRFS_INODE_ITEM_KEY;
  3078. key.offset = 0;
  3079. inode = btrfs_iget(fs_info->sb, &key, root, NULL);
  3080. if (IS_ERR(inode) || is_bad_inode(inode)) {
  3081. if (!IS_ERR(inode))
  3082. iput(inode);
  3083. return -ENOENT;
  3084. }
  3085. truncate:
  3086. ret = btrfs_check_trunc_cache_free_space(root,
  3087. &fs_info->global_block_rsv);
  3088. if (ret)
  3089. goto out;
  3090. trans = btrfs_join_transaction(root);
  3091. if (IS_ERR(trans)) {
  3092. ret = PTR_ERR(trans);
  3093. goto out;
  3094. }
  3095. ret = btrfs_truncate_free_space_cache(root, trans, block_group, inode);
  3096. btrfs_end_transaction(trans, root);
  3097. btrfs_btree_balance_dirty(root);
  3098. out:
  3099. iput(inode);
  3100. return ret;
  3101. }
  3102. /*
  3103. * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
  3104. * this function scans fs tree to find blocks reference the data extent
  3105. */
  3106. static int find_data_references(struct reloc_control *rc,
  3107. struct btrfs_key *extent_key,
  3108. struct extent_buffer *leaf,
  3109. struct btrfs_extent_data_ref *ref,
  3110. struct rb_root *blocks)
  3111. {
  3112. struct btrfs_path *path;
  3113. struct tree_block *block;
  3114. struct btrfs_root *root;
  3115. struct btrfs_file_extent_item *fi;
  3116. struct rb_node *rb_node;
  3117. struct btrfs_key key;
  3118. u64 ref_root;
  3119. u64 ref_objectid;
  3120. u64 ref_offset;
  3121. u32 ref_count;
  3122. u32 nritems;
  3123. int err = 0;
  3124. int added = 0;
  3125. int counted;
  3126. int ret;
  3127. ref_root = btrfs_extent_data_ref_root(leaf, ref);
  3128. ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
  3129. ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
  3130. ref_count = btrfs_extent_data_ref_count(leaf, ref);
  3131. /*
  3132. * This is an extent belonging to the free space cache, lets just delete
  3133. * it and redo the search.
  3134. */
  3135. if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
  3136. ret = delete_block_group_cache(rc->extent_root->fs_info,
  3137. rc->block_group,
  3138. NULL, ref_objectid);
  3139. if (ret != -ENOENT)
  3140. return ret;
  3141. ret = 0;
  3142. }
  3143. path = btrfs_alloc_path();
  3144. if (!path)
  3145. return -ENOMEM;
  3146. path->reada = READA_FORWARD;
  3147. root = read_fs_root(rc->extent_root->fs_info, ref_root);
  3148. if (IS_ERR(root)) {
  3149. err = PTR_ERR(root);
  3150. goto out;
  3151. }
  3152. key.objectid = ref_objectid;
  3153. key.type = BTRFS_EXTENT_DATA_KEY;
  3154. if (ref_offset > ((u64)-1 << 32))
  3155. key.offset = 0;
  3156. else
  3157. key.offset = ref_offset;
  3158. path->search_commit_root = 1;
  3159. path->skip_locking = 1;
  3160. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3161. if (ret < 0) {
  3162. err = ret;
  3163. goto out;
  3164. }
  3165. leaf = path->nodes[0];
  3166. nritems = btrfs_header_nritems(leaf);
  3167. /*
  3168. * the references in tree blocks that use full backrefs
  3169. * are not counted in
  3170. */
  3171. if (block_use_full_backref(rc, leaf))
  3172. counted = 0;
  3173. else
  3174. counted = 1;
  3175. rb_node = tree_search(blocks, leaf->start);
  3176. if (rb_node) {
  3177. if (counted)
  3178. added = 1;
  3179. else
  3180. path->slots[0] = nritems;
  3181. }
  3182. while (ref_count > 0) {
  3183. while (path->slots[0] >= nritems) {
  3184. ret = btrfs_next_leaf(root, path);
  3185. if (ret < 0) {
  3186. err = ret;
  3187. goto out;
  3188. }
  3189. if (WARN_ON(ret > 0))
  3190. goto out;
  3191. leaf = path->nodes[0];
  3192. nritems = btrfs_header_nritems(leaf);
  3193. added = 0;
  3194. if (block_use_full_backref(rc, leaf))
  3195. counted = 0;
  3196. else
  3197. counted = 1;
  3198. rb_node = tree_search(blocks, leaf->start);
  3199. if (rb_node) {
  3200. if (counted)
  3201. added = 1;
  3202. else
  3203. path->slots[0] = nritems;
  3204. }
  3205. }
  3206. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3207. if (WARN_ON(key.objectid != ref_objectid ||
  3208. key.type != BTRFS_EXTENT_DATA_KEY))
  3209. break;
  3210. fi = btrfs_item_ptr(leaf, path->slots[0],
  3211. struct btrfs_file_extent_item);
  3212. if (btrfs_file_extent_type(leaf, fi) ==
  3213. BTRFS_FILE_EXTENT_INLINE)
  3214. goto next;
  3215. if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  3216. extent_key->objectid)
  3217. goto next;
  3218. key.offset -= btrfs_file_extent_offset(leaf, fi);
  3219. if (key.offset != ref_offset)
  3220. goto next;
  3221. if (counted)
  3222. ref_count--;
  3223. if (added)
  3224. goto next;
  3225. if (!tree_block_processed(leaf->start, rc)) {
  3226. block = kmalloc(sizeof(*block), GFP_NOFS);
  3227. if (!block) {
  3228. err = -ENOMEM;
  3229. break;
  3230. }
  3231. block->bytenr = leaf->start;
  3232. btrfs_item_key_to_cpu(leaf, &block->key, 0);
  3233. block->level = 0;
  3234. block->key_ready = 1;
  3235. rb_node = tree_insert(blocks, block->bytenr,
  3236. &block->rb_node);
  3237. if (rb_node)
  3238. backref_tree_panic(rb_node, -EEXIST,
  3239. block->bytenr);
  3240. }
  3241. if (counted)
  3242. added = 1;
  3243. else
  3244. path->slots[0] = nritems;
  3245. next:
  3246. path->slots[0]++;
  3247. }
  3248. out:
  3249. btrfs_free_path(path);
  3250. return err;
  3251. }
  3252. /*
  3253. * helper to find all tree blocks that reference a given data extent
  3254. */
  3255. static noinline_for_stack
  3256. int add_data_references(struct reloc_control *rc,
  3257. struct btrfs_key *extent_key,
  3258. struct btrfs_path *path,
  3259. struct rb_root *blocks)
  3260. {
  3261. struct btrfs_key key;
  3262. struct extent_buffer *eb;
  3263. struct btrfs_extent_data_ref *dref;
  3264. struct btrfs_extent_inline_ref *iref;
  3265. unsigned long ptr;
  3266. unsigned long end;
  3267. u32 blocksize = rc->extent_root->nodesize;
  3268. int ret = 0;
  3269. int err = 0;
  3270. eb = path->nodes[0];
  3271. ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
  3272. end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
  3273. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  3274. if (ptr + sizeof(struct btrfs_extent_item_v0) == end)
  3275. ptr = end;
  3276. else
  3277. #endif
  3278. ptr += sizeof(struct btrfs_extent_item);
  3279. while (ptr < end) {
  3280. iref = (struct btrfs_extent_inline_ref *)ptr;
  3281. key.type = btrfs_extent_inline_ref_type(eb, iref);
  3282. if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  3283. key.offset = btrfs_extent_inline_ref_offset(eb, iref);
  3284. ret = __add_tree_block(rc, key.offset, blocksize,
  3285. blocks);
  3286. } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  3287. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  3288. ret = find_data_references(rc, extent_key,
  3289. eb, dref, blocks);
  3290. } else {
  3291. BUG();
  3292. }
  3293. if (ret) {
  3294. err = ret;
  3295. goto out;
  3296. }
  3297. ptr += btrfs_extent_inline_ref_size(key.type);
  3298. }
  3299. WARN_ON(ptr > end);
  3300. while (1) {
  3301. cond_resched();
  3302. eb = path->nodes[0];
  3303. if (path->slots[0] >= btrfs_header_nritems(eb)) {
  3304. ret = btrfs_next_leaf(rc->extent_root, path);
  3305. if (ret < 0) {
  3306. err = ret;
  3307. break;
  3308. }
  3309. if (ret > 0)
  3310. break;
  3311. eb = path->nodes[0];
  3312. }
  3313. btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
  3314. if (key.objectid != extent_key->objectid)
  3315. break;
  3316. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  3317. if (key.type == BTRFS_SHARED_DATA_REF_KEY ||
  3318. key.type == BTRFS_EXTENT_REF_V0_KEY) {
  3319. #else
  3320. BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
  3321. if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
  3322. #endif
  3323. ret = __add_tree_block(rc, key.offset, blocksize,
  3324. blocks);
  3325. } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
  3326. dref = btrfs_item_ptr(eb, path->slots[0],
  3327. struct btrfs_extent_data_ref);
  3328. ret = find_data_references(rc, extent_key,
  3329. eb, dref, blocks);
  3330. } else {
  3331. ret = 0;
  3332. }
  3333. if (ret) {
  3334. err = ret;
  3335. break;
  3336. }
  3337. path->slots[0]++;
  3338. }
  3339. out:
  3340. btrfs_release_path(path);
  3341. if (err)
  3342. free_block_list(blocks);
  3343. return err;
  3344. }
  3345. /*
  3346. * helper to find next unprocessed extent
  3347. */
  3348. static noinline_for_stack
  3349. int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
  3350. struct btrfs_key *extent_key)
  3351. {
  3352. struct btrfs_key key;
  3353. struct extent_buffer *leaf;
  3354. u64 start, end, last;
  3355. int ret;
  3356. last = rc->block_group->key.objectid + rc->block_group->key.offset;
  3357. while (1) {
  3358. cond_resched();
  3359. if (rc->search_start >= last) {
  3360. ret = 1;
  3361. break;
  3362. }
  3363. key.objectid = rc->search_start;
  3364. key.type = BTRFS_EXTENT_ITEM_KEY;
  3365. key.offset = 0;
  3366. path->search_commit_root = 1;
  3367. path->skip_locking = 1;
  3368. ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
  3369. 0, 0);
  3370. if (ret < 0)
  3371. break;
  3372. next:
  3373. leaf = path->nodes[0];
  3374. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  3375. ret = btrfs_next_leaf(rc->extent_root, path);
  3376. if (ret != 0)
  3377. break;
  3378. leaf = path->nodes[0];
  3379. }
  3380. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3381. if (key.objectid >= last) {
  3382. ret = 1;
  3383. break;
  3384. }
  3385. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  3386. key.type != BTRFS_METADATA_ITEM_KEY) {
  3387. path->slots[0]++;
  3388. goto next;
  3389. }
  3390. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  3391. key.objectid + key.offset <= rc->search_start) {
  3392. path->slots[0]++;
  3393. goto next;
  3394. }
  3395. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  3396. key.objectid + rc->extent_root->nodesize <=
  3397. rc->search_start) {
  3398. path->slots[0]++;
  3399. goto next;
  3400. }
  3401. ret = find_first_extent_bit(&rc->processed_blocks,
  3402. key.objectid, &start, &end,
  3403. EXTENT_DIRTY, NULL);
  3404. if (ret == 0 && start <= key.objectid) {
  3405. btrfs_release_path(path);
  3406. rc->search_start = end + 1;
  3407. } else {
  3408. if (key.type == BTRFS_EXTENT_ITEM_KEY)
  3409. rc->search_start = key.objectid + key.offset;
  3410. else
  3411. rc->search_start = key.objectid +
  3412. rc->extent_root->nodesize;
  3413. memcpy(extent_key, &key, sizeof(key));
  3414. return 0;
  3415. }
  3416. }
  3417. btrfs_release_path(path);
  3418. return ret;
  3419. }
  3420. static void set_reloc_control(struct reloc_control *rc)
  3421. {
  3422. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  3423. mutex_lock(&fs_info->reloc_mutex);
  3424. fs_info->reloc_ctl = rc;
  3425. mutex_unlock(&fs_info->reloc_mutex);
  3426. }
  3427. static void unset_reloc_control(struct reloc_control *rc)
  3428. {
  3429. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  3430. mutex_lock(&fs_info->reloc_mutex);
  3431. fs_info->reloc_ctl = NULL;
  3432. mutex_unlock(&fs_info->reloc_mutex);
  3433. }
  3434. static int check_extent_flags(u64 flags)
  3435. {
  3436. if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
  3437. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
  3438. return 1;
  3439. if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
  3440. !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
  3441. return 1;
  3442. if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
  3443. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  3444. return 1;
  3445. return 0;
  3446. }
  3447. static noinline_for_stack
  3448. int prepare_to_relocate(struct reloc_control *rc)
  3449. {
  3450. struct btrfs_trans_handle *trans;
  3451. int ret;
  3452. rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root,
  3453. BTRFS_BLOCK_RSV_TEMP);
  3454. if (!rc->block_rsv)
  3455. return -ENOMEM;
  3456. memset(&rc->cluster, 0, sizeof(rc->cluster));
  3457. rc->search_start = rc->block_group->key.objectid;
  3458. rc->extents_found = 0;
  3459. rc->nodes_relocated = 0;
  3460. rc->merging_rsv_size = 0;
  3461. rc->reserved_bytes = 0;
  3462. rc->block_rsv->size = rc->extent_root->nodesize *
  3463. RELOCATION_RESERVED_NODES;
  3464. ret = btrfs_block_rsv_refill(rc->extent_root,
  3465. rc->block_rsv, rc->block_rsv->size,
  3466. BTRFS_RESERVE_FLUSH_ALL);
  3467. if (ret)
  3468. return ret;
  3469. rc->create_reloc_tree = 1;
  3470. set_reloc_control(rc);
  3471. trans = btrfs_join_transaction(rc->extent_root);
  3472. if (IS_ERR(trans)) {
  3473. unset_reloc_control(rc);
  3474. /*
  3475. * extent tree is not a ref_cow tree and has no reloc_root to
  3476. * cleanup. And callers are responsible to free the above
  3477. * block rsv.
  3478. */
  3479. return PTR_ERR(trans);
  3480. }
  3481. btrfs_commit_transaction(trans, rc->extent_root);
  3482. return 0;
  3483. }
  3484. /*
  3485. * Qgroup fixer for data chunk relocation.
  3486. * The data relocation is done in the following steps
  3487. * 1) Copy data extents into data reloc tree
  3488. * 2) Create tree reloc tree(special snapshot) for related subvolumes
  3489. * 3) Modify file extents in tree reloc tree
  3490. * 4) Merge tree reloc tree with original fs tree, by swapping tree blocks
  3491. *
  3492. * The problem is, data and tree reloc tree are not accounted to qgroup,
  3493. * and 4) will only info qgroup to track tree blocks change, not file extents
  3494. * in the tree blocks.
  3495. *
  3496. * The good news is, related data extents are all in data reloc tree, so we
  3497. * only need to info qgroup to track all file extents in data reloc tree
  3498. * before commit trans.
  3499. */
  3500. static int qgroup_fix_relocated_data_extents(struct btrfs_trans_handle *trans,
  3501. struct reloc_control *rc)
  3502. {
  3503. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  3504. struct inode *inode = rc->data_inode;
  3505. struct btrfs_root *data_reloc_root = BTRFS_I(inode)->root;
  3506. struct btrfs_path *path;
  3507. struct btrfs_key key;
  3508. int ret = 0;
  3509. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  3510. return 0;
  3511. /*
  3512. * Only for stage where we update data pointers the qgroup fix is
  3513. * valid.
  3514. * For MOVING_DATA stage, we will miss the timing of swapping tree
  3515. * blocks, and won't fix it.
  3516. */
  3517. if (!(rc->stage == UPDATE_DATA_PTRS && rc->extents_found))
  3518. return 0;
  3519. path = btrfs_alloc_path();
  3520. if (!path)
  3521. return -ENOMEM;
  3522. key.objectid = btrfs_ino(inode);
  3523. key.type = BTRFS_EXTENT_DATA_KEY;
  3524. key.offset = 0;
  3525. ret = btrfs_search_slot(NULL, data_reloc_root, &key, path, 0, 0);
  3526. if (ret < 0)
  3527. goto out;
  3528. lock_extent(&BTRFS_I(inode)->io_tree, 0, (u64)-1);
  3529. while (1) {
  3530. struct btrfs_file_extent_item *fi;
  3531. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  3532. if (key.objectid > btrfs_ino(inode))
  3533. break;
  3534. if (key.type != BTRFS_EXTENT_DATA_KEY)
  3535. goto next;
  3536. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3537. struct btrfs_file_extent_item);
  3538. if (btrfs_file_extent_type(path->nodes[0], fi) !=
  3539. BTRFS_FILE_EXTENT_REG)
  3540. goto next;
  3541. ret = btrfs_qgroup_insert_dirty_extent(trans, fs_info,
  3542. btrfs_file_extent_disk_bytenr(path->nodes[0], fi),
  3543. btrfs_file_extent_disk_num_bytes(path->nodes[0], fi),
  3544. GFP_NOFS);
  3545. if (ret < 0)
  3546. break;
  3547. next:
  3548. ret = btrfs_next_item(data_reloc_root, path);
  3549. if (ret < 0)
  3550. break;
  3551. if (ret > 0) {
  3552. ret = 0;
  3553. break;
  3554. }
  3555. }
  3556. unlock_extent(&BTRFS_I(inode)->io_tree, 0 , (u64)-1);
  3557. out:
  3558. btrfs_free_path(path);
  3559. return ret;
  3560. }
  3561. static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
  3562. {
  3563. struct rb_root blocks = RB_ROOT;
  3564. struct btrfs_key key;
  3565. struct btrfs_trans_handle *trans = NULL;
  3566. struct btrfs_path *path;
  3567. struct btrfs_extent_item *ei;
  3568. u64 flags;
  3569. u32 item_size;
  3570. int ret;
  3571. int err = 0;
  3572. int progress = 0;
  3573. path = btrfs_alloc_path();
  3574. if (!path)
  3575. return -ENOMEM;
  3576. path->reada = READA_FORWARD;
  3577. ret = prepare_to_relocate(rc);
  3578. if (ret) {
  3579. err = ret;
  3580. goto out_free;
  3581. }
  3582. while (1) {
  3583. rc->reserved_bytes = 0;
  3584. ret = btrfs_block_rsv_refill(rc->extent_root,
  3585. rc->block_rsv, rc->block_rsv->size,
  3586. BTRFS_RESERVE_FLUSH_ALL);
  3587. if (ret) {
  3588. err = ret;
  3589. break;
  3590. }
  3591. progress++;
  3592. trans = btrfs_start_transaction(rc->extent_root, 0);
  3593. if (IS_ERR(trans)) {
  3594. err = PTR_ERR(trans);
  3595. trans = NULL;
  3596. break;
  3597. }
  3598. restart:
  3599. if (update_backref_cache(trans, &rc->backref_cache)) {
  3600. btrfs_end_transaction(trans, rc->extent_root);
  3601. continue;
  3602. }
  3603. ret = find_next_extent(rc, path, &key);
  3604. if (ret < 0)
  3605. err = ret;
  3606. if (ret != 0)
  3607. break;
  3608. rc->extents_found++;
  3609. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3610. struct btrfs_extent_item);
  3611. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  3612. if (item_size >= sizeof(*ei)) {
  3613. flags = btrfs_extent_flags(path->nodes[0], ei);
  3614. ret = check_extent_flags(flags);
  3615. BUG_ON(ret);
  3616. } else {
  3617. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  3618. u64 ref_owner;
  3619. int path_change = 0;
  3620. BUG_ON(item_size !=
  3621. sizeof(struct btrfs_extent_item_v0));
  3622. ret = get_ref_objectid_v0(rc, path, &key, &ref_owner,
  3623. &path_change);
  3624. if (ret < 0) {
  3625. err = ret;
  3626. break;
  3627. }
  3628. if (ref_owner < BTRFS_FIRST_FREE_OBJECTID)
  3629. flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
  3630. else
  3631. flags = BTRFS_EXTENT_FLAG_DATA;
  3632. if (path_change) {
  3633. btrfs_release_path(path);
  3634. path->search_commit_root = 1;
  3635. path->skip_locking = 1;
  3636. ret = btrfs_search_slot(NULL, rc->extent_root,
  3637. &key, path, 0, 0);
  3638. if (ret < 0) {
  3639. err = ret;
  3640. break;
  3641. }
  3642. BUG_ON(ret > 0);
  3643. }
  3644. #else
  3645. BUG();
  3646. #endif
  3647. }
  3648. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  3649. ret = add_tree_block(rc, &key, path, &blocks);
  3650. } else if (rc->stage == UPDATE_DATA_PTRS &&
  3651. (flags & BTRFS_EXTENT_FLAG_DATA)) {
  3652. ret = add_data_references(rc, &key, path, &blocks);
  3653. } else {
  3654. btrfs_release_path(path);
  3655. ret = 0;
  3656. }
  3657. if (ret < 0) {
  3658. err = ret;
  3659. break;
  3660. }
  3661. if (!RB_EMPTY_ROOT(&blocks)) {
  3662. ret = relocate_tree_blocks(trans, rc, &blocks);
  3663. if (ret < 0) {
  3664. /*
  3665. * if we fail to relocate tree blocks, force to update
  3666. * backref cache when committing transaction.
  3667. */
  3668. rc->backref_cache.last_trans = trans->transid - 1;
  3669. if (ret != -EAGAIN) {
  3670. err = ret;
  3671. break;
  3672. }
  3673. rc->extents_found--;
  3674. rc->search_start = key.objectid;
  3675. }
  3676. }
  3677. btrfs_end_transaction_throttle(trans, rc->extent_root);
  3678. btrfs_btree_balance_dirty(rc->extent_root);
  3679. trans = NULL;
  3680. if (rc->stage == MOVE_DATA_EXTENTS &&
  3681. (flags & BTRFS_EXTENT_FLAG_DATA)) {
  3682. rc->found_file_extent = 1;
  3683. ret = relocate_data_extent(rc->data_inode,
  3684. &key, &rc->cluster);
  3685. if (ret < 0) {
  3686. err = ret;
  3687. break;
  3688. }
  3689. }
  3690. }
  3691. if (trans && progress && err == -ENOSPC) {
  3692. ret = btrfs_force_chunk_alloc(trans, rc->extent_root,
  3693. rc->block_group->flags);
  3694. if (ret == 1) {
  3695. err = 0;
  3696. progress = 0;
  3697. goto restart;
  3698. }
  3699. }
  3700. btrfs_release_path(path);
  3701. clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
  3702. if (trans) {
  3703. btrfs_end_transaction_throttle(trans, rc->extent_root);
  3704. btrfs_btree_balance_dirty(rc->extent_root);
  3705. }
  3706. if (!err) {
  3707. ret = relocate_file_extent_cluster(rc->data_inode,
  3708. &rc->cluster);
  3709. if (ret < 0)
  3710. err = ret;
  3711. }
  3712. rc->create_reloc_tree = 0;
  3713. set_reloc_control(rc);
  3714. backref_cache_cleanup(&rc->backref_cache);
  3715. btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
  3716. err = prepare_to_merge(rc, err);
  3717. merge_reloc_roots(rc);
  3718. rc->merge_reloc_tree = 0;
  3719. unset_reloc_control(rc);
  3720. btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
  3721. /* get rid of pinned extents */
  3722. trans = btrfs_join_transaction(rc->extent_root);
  3723. if (IS_ERR(trans)) {
  3724. err = PTR_ERR(trans);
  3725. goto out_free;
  3726. }
  3727. ret = qgroup_fix_relocated_data_extents(trans, rc);
  3728. if (ret < 0) {
  3729. btrfs_abort_transaction(trans, ret);
  3730. if (!err)
  3731. err = ret;
  3732. goto out_free;
  3733. }
  3734. btrfs_commit_transaction(trans, rc->extent_root);
  3735. out_free:
  3736. btrfs_free_block_rsv(rc->extent_root, rc->block_rsv);
  3737. btrfs_free_path(path);
  3738. return err;
  3739. }
  3740. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  3741. struct btrfs_root *root, u64 objectid)
  3742. {
  3743. struct btrfs_path *path;
  3744. struct btrfs_inode_item *item;
  3745. struct extent_buffer *leaf;
  3746. int ret;
  3747. path = btrfs_alloc_path();
  3748. if (!path)
  3749. return -ENOMEM;
  3750. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  3751. if (ret)
  3752. goto out;
  3753. leaf = path->nodes[0];
  3754. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  3755. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  3756. btrfs_set_inode_generation(leaf, item, 1);
  3757. btrfs_set_inode_size(leaf, item, 0);
  3758. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  3759. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
  3760. BTRFS_INODE_PREALLOC);
  3761. btrfs_mark_buffer_dirty(leaf);
  3762. out:
  3763. btrfs_free_path(path);
  3764. return ret;
  3765. }
  3766. /*
  3767. * helper to create inode for data relocation.
  3768. * the inode is in data relocation tree and its link count is 0
  3769. */
  3770. static noinline_for_stack
  3771. struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
  3772. struct btrfs_block_group_cache *group)
  3773. {
  3774. struct inode *inode = NULL;
  3775. struct btrfs_trans_handle *trans;
  3776. struct btrfs_root *root;
  3777. struct btrfs_key key;
  3778. u64 objectid;
  3779. int err = 0;
  3780. root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
  3781. if (IS_ERR(root))
  3782. return ERR_CAST(root);
  3783. trans = btrfs_start_transaction(root, 6);
  3784. if (IS_ERR(trans))
  3785. return ERR_CAST(trans);
  3786. err = btrfs_find_free_objectid(root, &objectid);
  3787. if (err)
  3788. goto out;
  3789. err = __insert_orphan_inode(trans, root, objectid);
  3790. BUG_ON(err);
  3791. key.objectid = objectid;
  3792. key.type = BTRFS_INODE_ITEM_KEY;
  3793. key.offset = 0;
  3794. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  3795. BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
  3796. BTRFS_I(inode)->index_cnt = group->key.objectid;
  3797. err = btrfs_orphan_add(trans, inode);
  3798. out:
  3799. btrfs_end_transaction(trans, root);
  3800. btrfs_btree_balance_dirty(root);
  3801. if (err) {
  3802. if (inode)
  3803. iput(inode);
  3804. inode = ERR_PTR(err);
  3805. }
  3806. return inode;
  3807. }
  3808. static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
  3809. {
  3810. struct reloc_control *rc;
  3811. rc = kzalloc(sizeof(*rc), GFP_NOFS);
  3812. if (!rc)
  3813. return NULL;
  3814. INIT_LIST_HEAD(&rc->reloc_roots);
  3815. backref_cache_init(&rc->backref_cache);
  3816. mapping_tree_init(&rc->reloc_root_tree);
  3817. extent_io_tree_init(&rc->processed_blocks,
  3818. fs_info->btree_inode->i_mapping);
  3819. return rc;
  3820. }
  3821. /*
  3822. * function to relocate all extents in a block group.
  3823. */
  3824. int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
  3825. {
  3826. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  3827. struct reloc_control *rc;
  3828. struct inode *inode;
  3829. struct btrfs_path *path;
  3830. int ret;
  3831. int rw = 0;
  3832. int err = 0;
  3833. rc = alloc_reloc_control(fs_info);
  3834. if (!rc)
  3835. return -ENOMEM;
  3836. rc->extent_root = extent_root;
  3837. rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
  3838. BUG_ON(!rc->block_group);
  3839. ret = btrfs_inc_block_group_ro(extent_root, rc->block_group);
  3840. if (ret) {
  3841. err = ret;
  3842. goto out;
  3843. }
  3844. rw = 1;
  3845. path = btrfs_alloc_path();
  3846. if (!path) {
  3847. err = -ENOMEM;
  3848. goto out;
  3849. }
  3850. inode = lookup_free_space_inode(fs_info->tree_root, rc->block_group,
  3851. path);
  3852. btrfs_free_path(path);
  3853. if (!IS_ERR(inode))
  3854. ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
  3855. else
  3856. ret = PTR_ERR(inode);
  3857. if (ret && ret != -ENOENT) {
  3858. err = ret;
  3859. goto out;
  3860. }
  3861. rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
  3862. if (IS_ERR(rc->data_inode)) {
  3863. err = PTR_ERR(rc->data_inode);
  3864. rc->data_inode = NULL;
  3865. goto out;
  3866. }
  3867. btrfs_info(extent_root->fs_info,
  3868. "relocating block group %llu flags %llu",
  3869. rc->block_group->key.objectid, rc->block_group->flags);
  3870. btrfs_wait_block_group_reservations(rc->block_group);
  3871. btrfs_wait_nocow_writers(rc->block_group);
  3872. btrfs_wait_ordered_roots(fs_info, -1,
  3873. rc->block_group->key.objectid,
  3874. rc->block_group->key.offset);
  3875. while (1) {
  3876. mutex_lock(&fs_info->cleaner_mutex);
  3877. ret = relocate_block_group(rc);
  3878. mutex_unlock(&fs_info->cleaner_mutex);
  3879. if (ret < 0) {
  3880. err = ret;
  3881. goto out;
  3882. }
  3883. if (rc->extents_found == 0)
  3884. break;
  3885. btrfs_info(extent_root->fs_info, "found %llu extents",
  3886. rc->extents_found);
  3887. if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
  3888. ret = btrfs_wait_ordered_range(rc->data_inode, 0,
  3889. (u64)-1);
  3890. if (ret) {
  3891. err = ret;
  3892. goto out;
  3893. }
  3894. invalidate_mapping_pages(rc->data_inode->i_mapping,
  3895. 0, -1);
  3896. rc->stage = UPDATE_DATA_PTRS;
  3897. }
  3898. }
  3899. WARN_ON(rc->block_group->pinned > 0);
  3900. WARN_ON(rc->block_group->reserved > 0);
  3901. WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
  3902. out:
  3903. if (err && rw)
  3904. btrfs_dec_block_group_ro(extent_root, rc->block_group);
  3905. iput(rc->data_inode);
  3906. btrfs_put_block_group(rc->block_group);
  3907. kfree(rc);
  3908. return err;
  3909. }
  3910. static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
  3911. {
  3912. struct btrfs_trans_handle *trans;
  3913. int ret, err;
  3914. trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
  3915. if (IS_ERR(trans))
  3916. return PTR_ERR(trans);
  3917. memset(&root->root_item.drop_progress, 0,
  3918. sizeof(root->root_item.drop_progress));
  3919. root->root_item.drop_level = 0;
  3920. btrfs_set_root_refs(&root->root_item, 0);
  3921. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3922. &root->root_key, &root->root_item);
  3923. err = btrfs_end_transaction(trans, root->fs_info->tree_root);
  3924. if (err)
  3925. return err;
  3926. return ret;
  3927. }
  3928. /*
  3929. * recover relocation interrupted by system crash.
  3930. *
  3931. * this function resumes merging reloc trees with corresponding fs trees.
  3932. * this is important for keeping the sharing of tree blocks
  3933. */
  3934. int btrfs_recover_relocation(struct btrfs_root *root)
  3935. {
  3936. LIST_HEAD(reloc_roots);
  3937. struct btrfs_key key;
  3938. struct btrfs_root *fs_root;
  3939. struct btrfs_root *reloc_root;
  3940. struct btrfs_path *path;
  3941. struct extent_buffer *leaf;
  3942. struct reloc_control *rc = NULL;
  3943. struct btrfs_trans_handle *trans;
  3944. int ret;
  3945. int err = 0;
  3946. path = btrfs_alloc_path();
  3947. if (!path)
  3948. return -ENOMEM;
  3949. path->reada = READA_BACK;
  3950. key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  3951. key.type = BTRFS_ROOT_ITEM_KEY;
  3952. key.offset = (u64)-1;
  3953. while (1) {
  3954. ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key,
  3955. path, 0, 0);
  3956. if (ret < 0) {
  3957. err = ret;
  3958. goto out;
  3959. }
  3960. if (ret > 0) {
  3961. if (path->slots[0] == 0)
  3962. break;
  3963. path->slots[0]--;
  3964. }
  3965. leaf = path->nodes[0];
  3966. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3967. btrfs_release_path(path);
  3968. if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
  3969. key.type != BTRFS_ROOT_ITEM_KEY)
  3970. break;
  3971. reloc_root = btrfs_read_fs_root(root, &key);
  3972. if (IS_ERR(reloc_root)) {
  3973. err = PTR_ERR(reloc_root);
  3974. goto out;
  3975. }
  3976. list_add(&reloc_root->root_list, &reloc_roots);
  3977. if (btrfs_root_refs(&reloc_root->root_item) > 0) {
  3978. fs_root = read_fs_root(root->fs_info,
  3979. reloc_root->root_key.offset);
  3980. if (IS_ERR(fs_root)) {
  3981. ret = PTR_ERR(fs_root);
  3982. if (ret != -ENOENT) {
  3983. err = ret;
  3984. goto out;
  3985. }
  3986. ret = mark_garbage_root(reloc_root);
  3987. if (ret < 0) {
  3988. err = ret;
  3989. goto out;
  3990. }
  3991. }
  3992. }
  3993. if (key.offset == 0)
  3994. break;
  3995. key.offset--;
  3996. }
  3997. btrfs_release_path(path);
  3998. if (list_empty(&reloc_roots))
  3999. goto out;
  4000. rc = alloc_reloc_control(root->fs_info);
  4001. if (!rc) {
  4002. err = -ENOMEM;
  4003. goto out;
  4004. }
  4005. rc->extent_root = root->fs_info->extent_root;
  4006. set_reloc_control(rc);
  4007. trans = btrfs_join_transaction(rc->extent_root);
  4008. if (IS_ERR(trans)) {
  4009. unset_reloc_control(rc);
  4010. err = PTR_ERR(trans);
  4011. goto out_free;
  4012. }
  4013. rc->merge_reloc_tree = 1;
  4014. while (!list_empty(&reloc_roots)) {
  4015. reloc_root = list_entry(reloc_roots.next,
  4016. struct btrfs_root, root_list);
  4017. list_del(&reloc_root->root_list);
  4018. if (btrfs_root_refs(&reloc_root->root_item) == 0) {
  4019. list_add_tail(&reloc_root->root_list,
  4020. &rc->reloc_roots);
  4021. continue;
  4022. }
  4023. fs_root = read_fs_root(root->fs_info,
  4024. reloc_root->root_key.offset);
  4025. if (IS_ERR(fs_root)) {
  4026. err = PTR_ERR(fs_root);
  4027. goto out_free;
  4028. }
  4029. err = __add_reloc_root(reloc_root);
  4030. BUG_ON(err < 0); /* -ENOMEM or logic error */
  4031. fs_root->reloc_root = reloc_root;
  4032. }
  4033. err = btrfs_commit_transaction(trans, rc->extent_root);
  4034. if (err)
  4035. goto out_free;
  4036. merge_reloc_roots(rc);
  4037. unset_reloc_control(rc);
  4038. trans = btrfs_join_transaction(rc->extent_root);
  4039. if (IS_ERR(trans)) {
  4040. err = PTR_ERR(trans);
  4041. goto out_free;
  4042. }
  4043. err = qgroup_fix_relocated_data_extents(trans, rc);
  4044. if (err < 0) {
  4045. btrfs_abort_transaction(trans, err);
  4046. goto out_free;
  4047. }
  4048. err = btrfs_commit_transaction(trans, rc->extent_root);
  4049. out_free:
  4050. kfree(rc);
  4051. out:
  4052. if (!list_empty(&reloc_roots))
  4053. free_reloc_roots(&reloc_roots);
  4054. btrfs_free_path(path);
  4055. if (err == 0) {
  4056. /* cleanup orphan inode in data relocation tree */
  4057. fs_root = read_fs_root(root->fs_info,
  4058. BTRFS_DATA_RELOC_TREE_OBJECTID);
  4059. if (IS_ERR(fs_root))
  4060. err = PTR_ERR(fs_root);
  4061. else
  4062. err = btrfs_orphan_cleanup(fs_root);
  4063. }
  4064. return err;
  4065. }
  4066. /*
  4067. * helper to add ordered checksum for data relocation.
  4068. *
  4069. * cloning checksum properly handles the nodatasum extents.
  4070. * it also saves CPU time to re-calculate the checksum.
  4071. */
  4072. int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
  4073. {
  4074. struct btrfs_ordered_sum *sums;
  4075. struct btrfs_ordered_extent *ordered;
  4076. struct btrfs_root *root = BTRFS_I(inode)->root;
  4077. int ret;
  4078. u64 disk_bytenr;
  4079. u64 new_bytenr;
  4080. LIST_HEAD(list);
  4081. ordered = btrfs_lookup_ordered_extent(inode, file_pos);
  4082. BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
  4083. disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
  4084. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
  4085. disk_bytenr + len - 1, &list, 0);
  4086. if (ret)
  4087. goto out;
  4088. while (!list_empty(&list)) {
  4089. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  4090. list_del_init(&sums->list);
  4091. /*
  4092. * We need to offset the new_bytenr based on where the csum is.
  4093. * We need to do this because we will read in entire prealloc
  4094. * extents but we may have written to say the middle of the
  4095. * prealloc extent, so we need to make sure the csum goes with
  4096. * the right disk offset.
  4097. *
  4098. * We can do this because the data reloc inode refers strictly
  4099. * to the on disk bytes, so we don't have to worry about
  4100. * disk_len vs real len like with real inodes since it's all
  4101. * disk length.
  4102. */
  4103. new_bytenr = ordered->start + (sums->bytenr - disk_bytenr);
  4104. sums->bytenr = new_bytenr;
  4105. btrfs_add_ordered_sum(inode, ordered, sums);
  4106. }
  4107. out:
  4108. btrfs_put_ordered_extent(ordered);
  4109. return ret;
  4110. }
  4111. int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
  4112. struct btrfs_root *root, struct extent_buffer *buf,
  4113. struct extent_buffer *cow)
  4114. {
  4115. struct reloc_control *rc;
  4116. struct backref_node *node;
  4117. int first_cow = 0;
  4118. int level;
  4119. int ret = 0;
  4120. rc = root->fs_info->reloc_ctl;
  4121. if (!rc)
  4122. return 0;
  4123. BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
  4124. root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
  4125. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
  4126. if (buf == root->node)
  4127. __update_reloc_root(root, cow->start);
  4128. }
  4129. level = btrfs_header_level(buf);
  4130. if (btrfs_header_generation(buf) <=
  4131. btrfs_root_last_snapshot(&root->root_item))
  4132. first_cow = 1;
  4133. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
  4134. rc->create_reloc_tree) {
  4135. WARN_ON(!first_cow && level == 0);
  4136. node = rc->backref_cache.path[level];
  4137. BUG_ON(node->bytenr != buf->start &&
  4138. node->new_bytenr != buf->start);
  4139. drop_node_buffer(node);
  4140. extent_buffer_get(cow);
  4141. node->eb = cow;
  4142. node->new_bytenr = cow->start;
  4143. if (!node->pending) {
  4144. list_move_tail(&node->list,
  4145. &rc->backref_cache.pending[level]);
  4146. node->pending = 1;
  4147. }
  4148. if (first_cow)
  4149. __mark_block_processed(rc, node);
  4150. if (first_cow && level > 0)
  4151. rc->nodes_relocated += buf->len;
  4152. }
  4153. if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
  4154. ret = replace_file_extents(trans, rc, root, cow);
  4155. return ret;
  4156. }
  4157. /*
  4158. * called before creating snapshot. it calculates metadata reservation
  4159. * required for relocating tree blocks in the snapshot
  4160. */
  4161. void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
  4162. u64 *bytes_to_reserve)
  4163. {
  4164. struct btrfs_root *root;
  4165. struct reloc_control *rc;
  4166. root = pending->root;
  4167. if (!root->reloc_root)
  4168. return;
  4169. rc = root->fs_info->reloc_ctl;
  4170. if (!rc->merge_reloc_tree)
  4171. return;
  4172. root = root->reloc_root;
  4173. BUG_ON(btrfs_root_refs(&root->root_item) == 0);
  4174. /*
  4175. * relocation is in the stage of merging trees. the space
  4176. * used by merging a reloc tree is twice the size of
  4177. * relocated tree nodes in the worst case. half for cowing
  4178. * the reloc tree, half for cowing the fs tree. the space
  4179. * used by cowing the reloc tree will be freed after the
  4180. * tree is dropped. if we create snapshot, cowing the fs
  4181. * tree may use more space than it frees. so we need
  4182. * reserve extra space.
  4183. */
  4184. *bytes_to_reserve += rc->nodes_relocated;
  4185. }
  4186. /*
  4187. * called after snapshot is created. migrate block reservation
  4188. * and create reloc root for the newly created snapshot
  4189. */
  4190. int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
  4191. struct btrfs_pending_snapshot *pending)
  4192. {
  4193. struct btrfs_root *root = pending->root;
  4194. struct btrfs_root *reloc_root;
  4195. struct btrfs_root *new_root;
  4196. struct reloc_control *rc;
  4197. int ret;
  4198. if (!root->reloc_root)
  4199. return 0;
  4200. rc = root->fs_info->reloc_ctl;
  4201. rc->merging_rsv_size += rc->nodes_relocated;
  4202. if (rc->merge_reloc_tree) {
  4203. ret = btrfs_block_rsv_migrate(&pending->block_rsv,
  4204. rc->block_rsv,
  4205. rc->nodes_relocated, 1);
  4206. if (ret)
  4207. return ret;
  4208. }
  4209. new_root = pending->snap;
  4210. reloc_root = create_reloc_root(trans, root->reloc_root,
  4211. new_root->root_key.objectid);
  4212. if (IS_ERR(reloc_root))
  4213. return PTR_ERR(reloc_root);
  4214. ret = __add_reloc_root(reloc_root);
  4215. BUG_ON(ret < 0);
  4216. new_root->reloc_root = reloc_root;
  4217. if (rc->create_reloc_tree)
  4218. ret = clone_backref_node(trans, rc, root, reloc_root);
  4219. return ret;
  4220. }