relocation.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2009 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/writeback.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/rbtree.h>
  10. #include <linux/slab.h>
  11. #include <linux/error-injection.h>
  12. #include "ctree.h"
  13. #include "disk-io.h"
  14. #include "transaction.h"
  15. #include "volumes.h"
  16. #include "locking.h"
  17. #include "btrfs_inode.h"
  18. #include "async-thread.h"
  19. #include "free-space-cache.h"
  20. #include "inode-map.h"
  21. #include "qgroup.h"
  22. #include "print-tree.h"
  23. #include "delalloc-space.h"
  24. #include "block-group.h"
  25. #include "backref.h"
  26. #include "misc.h"
  27. /*
  28. * Relocation overview
  29. *
  30. * [What does relocation do]
  31. *
  32. * The objective of relocation is to relocate all extents of the target block
  33. * group to other block groups.
  34. * This is utilized by resize (shrink only), profile converting, compacting
  35. * space, or balance routine to spread chunks over devices.
  36. *
  37. * Before | After
  38. * ------------------------------------------------------------------
  39. * BG A: 10 data extents | BG A: deleted
  40. * BG B: 2 data extents | BG B: 10 data extents (2 old + 8 relocated)
  41. * BG C: 1 extents | BG C: 3 data extents (1 old + 2 relocated)
  42. *
  43. * [How does relocation work]
  44. *
  45. * 1. Mark the target block group read-only
  46. * New extents won't be allocated from the target block group.
  47. *
  48. * 2.1 Record each extent in the target block group
  49. * To build a proper map of extents to be relocated.
  50. *
  51. * 2.2 Build data reloc tree and reloc trees
  52. * Data reloc tree will contain an inode, recording all newly relocated
  53. * data extents.
  54. * There will be only one data reloc tree for one data block group.
  55. *
  56. * Reloc tree will be a special snapshot of its source tree, containing
  57. * relocated tree blocks.
  58. * Each tree referring to a tree block in target block group will get its
  59. * reloc tree built.
  60. *
  61. * 2.3 Swap source tree with its corresponding reloc tree
  62. * Each involved tree only refers to new extents after swap.
  63. *
  64. * 3. Cleanup reloc trees and data reloc tree.
  65. * As old extents in the target block group are still referenced by reloc
  66. * trees, we need to clean them up before really freeing the target block
  67. * group.
  68. *
  69. * The main complexity is in steps 2.2 and 2.3.
  70. *
  71. * The entry point of relocation is relocate_block_group() function.
  72. */
  73. #define RELOCATION_RESERVED_NODES 256
  74. /*
  75. * map address of tree root to tree
  76. */
  77. struct mapping_node {
  78. struct {
  79. struct rb_node rb_node;
  80. u64 bytenr;
  81. }; /* Use rb_simle_node for search/insert */
  82. void *data;
  83. };
  84. struct mapping_tree {
  85. struct rb_root rb_root;
  86. spinlock_t lock;
  87. };
  88. /*
  89. * present a tree block to process
  90. */
  91. struct tree_block {
  92. struct {
  93. struct rb_node rb_node;
  94. u64 bytenr;
  95. }; /* Use rb_simple_node for search/insert */
  96. struct btrfs_key key;
  97. unsigned int level:8;
  98. unsigned int key_ready:1;
  99. };
  100. #define MAX_EXTENTS 128
  101. struct file_extent_cluster {
  102. u64 start;
  103. u64 end;
  104. u64 boundary[MAX_EXTENTS];
  105. unsigned int nr;
  106. };
  107. struct reloc_control {
  108. /* block group to relocate */
  109. struct btrfs_block_group *block_group;
  110. /* extent tree */
  111. struct btrfs_root *extent_root;
  112. /* inode for moving data */
  113. struct inode *data_inode;
  114. struct btrfs_block_rsv *block_rsv;
  115. struct btrfs_backref_cache backref_cache;
  116. struct file_extent_cluster cluster;
  117. /* tree blocks have been processed */
  118. struct extent_io_tree processed_blocks;
  119. /* map start of tree root to corresponding reloc tree */
  120. struct mapping_tree reloc_root_tree;
  121. /* list of reloc trees */
  122. struct list_head reloc_roots;
  123. /* list of subvolume trees that get relocated */
  124. struct list_head dirty_subvol_roots;
  125. /* size of metadata reservation for merging reloc trees */
  126. u64 merging_rsv_size;
  127. /* size of relocated tree nodes */
  128. u64 nodes_relocated;
  129. /* reserved size for block group relocation*/
  130. u64 reserved_bytes;
  131. u64 search_start;
  132. u64 extents_found;
  133. unsigned int stage:8;
  134. unsigned int create_reloc_tree:1;
  135. unsigned int merge_reloc_tree:1;
  136. unsigned int found_file_extent:1;
  137. };
  138. /* stages of data relocation */
  139. #define MOVE_DATA_EXTENTS 0
  140. #define UPDATE_DATA_PTRS 1
  141. static void mark_block_processed(struct reloc_control *rc,
  142. struct btrfs_backref_node *node)
  143. {
  144. u32 blocksize;
  145. if (node->level == 0 ||
  146. in_range(node->bytenr, rc->block_group->start,
  147. rc->block_group->length)) {
  148. blocksize = rc->extent_root->fs_info->nodesize;
  149. set_extent_bits(&rc->processed_blocks, node->bytenr,
  150. node->bytenr + blocksize - 1, EXTENT_DIRTY);
  151. }
  152. node->processed = 1;
  153. }
  154. static void mapping_tree_init(struct mapping_tree *tree)
  155. {
  156. tree->rb_root = RB_ROOT;
  157. spin_lock_init(&tree->lock);
  158. }
  159. /*
  160. * walk up backref nodes until reach node presents tree root
  161. */
  162. static struct btrfs_backref_node *walk_up_backref(
  163. struct btrfs_backref_node *node,
  164. struct btrfs_backref_edge *edges[], int *index)
  165. {
  166. struct btrfs_backref_edge *edge;
  167. int idx = *index;
  168. while (!list_empty(&node->upper)) {
  169. edge = list_entry(node->upper.next,
  170. struct btrfs_backref_edge, list[LOWER]);
  171. edges[idx++] = edge;
  172. node = edge->node[UPPER];
  173. }
  174. BUG_ON(node->detached);
  175. *index = idx;
  176. return node;
  177. }
  178. /*
  179. * walk down backref nodes to find start of next reference path
  180. */
  181. static struct btrfs_backref_node *walk_down_backref(
  182. struct btrfs_backref_edge *edges[], int *index)
  183. {
  184. struct btrfs_backref_edge *edge;
  185. struct btrfs_backref_node *lower;
  186. int idx = *index;
  187. while (idx > 0) {
  188. edge = edges[idx - 1];
  189. lower = edge->node[LOWER];
  190. if (list_is_last(&edge->list[LOWER], &lower->upper)) {
  191. idx--;
  192. continue;
  193. }
  194. edge = list_entry(edge->list[LOWER].next,
  195. struct btrfs_backref_edge, list[LOWER]);
  196. edges[idx - 1] = edge;
  197. *index = idx;
  198. return edge->node[UPPER];
  199. }
  200. *index = 0;
  201. return NULL;
  202. }
  203. static void update_backref_node(struct btrfs_backref_cache *cache,
  204. struct btrfs_backref_node *node, u64 bytenr)
  205. {
  206. struct rb_node *rb_node;
  207. rb_erase(&node->rb_node, &cache->rb_root);
  208. node->bytenr = bytenr;
  209. rb_node = rb_simple_insert(&cache->rb_root, node->bytenr, &node->rb_node);
  210. if (rb_node)
  211. btrfs_backref_panic(cache->fs_info, bytenr, -EEXIST);
  212. }
  213. /*
  214. * update backref cache after a transaction commit
  215. */
  216. static int update_backref_cache(struct btrfs_trans_handle *trans,
  217. struct btrfs_backref_cache *cache)
  218. {
  219. struct btrfs_backref_node *node;
  220. int level = 0;
  221. if (cache->last_trans == 0) {
  222. cache->last_trans = trans->transid;
  223. return 0;
  224. }
  225. if (cache->last_trans == trans->transid)
  226. return 0;
  227. /*
  228. * detached nodes are used to avoid unnecessary backref
  229. * lookup. transaction commit changes the extent tree.
  230. * so the detached nodes are no longer useful.
  231. */
  232. while (!list_empty(&cache->detached)) {
  233. node = list_entry(cache->detached.next,
  234. struct btrfs_backref_node, list);
  235. btrfs_backref_cleanup_node(cache, node);
  236. }
  237. while (!list_empty(&cache->changed)) {
  238. node = list_entry(cache->changed.next,
  239. struct btrfs_backref_node, list);
  240. list_del_init(&node->list);
  241. BUG_ON(node->pending);
  242. update_backref_node(cache, node, node->new_bytenr);
  243. }
  244. /*
  245. * some nodes can be left in the pending list if there were
  246. * errors during processing the pending nodes.
  247. */
  248. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  249. list_for_each_entry(node, &cache->pending[level], list) {
  250. BUG_ON(!node->pending);
  251. if (node->bytenr == node->new_bytenr)
  252. continue;
  253. update_backref_node(cache, node, node->new_bytenr);
  254. }
  255. }
  256. cache->last_trans = 0;
  257. return 1;
  258. }
  259. static bool reloc_root_is_dead(struct btrfs_root *root)
  260. {
  261. /*
  262. * Pair with set_bit/clear_bit in clean_dirty_subvols and
  263. * btrfs_update_reloc_root. We need to see the updated bit before
  264. * trying to access reloc_root
  265. */
  266. smp_rmb();
  267. if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
  268. return true;
  269. return false;
  270. }
  271. /*
  272. * Check if this subvolume tree has valid reloc tree.
  273. *
  274. * Reloc tree after swap is considered dead, thus not considered as valid.
  275. * This is enough for most callers, as they don't distinguish dead reloc root
  276. * from no reloc root. But btrfs_should_ignore_reloc_root() below is a
  277. * special case.
  278. */
  279. static bool have_reloc_root(struct btrfs_root *root)
  280. {
  281. if (reloc_root_is_dead(root))
  282. return false;
  283. if (!root->reloc_root)
  284. return false;
  285. return true;
  286. }
  287. int btrfs_should_ignore_reloc_root(struct btrfs_root *root)
  288. {
  289. struct btrfs_root *reloc_root;
  290. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  291. return 0;
  292. /* This root has been merged with its reloc tree, we can ignore it */
  293. if (reloc_root_is_dead(root))
  294. return 1;
  295. reloc_root = root->reloc_root;
  296. if (!reloc_root)
  297. return 0;
  298. if (btrfs_header_generation(reloc_root->commit_root) ==
  299. root->fs_info->running_transaction->transid)
  300. return 0;
  301. /*
  302. * if there is reloc tree and it was created in previous
  303. * transaction backref lookup can find the reloc tree,
  304. * so backref node for the fs tree root is useless for
  305. * relocation.
  306. */
  307. return 1;
  308. }
  309. /*
  310. * find reloc tree by address of tree root
  311. */
  312. struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr)
  313. {
  314. struct reloc_control *rc = fs_info->reloc_ctl;
  315. struct rb_node *rb_node;
  316. struct mapping_node *node;
  317. struct btrfs_root *root = NULL;
  318. ASSERT(rc);
  319. spin_lock(&rc->reloc_root_tree.lock);
  320. rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, bytenr);
  321. if (rb_node) {
  322. node = rb_entry(rb_node, struct mapping_node, rb_node);
  323. root = (struct btrfs_root *)node->data;
  324. }
  325. spin_unlock(&rc->reloc_root_tree.lock);
  326. return btrfs_grab_root(root);
  327. }
  328. /*
  329. * For useless nodes, do two major clean ups:
  330. *
  331. * - Cleanup the children edges and nodes
  332. * If child node is also orphan (no parent) during cleanup, then the child
  333. * node will also be cleaned up.
  334. *
  335. * - Freeing up leaves (level 0), keeps nodes detached
  336. * For nodes, the node is still cached as "detached"
  337. *
  338. * Return false if @node is not in the @useless_nodes list.
  339. * Return true if @node is in the @useless_nodes list.
  340. */
  341. static bool handle_useless_nodes(struct reloc_control *rc,
  342. struct btrfs_backref_node *node)
  343. {
  344. struct btrfs_backref_cache *cache = &rc->backref_cache;
  345. struct list_head *useless_node = &cache->useless_node;
  346. bool ret = false;
  347. while (!list_empty(useless_node)) {
  348. struct btrfs_backref_node *cur;
  349. cur = list_first_entry(useless_node, struct btrfs_backref_node,
  350. list);
  351. list_del_init(&cur->list);
  352. /* Only tree root nodes can be added to @useless_nodes */
  353. ASSERT(list_empty(&cur->upper));
  354. if (cur == node)
  355. ret = true;
  356. /* The node is the lowest node */
  357. if (cur->lowest) {
  358. list_del_init(&cur->lower);
  359. cur->lowest = 0;
  360. }
  361. /* Cleanup the lower edges */
  362. while (!list_empty(&cur->lower)) {
  363. struct btrfs_backref_edge *edge;
  364. struct btrfs_backref_node *lower;
  365. edge = list_entry(cur->lower.next,
  366. struct btrfs_backref_edge, list[UPPER]);
  367. list_del(&edge->list[UPPER]);
  368. list_del(&edge->list[LOWER]);
  369. lower = edge->node[LOWER];
  370. btrfs_backref_free_edge(cache, edge);
  371. /* Child node is also orphan, queue for cleanup */
  372. if (list_empty(&lower->upper))
  373. list_add(&lower->list, useless_node);
  374. }
  375. /* Mark this block processed for relocation */
  376. mark_block_processed(rc, cur);
  377. /*
  378. * Backref nodes for tree leaves are deleted from the cache.
  379. * Backref nodes for upper level tree blocks are left in the
  380. * cache to avoid unnecessary backref lookup.
  381. */
  382. if (cur->level > 0) {
  383. list_add(&cur->list, &cache->detached);
  384. cur->detached = 1;
  385. } else {
  386. rb_erase(&cur->rb_node, &cache->rb_root);
  387. btrfs_backref_free_node(cache, cur);
  388. }
  389. }
  390. return ret;
  391. }
  392. /*
  393. * Build backref tree for a given tree block. Root of the backref tree
  394. * corresponds the tree block, leaves of the backref tree correspond roots of
  395. * b-trees that reference the tree block.
  396. *
  397. * The basic idea of this function is check backrefs of a given block to find
  398. * upper level blocks that reference the block, and then check backrefs of
  399. * these upper level blocks recursively. The recursion stops when tree root is
  400. * reached or backrefs for the block is cached.
  401. *
  402. * NOTE: if we find that backrefs for a block are cached, we know backrefs for
  403. * all upper level blocks that directly/indirectly reference the block are also
  404. * cached.
  405. */
  406. static noinline_for_stack struct btrfs_backref_node *build_backref_tree(
  407. struct reloc_control *rc, struct btrfs_key *node_key,
  408. int level, u64 bytenr)
  409. {
  410. struct btrfs_backref_iter *iter;
  411. struct btrfs_backref_cache *cache = &rc->backref_cache;
  412. /* For searching parent of TREE_BLOCK_REF */
  413. struct btrfs_path *path;
  414. struct btrfs_backref_node *cur;
  415. struct btrfs_backref_node *node = NULL;
  416. struct btrfs_backref_edge *edge;
  417. int ret;
  418. int err = 0;
  419. iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info, GFP_NOFS);
  420. if (!iter)
  421. return ERR_PTR(-ENOMEM);
  422. path = btrfs_alloc_path();
  423. if (!path) {
  424. err = -ENOMEM;
  425. goto out;
  426. }
  427. node = btrfs_backref_alloc_node(cache, bytenr, level);
  428. if (!node) {
  429. err = -ENOMEM;
  430. goto out;
  431. }
  432. node->lowest = 1;
  433. cur = node;
  434. /* Breadth-first search to build backref cache */
  435. do {
  436. ret = btrfs_backref_add_tree_node(cache, path, iter, node_key,
  437. cur);
  438. if (ret < 0) {
  439. err = ret;
  440. goto out;
  441. }
  442. edge = list_first_entry_or_null(&cache->pending_edge,
  443. struct btrfs_backref_edge, list[UPPER]);
  444. /*
  445. * The pending list isn't empty, take the first block to
  446. * process
  447. */
  448. if (edge) {
  449. list_del_init(&edge->list[UPPER]);
  450. cur = edge->node[UPPER];
  451. }
  452. } while (edge);
  453. /* Finish the upper linkage of newly added edges/nodes */
  454. ret = btrfs_backref_finish_upper_links(cache, node);
  455. if (ret < 0) {
  456. err = ret;
  457. goto out;
  458. }
  459. if (handle_useless_nodes(rc, node))
  460. node = NULL;
  461. out:
  462. btrfs_backref_iter_free(iter);
  463. btrfs_free_path(path);
  464. if (err) {
  465. btrfs_backref_error_cleanup(cache, node);
  466. return ERR_PTR(err);
  467. }
  468. ASSERT(!node || !node->detached);
  469. ASSERT(list_empty(&cache->useless_node) &&
  470. list_empty(&cache->pending_edge));
  471. return node;
  472. }
  473. /*
  474. * helper to add backref node for the newly created snapshot.
  475. * the backref node is created by cloning backref node that
  476. * corresponds to root of source tree
  477. */
  478. static int clone_backref_node(struct btrfs_trans_handle *trans,
  479. struct reloc_control *rc,
  480. struct btrfs_root *src,
  481. struct btrfs_root *dest)
  482. {
  483. struct btrfs_root *reloc_root = src->reloc_root;
  484. struct btrfs_backref_cache *cache = &rc->backref_cache;
  485. struct btrfs_backref_node *node = NULL;
  486. struct btrfs_backref_node *new_node;
  487. struct btrfs_backref_edge *edge;
  488. struct btrfs_backref_edge *new_edge;
  489. struct rb_node *rb_node;
  490. if (cache->last_trans > 0)
  491. update_backref_cache(trans, cache);
  492. rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start);
  493. if (rb_node) {
  494. node = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
  495. if (node->detached)
  496. node = NULL;
  497. else
  498. BUG_ON(node->new_bytenr != reloc_root->node->start);
  499. }
  500. if (!node) {
  501. rb_node = rb_simple_search(&cache->rb_root,
  502. reloc_root->commit_root->start);
  503. if (rb_node) {
  504. node = rb_entry(rb_node, struct btrfs_backref_node,
  505. rb_node);
  506. BUG_ON(node->detached);
  507. }
  508. }
  509. if (!node)
  510. return 0;
  511. new_node = btrfs_backref_alloc_node(cache, dest->node->start,
  512. node->level);
  513. if (!new_node)
  514. return -ENOMEM;
  515. new_node->lowest = node->lowest;
  516. new_node->checked = 1;
  517. new_node->root = btrfs_grab_root(dest);
  518. ASSERT(new_node->root);
  519. if (!node->lowest) {
  520. list_for_each_entry(edge, &node->lower, list[UPPER]) {
  521. new_edge = btrfs_backref_alloc_edge(cache);
  522. if (!new_edge)
  523. goto fail;
  524. btrfs_backref_link_edge(new_edge, edge->node[LOWER],
  525. new_node, LINK_UPPER);
  526. }
  527. } else {
  528. list_add_tail(&new_node->lower, &cache->leaves);
  529. }
  530. rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr,
  531. &new_node->rb_node);
  532. if (rb_node)
  533. btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST);
  534. if (!new_node->lowest) {
  535. list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
  536. list_add_tail(&new_edge->list[LOWER],
  537. &new_edge->node[LOWER]->upper);
  538. }
  539. }
  540. return 0;
  541. fail:
  542. while (!list_empty(&new_node->lower)) {
  543. new_edge = list_entry(new_node->lower.next,
  544. struct btrfs_backref_edge, list[UPPER]);
  545. list_del(&new_edge->list[UPPER]);
  546. btrfs_backref_free_edge(cache, new_edge);
  547. }
  548. btrfs_backref_free_node(cache, new_node);
  549. return -ENOMEM;
  550. }
  551. /*
  552. * helper to add 'address of tree root -> reloc tree' mapping
  553. */
  554. static int __must_check __add_reloc_root(struct btrfs_root *root)
  555. {
  556. struct btrfs_fs_info *fs_info = root->fs_info;
  557. struct rb_node *rb_node;
  558. struct mapping_node *node;
  559. struct reloc_control *rc = fs_info->reloc_ctl;
  560. node = kmalloc(sizeof(*node), GFP_NOFS);
  561. if (!node)
  562. return -ENOMEM;
  563. node->bytenr = root->commit_root->start;
  564. node->data = root;
  565. spin_lock(&rc->reloc_root_tree.lock);
  566. rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
  567. node->bytenr, &node->rb_node);
  568. spin_unlock(&rc->reloc_root_tree.lock);
  569. if (rb_node) {
  570. btrfs_panic(fs_info, -EEXIST,
  571. "Duplicate root found for start=%llu while inserting into relocation tree",
  572. node->bytenr);
  573. }
  574. list_add_tail(&root->root_list, &rc->reloc_roots);
  575. return 0;
  576. }
  577. /*
  578. * helper to delete the 'address of tree root -> reloc tree'
  579. * mapping
  580. */
  581. static void __del_reloc_root(struct btrfs_root *root)
  582. {
  583. struct btrfs_fs_info *fs_info = root->fs_info;
  584. struct rb_node *rb_node;
  585. struct mapping_node *node = NULL;
  586. struct reloc_control *rc = fs_info->reloc_ctl;
  587. bool put_ref = false;
  588. if (rc && root->node) {
  589. spin_lock(&rc->reloc_root_tree.lock);
  590. rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
  591. root->commit_root->start);
  592. if (rb_node) {
  593. node = rb_entry(rb_node, struct mapping_node, rb_node);
  594. rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
  595. RB_CLEAR_NODE(&node->rb_node);
  596. }
  597. spin_unlock(&rc->reloc_root_tree.lock);
  598. ASSERT(!node || (struct btrfs_root *)node->data == root);
  599. }
  600. /*
  601. * We only put the reloc root here if it's on the list. There's a lot
  602. * of places where the pattern is to splice the rc->reloc_roots, process
  603. * the reloc roots, and then add the reloc root back onto
  604. * rc->reloc_roots. If we call __del_reloc_root while it's off of the
  605. * list we don't want the reference being dropped, because the guy
  606. * messing with the list is in charge of the reference.
  607. */
  608. spin_lock(&fs_info->trans_lock);
  609. if (!list_empty(&root->root_list)) {
  610. put_ref = true;
  611. list_del_init(&root->root_list);
  612. }
  613. spin_unlock(&fs_info->trans_lock);
  614. if (put_ref)
  615. btrfs_put_root(root);
  616. kfree(node);
  617. }
  618. /*
  619. * helper to update the 'address of tree root -> reloc tree'
  620. * mapping
  621. */
  622. static int __update_reloc_root(struct btrfs_root *root)
  623. {
  624. struct btrfs_fs_info *fs_info = root->fs_info;
  625. struct rb_node *rb_node;
  626. struct mapping_node *node = NULL;
  627. struct reloc_control *rc = fs_info->reloc_ctl;
  628. spin_lock(&rc->reloc_root_tree.lock);
  629. rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
  630. root->commit_root->start);
  631. if (rb_node) {
  632. node = rb_entry(rb_node, struct mapping_node, rb_node);
  633. rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
  634. }
  635. spin_unlock(&rc->reloc_root_tree.lock);
  636. if (!node)
  637. return 0;
  638. BUG_ON((struct btrfs_root *)node->data != root);
  639. spin_lock(&rc->reloc_root_tree.lock);
  640. node->bytenr = root->node->start;
  641. rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
  642. node->bytenr, &node->rb_node);
  643. spin_unlock(&rc->reloc_root_tree.lock);
  644. if (rb_node)
  645. btrfs_backref_panic(fs_info, node->bytenr, -EEXIST);
  646. return 0;
  647. }
  648. static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
  649. struct btrfs_root *root, u64 objectid)
  650. {
  651. struct btrfs_fs_info *fs_info = root->fs_info;
  652. struct btrfs_root *reloc_root;
  653. struct extent_buffer *eb;
  654. struct btrfs_root_item *root_item;
  655. struct btrfs_key root_key;
  656. int ret = 0;
  657. bool must_abort = false;
  658. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  659. if (!root_item)
  660. return ERR_PTR(-ENOMEM);
  661. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  662. root_key.type = BTRFS_ROOT_ITEM_KEY;
  663. root_key.offset = objectid;
  664. if (root->root_key.objectid == objectid) {
  665. u64 commit_root_gen;
  666. /* called by btrfs_init_reloc_root */
  667. ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
  668. BTRFS_TREE_RELOC_OBJECTID);
  669. if (ret)
  670. goto fail;
  671. /*
  672. * Set the last_snapshot field to the generation of the commit
  673. * root - like this ctree.c:btrfs_block_can_be_shared() behaves
  674. * correctly (returns true) when the relocation root is created
  675. * either inside the critical section of a transaction commit
  676. * (through transaction.c:qgroup_account_snapshot()) and when
  677. * it's created before the transaction commit is started.
  678. */
  679. commit_root_gen = btrfs_header_generation(root->commit_root);
  680. btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
  681. } else {
  682. /*
  683. * called by btrfs_reloc_post_snapshot_hook.
  684. * the source tree is a reloc tree, all tree blocks
  685. * modified after it was created have RELOC flag
  686. * set in their headers. so it's OK to not update
  687. * the 'last_snapshot'.
  688. */
  689. ret = btrfs_copy_root(trans, root, root->node, &eb,
  690. BTRFS_TREE_RELOC_OBJECTID);
  691. if (ret)
  692. goto fail;
  693. }
  694. /*
  695. * We have changed references at this point, we must abort the
  696. * transaction if anything fails.
  697. */
  698. must_abort = true;
  699. memcpy(root_item, &root->root_item, sizeof(*root_item));
  700. btrfs_set_root_bytenr(root_item, eb->start);
  701. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  702. btrfs_set_root_generation(root_item, trans->transid);
  703. if (root->root_key.objectid == objectid) {
  704. btrfs_set_root_refs(root_item, 0);
  705. memset(&root_item->drop_progress, 0,
  706. sizeof(struct btrfs_disk_key));
  707. root_item->drop_level = 0;
  708. }
  709. btrfs_tree_unlock(eb);
  710. free_extent_buffer(eb);
  711. ret = btrfs_insert_root(trans, fs_info->tree_root,
  712. &root_key, root_item);
  713. if (ret)
  714. goto fail;
  715. kfree(root_item);
  716. reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);
  717. if (IS_ERR(reloc_root)) {
  718. ret = PTR_ERR(reloc_root);
  719. goto abort;
  720. }
  721. set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
  722. reloc_root->last_trans = trans->transid;
  723. return reloc_root;
  724. fail:
  725. kfree(root_item);
  726. abort:
  727. if (must_abort)
  728. btrfs_abort_transaction(trans, ret);
  729. return ERR_PTR(ret);
  730. }
  731. /*
  732. * create reloc tree for a given fs tree. reloc tree is just a
  733. * snapshot of the fs tree with special root objectid.
  734. *
  735. * The reloc_root comes out of here with two references, one for
  736. * root->reloc_root, and another for being on the rc->reloc_roots list.
  737. */
  738. int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
  739. struct btrfs_root *root)
  740. {
  741. struct btrfs_fs_info *fs_info = root->fs_info;
  742. struct btrfs_root *reloc_root;
  743. struct reloc_control *rc = fs_info->reloc_ctl;
  744. struct btrfs_block_rsv *rsv;
  745. int clear_rsv = 0;
  746. int ret;
  747. if (!rc)
  748. return 0;
  749. /*
  750. * The subvolume has reloc tree but the swap is finished, no need to
  751. * create/update the dead reloc tree
  752. */
  753. if (reloc_root_is_dead(root))
  754. return 0;
  755. /*
  756. * This is subtle but important. We do not do
  757. * record_root_in_transaction for reloc roots, instead we record their
  758. * corresponding fs root, and then here we update the last trans for the
  759. * reloc root. This means that we have to do this for the entire life
  760. * of the reloc root, regardless of which stage of the relocation we are
  761. * in.
  762. */
  763. if (root->reloc_root) {
  764. reloc_root = root->reloc_root;
  765. reloc_root->last_trans = trans->transid;
  766. return 0;
  767. }
  768. /*
  769. * We are merging reloc roots, we do not need new reloc trees. Also
  770. * reloc trees never need their own reloc tree.
  771. */
  772. if (!rc->create_reloc_tree ||
  773. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  774. return 0;
  775. if (!trans->reloc_reserved) {
  776. rsv = trans->block_rsv;
  777. trans->block_rsv = rc->block_rsv;
  778. clear_rsv = 1;
  779. }
  780. reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
  781. if (clear_rsv)
  782. trans->block_rsv = rsv;
  783. ret = __add_reloc_root(reloc_root);
  784. BUG_ON(ret < 0);
  785. root->reloc_root = btrfs_grab_root(reloc_root);
  786. return 0;
  787. }
  788. /*
  789. * update root item of reloc tree
  790. */
  791. int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
  792. struct btrfs_root *root)
  793. {
  794. struct btrfs_fs_info *fs_info = root->fs_info;
  795. struct btrfs_root *reloc_root;
  796. struct btrfs_root_item *root_item;
  797. int ret;
  798. if (!have_reloc_root(root))
  799. return 0;
  800. reloc_root = root->reloc_root;
  801. root_item = &reloc_root->root_item;
  802. /*
  803. * We are probably ok here, but __del_reloc_root() will drop its ref of
  804. * the root. We have the ref for root->reloc_root, but just in case
  805. * hold it while we update the reloc root.
  806. */
  807. btrfs_grab_root(reloc_root);
  808. /* root->reloc_root will stay until current relocation finished */
  809. if (fs_info->reloc_ctl->merge_reloc_tree &&
  810. btrfs_root_refs(root_item) == 0) {
  811. set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
  812. /*
  813. * Mark the tree as dead before we change reloc_root so
  814. * have_reloc_root will not touch it from now on.
  815. */
  816. smp_wmb();
  817. __del_reloc_root(reloc_root);
  818. }
  819. if (reloc_root->commit_root != reloc_root->node) {
  820. __update_reloc_root(reloc_root);
  821. btrfs_set_root_node(root_item, reloc_root->node);
  822. free_extent_buffer(reloc_root->commit_root);
  823. reloc_root->commit_root = btrfs_root_node(reloc_root);
  824. }
  825. ret = btrfs_update_root(trans, fs_info->tree_root,
  826. &reloc_root->root_key, root_item);
  827. btrfs_put_root(reloc_root);
  828. return ret;
  829. }
  830. /*
  831. * helper to find first cached inode with inode number >= objectid
  832. * in a subvolume
  833. */
  834. static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
  835. {
  836. struct rb_node *node;
  837. struct rb_node *prev;
  838. struct btrfs_inode *entry;
  839. struct inode *inode;
  840. spin_lock(&root->inode_lock);
  841. again:
  842. node = root->inode_tree.rb_node;
  843. prev = NULL;
  844. while (node) {
  845. prev = node;
  846. entry = rb_entry(node, struct btrfs_inode, rb_node);
  847. if (objectid < btrfs_ino(entry))
  848. node = node->rb_left;
  849. else if (objectid > btrfs_ino(entry))
  850. node = node->rb_right;
  851. else
  852. break;
  853. }
  854. if (!node) {
  855. while (prev) {
  856. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  857. if (objectid <= btrfs_ino(entry)) {
  858. node = prev;
  859. break;
  860. }
  861. prev = rb_next(prev);
  862. }
  863. }
  864. while (node) {
  865. entry = rb_entry(node, struct btrfs_inode, rb_node);
  866. inode = igrab(&entry->vfs_inode);
  867. if (inode) {
  868. spin_unlock(&root->inode_lock);
  869. return inode;
  870. }
  871. objectid = btrfs_ino(entry) + 1;
  872. if (cond_resched_lock(&root->inode_lock))
  873. goto again;
  874. node = rb_next(node);
  875. }
  876. spin_unlock(&root->inode_lock);
  877. return NULL;
  878. }
  879. /*
  880. * get new location of data
  881. */
  882. static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
  883. u64 bytenr, u64 num_bytes)
  884. {
  885. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  886. struct btrfs_path *path;
  887. struct btrfs_file_extent_item *fi;
  888. struct extent_buffer *leaf;
  889. int ret;
  890. path = btrfs_alloc_path();
  891. if (!path)
  892. return -ENOMEM;
  893. bytenr -= BTRFS_I(reloc_inode)->index_cnt;
  894. ret = btrfs_lookup_file_extent(NULL, root, path,
  895. btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
  896. if (ret < 0)
  897. goto out;
  898. if (ret > 0) {
  899. ret = -ENOENT;
  900. goto out;
  901. }
  902. leaf = path->nodes[0];
  903. fi = btrfs_item_ptr(leaf, path->slots[0],
  904. struct btrfs_file_extent_item);
  905. BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
  906. btrfs_file_extent_compression(leaf, fi) ||
  907. btrfs_file_extent_encryption(leaf, fi) ||
  908. btrfs_file_extent_other_encoding(leaf, fi));
  909. if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
  910. ret = -EINVAL;
  911. goto out;
  912. }
  913. *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  914. ret = 0;
  915. out:
  916. btrfs_free_path(path);
  917. return ret;
  918. }
  919. /*
  920. * update file extent items in the tree leaf to point to
  921. * the new locations.
  922. */
  923. static noinline_for_stack
  924. int replace_file_extents(struct btrfs_trans_handle *trans,
  925. struct reloc_control *rc,
  926. struct btrfs_root *root,
  927. struct extent_buffer *leaf)
  928. {
  929. struct btrfs_fs_info *fs_info = root->fs_info;
  930. struct btrfs_key key;
  931. struct btrfs_file_extent_item *fi;
  932. struct inode *inode = NULL;
  933. u64 parent;
  934. u64 bytenr;
  935. u64 new_bytenr = 0;
  936. u64 num_bytes;
  937. u64 end;
  938. u32 nritems;
  939. u32 i;
  940. int ret = 0;
  941. int first = 1;
  942. int dirty = 0;
  943. if (rc->stage != UPDATE_DATA_PTRS)
  944. return 0;
  945. /* reloc trees always use full backref */
  946. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  947. parent = leaf->start;
  948. else
  949. parent = 0;
  950. nritems = btrfs_header_nritems(leaf);
  951. for (i = 0; i < nritems; i++) {
  952. struct btrfs_ref ref = { 0 };
  953. cond_resched();
  954. btrfs_item_key_to_cpu(leaf, &key, i);
  955. if (key.type != BTRFS_EXTENT_DATA_KEY)
  956. continue;
  957. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  958. if (btrfs_file_extent_type(leaf, fi) ==
  959. BTRFS_FILE_EXTENT_INLINE)
  960. continue;
  961. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  962. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  963. if (bytenr == 0)
  964. continue;
  965. if (!in_range(bytenr, rc->block_group->start,
  966. rc->block_group->length))
  967. continue;
  968. /*
  969. * if we are modifying block in fs tree, wait for readpage
  970. * to complete and drop the extent cache
  971. */
  972. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  973. if (first) {
  974. inode = find_next_inode(root, key.objectid);
  975. first = 0;
  976. } else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) {
  977. btrfs_add_delayed_iput(inode);
  978. inode = find_next_inode(root, key.objectid);
  979. }
  980. if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
  981. end = key.offset +
  982. btrfs_file_extent_num_bytes(leaf, fi);
  983. WARN_ON(!IS_ALIGNED(key.offset,
  984. fs_info->sectorsize));
  985. WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
  986. end--;
  987. ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
  988. key.offset, end);
  989. if (!ret)
  990. continue;
  991. btrfs_drop_extent_cache(BTRFS_I(inode),
  992. key.offset, end, 1);
  993. unlock_extent(&BTRFS_I(inode)->io_tree,
  994. key.offset, end);
  995. }
  996. }
  997. ret = get_new_location(rc->data_inode, &new_bytenr,
  998. bytenr, num_bytes);
  999. if (ret) {
  1000. /*
  1001. * Don't have to abort since we've not changed anything
  1002. * in the file extent yet.
  1003. */
  1004. break;
  1005. }
  1006. btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
  1007. dirty = 1;
  1008. key.offset -= btrfs_file_extent_offset(leaf, fi);
  1009. btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
  1010. num_bytes, parent);
  1011. ref.real_root = root->root_key.objectid;
  1012. btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
  1013. key.objectid, key.offset);
  1014. ret = btrfs_inc_extent_ref(trans, &ref);
  1015. if (ret) {
  1016. btrfs_abort_transaction(trans, ret);
  1017. break;
  1018. }
  1019. btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
  1020. num_bytes, parent);
  1021. ref.real_root = root->root_key.objectid;
  1022. btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
  1023. key.objectid, key.offset);
  1024. ret = btrfs_free_extent(trans, &ref);
  1025. if (ret) {
  1026. btrfs_abort_transaction(trans, ret);
  1027. break;
  1028. }
  1029. }
  1030. if (dirty)
  1031. btrfs_mark_buffer_dirty(leaf);
  1032. if (inode)
  1033. btrfs_add_delayed_iput(inode);
  1034. return ret;
  1035. }
  1036. static noinline_for_stack
  1037. int memcmp_node_keys(struct extent_buffer *eb, int slot,
  1038. struct btrfs_path *path, int level)
  1039. {
  1040. struct btrfs_disk_key key1;
  1041. struct btrfs_disk_key key2;
  1042. btrfs_node_key(eb, &key1, slot);
  1043. btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
  1044. return memcmp(&key1, &key2, sizeof(key1));
  1045. }
  1046. /*
  1047. * try to replace tree blocks in fs tree with the new blocks
  1048. * in reloc tree. tree blocks haven't been modified since the
  1049. * reloc tree was create can be replaced.
  1050. *
  1051. * if a block was replaced, level of the block + 1 is returned.
  1052. * if no block got replaced, 0 is returned. if there are other
  1053. * errors, a negative error number is returned.
  1054. */
  1055. static noinline_for_stack
  1056. int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
  1057. struct btrfs_root *dest, struct btrfs_root *src,
  1058. struct btrfs_path *path, struct btrfs_key *next_key,
  1059. int lowest_level, int max_level)
  1060. {
  1061. struct btrfs_fs_info *fs_info = dest->fs_info;
  1062. struct extent_buffer *eb;
  1063. struct extent_buffer *parent;
  1064. struct btrfs_ref ref = { 0 };
  1065. struct btrfs_key key;
  1066. u64 old_bytenr;
  1067. u64 new_bytenr;
  1068. u64 old_ptr_gen;
  1069. u64 new_ptr_gen;
  1070. u64 last_snapshot;
  1071. u32 blocksize;
  1072. int cow = 0;
  1073. int level;
  1074. int ret;
  1075. int slot;
  1076. ASSERT(src->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
  1077. ASSERT(dest->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  1078. last_snapshot = btrfs_root_last_snapshot(&src->root_item);
  1079. again:
  1080. slot = path->slots[lowest_level];
  1081. btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
  1082. eb = btrfs_lock_root_node(dest);
  1083. btrfs_set_lock_blocking_write(eb);
  1084. level = btrfs_header_level(eb);
  1085. if (level < lowest_level) {
  1086. btrfs_tree_unlock(eb);
  1087. free_extent_buffer(eb);
  1088. return 0;
  1089. }
  1090. if (cow) {
  1091. ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb,
  1092. BTRFS_NESTING_COW);
  1093. BUG_ON(ret);
  1094. }
  1095. btrfs_set_lock_blocking_write(eb);
  1096. if (next_key) {
  1097. next_key->objectid = (u64)-1;
  1098. next_key->type = (u8)-1;
  1099. next_key->offset = (u64)-1;
  1100. }
  1101. parent = eb;
  1102. while (1) {
  1103. struct btrfs_key first_key;
  1104. level = btrfs_header_level(parent);
  1105. ASSERT(level >= lowest_level);
  1106. ret = btrfs_bin_search(parent, &key, &slot);
  1107. if (ret < 0)
  1108. break;
  1109. if (ret && slot > 0)
  1110. slot--;
  1111. if (next_key && slot + 1 < btrfs_header_nritems(parent))
  1112. btrfs_node_key_to_cpu(parent, next_key, slot + 1);
  1113. old_bytenr = btrfs_node_blockptr(parent, slot);
  1114. blocksize = fs_info->nodesize;
  1115. old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
  1116. btrfs_node_key_to_cpu(parent, &first_key, slot);
  1117. if (level <= max_level) {
  1118. eb = path->nodes[level];
  1119. new_bytenr = btrfs_node_blockptr(eb,
  1120. path->slots[level]);
  1121. new_ptr_gen = btrfs_node_ptr_generation(eb,
  1122. path->slots[level]);
  1123. } else {
  1124. new_bytenr = 0;
  1125. new_ptr_gen = 0;
  1126. }
  1127. if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
  1128. ret = level;
  1129. break;
  1130. }
  1131. if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
  1132. memcmp_node_keys(parent, slot, path, level)) {
  1133. if (level <= lowest_level) {
  1134. ret = 0;
  1135. break;
  1136. }
  1137. eb = read_tree_block(fs_info, old_bytenr, old_ptr_gen,
  1138. level - 1, &first_key);
  1139. if (IS_ERR(eb)) {
  1140. ret = PTR_ERR(eb);
  1141. break;
  1142. } else if (!extent_buffer_uptodate(eb)) {
  1143. ret = -EIO;
  1144. free_extent_buffer(eb);
  1145. break;
  1146. }
  1147. btrfs_tree_lock(eb);
  1148. if (cow) {
  1149. ret = btrfs_cow_block(trans, dest, eb, parent,
  1150. slot, &eb,
  1151. BTRFS_NESTING_COW);
  1152. BUG_ON(ret);
  1153. }
  1154. btrfs_set_lock_blocking_write(eb);
  1155. btrfs_tree_unlock(parent);
  1156. free_extent_buffer(parent);
  1157. parent = eb;
  1158. continue;
  1159. }
  1160. if (!cow) {
  1161. btrfs_tree_unlock(parent);
  1162. free_extent_buffer(parent);
  1163. cow = 1;
  1164. goto again;
  1165. }
  1166. btrfs_node_key_to_cpu(path->nodes[level], &key,
  1167. path->slots[level]);
  1168. btrfs_release_path(path);
  1169. path->lowest_level = level;
  1170. ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
  1171. path->lowest_level = 0;
  1172. BUG_ON(ret);
  1173. /*
  1174. * Info qgroup to trace both subtrees.
  1175. *
  1176. * We must trace both trees.
  1177. * 1) Tree reloc subtree
  1178. * If not traced, we will leak data numbers
  1179. * 2) Fs subtree
  1180. * If not traced, we will double count old data
  1181. *
  1182. * We don't scan the subtree right now, but only record
  1183. * the swapped tree blocks.
  1184. * The real subtree rescan is delayed until we have new
  1185. * CoW on the subtree root node before transaction commit.
  1186. */
  1187. ret = btrfs_qgroup_add_swapped_blocks(trans, dest,
  1188. rc->block_group, parent, slot,
  1189. path->nodes[level], path->slots[level],
  1190. last_snapshot);
  1191. if (ret < 0)
  1192. break;
  1193. /*
  1194. * swap blocks in fs tree and reloc tree.
  1195. */
  1196. btrfs_set_node_blockptr(parent, slot, new_bytenr);
  1197. btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
  1198. btrfs_mark_buffer_dirty(parent);
  1199. btrfs_set_node_blockptr(path->nodes[level],
  1200. path->slots[level], old_bytenr);
  1201. btrfs_set_node_ptr_generation(path->nodes[level],
  1202. path->slots[level], old_ptr_gen);
  1203. btrfs_mark_buffer_dirty(path->nodes[level]);
  1204. btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
  1205. blocksize, path->nodes[level]->start);
  1206. ref.skip_qgroup = true;
  1207. btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
  1208. ret = btrfs_inc_extent_ref(trans, &ref);
  1209. BUG_ON(ret);
  1210. btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
  1211. blocksize, 0);
  1212. ref.skip_qgroup = true;
  1213. btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
  1214. ret = btrfs_inc_extent_ref(trans, &ref);
  1215. BUG_ON(ret);
  1216. btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
  1217. blocksize, path->nodes[level]->start);
  1218. btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
  1219. ref.skip_qgroup = true;
  1220. ret = btrfs_free_extent(trans, &ref);
  1221. BUG_ON(ret);
  1222. btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
  1223. blocksize, 0);
  1224. btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
  1225. ref.skip_qgroup = true;
  1226. ret = btrfs_free_extent(trans, &ref);
  1227. BUG_ON(ret);
  1228. btrfs_unlock_up_safe(path, 0);
  1229. ret = level;
  1230. break;
  1231. }
  1232. btrfs_tree_unlock(parent);
  1233. free_extent_buffer(parent);
  1234. return ret;
  1235. }
  1236. /*
  1237. * helper to find next relocated block in reloc tree
  1238. */
  1239. static noinline_for_stack
  1240. int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
  1241. int *level)
  1242. {
  1243. struct extent_buffer *eb;
  1244. int i;
  1245. u64 last_snapshot;
  1246. u32 nritems;
  1247. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1248. for (i = 0; i < *level; i++) {
  1249. free_extent_buffer(path->nodes[i]);
  1250. path->nodes[i] = NULL;
  1251. }
  1252. for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
  1253. eb = path->nodes[i];
  1254. nritems = btrfs_header_nritems(eb);
  1255. while (path->slots[i] + 1 < nritems) {
  1256. path->slots[i]++;
  1257. if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
  1258. last_snapshot)
  1259. continue;
  1260. *level = i;
  1261. return 0;
  1262. }
  1263. free_extent_buffer(path->nodes[i]);
  1264. path->nodes[i] = NULL;
  1265. }
  1266. return 1;
  1267. }
  1268. /*
  1269. * walk down reloc tree to find relocated block of lowest level
  1270. */
  1271. static noinline_for_stack
  1272. int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
  1273. int *level)
  1274. {
  1275. struct btrfs_fs_info *fs_info = root->fs_info;
  1276. struct extent_buffer *eb = NULL;
  1277. int i;
  1278. u64 bytenr;
  1279. u64 ptr_gen = 0;
  1280. u64 last_snapshot;
  1281. u32 nritems;
  1282. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1283. for (i = *level; i > 0; i--) {
  1284. struct btrfs_key first_key;
  1285. eb = path->nodes[i];
  1286. nritems = btrfs_header_nritems(eb);
  1287. while (path->slots[i] < nritems) {
  1288. ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
  1289. if (ptr_gen > last_snapshot)
  1290. break;
  1291. path->slots[i]++;
  1292. }
  1293. if (path->slots[i] >= nritems) {
  1294. if (i == *level)
  1295. break;
  1296. *level = i + 1;
  1297. return 0;
  1298. }
  1299. if (i == 1) {
  1300. *level = i;
  1301. return 0;
  1302. }
  1303. bytenr = btrfs_node_blockptr(eb, path->slots[i]);
  1304. btrfs_node_key_to_cpu(eb, &first_key, path->slots[i]);
  1305. eb = read_tree_block(fs_info, bytenr, ptr_gen, i - 1,
  1306. &first_key);
  1307. if (IS_ERR(eb)) {
  1308. return PTR_ERR(eb);
  1309. } else if (!extent_buffer_uptodate(eb)) {
  1310. free_extent_buffer(eb);
  1311. return -EIO;
  1312. }
  1313. BUG_ON(btrfs_header_level(eb) != i - 1);
  1314. path->nodes[i - 1] = eb;
  1315. path->slots[i - 1] = 0;
  1316. }
  1317. return 1;
  1318. }
  1319. /*
  1320. * invalidate extent cache for file extents whose key in range of
  1321. * [min_key, max_key)
  1322. */
  1323. static int invalidate_extent_cache(struct btrfs_root *root,
  1324. struct btrfs_key *min_key,
  1325. struct btrfs_key *max_key)
  1326. {
  1327. struct btrfs_fs_info *fs_info = root->fs_info;
  1328. struct inode *inode = NULL;
  1329. u64 objectid;
  1330. u64 start, end;
  1331. u64 ino;
  1332. objectid = min_key->objectid;
  1333. while (1) {
  1334. cond_resched();
  1335. iput(inode);
  1336. if (objectid > max_key->objectid)
  1337. break;
  1338. inode = find_next_inode(root, objectid);
  1339. if (!inode)
  1340. break;
  1341. ino = btrfs_ino(BTRFS_I(inode));
  1342. if (ino > max_key->objectid) {
  1343. iput(inode);
  1344. break;
  1345. }
  1346. objectid = ino + 1;
  1347. if (!S_ISREG(inode->i_mode))
  1348. continue;
  1349. if (unlikely(min_key->objectid == ino)) {
  1350. if (min_key->type > BTRFS_EXTENT_DATA_KEY)
  1351. continue;
  1352. if (min_key->type < BTRFS_EXTENT_DATA_KEY)
  1353. start = 0;
  1354. else {
  1355. start = min_key->offset;
  1356. WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
  1357. }
  1358. } else {
  1359. start = 0;
  1360. }
  1361. if (unlikely(max_key->objectid == ino)) {
  1362. if (max_key->type < BTRFS_EXTENT_DATA_KEY)
  1363. continue;
  1364. if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
  1365. end = (u64)-1;
  1366. } else {
  1367. if (max_key->offset == 0)
  1368. continue;
  1369. end = max_key->offset;
  1370. WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
  1371. end--;
  1372. }
  1373. } else {
  1374. end = (u64)-1;
  1375. }
  1376. /* the lock_extent waits for readpage to complete */
  1377. lock_extent(&BTRFS_I(inode)->io_tree, start, end);
  1378. btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1);
  1379. unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
  1380. }
  1381. return 0;
  1382. }
  1383. static int find_next_key(struct btrfs_path *path, int level,
  1384. struct btrfs_key *key)
  1385. {
  1386. while (level < BTRFS_MAX_LEVEL) {
  1387. if (!path->nodes[level])
  1388. break;
  1389. if (path->slots[level] + 1 <
  1390. btrfs_header_nritems(path->nodes[level])) {
  1391. btrfs_node_key_to_cpu(path->nodes[level], key,
  1392. path->slots[level] + 1);
  1393. return 0;
  1394. }
  1395. level++;
  1396. }
  1397. return 1;
  1398. }
  1399. /*
  1400. * Insert current subvolume into reloc_control::dirty_subvol_roots
  1401. */
  1402. static void insert_dirty_subvol(struct btrfs_trans_handle *trans,
  1403. struct reloc_control *rc,
  1404. struct btrfs_root *root)
  1405. {
  1406. struct btrfs_root *reloc_root = root->reloc_root;
  1407. struct btrfs_root_item *reloc_root_item;
  1408. /* @root must be a subvolume tree root with a valid reloc tree */
  1409. ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  1410. ASSERT(reloc_root);
  1411. reloc_root_item = &reloc_root->root_item;
  1412. memset(&reloc_root_item->drop_progress, 0,
  1413. sizeof(reloc_root_item->drop_progress));
  1414. reloc_root_item->drop_level = 0;
  1415. btrfs_set_root_refs(reloc_root_item, 0);
  1416. btrfs_update_reloc_root(trans, root);
  1417. if (list_empty(&root->reloc_dirty_list)) {
  1418. btrfs_grab_root(root);
  1419. list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
  1420. }
  1421. }
  1422. static int clean_dirty_subvols(struct reloc_control *rc)
  1423. {
  1424. struct btrfs_root *root;
  1425. struct btrfs_root *next;
  1426. int ret = 0;
  1427. int ret2;
  1428. list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
  1429. reloc_dirty_list) {
  1430. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  1431. /* Merged subvolume, cleanup its reloc root */
  1432. struct btrfs_root *reloc_root = root->reloc_root;
  1433. list_del_init(&root->reloc_dirty_list);
  1434. root->reloc_root = NULL;
  1435. /*
  1436. * Need barrier to ensure clear_bit() only happens after
  1437. * root->reloc_root = NULL. Pairs with have_reloc_root.
  1438. */
  1439. smp_wmb();
  1440. clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
  1441. if (reloc_root) {
  1442. /*
  1443. * btrfs_drop_snapshot drops our ref we hold for
  1444. * ->reloc_root. If it fails however we must
  1445. * drop the ref ourselves.
  1446. */
  1447. ret2 = btrfs_drop_snapshot(reloc_root, 0, 1);
  1448. if (ret2 < 0) {
  1449. btrfs_put_root(reloc_root);
  1450. if (!ret)
  1451. ret = ret2;
  1452. }
  1453. }
  1454. btrfs_put_root(root);
  1455. } else {
  1456. /* Orphan reloc tree, just clean it up */
  1457. ret2 = btrfs_drop_snapshot(root, 0, 1);
  1458. if (ret2 < 0) {
  1459. btrfs_put_root(root);
  1460. if (!ret)
  1461. ret = ret2;
  1462. }
  1463. }
  1464. }
  1465. return ret;
  1466. }
  1467. /*
  1468. * merge the relocated tree blocks in reloc tree with corresponding
  1469. * fs tree.
  1470. */
  1471. static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
  1472. struct btrfs_root *root)
  1473. {
  1474. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  1475. struct btrfs_key key;
  1476. struct btrfs_key next_key;
  1477. struct btrfs_trans_handle *trans = NULL;
  1478. struct btrfs_root *reloc_root;
  1479. struct btrfs_root_item *root_item;
  1480. struct btrfs_path *path;
  1481. struct extent_buffer *leaf;
  1482. int reserve_level;
  1483. int level;
  1484. int max_level;
  1485. int replaced = 0;
  1486. int ret;
  1487. int err = 0;
  1488. u32 min_reserved;
  1489. path = btrfs_alloc_path();
  1490. if (!path)
  1491. return -ENOMEM;
  1492. path->reada = READA_FORWARD;
  1493. reloc_root = root->reloc_root;
  1494. root_item = &reloc_root->root_item;
  1495. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  1496. level = btrfs_root_level(root_item);
  1497. atomic_inc(&reloc_root->node->refs);
  1498. path->nodes[level] = reloc_root->node;
  1499. path->slots[level] = 0;
  1500. } else {
  1501. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  1502. level = root_item->drop_level;
  1503. BUG_ON(level == 0);
  1504. path->lowest_level = level;
  1505. ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
  1506. path->lowest_level = 0;
  1507. if (ret < 0) {
  1508. btrfs_free_path(path);
  1509. return ret;
  1510. }
  1511. btrfs_node_key_to_cpu(path->nodes[level], &next_key,
  1512. path->slots[level]);
  1513. WARN_ON(memcmp(&key, &next_key, sizeof(key)));
  1514. btrfs_unlock_up_safe(path, 0);
  1515. }
  1516. /*
  1517. * In merge_reloc_root(), we modify the upper level pointer to swap the
  1518. * tree blocks between reloc tree and subvolume tree. Thus for tree
  1519. * block COW, we COW at most from level 1 to root level for each tree.
  1520. *
  1521. * Thus the needed metadata size is at most root_level * nodesize,
  1522. * and * 2 since we have two trees to COW.
  1523. */
  1524. reserve_level = max_t(int, 1, btrfs_root_level(root_item));
  1525. min_reserved = fs_info->nodesize * reserve_level * 2;
  1526. memset(&next_key, 0, sizeof(next_key));
  1527. while (1) {
  1528. ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
  1529. BTRFS_RESERVE_FLUSH_LIMIT);
  1530. if (ret) {
  1531. err = ret;
  1532. goto out;
  1533. }
  1534. trans = btrfs_start_transaction(root, 0);
  1535. if (IS_ERR(trans)) {
  1536. err = PTR_ERR(trans);
  1537. trans = NULL;
  1538. goto out;
  1539. }
  1540. /*
  1541. * At this point we no longer have a reloc_control, so we can't
  1542. * depend on btrfs_init_reloc_root to update our last_trans.
  1543. *
  1544. * But that's ok, we started the trans handle on our
  1545. * corresponding fs_root, which means it's been added to the
  1546. * dirty list. At commit time we'll still call
  1547. * btrfs_update_reloc_root() and update our root item
  1548. * appropriately.
  1549. */
  1550. reloc_root->last_trans = trans->transid;
  1551. trans->block_rsv = rc->block_rsv;
  1552. replaced = 0;
  1553. max_level = level;
  1554. ret = walk_down_reloc_tree(reloc_root, path, &level);
  1555. if (ret < 0) {
  1556. err = ret;
  1557. goto out;
  1558. }
  1559. if (ret > 0)
  1560. break;
  1561. if (!find_next_key(path, level, &key) &&
  1562. btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
  1563. ret = 0;
  1564. } else {
  1565. ret = replace_path(trans, rc, root, reloc_root, path,
  1566. &next_key, level, max_level);
  1567. }
  1568. if (ret < 0) {
  1569. err = ret;
  1570. goto out;
  1571. }
  1572. if (ret > 0) {
  1573. level = ret;
  1574. btrfs_node_key_to_cpu(path->nodes[level], &key,
  1575. path->slots[level]);
  1576. replaced = 1;
  1577. }
  1578. ret = walk_up_reloc_tree(reloc_root, path, &level);
  1579. if (ret > 0)
  1580. break;
  1581. BUG_ON(level == 0);
  1582. /*
  1583. * save the merging progress in the drop_progress.
  1584. * this is OK since root refs == 1 in this case.
  1585. */
  1586. btrfs_node_key(path->nodes[level], &root_item->drop_progress,
  1587. path->slots[level]);
  1588. root_item->drop_level = level;
  1589. btrfs_end_transaction_throttle(trans);
  1590. trans = NULL;
  1591. btrfs_btree_balance_dirty(fs_info);
  1592. if (replaced && rc->stage == UPDATE_DATA_PTRS)
  1593. invalidate_extent_cache(root, &key, &next_key);
  1594. }
  1595. /*
  1596. * handle the case only one block in the fs tree need to be
  1597. * relocated and the block is tree root.
  1598. */
  1599. leaf = btrfs_lock_root_node(root);
  1600. ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf,
  1601. BTRFS_NESTING_COW);
  1602. btrfs_tree_unlock(leaf);
  1603. free_extent_buffer(leaf);
  1604. if (ret < 0)
  1605. err = ret;
  1606. out:
  1607. btrfs_free_path(path);
  1608. if (err == 0)
  1609. insert_dirty_subvol(trans, rc, root);
  1610. if (trans)
  1611. btrfs_end_transaction_throttle(trans);
  1612. btrfs_btree_balance_dirty(fs_info);
  1613. if (replaced && rc->stage == UPDATE_DATA_PTRS)
  1614. invalidate_extent_cache(root, &key, &next_key);
  1615. return err;
  1616. }
  1617. static noinline_for_stack
  1618. int prepare_to_merge(struct reloc_control *rc, int err)
  1619. {
  1620. struct btrfs_root *root = rc->extent_root;
  1621. struct btrfs_fs_info *fs_info = root->fs_info;
  1622. struct btrfs_root *reloc_root;
  1623. struct btrfs_trans_handle *trans;
  1624. LIST_HEAD(reloc_roots);
  1625. u64 num_bytes = 0;
  1626. int ret;
  1627. mutex_lock(&fs_info->reloc_mutex);
  1628. rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
  1629. rc->merging_rsv_size += rc->nodes_relocated * 2;
  1630. mutex_unlock(&fs_info->reloc_mutex);
  1631. again:
  1632. if (!err) {
  1633. num_bytes = rc->merging_rsv_size;
  1634. ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
  1635. BTRFS_RESERVE_FLUSH_ALL);
  1636. if (ret)
  1637. err = ret;
  1638. }
  1639. trans = btrfs_join_transaction(rc->extent_root);
  1640. if (IS_ERR(trans)) {
  1641. if (!err)
  1642. btrfs_block_rsv_release(fs_info, rc->block_rsv,
  1643. num_bytes, NULL);
  1644. return PTR_ERR(trans);
  1645. }
  1646. if (!err) {
  1647. if (num_bytes != rc->merging_rsv_size) {
  1648. btrfs_end_transaction(trans);
  1649. btrfs_block_rsv_release(fs_info, rc->block_rsv,
  1650. num_bytes, NULL);
  1651. goto again;
  1652. }
  1653. }
  1654. rc->merge_reloc_tree = 1;
  1655. while (!list_empty(&rc->reloc_roots)) {
  1656. reloc_root = list_entry(rc->reloc_roots.next,
  1657. struct btrfs_root, root_list);
  1658. list_del_init(&reloc_root->root_list);
  1659. root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
  1660. false);
  1661. BUG_ON(IS_ERR(root));
  1662. BUG_ON(root->reloc_root != reloc_root);
  1663. /*
  1664. * set reference count to 1, so btrfs_recover_relocation
  1665. * knows it should resumes merging
  1666. */
  1667. if (!err)
  1668. btrfs_set_root_refs(&reloc_root->root_item, 1);
  1669. btrfs_update_reloc_root(trans, root);
  1670. list_add(&reloc_root->root_list, &reloc_roots);
  1671. btrfs_put_root(root);
  1672. }
  1673. list_splice(&reloc_roots, &rc->reloc_roots);
  1674. if (!err)
  1675. btrfs_commit_transaction(trans);
  1676. else
  1677. btrfs_end_transaction(trans);
  1678. return err;
  1679. }
  1680. static noinline_for_stack
  1681. void free_reloc_roots(struct list_head *list)
  1682. {
  1683. struct btrfs_root *reloc_root, *tmp;
  1684. list_for_each_entry_safe(reloc_root, tmp, list, root_list)
  1685. __del_reloc_root(reloc_root);
  1686. }
  1687. static noinline_for_stack
  1688. void merge_reloc_roots(struct reloc_control *rc)
  1689. {
  1690. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  1691. struct btrfs_root *root;
  1692. struct btrfs_root *reloc_root;
  1693. LIST_HEAD(reloc_roots);
  1694. int found = 0;
  1695. int ret = 0;
  1696. again:
  1697. root = rc->extent_root;
  1698. /*
  1699. * this serializes us with btrfs_record_root_in_transaction,
  1700. * we have to make sure nobody is in the middle of
  1701. * adding their roots to the list while we are
  1702. * doing this splice
  1703. */
  1704. mutex_lock(&fs_info->reloc_mutex);
  1705. list_splice_init(&rc->reloc_roots, &reloc_roots);
  1706. mutex_unlock(&fs_info->reloc_mutex);
  1707. while (!list_empty(&reloc_roots)) {
  1708. found = 1;
  1709. reloc_root = list_entry(reloc_roots.next,
  1710. struct btrfs_root, root_list);
  1711. root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
  1712. false);
  1713. if (btrfs_root_refs(&reloc_root->root_item) > 0) {
  1714. BUG_ON(IS_ERR(root));
  1715. BUG_ON(root->reloc_root != reloc_root);
  1716. ret = merge_reloc_root(rc, root);
  1717. btrfs_put_root(root);
  1718. if (ret) {
  1719. if (list_empty(&reloc_root->root_list))
  1720. list_add_tail(&reloc_root->root_list,
  1721. &reloc_roots);
  1722. goto out;
  1723. }
  1724. } else {
  1725. if (!IS_ERR(root)) {
  1726. if (root->reloc_root == reloc_root) {
  1727. root->reloc_root = NULL;
  1728. btrfs_put_root(reloc_root);
  1729. }
  1730. clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE,
  1731. &root->state);
  1732. btrfs_put_root(root);
  1733. }
  1734. list_del_init(&reloc_root->root_list);
  1735. /* Don't forget to queue this reloc root for cleanup */
  1736. list_add_tail(&reloc_root->reloc_dirty_list,
  1737. &rc->dirty_subvol_roots);
  1738. }
  1739. }
  1740. if (found) {
  1741. found = 0;
  1742. goto again;
  1743. }
  1744. out:
  1745. if (ret) {
  1746. btrfs_handle_fs_error(fs_info, ret, NULL);
  1747. free_reloc_roots(&reloc_roots);
  1748. /* new reloc root may be added */
  1749. mutex_lock(&fs_info->reloc_mutex);
  1750. list_splice_init(&rc->reloc_roots, &reloc_roots);
  1751. mutex_unlock(&fs_info->reloc_mutex);
  1752. free_reloc_roots(&reloc_roots);
  1753. }
  1754. /*
  1755. * We used to have
  1756. *
  1757. * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
  1758. *
  1759. * here, but it's wrong. If we fail to start the transaction in
  1760. * prepare_to_merge() we will have only 0 ref reloc roots, none of which
  1761. * have actually been removed from the reloc_root_tree rb tree. This is
  1762. * fine because we're bailing here, and we hold a reference on the root
  1763. * for the list that holds it, so these roots will be cleaned up when we
  1764. * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root
  1765. * will be cleaned up on unmount.
  1766. *
  1767. * The remaining nodes will be cleaned up by free_reloc_control.
  1768. */
  1769. }
  1770. static void free_block_list(struct rb_root *blocks)
  1771. {
  1772. struct tree_block *block;
  1773. struct rb_node *rb_node;
  1774. while ((rb_node = rb_first(blocks))) {
  1775. block = rb_entry(rb_node, struct tree_block, rb_node);
  1776. rb_erase(rb_node, blocks);
  1777. kfree(block);
  1778. }
  1779. }
  1780. static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
  1781. struct btrfs_root *reloc_root)
  1782. {
  1783. struct btrfs_fs_info *fs_info = reloc_root->fs_info;
  1784. struct btrfs_root *root;
  1785. int ret;
  1786. if (reloc_root->last_trans == trans->transid)
  1787. return 0;
  1788. root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false);
  1789. BUG_ON(IS_ERR(root));
  1790. BUG_ON(root->reloc_root != reloc_root);
  1791. ret = btrfs_record_root_in_trans(trans, root);
  1792. btrfs_put_root(root);
  1793. return ret;
  1794. }
  1795. static noinline_for_stack
  1796. struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
  1797. struct reloc_control *rc,
  1798. struct btrfs_backref_node *node,
  1799. struct btrfs_backref_edge *edges[])
  1800. {
  1801. struct btrfs_backref_node *next;
  1802. struct btrfs_root *root;
  1803. int index = 0;
  1804. next = node;
  1805. while (1) {
  1806. cond_resched();
  1807. next = walk_up_backref(next, edges, &index);
  1808. root = next->root;
  1809. BUG_ON(!root);
  1810. BUG_ON(!test_bit(BTRFS_ROOT_SHAREABLE, &root->state));
  1811. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
  1812. record_reloc_root_in_trans(trans, root);
  1813. break;
  1814. }
  1815. btrfs_record_root_in_trans(trans, root);
  1816. root = root->reloc_root;
  1817. if (next->new_bytenr != root->node->start) {
  1818. BUG_ON(next->new_bytenr);
  1819. BUG_ON(!list_empty(&next->list));
  1820. next->new_bytenr = root->node->start;
  1821. btrfs_put_root(next->root);
  1822. next->root = btrfs_grab_root(root);
  1823. ASSERT(next->root);
  1824. list_add_tail(&next->list,
  1825. &rc->backref_cache.changed);
  1826. mark_block_processed(rc, next);
  1827. break;
  1828. }
  1829. WARN_ON(1);
  1830. root = NULL;
  1831. next = walk_down_backref(edges, &index);
  1832. if (!next || next->level <= node->level)
  1833. break;
  1834. }
  1835. if (!root)
  1836. return NULL;
  1837. next = node;
  1838. /* setup backref node path for btrfs_reloc_cow_block */
  1839. while (1) {
  1840. rc->backref_cache.path[next->level] = next;
  1841. if (--index < 0)
  1842. break;
  1843. next = edges[index]->node[UPPER];
  1844. }
  1845. return root;
  1846. }
  1847. /*
  1848. * Select a tree root for relocation.
  1849. *
  1850. * Return NULL if the block is not shareable. We should use do_relocation() in
  1851. * this case.
  1852. *
  1853. * Return a tree root pointer if the block is shareable.
  1854. * Return -ENOENT if the block is root of reloc tree.
  1855. */
  1856. static noinline_for_stack
  1857. struct btrfs_root *select_one_root(struct btrfs_backref_node *node)
  1858. {
  1859. struct btrfs_backref_node *next;
  1860. struct btrfs_root *root;
  1861. struct btrfs_root *fs_root = NULL;
  1862. struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  1863. int index = 0;
  1864. next = node;
  1865. while (1) {
  1866. cond_resched();
  1867. next = walk_up_backref(next, edges, &index);
  1868. root = next->root;
  1869. BUG_ON(!root);
  1870. /* No other choice for non-shareable tree */
  1871. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  1872. return root;
  1873. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
  1874. fs_root = root;
  1875. if (next != node)
  1876. return NULL;
  1877. next = walk_down_backref(edges, &index);
  1878. if (!next || next->level <= node->level)
  1879. break;
  1880. }
  1881. if (!fs_root)
  1882. return ERR_PTR(-ENOENT);
  1883. return fs_root;
  1884. }
  1885. static noinline_for_stack
  1886. u64 calcu_metadata_size(struct reloc_control *rc,
  1887. struct btrfs_backref_node *node, int reserve)
  1888. {
  1889. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  1890. struct btrfs_backref_node *next = node;
  1891. struct btrfs_backref_edge *edge;
  1892. struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  1893. u64 num_bytes = 0;
  1894. int index = 0;
  1895. BUG_ON(reserve && node->processed);
  1896. while (next) {
  1897. cond_resched();
  1898. while (1) {
  1899. if (next->processed && (reserve || next != node))
  1900. break;
  1901. num_bytes += fs_info->nodesize;
  1902. if (list_empty(&next->upper))
  1903. break;
  1904. edge = list_entry(next->upper.next,
  1905. struct btrfs_backref_edge, list[LOWER]);
  1906. edges[index++] = edge;
  1907. next = edge->node[UPPER];
  1908. }
  1909. next = walk_down_backref(edges, &index);
  1910. }
  1911. return num_bytes;
  1912. }
  1913. static int reserve_metadata_space(struct btrfs_trans_handle *trans,
  1914. struct reloc_control *rc,
  1915. struct btrfs_backref_node *node)
  1916. {
  1917. struct btrfs_root *root = rc->extent_root;
  1918. struct btrfs_fs_info *fs_info = root->fs_info;
  1919. u64 num_bytes;
  1920. int ret;
  1921. u64 tmp;
  1922. num_bytes = calcu_metadata_size(rc, node, 1) * 2;
  1923. trans->block_rsv = rc->block_rsv;
  1924. rc->reserved_bytes += num_bytes;
  1925. /*
  1926. * We are under a transaction here so we can only do limited flushing.
  1927. * If we get an enospc just kick back -EAGAIN so we know to drop the
  1928. * transaction and try to refill when we can flush all the things.
  1929. */
  1930. ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
  1931. BTRFS_RESERVE_FLUSH_LIMIT);
  1932. if (ret) {
  1933. tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES;
  1934. while (tmp <= rc->reserved_bytes)
  1935. tmp <<= 1;
  1936. /*
  1937. * only one thread can access block_rsv at this point,
  1938. * so we don't need hold lock to protect block_rsv.
  1939. * we expand more reservation size here to allow enough
  1940. * space for relocation and we will return earlier in
  1941. * enospc case.
  1942. */
  1943. rc->block_rsv->size = tmp + fs_info->nodesize *
  1944. RELOCATION_RESERVED_NODES;
  1945. return -EAGAIN;
  1946. }
  1947. return 0;
  1948. }
  1949. /*
  1950. * relocate a block tree, and then update pointers in upper level
  1951. * blocks that reference the block to point to the new location.
  1952. *
  1953. * if called by link_to_upper, the block has already been relocated.
  1954. * in that case this function just updates pointers.
  1955. */
  1956. static int do_relocation(struct btrfs_trans_handle *trans,
  1957. struct reloc_control *rc,
  1958. struct btrfs_backref_node *node,
  1959. struct btrfs_key *key,
  1960. struct btrfs_path *path, int lowest)
  1961. {
  1962. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  1963. struct btrfs_backref_node *upper;
  1964. struct btrfs_backref_edge *edge;
  1965. struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  1966. struct btrfs_root *root;
  1967. struct extent_buffer *eb;
  1968. u32 blocksize;
  1969. u64 bytenr;
  1970. u64 generation;
  1971. int slot;
  1972. int ret;
  1973. int err = 0;
  1974. BUG_ON(lowest && node->eb);
  1975. path->lowest_level = node->level + 1;
  1976. rc->backref_cache.path[node->level] = node;
  1977. list_for_each_entry(edge, &node->upper, list[LOWER]) {
  1978. struct btrfs_key first_key;
  1979. struct btrfs_ref ref = { 0 };
  1980. cond_resched();
  1981. upper = edge->node[UPPER];
  1982. root = select_reloc_root(trans, rc, upper, edges);
  1983. BUG_ON(!root);
  1984. if (upper->eb && !upper->locked) {
  1985. if (!lowest) {
  1986. ret = btrfs_bin_search(upper->eb, key, &slot);
  1987. if (ret < 0) {
  1988. err = ret;
  1989. goto next;
  1990. }
  1991. BUG_ON(ret);
  1992. bytenr = btrfs_node_blockptr(upper->eb, slot);
  1993. if (node->eb->start == bytenr)
  1994. goto next;
  1995. }
  1996. btrfs_backref_drop_node_buffer(upper);
  1997. }
  1998. if (!upper->eb) {
  1999. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  2000. if (ret) {
  2001. if (ret < 0)
  2002. err = ret;
  2003. else
  2004. err = -ENOENT;
  2005. btrfs_release_path(path);
  2006. break;
  2007. }
  2008. if (!upper->eb) {
  2009. upper->eb = path->nodes[upper->level];
  2010. path->nodes[upper->level] = NULL;
  2011. } else {
  2012. BUG_ON(upper->eb != path->nodes[upper->level]);
  2013. }
  2014. upper->locked = 1;
  2015. path->locks[upper->level] = 0;
  2016. slot = path->slots[upper->level];
  2017. btrfs_release_path(path);
  2018. } else {
  2019. ret = btrfs_bin_search(upper->eb, key, &slot);
  2020. if (ret < 0) {
  2021. err = ret;
  2022. goto next;
  2023. }
  2024. BUG_ON(ret);
  2025. }
  2026. bytenr = btrfs_node_blockptr(upper->eb, slot);
  2027. if (lowest) {
  2028. if (bytenr != node->bytenr) {
  2029. btrfs_err(root->fs_info,
  2030. "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
  2031. bytenr, node->bytenr, slot,
  2032. upper->eb->start);
  2033. err = -EIO;
  2034. goto next;
  2035. }
  2036. } else {
  2037. if (node->eb->start == bytenr)
  2038. goto next;
  2039. }
  2040. blocksize = root->fs_info->nodesize;
  2041. generation = btrfs_node_ptr_generation(upper->eb, slot);
  2042. btrfs_node_key_to_cpu(upper->eb, &first_key, slot);
  2043. eb = read_tree_block(fs_info, bytenr, generation,
  2044. upper->level - 1, &first_key);
  2045. if (IS_ERR(eb)) {
  2046. err = PTR_ERR(eb);
  2047. goto next;
  2048. } else if (!extent_buffer_uptodate(eb)) {
  2049. free_extent_buffer(eb);
  2050. err = -EIO;
  2051. goto next;
  2052. }
  2053. btrfs_tree_lock(eb);
  2054. btrfs_set_lock_blocking_write(eb);
  2055. if (!node->eb) {
  2056. ret = btrfs_cow_block(trans, root, eb, upper->eb,
  2057. slot, &eb, BTRFS_NESTING_COW);
  2058. btrfs_tree_unlock(eb);
  2059. free_extent_buffer(eb);
  2060. if (ret < 0) {
  2061. err = ret;
  2062. goto next;
  2063. }
  2064. BUG_ON(node->eb != eb);
  2065. } else {
  2066. btrfs_set_node_blockptr(upper->eb, slot,
  2067. node->eb->start);
  2068. btrfs_set_node_ptr_generation(upper->eb, slot,
  2069. trans->transid);
  2070. btrfs_mark_buffer_dirty(upper->eb);
  2071. btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
  2072. node->eb->start, blocksize,
  2073. upper->eb->start);
  2074. ref.real_root = root->root_key.objectid;
  2075. btrfs_init_tree_ref(&ref, node->level,
  2076. btrfs_header_owner(upper->eb));
  2077. ret = btrfs_inc_extent_ref(trans, &ref);
  2078. BUG_ON(ret);
  2079. ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
  2080. BUG_ON(ret);
  2081. }
  2082. next:
  2083. if (!upper->pending)
  2084. btrfs_backref_drop_node_buffer(upper);
  2085. else
  2086. btrfs_backref_unlock_node_buffer(upper);
  2087. if (err)
  2088. break;
  2089. }
  2090. if (!err && node->pending) {
  2091. btrfs_backref_drop_node_buffer(node);
  2092. list_move_tail(&node->list, &rc->backref_cache.changed);
  2093. node->pending = 0;
  2094. }
  2095. path->lowest_level = 0;
  2096. BUG_ON(err == -ENOSPC);
  2097. return err;
  2098. }
  2099. static int link_to_upper(struct btrfs_trans_handle *trans,
  2100. struct reloc_control *rc,
  2101. struct btrfs_backref_node *node,
  2102. struct btrfs_path *path)
  2103. {
  2104. struct btrfs_key key;
  2105. btrfs_node_key_to_cpu(node->eb, &key, 0);
  2106. return do_relocation(trans, rc, node, &key, path, 0);
  2107. }
  2108. static int finish_pending_nodes(struct btrfs_trans_handle *trans,
  2109. struct reloc_control *rc,
  2110. struct btrfs_path *path, int err)
  2111. {
  2112. LIST_HEAD(list);
  2113. struct btrfs_backref_cache *cache = &rc->backref_cache;
  2114. struct btrfs_backref_node *node;
  2115. int level;
  2116. int ret;
  2117. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  2118. while (!list_empty(&cache->pending[level])) {
  2119. node = list_entry(cache->pending[level].next,
  2120. struct btrfs_backref_node, list);
  2121. list_move_tail(&node->list, &list);
  2122. BUG_ON(!node->pending);
  2123. if (!err) {
  2124. ret = link_to_upper(trans, rc, node, path);
  2125. if (ret < 0)
  2126. err = ret;
  2127. }
  2128. }
  2129. list_splice_init(&list, &cache->pending[level]);
  2130. }
  2131. return err;
  2132. }
  2133. /*
  2134. * mark a block and all blocks directly/indirectly reference the block
  2135. * as processed.
  2136. */
  2137. static void update_processed_blocks(struct reloc_control *rc,
  2138. struct btrfs_backref_node *node)
  2139. {
  2140. struct btrfs_backref_node *next = node;
  2141. struct btrfs_backref_edge *edge;
  2142. struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
  2143. int index = 0;
  2144. while (next) {
  2145. cond_resched();
  2146. while (1) {
  2147. if (next->processed)
  2148. break;
  2149. mark_block_processed(rc, next);
  2150. if (list_empty(&next->upper))
  2151. break;
  2152. edge = list_entry(next->upper.next,
  2153. struct btrfs_backref_edge, list[LOWER]);
  2154. edges[index++] = edge;
  2155. next = edge->node[UPPER];
  2156. }
  2157. next = walk_down_backref(edges, &index);
  2158. }
  2159. }
  2160. static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
  2161. {
  2162. u32 blocksize = rc->extent_root->fs_info->nodesize;
  2163. if (test_range_bit(&rc->processed_blocks, bytenr,
  2164. bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
  2165. return 1;
  2166. return 0;
  2167. }
  2168. static int get_tree_block_key(struct btrfs_fs_info *fs_info,
  2169. struct tree_block *block)
  2170. {
  2171. struct extent_buffer *eb;
  2172. eb = read_tree_block(fs_info, block->bytenr, block->key.offset,
  2173. block->level, NULL);
  2174. if (IS_ERR(eb)) {
  2175. return PTR_ERR(eb);
  2176. } else if (!extent_buffer_uptodate(eb)) {
  2177. free_extent_buffer(eb);
  2178. return -EIO;
  2179. }
  2180. if (block->level == 0)
  2181. btrfs_item_key_to_cpu(eb, &block->key, 0);
  2182. else
  2183. btrfs_node_key_to_cpu(eb, &block->key, 0);
  2184. free_extent_buffer(eb);
  2185. block->key_ready = 1;
  2186. return 0;
  2187. }
  2188. /*
  2189. * helper function to relocate a tree block
  2190. */
  2191. static int relocate_tree_block(struct btrfs_trans_handle *trans,
  2192. struct reloc_control *rc,
  2193. struct btrfs_backref_node *node,
  2194. struct btrfs_key *key,
  2195. struct btrfs_path *path)
  2196. {
  2197. struct btrfs_root *root;
  2198. int ret = 0;
  2199. if (!node)
  2200. return 0;
  2201. /*
  2202. * If we fail here we want to drop our backref_node because we are going
  2203. * to start over and regenerate the tree for it.
  2204. */
  2205. ret = reserve_metadata_space(trans, rc, node);
  2206. if (ret)
  2207. goto out;
  2208. BUG_ON(node->processed);
  2209. root = select_one_root(node);
  2210. if (root == ERR_PTR(-ENOENT)) {
  2211. update_processed_blocks(rc, node);
  2212. goto out;
  2213. }
  2214. if (root) {
  2215. if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
  2216. BUG_ON(node->new_bytenr);
  2217. BUG_ON(!list_empty(&node->list));
  2218. btrfs_record_root_in_trans(trans, root);
  2219. root = root->reloc_root;
  2220. node->new_bytenr = root->node->start;
  2221. btrfs_put_root(node->root);
  2222. node->root = btrfs_grab_root(root);
  2223. ASSERT(node->root);
  2224. list_add_tail(&node->list, &rc->backref_cache.changed);
  2225. } else {
  2226. path->lowest_level = node->level;
  2227. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  2228. btrfs_release_path(path);
  2229. if (ret > 0)
  2230. ret = 0;
  2231. }
  2232. if (!ret)
  2233. update_processed_blocks(rc, node);
  2234. } else {
  2235. ret = do_relocation(trans, rc, node, key, path, 1);
  2236. }
  2237. out:
  2238. if (ret || node->level == 0 || node->cowonly)
  2239. btrfs_backref_cleanup_node(&rc->backref_cache, node);
  2240. return ret;
  2241. }
  2242. /*
  2243. * relocate a list of blocks
  2244. */
  2245. static noinline_for_stack
  2246. int relocate_tree_blocks(struct btrfs_trans_handle *trans,
  2247. struct reloc_control *rc, struct rb_root *blocks)
  2248. {
  2249. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2250. struct btrfs_backref_node *node;
  2251. struct btrfs_path *path;
  2252. struct tree_block *block;
  2253. struct tree_block *next;
  2254. int ret;
  2255. int err = 0;
  2256. path = btrfs_alloc_path();
  2257. if (!path) {
  2258. err = -ENOMEM;
  2259. goto out_free_blocks;
  2260. }
  2261. /* Kick in readahead for tree blocks with missing keys */
  2262. rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
  2263. if (!block->key_ready)
  2264. readahead_tree_block(fs_info, block->bytenr);
  2265. }
  2266. /* Get first keys */
  2267. rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
  2268. if (!block->key_ready) {
  2269. err = get_tree_block_key(fs_info, block);
  2270. if (err)
  2271. goto out_free_path;
  2272. }
  2273. }
  2274. /* Do tree relocation */
  2275. rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
  2276. node = build_backref_tree(rc, &block->key,
  2277. block->level, block->bytenr);
  2278. if (IS_ERR(node)) {
  2279. err = PTR_ERR(node);
  2280. goto out;
  2281. }
  2282. ret = relocate_tree_block(trans, rc, node, &block->key,
  2283. path);
  2284. if (ret < 0) {
  2285. err = ret;
  2286. break;
  2287. }
  2288. }
  2289. out:
  2290. err = finish_pending_nodes(trans, rc, path, err);
  2291. out_free_path:
  2292. btrfs_free_path(path);
  2293. out_free_blocks:
  2294. free_block_list(blocks);
  2295. return err;
  2296. }
  2297. static noinline_for_stack int prealloc_file_extent_cluster(
  2298. struct btrfs_inode *inode,
  2299. struct file_extent_cluster *cluster)
  2300. {
  2301. u64 alloc_hint = 0;
  2302. u64 start;
  2303. u64 end;
  2304. u64 offset = inode->index_cnt;
  2305. u64 num_bytes;
  2306. int nr;
  2307. int ret = 0;
  2308. u64 prealloc_start = cluster->start - offset;
  2309. u64 prealloc_end = cluster->end - offset;
  2310. u64 cur_offset = prealloc_start;
  2311. BUG_ON(cluster->start != cluster->boundary[0]);
  2312. ret = btrfs_alloc_data_chunk_ondemand(inode,
  2313. prealloc_end + 1 - prealloc_start);
  2314. if (ret)
  2315. return ret;
  2316. inode_lock(&inode->vfs_inode);
  2317. for (nr = 0; nr < cluster->nr; nr++) {
  2318. start = cluster->boundary[nr] - offset;
  2319. if (nr + 1 < cluster->nr)
  2320. end = cluster->boundary[nr + 1] - 1 - offset;
  2321. else
  2322. end = cluster->end - offset;
  2323. lock_extent(&inode->io_tree, start, end);
  2324. num_bytes = end + 1 - start;
  2325. ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start,
  2326. num_bytes, num_bytes,
  2327. end + 1, &alloc_hint);
  2328. cur_offset = end + 1;
  2329. unlock_extent(&inode->io_tree, start, end);
  2330. if (ret)
  2331. break;
  2332. }
  2333. inode_unlock(&inode->vfs_inode);
  2334. if (cur_offset < prealloc_end)
  2335. btrfs_free_reserved_data_space_noquota(inode->root->fs_info,
  2336. prealloc_end + 1 - cur_offset);
  2337. return ret;
  2338. }
  2339. static noinline_for_stack
  2340. int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
  2341. u64 block_start)
  2342. {
  2343. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2344. struct extent_map *em;
  2345. int ret = 0;
  2346. em = alloc_extent_map();
  2347. if (!em)
  2348. return -ENOMEM;
  2349. em->start = start;
  2350. em->len = end + 1 - start;
  2351. em->block_len = em->len;
  2352. em->block_start = block_start;
  2353. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  2354. lock_extent(&BTRFS_I(inode)->io_tree, start, end);
  2355. while (1) {
  2356. write_lock(&em_tree->lock);
  2357. ret = add_extent_mapping(em_tree, em, 0);
  2358. write_unlock(&em_tree->lock);
  2359. if (ret != -EEXIST) {
  2360. free_extent_map(em);
  2361. break;
  2362. }
  2363. btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
  2364. }
  2365. unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
  2366. return ret;
  2367. }
  2368. /*
  2369. * Allow error injection to test balance cancellation
  2370. */
  2371. int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)
  2372. {
  2373. return atomic_read(&fs_info->balance_cancel_req) ||
  2374. fatal_signal_pending(current);
  2375. }
  2376. ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
  2377. static int relocate_file_extent_cluster(struct inode *inode,
  2378. struct file_extent_cluster *cluster)
  2379. {
  2380. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2381. u64 page_start;
  2382. u64 page_end;
  2383. u64 offset = BTRFS_I(inode)->index_cnt;
  2384. unsigned long index;
  2385. unsigned long last_index;
  2386. struct page *page;
  2387. struct file_ra_state *ra;
  2388. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  2389. int nr = 0;
  2390. int ret = 0;
  2391. if (!cluster->nr)
  2392. return 0;
  2393. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  2394. if (!ra)
  2395. return -ENOMEM;
  2396. ret = prealloc_file_extent_cluster(BTRFS_I(inode), cluster);
  2397. if (ret)
  2398. goto out;
  2399. file_ra_state_init(ra, inode->i_mapping);
  2400. ret = setup_extent_mapping(inode, cluster->start - offset,
  2401. cluster->end - offset, cluster->start);
  2402. if (ret)
  2403. goto out;
  2404. index = (cluster->start - offset) >> PAGE_SHIFT;
  2405. last_index = (cluster->end - offset) >> PAGE_SHIFT;
  2406. while (index <= last_index) {
  2407. ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
  2408. PAGE_SIZE);
  2409. if (ret)
  2410. goto out;
  2411. page = find_lock_page(inode->i_mapping, index);
  2412. if (!page) {
  2413. page_cache_sync_readahead(inode->i_mapping,
  2414. ra, NULL, index,
  2415. last_index + 1 - index);
  2416. page = find_or_create_page(inode->i_mapping, index,
  2417. mask);
  2418. if (!page) {
  2419. btrfs_delalloc_release_metadata(BTRFS_I(inode),
  2420. PAGE_SIZE, true);
  2421. btrfs_delalloc_release_extents(BTRFS_I(inode),
  2422. PAGE_SIZE);
  2423. ret = -ENOMEM;
  2424. goto out;
  2425. }
  2426. }
  2427. if (PageReadahead(page)) {
  2428. page_cache_async_readahead(inode->i_mapping,
  2429. ra, NULL, page, index,
  2430. last_index + 1 - index);
  2431. }
  2432. if (!PageUptodate(page)) {
  2433. btrfs_readpage(NULL, page);
  2434. lock_page(page);
  2435. if (!PageUptodate(page)) {
  2436. unlock_page(page);
  2437. put_page(page);
  2438. btrfs_delalloc_release_metadata(BTRFS_I(inode),
  2439. PAGE_SIZE, true);
  2440. btrfs_delalloc_release_extents(BTRFS_I(inode),
  2441. PAGE_SIZE);
  2442. ret = -EIO;
  2443. goto out;
  2444. }
  2445. }
  2446. page_start = page_offset(page);
  2447. page_end = page_start + PAGE_SIZE - 1;
  2448. lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
  2449. set_page_extent_mapped(page);
  2450. if (nr < cluster->nr &&
  2451. page_start + offset == cluster->boundary[nr]) {
  2452. set_extent_bits(&BTRFS_I(inode)->io_tree,
  2453. page_start, page_end,
  2454. EXTENT_BOUNDARY);
  2455. nr++;
  2456. }
  2457. ret = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start,
  2458. page_end, 0, NULL);
  2459. if (ret) {
  2460. unlock_page(page);
  2461. put_page(page);
  2462. btrfs_delalloc_release_metadata(BTRFS_I(inode),
  2463. PAGE_SIZE, true);
  2464. btrfs_delalloc_release_extents(BTRFS_I(inode),
  2465. PAGE_SIZE);
  2466. clear_extent_bits(&BTRFS_I(inode)->io_tree,
  2467. page_start, page_end,
  2468. EXTENT_LOCKED | EXTENT_BOUNDARY);
  2469. goto out;
  2470. }
  2471. set_page_dirty(page);
  2472. unlock_extent(&BTRFS_I(inode)->io_tree,
  2473. page_start, page_end);
  2474. unlock_page(page);
  2475. put_page(page);
  2476. index++;
  2477. btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
  2478. balance_dirty_pages_ratelimited(inode->i_mapping);
  2479. btrfs_throttle(fs_info);
  2480. if (btrfs_should_cancel_balance(fs_info)) {
  2481. ret = -ECANCELED;
  2482. goto out;
  2483. }
  2484. }
  2485. WARN_ON(nr != cluster->nr);
  2486. out:
  2487. kfree(ra);
  2488. return ret;
  2489. }
  2490. static noinline_for_stack
  2491. int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
  2492. struct file_extent_cluster *cluster)
  2493. {
  2494. int ret;
  2495. if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
  2496. ret = relocate_file_extent_cluster(inode, cluster);
  2497. if (ret)
  2498. return ret;
  2499. cluster->nr = 0;
  2500. }
  2501. if (!cluster->nr)
  2502. cluster->start = extent_key->objectid;
  2503. else
  2504. BUG_ON(cluster->nr >= MAX_EXTENTS);
  2505. cluster->end = extent_key->objectid + extent_key->offset - 1;
  2506. cluster->boundary[cluster->nr] = extent_key->objectid;
  2507. cluster->nr++;
  2508. if (cluster->nr >= MAX_EXTENTS) {
  2509. ret = relocate_file_extent_cluster(inode, cluster);
  2510. if (ret)
  2511. return ret;
  2512. cluster->nr = 0;
  2513. }
  2514. return 0;
  2515. }
  2516. /*
  2517. * helper to add a tree block to the list.
  2518. * the major work is getting the generation and level of the block
  2519. */
  2520. static int add_tree_block(struct reloc_control *rc,
  2521. struct btrfs_key *extent_key,
  2522. struct btrfs_path *path,
  2523. struct rb_root *blocks)
  2524. {
  2525. struct extent_buffer *eb;
  2526. struct btrfs_extent_item *ei;
  2527. struct btrfs_tree_block_info *bi;
  2528. struct tree_block *block;
  2529. struct rb_node *rb_node;
  2530. u32 item_size;
  2531. int level = -1;
  2532. u64 generation;
  2533. eb = path->nodes[0];
  2534. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  2535. if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
  2536. item_size >= sizeof(*ei) + sizeof(*bi)) {
  2537. ei = btrfs_item_ptr(eb, path->slots[0],
  2538. struct btrfs_extent_item);
  2539. if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
  2540. bi = (struct btrfs_tree_block_info *)(ei + 1);
  2541. level = btrfs_tree_block_level(eb, bi);
  2542. } else {
  2543. level = (int)extent_key->offset;
  2544. }
  2545. generation = btrfs_extent_generation(eb, ei);
  2546. } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
  2547. btrfs_print_v0_err(eb->fs_info);
  2548. btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
  2549. return -EINVAL;
  2550. } else {
  2551. BUG();
  2552. }
  2553. btrfs_release_path(path);
  2554. BUG_ON(level == -1);
  2555. block = kmalloc(sizeof(*block), GFP_NOFS);
  2556. if (!block)
  2557. return -ENOMEM;
  2558. block->bytenr = extent_key->objectid;
  2559. block->key.objectid = rc->extent_root->fs_info->nodesize;
  2560. block->key.offset = generation;
  2561. block->level = level;
  2562. block->key_ready = 0;
  2563. rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);
  2564. if (rb_node)
  2565. btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr,
  2566. -EEXIST);
  2567. return 0;
  2568. }
  2569. /*
  2570. * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
  2571. */
  2572. static int __add_tree_block(struct reloc_control *rc,
  2573. u64 bytenr, u32 blocksize,
  2574. struct rb_root *blocks)
  2575. {
  2576. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2577. struct btrfs_path *path;
  2578. struct btrfs_key key;
  2579. int ret;
  2580. bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
  2581. if (tree_block_processed(bytenr, rc))
  2582. return 0;
  2583. if (rb_simple_search(blocks, bytenr))
  2584. return 0;
  2585. path = btrfs_alloc_path();
  2586. if (!path)
  2587. return -ENOMEM;
  2588. again:
  2589. key.objectid = bytenr;
  2590. if (skinny) {
  2591. key.type = BTRFS_METADATA_ITEM_KEY;
  2592. key.offset = (u64)-1;
  2593. } else {
  2594. key.type = BTRFS_EXTENT_ITEM_KEY;
  2595. key.offset = blocksize;
  2596. }
  2597. path->search_commit_root = 1;
  2598. path->skip_locking = 1;
  2599. ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
  2600. if (ret < 0)
  2601. goto out;
  2602. if (ret > 0 && skinny) {
  2603. if (path->slots[0]) {
  2604. path->slots[0]--;
  2605. btrfs_item_key_to_cpu(path->nodes[0], &key,
  2606. path->slots[0]);
  2607. if (key.objectid == bytenr &&
  2608. (key.type == BTRFS_METADATA_ITEM_KEY ||
  2609. (key.type == BTRFS_EXTENT_ITEM_KEY &&
  2610. key.offset == blocksize)))
  2611. ret = 0;
  2612. }
  2613. if (ret) {
  2614. skinny = false;
  2615. btrfs_release_path(path);
  2616. goto again;
  2617. }
  2618. }
  2619. if (ret) {
  2620. ASSERT(ret == 1);
  2621. btrfs_print_leaf(path->nodes[0]);
  2622. btrfs_err(fs_info,
  2623. "tree block extent item (%llu) is not found in extent tree",
  2624. bytenr);
  2625. WARN_ON(1);
  2626. ret = -EINVAL;
  2627. goto out;
  2628. }
  2629. ret = add_tree_block(rc, &key, path, blocks);
  2630. out:
  2631. btrfs_free_path(path);
  2632. return ret;
  2633. }
  2634. static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
  2635. struct btrfs_block_group *block_group,
  2636. struct inode *inode,
  2637. u64 ino)
  2638. {
  2639. struct btrfs_root *root = fs_info->tree_root;
  2640. struct btrfs_trans_handle *trans;
  2641. int ret = 0;
  2642. if (inode)
  2643. goto truncate;
  2644. inode = btrfs_iget(fs_info->sb, ino, root);
  2645. if (IS_ERR(inode))
  2646. return -ENOENT;
  2647. truncate:
  2648. ret = btrfs_check_trunc_cache_free_space(fs_info,
  2649. &fs_info->global_block_rsv);
  2650. if (ret)
  2651. goto out;
  2652. trans = btrfs_join_transaction(root);
  2653. if (IS_ERR(trans)) {
  2654. ret = PTR_ERR(trans);
  2655. goto out;
  2656. }
  2657. ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
  2658. btrfs_end_transaction(trans);
  2659. btrfs_btree_balance_dirty(fs_info);
  2660. out:
  2661. iput(inode);
  2662. return ret;
  2663. }
  2664. /*
  2665. * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
  2666. * cache inode, to avoid free space cache data extent blocking data relocation.
  2667. */
  2668. static int delete_v1_space_cache(struct extent_buffer *leaf,
  2669. struct btrfs_block_group *block_group,
  2670. u64 data_bytenr)
  2671. {
  2672. u64 space_cache_ino;
  2673. struct btrfs_file_extent_item *ei;
  2674. struct btrfs_key key;
  2675. bool found = false;
  2676. int i;
  2677. int ret;
  2678. if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID)
  2679. return 0;
  2680. for (i = 0; i < btrfs_header_nritems(leaf); i++) {
  2681. u8 type;
  2682. btrfs_item_key_to_cpu(leaf, &key, i);
  2683. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2684. continue;
  2685. ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  2686. type = btrfs_file_extent_type(leaf, ei);
  2687. if ((type == BTRFS_FILE_EXTENT_REG ||
  2688. type == BTRFS_FILE_EXTENT_PREALLOC) &&
  2689. btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) {
  2690. found = true;
  2691. space_cache_ino = key.objectid;
  2692. break;
  2693. }
  2694. }
  2695. if (!found)
  2696. return -ENOENT;
  2697. ret = delete_block_group_cache(leaf->fs_info, block_group, NULL,
  2698. space_cache_ino);
  2699. return ret;
  2700. }
  2701. /*
  2702. * helper to find all tree blocks that reference a given data extent
  2703. */
  2704. static noinline_for_stack
  2705. int add_data_references(struct reloc_control *rc,
  2706. struct btrfs_key *extent_key,
  2707. struct btrfs_path *path,
  2708. struct rb_root *blocks)
  2709. {
  2710. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2711. struct ulist *leaves = NULL;
  2712. struct ulist_iterator leaf_uiter;
  2713. struct ulist_node *ref_node = NULL;
  2714. const u32 blocksize = fs_info->nodesize;
  2715. int ret = 0;
  2716. btrfs_release_path(path);
  2717. ret = btrfs_find_all_leafs(NULL, fs_info, extent_key->objectid,
  2718. 0, &leaves, NULL, true);
  2719. if (ret < 0)
  2720. return ret;
  2721. ULIST_ITER_INIT(&leaf_uiter);
  2722. while ((ref_node = ulist_next(leaves, &leaf_uiter))) {
  2723. struct extent_buffer *eb;
  2724. eb = read_tree_block(fs_info, ref_node->val, 0, 0, NULL);
  2725. if (IS_ERR(eb)) {
  2726. ret = PTR_ERR(eb);
  2727. break;
  2728. }
  2729. ret = delete_v1_space_cache(eb, rc->block_group,
  2730. extent_key->objectid);
  2731. free_extent_buffer(eb);
  2732. if (ret < 0)
  2733. break;
  2734. ret = __add_tree_block(rc, ref_node->val, blocksize, blocks);
  2735. if (ret < 0)
  2736. break;
  2737. }
  2738. if (ret < 0)
  2739. free_block_list(blocks);
  2740. ulist_free(leaves);
  2741. return ret;
  2742. }
  2743. /*
  2744. * helper to find next unprocessed extent
  2745. */
  2746. static noinline_for_stack
  2747. int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
  2748. struct btrfs_key *extent_key)
  2749. {
  2750. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2751. struct btrfs_key key;
  2752. struct extent_buffer *leaf;
  2753. u64 start, end, last;
  2754. int ret;
  2755. last = rc->block_group->start + rc->block_group->length;
  2756. while (1) {
  2757. cond_resched();
  2758. if (rc->search_start >= last) {
  2759. ret = 1;
  2760. break;
  2761. }
  2762. key.objectid = rc->search_start;
  2763. key.type = BTRFS_EXTENT_ITEM_KEY;
  2764. key.offset = 0;
  2765. path->search_commit_root = 1;
  2766. path->skip_locking = 1;
  2767. ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
  2768. 0, 0);
  2769. if (ret < 0)
  2770. break;
  2771. next:
  2772. leaf = path->nodes[0];
  2773. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  2774. ret = btrfs_next_leaf(rc->extent_root, path);
  2775. if (ret != 0)
  2776. break;
  2777. leaf = path->nodes[0];
  2778. }
  2779. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2780. if (key.objectid >= last) {
  2781. ret = 1;
  2782. break;
  2783. }
  2784. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  2785. key.type != BTRFS_METADATA_ITEM_KEY) {
  2786. path->slots[0]++;
  2787. goto next;
  2788. }
  2789. if (key.type == BTRFS_EXTENT_ITEM_KEY &&
  2790. key.objectid + key.offset <= rc->search_start) {
  2791. path->slots[0]++;
  2792. goto next;
  2793. }
  2794. if (key.type == BTRFS_METADATA_ITEM_KEY &&
  2795. key.objectid + fs_info->nodesize <=
  2796. rc->search_start) {
  2797. path->slots[0]++;
  2798. goto next;
  2799. }
  2800. ret = find_first_extent_bit(&rc->processed_blocks,
  2801. key.objectid, &start, &end,
  2802. EXTENT_DIRTY, NULL);
  2803. if (ret == 0 && start <= key.objectid) {
  2804. btrfs_release_path(path);
  2805. rc->search_start = end + 1;
  2806. } else {
  2807. if (key.type == BTRFS_EXTENT_ITEM_KEY)
  2808. rc->search_start = key.objectid + key.offset;
  2809. else
  2810. rc->search_start = key.objectid +
  2811. fs_info->nodesize;
  2812. memcpy(extent_key, &key, sizeof(key));
  2813. return 0;
  2814. }
  2815. }
  2816. btrfs_release_path(path);
  2817. return ret;
  2818. }
  2819. static void set_reloc_control(struct reloc_control *rc)
  2820. {
  2821. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2822. mutex_lock(&fs_info->reloc_mutex);
  2823. fs_info->reloc_ctl = rc;
  2824. mutex_unlock(&fs_info->reloc_mutex);
  2825. }
  2826. static void unset_reloc_control(struct reloc_control *rc)
  2827. {
  2828. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2829. mutex_lock(&fs_info->reloc_mutex);
  2830. fs_info->reloc_ctl = NULL;
  2831. mutex_unlock(&fs_info->reloc_mutex);
  2832. }
  2833. static int check_extent_flags(u64 flags)
  2834. {
  2835. if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
  2836. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
  2837. return 1;
  2838. if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
  2839. !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
  2840. return 1;
  2841. if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
  2842. (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
  2843. return 1;
  2844. return 0;
  2845. }
  2846. static noinline_for_stack
  2847. int prepare_to_relocate(struct reloc_control *rc)
  2848. {
  2849. struct btrfs_trans_handle *trans;
  2850. int ret;
  2851. rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
  2852. BTRFS_BLOCK_RSV_TEMP);
  2853. if (!rc->block_rsv)
  2854. return -ENOMEM;
  2855. memset(&rc->cluster, 0, sizeof(rc->cluster));
  2856. rc->search_start = rc->block_group->start;
  2857. rc->extents_found = 0;
  2858. rc->nodes_relocated = 0;
  2859. rc->merging_rsv_size = 0;
  2860. rc->reserved_bytes = 0;
  2861. rc->block_rsv->size = rc->extent_root->fs_info->nodesize *
  2862. RELOCATION_RESERVED_NODES;
  2863. ret = btrfs_block_rsv_refill(rc->extent_root,
  2864. rc->block_rsv, rc->block_rsv->size,
  2865. BTRFS_RESERVE_FLUSH_ALL);
  2866. if (ret)
  2867. return ret;
  2868. rc->create_reloc_tree = 1;
  2869. set_reloc_control(rc);
  2870. trans = btrfs_join_transaction(rc->extent_root);
  2871. if (IS_ERR(trans)) {
  2872. unset_reloc_control(rc);
  2873. /*
  2874. * extent tree is not a ref_cow tree and has no reloc_root to
  2875. * cleanup. And callers are responsible to free the above
  2876. * block rsv.
  2877. */
  2878. return PTR_ERR(trans);
  2879. }
  2880. btrfs_commit_transaction(trans);
  2881. return 0;
  2882. }
  2883. static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
  2884. {
  2885. struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
  2886. struct rb_root blocks = RB_ROOT;
  2887. struct btrfs_key key;
  2888. struct btrfs_trans_handle *trans = NULL;
  2889. struct btrfs_path *path;
  2890. struct btrfs_extent_item *ei;
  2891. u64 flags;
  2892. u32 item_size;
  2893. int ret;
  2894. int err = 0;
  2895. int progress = 0;
  2896. path = btrfs_alloc_path();
  2897. if (!path)
  2898. return -ENOMEM;
  2899. path->reada = READA_FORWARD;
  2900. ret = prepare_to_relocate(rc);
  2901. if (ret) {
  2902. err = ret;
  2903. goto out_free;
  2904. }
  2905. while (1) {
  2906. rc->reserved_bytes = 0;
  2907. ret = btrfs_block_rsv_refill(rc->extent_root,
  2908. rc->block_rsv, rc->block_rsv->size,
  2909. BTRFS_RESERVE_FLUSH_ALL);
  2910. if (ret) {
  2911. err = ret;
  2912. break;
  2913. }
  2914. progress++;
  2915. trans = btrfs_start_transaction(rc->extent_root, 0);
  2916. if (IS_ERR(trans)) {
  2917. err = PTR_ERR(trans);
  2918. trans = NULL;
  2919. break;
  2920. }
  2921. restart:
  2922. if (update_backref_cache(trans, &rc->backref_cache)) {
  2923. btrfs_end_transaction(trans);
  2924. trans = NULL;
  2925. continue;
  2926. }
  2927. ret = find_next_extent(rc, path, &key);
  2928. if (ret < 0)
  2929. err = ret;
  2930. if (ret != 0)
  2931. break;
  2932. rc->extents_found++;
  2933. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2934. struct btrfs_extent_item);
  2935. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  2936. if (item_size >= sizeof(*ei)) {
  2937. flags = btrfs_extent_flags(path->nodes[0], ei);
  2938. ret = check_extent_flags(flags);
  2939. BUG_ON(ret);
  2940. } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
  2941. err = -EINVAL;
  2942. btrfs_print_v0_err(trans->fs_info);
  2943. btrfs_abort_transaction(trans, err);
  2944. break;
  2945. } else {
  2946. BUG();
  2947. }
  2948. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2949. ret = add_tree_block(rc, &key, path, &blocks);
  2950. } else if (rc->stage == UPDATE_DATA_PTRS &&
  2951. (flags & BTRFS_EXTENT_FLAG_DATA)) {
  2952. ret = add_data_references(rc, &key, path, &blocks);
  2953. } else {
  2954. btrfs_release_path(path);
  2955. ret = 0;
  2956. }
  2957. if (ret < 0) {
  2958. err = ret;
  2959. break;
  2960. }
  2961. if (!RB_EMPTY_ROOT(&blocks)) {
  2962. ret = relocate_tree_blocks(trans, rc, &blocks);
  2963. if (ret < 0) {
  2964. if (ret != -EAGAIN) {
  2965. err = ret;
  2966. break;
  2967. }
  2968. rc->extents_found--;
  2969. rc->search_start = key.objectid;
  2970. }
  2971. }
  2972. btrfs_end_transaction_throttle(trans);
  2973. btrfs_btree_balance_dirty(fs_info);
  2974. trans = NULL;
  2975. if (rc->stage == MOVE_DATA_EXTENTS &&
  2976. (flags & BTRFS_EXTENT_FLAG_DATA)) {
  2977. rc->found_file_extent = 1;
  2978. ret = relocate_data_extent(rc->data_inode,
  2979. &key, &rc->cluster);
  2980. if (ret < 0) {
  2981. err = ret;
  2982. break;
  2983. }
  2984. }
  2985. if (btrfs_should_cancel_balance(fs_info)) {
  2986. err = -ECANCELED;
  2987. break;
  2988. }
  2989. }
  2990. if (trans && progress && err == -ENOSPC) {
  2991. ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags);
  2992. if (ret == 1) {
  2993. err = 0;
  2994. progress = 0;
  2995. goto restart;
  2996. }
  2997. }
  2998. btrfs_release_path(path);
  2999. clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
  3000. if (trans) {
  3001. btrfs_end_transaction_throttle(trans);
  3002. btrfs_btree_balance_dirty(fs_info);
  3003. }
  3004. if (!err) {
  3005. ret = relocate_file_extent_cluster(rc->data_inode,
  3006. &rc->cluster);
  3007. if (ret < 0)
  3008. err = ret;
  3009. }
  3010. rc->create_reloc_tree = 0;
  3011. set_reloc_control(rc);
  3012. btrfs_backref_release_cache(&rc->backref_cache);
  3013. btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
  3014. /*
  3015. * Even in the case when the relocation is cancelled, we should all go
  3016. * through prepare_to_merge() and merge_reloc_roots().
  3017. *
  3018. * For error (including cancelled balance), prepare_to_merge() will
  3019. * mark all reloc trees orphan, then queue them for cleanup in
  3020. * merge_reloc_roots()
  3021. */
  3022. err = prepare_to_merge(rc, err);
  3023. merge_reloc_roots(rc);
  3024. rc->merge_reloc_tree = 0;
  3025. unset_reloc_control(rc);
  3026. btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
  3027. /* get rid of pinned extents */
  3028. trans = btrfs_join_transaction(rc->extent_root);
  3029. if (IS_ERR(trans)) {
  3030. err = PTR_ERR(trans);
  3031. goto out_free;
  3032. }
  3033. btrfs_commit_transaction(trans);
  3034. out_free:
  3035. ret = clean_dirty_subvols(rc);
  3036. if (ret < 0 && !err)
  3037. err = ret;
  3038. btrfs_free_block_rsv(fs_info, rc->block_rsv);
  3039. btrfs_free_path(path);
  3040. return err;
  3041. }
  3042. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  3043. struct btrfs_root *root, u64 objectid)
  3044. {
  3045. struct btrfs_path *path;
  3046. struct btrfs_inode_item *item;
  3047. struct extent_buffer *leaf;
  3048. int ret;
  3049. path = btrfs_alloc_path();
  3050. if (!path)
  3051. return -ENOMEM;
  3052. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  3053. if (ret)
  3054. goto out;
  3055. leaf = path->nodes[0];
  3056. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  3057. memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
  3058. btrfs_set_inode_generation(leaf, item, 1);
  3059. btrfs_set_inode_size(leaf, item, 0);
  3060. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  3061. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
  3062. BTRFS_INODE_PREALLOC);
  3063. btrfs_mark_buffer_dirty(leaf);
  3064. out:
  3065. btrfs_free_path(path);
  3066. return ret;
  3067. }
  3068. /*
  3069. * helper to create inode for data relocation.
  3070. * the inode is in data relocation tree and its link count is 0
  3071. */
  3072. static noinline_for_stack
  3073. struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
  3074. struct btrfs_block_group *group)
  3075. {
  3076. struct inode *inode = NULL;
  3077. struct btrfs_trans_handle *trans;
  3078. struct btrfs_root *root;
  3079. u64 objectid;
  3080. int err = 0;
  3081. root = btrfs_grab_root(fs_info->data_reloc_root);
  3082. trans = btrfs_start_transaction(root, 6);
  3083. if (IS_ERR(trans)) {
  3084. btrfs_put_root(root);
  3085. return ERR_CAST(trans);
  3086. }
  3087. err = btrfs_find_free_objectid(root, &objectid);
  3088. if (err)
  3089. goto out;
  3090. err = __insert_orphan_inode(trans, root, objectid);
  3091. BUG_ON(err);
  3092. inode = btrfs_iget(fs_info->sb, objectid, root);
  3093. BUG_ON(IS_ERR(inode));
  3094. BTRFS_I(inode)->index_cnt = group->start;
  3095. err = btrfs_orphan_add(trans, BTRFS_I(inode));
  3096. out:
  3097. btrfs_put_root(root);
  3098. btrfs_end_transaction(trans);
  3099. btrfs_btree_balance_dirty(fs_info);
  3100. if (err) {
  3101. if (inode)
  3102. iput(inode);
  3103. inode = ERR_PTR(err);
  3104. }
  3105. return inode;
  3106. }
  3107. static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
  3108. {
  3109. struct reloc_control *rc;
  3110. rc = kzalloc(sizeof(*rc), GFP_NOFS);
  3111. if (!rc)
  3112. return NULL;
  3113. INIT_LIST_HEAD(&rc->reloc_roots);
  3114. INIT_LIST_HEAD(&rc->dirty_subvol_roots);
  3115. btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);
  3116. mapping_tree_init(&rc->reloc_root_tree);
  3117. extent_io_tree_init(fs_info, &rc->processed_blocks,
  3118. IO_TREE_RELOC_BLOCKS, NULL);
  3119. return rc;
  3120. }
  3121. static void free_reloc_control(struct reloc_control *rc)
  3122. {
  3123. struct mapping_node *node, *tmp;
  3124. free_reloc_roots(&rc->reloc_roots);
  3125. rbtree_postorder_for_each_entry_safe(node, tmp,
  3126. &rc->reloc_root_tree.rb_root, rb_node)
  3127. kfree(node);
  3128. kfree(rc);
  3129. }
  3130. /*
  3131. * Print the block group being relocated
  3132. */
  3133. static void describe_relocation(struct btrfs_fs_info *fs_info,
  3134. struct btrfs_block_group *block_group)
  3135. {
  3136. char buf[128] = {'\0'};
  3137. btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
  3138. btrfs_info(fs_info,
  3139. "relocating block group %llu flags %s",
  3140. block_group->start, buf);
  3141. }
  3142. static const char *stage_to_string(int stage)
  3143. {
  3144. if (stage == MOVE_DATA_EXTENTS)
  3145. return "move data extents";
  3146. if (stage == UPDATE_DATA_PTRS)
  3147. return "update data pointers";
  3148. return "unknown";
  3149. }
  3150. /*
  3151. * function to relocate all extents in a block group.
  3152. */
  3153. int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
  3154. {
  3155. struct btrfs_block_group *bg;
  3156. struct btrfs_root *extent_root = fs_info->extent_root;
  3157. struct reloc_control *rc;
  3158. struct inode *inode;
  3159. struct btrfs_path *path;
  3160. int ret;
  3161. int rw = 0;
  3162. int err = 0;
  3163. bg = btrfs_lookup_block_group(fs_info, group_start);
  3164. if (!bg)
  3165. return -ENOENT;
  3166. if (btrfs_pinned_by_swapfile(fs_info, bg)) {
  3167. btrfs_put_block_group(bg);
  3168. return -ETXTBSY;
  3169. }
  3170. rc = alloc_reloc_control(fs_info);
  3171. if (!rc) {
  3172. btrfs_put_block_group(bg);
  3173. return -ENOMEM;
  3174. }
  3175. rc->extent_root = extent_root;
  3176. rc->block_group = bg;
  3177. ret = btrfs_inc_block_group_ro(rc->block_group, true);
  3178. if (ret) {
  3179. err = ret;
  3180. goto out;
  3181. }
  3182. rw = 1;
  3183. path = btrfs_alloc_path();
  3184. if (!path) {
  3185. err = -ENOMEM;
  3186. goto out;
  3187. }
  3188. inode = lookup_free_space_inode(rc->block_group, path);
  3189. btrfs_free_path(path);
  3190. if (!IS_ERR(inode))
  3191. ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
  3192. else
  3193. ret = PTR_ERR(inode);
  3194. if (ret && ret != -ENOENT) {
  3195. err = ret;
  3196. goto out;
  3197. }
  3198. rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
  3199. if (IS_ERR(rc->data_inode)) {
  3200. err = PTR_ERR(rc->data_inode);
  3201. rc->data_inode = NULL;
  3202. goto out;
  3203. }
  3204. describe_relocation(fs_info, rc->block_group);
  3205. btrfs_wait_block_group_reservations(rc->block_group);
  3206. btrfs_wait_nocow_writers(rc->block_group);
  3207. btrfs_wait_ordered_roots(fs_info, U64_MAX,
  3208. rc->block_group->start,
  3209. rc->block_group->length);
  3210. while (1) {
  3211. int finishes_stage;
  3212. mutex_lock(&fs_info->cleaner_mutex);
  3213. ret = relocate_block_group(rc);
  3214. mutex_unlock(&fs_info->cleaner_mutex);
  3215. if (ret < 0)
  3216. err = ret;
  3217. finishes_stage = rc->stage;
  3218. /*
  3219. * We may have gotten ENOSPC after we already dirtied some
  3220. * extents. If writeout happens while we're relocating a
  3221. * different block group we could end up hitting the
  3222. * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
  3223. * btrfs_reloc_cow_block. Make sure we write everything out
  3224. * properly so we don't trip over this problem, and then break
  3225. * out of the loop if we hit an error.
  3226. */
  3227. if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
  3228. ret = btrfs_wait_ordered_range(rc->data_inode, 0,
  3229. (u64)-1);
  3230. if (ret)
  3231. err = ret;
  3232. invalidate_mapping_pages(rc->data_inode->i_mapping,
  3233. 0, -1);
  3234. rc->stage = UPDATE_DATA_PTRS;
  3235. }
  3236. if (err < 0)
  3237. goto out;
  3238. if (rc->extents_found == 0)
  3239. break;
  3240. btrfs_info(fs_info, "found %llu extents, stage: %s",
  3241. rc->extents_found, stage_to_string(finishes_stage));
  3242. }
  3243. WARN_ON(rc->block_group->pinned > 0);
  3244. WARN_ON(rc->block_group->reserved > 0);
  3245. WARN_ON(rc->block_group->used > 0);
  3246. out:
  3247. if (err && rw)
  3248. btrfs_dec_block_group_ro(rc->block_group);
  3249. iput(rc->data_inode);
  3250. btrfs_put_block_group(rc->block_group);
  3251. free_reloc_control(rc);
  3252. return err;
  3253. }
  3254. static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
  3255. {
  3256. struct btrfs_fs_info *fs_info = root->fs_info;
  3257. struct btrfs_trans_handle *trans;
  3258. int ret, err;
  3259. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  3260. if (IS_ERR(trans))
  3261. return PTR_ERR(trans);
  3262. memset(&root->root_item.drop_progress, 0,
  3263. sizeof(root->root_item.drop_progress));
  3264. root->root_item.drop_level = 0;
  3265. btrfs_set_root_refs(&root->root_item, 0);
  3266. ret = btrfs_update_root(trans, fs_info->tree_root,
  3267. &root->root_key, &root->root_item);
  3268. err = btrfs_end_transaction(trans);
  3269. if (err)
  3270. return err;
  3271. return ret;
  3272. }
  3273. /*
  3274. * recover relocation interrupted by system crash.
  3275. *
  3276. * this function resumes merging reloc trees with corresponding fs trees.
  3277. * this is important for keeping the sharing of tree blocks
  3278. */
  3279. int btrfs_recover_relocation(struct btrfs_root *root)
  3280. {
  3281. struct btrfs_fs_info *fs_info = root->fs_info;
  3282. LIST_HEAD(reloc_roots);
  3283. struct btrfs_key key;
  3284. struct btrfs_root *fs_root;
  3285. struct btrfs_root *reloc_root;
  3286. struct btrfs_path *path;
  3287. struct extent_buffer *leaf;
  3288. struct reloc_control *rc = NULL;
  3289. struct btrfs_trans_handle *trans;
  3290. int ret;
  3291. int err = 0;
  3292. path = btrfs_alloc_path();
  3293. if (!path)
  3294. return -ENOMEM;
  3295. path->reada = READA_BACK;
  3296. key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  3297. key.type = BTRFS_ROOT_ITEM_KEY;
  3298. key.offset = (u64)-1;
  3299. while (1) {
  3300. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
  3301. path, 0, 0);
  3302. if (ret < 0) {
  3303. err = ret;
  3304. goto out;
  3305. }
  3306. if (ret > 0) {
  3307. if (path->slots[0] == 0)
  3308. break;
  3309. path->slots[0]--;
  3310. }
  3311. leaf = path->nodes[0];
  3312. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3313. btrfs_release_path(path);
  3314. if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
  3315. key.type != BTRFS_ROOT_ITEM_KEY)
  3316. break;
  3317. reloc_root = btrfs_read_tree_root(root, &key);
  3318. if (IS_ERR(reloc_root)) {
  3319. err = PTR_ERR(reloc_root);
  3320. goto out;
  3321. }
  3322. set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
  3323. list_add(&reloc_root->root_list, &reloc_roots);
  3324. if (btrfs_root_refs(&reloc_root->root_item) > 0) {
  3325. fs_root = btrfs_get_fs_root(fs_info,
  3326. reloc_root->root_key.offset, false);
  3327. if (IS_ERR(fs_root)) {
  3328. ret = PTR_ERR(fs_root);
  3329. if (ret != -ENOENT) {
  3330. err = ret;
  3331. goto out;
  3332. }
  3333. ret = mark_garbage_root(reloc_root);
  3334. if (ret < 0) {
  3335. err = ret;
  3336. goto out;
  3337. }
  3338. } else {
  3339. btrfs_put_root(fs_root);
  3340. }
  3341. }
  3342. if (key.offset == 0)
  3343. break;
  3344. key.offset--;
  3345. }
  3346. btrfs_release_path(path);
  3347. if (list_empty(&reloc_roots))
  3348. goto out;
  3349. rc = alloc_reloc_control(fs_info);
  3350. if (!rc) {
  3351. err = -ENOMEM;
  3352. goto out;
  3353. }
  3354. rc->extent_root = fs_info->extent_root;
  3355. set_reloc_control(rc);
  3356. trans = btrfs_join_transaction(rc->extent_root);
  3357. if (IS_ERR(trans)) {
  3358. err = PTR_ERR(trans);
  3359. goto out_unset;
  3360. }
  3361. rc->merge_reloc_tree = 1;
  3362. while (!list_empty(&reloc_roots)) {
  3363. reloc_root = list_entry(reloc_roots.next,
  3364. struct btrfs_root, root_list);
  3365. list_del(&reloc_root->root_list);
  3366. if (btrfs_root_refs(&reloc_root->root_item) == 0) {
  3367. list_add_tail(&reloc_root->root_list,
  3368. &rc->reloc_roots);
  3369. continue;
  3370. }
  3371. fs_root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
  3372. false);
  3373. if (IS_ERR(fs_root)) {
  3374. err = PTR_ERR(fs_root);
  3375. list_add_tail(&reloc_root->root_list, &reloc_roots);
  3376. btrfs_end_transaction(trans);
  3377. goto out_unset;
  3378. }
  3379. err = __add_reloc_root(reloc_root);
  3380. BUG_ON(err < 0); /* -ENOMEM or logic error */
  3381. fs_root->reloc_root = btrfs_grab_root(reloc_root);
  3382. btrfs_put_root(fs_root);
  3383. }
  3384. err = btrfs_commit_transaction(trans);
  3385. if (err)
  3386. goto out_unset;
  3387. merge_reloc_roots(rc);
  3388. unset_reloc_control(rc);
  3389. trans = btrfs_join_transaction(rc->extent_root);
  3390. if (IS_ERR(trans)) {
  3391. err = PTR_ERR(trans);
  3392. goto out_clean;
  3393. }
  3394. err = btrfs_commit_transaction(trans);
  3395. out_clean:
  3396. ret = clean_dirty_subvols(rc);
  3397. if (ret < 0 && !err)
  3398. err = ret;
  3399. out_unset:
  3400. unset_reloc_control(rc);
  3401. free_reloc_control(rc);
  3402. out:
  3403. free_reloc_roots(&reloc_roots);
  3404. btrfs_free_path(path);
  3405. if (err == 0) {
  3406. /* cleanup orphan inode in data relocation tree */
  3407. fs_root = btrfs_grab_root(fs_info->data_reloc_root);
  3408. ASSERT(fs_root);
  3409. err = btrfs_orphan_cleanup(fs_root);
  3410. btrfs_put_root(fs_root);
  3411. }
  3412. return err;
  3413. }
  3414. /*
  3415. * helper to add ordered checksum for data relocation.
  3416. *
  3417. * cloning checksum properly handles the nodatasum extents.
  3418. * it also saves CPU time to re-calculate the checksum.
  3419. */
  3420. int btrfs_reloc_clone_csums(struct btrfs_inode *inode, u64 file_pos, u64 len)
  3421. {
  3422. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  3423. struct btrfs_ordered_sum *sums;
  3424. struct btrfs_ordered_extent *ordered;
  3425. int ret;
  3426. u64 disk_bytenr;
  3427. u64 new_bytenr;
  3428. LIST_HEAD(list);
  3429. ordered = btrfs_lookup_ordered_extent(inode, file_pos);
  3430. BUG_ON(ordered->file_offset != file_pos || ordered->num_bytes != len);
  3431. disk_bytenr = file_pos + inode->index_cnt;
  3432. ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr,
  3433. disk_bytenr + len - 1, &list, 0);
  3434. if (ret)
  3435. goto out;
  3436. while (!list_empty(&list)) {
  3437. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  3438. list_del_init(&sums->list);
  3439. /*
  3440. * We need to offset the new_bytenr based on where the csum is.
  3441. * We need to do this because we will read in entire prealloc
  3442. * extents but we may have written to say the middle of the
  3443. * prealloc extent, so we need to make sure the csum goes with
  3444. * the right disk offset.
  3445. *
  3446. * We can do this because the data reloc inode refers strictly
  3447. * to the on disk bytes, so we don't have to worry about
  3448. * disk_len vs real len like with real inodes since it's all
  3449. * disk length.
  3450. */
  3451. new_bytenr = ordered->disk_bytenr + sums->bytenr - disk_bytenr;
  3452. sums->bytenr = new_bytenr;
  3453. btrfs_add_ordered_sum(ordered, sums);
  3454. }
  3455. out:
  3456. btrfs_put_ordered_extent(ordered);
  3457. return ret;
  3458. }
  3459. int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
  3460. struct btrfs_root *root, struct extent_buffer *buf,
  3461. struct extent_buffer *cow)
  3462. {
  3463. struct btrfs_fs_info *fs_info = root->fs_info;
  3464. struct reloc_control *rc;
  3465. struct btrfs_backref_node *node;
  3466. int first_cow = 0;
  3467. int level;
  3468. int ret = 0;
  3469. rc = fs_info->reloc_ctl;
  3470. if (!rc)
  3471. return 0;
  3472. BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
  3473. root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
  3474. level = btrfs_header_level(buf);
  3475. if (btrfs_header_generation(buf) <=
  3476. btrfs_root_last_snapshot(&root->root_item))
  3477. first_cow = 1;
  3478. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
  3479. rc->create_reloc_tree) {
  3480. WARN_ON(!first_cow && level == 0);
  3481. node = rc->backref_cache.path[level];
  3482. BUG_ON(node->bytenr != buf->start &&
  3483. node->new_bytenr != buf->start);
  3484. btrfs_backref_drop_node_buffer(node);
  3485. atomic_inc(&cow->refs);
  3486. node->eb = cow;
  3487. node->new_bytenr = cow->start;
  3488. if (!node->pending) {
  3489. list_move_tail(&node->list,
  3490. &rc->backref_cache.pending[level]);
  3491. node->pending = 1;
  3492. }
  3493. if (first_cow)
  3494. mark_block_processed(rc, node);
  3495. if (first_cow && level > 0)
  3496. rc->nodes_relocated += buf->len;
  3497. }
  3498. if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
  3499. ret = replace_file_extents(trans, rc, root, cow);
  3500. return ret;
  3501. }
  3502. /*
  3503. * called before creating snapshot. it calculates metadata reservation
  3504. * required for relocating tree blocks in the snapshot
  3505. */
  3506. void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
  3507. u64 *bytes_to_reserve)
  3508. {
  3509. struct btrfs_root *root = pending->root;
  3510. struct reloc_control *rc = root->fs_info->reloc_ctl;
  3511. if (!rc || !have_reloc_root(root))
  3512. return;
  3513. if (!rc->merge_reloc_tree)
  3514. return;
  3515. root = root->reloc_root;
  3516. BUG_ON(btrfs_root_refs(&root->root_item) == 0);
  3517. /*
  3518. * relocation is in the stage of merging trees. the space
  3519. * used by merging a reloc tree is twice the size of
  3520. * relocated tree nodes in the worst case. half for cowing
  3521. * the reloc tree, half for cowing the fs tree. the space
  3522. * used by cowing the reloc tree will be freed after the
  3523. * tree is dropped. if we create snapshot, cowing the fs
  3524. * tree may use more space than it frees. so we need
  3525. * reserve extra space.
  3526. */
  3527. *bytes_to_reserve += rc->nodes_relocated;
  3528. }
  3529. /*
  3530. * called after snapshot is created. migrate block reservation
  3531. * and create reloc root for the newly created snapshot
  3532. *
  3533. * This is similar to btrfs_init_reloc_root(), we come out of here with two
  3534. * references held on the reloc_root, one for root->reloc_root and one for
  3535. * rc->reloc_roots.
  3536. */
  3537. int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
  3538. struct btrfs_pending_snapshot *pending)
  3539. {
  3540. struct btrfs_root *root = pending->root;
  3541. struct btrfs_root *reloc_root;
  3542. struct btrfs_root *new_root;
  3543. struct reloc_control *rc = root->fs_info->reloc_ctl;
  3544. int ret;
  3545. if (!rc || !have_reloc_root(root))
  3546. return 0;
  3547. rc = root->fs_info->reloc_ctl;
  3548. rc->merging_rsv_size += rc->nodes_relocated;
  3549. if (rc->merge_reloc_tree) {
  3550. ret = btrfs_block_rsv_migrate(&pending->block_rsv,
  3551. rc->block_rsv,
  3552. rc->nodes_relocated, true);
  3553. if (ret)
  3554. return ret;
  3555. }
  3556. new_root = pending->snap;
  3557. reloc_root = create_reloc_root(trans, root->reloc_root,
  3558. new_root->root_key.objectid);
  3559. if (IS_ERR(reloc_root))
  3560. return PTR_ERR(reloc_root);
  3561. ret = __add_reloc_root(reloc_root);
  3562. BUG_ON(ret < 0);
  3563. new_root->reloc_root = btrfs_grab_root(reloc_root);
  3564. if (rc->create_reloc_tree)
  3565. ret = clone_backref_node(trans, rc, root, reloc_root);
  3566. return ret;
  3567. }