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