check-integrity.c 99 KB

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