check-integrity.c 94 KB

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