check-integrity.c 94 KB

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