check-integrity.c 97 KB

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