check-integrity.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295
  1. /*
  2. * Copyright (C) STRATO AG 2011. 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. /*
  19. * This module can be used to catch cases when the btrfs kernel
  20. * code executes write requests to the disk that bring the file
  21. * system in an inconsistent state. In such a state, a power-loss
  22. * or kernel panic event would cause that the data on disk is
  23. * lost or at least damaged.
  24. *
  25. * Code is added that examines all block write requests during
  26. * runtime (including writes of the super block). Three rules
  27. * are verified and an error is printed on violation of the
  28. * rules:
  29. * 1. It is not allowed to write a disk block which is
  30. * currently referenced by the super block (either directly
  31. * or indirectly).
  32. * 2. When a super block is written, it is verified that all
  33. * referenced (directly or indirectly) blocks fulfill the
  34. * following requirements:
  35. * 2a. All referenced blocks have either been present when
  36. * the file system was mounted, (i.e., they have been
  37. * referenced by the super block) or they have been
  38. * written since then and the write completion callback
  39. * was called and no write error was indicated and a
  40. * FLUSH request to the device where these blocks are
  41. * located was received and completed.
  42. * 2b. All referenced blocks need to have a generation
  43. * number which is equal to the parent's number.
  44. *
  45. * One issue that was found using this module was that the log
  46. * tree on disk became temporarily corrupted because disk blocks
  47. * that had been in use for the log tree had been freed and
  48. * reused too early, while being referenced by the written super
  49. * block.
  50. *
  51. * The search term in the kernel log that can be used to filter
  52. * on the existence of detected integrity issues is
  53. * "btrfs: attempt".
  54. *
  55. * The integrity check is enabled via mount options. These
  56. * mount options are only supported if the integrity check
  57. * tool is compiled by defining BTRFS_FS_CHECK_INTEGRITY.
  58. *
  59. * Example #1, apply integrity checks to all metadata:
  60. * mount /dev/sdb1 /mnt -o check_int
  61. *
  62. * Example #2, apply integrity checks to all metadata and
  63. * to data extents:
  64. * mount /dev/sdb1 /mnt -o check_int_data
  65. *
  66. * Example #3, apply integrity checks to all metadata and dump
  67. * the tree that the super block references to kernel messages
  68. * each time after a super block was written:
  69. * mount /dev/sdb1 /mnt -o check_int,check_int_print_mask=263
  70. *
  71. * If the integrity check tool is included and activated in
  72. * the mount options, plenty of kernel memory is used, and
  73. * plenty of additional CPU cycles are spent. Enabling this
  74. * functionality is not intended for normal use. In most
  75. * cases, unless you are a btrfs developer who needs to verify
  76. * the integrity of (super)-block write requests, do not
  77. * enable the config option BTRFS_FS_CHECK_INTEGRITY to
  78. * include and compile the integrity check tool.
  79. *
  80. * Expect millions of lines of information in the kernel log with an
  81. * enabled check_int_print_mask. Therefore set LOG_BUF_SHIFT in the
  82. * kernel config to at least 26 (which is 64MB). Usually the value is
  83. * limited to 21 (which is 2MB) in init/Kconfig. The file needs to be
  84. * changed like this before LOG_BUF_SHIFT can be set to a high value:
  85. * config LOG_BUF_SHIFT
  86. * int "Kernel log buffer size (16 => 64KB, 17 => 128KB)"
  87. * range 12 30
  88. */
  89. #include <linux/sched.h>
  90. #include <linux/slab.h>
  91. #include <linux/buffer_head.h>
  92. #include <linux/mutex.h>
  93. #include <linux/genhd.h>
  94. #include <linux/blkdev.h>
  95. #include "ctree.h"
  96. #include "disk-io.h"
  97. #include "hash.h"
  98. #include "transaction.h"
  99. #include "extent_io.h"
  100. #include "volumes.h"
  101. #include "print-tree.h"
  102. #include "locking.h"
  103. #include "check-integrity.h"
  104. #include "rcu-string.h"
  105. #define BTRFSIC_BLOCK_HASHTABLE_SIZE 0x10000
  106. #define BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE 0x10000
  107. #define BTRFSIC_DEV2STATE_HASHTABLE_SIZE 0x100
  108. #define BTRFSIC_BLOCK_MAGIC_NUMBER 0x14491051
  109. #define BTRFSIC_BLOCK_LINK_MAGIC_NUMBER 0x11070807
  110. #define BTRFSIC_DEV2STATE_MAGIC_NUMBER 0x20111530
  111. #define BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER 20111300
  112. #define BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL (200 - 6) /* in characters,
  113. * excluding " [...]" */
  114. #define BTRFSIC_GENERATION_UNKNOWN ((u64)-1)
  115. /*
  116. * The definition of the bitmask fields for the print_mask.
  117. * They are specified with the mount option check_integrity_print_mask.
  118. */
  119. #define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001
  120. #define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002
  121. #define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004
  122. #define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008
  123. #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010
  124. #define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020
  125. #define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040
  126. #define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080
  127. #define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100
  128. #define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200
  129. #define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400
  130. #define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800
  131. #define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000
  132. #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE 0x00002000
  133. struct btrfsic_dev_state;
  134. struct btrfsic_state;
  135. struct btrfsic_block {
  136. u32 magic_num; /* only used for debug purposes */
  137. unsigned int is_metadata:1; /* if it is meta-data, not data-data */
  138. unsigned int is_superblock:1; /* if it is one of the superblocks */
  139. unsigned int is_iodone:1; /* if is done by lower subsystem */
  140. unsigned int iodone_w_error:1; /* error was indicated to endio */
  141. unsigned int never_written:1; /* block was added because it was
  142. * referenced, not because it was
  143. * written */
  144. unsigned int mirror_num; /* large enough to hold
  145. * BTRFS_SUPER_MIRROR_MAX */
  146. struct btrfsic_dev_state *dev_state;
  147. u64 dev_bytenr; /* key, physical byte num on disk */
  148. u64 logical_bytenr; /* logical byte num on disk */
  149. u64 generation;
  150. struct btrfs_disk_key disk_key; /* extra info to print in case of
  151. * issues, will not always be correct */
  152. struct list_head collision_resolving_node; /* list node */
  153. struct list_head all_blocks_node; /* list node */
  154. /* the following two lists contain block_link items */
  155. struct list_head ref_to_list; /* list */
  156. struct list_head ref_from_list; /* list */
  157. struct btrfsic_block *next_in_same_bio;
  158. void *orig_bio_bh_private;
  159. union {
  160. bio_end_io_t *bio;
  161. bh_end_io_t *bh;
  162. } orig_bio_bh_end_io;
  163. int submit_bio_bh_rw;
  164. u64 flush_gen; /* only valid if !never_written */
  165. };
  166. /*
  167. * Elements of this type are allocated dynamically and required because
  168. * each block object can refer to and can be ref from multiple blocks.
  169. * The key to lookup them in the hashtable is the dev_bytenr of
  170. * the block ref to plus the one from the block refered from.
  171. * The fact that they are searchable via a hashtable and that a
  172. * ref_cnt is maintained is not required for the btrfs integrity
  173. * check algorithm itself, it is only used to make the output more
  174. * beautiful in case that an error is detected (an error is defined
  175. * as a write operation to a block while that block is still referenced).
  176. */
  177. struct btrfsic_block_link {
  178. u32 magic_num; /* only used for debug purposes */
  179. u32 ref_cnt;
  180. struct list_head node_ref_to; /* list node */
  181. struct list_head node_ref_from; /* list node */
  182. struct list_head collision_resolving_node; /* list node */
  183. struct btrfsic_block *block_ref_to;
  184. struct btrfsic_block *block_ref_from;
  185. u64 parent_generation;
  186. };
  187. struct btrfsic_dev_state {
  188. u32 magic_num; /* only used for debug purposes */
  189. struct block_device *bdev;
  190. struct btrfsic_state *state;
  191. struct list_head collision_resolving_node; /* list node */
  192. struct btrfsic_block dummy_block_for_bio_bh_flush;
  193. u64 last_flush_gen;
  194. char name[BDEVNAME_SIZE];
  195. };
  196. struct btrfsic_block_hashtable {
  197. struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
  198. };
  199. struct btrfsic_block_link_hashtable {
  200. struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
  201. };
  202. struct btrfsic_dev_state_hashtable {
  203. struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
  204. };
  205. struct btrfsic_block_data_ctx {
  206. u64 start; /* virtual bytenr */
  207. u64 dev_bytenr; /* physical bytenr on device */
  208. u32 len;
  209. struct btrfsic_dev_state *dev;
  210. char **datav;
  211. struct page **pagev;
  212. void *mem_to_free;
  213. };
  214. /* This structure is used to implement recursion without occupying
  215. * any stack space, refer to btrfsic_process_metablock() */
  216. struct btrfsic_stack_frame {
  217. u32 magic;
  218. u32 nr;
  219. int error;
  220. int i;
  221. int limit_nesting;
  222. int num_copies;
  223. int mirror_num;
  224. struct btrfsic_block *block;
  225. struct btrfsic_block_data_ctx *block_ctx;
  226. struct btrfsic_block *next_block;
  227. struct btrfsic_block_data_ctx next_block_ctx;
  228. struct btrfs_header *hdr;
  229. struct btrfsic_stack_frame *prev;
  230. };
  231. /* Some state per mounted filesystem */
  232. struct btrfsic_state {
  233. u32 print_mask;
  234. int include_extent_data;
  235. int csum_size;
  236. struct list_head all_blocks_list;
  237. struct btrfsic_block_hashtable block_hashtable;
  238. struct btrfsic_block_link_hashtable block_link_hashtable;
  239. struct btrfs_root *root;
  240. u64 max_superblock_generation;
  241. struct btrfsic_block *latest_superblock;
  242. u32 metablock_size;
  243. u32 datablock_size;
  244. };
  245. static void btrfsic_block_init(struct btrfsic_block *b);
  246. static struct btrfsic_block *btrfsic_block_alloc(void);
  247. static void btrfsic_block_free(struct btrfsic_block *b);
  248. static void btrfsic_block_link_init(struct btrfsic_block_link *n);
  249. static struct btrfsic_block_link *btrfsic_block_link_alloc(void);
  250. static void btrfsic_block_link_free(struct btrfsic_block_link *n);
  251. static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds);
  252. static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void);
  253. static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds);
  254. static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h);
  255. static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
  256. struct btrfsic_block_hashtable *h);
  257. static void btrfsic_block_hashtable_remove(struct btrfsic_block *b);
  258. static struct btrfsic_block *btrfsic_block_hashtable_lookup(
  259. struct block_device *bdev,
  260. u64 dev_bytenr,
  261. struct btrfsic_block_hashtable *h);
  262. static void btrfsic_block_link_hashtable_init(
  263. struct btrfsic_block_link_hashtable *h);
  264. static void btrfsic_block_link_hashtable_add(
  265. struct btrfsic_block_link *l,
  266. struct btrfsic_block_link_hashtable *h);
  267. static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l);
  268. static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
  269. struct block_device *bdev_ref_to,
  270. u64 dev_bytenr_ref_to,
  271. struct block_device *bdev_ref_from,
  272. u64 dev_bytenr_ref_from,
  273. struct btrfsic_block_link_hashtable *h);
  274. static void btrfsic_dev_state_hashtable_init(
  275. struct btrfsic_dev_state_hashtable *h);
  276. static void btrfsic_dev_state_hashtable_add(
  277. struct btrfsic_dev_state *ds,
  278. struct btrfsic_dev_state_hashtable *h);
  279. static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds);
  280. static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
  281. struct block_device *bdev,
  282. struct btrfsic_dev_state_hashtable *h);
  283. static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void);
  284. static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf);
  285. static int btrfsic_process_superblock(struct btrfsic_state *state,
  286. struct btrfs_fs_devices *fs_devices);
  287. static int btrfsic_process_metablock(struct btrfsic_state *state,
  288. struct btrfsic_block *block,
  289. struct btrfsic_block_data_ctx *block_ctx,
  290. int limit_nesting, int force_iodone_flag);
  291. static void btrfsic_read_from_block_data(
  292. struct btrfsic_block_data_ctx *block_ctx,
  293. void *dst, u32 offset, size_t len);
  294. static int btrfsic_create_link_to_next_block(
  295. struct btrfsic_state *state,
  296. struct btrfsic_block *block,
  297. struct btrfsic_block_data_ctx
  298. *block_ctx, u64 next_bytenr,
  299. int limit_nesting,
  300. struct btrfsic_block_data_ctx *next_block_ctx,
  301. struct btrfsic_block **next_blockp,
  302. int force_iodone_flag,
  303. int *num_copiesp, int *mirror_nump,
  304. struct btrfs_disk_key *disk_key,
  305. u64 parent_generation);
  306. static int btrfsic_handle_extent_data(struct btrfsic_state *state,
  307. struct btrfsic_block *block,
  308. struct btrfsic_block_data_ctx *block_ctx,
  309. u32 item_offset, int force_iodone_flag);
  310. static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
  311. struct btrfsic_block_data_ctx *block_ctx_out,
  312. int mirror_num);
  313. static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
  314. u32 len, struct block_device *bdev,
  315. struct btrfsic_block_data_ctx *block_ctx_out);
  316. static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx);
  317. static int btrfsic_read_block(struct btrfsic_state *state,
  318. struct btrfsic_block_data_ctx *block_ctx);
  319. static void btrfsic_dump_database(struct btrfsic_state *state);
  320. static int btrfsic_test_for_metadata(struct btrfsic_state *state,
  321. char **datav, unsigned int num_pages);
  322. static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
  323. u64 dev_bytenr, char **mapped_datav,
  324. unsigned int num_pages,
  325. struct bio *bio, int *bio_is_patched,
  326. struct buffer_head *bh,
  327. int submit_bio_bh_rw);
  328. static int btrfsic_process_written_superblock(
  329. struct btrfsic_state *state,
  330. struct btrfsic_block *const block,
  331. struct btrfs_super_block *const super_hdr);
  332. static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status);
  333. static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate);
  334. static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
  335. const struct btrfsic_block *block,
  336. int recursion_level);
  337. static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
  338. struct btrfsic_block *const block,
  339. int recursion_level);
  340. static void btrfsic_print_add_link(const struct btrfsic_state *state,
  341. const struct btrfsic_block_link *l);
  342. static void btrfsic_print_rem_link(const struct btrfsic_state *state,
  343. const struct btrfsic_block_link *l);
  344. static char btrfsic_get_block_type(const struct btrfsic_state *state,
  345. const struct btrfsic_block *block);
  346. static void btrfsic_dump_tree(const struct btrfsic_state *state);
  347. static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
  348. const struct btrfsic_block *block,
  349. int indent_level);
  350. static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
  351. struct btrfsic_state *state,
  352. struct btrfsic_block_data_ctx *next_block_ctx,
  353. struct btrfsic_block *next_block,
  354. struct btrfsic_block *from_block,
  355. u64 parent_generation);
  356. static struct btrfsic_block *btrfsic_block_lookup_or_add(
  357. struct btrfsic_state *state,
  358. struct btrfsic_block_data_ctx *block_ctx,
  359. const char *additional_string,
  360. int is_metadata,
  361. int is_iodone,
  362. int never_written,
  363. int mirror_num,
  364. int *was_created);
  365. static int btrfsic_process_superblock_dev_mirror(
  366. struct btrfsic_state *state,
  367. struct btrfsic_dev_state *dev_state,
  368. struct btrfs_device *device,
  369. int superblock_mirror_num,
  370. struct btrfsic_dev_state **selected_dev_state,
  371. struct btrfs_super_block *selected_super);
  372. static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
  373. struct block_device *bdev);
  374. static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
  375. u64 bytenr,
  376. struct btrfsic_dev_state *dev_state,
  377. u64 dev_bytenr);
  378. static struct mutex btrfsic_mutex;
  379. static int btrfsic_is_initialized;
  380. static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
  381. static void btrfsic_block_init(struct btrfsic_block *b)
  382. {
  383. b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
  384. b->dev_state = NULL;
  385. b->dev_bytenr = 0;
  386. b->logical_bytenr = 0;
  387. b->generation = BTRFSIC_GENERATION_UNKNOWN;
  388. b->disk_key.objectid = 0;
  389. b->disk_key.type = 0;
  390. b->disk_key.offset = 0;
  391. b->is_metadata = 0;
  392. b->is_superblock = 0;
  393. b->is_iodone = 0;
  394. b->iodone_w_error = 0;
  395. b->never_written = 0;
  396. b->mirror_num = 0;
  397. b->next_in_same_bio = NULL;
  398. b->orig_bio_bh_private = NULL;
  399. b->orig_bio_bh_end_io.bio = NULL;
  400. INIT_LIST_HEAD(&b->collision_resolving_node);
  401. INIT_LIST_HEAD(&b->all_blocks_node);
  402. INIT_LIST_HEAD(&b->ref_to_list);
  403. INIT_LIST_HEAD(&b->ref_from_list);
  404. b->submit_bio_bh_rw = 0;
  405. b->flush_gen = 0;
  406. }
  407. static struct btrfsic_block *btrfsic_block_alloc(void)
  408. {
  409. struct btrfsic_block *b;
  410. b = kzalloc(sizeof(*b), GFP_NOFS);
  411. if (NULL != b)
  412. btrfsic_block_init(b);
  413. return b;
  414. }
  415. static void btrfsic_block_free(struct btrfsic_block *b)
  416. {
  417. BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
  418. kfree(b);
  419. }
  420. static void btrfsic_block_link_init(struct btrfsic_block_link *l)
  421. {
  422. l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
  423. l->ref_cnt = 1;
  424. INIT_LIST_HEAD(&l->node_ref_to);
  425. INIT_LIST_HEAD(&l->node_ref_from);
  426. INIT_LIST_HEAD(&l->collision_resolving_node);
  427. l->block_ref_to = NULL;
  428. l->block_ref_from = NULL;
  429. }
  430. static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
  431. {
  432. struct btrfsic_block_link *l;
  433. l = kzalloc(sizeof(*l), GFP_NOFS);
  434. if (NULL != l)
  435. btrfsic_block_link_init(l);
  436. return l;
  437. }
  438. static void btrfsic_block_link_free(struct btrfsic_block_link *l)
  439. {
  440. BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
  441. kfree(l);
  442. }
  443. static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
  444. {
  445. ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
  446. ds->bdev = NULL;
  447. ds->state = NULL;
  448. ds->name[0] = '\0';
  449. INIT_LIST_HEAD(&ds->collision_resolving_node);
  450. ds->last_flush_gen = 0;
  451. btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
  452. ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
  453. ds->dummy_block_for_bio_bh_flush.dev_state = ds;
  454. }
  455. static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
  456. {
  457. struct btrfsic_dev_state *ds;
  458. ds = kzalloc(sizeof(*ds), GFP_NOFS);
  459. if (NULL != ds)
  460. btrfsic_dev_state_init(ds);
  461. return ds;
  462. }
  463. static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
  464. {
  465. BUG_ON(!(NULL == ds ||
  466. BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
  467. kfree(ds);
  468. }
  469. static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
  470. {
  471. int i;
  472. for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
  473. INIT_LIST_HEAD(h->table + i);
  474. }
  475. static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
  476. struct btrfsic_block_hashtable *h)
  477. {
  478. const unsigned int hashval =
  479. (((unsigned int)(b->dev_bytenr >> 16)) ^
  480. ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
  481. (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
  482. list_add(&b->collision_resolving_node, h->table + hashval);
  483. }
  484. static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
  485. {
  486. list_del(&b->collision_resolving_node);
  487. }
  488. static struct btrfsic_block *btrfsic_block_hashtable_lookup(
  489. struct block_device *bdev,
  490. u64 dev_bytenr,
  491. struct btrfsic_block_hashtable *h)
  492. {
  493. const unsigned int hashval =
  494. (((unsigned int)(dev_bytenr >> 16)) ^
  495. ((unsigned int)((uintptr_t)bdev))) &
  496. (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
  497. struct list_head *elem;
  498. list_for_each(elem, h->table + hashval) {
  499. struct btrfsic_block *const b =
  500. list_entry(elem, struct btrfsic_block,
  501. collision_resolving_node);
  502. if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
  503. return b;
  504. }
  505. return NULL;
  506. }
  507. static void btrfsic_block_link_hashtable_init(
  508. struct btrfsic_block_link_hashtable *h)
  509. {
  510. int i;
  511. for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
  512. INIT_LIST_HEAD(h->table + i);
  513. }
  514. static void btrfsic_block_link_hashtable_add(
  515. struct btrfsic_block_link *l,
  516. struct btrfsic_block_link_hashtable *h)
  517. {
  518. const unsigned int hashval =
  519. (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
  520. ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
  521. ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
  522. ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
  523. & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
  524. BUG_ON(NULL == l->block_ref_to);
  525. BUG_ON(NULL == l->block_ref_from);
  526. list_add(&l->collision_resolving_node, h->table + hashval);
  527. }
  528. static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
  529. {
  530. list_del(&l->collision_resolving_node);
  531. }
  532. static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
  533. struct block_device *bdev_ref_to,
  534. u64 dev_bytenr_ref_to,
  535. struct block_device *bdev_ref_from,
  536. u64 dev_bytenr_ref_from,
  537. struct btrfsic_block_link_hashtable *h)
  538. {
  539. const unsigned int hashval =
  540. (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
  541. ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
  542. ((unsigned int)((uintptr_t)bdev_ref_to)) ^
  543. ((unsigned int)((uintptr_t)bdev_ref_from))) &
  544. (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
  545. struct list_head *elem;
  546. list_for_each(elem, h->table + hashval) {
  547. struct btrfsic_block_link *const l =
  548. list_entry(elem, struct btrfsic_block_link,
  549. collision_resolving_node);
  550. BUG_ON(NULL == l->block_ref_to);
  551. BUG_ON(NULL == l->block_ref_from);
  552. if (l->block_ref_to->dev_state->bdev == bdev_ref_to &&
  553. l->block_ref_to->dev_bytenr == dev_bytenr_ref_to &&
  554. l->block_ref_from->dev_state->bdev == bdev_ref_from &&
  555. l->block_ref_from->dev_bytenr == dev_bytenr_ref_from)
  556. return l;
  557. }
  558. return NULL;
  559. }
  560. static void btrfsic_dev_state_hashtable_init(
  561. struct btrfsic_dev_state_hashtable *h)
  562. {
  563. int i;
  564. for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++)
  565. INIT_LIST_HEAD(h->table + i);
  566. }
  567. static void btrfsic_dev_state_hashtable_add(
  568. struct btrfsic_dev_state *ds,
  569. struct btrfsic_dev_state_hashtable *h)
  570. {
  571. const unsigned int hashval =
  572. (((unsigned int)((uintptr_t)ds->bdev)) &
  573. (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
  574. list_add(&ds->collision_resolving_node, h->table + hashval);
  575. }
  576. static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
  577. {
  578. list_del(&ds->collision_resolving_node);
  579. }
  580. static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
  581. struct block_device *bdev,
  582. struct btrfsic_dev_state_hashtable *h)
  583. {
  584. const unsigned int hashval =
  585. (((unsigned int)((uintptr_t)bdev)) &
  586. (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
  587. struct list_head *elem;
  588. list_for_each(elem, h->table + hashval) {
  589. struct btrfsic_dev_state *const ds =
  590. list_entry(elem, struct btrfsic_dev_state,
  591. collision_resolving_node);
  592. if (ds->bdev == bdev)
  593. return ds;
  594. }
  595. return NULL;
  596. }
  597. static int btrfsic_process_superblock(struct btrfsic_state *state,
  598. struct btrfs_fs_devices *fs_devices)
  599. {
  600. int ret = 0;
  601. struct btrfs_super_block *selected_super;
  602. struct list_head *dev_head = &fs_devices->devices;
  603. struct btrfs_device *device;
  604. struct btrfsic_dev_state *selected_dev_state = NULL;
  605. int pass;
  606. BUG_ON(NULL == state);
  607. selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
  608. if (NULL == selected_super) {
  609. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  610. return -1;
  611. }
  612. list_for_each_entry(device, dev_head, dev_list) {
  613. int i;
  614. struct btrfsic_dev_state *dev_state;
  615. if (!device->bdev || !device->name)
  616. continue;
  617. dev_state = btrfsic_dev_state_lookup(device->bdev);
  618. BUG_ON(NULL == dev_state);
  619. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  620. ret = btrfsic_process_superblock_dev_mirror(
  621. state, dev_state, device, i,
  622. &selected_dev_state, selected_super);
  623. if (0 != ret && 0 == i) {
  624. kfree(selected_super);
  625. return ret;
  626. }
  627. }
  628. }
  629. if (NULL == state->latest_superblock) {
  630. printk(KERN_INFO "btrfsic: no superblock found!\n");
  631. kfree(selected_super);
  632. return -1;
  633. }
  634. state->csum_size = btrfs_super_csum_size(selected_super);
  635. for (pass = 0; pass < 3; pass++) {
  636. int num_copies;
  637. int mirror_num;
  638. u64 next_bytenr;
  639. switch (pass) {
  640. case 0:
  641. next_bytenr = btrfs_super_root(selected_super);
  642. if (state->print_mask &
  643. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  644. printk(KERN_INFO "root@%llu\n", next_bytenr);
  645. break;
  646. case 1:
  647. next_bytenr = btrfs_super_chunk_root(selected_super);
  648. if (state->print_mask &
  649. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  650. printk(KERN_INFO "chunk@%llu\n", next_bytenr);
  651. break;
  652. case 2:
  653. next_bytenr = btrfs_super_log_root(selected_super);
  654. if (0 == next_bytenr)
  655. continue;
  656. if (state->print_mask &
  657. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  658. printk(KERN_INFO "log@%llu\n", next_bytenr);
  659. break;
  660. }
  661. num_copies =
  662. btrfs_num_copies(state->root->fs_info,
  663. next_bytenr, state->metablock_size);
  664. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  665. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  666. next_bytenr, num_copies);
  667. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  668. struct btrfsic_block *next_block;
  669. struct btrfsic_block_data_ctx tmp_next_block_ctx;
  670. struct btrfsic_block_link *l;
  671. ret = btrfsic_map_block(state, next_bytenr,
  672. state->metablock_size,
  673. &tmp_next_block_ctx,
  674. mirror_num);
  675. if (ret) {
  676. printk(KERN_INFO "btrfsic:"
  677. " btrfsic_map_block(root @%llu,"
  678. " mirror %d) failed!\n",
  679. next_bytenr, mirror_num);
  680. kfree(selected_super);
  681. return -1;
  682. }
  683. next_block = btrfsic_block_hashtable_lookup(
  684. tmp_next_block_ctx.dev->bdev,
  685. tmp_next_block_ctx.dev_bytenr,
  686. &state->block_hashtable);
  687. BUG_ON(NULL == next_block);
  688. l = btrfsic_block_link_hashtable_lookup(
  689. tmp_next_block_ctx.dev->bdev,
  690. tmp_next_block_ctx.dev_bytenr,
  691. state->latest_superblock->dev_state->
  692. bdev,
  693. state->latest_superblock->dev_bytenr,
  694. &state->block_link_hashtable);
  695. BUG_ON(NULL == l);
  696. ret = btrfsic_read_block(state, &tmp_next_block_ctx);
  697. if (ret < (int)PAGE_CACHE_SIZE) {
  698. printk(KERN_INFO
  699. "btrfsic: read @logical %llu failed!\n",
  700. tmp_next_block_ctx.start);
  701. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  702. kfree(selected_super);
  703. return -1;
  704. }
  705. ret = btrfsic_process_metablock(state,
  706. next_block,
  707. &tmp_next_block_ctx,
  708. BTRFS_MAX_LEVEL + 3, 1);
  709. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  710. }
  711. }
  712. kfree(selected_super);
  713. return ret;
  714. }
  715. static int btrfsic_process_superblock_dev_mirror(
  716. struct btrfsic_state *state,
  717. struct btrfsic_dev_state *dev_state,
  718. struct btrfs_device *device,
  719. int superblock_mirror_num,
  720. struct btrfsic_dev_state **selected_dev_state,
  721. struct btrfs_super_block *selected_super)
  722. {
  723. struct btrfs_super_block *super_tmp;
  724. u64 dev_bytenr;
  725. struct buffer_head *bh;
  726. struct btrfsic_block *superblock_tmp;
  727. int pass;
  728. struct block_device *const superblock_bdev = device->bdev;
  729. /* super block bytenr is always the unmapped device bytenr */
  730. dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
  731. if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
  732. return -1;
  733. bh = __bread(superblock_bdev, dev_bytenr / 4096,
  734. BTRFS_SUPER_INFO_SIZE);
  735. if (NULL == bh)
  736. return -1;
  737. super_tmp = (struct btrfs_super_block *)
  738. (bh->b_data + (dev_bytenr & 4095));
  739. if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
  740. btrfs_super_magic(super_tmp) != BTRFS_MAGIC ||
  741. memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
  742. btrfs_super_nodesize(super_tmp) != state->metablock_size ||
  743. btrfs_super_leafsize(super_tmp) != state->metablock_size ||
  744. btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
  745. brelse(bh);
  746. return 0;
  747. }
  748. superblock_tmp =
  749. btrfsic_block_hashtable_lookup(superblock_bdev,
  750. dev_bytenr,
  751. &state->block_hashtable);
  752. if (NULL == superblock_tmp) {
  753. superblock_tmp = btrfsic_block_alloc();
  754. if (NULL == superblock_tmp) {
  755. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  756. brelse(bh);
  757. return -1;
  758. }
  759. /* for superblock, only the dev_bytenr makes sense */
  760. superblock_tmp->dev_bytenr = dev_bytenr;
  761. superblock_tmp->dev_state = dev_state;
  762. superblock_tmp->logical_bytenr = dev_bytenr;
  763. superblock_tmp->generation = btrfs_super_generation(super_tmp);
  764. superblock_tmp->is_metadata = 1;
  765. superblock_tmp->is_superblock = 1;
  766. superblock_tmp->is_iodone = 1;
  767. superblock_tmp->never_written = 0;
  768. superblock_tmp->mirror_num = 1 + superblock_mirror_num;
  769. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  770. printk_in_rcu(KERN_INFO "New initial S-block (bdev %p, %s)"
  771. " @%llu (%s/%llu/%d)\n",
  772. superblock_bdev,
  773. rcu_str_deref(device->name), dev_bytenr,
  774. dev_state->name, dev_bytenr,
  775. superblock_mirror_num);
  776. list_add(&superblock_tmp->all_blocks_node,
  777. &state->all_blocks_list);
  778. btrfsic_block_hashtable_add(superblock_tmp,
  779. &state->block_hashtable);
  780. }
  781. /* select the one with the highest generation field */
  782. if (btrfs_super_generation(super_tmp) >
  783. state->max_superblock_generation ||
  784. 0 == state->max_superblock_generation) {
  785. memcpy(selected_super, super_tmp, sizeof(*selected_super));
  786. *selected_dev_state = dev_state;
  787. state->max_superblock_generation =
  788. btrfs_super_generation(super_tmp);
  789. state->latest_superblock = superblock_tmp;
  790. }
  791. for (pass = 0; pass < 3; pass++) {
  792. u64 next_bytenr;
  793. int num_copies;
  794. int mirror_num;
  795. const char *additional_string = NULL;
  796. struct btrfs_disk_key tmp_disk_key;
  797. tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
  798. tmp_disk_key.offset = 0;
  799. switch (pass) {
  800. case 0:
  801. btrfs_set_disk_key_objectid(&tmp_disk_key,
  802. BTRFS_ROOT_TREE_OBJECTID);
  803. additional_string = "initial root ";
  804. next_bytenr = btrfs_super_root(super_tmp);
  805. break;
  806. case 1:
  807. btrfs_set_disk_key_objectid(&tmp_disk_key,
  808. BTRFS_CHUNK_TREE_OBJECTID);
  809. additional_string = "initial chunk ";
  810. next_bytenr = btrfs_super_chunk_root(super_tmp);
  811. break;
  812. case 2:
  813. btrfs_set_disk_key_objectid(&tmp_disk_key,
  814. BTRFS_TREE_LOG_OBJECTID);
  815. additional_string = "initial log ";
  816. next_bytenr = btrfs_super_log_root(super_tmp);
  817. if (0 == next_bytenr)
  818. continue;
  819. break;
  820. }
  821. num_copies =
  822. btrfs_num_copies(state->root->fs_info,
  823. next_bytenr, state->metablock_size);
  824. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  825. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  826. next_bytenr, num_copies);
  827. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  828. struct btrfsic_block *next_block;
  829. struct btrfsic_block_data_ctx tmp_next_block_ctx;
  830. struct btrfsic_block_link *l;
  831. if (btrfsic_map_block(state, next_bytenr,
  832. state->metablock_size,
  833. &tmp_next_block_ctx,
  834. mirror_num)) {
  835. printk(KERN_INFO "btrfsic: btrfsic_map_block("
  836. "bytenr @%llu, mirror %d) failed!\n",
  837. next_bytenr, mirror_num);
  838. brelse(bh);
  839. return -1;
  840. }
  841. next_block = btrfsic_block_lookup_or_add(
  842. state, &tmp_next_block_ctx,
  843. additional_string, 1, 1, 0,
  844. mirror_num, NULL);
  845. if (NULL == next_block) {
  846. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  847. brelse(bh);
  848. return -1;
  849. }
  850. next_block->disk_key = tmp_disk_key;
  851. next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
  852. l = btrfsic_block_link_lookup_or_add(
  853. state, &tmp_next_block_ctx,
  854. next_block, superblock_tmp,
  855. BTRFSIC_GENERATION_UNKNOWN);
  856. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  857. if (NULL == l) {
  858. brelse(bh);
  859. return -1;
  860. }
  861. }
  862. }
  863. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
  864. btrfsic_dump_tree_sub(state, superblock_tmp, 0);
  865. brelse(bh);
  866. return 0;
  867. }
  868. static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
  869. {
  870. struct btrfsic_stack_frame *sf;
  871. sf = kzalloc(sizeof(*sf), GFP_NOFS);
  872. if (NULL == sf)
  873. printk(KERN_INFO "btrfsic: alloc memory failed!\n");
  874. else
  875. sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
  876. return sf;
  877. }
  878. static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
  879. {
  880. BUG_ON(!(NULL == sf ||
  881. BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
  882. kfree(sf);
  883. }
  884. static int btrfsic_process_metablock(
  885. struct btrfsic_state *state,
  886. struct btrfsic_block *const first_block,
  887. struct btrfsic_block_data_ctx *const first_block_ctx,
  888. int first_limit_nesting, int force_iodone_flag)
  889. {
  890. struct btrfsic_stack_frame initial_stack_frame = { 0 };
  891. struct btrfsic_stack_frame *sf;
  892. struct btrfsic_stack_frame *next_stack;
  893. struct btrfs_header *const first_hdr =
  894. (struct btrfs_header *)first_block_ctx->datav[0];
  895. BUG_ON(!first_hdr);
  896. sf = &initial_stack_frame;
  897. sf->error = 0;
  898. sf->i = -1;
  899. sf->limit_nesting = first_limit_nesting;
  900. sf->block = first_block;
  901. sf->block_ctx = first_block_ctx;
  902. sf->next_block = NULL;
  903. sf->hdr = first_hdr;
  904. sf->prev = NULL;
  905. continue_with_new_stack_frame:
  906. sf->block->generation = le64_to_cpu(sf->hdr->generation);
  907. if (0 == sf->hdr->level) {
  908. struct btrfs_leaf *const leafhdr =
  909. (struct btrfs_leaf *)sf->hdr;
  910. if (-1 == sf->i) {
  911. sf->nr = btrfs_stack_header_nritems(&leafhdr->header);
  912. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  913. printk(KERN_INFO
  914. "leaf %llu items %d generation %llu"
  915. " owner %llu\n",
  916. sf->block_ctx->start, sf->nr,
  917. btrfs_stack_header_generation(
  918. &leafhdr->header),
  919. btrfs_stack_header_owner(
  920. &leafhdr->header));
  921. }
  922. continue_with_current_leaf_stack_frame:
  923. if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
  924. sf->i++;
  925. sf->num_copies = 0;
  926. }
  927. if (sf->i < sf->nr) {
  928. struct btrfs_item disk_item;
  929. u32 disk_item_offset =
  930. (uintptr_t)(leafhdr->items + sf->i) -
  931. (uintptr_t)leafhdr;
  932. struct btrfs_disk_key *disk_key;
  933. u8 type;
  934. u32 item_offset;
  935. u32 item_size;
  936. if (disk_item_offset + sizeof(struct btrfs_item) >
  937. sf->block_ctx->len) {
  938. leaf_item_out_of_bounce_error:
  939. printk(KERN_INFO
  940. "btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
  941. sf->block_ctx->start,
  942. sf->block_ctx->dev->name);
  943. goto one_stack_frame_backwards;
  944. }
  945. btrfsic_read_from_block_data(sf->block_ctx,
  946. &disk_item,
  947. disk_item_offset,
  948. sizeof(struct btrfs_item));
  949. item_offset = btrfs_stack_item_offset(&disk_item);
  950. item_size = btrfs_stack_item_size(&disk_item);
  951. disk_key = &disk_item.key;
  952. type = btrfs_disk_key_type(disk_key);
  953. if (BTRFS_ROOT_ITEM_KEY == type) {
  954. struct btrfs_root_item root_item;
  955. u32 root_item_offset;
  956. u64 next_bytenr;
  957. root_item_offset = item_offset +
  958. offsetof(struct btrfs_leaf, items);
  959. if (root_item_offset + item_size >
  960. sf->block_ctx->len)
  961. goto leaf_item_out_of_bounce_error;
  962. btrfsic_read_from_block_data(
  963. sf->block_ctx, &root_item,
  964. root_item_offset,
  965. item_size);
  966. next_bytenr = btrfs_root_bytenr(&root_item);
  967. sf->error =
  968. btrfsic_create_link_to_next_block(
  969. state,
  970. sf->block,
  971. sf->block_ctx,
  972. next_bytenr,
  973. sf->limit_nesting,
  974. &sf->next_block_ctx,
  975. &sf->next_block,
  976. force_iodone_flag,
  977. &sf->num_copies,
  978. &sf->mirror_num,
  979. disk_key,
  980. btrfs_root_generation(
  981. &root_item));
  982. if (sf->error)
  983. goto one_stack_frame_backwards;
  984. if (NULL != sf->next_block) {
  985. struct btrfs_header *const next_hdr =
  986. (struct btrfs_header *)
  987. sf->next_block_ctx.datav[0];
  988. next_stack =
  989. btrfsic_stack_frame_alloc();
  990. if (NULL == next_stack) {
  991. sf->error = -1;
  992. btrfsic_release_block_ctx(
  993. &sf->
  994. next_block_ctx);
  995. goto one_stack_frame_backwards;
  996. }
  997. next_stack->i = -1;
  998. next_stack->block = sf->next_block;
  999. next_stack->block_ctx =
  1000. &sf->next_block_ctx;
  1001. next_stack->next_block = NULL;
  1002. next_stack->hdr = next_hdr;
  1003. next_stack->limit_nesting =
  1004. sf->limit_nesting - 1;
  1005. next_stack->prev = sf;
  1006. sf = next_stack;
  1007. goto continue_with_new_stack_frame;
  1008. }
  1009. } else if (BTRFS_EXTENT_DATA_KEY == type &&
  1010. state->include_extent_data) {
  1011. sf->error = btrfsic_handle_extent_data(
  1012. state,
  1013. sf->block,
  1014. sf->block_ctx,
  1015. item_offset,
  1016. force_iodone_flag);
  1017. if (sf->error)
  1018. goto one_stack_frame_backwards;
  1019. }
  1020. goto continue_with_current_leaf_stack_frame;
  1021. }
  1022. } else {
  1023. struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
  1024. if (-1 == sf->i) {
  1025. sf->nr = btrfs_stack_header_nritems(&nodehdr->header);
  1026. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1027. printk(KERN_INFO "node %llu level %d items %d"
  1028. " generation %llu owner %llu\n",
  1029. sf->block_ctx->start,
  1030. nodehdr->header.level, sf->nr,
  1031. btrfs_stack_header_generation(
  1032. &nodehdr->header),
  1033. btrfs_stack_header_owner(
  1034. &nodehdr->header));
  1035. }
  1036. continue_with_current_node_stack_frame:
  1037. if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
  1038. sf->i++;
  1039. sf->num_copies = 0;
  1040. }
  1041. if (sf->i < sf->nr) {
  1042. struct btrfs_key_ptr key_ptr;
  1043. u32 key_ptr_offset;
  1044. u64 next_bytenr;
  1045. key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
  1046. (uintptr_t)nodehdr;
  1047. if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
  1048. sf->block_ctx->len) {
  1049. printk(KERN_INFO
  1050. "btrfsic: node item out of bounce at logical %llu, dev %s\n",
  1051. sf->block_ctx->start,
  1052. sf->block_ctx->dev->name);
  1053. goto one_stack_frame_backwards;
  1054. }
  1055. btrfsic_read_from_block_data(
  1056. sf->block_ctx, &key_ptr, key_ptr_offset,
  1057. sizeof(struct btrfs_key_ptr));
  1058. next_bytenr = btrfs_stack_key_blockptr(&key_ptr);
  1059. sf->error = btrfsic_create_link_to_next_block(
  1060. state,
  1061. sf->block,
  1062. sf->block_ctx,
  1063. next_bytenr,
  1064. sf->limit_nesting,
  1065. &sf->next_block_ctx,
  1066. &sf->next_block,
  1067. force_iodone_flag,
  1068. &sf->num_copies,
  1069. &sf->mirror_num,
  1070. &key_ptr.key,
  1071. btrfs_stack_key_generation(&key_ptr));
  1072. if (sf->error)
  1073. goto one_stack_frame_backwards;
  1074. if (NULL != sf->next_block) {
  1075. struct btrfs_header *const next_hdr =
  1076. (struct btrfs_header *)
  1077. sf->next_block_ctx.datav[0];
  1078. next_stack = btrfsic_stack_frame_alloc();
  1079. if (NULL == next_stack) {
  1080. sf->error = -1;
  1081. goto one_stack_frame_backwards;
  1082. }
  1083. next_stack->i = -1;
  1084. next_stack->block = sf->next_block;
  1085. next_stack->block_ctx = &sf->next_block_ctx;
  1086. next_stack->next_block = NULL;
  1087. next_stack->hdr = next_hdr;
  1088. next_stack->limit_nesting =
  1089. sf->limit_nesting - 1;
  1090. next_stack->prev = sf;
  1091. sf = next_stack;
  1092. goto continue_with_new_stack_frame;
  1093. }
  1094. goto continue_with_current_node_stack_frame;
  1095. }
  1096. }
  1097. one_stack_frame_backwards:
  1098. if (NULL != sf->prev) {
  1099. struct btrfsic_stack_frame *const prev = sf->prev;
  1100. /* the one for the initial block is freed in the caller */
  1101. btrfsic_release_block_ctx(sf->block_ctx);
  1102. if (sf->error) {
  1103. prev->error = sf->error;
  1104. btrfsic_stack_frame_free(sf);
  1105. sf = prev;
  1106. goto one_stack_frame_backwards;
  1107. }
  1108. btrfsic_stack_frame_free(sf);
  1109. sf = prev;
  1110. goto continue_with_new_stack_frame;
  1111. } else {
  1112. BUG_ON(&initial_stack_frame != sf);
  1113. }
  1114. return sf->error;
  1115. }
  1116. static void btrfsic_read_from_block_data(
  1117. struct btrfsic_block_data_ctx *block_ctx,
  1118. void *dstv, u32 offset, size_t len)
  1119. {
  1120. size_t cur;
  1121. size_t offset_in_page;
  1122. char *kaddr;
  1123. char *dst = (char *)dstv;
  1124. size_t start_offset = block_ctx->start & ((u64)PAGE_CACHE_SIZE - 1);
  1125. unsigned long i = (start_offset + offset) >> PAGE_CACHE_SHIFT;
  1126. WARN_ON(offset + len > block_ctx->len);
  1127. offset_in_page = (start_offset + offset) & (PAGE_CACHE_SIZE - 1);
  1128. while (len > 0) {
  1129. cur = min(len, ((size_t)PAGE_CACHE_SIZE - offset_in_page));
  1130. BUG_ON(i >= (block_ctx->len + PAGE_CACHE_SIZE - 1) >>
  1131. PAGE_CACHE_SHIFT);
  1132. kaddr = block_ctx->datav[i];
  1133. memcpy(dst, kaddr + offset_in_page, cur);
  1134. dst += cur;
  1135. len -= cur;
  1136. offset_in_page = 0;
  1137. i++;
  1138. }
  1139. }
  1140. static int btrfsic_create_link_to_next_block(
  1141. struct btrfsic_state *state,
  1142. struct btrfsic_block *block,
  1143. struct btrfsic_block_data_ctx *block_ctx,
  1144. u64 next_bytenr,
  1145. int limit_nesting,
  1146. struct btrfsic_block_data_ctx *next_block_ctx,
  1147. struct btrfsic_block **next_blockp,
  1148. int force_iodone_flag,
  1149. int *num_copiesp, int *mirror_nump,
  1150. struct btrfs_disk_key *disk_key,
  1151. u64 parent_generation)
  1152. {
  1153. struct btrfsic_block *next_block = NULL;
  1154. int ret;
  1155. struct btrfsic_block_link *l;
  1156. int did_alloc_block_link;
  1157. int block_was_created;
  1158. *next_blockp = NULL;
  1159. if (0 == *num_copiesp) {
  1160. *num_copiesp =
  1161. btrfs_num_copies(state->root->fs_info,
  1162. next_bytenr, state->metablock_size);
  1163. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  1164. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  1165. next_bytenr, *num_copiesp);
  1166. *mirror_nump = 1;
  1167. }
  1168. if (*mirror_nump > *num_copiesp)
  1169. return 0;
  1170. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1171. printk(KERN_INFO
  1172. "btrfsic_create_link_to_next_block(mirror_num=%d)\n",
  1173. *mirror_nump);
  1174. ret = btrfsic_map_block(state, next_bytenr,
  1175. state->metablock_size,
  1176. next_block_ctx, *mirror_nump);
  1177. if (ret) {
  1178. printk(KERN_INFO
  1179. "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
  1180. next_bytenr, *mirror_nump);
  1181. btrfsic_release_block_ctx(next_block_ctx);
  1182. *next_blockp = NULL;
  1183. return -1;
  1184. }
  1185. next_block = btrfsic_block_lookup_or_add(state,
  1186. next_block_ctx, "referenced ",
  1187. 1, force_iodone_flag,
  1188. !force_iodone_flag,
  1189. *mirror_nump,
  1190. &block_was_created);
  1191. if (NULL == next_block) {
  1192. btrfsic_release_block_ctx(next_block_ctx);
  1193. *next_blockp = NULL;
  1194. return -1;
  1195. }
  1196. if (block_was_created) {
  1197. l = NULL;
  1198. next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
  1199. } else {
  1200. if (next_block->logical_bytenr != next_bytenr &&
  1201. !(!next_block->is_metadata &&
  1202. 0 == next_block->logical_bytenr)) {
  1203. printk(KERN_INFO
  1204. "Referenced block @%llu (%s/%llu/%d)"
  1205. " found in hash table, %c,"
  1206. " bytenr mismatch (!= stored %llu).\n",
  1207. next_bytenr, next_block_ctx->dev->name,
  1208. next_block_ctx->dev_bytenr, *mirror_nump,
  1209. btrfsic_get_block_type(state, next_block),
  1210. next_block->logical_bytenr);
  1211. } else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1212. printk(KERN_INFO
  1213. "Referenced block @%llu (%s/%llu/%d)"
  1214. " found in hash table, %c.\n",
  1215. next_bytenr, next_block_ctx->dev->name,
  1216. next_block_ctx->dev_bytenr, *mirror_nump,
  1217. btrfsic_get_block_type(state, next_block));
  1218. next_block->logical_bytenr = next_bytenr;
  1219. next_block->mirror_num = *mirror_nump;
  1220. l = btrfsic_block_link_hashtable_lookup(
  1221. next_block_ctx->dev->bdev,
  1222. next_block_ctx->dev_bytenr,
  1223. block_ctx->dev->bdev,
  1224. block_ctx->dev_bytenr,
  1225. &state->block_link_hashtable);
  1226. }
  1227. next_block->disk_key = *disk_key;
  1228. if (NULL == l) {
  1229. l = btrfsic_block_link_alloc();
  1230. if (NULL == l) {
  1231. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  1232. btrfsic_release_block_ctx(next_block_ctx);
  1233. *next_blockp = NULL;
  1234. return -1;
  1235. }
  1236. did_alloc_block_link = 1;
  1237. l->block_ref_to = next_block;
  1238. l->block_ref_from = block;
  1239. l->ref_cnt = 1;
  1240. l->parent_generation = parent_generation;
  1241. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1242. btrfsic_print_add_link(state, l);
  1243. list_add(&l->node_ref_to, &block->ref_to_list);
  1244. list_add(&l->node_ref_from, &next_block->ref_from_list);
  1245. btrfsic_block_link_hashtable_add(l,
  1246. &state->block_link_hashtable);
  1247. } else {
  1248. did_alloc_block_link = 0;
  1249. if (0 == limit_nesting) {
  1250. l->ref_cnt++;
  1251. l->parent_generation = parent_generation;
  1252. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1253. btrfsic_print_add_link(state, l);
  1254. }
  1255. }
  1256. if (limit_nesting > 0 && did_alloc_block_link) {
  1257. ret = btrfsic_read_block(state, next_block_ctx);
  1258. if (ret < (int)next_block_ctx->len) {
  1259. printk(KERN_INFO
  1260. "btrfsic: read block @logical %llu failed!\n",
  1261. next_bytenr);
  1262. btrfsic_release_block_ctx(next_block_ctx);
  1263. *next_blockp = NULL;
  1264. return -1;
  1265. }
  1266. *next_blockp = next_block;
  1267. } else {
  1268. *next_blockp = NULL;
  1269. }
  1270. (*mirror_nump)++;
  1271. return 0;
  1272. }
  1273. static int btrfsic_handle_extent_data(
  1274. struct btrfsic_state *state,
  1275. struct btrfsic_block *block,
  1276. struct btrfsic_block_data_ctx *block_ctx,
  1277. u32 item_offset, int force_iodone_flag)
  1278. {
  1279. int ret;
  1280. struct btrfs_file_extent_item file_extent_item;
  1281. u64 file_extent_item_offset;
  1282. u64 next_bytenr;
  1283. u64 num_bytes;
  1284. u64 generation;
  1285. struct btrfsic_block_link *l;
  1286. file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
  1287. item_offset;
  1288. if (file_extent_item_offset +
  1289. offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
  1290. block_ctx->len) {
  1291. printk(KERN_INFO
  1292. "btrfsic: file item out of bounce at logical %llu, dev %s\n",
  1293. block_ctx->start, block_ctx->dev->name);
  1294. return -1;
  1295. }
  1296. btrfsic_read_from_block_data(block_ctx, &file_extent_item,
  1297. file_extent_item_offset,
  1298. offsetof(struct btrfs_file_extent_item, disk_num_bytes));
  1299. if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
  1300. btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
  1301. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1302. printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu\n",
  1303. file_extent_item.type,
  1304. btrfs_stack_file_extent_disk_bytenr(
  1305. &file_extent_item));
  1306. return 0;
  1307. }
  1308. if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
  1309. block_ctx->len) {
  1310. printk(KERN_INFO
  1311. "btrfsic: file item out of bounce at logical %llu, dev %s\n",
  1312. block_ctx->start, block_ctx->dev->name);
  1313. return -1;
  1314. }
  1315. btrfsic_read_from_block_data(block_ctx, &file_extent_item,
  1316. file_extent_item_offset,
  1317. sizeof(struct btrfs_file_extent_item));
  1318. next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item);
  1319. if (btrfs_stack_file_extent_compression(&file_extent_item) ==
  1320. BTRFS_COMPRESS_NONE) {
  1321. next_bytenr += btrfs_stack_file_extent_offset(&file_extent_item);
  1322. num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
  1323. } else {
  1324. num_bytes = btrfs_stack_file_extent_disk_num_bytes(&file_extent_item);
  1325. }
  1326. generation = btrfs_stack_file_extent_generation(&file_extent_item);
  1327. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1328. printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu,"
  1329. " offset = %llu, num_bytes = %llu\n",
  1330. file_extent_item.type,
  1331. btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
  1332. btrfs_stack_file_extent_offset(&file_extent_item),
  1333. num_bytes);
  1334. while (num_bytes > 0) {
  1335. u32 chunk_len;
  1336. int num_copies;
  1337. int mirror_num;
  1338. if (num_bytes > state->datablock_size)
  1339. chunk_len = state->datablock_size;
  1340. else
  1341. chunk_len = num_bytes;
  1342. num_copies =
  1343. btrfs_num_copies(state->root->fs_info,
  1344. next_bytenr, state->datablock_size);
  1345. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  1346. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  1347. next_bytenr, num_copies);
  1348. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  1349. struct btrfsic_block_data_ctx next_block_ctx;
  1350. struct btrfsic_block *next_block;
  1351. int block_was_created;
  1352. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1353. printk(KERN_INFO "btrfsic_handle_extent_data("
  1354. "mirror_num=%d)\n", mirror_num);
  1355. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1356. printk(KERN_INFO
  1357. "\tdisk_bytenr = %llu, num_bytes %u\n",
  1358. next_bytenr, chunk_len);
  1359. ret = btrfsic_map_block(state, next_bytenr,
  1360. chunk_len, &next_block_ctx,
  1361. mirror_num);
  1362. if (ret) {
  1363. printk(KERN_INFO
  1364. "btrfsic: btrfsic_map_block(@%llu,"
  1365. " mirror=%d) failed!\n",
  1366. next_bytenr, mirror_num);
  1367. return -1;
  1368. }
  1369. next_block = btrfsic_block_lookup_or_add(
  1370. state,
  1371. &next_block_ctx,
  1372. "referenced ",
  1373. 0,
  1374. force_iodone_flag,
  1375. !force_iodone_flag,
  1376. mirror_num,
  1377. &block_was_created);
  1378. if (NULL == next_block) {
  1379. printk(KERN_INFO
  1380. "btrfsic: error, kmalloc failed!\n");
  1381. btrfsic_release_block_ctx(&next_block_ctx);
  1382. return -1;
  1383. }
  1384. if (!block_was_created) {
  1385. if (next_block->logical_bytenr != next_bytenr &&
  1386. !(!next_block->is_metadata &&
  1387. 0 == next_block->logical_bytenr)) {
  1388. printk(KERN_INFO
  1389. "Referenced block"
  1390. " @%llu (%s/%llu/%d)"
  1391. " found in hash table, D,"
  1392. " bytenr mismatch"
  1393. " (!= stored %llu).\n",
  1394. next_bytenr,
  1395. next_block_ctx.dev->name,
  1396. next_block_ctx.dev_bytenr,
  1397. mirror_num,
  1398. next_block->logical_bytenr);
  1399. }
  1400. next_block->logical_bytenr = next_bytenr;
  1401. next_block->mirror_num = mirror_num;
  1402. }
  1403. l = btrfsic_block_link_lookup_or_add(state,
  1404. &next_block_ctx,
  1405. next_block, block,
  1406. generation);
  1407. btrfsic_release_block_ctx(&next_block_ctx);
  1408. if (NULL == l)
  1409. return -1;
  1410. }
  1411. next_bytenr += chunk_len;
  1412. num_bytes -= chunk_len;
  1413. }
  1414. return 0;
  1415. }
  1416. static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
  1417. struct btrfsic_block_data_ctx *block_ctx_out,
  1418. int mirror_num)
  1419. {
  1420. int ret;
  1421. u64 length;
  1422. struct btrfs_bio *multi = NULL;
  1423. struct btrfs_device *device;
  1424. length = len;
  1425. ret = btrfs_map_block(state->root->fs_info, READ,
  1426. bytenr, &length, &multi, mirror_num);
  1427. if (ret) {
  1428. block_ctx_out->start = 0;
  1429. block_ctx_out->dev_bytenr = 0;
  1430. block_ctx_out->len = 0;
  1431. block_ctx_out->dev = NULL;
  1432. block_ctx_out->datav = NULL;
  1433. block_ctx_out->pagev = NULL;
  1434. block_ctx_out->mem_to_free = NULL;
  1435. return ret;
  1436. }
  1437. device = multi->stripes[0].dev;
  1438. block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev);
  1439. block_ctx_out->dev_bytenr = multi->stripes[0].physical;
  1440. block_ctx_out->start = bytenr;
  1441. block_ctx_out->len = len;
  1442. block_ctx_out->datav = NULL;
  1443. block_ctx_out->pagev = NULL;
  1444. block_ctx_out->mem_to_free = NULL;
  1445. kfree(multi);
  1446. if (NULL == block_ctx_out->dev) {
  1447. ret = -ENXIO;
  1448. printk(KERN_INFO "btrfsic: error, cannot lookup dev (#1)!\n");
  1449. }
  1450. return ret;
  1451. }
  1452. static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
  1453. u32 len, struct block_device *bdev,
  1454. struct btrfsic_block_data_ctx *block_ctx_out)
  1455. {
  1456. block_ctx_out->dev = btrfsic_dev_state_lookup(bdev);
  1457. block_ctx_out->dev_bytenr = bytenr;
  1458. block_ctx_out->start = bytenr;
  1459. block_ctx_out->len = len;
  1460. block_ctx_out->datav = NULL;
  1461. block_ctx_out->pagev = NULL;
  1462. block_ctx_out->mem_to_free = NULL;
  1463. if (NULL != block_ctx_out->dev) {
  1464. return 0;
  1465. } else {
  1466. printk(KERN_INFO "btrfsic: error, cannot lookup dev (#2)!\n");
  1467. return -ENXIO;
  1468. }
  1469. }
  1470. static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
  1471. {
  1472. if (block_ctx->mem_to_free) {
  1473. unsigned int num_pages;
  1474. BUG_ON(!block_ctx->datav);
  1475. BUG_ON(!block_ctx->pagev);
  1476. num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
  1477. PAGE_CACHE_SHIFT;
  1478. while (num_pages > 0) {
  1479. num_pages--;
  1480. if (block_ctx->datav[num_pages]) {
  1481. kunmap(block_ctx->pagev[num_pages]);
  1482. block_ctx->datav[num_pages] = NULL;
  1483. }
  1484. if (block_ctx->pagev[num_pages]) {
  1485. __free_page(block_ctx->pagev[num_pages]);
  1486. block_ctx->pagev[num_pages] = NULL;
  1487. }
  1488. }
  1489. kfree(block_ctx->mem_to_free);
  1490. block_ctx->mem_to_free = NULL;
  1491. block_ctx->pagev = NULL;
  1492. block_ctx->datav = NULL;
  1493. }
  1494. }
  1495. static int btrfsic_read_block(struct btrfsic_state *state,
  1496. struct btrfsic_block_data_ctx *block_ctx)
  1497. {
  1498. unsigned int num_pages;
  1499. unsigned int i;
  1500. u64 dev_bytenr;
  1501. int ret;
  1502. BUG_ON(block_ctx->datav);
  1503. BUG_ON(block_ctx->pagev);
  1504. BUG_ON(block_ctx->mem_to_free);
  1505. if (block_ctx->dev_bytenr & ((u64)PAGE_CACHE_SIZE - 1)) {
  1506. printk(KERN_INFO
  1507. "btrfsic: read_block() with unaligned bytenr %llu\n",
  1508. block_ctx->dev_bytenr);
  1509. return -1;
  1510. }
  1511. num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
  1512. PAGE_CACHE_SHIFT;
  1513. block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
  1514. sizeof(*block_ctx->pagev)) *
  1515. num_pages, GFP_NOFS);
  1516. if (!block_ctx->mem_to_free)
  1517. return -1;
  1518. block_ctx->datav = block_ctx->mem_to_free;
  1519. block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
  1520. for (i = 0; i < num_pages; i++) {
  1521. block_ctx->pagev[i] = alloc_page(GFP_NOFS);
  1522. if (!block_ctx->pagev[i])
  1523. return -1;
  1524. }
  1525. dev_bytenr = block_ctx->dev_bytenr;
  1526. for (i = 0; i < num_pages;) {
  1527. struct bio *bio;
  1528. unsigned int j;
  1529. bio = btrfs_io_bio_alloc(GFP_NOFS, num_pages - i);
  1530. if (!bio) {
  1531. printk(KERN_INFO
  1532. "btrfsic: bio_alloc() for %u pages failed!\n",
  1533. num_pages - i);
  1534. return -1;
  1535. }
  1536. bio->bi_bdev = block_ctx->dev->bdev;
  1537. bio->bi_iter.bi_sector = dev_bytenr >> 9;
  1538. for (j = i; j < num_pages; j++) {
  1539. ret = bio_add_page(bio, block_ctx->pagev[j],
  1540. PAGE_CACHE_SIZE, 0);
  1541. if (PAGE_CACHE_SIZE != ret)
  1542. break;
  1543. }
  1544. if (j == i) {
  1545. printk(KERN_INFO
  1546. "btrfsic: error, failed to add a single page!\n");
  1547. return -1;
  1548. }
  1549. if (submit_bio_wait(READ, bio)) {
  1550. printk(KERN_INFO
  1551. "btrfsic: read error at logical %llu dev %s!\n",
  1552. block_ctx->start, block_ctx->dev->name);
  1553. bio_put(bio);
  1554. return -1;
  1555. }
  1556. bio_put(bio);
  1557. dev_bytenr += (j - i) * PAGE_CACHE_SIZE;
  1558. i = j;
  1559. }
  1560. for (i = 0; i < num_pages; i++) {
  1561. block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
  1562. if (!block_ctx->datav[i]) {
  1563. printk(KERN_INFO "btrfsic: kmap() failed (dev %s)!\n",
  1564. block_ctx->dev->name);
  1565. return -1;
  1566. }
  1567. }
  1568. return block_ctx->len;
  1569. }
  1570. static void btrfsic_dump_database(struct btrfsic_state *state)
  1571. {
  1572. struct list_head *elem_all;
  1573. BUG_ON(NULL == state);
  1574. printk(KERN_INFO "all_blocks_list:\n");
  1575. list_for_each(elem_all, &state->all_blocks_list) {
  1576. const struct btrfsic_block *const b_all =
  1577. list_entry(elem_all, struct btrfsic_block,
  1578. all_blocks_node);
  1579. struct list_head *elem_ref_to;
  1580. struct list_head *elem_ref_from;
  1581. printk(KERN_INFO "%c-block @%llu (%s/%llu/%d)\n",
  1582. btrfsic_get_block_type(state, b_all),
  1583. b_all->logical_bytenr, b_all->dev_state->name,
  1584. b_all->dev_bytenr, b_all->mirror_num);
  1585. list_for_each(elem_ref_to, &b_all->ref_to_list) {
  1586. const struct btrfsic_block_link *const l =
  1587. list_entry(elem_ref_to,
  1588. struct btrfsic_block_link,
  1589. node_ref_to);
  1590. printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
  1591. " refers %u* to"
  1592. " %c @%llu (%s/%llu/%d)\n",
  1593. btrfsic_get_block_type(state, b_all),
  1594. b_all->logical_bytenr, b_all->dev_state->name,
  1595. b_all->dev_bytenr, b_all->mirror_num,
  1596. l->ref_cnt,
  1597. btrfsic_get_block_type(state, l->block_ref_to),
  1598. l->block_ref_to->logical_bytenr,
  1599. l->block_ref_to->dev_state->name,
  1600. l->block_ref_to->dev_bytenr,
  1601. l->block_ref_to->mirror_num);
  1602. }
  1603. list_for_each(elem_ref_from, &b_all->ref_from_list) {
  1604. const struct btrfsic_block_link *const l =
  1605. list_entry(elem_ref_from,
  1606. struct btrfsic_block_link,
  1607. node_ref_from);
  1608. printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
  1609. " is ref %u* from"
  1610. " %c @%llu (%s/%llu/%d)\n",
  1611. btrfsic_get_block_type(state, b_all),
  1612. b_all->logical_bytenr, b_all->dev_state->name,
  1613. b_all->dev_bytenr, b_all->mirror_num,
  1614. l->ref_cnt,
  1615. btrfsic_get_block_type(state, l->block_ref_from),
  1616. l->block_ref_from->logical_bytenr,
  1617. l->block_ref_from->dev_state->name,
  1618. l->block_ref_from->dev_bytenr,
  1619. l->block_ref_from->mirror_num);
  1620. }
  1621. printk(KERN_INFO "\n");
  1622. }
  1623. }
  1624. /*
  1625. * Test whether the disk block contains a tree block (leaf or node)
  1626. * (note that this test fails for the super block)
  1627. */
  1628. static int btrfsic_test_for_metadata(struct btrfsic_state *state,
  1629. char **datav, unsigned int num_pages)
  1630. {
  1631. struct btrfs_header *h;
  1632. u8 csum[BTRFS_CSUM_SIZE];
  1633. u32 crc = ~(u32)0;
  1634. unsigned int i;
  1635. if (num_pages * PAGE_CACHE_SIZE < state->metablock_size)
  1636. return 1; /* not metadata */
  1637. num_pages = state->metablock_size >> PAGE_CACHE_SHIFT;
  1638. h = (struct btrfs_header *)datav[0];
  1639. if (memcmp(h->fsid, state->root->fs_info->fsid, BTRFS_UUID_SIZE))
  1640. return 1;
  1641. for (i = 0; i < num_pages; i++) {
  1642. u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
  1643. size_t sublen = i ? PAGE_CACHE_SIZE :
  1644. (PAGE_CACHE_SIZE - BTRFS_CSUM_SIZE);
  1645. crc = btrfs_crc32c(crc, data, sublen);
  1646. }
  1647. btrfs_csum_final(crc, csum);
  1648. if (memcmp(csum, h->csum, state->csum_size))
  1649. return 1;
  1650. return 0; /* is metadata */
  1651. }
  1652. static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
  1653. u64 dev_bytenr, char **mapped_datav,
  1654. unsigned int num_pages,
  1655. struct bio *bio, int *bio_is_patched,
  1656. struct buffer_head *bh,
  1657. int submit_bio_bh_rw)
  1658. {
  1659. int is_metadata;
  1660. struct btrfsic_block *block;
  1661. struct btrfsic_block_data_ctx block_ctx;
  1662. int ret;
  1663. struct btrfsic_state *state = dev_state->state;
  1664. struct block_device *bdev = dev_state->bdev;
  1665. unsigned int processed_len;
  1666. if (NULL != bio_is_patched)
  1667. *bio_is_patched = 0;
  1668. again:
  1669. if (num_pages == 0)
  1670. return;
  1671. processed_len = 0;
  1672. is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
  1673. num_pages));
  1674. block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
  1675. &state->block_hashtable);
  1676. if (NULL != block) {
  1677. u64 bytenr = 0;
  1678. struct list_head *elem_ref_to;
  1679. struct list_head *tmp_ref_to;
  1680. if (block->is_superblock) {
  1681. bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
  1682. mapped_datav[0]);
  1683. if (num_pages * PAGE_CACHE_SIZE <
  1684. BTRFS_SUPER_INFO_SIZE) {
  1685. printk(KERN_INFO
  1686. "btrfsic: cannot work with too short bios!\n");
  1687. return;
  1688. }
  1689. is_metadata = 1;
  1690. BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_CACHE_SIZE - 1));
  1691. processed_len = BTRFS_SUPER_INFO_SIZE;
  1692. if (state->print_mask &
  1693. BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
  1694. printk(KERN_INFO
  1695. "[before new superblock is written]:\n");
  1696. btrfsic_dump_tree_sub(state, block, 0);
  1697. }
  1698. }
  1699. if (is_metadata) {
  1700. if (!block->is_superblock) {
  1701. if (num_pages * PAGE_CACHE_SIZE <
  1702. state->metablock_size) {
  1703. printk(KERN_INFO
  1704. "btrfsic: cannot work with too short bios!\n");
  1705. return;
  1706. }
  1707. processed_len = state->metablock_size;
  1708. bytenr = btrfs_stack_header_bytenr(
  1709. (struct btrfs_header *)
  1710. mapped_datav[0]);
  1711. btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
  1712. dev_state,
  1713. dev_bytenr);
  1714. }
  1715. if (block->logical_bytenr != bytenr &&
  1716. !(!block->is_metadata &&
  1717. block->logical_bytenr == 0))
  1718. printk(KERN_INFO
  1719. "Written block @%llu (%s/%llu/%d)"
  1720. " found in hash table, %c,"
  1721. " bytenr mismatch"
  1722. " (!= stored %llu).\n",
  1723. bytenr, dev_state->name, dev_bytenr,
  1724. block->mirror_num,
  1725. btrfsic_get_block_type(state, block),
  1726. block->logical_bytenr);
  1727. else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1728. printk(KERN_INFO
  1729. "Written block @%llu (%s/%llu/%d)"
  1730. " found in hash table, %c.\n",
  1731. bytenr, dev_state->name, dev_bytenr,
  1732. block->mirror_num,
  1733. btrfsic_get_block_type(state, block));
  1734. block->logical_bytenr = bytenr;
  1735. } else {
  1736. if (num_pages * PAGE_CACHE_SIZE <
  1737. state->datablock_size) {
  1738. printk(KERN_INFO
  1739. "btrfsic: cannot work with too short bios!\n");
  1740. return;
  1741. }
  1742. processed_len = state->datablock_size;
  1743. bytenr = block->logical_bytenr;
  1744. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1745. printk(KERN_INFO
  1746. "Written block @%llu (%s/%llu/%d)"
  1747. " found in hash table, %c.\n",
  1748. bytenr, dev_state->name, dev_bytenr,
  1749. block->mirror_num,
  1750. btrfsic_get_block_type(state, block));
  1751. }
  1752. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1753. printk(KERN_INFO
  1754. "ref_to_list: %cE, ref_from_list: %cE\n",
  1755. list_empty(&block->ref_to_list) ? ' ' : '!',
  1756. list_empty(&block->ref_from_list) ? ' ' : '!');
  1757. if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
  1758. printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
  1759. " @%llu (%s/%llu/%d), old(gen=%llu,"
  1760. " objectid=%llu, type=%d, offset=%llu),"
  1761. " new(gen=%llu),"
  1762. " which is referenced by most recent superblock"
  1763. " (superblockgen=%llu)!\n",
  1764. btrfsic_get_block_type(state, block), bytenr,
  1765. dev_state->name, dev_bytenr, block->mirror_num,
  1766. block->generation,
  1767. btrfs_disk_key_objectid(&block->disk_key),
  1768. block->disk_key.type,
  1769. btrfs_disk_key_offset(&block->disk_key),
  1770. btrfs_stack_header_generation(
  1771. (struct btrfs_header *) mapped_datav[0]),
  1772. state->max_superblock_generation);
  1773. btrfsic_dump_tree(state);
  1774. }
  1775. if (!block->is_iodone && !block->never_written) {
  1776. printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
  1777. " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu,"
  1778. " which is not yet iodone!\n",
  1779. btrfsic_get_block_type(state, block), bytenr,
  1780. dev_state->name, dev_bytenr, block->mirror_num,
  1781. block->generation,
  1782. btrfs_stack_header_generation(
  1783. (struct btrfs_header *)
  1784. mapped_datav[0]));
  1785. /* it would not be safe to go on */
  1786. btrfsic_dump_tree(state);
  1787. goto continue_loop;
  1788. }
  1789. /*
  1790. * Clear all references of this block. Do not free
  1791. * the block itself even if is not referenced anymore
  1792. * because it still carries valueable information
  1793. * like whether it was ever written and IO completed.
  1794. */
  1795. list_for_each_safe(elem_ref_to, tmp_ref_to,
  1796. &block->ref_to_list) {
  1797. struct btrfsic_block_link *const l =
  1798. list_entry(elem_ref_to,
  1799. struct btrfsic_block_link,
  1800. node_ref_to);
  1801. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1802. btrfsic_print_rem_link(state, l);
  1803. l->ref_cnt--;
  1804. if (0 == l->ref_cnt) {
  1805. list_del(&l->node_ref_to);
  1806. list_del(&l->node_ref_from);
  1807. btrfsic_block_link_hashtable_remove(l);
  1808. btrfsic_block_link_free(l);
  1809. }
  1810. }
  1811. if (block->is_superblock)
  1812. ret = btrfsic_map_superblock(state, bytenr,
  1813. processed_len,
  1814. bdev, &block_ctx);
  1815. else
  1816. ret = btrfsic_map_block(state, bytenr, processed_len,
  1817. &block_ctx, 0);
  1818. if (ret) {
  1819. printk(KERN_INFO
  1820. "btrfsic: btrfsic_map_block(root @%llu)"
  1821. " failed!\n", bytenr);
  1822. goto continue_loop;
  1823. }
  1824. block_ctx.datav = mapped_datav;
  1825. /* the following is required in case of writes to mirrors,
  1826. * use the same that was used for the lookup */
  1827. block_ctx.dev = dev_state;
  1828. block_ctx.dev_bytenr = dev_bytenr;
  1829. if (is_metadata || state->include_extent_data) {
  1830. block->never_written = 0;
  1831. block->iodone_w_error = 0;
  1832. if (NULL != bio) {
  1833. block->is_iodone = 0;
  1834. BUG_ON(NULL == bio_is_patched);
  1835. if (!*bio_is_patched) {
  1836. block->orig_bio_bh_private =
  1837. bio->bi_private;
  1838. block->orig_bio_bh_end_io.bio =
  1839. bio->bi_end_io;
  1840. block->next_in_same_bio = NULL;
  1841. bio->bi_private = block;
  1842. bio->bi_end_io = btrfsic_bio_end_io;
  1843. *bio_is_patched = 1;
  1844. } else {
  1845. struct btrfsic_block *chained_block =
  1846. (struct btrfsic_block *)
  1847. bio->bi_private;
  1848. BUG_ON(NULL == chained_block);
  1849. block->orig_bio_bh_private =
  1850. chained_block->orig_bio_bh_private;
  1851. block->orig_bio_bh_end_io.bio =
  1852. chained_block->orig_bio_bh_end_io.
  1853. bio;
  1854. block->next_in_same_bio = chained_block;
  1855. bio->bi_private = block;
  1856. }
  1857. } else if (NULL != bh) {
  1858. block->is_iodone = 0;
  1859. block->orig_bio_bh_private = bh->b_private;
  1860. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  1861. block->next_in_same_bio = NULL;
  1862. bh->b_private = block;
  1863. bh->b_end_io = btrfsic_bh_end_io;
  1864. } else {
  1865. block->is_iodone = 1;
  1866. block->orig_bio_bh_private = NULL;
  1867. block->orig_bio_bh_end_io.bio = NULL;
  1868. block->next_in_same_bio = NULL;
  1869. }
  1870. }
  1871. block->flush_gen = dev_state->last_flush_gen + 1;
  1872. block->submit_bio_bh_rw = submit_bio_bh_rw;
  1873. if (is_metadata) {
  1874. block->logical_bytenr = bytenr;
  1875. block->is_metadata = 1;
  1876. if (block->is_superblock) {
  1877. BUG_ON(PAGE_CACHE_SIZE !=
  1878. BTRFS_SUPER_INFO_SIZE);
  1879. ret = btrfsic_process_written_superblock(
  1880. state,
  1881. block,
  1882. (struct btrfs_super_block *)
  1883. mapped_datav[0]);
  1884. if (state->print_mask &
  1885. BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
  1886. printk(KERN_INFO
  1887. "[after new superblock is written]:\n");
  1888. btrfsic_dump_tree_sub(state, block, 0);
  1889. }
  1890. } else {
  1891. block->mirror_num = 0; /* unknown */
  1892. ret = btrfsic_process_metablock(
  1893. state,
  1894. block,
  1895. &block_ctx,
  1896. 0, 0);
  1897. }
  1898. if (ret)
  1899. printk(KERN_INFO
  1900. "btrfsic: btrfsic_process_metablock"
  1901. "(root @%llu) failed!\n",
  1902. dev_bytenr);
  1903. } else {
  1904. block->is_metadata = 0;
  1905. block->mirror_num = 0; /* unknown */
  1906. block->generation = BTRFSIC_GENERATION_UNKNOWN;
  1907. if (!state->include_extent_data
  1908. && list_empty(&block->ref_from_list)) {
  1909. /*
  1910. * disk block is overwritten with extent
  1911. * data (not meta data) and we are configured
  1912. * to not include extent data: take the
  1913. * chance and free the block's memory
  1914. */
  1915. btrfsic_block_hashtable_remove(block);
  1916. list_del(&block->all_blocks_node);
  1917. btrfsic_block_free(block);
  1918. }
  1919. }
  1920. btrfsic_release_block_ctx(&block_ctx);
  1921. } else {
  1922. /* block has not been found in hash table */
  1923. u64 bytenr;
  1924. if (!is_metadata) {
  1925. processed_len = state->datablock_size;
  1926. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1927. printk(KERN_INFO "Written block (%s/%llu/?)"
  1928. " !found in hash table, D.\n",
  1929. dev_state->name, dev_bytenr);
  1930. if (!state->include_extent_data) {
  1931. /* ignore that written D block */
  1932. goto continue_loop;
  1933. }
  1934. /* this is getting ugly for the
  1935. * include_extent_data case... */
  1936. bytenr = 0; /* unknown */
  1937. block_ctx.start = bytenr;
  1938. block_ctx.len = processed_len;
  1939. block_ctx.mem_to_free = NULL;
  1940. block_ctx.pagev = NULL;
  1941. } else {
  1942. processed_len = state->metablock_size;
  1943. bytenr = btrfs_stack_header_bytenr(
  1944. (struct btrfs_header *)
  1945. mapped_datav[0]);
  1946. btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
  1947. dev_bytenr);
  1948. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1949. printk(KERN_INFO
  1950. "Written block @%llu (%s/%llu/?)"
  1951. " !found in hash table, M.\n",
  1952. bytenr, dev_state->name, dev_bytenr);
  1953. ret = btrfsic_map_block(state, bytenr, processed_len,
  1954. &block_ctx, 0);
  1955. if (ret) {
  1956. printk(KERN_INFO
  1957. "btrfsic: btrfsic_map_block(root @%llu)"
  1958. " failed!\n",
  1959. dev_bytenr);
  1960. goto continue_loop;
  1961. }
  1962. }
  1963. block_ctx.datav = mapped_datav;
  1964. /* the following is required in case of writes to mirrors,
  1965. * use the same that was used for the lookup */
  1966. block_ctx.dev = dev_state;
  1967. block_ctx.dev_bytenr = dev_bytenr;
  1968. block = btrfsic_block_alloc();
  1969. if (NULL == block) {
  1970. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  1971. btrfsic_release_block_ctx(&block_ctx);
  1972. goto continue_loop;
  1973. }
  1974. block->dev_state = dev_state;
  1975. block->dev_bytenr = dev_bytenr;
  1976. block->logical_bytenr = bytenr;
  1977. block->is_metadata = is_metadata;
  1978. block->never_written = 0;
  1979. block->iodone_w_error = 0;
  1980. block->mirror_num = 0; /* unknown */
  1981. block->flush_gen = dev_state->last_flush_gen + 1;
  1982. block->submit_bio_bh_rw = submit_bio_bh_rw;
  1983. if (NULL != bio) {
  1984. block->is_iodone = 0;
  1985. BUG_ON(NULL == bio_is_patched);
  1986. if (!*bio_is_patched) {
  1987. block->orig_bio_bh_private = bio->bi_private;
  1988. block->orig_bio_bh_end_io.bio = bio->bi_end_io;
  1989. block->next_in_same_bio = NULL;
  1990. bio->bi_private = block;
  1991. bio->bi_end_io = btrfsic_bio_end_io;
  1992. *bio_is_patched = 1;
  1993. } else {
  1994. struct btrfsic_block *chained_block =
  1995. (struct btrfsic_block *)
  1996. bio->bi_private;
  1997. BUG_ON(NULL == chained_block);
  1998. block->orig_bio_bh_private =
  1999. chained_block->orig_bio_bh_private;
  2000. block->orig_bio_bh_end_io.bio =
  2001. chained_block->orig_bio_bh_end_io.bio;
  2002. block->next_in_same_bio = chained_block;
  2003. bio->bi_private = block;
  2004. }
  2005. } else if (NULL != bh) {
  2006. block->is_iodone = 0;
  2007. block->orig_bio_bh_private = bh->b_private;
  2008. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  2009. block->next_in_same_bio = NULL;
  2010. bh->b_private = block;
  2011. bh->b_end_io = btrfsic_bh_end_io;
  2012. } else {
  2013. block->is_iodone = 1;
  2014. block->orig_bio_bh_private = NULL;
  2015. block->orig_bio_bh_end_io.bio = NULL;
  2016. block->next_in_same_bio = NULL;
  2017. }
  2018. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2019. printk(KERN_INFO
  2020. "New written %c-block @%llu (%s/%llu/%d)\n",
  2021. is_metadata ? 'M' : 'D',
  2022. block->logical_bytenr, block->dev_state->name,
  2023. block->dev_bytenr, block->mirror_num);
  2024. list_add(&block->all_blocks_node, &state->all_blocks_list);
  2025. btrfsic_block_hashtable_add(block, &state->block_hashtable);
  2026. if (is_metadata) {
  2027. ret = btrfsic_process_metablock(state, block,
  2028. &block_ctx, 0, 0);
  2029. if (ret)
  2030. printk(KERN_INFO
  2031. "btrfsic: process_metablock(root @%llu)"
  2032. " failed!\n",
  2033. dev_bytenr);
  2034. }
  2035. btrfsic_release_block_ctx(&block_ctx);
  2036. }
  2037. continue_loop:
  2038. BUG_ON(!processed_len);
  2039. dev_bytenr += processed_len;
  2040. mapped_datav += processed_len >> PAGE_CACHE_SHIFT;
  2041. num_pages -= processed_len >> PAGE_CACHE_SHIFT;
  2042. goto again;
  2043. }
  2044. static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status)
  2045. {
  2046. struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
  2047. int iodone_w_error;
  2048. /* mutex is not held! This is not save if IO is not yet completed
  2049. * on umount */
  2050. iodone_w_error = 0;
  2051. if (bio_error_status)
  2052. iodone_w_error = 1;
  2053. BUG_ON(NULL == block);
  2054. bp->bi_private = block->orig_bio_bh_private;
  2055. bp->bi_end_io = block->orig_bio_bh_end_io.bio;
  2056. do {
  2057. struct btrfsic_block *next_block;
  2058. struct btrfsic_dev_state *const dev_state = block->dev_state;
  2059. if ((dev_state->state->print_mask &
  2060. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  2061. printk(KERN_INFO
  2062. "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
  2063. bio_error_status,
  2064. btrfsic_get_block_type(dev_state->state, block),
  2065. block->logical_bytenr, dev_state->name,
  2066. block->dev_bytenr, block->mirror_num);
  2067. next_block = block->next_in_same_bio;
  2068. block->iodone_w_error = iodone_w_error;
  2069. if (block->submit_bio_bh_rw & REQ_FLUSH) {
  2070. dev_state->last_flush_gen++;
  2071. if ((dev_state->state->print_mask &
  2072. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  2073. printk(KERN_INFO
  2074. "bio_end_io() new %s flush_gen=%llu\n",
  2075. dev_state->name,
  2076. dev_state->last_flush_gen);
  2077. }
  2078. if (block->submit_bio_bh_rw & REQ_FUA)
  2079. block->flush_gen = 0; /* FUA completed means block is
  2080. * on disk */
  2081. block->is_iodone = 1; /* for FLUSH, this releases the block */
  2082. block = next_block;
  2083. } while (NULL != block);
  2084. bp->bi_end_io(bp, bio_error_status);
  2085. }
  2086. static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
  2087. {
  2088. struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
  2089. int iodone_w_error = !uptodate;
  2090. struct btrfsic_dev_state *dev_state;
  2091. BUG_ON(NULL == block);
  2092. dev_state = block->dev_state;
  2093. if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  2094. printk(KERN_INFO
  2095. "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
  2096. iodone_w_error,
  2097. btrfsic_get_block_type(dev_state->state, block),
  2098. block->logical_bytenr, block->dev_state->name,
  2099. block->dev_bytenr, block->mirror_num);
  2100. block->iodone_w_error = iodone_w_error;
  2101. if (block->submit_bio_bh_rw & REQ_FLUSH) {
  2102. dev_state->last_flush_gen++;
  2103. if ((dev_state->state->print_mask &
  2104. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  2105. printk(KERN_INFO
  2106. "bh_end_io() new %s flush_gen=%llu\n",
  2107. dev_state->name, dev_state->last_flush_gen);
  2108. }
  2109. if (block->submit_bio_bh_rw & REQ_FUA)
  2110. block->flush_gen = 0; /* FUA completed means block is on disk */
  2111. bh->b_private = block->orig_bio_bh_private;
  2112. bh->b_end_io = block->orig_bio_bh_end_io.bh;
  2113. block->is_iodone = 1; /* for FLUSH, this releases the block */
  2114. bh->b_end_io(bh, uptodate);
  2115. }
  2116. static int btrfsic_process_written_superblock(
  2117. struct btrfsic_state *state,
  2118. struct btrfsic_block *const superblock,
  2119. struct btrfs_super_block *const super_hdr)
  2120. {
  2121. int pass;
  2122. superblock->generation = btrfs_super_generation(super_hdr);
  2123. if (!(superblock->generation > state->max_superblock_generation ||
  2124. 0 == state->max_superblock_generation)) {
  2125. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  2126. printk(KERN_INFO
  2127. "btrfsic: superblock @%llu (%s/%llu/%d)"
  2128. " with old gen %llu <= %llu\n",
  2129. superblock->logical_bytenr,
  2130. superblock->dev_state->name,
  2131. superblock->dev_bytenr, superblock->mirror_num,
  2132. btrfs_super_generation(super_hdr),
  2133. state->max_superblock_generation);
  2134. } else {
  2135. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  2136. printk(KERN_INFO
  2137. "btrfsic: got new superblock @%llu (%s/%llu/%d)"
  2138. " with new gen %llu > %llu\n",
  2139. superblock->logical_bytenr,
  2140. superblock->dev_state->name,
  2141. superblock->dev_bytenr, superblock->mirror_num,
  2142. btrfs_super_generation(super_hdr),
  2143. state->max_superblock_generation);
  2144. state->max_superblock_generation =
  2145. btrfs_super_generation(super_hdr);
  2146. state->latest_superblock = superblock;
  2147. }
  2148. for (pass = 0; pass < 3; pass++) {
  2149. int ret;
  2150. u64 next_bytenr;
  2151. struct btrfsic_block *next_block;
  2152. struct btrfsic_block_data_ctx tmp_next_block_ctx;
  2153. struct btrfsic_block_link *l;
  2154. int num_copies;
  2155. int mirror_num;
  2156. const char *additional_string = NULL;
  2157. struct btrfs_disk_key tmp_disk_key = {0};
  2158. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2159. BTRFS_ROOT_ITEM_KEY);
  2160. btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
  2161. switch (pass) {
  2162. case 0:
  2163. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2164. BTRFS_ROOT_TREE_OBJECTID);
  2165. additional_string = "root ";
  2166. next_bytenr = btrfs_super_root(super_hdr);
  2167. if (state->print_mask &
  2168. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2169. printk(KERN_INFO "root@%llu\n", next_bytenr);
  2170. break;
  2171. case 1:
  2172. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2173. BTRFS_CHUNK_TREE_OBJECTID);
  2174. additional_string = "chunk ";
  2175. next_bytenr = btrfs_super_chunk_root(super_hdr);
  2176. if (state->print_mask &
  2177. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2178. printk(KERN_INFO "chunk@%llu\n", next_bytenr);
  2179. break;
  2180. case 2:
  2181. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2182. BTRFS_TREE_LOG_OBJECTID);
  2183. additional_string = "log ";
  2184. next_bytenr = btrfs_super_log_root(super_hdr);
  2185. if (0 == next_bytenr)
  2186. continue;
  2187. if (state->print_mask &
  2188. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2189. printk(KERN_INFO "log@%llu\n", next_bytenr);
  2190. break;
  2191. }
  2192. num_copies =
  2193. btrfs_num_copies(state->root->fs_info,
  2194. next_bytenr, BTRFS_SUPER_INFO_SIZE);
  2195. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  2196. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  2197. next_bytenr, num_copies);
  2198. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2199. int was_created;
  2200. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2201. printk(KERN_INFO
  2202. "btrfsic_process_written_superblock("
  2203. "mirror_num=%d)\n", mirror_num);
  2204. ret = btrfsic_map_block(state, next_bytenr,
  2205. BTRFS_SUPER_INFO_SIZE,
  2206. &tmp_next_block_ctx,
  2207. mirror_num);
  2208. if (ret) {
  2209. printk(KERN_INFO
  2210. "btrfsic: btrfsic_map_block(@%llu,"
  2211. " mirror=%d) failed!\n",
  2212. next_bytenr, mirror_num);
  2213. return -1;
  2214. }
  2215. next_block = btrfsic_block_lookup_or_add(
  2216. state,
  2217. &tmp_next_block_ctx,
  2218. additional_string,
  2219. 1, 0, 1,
  2220. mirror_num,
  2221. &was_created);
  2222. if (NULL == next_block) {
  2223. printk(KERN_INFO
  2224. "btrfsic: error, kmalloc failed!\n");
  2225. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  2226. return -1;
  2227. }
  2228. next_block->disk_key = tmp_disk_key;
  2229. if (was_created)
  2230. next_block->generation =
  2231. BTRFSIC_GENERATION_UNKNOWN;
  2232. l = btrfsic_block_link_lookup_or_add(
  2233. state,
  2234. &tmp_next_block_ctx,
  2235. next_block,
  2236. superblock,
  2237. BTRFSIC_GENERATION_UNKNOWN);
  2238. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  2239. if (NULL == l)
  2240. return -1;
  2241. }
  2242. }
  2243. if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
  2244. btrfsic_dump_tree(state);
  2245. return 0;
  2246. }
  2247. static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
  2248. struct btrfsic_block *const block,
  2249. int recursion_level)
  2250. {
  2251. struct list_head *elem_ref_to;
  2252. int ret = 0;
  2253. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2254. /*
  2255. * Note that this situation can happen and does not
  2256. * indicate an error in regular cases. It happens
  2257. * when disk blocks are freed and later reused.
  2258. * The check-integrity module is not aware of any
  2259. * block free operations, it just recognizes block
  2260. * write operations. Therefore it keeps the linkage
  2261. * information for a block until a block is
  2262. * rewritten. This can temporarily cause incorrect
  2263. * and even circular linkage informations. This
  2264. * causes no harm unless such blocks are referenced
  2265. * by the most recent super block.
  2266. */
  2267. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2268. printk(KERN_INFO
  2269. "btrfsic: abort cyclic linkage (case 1).\n");
  2270. return ret;
  2271. }
  2272. /*
  2273. * This algorithm is recursive because the amount of used stack
  2274. * space is very small and the max recursion depth is limited.
  2275. */
  2276. list_for_each(elem_ref_to, &block->ref_to_list) {
  2277. const struct btrfsic_block_link *const l =
  2278. list_entry(elem_ref_to, struct btrfsic_block_link,
  2279. node_ref_to);
  2280. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2281. printk(KERN_INFO
  2282. "rl=%d, %c @%llu (%s/%llu/%d)"
  2283. " %u* refers to %c @%llu (%s/%llu/%d)\n",
  2284. recursion_level,
  2285. btrfsic_get_block_type(state, block),
  2286. block->logical_bytenr, block->dev_state->name,
  2287. block->dev_bytenr, block->mirror_num,
  2288. l->ref_cnt,
  2289. btrfsic_get_block_type(state, l->block_ref_to),
  2290. l->block_ref_to->logical_bytenr,
  2291. l->block_ref_to->dev_state->name,
  2292. l->block_ref_to->dev_bytenr,
  2293. l->block_ref_to->mirror_num);
  2294. if (l->block_ref_to->never_written) {
  2295. printk(KERN_INFO "btrfs: attempt to write superblock"
  2296. " which references block %c @%llu (%s/%llu/%d)"
  2297. " which is never written!\n",
  2298. btrfsic_get_block_type(state, l->block_ref_to),
  2299. l->block_ref_to->logical_bytenr,
  2300. l->block_ref_to->dev_state->name,
  2301. l->block_ref_to->dev_bytenr,
  2302. l->block_ref_to->mirror_num);
  2303. ret = -1;
  2304. } else if (!l->block_ref_to->is_iodone) {
  2305. printk(KERN_INFO "btrfs: attempt to write superblock"
  2306. " which references block %c @%llu (%s/%llu/%d)"
  2307. " which is not yet iodone!\n",
  2308. btrfsic_get_block_type(state, l->block_ref_to),
  2309. l->block_ref_to->logical_bytenr,
  2310. l->block_ref_to->dev_state->name,
  2311. l->block_ref_to->dev_bytenr,
  2312. l->block_ref_to->mirror_num);
  2313. ret = -1;
  2314. } else if (l->block_ref_to->iodone_w_error) {
  2315. printk(KERN_INFO "btrfs: attempt to write superblock"
  2316. " which references block %c @%llu (%s/%llu/%d)"
  2317. " which has write error!\n",
  2318. btrfsic_get_block_type(state, l->block_ref_to),
  2319. l->block_ref_to->logical_bytenr,
  2320. l->block_ref_to->dev_state->name,
  2321. l->block_ref_to->dev_bytenr,
  2322. l->block_ref_to->mirror_num);
  2323. ret = -1;
  2324. } else if (l->parent_generation !=
  2325. l->block_ref_to->generation &&
  2326. BTRFSIC_GENERATION_UNKNOWN !=
  2327. l->parent_generation &&
  2328. BTRFSIC_GENERATION_UNKNOWN !=
  2329. l->block_ref_to->generation) {
  2330. printk(KERN_INFO "btrfs: attempt to write superblock"
  2331. " which references block %c @%llu (%s/%llu/%d)"
  2332. " with generation %llu !="
  2333. " parent generation %llu!\n",
  2334. btrfsic_get_block_type(state, l->block_ref_to),
  2335. l->block_ref_to->logical_bytenr,
  2336. l->block_ref_to->dev_state->name,
  2337. l->block_ref_to->dev_bytenr,
  2338. l->block_ref_to->mirror_num,
  2339. l->block_ref_to->generation,
  2340. l->parent_generation);
  2341. ret = -1;
  2342. } else if (l->block_ref_to->flush_gen >
  2343. l->block_ref_to->dev_state->last_flush_gen) {
  2344. printk(KERN_INFO "btrfs: attempt to write superblock"
  2345. " which references block %c @%llu (%s/%llu/%d)"
  2346. " which is not flushed out of disk's write cache"
  2347. " (block flush_gen=%llu,"
  2348. " dev->flush_gen=%llu)!\n",
  2349. btrfsic_get_block_type(state, l->block_ref_to),
  2350. l->block_ref_to->logical_bytenr,
  2351. l->block_ref_to->dev_state->name,
  2352. l->block_ref_to->dev_bytenr,
  2353. l->block_ref_to->mirror_num, block->flush_gen,
  2354. l->block_ref_to->dev_state->last_flush_gen);
  2355. ret = -1;
  2356. } else if (-1 == btrfsic_check_all_ref_blocks(state,
  2357. l->block_ref_to,
  2358. recursion_level +
  2359. 1)) {
  2360. ret = -1;
  2361. }
  2362. }
  2363. return ret;
  2364. }
  2365. static int btrfsic_is_block_ref_by_superblock(
  2366. const struct btrfsic_state *state,
  2367. const struct btrfsic_block *block,
  2368. int recursion_level)
  2369. {
  2370. struct list_head *elem_ref_from;
  2371. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2372. /* refer to comment at "abort cyclic linkage (case 1)" */
  2373. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2374. printk(KERN_INFO
  2375. "btrfsic: abort cyclic linkage (case 2).\n");
  2376. return 0;
  2377. }
  2378. /*
  2379. * This algorithm is recursive because the amount of used stack space
  2380. * is very small and the max recursion depth is limited.
  2381. */
  2382. list_for_each(elem_ref_from, &block->ref_from_list) {
  2383. const struct btrfsic_block_link *const l =
  2384. list_entry(elem_ref_from, struct btrfsic_block_link,
  2385. node_ref_from);
  2386. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2387. printk(KERN_INFO
  2388. "rl=%d, %c @%llu (%s/%llu/%d)"
  2389. " is ref %u* from %c @%llu (%s/%llu/%d)\n",
  2390. recursion_level,
  2391. btrfsic_get_block_type(state, block),
  2392. block->logical_bytenr, block->dev_state->name,
  2393. block->dev_bytenr, block->mirror_num,
  2394. l->ref_cnt,
  2395. btrfsic_get_block_type(state, l->block_ref_from),
  2396. l->block_ref_from->logical_bytenr,
  2397. l->block_ref_from->dev_state->name,
  2398. l->block_ref_from->dev_bytenr,
  2399. l->block_ref_from->mirror_num);
  2400. if (l->block_ref_from->is_superblock &&
  2401. state->latest_superblock->dev_bytenr ==
  2402. l->block_ref_from->dev_bytenr &&
  2403. state->latest_superblock->dev_state->bdev ==
  2404. l->block_ref_from->dev_state->bdev)
  2405. return 1;
  2406. else if (btrfsic_is_block_ref_by_superblock(state,
  2407. l->block_ref_from,
  2408. recursion_level +
  2409. 1))
  2410. return 1;
  2411. }
  2412. return 0;
  2413. }
  2414. static void btrfsic_print_add_link(const struct btrfsic_state *state,
  2415. const struct btrfsic_block_link *l)
  2416. {
  2417. printk(KERN_INFO
  2418. "Add %u* link from %c @%llu (%s/%llu/%d)"
  2419. " to %c @%llu (%s/%llu/%d).\n",
  2420. l->ref_cnt,
  2421. btrfsic_get_block_type(state, l->block_ref_from),
  2422. l->block_ref_from->logical_bytenr,
  2423. l->block_ref_from->dev_state->name,
  2424. l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
  2425. btrfsic_get_block_type(state, l->block_ref_to),
  2426. l->block_ref_to->logical_bytenr,
  2427. l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
  2428. l->block_ref_to->mirror_num);
  2429. }
  2430. static void btrfsic_print_rem_link(const struct btrfsic_state *state,
  2431. const struct btrfsic_block_link *l)
  2432. {
  2433. printk(KERN_INFO
  2434. "Rem %u* link from %c @%llu (%s/%llu/%d)"
  2435. " to %c @%llu (%s/%llu/%d).\n",
  2436. l->ref_cnt,
  2437. btrfsic_get_block_type(state, l->block_ref_from),
  2438. l->block_ref_from->logical_bytenr,
  2439. l->block_ref_from->dev_state->name,
  2440. l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
  2441. btrfsic_get_block_type(state, l->block_ref_to),
  2442. l->block_ref_to->logical_bytenr,
  2443. l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
  2444. l->block_ref_to->mirror_num);
  2445. }
  2446. static char btrfsic_get_block_type(const struct btrfsic_state *state,
  2447. const struct btrfsic_block *block)
  2448. {
  2449. if (block->is_superblock &&
  2450. state->latest_superblock->dev_bytenr == block->dev_bytenr &&
  2451. state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
  2452. return 'S';
  2453. else if (block->is_superblock)
  2454. return 's';
  2455. else if (block->is_metadata)
  2456. return 'M';
  2457. else
  2458. return 'D';
  2459. }
  2460. static void btrfsic_dump_tree(const struct btrfsic_state *state)
  2461. {
  2462. btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
  2463. }
  2464. static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
  2465. const struct btrfsic_block *block,
  2466. int indent_level)
  2467. {
  2468. struct list_head *elem_ref_to;
  2469. int indent_add;
  2470. static char buf[80];
  2471. int cursor_position;
  2472. /*
  2473. * Should better fill an on-stack buffer with a complete line and
  2474. * dump it at once when it is time to print a newline character.
  2475. */
  2476. /*
  2477. * This algorithm is recursive because the amount of used stack space
  2478. * is very small and the max recursion depth is limited.
  2479. */
  2480. indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)",
  2481. btrfsic_get_block_type(state, block),
  2482. block->logical_bytenr, block->dev_state->name,
  2483. block->dev_bytenr, block->mirror_num);
  2484. if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2485. printk("[...]\n");
  2486. return;
  2487. }
  2488. printk(buf);
  2489. indent_level += indent_add;
  2490. if (list_empty(&block->ref_to_list)) {
  2491. printk("\n");
  2492. return;
  2493. }
  2494. if (block->mirror_num > 1 &&
  2495. !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
  2496. printk(" [...]\n");
  2497. return;
  2498. }
  2499. cursor_position = indent_level;
  2500. list_for_each(elem_ref_to, &block->ref_to_list) {
  2501. const struct btrfsic_block_link *const l =
  2502. list_entry(elem_ref_to, struct btrfsic_block_link,
  2503. node_ref_to);
  2504. while (cursor_position < indent_level) {
  2505. printk(" ");
  2506. cursor_position++;
  2507. }
  2508. if (l->ref_cnt > 1)
  2509. indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
  2510. else
  2511. indent_add = sprintf(buf, " --> ");
  2512. if (indent_level + indent_add >
  2513. BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2514. printk("[...]\n");
  2515. cursor_position = 0;
  2516. continue;
  2517. }
  2518. printk(buf);
  2519. btrfsic_dump_tree_sub(state, l->block_ref_to,
  2520. indent_level + indent_add);
  2521. cursor_position = 0;
  2522. }
  2523. }
  2524. static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
  2525. struct btrfsic_state *state,
  2526. struct btrfsic_block_data_ctx *next_block_ctx,
  2527. struct btrfsic_block *next_block,
  2528. struct btrfsic_block *from_block,
  2529. u64 parent_generation)
  2530. {
  2531. struct btrfsic_block_link *l;
  2532. l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
  2533. next_block_ctx->dev_bytenr,
  2534. from_block->dev_state->bdev,
  2535. from_block->dev_bytenr,
  2536. &state->block_link_hashtable);
  2537. if (NULL == l) {
  2538. l = btrfsic_block_link_alloc();
  2539. if (NULL == l) {
  2540. printk(KERN_INFO
  2541. "btrfsic: error, kmalloc" " failed!\n");
  2542. return NULL;
  2543. }
  2544. l->block_ref_to = next_block;
  2545. l->block_ref_from = from_block;
  2546. l->ref_cnt = 1;
  2547. l->parent_generation = parent_generation;
  2548. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2549. btrfsic_print_add_link(state, l);
  2550. list_add(&l->node_ref_to, &from_block->ref_to_list);
  2551. list_add(&l->node_ref_from, &next_block->ref_from_list);
  2552. btrfsic_block_link_hashtable_add(l,
  2553. &state->block_link_hashtable);
  2554. } else {
  2555. l->ref_cnt++;
  2556. l->parent_generation = parent_generation;
  2557. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2558. btrfsic_print_add_link(state, l);
  2559. }
  2560. return l;
  2561. }
  2562. static struct btrfsic_block *btrfsic_block_lookup_or_add(
  2563. struct btrfsic_state *state,
  2564. struct btrfsic_block_data_ctx *block_ctx,
  2565. const char *additional_string,
  2566. int is_metadata,
  2567. int is_iodone,
  2568. int never_written,
  2569. int mirror_num,
  2570. int *was_created)
  2571. {
  2572. struct btrfsic_block *block;
  2573. block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
  2574. block_ctx->dev_bytenr,
  2575. &state->block_hashtable);
  2576. if (NULL == block) {
  2577. struct btrfsic_dev_state *dev_state;
  2578. block = btrfsic_block_alloc();
  2579. if (NULL == block) {
  2580. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  2581. return NULL;
  2582. }
  2583. dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
  2584. if (NULL == dev_state) {
  2585. printk(KERN_INFO
  2586. "btrfsic: error, lookup dev_state failed!\n");
  2587. btrfsic_block_free(block);
  2588. return NULL;
  2589. }
  2590. block->dev_state = dev_state;
  2591. block->dev_bytenr = block_ctx->dev_bytenr;
  2592. block->logical_bytenr = block_ctx->start;
  2593. block->is_metadata = is_metadata;
  2594. block->is_iodone = is_iodone;
  2595. block->never_written = never_written;
  2596. block->mirror_num = mirror_num;
  2597. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2598. printk(KERN_INFO
  2599. "New %s%c-block @%llu (%s/%llu/%d)\n",
  2600. additional_string,
  2601. btrfsic_get_block_type(state, block),
  2602. block->logical_bytenr, dev_state->name,
  2603. block->dev_bytenr, mirror_num);
  2604. list_add(&block->all_blocks_node, &state->all_blocks_list);
  2605. btrfsic_block_hashtable_add(block, &state->block_hashtable);
  2606. if (NULL != was_created)
  2607. *was_created = 1;
  2608. } else {
  2609. if (NULL != was_created)
  2610. *was_created = 0;
  2611. }
  2612. return block;
  2613. }
  2614. static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
  2615. u64 bytenr,
  2616. struct btrfsic_dev_state *dev_state,
  2617. u64 dev_bytenr)
  2618. {
  2619. int num_copies;
  2620. int mirror_num;
  2621. int ret;
  2622. struct btrfsic_block_data_ctx block_ctx;
  2623. int match = 0;
  2624. num_copies = btrfs_num_copies(state->root->fs_info,
  2625. bytenr, state->metablock_size);
  2626. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2627. ret = btrfsic_map_block(state, bytenr, state->metablock_size,
  2628. &block_ctx, mirror_num);
  2629. if (ret) {
  2630. printk(KERN_INFO "btrfsic:"
  2631. " btrfsic_map_block(logical @%llu,"
  2632. " mirror %d) failed!\n",
  2633. bytenr, mirror_num);
  2634. continue;
  2635. }
  2636. if (dev_state->bdev == block_ctx.dev->bdev &&
  2637. dev_bytenr == block_ctx.dev_bytenr) {
  2638. match++;
  2639. btrfsic_release_block_ctx(&block_ctx);
  2640. break;
  2641. }
  2642. btrfsic_release_block_ctx(&block_ctx);
  2643. }
  2644. if (WARN_ON(!match)) {
  2645. printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
  2646. " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
  2647. " phys_bytenr=%llu)!\n",
  2648. bytenr, dev_state->name, dev_bytenr);
  2649. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2650. ret = btrfsic_map_block(state, bytenr,
  2651. state->metablock_size,
  2652. &block_ctx, mirror_num);
  2653. if (ret)
  2654. continue;
  2655. printk(KERN_INFO "Read logical bytenr @%llu maps to"
  2656. " (%s/%llu/%d)\n",
  2657. bytenr, block_ctx.dev->name,
  2658. block_ctx.dev_bytenr, mirror_num);
  2659. }
  2660. }
  2661. }
  2662. static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
  2663. struct block_device *bdev)
  2664. {
  2665. struct btrfsic_dev_state *ds;
  2666. ds = btrfsic_dev_state_hashtable_lookup(bdev,
  2667. &btrfsic_dev_state_hashtable);
  2668. return ds;
  2669. }
  2670. int btrfsic_submit_bh(int rw, struct buffer_head *bh)
  2671. {
  2672. struct btrfsic_dev_state *dev_state;
  2673. if (!btrfsic_is_initialized)
  2674. return submit_bh(rw, bh);
  2675. mutex_lock(&btrfsic_mutex);
  2676. /* since btrfsic_submit_bh() might also be called before
  2677. * btrfsic_mount(), this might return NULL */
  2678. dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
  2679. /* Only called to write the superblock (incl. FLUSH/FUA) */
  2680. if (NULL != dev_state &&
  2681. (rw & WRITE) && bh->b_size > 0) {
  2682. u64 dev_bytenr;
  2683. dev_bytenr = 4096 * bh->b_blocknr;
  2684. if (dev_state->state->print_mask &
  2685. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2686. printk(KERN_INFO
  2687. "submit_bh(rw=0x%x, blocknr=%llu (bytenr %llu),"
  2688. " size=%zu, data=%p, bdev=%p)\n",
  2689. rw, (unsigned long long)bh->b_blocknr,
  2690. dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev);
  2691. btrfsic_process_written_block(dev_state, dev_bytenr,
  2692. &bh->b_data, 1, NULL,
  2693. NULL, bh, rw);
  2694. } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
  2695. if (dev_state->state->print_mask &
  2696. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2697. printk(KERN_INFO
  2698. "submit_bh(rw=0x%x FLUSH, bdev=%p)\n",
  2699. rw, bh->b_bdev);
  2700. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2701. if ((dev_state->state->print_mask &
  2702. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2703. BTRFSIC_PRINT_MASK_VERBOSE)))
  2704. printk(KERN_INFO
  2705. "btrfsic_submit_bh(%s) with FLUSH"
  2706. " but dummy block already in use"
  2707. " (ignored)!\n",
  2708. dev_state->name);
  2709. } else {
  2710. struct btrfsic_block *const block =
  2711. &dev_state->dummy_block_for_bio_bh_flush;
  2712. block->is_iodone = 0;
  2713. block->never_written = 0;
  2714. block->iodone_w_error = 0;
  2715. block->flush_gen = dev_state->last_flush_gen + 1;
  2716. block->submit_bio_bh_rw = rw;
  2717. block->orig_bio_bh_private = bh->b_private;
  2718. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  2719. block->next_in_same_bio = NULL;
  2720. bh->b_private = block;
  2721. bh->b_end_io = btrfsic_bh_end_io;
  2722. }
  2723. }
  2724. mutex_unlock(&btrfsic_mutex);
  2725. return submit_bh(rw, bh);
  2726. }
  2727. static void __btrfsic_submit_bio(int rw, struct bio *bio)
  2728. {
  2729. struct btrfsic_dev_state *dev_state;
  2730. if (!btrfsic_is_initialized)
  2731. return;
  2732. mutex_lock(&btrfsic_mutex);
  2733. /* since btrfsic_submit_bio() is also called before
  2734. * btrfsic_mount(), this might return NULL */
  2735. dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
  2736. if (NULL != dev_state &&
  2737. (rw & WRITE) && NULL != bio->bi_io_vec) {
  2738. unsigned int i;
  2739. u64 dev_bytenr;
  2740. u64 cur_bytenr;
  2741. int bio_is_patched;
  2742. char **mapped_datav;
  2743. dev_bytenr = 512 * bio->bi_iter.bi_sector;
  2744. bio_is_patched = 0;
  2745. if (dev_state->state->print_mask &
  2746. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2747. printk(KERN_INFO
  2748. "submit_bio(rw=0x%x, bi_vcnt=%u,"
  2749. " bi_sector=%llu (bytenr %llu), bi_bdev=%p)\n",
  2750. rw, bio->bi_vcnt,
  2751. (unsigned long long)bio->bi_iter.bi_sector,
  2752. dev_bytenr, bio->bi_bdev);
  2753. mapped_datav = kmalloc(sizeof(*mapped_datav) * bio->bi_vcnt,
  2754. GFP_NOFS);
  2755. if (!mapped_datav)
  2756. goto leave;
  2757. cur_bytenr = dev_bytenr;
  2758. for (i = 0; i < bio->bi_vcnt; i++) {
  2759. BUG_ON(bio->bi_io_vec[i].bv_len != PAGE_CACHE_SIZE);
  2760. mapped_datav[i] = kmap(bio->bi_io_vec[i].bv_page);
  2761. if (!mapped_datav[i]) {
  2762. while (i > 0) {
  2763. i--;
  2764. kunmap(bio->bi_io_vec[i].bv_page);
  2765. }
  2766. kfree(mapped_datav);
  2767. goto leave;
  2768. }
  2769. if (dev_state->state->print_mask &
  2770. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE)
  2771. printk(KERN_INFO
  2772. "#%u: bytenr=%llu, len=%u, offset=%u\n",
  2773. i, cur_bytenr, bio->bi_io_vec[i].bv_len,
  2774. bio->bi_io_vec[i].bv_offset);
  2775. cur_bytenr += bio->bi_io_vec[i].bv_len;
  2776. }
  2777. btrfsic_process_written_block(dev_state, dev_bytenr,
  2778. mapped_datav, bio->bi_vcnt,
  2779. bio, &bio_is_patched,
  2780. NULL, rw);
  2781. while (i > 0) {
  2782. i--;
  2783. kunmap(bio->bi_io_vec[i].bv_page);
  2784. }
  2785. kfree(mapped_datav);
  2786. } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
  2787. if (dev_state->state->print_mask &
  2788. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2789. printk(KERN_INFO
  2790. "submit_bio(rw=0x%x FLUSH, bdev=%p)\n",
  2791. rw, bio->bi_bdev);
  2792. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2793. if ((dev_state->state->print_mask &
  2794. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2795. BTRFSIC_PRINT_MASK_VERBOSE)))
  2796. printk(KERN_INFO
  2797. "btrfsic_submit_bio(%s) with FLUSH"
  2798. " but dummy block already in use"
  2799. " (ignored)!\n",
  2800. dev_state->name);
  2801. } else {
  2802. struct btrfsic_block *const block =
  2803. &dev_state->dummy_block_for_bio_bh_flush;
  2804. block->is_iodone = 0;
  2805. block->never_written = 0;
  2806. block->iodone_w_error = 0;
  2807. block->flush_gen = dev_state->last_flush_gen + 1;
  2808. block->submit_bio_bh_rw = rw;
  2809. block->orig_bio_bh_private = bio->bi_private;
  2810. block->orig_bio_bh_end_io.bio = bio->bi_end_io;
  2811. block->next_in_same_bio = NULL;
  2812. bio->bi_private = block;
  2813. bio->bi_end_io = btrfsic_bio_end_io;
  2814. }
  2815. }
  2816. leave:
  2817. mutex_unlock(&btrfsic_mutex);
  2818. }
  2819. void btrfsic_submit_bio(int rw, struct bio *bio)
  2820. {
  2821. __btrfsic_submit_bio(rw, bio);
  2822. submit_bio(rw, bio);
  2823. }
  2824. int btrfsic_submit_bio_wait(int rw, struct bio *bio)
  2825. {
  2826. __btrfsic_submit_bio(rw, bio);
  2827. return submit_bio_wait(rw, bio);
  2828. }
  2829. int btrfsic_mount(struct btrfs_root *root,
  2830. struct btrfs_fs_devices *fs_devices,
  2831. int including_extent_data, u32 print_mask)
  2832. {
  2833. int ret;
  2834. struct btrfsic_state *state;
  2835. struct list_head *dev_head = &fs_devices->devices;
  2836. struct btrfs_device *device;
  2837. if (root->nodesize != root->leafsize) {
  2838. printk(KERN_INFO
  2839. "btrfsic: cannot handle nodesize %d != leafsize %d!\n",
  2840. root->nodesize, root->leafsize);
  2841. return -1;
  2842. }
  2843. if (root->nodesize & ((u64)PAGE_CACHE_SIZE - 1)) {
  2844. printk(KERN_INFO
  2845. "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
  2846. root->nodesize, PAGE_CACHE_SIZE);
  2847. return -1;
  2848. }
  2849. if (root->leafsize & ((u64)PAGE_CACHE_SIZE - 1)) {
  2850. printk(KERN_INFO
  2851. "btrfsic: cannot handle leafsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
  2852. root->leafsize, PAGE_CACHE_SIZE);
  2853. return -1;
  2854. }
  2855. if (root->sectorsize & ((u64)PAGE_CACHE_SIZE - 1)) {
  2856. printk(KERN_INFO
  2857. "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
  2858. root->sectorsize, PAGE_CACHE_SIZE);
  2859. return -1;
  2860. }
  2861. state = kzalloc(sizeof(*state), GFP_NOFS);
  2862. if (NULL == state) {
  2863. printk(KERN_INFO "btrfs check-integrity: kmalloc() failed!\n");
  2864. return -1;
  2865. }
  2866. if (!btrfsic_is_initialized) {
  2867. mutex_init(&btrfsic_mutex);
  2868. btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
  2869. btrfsic_is_initialized = 1;
  2870. }
  2871. mutex_lock(&btrfsic_mutex);
  2872. state->root = root;
  2873. state->print_mask = print_mask;
  2874. state->include_extent_data = including_extent_data;
  2875. state->csum_size = 0;
  2876. state->metablock_size = root->nodesize;
  2877. state->datablock_size = root->sectorsize;
  2878. INIT_LIST_HEAD(&state->all_blocks_list);
  2879. btrfsic_block_hashtable_init(&state->block_hashtable);
  2880. btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
  2881. state->max_superblock_generation = 0;
  2882. state->latest_superblock = NULL;
  2883. list_for_each_entry(device, dev_head, dev_list) {
  2884. struct btrfsic_dev_state *ds;
  2885. char *p;
  2886. if (!device->bdev || !device->name)
  2887. continue;
  2888. ds = btrfsic_dev_state_alloc();
  2889. if (NULL == ds) {
  2890. printk(KERN_INFO
  2891. "btrfs check-integrity: kmalloc() failed!\n");
  2892. mutex_unlock(&btrfsic_mutex);
  2893. return -1;
  2894. }
  2895. ds->bdev = device->bdev;
  2896. ds->state = state;
  2897. bdevname(ds->bdev, ds->name);
  2898. ds->name[BDEVNAME_SIZE - 1] = '\0';
  2899. for (p = ds->name; *p != '\0'; p++);
  2900. while (p > ds->name && *p != '/')
  2901. p--;
  2902. if (*p == '/')
  2903. p++;
  2904. strlcpy(ds->name, p, sizeof(ds->name));
  2905. btrfsic_dev_state_hashtable_add(ds,
  2906. &btrfsic_dev_state_hashtable);
  2907. }
  2908. ret = btrfsic_process_superblock(state, fs_devices);
  2909. if (0 != ret) {
  2910. mutex_unlock(&btrfsic_mutex);
  2911. btrfsic_unmount(root, fs_devices);
  2912. return ret;
  2913. }
  2914. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
  2915. btrfsic_dump_database(state);
  2916. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
  2917. btrfsic_dump_tree(state);
  2918. mutex_unlock(&btrfsic_mutex);
  2919. return 0;
  2920. }
  2921. void btrfsic_unmount(struct btrfs_root *root,
  2922. struct btrfs_fs_devices *fs_devices)
  2923. {
  2924. struct list_head *elem_all;
  2925. struct list_head *tmp_all;
  2926. struct btrfsic_state *state;
  2927. struct list_head *dev_head = &fs_devices->devices;
  2928. struct btrfs_device *device;
  2929. if (!btrfsic_is_initialized)
  2930. return;
  2931. mutex_lock(&btrfsic_mutex);
  2932. state = NULL;
  2933. list_for_each_entry(device, dev_head, dev_list) {
  2934. struct btrfsic_dev_state *ds;
  2935. if (!device->bdev || !device->name)
  2936. continue;
  2937. ds = btrfsic_dev_state_hashtable_lookup(
  2938. device->bdev,
  2939. &btrfsic_dev_state_hashtable);
  2940. if (NULL != ds) {
  2941. state = ds->state;
  2942. btrfsic_dev_state_hashtable_remove(ds);
  2943. btrfsic_dev_state_free(ds);
  2944. }
  2945. }
  2946. if (NULL == state) {
  2947. printk(KERN_INFO
  2948. "btrfsic: error, cannot find state information"
  2949. " on umount!\n");
  2950. mutex_unlock(&btrfsic_mutex);
  2951. return;
  2952. }
  2953. /*
  2954. * Don't care about keeping the lists' state up to date,
  2955. * just free all memory that was allocated dynamically.
  2956. * Free the blocks and the block_links.
  2957. */
  2958. list_for_each_safe(elem_all, tmp_all, &state->all_blocks_list) {
  2959. struct btrfsic_block *const b_all =
  2960. list_entry(elem_all, struct btrfsic_block,
  2961. all_blocks_node);
  2962. struct list_head *elem_ref_to;
  2963. struct list_head *tmp_ref_to;
  2964. list_for_each_safe(elem_ref_to, tmp_ref_to,
  2965. &b_all->ref_to_list) {
  2966. struct btrfsic_block_link *const l =
  2967. list_entry(elem_ref_to,
  2968. struct btrfsic_block_link,
  2969. node_ref_to);
  2970. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2971. btrfsic_print_rem_link(state, l);
  2972. l->ref_cnt--;
  2973. if (0 == l->ref_cnt)
  2974. btrfsic_block_link_free(l);
  2975. }
  2976. if (b_all->is_iodone || b_all->never_written)
  2977. btrfsic_block_free(b_all);
  2978. else
  2979. printk(KERN_INFO "btrfs: attempt to free %c-block"
  2980. " @%llu (%s/%llu/%d) on umount which is"
  2981. " not yet iodone!\n",
  2982. btrfsic_get_block_type(state, b_all),
  2983. b_all->logical_bytenr, b_all->dev_state->name,
  2984. b_all->dev_bytenr, b_all->mirror_num);
  2985. }
  2986. mutex_unlock(&btrfsic_mutex);
  2987. kfree(state);
  2988. }