relocation.c 114 KB

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