check-integrity.c 99 KB

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