check-integrity.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058
  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. if (!block_ctx->datav[i]) {
  1509. pr_info("btrfsic: kmap() failed (dev %s)!\n",
  1510. block_ctx->dev->name);
  1511. return -1;
  1512. }
  1513. }
  1514. return block_ctx->len;
  1515. }
  1516. static void btrfsic_dump_database(struct btrfsic_state *state)
  1517. {
  1518. const struct btrfsic_block *b_all;
  1519. BUG_ON(NULL == state);
  1520. pr_info("all_blocks_list:\n");
  1521. list_for_each_entry(b_all, &state->all_blocks_list, all_blocks_node) {
  1522. const struct btrfsic_block_link *l;
  1523. pr_info("%c-block @%llu (%s/%llu/%d)\n",
  1524. btrfsic_get_block_type(state, b_all),
  1525. b_all->logical_bytenr, b_all->dev_state->name,
  1526. b_all->dev_bytenr, b_all->mirror_num);
  1527. list_for_each_entry(l, &b_all->ref_to_list, node_ref_to) {
  1528. pr_info(" %c @%llu (%s/%llu/%d) refers %u* to %c @%llu (%s/%llu/%d)\n",
  1529. btrfsic_get_block_type(state, b_all),
  1530. b_all->logical_bytenr, b_all->dev_state->name,
  1531. b_all->dev_bytenr, b_all->mirror_num,
  1532. l->ref_cnt,
  1533. btrfsic_get_block_type(state, l->block_ref_to),
  1534. l->block_ref_to->logical_bytenr,
  1535. l->block_ref_to->dev_state->name,
  1536. l->block_ref_to->dev_bytenr,
  1537. l->block_ref_to->mirror_num);
  1538. }
  1539. list_for_each_entry(l, &b_all->ref_from_list, node_ref_from) {
  1540. pr_info(" %c @%llu (%s/%llu/%d) is ref %u* from %c @%llu (%s/%llu/%d)\n",
  1541. btrfsic_get_block_type(state, b_all),
  1542. b_all->logical_bytenr, b_all->dev_state->name,
  1543. b_all->dev_bytenr, b_all->mirror_num,
  1544. l->ref_cnt,
  1545. btrfsic_get_block_type(state, l->block_ref_from),
  1546. l->block_ref_from->logical_bytenr,
  1547. l->block_ref_from->dev_state->name,
  1548. l->block_ref_from->dev_bytenr,
  1549. l->block_ref_from->mirror_num);
  1550. }
  1551. pr_info("\n");
  1552. }
  1553. }
  1554. /*
  1555. * Test whether the disk block contains a tree block (leaf or node)
  1556. * (note that this test fails for the super block)
  1557. */
  1558. static int btrfsic_test_for_metadata(struct btrfsic_state *state,
  1559. char **datav, unsigned int num_pages)
  1560. {
  1561. struct btrfs_fs_info *fs_info = state->fs_info;
  1562. struct btrfs_header *h;
  1563. u8 csum[BTRFS_CSUM_SIZE];
  1564. u32 crc = ~(u32)0;
  1565. unsigned int i;
  1566. if (num_pages * PAGE_SIZE < state->metablock_size)
  1567. return 1; /* not metadata */
  1568. num_pages = state->metablock_size >> PAGE_SHIFT;
  1569. h = (struct btrfs_header *)datav[0];
  1570. if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  1571. return 1;
  1572. for (i = 0; i < num_pages; i++) {
  1573. u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
  1574. size_t sublen = i ? PAGE_SIZE :
  1575. (PAGE_SIZE - BTRFS_CSUM_SIZE);
  1576. crc = btrfs_crc32c(crc, data, sublen);
  1577. }
  1578. btrfs_csum_final(crc, csum);
  1579. if (memcmp(csum, h->csum, state->csum_size))
  1580. return 1;
  1581. return 0; /* is metadata */
  1582. }
  1583. static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
  1584. u64 dev_bytenr, char **mapped_datav,
  1585. unsigned int num_pages,
  1586. struct bio *bio, int *bio_is_patched,
  1587. struct buffer_head *bh,
  1588. int submit_bio_bh_rw)
  1589. {
  1590. int is_metadata;
  1591. struct btrfsic_block *block;
  1592. struct btrfsic_block_data_ctx block_ctx;
  1593. int ret;
  1594. struct btrfsic_state *state = dev_state->state;
  1595. struct block_device *bdev = dev_state->bdev;
  1596. unsigned int processed_len;
  1597. if (NULL != bio_is_patched)
  1598. *bio_is_patched = 0;
  1599. again:
  1600. if (num_pages == 0)
  1601. return;
  1602. processed_len = 0;
  1603. is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
  1604. num_pages));
  1605. block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
  1606. &state->block_hashtable);
  1607. if (NULL != block) {
  1608. u64 bytenr = 0;
  1609. struct btrfsic_block_link *l, *tmp;
  1610. if (block->is_superblock) {
  1611. bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
  1612. mapped_datav[0]);
  1613. if (num_pages * PAGE_SIZE <
  1614. BTRFS_SUPER_INFO_SIZE) {
  1615. pr_info("btrfsic: cannot work with too short bios!\n");
  1616. return;
  1617. }
  1618. is_metadata = 1;
  1619. BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_SIZE - 1));
  1620. processed_len = BTRFS_SUPER_INFO_SIZE;
  1621. if (state->print_mask &
  1622. BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
  1623. pr_info("[before new superblock is written]:\n");
  1624. btrfsic_dump_tree_sub(state, block, 0);
  1625. }
  1626. }
  1627. if (is_metadata) {
  1628. if (!block->is_superblock) {
  1629. if (num_pages * PAGE_SIZE <
  1630. state->metablock_size) {
  1631. pr_info("btrfsic: cannot work with too short bios!\n");
  1632. return;
  1633. }
  1634. processed_len = state->metablock_size;
  1635. bytenr = btrfs_stack_header_bytenr(
  1636. (struct btrfs_header *)
  1637. mapped_datav[0]);
  1638. btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
  1639. dev_state,
  1640. dev_bytenr);
  1641. }
  1642. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
  1643. if (block->logical_bytenr != bytenr &&
  1644. !(!block->is_metadata &&
  1645. block->logical_bytenr == 0))
  1646. pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n",
  1647. bytenr, dev_state->name,
  1648. dev_bytenr,
  1649. block->mirror_num,
  1650. btrfsic_get_block_type(state,
  1651. block),
  1652. block->logical_bytenr);
  1653. else
  1654. pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c.\n",
  1655. bytenr, dev_state->name,
  1656. dev_bytenr, block->mirror_num,
  1657. btrfsic_get_block_type(state,
  1658. block));
  1659. }
  1660. block->logical_bytenr = bytenr;
  1661. } else {
  1662. if (num_pages * PAGE_SIZE <
  1663. state->datablock_size) {
  1664. pr_info("btrfsic: cannot work with too short bios!\n");
  1665. return;
  1666. }
  1667. processed_len = state->datablock_size;
  1668. bytenr = block->logical_bytenr;
  1669. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1670. pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c.\n",
  1671. bytenr, dev_state->name, dev_bytenr,
  1672. block->mirror_num,
  1673. btrfsic_get_block_type(state, block));
  1674. }
  1675. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1676. pr_info("ref_to_list: %cE, ref_from_list: %cE\n",
  1677. list_empty(&block->ref_to_list) ? ' ' : '!',
  1678. list_empty(&block->ref_from_list) ? ' ' : '!');
  1679. if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
  1680. 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",
  1681. btrfsic_get_block_type(state, block), bytenr,
  1682. dev_state->name, dev_bytenr, block->mirror_num,
  1683. block->generation,
  1684. btrfs_disk_key_objectid(&block->disk_key),
  1685. block->disk_key.type,
  1686. btrfs_disk_key_offset(&block->disk_key),
  1687. btrfs_stack_header_generation(
  1688. (struct btrfs_header *) mapped_datav[0]),
  1689. state->max_superblock_generation);
  1690. btrfsic_dump_tree(state);
  1691. }
  1692. if (!block->is_iodone && !block->never_written) {
  1693. pr_info("btrfs: attempt to overwrite %c-block @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu, which is not yet iodone!\n",
  1694. btrfsic_get_block_type(state, block), bytenr,
  1695. dev_state->name, dev_bytenr, block->mirror_num,
  1696. block->generation,
  1697. btrfs_stack_header_generation(
  1698. (struct btrfs_header *)
  1699. mapped_datav[0]));
  1700. /* it would not be safe to go on */
  1701. btrfsic_dump_tree(state);
  1702. goto continue_loop;
  1703. }
  1704. /*
  1705. * Clear all references of this block. Do not free
  1706. * the block itself even if is not referenced anymore
  1707. * because it still carries valuable information
  1708. * like whether it was ever written and IO completed.
  1709. */
  1710. list_for_each_entry_safe(l, tmp, &block->ref_to_list,
  1711. node_ref_to) {
  1712. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1713. btrfsic_print_rem_link(state, l);
  1714. l->ref_cnt--;
  1715. if (0 == l->ref_cnt) {
  1716. list_del(&l->node_ref_to);
  1717. list_del(&l->node_ref_from);
  1718. btrfsic_block_link_hashtable_remove(l);
  1719. btrfsic_block_link_free(l);
  1720. }
  1721. }
  1722. block_ctx.dev = dev_state;
  1723. block_ctx.dev_bytenr = dev_bytenr;
  1724. block_ctx.start = bytenr;
  1725. block_ctx.len = processed_len;
  1726. block_ctx.pagev = NULL;
  1727. block_ctx.mem_to_free = NULL;
  1728. block_ctx.datav = mapped_datav;
  1729. if (is_metadata || state->include_extent_data) {
  1730. block->never_written = 0;
  1731. block->iodone_w_error = 0;
  1732. if (NULL != bio) {
  1733. block->is_iodone = 0;
  1734. BUG_ON(NULL == bio_is_patched);
  1735. if (!*bio_is_patched) {
  1736. block->orig_bio_bh_private =
  1737. bio->bi_private;
  1738. block->orig_bio_bh_end_io.bio =
  1739. bio->bi_end_io;
  1740. block->next_in_same_bio = NULL;
  1741. bio->bi_private = block;
  1742. bio->bi_end_io = btrfsic_bio_end_io;
  1743. *bio_is_patched = 1;
  1744. } else {
  1745. struct btrfsic_block *chained_block =
  1746. (struct btrfsic_block *)
  1747. bio->bi_private;
  1748. BUG_ON(NULL == chained_block);
  1749. block->orig_bio_bh_private =
  1750. chained_block->orig_bio_bh_private;
  1751. block->orig_bio_bh_end_io.bio =
  1752. chained_block->orig_bio_bh_end_io.
  1753. bio;
  1754. block->next_in_same_bio = chained_block;
  1755. bio->bi_private = block;
  1756. }
  1757. } else if (NULL != bh) {
  1758. block->is_iodone = 0;
  1759. block->orig_bio_bh_private = bh->b_private;
  1760. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  1761. block->next_in_same_bio = NULL;
  1762. bh->b_private = block;
  1763. bh->b_end_io = btrfsic_bh_end_io;
  1764. } else {
  1765. block->is_iodone = 1;
  1766. block->orig_bio_bh_private = NULL;
  1767. block->orig_bio_bh_end_io.bio = NULL;
  1768. block->next_in_same_bio = NULL;
  1769. }
  1770. }
  1771. block->flush_gen = dev_state->last_flush_gen + 1;
  1772. block->submit_bio_bh_rw = submit_bio_bh_rw;
  1773. if (is_metadata) {
  1774. block->logical_bytenr = bytenr;
  1775. block->is_metadata = 1;
  1776. if (block->is_superblock) {
  1777. BUG_ON(PAGE_SIZE !=
  1778. BTRFS_SUPER_INFO_SIZE);
  1779. ret = btrfsic_process_written_superblock(
  1780. state,
  1781. block,
  1782. (struct btrfs_super_block *)
  1783. mapped_datav[0]);
  1784. if (state->print_mask &
  1785. BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
  1786. pr_info("[after new superblock is written]:\n");
  1787. btrfsic_dump_tree_sub(state, block, 0);
  1788. }
  1789. } else {
  1790. block->mirror_num = 0; /* unknown */
  1791. ret = btrfsic_process_metablock(
  1792. state,
  1793. block,
  1794. &block_ctx,
  1795. 0, 0);
  1796. }
  1797. if (ret)
  1798. pr_info("btrfsic: btrfsic_process_metablock(root @%llu) failed!\n",
  1799. dev_bytenr);
  1800. } else {
  1801. block->is_metadata = 0;
  1802. block->mirror_num = 0; /* unknown */
  1803. block->generation = BTRFSIC_GENERATION_UNKNOWN;
  1804. if (!state->include_extent_data
  1805. && list_empty(&block->ref_from_list)) {
  1806. /*
  1807. * disk block is overwritten with extent
  1808. * data (not meta data) and we are configured
  1809. * to not include extent data: take the
  1810. * chance and free the block's memory
  1811. */
  1812. btrfsic_block_hashtable_remove(block);
  1813. list_del(&block->all_blocks_node);
  1814. btrfsic_block_free(block);
  1815. }
  1816. }
  1817. btrfsic_release_block_ctx(&block_ctx);
  1818. } else {
  1819. /* block has not been found in hash table */
  1820. u64 bytenr;
  1821. if (!is_metadata) {
  1822. processed_len = state->datablock_size;
  1823. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1824. pr_info("Written block (%s/%llu/?) !found in hash table, D.\n",
  1825. dev_state->name, dev_bytenr);
  1826. if (!state->include_extent_data) {
  1827. /* ignore that written D block */
  1828. goto continue_loop;
  1829. }
  1830. /* this is getting ugly for the
  1831. * include_extent_data case... */
  1832. bytenr = 0; /* unknown */
  1833. } else {
  1834. processed_len = state->metablock_size;
  1835. bytenr = btrfs_stack_header_bytenr(
  1836. (struct btrfs_header *)
  1837. mapped_datav[0]);
  1838. btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
  1839. dev_bytenr);
  1840. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1841. pr_info("Written block @%llu (%s/%llu/?) !found in hash table, M.\n",
  1842. bytenr, dev_state->name, dev_bytenr);
  1843. }
  1844. block_ctx.dev = dev_state;
  1845. block_ctx.dev_bytenr = dev_bytenr;
  1846. block_ctx.start = bytenr;
  1847. block_ctx.len = processed_len;
  1848. block_ctx.pagev = NULL;
  1849. block_ctx.mem_to_free = NULL;
  1850. block_ctx.datav = mapped_datav;
  1851. block = btrfsic_block_alloc();
  1852. if (NULL == block) {
  1853. pr_info("btrfsic: error, kmalloc failed!\n");
  1854. btrfsic_release_block_ctx(&block_ctx);
  1855. goto continue_loop;
  1856. }
  1857. block->dev_state = dev_state;
  1858. block->dev_bytenr = dev_bytenr;
  1859. block->logical_bytenr = bytenr;
  1860. block->is_metadata = is_metadata;
  1861. block->never_written = 0;
  1862. block->iodone_w_error = 0;
  1863. block->mirror_num = 0; /* unknown */
  1864. block->flush_gen = dev_state->last_flush_gen + 1;
  1865. block->submit_bio_bh_rw = submit_bio_bh_rw;
  1866. if (NULL != bio) {
  1867. block->is_iodone = 0;
  1868. BUG_ON(NULL == bio_is_patched);
  1869. if (!*bio_is_patched) {
  1870. block->orig_bio_bh_private = bio->bi_private;
  1871. block->orig_bio_bh_end_io.bio = bio->bi_end_io;
  1872. block->next_in_same_bio = NULL;
  1873. bio->bi_private = block;
  1874. bio->bi_end_io = btrfsic_bio_end_io;
  1875. *bio_is_patched = 1;
  1876. } else {
  1877. struct btrfsic_block *chained_block =
  1878. (struct btrfsic_block *)
  1879. bio->bi_private;
  1880. BUG_ON(NULL == chained_block);
  1881. block->orig_bio_bh_private =
  1882. chained_block->orig_bio_bh_private;
  1883. block->orig_bio_bh_end_io.bio =
  1884. chained_block->orig_bio_bh_end_io.bio;
  1885. block->next_in_same_bio = chained_block;
  1886. bio->bi_private = block;
  1887. }
  1888. } else if (NULL != bh) {
  1889. block->is_iodone = 0;
  1890. block->orig_bio_bh_private = bh->b_private;
  1891. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  1892. block->next_in_same_bio = NULL;
  1893. bh->b_private = block;
  1894. bh->b_end_io = btrfsic_bh_end_io;
  1895. } else {
  1896. block->is_iodone = 1;
  1897. block->orig_bio_bh_private = NULL;
  1898. block->orig_bio_bh_end_io.bio = NULL;
  1899. block->next_in_same_bio = NULL;
  1900. }
  1901. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1902. pr_info("New written %c-block @%llu (%s/%llu/%d)\n",
  1903. is_metadata ? 'M' : 'D',
  1904. block->logical_bytenr, block->dev_state->name,
  1905. block->dev_bytenr, block->mirror_num);
  1906. list_add(&block->all_blocks_node, &state->all_blocks_list);
  1907. btrfsic_block_hashtable_add(block, &state->block_hashtable);
  1908. if (is_metadata) {
  1909. ret = btrfsic_process_metablock(state, block,
  1910. &block_ctx, 0, 0);
  1911. if (ret)
  1912. pr_info("btrfsic: process_metablock(root @%llu) failed!\n",
  1913. dev_bytenr);
  1914. }
  1915. btrfsic_release_block_ctx(&block_ctx);
  1916. }
  1917. continue_loop:
  1918. BUG_ON(!processed_len);
  1919. dev_bytenr += processed_len;
  1920. mapped_datav += processed_len >> PAGE_SHIFT;
  1921. num_pages -= processed_len >> PAGE_SHIFT;
  1922. goto again;
  1923. }
  1924. static void btrfsic_bio_end_io(struct bio *bp)
  1925. {
  1926. struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
  1927. int iodone_w_error;
  1928. /* mutex is not held! This is not save if IO is not yet completed
  1929. * on umount */
  1930. iodone_w_error = 0;
  1931. if (bp->bi_error)
  1932. iodone_w_error = 1;
  1933. BUG_ON(NULL == block);
  1934. bp->bi_private = block->orig_bio_bh_private;
  1935. bp->bi_end_io = block->orig_bio_bh_end_io.bio;
  1936. do {
  1937. struct btrfsic_block *next_block;
  1938. struct btrfsic_dev_state *const dev_state = block->dev_state;
  1939. if ((dev_state->state->print_mask &
  1940. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  1941. pr_info("bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
  1942. bp->bi_error,
  1943. btrfsic_get_block_type(dev_state->state, block),
  1944. block->logical_bytenr, dev_state->name,
  1945. block->dev_bytenr, block->mirror_num);
  1946. next_block = block->next_in_same_bio;
  1947. block->iodone_w_error = iodone_w_error;
  1948. if (block->submit_bio_bh_rw & REQ_PREFLUSH) {
  1949. dev_state->last_flush_gen++;
  1950. if ((dev_state->state->print_mask &
  1951. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  1952. pr_info("bio_end_io() new %s flush_gen=%llu\n",
  1953. dev_state->name,
  1954. dev_state->last_flush_gen);
  1955. }
  1956. if (block->submit_bio_bh_rw & REQ_FUA)
  1957. block->flush_gen = 0; /* FUA completed means block is
  1958. * on disk */
  1959. block->is_iodone = 1; /* for FLUSH, this releases the block */
  1960. block = next_block;
  1961. } while (NULL != block);
  1962. bp->bi_end_io(bp);
  1963. }
  1964. static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
  1965. {
  1966. struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
  1967. int iodone_w_error = !uptodate;
  1968. struct btrfsic_dev_state *dev_state;
  1969. BUG_ON(NULL == block);
  1970. dev_state = block->dev_state;
  1971. if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  1972. pr_info("bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
  1973. iodone_w_error,
  1974. btrfsic_get_block_type(dev_state->state, block),
  1975. block->logical_bytenr, block->dev_state->name,
  1976. block->dev_bytenr, block->mirror_num);
  1977. block->iodone_w_error = iodone_w_error;
  1978. if (block->submit_bio_bh_rw & REQ_PREFLUSH) {
  1979. dev_state->last_flush_gen++;
  1980. if ((dev_state->state->print_mask &
  1981. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  1982. pr_info("bh_end_io() new %s flush_gen=%llu\n",
  1983. dev_state->name, dev_state->last_flush_gen);
  1984. }
  1985. if (block->submit_bio_bh_rw & REQ_FUA)
  1986. block->flush_gen = 0; /* FUA completed means block is on disk */
  1987. bh->b_private = block->orig_bio_bh_private;
  1988. bh->b_end_io = block->orig_bio_bh_end_io.bh;
  1989. block->is_iodone = 1; /* for FLUSH, this releases the block */
  1990. bh->b_end_io(bh, uptodate);
  1991. }
  1992. static int btrfsic_process_written_superblock(
  1993. struct btrfsic_state *state,
  1994. struct btrfsic_block *const superblock,
  1995. struct btrfs_super_block *const super_hdr)
  1996. {
  1997. struct btrfs_fs_info *fs_info = state->fs_info;
  1998. int pass;
  1999. superblock->generation = btrfs_super_generation(super_hdr);
  2000. if (!(superblock->generation > state->max_superblock_generation ||
  2001. 0 == state->max_superblock_generation)) {
  2002. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  2003. pr_info("btrfsic: superblock @%llu (%s/%llu/%d) with old gen %llu <= %llu\n",
  2004. superblock->logical_bytenr,
  2005. superblock->dev_state->name,
  2006. superblock->dev_bytenr, superblock->mirror_num,
  2007. btrfs_super_generation(super_hdr),
  2008. state->max_superblock_generation);
  2009. } else {
  2010. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  2011. pr_info("btrfsic: got new superblock @%llu (%s/%llu/%d) with new gen %llu > %llu\n",
  2012. superblock->logical_bytenr,
  2013. superblock->dev_state->name,
  2014. superblock->dev_bytenr, superblock->mirror_num,
  2015. btrfs_super_generation(super_hdr),
  2016. state->max_superblock_generation);
  2017. state->max_superblock_generation =
  2018. btrfs_super_generation(super_hdr);
  2019. state->latest_superblock = superblock;
  2020. }
  2021. for (pass = 0; pass < 3; pass++) {
  2022. int ret;
  2023. u64 next_bytenr;
  2024. struct btrfsic_block *next_block;
  2025. struct btrfsic_block_data_ctx tmp_next_block_ctx;
  2026. struct btrfsic_block_link *l;
  2027. int num_copies;
  2028. int mirror_num;
  2029. const char *additional_string = NULL;
  2030. struct btrfs_disk_key tmp_disk_key = {0};
  2031. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2032. BTRFS_ROOT_ITEM_KEY);
  2033. btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
  2034. switch (pass) {
  2035. case 0:
  2036. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2037. BTRFS_ROOT_TREE_OBJECTID);
  2038. additional_string = "root ";
  2039. next_bytenr = btrfs_super_root(super_hdr);
  2040. if (state->print_mask &
  2041. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2042. pr_info("root@%llu\n", next_bytenr);
  2043. break;
  2044. case 1:
  2045. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2046. BTRFS_CHUNK_TREE_OBJECTID);
  2047. additional_string = "chunk ";
  2048. next_bytenr = btrfs_super_chunk_root(super_hdr);
  2049. if (state->print_mask &
  2050. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2051. pr_info("chunk@%llu\n", next_bytenr);
  2052. break;
  2053. case 2:
  2054. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2055. BTRFS_TREE_LOG_OBJECTID);
  2056. additional_string = "log ";
  2057. next_bytenr = btrfs_super_log_root(super_hdr);
  2058. if (0 == next_bytenr)
  2059. continue;
  2060. if (state->print_mask &
  2061. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2062. pr_info("log@%llu\n", next_bytenr);
  2063. break;
  2064. }
  2065. num_copies = btrfs_num_copies(fs_info, next_bytenr,
  2066. BTRFS_SUPER_INFO_SIZE);
  2067. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  2068. pr_info("num_copies(log_bytenr=%llu) = %d\n",
  2069. next_bytenr, num_copies);
  2070. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2071. int was_created;
  2072. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2073. pr_info("btrfsic_process_written_superblock(mirror_num=%d)\n", mirror_num);
  2074. ret = btrfsic_map_block(state, next_bytenr,
  2075. BTRFS_SUPER_INFO_SIZE,
  2076. &tmp_next_block_ctx,
  2077. mirror_num);
  2078. if (ret) {
  2079. pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
  2080. next_bytenr, mirror_num);
  2081. return -1;
  2082. }
  2083. next_block = btrfsic_block_lookup_or_add(
  2084. state,
  2085. &tmp_next_block_ctx,
  2086. additional_string,
  2087. 1, 0, 1,
  2088. mirror_num,
  2089. &was_created);
  2090. if (NULL == next_block) {
  2091. pr_info("btrfsic: error, kmalloc failed!\n");
  2092. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  2093. return -1;
  2094. }
  2095. next_block->disk_key = tmp_disk_key;
  2096. if (was_created)
  2097. next_block->generation =
  2098. BTRFSIC_GENERATION_UNKNOWN;
  2099. l = btrfsic_block_link_lookup_or_add(
  2100. state,
  2101. &tmp_next_block_ctx,
  2102. next_block,
  2103. superblock,
  2104. BTRFSIC_GENERATION_UNKNOWN);
  2105. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  2106. if (NULL == l)
  2107. return -1;
  2108. }
  2109. }
  2110. if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
  2111. btrfsic_dump_tree(state);
  2112. return 0;
  2113. }
  2114. static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
  2115. struct btrfsic_block *const block,
  2116. int recursion_level)
  2117. {
  2118. const struct btrfsic_block_link *l;
  2119. int ret = 0;
  2120. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2121. /*
  2122. * Note that this situation can happen and does not
  2123. * indicate an error in regular cases. It happens
  2124. * when disk blocks are freed and later reused.
  2125. * The check-integrity module is not aware of any
  2126. * block free operations, it just recognizes block
  2127. * write operations. Therefore it keeps the linkage
  2128. * information for a block until a block is
  2129. * rewritten. This can temporarily cause incorrect
  2130. * and even circular linkage informations. This
  2131. * causes no harm unless such blocks are referenced
  2132. * by the most recent super block.
  2133. */
  2134. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2135. pr_info("btrfsic: abort cyclic linkage (case 1).\n");
  2136. return ret;
  2137. }
  2138. /*
  2139. * This algorithm is recursive because the amount of used stack
  2140. * space is very small and the max recursion depth is limited.
  2141. */
  2142. list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
  2143. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2144. pr_info("rl=%d, %c @%llu (%s/%llu/%d) %u* refers to %c @%llu (%s/%llu/%d)\n",
  2145. recursion_level,
  2146. btrfsic_get_block_type(state, block),
  2147. block->logical_bytenr, block->dev_state->name,
  2148. block->dev_bytenr, block->mirror_num,
  2149. l->ref_cnt,
  2150. btrfsic_get_block_type(state, l->block_ref_to),
  2151. l->block_ref_to->logical_bytenr,
  2152. l->block_ref_to->dev_state->name,
  2153. l->block_ref_to->dev_bytenr,
  2154. l->block_ref_to->mirror_num);
  2155. if (l->block_ref_to->never_written) {
  2156. pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is never written!\n",
  2157. btrfsic_get_block_type(state, l->block_ref_to),
  2158. l->block_ref_to->logical_bytenr,
  2159. l->block_ref_to->dev_state->name,
  2160. l->block_ref_to->dev_bytenr,
  2161. l->block_ref_to->mirror_num);
  2162. ret = -1;
  2163. } else if (!l->block_ref_to->is_iodone) {
  2164. pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is not yet iodone!\n",
  2165. btrfsic_get_block_type(state, l->block_ref_to),
  2166. l->block_ref_to->logical_bytenr,
  2167. l->block_ref_to->dev_state->name,
  2168. l->block_ref_to->dev_bytenr,
  2169. l->block_ref_to->mirror_num);
  2170. ret = -1;
  2171. } else if (l->block_ref_to->iodone_w_error) {
  2172. pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which has write error!\n",
  2173. btrfsic_get_block_type(state, l->block_ref_to),
  2174. l->block_ref_to->logical_bytenr,
  2175. l->block_ref_to->dev_state->name,
  2176. l->block_ref_to->dev_bytenr,
  2177. l->block_ref_to->mirror_num);
  2178. ret = -1;
  2179. } else if (l->parent_generation !=
  2180. l->block_ref_to->generation &&
  2181. BTRFSIC_GENERATION_UNKNOWN !=
  2182. l->parent_generation &&
  2183. BTRFSIC_GENERATION_UNKNOWN !=
  2184. l->block_ref_to->generation) {
  2185. pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) with generation %llu != parent generation %llu!\n",
  2186. btrfsic_get_block_type(state, l->block_ref_to),
  2187. l->block_ref_to->logical_bytenr,
  2188. l->block_ref_to->dev_state->name,
  2189. l->block_ref_to->dev_bytenr,
  2190. l->block_ref_to->mirror_num,
  2191. l->block_ref_to->generation,
  2192. l->parent_generation);
  2193. ret = -1;
  2194. } else if (l->block_ref_to->flush_gen >
  2195. l->block_ref_to->dev_state->last_flush_gen) {
  2196. 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",
  2197. btrfsic_get_block_type(state, l->block_ref_to),
  2198. l->block_ref_to->logical_bytenr,
  2199. l->block_ref_to->dev_state->name,
  2200. l->block_ref_to->dev_bytenr,
  2201. l->block_ref_to->mirror_num, block->flush_gen,
  2202. l->block_ref_to->dev_state->last_flush_gen);
  2203. ret = -1;
  2204. } else if (-1 == btrfsic_check_all_ref_blocks(state,
  2205. l->block_ref_to,
  2206. recursion_level +
  2207. 1)) {
  2208. ret = -1;
  2209. }
  2210. }
  2211. return ret;
  2212. }
  2213. static int btrfsic_is_block_ref_by_superblock(
  2214. const struct btrfsic_state *state,
  2215. const struct btrfsic_block *block,
  2216. int recursion_level)
  2217. {
  2218. const struct btrfsic_block_link *l;
  2219. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2220. /* refer to comment at "abort cyclic linkage (case 1)" */
  2221. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2222. pr_info("btrfsic: abort cyclic linkage (case 2).\n");
  2223. return 0;
  2224. }
  2225. /*
  2226. * This algorithm is recursive because the amount of used stack space
  2227. * is very small and the max recursion depth is limited.
  2228. */
  2229. list_for_each_entry(l, &block->ref_from_list, node_ref_from) {
  2230. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2231. pr_info("rl=%d, %c @%llu (%s/%llu/%d) is ref %u* from %c @%llu (%s/%llu/%d)\n",
  2232. recursion_level,
  2233. btrfsic_get_block_type(state, block),
  2234. block->logical_bytenr, block->dev_state->name,
  2235. block->dev_bytenr, block->mirror_num,
  2236. l->ref_cnt,
  2237. btrfsic_get_block_type(state, l->block_ref_from),
  2238. l->block_ref_from->logical_bytenr,
  2239. l->block_ref_from->dev_state->name,
  2240. l->block_ref_from->dev_bytenr,
  2241. l->block_ref_from->mirror_num);
  2242. if (l->block_ref_from->is_superblock &&
  2243. state->latest_superblock->dev_bytenr ==
  2244. l->block_ref_from->dev_bytenr &&
  2245. state->latest_superblock->dev_state->bdev ==
  2246. l->block_ref_from->dev_state->bdev)
  2247. return 1;
  2248. else if (btrfsic_is_block_ref_by_superblock(state,
  2249. l->block_ref_from,
  2250. recursion_level +
  2251. 1))
  2252. return 1;
  2253. }
  2254. return 0;
  2255. }
  2256. static void btrfsic_print_add_link(const struct btrfsic_state *state,
  2257. const struct btrfsic_block_link *l)
  2258. {
  2259. pr_info("Add %u* link from %c @%llu (%s/%llu/%d) to %c @%llu (%s/%llu/%d).\n",
  2260. l->ref_cnt,
  2261. btrfsic_get_block_type(state, l->block_ref_from),
  2262. l->block_ref_from->logical_bytenr,
  2263. l->block_ref_from->dev_state->name,
  2264. l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
  2265. btrfsic_get_block_type(state, l->block_ref_to),
  2266. l->block_ref_to->logical_bytenr,
  2267. l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
  2268. l->block_ref_to->mirror_num);
  2269. }
  2270. static void btrfsic_print_rem_link(const struct btrfsic_state *state,
  2271. const struct btrfsic_block_link *l)
  2272. {
  2273. pr_info("Rem %u* link from %c @%llu (%s/%llu/%d) to %c @%llu (%s/%llu/%d).\n",
  2274. l->ref_cnt,
  2275. btrfsic_get_block_type(state, l->block_ref_from),
  2276. l->block_ref_from->logical_bytenr,
  2277. l->block_ref_from->dev_state->name,
  2278. l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
  2279. btrfsic_get_block_type(state, l->block_ref_to),
  2280. l->block_ref_to->logical_bytenr,
  2281. l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
  2282. l->block_ref_to->mirror_num);
  2283. }
  2284. static char btrfsic_get_block_type(const struct btrfsic_state *state,
  2285. const struct btrfsic_block *block)
  2286. {
  2287. if (block->is_superblock &&
  2288. state->latest_superblock->dev_bytenr == block->dev_bytenr &&
  2289. state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
  2290. return 'S';
  2291. else if (block->is_superblock)
  2292. return 's';
  2293. else if (block->is_metadata)
  2294. return 'M';
  2295. else
  2296. return 'D';
  2297. }
  2298. static void btrfsic_dump_tree(const struct btrfsic_state *state)
  2299. {
  2300. btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
  2301. }
  2302. static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
  2303. const struct btrfsic_block *block,
  2304. int indent_level)
  2305. {
  2306. const struct btrfsic_block_link *l;
  2307. int indent_add;
  2308. static char buf[80];
  2309. int cursor_position;
  2310. /*
  2311. * Should better fill an on-stack buffer with a complete line and
  2312. * dump it at once when it is time to print a newline character.
  2313. */
  2314. /*
  2315. * This algorithm is recursive because the amount of used stack space
  2316. * is very small and the max recursion depth is limited.
  2317. */
  2318. indent_add = sprintf(buf, "%c-%llu(%s/%llu/%u)",
  2319. btrfsic_get_block_type(state, block),
  2320. block->logical_bytenr, block->dev_state->name,
  2321. block->dev_bytenr, block->mirror_num);
  2322. if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2323. printk("[...]\n");
  2324. return;
  2325. }
  2326. printk(buf);
  2327. indent_level += indent_add;
  2328. if (list_empty(&block->ref_to_list)) {
  2329. printk("\n");
  2330. return;
  2331. }
  2332. if (block->mirror_num > 1 &&
  2333. !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
  2334. printk(" [...]\n");
  2335. return;
  2336. }
  2337. cursor_position = indent_level;
  2338. list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
  2339. while (cursor_position < indent_level) {
  2340. printk(" ");
  2341. cursor_position++;
  2342. }
  2343. if (l->ref_cnt > 1)
  2344. indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
  2345. else
  2346. indent_add = sprintf(buf, " --> ");
  2347. if (indent_level + indent_add >
  2348. BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2349. printk("[...]\n");
  2350. cursor_position = 0;
  2351. continue;
  2352. }
  2353. printk(buf);
  2354. btrfsic_dump_tree_sub(state, l->block_ref_to,
  2355. indent_level + indent_add);
  2356. cursor_position = 0;
  2357. }
  2358. }
  2359. static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
  2360. struct btrfsic_state *state,
  2361. struct btrfsic_block_data_ctx *next_block_ctx,
  2362. struct btrfsic_block *next_block,
  2363. struct btrfsic_block *from_block,
  2364. u64 parent_generation)
  2365. {
  2366. struct btrfsic_block_link *l;
  2367. l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
  2368. next_block_ctx->dev_bytenr,
  2369. from_block->dev_state->bdev,
  2370. from_block->dev_bytenr,
  2371. &state->block_link_hashtable);
  2372. if (NULL == l) {
  2373. l = btrfsic_block_link_alloc();
  2374. if (NULL == l) {
  2375. pr_info("btrfsic: error, kmalloc failed!\n");
  2376. return NULL;
  2377. }
  2378. l->block_ref_to = next_block;
  2379. l->block_ref_from = from_block;
  2380. l->ref_cnt = 1;
  2381. l->parent_generation = parent_generation;
  2382. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2383. btrfsic_print_add_link(state, l);
  2384. list_add(&l->node_ref_to, &from_block->ref_to_list);
  2385. list_add(&l->node_ref_from, &next_block->ref_from_list);
  2386. btrfsic_block_link_hashtable_add(l,
  2387. &state->block_link_hashtable);
  2388. } else {
  2389. l->ref_cnt++;
  2390. l->parent_generation = parent_generation;
  2391. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2392. btrfsic_print_add_link(state, l);
  2393. }
  2394. return l;
  2395. }
  2396. static struct btrfsic_block *btrfsic_block_lookup_or_add(
  2397. struct btrfsic_state *state,
  2398. struct btrfsic_block_data_ctx *block_ctx,
  2399. const char *additional_string,
  2400. int is_metadata,
  2401. int is_iodone,
  2402. int never_written,
  2403. int mirror_num,
  2404. int *was_created)
  2405. {
  2406. struct btrfsic_block *block;
  2407. block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
  2408. block_ctx->dev_bytenr,
  2409. &state->block_hashtable);
  2410. if (NULL == block) {
  2411. struct btrfsic_dev_state *dev_state;
  2412. block = btrfsic_block_alloc();
  2413. if (NULL == block) {
  2414. pr_info("btrfsic: error, kmalloc failed!\n");
  2415. return NULL;
  2416. }
  2417. dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
  2418. if (NULL == dev_state) {
  2419. pr_info("btrfsic: error, lookup dev_state failed!\n");
  2420. btrfsic_block_free(block);
  2421. return NULL;
  2422. }
  2423. block->dev_state = dev_state;
  2424. block->dev_bytenr = block_ctx->dev_bytenr;
  2425. block->logical_bytenr = block_ctx->start;
  2426. block->is_metadata = is_metadata;
  2427. block->is_iodone = is_iodone;
  2428. block->never_written = never_written;
  2429. block->mirror_num = mirror_num;
  2430. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2431. pr_info("New %s%c-block @%llu (%s/%llu/%d)\n",
  2432. additional_string,
  2433. btrfsic_get_block_type(state, block),
  2434. block->logical_bytenr, dev_state->name,
  2435. block->dev_bytenr, mirror_num);
  2436. list_add(&block->all_blocks_node, &state->all_blocks_list);
  2437. btrfsic_block_hashtable_add(block, &state->block_hashtable);
  2438. if (NULL != was_created)
  2439. *was_created = 1;
  2440. } else {
  2441. if (NULL != was_created)
  2442. *was_created = 0;
  2443. }
  2444. return block;
  2445. }
  2446. static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
  2447. u64 bytenr,
  2448. struct btrfsic_dev_state *dev_state,
  2449. u64 dev_bytenr)
  2450. {
  2451. struct btrfs_fs_info *fs_info = state->fs_info;
  2452. struct btrfsic_block_data_ctx block_ctx;
  2453. int num_copies;
  2454. int mirror_num;
  2455. int match = 0;
  2456. int ret;
  2457. num_copies = btrfs_num_copies(fs_info, bytenr, state->metablock_size);
  2458. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2459. ret = btrfsic_map_block(state, bytenr, state->metablock_size,
  2460. &block_ctx, mirror_num);
  2461. if (ret) {
  2462. pr_info("btrfsic: btrfsic_map_block(logical @%llu, mirror %d) failed!\n",
  2463. bytenr, mirror_num);
  2464. continue;
  2465. }
  2466. if (dev_state->bdev == block_ctx.dev->bdev &&
  2467. dev_bytenr == block_ctx.dev_bytenr) {
  2468. match++;
  2469. btrfsic_release_block_ctx(&block_ctx);
  2470. break;
  2471. }
  2472. btrfsic_release_block_ctx(&block_ctx);
  2473. }
  2474. if (WARN_ON(!match)) {
  2475. 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",
  2476. bytenr, dev_state->name, dev_bytenr);
  2477. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2478. ret = btrfsic_map_block(state, bytenr,
  2479. state->metablock_size,
  2480. &block_ctx, mirror_num);
  2481. if (ret)
  2482. continue;
  2483. pr_info("Read logical bytenr @%llu maps to (%s/%llu/%d)\n",
  2484. bytenr, block_ctx.dev->name,
  2485. block_ctx.dev_bytenr, mirror_num);
  2486. }
  2487. }
  2488. }
  2489. static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
  2490. struct block_device *bdev)
  2491. {
  2492. return btrfsic_dev_state_hashtable_lookup(bdev,
  2493. &btrfsic_dev_state_hashtable);
  2494. }
  2495. int btrfsic_submit_bh(int op, int op_flags, struct buffer_head *bh)
  2496. {
  2497. struct btrfsic_dev_state *dev_state;
  2498. if (!btrfsic_is_initialized)
  2499. return submit_bh(op, op_flags, bh);
  2500. mutex_lock(&btrfsic_mutex);
  2501. /* since btrfsic_submit_bh() might also be called before
  2502. * btrfsic_mount(), this might return NULL */
  2503. dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
  2504. /* Only called to write the superblock (incl. FLUSH/FUA) */
  2505. if (NULL != dev_state &&
  2506. (op == REQ_OP_WRITE) && bh->b_size > 0) {
  2507. u64 dev_bytenr;
  2508. dev_bytenr = 4096 * bh->b_blocknr;
  2509. if (dev_state->state->print_mask &
  2510. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2511. pr_info("submit_bh(op=0x%x,0x%x, blocknr=%llu (bytenr %llu), size=%zu, data=%p, bdev=%p)\n",
  2512. op, op_flags, (unsigned long long)bh->b_blocknr,
  2513. dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev);
  2514. btrfsic_process_written_block(dev_state, dev_bytenr,
  2515. &bh->b_data, 1, NULL,
  2516. NULL, bh, op_flags);
  2517. } else if (NULL != dev_state && (op_flags & REQ_PREFLUSH)) {
  2518. if (dev_state->state->print_mask &
  2519. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2520. pr_info("submit_bh(op=0x%x,0x%x FLUSH, bdev=%p)\n",
  2521. op, op_flags, bh->b_bdev);
  2522. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2523. if ((dev_state->state->print_mask &
  2524. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2525. BTRFSIC_PRINT_MASK_VERBOSE)))
  2526. pr_info("btrfsic_submit_bh(%s) with FLUSH but dummy block already in use (ignored)!\n",
  2527. dev_state->name);
  2528. } else {
  2529. struct btrfsic_block *const block =
  2530. &dev_state->dummy_block_for_bio_bh_flush;
  2531. block->is_iodone = 0;
  2532. block->never_written = 0;
  2533. block->iodone_w_error = 0;
  2534. block->flush_gen = dev_state->last_flush_gen + 1;
  2535. block->submit_bio_bh_rw = op_flags;
  2536. block->orig_bio_bh_private = bh->b_private;
  2537. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  2538. block->next_in_same_bio = NULL;
  2539. bh->b_private = block;
  2540. bh->b_end_io = btrfsic_bh_end_io;
  2541. }
  2542. }
  2543. mutex_unlock(&btrfsic_mutex);
  2544. return submit_bh(op, op_flags, bh);
  2545. }
  2546. static void __btrfsic_submit_bio(struct bio *bio)
  2547. {
  2548. struct btrfsic_dev_state *dev_state;
  2549. if (!btrfsic_is_initialized)
  2550. return;
  2551. mutex_lock(&btrfsic_mutex);
  2552. /* since btrfsic_submit_bio() is also called before
  2553. * btrfsic_mount(), this might return NULL */
  2554. dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
  2555. if (NULL != dev_state &&
  2556. (bio_op(bio) == REQ_OP_WRITE) && bio_has_data(bio)) {
  2557. unsigned int i;
  2558. u64 dev_bytenr;
  2559. u64 cur_bytenr;
  2560. struct bio_vec *bvec;
  2561. int bio_is_patched;
  2562. char **mapped_datav;
  2563. dev_bytenr = 512 * bio->bi_iter.bi_sector;
  2564. bio_is_patched = 0;
  2565. if (dev_state->state->print_mask &
  2566. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2567. pr_info("submit_bio(rw=%d,0x%x, bi_vcnt=%u, bi_sector=%llu (bytenr %llu), bi_bdev=%p)\n",
  2568. bio_op(bio), bio->bi_opf, bio->bi_vcnt,
  2569. (unsigned long long)bio->bi_iter.bi_sector,
  2570. dev_bytenr, bio->bi_bdev);
  2571. mapped_datav = kmalloc_array(bio->bi_vcnt,
  2572. sizeof(*mapped_datav), GFP_NOFS);
  2573. if (!mapped_datav)
  2574. goto leave;
  2575. cur_bytenr = dev_bytenr;
  2576. bio_for_each_segment_all(bvec, bio, i) {
  2577. BUG_ON(bvec->bv_len != PAGE_SIZE);
  2578. mapped_datav[i] = kmap(bvec->bv_page);
  2579. if (dev_state->state->print_mask &
  2580. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE)
  2581. pr_info("#%u: bytenr=%llu, len=%u, offset=%u\n",
  2582. i, cur_bytenr, bvec->bv_len, bvec->bv_offset);
  2583. cur_bytenr += bvec->bv_len;
  2584. }
  2585. btrfsic_process_written_block(dev_state, dev_bytenr,
  2586. mapped_datav, bio->bi_vcnt,
  2587. bio, &bio_is_patched,
  2588. NULL, bio->bi_opf);
  2589. bio_for_each_segment_all(bvec, bio, i)
  2590. kunmap(bvec->bv_page);
  2591. kfree(mapped_datav);
  2592. } else if (NULL != dev_state && (bio->bi_opf & REQ_PREFLUSH)) {
  2593. if (dev_state->state->print_mask &
  2594. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2595. pr_info("submit_bio(rw=%d,0x%x FLUSH, bdev=%p)\n",
  2596. bio_op(bio), bio->bi_opf, bio->bi_bdev);
  2597. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2598. if ((dev_state->state->print_mask &
  2599. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2600. BTRFSIC_PRINT_MASK_VERBOSE)))
  2601. pr_info("btrfsic_submit_bio(%s) with FLUSH but dummy block already in use (ignored)!\n",
  2602. dev_state->name);
  2603. } else {
  2604. struct btrfsic_block *const block =
  2605. &dev_state->dummy_block_for_bio_bh_flush;
  2606. block->is_iodone = 0;
  2607. block->never_written = 0;
  2608. block->iodone_w_error = 0;
  2609. block->flush_gen = dev_state->last_flush_gen + 1;
  2610. block->submit_bio_bh_rw = bio->bi_opf;
  2611. block->orig_bio_bh_private = bio->bi_private;
  2612. block->orig_bio_bh_end_io.bio = bio->bi_end_io;
  2613. block->next_in_same_bio = NULL;
  2614. bio->bi_private = block;
  2615. bio->bi_end_io = btrfsic_bio_end_io;
  2616. }
  2617. }
  2618. leave:
  2619. mutex_unlock(&btrfsic_mutex);
  2620. }
  2621. void btrfsic_submit_bio(struct bio *bio)
  2622. {
  2623. __btrfsic_submit_bio(bio);
  2624. submit_bio(bio);
  2625. }
  2626. int btrfsic_submit_bio_wait(struct bio *bio)
  2627. {
  2628. __btrfsic_submit_bio(bio);
  2629. return submit_bio_wait(bio);
  2630. }
  2631. int btrfsic_mount(struct btrfs_fs_info *fs_info,
  2632. struct btrfs_fs_devices *fs_devices,
  2633. int including_extent_data, u32 print_mask)
  2634. {
  2635. int ret;
  2636. struct btrfsic_state *state;
  2637. struct list_head *dev_head = &fs_devices->devices;
  2638. struct btrfs_device *device;
  2639. if (fs_info->nodesize & ((u64)PAGE_SIZE - 1)) {
  2640. pr_info("btrfsic: cannot handle nodesize %d not being a multiple of PAGE_SIZE %ld!\n",
  2641. fs_info->nodesize, PAGE_SIZE);
  2642. return -1;
  2643. }
  2644. if (fs_info->sectorsize & ((u64)PAGE_SIZE - 1)) {
  2645. pr_info("btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_SIZE %ld!\n",
  2646. fs_info->sectorsize, PAGE_SIZE);
  2647. return -1;
  2648. }
  2649. state = kzalloc(sizeof(*state), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  2650. if (!state) {
  2651. state = vzalloc(sizeof(*state));
  2652. if (!state) {
  2653. pr_info("btrfs check-integrity: vzalloc() failed!\n");
  2654. return -1;
  2655. }
  2656. }
  2657. if (!btrfsic_is_initialized) {
  2658. mutex_init(&btrfsic_mutex);
  2659. btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
  2660. btrfsic_is_initialized = 1;
  2661. }
  2662. mutex_lock(&btrfsic_mutex);
  2663. state->fs_info = fs_info;
  2664. state->print_mask = print_mask;
  2665. state->include_extent_data = including_extent_data;
  2666. state->csum_size = 0;
  2667. state->metablock_size = fs_info->nodesize;
  2668. state->datablock_size = fs_info->sectorsize;
  2669. INIT_LIST_HEAD(&state->all_blocks_list);
  2670. btrfsic_block_hashtable_init(&state->block_hashtable);
  2671. btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
  2672. state->max_superblock_generation = 0;
  2673. state->latest_superblock = NULL;
  2674. list_for_each_entry(device, dev_head, dev_list) {
  2675. struct btrfsic_dev_state *ds;
  2676. const char *p;
  2677. if (!device->bdev || !device->name)
  2678. continue;
  2679. ds = btrfsic_dev_state_alloc();
  2680. if (NULL == ds) {
  2681. pr_info("btrfs check-integrity: kmalloc() failed!\n");
  2682. mutex_unlock(&btrfsic_mutex);
  2683. return -1;
  2684. }
  2685. ds->bdev = device->bdev;
  2686. ds->state = state;
  2687. bdevname(ds->bdev, ds->name);
  2688. ds->name[BDEVNAME_SIZE - 1] = '\0';
  2689. p = kbasename(ds->name);
  2690. strlcpy(ds->name, p, sizeof(ds->name));
  2691. btrfsic_dev_state_hashtable_add(ds,
  2692. &btrfsic_dev_state_hashtable);
  2693. }
  2694. ret = btrfsic_process_superblock(state, fs_devices);
  2695. if (0 != ret) {
  2696. mutex_unlock(&btrfsic_mutex);
  2697. btrfsic_unmount(fs_devices);
  2698. return ret;
  2699. }
  2700. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
  2701. btrfsic_dump_database(state);
  2702. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
  2703. btrfsic_dump_tree(state);
  2704. mutex_unlock(&btrfsic_mutex);
  2705. return 0;
  2706. }
  2707. void btrfsic_unmount(struct btrfs_fs_devices *fs_devices)
  2708. {
  2709. struct btrfsic_block *b_all, *tmp_all;
  2710. struct btrfsic_state *state;
  2711. struct list_head *dev_head = &fs_devices->devices;
  2712. struct btrfs_device *device;
  2713. if (!btrfsic_is_initialized)
  2714. return;
  2715. mutex_lock(&btrfsic_mutex);
  2716. state = NULL;
  2717. list_for_each_entry(device, dev_head, dev_list) {
  2718. struct btrfsic_dev_state *ds;
  2719. if (!device->bdev || !device->name)
  2720. continue;
  2721. ds = btrfsic_dev_state_hashtable_lookup(
  2722. device->bdev,
  2723. &btrfsic_dev_state_hashtable);
  2724. if (NULL != ds) {
  2725. state = ds->state;
  2726. btrfsic_dev_state_hashtable_remove(ds);
  2727. btrfsic_dev_state_free(ds);
  2728. }
  2729. }
  2730. if (NULL == state) {
  2731. pr_info("btrfsic: error, cannot find state information on umount!\n");
  2732. mutex_unlock(&btrfsic_mutex);
  2733. return;
  2734. }
  2735. /*
  2736. * Don't care about keeping the lists' state up to date,
  2737. * just free all memory that was allocated dynamically.
  2738. * Free the blocks and the block_links.
  2739. */
  2740. list_for_each_entry_safe(b_all, tmp_all, &state->all_blocks_list,
  2741. all_blocks_node) {
  2742. struct btrfsic_block_link *l, *tmp;
  2743. list_for_each_entry_safe(l, tmp, &b_all->ref_to_list,
  2744. node_ref_to) {
  2745. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2746. btrfsic_print_rem_link(state, l);
  2747. l->ref_cnt--;
  2748. if (0 == l->ref_cnt)
  2749. btrfsic_block_link_free(l);
  2750. }
  2751. if (b_all->is_iodone || b_all->never_written)
  2752. btrfsic_block_free(b_all);
  2753. else
  2754. pr_info("btrfs: attempt to free %c-block @%llu (%s/%llu/%d) on umount which is not yet iodone!\n",
  2755. btrfsic_get_block_type(state, b_all),
  2756. b_all->logical_bytenr, b_all->dev_state->name,
  2757. b_all->dev_bytenr, b_all->mirror_num);
  2758. }
  2759. mutex_unlock(&btrfsic_mutex);
  2760. kvfree(state);
  2761. }