check-integrity.c 93 KB

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