check-integrity.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060
  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_root *root,
  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_fs_info *fs_info = root->fs_info;
  2639. struct btrfs_device *device;
  2640. if (fs_info->nodesize & ((u64)PAGE_SIZE - 1)) {
  2641. pr_info("btrfsic: cannot handle nodesize %d not being a multiple of PAGE_SIZE %ld!\n",
  2642. fs_info->nodesize, PAGE_SIZE);
  2643. return -1;
  2644. }
  2645. if (fs_info->sectorsize & ((u64)PAGE_SIZE - 1)) {
  2646. pr_info("btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_SIZE %ld!\n",
  2647. fs_info->sectorsize, PAGE_SIZE);
  2648. return -1;
  2649. }
  2650. state = kzalloc(sizeof(*state), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  2651. if (!state) {
  2652. state = vzalloc(sizeof(*state));
  2653. if (!state) {
  2654. pr_info("btrfs check-integrity: vzalloc() failed!\n");
  2655. return -1;
  2656. }
  2657. }
  2658. if (!btrfsic_is_initialized) {
  2659. mutex_init(&btrfsic_mutex);
  2660. btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
  2661. btrfsic_is_initialized = 1;
  2662. }
  2663. mutex_lock(&btrfsic_mutex);
  2664. state->fs_info = root->fs_info;
  2665. state->print_mask = print_mask;
  2666. state->include_extent_data = including_extent_data;
  2667. state->csum_size = 0;
  2668. state->metablock_size = fs_info->nodesize;
  2669. state->datablock_size = fs_info->sectorsize;
  2670. INIT_LIST_HEAD(&state->all_blocks_list);
  2671. btrfsic_block_hashtable_init(&state->block_hashtable);
  2672. btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
  2673. state->max_superblock_generation = 0;
  2674. state->latest_superblock = NULL;
  2675. list_for_each_entry(device, dev_head, dev_list) {
  2676. struct btrfsic_dev_state *ds;
  2677. const char *p;
  2678. if (!device->bdev || !device->name)
  2679. continue;
  2680. ds = btrfsic_dev_state_alloc();
  2681. if (NULL == ds) {
  2682. pr_info("btrfs check-integrity: kmalloc() failed!\n");
  2683. mutex_unlock(&btrfsic_mutex);
  2684. return -1;
  2685. }
  2686. ds->bdev = device->bdev;
  2687. ds->state = state;
  2688. bdevname(ds->bdev, ds->name);
  2689. ds->name[BDEVNAME_SIZE - 1] = '\0';
  2690. p = kbasename(ds->name);
  2691. strlcpy(ds->name, p, sizeof(ds->name));
  2692. btrfsic_dev_state_hashtable_add(ds,
  2693. &btrfsic_dev_state_hashtable);
  2694. }
  2695. ret = btrfsic_process_superblock(state, fs_devices);
  2696. if (0 != ret) {
  2697. mutex_unlock(&btrfsic_mutex);
  2698. btrfsic_unmount(root, fs_devices);
  2699. return ret;
  2700. }
  2701. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
  2702. btrfsic_dump_database(state);
  2703. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
  2704. btrfsic_dump_tree(state);
  2705. mutex_unlock(&btrfsic_mutex);
  2706. return 0;
  2707. }
  2708. void btrfsic_unmount(struct btrfs_root *root,
  2709. struct btrfs_fs_devices *fs_devices)
  2710. {
  2711. struct btrfsic_block *b_all, *tmp_all;
  2712. struct btrfsic_state *state;
  2713. struct list_head *dev_head = &fs_devices->devices;
  2714. struct btrfs_device *device;
  2715. if (!btrfsic_is_initialized)
  2716. return;
  2717. mutex_lock(&btrfsic_mutex);
  2718. state = NULL;
  2719. list_for_each_entry(device, dev_head, dev_list) {
  2720. struct btrfsic_dev_state *ds;
  2721. if (!device->bdev || !device->name)
  2722. continue;
  2723. ds = btrfsic_dev_state_hashtable_lookup(
  2724. device->bdev,
  2725. &btrfsic_dev_state_hashtable);
  2726. if (NULL != ds) {
  2727. state = ds->state;
  2728. btrfsic_dev_state_hashtable_remove(ds);
  2729. btrfsic_dev_state_free(ds);
  2730. }
  2731. }
  2732. if (NULL == state) {
  2733. pr_info("btrfsic: error, cannot find state information on umount!\n");
  2734. mutex_unlock(&btrfsic_mutex);
  2735. return;
  2736. }
  2737. /*
  2738. * Don't care about keeping the lists' state up to date,
  2739. * just free all memory that was allocated dynamically.
  2740. * Free the blocks and the block_links.
  2741. */
  2742. list_for_each_entry_safe(b_all, tmp_all, &state->all_blocks_list,
  2743. all_blocks_node) {
  2744. struct btrfsic_block_link *l, *tmp;
  2745. list_for_each_entry_safe(l, tmp, &b_all->ref_to_list,
  2746. node_ref_to) {
  2747. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2748. btrfsic_print_rem_link(state, l);
  2749. l->ref_cnt--;
  2750. if (0 == l->ref_cnt)
  2751. btrfsic_block_link_free(l);
  2752. }
  2753. if (b_all->is_iodone || b_all->never_written)
  2754. btrfsic_block_free(b_all);
  2755. else
  2756. pr_info("btrfs: attempt to free %c-block @%llu (%s/%llu/%d) on umount which is not yet iodone!\n",
  2757. btrfsic_get_block_type(state, b_all),
  2758. b_all->logical_bytenr, b_all->dev_state->name,
  2759. b_all->dev_bytenr, b_all->mirror_num);
  2760. }
  2761. mutex_unlock(&btrfsic_mutex);
  2762. kvfree(state);
  2763. }