check-integrity.c 94 KB

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